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 * $FreeBSD: src/sys/dev/pci/vga_pci.c,v 1.5.8.1 2009/04/15 03:14:26 kensmith Exp $ 30 */ 31 32 /* 33 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and 34 * drm(4) should attach as children of this device. 35 * 36 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver 37 * in or rename it. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/bus.h> 42 #include <sys/kernel.h> 43 #include <sys/module.h> 44 #include <sys/rman.h> 45 #include <sys/sysctl.h> 46 #include <sys/systm.h> 47 48 #include <machine/pmap.h> 49 50 #include <bus/pci/pcireg.h> 51 #include <bus/pci/pcivar.h> 52 53 struct vga_resource { 54 struct resource *vr_res; 55 int vr_refs; 56 }; 57 58 struct vga_pci_softc { 59 device_t vga_msi_child; /* Child driver using MSI. */ 60 struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1]; 61 struct vga_resource vga_bios; 62 }; 63 64 SYSCTL_DECL(_hw_pci); 65 66 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid); 67 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child, 68 int type, int *rid, u_long start, u_long end, u_long count, u_int flags); 69 static int vga_pci_release_resource(device_t dev, device_t child, int type, 70 int rid, struct resource *r); 71 72 int vga_pci_default_unit = -1; 73 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit); 74 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RD, 75 &vga_pci_default_unit, -1, "Default VGA-compatible display"); 76 77 int 78 vga_pci_is_boot_display(device_t dev) 79 { 80 81 /* 82 * Return true if the given device is the default display used 83 * at boot time. 84 */ 85 return ( 86 (pci_get_class(dev) == PCIC_DISPLAY || 87 (pci_get_class(dev) == PCIC_OLD && 88 pci_get_subclass(dev) == PCIS_OLD_VGA)) && 89 device_get_unit(dev) == vga_pci_default_unit); 90 } 91 92 void * 93 vga_pci_map_bios(device_t dev, size_t *size) 94 { 95 int rid; 96 struct resource *res; 97 98 if (vga_pci_is_boot_display(dev)) { 99 /* 100 * On x86, the System BIOS copy the default display 101 * device's Video BIOS at a fixed location in system 102 * memory (0xC0000, 128 kBytes long) at boot time. 103 * 104 * We use this copy for the default boot device, because 105 * the original ROM may not be valid after boot. 106 */ 107 108 *size = VGA_PCI_BIOS_SHADOW_SIZE; 109 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size)); 110 } 111 112 rid = PCIR_BIOS; 113 res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul, 114 ~0ul, 1, RF_ACTIVE); 115 if (res == NULL) { 116 return (NULL); 117 } 118 119 *size = rman_get_size(res); 120 return (rman_get_virtual(res)); 121 } 122 123 void 124 vga_pci_unmap_bios(device_t dev, void *bios) 125 { 126 struct vga_resource *vr; 127 128 if (bios == NULL) { 129 return; 130 } 131 132 if (vga_pci_is_boot_display(dev)) { 133 /* We mapped the BIOS shadow copy located at 0xC0000. */ 134 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE); 135 136 return; 137 } 138 139 /* 140 * Look up the PCIR_BIOS resource in our softc. It should match 141 * the address we returned previously. 142 */ 143 vr = lookup_res(device_get_softc(dev), PCIR_BIOS); 144 KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped")); 145 KASSERT(rman_get_virtual(vr->vr_res) == bios, 146 ("vga_pci_unmap_bios: mismatch")); 147 vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS, 148 vr->vr_res); 149 } 150 151 static int 152 vga_pci_probe(device_t dev) 153 { 154 device_t bdev; 155 int unit; 156 uint16_t bctl; 157 158 switch (pci_get_class(dev)) { 159 case PCIC_DISPLAY: 160 break; 161 case PCIC_OLD: 162 if (pci_get_subclass(dev) != PCIS_OLD_VGA) 163 return (ENXIO); 164 break; 165 default: 166 return (ENXIO); 167 } 168 169 /* Probe default display. */ 170 unit = device_get_unit(dev); 171 bdev = device_get_parent(device_get_parent(dev)); 172 bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2); 173 if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0) 174 vga_pci_default_unit = unit; 175 if (vga_pci_default_unit == unit) 176 device_set_flags(dev, 1); 177 178 device_set_desc(dev, "VGA-compatible display"); 179 return (BUS_PROBE_GENERIC); 180 } 181 182 static int 183 vga_pci_attach(device_t dev) 184 { 185 186 bus_generic_probe(dev); 187 188 /* Always create a drm child for now to make it easier on drm. */ 189 device_add_child(dev, "drm", -1); 190 device_add_child(dev, "drmn", -1); 191 bus_generic_attach(dev); 192 return (0); 193 } 194 195 static int 196 vga_pci_suspend(device_t dev) 197 { 198 199 return (bus_generic_suspend(dev)); 200 } 201 202 static int 203 vga_pci_resume(device_t dev) 204 { 205 206 return (bus_generic_resume(dev)); 207 } 208 209 /* Bus interface. */ 210 211 static int 212 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 213 { 214 215 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result)); 216 } 217 218 static int 219 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 220 { 221 222 return (EINVAL); 223 } 224 225 static struct vga_resource * 226 lookup_res(struct vga_pci_softc *sc, int rid) 227 { 228 int bar; 229 230 if (rid == PCIR_BIOS) 231 return (&sc->vga_bios); 232 bar = PCI_RID2BAR(rid); 233 if (bar >= 0 && bar <= PCIR_MAX_BAR_0) 234 return (&sc->vga_bars[bar]); 235 return (NULL); 236 } 237 238 static struct resource * 239 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 240 u_long start, u_long end, u_long count, u_int flags) 241 { 242 struct vga_resource *vr; 243 244 switch (type) { 245 case SYS_RES_MEMORY: 246 case SYS_RES_IOPORT: 247 /* 248 * For BARs, we cache the resource so that we only allocate it 249 * from the PCI bus once. 250 */ 251 vr = lookup_res(device_get_softc(dev), *rid); 252 if (vr == NULL) 253 return (NULL); 254 if (vr->vr_res == NULL) 255 vr->vr_res = bus_alloc_resource(dev, type, rid, start, 256 end, count, flags); 257 if (vr->vr_res != NULL) 258 vr->vr_refs++; 259 return (vr->vr_res); 260 } 261 return (bus_alloc_resource(dev, type, rid, start, end, count, flags)); 262 } 263 264 static int 265 vga_pci_release_resource(device_t dev, device_t child, int type, int rid, 266 struct resource *r) 267 { 268 269 return (bus_release_resource(dev, type, rid, r)); 270 } 271 272 /* PCI interface. */ 273 274 static uint32_t 275 vga_pci_read_config(device_t dev, device_t child, int reg, int width) 276 { 277 278 return (pci_read_config(dev, reg, width)); 279 } 280 281 static void 282 vga_pci_write_config(device_t dev, device_t child, int reg, 283 uint32_t val, int width) 284 { 285 286 pci_write_config(dev, reg, val, width); 287 } 288 289 static int 290 vga_pci_enable_busmaster(device_t dev, device_t child) 291 { 292 293 return (pci_enable_busmaster(dev)); 294 } 295 296 static int 297 vga_pci_disable_busmaster(device_t dev, device_t child) 298 { 299 300 return (pci_disable_busmaster(dev)); 301 } 302 303 static int 304 vga_pci_enable_io(device_t dev, device_t child, int space) 305 { 306 307 device_printf(dev, "child %s requested pci_enable_io\n", 308 device_get_nameunit(child)); 309 return (pci_enable_io(dev, space)); 310 } 311 312 static int 313 vga_pci_disable_io(device_t dev, device_t child, int space) 314 { 315 316 device_printf(dev, "child %s requested pci_disable_io\n", 317 device_get_nameunit(child)); 318 return (pci_disable_io(dev, space)); 319 } 320 321 static int 322 vga_pci_set_powerstate(device_t dev, device_t child, int state) 323 { 324 325 device_printf(dev, "child %s requested pci_set_powerstate\n", 326 device_get_nameunit(child)); 327 return (pci_set_powerstate(dev, state)); 328 } 329 330 static int 331 vga_pci_get_powerstate(device_t dev, device_t child) 332 { 333 334 device_printf(dev, "child %s requested pci_get_powerstate\n", 335 device_get_nameunit(child)); 336 return (pci_get_powerstate(dev)); 337 } 338 339 static int 340 vga_pci_assign_interrupt(device_t dev, device_t child) 341 { 342 343 device_printf(dev, "child %s requested pci_assign_interrupt\n", 344 device_get_nameunit(child)); 345 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev)); 346 } 347 348 static int 349 vga_pci_find_extcap(device_t dev, device_t child, int capability, 350 int *capreg) 351 { 352 353 return (pci_find_extcap(dev, capability, capreg)); 354 } 355 356 static device_method_t vga_pci_methods[] = { 357 /* Device interface */ 358 DEVMETHOD(device_probe, vga_pci_probe), 359 DEVMETHOD(device_attach, vga_pci_attach), 360 DEVMETHOD(device_shutdown, bus_generic_shutdown), 361 DEVMETHOD(device_suspend, vga_pci_suspend), 362 DEVMETHOD(device_resume, vga_pci_resume), 363 364 /* Bus interface */ 365 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar), 366 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar), 367 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 368 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 369 370 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource), 371 DEVMETHOD(bus_release_resource, vga_pci_release_resource), 372 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 373 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 374 375 /* PCI interface */ 376 DEVMETHOD(pci_read_config, vga_pci_read_config), 377 DEVMETHOD(pci_write_config, vga_pci_write_config), 378 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster), 379 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster), 380 DEVMETHOD(pci_enable_io, vga_pci_enable_io), 381 DEVMETHOD(pci_disable_io, vga_pci_disable_io), 382 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate), 383 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate), 384 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt), 385 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap), 386 387 DEVMETHOD_END 388 }; 389 390 static driver_t vga_pci_driver = { 391 "vgapci", 392 vga_pci_methods, 393 1, 394 }; 395 396 static devclass_t vga_devclass; 397 398 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, NULL, NULL); 399