1c0e09200SDave Airlie /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */ 2c0e09200SDave Airlie /** 3c0e09200SDave Airlie * \file drm_pci.c 4c0e09200SDave Airlie * \brief Functions and ioctls to manage PCI memory 5c0e09200SDave Airlie * 6c0e09200SDave Airlie * \warning These interfaces aren't stable yet. 7c0e09200SDave Airlie * 8c0e09200SDave Airlie * \todo Implement the remaining ioctl's for the PCI pools. 9c0e09200SDave Airlie * \todo The wrappers here are so thin that they would be better off inlined.. 10c0e09200SDave Airlie * 11c0e09200SDave Airlie * \author José Fonseca <jrfonseca@tungstengraphics.com> 12c0e09200SDave Airlie * \author Leif Delgass <ldelgass@retinalburn.net> 13c0e09200SDave Airlie */ 14c0e09200SDave Airlie 15c0e09200SDave Airlie /* 16c0e09200SDave Airlie * Copyright 2003 José Fonseca. 17c0e09200SDave Airlie * Copyright 2003 Leif Delgass. 18c0e09200SDave Airlie * All Rights Reserved. 19c0e09200SDave Airlie * 20c0e09200SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 21c0e09200SDave Airlie * copy of this software and associated documentation files (the "Software"), 22c0e09200SDave Airlie * to deal in the Software without restriction, including without limitation 23c0e09200SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 24c0e09200SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 25c0e09200SDave Airlie * Software is furnished to do so, subject to the following conditions: 26c0e09200SDave Airlie * 27c0e09200SDave Airlie * The above copyright notice and this permission notice (including the next 28c0e09200SDave Airlie * paragraph) shall be included in all copies or substantial portions of the 29c0e09200SDave Airlie * Software. 30c0e09200SDave Airlie * 31c0e09200SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 32c0e09200SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 33c0e09200SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 34c0e09200SDave Airlie * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 35c0e09200SDave Airlie * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 36c0e09200SDave Airlie * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37c0e09200SDave Airlie */ 38c0e09200SDave Airlie 39c0e09200SDave Airlie #include <linux/pci.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 41c0e09200SDave Airlie #include <linux/dma-mapping.h> 422d1a8a48SPaul Gortmaker #include <linux/export.h> 43c0e09200SDave Airlie #include "drmP.h" 44c0e09200SDave Airlie 45c0e09200SDave Airlie /**********************************************************************/ 46c0e09200SDave Airlie /** \name PCI memory */ 47c0e09200SDave Airlie /*@{*/ 48c0e09200SDave Airlie 49c0e09200SDave Airlie /** 50c0e09200SDave Airlie * \brief Allocate a PCI consistent memory block, for DMA. 51c0e09200SDave Airlie */ 52e6be8d9dSZhenyu Wang drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align) 53c0e09200SDave Airlie { 54c0e09200SDave Airlie drm_dma_handle_t *dmah; 55c0e09200SDave Airlie #if 1 56c0e09200SDave Airlie unsigned long addr; 57c0e09200SDave Airlie size_t sz; 58c0e09200SDave Airlie #endif 59c0e09200SDave Airlie 60c0e09200SDave Airlie /* pci_alloc_consistent only guarantees alignment to the smallest 61c0e09200SDave Airlie * PAGE_SIZE order which is greater than or equal to the requested size. 62c0e09200SDave Airlie * Return NULL here for now to make sure nobody tries for larger alignment 63c0e09200SDave Airlie */ 64c0e09200SDave Airlie if (align > size) 65c0e09200SDave Airlie return NULL; 66c0e09200SDave Airlie 67c0e09200SDave Airlie dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL); 68c0e09200SDave Airlie if (!dmah) 69c0e09200SDave Airlie return NULL; 70c0e09200SDave Airlie 71c0e09200SDave Airlie dmah->size = size; 72c0e09200SDave Airlie dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL | __GFP_COMP); 73c0e09200SDave Airlie 74c0e09200SDave Airlie if (dmah->vaddr == NULL) { 75c0e09200SDave Airlie kfree(dmah); 76c0e09200SDave Airlie return NULL; 77c0e09200SDave Airlie } 78c0e09200SDave Airlie 79c0e09200SDave Airlie memset(dmah->vaddr, 0, size); 80c0e09200SDave Airlie 81c0e09200SDave Airlie /* XXX - Is virt_to_page() legal for consistent mem? */ 82c0e09200SDave Airlie /* Reserve */ 83c0e09200SDave Airlie for (addr = (unsigned long)dmah->vaddr, sz = size; 84c0e09200SDave Airlie sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) { 85c0e09200SDave Airlie SetPageReserved(virt_to_page(addr)); 86c0e09200SDave Airlie } 87c0e09200SDave Airlie 88c0e09200SDave Airlie return dmah; 89c0e09200SDave Airlie } 90c0e09200SDave Airlie 91c0e09200SDave Airlie EXPORT_SYMBOL(drm_pci_alloc); 92c0e09200SDave Airlie 93c0e09200SDave Airlie /** 94c0e09200SDave Airlie * \brief Free a PCI consistent memory block without freeing its descriptor. 95c0e09200SDave Airlie * 96c0e09200SDave Airlie * This function is for internal use in the Linux-specific DRM core code. 97c0e09200SDave Airlie */ 98c0e09200SDave Airlie void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah) 99c0e09200SDave Airlie { 100c0e09200SDave Airlie #if 1 101c0e09200SDave Airlie unsigned long addr; 102c0e09200SDave Airlie size_t sz; 103c0e09200SDave Airlie #endif 104c0e09200SDave Airlie 1059a298b2aSEric Anholt if (dmah->vaddr) { 106c0e09200SDave Airlie /* XXX - Is virt_to_page() legal for consistent mem? */ 107c0e09200SDave Airlie /* Unreserve */ 108c0e09200SDave Airlie for (addr = (unsigned long)dmah->vaddr, sz = dmah->size; 109c0e09200SDave Airlie sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) { 110c0e09200SDave Airlie ClearPageReserved(virt_to_page(addr)); 111c0e09200SDave Airlie } 112c0e09200SDave Airlie dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr, 113c0e09200SDave Airlie dmah->busaddr); 114c0e09200SDave Airlie } 115c0e09200SDave Airlie } 116c0e09200SDave Airlie 117c0e09200SDave Airlie /** 118c0e09200SDave Airlie * \brief Free a PCI consistent memory block 119c0e09200SDave Airlie */ 120c0e09200SDave Airlie void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah) 121c0e09200SDave Airlie { 122c0e09200SDave Airlie __drm_pci_free(dev, dmah); 123c0e09200SDave Airlie kfree(dmah); 124c0e09200SDave Airlie } 125c0e09200SDave Airlie 126c0e09200SDave Airlie EXPORT_SYMBOL(drm_pci_free); 127c0e09200SDave Airlie 128dcdb1674SJordan Crouse #ifdef CONFIG_PCI 1298410ea3bSDave Airlie 1308410ea3bSDave Airlie static int drm_get_pci_domain(struct drm_device *dev) 1318410ea3bSDave Airlie { 1328410ea3bSDave Airlie #ifndef __alpha__ 1338410ea3bSDave Airlie /* For historical reasons, drm_get_pci_domain() is busticated 1348410ea3bSDave Airlie * on most archs and has to remain so for userspace interface 1358410ea3bSDave Airlie * < 1.4, except on alpha which was right from the beginning 1368410ea3bSDave Airlie */ 1378410ea3bSDave Airlie if (dev->if_version < 0x10004) 1388410ea3bSDave Airlie return 0; 1398410ea3bSDave Airlie #endif /* __alpha__ */ 1408410ea3bSDave Airlie 1418410ea3bSDave Airlie return pci_domain_nr(dev->pdev->bus); 1428410ea3bSDave Airlie } 1438410ea3bSDave Airlie 1448410ea3bSDave Airlie static int drm_pci_get_irq(struct drm_device *dev) 1458410ea3bSDave Airlie { 1468410ea3bSDave Airlie return dev->pdev->irq; 1478410ea3bSDave Airlie } 1488410ea3bSDave Airlie 1498410ea3bSDave Airlie static const char *drm_pci_get_name(struct drm_device *dev) 1508410ea3bSDave Airlie { 1518410ea3bSDave Airlie struct pci_driver *pdriver = dev->driver->kdriver.pci; 1528410ea3bSDave Airlie return pdriver->name; 1538410ea3bSDave Airlie } 1548410ea3bSDave Airlie 1558410ea3bSDave Airlie int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master) 1568410ea3bSDave Airlie { 1578410ea3bSDave Airlie int len, ret; 1588410ea3bSDave Airlie struct pci_driver *pdriver = dev->driver->kdriver.pci; 1598410ea3bSDave Airlie master->unique_len = 40; 1608410ea3bSDave Airlie master->unique_size = master->unique_len; 1618410ea3bSDave Airlie master->unique = kmalloc(master->unique_size, GFP_KERNEL); 1628410ea3bSDave Airlie if (master->unique == NULL) 1638410ea3bSDave Airlie return -ENOMEM; 1648410ea3bSDave Airlie 1658410ea3bSDave Airlie 1668410ea3bSDave Airlie len = snprintf(master->unique, master->unique_len, 1678410ea3bSDave Airlie "pci:%04x:%02x:%02x.%d", 1688410ea3bSDave Airlie drm_get_pci_domain(dev), 1698410ea3bSDave Airlie dev->pdev->bus->number, 1708410ea3bSDave Airlie PCI_SLOT(dev->pdev->devfn), 1718410ea3bSDave Airlie PCI_FUNC(dev->pdev->devfn)); 1728410ea3bSDave Airlie 1738410ea3bSDave Airlie if (len >= master->unique_len) { 1748410ea3bSDave Airlie DRM_ERROR("buffer overflow"); 1758410ea3bSDave Airlie ret = -EINVAL; 1768410ea3bSDave Airlie goto err; 1778410ea3bSDave Airlie } else 1788410ea3bSDave Airlie master->unique_len = len; 1798410ea3bSDave Airlie 1808410ea3bSDave Airlie dev->devname = 1818410ea3bSDave Airlie kmalloc(strlen(pdriver->name) + 1828410ea3bSDave Airlie master->unique_len + 2, GFP_KERNEL); 1838410ea3bSDave Airlie 1848410ea3bSDave Airlie if (dev->devname == NULL) { 1858410ea3bSDave Airlie ret = -ENOMEM; 1868410ea3bSDave Airlie goto err; 1878410ea3bSDave Airlie } 1888410ea3bSDave Airlie 1898410ea3bSDave Airlie sprintf(dev->devname, "%s@%s", pdriver->name, 1908410ea3bSDave Airlie master->unique); 1918410ea3bSDave Airlie 1928410ea3bSDave Airlie return 0; 1938410ea3bSDave Airlie err: 1948410ea3bSDave Airlie return ret; 1958410ea3bSDave Airlie } 1968410ea3bSDave Airlie 1978410ea3bSDave Airlie int drm_pci_set_unique(struct drm_device *dev, 1988410ea3bSDave Airlie struct drm_master *master, 1998410ea3bSDave Airlie struct drm_unique *u) 2008410ea3bSDave Airlie { 2018410ea3bSDave Airlie int domain, bus, slot, func, ret; 2028410ea3bSDave Airlie const char *bus_name; 2038410ea3bSDave Airlie 2048410ea3bSDave Airlie master->unique_len = u->unique_len; 2058410ea3bSDave Airlie master->unique_size = u->unique_len + 1; 2068410ea3bSDave Airlie master->unique = kmalloc(master->unique_size, GFP_KERNEL); 2078410ea3bSDave Airlie if (!master->unique) { 2088410ea3bSDave Airlie ret = -ENOMEM; 2098410ea3bSDave Airlie goto err; 2108410ea3bSDave Airlie } 2118410ea3bSDave Airlie 2128410ea3bSDave Airlie if (copy_from_user(master->unique, u->unique, master->unique_len)) { 2138410ea3bSDave Airlie ret = -EFAULT; 2148410ea3bSDave Airlie goto err; 2158410ea3bSDave Airlie } 2168410ea3bSDave Airlie 2178410ea3bSDave Airlie master->unique[master->unique_len] = '\0'; 2188410ea3bSDave Airlie 2198410ea3bSDave Airlie bus_name = dev->driver->bus->get_name(dev); 2208410ea3bSDave Airlie dev->devname = kmalloc(strlen(bus_name) + 2218410ea3bSDave Airlie strlen(master->unique) + 2, GFP_KERNEL); 2228410ea3bSDave Airlie if (!dev->devname) { 2238410ea3bSDave Airlie ret = -ENOMEM; 2248410ea3bSDave Airlie goto err; 2258410ea3bSDave Airlie } 2268410ea3bSDave Airlie 2278410ea3bSDave Airlie sprintf(dev->devname, "%s@%s", bus_name, 2288410ea3bSDave Airlie master->unique); 2298410ea3bSDave Airlie 2308410ea3bSDave Airlie /* Return error if the busid submitted doesn't match the device's actual 2318410ea3bSDave Airlie * busid. 2328410ea3bSDave Airlie */ 2338410ea3bSDave Airlie ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func); 2348410ea3bSDave Airlie if (ret != 3) { 2358410ea3bSDave Airlie ret = -EINVAL; 2368410ea3bSDave Airlie goto err; 2378410ea3bSDave Airlie } 2388410ea3bSDave Airlie 2398410ea3bSDave Airlie domain = bus >> 8; 2408410ea3bSDave Airlie bus &= 0xff; 2418410ea3bSDave Airlie 2428410ea3bSDave Airlie if ((domain != drm_get_pci_domain(dev)) || 2438410ea3bSDave Airlie (bus != dev->pdev->bus->number) || 2448410ea3bSDave Airlie (slot != PCI_SLOT(dev->pdev->devfn)) || 2458410ea3bSDave Airlie (func != PCI_FUNC(dev->pdev->devfn))) { 2468410ea3bSDave Airlie ret = -EINVAL; 2478410ea3bSDave Airlie goto err; 2488410ea3bSDave Airlie } 2498410ea3bSDave Airlie return 0; 2508410ea3bSDave Airlie err: 2518410ea3bSDave Airlie return ret; 2528410ea3bSDave Airlie } 2538410ea3bSDave Airlie 2548410ea3bSDave Airlie 25545e97ab6SWolfram Sang static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p) 2568410ea3bSDave Airlie { 2578410ea3bSDave Airlie if ((p->busnum >> 8) != drm_get_pci_domain(dev) || 2588410ea3bSDave Airlie (p->busnum & 0xff) != dev->pdev->bus->number || 2598410ea3bSDave Airlie p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn)) 2608410ea3bSDave Airlie return -EINVAL; 2618410ea3bSDave Airlie 2628410ea3bSDave Airlie p->irq = dev->pdev->irq; 2638410ea3bSDave Airlie 2648410ea3bSDave Airlie DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum, 2658410ea3bSDave Airlie p->irq); 2668410ea3bSDave Airlie return 0; 2678410ea3bSDave Airlie } 2688410ea3bSDave Airlie 2698410ea3bSDave Airlie int drm_pci_agp_init(struct drm_device *dev) 2708410ea3bSDave Airlie { 2718410ea3bSDave Airlie if (drm_core_has_AGP(dev)) { 2728410ea3bSDave Airlie if (drm_pci_device_is_agp(dev)) 2738410ea3bSDave Airlie dev->agp = drm_agp_init(dev); 2748410ea3bSDave Airlie if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) 2758410ea3bSDave Airlie && (dev->agp == NULL)) { 2768410ea3bSDave Airlie DRM_ERROR("Cannot initialize the agpgart module.\n"); 2778410ea3bSDave Airlie return -EINVAL; 2788410ea3bSDave Airlie } 2798410ea3bSDave Airlie if (drm_core_has_MTRR(dev)) { 2808410ea3bSDave Airlie if (dev->agp) 2818410ea3bSDave Airlie dev->agp->agp_mtrr = 2828410ea3bSDave Airlie mtrr_add(dev->agp->agp_info.aper_base, 2838410ea3bSDave Airlie dev->agp->agp_info.aper_size * 2848410ea3bSDave Airlie 1024 * 1024, MTRR_TYPE_WRCOMB, 1); 2858410ea3bSDave Airlie } 2868410ea3bSDave Airlie } 2878410ea3bSDave Airlie return 0; 2888410ea3bSDave Airlie } 2898410ea3bSDave Airlie 2908410ea3bSDave Airlie static struct drm_bus drm_pci_bus = { 2918410ea3bSDave Airlie .bus_type = DRIVER_BUS_PCI, 2928410ea3bSDave Airlie .get_irq = drm_pci_get_irq, 2938410ea3bSDave Airlie .get_name = drm_pci_get_name, 2948410ea3bSDave Airlie .set_busid = drm_pci_set_busid, 2958410ea3bSDave Airlie .set_unique = drm_pci_set_unique, 29645e97ab6SWolfram Sang .irq_by_busid = drm_pci_irq_by_busid, 2978410ea3bSDave Airlie .agp_init = drm_pci_agp_init, 2988410ea3bSDave Airlie }; 2998410ea3bSDave Airlie 300dcdb1674SJordan Crouse /** 301dcdb1674SJordan Crouse * Register. 302dcdb1674SJordan Crouse * 303dcdb1674SJordan Crouse * \param pdev - PCI device structure 304dcdb1674SJordan Crouse * \param ent entry from the PCI ID table with device type flags 305dcdb1674SJordan Crouse * \return zero on success or a negative number on failure. 306dcdb1674SJordan Crouse * 307dcdb1674SJordan Crouse * Attempt to gets inter module "drm" information. If we are first 308dcdb1674SJordan Crouse * then register the character device and inter module information. 309dcdb1674SJordan Crouse * Try and register, if we fail to register, backout previous work. 310dcdb1674SJordan Crouse */ 311dcdb1674SJordan Crouse int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent, 312dcdb1674SJordan Crouse struct drm_driver *driver) 313dcdb1674SJordan Crouse { 314dcdb1674SJordan Crouse struct drm_device *dev; 315dcdb1674SJordan Crouse int ret; 316dcdb1674SJordan Crouse 317dcdb1674SJordan Crouse DRM_DEBUG("\n"); 318dcdb1674SJordan Crouse 319dcdb1674SJordan Crouse dev = kzalloc(sizeof(*dev), GFP_KERNEL); 320dcdb1674SJordan Crouse if (!dev) 321dcdb1674SJordan Crouse return -ENOMEM; 322dcdb1674SJordan Crouse 323dcdb1674SJordan Crouse ret = pci_enable_device(pdev); 324dcdb1674SJordan Crouse if (ret) 325dcdb1674SJordan Crouse goto err_g1; 326dcdb1674SJordan Crouse 327dcdb1674SJordan Crouse dev->pdev = pdev; 328dcdb1674SJordan Crouse dev->dev = &pdev->dev; 329dcdb1674SJordan Crouse 330dcdb1674SJordan Crouse dev->pci_device = pdev->device; 331dcdb1674SJordan Crouse dev->pci_vendor = pdev->vendor; 332dcdb1674SJordan Crouse 333dcdb1674SJordan Crouse #ifdef __alpha__ 334dcdb1674SJordan Crouse dev->hose = pdev->sysdata; 335dcdb1674SJordan Crouse #endif 336dcdb1674SJordan Crouse 337b64c115eSDave Airlie mutex_lock(&drm_global_mutex); 338b64c115eSDave Airlie 339dcdb1674SJordan Crouse if ((ret = drm_fill_in_dev(dev, ent, driver))) { 340dcdb1674SJordan Crouse printk(KERN_ERR "DRM: Fill_in_dev failed.\n"); 341dcdb1674SJordan Crouse goto err_g2; 342dcdb1674SJordan Crouse } 343dcdb1674SJordan Crouse 344dcdb1674SJordan Crouse if (drm_core_check_feature(dev, DRIVER_MODESET)) { 345dcdb1674SJordan Crouse pci_set_drvdata(pdev, dev); 346dcdb1674SJordan Crouse ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL); 347dcdb1674SJordan Crouse if (ret) 348dcdb1674SJordan Crouse goto err_g2; 349dcdb1674SJordan Crouse } 350dcdb1674SJordan Crouse 351dcdb1674SJordan Crouse if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY))) 352dcdb1674SJordan Crouse goto err_g3; 353dcdb1674SJordan Crouse 354dcdb1674SJordan Crouse if (dev->driver->load) { 355dcdb1674SJordan Crouse ret = dev->driver->load(dev, ent->driver_data); 356dcdb1674SJordan Crouse if (ret) 357dcdb1674SJordan Crouse goto err_g4; 358dcdb1674SJordan Crouse } 359dcdb1674SJordan Crouse 360dcdb1674SJordan Crouse /* setup the grouping for the legacy output */ 361dcdb1674SJordan Crouse if (drm_core_check_feature(dev, DRIVER_MODESET)) { 362dcdb1674SJordan Crouse ret = drm_mode_group_init_legacy_group(dev, 363dcdb1674SJordan Crouse &dev->primary->mode_group); 364dcdb1674SJordan Crouse if (ret) 365dcdb1674SJordan Crouse goto err_g4; 366dcdb1674SJordan Crouse } 367dcdb1674SJordan Crouse 368dcdb1674SJordan Crouse list_add_tail(&dev->driver_item, &driver->device_list); 369dcdb1674SJordan Crouse 370dcdb1674SJordan Crouse DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n", 371dcdb1674SJordan Crouse driver->name, driver->major, driver->minor, driver->patchlevel, 372dcdb1674SJordan Crouse driver->date, pci_name(pdev), dev->primary->index); 373dcdb1674SJordan Crouse 374b64c115eSDave Airlie mutex_unlock(&drm_global_mutex); 375dcdb1674SJordan Crouse return 0; 376dcdb1674SJordan Crouse 377dcdb1674SJordan Crouse err_g4: 378dcdb1674SJordan Crouse drm_put_minor(&dev->primary); 379dcdb1674SJordan Crouse err_g3: 380dcdb1674SJordan Crouse if (drm_core_check_feature(dev, DRIVER_MODESET)) 381dcdb1674SJordan Crouse drm_put_minor(&dev->control); 382dcdb1674SJordan Crouse err_g2: 383dcdb1674SJordan Crouse pci_disable_device(pdev); 384dcdb1674SJordan Crouse err_g1: 385dcdb1674SJordan Crouse kfree(dev); 386b64c115eSDave Airlie mutex_unlock(&drm_global_mutex); 387dcdb1674SJordan Crouse return ret; 388dcdb1674SJordan Crouse } 389dcdb1674SJordan Crouse EXPORT_SYMBOL(drm_get_pci_dev); 390dcdb1674SJordan Crouse 391dcdb1674SJordan Crouse /** 3928410ea3bSDave Airlie * PCI device initialization. Called direct from modules at load time. 393dcdb1674SJordan Crouse * 394dcdb1674SJordan Crouse * \return zero on success or a negative number on failure. 395dcdb1674SJordan Crouse * 396dcdb1674SJordan Crouse * Initializes a drm_device structures,registering the 397dcdb1674SJordan Crouse * stubs and initializing the AGP device. 398dcdb1674SJordan Crouse * 399dcdb1674SJordan Crouse * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and 400dcdb1674SJordan Crouse * after the initialization for driver customization. 401dcdb1674SJordan Crouse */ 4028410ea3bSDave Airlie int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver) 403dcdb1674SJordan Crouse { 404dcdb1674SJordan Crouse struct pci_dev *pdev = NULL; 405dcdb1674SJordan Crouse const struct pci_device_id *pid; 406dcdb1674SJordan Crouse int i; 407dcdb1674SJordan Crouse 4088410ea3bSDave Airlie DRM_DEBUG("\n"); 4098410ea3bSDave Airlie 4108410ea3bSDave Airlie INIT_LIST_HEAD(&driver->device_list); 4118410ea3bSDave Airlie driver->kdriver.pci = pdriver; 4128410ea3bSDave Airlie driver->bus = &drm_pci_bus; 4138410ea3bSDave Airlie 414dcdb1674SJordan Crouse if (driver->driver_features & DRIVER_MODESET) 4158410ea3bSDave Airlie return pci_register_driver(pdriver); 416dcdb1674SJordan Crouse 417dcdb1674SJordan Crouse /* If not using KMS, fall back to stealth mode manual scanning. */ 4188410ea3bSDave Airlie for (i = 0; pdriver->id_table[i].vendor != 0; i++) { 4198410ea3bSDave Airlie pid = &pdriver->id_table[i]; 420dcdb1674SJordan Crouse 421dcdb1674SJordan Crouse /* Loop around setting up a DRM device for each PCI device 422dcdb1674SJordan Crouse * matching our ID and device class. If we had the internal 423dcdb1674SJordan Crouse * function that pci_get_subsys and pci_get_class used, we'd 424dcdb1674SJordan Crouse * be able to just pass pid in instead of doing a two-stage 425dcdb1674SJordan Crouse * thing. 426dcdb1674SJordan Crouse */ 427dcdb1674SJordan Crouse pdev = NULL; 428dcdb1674SJordan Crouse while ((pdev = 429dcdb1674SJordan Crouse pci_get_subsys(pid->vendor, pid->device, pid->subvendor, 430dcdb1674SJordan Crouse pid->subdevice, pdev)) != NULL) { 431dcdb1674SJordan Crouse if ((pdev->class & pid->class_mask) != pid->class) 432dcdb1674SJordan Crouse continue; 433dcdb1674SJordan Crouse 434dcdb1674SJordan Crouse /* stealth mode requires a manual probe */ 435dcdb1674SJordan Crouse pci_dev_get(pdev); 436dcdb1674SJordan Crouse drm_get_pci_dev(pdev, pid, driver); 437dcdb1674SJordan Crouse } 438dcdb1674SJordan Crouse } 439dcdb1674SJordan Crouse return 0; 440dcdb1674SJordan Crouse } 441dcdb1674SJordan Crouse 442dcdb1674SJordan Crouse #else 443dcdb1674SJordan Crouse 4448410ea3bSDave Airlie int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver) 445dcdb1674SJordan Crouse { 446dcdb1674SJordan Crouse return -1; 447dcdb1674SJordan Crouse } 448dcdb1674SJordan Crouse 449dcdb1674SJordan Crouse #endif 4508410ea3bSDave Airlie 4518410ea3bSDave Airlie EXPORT_SYMBOL(drm_pci_init); 4528410ea3bSDave Airlie 453c0e09200SDave Airlie /*@}*/ 4548410ea3bSDave Airlie void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver) 4558410ea3bSDave Airlie { 4568410ea3bSDave Airlie struct drm_device *dev, *tmp; 4578410ea3bSDave Airlie DRM_DEBUG("\n"); 4588410ea3bSDave Airlie 4598410ea3bSDave Airlie if (driver->driver_features & DRIVER_MODESET) { 4608410ea3bSDave Airlie pci_unregister_driver(pdriver); 4618410ea3bSDave Airlie } else { 4628410ea3bSDave Airlie list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item) 4638410ea3bSDave Airlie drm_put_dev(dev); 4648410ea3bSDave Airlie } 4658410ea3bSDave Airlie DRM_INFO("Module unloaded\n"); 4668410ea3bSDave Airlie } 4678410ea3bSDave Airlie EXPORT_SYMBOL(drm_pci_exit); 468f4297784SDave Airlie 469f4297784SDave Airlie int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask) 470f4297784SDave Airlie { 471f4297784SDave Airlie struct pci_dev *root; 472f4297784SDave Airlie int pos; 473f4297784SDave Airlie u32 lnkcap, lnkcap2; 474f4297784SDave Airlie 475f4297784SDave Airlie *mask = 0; 476f4297784SDave Airlie if (!dev->pdev) 477f4297784SDave Airlie return -EINVAL; 478f4297784SDave Airlie 479f4297784SDave Airlie if (!pci_is_pcie(dev->pdev)) 480f4297784SDave Airlie return -EINVAL; 481f4297784SDave Airlie 482f4297784SDave Airlie root = dev->pdev->bus->self; 483f4297784SDave Airlie 484f4297784SDave Airlie pos = pci_pcie_cap(root); 485f4297784SDave Airlie if (!pos) 486f4297784SDave Airlie return -EINVAL; 487f4297784SDave Airlie 488f4297784SDave Airlie /* we've been informed via and serverworks don't make the cut */ 489f4297784SDave Airlie if (root->vendor == PCI_VENDOR_ID_VIA || 490f4297784SDave Airlie root->vendor == PCI_VENDOR_ID_SERVERWORKS) 491f4297784SDave Airlie return -EINVAL; 492f4297784SDave Airlie 493f4297784SDave Airlie pci_read_config_dword(root, pos + PCI_EXP_LNKCAP, &lnkcap); 494f4297784SDave Airlie pci_read_config_dword(root, pos + PCI_EXP_LNKCAP2, &lnkcap2); 495f4297784SDave Airlie 496f4297784SDave Airlie lnkcap &= PCI_EXP_LNKCAP_SLS; 497f4297784SDave Airlie lnkcap2 &= 0xfe; 498f4297784SDave Airlie 499f4297784SDave Airlie if (lnkcap2) { /* PCIE GEN 3.0 */ 500f4297784SDave Airlie if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB) 501f4297784SDave Airlie *mask |= DRM_PCIE_SPEED_25; 502f4297784SDave Airlie if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB) 503f4297784SDave Airlie *mask |= DRM_PCIE_SPEED_50; 504f4297784SDave Airlie if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB) 505f4297784SDave Airlie *mask |= DRM_PCIE_SPEED_80; 506f4297784SDave Airlie } else { 507f4297784SDave Airlie if (lnkcap & 1) 508f4297784SDave Airlie *mask |= DRM_PCIE_SPEED_25; 509f4297784SDave Airlie if (lnkcap & 2) 510f4297784SDave Airlie *mask |= DRM_PCIE_SPEED_50; 511f4297784SDave Airlie } 512f4297784SDave Airlie 513f4297784SDave Airlie DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2); 514f4297784SDave Airlie return 0; 515f4297784SDave Airlie } 516f4297784SDave Airlie EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask); 517