xref: /dragonfly/sys/dev/drm/radeon/radeon_irq_kms.c (revision 2b7dbe20)
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 #include <drm/drmP.h>
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/radeon_drm.h>
31 #include "radeon_reg.h"
32 #include "radeon_irq_kms.h"
33 #include "radeon.h"
34 #include "atom.h"
35 
36 #include <linux/pm_runtime.h>
37 
38 #define RADEON_WAIT_IDLE_TIMEOUT 200
39 
40 /**
41  * radeon_driver_irq_handler_kms - irq handler for KMS
42  *
43  * @int irq, void *arg: args
44  *
45  * This is the irq handler for the radeon KMS driver (all asics).
46  * radeon_irq_process is a macro that points to the per-asic
47  * irq handler callback.
48  */
49 irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
50 {
51 	struct drm_device *dev = (struct drm_device *) arg;
52 	struct radeon_device *rdev = dev->dev_private;
53 	irqreturn_t ret;
54 
55 	ret = radeon_irq_process(rdev);
56 #ifdef PM_TODO
57 	if (ret == IRQ_HANDLED)
58 		pm_runtime_mark_last_busy(dev->dev);
59 #endif
60 	return ret;
61 }
62 
63 /*
64  * Handle hotplug events outside the interrupt handler proper.
65  */
66 /**
67  * radeon_hotplug_work_func - display hotplug work handler
68  *
69  * @work: work struct
70  *
71  * This is the hot plug event work handler (all asics).
72  * The work gets scheduled from the irq handler if there
73  * was a hot plug interrupt.  It walks the connector table
74  * and calls the hotplug handler for each one, then sends
75  * a drm hotplug event to alert userspace.
76  */
77 static void radeon_hotplug_work_func(struct work_struct *work)
78 {
79 	struct radeon_device *rdev = container_of(work, struct radeon_device,
80 						  hotplug_work.work);
81 	struct drm_device *dev = rdev->ddev;
82 	struct drm_mode_config *mode_config = &dev->mode_config;
83 	struct drm_connector *connector;
84 
85 	/* we can race here at startup, some boards seem to trigger
86 	 * hotplug irqs when they shouldn't. */
87 	if (!rdev->mode_info.mode_config_initialized)
88 		return;
89 
90 	mutex_lock(&mode_config->mutex);
91 	list_for_each_entry(connector, &mode_config->connector_list, head)
92 		radeon_connector_hotplug(connector);
93 	mutex_unlock(&mode_config->mutex);
94 	/* Just fire off a uevent and let userspace tell us what to do */
95 	drm_helper_hpd_irq_event(dev);
96 }
97 
98 static void radeon_dp_work_func(struct work_struct *work)
99 {
100 	struct radeon_device *rdev = container_of(work, struct radeon_device,
101 						  dp_work);
102 	struct drm_device *dev = rdev->ddev;
103 	struct drm_mode_config *mode_config = &dev->mode_config;
104 	struct drm_connector *connector;
105 
106 	/* this should take a mutex */
107 	list_for_each_entry(connector, &mode_config->connector_list, head)
108 		radeon_connector_hotplug(connector);
109 }
110 /**
111  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
112  *
113  * @dev: drm dev pointer
114  *
115  * Gets the hw ready to enable irqs (all asics).
116  * This function disables all interrupt sources on the GPU.
117  */
118 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
119 {
120 	struct radeon_device *rdev = dev->dev_private;
121 	unsigned long irqflags;
122 	unsigned i;
123 
124 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
125 	/* Disable *all* interrupts */
126 	for (i = 0; i < RADEON_NUM_RINGS; i++)
127 		atomic_set(&rdev->irq.ring_int[i], 0);
128 	rdev->irq.dpm_thermal = false;
129 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
130 		rdev->irq.hpd[i] = false;
131 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
132 		rdev->irq.crtc_vblank_int[i] = false;
133 		atomic_set(&rdev->irq.pflip[i], 0);
134 		rdev->irq.afmt[i] = false;
135 	}
136 	radeon_irq_set(rdev);
137 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
138 	/* Clear bits */
139 	radeon_irq_process(rdev);
140 }
141 
142 /**
143  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
144  *
145  * @dev: drm dev pointer
146  *
147  * Handles stuff to be done after enabling irqs (all asics).
148  * Returns 0 on success.
149  */
150 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
151 {
152 	struct radeon_device *rdev = dev->dev_private;
153 
154 	if (ASIC_IS_AVIVO(rdev))
155 		dev->max_vblank_count = 0x00ffffff;
156 	else
157 		dev->max_vblank_count = 0x001fffff;
158 
159 	return 0;
160 }
161 
162 /**
163  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
164  *
165  * @dev: drm dev pointer
166  *
167  * This function disables all interrupt sources on the GPU (all asics).
168  */
169 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
170 {
171 	struct radeon_device *rdev = dev->dev_private;
172 	unsigned long irqflags;
173 	unsigned i;
174 
175 	if (rdev == NULL) {
176 		return;
177 	}
178 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
179 	/* Disable *all* interrupts */
180 	for (i = 0; i < RADEON_NUM_RINGS; i++)
181 		atomic_set(&rdev->irq.ring_int[i], 0);
182 	rdev->irq.dpm_thermal = false;
183 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
184 		rdev->irq.hpd[i] = false;
185 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
186 		rdev->irq.crtc_vblank_int[i] = false;
187 		atomic_set(&rdev->irq.pflip[i], 0);
188 		rdev->irq.afmt[i] = false;
189 	}
190 	radeon_irq_set(rdev);
191 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
192 }
193 
194 #if 0
195 /**
196  * radeon_msi_ok - asic specific msi checks
197  *
198  * @rdev: radeon device pointer
199  *
200  * Handles asic specific MSI checks to determine if
201  * MSIs should be enabled on a particular chip (all asics).
202  * Returns true if MSIs should be enabled, false if MSIs
203  * should not be enabled.
204  */
205 static bool radeon_msi_ok(struct radeon_device *rdev)
206 {
207 	/* RV370/RV380 was first asic with MSI support */
208 	if (rdev->family < CHIP_RV380)
209 		return false;
210 
211 	/* MSIs don't work on AGP */
212 	if (drm_pci_device_is_agp(rdev))
213 		return false;
214 
215 	/*
216 	 * Older chips have a HW limitation, they can only generate 40 bits
217 	 * of address for "64-bit" MSIs which breaks on some platforms, notably
218 	 * IBM POWER servers, so we limit them
219 	 */
220 	if (rdev->family < CHIP_BONAIRE) {
221 		dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
222 		rdev->pdev->no_64bit_msi = 1;
223 	}
224 
225 	/* force MSI on */
226 	if (radeon_msi == 1)
227 		return true;
228 	else if (radeon_msi == 0)
229 		return false;
230 
231 	/* Quirks */
232 	/* HP RS690 only seems to work with MSIs. */
233 	if ((rdev->pdev->device == 0x791f) &&
234 	    (rdev->pdev->subsystem_vendor == 0x103c) &&
235 	    (rdev->pdev->subsystem_device == 0x30c2))
236 		return true;
237 
238 	/* Dell RS690 only seems to work with MSIs. */
239 	if ((rdev->pdev->device == 0x791f) &&
240 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
241 	    (rdev->pdev->subsystem_device == 0x01fc))
242 		return true;
243 
244 	/* Dell RS690 only seems to work with MSIs. */
245 	if ((rdev->pdev->device == 0x791f) &&
246 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
247 	    (rdev->pdev->subsystem_device == 0x01fd))
248 		return true;
249 
250 	/* Gateway RS690 only seems to work with MSIs. */
251 	if ((rdev->pdev->device == 0x791f) &&
252 	    (rdev->pdev->subsystem_vendor == 0x107b) &&
253 	    (rdev->pdev->subsystem_device == 0x0185))
254 		return true;
255 
256 	/* try and enable MSIs by default on all RS690s */
257 	if (rdev->family == CHIP_RS690)
258 		return true;
259 
260 	/* RV515 seems to have MSI issues where it loses
261 	 * MSI rearms occasionally. This leads to lockups and freezes.
262 	 * disable it by default.
263 	 */
264 	if (rdev->family == CHIP_RV515)
265 		return false;
266 	if (rdev->flags & RADEON_IS_IGP) {
267 		/* APUs work fine with MSIs */
268 		if (rdev->family >= CHIP_PALM)
269 			return true;
270 		/* lots of IGPs have problems with MSIs */
271 		return false;
272 	}
273 
274 	return true;
275 }
276 #endif
277 
278 /**
279  * radeon_irq_kms_init - init driver interrupt info
280  *
281  * @rdev: radeon device pointer
282  *
283  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
284  * Returns 0 for success, error for failure.
285  */
286 int radeon_irq_kms_init(struct radeon_device *rdev)
287 {
288 	int r = 0;
289 
290 	lockinit(&rdev->irq.lock, "drdil", 0, 0);
291 	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
292 	if (r) {
293 		return r;
294 	}
295 
296 	/* enable msi */
297 	rdev->msi_enabled = (rdev->ddev->pdev->_irq_type == PCI_INTR_TYPE_MSI);
298 
299 #ifndef __DragonFly__
300 	if (radeon_msi_ok(rdev)) {
301 		int ret = pci_enable_msi(rdev->pdev);
302 		if (!ret) {
303 			rdev->msi_enabled = 1;
304 			dev_info(rdev->dev, "radeon: using MSI.\n");
305 		}
306 	}
307 #endif
308 
309 	INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
310 	INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
311 	INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
312 
313 	rdev->irq.installed = true;
314 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
315 	if (r) {
316 		rdev->irq.installed = false;
317 		flush_delayed_work(&rdev->hotplug_work);
318 		return r;
319 	}
320 
321 	DRM_INFO("radeon: irq initialized.\n");
322 	return 0;
323 }
324 
325 /**
326  * radeon_irq_kms_fini - tear down driver interrupt info
327  *
328  * @rdev: radeon device pointer
329  *
330  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
331  */
332 void radeon_irq_kms_fini(struct radeon_device *rdev)
333 {
334 	drm_vblank_cleanup(rdev->ddev);
335 	if (rdev->irq.installed) {
336 		drm_irq_uninstall(rdev->ddev);
337 		rdev->irq.installed = false;
338 #ifndef __DragonFly__
339 		if (rdev->msi_enabled)
340 			pci_disable_msi(rdev->pdev);
341 #endif
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