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_crtc_helper.h>
33 #include <drm/drm_device.h>
34 #include <drm/drm_drv.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/drm_vblank.h>
37 #include <drm/radeon_drm.h>
41 #include "radeon_kms.h"
42 #include "radeon_reg.h"
45 #define RADEON_WAIT_IDLE_TIMEOUT 200
48 * radeon_driver_irq_handler_kms - irq handler for KMS
50 * This is the irq handler for the radeon KMS driver (all asics).
51 * radeon_irq_process is a macro that points to the per-asic
52 * irq handler callback.
54 static irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
56 struct drm_device *dev = (struct drm_device *) arg;
57 struct radeon_device *rdev = dev->dev_private;
60 ret = radeon_irq_process(rdev);
61 if (ret == IRQ_HANDLED)
62 pm_runtime_mark_last_busy(dev->dev);
67 * Handle hotplug events outside the interrupt handler proper.
70 * radeon_hotplug_work_func - display hotplug work handler
74 * This is the hot plug event work handler (all asics).
75 * The work gets scheduled from the irq handler if there
76 * was a hot plug interrupt. It walks the connector table
77 * and calls the hotplug handler for each one, then sends
78 * a drm hotplug event to alert userspace.
80 static void radeon_hotplug_work_func(struct work_struct *work)
82 struct radeon_device *rdev = container_of(work, struct radeon_device,
84 struct drm_device *dev = rdev->ddev;
85 struct drm_mode_config *mode_config = &dev->mode_config;
86 struct drm_connector *connector;
88 /* we can race here at startup, some boards seem to trigger
89 * hotplug irqs when they shouldn't. */
90 if (!rdev->mode_info.mode_config_initialized)
93 mutex_lock(&mode_config->mutex);
94 list_for_each_entry(connector, &mode_config->connector_list, head)
95 radeon_connector_hotplug(connector);
96 mutex_unlock(&mode_config->mutex);
97 /* Just fire off a uevent and let userspace tell us what to do */
98 drm_helper_hpd_irq_event(dev);
101 static void radeon_dp_work_func(struct work_struct *work)
103 struct radeon_device *rdev = container_of(work, struct radeon_device,
105 struct drm_device *dev = rdev->ddev;
106 struct drm_mode_config *mode_config = &dev->mode_config;
107 struct drm_connector *connector;
109 /* this should take a mutex */
110 list_for_each_entry(connector, &mode_config->connector_list, head)
111 radeon_connector_hotplug(connector);
114 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
116 * @dev: drm dev pointer
118 * Gets the hw ready to enable irqs (all asics).
119 * This function disables all interrupt sources on the GPU.
121 static void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
123 struct radeon_device *rdev = dev->dev_private;
124 unsigned long irqflags;
127 spin_lock_irqsave(&rdev->irq.lock, irqflags);
128 /* Disable *all* interrupts */
129 for (i = 0; i < RADEON_NUM_RINGS; i++)
130 atomic_set(&rdev->irq.ring_int[i], 0);
131 rdev->irq.dpm_thermal = false;
132 for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
133 rdev->irq.hpd[i] = false;
134 for (i = 0; i < RADEON_MAX_CRTCS; i++) {
135 rdev->irq.crtc_vblank_int[i] = false;
136 atomic_set(&rdev->irq.pflip[i], 0);
137 rdev->irq.afmt[i] = false;
139 radeon_irq_set(rdev);
140 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
142 radeon_irq_process(rdev);
146 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
148 * @dev: drm dev pointer
150 * Handles stuff to be done after enabling irqs (all asics).
151 * Returns 0 on success.
153 static int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
155 struct radeon_device *rdev = dev->dev_private;
157 if (ASIC_IS_AVIVO(rdev))
158 dev->max_vblank_count = 0x00ffffff;
160 dev->max_vblank_count = 0x001fffff;
166 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
168 * @dev: drm dev pointer
170 * This function disables all interrupt sources on the GPU (all asics).
172 static void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
174 struct radeon_device *rdev = dev->dev_private;
175 unsigned long irqflags;
181 spin_lock_irqsave(&rdev->irq.lock, irqflags);
182 /* Disable *all* interrupts */
183 for (i = 0; i < RADEON_NUM_RINGS; i++)
184 atomic_set(&rdev->irq.ring_int[i], 0);
185 rdev->irq.dpm_thermal = false;
186 for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
187 rdev->irq.hpd[i] = false;
188 for (i = 0; i < RADEON_MAX_CRTCS; i++) {
189 rdev->irq.crtc_vblank_int[i] = false;
190 atomic_set(&rdev->irq.pflip[i], 0);
191 rdev->irq.afmt[i] = false;
193 radeon_irq_set(rdev);
194 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
197 static int radeon_irq_install(struct radeon_device *rdev, int irq)
199 struct drm_device *dev = rdev->ddev;
202 if (irq == IRQ_NOTCONNECTED)
205 radeon_driver_irq_preinstall_kms(dev);
207 /* PCI devices require shared interrupts. */
208 ret = request_irq(irq, radeon_driver_irq_handler_kms,
209 IRQF_SHARED, dev->driver->name, dev);
213 radeon_driver_irq_postinstall_kms(dev);
218 static void radeon_irq_uninstall(struct radeon_device *rdev)
220 struct drm_device *dev = rdev->ddev;
221 struct pci_dev *pdev = to_pci_dev(dev->dev);
223 radeon_driver_irq_uninstall_kms(dev);
224 free_irq(pdev->irq, dev);
228 * radeon_msi_ok - asic specific msi checks
230 * @rdev: radeon device pointer
232 * Handles asic specific MSI checks to determine if
233 * MSIs should be enabled on a particular chip (all asics).
234 * Returns true if MSIs should be enabled, false if MSIs
235 * should not be enabled.
237 static bool radeon_msi_ok(struct radeon_device *rdev)
239 /* RV370/RV380 was first asic with MSI support */
240 if (rdev->family < CHIP_RV380)
243 /* MSIs don't work on AGP */
244 if (rdev->flags & RADEON_IS_AGP)
248 * Older chips have a HW limitation, they can only generate 40 bits
249 * of address for "64-bit" MSIs which breaks on some platforms, notably
250 * IBM POWER servers, so we limit them
252 if (rdev->family < CHIP_BONAIRE) {
253 dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
254 rdev->pdev->no_64bit_msi = 1;
260 else if (radeon_msi == 0)
264 /* HP RS690 only seems to work with MSIs. */
265 if ((rdev->pdev->device == 0x791f) &&
266 (rdev->pdev->subsystem_vendor == 0x103c) &&
267 (rdev->pdev->subsystem_device == 0x30c2))
270 /* Dell RS690 only seems to work with MSIs. */
271 if ((rdev->pdev->device == 0x791f) &&
272 (rdev->pdev->subsystem_vendor == 0x1028) &&
273 (rdev->pdev->subsystem_device == 0x01fc))
276 /* Dell RS690 only seems to work with MSIs. */
277 if ((rdev->pdev->device == 0x791f) &&
278 (rdev->pdev->subsystem_vendor == 0x1028) &&
279 (rdev->pdev->subsystem_device == 0x01fd))
282 /* Gateway RS690 only seems to work with MSIs. */
283 if ((rdev->pdev->device == 0x791f) &&
284 (rdev->pdev->subsystem_vendor == 0x107b) &&
285 (rdev->pdev->subsystem_device == 0x0185))
288 /* try and enable MSIs by default on all RS690s */
289 if (rdev->family == CHIP_RS690)
292 /* RV515 seems to have MSI issues where it loses
293 * MSI rearms occasionally. This leads to lockups and freezes.
294 * disable it by default.
296 if (rdev->family == CHIP_RV515)
298 if (rdev->flags & RADEON_IS_IGP) {
299 /* APUs work fine with MSIs */
300 if (rdev->family >= CHIP_PALM)
302 /* lots of IGPs have problems with MSIs */
310 * radeon_irq_kms_init - init driver interrupt info
312 * @rdev: radeon device pointer
314 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
315 * Returns 0 for success, error for failure.
317 int radeon_irq_kms_init(struct radeon_device *rdev)
321 spin_lock_init(&rdev->irq.lock);
323 /* Disable vblank irqs aggressively for power-saving */
324 rdev->ddev->vblank_disable_immediate = true;
326 r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
332 rdev->msi_enabled = 0;
334 if (radeon_msi_ok(rdev)) {
335 int ret = pci_enable_msi(rdev->pdev);
337 rdev->msi_enabled = 1;
338 dev_info(rdev->dev, "radeon: using MSI.\n");
342 INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
343 INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
344 INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
346 rdev->irq.installed = true;
347 r = radeon_irq_install(rdev, rdev->pdev->irq);
349 rdev->irq.installed = false;
350 flush_delayed_work(&rdev->hotplug_work);
354 DRM_INFO("radeon: irq initialized.\n");
359 * radeon_irq_kms_fini - tear down driver interrupt info
361 * @rdev: radeon device pointer
363 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
365 void radeon_irq_kms_fini(struct radeon_device *rdev)
367 if (rdev->irq.installed) {
368 radeon_irq_uninstall(rdev);
369 rdev->irq.installed = false;
370 if (rdev->msi_enabled)
371 pci_disable_msi(rdev->pdev);
372 flush_delayed_work(&rdev->hotplug_work);
377 * radeon_irq_kms_sw_irq_get - enable software interrupt
379 * @rdev: radeon device pointer
380 * @ring: ring whose interrupt you want to enable
382 * Enables the software interrupt for a specific ring (all asics).
383 * The software interrupt is generally used to signal a fence on
386 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
388 unsigned long irqflags;
390 if (!rdev->irq.installed)
393 if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
394 spin_lock_irqsave(&rdev->irq.lock, irqflags);
395 radeon_irq_set(rdev);
396 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
401 * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
403 * @rdev: radeon device pointer
404 * @ring: ring whose interrupt you want to enable
406 * Enables the software interrupt for a specific ring (all asics).
407 * The software interrupt is generally used to signal a fence on
410 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
412 return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
416 * radeon_irq_kms_sw_irq_put - disable software interrupt
418 * @rdev: radeon device pointer
419 * @ring: ring whose interrupt you want to disable
421 * Disables the software interrupt for a specific ring (all asics).
422 * The software interrupt is generally used to signal a fence on
425 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
427 unsigned long irqflags;
429 if (!rdev->irq.installed)
432 if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
433 spin_lock_irqsave(&rdev->irq.lock, irqflags);
434 radeon_irq_set(rdev);
435 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
440 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
442 * @rdev: radeon device pointer
443 * @crtc: crtc whose interrupt you want to enable
445 * Enables the pageflip interrupt for a specific crtc (all asics).
446 * For pageflips we use the vblank interrupt source.
448 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
450 unsigned long irqflags;
452 if (crtc < 0 || crtc >= rdev->num_crtc)
455 if (!rdev->irq.installed)
458 if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
459 spin_lock_irqsave(&rdev->irq.lock, irqflags);
460 radeon_irq_set(rdev);
461 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
466 * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
468 * @rdev: radeon device pointer
469 * @crtc: crtc whose interrupt you want to disable
471 * Disables the pageflip interrupt for a specific crtc (all asics).
472 * For pageflips we use the vblank interrupt source.
474 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
476 unsigned long irqflags;
478 if (crtc < 0 || crtc >= rdev->num_crtc)
481 if (!rdev->irq.installed)
484 if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
485 spin_lock_irqsave(&rdev->irq.lock, irqflags);
486 radeon_irq_set(rdev);
487 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
492 * radeon_irq_kms_enable_afmt - enable audio format change interrupt
494 * @rdev: radeon device pointer
495 * @block: afmt block whose interrupt you want to enable
497 * Enables the afmt change interrupt for a specific afmt block (all asics).
499 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
501 unsigned long irqflags;
503 if (!rdev->irq.installed)
506 spin_lock_irqsave(&rdev->irq.lock, irqflags);
507 rdev->irq.afmt[block] = true;
508 radeon_irq_set(rdev);
509 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
514 * radeon_irq_kms_disable_afmt - disable audio format change interrupt
516 * @rdev: radeon device pointer
517 * @block: afmt block whose interrupt you want to disable
519 * Disables the afmt change interrupt for a specific afmt block (all asics).
521 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
523 unsigned long irqflags;
525 if (!rdev->irq.installed)
528 spin_lock_irqsave(&rdev->irq.lock, irqflags);
529 rdev->irq.afmt[block] = false;
530 radeon_irq_set(rdev);
531 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
535 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
537 * @rdev: radeon device pointer
538 * @hpd_mask: mask of hpd pins you want to enable.
540 * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
542 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
544 unsigned long irqflags;
547 if (!rdev->irq.installed)
550 spin_lock_irqsave(&rdev->irq.lock, irqflags);
551 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
552 rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
553 radeon_irq_set(rdev);
554 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
558 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
560 * @rdev: radeon device pointer
561 * @hpd_mask: mask of hpd pins you want to disable.
563 * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
565 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
567 unsigned long irqflags;
570 if (!rdev->irq.installed)
573 spin_lock_irqsave(&rdev->irq.lock, irqflags);
574 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
575 rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
576 radeon_irq_set(rdev);
577 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
581 * radeon_irq_kms_set_irq_n_enabled - helper for updating interrupt enable registers
583 * @rdev: radeon device pointer
584 * @reg: the register to write to enable/disable interrupts
585 * @mask: the mask that enables the interrupts
586 * @enable: whether to enable or disable the interrupt register
587 * @name: the name of the interrupt register to print to the kernel log
588 * @n: the number of the interrupt register to print to the kernel log
590 * Helper for updating the enable state of interrupt registers. Checks whether
591 * or not the interrupt matches the enable state we want. If it doesn't, then
592 * we update it and print a debugging message to the kernel log indicating the
593 * new state of the interrupt register.
595 * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
597 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
599 bool enable, const char *name, unsigned n)
601 u32 tmp = RREG32(reg);
603 /* Interrupt state didn't change */
604 if (!!(tmp & mask) == enable)
608 DRM_DEBUG("%s%d interrupts enabled\n", name, n);
609 WREG32(reg, tmp |= mask);
611 DRM_DEBUG("%s%d interrupts disabled\n", name, n);
612 WREG32(reg, tmp & ~mask);