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/drm_probe_helper.h>
31 #include <drm/radeon_drm.h>
32 #include "radeon_reg.h"
36 #include <linux/pm_runtime.h>
38 #define RADEON_WAIT_IDLE_TIMEOUT 200
41 * radeon_driver_irq_handler_kms - irq handler for KMS
43 * @int irq, void *arg: args
45 * This is the irq handler for the radeon KMS driver (all asics).
46 * radeon_irq_process is a macro that points to the per-asic
47 * irq handler callback.
49 irqreturn_t
radeon_driver_irq_handler_kms(int irq
, void *arg
)
51 struct drm_device
*dev
= (struct drm_device
*) arg
;
52 struct radeon_device
*rdev
= dev
->dev_private
;
55 ret
= radeon_irq_process(rdev
);
56 if (ret
== IRQ_HANDLED
)
57 pm_runtime_mark_last_busy(dev
->dev
);
62 * Handle hotplug events outside the interrupt handler proper.
65 * radeon_hotplug_work_func - display hotplug work handler
69 * This is the hot plug event work handler (all asics).
70 * The work gets scheduled from the irq handler if there
71 * was a hot plug interrupt. It walks the connector table
72 * and calls the hotplug handler for each one, then sends
73 * a drm hotplug event to alert userspace.
75 static void radeon_hotplug_work_func(struct work_struct
*work
)
77 struct radeon_device
*rdev
= container_of(work
, struct radeon_device
,
79 struct drm_device
*dev
= rdev
->ddev
;
80 struct drm_mode_config
*mode_config
= &dev
->mode_config
;
81 struct drm_connector
*connector
;
83 /* we can race here at startup, some boards seem to trigger
84 * hotplug irqs when they shouldn't. */
85 if (!rdev
->mode_info
.mode_config_initialized
)
88 mutex_lock(&mode_config
->mutex
);
89 list_for_each_entry(connector
, &mode_config
->connector_list
, head
)
90 radeon_connector_hotplug(connector
);
91 mutex_unlock(&mode_config
->mutex
);
92 /* Just fire off a uevent and let userspace tell us what to do */
93 drm_helper_hpd_irq_event(dev
);
96 static void radeon_dp_work_func(struct work_struct
*work
)
98 struct radeon_device
*rdev
= container_of(work
, struct radeon_device
,
100 struct drm_device
*dev
= rdev
->ddev
;
101 struct drm_mode_config
*mode_config
= &dev
->mode_config
;
102 struct drm_connector
*connector
;
104 /* this should take a mutex */
105 list_for_each_entry(connector
, &mode_config
->connector_list
, head
)
106 radeon_connector_hotplug(connector
);
109 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
111 * @dev: drm dev pointer
113 * Gets the hw ready to enable irqs (all asics).
114 * This function disables all interrupt sources on the GPU.
116 void radeon_driver_irq_preinstall_kms(struct drm_device
*dev
)
118 struct radeon_device
*rdev
= dev
->dev_private
;
119 unsigned long irqflags
;
122 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
123 /* Disable *all* interrupts */
124 for (i
= 0; i
< RADEON_NUM_RINGS
; i
++)
125 atomic_set(&rdev
->irq
.ring_int
[i
], 0);
126 rdev
->irq
.dpm_thermal
= false;
127 for (i
= 0; i
< RADEON_MAX_HPD_PINS
; i
++)
128 rdev
->irq
.hpd
[i
] = false;
129 for (i
= 0; i
< RADEON_MAX_CRTCS
; i
++) {
130 rdev
->irq
.crtc_vblank_int
[i
] = false;
131 atomic_set(&rdev
->irq
.pflip
[i
], 0);
132 rdev
->irq
.afmt
[i
] = false;
134 radeon_irq_set(rdev
);
135 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
137 radeon_irq_process(rdev
);
141 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
143 * @dev: drm dev pointer
145 * Handles stuff to be done after enabling irqs (all asics).
146 * Returns 0 on success.
148 int radeon_driver_irq_postinstall_kms(struct drm_device
*dev
)
150 struct radeon_device
*rdev
= dev
->dev_private
;
152 if (ASIC_IS_AVIVO(rdev
))
153 dev
->max_vblank_count
= 0x00ffffff;
155 dev
->max_vblank_count
= 0x001fffff;
161 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
163 * @dev: drm dev pointer
165 * This function disables all interrupt sources on the GPU (all asics).
167 void radeon_driver_irq_uninstall_kms(struct drm_device
*dev
)
169 struct radeon_device
*rdev
= dev
->dev_private
;
170 unsigned long irqflags
;
176 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
177 /* Disable *all* interrupts */
178 for (i
= 0; i
< RADEON_NUM_RINGS
; i
++)
179 atomic_set(&rdev
->irq
.ring_int
[i
], 0);
180 rdev
->irq
.dpm_thermal
= false;
181 for (i
= 0; i
< RADEON_MAX_HPD_PINS
; i
++)
182 rdev
->irq
.hpd
[i
] = false;
183 for (i
= 0; i
< RADEON_MAX_CRTCS
; i
++) {
184 rdev
->irq
.crtc_vblank_int
[i
] = false;
185 atomic_set(&rdev
->irq
.pflip
[i
], 0);
186 rdev
->irq
.afmt
[i
] = false;
188 radeon_irq_set(rdev
);
189 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
193 * radeon_msi_ok - asic specific msi checks
195 * @rdev: radeon device pointer
197 * Handles asic specific MSI checks to determine if
198 * MSIs should be enabled on a particular chip (all asics).
199 * Returns true if MSIs should be enabled, false if MSIs
200 * should not be enabled.
202 static bool radeon_msi_ok(struct radeon_device
*rdev
)
204 /* RV370/RV380 was first asic with MSI support */
205 if (rdev
->family
< CHIP_RV380
)
208 /* MSIs don't work on AGP */
209 if (rdev
->flags
& RADEON_IS_AGP
)
213 * Older chips have a HW limitation, they can only generate 40 bits
214 * of address for "64-bit" MSIs which breaks on some platforms, notably
215 * IBM POWER servers, so we limit them
217 if (rdev
->family
< CHIP_BONAIRE
) {
218 dev_info(rdev
->dev
, "radeon: MSI limited to 32-bit\n");
219 rdev
->pdev
->no_64bit_msi
= 1;
225 else if (radeon_msi
== 0)
229 /* HP RS690 only seems to work with MSIs. */
230 if ((rdev
->pdev
->device
== 0x791f) &&
231 (rdev
->pdev
->subsystem_vendor
== 0x103c) &&
232 (rdev
->pdev
->subsystem_device
== 0x30c2))
235 /* Dell RS690 only seems to work with MSIs. */
236 if ((rdev
->pdev
->device
== 0x791f) &&
237 (rdev
->pdev
->subsystem_vendor
== 0x1028) &&
238 (rdev
->pdev
->subsystem_device
== 0x01fc))
241 /* Dell RS690 only seems to work with MSIs. */
242 if ((rdev
->pdev
->device
== 0x791f) &&
243 (rdev
->pdev
->subsystem_vendor
== 0x1028) &&
244 (rdev
->pdev
->subsystem_device
== 0x01fd))
247 /* Gateway RS690 only seems to work with MSIs. */
248 if ((rdev
->pdev
->device
== 0x791f) &&
249 (rdev
->pdev
->subsystem_vendor
== 0x107b) &&
250 (rdev
->pdev
->subsystem_device
== 0x0185))
253 /* try and enable MSIs by default on all RS690s */
254 if (rdev
->family
== CHIP_RS690
)
257 /* RV515 seems to have MSI issues where it loses
258 * MSI rearms occasionally. This leads to lockups and freezes.
259 * disable it by default.
261 if (rdev
->family
== CHIP_RV515
)
263 if (rdev
->flags
& RADEON_IS_IGP
) {
264 /* APUs work fine with MSIs */
265 if (rdev
->family
>= CHIP_PALM
)
267 /* lots of IGPs have problems with MSIs */
275 * radeon_irq_kms_init - init driver interrupt info
277 * @rdev: radeon device pointer
279 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
280 * Returns 0 for success, error for failure.
282 int radeon_irq_kms_init(struct radeon_device
*rdev
)
286 spin_lock_init(&rdev
->irq
.lock
);
288 /* Disable vblank irqs aggressively for power-saving */
289 rdev
->ddev
->vblank_disable_immediate
= true;
291 r
= drm_vblank_init(rdev
->ddev
, rdev
->num_crtc
);
297 rdev
->msi_enabled
= 0;
299 if (radeon_msi_ok(rdev
)) {
300 int ret
= pci_enable_msi(rdev
->pdev
);
302 rdev
->msi_enabled
= 1;
303 dev_info(rdev
->dev
, "radeon: using MSI.\n");
307 INIT_DELAYED_WORK(&rdev
->hotplug_work
, radeon_hotplug_work_func
);
308 INIT_WORK(&rdev
->dp_work
, radeon_dp_work_func
);
309 INIT_WORK(&rdev
->audio_work
, r600_audio_update_hdmi
);
311 rdev
->irq
.installed
= true;
312 r
= drm_irq_install(rdev
->ddev
, rdev
->ddev
->pdev
->irq
);
314 rdev
->irq
.installed
= false;
315 flush_delayed_work(&rdev
->hotplug_work
);
319 DRM_INFO("radeon: irq initialized.\n");
324 * radeon_irq_kms_fini - tear down driver interrupt info
326 * @rdev: radeon device pointer
328 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
330 void radeon_irq_kms_fini(struct radeon_device
*rdev
)
332 if (rdev
->irq
.installed
) {
333 drm_irq_uninstall(rdev
->ddev
);
334 rdev
->irq
.installed
= false;
335 if (rdev
->msi_enabled
)
336 pci_disable_msi(rdev
->pdev
);
337 flush_delayed_work(&rdev
->hotplug_work
);
342 * radeon_irq_kms_sw_irq_get - enable software interrupt
344 * @rdev: radeon device pointer
345 * @ring: ring whose interrupt you want to enable
347 * Enables the software interrupt for a specific ring (all asics).
348 * The software interrupt is generally used to signal a fence on
351 void radeon_irq_kms_sw_irq_get(struct radeon_device
*rdev
, int ring
)
353 unsigned long irqflags
;
355 if (!rdev
->ddev
->irq_enabled
)
358 if (atomic_inc_return(&rdev
->irq
.ring_int
[ring
]) == 1) {
359 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
360 radeon_irq_set(rdev
);
361 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
366 * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
368 * @rdev: radeon device pointer
369 * @ring: ring whose interrupt you want to enable
371 * Enables the software interrupt for a specific ring (all asics).
372 * The software interrupt is generally used to signal a fence on
375 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device
*rdev
, int ring
)
377 return atomic_inc_return(&rdev
->irq
.ring_int
[ring
]) == 1;
381 * radeon_irq_kms_sw_irq_put - disable software interrupt
383 * @rdev: radeon device pointer
384 * @ring: ring whose interrupt you want to disable
386 * Disables the software interrupt for a specific ring (all asics).
387 * The software interrupt is generally used to signal a fence on
390 void radeon_irq_kms_sw_irq_put(struct radeon_device
*rdev
, int ring
)
392 unsigned long irqflags
;
394 if (!rdev
->ddev
->irq_enabled
)
397 if (atomic_dec_and_test(&rdev
->irq
.ring_int
[ring
])) {
398 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
399 radeon_irq_set(rdev
);
400 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
405 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
407 * @rdev: radeon device pointer
408 * @crtc: crtc whose interrupt you want to enable
410 * Enables the pageflip interrupt for a specific crtc (all asics).
411 * For pageflips we use the vblank interrupt source.
413 void radeon_irq_kms_pflip_irq_get(struct radeon_device
*rdev
, int crtc
)
415 unsigned long irqflags
;
417 if (crtc
< 0 || crtc
>= rdev
->num_crtc
)
420 if (!rdev
->ddev
->irq_enabled
)
423 if (atomic_inc_return(&rdev
->irq
.pflip
[crtc
]) == 1) {
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_put - disable pageflip interrupt
433 * @rdev: radeon device pointer
434 * @crtc: crtc whose interrupt you want to disable
436 * Disables 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_put(struct radeon_device
*rdev
, int crtc
)
441 unsigned long irqflags
;
443 if (crtc
< 0 || crtc
>= rdev
->num_crtc
)
446 if (!rdev
->ddev
->irq_enabled
)
449 if (atomic_dec_and_test(&rdev
->irq
.pflip
[crtc
])) {
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_enable_afmt - enable audio format change interrupt
459 * @rdev: radeon device pointer
460 * @block: afmt block whose interrupt you want to enable
462 * Enables the afmt change interrupt for a specific afmt block (all asics).
464 void radeon_irq_kms_enable_afmt(struct radeon_device
*rdev
, int block
)
466 unsigned long irqflags
;
468 if (!rdev
->ddev
->irq_enabled
)
471 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
472 rdev
->irq
.afmt
[block
] = true;
473 radeon_irq_set(rdev
);
474 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
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 unsigned long irqflags
;
490 if (!rdev
->ddev
->irq_enabled
)
493 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
494 rdev
->irq
.afmt
[block
] = false;
495 radeon_irq_set(rdev
);
496 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
500 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
502 * @rdev: radeon device pointer
503 * @hpd_mask: mask of hpd pins you want to enable.
505 * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
507 void radeon_irq_kms_enable_hpd(struct radeon_device
*rdev
, unsigned hpd_mask
)
509 unsigned long irqflags
;
512 if (!rdev
->ddev
->irq_enabled
)
515 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
516 for (i
= 0; i
< RADEON_MAX_HPD_PINS
; ++i
)
517 rdev
->irq
.hpd
[i
] |= !!(hpd_mask
& (1 << i
));
518 radeon_irq_set(rdev
);
519 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
523 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
525 * @rdev: radeon device pointer
526 * @hpd_mask: mask of hpd pins you want to disable.
528 * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
530 void radeon_irq_kms_disable_hpd(struct radeon_device
*rdev
, unsigned hpd_mask
)
532 unsigned long irqflags
;
535 if (!rdev
->ddev
->irq_enabled
)
538 spin_lock_irqsave(&rdev
->irq
.lock
, irqflags
);
539 for (i
= 0; i
< RADEON_MAX_HPD_PINS
; ++i
)
540 rdev
->irq
.hpd
[i
] &= !(hpd_mask
& (1 << i
));
541 radeon_irq_set(rdev
);
542 spin_unlock_irqrestore(&rdev
->irq
.lock
, irqflags
);
546 * radeon_irq_kms_update_int_n - helper for updating interrupt enable registers
548 * @rdev: radeon device pointer
549 * @reg: the register to write to enable/disable interrupts
550 * @mask: the mask that enables the interrupts
551 * @enable: whether to enable or disable the interrupt register
552 * @name: the name of the interrupt register to print to the kernel log
553 * @num: the number of the interrupt register to print to the kernel log
555 * Helper for updating the enable state of interrupt registers. Checks whether
556 * or not the interrupt matches the enable state we want. If it doesn't, then
557 * we update it and print a debugging message to the kernel log indicating the
558 * new state of the interrupt register.
560 * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
562 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device
*rdev
,
564 bool enable
, const char *name
, unsigned n
)
566 u32 tmp
= RREG32(reg
);
568 /* Interrupt state didn't change */
569 if (!!(tmp
& mask
) == enable
)
573 DRM_DEBUG("%s%d interrupts enabled\n", name
, n
);
574 WREG32(reg
, tmp
|= mask
);
576 DRM_DEBUG("%s%d interrupts disabled\n", name
, n
);
577 WREG32(reg
, tmp
& ~mask
);