2 * Copyright (c) 2000 Doug Rabson
3 * Copyright (c) 2000 Ruslan Ermilov
4 * Copyright (c) 2011 The FreeBSD Foundation
5 * Copyright (c) 2017-2019 François Tigeot <ftigeot@wolfpond.org>
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
35 * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
37 * This is generic Intel GTT handling code, morphed from the AGP
42 #define KTR_AGP_I810 KTR_DEV
44 #define KTR_AGP_I810 0
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
56 #include <bus/pci/pcivar.h>
57 #include <bus/pci/pcireg.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_pageout.h>
68 #include <vm/vm_page2.h>
70 #include <machine/md_var.h>
72 #include <linux/slab.h>
73 #include <linux/scatterlist.h>
74 #include <drm/intel-gtt.h>
76 struct agp_i810_match;
78 static int agp_i915_check_active(device_t bridge_dev);
80 static void agp_i810_set_desc(device_t dev, const struct agp_i810_match *match);
82 static void agp_i915_dump_regs(device_t dev);
83 static void agp_i965_dump_regs(device_t dev);
85 static int agp_i915_get_stolen_size(device_t dev);
87 static int agp_i915_get_gtt_mappable_entries(device_t dev);
89 static int agp_i810_get_gtt_total_entries(device_t dev);
90 static int agp_i965_get_gtt_total_entries(device_t dev);
91 static int agp_gen5_get_gtt_total_entries(device_t dev);
93 static int agp_i830_install_gatt(device_t dev);
95 static void agp_i830_deinstall_gatt(device_t dev);
97 static void agp_i915_install_gtt_pte(device_t dev, u_int index,
98 vm_offset_t physical, int flags);
99 static void agp_i965_install_gtt_pte(device_t dev, u_int index,
100 vm_offset_t physical, int flags);
101 static void agp_g4x_install_gtt_pte(device_t dev, u_int index,
102 vm_offset_t physical, int flags);
104 static void agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte);
105 static void agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte);
106 static void agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte);
108 static void agp_i915_sync_gtt_pte(device_t dev, u_int index);
109 static void agp_i965_sync_gtt_pte(device_t dev, u_int index);
110 static void agp_g4x_sync_gtt_pte(device_t dev, u_int index);
112 static int agp_i915_set_aperture(device_t dev, u_int32_t aperture);
114 static int agp_i915_chipset_flush_setup(device_t dev);
115 static int agp_i965_chipset_flush_setup(device_t dev);
117 static void agp_i915_chipset_flush_teardown(device_t dev);
118 static void agp_i965_chipset_flush_teardown(device_t dev);
120 static void agp_i915_chipset_flush(device_t dev);
123 CHIP_I810, /* i810/i815 */
124 CHIP_I830, /* 830M/845G */
125 CHIP_I855, /* 852GM/855GM/865G */
126 CHIP_I915, /* 915G/915GM */
127 CHIP_I965, /* G965 */
128 CHIP_G33, /* G33/Q33/Q35 */
129 CHIP_IGD, /* Pineview */
130 CHIP_G4X, /* G45/Q45 */
133 /* The i810 through i855 have the registers at BAR 1, and the GATT gets
134 * allocated by us. The i915 has registers in BAR 0 and the GATT is at the
135 * start of the stolen memory, and should only be accessed by the OS through
136 * BAR 3. The G965 has registers and GATT in the same BAR (0) -- first 512KB
137 * is registers, second 512KB is GATT.
139 static struct resource_spec agp_i915_res_spec[] = {
140 { SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
141 { SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
145 static struct resource_spec agp_i965_res_spec[] = {
146 { SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
150 struct agp_i810_softc {
151 struct agp_softc agp;
152 u_int32_t initial_aperture; /* aperture size at startup */
153 struct agp_gatt *gatt;
154 u_int32_t dcache_size; /* i810 only */
155 u_int32_t stolen; /* number of i830/845 gtt
156 entries for stolen memory */
157 u_int stolen_size; /* BIOS-reserved graphics memory */
158 u_int gtt_total_entries; /* Total number of gtt ptes */
159 u_int gtt_mappable_entries; /* Number of gtt ptes mappable by CPU */
160 device_t bdev; /* bridge device */
161 void *argb_cursor; /* contigmalloc area for ARGB cursor */
162 struct resource *sc_res[3];
163 const struct agp_i810_match *match;
164 int sc_flush_page_rid;
165 struct resource *sc_flush_page_res;
166 void *sc_flush_page_vaddr;
167 int sc_bios_allocated_flush_page;
170 static device_t intel_agp;
172 struct agp_i810_driver {
175 int busdma_addr_mask_sz;
176 struct resource_spec *res_spec;
177 int (*check_active)(device_t);
178 void (*set_desc)(device_t, const struct agp_i810_match *);
179 void (*dump_regs)(device_t);
180 int (*get_stolen_size)(device_t);
181 int (*get_gtt_total_entries)(device_t);
182 int (*get_gtt_mappable_entries)(device_t);
183 int (*install_gatt)(device_t);
184 void (*deinstall_gatt)(device_t);
185 void (*write_gtt)(device_t, u_int, uint32_t);
186 void (*install_gtt_pte)(device_t, u_int, vm_offset_t, int);
187 void (*sync_gtt_pte)(device_t, u_int);
188 int (*set_aperture)(device_t, u_int32_t);
189 int (*chipset_flush_setup)(device_t);
190 void (*chipset_flush_teardown)(device_t);
191 void (*chipset_flush)(device_t);
195 struct intel_gtt base;
198 static const struct agp_i810_driver agp_i810_i915_driver = {
199 .chiptype = CHIP_I915,
201 .busdma_addr_mask_sz = 32,
202 .res_spec = agp_i915_res_spec,
203 .check_active = agp_i915_check_active,
204 .set_desc = agp_i810_set_desc,
205 .dump_regs = agp_i915_dump_regs,
206 .get_stolen_size = agp_i915_get_stolen_size,
207 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
208 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
209 .install_gatt = agp_i830_install_gatt,
210 .deinstall_gatt = agp_i830_deinstall_gatt,
211 .write_gtt = agp_i915_write_gtt,
212 .install_gtt_pte = agp_i915_install_gtt_pte,
213 .sync_gtt_pte = agp_i915_sync_gtt_pte,
214 .set_aperture = agp_i915_set_aperture,
215 .chipset_flush_setup = agp_i915_chipset_flush_setup,
216 .chipset_flush_teardown = agp_i915_chipset_flush_teardown,
217 .chipset_flush = agp_i915_chipset_flush,
220 static const struct agp_i810_driver agp_i810_g965_driver = {
221 .chiptype = CHIP_I965,
223 .busdma_addr_mask_sz = 36,
224 .res_spec = agp_i965_res_spec,
225 .check_active = agp_i915_check_active,
226 .set_desc = agp_i810_set_desc,
227 .dump_regs = agp_i965_dump_regs,
228 .get_stolen_size = agp_i915_get_stolen_size,
229 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
230 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
231 .install_gatt = agp_i830_install_gatt,
232 .deinstall_gatt = agp_i830_deinstall_gatt,
233 .write_gtt = agp_i965_write_gtt,
234 .install_gtt_pte = agp_i965_install_gtt_pte,
235 .sync_gtt_pte = agp_i965_sync_gtt_pte,
236 .set_aperture = agp_i915_set_aperture,
237 .chipset_flush_setup = agp_i965_chipset_flush_setup,
238 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
239 .chipset_flush = agp_i915_chipset_flush,
242 static const struct agp_i810_driver agp_i810_g33_driver = {
243 .chiptype = CHIP_G33,
245 .busdma_addr_mask_sz = 36,
246 .res_spec = agp_i915_res_spec,
247 .check_active = agp_i915_check_active,
248 .set_desc = agp_i810_set_desc,
249 .dump_regs = agp_i965_dump_regs,
250 .get_stolen_size = agp_i915_get_stolen_size,
251 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
252 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
253 .install_gatt = agp_i830_install_gatt,
254 .deinstall_gatt = agp_i830_deinstall_gatt,
255 .write_gtt = agp_i915_write_gtt,
256 .install_gtt_pte = agp_i965_install_gtt_pte,
257 .sync_gtt_pte = agp_i915_sync_gtt_pte,
258 .set_aperture = agp_i915_set_aperture,
259 .chipset_flush_setup = agp_i965_chipset_flush_setup,
260 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
261 .chipset_flush = agp_i915_chipset_flush,
264 static const struct agp_i810_driver pineview_gtt_driver = {
265 .chiptype = CHIP_IGD,
267 .busdma_addr_mask_sz = 36,
268 .res_spec = agp_i915_res_spec,
269 .check_active = agp_i915_check_active,
270 .set_desc = agp_i810_set_desc,
271 .dump_regs = agp_i915_dump_regs,
272 .get_stolen_size = agp_i915_get_stolen_size,
273 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
274 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
275 .install_gatt = agp_i830_install_gatt,
276 .deinstall_gatt = agp_i830_deinstall_gatt,
277 .write_gtt = agp_i915_write_gtt,
278 .install_gtt_pte = agp_i965_install_gtt_pte,
279 .sync_gtt_pte = agp_i915_sync_gtt_pte,
280 .set_aperture = agp_i915_set_aperture,
281 .chipset_flush_setup = agp_i965_chipset_flush_setup,
282 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
283 .chipset_flush = agp_i915_chipset_flush,
286 static const struct agp_i810_driver agp_i810_g4x_driver = {
287 .chiptype = CHIP_G4X,
289 .busdma_addr_mask_sz = 36,
290 .res_spec = agp_i965_res_spec,
291 .check_active = agp_i915_check_active,
292 .set_desc = agp_i810_set_desc,
293 .dump_regs = agp_i965_dump_regs,
294 .get_stolen_size = agp_i915_get_stolen_size,
295 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
296 .get_gtt_total_entries = agp_gen5_get_gtt_total_entries,
297 .install_gatt = agp_i830_install_gatt,
298 .deinstall_gatt = agp_i830_deinstall_gatt,
299 .write_gtt = agp_g4x_write_gtt,
300 .install_gtt_pte = agp_g4x_install_gtt_pte,
301 .sync_gtt_pte = agp_g4x_sync_gtt_pte,
302 .set_aperture = agp_i915_set_aperture,
303 .chipset_flush_setup = agp_i965_chipset_flush_setup,
304 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
305 .chipset_flush = agp_i915_chipset_flush,
308 /* For adding new devices, devid is the id of the graphics controller
309 * (pci:0:2:0, for example). The placeholder (usually at pci:0:2:1) for the
310 * second head should never be added. The bridge_offset is the offset to
311 * subtract from devid to get the id of the hostb that the device is on.
313 static const struct agp_i810_match {
316 const struct agp_i810_driver *driver;
317 } agp_i810_matches[] = {
320 .name = "Intel 82915G (915G GMCH) SVGA controller",
321 .driver = &agp_i810_i915_driver
325 .name = "Intel E7221 SVGA controller",
326 .driver = &agp_i810_i915_driver
330 .name = "Intel 82915GM (915GM GMCH) SVGA controller",
331 .driver = &agp_i810_i915_driver
335 .name = "Intel 82945G (945G GMCH) SVGA controller",
336 .driver = &agp_i810_i915_driver
340 .name = "Intel 82945GM (945GM GMCH) SVGA controller",
341 .driver = &agp_i810_i915_driver
345 .name = "Intel 945GME SVGA controller",
346 .driver = &agp_i810_i915_driver
350 .name = "Intel 946GZ SVGA controller",
351 .driver = &agp_i810_g965_driver
355 .name = "Intel G965 SVGA controller",
356 .driver = &agp_i810_g965_driver
360 .name = "Intel Q965 SVGA controller",
361 .driver = &agp_i810_g965_driver
365 .name = "Intel G965 SVGA controller",
366 .driver = &agp_i810_g965_driver
370 .name = "Intel Q35 SVGA controller",
371 .driver = &agp_i810_g33_driver
375 .name = "Intel G33 SVGA controller",
376 .driver = &agp_i810_g33_driver
380 .name = "Intel Q33 SVGA controller",
381 .driver = &agp_i810_g33_driver
385 .name = "Intel Pineview SVGA controller",
386 .driver = &pineview_gtt_driver
390 .name = "Intel Pineview (M) SVGA controller",
391 .driver = &pineview_gtt_driver
395 .name = "Intel GM965 SVGA controller",
396 .driver = &agp_i810_g965_driver
400 .name = "Intel GME965 SVGA controller",
401 .driver = &agp_i810_g965_driver
405 .name = "Intel GM45 SVGA controller",
406 .driver = &agp_i810_g4x_driver
410 .name = "Intel Eaglelake SVGA controller",
411 .driver = &agp_i810_g4x_driver
415 .name = "Intel Q45 SVGA controller",
416 .driver = &agp_i810_g4x_driver
420 .name = "Intel G45 SVGA controller",
421 .driver = &agp_i810_g4x_driver
425 .name = "Intel G41 SVGA controller",
426 .driver = &agp_i810_g4x_driver
430 .name = "Intel Ironlake (D) SVGA controller",
431 .driver = &agp_i810_g4x_driver
435 .name = "Intel Ironlake (M) SVGA controller",
436 .driver = &agp_i810_g4x_driver
444 static const struct agp_i810_match*
445 agp_i810_match(device_t dev)
449 if (pci_get_vendor(dev) != PCI_VENDOR_INTEL)
452 devid = pci_get_device(dev);
453 for (i = 0; agp_i810_matches[i].devid != 0; i++) {
454 if (agp_i810_matches[i].devid == devid)
457 if (agp_i810_matches[i].devid == 0)
460 return (&agp_i810_matches[i]);
464 * Find bridge device.
467 agp_i810_find_bridge(device_t dev)
470 return (pci_find_dbsf(0, 0, 0, 0));
474 agp_i810_identify(driver_t *driver, device_t parent)
477 if (device_find_child(parent, "agp", -1) == NULL &&
478 agp_i810_match(parent))
479 device_add_child(parent, "agp", -1);
483 agp_i915_check_active(device_t bridge_dev)
487 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
488 if ((deven & AGP_I915_DEVEN_D2F0) == AGP_I915_DEVEN_D2F0_DISABLED)
494 agp_i810_set_desc(device_t dev, const struct agp_i810_match *match)
497 device_set_desc(dev, match->name);
501 agp_i810_probe(device_t dev)
504 const struct agp_i810_match *match;
507 if (resource_disabled("agp", device_get_unit(dev)))
509 match = agp_i810_match(dev);
513 bdev = agp_i810_find_bridge(dev);
516 kprintf("I810: can't find bridge device\n");
521 * checking whether internal graphics device has been activated.
523 if (match->driver->check_active != NULL) {
524 err = match->driver->check_active(bdev);
527 kprintf("i810: disabled, not probing\n");
532 match->driver->set_desc(dev, match);
533 return (BUS_PROBE_DEFAULT);
537 agp_i915_dump_regs(device_t dev)
539 struct agp_i810_softc *sc = device_get_softc(dev);
541 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
542 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
543 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
544 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
545 device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
546 pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
550 agp_i965_dump_regs(device_t dev)
552 struct agp_i810_softc *sc = device_get_softc(dev);
554 device_printf(dev, "AGP_I965_PGTBL_CTL2: %08x\n",
555 bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2));
556 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
557 pci_read_config(dev, AGP_I855_GCC1, 1));
558 device_printf(dev, "AGP_I965_MSAC: 0x%02x\n",
559 pci_read_config(dev, AGP_I965_MSAC, 1));
563 agp_i915_get_stolen_size(device_t dev)
565 struct agp_i810_softc *sc;
566 unsigned int gcc1, stolen, gtt_size;
568 sc = device_get_softc(dev);
571 * Stolen memory is set up at the beginning of the aperture by
572 * the BIOS, consisting of the GATT followed by 4kb for the
575 switch (sc->match->driver->chiptype) {
583 switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
584 AGP_I810_PGTBL_SIZE_MASK) {
585 case AGP_I810_PGTBL_SIZE_128KB:
588 case AGP_I810_PGTBL_SIZE_256KB:
591 case AGP_I810_PGTBL_SIZE_512KB:
594 case AGP_I965_PGTBL_SIZE_1MB:
597 case AGP_I965_PGTBL_SIZE_2MB:
600 case AGP_I965_PGTBL_SIZE_1_5MB:
601 gtt_size = 1024 + 512;
604 device_printf(dev, "Bad PGTBL size\n");
609 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2);
610 switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) {
611 case AGP_G33_MGGC_GGMS_SIZE_1M:
614 case AGP_G33_MGGC_GGMS_SIZE_2M:
618 device_printf(dev, "Bad PGTBL size\n");
627 device_printf(dev, "Bad chiptype\n");
631 /* GCC1 is called MGGC on i915+ */
632 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
633 switch (gcc1 & AGP_I855_GCC1_GMS) {
634 case AGP_I855_GCC1_GMS_STOLEN_1M:
637 case AGP_I855_GCC1_GMS_STOLEN_4M:
640 case AGP_I855_GCC1_GMS_STOLEN_8M:
643 case AGP_I855_GCC1_GMS_STOLEN_16M:
646 case AGP_I855_GCC1_GMS_STOLEN_32M:
649 case AGP_I915_GCC1_GMS_STOLEN_48M:
650 stolen = sc->match->driver->gen > 2 ? 48 * 1024 : 0;
652 case AGP_I915_GCC1_GMS_STOLEN_64M:
653 stolen = sc->match->driver->gen > 2 ? 64 * 1024 : 0;
655 case AGP_G33_GCC1_GMS_STOLEN_128M:
656 stolen = sc->match->driver->gen > 2 ? 128 * 1024 : 0;
658 case AGP_G33_GCC1_GMS_STOLEN_256M:
659 stolen = sc->match->driver->gen > 2 ? 256 * 1024 : 0;
661 case AGP_G4X_GCC1_GMS_STOLEN_96M:
662 if (sc->match->driver->chiptype == CHIP_I965 ||
663 sc->match->driver->chiptype == CHIP_G4X)
668 case AGP_G4X_GCC1_GMS_STOLEN_160M:
669 if (sc->match->driver->chiptype == CHIP_I965 ||
670 sc->match->driver->chiptype == CHIP_G4X)
675 case AGP_G4X_GCC1_GMS_STOLEN_224M:
676 if (sc->match->driver->chiptype == CHIP_I965 ||
677 sc->match->driver->chiptype == CHIP_G4X)
682 case AGP_G4X_GCC1_GMS_STOLEN_352M:
683 if (sc->match->driver->chiptype == CHIP_I965 ||
684 sc->match->driver->chiptype == CHIP_G4X)
691 "unknown memory configuration, disabling (GCC1 %x)\n",
697 sc->stolen_size = stolen * 1024;
698 sc->stolen = (stolen - gtt_size) * 1024 / 4096;
704 agp_i915_get_gtt_mappable_entries(device_t dev)
706 struct agp_i810_softc *sc;
709 sc = device_get_softc(dev);
710 ap = AGP_GET_APERTURE(dev);
711 sc->gtt_mappable_entries = ap >> AGP_PAGE_SHIFT;
716 agp_i810_get_gtt_total_entries(device_t dev)
718 struct agp_i810_softc *sc;
720 sc = device_get_softc(dev);
721 sc->gtt_total_entries = sc->gtt_mappable_entries;
726 agp_i965_get_gtt_total_entries(device_t dev)
728 struct agp_i810_softc *sc;
732 sc = device_get_softc(dev);
734 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
735 switch (pgetbl_ctl & AGP_I810_PGTBL_SIZE_MASK) {
736 case AGP_I810_PGTBL_SIZE_128KB:
737 sc->gtt_total_entries = 128 * 1024 / 4;
739 case AGP_I810_PGTBL_SIZE_256KB:
740 sc->gtt_total_entries = 256 * 1024 / 4;
742 case AGP_I810_PGTBL_SIZE_512KB:
743 sc->gtt_total_entries = 512 * 1024 / 4;
745 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
746 case AGP_I810_PGTBL_SIZE_1MB:
747 sc->gtt_total_entries = 1024 * 1024 / 4;
749 case AGP_I810_PGTBL_SIZE_2MB:
750 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
752 case AGP_I810_PGTBL_SIZE_1_5MB:
753 sc->gtt_total_entries = (1024 + 512) * 1024 / 4;
756 device_printf(dev, "Unknown page table size\n");
763 agp_gen5_adjust_pgtbl_size(device_t dev, uint32_t sz)
765 struct agp_i810_softc *sc;
766 uint32_t pgetbl_ctl, pgetbl_ctl2;
768 sc = device_get_softc(dev);
770 /* Disable per-process page table. */
771 pgetbl_ctl2 = bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2);
772 pgetbl_ctl2 &= ~AGP_I810_PGTBL_ENABLED;
773 bus_write_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2, pgetbl_ctl2);
775 /* Write the new ggtt size. */
776 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
777 pgetbl_ctl &= ~AGP_I810_PGTBL_SIZE_MASK;
779 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgetbl_ctl);
783 agp_gen5_get_gtt_total_entries(device_t dev)
785 struct agp_i810_softc *sc;
788 sc = device_get_softc(dev);
790 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
791 switch (gcc1 & AGP_G4x_GCC1_SIZE_MASK) {
792 case AGP_G4x_GCC1_SIZE_1M:
793 case AGP_G4x_GCC1_SIZE_VT_1M:
794 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1MB);
796 case AGP_G4x_GCC1_SIZE_VT_1_5M:
797 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1_5MB);
799 case AGP_G4x_GCC1_SIZE_2M:
800 case AGP_G4x_GCC1_SIZE_VT_2M:
801 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_2MB);
804 device_printf(dev, "Unknown page table size\n");
808 return (agp_i965_get_gtt_total_entries(dev));
812 agp_i830_install_gatt(device_t dev)
814 struct agp_i810_softc *sc;
817 sc = device_get_softc(dev);
820 * The i830 automatically initializes the 128k gatt on boot.
821 * GATT address is already in there, make sure it's enabled.
823 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
825 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
827 sc->gatt->ag_physical = pgtblctl & ~1;
832 agp_i810_attach(device_t dev)
834 struct agp_i810_softc *sc;
837 sc = device_get_softc(dev);
838 sc->bdev = agp_i810_find_bridge(dev);
839 if (sc->bdev == NULL)
842 sc->match = agp_i810_match(dev);
844 agp_set_aperture_resource(dev, sc->match->driver->gen <= 2 ?
845 AGP_APBASE : AGP_I915_GMADR);
846 error = agp_generic_attach(dev);
850 if (ptoa((vm_paddr_t)Maxmem) >
851 (1ULL << sc->match->driver->busdma_addr_mask_sz) - 1) {
852 device_printf(dev, "agp_i810 does not support physical "
853 "memory above %ju.\n", (uintmax_t)(1ULL <<
854 sc->match->driver->busdma_addr_mask_sz) - 1);
858 if (bus_alloc_resources(dev, sc->match->driver->res_spec, sc->sc_res)) {
859 agp_generic_detach(dev);
863 sc->initial_aperture = AGP_GET_APERTURE(dev);
864 sc->gatt = kmalloc(sizeof(struct agp_gatt), M_DRM, M_WAITOK);
865 sc->gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
867 if ((error = sc->match->driver->get_stolen_size(dev)) != 0 ||
868 (error = sc->match->driver->install_gatt(dev)) != 0 ||
869 (error = sc->match->driver->get_gtt_mappable_entries(dev)) != 0 ||
870 (error = sc->match->driver->get_gtt_total_entries(dev)) != 0 ||
871 (error = sc->match->driver->chipset_flush_setup(dev)) != 0) {
872 bus_release_resources(dev, sc->match->driver->res_spec,
875 agp_generic_detach(dev);
880 device_printf(dev, "aperture size is %dM",
881 sc->initial_aperture / 1024 / 1024);
883 kprintf(", detected %dk stolen memory\n", sc->stolen * 4);
887 sc->match->driver->dump_regs(dev);
888 device_printf(dev, "Mappable GTT entries: %d\n",
889 sc->gtt_mappable_entries);
890 device_printf(dev, "Total GTT entries: %d\n",
891 sc->gtt_total_entries);
897 agp_i830_deinstall_gatt(device_t dev)
899 struct agp_i810_softc *sc;
900 unsigned int pgtblctl;
902 sc = device_get_softc(dev);
903 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
905 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
909 agp_i810_detach(device_t dev)
911 struct agp_i810_softc *sc;
913 sc = device_get_softc(dev);
916 /* Clear the GATT base. */
917 sc->match->driver->deinstall_gatt(dev);
919 sc->match->driver->chipset_flush_teardown(dev);
921 /* Put the aperture back the way it started. */
922 AGP_SET_APERTURE(dev, sc->initial_aperture);
925 bus_release_resources(dev, sc->match->driver->res_spec, sc->sc_res);
932 agp_i810_resume(device_t dev)
934 struct agp_i810_softc *sc;
935 sc = device_get_softc(dev);
937 AGP_SET_APERTURE(dev, sc->initial_aperture);
939 /* Install the GATT. */
940 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
941 sc->gatt->ag_physical | 1);
943 return (bus_generic_resume(dev));
947 * Sets the PCI resource size of the aperture on i830-class and below chipsets,
948 * while returning failure on later chipsets when an actual change is
951 * This whole function is likely bogus, as the kernel would probably need to
952 * reconfigure the placement of the AGP aperture if a larger size is requested,
953 * which doesn't happen currently.
957 agp_i915_set_aperture(device_t dev, u_int32_t aperture)
960 return (agp_generic_set_aperture(dev, aperture));
964 agp_i810_method_set_aperture(device_t dev, u_int32_t aperture)
966 struct agp_i810_softc *sc;
968 sc = device_get_softc(dev);
969 return (sc->match->driver->set_aperture(dev, aperture));
973 * Writes a GTT entry mapping the page at the given offset from the
974 * beginning of the aperture to the given physical address. Setup the
975 * caching mode according to flags.
977 * For gen 1, 2 and 3, GTT start is located at AGP_I810_GTT offset
978 * from corresponding BAR start. For gen 4, offset is 512KB +
979 * AGP_I810_GTT, for gen 5 and 6 it is 2MB + AGP_I810_GTT.
981 * Also, the bits of the physical page address above 4GB needs to be
982 * placed into bits 40-32 of PTE.
985 agp_i915_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
990 pte = (u_int32_t)physical | I810_PTE_VALID;
991 if (flags == AGP_USER_CACHED_MEMORY)
992 pte |= I830_PTE_SYSTEM_CACHED;
994 agp_i915_write_gtt(dev, index, pte);
998 agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte)
1000 struct agp_i810_softc *sc;
1002 sc = device_get_softc(dev);
1003 bus_write_4(sc->sc_res[0], index * 4, pte);
1007 agp_i965_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1012 pte = (u_int32_t)physical | I810_PTE_VALID;
1013 if (flags == AGP_USER_CACHED_MEMORY)
1014 pte |= I830_PTE_SYSTEM_CACHED;
1016 pte |= (physical >> 28) & 0xf0;
1017 agp_i965_write_gtt(dev, index, pte);
1021 agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte)
1023 struct agp_i810_softc *sc;
1025 sc = device_get_softc(dev);
1026 bus_write_4(sc->sc_res[0], index * 4 + (512 * 1024), pte);
1030 agp_g4x_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1035 pte = (u_int32_t)physical | I810_PTE_VALID;
1036 if (flags == AGP_USER_CACHED_MEMORY)
1037 pte |= I830_PTE_SYSTEM_CACHED;
1039 pte |= (physical >> 28) & 0xf0;
1040 agp_g4x_write_gtt(dev, index, pte);
1044 agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte)
1046 struct agp_i810_softc *sc;
1048 sc = device_get_softc(dev);
1049 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1053 agp_i810_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
1055 struct agp_i810_softc *sc = device_get_softc(dev);
1058 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
1059 device_printf(dev, "failed: offset is 0x%08jx, "
1060 "shift is %d, entries is %d\n", (intmax_t)offset,
1061 AGP_PAGE_SHIFT, sc->gatt->ag_entries);
1064 index = offset >> AGP_PAGE_SHIFT;
1065 if (sc->stolen != 0 && index < sc->stolen) {
1066 device_printf(dev, "trying to bind into stolen memory\n");
1069 sc->match->driver->install_gtt_pte(dev, index, physical, 0);
1074 agp_i810_unbind_page(device_t dev, vm_offset_t offset)
1076 struct agp_i810_softc *sc;
1079 sc = device_get_softc(dev);
1080 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
1082 index = offset >> AGP_PAGE_SHIFT;
1083 if (sc->stolen != 0 && index < sc->stolen) {
1084 device_printf(dev, "trying to unbind from stolen memory\n");
1087 sc->match->driver->install_gtt_pte(dev, index, 0, 0);
1092 agp_i915_sync_gtt_pte(device_t dev, u_int index)
1094 struct agp_i810_softc *sc;
1096 sc = device_get_softc(dev);
1097 bus_read_4(sc->sc_res[1], index * 4);
1101 agp_i965_sync_gtt_pte(device_t dev, u_int index)
1103 struct agp_i810_softc *sc;
1105 sc = device_get_softc(dev);
1106 bus_read_4(sc->sc_res[0], index * 4 + (512 * 1024));
1110 agp_g4x_sync_gtt_pte(device_t dev, u_int index)
1112 struct agp_i810_softc *sc;
1114 sc = device_get_softc(dev);
1115 bus_read_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024));
1119 * Writing via memory mapped registers already flushes all TLBs.
1122 agp_i810_flush_tlb(device_t dev)
1127 agp_i810_enable(device_t dev, u_int32_t mode)
1133 static struct agp_memory *
1134 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
1136 struct agp_i810_softc *sc;
1137 struct agp_memory *mem;
1140 sc = device_get_softc(dev);
1142 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1143 sc->agp.as_allocated + size > sc->agp.as_maxmem)
1148 * Mapping local DRAM into GATT.
1150 if (sc->match->driver->chiptype != CHIP_I810)
1152 if (size != sc->dcache_size)
1154 } else if (type == 2) {
1156 * Type 2 is the contiguous physical memory type, that hands
1157 * back a physical address. This is used for cursors on i810.
1158 * Hand back as many single pages with physical as the user
1159 * wants, but only allow one larger allocation (ARGB cursor)
1162 if (size != AGP_PAGE_SIZE) {
1163 if (sc->argb_cursor != NULL)
1166 /* Allocate memory for ARGB cursor, if we can. */
1167 sc->argb_cursor = contigmalloc(size, M_DRM,
1168 0, 0, ~0, PAGE_SIZE, 0);
1169 if (sc->argb_cursor == NULL)
1174 mem = kmalloc(sizeof *mem, M_DRM, M_INTWAIT);
1175 mem->am_id = sc->agp.as_nextid++;
1176 mem->am_size = size;
1177 mem->am_type = type;
1178 if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
1179 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
1180 atop(round_page(size)));
1185 if (size == AGP_PAGE_SIZE) {
1187 * Allocate and wire down the page now so that we can
1188 * get its physical address.
1190 VM_OBJECT_LOCK(mem->am_obj);
1191 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NORMAL |
1195 VM_OBJECT_UNLOCK(mem->am_obj);
1196 mem->am_physical = VM_PAGE_TO_PHYS(m);
1199 /* Our allocation is already nicely wired down for us.
1200 * Just grab the physical address.
1202 mem->am_physical = vtophys(sc->argb_cursor);
1205 mem->am_physical = 0;
1208 mem->am_is_bound = 0;
1209 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
1210 sc->agp.as_allocated += size;
1216 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
1218 struct agp_i810_softc *sc;
1220 if (mem->am_is_bound)
1223 sc = device_get_softc(dev);
1225 if (mem->am_type == 2) {
1226 if (mem->am_size == AGP_PAGE_SIZE) {
1228 * Unwire the page which we wired in alloc_memory.
1232 vm_object_hold(mem->am_obj);
1233 m = vm_page_lookup_busy_wait(mem->am_obj, 0,
1235 vm_object_drop(mem->am_obj);
1236 vm_page_unwire(m, 0);
1239 contigfree(sc->argb_cursor, mem->am_size, M_DRM);
1240 sc->argb_cursor = NULL;
1244 sc->agp.as_allocated -= mem->am_size;
1245 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
1247 vm_object_deallocate(mem->am_obj);
1253 agp_i810_bind_memory(device_t dev, struct agp_memory *mem, vm_offset_t offset)
1255 struct agp_i810_softc *sc;
1258 /* Do some sanity checks first. */
1259 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
1260 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
1261 device_printf(dev, "binding memory at bad offset %#x\n",
1266 sc = device_get_softc(dev);
1267 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1268 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1269 if (mem->am_is_bound) {
1270 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1273 /* The memory's already wired down, just stick it in the GTT. */
1274 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1275 sc->match->driver->install_gtt_pte(dev, (offset + i) >>
1276 AGP_PAGE_SHIFT, mem->am_physical + i, 0);
1279 mem->am_offset = offset;
1280 mem->am_is_bound = 1;
1281 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1285 if (mem->am_type != 1)
1286 return (agp_generic_bind_memory(dev, mem, offset));
1289 * Mapping local DRAM into GATT.
1291 if (sc->match->driver->chiptype != CHIP_I810)
1293 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
1294 bus_write_4(sc->sc_res[0],
1295 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
1301 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
1303 struct agp_i810_softc *sc;
1306 sc = device_get_softc(dev);
1308 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1309 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1310 if (!mem->am_is_bound) {
1311 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1315 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1316 sc->match->driver->install_gtt_pte(dev,
1317 (mem->am_offset + i) >> AGP_PAGE_SHIFT, 0, 0);
1320 mem->am_is_bound = 0;
1321 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1325 if (mem->am_type != 1)
1326 return (agp_generic_unbind_memory(dev, mem));
1328 if (sc->match->driver->chiptype != CHIP_I810)
1330 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1331 sc->match->driver->install_gtt_pte(dev, i >> AGP_PAGE_SHIFT,
1337 static device_method_t agp_i810_methods[] = {
1338 /* Device interface */
1339 DEVMETHOD(device_identify, agp_i810_identify),
1340 DEVMETHOD(device_probe, agp_i810_probe),
1341 DEVMETHOD(device_attach, agp_i810_attach),
1342 DEVMETHOD(device_detach, agp_i810_detach),
1343 DEVMETHOD(device_suspend, bus_generic_suspend),
1344 DEVMETHOD(device_resume, agp_i810_resume),
1347 DEVMETHOD(agp_get_aperture, agp_generic_get_aperture),
1348 DEVMETHOD(agp_set_aperture, agp_i810_method_set_aperture),
1349 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
1350 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
1351 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
1352 DEVMETHOD(agp_enable, agp_i810_enable),
1353 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
1354 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
1355 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
1356 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
1357 DEVMETHOD(agp_chipset_flush, agp_intel_gtt_chipset_flush),
1362 static driver_t agp_i810_driver = {
1365 sizeof(struct agp_i810_softc),
1368 static devclass_t agp_devclass;
1370 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, NULL, NULL);
1371 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1372 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
1374 extern vm_page_t bogus_page;
1377 agp_intel_gtt_clear_range(device_t dev, u_int first_entry, u_int num_entries)
1379 struct agp_i810_softc *sc;
1382 sc = device_get_softc(dev);
1383 for (i = 0; i < num_entries; i++)
1384 sc->match->driver->install_gtt_pte(dev, first_entry + i,
1385 VM_PAGE_TO_PHYS(bogus_page), 0);
1386 sc->match->driver->sync_gtt_pte(dev, first_entry + num_entries - 1);
1390 agp_intel_gtt_insert_pages(device_t dev, u_int first_entry, u_int num_entries,
1391 vm_page_t *pages, u_int flags)
1393 struct agp_i810_softc *sc;
1396 sc = device_get_softc(dev);
1397 for (i = 0; i < num_entries; i++) {
1398 KKASSERT(pages[i]->valid == VM_PAGE_BITS_ALL);
1399 KKASSERT(pages[i]->wire_count > 0);
1400 sc->match->driver->install_gtt_pte(dev, first_entry + i,
1401 VM_PAGE_TO_PHYS(pages[i]), flags);
1403 sc->match->driver->sync_gtt_pte(dev, first_entry + num_entries - 1);
1407 intel_gtt_insert_page(dma_addr_t addr, unsigned int pg, unsigned int flags)
1409 struct agp_i810_softc *sc = device_get_softc(intel_agp);
1411 sc->match->driver->install_gtt_pte(intel_agp, addr, pg, flags);
1415 intel_gtt_insert_sg_entries(struct sg_table *st,
1416 unsigned int pg_start,
1419 struct agp_i810_softc *sc = device_get_softc(intel_agp);
1420 struct scatterlist *sg;
1422 int i, j, npages, subpage;
1425 for_each_sg(st->sgl, sg, st->nents, j) {
1426 npages = sg_dma_len(sg) / PAGE_SIZE;
1427 for (subpage = 0; subpage < npages; subpage++) {
1428 page = sg_dma_address(sg) + subpage * PAGE_SIZE;
1429 sc->match->driver->install_gtt_pte(intel_agp,
1430 pg_start + i, page, flags);
1434 sc->match->driver->sync_gtt_pte(intel_agp, pg_start + i - 1);
1439 agp_intel_gtt_get(device_t dev)
1441 struct agp_i810_softc *sc;
1442 struct intel_gtt res;
1444 sc = device_get_softc(dev);
1445 res.stolen_size = sc->stolen_size;
1446 res.gtt_total_entries = sc->gtt_total_entries;
1447 res.gtt_mappable_entries = sc->gtt_mappable_entries;
1448 res.do_idle_maps = 0;
1449 res.scratch_page_dma = VM_PAGE_TO_PHYS(bogus_page);
1454 agp_i915_chipset_flush_alloc_page(device_t dev, uint64_t start, uint64_t end)
1456 struct agp_i810_softc *sc;
1459 sc = device_get_softc(dev);
1460 vga = device_get_parent(dev);
1461 sc->sc_flush_page_rid = 100;
1462 sc->sc_flush_page_res = BUS_ALLOC_RESOURCE(device_get_parent(vga), dev,
1463 SYS_RES_MEMORY, &sc->sc_flush_page_rid, start, end, PAGE_SIZE,
1465 if (sc->sc_flush_page_res == NULL) {
1466 device_printf(dev, "Failed to allocate flush page at 0x%jx\n",
1470 sc->sc_flush_page_vaddr = rman_get_virtual(sc->sc_flush_page_res);
1472 device_printf(dev, "Allocated flush page phys 0x%jx virt %p\n",
1473 (uintmax_t)rman_get_start(sc->sc_flush_page_res),
1474 sc->sc_flush_page_vaddr);
1480 agp_i915_chipset_flush_free_page(device_t dev)
1482 struct agp_i810_softc *sc;
1485 sc = device_get_softc(dev);
1486 vga = device_get_parent(dev);
1487 if (sc->sc_flush_page_res == NULL)
1489 BUS_DEACTIVATE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
1490 sc->sc_flush_page_rid, sc->sc_flush_page_res);
1491 BUS_RELEASE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
1492 sc->sc_flush_page_rid, sc->sc_flush_page_res);
1496 agp_i915_chipset_flush_setup(device_t dev)
1498 struct agp_i810_softc *sc;
1502 sc = device_get_softc(dev);
1503 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
1504 if ((temp & 1) != 0) {
1508 "Found already configured flush page at 0x%jx\n",
1510 sc->sc_bios_allocated_flush_page = 1;
1512 * In the case BIOS initialized the flush pointer (?)
1513 * register, expect that BIOS also set up the resource
1516 error = agp_i915_chipset_flush_alloc_page(dev, temp,
1517 temp + PAGE_SIZE - 1);
1521 sc->sc_bios_allocated_flush_page = 0;
1522 error = agp_i915_chipset_flush_alloc_page(dev, 0, 0xffffffff);
1525 temp = rman_get_start(sc->sc_flush_page_res);
1526 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp | 1, 4);
1532 agp_i915_chipset_flush_teardown(device_t dev)
1534 struct agp_i810_softc *sc;
1537 sc = device_get_softc(dev);
1538 if (sc->sc_flush_page_res == NULL)
1540 if (!sc->sc_bios_allocated_flush_page) {
1541 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
1543 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp, 4);
1545 agp_i915_chipset_flush_free_page(dev);
1549 agp_i965_chipset_flush_setup(device_t dev)
1551 struct agp_i810_softc *sc;
1553 uint32_t temp_hi, temp_lo;
1556 sc = device_get_softc(dev);
1558 temp_hi = pci_read_config(sc->bdev, AGP_I965_IFPADDR + 4, 4);
1559 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
1561 if ((temp_lo & 1) != 0) {
1562 temp = ((uint64_t)temp_hi << 32) | (temp_lo & ~1);
1565 "Found already configured flush page at 0x%jx\n",
1567 sc->sc_bios_allocated_flush_page = 1;
1569 * In the case BIOS initialized the flush pointer (?)
1570 * register, expect that BIOS also set up the resource
1573 error = agp_i915_chipset_flush_alloc_page(dev, temp,
1574 temp + PAGE_SIZE - 1);
1578 sc->sc_bios_allocated_flush_page = 0;
1579 error = agp_i915_chipset_flush_alloc_page(dev, 0, ~0);
1582 temp = rman_get_start(sc->sc_flush_page_res);
1583 pci_write_config(sc->bdev, AGP_I965_IFPADDR + 4,
1584 (temp >> 32) & UINT32_MAX, 4);
1585 pci_write_config(sc->bdev, AGP_I965_IFPADDR,
1586 (temp & UINT32_MAX) | 1, 4);
1592 agp_i965_chipset_flush_teardown(device_t dev)
1594 struct agp_i810_softc *sc;
1597 sc = device_get_softc(dev);
1598 if (sc->sc_flush_page_res == NULL)
1600 if (!sc->sc_bios_allocated_flush_page) {
1601 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
1603 pci_write_config(sc->bdev, AGP_I965_IFPADDR, temp_lo, 4);
1605 agp_i915_chipset_flush_free_page(dev);
1609 agp_i915_chipset_flush(device_t dev)
1611 struct agp_i810_softc *sc;
1613 sc = device_get_softc(dev);
1614 *(uint32_t *)sc->sc_flush_page_vaddr = 1;
1618 agp_intel_gtt_chipset_flush(device_t dev)
1620 struct agp_i810_softc *sc;
1622 sc = device_get_softc(dev);
1623 sc->match->driver->chipset_flush(dev);
1628 intel_gtt_clear_range(u_int first_entry, u_int num_entries)
1631 agp_intel_gtt_clear_range(intel_agp, first_entry, num_entries);
1634 void intel_gtt_get(u64 *gtt_total, size_t *stolen_size,
1635 phys_addr_t *mappable_base, u64 *mappable_end)
1637 struct agp_info ainfo;
1639 intel_private.base = agp_intel_gtt_get(intel_agp);
1641 *gtt_total = intel_private.base.gtt_total_entries << PAGE_SHIFT;
1642 *stolen_size = intel_private.base.stolen_size;
1643 agp_get_info(intel_agp, &ainfo);
1644 *mappable_base = ainfo.ai_aperture_base;
1645 *mappable_end = intel_private.base.gtt_mappable_entries << PAGE_SHIFT;
1649 intel_gtt_chipset_flush(void)
1652 return (agp_intel_gtt_chipset_flush(intel_agp));
1659 intel_gtt_sync_pte(u_int entry)
1661 struct agp_i810_softc *sc;
1663 sc = device_get_softc(intel_agp);
1664 sc->match->driver->sync_gtt_pte(intel_agp, entry);
1671 intel_gtt_write(u_int entry, uint32_t val)
1673 struct agp_i810_softc *sc;
1675 sc = device_get_softc(intel_agp);
1676 sc->match->driver->write_gtt(intel_agp, entry, val);
1679 #define GFX_FLSH_CNTL 0x2170
1682 intel_enable_gtt(void)
1684 struct agp_i810_softc *sc = device_get_softc(intel_agp);
1686 /* Some chipsets such as Pineview can't report if the GTT
1687 * has been enabled or not.
1688 * Assume everything is fine. */
1690 /* Flush all chipset write buffers nevertheless */
1691 bus_write_4(sc->sc_res[0], GFX_FLSH_CNTL, 1);
1692 bus_write_4(sc->sc_res[0], GFX_FLSH_CNTL, 0);