Subversion Repositories Kolibri OS

Rev

Rev 5060 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2003 José Fonseca.
  3.  * Copyright 2003 Leif Delgass.
  4.  * All Rights Reserved.
  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 (including the next
  14.  * paragraph) shall be included in all copies or substantial portions of the
  15.  * Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20.  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21.  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. #include <linux/pci.h>
  26. #include <linux/slab.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/export.h>
  29. #include <drm/drmP.h>
  30. #include "drm_legacy.h"
  31.  
  32. #include <syscall.h>
  33. /**
  34.  * drm_pci_alloc - Allocate a PCI consistent memory block, for DMA.
  35.  * @dev: DRM device
  36.  * @size: size of block to allocate
  37.  * @align: alignment of block
  38.  *
  39.  * Return: A handle to the allocated memory block on success or NULL on
  40.  * failure.
  41.  */
  42. drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align)
  43. {
  44.         drm_dma_handle_t *dmah;
  45.         unsigned long addr;
  46.         size_t sz;
  47.  
  48.         /* pci_alloc_consistent only guarantees alignment to the smallest
  49.          * PAGE_SIZE order which is greater than or equal to the requested size.
  50.          * Return NULL here for now to make sure nobody tries for larger alignment
  51.          */
  52.         if (align > size)
  53.                 return NULL;
  54.  
  55.         dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
  56.         if (!dmah)
  57.                 return NULL;
  58.  
  59.         dmah->size = size;
  60.     dmah->vaddr = (void*)KernelAlloc(size);
  61.     dmah->busaddr = GetPgAddr(dmah->vaddr);
  62.  
  63.         if (dmah->vaddr == NULL) {
  64.                 kfree(dmah);
  65.                 return NULL;
  66.         }
  67.  
  68.         memset(dmah->vaddr, 0, size);
  69.  
  70.         return dmah;
  71. }
  72.  
  73. EXPORT_SYMBOL(drm_pci_alloc);
  74.  
  75. #if 0
  76. /**
  77.  * \brief Free a PCI consistent memory block without freeing its descriptor.
  78.  *
  79.  * This function is for internal use in the Linux-specific DRM core code.
  80.  */
  81. void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  82. {
  83.         unsigned long addr;
  84.         size_t sz;
  85.  
  86.         if (dmah->vaddr) {
  87.                 /* XXX - Is virt_to_page() legal for consistent mem? */
  88.                 /* Unreserve */
  89.                 for (addr = (unsigned long)dmah->vaddr, sz = dmah->size;
  90.                      sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  91.                         ClearPageReserved(virt_to_page((void *)addr));
  92.                 }
  93.                 dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
  94.                                   dmah->busaddr);
  95.         }
  96. }
  97.  
  98. /**
  99.  * drm_pci_free - Free a PCI consistent memory block
  100.  * @dev: DRM device
  101.  * @dmah: handle to memory block
  102.  */
  103. void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  104. {
  105.         __drm_pci_free(dev, dmah);
  106.         kfree(dmah);
  107. }
  108.  
  109. EXPORT_SYMBOL(drm_pci_free);
  110.  
  111.  
  112. static int drm_get_pci_domain(struct drm_device *dev)
  113. {
  114. #ifndef __alpha__
  115.         /* For historical reasons, drm_get_pci_domain() is busticated
  116.          * on most archs and has to remain so for userspace interface
  117.          * < 1.4, except on alpha which was right from the beginning
  118.          */
  119.         if (dev->if_version < 0x10004)
  120.                 return 0;
  121. #endif /* __alpha__ */
  122.  
  123.         return pci_domain_nr(dev->pdev->bus);
  124. }
  125.  
  126. static int drm_pci_get_irq(struct drm_device *dev)
  127. {
  128.         return dev->pdev->irq;
  129. }
  130.  
  131. static const char *drm_pci_get_name(struct drm_device *dev)
  132. {
  133.         struct pci_driver *pdriver = dev->driver->kdriver.pci;
  134.         return pdriver->name;
  135. }
  136.  
  137. static int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
  138. {
  139.         int len, ret;
  140.         struct pci_driver *pdriver = dev->driver->kdriver.pci;
  141.         master->unique_len = 40;
  142.         master->unique_size = master->unique_len;
  143.         master->unique = kmalloc(master->unique_size, GFP_KERNEL);
  144.         if (master->unique == NULL)
  145.                 return -ENOMEM;
  146.  
  147.  
  148.         len = snprintf(master->unique, master->unique_len,
  149.                        "pci:%04x:%02x:%02x.%d",
  150.                        drm_get_pci_domain(dev),
  151.                        dev->pdev->bus->number,
  152.                        PCI_SLOT(dev->pdev->devfn),
  153.                        PCI_FUNC(dev->pdev->devfn));
  154.  
  155.         if (len >= master->unique_len) {
  156.                 DRM_ERROR("buffer overflow");
  157.                 ret = -EINVAL;
  158.                 goto err;
  159.         } else
  160.                 master->unique_len = len;
  161.  
  162.         dev->devname =
  163.                 kmalloc(strlen(pdriver->name) +
  164.                         master->unique_len + 2, GFP_KERNEL);
  165.  
  166.         if (dev->devname == NULL) {
  167.                 ret = -ENOMEM;
  168.                 goto err;
  169.         }
  170.  
  171.         sprintf(dev->devname, "%s@%s", pdriver->name,
  172.                 master->unique);
  173.  
  174.         return 0;
  175. err:
  176.         return ret;
  177. }
  178.  
  179. static int drm_pci_set_unique(struct drm_device *dev,
  180.                               struct drm_master *master,
  181.                               struct drm_unique *u)
  182. {
  183.         int domain, bus, slot, func, ret;
  184.         const char *bus_name;
  185.  
  186.         master->unique_len = u->unique_len;
  187.         master->unique_size = u->unique_len + 1;
  188.         master->unique = kmalloc(master->unique_size, GFP_KERNEL);
  189.         if (!master->unique) {
  190.                 ret = -ENOMEM;
  191.                 goto err;
  192.         }
  193.  
  194.         if (copy_from_user(master->unique, u->unique, master->unique_len)) {
  195.                 ret = -EFAULT;
  196.                 goto err;
  197.         }
  198.  
  199.         master->unique[master->unique_len] = '\0';
  200.  
  201.         bus_name = dev->driver->bus->get_name(dev);
  202.         dev->devname = kmalloc(strlen(bus_name) +
  203.                                strlen(master->unique) + 2, GFP_KERNEL);
  204.         if (!dev->devname) {
  205.                 ret = -ENOMEM;
  206.                 goto err;
  207.         }
  208.  
  209.         sprintf(dev->devname, "%s@%s", bus_name,
  210.                 master->unique);
  211.  
  212.         /* Return error if the busid submitted doesn't match the device's actual
  213.          * busid.
  214.          */
  215.         ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
  216.         if (ret != 3) {
  217.                 ret = -EINVAL;
  218.                 goto err;
  219.         }
  220.  
  221.         domain = bus >> 8;
  222.         bus &= 0xff;
  223.  
  224.         if ((domain != drm_get_pci_domain(dev)) ||
  225.             (bus != dev->pdev->bus->number) ||
  226.             (slot != PCI_SLOT(dev->pdev->devfn)) ||
  227.             (func != PCI_FUNC(dev->pdev->devfn))) {
  228.                 ret = -EINVAL;
  229.                 goto err;
  230.         }
  231.         return 0;
  232. err:
  233.         return ret;
  234. }
  235.  
  236. static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
  237. {
  238.         if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
  239.             (p->busnum & 0xff) != dev->pdev->bus->number ||
  240.             p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
  241.                 return -EINVAL;
  242.  
  243.         p->irq = dev->pdev->irq;
  244.  
  245.         DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
  246.                   p->irq);
  247.         return 0;
  248. }
  249.  
  250. static void drm_pci_agp_init(struct drm_device *dev)
  251. {
  252.         if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
  253.                 if (drm_pci_device_is_agp(dev))
  254.                         dev->agp = drm_agp_init(dev);
  255.                 if (dev->agp) {
  256.                         dev->agp->agp_mtrr = arch_phys_wc_add(
  257.                                 dev->agp->agp_info.aper_base,
  258.                                 dev->agp->agp_info.aper_size *
  259.                                 1024 * 1024);
  260.                 }
  261.         }
  262. }
  263.  
  264. void drm_pci_agp_destroy(struct drm_device *dev)
  265. {
  266.         if (dev->agp) {
  267.                 arch_phys_wc_del(dev->agp->agp_mtrr);
  268.                 drm_agp_clear(dev);
  269.                 kfree(dev->agp);
  270.                 dev->agp = NULL;
  271.         }
  272. }
  273.  
  274. static struct drm_bus drm_pci_bus = {
  275.         .bus_type = DRIVER_BUS_PCI,
  276.         .get_irq = drm_pci_get_irq,
  277.         .get_name = drm_pci_get_name,
  278.         .set_busid = drm_pci_set_busid,
  279.         .set_unique = drm_pci_set_unique,
  280.         .irq_by_busid = drm_pci_irq_by_busid,
  281. };
  282. #endif
  283.  
  284. /**
  285.  * Register.
  286.  *
  287.  * \param pdev - PCI device structure
  288.  * \param ent entry from the PCI ID table with device type flags
  289.  * \return zero on success or a negative number on failure.
  290.  *
  291.  * Attempt to gets inter module "drm" information. If we are first
  292.  * then register the character device and inter module information.
  293.  * Try and register, if we fail to register, backout previous work.
  294.  */
  295. int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  296.                     struct drm_driver *driver)
  297. {
  298.     static struct drm_device drm_dev;
  299.     static struct drm_file   drm_file;
  300.  
  301.         struct drm_device *dev;
  302.     struct drm_file   *priv;
  303.  
  304.         int ret;
  305.  
  306.     dev  = &drm_dev;
  307.     priv = &drm_file;
  308.  
  309.     drm_file_handlers[0] = priv;
  310.  
  311.  //   ret = pci_enable_device(pdev);
  312.  //   if (ret)
  313.  //       goto err_g1;
  314.  
  315.     pci_set_master(pdev);
  316.  
  317.     if ((ret = drm_fill_in_dev(dev, ent, driver))) {
  318.         printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  319.         goto err_g2;
  320.     }
  321.  
  322.         DRM_DEBUG("\n");
  323.  
  324.  
  325.         dev->pdev = pdev;
  326. #ifdef __alpha__
  327.         dev->hose = pdev->sysdata;
  328. #endif
  329.  
  330.  
  331.         if ((ret = drm_fill_in_dev(dev, ent, driver))) {
  332.                 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  333.                 goto err_g2;
  334.         }
  335.  
  336. #if 0
  337.         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  338.                 pci_set_drvdata(pdev, dev);
  339.                 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  340.                 if (ret)
  341.                         goto err_g2;
  342.         }
  343.  
  344.         if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
  345.                 ret = drm_get_minor(dev, &dev->render, DRM_MINOR_RENDER);
  346.                 if (ret)
  347.                         goto err_g21;
  348.         }
  349.  
  350.         if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  351.                 goto err_g3;
  352. #endif
  353.  
  354.         if (dev->driver->load) {
  355.                 ret = dev->driver->load(dev, ent->driver_data);
  356.                 if (ret)
  357.                         goto err_g4;
  358.         }
  359.  
  360.     if (dev->driver->open) {
  361.         ret = dev->driver->open(dev, priv);
  362.         if (ret < 0)
  363.             goto err_g4;
  364.     }
  365.  
  366.  
  367. //   mutex_unlock(&drm_global_mutex);
  368.         return 0;
  369.  
  370. err_g4:
  371. //   drm_put_minor(&dev->primary);
  372. err_g3:
  373. //   if (dev->render)
  374. //       drm_put_minor(&dev->render);
  375. err_g21:
  376. //   if (drm_core_check_feature(dev, DRIVER_MODESET))
  377. //       drm_put_minor(&dev->control);
  378. err_g2:
  379. //   pci_disable_device(pdev);
  380. err_g1:
  381. //   kfree(dev);
  382. //   mutex_unlock(&drm_global_mutex);
  383.         return ret;
  384. }
  385. EXPORT_SYMBOL(drm_get_pci_dev);
  386.  
  387. int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
  388. {
  389.         struct pci_dev *root;
  390.         u32 lnkcap, lnkcap2;
  391.  
  392.         *mask = 0;
  393.         if (!dev->pdev)
  394.                 return -EINVAL;
  395.  
  396.  
  397.     return -EINVAL;
  398.  
  399. #if 0
  400.         root = dev->pdev->bus->self;
  401.  
  402.         /* we've been informed via and serverworks don't make the cut */
  403.         if (root->vendor == PCI_VENDOR_ID_VIA ||
  404.             root->vendor == PCI_VENDOR_ID_SERVERWORKS)
  405.                 return -EINVAL;
  406.  
  407.         pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
  408.         pcie_capability_read_dword(root, PCI_EXP_LNKCAP2, &lnkcap2);
  409.  
  410.         if (lnkcap2) {  /* PCIe r3.0-compliant */
  411.                 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  412.                         *mask |= DRM_PCIE_SPEED_25;
  413.                 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  414.                         *mask |= DRM_PCIE_SPEED_50;
  415.                 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  416.                         *mask |= DRM_PCIE_SPEED_80;
  417.         } else {        /* pre-r3.0 */
  418.                 if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
  419.                         *mask |= DRM_PCIE_SPEED_25;
  420.                 if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
  421.                         *mask |= (DRM_PCIE_SPEED_25 | DRM_PCIE_SPEED_50);
  422.         }
  423.  
  424.         DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2);
  425.         return 0;
  426. #endif
  427.  
  428. }
  429. EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);
  430.