linux_pci.c revision 331996
1/*-
2 * Copyright (c) 2015-2016 Mellanox Technologies, Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/11/sys/compat/linuxkpi/common/src/linux_pci.c 331996 2018-04-04 08:41:10Z hselasky $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/malloc.h>
33#include <sys/kernel.h>
34#include <sys/sysctl.h>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/bus.h>
38#include <sys/fcntl.h>
39#include <sys/file.h>
40#include <sys/filio.h>
41#include <sys/rwlock.h>
42
43#include <vm/vm.h>
44#include <vm/pmap.h>
45
46#include <machine/stdarg.h>
47
48#include <linux/kobject.h>
49#include <linux/device.h>
50#include <linux/slab.h>
51#include <linux/module.h>
52#include <linux/cdev.h>
53#include <linux/file.h>
54#include <linux/sysfs.h>
55#include <linux/mm.h>
56#include <linux/io.h>
57#include <linux/vmalloc.h>
58#include <linux/pci.h>
59#include <linux/compat.h>
60
61static device_probe_t linux_pci_probe;
62static device_attach_t linux_pci_attach;
63static device_detach_t linux_pci_detach;
64static device_suspend_t linux_pci_suspend;
65static device_resume_t linux_pci_resume;
66static device_shutdown_t linux_pci_shutdown;
67
68static device_method_t pci_methods[] = {
69	DEVMETHOD(device_probe, linux_pci_probe),
70	DEVMETHOD(device_attach, linux_pci_attach),
71	DEVMETHOD(device_detach, linux_pci_detach),
72	DEVMETHOD(device_suspend, linux_pci_suspend),
73	DEVMETHOD(device_resume, linux_pci_resume),
74	DEVMETHOD(device_shutdown, linux_pci_shutdown),
75	DEVMETHOD_END
76};
77
78static struct pci_driver *
79linux_pci_find(device_t dev, const struct pci_device_id **idp)
80{
81	const struct pci_device_id *id;
82	struct pci_driver *pdrv;
83	uint16_t vendor;
84	uint16_t device;
85
86	vendor = pci_get_vendor(dev);
87	device = pci_get_device(dev);
88
89	spin_lock(&pci_lock);
90	list_for_each_entry(pdrv, &pci_drivers, links) {
91		for (id = pdrv->id_table; id->vendor != 0; id++) {
92			if (vendor == id->vendor && device == id->device) {
93				*idp = id;
94				spin_unlock(&pci_lock);
95				return (pdrv);
96			}
97		}
98	}
99	spin_unlock(&pci_lock);
100	return (NULL);
101}
102
103static int
104linux_pci_probe(device_t dev)
105{
106	const struct pci_device_id *id;
107	struct pci_driver *pdrv;
108
109	if ((pdrv = linux_pci_find(dev, &id)) == NULL)
110		return (ENXIO);
111	if (device_get_driver(dev) != &pdrv->bsddriver)
112		return (ENXIO);
113	device_set_desc(dev, pdrv->name);
114	return (0);
115}
116
117static int
118linux_pci_attach(device_t dev)
119{
120	struct resource_list_entry *rle;
121	struct pci_bus *pbus;
122	struct pci_dev *pdev;
123	struct pci_devinfo *dinfo;
124	struct pci_driver *pdrv;
125	const struct pci_device_id *id;
126	device_t parent;
127	devclass_t devclass;
128	int error;
129
130	linux_set_current(curthread);
131
132	pdrv = linux_pci_find(dev, &id);
133	pdev = device_get_softc(dev);
134
135	parent = device_get_parent(dev);
136	devclass = device_get_devclass(parent);
137	if (pdrv->isdrm) {
138		dinfo = device_get_ivars(parent);
139		device_set_ivars(dev, dinfo);
140	} else {
141		dinfo = device_get_ivars(dev);
142	}
143
144	pdev->dev.parent = &linux_root_device;
145	pdev->dev.bsddev = dev;
146	INIT_LIST_HEAD(&pdev->dev.irqents);
147	pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
148	pdev->device = id->device;
149	pdev->vendor = id->vendor;
150	pdev->subsystem_vendor = dinfo->cfg.subvendor;
151	pdev->subsystem_device = dinfo->cfg.subdevice;
152	pdev->class = pci_get_class(dev);
153	pdev->revision = pci_get_revid(dev);
154	pdev->dev.dma_mask = &pdev->dma_mask;
155	pdev->pdrv = pdrv;
156	kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
157	kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
158	kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
159	    kobject_name(&pdev->dev.kobj));
160	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0);
161	if (rle != NULL)
162		pdev->dev.irq = rle->start;
163	else
164		pdev->dev.irq = LINUX_IRQ_INVALID;
165	pdev->irq = pdev->dev.irq;
166
167	if (pdev->bus == NULL) {
168		pbus = malloc(sizeof(*pbus), M_DEVBUF, M_WAITOK | M_ZERO);
169		pbus->self = pdev;
170		pbus->number = pci_get_bus(dev);
171		pdev->bus = pbus;
172	}
173
174	spin_lock(&pci_lock);
175	list_add(&pdev->links, &pci_devices);
176	spin_unlock(&pci_lock);
177
178	error = pdrv->probe(pdev, id);
179	if (error) {
180		spin_lock(&pci_lock);
181		list_del(&pdev->links);
182		spin_unlock(&pci_lock);
183		put_device(&pdev->dev);
184		error = -error;
185	}
186	return (error);
187}
188
189static int
190linux_pci_detach(device_t dev)
191{
192	struct pci_dev *pdev;
193
194	linux_set_current(curthread);
195	pdev = device_get_softc(dev);
196
197	pdev->pdrv->remove(pdev);
198
199	spin_lock(&pci_lock);
200	list_del(&pdev->links);
201	spin_unlock(&pci_lock);
202	put_device(&pdev->dev);
203
204	return (0);
205}
206
207static int
208linux_pci_suspend(device_t dev)
209{
210	const struct dev_pm_ops *pmops;
211	struct pm_message pm = { };
212	struct pci_dev *pdev;
213	int error;
214
215	error = 0;
216	linux_set_current(curthread);
217	pdev = device_get_softc(dev);
218	pmops = pdev->pdrv->driver.pm;
219
220	if (pdev->pdrv->suspend != NULL)
221		error = -pdev->pdrv->suspend(pdev, pm);
222	else if (pmops != NULL && pmops->suspend != NULL) {
223		error = -pmops->suspend(&pdev->dev);
224		if (error == 0 && pmops->suspend_late != NULL)
225			error = -pmops->suspend_late(&pdev->dev);
226	}
227	return (error);
228}
229
230static int
231linux_pci_resume(device_t dev)
232{
233	const struct dev_pm_ops *pmops;
234	struct pci_dev *pdev;
235	int error;
236
237	error = 0;
238	linux_set_current(curthread);
239	pdev = device_get_softc(dev);
240	pmops = pdev->pdrv->driver.pm;
241
242	if (pdev->pdrv->resume != NULL)
243		error = -pdev->pdrv->resume(pdev);
244	else if (pmops != NULL && pmops->resume != NULL) {
245		if (pmops->resume_early != NULL)
246			error = -pmops->resume_early(&pdev->dev);
247		if (error == 0 && pmops->resume != NULL)
248			error = -pmops->resume(&pdev->dev);
249	}
250	return (error);
251}
252
253static int
254linux_pci_shutdown(device_t dev)
255{
256	struct pci_dev *pdev;
257
258	linux_set_current(curthread);
259	pdev = device_get_softc(dev);
260	if (pdev->pdrv->shutdown != NULL)
261		pdev->pdrv->shutdown(pdev);
262	return (0);
263}
264
265static int
266_linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
267{
268	int error;
269
270	linux_set_current(curthread);
271	spin_lock(&pci_lock);
272	list_add(&pdrv->links, &pci_drivers);
273	spin_unlock(&pci_lock);
274	pdrv->bsddriver.name = pdrv->name;
275	pdrv->bsddriver.methods = pci_methods;
276	pdrv->bsddriver.size = sizeof(struct pci_dev);
277
278	mtx_lock(&Giant);
279	error = devclass_add_driver(dc, &pdrv->bsddriver,
280	    BUS_PASS_DEFAULT, &pdrv->bsdclass);
281	mtx_unlock(&Giant);
282	return (-error);
283}
284
285int
286linux_pci_register_driver(struct pci_driver *pdrv)
287{
288	devclass_t dc;
289
290	dc = devclass_find("pci");
291	if (dc == NULL)
292		return (-ENXIO);
293	pdrv->isdrm = false;
294	return (_linux_pci_register_driver(pdrv, dc));
295}
296
297int
298linux_pci_register_drm_driver(struct pci_driver *pdrv)
299{
300	devclass_t dc;
301
302	dc = devclass_create("vgapci");
303	if (dc == NULL)
304		return (-ENXIO);
305	pdrv->isdrm = true;
306	pdrv->name = "drmn";
307	return (_linux_pci_register_driver(pdrv, dc));
308}
309
310void
311linux_pci_unregister_driver(struct pci_driver *pdrv)
312{
313	devclass_t bus;
314
315	bus = devclass_find("pci");
316
317	spin_lock(&pci_lock);
318	list_del(&pdrv->links);
319	spin_unlock(&pci_lock);
320	mtx_lock(&Giant);
321	if (bus != NULL)
322		devclass_delete_driver(bus, &pdrv->bsddriver);
323	mtx_unlock(&Giant);
324}
325