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