xref: /freebsd/sys/dev/pci/vga_pci.c (revision 39beb93c)
1 /*-
2  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
35  * drm(4) should attach as children of this device.
36  *
37  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
38  * in or rename it.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 
49 struct vga_pci_softc {
50 	device_t	vga_msi_child;	/* Child driver using MSI. */
51 };
52 
53 static int
54 vga_pci_probe(device_t dev)
55 {
56 
57 	switch (pci_get_class(dev)) {
58 	case PCIC_DISPLAY:
59 		break;
60 	case PCIC_OLD:
61 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
62 			return (ENXIO);
63 		break;
64 	default:
65 		return (ENXIO);
66 	}
67 	device_set_desc(dev, "VGA-compatible display");
68 	return (BUS_PROBE_GENERIC);
69 }
70 
71 static int
72 vga_pci_attach(device_t dev)
73 {
74 
75 	bus_generic_probe(dev);
76 
77 	/* Always create a drm child for now to make it easier on drm. */
78 	device_add_child(dev, "drm", -1);
79 	bus_generic_attach(dev);
80 	return (0);
81 }
82 
83 static int
84 vga_pci_suspend(device_t dev)
85 {
86 
87 	return (bus_generic_suspend(dev));
88 }
89 
90 static int
91 vga_pci_resume(device_t dev)
92 {
93 
94 	return (bus_generic_resume(dev));
95 }
96 
97 /* Bus interface. */
98 
99 static int
100 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
101 {
102 
103 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
104 }
105 
106 static int
107 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
108 {
109 
110 	return (EINVAL);
111 }
112 
113 static int
114 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
115     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
116     void **cookiep)
117 {
118 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
119 	    filter, intr, arg, cookiep));
120 }
121 
122 static int
123 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
124     void *cookie)
125 {
126 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
127 }
128 
129 static struct resource *
130 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
131     u_long start, u_long end, u_long count, u_int flags)
132 {
133 
134 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
135 }
136 
137 static int
138 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
139     struct resource *r)
140 {
141 
142 	return (bus_release_resource(dev, type, rid, r));
143 }
144 
145 /* PCI interface. */
146 
147 static uint32_t
148 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
149 {
150 
151 	return (pci_read_config(dev, reg, width));
152 }
153 
154 static void
155 vga_pci_write_config(device_t dev, device_t child, int reg,
156     uint32_t val, int width)
157 {
158 
159 	pci_write_config(dev, reg, val, width);
160 }
161 
162 static int
163 vga_pci_enable_busmaster(device_t dev, device_t child)
164 {
165 
166 	device_printf(dev, "child %s requested pci_enable_busmaster\n",
167 	    device_get_nameunit(child));
168 	return (pci_enable_busmaster(dev));
169 }
170 
171 static int
172 vga_pci_disable_busmaster(device_t dev, device_t child)
173 {
174 
175 	device_printf(dev, "child %s requested pci_disable_busmaster\n",
176 	    device_get_nameunit(child));
177 	return (pci_disable_busmaster(dev));
178 }
179 
180 static int
181 vga_pci_enable_io(device_t dev, device_t child, int space)
182 {
183 
184 	device_printf(dev, "child %s requested pci_enable_io\n",
185 	    device_get_nameunit(child));
186 	return (pci_enable_io(dev, space));
187 }
188 
189 static int
190 vga_pci_disable_io(device_t dev, device_t child, int space)
191 {
192 
193 	device_printf(dev, "child %s requested pci_disable_io\n",
194 	    device_get_nameunit(child));
195 	return (pci_disable_io(dev, space));
196 }
197 
198 static int
199 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
200 {
201 
202 	return (pci_get_vpd_ident(dev, identptr));
203 }
204 
205 static int
206 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
207     const char **vptr)
208 {
209 
210 	return (pci_get_vpd_readonly(dev, kw, vptr));
211 }
212 
213 static int
214 vga_pci_set_powerstate(device_t dev, device_t child, int state)
215 {
216 
217 	device_printf(dev, "child %s requested pci_set_powerstate\n",
218 	    device_get_nameunit(child));
219 	return (pci_set_powerstate(dev, state));
220 }
221 
222 static int
223 vga_pci_get_powerstate(device_t dev, device_t child)
224 {
225 
226 	device_printf(dev, "child %s requested pci_get_powerstate\n",
227 	    device_get_nameunit(child));
228 	return (pci_get_powerstate(dev));
229 }
230 
231 static int
232 vga_pci_assign_interrupt(device_t dev, device_t child)
233 {
234 
235 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
236 	    device_get_nameunit(child));
237 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
238 }
239 
240 static int
241 vga_pci_find_extcap(device_t dev, device_t child, int capability,
242     int *capreg)
243 {
244 
245 	return (pci_find_extcap(dev, capability, capreg));
246 }
247 
248 static int
249 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
250 {
251 	struct vga_pci_softc *sc;
252 	int error;
253 
254 	sc = device_get_softc(dev);
255 	if (sc->vga_msi_child != NULL)
256 		return (EBUSY);
257 	error = pci_alloc_msi(dev, count);
258 	if (error == 0)
259 		sc->vga_msi_child = child;
260 	return (error);
261 }
262 
263 static int
264 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
265 {
266 	struct vga_pci_softc *sc;
267 	int error;
268 
269 	sc = device_get_softc(dev);
270 	if (sc->vga_msi_child != NULL)
271 		return (EBUSY);
272 	error = pci_alloc_msix(dev, count);
273 	if (error == 0)
274 		sc->vga_msi_child = child;
275 	return (error);
276 }
277 
278 static int
279 vga_pci_remap_msix(device_t dev, device_t child, int count,
280     const u_int *vectors)
281 {
282 	struct vga_pci_softc *sc;
283 
284 	sc = device_get_softc(dev);
285 	if (sc->vga_msi_child != child)
286 		return (ENXIO);
287 	return (pci_remap_msix(dev, count, vectors));
288 }
289 
290 static int
291 vga_pci_release_msi(device_t dev, device_t child)
292 {
293 	struct vga_pci_softc *sc;
294 	int error;
295 
296 	sc = device_get_softc(dev);
297 	if (sc->vga_msi_child != child)
298 		return (ENXIO);
299 	error = pci_release_msi(dev);
300 	if (error == 0)
301 		sc->vga_msi_child = NULL;
302 	return (error);
303 }
304 
305 static int
306 vga_pci_msi_count(device_t dev, device_t child)
307 {
308 
309 	return (pci_msi_count(dev));
310 }
311 
312 static int
313 vga_pci_msix_count(device_t dev, device_t child)
314 {
315 
316 	return (pci_msix_count(dev));
317 }
318 
319 static device_method_t vga_pci_methods[] = {
320 	/* Device interface */
321 	DEVMETHOD(device_probe,		vga_pci_probe),
322 	DEVMETHOD(device_attach,	vga_pci_attach),
323 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
324 	DEVMETHOD(device_suspend,	vga_pci_suspend),
325 	DEVMETHOD(device_resume,	vga_pci_resume),
326 
327 	/* Bus interface */
328 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
329 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
330 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
331 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
332 
333 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
334 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
335 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
336 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
337 
338 	/* PCI interface */
339 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
340 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
341 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
342 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
343 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
344 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
345 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
346 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
347 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
348 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
349 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
350 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
351 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
352 	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
353 	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
354 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
355 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
356 	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
357 
358 	{ 0, 0 }
359 };
360 
361 static driver_t vga_pci_driver = {
362 	"vgapci",
363 	vga_pci_methods,
364 	sizeof(struct vga_pci_softc),
365 };
366 
367 static devclass_t vga_devclass;
368 
369 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
370