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