2 * Copyright (c) 2000 Doug Rabson
3 * Copyright (c) 2000 Ruslan Ermilov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/agp/agp_i810.c,v 1.43 2007/11/12 21:51:36 jhb Exp $
31 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
32 * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
45 #include <bus/pci/pcivar.h>
46 #include <bus/pci/pcireg.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pageout.h>
56 #include <machine/md_var.h>
58 #define bus_read_1(r, o) \
59 bus_space_read_1((r)->r_bustag, (r)->r_bushandle, (o))
60 #define bus_read_4(r, o) \
61 bus_space_read_4((r)->r_bustag, (r)->r_bushandle, (o))
62 #define bus_write_4(r, o, v) \
63 bus_space_write_4((r)->r_bustag, (r)->r_bushandle, (o), (v))
65 MALLOC_DECLARE(M_AGP);
68 CHIP_I810, /* i810/i815 */
69 CHIP_I830, /* 830M/845G */
70 CHIP_I855, /* 852GM/855GM/865G */
71 CHIP_I915, /* 915G/915GM */
73 CHIP_G33, /* G33/Q33/Q35 */
74 CHIP_IGD, /* G33 like IGD */
75 CHIP_G4X, /* G45/Q45 */
78 /* The i810 through i855 have the registers at BAR 1, and the GATT gets
79 * allocated by us. The i915 has registers in BAR 0 and the GATT is at the
80 * start of the stolen memory, and should only be accessed by the OS through
81 * BAR 3. The G965 has registers and GATT in the same BAR (0) -- first 512KB
82 * is registers, second 512KB is GATT.
84 static struct resource_spec agp_i810_res_spec[] = {
85 { SYS_RES_MEMORY, AGP_I810_MMADR, RF_ACTIVE | RF_SHAREABLE },
89 static struct resource_spec agp_i915_res_spec[] = {
90 { SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
91 { SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
95 static struct resource_spec agp_i965_res_spec[] = {
96 { SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
100 struct agp_i810_softc {
101 struct agp_softc agp;
102 u_int32_t initial_aperture; /* aperture size at startup */
103 struct agp_gatt *gatt;
104 int chiptype; /* i810-like or i830 */
105 u_int32_t dcache_size; /* i810 only */
106 u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */
107 device_t bdev; /* bridge device */
109 void *argb_cursor; /* contigmalloc area for ARGB cursor */
111 struct resource_spec * sc_res_spec;
112 struct resource *sc_res[2];
115 /* For adding new devices, devid is the id of the graphics controller
116 * (pci:0:2:0, for example). The placeholder (usually at pci:0:2:1) for the
117 * second head should never be added. The bridge_offset is the offset to
118 * subtract from devid to get the id of the hostb that the device is on.
120 static const struct agp_i810_match {
125 } agp_i810_matches[] = {
126 {0x71218086, CHIP_I810, 0x00010000,
127 "Intel 82810 (i810 GMCH) SVGA controller"},
128 {0x71238086, CHIP_I810, 0x00010000,
129 "Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller"},
130 {0x71258086, CHIP_I810, 0x00010000,
131 "Intel 82810E (i810E GMCH) SVGA controller"},
132 {0x11328086, CHIP_I810, 0x00020000,
133 "Intel 82815 (i815 GMCH) SVGA controller"},
134 {0x35778086, CHIP_I830, 0x00020000,
135 "Intel 82830M (830M GMCH) SVGA controller"},
136 {0x25628086, CHIP_I830, 0x00020000,
137 "Intel 82845M (845M GMCH) SVGA controller"},
138 {0x35828086, CHIP_I855, 0x00020000,
139 "Intel 82852/855GM SVGA controller"},
140 {0x25728086, CHIP_I855, 0x00020000,
141 "Intel 82865G (865G GMCH) SVGA controller"},
142 {0x25828086, CHIP_I915, 0x00020000,
143 "Intel 82915G (915G GMCH) SVGA controller"},
144 {0x258A8086, CHIP_I915, 0x00020000,
145 "Intel E7221 SVGA controller"},
146 {0x25928086, CHIP_I915, 0x00020000,
147 "Intel 82915GM (915GM GMCH) SVGA controller"},
148 {0x27728086, CHIP_I915, 0x00020000,
149 "Intel 82945G (945G GMCH) SVGA controller"},
150 {0x27A28086, CHIP_I915, 0x00020000,
151 "Intel 82945GM (945GM GMCH) SVGA controller"},
152 {0x27AE8086, CHIP_I915, 0x00020000,
153 "Intel 945GME SVGA controller"},
154 {0x29728086, CHIP_I965, 0x00020000,
155 "Intel 946GZ SVGA controller"},
156 {0x29828086, CHIP_I965, 0x00020000,
157 "Intel G965 SVGA controller"},
158 {0x29928086, CHIP_I965, 0x00020000,
159 "Intel Q965 SVGA controller"},
160 {0x29A28086, CHIP_I965, 0x00020000,
161 "Intel G965 SVGA controller"},
162 {0x29B28086, CHIP_G33, 0x00020000,
163 "Intel Q35 SVGA controller"},
164 {0x29C28086, CHIP_G33, 0x00020000,
165 "Intel G33 SVGA controller"},
166 {0x29D28086, CHIP_G33, 0x00020000,
167 "Intel Q33 SVGA controller"},
168 {0x2A028086, CHIP_I965, 0x00020000,
169 "Intel GM965 SVGA controller"},
170 {0x2A128086, CHIP_I965, 0x00020000,
171 "Intel GME965 SVGA controller"},
172 {0x2A428086, CHIP_G4X, 0x00020000,
173 "Intel GM45 SVGA controller"},
174 {0x2E028086, CHIP_G4X, 0x00020000,
175 "Intel 4 Series SVGA controller"},
176 {0x2E128086, CHIP_G4X, 0x00020000,
177 "Intel Q45 SVGA controller"},
178 {0x2E228086, CHIP_G4X, 0x00020000,
179 "Intel G45 SVGA controller"},
180 {0x2E328086, CHIP_G4X, 0x00020000,
181 "Intel G41 SVGA controller"},
182 {0xA0018086, CHIP_IGD, 0x00010000,
183 "Intel IGD SVGA controller"},
184 {0xA0118086, CHIP_IGD, 0x00010000,
185 "Intel IGD SVGA controller"},
189 static const struct agp_i810_match*
190 agp_i810_match(device_t dev)
194 if (pci_get_class(dev) != PCIC_DISPLAY
195 || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
198 devid = pci_get_devid(dev);
199 for (i = 0; agp_i810_matches[i].devid != 0; i++) {
200 if (agp_i810_matches[i].devid == devid)
203 if (agp_i810_matches[i].devid == 0)
206 return &agp_i810_matches[i];
210 * Find bridge device.
213 agp_i810_find_bridge(device_t dev)
215 device_t *children, child;
218 const struct agp_i810_match *match;
220 match = agp_i810_match(dev);
221 devid = match->devid - match->bridge_offset;
223 if (device_get_children(device_get_parent(device_get_parent(dev)),
224 &children, &nchildren))
227 for (i = 0; i < nchildren; i++) {
230 if (pci_get_devid(child) == devid) {
231 kfree(children, M_TEMP);
235 kfree(children, M_TEMP);
240 agp_i810_identify(driver_t *driver, device_t parent)
243 if (device_find_child(parent, "agp", -1) == NULL &&
244 agp_i810_match(parent))
245 device_add_child(parent, "agp", -1);
249 agp_i810_probe(device_t dev)
252 const struct agp_i810_match *match;
256 if (resource_disabled("agp", device_get_unit(dev)))
258 match = agp_i810_match(dev);
262 bdev = agp_i810_find_bridge(dev);
265 kprintf("I810: can't find bridge device\n");
270 * checking whether internal graphics device has been activated.
272 switch (match->chiptype) {
274 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
275 if ((smram & AGP_I810_SMRAM_GMS) ==
276 AGP_I810_SMRAM_GMS_DISABLED) {
278 kprintf("I810: disabled, not probing\n");
284 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
285 if ((gcc1 & AGP_I830_GCC1_DEV2) ==
286 AGP_I830_GCC1_DEV2_DISABLED) {
288 kprintf("I830: disabled, not probing\n");
297 deven = pci_read_config(bdev, AGP_I915_DEVEN, 4);
298 if ((deven & AGP_I915_DEVEN_D2F0) ==
299 AGP_I915_DEVEN_D2F0_DISABLED) {
301 kprintf("I915: disabled, not probing\n");
308 if (match->devid == 0x35828086) {
309 switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
312 "Intel 82855GME (855GME GMCH) SVGA controller");
316 "Intel 82855GM (855GM GMCH) SVGA controller");
320 "Intel 82852GME (852GME GMCH) SVGA controller");
324 "Intel 82852GM (852GM GMCH) SVGA controller");
328 "Intel 8285xM (85xGM GMCH) SVGA controller");
332 device_set_desc(dev, match->name);
335 return BUS_PROBE_DEFAULT;
339 agp_i810_dump_regs(device_t dev)
341 struct agp_i810_softc *sc = device_get_softc(dev);
343 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
344 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
346 switch (sc->chiptype) {
348 device_printf(dev, "AGP_I810_MISCC: 0x%04x\n",
349 pci_read_config(sc->bdev, AGP_I810_MISCC, 2));
352 device_printf(dev, "AGP_I830_GCC1: 0x%02x\n",
353 pci_read_config(sc->bdev, AGP_I830_GCC1, 1));
356 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
357 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
364 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
365 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
366 device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
367 pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
370 device_printf(dev, "Aperture resource size: %d bytes\n",
371 AGP_GET_APERTURE(dev));
375 agp_i810_attach(device_t dev)
377 struct agp_i810_softc *sc = device_get_softc(dev);
378 struct agp_gatt *gatt;
379 const struct agp_i810_match *match;
382 sc->bdev = agp_i810_find_bridge(dev);
386 match = agp_i810_match(dev);
387 sc->chiptype = match->chiptype;
389 switch (sc->chiptype) {
393 sc->sc_res_spec = agp_i810_res_spec;
394 agp_set_aperture_resource(dev, AGP_APBASE);
399 sc->sc_res_spec = agp_i915_res_spec;
400 agp_set_aperture_resource(dev, AGP_I915_GMADR);
404 sc->sc_res_spec = agp_i965_res_spec;
405 agp_set_aperture_resource(dev, AGP_I915_GMADR);
409 error = agp_generic_attach(dev);
413 if (sc->chiptype != CHIP_I965 && sc->chiptype != CHIP_G33 &&
414 sc->chiptype != CHIP_IGD && sc->chiptype != CHIP_G4X &&
415 ptoa((vm_paddr_t)Maxmem) > 0xfffffffful)
417 device_printf(dev, "agp_i810.c does not support physical "
418 "memory above 4GB.\n");
422 if (bus_alloc_resources(dev, sc->sc_res_spec, sc->sc_res)) {
423 agp_generic_detach(dev);
427 sc->initial_aperture = AGP_GET_APERTURE(dev);
428 if (sc->initial_aperture == 0) {
429 device_printf(dev, "bad initial aperture size, disabling\n");
433 gatt = kmalloc( sizeof(struct agp_gatt), M_AGP, M_INTWAIT);
436 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
438 if ( sc->chiptype == CHIP_I810 ) {
439 /* Some i810s have on-chip memory called dcache */
440 if (bus_read_1(sc->sc_res[0], AGP_I810_DRT) &
441 AGP_I810_DRT_POPULATED)
442 sc->dcache_size = 4 * 1024 * 1024;
446 /* According to the specs the gatt on the i810 must be 64k */
447 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
448 0, ~0, PAGE_SIZE, 0);
449 if (!gatt->ag_virtual) {
451 device_printf(dev, "contiguous allocation failed\n");
452 bus_release_resources(dev, sc->sc_res_spec,
455 agp_generic_detach(dev);
458 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
460 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
462 /* Install the GATT. */
463 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
464 gatt->ag_physical | 1);
465 } else if ( sc->chiptype == CHIP_I830 ) {
466 /* The i830 automatically initializes the 128k gatt on boot. */
467 unsigned int gcc1, pgtblctl;
469 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
470 switch (gcc1 & AGP_I830_GCC1_GMS) {
471 case AGP_I830_GCC1_GMS_STOLEN_512:
472 sc->stolen = (512 - 132) * 1024 / 4096;
474 case AGP_I830_GCC1_GMS_STOLEN_1024:
475 sc->stolen = (1024 - 132) * 1024 / 4096;
477 case AGP_I830_GCC1_GMS_STOLEN_8192:
478 sc->stolen = (8192 - 132) * 1024 / 4096;
482 device_printf(dev, "unknown memory configuration, disabling\n");
483 bus_release_resources(dev, sc->sc_res_spec,
486 agp_generic_detach(dev);
489 if (sc->stolen > 0) {
490 device_printf(dev, "detected %dk stolen memory\n",
493 device_printf(dev, "aperture size is %dM\n",
494 sc->initial_aperture / 1024 / 1024);
496 /* GATT address is already in there, make sure it's enabled */
497 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
499 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
501 gatt->ag_physical = pgtblctl & ~1;
502 } else if (sc->chiptype == CHIP_I855 || sc->chiptype == CHIP_I915 ||
503 sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33 ||
504 sc->chiptype == CHIP_IGD || sc->chiptype == CHIP_G4X) {
505 unsigned int gcc1, pgtblctl, stolen, gtt_size;
507 /* Stolen memory is set up at the beginning of the aperture by
508 * the BIOS, consisting of the GATT followed by 4kb for the
511 switch (sc->chiptype) {
519 switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
520 AGP_I810_PGTBL_SIZE_MASK) {
521 case AGP_I810_PGTBL_SIZE_128KB:
524 case AGP_I810_PGTBL_SIZE_256KB:
527 case AGP_I810_PGTBL_SIZE_512KB:
530 case AGP_I965_PGTBL_SIZE_1MB:
533 case AGP_I965_PGTBL_SIZE_2MB:
536 case AGP_I965_PGTBL_SIZE_1_5MB:
537 gtt_size = 1024 + 512;
540 device_printf(dev, "Bad PGTBL size\n");
541 bus_release_resources(dev, sc->sc_res_spec,
544 agp_generic_detach(dev);
549 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2);
550 switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) {
551 case AGP_G33_MGGC_GGMS_SIZE_1M:
554 case AGP_G33_MGGC_GGMS_SIZE_2M:
558 device_printf(dev, "Bad PGTBL size\n");
559 bus_release_resources(dev, sc->sc_res_spec,
562 agp_generic_detach(dev);
571 device_printf(dev, "Bad chiptype\n");
572 bus_release_resources(dev, sc->sc_res_spec,
575 agp_generic_detach(dev);
579 /* GCC1 is called MGGC on i915+ */
580 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
581 switch (gcc1 & AGP_I855_GCC1_GMS) {
582 case AGP_I855_GCC1_GMS_STOLEN_1M:
585 case AGP_I855_GCC1_GMS_STOLEN_4M:
588 case AGP_I855_GCC1_GMS_STOLEN_8M:
591 case AGP_I855_GCC1_GMS_STOLEN_16M:
594 case AGP_I855_GCC1_GMS_STOLEN_32M:
597 case AGP_I915_GCC1_GMS_STOLEN_48M:
598 if (sc->chiptype == CHIP_I915 ||
599 sc->chiptype == CHIP_I965 ||
600 sc->chiptype == CHIP_G33 ||
601 sc->chiptype == CHIP_IGD ||
602 sc->chiptype == CHIP_G4X) {
608 case AGP_I915_GCC1_GMS_STOLEN_64M:
609 if (sc->chiptype == CHIP_I915 ||
610 sc->chiptype == CHIP_I965 ||
611 sc->chiptype == CHIP_G33 ||
612 sc->chiptype == CHIP_IGD ||
613 sc->chiptype == CHIP_G4X) {
619 case AGP_G33_GCC1_GMS_STOLEN_128M:
620 if (sc->chiptype == CHIP_I965 ||
621 sc->chiptype == CHIP_G33 ||
622 sc->chiptype == CHIP_IGD ||
623 sc->chiptype == CHIP_G4X) {
629 case AGP_G33_GCC1_GMS_STOLEN_256M:
630 if (sc->chiptype == CHIP_I965 ||
631 sc->chiptype == CHIP_G33 ||
632 sc->chiptype == CHIP_IGD ||
633 sc->chiptype == CHIP_G4X) {
639 case AGP_G4X_GCC1_GMS_STOLEN_96M:
640 if (sc->chiptype == CHIP_I965 ||
641 sc->chiptype == CHIP_G4X) {
647 case AGP_G4X_GCC1_GMS_STOLEN_160M:
648 if (sc->chiptype == CHIP_I965 ||
649 sc->chiptype == CHIP_G4X) {
655 case AGP_G4X_GCC1_GMS_STOLEN_224M:
656 if (sc->chiptype == CHIP_I965 ||
657 sc->chiptype == CHIP_G4X) {
663 case AGP_G4X_GCC1_GMS_STOLEN_352M:
664 if (sc->chiptype == CHIP_I965 ||
665 sc->chiptype == CHIP_G4X) {
672 device_printf(dev, "unknown memory configuration, "
674 bus_release_resources(dev, sc->sc_res_spec,
677 agp_generic_detach(dev);
683 sc->stolen = (stolen - gtt_size) * 1024 / 4096;
685 device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
686 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
688 /* GATT address is already in there, make sure it's enabled */
689 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
691 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
693 gatt->ag_physical = pgtblctl & ~1;
697 agp_i810_dump_regs(dev);
703 agp_i810_detach(device_t dev)
705 struct agp_i810_softc *sc = device_get_softc(dev);
709 /* Clear the GATT base. */
710 if ( sc->chiptype == CHIP_I810 ) {
711 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 0);
713 unsigned int pgtblctl;
714 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
716 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
719 /* Put the aperture back the way it started. */
720 AGP_SET_APERTURE(dev, sc->initial_aperture);
722 if ( sc->chiptype == CHIP_I810 ) {
723 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
725 kfree(sc->gatt, M_AGP);
727 bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
734 agp_i810_resume(device_t dev)
736 struct agp_i810_softc *sc;
737 sc = device_get_softc(dev);
739 AGP_SET_APERTURE(dev, sc->initial_aperture);
741 /* Install the GATT. */
742 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
743 sc->gatt->ag_physical | 1);
745 return (bus_generic_resume(dev));
749 * Sets the PCI resource size of the aperture on i830-class and below chipsets,
750 * while returning failure on later chipsets when an actual change is
753 * This whole function is likely bogus, as the kernel would probably need to
754 * reconfigure the placement of the AGP aperture if a larger size is requested,
755 * which doesn't happen currently.
758 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
760 struct agp_i810_softc *sc = device_get_softc(dev);
761 u_int16_t miscc, gcc1;
763 switch (sc->chiptype) {
766 * Double check for sanity.
768 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
769 device_printf(dev, "bad aperture size %d\n", aperture);
773 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
774 miscc &= ~AGP_I810_MISCC_WINSIZE;
775 if (aperture == 32 * 1024 * 1024)
776 miscc |= AGP_I810_MISCC_WINSIZE_32;
778 miscc |= AGP_I810_MISCC_WINSIZE_64;
780 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
783 if (aperture != 64 * 1024 * 1024 &&
784 aperture != 128 * 1024 * 1024) {
785 device_printf(dev, "bad aperture size %d\n", aperture);
788 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
789 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
790 if (aperture == 64 * 1024 * 1024)
791 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
793 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
795 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
803 return agp_generic_set_aperture(dev, aperture);
810 * Writes a GTT entry mapping the page at the given offset from the beginning
811 * of the aperture to the given physical address.
814 agp_i810_write_gtt_entry(device_t dev, int offset, vm_offset_t physical,
817 struct agp_i810_softc *sc = device_get_softc(dev);
820 pte = (u_int32_t)physical | 1;
821 if (sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33 ||
822 sc->chiptype == CHIP_IGD || sc->chiptype == CHIP_G4X) {
823 pte |= (physical & 0x0000000f00000000ull) >> 28;
825 /* If we do actually have memory above 4GB on an older system,
826 * crash cleanly rather than scribble on system memory,
827 * so we know we need to fix it.
829 KASSERT((pte & 0x0000000f00000000ull) == 0,
830 (">4GB physical address in agp"));
833 switch (sc->chiptype) {
837 bus_write_4(sc->sc_res[0],
838 AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, pte);
843 bus_write_4(sc->sc_res[1],
844 (offset >> AGP_PAGE_SHIFT) * 4, pte);
847 bus_write_4(sc->sc_res[0],
848 (offset >> AGP_PAGE_SHIFT) * 4 + (512 * 1024), pte);
851 bus_write_4(sc->sc_res[0],
852 (offset >> AGP_PAGE_SHIFT) * 4 + (2 * 1024 * 1024), pte);
858 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
860 struct agp_i810_softc *sc = device_get_softc(dev);
862 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
863 device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
867 if ( sc->chiptype != CHIP_I810 ) {
868 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
869 device_printf(dev, "trying to bind into stolen memory");
874 agp_i810_write_gtt_entry(dev, offset, physical, 1);
880 agp_i810_unbind_page(device_t dev, int offset)
882 struct agp_i810_softc *sc = device_get_softc(dev);
884 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
887 if ( sc->chiptype != CHIP_I810 ) {
888 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
889 device_printf(dev, "trying to unbind from stolen memory");
894 agp_i810_write_gtt_entry(dev, offset, 0, 0);
900 * Writing via memory mapped registers already flushes all TLBs.
903 agp_i810_flush_tlb(device_t dev)
908 agp_i810_enable(device_t dev, u_int32_t mode)
914 static struct agp_memory *
915 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
917 struct agp_i810_softc *sc = device_get_softc(dev);
918 struct agp_memory *mem;
920 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
923 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
928 * Mapping local DRAM into GATT.
930 if ( sc->chiptype != CHIP_I810 )
932 if (size != sc->dcache_size)
934 } else if (type == 2) {
936 * Type 2 is the contiguous physical memory type, that hands
937 * back a physical address. This is used for cursors on i810.
938 * Hand back as many single pages with physical as the user
939 * wants, but only allow one larger allocation (ARGB cursor)
942 if (size != AGP_PAGE_SIZE) {
943 if (sc->argb_cursor != NULL)
946 /* Allocate memory for ARGB cursor, if we can. */
947 sc->argb_cursor = contigmalloc(size, M_AGP,
948 0, 0, ~0, PAGE_SIZE, 0);
949 if (sc->argb_cursor == NULL)
954 mem = kmalloc(sizeof *mem, M_AGP, M_INTWAIT);
955 mem->am_id = sc->agp.as_nextid++;
958 if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
959 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
960 atop(round_page(size)));
965 if (size == AGP_PAGE_SIZE) {
967 * Allocate and wire down the page now so that we can
968 * get its physical address.
972 m = vm_page_grab(mem->am_obj, 0,
973 VM_ALLOC_NORMAL|VM_ALLOC_ZERO|VM_ALLOC_RETRY);
974 if ((m->flags & PG_ZERO) == 0)
975 vm_page_zero_fill(m);
977 mem->am_physical = VM_PAGE_TO_PHYS(m);
980 /* Our allocation is already nicely wired down for us.
981 * Just grab the physical address.
983 mem->am_physical = vtophys(sc->argb_cursor);
986 mem->am_physical = 0;
990 mem->am_is_bound = 0;
991 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
992 sc->agp.as_allocated += size;
998 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
1000 struct agp_i810_softc *sc = device_get_softc(dev);
1002 if (mem->am_is_bound)
1005 if (mem->am_type == 2) {
1006 if (mem->am_size == AGP_PAGE_SIZE) {
1008 * Unwire the page which we wired in alloc_memory.
1011 lwkt_gettoken(&vm_token);
1012 m = vm_page_lookup(mem->am_obj, 0);
1013 vm_page_unwire(m, 0);
1014 lwkt_reltoken(&vm_token);
1016 contigfree(sc->argb_cursor, mem->am_size, M_AGP);
1017 sc->argb_cursor = NULL;
1021 sc->agp.as_allocated -= mem->am_size;
1022 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
1024 vm_object_deallocate(mem->am_obj);
1030 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
1033 struct agp_i810_softc *sc = device_get_softc(dev);
1036 /* Do some sanity checks first. */
1037 if (offset < 0 || (offset & (AGP_PAGE_SIZE - 1)) != 0 ||
1038 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
1039 device_printf(dev, "binding memory at bad offset %#x\n",
1044 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1045 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1046 if (mem->am_is_bound) {
1047 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1050 /* The memory's already wired down, just stick it in the GTT. */
1051 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1052 agp_i810_write_gtt_entry(dev, offset + i,
1053 mem->am_physical + i, 1);
1056 mem->am_offset = offset;
1057 mem->am_is_bound = 1;
1058 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1062 if (mem->am_type != 1)
1063 return agp_generic_bind_memory(dev, mem, offset);
1065 if ( sc->chiptype != CHIP_I810 )
1068 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1069 bus_write_4(sc->sc_res[0],
1070 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
1077 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
1079 struct agp_i810_softc *sc = device_get_softc(dev);
1082 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1083 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1084 if (!mem->am_is_bound) {
1085 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1089 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1090 agp_i810_write_gtt_entry(dev, mem->am_offset + i,
1094 mem->am_is_bound = 0;
1095 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1099 if (mem->am_type != 1)
1100 return agp_generic_unbind_memory(dev, mem);
1102 if ( sc->chiptype != CHIP_I810 )
1105 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1106 bus_write_4(sc->sc_res[0],
1107 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
1113 static device_method_t agp_i810_methods[] = {
1114 /* Device interface */
1115 DEVMETHOD(device_identify, agp_i810_identify),
1116 DEVMETHOD(device_probe, agp_i810_probe),
1117 DEVMETHOD(device_attach, agp_i810_attach),
1118 DEVMETHOD(device_detach, agp_i810_detach),
1119 DEVMETHOD(device_suspend, bus_generic_suspend),
1120 DEVMETHOD(device_resume, agp_i810_resume),
1123 DEVMETHOD(agp_get_aperture, agp_generic_get_aperture),
1124 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
1125 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
1126 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
1127 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
1128 DEVMETHOD(agp_enable, agp_i810_enable),
1129 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
1130 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
1131 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
1132 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
1137 static driver_t agp_i810_driver = {
1140 sizeof(struct agp_i810_softc),
1143 static devclass_t agp_devclass;
1145 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, NULL, NULL);
1146 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1147 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);