1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 
29 #include <linux/pci.h>
30 #include <linux/pm_runtime.h>
31 
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_device.h>
34 #include <drm/drm_irq.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/drm_vblank.h>
37 #include <drm/radeon_drm.h>
38 
39 #include "atom.h"
40 #include "radeon.h"
41 #include "radeon_kms.h"
42 #include "radeon_reg.h"
43 
44 
45 #define RADEON_WAIT_IDLE_TIMEOUT 200
46 
47 /*
48  * radeon_driver_irq_handler_kms - irq handler for KMS
49  *
50  * This is the irq handler for the radeon KMS driver (all asics).
51  * radeon_irq_process is a macro that points to the per-asic
52  * irq handler callback.
53  */
54 irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
55 {
56 	struct drm_device *dev = (struct drm_device *) arg;
57 	struct radeon_device *rdev = dev->dev_private;
58 	irqreturn_t ret;
59 
60 	ret = radeon_irq_process(rdev);
61 	if (ret == IRQ_HANDLED)
62 		pm_runtime_mark_last_busy(dev->dev);
63 	return ret;
64 }
65 
66 /*
67  * Handle hotplug events outside the interrupt handler proper.
68  */
69 /**
70  * radeon_hotplug_work_func - display hotplug work handler
71  *
72  * @work: work struct
73  *
74  * This is the hot plug event work handler (all asics).
75  * The work gets scheduled from the irq handler if there
76  * was a hot plug interrupt.  It walks the connector table
77  * and calls the hotplug handler for each one, then sends
78  * a drm hotplug event to alert userspace.
79  */
80 static void radeon_hotplug_work_func(struct work_struct *work)
81 {
82 	struct radeon_device *rdev = container_of(work, struct radeon_device,
83 						  hotplug_work.work);
84 	struct drm_device *dev = rdev->ddev;
85 	struct drm_mode_config *mode_config = &dev->mode_config;
86 	struct drm_connector *connector;
87 
88 	/* we can race here at startup, some boards seem to trigger
89 	 * hotplug irqs when they shouldn't. */
90 	if (!rdev->mode_info.mode_config_initialized)
91 		return;
92 
93 	mutex_lock(&mode_config->mutex);
94 	list_for_each_entry(connector, &mode_config->connector_list, head)
95 		radeon_connector_hotplug(connector);
96 	mutex_unlock(&mode_config->mutex);
97 	/* Just fire off a uevent and let userspace tell us what to do */
98 	drm_helper_hpd_irq_event(dev);
99 }
100 
101 static void radeon_dp_work_func(struct work_struct *work)
102 {
103 	struct radeon_device *rdev = container_of(work, struct radeon_device,
104 						  dp_work);
105 	struct drm_device *dev = rdev->ddev;
106 	struct drm_mode_config *mode_config = &dev->mode_config;
107 	struct drm_connector *connector;
108 
109 	/* this should take a mutex */
110 	list_for_each_entry(connector, &mode_config->connector_list, head)
111 		radeon_connector_hotplug(connector);
112 }
113 /**
114  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
115  *
116  * @dev: drm dev pointer
117  *
118  * Gets the hw ready to enable irqs (all asics).
119  * This function disables all interrupt sources on the GPU.
120  */
121 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
122 {
123 	struct radeon_device *rdev = dev->dev_private;
124 	unsigned long irqflags;
125 	unsigned i;
126 
127 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
128 	/* Disable *all* interrupts */
129 	for (i = 0; i < RADEON_NUM_RINGS; i++)
130 		atomic_set(&rdev->irq.ring_int[i], 0);
131 	rdev->irq.dpm_thermal = false;
132 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
133 		rdev->irq.hpd[i] = false;
134 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
135 		rdev->irq.crtc_vblank_int[i] = false;
136 		atomic_set(&rdev->irq.pflip[i], 0);
137 		rdev->irq.afmt[i] = false;
138 	}
139 	radeon_irq_set(rdev);
140 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
141 	/* Clear bits */
142 	radeon_irq_process(rdev);
143 }
144 
145 /**
146  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
147  *
148  * @dev: drm dev pointer
149  *
150  * Handles stuff to be done after enabling irqs (all asics).
151  * Returns 0 on success.
152  */
153 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
154 {
155 	struct radeon_device *rdev = dev->dev_private;
156 
157 	if (ASIC_IS_AVIVO(rdev))
158 		dev->max_vblank_count = 0x00ffffff;
159 	else
160 		dev->max_vblank_count = 0x001fffff;
161 
162 	return 0;
163 }
164 
165 /**
166  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
167  *
168  * @dev: drm dev pointer
169  *
170  * This function disables all interrupt sources on the GPU (all asics).
171  */
172 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
173 {
174 	struct radeon_device *rdev = dev->dev_private;
175 	unsigned long irqflags;
176 	unsigned i;
177 
178 	if (rdev == NULL) {
179 		return;
180 	}
181 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
182 	/* Disable *all* interrupts */
183 	for (i = 0; i < RADEON_NUM_RINGS; i++)
184 		atomic_set(&rdev->irq.ring_int[i], 0);
185 	rdev->irq.dpm_thermal = false;
186 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
187 		rdev->irq.hpd[i] = false;
188 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
189 		rdev->irq.crtc_vblank_int[i] = false;
190 		atomic_set(&rdev->irq.pflip[i], 0);
191 		rdev->irq.afmt[i] = false;
192 	}
193 	radeon_irq_set(rdev);
194 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
195 }
196 
197 /**
198  * radeon_msi_ok - asic specific msi checks
199  *
200  * @rdev: radeon device pointer
201  *
202  * Handles asic specific MSI checks to determine if
203  * MSIs should be enabled on a particular chip (all asics).
204  * Returns true if MSIs should be enabled, false if MSIs
205  * should not be enabled.
206  */
207 static bool radeon_msi_ok(struct radeon_device *rdev)
208 {
209 	/* RV370/RV380 was first asic with MSI support */
210 	if (rdev->family < CHIP_RV380)
211 		return false;
212 
213 	/* MSIs don't work on AGP */
214 	if (rdev->flags & RADEON_IS_AGP)
215 		return false;
216 
217 	/*
218 	 * Older chips have a HW limitation, they can only generate 40 bits
219 	 * of address for "64-bit" MSIs which breaks on some platforms, notably
220 	 * IBM POWER servers, so we limit them
221 	 */
222 	if (rdev->family < CHIP_BONAIRE) {
223 		dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
224 		rdev->pdev->no_64bit_msi = 1;
225 	}
226 
227 	/* force MSI on */
228 	if (radeon_msi == 1)
229 		return true;
230 	else if (radeon_msi == 0)
231 		return false;
232 
233 	/* Quirks */
234 	/* HP RS690 only seems to work with MSIs. */
235 	if ((rdev->pdev->device == 0x791f) &&
236 	    (rdev->pdev->subsystem_vendor == 0x103c) &&
237 	    (rdev->pdev->subsystem_device == 0x30c2))
238 		return true;
239 
240 	/* Dell RS690 only seems to work with MSIs. */
241 	if ((rdev->pdev->device == 0x791f) &&
242 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
243 	    (rdev->pdev->subsystem_device == 0x01fc))
244 		return true;
245 
246 	/* Dell RS690 only seems to work with MSIs. */
247 	if ((rdev->pdev->device == 0x791f) &&
248 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
249 	    (rdev->pdev->subsystem_device == 0x01fd))
250 		return true;
251 
252 	/* Gateway RS690 only seems to work with MSIs. */
253 	if ((rdev->pdev->device == 0x791f) &&
254 	    (rdev->pdev->subsystem_vendor == 0x107b) &&
255 	    (rdev->pdev->subsystem_device == 0x0185))
256 		return true;
257 
258 	/* try and enable MSIs by default on all RS690s */
259 	if (rdev->family == CHIP_RS690)
260 		return true;
261 
262 	/* RV515 seems to have MSI issues where it loses
263 	 * MSI rearms occasionally. This leads to lockups and freezes.
264 	 * disable it by default.
265 	 */
266 	if (rdev->family == CHIP_RV515)
267 		return false;
268 	if (rdev->flags & RADEON_IS_IGP) {
269 		/* APUs work fine with MSIs */
270 		if (rdev->family >= CHIP_PALM)
271 			return true;
272 		/* lots of IGPs have problems with MSIs */
273 		return false;
274 	}
275 
276 	return true;
277 }
278 
279 /**
280  * radeon_irq_kms_init - init driver interrupt info
281  *
282  * @rdev: radeon device pointer
283  *
284  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
285  * Returns 0 for success, error for failure.
286  */
287 int radeon_irq_kms_init(struct radeon_device *rdev)
288 {
289 	int r = 0;
290 
291 	spin_lock_init(&rdev->irq.lock);
292 
293 	/* Disable vblank irqs aggressively for power-saving */
294 	rdev->ddev->vblank_disable_immediate = true;
295 
296 	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
297 	if (r) {
298 		return r;
299 	}
300 
301 	/* enable msi */
302 	rdev->msi_enabled = 0;
303 
304 	if (radeon_msi_ok(rdev)) {
305 		int ret = pci_enable_msi(rdev->pdev);
306 		if (!ret) {
307 			rdev->msi_enabled = 1;
308 			dev_info(rdev->dev, "radeon: using MSI.\n");
309 		}
310 	}
311 
312 	INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
313 	INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
314 	INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
315 
316 	rdev->irq.installed = true;
317 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
318 	if (r) {
319 		rdev->irq.installed = false;
320 		flush_delayed_work(&rdev->hotplug_work);
321 		return r;
322 	}
323 
324 	DRM_INFO("radeon: irq initialized.\n");
325 	return 0;
326 }
327 
328 /**
329  * radeon_irq_kms_fini - tear down driver interrupt info
330  *
331  * @rdev: radeon device pointer
332  *
333  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
334  */
335 void radeon_irq_kms_fini(struct radeon_device *rdev)
336 {
337 	if (rdev->irq.installed) {
338 		drm_irq_uninstall(rdev->ddev);
339 		rdev->irq.installed = false;
340 		if (rdev->msi_enabled)
341 			pci_disable_msi(rdev->pdev);
342 		flush_delayed_work(&rdev->hotplug_work);
343 	}
344 }
345 
346 /**
347  * radeon_irq_kms_sw_irq_get - enable software interrupt
348  *
349  * @rdev: radeon device pointer
350  * @ring: ring whose interrupt you want to enable
351  *
352  * Enables the software interrupt for a specific ring (all asics).
353  * The software interrupt is generally used to signal a fence on
354  * a particular ring.
355  */
356 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
357 {
358 	unsigned long irqflags;
359 
360 	if (!rdev->ddev->irq_enabled)
361 		return;
362 
363 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
364 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
365 		radeon_irq_set(rdev);
366 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
367 	}
368 }
369 
370 /**
371  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
372  *
373  * @rdev: radeon device pointer
374  * @ring: ring whose interrupt you want to enable
375  *
376  * Enables the software interrupt for a specific ring (all asics).
377  * The software interrupt is generally used to signal a fence on
378  * a particular ring.
379  */
380 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
381 {
382 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
383 }
384 
385 /**
386  * radeon_irq_kms_sw_irq_put - disable software interrupt
387  *
388  * @rdev: radeon device pointer
389  * @ring: ring whose interrupt you want to disable
390  *
391  * Disables the software interrupt for a specific ring (all asics).
392  * The software interrupt is generally used to signal a fence on
393  * a particular ring.
394  */
395 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
396 {
397 	unsigned long irqflags;
398 
399 	if (!rdev->ddev->irq_enabled)
400 		return;
401 
402 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
403 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
404 		radeon_irq_set(rdev);
405 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
406 	}
407 }
408 
409 /**
410  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
411  *
412  * @rdev: radeon device pointer
413  * @crtc: crtc whose interrupt you want to enable
414  *
415  * Enables the pageflip interrupt for a specific crtc (all asics).
416  * For pageflips we use the vblank interrupt source.
417  */
418 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
419 {
420 	unsigned long irqflags;
421 
422 	if (crtc < 0 || crtc >= rdev->num_crtc)
423 		return;
424 
425 	if (!rdev->ddev->irq_enabled)
426 		return;
427 
428 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
429 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
430 		radeon_irq_set(rdev);
431 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
432 	}
433 }
434 
435 /**
436  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
437  *
438  * @rdev: radeon device pointer
439  * @crtc: crtc whose interrupt you want to disable
440  *
441  * Disables the pageflip interrupt for a specific crtc (all asics).
442  * For pageflips we use the vblank interrupt source.
443  */
444 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
445 {
446 	unsigned long irqflags;
447 
448 	if (crtc < 0 || crtc >= rdev->num_crtc)
449 		return;
450 
451 	if (!rdev->ddev->irq_enabled)
452 		return;
453 
454 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
455 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
456 		radeon_irq_set(rdev);
457 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
458 	}
459 }
460 
461 /**
462  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
463  *
464  * @rdev: radeon device pointer
465  * @block: afmt block whose interrupt you want to enable
466  *
467  * Enables the afmt change interrupt for a specific afmt block (all asics).
468  */
469 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
470 {
471 	unsigned long irqflags;
472 
473 	if (!rdev->ddev->irq_enabled)
474 		return;
475 
476 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
477 	rdev->irq.afmt[block] = true;
478 	radeon_irq_set(rdev);
479 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
480 
481 }
482 
483 /**
484  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
485  *
486  * @rdev: radeon device pointer
487  * @block: afmt block whose interrupt you want to disable
488  *
489  * Disables the afmt change interrupt for a specific afmt block (all asics).
490  */
491 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
492 {
493 	unsigned long irqflags;
494 
495 	if (!rdev->ddev->irq_enabled)
496 		return;
497 
498 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
499 	rdev->irq.afmt[block] = false;
500 	radeon_irq_set(rdev);
501 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
502 }
503 
504 /**
505  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
506  *
507  * @rdev: radeon device pointer
508  * @hpd_mask: mask of hpd pins you want to enable.
509  *
510  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
511  */
512 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
513 {
514 	unsigned long irqflags;
515 	int i;
516 
517 	if (!rdev->ddev->irq_enabled)
518 		return;
519 
520 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
521 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
522 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
523 	radeon_irq_set(rdev);
524 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
525 }
526 
527 /**
528  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
529  *
530  * @rdev: radeon device pointer
531  * @hpd_mask: mask of hpd pins you want to disable.
532  *
533  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
534  */
535 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
536 {
537 	unsigned long irqflags;
538 	int i;
539 
540 	if (!rdev->ddev->irq_enabled)
541 		return;
542 
543 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
544 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
545 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
546 	radeon_irq_set(rdev);
547 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
548 }
549 
550 /**
551  * radeon_irq_kms_set_irq_n_enabled - helper for updating interrupt enable registers
552  *
553  * @rdev: radeon device pointer
554  * @reg: the register to write to enable/disable interrupts
555  * @mask: the mask that enables the interrupts
556  * @enable: whether to enable or disable the interrupt register
557  * @name: the name of the interrupt register to print to the kernel log
558  * @n: the number of the interrupt register to print to the kernel log
559  *
560  * Helper for updating the enable state of interrupt registers. Checks whether
561  * or not the interrupt matches the enable state we want. If it doesn't, then
562  * we update it and print a debugging message to the kernel log indicating the
563  * new state of the interrupt register.
564  *
565  * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
566  */
567 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
568 				      u32 reg, u32 mask,
569 				      bool enable, const char *name, unsigned n)
570 {
571 	u32 tmp = RREG32(reg);
572 
573 	/* Interrupt state didn't change */
574 	if (!!(tmp & mask) == enable)
575 		return;
576 
577 	if (enable) {
578 		DRM_DEBUG("%s%d interrupts enabled\n", name, n);
579 		WREG32(reg, tmp |= mask);
580 	} else {
581 		DRM_DEBUG("%s%d interrupts disabled\n", name, n);
582 		WREG32(reg, tmp & ~mask);
583 	}
584 }
585