xref: /freebsd/sys/dev/pci/vga_pci.c (revision 9dbf5b0e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following 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 AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
31  * drm(4) should attach as children of this device.
32  *
33  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
34  * in or rename it.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/rman.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 
45 #if defined(__amd64__) || defined(__i386__)
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #endif
49 
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 
53 #include <compat/x86bios/x86bios.h> /* To re-POST the card. */
54 
55 struct vga_resource {
56 	struct resource	*vr_res;
57 	int	vr_refs;
58 };
59 
60 struct vga_pci_softc {
61 	device_t	vga_msi_child;	/* Child driver using MSI. */
62 	struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
63 	struct vga_resource vga_bios;
64 };
65 
66 SYSCTL_DECL(_hw_pci);
67 
68 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
69 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
70     int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
71     u_int flags);
72 static int	vga_pci_release_resource(device_t dev, device_t child,
73     struct resource *r);
74 
75 int vga_pci_default_unit = -1;
76 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
77     &vga_pci_default_unit, -1, "Default VGA-compatible display");
78 
79 int
vga_pci_is_boot_display(device_t dev)80 vga_pci_is_boot_display(device_t dev)
81 {
82 	int unit;
83 	device_t pcib;
84 	uint16_t config;
85 
86 	/* Check that the given device is a video card */
87 	if ((pci_get_class(dev) != PCIC_DISPLAY &&
88 	    (pci_get_class(dev) != PCIC_OLD ||
89 	     pci_get_subclass(dev) != PCIS_OLD_VGA)))
90 		return (0);
91 
92 	unit = device_get_unit(dev);
93 
94 	if (vga_pci_default_unit >= 0) {
95 		/*
96 		 * The boot display device was determined by a previous
97 		 * call to this function, or the user forced it using
98 		 * the hw.pci.default_vgapci_unit tunable.
99 		 */
100 		return (vga_pci_default_unit == unit);
101 	}
102 
103 	/*
104 	 * The primary video card used as a boot display must have the
105 	 * "I/O" and "Memory Address Space Decoding" bits set in its
106 	 * Command register.
107 	 *
108 	 * Furthermore, if the card is attached to a bridge, instead of
109 	 * the root PCI bus, the bridge must have the "VGA Enable" bit
110 	 * set in its Control register.
111 	 */
112 
113 	pcib = device_get_parent(device_get_parent(dev));
114 	if (device_get_devclass(device_get_parent(pcib)) ==
115 	    devclass_find("pci")) {
116 		/*
117 		 * The parent bridge is a PCI-to-PCI bridge: check the
118 		 * value of the "VGA Enable" bit.
119 		 */
120 		config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
121 		if ((config & PCIB_BCR_VGA_ENABLE) == 0)
122 			return (0);
123 	}
124 
125 	config = pci_read_config(dev, PCIR_COMMAND, 2);
126 	if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
127 		return (0);
128 
129 	/*
130 	 * Disable interrupts until a chipset driver is loaded for
131 	 * this PCI device. Else unhandled display adapter interrupts
132 	 * might freeze the CPU.
133 	 */
134 	pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2);
135 
136 	/* This video card is the boot display: record its unit number. */
137 	vga_pci_default_unit = unit;
138 	device_set_flags(dev, 1);
139 
140 	return (1);
141 }
142 
143 static void
vga_pci_reset(device_t dev)144 vga_pci_reset(device_t dev)
145 {
146 	int ps;
147 	/*
148 	 * FLR is unsupported on GPUs so attempt a power-management reset by cycling
149 	 * the device in/out of D3 state.
150 	 * PCI spec says we can only go into D3 state from D0 state.
151 	 * Transition from D[12] into D0 before going to D3 state.
152 	 */
153 	ps = pci_get_powerstate(dev);
154 	if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
155 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
156 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3)
157 		pci_set_powerstate(dev, PCI_POWERSTATE_D3);
158 	pci_set_powerstate(dev, ps);
159 }
160 
161 void *
vga_pci_map_bios(device_t dev,size_t * size)162 vga_pci_map_bios(device_t dev, size_t *size)
163 {
164 	struct vga_resource *vr;
165 	struct resource *res;
166 	device_t pcib;
167 	uint32_t rom_addr;
168 	uint16_t config;
169 	volatile unsigned char *bios;
170 	int i, rid, found;
171 
172 #if defined(__amd64__) || defined(__i386__)
173 	if (vga_pci_is_boot_display(dev)) {
174 		/*
175 		 * On x86, the System BIOS copy the default display
176 		 * device's Video BIOS at a fixed location in system
177 		 * memory (0xC0000, 128 kBytes long) at boot time.
178 		 *
179 		 * We use this copy for the default boot device, because
180 		 * the original ROM may not be valid after boot.
181 		 */
182 
183 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
184 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
185 	}
186 #endif
187 
188 	pcib = device_get_parent(device_get_parent(dev));
189 	if (device_get_devclass(device_get_parent(pcib)) ==
190 	    devclass_find("pci")) {
191 		/*
192 		 * The parent bridge is a PCI-to-PCI bridge: check the
193 		 * value of the "VGA Enable" bit.
194 		 */
195 		config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
196 		if ((config & PCIB_BCR_VGA_ENABLE) == 0) {
197 			config |= PCIB_BCR_VGA_ENABLE;
198 			pci_write_config(pcib, PCIR_BRIDGECTL_1, config, 2);
199 		}
200 	}
201 
202 	switch(pci_read_config(dev, PCIR_HDRTYPE, 1)) {
203 	case PCIM_HDRTYPE_BRIDGE:
204 		rid = PCIR_BIOS_1;
205 		break;
206 	case PCIM_HDRTYPE_CARDBUS:
207 		rid = 0;
208 		break;
209 	default:
210 		rid = PCIR_BIOS;
211 		break;
212 	}
213 	if (rid == 0)
214 		return (NULL);
215 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0,
216 	    ~0, 1, RF_ACTIVE);
217 
218 	if (res == NULL) {
219 		device_printf(dev, "vga_pci_alloc_resource failed\n");
220 		return (NULL);
221 	}
222 	bios = rman_get_virtual(res);
223 	*size = rman_get_size(res);
224 	for (found = i = 0; i < hz; i++) {
225 		found = (bios[0] == 0x55 && bios[1] == 0xaa);
226 		if (found)
227 			break;
228 		pause("vgabios", 1);
229 	}
230 	if (found)
231 		return (__DEVOLATILE(void *, bios));
232 	if (bootverbose)
233 		device_printf(dev, "initial ROM mapping failed -- resetting\n");
234 
235 	/*
236 	 * Enable ROM decode
237 	 */
238 	vga_pci_reset(dev);
239 	rom_addr = pci_read_config(dev, rid, 4);
240 	rom_addr &= 0x7ff;
241 	rom_addr |= rman_get_start(res) | 0x1;
242 	pci_write_config(dev, rid, rom_addr, 4);
243 	vr = lookup_res(device_get_softc(dev), rid);
244 	vga_pci_release_resource(dev, NULL, vr->vr_res);
245 
246 	/*
247 	 * re-allocate
248 	 */
249 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0,
250 	    ~0, 1, RF_ACTIVE);
251 	if (res == NULL) {
252 		device_printf(dev, "vga_pci_alloc_resource failed\n");
253 		return (NULL);
254 	}
255 	bios = rman_get_virtual(res);
256 	*size = rman_get_size(res);
257 	for (found = i = 0; i < 3*hz; i++) {
258 		found = (bios[0] == 0x55 && bios[1] == 0xaa);
259 		if (found)
260 			break;
261 		pause("vgabios", 1);
262 	}
263 	if (found)
264 		return (__DEVOLATILE(void *, bios));
265 	device_printf(dev, "ROM mapping failed\n");
266 	vr = lookup_res(device_get_softc(dev), rid);
267 	vga_pci_release_resource(dev, NULL, vr->vr_res);
268 	return (NULL);
269 }
270 
271 void
vga_pci_unmap_bios(device_t dev,void * bios)272 vga_pci_unmap_bios(device_t dev, void *bios)
273 {
274 	struct vga_resource *vr;
275 	int rid;
276 
277 	if (bios == NULL) {
278 		return;
279 	}
280 
281 #if defined(__amd64__) || defined(__i386__)
282 	if (vga_pci_is_boot_display(dev)) {
283 		/* We mapped the BIOS shadow copy located at 0xC0000. */
284 		pmap_unmapdev(bios, VGA_PCI_BIOS_SHADOW_SIZE);
285 
286 		return;
287 	}
288 #endif
289 	switch(pci_read_config(dev, PCIR_HDRTYPE, 1)) {
290 	case PCIM_HDRTYPE_BRIDGE:
291 		rid = PCIR_BIOS_1;
292 		break;
293 	case PCIM_HDRTYPE_CARDBUS:
294 		rid = 0;
295 		break;
296 	default:
297 		rid = PCIR_BIOS;
298 		break;
299 	}
300 	if (rid == 0)
301 		return;
302 	/*
303 	 * Look up the PCIR_BIOS resource in our softc.  It should match
304 	 * the address we returned previously.
305 	 */
306 	vr = lookup_res(device_get_softc(dev), rid);
307 	KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
308 	KASSERT(rman_get_virtual(vr->vr_res) == bios,
309 	    ("vga_pci_unmap_bios: mismatch"));
310 	vga_pci_release_resource(dev, NULL, vr->vr_res);
311 }
312 
313 int
vga_pci_repost(device_t dev)314 vga_pci_repost(device_t dev)
315 {
316 #if defined(__amd64__) || defined(__i386__)
317 	x86regs_t regs;
318 
319 	if (!vga_pci_is_boot_display(dev))
320 		return (EINVAL);
321 
322 	if (x86bios_get_orm(VGA_PCI_BIOS_SHADOW_ADDR) == NULL)
323 		return (ENOTSUP);
324 
325 	x86bios_init_regs(&regs);
326 
327 	regs.R_AH = pci_get_bus(dev);
328 	regs.R_AL = (pci_get_slot(dev) << 3) | (pci_get_function(dev) & 0x07);
329 	regs.R_DL = 0x80;
330 
331 	device_printf(dev, "REPOSTing\n");
332 	x86bios_call(&regs, X86BIOS_PHYSTOSEG(VGA_PCI_BIOS_SHADOW_ADDR + 3),
333 	    X86BIOS_PHYSTOOFF(VGA_PCI_BIOS_SHADOW_ADDR + 3));
334 
335 	x86bios_get_intr(0x10);
336 
337 	return (0);
338 #else
339 	return (ENOTSUP);
340 #endif
341 }
342 
343 static int
vga_pci_probe(device_t dev)344 vga_pci_probe(device_t dev)
345 {
346 
347 	switch (pci_get_class(dev)) {
348 	case PCIC_DISPLAY:
349 		break;
350 	case PCIC_OLD:
351 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
352 			return (ENXIO);
353 		break;
354 	default:
355 		return (ENXIO);
356 	}
357 
358 	/* Probe default display. */
359 	vga_pci_is_boot_display(dev);
360 
361 	device_set_desc(dev, "VGA-compatible display");
362 	return (BUS_PROBE_GENERIC);
363 }
364 
365 static int
vga_pci_attach(device_t dev)366 vga_pci_attach(device_t dev)
367 {
368 
369 	bus_generic_probe(dev);
370 
371 	/* Always create a drmn child for now to make it easier on drm. */
372 	device_add_child(dev, "drmn", -1);
373 	bus_generic_attach(dev);
374 
375 	if (vga_pci_is_boot_display(dev))
376 		device_printf(dev, "Boot video device\n");
377 
378 	return (0);
379 }
380 
381 static int
vga_pci_suspend(device_t dev)382 vga_pci_suspend(device_t dev)
383 {
384 
385 	return (bus_generic_suspend(dev));
386 }
387 
388 static int
vga_pci_detach(device_t dev)389 vga_pci_detach(device_t dev)
390 {
391 	int error;
392 
393 	error = bus_generic_detach(dev);
394 	if (error == 0)
395 		error = device_delete_children(dev);
396 	return (error);
397 }
398 
399 static int
vga_pci_resume(device_t dev)400 vga_pci_resume(device_t dev)
401 {
402 
403 	return (bus_generic_resume(dev));
404 }
405 
406 /* Bus interface. */
407 
408 static int
vga_pci_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)409 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
410 {
411 
412 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
413 }
414 
415 static int
vga_pci_write_ivar(device_t dev,device_t child,int which,uintptr_t value)416 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
417 {
418 
419 	return (EINVAL);
420 }
421 
422 static int
vga_pci_setup_intr(device_t dev,device_t child,struct resource * irq,int flags,driver_filter_t * filter,driver_intr_t * intr,void * arg,void ** cookiep)423 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
424     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
425     void **cookiep)
426 {
427 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
428 	    filter, intr, arg, cookiep));
429 }
430 
431 static int
vga_pci_teardown_intr(device_t dev,device_t child,struct resource * irq,void * cookie)432 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
433     void *cookie)
434 {
435 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
436 }
437 
438 static struct vga_resource *
lookup_res(struct vga_pci_softc * sc,int rid)439 lookup_res(struct vga_pci_softc *sc, int rid)
440 {
441 	int bar;
442 
443 	if (rid == PCIR_BIOS)
444 		return (&sc->vga_bios);
445 	bar = PCI_RID2BAR(rid);
446 	if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
447 		return (&sc->vga_bars[bar]);
448 	return (NULL);
449 }
450 
451 static struct resource *
vga_pci_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)452 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
453     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
454 {
455 	struct vga_resource *vr;
456 
457 	switch (type) {
458 	case SYS_RES_MEMORY:
459 	case SYS_RES_IOPORT:
460 		/*
461 		 * For BARs, we cache the resource so that we only allocate it
462 		 * from the PCI bus once.
463 		 */
464 		vr = lookup_res(device_get_softc(dev), *rid);
465 		if (vr == NULL)
466 			return (NULL);
467 		if (vr->vr_res == NULL)
468 			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
469 			    end, count, flags);
470 		if (vr->vr_res != NULL)
471 			vr->vr_refs++;
472 		return (vr->vr_res);
473 	}
474 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
475 }
476 
477 static int
vga_pci_release_resource(device_t dev,device_t child,struct resource * r)478 vga_pci_release_resource(device_t dev, device_t child, struct resource *r)
479 {
480 	struct vga_resource *vr;
481 	int error;
482 
483 	switch (rman_get_type(r)) {
484 	case SYS_RES_MEMORY:
485 	case SYS_RES_IOPORT:
486 		/*
487 		 * For BARs, we release the resource from the PCI bus
488 		 * when the last child reference goes away.
489 		 */
490 		vr = lookup_res(device_get_softc(dev), rman_get_rid(r));
491 		if (vr == NULL)
492 			return (EINVAL);
493 		if (vr->vr_res == NULL)
494 			return (EINVAL);
495 		KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
496 		if (vr->vr_refs > 1) {
497 			vr->vr_refs--;
498 			return (0);
499 		}
500 		KASSERT(vr->vr_refs > 0,
501 		    ("vga_pci resource reference count underflow"));
502 		error = bus_release_resource(dev, r);
503 		if (error == 0) {
504 			vr->vr_res = NULL;
505 			vr->vr_refs = 0;
506 		}
507 		return (error);
508 	}
509 
510 	return (bus_release_resource(dev, r));
511 }
512 
513 /* PCI interface. */
514 
515 static uint32_t
vga_pci_read_config(device_t dev,device_t child,int reg,int width)516 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
517 {
518 
519 	return (pci_read_config(dev, reg, width));
520 }
521 
522 static void
vga_pci_write_config(device_t dev,device_t child,int reg,uint32_t val,int width)523 vga_pci_write_config(device_t dev, device_t child, int reg,
524     uint32_t val, int width)
525 {
526 
527 	pci_write_config(dev, reg, val, width);
528 }
529 
530 static int
vga_pci_enable_busmaster(device_t dev,device_t child)531 vga_pci_enable_busmaster(device_t dev, device_t child)
532 {
533 
534 	return (pci_enable_busmaster(dev));
535 }
536 
537 static int
vga_pci_disable_busmaster(device_t dev,device_t child)538 vga_pci_disable_busmaster(device_t dev, device_t child)
539 {
540 
541 	return (pci_disable_busmaster(dev));
542 }
543 
544 static int
vga_pci_enable_io(device_t dev,device_t child,int space)545 vga_pci_enable_io(device_t dev, device_t child, int space)
546 {
547 
548 	device_printf(dev, "child %s requested pci_enable_io\n",
549 	    device_get_nameunit(child));
550 	return (pci_enable_io(dev, space));
551 }
552 
553 static int
vga_pci_disable_io(device_t dev,device_t child,int space)554 vga_pci_disable_io(device_t dev, device_t child, int space)
555 {
556 
557 	device_printf(dev, "child %s requested pci_disable_io\n",
558 	    device_get_nameunit(child));
559 	return (pci_disable_io(dev, space));
560 }
561 
562 static int
vga_pci_get_vpd_ident(device_t dev,device_t child,const char ** identptr)563 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
564 {
565 
566 	return (pci_get_vpd_ident(dev, identptr));
567 }
568 
569 static int
vga_pci_get_vpd_readonly(device_t dev,device_t child,const char * kw,const char ** vptr)570 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
571     const char **vptr)
572 {
573 
574 	return (pci_get_vpd_readonly(dev, kw, vptr));
575 }
576 
577 static int
vga_pci_set_powerstate(device_t dev,device_t child,int state)578 vga_pci_set_powerstate(device_t dev, device_t child, int state)
579 {
580 
581 	device_printf(dev, "child %s requested pci_set_powerstate\n",
582 	    device_get_nameunit(child));
583 	return (pci_set_powerstate(dev, state));
584 }
585 
586 static int
vga_pci_get_powerstate(device_t dev,device_t child)587 vga_pci_get_powerstate(device_t dev, device_t child)
588 {
589 
590 	device_printf(dev, "child %s requested pci_get_powerstate\n",
591 	    device_get_nameunit(child));
592 	return (pci_get_powerstate(dev));
593 }
594 
595 static int
vga_pci_assign_interrupt(device_t dev,device_t child)596 vga_pci_assign_interrupt(device_t dev, device_t child)
597 {
598 
599 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
600 	    device_get_nameunit(child));
601 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
602 }
603 
604 static int
vga_pci_find_cap(device_t dev,device_t child,int capability,int * capreg)605 vga_pci_find_cap(device_t dev, device_t child, int capability,
606     int *capreg)
607 {
608 
609 	return (pci_find_cap(dev, capability, capreg));
610 }
611 
612 static int
vga_pci_find_next_cap(device_t dev,device_t child,int capability,int start,int * capreg)613 vga_pci_find_next_cap(device_t dev, device_t child, int capability,
614     int start, int *capreg)
615 {
616 
617 	return (pci_find_next_cap(dev, capability, start, capreg));
618 }
619 
620 static int
vga_pci_find_extcap(device_t dev,device_t child,int capability,int * capreg)621 vga_pci_find_extcap(device_t dev, device_t child, int capability,
622     int *capreg)
623 {
624 
625 	return (pci_find_extcap(dev, capability, capreg));
626 }
627 
628 static int
vga_pci_find_next_extcap(device_t dev,device_t child,int capability,int start,int * capreg)629 vga_pci_find_next_extcap(device_t dev, device_t child, int capability,
630     int start, int *capreg)
631 {
632 
633 	return (pci_find_next_extcap(dev, capability, start, capreg));
634 }
635 
636 static int
vga_pci_find_htcap(device_t dev,device_t child,int capability,int * capreg)637 vga_pci_find_htcap(device_t dev, device_t child, int capability,
638     int *capreg)
639 {
640 
641 	return (pci_find_htcap(dev, capability, capreg));
642 }
643 
644 static int
vga_pci_find_next_htcap(device_t dev,device_t child,int capability,int start,int * capreg)645 vga_pci_find_next_htcap(device_t dev, device_t child, int capability,
646     int start, int *capreg)
647 {
648 
649 	return (pci_find_next_htcap(dev, capability, start, capreg));
650 }
651 
652 static int
vga_pci_alloc_msi(device_t dev,device_t child,int * count)653 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
654 {
655 	struct vga_pci_softc *sc;
656 	int error;
657 
658 	sc = device_get_softc(dev);
659 	if (sc->vga_msi_child != NULL)
660 		return (EBUSY);
661 	error = pci_alloc_msi(dev, count);
662 	if (error == 0)
663 		sc->vga_msi_child = child;
664 	return (error);
665 }
666 
667 static int
vga_pci_alloc_msix(device_t dev,device_t child,int * count)668 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
669 {
670 	struct vga_pci_softc *sc;
671 	int error;
672 
673 	sc = device_get_softc(dev);
674 	if (sc->vga_msi_child != NULL)
675 		return (EBUSY);
676 	error = pci_alloc_msix(dev, count);
677 	if (error == 0)
678 		sc->vga_msi_child = child;
679 	return (error);
680 }
681 
682 static int
vga_pci_remap_msix(device_t dev,device_t child,int count,const u_int * vectors)683 vga_pci_remap_msix(device_t dev, device_t child, int count,
684     const u_int *vectors)
685 {
686 	struct vga_pci_softc *sc;
687 
688 	sc = device_get_softc(dev);
689 	if (sc->vga_msi_child != child)
690 		return (ENXIO);
691 	return (pci_remap_msix(dev, count, vectors));
692 }
693 
694 static int
vga_pci_release_msi(device_t dev,device_t child)695 vga_pci_release_msi(device_t dev, device_t child)
696 {
697 	struct vga_pci_softc *sc;
698 	int error;
699 
700 	sc = device_get_softc(dev);
701 	if (sc->vga_msi_child != child)
702 		return (ENXIO);
703 	error = pci_release_msi(dev);
704 	if (error == 0)
705 		sc->vga_msi_child = NULL;
706 	return (error);
707 }
708 
709 static int
vga_pci_msi_count(device_t dev,device_t child)710 vga_pci_msi_count(device_t dev, device_t child)
711 {
712 
713 	return (pci_msi_count(dev));
714 }
715 
716 static int
vga_pci_msix_count(device_t dev,device_t child)717 vga_pci_msix_count(device_t dev, device_t child)
718 {
719 
720 	return (pci_msix_count(dev));
721 }
722 
723 static bus_dma_tag_t
vga_pci_get_dma_tag(device_t bus,device_t child)724 vga_pci_get_dma_tag(device_t bus, device_t child)
725 {
726 
727 	return (bus_get_dma_tag(bus));
728 }
729 
730 static device_method_t vga_pci_methods[] = {
731 	/* Device interface */
732 	DEVMETHOD(device_probe,		vga_pci_probe),
733 	DEVMETHOD(device_attach,	vga_pci_attach),
734 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
735 	DEVMETHOD(device_suspend,	vga_pci_suspend),
736 	DEVMETHOD(device_detach,	vga_pci_detach),
737 	DEVMETHOD(device_resume,	vga_pci_resume),
738 
739 	/* Bus interface */
740 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
741 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
742 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
743 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
744 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
745 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
746 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
747 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
748 	DEVMETHOD(bus_get_dma_tag,	vga_pci_get_dma_tag),
749 
750 	/* PCI interface */
751 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
752 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
753 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
754 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
755 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
756 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
757 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
758 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
759 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
760 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
761 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
762 	DEVMETHOD(pci_find_cap,		vga_pci_find_cap),
763 	DEVMETHOD(pci_find_next_cap,	vga_pci_find_next_cap),
764 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
765 	DEVMETHOD(pci_find_next_extcap,	vga_pci_find_next_extcap),
766 	DEVMETHOD(pci_find_htcap,	vga_pci_find_htcap),
767 	DEVMETHOD(pci_find_next_htcap,	vga_pci_find_next_htcap),
768 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
769 	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
770 	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
771 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
772 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
773 	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
774 	{ 0, 0 }
775 };
776 
777 static driver_t vga_pci_driver = {
778 	"vgapci",
779 	vga_pci_methods,
780 	sizeof(struct vga_pci_softc),
781 };
782 
783 DRIVER_MODULE(vgapci, pci, vga_pci_driver, 0, 0);
784 MODULE_DEPEND(vgapci, x86bios, 1, 1, 1);
785