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