Home | History | Annotate | Line # | Download | only in drm
drm_pci.c revision 1.1
      1  1.1  riastrad /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */
      2  1.1  riastrad /**
      3  1.1  riastrad  * \file drm_pci.c
      4  1.1  riastrad  * \brief Functions and ioctls to manage PCI memory
      5  1.1  riastrad  *
      6  1.1  riastrad  * \warning These interfaces aren't stable yet.
      7  1.1  riastrad  *
      8  1.1  riastrad  * \todo Implement the remaining ioctl's for the PCI pools.
      9  1.1  riastrad  * \todo The wrappers here are so thin that they would be better off inlined..
     10  1.1  riastrad  *
     11  1.1  riastrad  * \author Jos Fonseca <jrfonseca (at) tungstengraphics.com>
     12  1.1  riastrad  * \author Leif Delgass <ldelgass (at) retinalburn.net>
     13  1.1  riastrad  */
     14  1.1  riastrad 
     15  1.1  riastrad /*
     16  1.1  riastrad  * Copyright 2003 Jos Fonseca.
     17  1.1  riastrad  * Copyright 2003 Leif Delgass.
     18  1.1  riastrad  * All Rights Reserved.
     19  1.1  riastrad  *
     20  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     21  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     22  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     23  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     24  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     25  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     26  1.1  riastrad  *
     27  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     28  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     29  1.1  riastrad  * Software.
     30  1.1  riastrad  *
     31  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     32  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     33  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     34  1.1  riastrad  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     35  1.1  riastrad  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     36  1.1  riastrad  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     37  1.1  riastrad  */
     38  1.1  riastrad 
     39  1.1  riastrad #include <linux/pci.h>
     40  1.1  riastrad #include <linux/slab.h>
     41  1.1  riastrad #include <linux/dma-mapping.h>
     42  1.1  riastrad #include <linux/export.h>
     43  1.1  riastrad #include <drm/drmP.h>
     44  1.1  riastrad 
     45  1.1  riastrad /**********************************************************************/
     46  1.1  riastrad /** \name PCI memory */
     47  1.1  riastrad /*@{*/
     48  1.1  riastrad 
     49  1.1  riastrad /**
     50  1.1  riastrad  * \brief Allocate a PCI consistent memory block, for DMA.
     51  1.1  riastrad  */
     52  1.1  riastrad drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align)
     53  1.1  riastrad {
     54  1.1  riastrad 	drm_dma_handle_t *dmah;
     55  1.1  riastrad #if 1
     56  1.1  riastrad 	unsigned long addr;
     57  1.1  riastrad 	size_t sz;
     58  1.1  riastrad #endif
     59  1.1  riastrad 
     60  1.1  riastrad 	/* pci_alloc_consistent only guarantees alignment to the smallest
     61  1.1  riastrad 	 * PAGE_SIZE order which is greater than or equal to the requested size.
     62  1.1  riastrad 	 * Return NULL here for now to make sure nobody tries for larger alignment
     63  1.1  riastrad 	 */
     64  1.1  riastrad 	if (align > size)
     65  1.1  riastrad 		return NULL;
     66  1.1  riastrad 
     67  1.1  riastrad 	dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
     68  1.1  riastrad 	if (!dmah)
     69  1.1  riastrad 		return NULL;
     70  1.1  riastrad 
     71  1.1  riastrad 	dmah->size = size;
     72  1.1  riastrad 	dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL | __GFP_COMP);
     73  1.1  riastrad 
     74  1.1  riastrad 	if (dmah->vaddr == NULL) {
     75  1.1  riastrad 		kfree(dmah);
     76  1.1  riastrad 		return NULL;
     77  1.1  riastrad 	}
     78  1.1  riastrad 
     79  1.1  riastrad 	memset(dmah->vaddr, 0, size);
     80  1.1  riastrad 
     81  1.1  riastrad 	/* XXX - Is virt_to_page() legal for consistent mem? */
     82  1.1  riastrad 	/* Reserve */
     83  1.1  riastrad 	for (addr = (unsigned long)dmah->vaddr, sz = size;
     84  1.1  riastrad 	     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
     85  1.1  riastrad 		SetPageReserved(virt_to_page(addr));
     86  1.1  riastrad 	}
     87  1.1  riastrad 
     88  1.1  riastrad 	return dmah;
     89  1.1  riastrad }
     90  1.1  riastrad 
     91  1.1  riastrad EXPORT_SYMBOL(drm_pci_alloc);
     92  1.1  riastrad 
     93  1.1  riastrad /**
     94  1.1  riastrad  * \brief Free a PCI consistent memory block without freeing its descriptor.
     95  1.1  riastrad  *
     96  1.1  riastrad  * This function is for internal use in the Linux-specific DRM core code.
     97  1.1  riastrad  */
     98  1.1  riastrad void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
     99  1.1  riastrad {
    100  1.1  riastrad #if 1
    101  1.1  riastrad 	unsigned long addr;
    102  1.1  riastrad 	size_t sz;
    103  1.1  riastrad #endif
    104  1.1  riastrad 
    105  1.1  riastrad 	if (dmah->vaddr) {
    106  1.1  riastrad 		/* XXX - Is virt_to_page() legal for consistent mem? */
    107  1.1  riastrad 		/* Unreserve */
    108  1.1  riastrad 		for (addr = (unsigned long)dmah->vaddr, sz = dmah->size;
    109  1.1  riastrad 		     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
    110  1.1  riastrad 			ClearPageReserved(virt_to_page(addr));
    111  1.1  riastrad 		}
    112  1.1  riastrad 		dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
    113  1.1  riastrad 				  dmah->busaddr);
    114  1.1  riastrad 	}
    115  1.1  riastrad }
    116  1.1  riastrad 
    117  1.1  riastrad /**
    118  1.1  riastrad  * \brief Free a PCI consistent memory block
    119  1.1  riastrad  */
    120  1.1  riastrad void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
    121  1.1  riastrad {
    122  1.1  riastrad 	__drm_pci_free(dev, dmah);
    123  1.1  riastrad 	kfree(dmah);
    124  1.1  riastrad }
    125  1.1  riastrad 
    126  1.1  riastrad EXPORT_SYMBOL(drm_pci_free);
    127  1.1  riastrad 
    128  1.1  riastrad #ifdef CONFIG_PCI
    129  1.1  riastrad 
    130  1.1  riastrad static int drm_get_pci_domain(struct drm_device *dev)
    131  1.1  riastrad {
    132  1.1  riastrad #ifndef __alpha__
    133  1.1  riastrad 	/* For historical reasons, drm_get_pci_domain() is busticated
    134  1.1  riastrad 	 * on most archs and has to remain so for userspace interface
    135  1.1  riastrad 	 * < 1.4, except on alpha which was right from the beginning
    136  1.1  riastrad 	 */
    137  1.1  riastrad 	if (dev->if_version < 0x10004)
    138  1.1  riastrad 		return 0;
    139  1.1  riastrad #endif /* __alpha__ */
    140  1.1  riastrad 
    141  1.1  riastrad 	return pci_domain_nr(dev->pdev->bus);
    142  1.1  riastrad }
    143  1.1  riastrad 
    144  1.1  riastrad static int drm_pci_get_irq(struct drm_device *dev)
    145  1.1  riastrad {
    146  1.1  riastrad 	return dev->pdev->irq;
    147  1.1  riastrad }
    148  1.1  riastrad 
    149  1.1  riastrad static const char *drm_pci_get_name(struct drm_device *dev)
    150  1.1  riastrad {
    151  1.1  riastrad 	struct pci_driver *pdriver = dev->driver->kdriver.pci;
    152  1.1  riastrad 	return pdriver->name;
    153  1.1  riastrad }
    154  1.1  riastrad 
    155  1.1  riastrad int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
    156  1.1  riastrad {
    157  1.1  riastrad 	int len, ret;
    158  1.1  riastrad 	struct pci_driver *pdriver = dev->driver->kdriver.pci;
    159  1.1  riastrad 	master->unique_len = 40;
    160  1.1  riastrad 	master->unique_size = master->unique_len;
    161  1.1  riastrad 	master->unique = kmalloc(master->unique_size, GFP_KERNEL);
    162  1.1  riastrad 	if (master->unique == NULL)
    163  1.1  riastrad 		return -ENOMEM;
    164  1.1  riastrad 
    165  1.1  riastrad 
    166  1.1  riastrad 	len = snprintf(master->unique, master->unique_len,
    167  1.1  riastrad 		       "pci:%04x:%02x:%02x.%d",
    168  1.1  riastrad 		       drm_get_pci_domain(dev),
    169  1.1  riastrad 		       dev->pdev->bus->number,
    170  1.1  riastrad 		       PCI_SLOT(dev->pdev->devfn),
    171  1.1  riastrad 		       PCI_FUNC(dev->pdev->devfn));
    172  1.1  riastrad 
    173  1.1  riastrad 	if (len >= master->unique_len) {
    174  1.1  riastrad 		DRM_ERROR("buffer overflow");
    175  1.1  riastrad 		ret = -EINVAL;
    176  1.1  riastrad 		goto err;
    177  1.1  riastrad 	} else
    178  1.1  riastrad 		master->unique_len = len;
    179  1.1  riastrad 
    180  1.1  riastrad 	dev->devname =
    181  1.1  riastrad 		kmalloc(strlen(pdriver->name) +
    182  1.1  riastrad 			master->unique_len + 2, GFP_KERNEL);
    183  1.1  riastrad 
    184  1.1  riastrad 	if (dev->devname == NULL) {
    185  1.1  riastrad 		ret = -ENOMEM;
    186  1.1  riastrad 		goto err;
    187  1.1  riastrad 	}
    188  1.1  riastrad 
    189  1.1  riastrad 	sprintf(dev->devname, "%s@%s", pdriver->name,
    190  1.1  riastrad 		master->unique);
    191  1.1  riastrad 
    192  1.1  riastrad 	return 0;
    193  1.1  riastrad err:
    194  1.1  riastrad 	return ret;
    195  1.1  riastrad }
    196  1.1  riastrad 
    197  1.1  riastrad int drm_pci_set_unique(struct drm_device *dev,
    198  1.1  riastrad 		       struct drm_master *master,
    199  1.1  riastrad 		       struct drm_unique *u)
    200  1.1  riastrad {
    201  1.1  riastrad 	int domain, bus, slot, func, ret;
    202  1.1  riastrad 	const char *bus_name;
    203  1.1  riastrad 
    204  1.1  riastrad 	master->unique_len = u->unique_len;
    205  1.1  riastrad 	master->unique_size = u->unique_len + 1;
    206  1.1  riastrad 	master->unique = kmalloc(master->unique_size, GFP_KERNEL);
    207  1.1  riastrad 	if (!master->unique) {
    208  1.1  riastrad 		ret = -ENOMEM;
    209  1.1  riastrad 		goto err;
    210  1.1  riastrad 	}
    211  1.1  riastrad 
    212  1.1  riastrad 	if (copy_from_user(master->unique, u->unique, master->unique_len)) {
    213  1.1  riastrad 		ret = -EFAULT;
    214  1.1  riastrad 		goto err;
    215  1.1  riastrad 	}
    216  1.1  riastrad 
    217  1.1  riastrad 	master->unique[master->unique_len] = '\0';
    218  1.1  riastrad 
    219  1.1  riastrad 	bus_name = dev->driver->bus->get_name(dev);
    220  1.1  riastrad 	dev->devname = kmalloc(strlen(bus_name) +
    221  1.1  riastrad 			       strlen(master->unique) + 2, GFP_KERNEL);
    222  1.1  riastrad 	if (!dev->devname) {
    223  1.1  riastrad 		ret = -ENOMEM;
    224  1.1  riastrad 		goto err;
    225  1.1  riastrad 	}
    226  1.1  riastrad 
    227  1.1  riastrad 	sprintf(dev->devname, "%s@%s", bus_name,
    228  1.1  riastrad 		master->unique);
    229  1.1  riastrad 
    230  1.1  riastrad 	/* Return error if the busid submitted doesn't match the device's actual
    231  1.1  riastrad 	 * busid.
    232  1.1  riastrad 	 */
    233  1.1  riastrad 	ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
    234  1.1  riastrad 	if (ret != 3) {
    235  1.1  riastrad 		ret = -EINVAL;
    236  1.1  riastrad 		goto err;
    237  1.1  riastrad 	}
    238  1.1  riastrad 
    239  1.1  riastrad 	domain = bus >> 8;
    240  1.1  riastrad 	bus &= 0xff;
    241  1.1  riastrad 
    242  1.1  riastrad 	if ((domain != drm_get_pci_domain(dev)) ||
    243  1.1  riastrad 	    (bus != dev->pdev->bus->number) ||
    244  1.1  riastrad 	    (slot != PCI_SLOT(dev->pdev->devfn)) ||
    245  1.1  riastrad 	    (func != PCI_FUNC(dev->pdev->devfn))) {
    246  1.1  riastrad 		ret = -EINVAL;
    247  1.1  riastrad 		goto err;
    248  1.1  riastrad 	}
    249  1.1  riastrad 	return 0;
    250  1.1  riastrad err:
    251  1.1  riastrad 	return ret;
    252  1.1  riastrad }
    253  1.1  riastrad 
    254  1.1  riastrad 
    255  1.1  riastrad static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
    256  1.1  riastrad {
    257  1.1  riastrad 	if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
    258  1.1  riastrad 	    (p->busnum & 0xff) != dev->pdev->bus->number ||
    259  1.1  riastrad 	    p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
    260  1.1  riastrad 		return -EINVAL;
    261  1.1  riastrad 
    262  1.1  riastrad 	p->irq = dev->pdev->irq;
    263  1.1  riastrad 
    264  1.1  riastrad 	DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
    265  1.1  riastrad 		  p->irq);
    266  1.1  riastrad 	return 0;
    267  1.1  riastrad }
    268  1.1  riastrad 
    269  1.1  riastrad int drm_pci_agp_init(struct drm_device *dev)
    270  1.1  riastrad {
    271  1.1  riastrad 	if (drm_core_has_AGP(dev)) {
    272  1.1  riastrad 		if (drm_pci_device_is_agp(dev))
    273  1.1  riastrad 			dev->agp = drm_agp_init(dev);
    274  1.1  riastrad 		if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
    275  1.1  riastrad 		    && (dev->agp == NULL)) {
    276  1.1  riastrad 			DRM_ERROR("Cannot initialize the agpgart module.\n");
    277  1.1  riastrad 			return -EINVAL;
    278  1.1  riastrad 		}
    279  1.1  riastrad 		if (drm_core_has_MTRR(dev)) {
    280  1.1  riastrad 			if (dev->agp)
    281  1.1  riastrad 				dev->agp->agp_mtrr =
    282  1.1  riastrad 					mtrr_add(dev->agp->agp_info.aper_base,
    283  1.1  riastrad 						 dev->agp->agp_info.aper_size *
    284  1.1  riastrad 						 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
    285  1.1  riastrad 		}
    286  1.1  riastrad 	}
    287  1.1  riastrad 	return 0;
    288  1.1  riastrad }
    289  1.1  riastrad 
    290  1.1  riastrad static struct drm_bus drm_pci_bus = {
    291  1.1  riastrad 	.bus_type = DRIVER_BUS_PCI,
    292  1.1  riastrad 	.get_irq = drm_pci_get_irq,
    293  1.1  riastrad 	.get_name = drm_pci_get_name,
    294  1.1  riastrad 	.set_busid = drm_pci_set_busid,
    295  1.1  riastrad 	.set_unique = drm_pci_set_unique,
    296  1.1  riastrad 	.irq_by_busid = drm_pci_irq_by_busid,
    297  1.1  riastrad 	.agp_init = drm_pci_agp_init,
    298  1.1  riastrad };
    299  1.1  riastrad 
    300  1.1  riastrad /**
    301  1.1  riastrad  * Register.
    302  1.1  riastrad  *
    303  1.1  riastrad  * \param pdev - PCI device structure
    304  1.1  riastrad  * \param ent entry from the PCI ID table with device type flags
    305  1.1  riastrad  * \return zero on success or a negative number on failure.
    306  1.1  riastrad  *
    307  1.1  riastrad  * Attempt to gets inter module "drm" information. If we are first
    308  1.1  riastrad  * then register the character device and inter module information.
    309  1.1  riastrad  * Try and register, if we fail to register, backout previous work.
    310  1.1  riastrad  */
    311  1.1  riastrad int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
    312  1.1  riastrad 		    struct drm_driver *driver)
    313  1.1  riastrad {
    314  1.1  riastrad 	struct drm_device *dev;
    315  1.1  riastrad 	int ret;
    316  1.1  riastrad 
    317  1.1  riastrad 	DRM_DEBUG("\n");
    318  1.1  riastrad 
    319  1.1  riastrad 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
    320  1.1  riastrad 	if (!dev)
    321  1.1  riastrad 		return -ENOMEM;
    322  1.1  riastrad 
    323  1.1  riastrad 	ret = pci_enable_device(pdev);
    324  1.1  riastrad 	if (ret)
    325  1.1  riastrad 		goto err_g1;
    326  1.1  riastrad 
    327  1.1  riastrad 	dev->pdev = pdev;
    328  1.1  riastrad 	dev->dev = &pdev->dev;
    329  1.1  riastrad 
    330  1.1  riastrad 	dev->pci_device = pdev->device;
    331  1.1  riastrad 	dev->pci_vendor = pdev->vendor;
    332  1.1  riastrad 
    333  1.1  riastrad #ifdef __alpha__
    334  1.1  riastrad 	dev->hose = pdev->sysdata;
    335  1.1  riastrad #endif
    336  1.1  riastrad 
    337  1.1  riastrad 	mutex_lock(&drm_global_mutex);
    338  1.1  riastrad 
    339  1.1  riastrad 	if ((ret = drm_fill_in_dev(dev, ent, driver))) {
    340  1.1  riastrad 		printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
    341  1.1  riastrad 		goto err_g2;
    342  1.1  riastrad 	}
    343  1.1  riastrad 
    344  1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
    345  1.1  riastrad 		pci_set_drvdata(pdev, dev);
    346  1.1  riastrad 		ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
    347  1.1  riastrad 		if (ret)
    348  1.1  riastrad 			goto err_g2;
    349  1.1  riastrad 	}
    350  1.1  riastrad 
    351  1.1  riastrad 	if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
    352  1.1  riastrad 		goto err_g3;
    353  1.1  riastrad 
    354  1.1  riastrad 	if (dev->driver->load) {
    355  1.1  riastrad 		ret = dev->driver->load(dev, ent->driver_data);
    356  1.1  riastrad 		if (ret)
    357  1.1  riastrad 			goto err_g4;
    358  1.1  riastrad 	}
    359  1.1  riastrad 
    360  1.1  riastrad 	/* setup the grouping for the legacy output */
    361  1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
    362  1.1  riastrad 		ret = drm_mode_group_init_legacy_group(dev,
    363  1.1  riastrad 						&dev->primary->mode_group);
    364  1.1  riastrad 		if (ret)
    365  1.1  riastrad 			goto err_g4;
    366  1.1  riastrad 	}
    367  1.1  riastrad 
    368  1.1  riastrad 	list_add_tail(&dev->driver_item, &driver->device_list);
    369  1.1  riastrad 
    370  1.1  riastrad 	DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
    371  1.1  riastrad 		 driver->name, driver->major, driver->minor, driver->patchlevel,
    372  1.1  riastrad 		 driver->date, pci_name(pdev), dev->primary->index);
    373  1.1  riastrad 
    374  1.1  riastrad 	mutex_unlock(&drm_global_mutex);
    375  1.1  riastrad 	return 0;
    376  1.1  riastrad 
    377  1.1  riastrad err_g4:
    378  1.1  riastrad 	drm_put_minor(&dev->primary);
    379  1.1  riastrad err_g3:
    380  1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET))
    381  1.1  riastrad 		drm_put_minor(&dev->control);
    382  1.1  riastrad err_g2:
    383  1.1  riastrad 	pci_disable_device(pdev);
    384  1.1  riastrad err_g1:
    385  1.1  riastrad 	kfree(dev);
    386  1.1  riastrad 	mutex_unlock(&drm_global_mutex);
    387  1.1  riastrad 	return ret;
    388  1.1  riastrad }
    389  1.1  riastrad EXPORT_SYMBOL(drm_get_pci_dev);
    390  1.1  riastrad 
    391  1.1  riastrad /**
    392  1.1  riastrad  * PCI device initialization. Called direct from modules at load time.
    393  1.1  riastrad  *
    394  1.1  riastrad  * \return zero on success or a negative number on failure.
    395  1.1  riastrad  *
    396  1.1  riastrad  * Initializes a drm_device structures,registering the
    397  1.1  riastrad  * stubs and initializing the AGP device.
    398  1.1  riastrad  *
    399  1.1  riastrad  * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
    400  1.1  riastrad  * after the initialization for driver customization.
    401  1.1  riastrad  */
    402  1.1  riastrad int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
    403  1.1  riastrad {
    404  1.1  riastrad 	struct pci_dev *pdev = NULL;
    405  1.1  riastrad 	const struct pci_device_id *pid;
    406  1.1  riastrad 	int i;
    407  1.1  riastrad 
    408  1.1  riastrad 	DRM_DEBUG("\n");
    409  1.1  riastrad 
    410  1.1  riastrad 	INIT_LIST_HEAD(&driver->device_list);
    411  1.1  riastrad 	driver->kdriver.pci = pdriver;
    412  1.1  riastrad 	driver->bus = &drm_pci_bus;
    413  1.1  riastrad 
    414  1.1  riastrad 	if (driver->driver_features & DRIVER_MODESET)
    415  1.1  riastrad 		return pci_register_driver(pdriver);
    416  1.1  riastrad 
    417  1.1  riastrad 	/* If not using KMS, fall back to stealth mode manual scanning. */
    418  1.1  riastrad 	for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
    419  1.1  riastrad 		pid = &pdriver->id_table[i];
    420  1.1  riastrad 
    421  1.1  riastrad 		/* Loop around setting up a DRM device for each PCI device
    422  1.1  riastrad 		 * matching our ID and device class.  If we had the internal
    423  1.1  riastrad 		 * function that pci_get_subsys and pci_get_class used, we'd
    424  1.1  riastrad 		 * be able to just pass pid in instead of doing a two-stage
    425  1.1  riastrad 		 * thing.
    426  1.1  riastrad 		 */
    427  1.1  riastrad 		pdev = NULL;
    428  1.1  riastrad 		while ((pdev =
    429  1.1  riastrad 			pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
    430  1.1  riastrad 				       pid->subdevice, pdev)) != NULL) {
    431  1.1  riastrad 			if ((pdev->class & pid->class_mask) != pid->class)
    432  1.1  riastrad 				continue;
    433  1.1  riastrad 
    434  1.1  riastrad 			/* stealth mode requires a manual probe */
    435  1.1  riastrad 			pci_dev_get(pdev);
    436  1.1  riastrad 			drm_get_pci_dev(pdev, pid, driver);
    437  1.1  riastrad 		}
    438  1.1  riastrad 	}
    439  1.1  riastrad 	return 0;
    440  1.1  riastrad }
    441  1.1  riastrad 
    442  1.1  riastrad #else
    443  1.1  riastrad 
    444  1.1  riastrad int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
    445  1.1  riastrad {
    446  1.1  riastrad 	return -1;
    447  1.1  riastrad }
    448  1.1  riastrad 
    449  1.1  riastrad #endif
    450  1.1  riastrad 
    451  1.1  riastrad EXPORT_SYMBOL(drm_pci_init);
    452  1.1  riastrad 
    453  1.1  riastrad /*@}*/
    454  1.1  riastrad void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
    455  1.1  riastrad {
    456  1.1  riastrad 	struct drm_device *dev, *tmp;
    457  1.1  riastrad 	DRM_DEBUG("\n");
    458  1.1  riastrad 
    459  1.1  riastrad 	if (driver->driver_features & DRIVER_MODESET) {
    460  1.1  riastrad 		pci_unregister_driver(pdriver);
    461  1.1  riastrad 	} else {
    462  1.1  riastrad 		list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
    463  1.1  riastrad 			drm_put_dev(dev);
    464  1.1  riastrad 	}
    465  1.1  riastrad 	DRM_INFO("Module unloaded\n");
    466  1.1  riastrad }
    467  1.1  riastrad EXPORT_SYMBOL(drm_pci_exit);
    468  1.1  riastrad 
    469  1.1  riastrad int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
    470  1.1  riastrad {
    471  1.1  riastrad 	struct pci_dev *root;
    472  1.1  riastrad 	int pos;
    473  1.1  riastrad 	u32 lnkcap = 0, lnkcap2 = 0;
    474  1.1  riastrad 
    475  1.1  riastrad 	*mask = 0;
    476  1.1  riastrad 	if (!dev->pdev)
    477  1.1  riastrad 		return -EINVAL;
    478  1.1  riastrad 
    479  1.1  riastrad 	if (!pci_is_pcie(dev->pdev))
    480  1.1  riastrad 		return -EINVAL;
    481  1.1  riastrad 
    482  1.1  riastrad 	root = dev->pdev->bus->self;
    483  1.1  riastrad 
    484  1.1  riastrad 	pos = pci_pcie_cap(root);
    485  1.1  riastrad 	if (!pos)
    486  1.1  riastrad 		return -EINVAL;
    487  1.1  riastrad 
    488  1.1  riastrad 	/* we've been informed via and serverworks don't make the cut */
    489  1.1  riastrad 	if (root->vendor == PCI_VENDOR_ID_VIA ||
    490  1.1  riastrad 	    root->vendor == PCI_VENDOR_ID_SERVERWORKS)
    491  1.1  riastrad 		return -EINVAL;
    492  1.1  riastrad 
    493  1.1  riastrad 	pci_read_config_dword(root, pos + PCI_EXP_LNKCAP, &lnkcap);
    494  1.1  riastrad 	pci_read_config_dword(root, pos + PCI_EXP_LNKCAP2, &lnkcap2);
    495  1.1  riastrad 
    496  1.1  riastrad 	lnkcap &= PCI_EXP_LNKCAP_SLS;
    497  1.1  riastrad 	lnkcap2 &= 0xfe;
    498  1.1  riastrad 
    499  1.1  riastrad 	if (lnkcap2) { /* PCIE GEN 3.0 */
    500  1.1  riastrad 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
    501  1.1  riastrad 			*mask |= DRM_PCIE_SPEED_25;
    502  1.1  riastrad 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
    503  1.1  riastrad 			*mask |= DRM_PCIE_SPEED_50;
    504  1.1  riastrad 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
    505  1.1  riastrad 			*mask |= DRM_PCIE_SPEED_80;
    506  1.1  riastrad 	} else {
    507  1.1  riastrad 		if (lnkcap & 1)
    508  1.1  riastrad 			*mask |= DRM_PCIE_SPEED_25;
    509  1.1  riastrad 		if (lnkcap & 2)
    510  1.1  riastrad 			*mask |= DRM_PCIE_SPEED_50;
    511  1.1  riastrad 	}
    512  1.1  riastrad 
    513  1.1  riastrad 	DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2);
    514  1.1  riastrad 	return 0;
    515  1.1  riastrad }
    516  1.1  riastrad EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);
    517