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 <drm/drm_crtc_helper.h>
30 #include <drm/radeon_drm.h>
31 #include "radeon_reg.h"
32 #include "radeon_irq_kms.h"
37 #include <linux/pm_runtime.h>
40 #define RADEON_WAIT_IDLE_TIMEOUT 200
43 * radeon_driver_irq_handler_kms - irq handler for KMS
47 * This is the irq handler for the radeon KMS driver (all asics).
48 * radeon_irq_process is a macro that points to the per-asic
49 * irq handler callback.
51 irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
53 struct drm_device *dev = (struct drm_device *) arg;
54 struct radeon_device *rdev = dev->dev_private;
57 ret = radeon_irq_process(rdev);
59 if (ret == IRQ_HANDLED)
60 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 if (mode_config->num_connector) {
94 list_for_each_entry(connector, &mode_config->connector_list, head)
95 radeon_connector_hotplug(connector);
97 mutex_unlock(&mode_config->mutex);
98 /* Just fire off a uevent and let userspace tell us what to do */
99 drm_helper_hpd_irq_event(dev);
102 static void radeon_dp_work_func(struct work_struct *work)
104 struct radeon_device *rdev = container_of(work, struct radeon_device,
106 struct drm_device *dev = rdev->ddev;
107 struct drm_mode_config *mode_config = &dev->mode_config;
108 struct drm_connector *connector;
110 /* this should take a mutex */
111 if (mode_config->num_connector) {
112 list_for_each_entry(connector, &mode_config->connector_list, head)
113 radeon_connector_hotplug(connector);
117 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
119 * @dev: drm dev pointer
121 * Gets the hw ready to enable irqs (all asics).
122 * This function disables all interrupt sources on the GPU.
124 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
126 struct radeon_device *rdev = dev->dev_private;
129 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
130 /* Disable *all* interrupts */
131 for (i = 0; i < RADEON_NUM_RINGS; i++)
132 atomic_set(&rdev->irq.ring_int[i], 0);
133 rdev->irq.dpm_thermal = false;
134 for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
135 rdev->irq.hpd[i] = false;
136 for (i = 0; i < RADEON_MAX_CRTCS; i++) {
137 rdev->irq.crtc_vblank_int[i] = false;
138 atomic_set(&rdev->irq.pflip[i], 0);
139 rdev->irq.afmt[i] = false;
141 radeon_irq_set(rdev);
142 lockmgr(&rdev->irq.lock, LK_RELEASE);
144 radeon_irq_process(rdev);
148 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
150 * @dev: drm dev pointer
152 * Handles stuff to be done after enabling irqs (all asics).
153 * Returns 0 on success.
155 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
157 struct radeon_device *rdev = dev->dev_private;
159 if (ASIC_IS_AVIVO(rdev))
160 dev->max_vblank_count = 0x00ffffff;
162 dev->max_vblank_count = 0x001fffff;
168 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
170 * @dev: drm dev pointer
172 * This function disables all interrupt sources on the GPU (all asics).
174 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
176 struct radeon_device *rdev = dev->dev_private;
182 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
183 /* Disable *all* interrupts */
184 for (i = 0; i < RADEON_NUM_RINGS; i++)
185 atomic_set(&rdev->irq.ring_int[i], 0);
186 rdev->irq.dpm_thermal = false;
187 for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
188 rdev->irq.hpd[i] = false;
189 for (i = 0; i < RADEON_MAX_CRTCS; i++) {
190 rdev->irq.crtc_vblank_int[i] = false;
191 atomic_set(&rdev->irq.pflip[i], 0);
192 rdev->irq.afmt[i] = false;
194 radeon_irq_set(rdev);
195 lockmgr(&rdev->irq.lock, LK_RELEASE);
199 * radeon_msi_ok - asic specific msi checks
201 * @rdev: radeon device pointer
203 * Handles asic specific MSI checks to determine if
204 * MSIs should be enabled on a particular chip (all asics).
205 * Returns true if MSIs should be enabled, false if MSIs
206 * should not be enabled.
208 int radeon_msi_ok(struct drm_device *rdev, unsigned long flags)
212 family = flags & RADEON_FAMILY_MASK;
214 /* RV370/RV380 was first asic with MSI support */
215 if (family < CHIP_RV380)
218 /* MSIs don't work on AGP */
219 if (drm_pci_device_is_agp(rdev))
223 * Older chips have a HW limitation, they can only generate 40 bits
224 * of address for "64-bit" MSIs which breaks on some platforms, notably
225 * IBM POWER servers, so we limit them
227 if (family < CHIP_BONAIRE) {
228 dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
229 rdev->pdev->no_64bit_msi = 1;
235 else if (radeon_msi == 0)
239 /* HP RS690 only seems to work with MSIs. */
240 if ((rdev->pdev->device == 0x791f) &&
241 (rdev->pdev->subsystem_vendor == 0x103c) &&
242 (rdev->pdev->subsystem_device == 0x30c2))
245 /* Dell RS690 only seems to work with MSIs. */
246 if ((rdev->pdev->device == 0x791f) &&
247 (rdev->pdev->subsystem_vendor == 0x1028) &&
248 (rdev->pdev->subsystem_device == 0x01fc))
251 /* Dell RS690 only seems to work with MSIs. */
252 if ((rdev->pdev->device == 0x791f) &&
253 (rdev->pdev->subsystem_vendor == 0x1028) &&
254 (rdev->pdev->subsystem_device == 0x01fd))
257 /* Gateway RS690 only seems to work with MSIs. */
258 if ((rdev->pdev->device == 0x791f) &&
259 (rdev->pdev->subsystem_vendor == 0x107b) &&
260 (rdev->pdev->subsystem_device == 0x0185))
263 /* try and enable MSIs by default on all RS690s */
264 if (family == CHIP_RS690)
267 /* RV515 seems to have MSI issues where it loses
268 * MSI rearms occasionally. This leads to lockups and freezes.
269 * disable it by default.
271 if (family == CHIP_RV515)
273 if (flags & RADEON_IS_IGP) {
274 /* APUs work fine with MSIs */
275 if (family >= CHIP_PALM)
277 /* lots of IGPs have problems with MSIs */
285 * radeon_irq_kms_init - init driver interrupt info
287 * @rdev: radeon device pointer
289 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
290 * Returns 0 for success, error for failure.
292 int radeon_irq_kms_init(struct radeon_device *rdev)
296 lockinit(&rdev->irq.lock, "drdil", 0, LK_CANRECURSE);
297 r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
303 rdev->msi_enabled = (rdev->ddev->pdev->_irq_type == PCI_INTR_TYPE_MSI);
305 #ifndef __DragonFly__
306 if (radeon_msi_ok(rdev)) {
307 int ret = pci_enable_msi(rdev->pdev);
309 rdev->msi_enabled = 1;
310 dev_info(rdev->dev, "radeon: using MSI.\n");
315 INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
316 INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
317 INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
319 rdev->irq.installed = true;
320 r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
322 rdev->irq.installed = false;
323 flush_delayed_work(&rdev->hotplug_work);
327 DRM_INFO("radeon: irq initialized.\n");
332 * radeon_irq_kms_fini - tear down driver interrupt info
334 * @rdev: radeon device pointer
336 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
338 void radeon_irq_kms_fini(struct radeon_device *rdev)
340 drm_vblank_cleanup(rdev->ddev);
341 if (rdev->irq.installed) {
342 drm_irq_uninstall(rdev->ddev);
343 rdev->irq.installed = false;
344 #ifndef __DragonFly__
345 if (rdev->msi_enabled)
346 pci_disable_msi(rdev->pdev);
348 flush_delayed_work(&rdev->hotplug_work);
353 * radeon_irq_kms_sw_irq_get - enable software interrupt
355 * @rdev: radeon device pointer
356 * @ring: ring whose interrupt you want to enable
358 * Enables the software interrupt for a specific ring (all asics).
359 * The software interrupt is generally used to signal a fence on
362 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
364 if (!rdev->ddev->irq_enabled)
367 if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
368 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
369 radeon_irq_set(rdev);
370 lockmgr(&rdev->irq.lock, LK_RELEASE);
375 * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
377 * @rdev: radeon device pointer
378 * @ring: ring whose interrupt you want to enable
380 * Enables the software interrupt for a specific ring (all asics).
381 * The software interrupt is generally used to signal a fence on
384 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
386 return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
390 * radeon_irq_kms_sw_irq_put - disable software interrupt
392 * @rdev: radeon device pointer
393 * @ring: ring whose interrupt you want to disable
395 * Disables the software interrupt for a specific ring (all asics).
396 * The software interrupt is generally used to signal a fence on
399 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
401 if (!rdev->ddev->irq_enabled)
404 if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
405 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
406 radeon_irq_set(rdev);
407 lockmgr(&rdev->irq.lock, LK_RELEASE);
412 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
414 * @rdev: radeon device pointer
415 * @crtc: crtc whose interrupt you want to enable
417 * Enables the pageflip interrupt for a specific crtc (all asics).
418 * For pageflips we use the vblank interrupt source.
420 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
422 if (crtc < 0 || crtc >= rdev->num_crtc)
425 if (!rdev->ddev->irq_enabled)
428 if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
429 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
430 radeon_irq_set(rdev);
431 lockmgr(&rdev->irq.lock, LK_RELEASE);
436 * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
438 * @rdev: radeon device pointer
439 * @crtc: crtc whose interrupt you want to disable
441 * Disables the pageflip interrupt for a specific crtc (all asics).
442 * For pageflips we use the vblank interrupt source.
444 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
446 if (crtc < 0 || crtc >= rdev->num_crtc)
449 if (!rdev->ddev->irq_enabled)
452 if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
453 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
454 radeon_irq_set(rdev);
455 lockmgr(&rdev->irq.lock, LK_RELEASE);
460 * radeon_irq_kms_enable_afmt - enable audio format change interrupt
462 * @rdev: radeon device pointer
463 * @block: afmt block whose interrupt you want to enable
465 * Enables the afmt change interrupt for a specific afmt block (all asics).
467 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
469 if (!rdev->ddev->irq_enabled)
472 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
473 rdev->irq.afmt[block] = true;
474 radeon_irq_set(rdev);
475 lockmgr(&rdev->irq.lock, LK_RELEASE);
479 * radeon_irq_kms_disable_afmt - disable audio format change interrupt
481 * @rdev: radeon device pointer
482 * @block: afmt block whose interrupt you want to disable
484 * Disables the afmt change interrupt for a specific afmt block (all asics).
486 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
488 if (!rdev->ddev->irq_enabled)
491 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
492 rdev->irq.afmt[block] = false;
493 radeon_irq_set(rdev);
494 lockmgr(&rdev->irq.lock, LK_RELEASE);
498 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
500 * @rdev: radeon device pointer
501 * @hpd_mask: mask of hpd pins you want to enable.
503 * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
505 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
509 if (!rdev->ddev->irq_enabled)
512 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
513 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
514 rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
515 radeon_irq_set(rdev);
516 lockmgr(&rdev->irq.lock, LK_RELEASE);
520 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
522 * @rdev: radeon device pointer
523 * @hpd_mask: mask of hpd pins you want to disable.
525 * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
527 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
531 if (!rdev->ddev->irq_enabled)
534 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
535 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
536 rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
537 radeon_irq_set(rdev);
538 lockmgr(&rdev->irq.lock, LK_RELEASE);