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