Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2008 Advanced Micro Devices, Inc.
0003  * Copyright 2008 Red Hat Inc.
0004  * Copyright 2009 Jerome Glisse.
0005  *
0006  * Permission is hereby granted, free of charge, to any person obtaining a
0007  * copy of this software and associated documentation files (the "Software"),
0008  * to deal in the Software without restriction, including without limitation
0009  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010  * and/or sell copies of the Software, and to permit persons to whom the
0011  * Software is furnished to do so, subject to the following conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be included in
0014  * all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0019  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0020  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0021  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0022  * OTHER DEALINGS IN THE SOFTWARE.
0023  *
0024  * Authors: Dave Airlie
0025  *          Alex Deucher
0026  *          Jerome Glisse
0027  */
0028 
0029 #include <linux/pci.h>
0030 #include <linux/pm_runtime.h>
0031 
0032 #include <drm/drm_crtc_helper.h>
0033 #include <drm/drm_device.h>
0034 #include <drm/drm_drv.h>
0035 #include <drm/drm_probe_helper.h>
0036 #include <drm/drm_vblank.h>
0037 #include <drm/radeon_drm.h>
0038 
0039 #include "atom.h"
0040 #include "radeon.h"
0041 #include "radeon_kms.h"
0042 #include "radeon_reg.h"
0043 
0044 
0045 #define RADEON_WAIT_IDLE_TIMEOUT 200
0046 
0047 /*
0048  * radeon_driver_irq_handler_kms - irq handler for KMS
0049  *
0050  * This is the irq handler for the radeon KMS driver (all asics).
0051  * radeon_irq_process is a macro that points to the per-asic
0052  * irq handler callback.
0053  */
0054 static irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
0055 {
0056     struct drm_device *dev = (struct drm_device *) arg;
0057     struct radeon_device *rdev = dev->dev_private;
0058     irqreturn_t ret;
0059 
0060     ret = radeon_irq_process(rdev);
0061     if (ret == IRQ_HANDLED)
0062         pm_runtime_mark_last_busy(dev->dev);
0063     return ret;
0064 }
0065 
0066 /*
0067  * Handle hotplug events outside the interrupt handler proper.
0068  */
0069 /**
0070  * radeon_hotplug_work_func - display hotplug work handler
0071  *
0072  * @work: work struct
0073  *
0074  * This is the hot plug event work handler (all asics).
0075  * The work gets scheduled from the irq handler if there
0076  * was a hot plug interrupt.  It walks the connector table
0077  * and calls the hotplug handler for each one, then sends
0078  * a drm hotplug event to alert userspace.
0079  */
0080 static void radeon_hotplug_work_func(struct work_struct *work)
0081 {
0082     struct radeon_device *rdev = container_of(work, struct radeon_device,
0083                           hotplug_work.work);
0084     struct drm_device *dev = rdev->ddev;
0085     struct drm_mode_config *mode_config = &dev->mode_config;
0086     struct drm_connector *connector;
0087 
0088     /* we can race here at startup, some boards seem to trigger
0089      * hotplug irqs when they shouldn't. */
0090     if (!rdev->mode_info.mode_config_initialized)
0091         return;
0092 
0093     mutex_lock(&mode_config->mutex);
0094     list_for_each_entry(connector, &mode_config->connector_list, head)
0095         radeon_connector_hotplug(connector);
0096     mutex_unlock(&mode_config->mutex);
0097     /* Just fire off a uevent and let userspace tell us what to do */
0098     drm_helper_hpd_irq_event(dev);
0099 }
0100 
0101 static void radeon_dp_work_func(struct work_struct *work)
0102 {
0103     struct radeon_device *rdev = container_of(work, struct radeon_device,
0104                           dp_work);
0105     struct drm_device *dev = rdev->ddev;
0106     struct drm_mode_config *mode_config = &dev->mode_config;
0107     struct drm_connector *connector;
0108 
0109     /* this should take a mutex */
0110     list_for_each_entry(connector, &mode_config->connector_list, head)
0111         radeon_connector_hotplug(connector);
0112 }
0113 /**
0114  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
0115  *
0116  * @dev: drm dev pointer
0117  *
0118  * Gets the hw ready to enable irqs (all asics).
0119  * This function disables all interrupt sources on the GPU.
0120  */
0121 static void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
0122 {
0123     struct radeon_device *rdev = dev->dev_private;
0124     unsigned long irqflags;
0125     unsigned i;
0126 
0127     spin_lock_irqsave(&rdev->irq.lock, irqflags);
0128     /* Disable *all* interrupts */
0129     for (i = 0; i < RADEON_NUM_RINGS; i++)
0130         atomic_set(&rdev->irq.ring_int[i], 0);
0131     rdev->irq.dpm_thermal = false;
0132     for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
0133         rdev->irq.hpd[i] = false;
0134     for (i = 0; i < RADEON_MAX_CRTCS; i++) {
0135         rdev->irq.crtc_vblank_int[i] = false;
0136         atomic_set(&rdev->irq.pflip[i], 0);
0137         rdev->irq.afmt[i] = false;
0138     }
0139     radeon_irq_set(rdev);
0140     spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0141     /* Clear bits */
0142     radeon_irq_process(rdev);
0143 }
0144 
0145 /**
0146  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
0147  *
0148  * @dev: drm dev pointer
0149  *
0150  * Handles stuff to be done after enabling irqs (all asics).
0151  * Returns 0 on success.
0152  */
0153 static int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
0154 {
0155     struct radeon_device *rdev = dev->dev_private;
0156 
0157     if (ASIC_IS_AVIVO(rdev))
0158         dev->max_vblank_count = 0x00ffffff;
0159     else
0160         dev->max_vblank_count = 0x001fffff;
0161 
0162     return 0;
0163 }
0164 
0165 /**
0166  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
0167  *
0168  * @dev: drm dev pointer
0169  *
0170  * This function disables all interrupt sources on the GPU (all asics).
0171  */
0172 static void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
0173 {
0174     struct radeon_device *rdev = dev->dev_private;
0175     unsigned long irqflags;
0176     unsigned i;
0177 
0178     if (rdev == NULL) {
0179         return;
0180     }
0181     spin_lock_irqsave(&rdev->irq.lock, irqflags);
0182     /* Disable *all* interrupts */
0183     for (i = 0; i < RADEON_NUM_RINGS; i++)
0184         atomic_set(&rdev->irq.ring_int[i], 0);
0185     rdev->irq.dpm_thermal = false;
0186     for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
0187         rdev->irq.hpd[i] = false;
0188     for (i = 0; i < RADEON_MAX_CRTCS; i++) {
0189         rdev->irq.crtc_vblank_int[i] = false;
0190         atomic_set(&rdev->irq.pflip[i], 0);
0191         rdev->irq.afmt[i] = false;
0192     }
0193     radeon_irq_set(rdev);
0194     spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0195 }
0196 
0197 static int radeon_irq_install(struct radeon_device *rdev, int irq)
0198 {
0199     struct drm_device *dev = rdev->ddev;
0200     int ret;
0201 
0202     if (irq == IRQ_NOTCONNECTED)
0203         return -ENOTCONN;
0204 
0205     radeon_driver_irq_preinstall_kms(dev);
0206 
0207     /* PCI devices require shared interrupts. */
0208     ret = request_irq(irq, radeon_driver_irq_handler_kms,
0209               IRQF_SHARED, dev->driver->name, dev);
0210     if (ret)
0211         return ret;
0212 
0213     radeon_driver_irq_postinstall_kms(dev);
0214 
0215     return 0;
0216 }
0217 
0218 static void radeon_irq_uninstall(struct radeon_device *rdev)
0219 {
0220     struct drm_device *dev = rdev->ddev;
0221     struct pci_dev *pdev = to_pci_dev(dev->dev);
0222 
0223     radeon_driver_irq_uninstall_kms(dev);
0224     free_irq(pdev->irq, dev);
0225 }
0226 
0227 /**
0228  * radeon_msi_ok - asic specific msi checks
0229  *
0230  * @rdev: radeon device pointer
0231  *
0232  * Handles asic specific MSI checks to determine if
0233  * MSIs should be enabled on a particular chip (all asics).
0234  * Returns true if MSIs should be enabled, false if MSIs
0235  * should not be enabled.
0236  */
0237 static bool radeon_msi_ok(struct radeon_device *rdev)
0238 {
0239     /* RV370/RV380 was first asic with MSI support */
0240     if (rdev->family < CHIP_RV380)
0241         return false;
0242 
0243     /* MSIs don't work on AGP */
0244     if (rdev->flags & RADEON_IS_AGP)
0245         return false;
0246 
0247     /*
0248      * Older chips have a HW limitation, they can only generate 40 bits
0249      * of address for "64-bit" MSIs which breaks on some platforms, notably
0250      * IBM POWER servers, so we limit them
0251      */
0252     if (rdev->family < CHIP_BONAIRE) {
0253         dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
0254         rdev->pdev->no_64bit_msi = 1;
0255     }
0256 
0257     /* force MSI on */
0258     if (radeon_msi == 1)
0259         return true;
0260     else if (radeon_msi == 0)
0261         return false;
0262 
0263     /* Quirks */
0264     /* HP RS690 only seems to work with MSIs. */
0265     if ((rdev->pdev->device == 0x791f) &&
0266         (rdev->pdev->subsystem_vendor == 0x103c) &&
0267         (rdev->pdev->subsystem_device == 0x30c2))
0268         return true;
0269 
0270     /* Dell RS690 only seems to work with MSIs. */
0271     if ((rdev->pdev->device == 0x791f) &&
0272         (rdev->pdev->subsystem_vendor == 0x1028) &&
0273         (rdev->pdev->subsystem_device == 0x01fc))
0274         return true;
0275 
0276     /* Dell RS690 only seems to work with MSIs. */
0277     if ((rdev->pdev->device == 0x791f) &&
0278         (rdev->pdev->subsystem_vendor == 0x1028) &&
0279         (rdev->pdev->subsystem_device == 0x01fd))
0280         return true;
0281 
0282     /* Gateway RS690 only seems to work with MSIs. */
0283     if ((rdev->pdev->device == 0x791f) &&
0284         (rdev->pdev->subsystem_vendor == 0x107b) &&
0285         (rdev->pdev->subsystem_device == 0x0185))
0286         return true;
0287 
0288     /* try and enable MSIs by default on all RS690s */
0289     if (rdev->family == CHIP_RS690)
0290         return true;
0291 
0292     /* RV515 seems to have MSI issues where it loses
0293      * MSI rearms occasionally. This leads to lockups and freezes.
0294      * disable it by default.
0295      */
0296     if (rdev->family == CHIP_RV515)
0297         return false;
0298     if (rdev->flags & RADEON_IS_IGP) {
0299         /* APUs work fine with MSIs */
0300         if (rdev->family >= CHIP_PALM)
0301             return true;
0302         /* lots of IGPs have problems with MSIs */
0303         return false;
0304     }
0305 
0306     return true;
0307 }
0308 
0309 /**
0310  * radeon_irq_kms_init - init driver interrupt info
0311  *
0312  * @rdev: radeon device pointer
0313  *
0314  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
0315  * Returns 0 for success, error for failure.
0316  */
0317 int radeon_irq_kms_init(struct radeon_device *rdev)
0318 {
0319     int r = 0;
0320 
0321     spin_lock_init(&rdev->irq.lock);
0322 
0323     /* Disable vblank irqs aggressively for power-saving */
0324     rdev->ddev->vblank_disable_immediate = true;
0325 
0326     r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
0327     if (r) {
0328         return r;
0329     }
0330 
0331     /* enable msi */
0332     rdev->msi_enabled = 0;
0333 
0334     if (radeon_msi_ok(rdev)) {
0335         int ret = pci_enable_msi(rdev->pdev);
0336         if (!ret) {
0337             rdev->msi_enabled = 1;
0338             dev_info(rdev->dev, "radeon: using MSI.\n");
0339         }
0340     }
0341 
0342     INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
0343     INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
0344     INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
0345 
0346     rdev->irq.installed = true;
0347     r = radeon_irq_install(rdev, rdev->pdev->irq);
0348     if (r) {
0349         rdev->irq.installed = false;
0350         flush_delayed_work(&rdev->hotplug_work);
0351         return r;
0352     }
0353 
0354     DRM_INFO("radeon: irq initialized.\n");
0355     return 0;
0356 }
0357 
0358 /**
0359  * radeon_irq_kms_fini - tear down driver interrupt info
0360  *
0361  * @rdev: radeon device pointer
0362  *
0363  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
0364  */
0365 void radeon_irq_kms_fini(struct radeon_device *rdev)
0366 {
0367     if (rdev->irq.installed) {
0368         radeon_irq_uninstall(rdev);
0369         rdev->irq.installed = false;
0370         if (rdev->msi_enabled)
0371             pci_disable_msi(rdev->pdev);
0372         flush_delayed_work(&rdev->hotplug_work);
0373     }
0374 }
0375 
0376 /**
0377  * radeon_irq_kms_sw_irq_get - enable software interrupt
0378  *
0379  * @rdev: radeon device pointer
0380  * @ring: ring whose interrupt you want to enable
0381  *
0382  * Enables the software interrupt for a specific ring (all asics).
0383  * The software interrupt is generally used to signal a fence on
0384  * a particular ring.
0385  */
0386 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
0387 {
0388     unsigned long irqflags;
0389 
0390     if (!rdev->irq.installed)
0391         return;
0392 
0393     if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
0394         spin_lock_irqsave(&rdev->irq.lock, irqflags);
0395         radeon_irq_set(rdev);
0396         spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0397     }
0398 }
0399 
0400 /**
0401  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
0402  *
0403  * @rdev: radeon device pointer
0404  * @ring: ring whose interrupt you want to enable
0405  *
0406  * Enables the software interrupt for a specific ring (all asics).
0407  * The software interrupt is generally used to signal a fence on
0408  * a particular ring.
0409  */
0410 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
0411 {
0412     return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
0413 }
0414 
0415 /**
0416  * radeon_irq_kms_sw_irq_put - disable software interrupt
0417  *
0418  * @rdev: radeon device pointer
0419  * @ring: ring whose interrupt you want to disable
0420  *
0421  * Disables the software interrupt for a specific ring (all asics).
0422  * The software interrupt is generally used to signal a fence on
0423  * a particular ring.
0424  */
0425 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
0426 {
0427     unsigned long irqflags;
0428 
0429     if (!rdev->irq.installed)
0430         return;
0431 
0432     if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
0433         spin_lock_irqsave(&rdev->irq.lock, irqflags);
0434         radeon_irq_set(rdev);
0435         spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0436     }
0437 }
0438 
0439 /**
0440  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
0441  *
0442  * @rdev: radeon device pointer
0443  * @crtc: crtc whose interrupt you want to enable
0444  *
0445  * Enables the pageflip interrupt for a specific crtc (all asics).
0446  * For pageflips we use the vblank interrupt source.
0447  */
0448 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
0449 {
0450     unsigned long irqflags;
0451 
0452     if (crtc < 0 || crtc >= rdev->num_crtc)
0453         return;
0454 
0455     if (!rdev->irq.installed)
0456         return;
0457 
0458     if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
0459         spin_lock_irqsave(&rdev->irq.lock, irqflags);
0460         radeon_irq_set(rdev);
0461         spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0462     }
0463 }
0464 
0465 /**
0466  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
0467  *
0468  * @rdev: radeon device pointer
0469  * @crtc: crtc whose interrupt you want to disable
0470  *
0471  * Disables the pageflip interrupt for a specific crtc (all asics).
0472  * For pageflips we use the vblank interrupt source.
0473  */
0474 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
0475 {
0476     unsigned long irqflags;
0477 
0478     if (crtc < 0 || crtc >= rdev->num_crtc)
0479         return;
0480 
0481     if (!rdev->irq.installed)
0482         return;
0483 
0484     if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
0485         spin_lock_irqsave(&rdev->irq.lock, irqflags);
0486         radeon_irq_set(rdev);
0487         spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0488     }
0489 }
0490 
0491 /**
0492  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
0493  *
0494  * @rdev: radeon device pointer
0495  * @block: afmt block whose interrupt you want to enable
0496  *
0497  * Enables the afmt change interrupt for a specific afmt block (all asics).
0498  */
0499 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
0500 {
0501     unsigned long irqflags;
0502 
0503     if (!rdev->irq.installed)
0504         return;
0505 
0506     spin_lock_irqsave(&rdev->irq.lock, irqflags);
0507     rdev->irq.afmt[block] = true;
0508     radeon_irq_set(rdev);
0509     spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0510 
0511 }
0512 
0513 /**
0514  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
0515  *
0516  * @rdev: radeon device pointer
0517  * @block: afmt block whose interrupt you want to disable
0518  *
0519  * Disables the afmt change interrupt for a specific afmt block (all asics).
0520  */
0521 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
0522 {
0523     unsigned long irqflags;
0524 
0525     if (!rdev->irq.installed)
0526         return;
0527 
0528     spin_lock_irqsave(&rdev->irq.lock, irqflags);
0529     rdev->irq.afmt[block] = false;
0530     radeon_irq_set(rdev);
0531     spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0532 }
0533 
0534 /**
0535  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
0536  *
0537  * @rdev: radeon device pointer
0538  * @hpd_mask: mask of hpd pins you want to enable.
0539  *
0540  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
0541  */
0542 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
0543 {
0544     unsigned long irqflags;
0545     int i;
0546 
0547     if (!rdev->irq.installed)
0548         return;
0549 
0550     spin_lock_irqsave(&rdev->irq.lock, irqflags);
0551     for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
0552         rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
0553     radeon_irq_set(rdev);
0554     spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0555 }
0556 
0557 /**
0558  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
0559  *
0560  * @rdev: radeon device pointer
0561  * @hpd_mask: mask of hpd pins you want to disable.
0562  *
0563  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
0564  */
0565 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
0566 {
0567     unsigned long irqflags;
0568     int i;
0569 
0570     if (!rdev->irq.installed)
0571         return;
0572 
0573     spin_lock_irqsave(&rdev->irq.lock, irqflags);
0574     for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
0575         rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
0576     radeon_irq_set(rdev);
0577     spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
0578 }
0579 
0580 /**
0581  * radeon_irq_kms_set_irq_n_enabled - helper for updating interrupt enable registers
0582  *
0583  * @rdev: radeon device pointer
0584  * @reg: the register to write to enable/disable interrupts
0585  * @mask: the mask that enables the interrupts
0586  * @enable: whether to enable or disable the interrupt register
0587  * @name: the name of the interrupt register to print to the kernel log
0588  * @n: the number of the interrupt register to print to the kernel log
0589  *
0590  * Helper for updating the enable state of interrupt registers. Checks whether
0591  * or not the interrupt matches the enable state we want. If it doesn't, then
0592  * we update it and print a debugging message to the kernel log indicating the
0593  * new state of the interrupt register.
0594  *
0595  * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
0596  */
0597 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
0598                       u32 reg, u32 mask,
0599                       bool enable, const char *name, unsigned n)
0600 {
0601     u32 tmp = RREG32(reg);
0602 
0603     /* Interrupt state didn't change */
0604     if (!!(tmp & mask) == enable)
0605         return;
0606 
0607     if (enable) {
0608         DRM_DEBUG("%s%d interrupts enabled\n", name, n);
0609         WREG32(reg, tmp |= mask);
0610     } else {
0611         DRM_DEBUG("%s%d interrupts disabled\n", name, n);
0612         WREG32(reg, tmp & ~mask);
0613     }
0614 }