xref: /dragonfly/sys/dev/drm/radeon/radeon_irq_kms.c (revision ef2687d4)
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 *dev, 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_device_is_agp(dev))
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 ((dev->pci_device == 0x791f) &&
205 	    (dev->pci_subvendor == 0x103c) &&
206 	    (dev->pci_subdevice == 0x30c2))
207 		return true;
208 
209 	/* Dell RS690 only seems to work with MSIs. */
210 	if ((dev->pci_device == 0x791f) &&
211 	    (dev->pci_subvendor == 0x1028) &&
212 	    (dev->pci_subdevice == 0x01fc))
213 		return true;
214 
215 	/* Dell RS690 only seems to work with MSIs. */
216 	if ((dev->pci_device == 0x791f) &&
217 	    (dev->pci_subvendor == 0x1028) &&
218 	    (dev->pci_subdevice == 0x01fd))
219 		return true;
220 
221 	/* Gateway RS690 only seems to work with MSIs. */
222 	if ((dev->pci_device == 0x791f) &&
223 	    (dev->pci_subvendor == 0x107b) &&
224 	    (dev->pci_subdevice == 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_put - disable software interrupt
326  *
327  * @rdev: radeon device pointer
328  * @ring: ring whose interrupt you want to disable
329  *
330  * Disables 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 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
335 {
336 	if (!rdev->ddev->irq_enabled)
337 		return;
338 
339 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
340 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
341 		radeon_irq_set(rdev);
342 		lockmgr(&rdev->irq.lock, LK_RELEASE);
343 	}
344 }
345 
346 /**
347  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
348  *
349  * @rdev: radeon device pointer
350  * @crtc: crtc whose interrupt you want to enable
351  *
352  * Enables the pageflip interrupt for a specific crtc (all asics).
353  * For pageflips we use the vblank interrupt source.
354  */
355 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
356 {
357 	if (crtc < 0 || crtc >= rdev->num_crtc)
358 		return;
359 
360 	if (!rdev->ddev->irq_enabled)
361 		return;
362 
363 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
364 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
365 		radeon_irq_set(rdev);
366 		lockmgr(&rdev->irq.lock, LK_RELEASE);
367 	}
368 }
369 
370 /**
371  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
372  *
373  * @rdev: radeon device pointer
374  * @crtc: crtc whose interrupt you want to disable
375  *
376  * Disables the pageflip interrupt for a specific crtc (all asics).
377  * For pageflips we use the vblank interrupt source.
378  */
379 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
380 {
381 	if (crtc < 0 || crtc >= rdev->num_crtc)
382 		return;
383 
384 	if (!rdev->ddev->irq_enabled)
385 		return;
386 
387 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
388 		lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
389 		radeon_irq_set(rdev);
390 		lockmgr(&rdev->irq.lock, LK_RELEASE);
391 	}
392 }
393 
394 /**
395  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
396  *
397  * @rdev: radeon device pointer
398  * @block: afmt block whose interrupt you want to enable
399  *
400  * Enables the afmt change interrupt for a specific afmt block (all asics).
401  */
402 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
403 {
404 	if (!rdev->ddev->irq_enabled)
405 		return;
406 
407 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
408 	rdev->irq.afmt[block] = true;
409 	radeon_irq_set(rdev);
410 	lockmgr(&rdev->irq.lock, LK_RELEASE);
411 }
412 
413 /**
414  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
415  *
416  * @rdev: radeon device pointer
417  * @block: afmt block whose interrupt you want to disable
418  *
419  * Disables the afmt change interrupt for a specific afmt block (all asics).
420  */
421 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
422 {
423 	if (!rdev->ddev->irq_enabled)
424 		return;
425 
426 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
427 	rdev->irq.afmt[block] = false;
428 	radeon_irq_set(rdev);
429 	lockmgr(&rdev->irq.lock, LK_RELEASE);
430 }
431 
432 /**
433  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
434  *
435  * @rdev: radeon device pointer
436  * @hpd_mask: mask of hpd pins you want to enable.
437  *
438  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
439  */
440 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
441 {
442 	int i;
443 
444 	if (!rdev->ddev->irq_enabled)
445 		return;
446 
447 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
448 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
449 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
450 	radeon_irq_set(rdev);
451 	lockmgr(&rdev->irq.lock, LK_RELEASE);
452 }
453 
454 /**
455  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
456  *
457  * @rdev: radeon device pointer
458  * @hpd_mask: mask of hpd pins you want to disable.
459  *
460  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
461  */
462 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
463 {
464 	int i;
465 
466 	if (!rdev->ddev->irq_enabled)
467 		return;
468 
469 	lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
470 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
471 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
472 	radeon_irq_set(rdev);
473 	lockmgr(&rdev->irq.lock, LK_RELEASE);
474 }
475 
476