xref: /openbsd-src/sys/dev/pci/drm/drm_pci.c (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /*
2  * Copyright 2003 José Fonseca.
3  * Copyright 2003 Leif Delgass.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <linux/dma-mapping.h>
26 #include <linux/export.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 
30 #include <drm/drm.h>
31 #include <drm/drm_agpsupport.h>
32 #include <drm/drm_drv.h>
33 #include <drm/drm_pci.h>
34 #include <drm/drm_print.h>
35 
36 #include "drm_internal.h"
37 #include "drm_legacy.h"
38 
39 #ifdef __linux__
40 
41 /**
42  * drm_pci_alloc - Allocate a PCI consistent memory block, for DMA.
43  * @dev: DRM device
44  * @size: size of block to allocate
45  * @align: alignment of block
46  *
47  * FIXME: This is a needless abstraction of the Linux dma-api and should be
48  * removed.
49  *
50  * Return: A handle to the allocated memory block on success or NULL on
51  * failure.
52  */
53 drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align)
54 {
55 	drm_dma_handle_t *dmah;
56 
57 	/* pci_alloc_consistent only guarantees alignment to the smallest
58 	 * PAGE_SIZE order which is greater than or equal to the requested size.
59 	 * Return NULL here for now to make sure nobody tries for larger alignment
60 	 */
61 	if (align > size)
62 		return NULL;
63 
64 	dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
65 	if (!dmah)
66 		return NULL;
67 
68 	dmah->size = size;
69 	dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size,
70 					 &dmah->busaddr,
71 					 GFP_KERNEL);
72 
73 	if (dmah->vaddr == NULL) {
74 		kfree(dmah);
75 		return NULL;
76 	}
77 
78 	return dmah;
79 }
80 EXPORT_SYMBOL(drm_pci_alloc);
81 
82 /**
83  * drm_pci_free - Free a PCI consistent memory block
84  * @dev: DRM device
85  * @dmah: handle to memory block
86  *
87  * FIXME: This is a needless abstraction of the Linux dma-api and should be
88  * removed.
89  */
90 void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
91 {
92 	dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
93 			  dmah->busaddr);
94 	kfree(dmah);
95 }
96 
97 EXPORT_SYMBOL(drm_pci_free);
98 
99 #endif /* __linux__ */
100 
101 static int drm_get_pci_domain(struct drm_device *dev)
102 {
103 #ifndef __alpha__
104 	/* For historical reasons, drm_get_pci_domain() is busticated
105 	 * on most archs and has to remain so for userspace interface
106 	 * < 1.4, except on alpha which was right from the beginning
107 	 */
108 	if (dev->if_version < 0x10004)
109 		return 0;
110 #endif /* __alpha__ */
111 
112 	return pci_domain_nr(dev->pdev->bus);
113 }
114 
115 int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
116 {
117 	master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
118 					drm_get_pci_domain(dev),
119 					dev->pdev->bus->number,
120 					PCI_SLOT(dev->pdev->devfn),
121 					PCI_FUNC(dev->pdev->devfn));
122 	if (!master->unique)
123 		return -ENOMEM;
124 
125 	master->unique_len = strlen(master->unique);
126 	return 0;
127 }
128 
129 #ifdef __linux__
130 
131 static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
132 {
133 	if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
134 	    (p->busnum & 0xff) != dev->pdev->bus->number ||
135 	    p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
136 		return -EINVAL;
137 
138 	p->irq = dev->pdev->irq;
139 
140 	DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
141 		  p->irq);
142 	return 0;
143 }
144 
145 /**
146  * drm_irq_by_busid - Get interrupt from bus ID
147  * @dev: DRM device
148  * @data: IOCTL parameter pointing to a drm_irq_busid structure
149  * @file_priv: DRM file private.
150  *
151  * Finds the PCI device with the specified bus id and gets its IRQ number.
152  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
153  * to that of the device that this DRM instance attached to.
154  *
155  * Return: 0 on success or a negative error code on failure.
156  */
157 int drm_irq_by_busid(struct drm_device *dev, void *data,
158 		     struct drm_file *file_priv)
159 {
160 	struct drm_irq_busid *p = data;
161 
162 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
163 		return -EOPNOTSUPP;
164 
165 	/* UMS was only ever support on PCI devices. */
166 	if (WARN_ON(!dev->pdev))
167 		return -EINVAL;
168 
169 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
170 		return -EOPNOTSUPP;
171 
172 	return drm_pci_irq_by_busid(dev, p);
173 }
174 
175 void drm_pci_agp_destroy(struct drm_device *dev)
176 {
177 	if (dev->agp) {
178 		arch_phys_wc_del(dev->agp->agp_mtrr);
179 		drm_legacy_agp_clear(dev);
180 		kfree(dev->agp);
181 		dev->agp = NULL;
182 	}
183 }
184 
185 #ifdef CONFIG_DRM_LEGACY
186 
187 static void drm_pci_agp_init(struct drm_device *dev)
188 {
189 	if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
190 		if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP))
191 			dev->agp = drm_agp_init(dev);
192 		if (dev->agp) {
193 			dev->agp->agp_mtrr = arch_phys_wc_add(
194 				dev->agp->agp_info.aper_base,
195 				dev->agp->agp_info.aper_size *
196 				1024 * 1024);
197 		}
198 	}
199 }
200 
201 static int drm_get_pci_dev(struct pci_dev *pdev,
202 			   const struct pci_device_id *ent,
203 			   struct drm_driver *driver)
204 {
205 	struct drm_device *dev;
206 	int ret;
207 
208 	DRM_DEBUG("\n");
209 
210 	dev = drm_dev_alloc(driver, &pdev->dev);
211 	if (IS_ERR(dev))
212 		return PTR_ERR(dev);
213 
214 	ret = pci_enable_device(pdev);
215 	if (ret)
216 		goto err_free;
217 
218 	dev->pdev = pdev;
219 #ifdef __alpha__
220 	dev->hose = pdev->sysdata;
221 #endif
222 
223 	if (drm_core_check_feature(dev, DRIVER_MODESET))
224 		pci_set_drvdata(pdev, dev);
225 
226 	drm_pci_agp_init(dev);
227 
228 	ret = drm_dev_register(dev, ent->driver_data);
229 	if (ret)
230 		goto err_agp;
231 
232 	/* No locking needed since shadow-attach is single-threaded since it may
233 	 * only be called from the per-driver module init hook. */
234 	if (drm_core_check_feature(dev, DRIVER_LEGACY))
235 		list_add_tail(&dev->legacy_dev_list, &driver->legacy_dev_list);
236 
237 	return 0;
238 
239 err_agp:
240 	drm_pci_agp_destroy(dev);
241 	pci_disable_device(pdev);
242 err_free:
243 	drm_dev_put(dev);
244 	return ret;
245 }
246 
247 /**
248  * drm_legacy_pci_init - shadow-attach a legacy DRM PCI driver
249  * @driver: DRM device driver
250  * @pdriver: PCI device driver
251  *
252  * This is only used by legacy dri1 drivers and deprecated.
253  *
254  * Return: 0 on success or a negative error code on failure.
255  */
256 int drm_legacy_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
257 {
258 	struct pci_dev *pdev = NULL;
259 	const struct pci_device_id *pid;
260 	int i;
261 
262 	DRM_DEBUG("\n");
263 
264 	if (WARN_ON(!(driver->driver_features & DRIVER_LEGACY)))
265 		return -EINVAL;
266 
267 	/* If not using KMS, fall back to stealth mode manual scanning. */
268 	INIT_LIST_HEAD(&driver->legacy_dev_list);
269 	for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
270 		pid = &pdriver->id_table[i];
271 
272 		/* Loop around setting up a DRM device for each PCI device
273 		 * matching our ID and device class.  If we had the internal
274 		 * function that pci_get_subsys and pci_get_class used, we'd
275 		 * be able to just pass pid in instead of doing a two-stage
276 		 * thing.
277 		 */
278 		pdev = NULL;
279 		while ((pdev =
280 			pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
281 				       pid->subdevice, pdev)) != NULL) {
282 			if ((pdev->class & pid->class_mask) != pid->class)
283 				continue;
284 
285 			/* stealth mode requires a manual probe */
286 			pci_dev_get(pdev);
287 			drm_get_pci_dev(pdev, pid, driver);
288 		}
289 	}
290 	return 0;
291 }
292 EXPORT_SYMBOL(drm_legacy_pci_init);
293 
294 /**
295  * drm_legacy_pci_exit - unregister shadow-attach legacy DRM driver
296  * @driver: DRM device driver
297  * @pdriver: PCI device driver
298  *
299  * Unregister a DRM driver shadow-attached through drm_legacy_pci_init(). This
300  * is deprecated and only used by dri1 drivers.
301  */
302 void drm_legacy_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
303 {
304 	struct drm_device *dev, *tmp;
305 	DRM_DEBUG("\n");
306 
307 	if (!(driver->driver_features & DRIVER_LEGACY)) {
308 		WARN_ON(1);
309 	} else {
310 		list_for_each_entry_safe(dev, tmp, &driver->legacy_dev_list,
311 					 legacy_dev_list) {
312 			list_del(&dev->legacy_dev_list);
313 			drm_put_dev(dev);
314 		}
315 	}
316 	DRM_INFO("Module unloaded\n");
317 }
318 EXPORT_SYMBOL(drm_legacy_pci_exit);
319 
320 #endif
321 
322 #endif /* __linux__ */
323