xref: /dragonfly/sys/dev/drm/radeon/radeon_irq_kms.c (revision b990a6be)
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 	/* enable msi */
266 	rdev->msi_enabled = (rdev->ddev->irq_type == PCI_INTR_TYPE_MSI);
267 
268 	TASK_INIT(&rdev->hotplug_work, 0, radeon_hotplug_work_func, rdev);
269 	TASK_INIT(&rdev->audio_work, 0, r600_audio_update_hdmi, rdev);
270 
271 	rdev->irq.installed = true;
272 	DRM_UNLOCK(rdev->ddev);
273 	r = drm_irq_install(rdev->ddev, rdev->ddev->irq);
274 	DRM_LOCK(rdev->ddev);
275 	if (r) {
276 		rdev->irq.installed = false;
277 		taskqueue_drain(rdev->tq, &rdev->hotplug_work);
278 		return r;
279 	}
280 
281 	DRM_INFO("radeon: irq initialized.\n");
282 	return 0;
283 }
284 
285 /**
286  * radeon_irq_kms_fini - tear down driver interrupt info
287  *
288  * @rdev: radeon device pointer
289  *
290  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
291  */
292 void radeon_irq_kms_fini(struct radeon_device *rdev)
293 {
294 	drm_vblank_cleanup(rdev->ddev);
295 	if (rdev->irq.installed) {
296 		drm_irq_uninstall(rdev->ddev);
297 		rdev->irq.installed = false;
298 		taskqueue_drain(rdev->tq, &rdev->hotplug_work);
299 	}
300 }
301 
302 /**
303  * radeon_irq_kms_sw_irq_get - enable software interrupt
304  *
305  * @rdev: radeon device pointer
306  * @ring: ring whose interrupt you want to enable
307  *
308  * Enables the software interrupt for a specific ring (all asics).
309  * The software interrupt is generally used to signal a fence on
310  * a particular ring.
311  */
312 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
313 {
314 	if (!rdev->ddev->irq_enabled)
315 		return;
316 
317 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
318 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
319 		radeon_irq_set(rdev);
320 		lockmgr(&rdev->irq.lock, LK_RELEASE);
321 	}
322 }
323 
324 /**
325  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
326  *
327  * @rdev: radeon device pointer
328  * @ring: ring whose interrupt you want to enable
329  *
330  * Enables the software interrupt for a specific ring (all asics).
331  * The software interrupt is generally used to signal a fence on
332  * a particular ring.
333  */
334 #ifdef TODO_954605c
335 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
336 {
337 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
338 }
339 #endif
340 
341 /**
342  * radeon_irq_kms_sw_irq_put - disable software interrupt
343  *
344  * @rdev: radeon device pointer
345  * @ring: ring whose interrupt you want to disable
346  *
347  * Disables the software interrupt for a specific ring (all asics).
348  * The software interrupt is generally used to signal a fence on
349  * a particular ring.
350  */
351 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
352 {
353 	if (!rdev->ddev->irq_enabled)
354 		return;
355 
356 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
357 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
358 		radeon_irq_set(rdev);
359 		lockmgr(&rdev->irq.lock, LK_RELEASE);
360 	}
361 }
362 
363 /**
364  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
365  *
366  * @rdev: radeon device pointer
367  * @crtc: crtc whose interrupt you want to enable
368  *
369  * Enables the pageflip interrupt for a specific crtc (all asics).
370  * For pageflips we use the vblank interrupt source.
371  */
372 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
373 {
374 	if (crtc < 0 || crtc >= rdev->num_crtc)
375 		return;
376 
377 	if (!rdev->ddev->irq_enabled)
378 		return;
379 
380 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
381 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
382 		radeon_irq_set(rdev);
383 		lockmgr(&rdev->irq.lock, LK_RELEASE);
384 	}
385 }
386 
387 /**
388  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
389  *
390  * @rdev: radeon device pointer
391  * @crtc: crtc whose interrupt you want to disable
392  *
393  * Disables the pageflip interrupt for a specific crtc (all asics).
394  * For pageflips we use the vblank interrupt source.
395  */
396 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
397 {
398 	if (crtc < 0 || crtc >= rdev->num_crtc)
399 		return;
400 
401 	if (!rdev->ddev->irq_enabled)
402 		return;
403 
404 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
405 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
406 		radeon_irq_set(rdev);
407 		lockmgr(&rdev->irq.lock, LK_RELEASE);
408 	}
409 }
410 
411 /**
412  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
413  *
414  * @rdev: radeon device pointer
415  * @block: afmt block whose interrupt you want to enable
416  *
417  * Enables the afmt change interrupt for a specific afmt block (all asics).
418  */
419 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
420 {
421 	if (!rdev->ddev->irq_enabled)
422 		return;
423 
424 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
425 	rdev->irq.afmt[block] = true;
426 	radeon_irq_set(rdev);
427 	lockmgr(&rdev->irq.lock, LK_RELEASE);
428 }
429 
430 /**
431  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
432  *
433  * @rdev: radeon device pointer
434  * @block: afmt block whose interrupt you want to disable
435  *
436  * Disables the afmt change interrupt for a specific afmt block (all asics).
437  */
438 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
439 {
440 	if (!rdev->ddev->irq_enabled)
441 		return;
442 
443 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
444 	rdev->irq.afmt[block] = false;
445 	radeon_irq_set(rdev);
446 	lockmgr(&rdev->irq.lock, LK_RELEASE);
447 }
448 
449 /**
450  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
451  *
452  * @rdev: radeon device pointer
453  * @hpd_mask: mask of hpd pins you want to enable.
454  *
455  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
456  */
457 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
458 {
459 	int i;
460 
461 	if (!rdev->ddev->irq_enabled)
462 		return;
463 
464 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
465 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
466 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
467 	radeon_irq_set(rdev);
468 	lockmgr(&rdev->irq.lock, LK_RELEASE);
469 }
470 
471 /**
472  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
473  *
474  * @rdev: radeon device pointer
475  * @hpd_mask: mask of hpd pins you want to disable.
476  *
477  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
478  */
479 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
480 {
481 	int i;
482 
483 	if (!rdev->ddev->irq_enabled)
484 		return;
485 
486 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
487 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
488 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
489 	radeon_irq_set(rdev);
490 	lockmgr(&rdev->irq.lock, LK_RELEASE);
491 }
492 
493