xref: /openbsd-src/sys/dev/pci/drm/drm_pci.c (revision 5ca02815211fc20fa71222bf4e6148b043e505b3)
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/list.h>
28 #include <linux/mutex.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 
32 #include <drm/drm.h>
33 #include <drm/drm_drv.h>
34 #include <drm/drm_print.h>
35 
36 #include "drm_internal.h"
37 #include "drm_legacy.h"
38 
39 #ifdef CONFIG_DRM_LEGACY
40 /* List of devices hanging off drivers with stealth attach. */
41 static LIST_HEAD(legacy_dev_list);
42 static DEFINE_MUTEX(legacy_dev_list_lock);
43 #endif
44 
drm_get_pci_domain(struct drm_device * dev)45 static int drm_get_pci_domain(struct drm_device *dev)
46 {
47 #ifndef __alpha__
48 	/* For historical reasons, drm_get_pci_domain() is busticated
49 	 * on most archs and has to remain so for userspace interface
50 	 * < 1.4, except on alpha which was right from the beginning
51 	 */
52 	if (dev->if_version < 0x10004)
53 		return 0;
54 #endif /* __alpha__ */
55 
56 #ifdef __linux__
57 	return pci_domain_nr(to_pci_dev(dev->dev)->bus);
58 #else
59 	return pci_domain_nr(dev->pdev->bus);
60 #endif
61 }
62 
drm_pci_set_busid(struct drm_device * dev,struct drm_master * master)63 int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
64 {
65 #ifdef __linux__
66 	struct pci_dev *pdev = to_pci_dev(dev->dev);
67 #else
68 	struct pci_dev *pdev = dev->pdev;
69 #endif
70 
71 	master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
72 					drm_get_pci_domain(dev),
73 					pdev->bus->number,
74 					PCI_SLOT(pdev->devfn),
75 					PCI_FUNC(pdev->devfn));
76 	if (!master->unique)
77 		return -ENOMEM;
78 
79 	master->unique_len = strlen(master->unique);
80 	return 0;
81 }
82 
83 #ifdef CONFIG_DRM_LEGACY
84 
drm_legacy_pci_irq_by_busid(struct drm_device * dev,struct drm_irq_busid * p)85 static int drm_legacy_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
86 {
87 	struct pci_dev *pdev = to_pci_dev(dev->dev);
88 
89 	if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
90 	    (p->busnum & 0xff) != pdev->bus->number ||
91 	    p->devnum != PCI_SLOT(pdev->devfn) || p->funcnum != PCI_FUNC(pdev->devfn))
92 		return -EINVAL;
93 
94 	p->irq = pdev->irq;
95 
96 	DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
97 		  p->irq);
98 	return 0;
99 }
100 
101 /**
102  * drm_legacy_irq_by_busid - Get interrupt from bus ID
103  * @dev: DRM device
104  * @data: IOCTL parameter pointing to a drm_irq_busid structure
105  * @file_priv: DRM file private.
106  *
107  * Finds the PCI device with the specified bus id and gets its IRQ number.
108  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
109  * to that of the device that this DRM instance attached to.
110  *
111  * Return: 0 on success or a negative error code on failure.
112  */
drm_legacy_irq_by_busid(struct drm_device * dev,void * data,struct drm_file * file_priv)113 int drm_legacy_irq_by_busid(struct drm_device *dev, void *data,
114 			    struct drm_file *file_priv)
115 {
116 	struct drm_irq_busid *p = data;
117 
118 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
119 		return -EOPNOTSUPP;
120 
121 	/* UMS was only ever support on PCI devices. */
122 	if (WARN_ON(!dev_is_pci(dev->dev)))
123 		return -EINVAL;
124 
125 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
126 		return -EOPNOTSUPP;
127 
128 	return drm_legacy_pci_irq_by_busid(dev, p);
129 }
130 
drm_legacy_pci_agp_destroy(struct drm_device * dev)131 void drm_legacy_pci_agp_destroy(struct drm_device *dev)
132 {
133 	if (dev->agp) {
134 		arch_phys_wc_del(dev->agp->agp_mtrr);
135 		drm_legacy_agp_clear(dev);
136 		kfree(dev->agp);
137 		dev->agp = NULL;
138 	}
139 }
140 
drm_legacy_pci_agp_init(struct drm_device * dev)141 static void drm_legacy_pci_agp_init(struct drm_device *dev)
142 {
143 	if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
144 		if (pci_find_capability(to_pci_dev(dev->dev), PCI_CAP_ID_AGP))
145 			dev->agp = drm_legacy_agp_init(dev);
146 		if (dev->agp) {
147 			dev->agp->agp_mtrr = arch_phys_wc_add(
148 				dev->agp->agp_info.aper_base,
149 				dev->agp->agp_info.aper_size *
150 				1024 * 1024);
151 		}
152 	}
153 }
154 
drm_legacy_get_pci_dev(struct pci_dev * pdev,const struct pci_device_id * ent,const struct drm_driver * driver)155 static int drm_legacy_get_pci_dev(struct pci_dev *pdev,
156 				  const struct pci_device_id *ent,
157 				  const struct drm_driver *driver)
158 {
159 	struct drm_device *dev;
160 	int ret;
161 
162 	DRM_DEBUG("\n");
163 
164 	dev = drm_dev_alloc(driver, &pdev->dev);
165 	if (IS_ERR(dev))
166 		return PTR_ERR(dev);
167 
168 	ret = pci_enable_device(pdev);
169 	if (ret)
170 		goto err_free;
171 
172 #ifdef __alpha__
173 	dev->hose = pdev->sysdata;
174 #endif
175 
176 	drm_legacy_pci_agp_init(dev);
177 
178 	ret = drm_dev_register(dev, ent->driver_data);
179 	if (ret)
180 		goto err_agp;
181 
182 	if (drm_core_check_feature(dev, DRIVER_LEGACY)) {
183 		mutex_lock(&legacy_dev_list_lock);
184 		list_add_tail(&dev->legacy_dev_list, &legacy_dev_list);
185 		mutex_unlock(&legacy_dev_list_lock);
186 	}
187 
188 	return 0;
189 
190 err_agp:
191 	drm_legacy_pci_agp_destroy(dev);
192 	pci_disable_device(pdev);
193 err_free:
194 	drm_dev_put(dev);
195 	return ret;
196 }
197 
198 /**
199  * drm_legacy_pci_init - shadow-attach a legacy DRM PCI driver
200  * @driver: DRM device driver
201  * @pdriver: PCI device driver
202  *
203  * This is only used by legacy dri1 drivers and deprecated.
204  *
205  * Return: 0 on success or a negative error code on failure.
206  */
drm_legacy_pci_init(const struct drm_driver * driver,struct pci_driver * pdriver)207 int drm_legacy_pci_init(const struct drm_driver *driver,
208 			struct pci_driver *pdriver)
209 {
210 	struct pci_dev *pdev = NULL;
211 	const struct pci_device_id *pid;
212 	int i;
213 
214 	DRM_DEBUG("\n");
215 
216 	if (WARN_ON(!(driver->driver_features & DRIVER_LEGACY)))
217 		return -EINVAL;
218 
219 	/* If not using KMS, fall back to stealth mode manual scanning. */
220 	for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
221 		pid = &pdriver->id_table[i];
222 
223 		/* Loop around setting up a DRM device for each PCI device
224 		 * matching our ID and device class.  If we had the internal
225 		 * function that pci_get_subsys and pci_get_class used, we'd
226 		 * be able to just pass pid in instead of doing a two-stage
227 		 * thing.
228 		 */
229 		pdev = NULL;
230 		while ((pdev =
231 			pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
232 				       pid->subdevice, pdev)) != NULL) {
233 			if ((pdev->class & pid->class_mask) != pid->class)
234 				continue;
235 
236 			/* stealth mode requires a manual probe */
237 			pci_dev_get(pdev);
238 			drm_legacy_get_pci_dev(pdev, pid, driver);
239 		}
240 	}
241 	return 0;
242 }
243 EXPORT_SYMBOL(drm_legacy_pci_init);
244 
245 /**
246  * drm_legacy_pci_exit - unregister shadow-attach legacy DRM driver
247  * @driver: DRM device driver
248  * @pdriver: PCI device driver
249  *
250  * Unregister a DRM driver shadow-attached through drm_legacy_pci_init(). This
251  * is deprecated and only used by dri1 drivers.
252  */
drm_legacy_pci_exit(const struct drm_driver * driver,struct pci_driver * pdriver)253 void drm_legacy_pci_exit(const struct drm_driver *driver,
254 			 struct pci_driver *pdriver)
255 {
256 	struct drm_device *dev, *tmp;
257 
258 	DRM_DEBUG("\n");
259 
260 	if (!(driver->driver_features & DRIVER_LEGACY)) {
261 		WARN_ON(1);
262 	} else {
263 		mutex_lock(&legacy_dev_list_lock);
264 		list_for_each_entry_safe(dev, tmp, &legacy_dev_list,
265 					 legacy_dev_list) {
266 			if (dev->driver == driver) {
267 				list_del(&dev->legacy_dev_list);
268 				drm_put_dev(dev);
269 			}
270 		}
271 		mutex_unlock(&legacy_dev_list_lock);
272 	}
273 	DRM_INFO("Module unloaded\n");
274 }
275 EXPORT_SYMBOL(drm_legacy_pci_exit);
276 
277 #endif
278