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