xref: /openbsd/sys/dev/pci/drm/radeon/radeon_irq_kms.c (revision 09467b48)
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 <linux/pci.h>
30 #include <linux/pm_runtime.h>
31 
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_device.h>
34 #include <drm/drm_irq.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/drm_vblank.h>
37 #include <drm/radeon_drm.h>
38 
39 #include "atom.h"
40 #include "radeon.h"
41 #include "radeon_reg.h"
42 
43 
44 #define RADEON_WAIT_IDLE_TIMEOUT 200
45 
46 /**
47  * radeon_driver_irq_handler_kms - irq handler for KMS
48  *
49  * @int irq, void *arg: args
50  *
51  * This is the irq handler for the radeon KMS driver (all asics).
52  * radeon_irq_process is a macro that points to the per-asic
53  * irq handler callback.
54  */
55 irqreturn_t radeon_driver_irq_handler_kms(void *arg)
56 {
57 	struct drm_device *dev = (struct drm_device *) arg;
58 	struct radeon_device *rdev = dev->dev_private;
59 	irqreturn_t ret;
60 
61 	if (!rdev->irq.installed)
62 		return 0;
63 
64 	ret = radeon_irq_process(rdev);
65 	if (ret == IRQ_HANDLED)
66 		pm_runtime_mark_last_busy(dev->dev);
67 	return ret;
68 }
69 
70 /*
71  * Handle hotplug events outside the interrupt handler proper.
72  */
73 /**
74  * radeon_hotplug_work_func - display hotplug work handler
75  *
76  * @work: work struct
77  *
78  * This is the hot plug event work handler (all asics).
79  * The work gets scheduled from the irq handler if there
80  * was a hot plug interrupt.  It walks the connector table
81  * and calls the hotplug handler for each one, then sends
82  * a drm hotplug event to alert userspace.
83  */
84 static void radeon_hotplug_work_func(struct work_struct *work)
85 {
86 	struct radeon_device *rdev = container_of(work, struct radeon_device,
87 						  hotplug_work.work);
88 	struct drm_device *dev = rdev->ddev;
89 	struct drm_mode_config *mode_config = &dev->mode_config;
90 	struct drm_connector *connector;
91 
92 	/* we can race here at startup, some boards seem to trigger
93 	 * hotplug irqs when they shouldn't. */
94 	if (!rdev->mode_info.mode_config_initialized)
95 		return;
96 
97 	mutex_lock(&mode_config->mutex);
98 	list_for_each_entry(connector, &mode_config->connector_list, head)
99 		radeon_connector_hotplug(connector);
100 	mutex_unlock(&mode_config->mutex);
101 	/* Just fire off a uevent and let userspace tell us what to do */
102 	drm_helper_hpd_irq_event(dev);
103 }
104 
105 static void radeon_dp_work_func(struct work_struct *work)
106 {
107 	struct radeon_device *rdev = container_of(work, struct radeon_device,
108 						  dp_work);
109 	struct drm_device *dev = rdev->ddev;
110 	struct drm_mode_config *mode_config = &dev->mode_config;
111 	struct drm_connector *connector;
112 
113 	/* this should take a mutex */
114 	list_for_each_entry(connector, &mode_config->connector_list, head)
115 		radeon_connector_hotplug(connector);
116 }
117 /**
118  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
119  *
120  * @dev: drm dev pointer
121  *
122  * Gets the hw ready to enable irqs (all asics).
123  * This function disables all interrupt sources on the GPU.
124  */
125 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
126 {
127 	struct radeon_device *rdev = dev->dev_private;
128 	unsigned long irqflags;
129 	unsigned i;
130 
131 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
132 	/* Disable *all* interrupts */
133 	for (i = 0; i < RADEON_NUM_RINGS; i++)
134 		atomic_set(&rdev->irq.ring_int[i], 0);
135 	rdev->irq.dpm_thermal = false;
136 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
137 		rdev->irq.hpd[i] = false;
138 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
139 		rdev->irq.crtc_vblank_int[i] = false;
140 		atomic_set(&rdev->irq.pflip[i], 0);
141 		rdev->irq.afmt[i] = false;
142 	}
143 	radeon_irq_set(rdev);
144 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
145 	/* Clear bits */
146 	radeon_irq_process(rdev);
147 }
148 
149 /**
150  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
151  *
152  * @dev: drm dev pointer
153  *
154  * Handles stuff to be done after enabling irqs (all asics).
155  * Returns 0 on success.
156  */
157 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
158 {
159 	struct radeon_device *rdev = dev->dev_private;
160 
161 	if (ASIC_IS_AVIVO(rdev))
162 		dev->max_vblank_count = 0x00ffffff;
163 	else
164 		dev->max_vblank_count = 0x001fffff;
165 
166 	return 0;
167 }
168 
169 /**
170  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
171  *
172  * @dev: drm dev pointer
173  *
174  * This function disables all interrupt sources on the GPU (all asics).
175  */
176 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
177 {
178 	struct radeon_device *rdev = dev->dev_private;
179 	unsigned long irqflags;
180 	unsigned i;
181 
182 	if (rdev == NULL) {
183 		return;
184 	}
185 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
186 	/* Disable *all* interrupts */
187 	for (i = 0; i < RADEON_NUM_RINGS; i++)
188 		atomic_set(&rdev->irq.ring_int[i], 0);
189 	rdev->irq.dpm_thermal = false;
190 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
191 		rdev->irq.hpd[i] = false;
192 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
193 		rdev->irq.crtc_vblank_int[i] = false;
194 		atomic_set(&rdev->irq.pflip[i], 0);
195 		rdev->irq.afmt[i] = false;
196 	}
197 	radeon_irq_set(rdev);
198 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
199 }
200 
201 /**
202  * radeon_msi_ok - asic specific msi checks
203  *
204  * @rdev: radeon device pointer
205  *
206  * Handles asic specific MSI checks to determine if
207  * MSIs should be enabled on a particular chip (all asics).
208  * Returns true if MSIs should be enabled, false if MSIs
209  * should not be enabled.
210  */
211 bool radeon_msi_ok(struct radeon_device *rdev)
212 {
213 	/* RV370/RV380 was first asic with MSI support */
214 	if (rdev->family < CHIP_RV380)
215 		return false;
216 
217 	/* MSIs don't work on AGP */
218 	if (rdev->flags & RADEON_IS_AGP)
219 		return false;
220 
221 	/*
222 	 * Older chips have a HW limitation, they can only generate 40 bits
223 	 * of address for "64-bit" MSIs which breaks on some platforms, notably
224 	 * IBM POWER servers, so we limit them
225 	 */
226 	if (rdev->family < CHIP_BONAIRE) {
227 		dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
228 		rdev->pdev->no_64bit_msi = 1;
229 	}
230 
231 	/* force MSI on */
232 	if (radeon_msi == 1)
233 		return true;
234 	else if (radeon_msi == 0)
235 		return false;
236 
237 	/* Quirks */
238 	/* HP RS690 only seems to work with MSIs. */
239 	if ((rdev->pdev->device == 0x791f) &&
240 	    (rdev->pdev->subsystem_vendor == 0x103c) &&
241 	    (rdev->pdev->subsystem_device == 0x30c2))
242 		return true;
243 
244 	/* Dell RS690 only seems to work with MSIs. */
245 	if ((rdev->pdev->device == 0x791f) &&
246 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
247 	    (rdev->pdev->subsystem_device == 0x01fc))
248 		return true;
249 
250 	/* Dell RS690 only seems to work with MSIs. */
251 	if ((rdev->pdev->device == 0x791f) &&
252 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
253 	    (rdev->pdev->subsystem_device == 0x01fd))
254 		return true;
255 
256 	/* Gateway RS690 only seems to work with MSIs. */
257 	if ((rdev->pdev->device == 0x791f) &&
258 	    (rdev->pdev->subsystem_vendor == 0x107b) &&
259 	    (rdev->pdev->subsystem_device == 0x0185))
260 		return true;
261 
262 	/* try and enable MSIs by default on all RS690s */
263 	if (rdev->family == CHIP_RS690)
264 		return true;
265 
266 	/* RV515 seems to have MSI issues where it loses
267 	 * MSI rearms occasionally. This leads to lockups and freezes.
268 	 * disable it by default.
269 	 */
270 	if (rdev->family == CHIP_RV515)
271 		return false;
272 	if (rdev->flags & RADEON_IS_IGP) {
273 		/* APUs work fine with MSIs */
274 		if (rdev->family >= CHIP_PALM)
275 			return true;
276 		/* lots of IGPs have problems with MSIs */
277 		return false;
278 	}
279 
280 	return true;
281 }
282 
283 /**
284  * radeon_irq_kms_init - init driver interrupt info
285  *
286  * @rdev: radeon device pointer
287  *
288  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
289  * Returns 0 for success, error for failure.
290  */
291 int radeon_irq_kms_init(struct radeon_device *rdev)
292 {
293 	int r = 0;
294 
295 	mtx_init(&rdev->irq.lock, IPL_TTY);
296 
297 	/* Disable vblank irqs aggressively for power-saving */
298 	rdev->ddev->vblank_disable_immediate = true;
299 
300 	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
301 	if (r) {
302 		return r;
303 	}
304 
305 	/* enable msi */
306 	rdev->msi_enabled = 0;
307 
308 	if (radeon_msi_ok(rdev)) {
309 		int ret = pci_enable_msi(rdev->pdev);
310 		if (!ret) {
311 			rdev->msi_enabled = 1;
312 			dev_info(rdev->dev, "radeon: using MSI.\n");
313 		}
314 	}
315 
316 	INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
317 	INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
318 	INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
319 
320 	rdev->irq.installed = true;
321 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
322 	if (r) {
323 		rdev->irq.installed = false;
324 		flush_delayed_work(&rdev->hotplug_work);
325 		return r;
326 	}
327 
328 	DRM_INFO("radeon: irq initialized.\n");
329 	return 0;
330 }
331 
332 /**
333  * radeon_irq_kms_fini - tear down driver interrupt info
334  *
335  * @rdev: radeon device pointer
336  *
337  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
338  */
339 void radeon_irq_kms_fini(struct radeon_device *rdev)
340 {
341 	if (rdev->irq.installed) {
342 		drm_irq_uninstall(rdev->ddev);
343 		rdev->irq.installed = false;
344 		if (rdev->msi_enabled)
345 			pci_disable_msi(rdev->pdev);
346 		flush_delayed_work(&rdev->hotplug_work);
347 	}
348 }
349 
350 /**
351  * radeon_irq_kms_sw_irq_get - enable software interrupt
352  *
353  * @rdev: radeon device pointer
354  * @ring: ring whose interrupt you want to enable
355  *
356  * Enables the software interrupt for a specific ring (all asics).
357  * The software interrupt is generally used to signal a fence on
358  * a particular ring.
359  */
360 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
361 {
362 	unsigned long irqflags;
363 
364 	if (!rdev->ddev->irq_enabled)
365 		return;
366 
367 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
368 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
369 		radeon_irq_set(rdev);
370 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
371 	}
372 }
373 
374 /**
375  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
376  *
377  * @rdev: radeon device pointer
378  * @ring: ring whose interrupt you want to enable
379  *
380  * Enables the software interrupt for a specific ring (all asics).
381  * The software interrupt is generally used to signal a fence on
382  * a particular ring.
383  */
384 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
385 {
386 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
387 }
388 
389 /**
390  * radeon_irq_kms_sw_irq_put - disable software interrupt
391  *
392  * @rdev: radeon device pointer
393  * @ring: ring whose interrupt you want to disable
394  *
395  * Disables the software interrupt for a specific ring (all asics).
396  * The software interrupt is generally used to signal a fence on
397  * a particular ring.
398  */
399 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
400 {
401 	unsigned long irqflags;
402 
403 	if (!rdev->ddev->irq_enabled)
404 		return;
405 
406 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
407 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
408 		radeon_irq_set(rdev);
409 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
410 	}
411 }
412 
413 /**
414  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
415  *
416  * @rdev: radeon device pointer
417  * @crtc: crtc whose interrupt you want to enable
418  *
419  * Enables the pageflip interrupt for a specific crtc (all asics).
420  * For pageflips we use the vblank interrupt source.
421  */
422 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
423 {
424 	unsigned long irqflags;
425 
426 	if (crtc < 0 || crtc >= rdev->num_crtc)
427 		return;
428 
429 	if (!rdev->ddev->irq_enabled)
430 		return;
431 
432 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
433 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
434 		radeon_irq_set(rdev);
435 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
436 	}
437 }
438 
439 /**
440  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
441  *
442  * @rdev: radeon device pointer
443  * @crtc: crtc whose interrupt you want to disable
444  *
445  * Disables the pageflip interrupt for a specific crtc (all asics).
446  * For pageflips we use the vblank interrupt source.
447  */
448 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
449 {
450 	unsigned long irqflags;
451 
452 	if (crtc < 0 || crtc >= rdev->num_crtc)
453 		return;
454 
455 	if (!rdev->ddev->irq_enabled)
456 		return;
457 
458 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
459 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
460 		radeon_irq_set(rdev);
461 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
462 	}
463 }
464 
465 /**
466  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
467  *
468  * @rdev: radeon device pointer
469  * @block: afmt block whose interrupt you want to enable
470  *
471  * Enables the afmt change interrupt for a specific afmt block (all asics).
472  */
473 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
474 {
475 	unsigned long irqflags;
476 
477 	if (!rdev->ddev->irq_enabled)
478 		return;
479 
480 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
481 	rdev->irq.afmt[block] = true;
482 	radeon_irq_set(rdev);
483 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
484 
485 }
486 
487 /**
488  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
489  *
490  * @rdev: radeon device pointer
491  * @block: afmt block whose interrupt you want to disable
492  *
493  * Disables the afmt change interrupt for a specific afmt block (all asics).
494  */
495 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
496 {
497 	unsigned long irqflags;
498 
499 	if (!rdev->ddev->irq_enabled)
500 		return;
501 
502 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
503 	rdev->irq.afmt[block] = false;
504 	radeon_irq_set(rdev);
505 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
506 }
507 
508 /**
509  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
510  *
511  * @rdev: radeon device pointer
512  * @hpd_mask: mask of hpd pins you want to enable.
513  *
514  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
515  */
516 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
517 {
518 	unsigned long irqflags;
519 	int i;
520 
521 	if (!rdev->ddev->irq_enabled)
522 		return;
523 
524 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
525 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
526 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
527 	radeon_irq_set(rdev);
528 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
529 }
530 
531 /**
532  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
533  *
534  * @rdev: radeon device pointer
535  * @hpd_mask: mask of hpd pins you want to disable.
536  *
537  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
538  */
539 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
540 {
541 	unsigned long irqflags;
542 	int i;
543 
544 	if (!rdev->ddev->irq_enabled)
545 		return;
546 
547 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
548 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
549 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
550 	radeon_irq_set(rdev);
551 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
552 }
553 
554 /**
555  * radeon_irq_kms_update_int_n - helper for updating interrupt enable registers
556  *
557  * @rdev: radeon device pointer
558  * @reg: the register to write to enable/disable interrupts
559  * @mask: the mask that enables the interrupts
560  * @enable: whether to enable or disable the interrupt register
561  * @name: the name of the interrupt register to print to the kernel log
562  * @num: the number of the interrupt register to print to the kernel log
563  *
564  * Helper for updating the enable state of interrupt registers. Checks whether
565  * or not the interrupt matches the enable state we want. If it doesn't, then
566  * we update it and print a debugging message to the kernel log indicating the
567  * new state of the interrupt register.
568  *
569  * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
570  */
571 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
572 				      u32 reg, u32 mask,
573 				      bool enable, const char *name, unsigned n)
574 {
575 	u32 tmp = RREG32(reg);
576 
577 	/* Interrupt state didn't change */
578 	if (!!(tmp & mask) == enable)
579 		return;
580 
581 	if (enable) {
582 		DRM_DEBUG("%s%d interrupts enabled\n", name, n);
583 		WREG32(reg, tmp |= mask);
584 	} else {
585 		DRM_DEBUG("%s%d interrupts disabled\n", name, n);
586 		WREG32(reg, tmp & ~mask);
587 	}
588 }
589