Merge remote branch 'airlied/drm-next' into drm-intel-next
[linux-2.6.git] / drivers / gpu / drm / radeon / radeon_device.c
index f23083b..e3f9edf 100644 (file)
@@ -29,6 +29,7 @@
 #include <drm/drmP.h>
 #include <drm/drm_crtc_helper.h>
 #include <drm/radeon_drm.h>
+#include <linux/vgaarb.h>
 #include "radeon_reg.h"
 #include "radeon.h"
 #include "radeon_asic.h"
@@ -37,7 +38,7 @@
 /*
  * Clear GPU surface registers.
  */
-static void radeon_surface_init(struct radeon_device *rdev)
+void radeon_surface_init(struct radeon_device *rdev)
 {
        /* FIXME: check this out */
        if (rdev->family < CHIP_R600) {
@@ -56,7 +57,7 @@ static void radeon_surface_init(struct radeon_device *rdev)
 /*
  * GPU scratch registers helpers function.
  */
-static void radeon_scratch_init(struct radeon_device *rdev)
+void radeon_scratch_init(struct radeon_device *rdev)
 {
        int i;
 
@@ -121,7 +122,7 @@ int radeon_mc_setup(struct radeon_device *rdev)
        if (rdev->mc.vram_location != 0xFFFFFFFFUL) {
                /* vram location was already setup try to put gtt after
                 * if it fits */
-               tmp = rdev->mc.vram_location + rdev->mc.vram_size;
+               tmp = rdev->mc.vram_location + rdev->mc.mc_vram_size;
                tmp = (tmp + rdev->mc.gtt_size - 1) & ~(rdev->mc.gtt_size - 1);
                if ((0xFFFFFFFFUL - tmp) >= rdev->mc.gtt_size) {
                        rdev->mc.gtt_location = tmp;
@@ -136,13 +137,13 @@ int radeon_mc_setup(struct radeon_device *rdev)
        } else if (rdev->mc.gtt_location != 0xFFFFFFFFUL) {
                /* gtt location was already setup try to put vram before
                 * if it fits */
-               if (rdev->mc.vram_size < rdev->mc.gtt_location) {
+               if (rdev->mc.mc_vram_size < rdev->mc.gtt_location) {
                        rdev->mc.vram_location = 0;
                } else {
                        tmp = rdev->mc.gtt_location + rdev->mc.gtt_size;
-                       tmp += (rdev->mc.vram_size - 1);
-                       tmp &= ~(rdev->mc.vram_size - 1);
-                       if ((0xFFFFFFFFUL - tmp) >= rdev->mc.vram_size) {
+                       tmp += (rdev->mc.mc_vram_size - 1);
+                       tmp &= ~(rdev->mc.mc_vram_size - 1);
+                       if ((0xFFFFFFFFUL - tmp) >= rdev->mc.mc_vram_size) {
                                rdev->mc.vram_location = tmp;
                        } else {
                                printk(KERN_ERR "[drm] vram too big to fit "
@@ -152,16 +153,22 @@ int radeon_mc_setup(struct radeon_device *rdev)
                }
        } else {
                rdev->mc.vram_location = 0;
-               rdev->mc.gtt_location = rdev->mc.vram_size;
+               tmp = rdev->mc.mc_vram_size;
+               tmp = (tmp + rdev->mc.gtt_size - 1) & ~(rdev->mc.gtt_size - 1);
+               rdev->mc.gtt_location = tmp;
        }
-       DRM_INFO("radeon: VRAM %uM\n", rdev->mc.vram_size >> 20);
+       rdev->mc.vram_start = rdev->mc.vram_location;
+       rdev->mc.vram_end = rdev->mc.vram_location + rdev->mc.mc_vram_size - 1;
+       rdev->mc.gtt_start = rdev->mc.gtt_location;
+       rdev->mc.gtt_end = rdev->mc.gtt_location + rdev->mc.gtt_size - 1;
+       DRM_INFO("radeon: VRAM %uM\n", (unsigned)(rdev->mc.mc_vram_size >> 20));
        DRM_INFO("radeon: VRAM from 0x%08X to 0x%08X\n",
-                rdev->mc.vram_location,
-                rdev->mc.vram_location + rdev->mc.vram_size - 1);
-       DRM_INFO("radeon: GTT %uM\n", rdev->mc.gtt_size >> 20);
+                (unsigned)rdev->mc.vram_location,
+                (unsigned)(rdev->mc.vram_location + rdev->mc.mc_vram_size - 1));
+       DRM_INFO("radeon: GTT %uM\n", (unsigned)(rdev->mc.gtt_size >> 20));
        DRM_INFO("radeon: GTT from 0x%08X to 0x%08X\n",
-                rdev->mc.gtt_location,
-                rdev->mc.gtt_location + rdev->mc.gtt_size - 1);
+                (unsigned)rdev->mc.gtt_location,
+                (unsigned)(rdev->mc.gtt_location + rdev->mc.gtt_size - 1));
        return 0;
 }
 
@@ -169,7 +176,7 @@ int radeon_mc_setup(struct radeon_device *rdev)
 /*
  * GPU helpers function.
  */
-static bool radeon_card_posted(struct radeon_device *rdev)
+bool radeon_card_posted(struct radeon_device *rdev)
 {
        uint32_t reg;
 
@@ -201,6 +208,31 @@ static bool radeon_card_posted(struct radeon_device *rdev)
 
 }
 
+int radeon_dummy_page_init(struct radeon_device *rdev)
+{
+       rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
+       if (rdev->dummy_page.page == NULL)
+               return -ENOMEM;
+       rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
+                                       0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
+       if (!rdev->dummy_page.addr) {
+               __free_page(rdev->dummy_page.page);
+               rdev->dummy_page.page = NULL;
+               return -ENOMEM;
+       }
+       return 0;
+}
+
+void radeon_dummy_page_fini(struct radeon_device *rdev)
+{
+       if (rdev->dummy_page.page == NULL)
+               return;
+       pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
+                       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
+       __free_page(rdev->dummy_page.page);
+       rdev->dummy_page.page = NULL;
+}
+
 
 /*
  * Registers accessors functions.
@@ -221,31 +253,28 @@ void radeon_invalid_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
 
 void radeon_register_accessor_init(struct radeon_device *rdev)
 {
-       rdev->mm_rreg = &r100_mm_rreg;
-       rdev->mm_wreg = &r100_mm_wreg;
        rdev->mc_rreg = &radeon_invalid_rreg;
        rdev->mc_wreg = &radeon_invalid_wreg;
        rdev->pll_rreg = &radeon_invalid_rreg;
        rdev->pll_wreg = &radeon_invalid_wreg;
-       rdev->pcie_rreg = &radeon_invalid_rreg;
-       rdev->pcie_wreg = &radeon_invalid_wreg;
        rdev->pciep_rreg = &radeon_invalid_rreg;
        rdev->pciep_wreg = &radeon_invalid_wreg;
 
        /* Don't change order as we are overridding accessor. */
        if (rdev->family < CHIP_RV515) {
-               rdev->pcie_rreg = &rv370_pcie_rreg;
-               rdev->pcie_wreg = &rv370_pcie_wreg;
-       }
-       if (rdev->family >= CHIP_RV515) {
-               rdev->pcie_rreg = &rv515_pcie_rreg;
-               rdev->pcie_wreg = &rv515_pcie_wreg;
+               rdev->pcie_reg_mask = 0xff;
+       } else {
+               rdev->pcie_reg_mask = 0x7ff;
        }
        /* FIXME: not sure here */
        if (rdev->family <= CHIP_R580) {
                rdev->pll_rreg = &r100_pll_rreg;
                rdev->pll_wreg = &r100_pll_wreg;
        }
+       if (rdev->family >= CHIP_R420) {
+               rdev->mc_rreg = &r420_mc_rreg;
+               rdev->mc_wreg = &r420_mc_wreg;
+       }
        if (rdev->family >= CHIP_RV515) {
                rdev->mc_rreg = &rv515_mc_rreg;
                rdev->mc_wreg = &rv515_mc_wreg;
@@ -292,6 +321,10 @@ int radeon_asic_init(struct radeon_device *rdev)
        case CHIP_RV350:
        case CHIP_RV380:
                rdev->asic = &r300_asic;
+               if (rdev->flags & RADEON_IS_PCIE) {
+                       rdev->asic->gart_tlb_flush = &rv370_pcie_gart_tlb_flush;
+                       rdev->asic->gart_set_page = &rv370_pcie_gart_set_page;
+               }
                break;
        case CHIP_R420:
        case CHIP_R423:
@@ -326,9 +359,15 @@ int radeon_asic_init(struct radeon_device *rdev)
        case CHIP_RV635:
        case CHIP_RV670:
        case CHIP_RS780:
+       case CHIP_RS880:
+               rdev->asic = &r600_asic;
+               break;
        case CHIP_RV770:
        case CHIP_RV730:
        case CHIP_RV710:
+       case CHIP_RV740:
+               rdev->asic = &rv770_asic;
+               break;
        default:
                /* FIXME: not supported yet */
                return -EINVAL;
@@ -344,7 +383,6 @@ int radeon_clocks_init(struct radeon_device *rdev)
 {
        int r;
 
-       radeon_get_clock_info(rdev->ddev);
        r = radeon_static_clocks_init(rdev->ddev);
        if (r) {
                return r;
@@ -406,20 +444,24 @@ static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
        return r;
 }
 
-static struct card_info atom_card_info = {
-       .dev = NULL,
-       .reg_read = cail_reg_read,
-       .reg_write = cail_reg_write,
-       .mc_read = cail_mc_read,
-       .mc_write = cail_mc_write,
-       .pll_read = cail_pll_read,
-       .pll_write = cail_pll_write,
-};
-
 int radeon_atombios_init(struct radeon_device *rdev)
 {
-       atom_card_info.dev = rdev->ddev;
-       rdev->mode_info.atom_context = atom_parse(&atom_card_info, rdev->bios);
+       struct card_info *atom_card_info =
+           kzalloc(sizeof(struct card_info), GFP_KERNEL);
+
+       if (!atom_card_info)
+               return -ENOMEM;
+
+       rdev->mode_info.atom_card_info = atom_card_info;
+       atom_card_info->dev = rdev->ddev;
+       atom_card_info->reg_read = cail_reg_read;
+       atom_card_info->reg_write = cail_reg_write;
+       atom_card_info->mc_read = cail_mc_read;
+       atom_card_info->mc_write = cail_mc_write;
+       atom_card_info->pll_read = cail_pll_read;
+       atom_card_info->pll_write = cail_pll_write;
+
+       rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
        radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
        return 0;
 }
@@ -427,6 +469,7 @@ int radeon_atombios_init(struct radeon_device *rdev)
 void radeon_atombios_fini(struct radeon_device *rdev)
 {
        kfree(rdev->mode_info.atom_context);
+       kfree(rdev->mode_info.atom_card_info);
 }
 
 int radeon_combios_init(struct radeon_device *rdev)
@@ -439,9 +482,39 @@ void radeon_combios_fini(struct radeon_device *rdev)
 {
 }
 
-int radeon_modeset_init(struct radeon_device *rdev);
-void radeon_modeset_fini(struct radeon_device *rdev);
+/* if we get transitioned to only one device, tak VGA back */
+static unsigned int radeon_vga_set_decode(void *cookie, bool state)
+{
+       struct radeon_device *rdev = cookie;
+       radeon_vga_set_state(rdev, state);
+       if (state)
+               return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
+                      VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
+       else
+               return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
+}
 
+void radeon_agp_disable(struct radeon_device *rdev)
+{
+       rdev->flags &= ~RADEON_IS_AGP;
+       if (rdev->family >= CHIP_R600) {
+               DRM_INFO("Forcing AGP to PCIE mode\n");
+               rdev->flags |= RADEON_IS_PCIE;
+       } else if (rdev->family >= CHIP_RV515 ||
+                       rdev->family == CHIP_RV380 ||
+                       rdev->family == CHIP_RV410 ||
+                       rdev->family == CHIP_R423) {
+               DRM_INFO("Forcing AGP to PCIE mode\n");
+               rdev->flags |= RADEON_IS_PCIE;
+               rdev->asic->gart_tlb_flush = &rv370_pcie_gart_tlb_flush;
+               rdev->asic->gart_set_page = &rv370_pcie_gart_set_page;
+       } else {
+               DRM_INFO("Forcing AGP to PCI mode\n");
+               rdev->flags |= RADEON_IS_PCI;
+               rdev->asic->gart_tlb_flush = &r100_pci_gart_tlb_flush;
+               rdev->asic->gart_set_page = &r100_pci_gart_set_page;
+       }
+}
 
 /*
  * Radeon device.
@@ -451,11 +524,12 @@ int radeon_device_init(struct radeon_device *rdev,
                       struct pci_dev *pdev,
                       uint32_t flags)
 {
-       int r, ret;
+       int r;
        int dma_bits;
 
        DRM_INFO("radeon: Initializing kernel modesetting.\n");
        rdev->shutdown = false;
+       rdev->dev = &pdev->dev;
        rdev->ddev = ddev;
        rdev->pdev = pdev;
        rdev->flags = flags;
@@ -464,35 +538,23 @@ int radeon_device_init(struct radeon_device *rdev,
        rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
        rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
        rdev->gpu_lockup = false;
+       rdev->accel_working = false;
        /* mutex initialization are all done here so we
         * can recall function without having locking issues */
        mutex_init(&rdev->cs_mutex);
        mutex_init(&rdev->ib_pool.mutex);
        mutex_init(&rdev->cp.mutex);
        rwlock_init(&rdev->fence_drv.lock);
-
-       if (radeon_agpmode == -1) {
-               rdev->flags &= ~RADEON_IS_AGP;
-               if (rdev->family > CHIP_RV515 ||
-                   rdev->family == CHIP_RV380 ||
-                   rdev->family == CHIP_RV410 ||
-                   rdev->family == CHIP_R423) {
-                       DRM_INFO("Forcing AGP to PCIE mode\n");
-                       rdev->flags |= RADEON_IS_PCIE;
-               } else {
-                       DRM_INFO("Forcing AGP to PCI mode\n");
-                       rdev->flags |= RADEON_IS_PCI;
-               }
-       }
+       INIT_LIST_HEAD(&rdev->gem.objects);
 
        /* Set asic functions */
        r = radeon_asic_init(rdev);
        if (r) {
                return r;
        }
-       r = radeon_init(rdev);
-       if (r) {
-               return r;
+
+       if (radeon_agpmode == -1) {
+               radeon_agp_disable(rdev);
        }
 
        /* set DMA mask + need_dma32 flags.
@@ -524,153 +586,41 @@ int radeon_device_init(struct radeon_device *rdev,
        DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
        DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
 
-       /* Setup errata flags */
-       radeon_errata(rdev);
-       /* Initialize scratch registers */
-       radeon_scratch_init(rdev);
-       /* Initialize surface registers */
-       radeon_surface_init(rdev);
-
-       /* TODO: disable VGA need to use VGA request */
-       /* BIOS*/
-       if (!radeon_get_bios(rdev)) {
-               if (ASIC_IS_AVIVO(rdev))
-                       return -EINVAL;
-       }
-       if (rdev->is_atom_bios) {
-               r = radeon_atombios_init(rdev);
-               if (r) {
-                       return r;
-               }
-       } else {
-               r = radeon_combios_init(rdev);
-               if (r) {
-                       return r;
-               }
-       }
-       /* Reset gpu before posting otherwise ATOM will enter infinite loop */
-       if (radeon_gpu_reset(rdev)) {
-               /* FIXME: what do we want to do here ? */
-       }
-       /* check if cards are posted or not */
-       if (!radeon_card_posted(rdev) && rdev->bios) {
-               DRM_INFO("GPU not posted. posting now...\n");
-               if (rdev->is_atom_bios) {
-                       atom_asic_init(rdev->mode_info.atom_context);
-               } else {
-                       radeon_combios_asic_init(rdev->ddev);
-               }
-       }
-       /* Get vram informations */
-       radeon_vram_info(rdev);
-
-       /* Add an MTRR for the VRAM */
-       rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
-                                     MTRR_TYPE_WRCOMB, 1);
-       DRM_INFO("Detected VRAM RAM=%uM, BAR=%uM\n",
-                rdev->mc.vram_size >> 20,
-                (unsigned)rdev->mc.aper_size >> 20);
-       DRM_INFO("RAM width %dbits %cDR\n",
-                rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
-       /* Initialize clocks */
-       r = radeon_clocks_init(rdev);
-       if (r) {
-               return r;
-       }
-       /* Initialize memory controller (also test AGP) */
-       r = radeon_mc_init(rdev);
-       if (r) {
-               return r;
-       }
-       /* Fence driver */
-       r = radeon_fence_driver_init(rdev);
-       if (r) {
-               return r;
-       }
-       r = radeon_irq_kms_init(rdev);
-       if (r) {
-               return r;
-       }
-       /* Memory manager */
-       r = radeon_object_init(rdev);
-       if (r) {
+       /* if we have > 1 VGA cards, then disable the radeon VGA resources */
+       /* this will fail for cards that aren't VGA class devices, just
+        * ignore it */
+       vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
+
+       r = radeon_init(rdev);
+       if (r)
                return r;
-       }
-       /* Initialize GART (initialize after TTM so we can allocate
-        * memory through TTM but finalize after TTM) */
-       r = radeon_gart_enable(rdev);
-       if (!r) {
-               r = radeon_gem_init(rdev);
-       }
 
-       /* 1M ring buffer */
-       if (!r) {
-               r = radeon_cp_init(rdev, 1024 * 1024);
-       }
-       if (!r) {
-               r = radeon_wb_init(rdev);
-               if (r) {
-                       DRM_ERROR("radeon: failled initializing WB (%d).\n", r);
+       if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
+               /* Acceleration not working on AGP card try again
+                * with fallback to PCI or PCIE GART
+                */
+               radeon_gpu_reset(rdev);
+               radeon_fini(rdev);
+               radeon_agp_disable(rdev);
+               r = radeon_init(rdev);
+               if (r)
                        return r;
-               }
-       }
-       if (!r) {
-               r = radeon_ib_pool_init(rdev);
-               if (r) {
-                       DRM_ERROR("radeon: failled initializing IB pool (%d).\n", r);
-                       return r;
-               }
        }
-       if (!r) {
-               r = radeon_ib_test(rdev);
-               if (r) {
-                       DRM_ERROR("radeon: failled testing IB (%d).\n", r);
-                       return r;
-               }
-       }
-       ret = r;
-       r = radeon_modeset_init(rdev);
-       if (r) {
-               return r;
-       }
-       if (!ret) {
-               DRM_INFO("radeon: kernel modesetting successfully initialized.\n");
+       if (radeon_testing) {
+               radeon_test_moves(rdev);
        }
        if (radeon_benchmarking) {
                radeon_benchmark(rdev);
        }
-       return ret;
+       return 0;
 }
 
 void radeon_device_fini(struct radeon_device *rdev)
 {
-       if (rdev == NULL || rdev->rmmio == NULL) {
-               return;
-       }
        DRM_INFO("radeon: finishing device.\n");
        rdev->shutdown = true;
-       /* Order matter so becarefull if you rearrange anythings */
-       radeon_modeset_fini(rdev);
-       radeon_ib_pool_fini(rdev);
-       radeon_cp_fini(rdev);
-       radeon_wb_fini(rdev);
-       radeon_gem_fini(rdev);
-       radeon_object_fini(rdev);
-       /* mc_fini must be after object_fini */
-       radeon_mc_fini(rdev);
-#if __OS_HAS_AGP
-       radeon_agp_fini(rdev);
-#endif
-       radeon_irq_kms_fini(rdev);
-       radeon_fence_driver_fini(rdev);
-       radeon_clocks_fini(rdev);
-       if (rdev->is_atom_bios) {
-               radeon_atombios_fini(rdev);
-       } else {
-               radeon_combios_fini(rdev);
-       }
-       kfree(rdev->bios);
-       rdev->bios = NULL;
+       radeon_fini(rdev);
+       vga_client_register(rdev->pdev, NULL, NULL, NULL);
        iounmap(rdev->rmmio);
        rdev->rmmio = NULL;
 }
@@ -708,15 +658,12 @@ int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
        /* wait for gpu to finish processing current batch */
        radeon_fence_wait_last(rdev);
 
-       radeon_cp_disable(rdev);
-       radeon_gart_disable(rdev);
+       radeon_save_bios_scratch_regs(rdev);
 
+       radeon_suspend(rdev);
        /* evict remaining vram memory */
        radeon_object_evict_vram(rdev);
 
-       rdev->irq.sw_int = false;
-       radeon_irq_set(rdev);
-
        pci_save_state(dev->pdev);
        if (state.event == PM_EVENT_SUSPEND) {
                /* Shut down the device */
@@ -732,7 +679,6 @@ int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
 int radeon_resume_kms(struct drm_device *dev)
 {
        struct radeon_device *rdev = dev->dev_private;
-       int r;
 
        acquire_console_sem();
        pci_set_power_state(dev->pdev, PCI_D0);
@@ -742,39 +688,8 @@ int radeon_resume_kms(struct drm_device *dev)
                return -1;
        }
        pci_set_master(dev->pdev);
-       /* Reset gpu before posting otherwise ATOM will enter infinite loop */
-       if (radeon_gpu_reset(rdev)) {
-               /* FIXME: what do we want to do here ? */
-       }
-       /* post card */
-       if (rdev->is_atom_bios) {
-               atom_asic_init(rdev->mode_info.atom_context);
-       } else {
-               radeon_combios_asic_init(rdev->ddev);
-       }
-       /* Initialize clocks */
-       r = radeon_clocks_init(rdev);
-       if (r) {
-               release_console_sem();
-               return r;
-       }
-       /* Enable IRQ */
-       rdev->irq.sw_int = true;
-       radeon_irq_set(rdev);
-       /* Initialize GPU Memory Controller */
-       r = radeon_mc_init(rdev);
-       if (r) {
-               goto out;
-       }
-       r = radeon_gart_enable(rdev);
-       if (r) {
-               goto out;
-       }
-       r = radeon_cp_init(rdev, rdev->cp.ring_size);
-       if (r) {
-               goto out;
-       }
-out:
+       radeon_resume(rdev);
+       radeon_restore_bios_scratch_regs(rdev);
        fb_set_suspend(rdev->fbdev_info, 0);
        release_console_sem();