xref: /dragonfly/sys/dev/drm/radeon/radeon_irq_kms.c (revision 56380a7f)
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 #ifdef PM_TODO
37 #include <linux/pm_runtime.h>
38 #endif
39 
40 #define RADEON_WAIT_IDLE_TIMEOUT 200
41 
42 /**
43  * radeon_driver_irq_handler_kms - irq handler for KMS
44  *
45  * @void *arg: args
46  *
47  * This is the irq handler for the radeon KMS driver (all asics).
48  * radeon_irq_process is a macro that points to the per-asic
49  * irq handler callback.
50  */
51 irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
52 {
53 	struct drm_device *dev = (struct drm_device *) arg;
54 	struct radeon_device *rdev = dev->dev_private;
55 	irqreturn_t ret;
56 
57 	ret = radeon_irq_process(rdev);
58 #ifdef PM_TODO
59 	if (ret == IRQ_HANDLED)
60 		pm_runtime_mark_last_busy(dev->dev);
61 #endif
62 	return ret;
63 }
64 
65 /*
66  * Handle hotplug events outside the interrupt handler proper.
67  */
68 /**
69  * radeon_hotplug_work_func - display hotplug work handler
70  *
71  * @work: work struct
72  *
73  * This is the hot plug event work handler (all asics).
74  * The work gets scheduled from the irq handler if there
75  * was a hot plug interrupt.  It walks the connector table
76  * and calls the hotplug handler for each one, then sends
77  * a drm hotplug event to alert userspace.
78  */
79 static void radeon_hotplug_work_func(struct work_struct *work)
80 {
81 	struct radeon_device *rdev = container_of(work, struct radeon_device,
82 						  hotplug_work);
83 	struct drm_device *dev = rdev->ddev;
84 	struct drm_mode_config *mode_config = &dev->mode_config;
85 	struct drm_connector *connector;
86 
87 	if (mode_config->num_connector) {
88 		list_for_each_entry(connector, &mode_config->connector_list, head)
89 			radeon_connector_hotplug(connector);
90 	}
91 	/* Just fire off a uevent and let userspace tell us what to do */
92 	drm_helper_hpd_irq_event(dev);
93 }
94 
95 /**
96  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
97  *
98  * @dev: drm dev pointer
99  *
100  * Gets the hw ready to enable irqs (all asics).
101  * This function disables all interrupt sources on the GPU.
102  */
103 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
104 {
105 	struct radeon_device *rdev = dev->dev_private;
106 	unsigned i;
107 
108 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
109 	/* Disable *all* interrupts */
110 	for (i = 0; i < RADEON_NUM_RINGS; i++)
111 		atomic_set(&rdev->irq.ring_int[i], 0);
112 	rdev->irq.dpm_thermal = false;
113 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
114 		rdev->irq.hpd[i] = false;
115 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
116 		rdev->irq.crtc_vblank_int[i] = false;
117 		atomic_set(&rdev->irq.pflip[i], 0);
118 		rdev->irq.afmt[i] = false;
119 	}
120 	radeon_irq_set(rdev);
121 	lockmgr(&rdev->irq.lock, LK_RELEASE);
122 	/* Clear bits */
123 	radeon_irq_process(rdev);
124 }
125 
126 /**
127  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
128  *
129  * @dev: drm dev pointer
130  *
131  * Handles stuff to be done after enabling irqs (all asics).
132  * Returns 0 on success.
133  */
134 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
135 {
136 	dev->max_vblank_count = 0x001fffff;
137 	return 0;
138 }
139 
140 /**
141  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
142  *
143  * @dev: drm dev pointer
144  *
145  * This function disables all interrupt sources on the GPU (all asics).
146  */
147 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
148 {
149 	struct radeon_device *rdev = dev->dev_private;
150 	unsigned i;
151 
152 	if (rdev == NULL) {
153 		return;
154 	}
155 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
156 	/* Disable *all* interrupts */
157 	for (i = 0; i < RADEON_NUM_RINGS; i++)
158 		atomic_set(&rdev->irq.ring_int[i], 0);
159 	rdev->irq.dpm_thermal = false;
160 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
161 		rdev->irq.hpd[i] = false;
162 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
163 		rdev->irq.crtc_vblank_int[i] = false;
164 		atomic_set(&rdev->irq.pflip[i], 0);
165 		rdev->irq.afmt[i] = false;
166 	}
167 	radeon_irq_set(rdev);
168 	lockmgr(&rdev->irq.lock, LK_RELEASE);
169 }
170 
171 /**
172  * radeon_msi_ok - asic specific msi checks
173  *
174  * @rdev: radeon device pointer
175  *
176  * Handles asic specific MSI checks to determine if
177  * MSIs should be enabled on a particular chip (all asics).
178  * Returns true if MSIs should be enabled, false if MSIs
179  * should not be enabled.
180  */
181 int radeon_msi_ok(struct drm_device *rdev, unsigned long flags)
182 {
183 	int family;
184 
185 	family = flags & RADEON_FAMILY_MASK;
186 
187 	/* RV370/RV380 was first asic with MSI support */
188 	if (family < CHIP_RV380)
189 		return false;
190 
191 	/* MSIs don't work on AGP */
192 	if (drm_pci_device_is_agp(rdev))
193 		return false;
194 
195 	/* force MSI on */
196 	if (radeon_msi == 1)
197 		return true;
198 	else if (radeon_msi == 0)
199 		return false;
200 
201 	/* Quirks */
202 	/* HP RS690 only seems to work with MSIs. */
203 	if ((rdev->pdev->device == 0x791f) &&
204 	    (rdev->pdev->subsystem_vendor == 0x103c) &&
205 	    (rdev->pdev->subsystem_device == 0x30c2))
206 		return true;
207 
208 	/* Dell RS690 only seems to work with MSIs. */
209 	if ((rdev->pdev->device == 0x791f) &&
210 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
211 	    (rdev->pdev->subsystem_device == 0x01fc))
212 		return true;
213 
214 	/* Dell RS690 only seems to work with MSIs. */
215 	if ((rdev->pdev->device == 0x791f) &&
216 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
217 	    (rdev->pdev->subsystem_device == 0x01fd))
218 		return true;
219 
220 	/* Gateway RS690 only seems to work with MSIs. */
221 	if ((rdev->pdev->device == 0x791f) &&
222 	    (rdev->pdev->subsystem_vendor == 0x107b) &&
223 	    (rdev->pdev->subsystem_device == 0x0185))
224 		return true;
225 
226 	/* try and enable MSIs by default on all RS690s */
227 	if (family == CHIP_RS690)
228 		return true;
229 
230 	/* RV515 seems to have MSI issues where it loses
231 	 * MSI rearms occasionally. This leads to lockups and freezes.
232 	 * disable it by default.
233 	 */
234 	if (family == CHIP_RV515)
235 		return false;
236 	if (flags & RADEON_IS_IGP) {
237 		/* APUs work fine with MSIs */
238 		if (family >= CHIP_PALM)
239 			return true;
240 		/* lots of IGPs have problems with MSIs */
241 		return false;
242 	}
243 
244 	return true;
245 }
246 
247 /**
248  * radeon_irq_kms_init - init driver interrupt info
249  *
250  * @rdev: radeon device pointer
251  *
252  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
253  * Returns 0 for success, error for failure.
254  */
255 int radeon_irq_kms_init(struct radeon_device *rdev)
256 {
257 	int r = 0;
258 
259 	lockinit(&rdev->irq.lock, "drdil", 0, LK_CANRECURSE);
260 	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
261 	if (r) {
262 		return r;
263 	}
264 
265 	/* enable msi */
266 	rdev->msi_enabled = (rdev->ddev->pdev->_irq_type == PCI_INTR_TYPE_MSI);
267 
268 #ifndef __DragonFly__
269 	if (radeon_msi_ok(rdev)) {
270 		int ret = pci_enable_msi(rdev->pdev);
271 		if (!ret) {
272 			rdev->msi_enabled = 1;
273 			dev_info(rdev->dev, "radeon: using MSI.\n");
274 		}
275 	}
276 #endif
277 
278 	INIT_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
279 	INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
280 
281 	rdev->irq.installed = true;
282 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
283 	if (r) {
284 		rdev->irq.installed = false;
285 		flush_work(&rdev->hotplug_work);
286 		return r;
287 	}
288 
289 	DRM_INFO("radeon: irq initialized.\n");
290 	return 0;
291 }
292 
293 /**
294  * radeon_irq_kms_fini - tear down driver interrupt info
295  *
296  * @rdev: radeon device pointer
297  *
298  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
299  */
300 void radeon_irq_kms_fini(struct radeon_device *rdev)
301 {
302 	drm_vblank_cleanup(rdev->ddev);
303 	if (rdev->irq.installed) {
304 		drm_irq_uninstall(rdev->ddev);
305 		rdev->irq.installed = false;
306 		flush_work(&rdev->hotplug_work);
307 	}
308 }
309 
310 /**
311  * radeon_irq_kms_sw_irq_get - enable software interrupt
312  *
313  * @rdev: radeon device pointer
314  * @ring: ring whose interrupt you want to enable
315  *
316  * Enables the software interrupt for a specific ring (all asics).
317  * The software interrupt is generally used to signal a fence on
318  * a particular ring.
319  */
320 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
321 {
322 	if (!rdev->ddev->irq_enabled)
323 		return;
324 
325 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
326 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
327 		radeon_irq_set(rdev);
328 		lockmgr(&rdev->irq.lock, LK_RELEASE);
329 	}
330 }
331 
332 /**
333  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
334  *
335  * @rdev: radeon device pointer
336  * @ring: ring whose interrupt you want to enable
337  *
338  * Enables the software interrupt for a specific ring (all asics).
339  * The software interrupt is generally used to signal a fence on
340  * a particular ring.
341  */
342 #ifdef TODO_954605c
343 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
344 {
345 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
346 }
347 #endif
348 
349 /**
350  * radeon_irq_kms_sw_irq_put - disable software interrupt
351  *
352  * @rdev: radeon device pointer
353  * @ring: ring whose interrupt you want to disable
354  *
355  * Disables the software interrupt for a specific ring (all asics).
356  * The software interrupt is generally used to signal a fence on
357  * a particular ring.
358  */
359 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
360 {
361 	if (!rdev->ddev->irq_enabled)
362 		return;
363 
364 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
365 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
366 		radeon_irq_set(rdev);
367 		lockmgr(&rdev->irq.lock, LK_RELEASE);
368 	}
369 }
370 
371 /**
372  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
373  *
374  * @rdev: radeon device pointer
375  * @crtc: crtc whose interrupt you want to enable
376  *
377  * Enables the pageflip interrupt for a specific crtc (all asics).
378  * For pageflips we use the vblank interrupt source.
379  */
380 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
381 {
382 	if (crtc < 0 || crtc >= rdev->num_crtc)
383 		return;
384 
385 	if (!rdev->ddev->irq_enabled)
386 		return;
387 
388 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
389 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
390 		radeon_irq_set(rdev);
391 		lockmgr(&rdev->irq.lock, LK_RELEASE);
392 	}
393 }
394 
395 /**
396  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
397  *
398  * @rdev: radeon device pointer
399  * @crtc: crtc whose interrupt you want to disable
400  *
401  * Disables the pageflip interrupt for a specific crtc (all asics).
402  * For pageflips we use the vblank interrupt source.
403  */
404 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
405 {
406 	if (crtc < 0 || crtc >= rdev->num_crtc)
407 		return;
408 
409 	if (!rdev->ddev->irq_enabled)
410 		return;
411 
412 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
413 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
414 		radeon_irq_set(rdev);
415 		lockmgr(&rdev->irq.lock, LK_RELEASE);
416 	}
417 }
418 
419 /**
420  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
421  *
422  * @rdev: radeon device pointer
423  * @block: afmt block whose interrupt you want to enable
424  *
425  * Enables the afmt change interrupt for a specific afmt block (all asics).
426  */
427 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
428 {
429 	if (!rdev->ddev->irq_enabled)
430 		return;
431 
432 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
433 	rdev->irq.afmt[block] = true;
434 	radeon_irq_set(rdev);
435 	lockmgr(&rdev->irq.lock, LK_RELEASE);
436 }
437 
438 /**
439  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
440  *
441  * @rdev: radeon device pointer
442  * @block: afmt block whose interrupt you want to disable
443  *
444  * Disables the afmt change interrupt for a specific afmt block (all asics).
445  */
446 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
447 {
448 	if (!rdev->ddev->irq_enabled)
449 		return;
450 
451 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
452 	rdev->irq.afmt[block] = false;
453 	radeon_irq_set(rdev);
454 	lockmgr(&rdev->irq.lock, LK_RELEASE);
455 }
456 
457 /**
458  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
459  *
460  * @rdev: radeon device pointer
461  * @hpd_mask: mask of hpd pins you want to enable.
462  *
463  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
464  */
465 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
466 {
467 	int i;
468 
469 	if (!rdev->ddev->irq_enabled)
470 		return;
471 
472 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
473 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
474 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
475 	radeon_irq_set(rdev);
476 	lockmgr(&rdev->irq.lock, LK_RELEASE);
477 }
478 
479 /**
480  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
481  *
482  * @rdev: radeon device pointer
483  * @hpd_mask: mask of hpd pins you want to disable.
484  *
485  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
486  */
487 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
488 {
489 	int i;
490 
491 	if (!rdev->ddev->irq_enabled)
492 		return;
493 
494 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
495 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
496 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
497 	radeon_irq_set(rdev);
498 	lockmgr(&rdev->irq.lock, LK_RELEASE);
499 }
500 
501