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