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