xref: /dflybsd-src/sys/dev/drm/drm_pci.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
11b13d190SFrançois Tigeot /*
21b13d190SFrançois Tigeot  * Copyright 2003 José Fonseca.
31b13d190SFrançois Tigeot  * Copyright 2003 Leif Delgass.
47f3c3d6fSHasso Tepper  * All Rights Reserved.
57f3c3d6fSHasso Tepper  *
67f3c3d6fSHasso Tepper  * Permission is hereby granted, free of charge, to any person obtaining a
77f3c3d6fSHasso Tepper  * copy of this software and associated documentation files (the "Software"),
87f3c3d6fSHasso Tepper  * to deal in the Software without restriction, including without limitation
97f3c3d6fSHasso Tepper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107f3c3d6fSHasso Tepper  * and/or sell copies of the Software, and to permit persons to whom the
117f3c3d6fSHasso Tepper  * Software is furnished to do so, subject to the following conditions:
127f3c3d6fSHasso Tepper  *
137f3c3d6fSHasso Tepper  * The above copyright notice and this permission notice (including the next
147f3c3d6fSHasso Tepper  * paragraph) shall be included in all copies or substantial portions of the
157f3c3d6fSHasso Tepper  * Software.
167f3c3d6fSHasso Tepper  *
177f3c3d6fSHasso Tepper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187f3c3d6fSHasso Tepper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197f3c3d6fSHasso Tepper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
201b13d190SFrançois Tigeot  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
217f3c3d6fSHasso Tepper  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
227f3c3d6fSHasso Tepper  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
237f3c3d6fSHasso Tepper  */
247f3c3d6fSHasso Tepper 
25d4d73b30SFrançois Tigeot #include <linux/pci.h>
26a85cb24fSFrançois Tigeot #include <linux/slab.h>
27d4d73b30SFrançois Tigeot #include <linux/dma-mapping.h>
28477eb7f9SFrançois Tigeot #include <linux/export.h>
29a85cb24fSFrançois Tigeot #include <drm/drm_pci.h>
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
312c9916cdSFrançois Tigeot #include "drm_internal.h"
32477eb7f9SFrançois Tigeot #include "drm_legacy.h"
337f3c3d6fSHasso Tepper 
347f3c3d6fSHasso Tepper /**
35d4d73b30SFrançois Tigeot  * drm_pci_alloc - Allocate a PCI consistent memory block, for DMA.
36d4d73b30SFrançois Tigeot  * @dev: DRM device
37d4d73b30SFrançois Tigeot  * @size: size of block to allocate
38d4d73b30SFrançois Tigeot  * @align: alignment of block
39d4d73b30SFrançois Tigeot  *
40a85cb24fSFrançois Tigeot  * FIXME: This is a needless abstraction of the Linux dma-api and should be
41a85cb24fSFrançois Tigeot  * removed.
42a85cb24fSFrançois Tigeot  *
43d4d73b30SFrançois Tigeot  * Return: A handle to the allocated memory block on success or NULL on
44d4d73b30SFrançois Tigeot  * failure.
457f3c3d6fSHasso Tepper  */
drm_pci_alloc(struct drm_device * dev,size_t size,size_t align)46b31e9d59SFrançois Tigeot drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align)
477f3c3d6fSHasso Tepper {
487f3c3d6fSHasso Tepper 	drm_dma_handle_t *dmah;
49d4d73b30SFrançois Tigeot 	unsigned long addr;
50d4d73b30SFrançois Tigeot 	size_t sz;
517f3c3d6fSHasso Tepper 
52c9fdb286SFrançois Tigeot 	/* pci_alloc_consistent only guarantees alignment to the smallest
53c9fdb286SFrançois Tigeot 	 * PAGE_SIZE order which is greater than or equal to the requested size.
54c9fdb286SFrançois Tigeot 	 * Return NULL here for now to make sure nobody tries for larger alignment
55c9fdb286SFrançois Tigeot 	 */
56c9fdb286SFrançois Tigeot 	if (align > size)
57c9fdb286SFrançois Tigeot 		return NULL;
58c9fdb286SFrançois Tigeot 
59*3f2dd94aSFrançois Tigeot 	dmah = kmalloc(sizeof(drm_dma_handle_t), M_DRM, GFP_KERNEL);
60c9fdb286SFrançois Tigeot 	if (!dmah)
617f3c3d6fSHasso Tepper 		return NULL;
627f3c3d6fSHasso Tepper 
63c9fdb286SFrançois Tigeot 	dmah->size = size;
64d4d73b30SFrançois Tigeot 	dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL);
65b3705d71SHasso Tepper 
66d4d73b30SFrançois Tigeot 	if (dmah->vaddr == NULL) {
67175896dfSzrj 		kfree(dmah);
687f3c3d6fSHasso Tepper 		return NULL;
697f3c3d6fSHasso Tepper 	}
707f3c3d6fSHasso Tepper 
71c9fdb286SFrançois Tigeot 	memset(dmah->vaddr, 0, size);
72c9fdb286SFrançois Tigeot 
73d4d73b30SFrançois Tigeot 	/* XXX - Is virt_to_page() legal for consistent mem? */
74d4d73b30SFrançois Tigeot 	/* Reserve */
75d4d73b30SFrançois Tigeot 	for (addr = (unsigned long)dmah->vaddr, sz = size;
76d4d73b30SFrançois Tigeot 	     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
77d4d73b30SFrançois Tigeot #if 0
78d4d73b30SFrançois Tigeot 		SetPageReserved(virt_to_page((void *)addr));
79d4d73b30SFrançois Tigeot #endif
80d4d73b30SFrançois Tigeot 	}
81d4d73b30SFrançois Tigeot 
827f3c3d6fSHasso Tepper 	return dmah;
837f3c3d6fSHasso Tepper }
847f3c3d6fSHasso Tepper 
85d4d73b30SFrançois Tigeot EXPORT_SYMBOL(drm_pci_alloc);
86d4d73b30SFrançois Tigeot 
871b13d190SFrançois Tigeot /*
881b13d190SFrançois Tigeot  * Free a PCI consistent memory block without freeing its descriptor.
891b13d190SFrançois Tigeot  *
901b13d190SFrançois Tigeot  * This function is for internal use in the Linux-specific DRM core code.
917f3c3d6fSHasso Tepper  */
__drm_legacy_pci_free(struct drm_device * dev,drm_dma_handle_t * dmah)921b13d190SFrançois Tigeot void __drm_legacy_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
937f3c3d6fSHasso Tepper {
94d4d73b30SFrançois Tigeot 	unsigned long addr;
95d4d73b30SFrançois Tigeot 	size_t sz;
967f3c3d6fSHasso Tepper 
97d4d73b30SFrançois Tigeot 	if (dmah->vaddr) {
98d4d73b30SFrançois Tigeot 		/* XXX - Is virt_to_page() legal for consistent mem? */
99d4d73b30SFrançois Tigeot 		/* Unreserve */
100d4d73b30SFrançois Tigeot 		for (addr = (unsigned long)dmah->vaddr, sz = dmah->size;
101d4d73b30SFrançois Tigeot 		     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
102d4d73b30SFrançois Tigeot #if 0
103d4d73b30SFrançois Tigeot 			ClearPageReserved(virt_to_page((void *)addr));
104d4d73b30SFrançois Tigeot #endif
105d4d73b30SFrançois Tigeot 		}
106d4d73b30SFrançois Tigeot 		dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
107d4d73b30SFrançois Tigeot 				  dmah->busaddr);
108d4d73b30SFrançois Tigeot 	}
1097f3c3d6fSHasso Tepper }
1107f3c3d6fSHasso Tepper 
1111b13d190SFrançois Tigeot /**
1121b13d190SFrançois Tigeot  * drm_pci_free - Free a PCI consistent memory block
1131b13d190SFrançois Tigeot  * @dev: DRM device
1141b13d190SFrançois Tigeot  * @dmah: handle to memory block
115a85cb24fSFrançois Tigeot  *
116a85cb24fSFrançois Tigeot  * FIXME: This is a needless abstraction of the Linux dma-api and should be
117a85cb24fSFrançois Tigeot  * removed.
1181b13d190SFrançois Tigeot  */
drm_pci_free(struct drm_device * dev,drm_dma_handle_t * dmah)1191b13d190SFrançois Tigeot void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
1201b13d190SFrançois Tigeot {
1211b13d190SFrançois Tigeot 	__drm_legacy_pci_free(dev, dmah);
1221b13d190SFrançois Tigeot 	kfree(dmah);
1231b13d190SFrançois Tigeot }
1246f486c69SFrançois Tigeot 
125d4d73b30SFrançois Tigeot EXPORT_SYMBOL(drm_pci_free);
126d4d73b30SFrançois Tigeot 
127d4d73b30SFrançois Tigeot #ifdef CONFIG_PCI
128d4d73b30SFrançois Tigeot 
drm_get_pci_domain(struct drm_device * dev)129d4d73b30SFrançois Tigeot static int drm_get_pci_domain(struct drm_device *dev)
130d4d73b30SFrançois Tigeot {
131d4d73b30SFrançois Tigeot #ifndef __alpha__
132d4d73b30SFrançois Tigeot 	/* For historical reasons, drm_get_pci_domain() is busticated
133d4d73b30SFrançois Tigeot 	 * on most archs and has to remain so for userspace interface
134d4d73b30SFrançois Tigeot 	 * < 1.4, except on alpha which was right from the beginning
135d4d73b30SFrançois Tigeot 	 */
136d4d73b30SFrançois Tigeot 	if (dev->if_version < 0x10004)
137d4d73b30SFrançois Tigeot 		return 0;
138d4d73b30SFrançois Tigeot #endif /* __alpha__ */
139d4d73b30SFrançois Tigeot 
140d4d73b30SFrançois Tigeot #if 0
141d4d73b30SFrançois Tigeot 	return pci_domain_nr(dev->pdev->bus);
142d4d73b30SFrançois Tigeot #else
143d4d73b30SFrançois Tigeot 	return dev->pci_domain;
144d4d73b30SFrançois Tigeot #endif
145d4d73b30SFrançois Tigeot }
146d4d73b30SFrançois Tigeot 
drm_pci_set_busid(struct drm_device * dev,struct drm_master * master)147d4d73b30SFrançois Tigeot int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
148d4d73b30SFrançois Tigeot {
149d4d73b30SFrançois Tigeot 	master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
150d4d73b30SFrançois Tigeot 					drm_get_pci_domain(dev),
151d4d73b30SFrançois Tigeot 					dev->pdev->bus->number,
152d4d73b30SFrançois Tigeot 					PCI_SLOT(dev->pdev->devfn),
153d4d73b30SFrançois Tigeot 					PCI_FUNC(dev->pdev->devfn));
154d4d73b30SFrançois Tigeot 	if (!master->unique)
155d4d73b30SFrançois Tigeot 		return -ENOMEM;
156d4d73b30SFrançois Tigeot 
157d4d73b30SFrançois Tigeot 	master->unique_len = strlen(master->unique);
158d4d73b30SFrançois Tigeot 	return 0;
159d4d73b30SFrançois Tigeot }
160d4d73b30SFrançois Tigeot 
drm_pci_irq_by_busid(struct drm_device * dev,struct drm_irq_busid * p)161d4d73b30SFrançois Tigeot static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
162d4d73b30SFrançois Tigeot {
163*3f2dd94aSFrançois Tigeot 	if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
164*3f2dd94aSFrançois Tigeot 	    (p->busnum & 0xff) != dev->pdev->bus->number ||
165*3f2dd94aSFrançois Tigeot 	    p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
166d4d73b30SFrançois Tigeot 		return -EINVAL;
167d4d73b30SFrançois Tigeot 
168d4d73b30SFrançois Tigeot 	p->irq = dev->pdev->irq;
169d4d73b30SFrançois Tigeot 
170d4d73b30SFrançois Tigeot 	DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
171d4d73b30SFrançois Tigeot 		  p->irq);
172d4d73b30SFrançois Tigeot 	return 0;
173d4d73b30SFrançois Tigeot }
174d4d73b30SFrançois Tigeot 
175d4d73b30SFrançois Tigeot /**
176d4d73b30SFrançois Tigeot  * drm_irq_by_busid - Get interrupt from bus ID
177d4d73b30SFrançois Tigeot  * @dev: DRM device
178d4d73b30SFrançois Tigeot  * @data: IOCTL parameter pointing to a drm_irq_busid structure
179d4d73b30SFrançois Tigeot  * @file_priv: DRM file private.
180d4d73b30SFrançois Tigeot  *
181d4d73b30SFrançois Tigeot  * Finds the PCI device with the specified bus id and gets its IRQ number.
182d4d73b30SFrançois Tigeot  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
183d4d73b30SFrançois Tigeot  * to that of the device that this DRM instance attached to.
184d4d73b30SFrançois Tigeot  *
185d4d73b30SFrançois Tigeot  * Return: 0 on success or a negative error code on failure.
186d4d73b30SFrançois Tigeot  */
drm_irq_by_busid(struct drm_device * dev,void * data,struct drm_file * file_priv)187d4d73b30SFrançois Tigeot int drm_irq_by_busid(struct drm_device *dev, void *data,
188d4d73b30SFrançois Tigeot 		     struct drm_file *file_priv)
189d4d73b30SFrançois Tigeot {
190d4d73b30SFrançois Tigeot 	struct drm_irq_busid *p = data;
191d4d73b30SFrançois Tigeot 
1921dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
193d4d73b30SFrançois Tigeot 		return -EINVAL;
194d4d73b30SFrançois Tigeot 
195d4d73b30SFrançois Tigeot 	/* UMS was only ever support on PCI devices. */
196d4d73b30SFrançois Tigeot 	if (WARN_ON(!dev->pdev))
197d4d73b30SFrançois Tigeot 		return -EINVAL;
198d4d73b30SFrançois Tigeot 
199d4d73b30SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
200d4d73b30SFrançois Tigeot 		return -EINVAL;
201d4d73b30SFrançois Tigeot 
202d4d73b30SFrançois Tigeot 	return drm_pci_irq_by_busid(dev, p);
203d4d73b30SFrançois Tigeot }
204d4d73b30SFrançois Tigeot 
205*3f2dd94aSFrançois Tigeot #ifdef __DragonFly__
206*3f2dd94aSFrançois Tigeot int
drm_getpciinfo(struct drm_device * dev,void * data,struct drm_file * file_priv)207*3f2dd94aSFrançois Tigeot drm_getpciinfo(struct drm_device *dev, void *data, struct drm_file *file_priv)
208*3f2dd94aSFrançois Tigeot {
209*3f2dd94aSFrançois Tigeot 	struct drm_pciinfo *info = data;
210*3f2dd94aSFrançois Tigeot 
211*3f2dd94aSFrançois Tigeot 	info->domain = 0;
212*3f2dd94aSFrançois Tigeot 	info->bus = dev->pci_bus;
213*3f2dd94aSFrançois Tigeot 	info->dev = PCI_SLOT(dev->pdev->devfn);
214*3f2dd94aSFrançois Tigeot 	info->func = PCI_FUNC(dev->pdev->devfn);
215*3f2dd94aSFrançois Tigeot 	info->vendor_id = dev->pdev->vendor;
216*3f2dd94aSFrançois Tigeot 	info->device_id = dev->pdev->device;
217*3f2dd94aSFrançois Tigeot 	info->subvendor_id = dev->pdev->subsystem_vendor;
218*3f2dd94aSFrançois Tigeot 	info->subdevice_id = dev->pdev->subsystem_device;
219*3f2dd94aSFrançois Tigeot 	info->revision_id = 0;
220*3f2dd94aSFrançois Tigeot 
221*3f2dd94aSFrançois Tigeot 	return 0;
222*3f2dd94aSFrançois Tigeot }
223*3f2dd94aSFrançois Tigeot #endif
224*3f2dd94aSFrançois Tigeot 
225d4d73b30SFrançois Tigeot /**
226fcf17be7SFrançois Tigeot  * drm_get_pci_dev - Register a PCI device with the DRM subsystem
227fcf17be7SFrançois Tigeot  * @pdev: PCI device
228fcf17be7SFrançois Tigeot  * @ent: entry from the PCI ID table that matches @pdev
229fcf17be7SFrançois Tigeot  * @driver: DRM device driver
230fcf17be7SFrançois Tigeot  *
231fcf17be7SFrançois Tigeot  * Attempt to gets inter module "drm" information. If we are first
232fcf17be7SFrançois Tigeot  * then register the character device and inter module information.
233fcf17be7SFrançois Tigeot  * Try and register, if we fail to register, backout previous work.
234fcf17be7SFrançois Tigeot  *
235fcf17be7SFrançois Tigeot  * NOTE: This function is deprecated, please use drm_dev_alloc() and
236a85cb24fSFrançois Tigeot  * drm_dev_register() instead and remove your &drm_driver.load callback.
237fcf17be7SFrançois Tigeot  *
238fcf17be7SFrançois Tigeot  * Return: 0 on success or a negative error code on failure.
239fcf17be7SFrançois Tigeot  */
drm_get_pci_dev(struct pci_dev * pdev,const struct pci_device_id * ent,struct drm_driver * driver)240fcf17be7SFrançois Tigeot int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
241fcf17be7SFrançois Tigeot 		    struct drm_driver *driver)
242fcf17be7SFrançois Tigeot {
243fcf17be7SFrançois Tigeot 	struct drm_device *dev;
244fcf17be7SFrançois Tigeot 	int ret;
245fcf17be7SFrançois Tigeot 
246fcf17be7SFrançois Tigeot 	DRM_DEBUG("\n");
247fcf17be7SFrançois Tigeot 
248fcf17be7SFrançois Tigeot 	dev = drm_dev_alloc(driver, &pdev->dev);
2491dedbd3bSFrançois Tigeot 	if (IS_ERR(dev))
2501dedbd3bSFrançois Tigeot 		return PTR_ERR(dev);
251fcf17be7SFrançois Tigeot 
252fcf17be7SFrançois Tigeot #if 0
253fcf17be7SFrançois Tigeot 	ret = pci_enable_device(pdev);
254fcf17be7SFrançois Tigeot 	if (ret)
255fcf17be7SFrançois Tigeot 		goto err_free;
256fcf17be7SFrançois Tigeot #endif
257fcf17be7SFrançois Tigeot 
258fcf17be7SFrançois Tigeot 	dev->pdev = pdev;
259fcf17be7SFrançois Tigeot #ifdef __alpha__
260fcf17be7SFrançois Tigeot 	dev->hose = pdev->sysdata;
261fcf17be7SFrançois Tigeot #endif
262fcf17be7SFrançois Tigeot 
263fcf17be7SFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET))
264fcf17be7SFrançois Tigeot 		pci_set_drvdata(pdev, dev);
265fcf17be7SFrançois Tigeot 
266fcf17be7SFrançois Tigeot #if 0
267fcf17be7SFrançois Tigeot 	drm_pci_agp_init(dev);
268fcf17be7SFrançois Tigeot #endif
269fcf17be7SFrançois Tigeot 
270fcf17be7SFrançois Tigeot 	ret = drm_dev_register(dev, ent->driver_data);
271fcf17be7SFrançois Tigeot 	if (ret)
272fcf17be7SFrançois Tigeot 		goto err_agp;
273fcf17be7SFrançois Tigeot 
274fcf17be7SFrançois Tigeot 	/* No locking needed since shadow-attach is single-threaded since it may
275fcf17be7SFrançois Tigeot 	 * only be called from the per-driver module init hook. */
2761dedbd3bSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
277fcf17be7SFrançois Tigeot 		list_add_tail(&dev->legacy_dev_list, &driver->legacy_dev_list);
278fcf17be7SFrançois Tigeot 
279fcf17be7SFrançois Tigeot 	return 0;
280fcf17be7SFrançois Tigeot 
281fcf17be7SFrançois Tigeot err_agp:
282fcf17be7SFrançois Tigeot #if 0
283fcf17be7SFrançois Tigeot 	drm_pci_agp_destroy(dev);
284fcf17be7SFrançois Tigeot 	pci_disable_device(pdev);
285fcf17be7SFrançois Tigeot err_free:
286fcf17be7SFrançois Tigeot 	drm_dev_unref(dev);
287fcf17be7SFrançois Tigeot #endif
288fcf17be7SFrançois Tigeot 	return ret;
289fcf17be7SFrançois Tigeot }
290fcf17be7SFrançois Tigeot EXPORT_SYMBOL(drm_get_pci_dev);
291fcf17be7SFrançois Tigeot 
292fcf17be7SFrançois Tigeot /**
293*3f2dd94aSFrançois Tigeot  * drm_legacy_pci_init - shadow-attach a legacy DRM PCI driver
294d4d73b30SFrançois Tigeot  * @driver: DRM device driver
295d4d73b30SFrançois Tigeot  * @pdriver: PCI device driver
296d4d73b30SFrançois Tigeot  *
297*3f2dd94aSFrançois Tigeot  * This is only used by legacy dri1 drivers and deprecated.
298d4d73b30SFrançois Tigeot  *
299d4d73b30SFrançois Tigeot  * Return: 0 on success or a negative error code on failure.
300d4d73b30SFrançois Tigeot  */
drm_legacy_pci_init(struct drm_driver * driver,struct pci_driver * pdriver)301*3f2dd94aSFrançois Tigeot int drm_legacy_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
302d4d73b30SFrançois Tigeot {
303d4d73b30SFrançois Tigeot #if 0
304d4d73b30SFrançois Tigeot 	struct pci_dev *pdev = NULL;
305d4d73b30SFrançois Tigeot 	const struct pci_device_id *pid;
306d4d73b30SFrançois Tigeot 	int i;
307d4d73b30SFrançois Tigeot #endif
308d4d73b30SFrançois Tigeot 
309d4d73b30SFrançois Tigeot 	DRM_DEBUG("\n");
310d4d73b30SFrançois Tigeot 
311*3f2dd94aSFrançois Tigeot 	if (WARN_ON(!(driver->driver_features & DRIVER_LEGACY)))
312*3f2dd94aSFrançois Tigeot 		return -EINVAL;
313d4d73b30SFrançois Tigeot 
314d4d73b30SFrançois Tigeot #if 0
315d4d73b30SFrançois Tigeot 	/* If not using KMS, fall back to stealth mode manual scanning. */
316d4d73b30SFrançois Tigeot 	INIT_LIST_HEAD(&driver->legacy_dev_list);
317d4d73b30SFrançois Tigeot 	for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
318d4d73b30SFrançois Tigeot 		pid = &pdriver->id_table[i];
319d4d73b30SFrançois Tigeot 
320d4d73b30SFrançois Tigeot 		/* Loop around setting up a DRM device for each PCI device
321d4d73b30SFrançois Tigeot 		 * matching our ID and device class.  If we had the internal
322d4d73b30SFrançois Tigeot 		 * function that pci_get_subsys and pci_get_class used, we'd
323d4d73b30SFrançois Tigeot 		 * be able to just pass pid in instead of doing a two-stage
324d4d73b30SFrançois Tigeot 		 * thing.
325d4d73b30SFrançois Tigeot 		 */
326d4d73b30SFrançois Tigeot 		pdev = NULL;
327d4d73b30SFrançois Tigeot 		while ((pdev =
328d4d73b30SFrançois Tigeot 			pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
329d4d73b30SFrançois Tigeot 				       pid->subdevice, pdev)) != NULL) {
330d4d73b30SFrançois Tigeot 			if ((pdev->class & pid->class_mask) != pid->class)
331d4d73b30SFrançois Tigeot 				continue;
332d4d73b30SFrançois Tigeot 
333d4d73b30SFrançois Tigeot 			/* stealth mode requires a manual probe */
334d4d73b30SFrançois Tigeot 			pci_dev_get(pdev);
335d4d73b30SFrançois Tigeot 			drm_get_pci_dev(pdev, pid, driver);
336d4d73b30SFrançois Tigeot 		}
337d4d73b30SFrançois Tigeot 	}
338d4d73b30SFrançois Tigeot #endif
339d4d73b30SFrançois Tigeot 	return 0;
340d4d73b30SFrançois Tigeot }
341*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_legacy_pci_init);
342d4d73b30SFrançois Tigeot 
drm_pcie_get_speed_cap_mask(struct drm_device * dev,u32 * mask)3436f486c69SFrançois Tigeot int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
3446f486c69SFrançois Tigeot {
345*3f2dd94aSFrançois Tigeot 	struct pci_dev *root;
346*3f2dd94aSFrançois Tigeot 	u32 lnkcap, lnkcap2;
3476f486c69SFrançois Tigeot 
3486f486c69SFrançois Tigeot 	*mask = 0;
349d4d73b30SFrançois Tigeot 	if (!dev->pdev)
350d4d73b30SFrançois Tigeot 		return -EINVAL;
3516f486c69SFrançois Tigeot 
352*3f2dd94aSFrançois Tigeot 	root = dev->pdev->bus->self;
3536f486c69SFrançois Tigeot 
3546f486c69SFrançois Tigeot 	/* we've been informed via and serverworks don't make the cut */
355*3f2dd94aSFrançois Tigeot 	if (root->vendor == PCI_VENDOR_ID_VIA ||
356*3f2dd94aSFrançois Tigeot 	    root->vendor == PCI_VENDOR_ID_SERVERWORKS)
3576f486c69SFrançois Tigeot 		return -EINVAL;
3586f486c69SFrançois Tigeot 
359*3f2dd94aSFrançois Tigeot 	pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
360*3f2dd94aSFrançois Tigeot 	pcie_capability_read_dword(root, PCI_EXP_LNKCAP2, &lnkcap2);
3616f486c69SFrançois Tigeot 
362e3ac6954Szrj 	if (lnkcap2) {	/* PCIe r3.0-compliant */
3636f486c69SFrançois Tigeot 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
3646f486c69SFrançois Tigeot 			*mask |= DRM_PCIE_SPEED_25;
3656f486c69SFrançois Tigeot 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
3666f486c69SFrançois Tigeot 			*mask |= DRM_PCIE_SPEED_50;
3676f486c69SFrançois Tigeot 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
3686f486c69SFrançois Tigeot 			*mask |= DRM_PCIE_SPEED_80;
369e3ac6954Szrj 	} else {	/* pre-r3.0 */
370e3ac6954Szrj 		if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
3716f486c69SFrançois Tigeot 			*mask |= DRM_PCIE_SPEED_25;
372e3ac6954Szrj 		if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
373d4d73b30SFrançois Tigeot 			*mask |= (DRM_PCIE_SPEED_25 | DRM_PCIE_SPEED_50);
3746f486c69SFrançois Tigeot 	}
3756f486c69SFrançois Tigeot 
376*3f2dd94aSFrançois Tigeot 	DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2);
3776f486c69SFrançois Tigeot 	return 0;
3786f486c69SFrançois Tigeot }
379e3ac6954Szrj EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);
380ba55f2f5SFrançois Tigeot 
drm_pcie_get_max_link_width(struct drm_device * dev,u32 * mlw)381aee94f86SFrançois Tigeot int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw)
382aee94f86SFrançois Tigeot {
383aee94f86SFrançois Tigeot 	struct pci_dev *root;
384*3f2dd94aSFrançois Tigeot 	u32 lnkcap;
385aee94f86SFrançois Tigeot 
386aee94f86SFrançois Tigeot 	*mlw = 0;
387aee94f86SFrançois Tigeot 	if (!dev->pdev)
388aee94f86SFrançois Tigeot 		return -EINVAL;
389aee94f86SFrançois Tigeot 
390aee94f86SFrançois Tigeot 	root = dev->pdev->bus->self;
391aee94f86SFrançois Tigeot 
392aee94f86SFrançois Tigeot 	pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
393aee94f86SFrançois Tigeot 
394aee94f86SFrançois Tigeot 	*mlw = (lnkcap & PCI_EXP_LNKCAP_MLW) >> 4;
395aee94f86SFrançois Tigeot 
396aee94f86SFrançois Tigeot 	DRM_INFO("probing mlw for device %x:%x = %x\n", root->vendor, root->device, lnkcap);
397aee94f86SFrançois Tigeot 	return 0;
398aee94f86SFrançois Tigeot }
399aee94f86SFrançois Tigeot EXPORT_SYMBOL(drm_pcie_get_max_link_width);
400aee94f86SFrançois Tigeot 
401d4d73b30SFrançois Tigeot #else
402d4d73b30SFrançois Tigeot 
drm_pci_agp_destroy(struct drm_device * dev)403d4d73b30SFrançois Tigeot void drm_pci_agp_destroy(struct drm_device *dev) {}
404d4d73b30SFrançois Tigeot 
drm_irq_by_busid(struct drm_device * dev,void * data,struct drm_file * file_priv)405ba55f2f5SFrançois Tigeot int drm_irq_by_busid(struct drm_device *dev, void *data,
406ba55f2f5SFrançois Tigeot 		     struct drm_file *file_priv)
407ba55f2f5SFrançois Tigeot {
408b922632fSImre Vadász 	return -EINVAL;
409ba55f2f5SFrançois Tigeot }
410d4d73b30SFrançois Tigeot #endif
411d4d73b30SFrançois Tigeot 
4121dedbd3bSFrançois Tigeot /**
413*3f2dd94aSFrançois Tigeot  * drm_legacy_pci_exit - unregister shadow-attach legacy DRM driver
4141dedbd3bSFrançois Tigeot  * @driver: DRM device driver
4151dedbd3bSFrançois Tigeot  * @pdriver: PCI device driver
4161dedbd3bSFrançois Tigeot  *
417*3f2dd94aSFrançois Tigeot  * Unregister a DRM driver shadow-attached through drm_legacy_pci_init(). This
418*3f2dd94aSFrançois Tigeot  * is deprecated and only used by dri1 drivers.
4191dedbd3bSFrançois Tigeot  */
drm_legacy_pci_exit(struct drm_driver * driver,struct pci_driver * pdriver)420*3f2dd94aSFrançois Tigeot void drm_legacy_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
4211dedbd3bSFrançois Tigeot {
4221dedbd3bSFrançois Tigeot 	struct drm_device *dev, *tmp;
4231dedbd3bSFrançois Tigeot 	DRM_DEBUG("\n");
4241dedbd3bSFrançois Tigeot 
4251dedbd3bSFrançois Tigeot 	if (!(driver->driver_features & DRIVER_LEGACY)) {
426*3f2dd94aSFrançois Tigeot 		WARN_ON(1);
4271dedbd3bSFrançois Tigeot 	} else {
4281dedbd3bSFrançois Tigeot 		list_for_each_entry_safe(dev, tmp, &driver->legacy_dev_list,
4291dedbd3bSFrançois Tigeot 					 legacy_dev_list) {
4301dedbd3bSFrançois Tigeot 			list_del(&dev->legacy_dev_list);
4311dedbd3bSFrançois Tigeot #if 0
4321dedbd3bSFrançois Tigeot 			drm_put_dev(dev);
4331dedbd3bSFrançois Tigeot #endif
4341dedbd3bSFrançois Tigeot 		}
4351dedbd3bSFrançois Tigeot 	}
4361dedbd3bSFrançois Tigeot 	DRM_INFO("Module unloaded\n");
4371dedbd3bSFrançois Tigeot }
438*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_legacy_pci_exit);
439