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
29 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
30 * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <dev/agp/agppriv.h>
49 #include <dev/agp/agpreg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pageout.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
61 #include <machine/md_var.h>
64 MALLOC_DECLARE(M_AGP);
67 CHIP_I810, /* i810/i815 */
68 CHIP_I830, /* 830M/845G */
69 CHIP_I855, /* 852GM/855GM/865G */
70 CHIP_I915, /* 915G/915GM */
72 CHIP_G33, /* G33/Q33/Q35 */
73 CHIP_IGD, /* Pineview */
74 CHIP_G4X, /* G45/Q45 */
77 /* The i810 through i855 have the registers at BAR 1, and the GATT gets
78 * allocated by us. The i915 has registers in BAR 0 and the GATT is at the
79 * start of the stolen memory, and should only be accessed by the OS through
80 * BAR 3. The G965 has registers and GATT in the same BAR (0) -- first 512KB
81 * is registers, second 512KB is GATT.
83 static struct resource_spec agp_i810_res_spec[] = {
84 { SYS_RES_MEMORY, AGP_I810_MMADR, RF_ACTIVE | RF_SHAREABLE },
88 static struct resource_spec agp_i915_res_spec[] = {
89 { SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
90 { SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
94 static struct resource_spec agp_i965_res_spec[] = {
95 { SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
99 struct agp_i810_softc {
100 struct agp_softc agp;
101 u_int32_t initial_aperture; /* aperture size at startup */
102 struct agp_gatt *gatt;
103 int chiptype; /* i810-like or i830 */
104 u_int32_t dcache_size; /* i810 only */
105 u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */
106 device_t bdev; /* bridge device */
108 void *argb_cursor; /* contigmalloc area for ARGB cursor */
110 struct resource_spec * sc_res_spec;
111 struct resource *sc_res[2];
114 /* For adding new devices, devid is the id of the graphics controller
115 * (pci:0:2:0, for example). The placeholder (usually at pci:0:2:1) for the
116 * second head should never be added. The bridge_offset is the offset to
117 * subtract from devid to get the id of the hostb that the device is on.
119 static const struct agp_i810_match {
124 } agp_i810_matches[] = {
125 {0x71218086, CHIP_I810, 0x00010000,
126 "Intel 82810 (i810 GMCH) SVGA controller"},
127 {0x71238086, CHIP_I810, 0x00010000,
128 "Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller"},
129 {0x71258086, CHIP_I810, 0x00010000,
130 "Intel 82810E (i810E GMCH) SVGA controller"},
131 {0x11328086, CHIP_I810, 0x00020000,
132 "Intel 82815 (i815 GMCH) SVGA controller"},
133 {0x35778086, CHIP_I830, 0x00020000,
134 "Intel 82830M (830M GMCH) SVGA controller"},
135 {0x25628086, CHIP_I830, 0x00020000,
136 "Intel 82845M (845M GMCH) SVGA controller"},
137 {0x35828086, CHIP_I855, 0x00020000,
138 "Intel 82852/855GM SVGA controller"},
139 {0x25728086, CHIP_I855, 0x00020000,
140 "Intel 82865G (865G GMCH) SVGA controller"},
141 {0x25828086, CHIP_I915, 0x00020000,
142 "Intel 82915G (915G GMCH) SVGA controller"},
143 {0x258A8086, CHIP_I915, 0x00020000,
144 "Intel E7221 SVGA controller"},
145 {0x25928086, CHIP_I915, 0x00020000,
146 "Intel 82915GM (915GM GMCH) SVGA controller"},
147 {0x27728086, CHIP_I915, 0x00020000,
148 "Intel 82945G (945G GMCH) SVGA controller"},
149 {0x27A28086, CHIP_I915, 0x00020000,
150 "Intel 82945GM (945GM GMCH) SVGA controller"},
151 {0x27AE8086, CHIP_I915, 0x00020000,
152 "Intel 945GME SVGA controller"},
153 {0x29728086, CHIP_I965, 0x00020000,
154 "Intel 946GZ SVGA controller"},
155 {0x29828086, CHIP_I965, 0x00020000,
156 "Intel G965 SVGA controller"},
157 {0x29928086, CHIP_I965, 0x00020000,
158 "Intel Q965 SVGA controller"},
159 {0x29A28086, CHIP_I965, 0x00020000,
160 "Intel G965 SVGA controller"},
161 {0x29B28086, CHIP_G33, 0x00020000,
162 "Intel Q35 SVGA controller"},
163 {0x29C28086, CHIP_G33, 0x00020000,
164 "Intel G33 SVGA controller"},
165 {0x29D28086, CHIP_G33, 0x00020000,
166 "Intel Q33 SVGA controller"},
167 {0xA0018086, CHIP_IGD, 0x00010000,
168 "Intel Pineview SVGA controller"},
169 {0xA0118086, CHIP_IGD, 0x00010000,
170 "Intel Pineview (M) SVGA controller"},
171 {0x2A028086, CHIP_I965, 0x00020000,
172 "Intel GM965 SVGA controller"},
173 {0x2A128086, CHIP_I965, 0x00020000,
174 "Intel GME965 SVGA controller"},
175 {0x2A428086, CHIP_G4X, 0x00020000,
176 "Intel GM45 SVGA controller"},
177 {0x2E028086, CHIP_G4X, 0x00020000,
178 "Intel Eaglelake SVGA controller"},
179 {0x2E128086, CHIP_G4X, 0x00020000,
180 "Intel Q45 SVGA controller"},
181 {0x2E228086, CHIP_G4X, 0x00020000,
182 "Intel G45 SVGA controller"},
183 {0x2E328086, CHIP_G4X, 0x00020000,
184 "Intel G41 SVGA controller"},
185 {0x00428086, CHIP_G4X, 0x00020000,
186 "Intel Ironlake (D) SVGA controller"},
187 {0x00468086, CHIP_G4X, 0x00020000,
188 "Intel Ironlake (M) SVGA controller"},
192 static const struct agp_i810_match*
193 agp_i810_match(device_t dev)
197 if (pci_get_class(dev) != PCIC_DISPLAY
198 || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
201 devid = pci_get_devid(dev);
202 for (i = 0; agp_i810_matches[i].devid != 0; i++) {
203 if (agp_i810_matches[i].devid == devid)
206 if (agp_i810_matches[i].devid == 0)
209 return &agp_i810_matches[i];
213 * Find bridge device.
216 agp_i810_find_bridge(device_t dev)
218 device_t *children, child;
221 const struct agp_i810_match *match;
223 match = agp_i810_match(dev);
224 devid = match->devid - match->bridge_offset;
226 if (device_get_children(device_get_parent(device_get_parent(dev)),
227 &children, &nchildren))
230 for (i = 0; i < nchildren; i++) {
233 if (pci_get_devid(child) == devid) {
234 free(children, M_TEMP);
238 free(children, M_TEMP);
243 agp_i810_identify(driver_t *driver, device_t parent)
246 if (device_find_child(parent, "agp", -1) == NULL &&
247 agp_i810_match(parent))
248 device_add_child(parent, "agp", -1);
252 agp_i810_probe(device_t dev)
255 const struct agp_i810_match *match;
259 if (resource_disabled("agp", device_get_unit(dev)))
261 match = agp_i810_match(dev);
265 bdev = agp_i810_find_bridge(dev);
268 printf("I810: can't find bridge device\n");
273 * checking whether internal graphics device has been activated.
275 switch (match->chiptype) {
277 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
278 if ((smram & AGP_I810_SMRAM_GMS) ==
279 AGP_I810_SMRAM_GMS_DISABLED) {
281 printf("I810: disabled, not probing\n");
287 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
288 if ((gcc1 & AGP_I830_GCC1_DEV2) ==
289 AGP_I830_GCC1_DEV2_DISABLED) {
291 printf("I830: disabled, not probing\n");
300 deven = pci_read_config(bdev, AGP_I915_DEVEN, 4);
301 if ((deven & AGP_I915_DEVEN_D2F0) ==
302 AGP_I915_DEVEN_D2F0_DISABLED) {
304 printf("I915: disabled, not probing\n");
310 if (match->devid == 0x35828086) {
311 switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
314 "Intel 82855GME (855GME GMCH) SVGA controller");
318 "Intel 82855GM (855GM GMCH) SVGA controller");
322 "Intel 82852GME (852GME GMCH) SVGA controller");
326 "Intel 82852GM (852GM GMCH) SVGA controller");
330 "Intel 8285xM (85xGM GMCH) SVGA controller");
334 device_set_desc(dev, match->name);
337 return BUS_PROBE_DEFAULT;
341 agp_i810_dump_regs(device_t dev)
343 struct agp_i810_softc *sc = device_get_softc(dev);
345 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
346 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
348 switch (sc->chiptype) {
350 device_printf(dev, "AGP_I810_MISCC: 0x%04x\n",
351 pci_read_config(sc->bdev, AGP_I810_MISCC, 2));
354 device_printf(dev, "AGP_I830_GCC1: 0x%02x\n",
355 pci_read_config(sc->bdev, AGP_I830_GCC1, 1));
358 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
359 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
366 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
367 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
368 device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
369 pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
372 device_printf(dev, "Aperture resource size: %d bytes\n",
373 AGP_GET_APERTURE(dev));
377 agp_i810_attach(device_t dev)
379 struct agp_i810_softc *sc = device_get_softc(dev);
380 struct agp_gatt *gatt;
381 const struct agp_i810_match *match;
384 sc->bdev = agp_i810_find_bridge(dev);
388 match = agp_i810_match(dev);
389 sc->chiptype = match->chiptype;
391 switch (sc->chiptype) {
395 sc->sc_res_spec = agp_i810_res_spec;
396 agp_set_aperture_resource(dev, AGP_APBASE);
401 sc->sc_res_spec = agp_i915_res_spec;
402 agp_set_aperture_resource(dev, AGP_I915_GMADR);
406 sc->sc_res_spec = agp_i965_res_spec;
407 agp_set_aperture_resource(dev, AGP_I915_GMADR);
411 error = agp_generic_attach(dev);
415 if (sc->chiptype != CHIP_I965 && sc->chiptype != CHIP_G33 &&
416 sc->chiptype != CHIP_IGD && sc->chiptype != CHIP_G4X &&
417 ptoa((vm_paddr_t)Maxmem) > 0xfffffffful)
419 device_printf(dev, "agp_i810.c does not support physical "
420 "memory above 4GB.\n");
424 if (bus_alloc_resources(dev, sc->sc_res_spec, sc->sc_res)) {
425 agp_generic_detach(dev);
429 sc->initial_aperture = AGP_GET_APERTURE(dev);
431 gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
433 bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
434 agp_generic_detach(dev);
439 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
441 if ( sc->chiptype == CHIP_I810 ) {
442 /* Some i810s have on-chip memory called dcache */
443 if (bus_read_1(sc->sc_res[0], AGP_I810_DRT) &
444 AGP_I810_DRT_POPULATED)
445 sc->dcache_size = 4 * 1024 * 1024;
449 /* According to the specs the gatt on the i810 must be 64k */
450 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
451 0, ~0, PAGE_SIZE, 0);
452 if (!gatt->ag_virtual) {
454 device_printf(dev, "contiguous allocation failed\n");
455 bus_release_resources(dev, sc->sc_res_spec,
458 agp_generic_detach(dev);
461 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
463 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
465 /* Install the GATT. */
466 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
467 gatt->ag_physical | 1);
468 } else if ( sc->chiptype == CHIP_I830 ) {
469 /* The i830 automatically initializes the 128k gatt on boot. */
470 unsigned int gcc1, pgtblctl;
472 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
473 switch (gcc1 & AGP_I830_GCC1_GMS) {
474 case AGP_I830_GCC1_GMS_STOLEN_512:
475 sc->stolen = (512 - 132) * 1024 / 4096;
477 case AGP_I830_GCC1_GMS_STOLEN_1024:
478 sc->stolen = (1024 - 132) * 1024 / 4096;
480 case AGP_I830_GCC1_GMS_STOLEN_8192:
481 sc->stolen = (8192 - 132) * 1024 / 4096;
485 device_printf(dev, "unknown memory configuration, disabling\n");
486 bus_release_resources(dev, sc->sc_res_spec,
489 agp_generic_detach(dev);
493 /* GATT address is already in there, make sure it's enabled */
494 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
496 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
498 gatt->ag_physical = pgtblctl & ~1;
499 } else if (sc->chiptype == CHIP_I855 || sc->chiptype == CHIP_I915 ||
500 sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33 ||
501 sc->chiptype == CHIP_IGD || sc->chiptype == CHIP_G4X) {
502 unsigned int gcc1, pgtblctl, stolen, gtt_size;
504 /* Stolen memory is set up at the beginning of the aperture by
505 * the BIOS, consisting of the GATT followed by 4kb for the
508 switch (sc->chiptype) {
516 switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
517 AGP_I810_PGTBL_SIZE_MASK) {
518 case AGP_I810_PGTBL_SIZE_128KB:
521 case AGP_I810_PGTBL_SIZE_256KB:
524 case AGP_I810_PGTBL_SIZE_512KB:
527 case AGP_I965_PGTBL_SIZE_1MB:
530 case AGP_I965_PGTBL_SIZE_2MB:
533 case AGP_I965_PGTBL_SIZE_1_5MB:
534 gtt_size = 1024 + 512;
537 device_printf(dev, "Bad PGTBL size\n");
538 bus_release_resources(dev, sc->sc_res_spec,
541 agp_generic_detach(dev);
546 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2);
547 switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) {
548 case AGP_G33_MGGC_GGMS_SIZE_1M:
551 case AGP_G33_MGGC_GGMS_SIZE_2M:
555 device_printf(dev, "Bad PGTBL size\n");
556 bus_release_resources(dev, sc->sc_res_spec,
559 agp_generic_detach(dev);
568 device_printf(dev, "Bad chiptype\n");
569 bus_release_resources(dev, sc->sc_res_spec,
572 agp_generic_detach(dev);
576 /* GCC1 is called MGGC on i915+ */
577 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
578 switch (gcc1 & AGP_I855_GCC1_GMS) {
579 case AGP_I855_GCC1_GMS_STOLEN_1M:
582 case AGP_I855_GCC1_GMS_STOLEN_4M:
585 case AGP_I855_GCC1_GMS_STOLEN_8M:
588 case AGP_I855_GCC1_GMS_STOLEN_16M:
591 case AGP_I855_GCC1_GMS_STOLEN_32M:
594 case AGP_I915_GCC1_GMS_STOLEN_48M:
595 if (sc->chiptype == CHIP_I915 ||
596 sc->chiptype == CHIP_I965 ||
597 sc->chiptype == CHIP_G33 ||
598 sc->chiptype == CHIP_IGD ||
599 sc->chiptype == CHIP_G4X) {
605 case AGP_I915_GCC1_GMS_STOLEN_64M:
606 if (sc->chiptype == CHIP_I915 ||
607 sc->chiptype == CHIP_I965 ||
608 sc->chiptype == CHIP_G33 ||
609 sc->chiptype == CHIP_IGD ||
610 sc->chiptype == CHIP_G4X) {
616 case AGP_G33_GCC1_GMS_STOLEN_128M:
617 if (sc->chiptype == CHIP_I965 ||
618 sc->chiptype == CHIP_G33 ||
619 sc->chiptype == CHIP_IGD ||
620 sc->chiptype == CHIP_G4X) {
626 case AGP_G33_GCC1_GMS_STOLEN_256M:
627 if (sc->chiptype == CHIP_I965 ||
628 sc->chiptype == CHIP_G33 ||
629 sc->chiptype == CHIP_IGD ||
630 sc->chiptype == CHIP_G4X) {
636 case AGP_G4X_GCC1_GMS_STOLEN_96M:
637 if (sc->chiptype == CHIP_I965 ||
638 sc->chiptype == CHIP_G4X) {
644 case AGP_G4X_GCC1_GMS_STOLEN_160M:
645 if (sc->chiptype == CHIP_I965 ||
646 sc->chiptype == CHIP_G4X) {
652 case AGP_G4X_GCC1_GMS_STOLEN_224M:
653 if (sc->chiptype == CHIP_I965 ||
654 sc->chiptype == CHIP_G4X) {
660 case AGP_G4X_GCC1_GMS_STOLEN_352M:
661 if (sc->chiptype == CHIP_I965 ||
662 sc->chiptype == CHIP_G4X) {
669 device_printf(dev, "unknown memory configuration, "
671 bus_release_resources(dev, sc->sc_res_spec,
674 agp_generic_detach(dev);
680 sc->stolen = (stolen - gtt_size) * 1024 / 4096;
682 /* GATT address is already in there, make sure it's enabled */
683 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
685 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
687 gatt->ag_physical = pgtblctl & ~1;
690 device_printf(dev, "aperture size is %dM",
691 sc->initial_aperture / 1024 / 1024);
693 printf(", detected %dk stolen memory\n", sc->stolen * 4);
698 agp_i810_dump_regs(dev);
704 agp_i810_detach(device_t dev)
706 struct agp_i810_softc *sc = device_get_softc(dev);
710 /* Clear the GATT base. */
711 if ( sc->chiptype == CHIP_I810 ) {
712 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 0);
714 unsigned int pgtblctl;
715 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
717 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
720 /* Put the aperture back the way it started. */
721 AGP_SET_APERTURE(dev, sc->initial_aperture);
723 if ( sc->chiptype == CHIP_I810 ) {
724 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
726 free(sc->gatt, M_AGP);
728 bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
735 agp_i810_resume(device_t dev)
737 struct agp_i810_softc *sc;
738 sc = device_get_softc(dev);
740 AGP_SET_APERTURE(dev, sc->initial_aperture);
742 /* Install the GATT. */
743 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
744 sc->gatt->ag_physical | 1);
746 return (bus_generic_resume(dev));
750 * Sets the PCI resource size of the aperture on i830-class and below chipsets,
751 * while returning failure on later chipsets when an actual change is
754 * This whole function is likely bogus, as the kernel would probably need to
755 * reconfigure the placement of the AGP aperture if a larger size is requested,
756 * which doesn't happen currently.
759 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
761 struct agp_i810_softc *sc = device_get_softc(dev);
762 u_int16_t miscc, gcc1;
764 switch (sc->chiptype) {
767 * Double check for sanity.
769 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
770 device_printf(dev, "bad aperture size %d\n", aperture);
774 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
775 miscc &= ~AGP_I810_MISCC_WINSIZE;
776 if (aperture == 32 * 1024 * 1024)
777 miscc |= AGP_I810_MISCC_WINSIZE_32;
779 miscc |= AGP_I810_MISCC_WINSIZE_64;
781 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
784 if (aperture != 64 * 1024 * 1024 &&
785 aperture != 128 * 1024 * 1024) {
786 device_printf(dev, "bad aperture size %d\n", aperture);
789 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
790 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
791 if (aperture == 64 * 1024 * 1024)
792 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
794 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
796 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
804 return agp_generic_set_aperture(dev, aperture);
811 * Writes a GTT entry mapping the page at the given offset from the beginning
812 * of the aperture to the given physical address.
815 agp_i810_write_gtt_entry(device_t dev, int offset, vm_offset_t physical,
818 struct agp_i810_softc *sc = device_get_softc(dev);
821 pte = (u_int32_t)physical | 1;
822 if (sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33 ||
823 sc->chiptype == CHIP_IGD || sc->chiptype == CHIP_G4X) {
824 pte |= (physical & 0x0000000f00000000ull) >> 28;
826 /* If we do actually have memory above 4GB on an older system,
827 * crash cleanly rather than scribble on system memory,
828 * so we know we need to fix it.
830 KASSERT((pte & 0x0000000f00000000ull) == 0,
831 (">4GB physical address in agp"));
834 switch (sc->chiptype) {
838 bus_write_4(sc->sc_res[0],
839 AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, pte);
844 bus_write_4(sc->sc_res[1],
845 (offset >> AGP_PAGE_SHIFT) * 4, pte);
848 bus_write_4(sc->sc_res[0],
849 (offset >> AGP_PAGE_SHIFT) * 4 + (512 * 1024), pte);
852 bus_write_4(sc->sc_res[0],
853 (offset >> AGP_PAGE_SHIFT) * 4 + (2 * 1024 * 1024), pte);
859 agp_i810_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
861 struct agp_i810_softc *sc = device_get_softc(dev);
863 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
864 device_printf(dev, "failed: offset is 0x%08jx, shift is %d, entries is %d\n", (intmax_t)offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
868 if ( sc->chiptype != CHIP_I810 ) {
869 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
870 device_printf(dev, "trying to bind into stolen memory");
875 agp_i810_write_gtt_entry(dev, offset, physical, 1);
881 agp_i810_unbind_page(device_t dev, vm_offset_t offset)
883 struct agp_i810_softc *sc = device_get_softc(dev);
885 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
888 if ( sc->chiptype != CHIP_I810 ) {
889 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
890 device_printf(dev, "trying to unbind from stolen memory");
895 agp_i810_write_gtt_entry(dev, offset, 0, 0);
901 * Writing via memory mapped registers already flushes all TLBs.
904 agp_i810_flush_tlb(device_t dev)
909 agp_i810_enable(device_t dev, u_int32_t mode)
915 static struct agp_memory *
916 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
918 struct agp_i810_softc *sc = device_get_softc(dev);
919 struct agp_memory *mem;
921 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
924 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
929 * Mapping local DRAM into GATT.
931 if ( sc->chiptype != CHIP_I810 )
933 if (size != sc->dcache_size)
935 } else if (type == 2) {
937 * Type 2 is the contiguous physical memory type, that hands
938 * back a physical address. This is used for cursors on i810.
939 * Hand back as many single pages with physical as the user
940 * wants, but only allow one larger allocation (ARGB cursor)
943 if (size != AGP_PAGE_SIZE) {
944 if (sc->argb_cursor != NULL)
947 /* Allocate memory for ARGB cursor, if we can. */
948 sc->argb_cursor = contigmalloc(size, M_AGP,
949 0, 0, ~0, PAGE_SIZE, 0);
950 if (sc->argb_cursor == NULL)
955 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
956 mem->am_id = sc->agp.as_nextid++;
959 if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
960 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
961 atop(round_page(size)));
966 if (size == AGP_PAGE_SIZE) {
968 * Allocate and wire down the page now so that we can
969 * get its physical address.
973 VM_OBJECT_LOCK(mem->am_obj);
974 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY |
975 VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
976 VM_OBJECT_UNLOCK(mem->am_obj);
977 mem->am_physical = VM_PAGE_TO_PHYS(m);
979 /* Our allocation is already nicely wired down for us.
980 * Just grab the physical address.
982 mem->am_physical = vtophys(sc->argb_cursor);
985 mem->am_physical = 0;
989 mem->am_is_bound = 0;
990 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
991 sc->agp.as_allocated += size;
997 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
999 struct agp_i810_softc *sc = device_get_softc(dev);
1001 if (mem->am_is_bound)
1004 if (mem->am_type == 2) {
1005 if (mem->am_size == AGP_PAGE_SIZE) {
1007 * Unwire the page which we wired in alloc_memory.
1011 VM_OBJECT_LOCK(mem->am_obj);
1012 m = vm_page_lookup(mem->am_obj, 0);
1014 vm_page_unwire(m, 0);
1016 VM_OBJECT_UNLOCK(mem->am_obj);
1018 contigfree(sc->argb_cursor, mem->am_size, M_AGP);
1019 sc->argb_cursor = NULL;
1023 sc->agp.as_allocated -= mem->am_size;
1024 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
1026 vm_object_deallocate(mem->am_obj);
1032 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
1035 struct agp_i810_softc *sc = device_get_softc(dev);
1038 /* Do some sanity checks first. */
1039 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
1040 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
1041 device_printf(dev, "binding memory at bad offset %#x\n",
1046 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1047 mtx_lock(&sc->agp.as_lock);
1048 if (mem->am_is_bound) {
1049 mtx_unlock(&sc->agp.as_lock);
1052 /* The memory's already wired down, just stick it in the GTT. */
1053 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1054 agp_i810_write_gtt_entry(dev, offset + i,
1055 mem->am_physical + i, 1);
1058 mem->am_offset = offset;
1059 mem->am_is_bound = 1;
1060 mtx_unlock(&sc->agp.as_lock);
1064 if (mem->am_type != 1)
1065 return agp_generic_bind_memory(dev, mem, offset);
1067 if ( sc->chiptype != CHIP_I810 )
1070 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1071 bus_write_4(sc->sc_res[0],
1072 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
1079 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
1081 struct agp_i810_softc *sc = device_get_softc(dev);
1084 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1085 mtx_lock(&sc->agp.as_lock);
1086 if (!mem->am_is_bound) {
1087 mtx_unlock(&sc->agp.as_lock);
1091 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1092 agp_i810_write_gtt_entry(dev, mem->am_offset + i,
1096 mem->am_is_bound = 0;
1097 mtx_unlock(&sc->agp.as_lock);
1101 if (mem->am_type != 1)
1102 return agp_generic_unbind_memory(dev, mem);
1104 if ( sc->chiptype != CHIP_I810 )
1107 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1108 bus_write_4(sc->sc_res[0],
1109 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
1115 static device_method_t agp_i810_methods[] = {
1116 /* Device interface */
1117 DEVMETHOD(device_identify, agp_i810_identify),
1118 DEVMETHOD(device_probe, agp_i810_probe),
1119 DEVMETHOD(device_attach, agp_i810_attach),
1120 DEVMETHOD(device_detach, agp_i810_detach),
1121 DEVMETHOD(device_suspend, bus_generic_suspend),
1122 DEVMETHOD(device_resume, agp_i810_resume),
1125 DEVMETHOD(agp_get_aperture, agp_generic_get_aperture),
1126 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
1127 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
1128 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
1129 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
1130 DEVMETHOD(agp_enable, agp_i810_enable),
1131 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
1132 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
1133 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
1134 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
1139 static driver_t agp_i810_driver = {
1142 sizeof(struct agp_i810_softc),
1145 static devclass_t agp_devclass;
1147 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, 0, 0);
1148 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1149 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);