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