agp_i810.c revision 186434
163010Sdfr/*- 263010Sdfr * Copyright (c) 2000 Doug Rabson 363010Sdfr * Copyright (c) 2000 Ruslan Ermilov 463010Sdfr * All rights reserved. 563010Sdfr * 663010Sdfr * Redistribution and use in source and binary forms, with or without 763010Sdfr * modification, are permitted provided that the following conditions 863010Sdfr * are met: 963010Sdfr * 1. Redistributions of source code must retain the above copyright 1063010Sdfr * notice, this list of conditions and the following disclaimer. 1163010Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1263010Sdfr * notice, this list of conditions and the following disclaimer in the 1363010Sdfr * documentation and/or other materials provided with the distribution. 1463010Sdfr * 1563010Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1663010Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1763010Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1863010Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1963010Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2063010Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2163010Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2263010Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2363010Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2463010Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2563010Sdfr * SUCH DAMAGE. 2663010Sdfr */ 2763010Sdfr 28103243Sanholt/* 29103243Sanholt * Fixes for 830/845G support: David Dawes <dawes@xfree86.org> 30110785Sanholt * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org> 31103243Sanholt */ 32103243Sanholt 33116192Sobrien#include <sys/cdefs.h> 34116192Sobrien__FBSDID("$FreeBSD: head/sys/dev/agp/agp_i810.c 186434 2008-12-23 16:16:30Z rnoland $"); 35116192Sobrien 3663010Sdfr#include "opt_bus.h" 3763010Sdfr 3863010Sdfr#include <sys/param.h> 3963010Sdfr#include <sys/systm.h> 4063010Sdfr#include <sys/malloc.h> 4163010Sdfr#include <sys/kernel.h> 42129878Sphk#include <sys/module.h> 4363010Sdfr#include <sys/bus.h> 4463010Sdfr#include <sys/lock.h> 4576827Salfred#include <sys/mutex.h> 4679339Sjhb#include <sys/proc.h> 4763010Sdfr 48173573Sjhb#include <dev/agp/agppriv.h> 49173573Sjhb#include <dev/agp/agpreg.h> 50119288Simp#include <dev/pci/pcivar.h> 51119288Simp#include <dev/pci/pcireg.h> 5263010Sdfr 5363010Sdfr#include <vm/vm.h> 5463010Sdfr#include <vm/vm_object.h> 5563010Sdfr#include <vm/vm_page.h> 5663010Sdfr#include <vm/vm_pageout.h> 5763010Sdfr#include <vm/pmap.h> 5863010Sdfr 5963010Sdfr#include <machine/bus.h> 6063010Sdfr#include <machine/resource.h> 61171433Sanholt#include <machine/md_var.h> 6263010Sdfr#include <sys/rman.h> 6363010Sdfr 6463010SdfrMALLOC_DECLARE(M_AGP); 6563010Sdfr 66171433Sanholtenum { 67171433Sanholt CHIP_I810, /* i810/i815 */ 68171433Sanholt CHIP_I830, /* 830M/845G */ 69171433Sanholt CHIP_I855, /* 852GM/855GM/865G */ 70171433Sanholt CHIP_I915, /* 915G/915GM */ 71171433Sanholt CHIP_I965, /* G965 */ 72171433Sanholt CHIP_G33, /* G33/Q33/Q35 */ 73183555Srnoland CHIP_G4X, /* G45/Q45 */ 74171433Sanholt}; 7563010Sdfr 76171433Sanholt/* The i810 through i855 have the registers at BAR 1, and the GATT gets 77171433Sanholt * allocated by us. The i915 has registers in BAR 0 and the GATT is at the 78171433Sanholt * start of the stolen memory, and should only be accessed by the OS through 79171433Sanholt * BAR 3. The G965 has registers and GATT in the same BAR (0) -- first 512KB 80171433Sanholt * is registers, second 512KB is GATT. 81171433Sanholt */ 82171433Sanholtstatic struct resource_spec agp_i810_res_spec[] = { 83171433Sanholt { SYS_RES_MEMORY, AGP_I810_MMADR, RF_ACTIVE | RF_SHAREABLE }, 84171433Sanholt { -1, 0 } 85171433Sanholt}; 86103243Sanholt 87171433Sanholtstatic struct resource_spec agp_i915_res_spec[] = { 88171433Sanholt { SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE }, 89171433Sanholt { SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE }, 90171433Sanholt { -1, 0 } 91171433Sanholt}; 92171433Sanholt 93171433Sanholtstatic struct resource_spec agp_i965_res_spec[] = { 94171433Sanholt { SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE }, 95171433Sanholt { -1, 0 } 96171433Sanholt}; 97171433Sanholt 9863010Sdfrstruct agp_i810_softc { 9963010Sdfr struct agp_softc agp; 10063010Sdfr u_int32_t initial_aperture; /* aperture size at startup */ 10163010Sdfr struct agp_gatt *gatt; 102103243Sanholt int chiptype; /* i810-like or i830 */ 103103243Sanholt u_int32_t dcache_size; /* i810 only */ 104103243Sanholt u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */ 10563010Sdfr device_t bdev; /* bridge device */ 106153031Sanholt 107171433Sanholt void *argb_cursor; /* contigmalloc area for ARGB cursor */ 108153031Sanholt 109171433Sanholt struct resource_spec * sc_res_spec; 110171433Sanholt struct resource *sc_res[2]; 11163010Sdfr}; 11263010Sdfr 113159926Sanholt/* For adding new devices, devid is the id of the graphics controller 114159926Sanholt * (pci:0:2:0, for example). The placeholder (usually at pci:0:2:1) for the 115159926Sanholt * second head should never be added. The bridge_offset is the offset to 116159926Sanholt * subtract from devid to get the id of the hostb that the device is on. 117159926Sanholt */ 118159926Sanholtstatic const struct agp_i810_match { 119159926Sanholt int devid; 120159926Sanholt int chiptype; 121159926Sanholt int bridge_offset; 122159926Sanholt char *name; 123159926Sanholt} agp_i810_matches[] = { 124159926Sanholt {0x71218086, CHIP_I810, 0x00010000, 125159926Sanholt "Intel 82810 (i810 GMCH) SVGA controller"}, 126159926Sanholt {0x71238086, CHIP_I810, 0x00010000, 127159926Sanholt "Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller"}, 128159926Sanholt {0x71258086, CHIP_I810, 0x00010000, 129159926Sanholt "Intel 82810E (i810E GMCH) SVGA controller"}, 130159926Sanholt {0x11328086, CHIP_I810, 0x00020000, 131159926Sanholt "Intel 82815 (i815 GMCH) SVGA controller"}, 132159926Sanholt {0x35778086, CHIP_I830, 0x00020000, 133159926Sanholt "Intel 82830M (830M GMCH) SVGA controller"}, 134173946Sremko {0x25628086, CHIP_I830, 0x00020000, 135173946Sremko "Intel 82845M (845M GMCH) SVGA controller"}, 136159981Sanholt {0x35828086, CHIP_I855, 0x00020000, 137183555Srnoland "Intel 82852/855GM SVGA controller"}, 138159981Sanholt {0x25728086, CHIP_I855, 0x00020000, 139159926Sanholt "Intel 82865G (865G GMCH) SVGA controller"}, 140159926Sanholt {0x25828086, CHIP_I915, 0x00020000, 141159926Sanholt "Intel 82915G (915G GMCH) SVGA controller"}, 142172187Salc {0x258A8086, CHIP_I915, 0x00020000, 143172187Salc "Intel E7221 SVGA controller"}, 144159926Sanholt {0x25928086, CHIP_I915, 0x00020000, 145159926Sanholt "Intel 82915GM (915GM GMCH) SVGA controller"}, 146159926Sanholt {0x27728086, CHIP_I915, 0x00020000, 147159926Sanholt "Intel 82945G (945G GMCH) SVGA controller"}, 148159926Sanholt {0x27A28086, CHIP_I915, 0x00020000, 149159926Sanholt "Intel 82945GM (945GM GMCH) SVGA controller"}, 150179901Sgonzo {0x27AE8086, CHIP_I915, 0x00020000, 151171433Sanholt "Intel 945GME SVGA controller"}, 152171433Sanholt {0x29728086, CHIP_I965, 0x00020000, 153171433Sanholt "Intel 946GZ SVGA controller"}, 154171433Sanholt {0x29828086, CHIP_I965, 0x00020000, 155171433Sanholt "Intel G965 SVGA controller"}, 156171433Sanholt {0x29928086, CHIP_I965, 0x00020000, 157171433Sanholt "Intel Q965 SVGA controller"}, 158183555Srnoland {0x29A28086, CHIP_I965, 0x00020000, 159171433Sanholt "Intel G965 SVGA controller"}, 160183555Srnoland {0x29B28086, CHIP_G33, 0x00020000, 161171433Sanholt "Intel Q35 SVGA controller"}, 162183555Srnoland {0x29C28086, CHIP_G33, 0x00020000, 163171433Sanholt "Intel G33 SVGA controller"}, 164183555Srnoland {0x29D28086, CHIP_G33, 0x00020000, 165171433Sanholt "Intel Q33 SVGA controller"}, 166183555Srnoland {0x2A028086, CHIP_I965, 0x00020000, 167171433Sanholt "Intel GM965 SVGA controller"}, 168183555Srnoland {0x2A128086, CHIP_I965, 0x00020000, 169171433Sanholt "Intel GME965 SVGA controller"}, 170186434Srnoland {0x2A428086, CHIP_G4X, 0x00020000, 171183555Srnoland "Intel GM45 SVGA controller"}, 172183555Srnoland {0x2E028086, CHIP_G4X, 0x00020000, 173183555Srnoland "Intel 4 Series SVGA controller"}, 174183555Srnoland {0x2E128086, CHIP_G4X, 0x00020000, 175183555Srnoland "Intel Q45 SVGA controller"}, 176183555Srnoland {0x2E228086, CHIP_G4X, 0x00020000, 177183555Srnoland "Intel G45 SVGA controller"}, 178159926Sanholt {0, 0, 0, NULL} 179159926Sanholt}; 180159926Sanholt 181159926Sanholtstatic const struct agp_i810_match* 18263010Sdfragp_i810_match(device_t dev) 18363010Sdfr{ 184159926Sanholt int i, devid; 185159926Sanholt 18663010Sdfr if (pci_get_class(dev) != PCIC_DISPLAY 18763010Sdfr || pci_get_subclass(dev) != PCIS_DISPLAY_VGA) 18863010Sdfr return NULL; 18963010Sdfr 190159926Sanholt devid = pci_get_devid(dev); 191159926Sanholt for (i = 0; agp_i810_matches[i].devid != 0; i++) { 192159926Sanholt if (agp_i810_matches[i].devid == devid) 193159926Sanholt break; 194159926Sanholt } 195159926Sanholt if (agp_i810_matches[i].devid == 0) 196159926Sanholt return NULL; 197159926Sanholt else 198159926Sanholt return &agp_i810_matches[i]; 19963010Sdfr} 20063010Sdfr 20163010Sdfr/* 20263010Sdfr * Find bridge device. 20363010Sdfr */ 20463010Sdfrstatic device_t 20563010Sdfragp_i810_find_bridge(device_t dev) 20663010Sdfr{ 20763010Sdfr device_t *children, child; 20863010Sdfr int nchildren, i; 20963010Sdfr u_int32_t devid; 210159926Sanholt const struct agp_i810_match *match; 211159926Sanholt 212159926Sanholt match = agp_i810_match(dev); 213159926Sanholt devid = match->devid - match->bridge_offset; 21463010Sdfr 215153579Sjhb if (device_get_children(device_get_parent(device_get_parent(dev)), 216153579Sjhb &children, &nchildren)) 21763010Sdfr return 0; 21863010Sdfr 21963010Sdfr for (i = 0; i < nchildren; i++) { 22063010Sdfr child = children[i]; 22163010Sdfr 22263010Sdfr if (pci_get_devid(child) == devid) { 22363010Sdfr free(children, M_TEMP); 22463010Sdfr return child; 22563010Sdfr } 22663010Sdfr } 22763010Sdfr free(children, M_TEMP); 22863010Sdfr return 0; 22963010Sdfr} 23063010Sdfr 231155186Sjhbstatic void 232155186Sjhbagp_i810_identify(driver_t *driver, device_t parent) 233155186Sjhb{ 234155186Sjhb 235155186Sjhb if (device_find_child(parent, "agp", -1) == NULL && 236155186Sjhb agp_i810_match(parent)) 237155186Sjhb device_add_child(parent, "agp", -1); 238155186Sjhb} 239155186Sjhb 24063010Sdfrstatic int 24163010Sdfragp_i810_probe(device_t dev) 24263010Sdfr{ 243159926Sanholt device_t bdev; 244159926Sanholt const struct agp_i810_match *match; 245171433Sanholt u_int8_t smram; 246171433Sanholt int gcc1, deven; 24763010Sdfr 248127815Snjl if (resource_disabled("agp", device_get_unit(dev))) 249127815Snjl return (ENXIO); 250159926Sanholt match = agp_i810_match(dev); 251159926Sanholt if (match == NULL) 252159926Sanholt return ENXIO; 253159926Sanholt 254159926Sanholt bdev = agp_i810_find_bridge(dev); 255159926Sanholt if (!bdev) { 256159926Sanholt if (bootverbose) 257159926Sanholt printf("I810: can't find bridge device\n"); 258159926Sanholt return ENXIO; 259159926Sanholt } 260159926Sanholt 261159926Sanholt /* 262159926Sanholt * checking whether internal graphics device has been activated. 263159926Sanholt */ 264171433Sanholt switch (match->chiptype) { 265171433Sanholt case CHIP_I810: 266159926Sanholt smram = pci_read_config(bdev, AGP_I810_SMRAM, 1); 267171433Sanholt if ((smram & AGP_I810_SMRAM_GMS) == 268171433Sanholt AGP_I810_SMRAM_GMS_DISABLED) { 269159926Sanholt if (bootverbose) 270159926Sanholt printf("I810: disabled, not probing\n"); 271159926Sanholt return ENXIO; 272159926Sanholt } 273171433Sanholt break; 274171433Sanholt case CHIP_I830: 275171433Sanholt case CHIP_I855: 276159926Sanholt gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1); 277159926Sanholt if ((gcc1 & AGP_I830_GCC1_DEV2) == 278159926Sanholt AGP_I830_GCC1_DEV2_DISABLED) { 27963010Sdfr if (bootverbose) 280159926Sanholt printf("I830: disabled, not probing\n"); 28163010Sdfr return ENXIO; 28263010Sdfr } 283171433Sanholt break; 284171433Sanholt case CHIP_I915: 285171433Sanholt case CHIP_I965: 286171433Sanholt case CHIP_G33: 287186434Srnoland case CHIP_G4X: 288171433Sanholt deven = pci_read_config(bdev, AGP_I915_DEVEN, 4); 289171433Sanholt if ((deven & AGP_I915_DEVEN_D2F0) == 290159926Sanholt AGP_I915_DEVEN_D2F0_DISABLED) { 291159926Sanholt if (bootverbose) 292159926Sanholt printf("I915: disabled, not probing\n"); 293159926Sanholt return ENXIO; 294159926Sanholt } 295171433Sanholt break; 296159926Sanholt } 297121437Sjhb 298159926Sanholt if (match->devid == 0x35828086) { 299159926Sanholt switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) { 300159926Sanholt case AGP_I855_GME: 301159926Sanholt device_set_desc(dev, 302159926Sanholt "Intel 82855GME (855GME GMCH) SVGA controller"); 303121437Sjhb break; 304159926Sanholt case AGP_I855_GM: 305159926Sanholt device_set_desc(dev, 306159926Sanholt "Intel 82855GM (855GM GMCH) SVGA controller"); 307153031Sanholt break; 308159926Sanholt case AGP_I852_GME: 309159926Sanholt device_set_desc(dev, 310159926Sanholt "Intel 82852GME (852GME GMCH) SVGA controller"); 311159926Sanholt break; 312159926Sanholt case AGP_I852_GM: 313159926Sanholt device_set_desc(dev, 314159926Sanholt "Intel 82852GM (852GM GMCH) SVGA controller"); 315159926Sanholt break; 316121437Sjhb default: 317159926Sanholt device_set_desc(dev, 318159926Sanholt "Intel 8285xM (85xGM GMCH) SVGA controller"); 319159926Sanholt break; 32063010Sdfr } 321159926Sanholt } else { 322159926Sanholt device_set_desc(dev, match->name); 32363010Sdfr } 32463010Sdfr 325159926Sanholt return BUS_PROBE_DEFAULT; 32663010Sdfr} 32763010Sdfr 328171433Sanholtstatic void 329171433Sanholtagp_i810_dump_regs(device_t dev) 330171433Sanholt{ 331171433Sanholt struct agp_i810_softc *sc = device_get_softc(dev); 332171433Sanholt 333171433Sanholt device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n", 334171433Sanholt bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL)); 335171433Sanholt 336171433Sanholt switch (sc->chiptype) { 337171433Sanholt case CHIP_I810: 338171433Sanholt device_printf(dev, "AGP_I810_MISCC: 0x%04x\n", 339171433Sanholt pci_read_config(sc->bdev, AGP_I810_MISCC, 2)); 340171433Sanholt break; 341171433Sanholt case CHIP_I830: 342171433Sanholt device_printf(dev, "AGP_I830_GCC1: 0x%02x\n", 343171433Sanholt pci_read_config(sc->bdev, AGP_I830_GCC1, 1)); 344171433Sanholt break; 345171433Sanholt case CHIP_I855: 346171433Sanholt device_printf(dev, "AGP_I855_GCC1: 0x%02x\n", 347171433Sanholt pci_read_config(sc->bdev, AGP_I855_GCC1, 1)); 348171433Sanholt break; 349171433Sanholt case CHIP_I915: 350171433Sanholt case CHIP_I965: 351171433Sanholt case CHIP_G33: 352186434Srnoland case CHIP_G4X: 353171433Sanholt device_printf(dev, "AGP_I855_GCC1: 0x%02x\n", 354171433Sanholt pci_read_config(sc->bdev, AGP_I855_GCC1, 1)); 355171433Sanholt device_printf(dev, "AGP_I915_MSAC: 0x%02x\n", 356171433Sanholt pci_read_config(sc->bdev, AGP_I915_MSAC, 1)); 357171433Sanholt break; 358171433Sanholt } 359171433Sanholt device_printf(dev, "Aperture resource size: %d bytes\n", 360171433Sanholt AGP_GET_APERTURE(dev)); 361171433Sanholt} 362171433Sanholt 36363010Sdfrstatic int 36463010Sdfragp_i810_attach(device_t dev) 36563010Sdfr{ 36663010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 36763010Sdfr struct agp_gatt *gatt; 368159926Sanholt const struct agp_i810_match *match; 369171433Sanholt int error; 37063010Sdfr 37163010Sdfr sc->bdev = agp_i810_find_bridge(dev); 37263010Sdfr if (!sc->bdev) 37363010Sdfr return ENOENT; 37463010Sdfr 375171433Sanholt match = agp_i810_match(dev); 376171433Sanholt sc->chiptype = match->chiptype; 377171433Sanholt 378171433Sanholt switch (sc->chiptype) { 379171433Sanholt case CHIP_I810: 380171433Sanholt case CHIP_I830: 381171433Sanholt case CHIP_I855: 382171433Sanholt sc->sc_res_spec = agp_i810_res_spec; 383171433Sanholt agp_set_aperture_resource(dev, AGP_APBASE); 384171433Sanholt break; 385171433Sanholt case CHIP_I915: 386171433Sanholt case CHIP_G33: 387171433Sanholt sc->sc_res_spec = agp_i915_res_spec; 388171433Sanholt agp_set_aperture_resource(dev, AGP_I915_GMADR); 389171433Sanholt break; 390171433Sanholt case CHIP_I965: 391183555Srnoland case CHIP_G4X: 392171433Sanholt sc->sc_res_spec = agp_i965_res_spec; 393171433Sanholt agp_set_aperture_resource(dev, AGP_I915_GMADR); 394171433Sanholt break; 395171433Sanholt } 396171433Sanholt 39763010Sdfr error = agp_generic_attach(dev); 39863010Sdfr if (error) 39963010Sdfr return error; 40063010Sdfr 401171433Sanholt if (sc->chiptype != CHIP_I965 && sc->chiptype != CHIP_G33 && 402186434Srnoland sc->chiptype != CHIP_G4X && ptoa((vm_paddr_t)Maxmem) > 0xfffffffful) 403171433Sanholt { 404171433Sanholt device_printf(dev, "agp_i810.c does not support physical " 405171433Sanholt "memory above 4GB.\n"); 406171433Sanholt return ENOENT; 407171433Sanholt } 408103243Sanholt 409171433Sanholt if (bus_alloc_resources(dev, sc->sc_res_spec, sc->sc_res)) { 41063010Sdfr agp_generic_detach(dev); 411153031Sanholt return ENODEV; 41263010Sdfr } 41363010Sdfr 41463010Sdfr sc->initial_aperture = AGP_GET_APERTURE(dev); 41563010Sdfr 416103243Sanholt gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT); 417103243Sanholt if (!gatt) { 418171433Sanholt bus_release_resources(dev, sc->sc_res_spec, sc->sc_res); 419103243Sanholt agp_generic_detach(dev); 420103243Sanholt return ENOMEM; 421103243Sanholt } 422103243Sanholt sc->gatt = gatt; 42363010Sdfr 424103243Sanholt gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT; 42563010Sdfr 426103243Sanholt if ( sc->chiptype == CHIP_I810 ) { 427103243Sanholt /* Some i810s have on-chip memory called dcache */ 428171433Sanholt if (bus_read_1(sc->sc_res[0], AGP_I810_DRT) & 429171433Sanholt AGP_I810_DRT_POPULATED) 430103243Sanholt sc->dcache_size = 4 * 1024 * 1024; 431103243Sanholt else 432103243Sanholt sc->dcache_size = 0; 433103243Sanholt 434103243Sanholt /* According to the specs the gatt on the i810 must be 64k */ 435103243Sanholt gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0, 436103243Sanholt 0, ~0, PAGE_SIZE, 0); 437103243Sanholt if (!gatt->ag_virtual) { 438103243Sanholt if (bootverbose) 439103243Sanholt device_printf(dev, "contiguous allocation failed\n"); 440171433Sanholt bus_release_resources(dev, sc->sc_res_spec, 441171433Sanholt sc->sc_res); 442103243Sanholt free(gatt, M_AGP); 44363010Sdfr agp_generic_detach(dev); 44463010Sdfr return ENOMEM; 44563010Sdfr } 446103243Sanholt bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t)); 447103243Sanholt 448103243Sanholt gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual); 449103243Sanholt agp_flush_cache(); 450103243Sanholt /* Install the GATT. */ 451171433Sanholt bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 452171433Sanholt gatt->ag_physical | 1); 453110785Sanholt } else if ( sc->chiptype == CHIP_I830 ) { 454103243Sanholt /* The i830 automatically initializes the 128k gatt on boot. */ 455103243Sanholt unsigned int gcc1, pgtblctl; 456103243Sanholt 457103243Sanholt gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1); 458103243Sanholt switch (gcc1 & AGP_I830_GCC1_GMS) { 459103243Sanholt case AGP_I830_GCC1_GMS_STOLEN_512: 460103243Sanholt sc->stolen = (512 - 132) * 1024 / 4096; 461103243Sanholt break; 462103243Sanholt case AGP_I830_GCC1_GMS_STOLEN_1024: 463103243Sanholt sc->stolen = (1024 - 132) * 1024 / 4096; 464103243Sanholt break; 465103243Sanholt case AGP_I830_GCC1_GMS_STOLEN_8192: 466103243Sanholt sc->stolen = (8192 - 132) * 1024 / 4096; 467103243Sanholt break; 468103243Sanholt default: 469103243Sanholt sc->stolen = 0; 470103243Sanholt device_printf(dev, "unknown memory configuration, disabling\n"); 471171433Sanholt bus_release_resources(dev, sc->sc_res_spec, 472171433Sanholt sc->sc_res); 473171433Sanholt free(gatt, M_AGP); 474103243Sanholt agp_generic_detach(dev); 475103243Sanholt return EINVAL; 476103243Sanholt } 477171433Sanholt if (sc->stolen > 0) { 478171433Sanholt device_printf(dev, "detected %dk stolen memory\n", 479171433Sanholt sc->stolen * 4); 480171433Sanholt } 481171433Sanholt device_printf(dev, "aperture size is %dM\n", 482171433Sanholt sc->initial_aperture / 1024 / 1024); 483103243Sanholt 484103243Sanholt /* GATT address is already in there, make sure it's enabled */ 485171433Sanholt pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL); 486103243Sanholt pgtblctl |= 1; 487171433Sanholt bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl); 488103243Sanholt 489103243Sanholt gatt->ag_physical = pgtblctl & ~1; 490171433Sanholt } else if (sc->chiptype == CHIP_I855 || sc->chiptype == CHIP_I915 || 491183555Srnoland sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33 || 492183555Srnoland sc->chiptype == CHIP_G4X) { 493171433Sanholt unsigned int gcc1, pgtblctl, stolen, gtt_size; 494153031Sanholt 495153031Sanholt /* Stolen memory is set up at the beginning of the aperture by 496171433Sanholt * the BIOS, consisting of the GATT followed by 4kb for the 497171433Sanholt * BIOS display. 498153031Sanholt */ 499171433Sanholt switch (sc->chiptype) { 500171433Sanholt case CHIP_I855: 501171433Sanholt gtt_size = 128; 502171433Sanholt break; 503171433Sanholt case CHIP_I915: 504171433Sanholt gtt_size = 256; 505171433Sanholt break; 506171433Sanholt case CHIP_I965: 507171433Sanholt switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) & 508171433Sanholt AGP_I810_PGTBL_SIZE_MASK) { 509171433Sanholt case AGP_I810_PGTBL_SIZE_128KB: 510171433Sanholt gtt_size = 128; 511110785Sanholt break; 512171433Sanholt case AGP_I810_PGTBL_SIZE_256KB: 513171433Sanholt gtt_size = 256; 514110785Sanholt break; 515171433Sanholt case AGP_I810_PGTBL_SIZE_512KB: 516171433Sanholt gtt_size = 512; 517110785Sanholt break; 518183555Srnoland case AGP_I965_PGTBL_SIZE_1MB: 519183555Srnoland gtt_size = 1024; 520183555Srnoland break; 521183555Srnoland case AGP_I965_PGTBL_SIZE_2MB: 522183555Srnoland gtt_size = 2048; 523183555Srnoland break; 524183555Srnoland case AGP_I965_PGTBL_SIZE_1_5MB: 525183555Srnoland gtt_size = 1024 + 512; 526183555Srnoland break; 527110785Sanholt default: 528171433Sanholt device_printf(dev, "Bad PGTBL size\n"); 529171433Sanholt bus_release_resources(dev, sc->sc_res_spec, 530171433Sanholt sc->sc_res); 531171433Sanholt free(gatt, M_AGP); 532110785Sanholt agp_generic_detach(dev); 533110785Sanholt return EINVAL; 534171433Sanholt } 535171433Sanholt break; 536183555Srnoland case CHIP_G33: 537183555Srnoland gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2); 538183555Srnoland switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) { 539183555Srnoland case AGP_G33_MGGC_GGMS_SIZE_1M: 540183555Srnoland gtt_size = 1024; 541183555Srnoland break; 542183555Srnoland case AGP_G33_MGGC_GGMS_SIZE_2M: 543183555Srnoland gtt_size = 2048; 544183555Srnoland break; 545183555Srnoland default: 546183555Srnoland device_printf(dev, "Bad PGTBL size\n"); 547183555Srnoland bus_release_resources(dev, sc->sc_res_spec, 548183555Srnoland sc->sc_res); 549183555Srnoland free(gatt, M_AGP); 550183555Srnoland agp_generic_detach(dev); 551183555Srnoland return EINVAL; 552183555Srnoland } 553183555Srnoland break; 554183555Srnoland case CHIP_G4X: 555183555Srnoland gtt_size = 0; 556183555Srnoland break; 557171433Sanholt default: 558171433Sanholt device_printf(dev, "Bad chiptype\n"); 559171433Sanholt bus_release_resources(dev, sc->sc_res_spec, 560171433Sanholt sc->sc_res); 561171433Sanholt free(gatt, M_AGP); 562171433Sanholt agp_generic_detach(dev); 563171433Sanholt return EINVAL; 564110785Sanholt } 565171433Sanholt 566171433Sanholt /* GCC1 is called MGGC on i915+ */ 567171433Sanholt gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1); 568171433Sanholt switch (gcc1 & AGP_I855_GCC1_GMS) { 569171433Sanholt case AGP_I855_GCC1_GMS_STOLEN_1M: 570171433Sanholt stolen = 1024; 571171433Sanholt break; 572171433Sanholt case AGP_I855_GCC1_GMS_STOLEN_4M: 573183555Srnoland stolen = 4 * 1024; 574171433Sanholt break; 575171433Sanholt case AGP_I855_GCC1_GMS_STOLEN_8M: 576183555Srnoland stolen = 8 * 1024; 577171433Sanholt break; 578171433Sanholt case AGP_I855_GCC1_GMS_STOLEN_16M: 579183555Srnoland stolen = 16 * 1024; 580171433Sanholt break; 581171433Sanholt case AGP_I855_GCC1_GMS_STOLEN_32M: 582183555Srnoland stolen = 32 * 1024; 583171433Sanholt break; 584171433Sanholt case AGP_I915_GCC1_GMS_STOLEN_48M: 585183555Srnoland if (sc->chiptype == CHIP_I915 || 586183555Srnoland sc->chiptype == CHIP_I965 || 587183555Srnoland sc->chiptype == CHIP_G33 || 588183555Srnoland sc->chiptype == CHIP_G4X) { 589183555Srnoland stolen = 48 * 1024; 590183555Srnoland } else { 591183555Srnoland stolen = 0; 592183555Srnoland } 593171433Sanholt break; 594171433Sanholt case AGP_I915_GCC1_GMS_STOLEN_64M: 595183555Srnoland if (sc->chiptype == CHIP_I915 || 596183555Srnoland sc->chiptype == CHIP_I965 || 597183555Srnoland sc->chiptype == CHIP_G33 || 598183555Srnoland sc->chiptype == CHIP_G4X) { 599183555Srnoland stolen = 64 * 1024; 600183555Srnoland } else { 601183555Srnoland stolen = 0; 602183555Srnoland } 603171433Sanholt break; 604171433Sanholt case AGP_G33_GCC1_GMS_STOLEN_128M: 605183555Srnoland if (sc->chiptype == CHIP_I965 || 606183555Srnoland sc->chiptype == CHIP_G33 || 607183555Srnoland sc->chiptype == CHIP_G4X) { 608183555Srnoland stolen = 128 * 1024; 609183555Srnoland } else { 610183555Srnoland stolen = 0; 611183555Srnoland } 612171433Sanholt break; 613171433Sanholt case AGP_G33_GCC1_GMS_STOLEN_256M: 614183555Srnoland if (sc->chiptype == CHIP_I965 || 615183555Srnoland sc->chiptype == CHIP_G33 || 616183555Srnoland sc->chiptype == CHIP_G4X) { 617183555Srnoland stolen = 256 * 1024; 618183555Srnoland } else { 619183555Srnoland stolen = 0; 620183555Srnoland } 621171433Sanholt break; 622183555Srnoland case AGP_G4X_GCC1_GMS_STOLEN_96M: 623183555Srnoland if (sc->chiptype == CHIP_I965 || 624183555Srnoland sc->chiptype == CHIP_G4X) { 625183555Srnoland stolen = 96 * 1024; 626183555Srnoland } else { 627183555Srnoland stolen = 0; 628183555Srnoland } 629183555Srnoland break; 630183555Srnoland case AGP_G4X_GCC1_GMS_STOLEN_160M: 631183555Srnoland if (sc->chiptype == CHIP_I965 || 632183555Srnoland sc->chiptype == CHIP_G4X) { 633183555Srnoland stolen = 160 * 1024; 634183555Srnoland } else { 635183555Srnoland stolen = 0; 636183555Srnoland } 637183555Srnoland break; 638183555Srnoland case AGP_G4X_GCC1_GMS_STOLEN_224M: 639183555Srnoland if (sc->chiptype == CHIP_I965 || 640183555Srnoland sc->chiptype == CHIP_G4X) { 641183555Srnoland stolen = 224 * 1024; 642183555Srnoland } else { 643183555Srnoland stolen = 0; 644183555Srnoland } 645183555Srnoland break; 646183555Srnoland case AGP_G4X_GCC1_GMS_STOLEN_352M: 647183555Srnoland if (sc->chiptype == CHIP_I965 || 648183555Srnoland sc->chiptype == CHIP_G4X) { 649183555Srnoland stolen = 352 * 1024; 650183555Srnoland } else { 651183555Srnoland stolen = 0; 652183555Srnoland } 653183555Srnoland break; 654171433Sanholt default: 655171433Sanholt device_printf(dev, "unknown memory configuration, " 656171433Sanholt "disabling\n"); 657171433Sanholt bus_release_resources(dev, sc->sc_res_spec, 658171433Sanholt sc->sc_res); 659171433Sanholt free(gatt, M_AGP); 660171433Sanholt agp_generic_detach(dev); 661171433Sanholt return EINVAL; 662171433Sanholt } 663183555Srnoland 664186434Srnoland gtt_size += 4; 665183555Srnoland 666183555Srnoland sc->stolen = (stolen - gtt_size) * 1024 / 4096; 667110785Sanholt if (sc->stolen > 0) 668110785Sanholt device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4); 669110785Sanholt device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024); 670110785Sanholt 671110785Sanholt /* GATT address is already in there, make sure it's enabled */ 672171433Sanholt pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL); 673110785Sanholt pgtblctl |= 1; 674171433Sanholt bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl); 675110785Sanholt 676110785Sanholt gatt->ag_physical = pgtblctl & ~1; 67763010Sdfr } 67863010Sdfr 679171433Sanholt if (0) 680171433Sanholt agp_i810_dump_regs(dev); 681171433Sanholt 682153579Sjhb return 0; 68363010Sdfr} 68463010Sdfr 68563010Sdfrstatic int 68663010Sdfragp_i810_detach(device_t dev) 68763010Sdfr{ 68863010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 68963010Sdfr 690173203Sjhb agp_free_cdev(dev); 69163010Sdfr 69263010Sdfr /* Clear the GATT base. */ 693103243Sanholt if ( sc->chiptype == CHIP_I810 ) { 694171433Sanholt bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 0); 695103243Sanholt } else { 696103243Sanholt unsigned int pgtblctl; 697171433Sanholt pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL); 698103243Sanholt pgtblctl &= ~1; 699171433Sanholt bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl); 700103243Sanholt } 70163010Sdfr 70263010Sdfr /* Put the aperture back the way it started. */ 70363010Sdfr AGP_SET_APERTURE(dev, sc->initial_aperture); 70463010Sdfr 705103243Sanholt if ( sc->chiptype == CHIP_I810 ) { 706103243Sanholt contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP); 707103243Sanholt } 708103243Sanholt free(sc->gatt, M_AGP); 70963010Sdfr 710171433Sanholt bus_release_resources(dev, sc->sc_res_spec, sc->sc_res); 711173203Sjhb agp_free_res(dev); 71263010Sdfr 71363010Sdfr return 0; 71463010Sdfr} 71563010Sdfr 716177115Sremkostatic int 717177115Sremkoagp_i810_resume(device_t dev) 718177115Sremko{ 719177115Sremko struct agp_i810_softc *sc; 720177115Sremko sc = device_get_softc(dev); 721177115Sremko 722177115Sremko AGP_SET_APERTURE(dev, sc->initial_aperture); 723177115Sremko 724177115Sremko /* Install the GATT. */ 725177115Sremko bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 726177115Sremko sc->gatt->ag_physical | 1); 727177115Sremko 728177115Sremko return (bus_generic_resume(dev)); 729177115Sremko} 730177115Sremko 731171433Sanholt/** 732171433Sanholt * Sets the PCI resource size of the aperture on i830-class and below chipsets, 733171433Sanholt * while returning failure on later chipsets when an actual change is 734171433Sanholt * requested. 735171433Sanholt * 736171433Sanholt * This whole function is likely bogus, as the kernel would probably need to 737171433Sanholt * reconfigure the placement of the AGP aperture if a larger size is requested, 738171433Sanholt * which doesn't happen currently. 739171433Sanholt */ 74063010Sdfrstatic int 74163010Sdfragp_i810_set_aperture(device_t dev, u_int32_t aperture) 74263010Sdfr{ 74363010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 744153031Sanholt u_int16_t miscc, gcc1; 74563010Sdfr 746153031Sanholt switch (sc->chiptype) { 747153031Sanholt case CHIP_I810: 748103243Sanholt /* 749103243Sanholt * Double check for sanity. 750103243Sanholt */ 751103243Sanholt if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) { 752103243Sanholt device_printf(dev, "bad aperture size %d\n", aperture); 753103243Sanholt return EINVAL; 754103243Sanholt } 755153031Sanholt 756103243Sanholt miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2); 757103243Sanholt miscc &= ~AGP_I810_MISCC_WINSIZE; 758103243Sanholt if (aperture == 32 * 1024 * 1024) 759103243Sanholt miscc |= AGP_I810_MISCC_WINSIZE_32; 760103243Sanholt else 761103243Sanholt miscc |= AGP_I810_MISCC_WINSIZE_64; 762103243Sanholt 763103243Sanholt pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2); 764153031Sanholt break; 765153031Sanholt case CHIP_I830: 766153031Sanholt if (aperture != 64 * 1024 * 1024 && 767153031Sanholt aperture != 128 * 1024 * 1024) { 768103243Sanholt device_printf(dev, "bad aperture size %d\n", aperture); 769103243Sanholt return EINVAL; 770103243Sanholt } 771103243Sanholt gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2); 772103243Sanholt gcc1 &= ~AGP_I830_GCC1_GMASIZE; 773103243Sanholt if (aperture == 64 * 1024 * 1024) 774103243Sanholt gcc1 |= AGP_I830_GCC1_GMASIZE_64; 775103243Sanholt else 776103243Sanholt gcc1 |= AGP_I830_GCC1_GMASIZE_128; 77763010Sdfr 778103243Sanholt pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2); 779153031Sanholt break; 780153031Sanholt case CHIP_I855: 781153031Sanholt case CHIP_I915: 782171433Sanholt case CHIP_I965: 783171433Sanholt case CHIP_G33: 784186434Srnoland case CHIP_G4X: 785171433Sanholt return agp_generic_set_aperture(dev, aperture); 786171433Sanholt } 787153031Sanholt 788171433Sanholt return 0; 789171433Sanholt} 790153031Sanholt 791171433Sanholt/** 792171433Sanholt * Writes a GTT entry mapping the page at the given offset from the beginning 793171433Sanholt * of the aperture to the given physical address. 794171433Sanholt */ 795171433Sanholtstatic void 796171433Sanholtagp_i810_write_gtt_entry(device_t dev, int offset, vm_offset_t physical, 797171433Sanholt int enabled) 798171433Sanholt{ 799171433Sanholt struct agp_i810_softc *sc = device_get_softc(dev); 800171433Sanholt u_int32_t pte; 801171433Sanholt 802171433Sanholt pte = (u_int32_t)physical | 1; 803186434Srnoland if (sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33 || 804186434Srnoland sc->chiptype == CHIP_G4X) { 805171433Sanholt pte |= (physical & 0x0000000f00000000ull) >> 28; 806171433Sanholt } else { 807171433Sanholt /* If we do actually have memory above 4GB on an older system, 808171433Sanholt * crash cleanly rather than scribble on system memory, 809171433Sanholt * so we know we need to fix it. 810171433Sanholt */ 811171433Sanholt KASSERT((pte & 0x0000000f00000000ull) == 0, 812171433Sanholt (">4GB physical address in agp")); 813171433Sanholt } 814171433Sanholt 815171433Sanholt switch (sc->chiptype) { 816171433Sanholt case CHIP_I810: 817171433Sanholt case CHIP_I830: 818171433Sanholt case CHIP_I855: 819171433Sanholt bus_write_4(sc->sc_res[0], 820171433Sanholt AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, pte); 821153031Sanholt break; 822171433Sanholt case CHIP_I915: 823171433Sanholt case CHIP_G33: 824171433Sanholt bus_write_4(sc->sc_res[1], 825171433Sanholt (offset >> AGP_PAGE_SHIFT) * 4, pte); 826171433Sanholt break; 827171433Sanholt case CHIP_I965: 828171433Sanholt bus_write_4(sc->sc_res[0], 829171433Sanholt (offset >> AGP_PAGE_SHIFT) * 4 + (512 * 1024), pte); 830171433Sanholt break; 831186434Srnoland case CHIP_G4X: 832186434Srnoland bus_write_4(sc->sc_res[0], 833186434Srnoland (offset >> AGP_PAGE_SHIFT) * 4 + (2 * 1024 * 1024), pte); 834186434Srnoland break; 835103243Sanholt } 83663010Sdfr} 83763010Sdfr 83863010Sdfrstatic int 83963010Sdfragp_i810_bind_page(device_t dev, int offset, vm_offset_t physical) 84063010Sdfr{ 84163010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 84263010Sdfr 843103243Sanholt if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) { 844103243Sanholt device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries); 84563010Sdfr return EINVAL; 846103243Sanholt } 84763010Sdfr 848110785Sanholt if ( sc->chiptype != CHIP_I810 ) { 849103243Sanholt if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) { 850103243Sanholt device_printf(dev, "trying to bind into stolen memory"); 851103243Sanholt return EINVAL; 852103243Sanholt } 853103243Sanholt } 854103243Sanholt 855171433Sanholt agp_i810_write_gtt_entry(dev, offset, physical, 1); 856153031Sanholt 85763010Sdfr return 0; 85863010Sdfr} 85963010Sdfr 86063010Sdfrstatic int 86163010Sdfragp_i810_unbind_page(device_t dev, int offset) 86263010Sdfr{ 86363010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 86463010Sdfr 86563010Sdfr if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 86663010Sdfr return EINVAL; 86763010Sdfr 868110785Sanholt if ( sc->chiptype != CHIP_I810 ) { 869103272Sanholt if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) { 870103243Sanholt device_printf(dev, "trying to unbind from stolen memory"); 871103272Sanholt return EINVAL; 872103272Sanholt } 873103243Sanholt } 874103243Sanholt 875171433Sanholt agp_i810_write_gtt_entry(dev, offset, 0, 0); 876171433Sanholt 87763010Sdfr return 0; 87863010Sdfr} 87963010Sdfr 88063010Sdfr/* 88163010Sdfr * Writing via memory mapped registers already flushes all TLBs. 88263010Sdfr */ 88363010Sdfrstatic void 88463010Sdfragp_i810_flush_tlb(device_t dev) 88563010Sdfr{ 88663010Sdfr} 88763010Sdfr 88863010Sdfrstatic int 88963010Sdfragp_i810_enable(device_t dev, u_int32_t mode) 89063010Sdfr{ 89163010Sdfr 89263010Sdfr return 0; 89363010Sdfr} 89463010Sdfr 89563010Sdfrstatic struct agp_memory * 89663010Sdfragp_i810_alloc_memory(device_t dev, int type, vm_size_t size) 89763010Sdfr{ 89863010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 89963010Sdfr struct agp_memory *mem; 90063010Sdfr 90163010Sdfr if ((size & (AGP_PAGE_SIZE - 1)) != 0) 90263010Sdfr return 0; 90363010Sdfr 90463010Sdfr if (sc->agp.as_allocated + size > sc->agp.as_maxmem) 90563010Sdfr return 0; 90663010Sdfr 90763010Sdfr if (type == 1) { 90863010Sdfr /* 90963010Sdfr * Mapping local DRAM into GATT. 91063010Sdfr */ 911110785Sanholt if ( sc->chiptype != CHIP_I810 ) 912103243Sanholt return 0; 91363010Sdfr if (size != sc->dcache_size) 91463010Sdfr return 0; 91563010Sdfr } else if (type == 2) { 91663010Sdfr /* 917158655Sanholt * Type 2 is the contiguous physical memory type, that hands 918158655Sanholt * back a physical address. This is used for cursors on i810. 919158655Sanholt * Hand back as many single pages with physical as the user 920158655Sanholt * wants, but only allow one larger allocation (ARGB cursor) 921158655Sanholt * for simplicity. 92263010Sdfr */ 923158655Sanholt if (size != AGP_PAGE_SIZE) { 924158655Sanholt if (sc->argb_cursor != NULL) 925158655Sanholt return 0; 926158655Sanholt 927158655Sanholt /* Allocate memory for ARGB cursor, if we can. */ 928158655Sanholt sc->argb_cursor = contigmalloc(size, M_AGP, 929158655Sanholt 0, 0, ~0, PAGE_SIZE, 0); 930158655Sanholt if (sc->argb_cursor == NULL) 931158655Sanholt return 0; 932158655Sanholt } 93363010Sdfr } 93463010Sdfr 935111119Simp mem = malloc(sizeof *mem, M_AGP, M_WAITOK); 93663010Sdfr mem->am_id = sc->agp.as_nextid++; 93763010Sdfr mem->am_size = size; 93863010Sdfr mem->am_type = type; 939158655Sanholt if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE)) 94063010Sdfr mem->am_obj = vm_object_allocate(OBJT_DEFAULT, 94163010Sdfr atop(round_page(size))); 94263010Sdfr else 94363010Sdfr mem->am_obj = 0; 94463010Sdfr 94563010Sdfr if (type == 2) { 946158655Sanholt if (size == AGP_PAGE_SIZE) { 947158655Sanholt /* 948158655Sanholt * Allocate and wire down the page now so that we can 949158655Sanholt * get its physical address. 950158655Sanholt */ 951158655Sanholt vm_page_t m; 952158655Sanholt 953158655Sanholt VM_OBJECT_LOCK(mem->am_obj); 954158655Sanholt m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY | 955158655Sanholt VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY); 956158655Sanholt VM_OBJECT_UNLOCK(mem->am_obj); 957158655Sanholt mem->am_physical = VM_PAGE_TO_PHYS(m); 958158655Sanholt } else { 959158655Sanholt /* Our allocation is already nicely wired down for us. 960158655Sanholt * Just grab the physical address. 961158655Sanholt */ 962158655Sanholt mem->am_physical = vtophys(sc->argb_cursor); 963158655Sanholt } 96463010Sdfr } else { 96563010Sdfr mem->am_physical = 0; 96663010Sdfr } 96763010Sdfr 96863010Sdfr mem->am_offset = 0; 96963010Sdfr mem->am_is_bound = 0; 97063010Sdfr TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link); 97163010Sdfr sc->agp.as_allocated += size; 97263010Sdfr 97363010Sdfr return mem; 97463010Sdfr} 97563010Sdfr 97663010Sdfrstatic int 97763010Sdfragp_i810_free_memory(device_t dev, struct agp_memory *mem) 97863010Sdfr{ 97963010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 98063010Sdfr 98163010Sdfr if (mem->am_is_bound) 98263010Sdfr return EBUSY; 98363010Sdfr 98463010Sdfr if (mem->am_type == 2) { 985158655Sanholt if (mem->am_size == AGP_PAGE_SIZE) { 986158655Sanholt /* 987158655Sanholt * Unwire the page which we wired in alloc_memory. 988158655Sanholt */ 989158655Sanholt vm_page_t m; 990158655Sanholt 991158655Sanholt VM_OBJECT_LOCK(mem->am_obj); 992158655Sanholt m = vm_page_lookup(mem->am_obj, 0); 993158655Sanholt VM_OBJECT_UNLOCK(mem->am_obj); 994158655Sanholt vm_page_lock_queues(); 995158655Sanholt vm_page_unwire(m, 0); 996158655Sanholt vm_page_unlock_queues(); 997158655Sanholt } else { 998158655Sanholt contigfree(sc->argb_cursor, mem->am_size, M_AGP); 999158655Sanholt sc->argb_cursor = NULL; 1000158655Sanholt } 100163010Sdfr } 100263010Sdfr 100363010Sdfr sc->agp.as_allocated -= mem->am_size; 100463010Sdfr TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link); 100563010Sdfr if (mem->am_obj) 100663010Sdfr vm_object_deallocate(mem->am_obj); 100763010Sdfr free(mem, M_AGP); 100863010Sdfr return 0; 100963010Sdfr} 101063010Sdfr 101163010Sdfrstatic int 101263010Sdfragp_i810_bind_memory(device_t dev, struct agp_memory *mem, 101363010Sdfr vm_offset_t offset) 101463010Sdfr{ 101563010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 101663010Sdfr vm_offset_t i; 101763010Sdfr 1018158655Sanholt /* Do some sanity checks first. */ 1019158655Sanholt if (offset < 0 || (offset & (AGP_PAGE_SIZE - 1)) != 0 || 1020158655Sanholt offset + mem->am_size > AGP_GET_APERTURE(dev)) { 1021158655Sanholt device_printf(dev, "binding memory at bad offset %#x\n", 1022158655Sanholt (int)offset); 1023158655Sanholt return EINVAL; 1024158655Sanholt } 1025158655Sanholt 1026158655Sanholt if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) { 1027158655Sanholt mtx_lock(&sc->agp.as_lock); 1028158655Sanholt if (mem->am_is_bound) { 1029158655Sanholt mtx_unlock(&sc->agp.as_lock); 1030158655Sanholt return EINVAL; 1031158655Sanholt } 1032158655Sanholt /* The memory's already wired down, just stick it in the GTT. */ 1033158655Sanholt for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) { 1034171433Sanholt agp_i810_write_gtt_entry(dev, offset + i, 1035171433Sanholt mem->am_physical + i, 1); 1036158655Sanholt } 1037158655Sanholt agp_flush_cache(); 1038158655Sanholt mem->am_offset = offset; 1039158655Sanholt mem->am_is_bound = 1; 1040158655Sanholt mtx_unlock(&sc->agp.as_lock); 1041158655Sanholt return 0; 1042158655Sanholt } 1043158655Sanholt 104463010Sdfr if (mem->am_type != 1) 104563010Sdfr return agp_generic_bind_memory(dev, mem, offset); 104663010Sdfr 1047110785Sanholt if ( sc->chiptype != CHIP_I810 ) 1048103243Sanholt return EINVAL; 1049103243Sanholt 105063010Sdfr for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) { 1051171433Sanholt bus_write_4(sc->sc_res[0], 1052171433Sanholt AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3); 105363010Sdfr } 105463010Sdfr 105563010Sdfr return 0; 105663010Sdfr} 105763010Sdfr 105863010Sdfrstatic int 105963010Sdfragp_i810_unbind_memory(device_t dev, struct agp_memory *mem) 106063010Sdfr{ 106163010Sdfr struct agp_i810_softc *sc = device_get_softc(dev); 106263010Sdfr vm_offset_t i; 106363010Sdfr 1064158655Sanholt if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) { 1065158655Sanholt mtx_lock(&sc->agp.as_lock); 1066158655Sanholt if (!mem->am_is_bound) { 1067158655Sanholt mtx_unlock(&sc->agp.as_lock); 1068158655Sanholt return EINVAL; 1069158655Sanholt } 1070158655Sanholt 1071158655Sanholt for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) { 1072171433Sanholt agp_i810_write_gtt_entry(dev, mem->am_offset + i, 1073171433Sanholt 0, 0); 1074158655Sanholt } 1075158655Sanholt agp_flush_cache(); 1076158655Sanholt mem->am_is_bound = 0; 1077158655Sanholt mtx_unlock(&sc->agp.as_lock); 1078158655Sanholt return 0; 1079158655Sanholt } 1080158655Sanholt 108163010Sdfr if (mem->am_type != 1) 108263010Sdfr return agp_generic_unbind_memory(dev, mem); 108363010Sdfr 1084110785Sanholt if ( sc->chiptype != CHIP_I810 ) 1085103243Sanholt return EINVAL; 1086103243Sanholt 1087171433Sanholt for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) { 1088171433Sanholt bus_write_4(sc->sc_res[0], 1089171433Sanholt AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0); 1090171433Sanholt } 109163010Sdfr 109263010Sdfr return 0; 109363010Sdfr} 109463010Sdfr 109563010Sdfrstatic device_method_t agp_i810_methods[] = { 109663010Sdfr /* Device interface */ 1097155186Sjhb DEVMETHOD(device_identify, agp_i810_identify), 109863010Sdfr DEVMETHOD(device_probe, agp_i810_probe), 109963010Sdfr DEVMETHOD(device_attach, agp_i810_attach), 110063010Sdfr DEVMETHOD(device_detach, agp_i810_detach), 1101177115Sremko DEVMETHOD(device_suspend, bus_generic_suspend), 1102177115Sremko DEVMETHOD(device_resume, agp_i810_resume), 110363010Sdfr 110463010Sdfr /* AGP interface */ 1105171433Sanholt DEVMETHOD(agp_get_aperture, agp_generic_get_aperture), 110663010Sdfr DEVMETHOD(agp_set_aperture, agp_i810_set_aperture), 110763010Sdfr DEVMETHOD(agp_bind_page, agp_i810_bind_page), 110863010Sdfr DEVMETHOD(agp_unbind_page, agp_i810_unbind_page), 110963010Sdfr DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb), 111063010Sdfr DEVMETHOD(agp_enable, agp_i810_enable), 111163010Sdfr DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory), 111263010Sdfr DEVMETHOD(agp_free_memory, agp_i810_free_memory), 111363010Sdfr DEVMETHOD(agp_bind_memory, agp_i810_bind_memory), 111463010Sdfr DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory), 111563010Sdfr 111663010Sdfr { 0, 0 } 111763010Sdfr}; 111863010Sdfr 111963010Sdfrstatic driver_t agp_i810_driver = { 112063010Sdfr "agp", 112163010Sdfr agp_i810_methods, 112263010Sdfr sizeof(struct agp_i810_softc), 112363010Sdfr}; 112463010Sdfr 112563010Sdfrstatic devclass_t agp_devclass; 112663010Sdfr 1127153579SjhbDRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, 0, 0); 1128113506SmdoddMODULE_DEPEND(agp_i810, agp, 1, 1, 1); 1129113506SmdoddMODULE_DEPEND(agp_i810, pci, 1, 1, 1); 1130