2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
29 #include <linux/pci.h>
30 #include <linux/pm_runtime.h>
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>
40 #include "radeon_kms.h"
41 #include "radeon_reg.h"
44 #define RADEON_WAIT_IDLE_TIMEOUT 200
47 * radeon_driver_irq_handler_kms - irq handler for KMS
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.
53 static irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
55 struct drm_device *dev = (struct drm_device *) arg;
56 struct radeon_device *rdev = dev->dev_private;
59 ret = radeon_irq_process(rdev);
60 if (ret == IRQ_HANDLED)
61 pm_runtime_mark_last_busy(dev->dev);
66 * Handle hotplug events outside the interrupt handler proper.
69 * radeon_hotplug_work_func - display hotplug work handler
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.
79 static void radeon_hotplug_work_func(struct work_struct *work)
81 struct radeon_device *rdev = container_of(work, struct radeon_device,
83 struct drm_device *dev = rdev->ddev;
84 struct drm_mode_config *mode_config = &dev->mode_config;
85 struct drm_connector *connector;
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)
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);
100 static void radeon_dp_work_func(struct work_struct *work)
105 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
107 * @dev: drm dev pointer
109 * Gets the hw ready to enable irqs (all asics).
110 * This function disables all interrupt sources on the GPU.
112 static void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
114 struct radeon_device *rdev = dev->dev_private;
115 unsigned long irqflags;
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;
130 radeon_irq_set(rdev);
131 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
133 radeon_irq_process(rdev);
137 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
139 * @dev: drm dev pointer
141 * Handles stuff to be done after enabling irqs (all asics).
142 * Returns 0 on success.
144 static int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
146 struct radeon_device *rdev = dev->dev_private;
148 if (ASIC_IS_AVIVO(rdev))
149 dev->max_vblank_count = 0x00ffffff;
151 dev->max_vblank_count = 0x001fffff;
157 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
159 * @dev: drm dev pointer
161 * This function disables all interrupt sources on the GPU (all asics).
163 static void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
165 struct radeon_device *rdev = dev->dev_private;
166 unsigned long irqflags;
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;
184 radeon_irq_set(rdev);
185 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
188 static int radeon_irq_install(struct radeon_device *rdev, int irq)
190 struct drm_device *dev = rdev->ddev;
193 if (irq == IRQ_NOTCONNECTED)
196 radeon_driver_irq_preinstall_kms(dev);
198 /* PCI devices require shared interrupts. */
199 ret = request_irq(irq, radeon_driver_irq_handler_kms,
200 IRQF_SHARED, dev->driver->name, dev);
204 radeon_driver_irq_postinstall_kms(dev);
209 static void radeon_irq_uninstall(struct radeon_device *rdev)
211 struct drm_device *dev = rdev->ddev;
212 struct pci_dev *pdev = to_pci_dev(dev->dev);
214 radeon_driver_irq_uninstall_kms(dev);
215 free_irq(pdev->irq, dev);
219 * radeon_msi_ok - asic specific msi checks
221 * @rdev: radeon device pointer
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.
228 static bool radeon_msi_ok(struct radeon_device *rdev)
230 /* RV370/RV380 was first asic with MSI support */
231 if (rdev->family < CHIP_RV380)
234 /* MSIs don't work on AGP */
235 if (rdev->flags & RADEON_IS_AGP)
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
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;
251 else if (radeon_msi == 0)
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))
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))
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))
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))
279 /* try and enable MSIs by default on all RS690s */
280 if (rdev->family == CHIP_RS690)
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.
287 if (rdev->family == CHIP_RV515)
289 if (rdev->flags & RADEON_IS_IGP) {
290 /* APUs work fine with MSIs */
291 if (rdev->family >= CHIP_PALM)
293 /* lots of IGPs have problems with MSIs */
301 * radeon_irq_kms_init - init driver interrupt info
303 * @rdev: radeon device pointer
305 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
306 * Returns 0 for success, error for failure.
308 int radeon_irq_kms_init(struct radeon_device *rdev)
312 spin_lock_init(&rdev->irq.lock);
314 /* Disable vblank irqs aggressively for power-saving */
315 rdev->ddev->vblank_disable_immediate = true;
317 r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
323 rdev->msi_enabled = 0;
325 if (radeon_msi_ok(rdev)) {
326 int ret = pci_enable_msi(rdev->pdev);
328 rdev->msi_enabled = 1;
329 dev_info(rdev->dev, "radeon: using MSI.\n");
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);
337 rdev->irq.installed = true;
338 r = radeon_irq_install(rdev, rdev->pdev->irq);
340 rdev->irq.installed = false;
341 flush_delayed_work(&rdev->hotplug_work);
345 DRM_INFO("radeon: irq initialized.\n");
350 * radeon_irq_kms_fini - tear down driver interrupt info
352 * @rdev: radeon device pointer
354 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
356 void radeon_irq_kms_fini(struct radeon_device *rdev)
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);
368 * radeon_irq_kms_sw_irq_get - enable software interrupt
370 * @rdev: radeon device pointer
371 * @ring: ring whose interrupt you want to enable
373 * Enables the software interrupt for a specific ring (all asics).
374 * The software interrupt is generally used to signal a fence on
377 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
379 unsigned long irqflags;
381 if (!rdev->irq.installed)
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);
392 * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
394 * @rdev: radeon device pointer
395 * @ring: ring whose interrupt you want to enable
397 * Enables the software interrupt for a specific ring (all asics).
398 * The software interrupt is generally used to signal a fence on
401 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
403 return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
407 * radeon_irq_kms_sw_irq_put - disable software interrupt
409 * @rdev: radeon device pointer
410 * @ring: ring whose interrupt you want to disable
412 * Disables the software interrupt for a specific ring (all asics).
413 * The software interrupt is generally used to signal a fence on
416 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
418 unsigned long irqflags;
420 if (!rdev->irq.installed)
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);
431 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
433 * @rdev: radeon device pointer
434 * @crtc: crtc whose interrupt you want to enable
436 * Enables the pageflip interrupt for a specific crtc (all asics).
437 * For pageflips we use the vblank interrupt source.
439 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
441 unsigned long irqflags;
443 if (crtc < 0 || crtc >= rdev->num_crtc)
446 if (!rdev->irq.installed)
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);
457 * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
459 * @rdev: radeon device pointer
460 * @crtc: crtc whose interrupt you want to disable
462 * Disables the pageflip interrupt for a specific crtc (all asics).
463 * For pageflips we use the vblank interrupt source.
465 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
467 unsigned long irqflags;
469 if (crtc < 0 || crtc >= rdev->num_crtc)
472 if (!rdev->irq.installed)
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);
483 * radeon_irq_kms_enable_afmt - enable audio format change interrupt
485 * @rdev: radeon device pointer
486 * @block: afmt block whose interrupt you want to enable
488 * Enables the afmt change interrupt for a specific afmt block (all asics).
490 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
492 unsigned long irqflags;
494 if (!rdev->irq.installed)
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);
505 * radeon_irq_kms_disable_afmt - disable audio format change interrupt
507 * @rdev: radeon device pointer
508 * @block: afmt block whose interrupt you want to disable
510 * Disables the afmt change interrupt for a specific afmt block (all asics).
512 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
514 unsigned long irqflags;
516 if (!rdev->irq.installed)
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);
526 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
528 * @rdev: radeon device pointer
529 * @hpd_mask: mask of hpd pins you want to enable.
531 * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
533 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
535 unsigned long irqflags;
538 if (!rdev->irq.installed)
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);
549 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
551 * @rdev: radeon device pointer
552 * @hpd_mask: mask of hpd pins you want to disable.
554 * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
556 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
558 unsigned long irqflags;
561 if (!rdev->irq.installed)
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);
572 * radeon_irq_kms_set_irq_n_enabled - helper for updating interrupt enable registers
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
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.
586 * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
588 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
590 bool enable, const char *name, unsigned n)
592 u32 tmp = RREG32(reg);
594 /* Interrupt state didn't change */
595 if (!!(tmp & mask) == enable)
599 DRM_DEBUG("%s%d interrupts enabled\n", name, n);
600 WREG32(reg, tmp |= mask);
602 DRM_DEBUG("%s%d interrupts disabled\n", name, n);
603 WREG32(reg, tmp & ~mask);