drm_pci.c revision 1.2 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.2 christos size_t devlen = strlen(pdriver->name) + master->unique_len + 2;
181 1.2 christos dev->devname = kmalloc(dev->devlen, GFP_KERNEL);
182 1.1 riastrad
183 1.1 riastrad if (dev->devname == NULL) {
184 1.1 riastrad ret = -ENOMEM;
185 1.1 riastrad goto err;
186 1.1 riastrad }
187 1.1 riastrad
188 1.2 christos snprintf(dev->devname, devlen, "%s@%s", pdriver->name,
189 1.1 riastrad master->unique);
190 1.1 riastrad
191 1.1 riastrad return 0;
192 1.1 riastrad err:
193 1.1 riastrad return ret;
194 1.1 riastrad }
195 1.1 riastrad
196 1.1 riastrad int drm_pci_set_unique(struct drm_device *dev,
197 1.1 riastrad struct drm_master *master,
198 1.1 riastrad struct drm_unique *u)
199 1.1 riastrad {
200 1.1 riastrad int domain, bus, slot, func, ret;
201 1.1 riastrad const char *bus_name;
202 1.1 riastrad
203 1.1 riastrad master->unique_len = u->unique_len;
204 1.1 riastrad master->unique_size = u->unique_len + 1;
205 1.1 riastrad master->unique = kmalloc(master->unique_size, GFP_KERNEL);
206 1.1 riastrad if (!master->unique) {
207 1.1 riastrad ret = -ENOMEM;
208 1.1 riastrad goto err;
209 1.1 riastrad }
210 1.1 riastrad
211 1.1 riastrad if (copy_from_user(master->unique, u->unique, master->unique_len)) {
212 1.1 riastrad ret = -EFAULT;
213 1.1 riastrad goto err;
214 1.1 riastrad }
215 1.1 riastrad
216 1.1 riastrad master->unique[master->unique_len] = '\0';
217 1.1 riastrad
218 1.1 riastrad bus_name = dev->driver->bus->get_name(dev);
219 1.2 christos size_t devlen = strlen(bus_name) + strlen(master->unique) + 2;
220 1.2 christos dev->devname = kmalloc(devlen, GFP_KERNEL);
221 1.1 riastrad if (!dev->devname) {
222 1.1 riastrad ret = -ENOMEM;
223 1.1 riastrad goto err;
224 1.1 riastrad }
225 1.1 riastrad
226 1.2 christos snprintf(dev->devname, devlen, "%s@%s", bus_name,
227 1.1 riastrad master->unique);
228 1.1 riastrad
229 1.1 riastrad /* Return error if the busid submitted doesn't match the device's actual
230 1.1 riastrad * busid.
231 1.1 riastrad */
232 1.1 riastrad ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
233 1.1 riastrad if (ret != 3) {
234 1.1 riastrad ret = -EINVAL;
235 1.1 riastrad goto err;
236 1.1 riastrad }
237 1.1 riastrad
238 1.1 riastrad domain = bus >> 8;
239 1.1 riastrad bus &= 0xff;
240 1.1 riastrad
241 1.1 riastrad if ((domain != drm_get_pci_domain(dev)) ||
242 1.1 riastrad (bus != dev->pdev->bus->number) ||
243 1.1 riastrad (slot != PCI_SLOT(dev->pdev->devfn)) ||
244 1.1 riastrad (func != PCI_FUNC(dev->pdev->devfn))) {
245 1.1 riastrad ret = -EINVAL;
246 1.1 riastrad goto err;
247 1.1 riastrad }
248 1.1 riastrad return 0;
249 1.1 riastrad err:
250 1.1 riastrad return ret;
251 1.1 riastrad }
252 1.1 riastrad
253 1.1 riastrad
254 1.1 riastrad static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
255 1.1 riastrad {
256 1.1 riastrad if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
257 1.1 riastrad (p->busnum & 0xff) != dev->pdev->bus->number ||
258 1.1 riastrad p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
259 1.1 riastrad return -EINVAL;
260 1.1 riastrad
261 1.1 riastrad p->irq = dev->pdev->irq;
262 1.1 riastrad
263 1.1 riastrad DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
264 1.1 riastrad p->irq);
265 1.1 riastrad return 0;
266 1.1 riastrad }
267 1.1 riastrad
268 1.1 riastrad int drm_pci_agp_init(struct drm_device *dev)
269 1.1 riastrad {
270 1.1 riastrad if (drm_core_has_AGP(dev)) {
271 1.1 riastrad if (drm_pci_device_is_agp(dev))
272 1.1 riastrad dev->agp = drm_agp_init(dev);
273 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
274 1.1 riastrad && (dev->agp == NULL)) {
275 1.1 riastrad DRM_ERROR("Cannot initialize the agpgart module.\n");
276 1.1 riastrad return -EINVAL;
277 1.1 riastrad }
278 1.1 riastrad if (drm_core_has_MTRR(dev)) {
279 1.1 riastrad if (dev->agp)
280 1.1 riastrad dev->agp->agp_mtrr =
281 1.1 riastrad mtrr_add(dev->agp->agp_info.aper_base,
282 1.1 riastrad dev->agp->agp_info.aper_size *
283 1.1 riastrad 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
284 1.1 riastrad }
285 1.1 riastrad }
286 1.1 riastrad return 0;
287 1.1 riastrad }
288 1.1 riastrad
289 1.1 riastrad static struct drm_bus drm_pci_bus = {
290 1.1 riastrad .bus_type = DRIVER_BUS_PCI,
291 1.1 riastrad .get_irq = drm_pci_get_irq,
292 1.1 riastrad .get_name = drm_pci_get_name,
293 1.1 riastrad .set_busid = drm_pci_set_busid,
294 1.1 riastrad .set_unique = drm_pci_set_unique,
295 1.1 riastrad .irq_by_busid = drm_pci_irq_by_busid,
296 1.1 riastrad .agp_init = drm_pci_agp_init,
297 1.1 riastrad };
298 1.1 riastrad
299 1.1 riastrad /**
300 1.1 riastrad * Register.
301 1.1 riastrad *
302 1.1 riastrad * \param pdev - PCI device structure
303 1.1 riastrad * \param ent entry from the PCI ID table with device type flags
304 1.1 riastrad * \return zero on success or a negative number on failure.
305 1.1 riastrad *
306 1.1 riastrad * Attempt to gets inter module "drm" information. If we are first
307 1.1 riastrad * then register the character device and inter module information.
308 1.1 riastrad * Try and register, if we fail to register, backout previous work.
309 1.1 riastrad */
310 1.1 riastrad int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
311 1.1 riastrad struct drm_driver *driver)
312 1.1 riastrad {
313 1.1 riastrad struct drm_device *dev;
314 1.1 riastrad int ret;
315 1.1 riastrad
316 1.1 riastrad DRM_DEBUG("\n");
317 1.1 riastrad
318 1.1 riastrad dev = kzalloc(sizeof(*dev), GFP_KERNEL);
319 1.1 riastrad if (!dev)
320 1.1 riastrad return -ENOMEM;
321 1.1 riastrad
322 1.1 riastrad ret = pci_enable_device(pdev);
323 1.1 riastrad if (ret)
324 1.1 riastrad goto err_g1;
325 1.1 riastrad
326 1.1 riastrad dev->pdev = pdev;
327 1.1 riastrad dev->dev = &pdev->dev;
328 1.1 riastrad
329 1.1 riastrad dev->pci_device = pdev->device;
330 1.1 riastrad dev->pci_vendor = pdev->vendor;
331 1.1 riastrad
332 1.1 riastrad #ifdef __alpha__
333 1.1 riastrad dev->hose = pdev->sysdata;
334 1.1 riastrad #endif
335 1.1 riastrad
336 1.1 riastrad mutex_lock(&drm_global_mutex);
337 1.1 riastrad
338 1.1 riastrad if ((ret = drm_fill_in_dev(dev, ent, driver))) {
339 1.1 riastrad printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
340 1.1 riastrad goto err_g2;
341 1.1 riastrad }
342 1.1 riastrad
343 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET)) {
344 1.1 riastrad pci_set_drvdata(pdev, dev);
345 1.1 riastrad ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
346 1.1 riastrad if (ret)
347 1.1 riastrad goto err_g2;
348 1.1 riastrad }
349 1.1 riastrad
350 1.1 riastrad if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
351 1.1 riastrad goto err_g3;
352 1.1 riastrad
353 1.1 riastrad if (dev->driver->load) {
354 1.1 riastrad ret = dev->driver->load(dev, ent->driver_data);
355 1.1 riastrad if (ret)
356 1.1 riastrad goto err_g4;
357 1.1 riastrad }
358 1.1 riastrad
359 1.1 riastrad /* setup the grouping for the legacy output */
360 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET)) {
361 1.1 riastrad ret = drm_mode_group_init_legacy_group(dev,
362 1.1 riastrad &dev->primary->mode_group);
363 1.1 riastrad if (ret)
364 1.1 riastrad goto err_g4;
365 1.1 riastrad }
366 1.1 riastrad
367 1.1 riastrad list_add_tail(&dev->driver_item, &driver->device_list);
368 1.1 riastrad
369 1.1 riastrad DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
370 1.1 riastrad driver->name, driver->major, driver->minor, driver->patchlevel,
371 1.1 riastrad driver->date, pci_name(pdev), dev->primary->index);
372 1.1 riastrad
373 1.1 riastrad mutex_unlock(&drm_global_mutex);
374 1.1 riastrad return 0;
375 1.1 riastrad
376 1.1 riastrad err_g4:
377 1.1 riastrad drm_put_minor(&dev->primary);
378 1.1 riastrad err_g3:
379 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET))
380 1.1 riastrad drm_put_minor(&dev->control);
381 1.1 riastrad err_g2:
382 1.1 riastrad pci_disable_device(pdev);
383 1.1 riastrad err_g1:
384 1.1 riastrad kfree(dev);
385 1.1 riastrad mutex_unlock(&drm_global_mutex);
386 1.1 riastrad return ret;
387 1.1 riastrad }
388 1.1 riastrad EXPORT_SYMBOL(drm_get_pci_dev);
389 1.1 riastrad
390 1.1 riastrad /**
391 1.1 riastrad * PCI device initialization. Called direct from modules at load time.
392 1.1 riastrad *
393 1.1 riastrad * \return zero on success or a negative number on failure.
394 1.1 riastrad *
395 1.1 riastrad * Initializes a drm_device structures,registering the
396 1.1 riastrad * stubs and initializing the AGP device.
397 1.1 riastrad *
398 1.1 riastrad * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
399 1.1 riastrad * after the initialization for driver customization.
400 1.1 riastrad */
401 1.1 riastrad int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
402 1.1 riastrad {
403 1.1 riastrad struct pci_dev *pdev = NULL;
404 1.1 riastrad const struct pci_device_id *pid;
405 1.1 riastrad int i;
406 1.1 riastrad
407 1.1 riastrad DRM_DEBUG("\n");
408 1.1 riastrad
409 1.1 riastrad INIT_LIST_HEAD(&driver->device_list);
410 1.1 riastrad driver->kdriver.pci = pdriver;
411 1.1 riastrad driver->bus = &drm_pci_bus;
412 1.1 riastrad
413 1.1 riastrad if (driver->driver_features & DRIVER_MODESET)
414 1.1 riastrad return pci_register_driver(pdriver);
415 1.1 riastrad
416 1.1 riastrad /* If not using KMS, fall back to stealth mode manual scanning. */
417 1.1 riastrad for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
418 1.1 riastrad pid = &pdriver->id_table[i];
419 1.1 riastrad
420 1.1 riastrad /* Loop around setting up a DRM device for each PCI device
421 1.1 riastrad * matching our ID and device class. If we had the internal
422 1.1 riastrad * function that pci_get_subsys and pci_get_class used, we'd
423 1.1 riastrad * be able to just pass pid in instead of doing a two-stage
424 1.1 riastrad * thing.
425 1.1 riastrad */
426 1.1 riastrad pdev = NULL;
427 1.1 riastrad while ((pdev =
428 1.1 riastrad pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
429 1.1 riastrad pid->subdevice, pdev)) != NULL) {
430 1.1 riastrad if ((pdev->class & pid->class_mask) != pid->class)
431 1.1 riastrad continue;
432 1.1 riastrad
433 1.1 riastrad /* stealth mode requires a manual probe */
434 1.1 riastrad pci_dev_get(pdev);
435 1.1 riastrad drm_get_pci_dev(pdev, pid, driver);
436 1.1 riastrad }
437 1.1 riastrad }
438 1.1 riastrad return 0;
439 1.1 riastrad }
440 1.1 riastrad
441 1.1 riastrad #else
442 1.1 riastrad
443 1.1 riastrad int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
444 1.1 riastrad {
445 1.1 riastrad return -1;
446 1.1 riastrad }
447 1.1 riastrad
448 1.1 riastrad #endif
449 1.1 riastrad
450 1.1 riastrad EXPORT_SYMBOL(drm_pci_init);
451 1.1 riastrad
452 1.1 riastrad /*@}*/
453 1.1 riastrad void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
454 1.1 riastrad {
455 1.1 riastrad struct drm_device *dev, *tmp;
456 1.1 riastrad DRM_DEBUG("\n");
457 1.1 riastrad
458 1.1 riastrad if (driver->driver_features & DRIVER_MODESET) {
459 1.1 riastrad pci_unregister_driver(pdriver);
460 1.1 riastrad } else {
461 1.1 riastrad list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
462 1.1 riastrad drm_put_dev(dev);
463 1.1 riastrad }
464 1.1 riastrad DRM_INFO("Module unloaded\n");
465 1.1 riastrad }
466 1.1 riastrad EXPORT_SYMBOL(drm_pci_exit);
467 1.1 riastrad
468 1.1 riastrad int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
469 1.1 riastrad {
470 1.1 riastrad struct pci_dev *root;
471 1.1 riastrad int pos;
472 1.1 riastrad u32 lnkcap = 0, lnkcap2 = 0;
473 1.1 riastrad
474 1.1 riastrad *mask = 0;
475 1.1 riastrad if (!dev->pdev)
476 1.1 riastrad return -EINVAL;
477 1.1 riastrad
478 1.1 riastrad if (!pci_is_pcie(dev->pdev))
479 1.1 riastrad return -EINVAL;
480 1.1 riastrad
481 1.1 riastrad root = dev->pdev->bus->self;
482 1.1 riastrad
483 1.1 riastrad pos = pci_pcie_cap(root);
484 1.1 riastrad if (!pos)
485 1.1 riastrad return -EINVAL;
486 1.1 riastrad
487 1.1 riastrad /* we've been informed via and serverworks don't make the cut */
488 1.1 riastrad if (root->vendor == PCI_VENDOR_ID_VIA ||
489 1.1 riastrad root->vendor == PCI_VENDOR_ID_SERVERWORKS)
490 1.1 riastrad return -EINVAL;
491 1.1 riastrad
492 1.1 riastrad pci_read_config_dword(root, pos + PCI_EXP_LNKCAP, &lnkcap);
493 1.1 riastrad pci_read_config_dword(root, pos + PCI_EXP_LNKCAP2, &lnkcap2);
494 1.1 riastrad
495 1.1 riastrad lnkcap &= PCI_EXP_LNKCAP_SLS;
496 1.1 riastrad lnkcap2 &= 0xfe;
497 1.1 riastrad
498 1.1 riastrad if (lnkcap2) { /* PCIE GEN 3.0 */
499 1.1 riastrad if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
500 1.1 riastrad *mask |= DRM_PCIE_SPEED_25;
501 1.1 riastrad if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
502 1.1 riastrad *mask |= DRM_PCIE_SPEED_50;
503 1.1 riastrad if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
504 1.1 riastrad *mask |= DRM_PCIE_SPEED_80;
505 1.1 riastrad } else {
506 1.1 riastrad if (lnkcap & 1)
507 1.1 riastrad *mask |= DRM_PCIE_SPEED_25;
508 1.1 riastrad if (lnkcap & 2)
509 1.1 riastrad *mask |= DRM_PCIE_SPEED_50;
510 1.1 riastrad }
511 1.1 riastrad
512 1.1 riastrad DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2);
513 1.1 riastrad return 0;
514 1.1 riastrad }
515 1.1 riastrad EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);
516