2 * Copyright (c) 2000 Doug Rabson
3 * Copyright (c) 2000 Ruslan Ermilov
4 * Copyright (c) 2011 The FreeBSD Foundation
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
34 * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
36 * This is generic Intel GTT handling code, morphed from the AGP
41 #define KTR_AGP_I810 KTR_DEV
43 #define KTR_AGP_I810 0
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
55 #include <bus/pci/pcivar.h>
56 #include <bus/pci/pcireg.h>
59 #include <drm/intel-gtt.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_pageout.h>
67 #include <machine/md_var.h>
69 #define bus_read_1(r, o) \
70 bus_space_read_1((r)->r_bustag, (r)->r_bushandle, (o))
71 #define bus_read_4(r, o) \
72 bus_space_read_4((r)->r_bustag, (r)->r_bushandle, (o))
73 #define bus_write_4(r, o, v) \
74 bus_space_write_4((r)->r_bustag, (r)->r_bushandle, (o), (v))
76 MALLOC_DECLARE(M_AGP);
78 struct agp_i810_match;
80 static int agp_i915_check_active(device_t bridge_dev);
81 static int agp_sb_check_active(device_t bridge_dev);
83 static void agp_i810_set_desc(device_t dev, const struct agp_i810_match *match);
85 static void agp_i915_dump_regs(device_t dev);
86 static void agp_i965_dump_regs(device_t dev);
87 static void agp_sb_dump_regs(device_t dev);
89 static int agp_i915_get_stolen_size(device_t dev);
90 static int agp_sb_get_stolen_size(device_t dev);
91 static int agp_gen8_get_stolen_size(device_t dev);
93 static int agp_i915_get_gtt_mappable_entries(device_t dev);
95 static int agp_i810_get_gtt_total_entries(device_t dev);
96 static int agp_i965_get_gtt_total_entries(device_t dev);
97 static int agp_gen5_get_gtt_total_entries(device_t dev);
98 static int agp_sb_get_gtt_total_entries(device_t dev);
99 static int agp_gen8_get_gtt_total_entries(device_t dev);
101 static int agp_i830_install_gatt(device_t dev);
103 static void agp_i830_deinstall_gatt(device_t dev);
105 static void agp_i915_install_gtt_pte(device_t dev, u_int index,
106 vm_offset_t physical, int flags);
107 static void agp_i965_install_gtt_pte(device_t dev, u_int index,
108 vm_offset_t physical, int flags);
109 static void agp_g4x_install_gtt_pte(device_t dev, u_int index,
110 vm_offset_t physical, int flags);
111 static void agp_sb_install_gtt_pte(device_t dev, u_int index,
112 vm_offset_t physical, int flags);
113 static void agp_gen8_install_gtt_pte(device_t dev, u_int index,
114 vm_offset_t physical, int flags);
116 static void agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte);
117 static void agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte);
118 static void agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte);
119 static void agp_sb_write_gtt(device_t dev, u_int index, uint32_t pte);
121 static void agp_i915_sync_gtt_pte(device_t dev, u_int index);
122 static void agp_i965_sync_gtt_pte(device_t dev, u_int index);
123 static void agp_g4x_sync_gtt_pte(device_t dev, u_int index);
125 static int agp_i915_set_aperture(device_t dev, u_int32_t aperture);
127 static int agp_i810_chipset_flush_setup(device_t dev);
128 static int agp_i915_chipset_flush_setup(device_t dev);
129 static int agp_i965_chipset_flush_setup(device_t dev);
131 static void agp_i810_chipset_flush_teardown(device_t dev);
132 static void agp_i915_chipset_flush_teardown(device_t dev);
133 static void agp_i965_chipset_flush_teardown(device_t dev);
135 static void agp_i810_chipset_flush(device_t dev);
136 static void agp_i915_chipset_flush(device_t dev);
139 CHIP_I810, /* i810/i815 */
140 CHIP_I830, /* 830M/845G */
141 CHIP_I855, /* 852GM/855GM/865G */
142 CHIP_I915, /* 915G/915GM */
143 CHIP_I965, /* G965 */
144 CHIP_G33, /* G33/Q33/Q35 */
145 CHIP_IGD, /* Pineview */
146 CHIP_G4X, /* G45/Q45 */
147 CHIP_SB, /* SandyBridge */
150 /* The i810 through i855 have the registers at BAR 1, and the GATT gets
151 * allocated by us. The i915 has registers in BAR 0 and the GATT is at the
152 * start of the stolen memory, and should only be accessed by the OS through
153 * BAR 3. The G965 has registers and GATT in the same BAR (0) -- first 512KB
154 * is registers, second 512KB is GATT.
156 static struct resource_spec agp_i915_res_spec[] = {
157 { SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
158 { SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
162 static struct resource_spec agp_i965_res_spec[] = {
163 { SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
167 static struct resource_spec agp_g4x_res_spec[] = {
168 { SYS_RES_MEMORY, AGP_G4X_MMADR, RF_ACTIVE | RF_SHAREABLE },
169 { SYS_RES_MEMORY, AGP_G4X_GTTADR, RF_ACTIVE | RF_SHAREABLE },
173 struct agp_i810_softc {
174 struct agp_softc agp;
175 u_int32_t initial_aperture; /* aperture size at startup */
176 struct agp_gatt *gatt;
177 u_int32_t dcache_size; /* i810 only */
178 u_int32_t stolen; /* number of i830/845 gtt
179 entries for stolen memory */
180 u_int stolen_size; /* BIOS-reserved graphics memory */
181 u_int gtt_total_entries; /* Total number of gtt ptes */
182 u_int gtt_mappable_entries; /* Number of gtt ptes mappable by CPU */
183 device_t bdev; /* bridge device */
184 void *argb_cursor; /* contigmalloc area for ARGB cursor */
185 struct resource *sc_res[2];
186 const struct agp_i810_match *match;
187 int sc_flush_page_rid;
188 struct resource *sc_flush_page_res;
189 void *sc_flush_page_vaddr;
190 int sc_bios_allocated_flush_page;
193 static device_t intel_agp;
195 struct agp_i810_driver {
198 int busdma_addr_mask_sz;
199 struct resource_spec *res_spec;
200 int (*check_active)(device_t);
201 void (*set_desc)(device_t, const struct agp_i810_match *);
202 void (*dump_regs)(device_t);
203 int (*get_stolen_size)(device_t);
204 int (*get_gtt_total_entries)(device_t);
205 int (*get_gtt_mappable_entries)(device_t);
206 int (*install_gatt)(device_t);
207 void (*deinstall_gatt)(device_t);
208 void (*write_gtt)(device_t, u_int, uint32_t);
209 void (*install_gtt_pte)(device_t, u_int, vm_offset_t, int);
210 void (*sync_gtt_pte)(device_t, u_int);
211 int (*set_aperture)(device_t, u_int32_t);
212 int (*chipset_flush_setup)(device_t);
213 void (*chipset_flush_teardown)(device_t);
214 void (*chipset_flush)(device_t);
218 struct intel_gtt base;
221 static const struct agp_i810_driver agp_i810_i915_driver = {
222 .chiptype = CHIP_I915,
224 .busdma_addr_mask_sz = 32,
225 .res_spec = agp_i915_res_spec,
226 .check_active = agp_i915_check_active,
227 .set_desc = agp_i810_set_desc,
228 .dump_regs = agp_i915_dump_regs,
229 .get_stolen_size = agp_i915_get_stolen_size,
230 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
231 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
232 .install_gatt = agp_i830_install_gatt,
233 .deinstall_gatt = agp_i830_deinstall_gatt,
234 .write_gtt = agp_i915_write_gtt,
235 .install_gtt_pte = agp_i915_install_gtt_pte,
236 .sync_gtt_pte = agp_i915_sync_gtt_pte,
237 .set_aperture = agp_i915_set_aperture,
238 .chipset_flush_setup = agp_i915_chipset_flush_setup,
239 .chipset_flush_teardown = agp_i915_chipset_flush_teardown,
240 .chipset_flush = agp_i915_chipset_flush,
243 static const struct agp_i810_driver agp_i810_g965_driver = {
244 .chiptype = CHIP_I965,
246 .busdma_addr_mask_sz = 36,
247 .res_spec = agp_i965_res_spec,
248 .check_active = agp_i915_check_active,
249 .set_desc = agp_i810_set_desc,
250 .dump_regs = agp_i965_dump_regs,
251 .get_stolen_size = agp_i915_get_stolen_size,
252 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
253 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
254 .install_gatt = agp_i830_install_gatt,
255 .deinstall_gatt = agp_i830_deinstall_gatt,
256 .write_gtt = agp_i965_write_gtt,
257 .install_gtt_pte = agp_i965_install_gtt_pte,
258 .sync_gtt_pte = agp_i965_sync_gtt_pte,
259 .set_aperture = agp_i915_set_aperture,
260 .chipset_flush_setup = agp_i965_chipset_flush_setup,
261 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
262 .chipset_flush = agp_i915_chipset_flush,
265 static const struct agp_i810_driver agp_i810_g33_driver = {
266 .chiptype = CHIP_G33,
268 .busdma_addr_mask_sz = 36,
269 .res_spec = agp_i915_res_spec,
270 .check_active = agp_i915_check_active,
271 .set_desc = agp_i810_set_desc,
272 .dump_regs = agp_i965_dump_regs,
273 .get_stolen_size = agp_i915_get_stolen_size,
274 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
275 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
276 .install_gatt = agp_i830_install_gatt,
277 .deinstall_gatt = agp_i830_deinstall_gatt,
278 .write_gtt = agp_i915_write_gtt,
279 .install_gtt_pte = agp_i965_install_gtt_pte,
280 .sync_gtt_pte = agp_i915_sync_gtt_pte,
281 .set_aperture = agp_i915_set_aperture,
282 .chipset_flush_setup = agp_i965_chipset_flush_setup,
283 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
284 .chipset_flush = agp_i915_chipset_flush,
287 static const struct agp_i810_driver pineview_gtt_driver = {
288 .chiptype = CHIP_IGD,
290 .busdma_addr_mask_sz = 36,
291 .res_spec = agp_i915_res_spec,
292 .check_active = agp_i915_check_active,
293 .set_desc = agp_i810_set_desc,
294 .dump_regs = agp_i915_dump_regs,
295 .get_stolen_size = agp_i915_get_stolen_size,
296 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
297 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
298 .install_gatt = agp_i830_install_gatt,
299 .deinstall_gatt = agp_i830_deinstall_gatt,
300 .write_gtt = agp_i915_write_gtt,
301 .install_gtt_pte = agp_i965_install_gtt_pte,
302 .sync_gtt_pte = agp_i915_sync_gtt_pte,
303 .set_aperture = agp_i915_set_aperture,
304 .chipset_flush_setup = agp_i965_chipset_flush_setup,
305 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
306 .chipset_flush = agp_i915_chipset_flush,
309 static const struct agp_i810_driver agp_i810_g4x_driver = {
310 .chiptype = CHIP_G4X,
312 .busdma_addr_mask_sz = 36,
313 .res_spec = agp_i965_res_spec,
314 .check_active = agp_i915_check_active,
315 .set_desc = agp_i810_set_desc,
316 .dump_regs = agp_i965_dump_regs,
317 .get_stolen_size = agp_i915_get_stolen_size,
318 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
319 .get_gtt_total_entries = agp_gen5_get_gtt_total_entries,
320 .install_gatt = agp_i830_install_gatt,
321 .deinstall_gatt = agp_i830_deinstall_gatt,
322 .write_gtt = agp_g4x_write_gtt,
323 .install_gtt_pte = agp_g4x_install_gtt_pte,
324 .sync_gtt_pte = agp_g4x_sync_gtt_pte,
325 .set_aperture = agp_i915_set_aperture,
326 .chipset_flush_setup = agp_i965_chipset_flush_setup,
327 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
328 .chipset_flush = agp_i915_chipset_flush,
331 static const struct agp_i810_driver agp_i810_sb_driver = {
334 .busdma_addr_mask_sz = 40,
335 .res_spec = agp_g4x_res_spec,
336 .check_active = agp_sb_check_active,
337 .set_desc = agp_i810_set_desc,
338 .dump_regs = agp_sb_dump_regs,
339 .get_stolen_size = agp_sb_get_stolen_size,
340 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
341 .get_gtt_total_entries = agp_sb_get_gtt_total_entries,
342 .install_gatt = agp_i830_install_gatt,
343 .deinstall_gatt = agp_i830_deinstall_gatt,
344 .write_gtt = agp_sb_write_gtt,
345 .install_gtt_pte = agp_sb_install_gtt_pte,
346 .sync_gtt_pte = agp_g4x_sync_gtt_pte,
347 .set_aperture = agp_i915_set_aperture,
348 .chipset_flush_setup = agp_i810_chipset_flush_setup,
349 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
350 .chipset_flush = agp_i810_chipset_flush,
353 static const struct agp_i810_driver valleyview_gtt_driver = {
356 .busdma_addr_mask_sz = 40,
357 .res_spec = agp_g4x_res_spec,
358 .check_active = agp_sb_check_active,
359 .set_desc = agp_i810_set_desc,
360 .dump_regs = agp_sb_dump_regs,
361 .get_stolen_size = agp_sb_get_stolen_size,
362 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
363 .get_gtt_total_entries = agp_sb_get_gtt_total_entries,
364 .install_gatt = agp_i830_install_gatt,
365 .deinstall_gatt = agp_i830_deinstall_gatt,
366 .write_gtt = agp_sb_write_gtt,
367 .install_gtt_pte = agp_sb_install_gtt_pte,
368 .sync_gtt_pte = agp_g4x_sync_gtt_pte,
369 .set_aperture = agp_i915_set_aperture,
370 .chipset_flush_setup = agp_i810_chipset_flush_setup,
371 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
372 .chipset_flush = agp_i810_chipset_flush,
375 static const struct agp_i810_driver broadwell_gtt_driver = {
378 .busdma_addr_mask_sz = 40,
379 .res_spec = agp_g4x_res_spec,
380 .check_active = agp_sb_check_active,
381 .set_desc = agp_i810_set_desc,
382 .dump_regs = agp_sb_dump_regs,
383 .get_stolen_size = agp_gen8_get_stolen_size,
384 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
385 .get_gtt_total_entries = agp_gen8_get_gtt_total_entries,
386 .install_gatt = agp_i830_install_gatt,
387 .deinstall_gatt = agp_i830_deinstall_gatt,
389 .install_gtt_pte = agp_gen8_install_gtt_pte,
390 .sync_gtt_pte = agp_g4x_sync_gtt_pte,
391 .set_aperture = agp_i915_set_aperture,
392 .chipset_flush_setup = agp_i810_chipset_flush_setup,
393 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
394 .chipset_flush = agp_i810_chipset_flush,
397 /* For adding new devices, devid is the id of the graphics controller
398 * (pci:0:2:0, for example). The placeholder (usually at pci:0:2:1) for the
399 * second head should never be added. The bridge_offset is the offset to
400 * subtract from devid to get the id of the hostb that the device is on.
402 static const struct agp_i810_match {
405 const struct agp_i810_driver *driver;
406 } agp_i810_matches[] = {
409 .name = "Intel 82915G (915G GMCH) SVGA controller",
410 .driver = &agp_i810_i915_driver
414 .name = "Intel E7221 SVGA controller",
415 .driver = &agp_i810_i915_driver
419 .name = "Intel 82915GM (915GM GMCH) SVGA controller",
420 .driver = &agp_i810_i915_driver
424 .name = "Intel 82945G (945G GMCH) SVGA controller",
425 .driver = &agp_i810_i915_driver
429 .name = "Intel 82945GM (945GM GMCH) SVGA controller",
430 .driver = &agp_i810_i915_driver
434 .name = "Intel 945GME SVGA controller",
435 .driver = &agp_i810_i915_driver
439 .name = "Intel 946GZ SVGA controller",
440 .driver = &agp_i810_g965_driver
444 .name = "Intel G965 SVGA controller",
445 .driver = &agp_i810_g965_driver
449 .name = "Intel Q965 SVGA controller",
450 .driver = &agp_i810_g965_driver
454 .name = "Intel G965 SVGA controller",
455 .driver = &agp_i810_g965_driver
459 .name = "Intel Q35 SVGA controller",
460 .driver = &agp_i810_g33_driver
464 .name = "Intel G33 SVGA controller",
465 .driver = &agp_i810_g33_driver
469 .name = "Intel Q33 SVGA controller",
470 .driver = &agp_i810_g33_driver
474 .name = "Intel Pineview SVGA controller",
475 .driver = &pineview_gtt_driver
479 .name = "Intel Pineview (M) SVGA controller",
480 .driver = &pineview_gtt_driver
484 .name = "Intel GM965 SVGA controller",
485 .driver = &agp_i810_g965_driver
489 .name = "Intel GME965 SVGA controller",
490 .driver = &agp_i810_g965_driver
494 .name = "Intel GM45 SVGA controller",
495 .driver = &agp_i810_g4x_driver
499 .name = "Intel Eaglelake SVGA controller",
500 .driver = &agp_i810_g4x_driver
504 .name = "Intel Q45 SVGA controller",
505 .driver = &agp_i810_g4x_driver
509 .name = "Intel G45 SVGA controller",
510 .driver = &agp_i810_g4x_driver
514 .name = "Intel G41 SVGA controller",
515 .driver = &agp_i810_g4x_driver
519 .name = "Intel Ironlake (D) SVGA controller",
520 .driver = &agp_i810_g4x_driver
524 .name = "Intel Ironlake (M) SVGA controller",
525 .driver = &agp_i810_g4x_driver
529 .name = "SandyBridge desktop GT1 IG",
530 .driver = &agp_i810_sb_driver
534 .name = "SandyBridge desktop GT2 IG",
535 .driver = &agp_i810_sb_driver
539 .name = "SandyBridge desktop GT2+ IG",
540 .driver = &agp_i810_sb_driver
544 .name = "SandyBridge mobile GT1 IG",
545 .driver = &agp_i810_sb_driver
549 .name = "SandyBridge mobile GT2 IG",
550 .driver = &agp_i810_sb_driver
554 .name = "SandyBridge mobile GT2+ IG",
555 .driver = &agp_i810_sb_driver
559 .name = "SandyBridge server IG",
560 .driver = &agp_i810_sb_driver
564 .name = "IvyBridge desktop GT1 IG",
565 .driver = &agp_i810_sb_driver
569 .name = "IvyBridge desktop GT2 IG",
570 .driver = &agp_i810_sb_driver
574 .name = "IvyBridge mobile GT1 IG",
575 .driver = &agp_i810_sb_driver
579 .name = "IvyBridge mobile GT2 IG",
580 .driver = &agp_i810_sb_driver
584 .name = "IvyBridge server GT1 IG",
585 .driver = &agp_i810_sb_driver
589 .name = "IvyBridge server GT2 IG",
590 .driver = &agp_i810_sb_driver
594 .name = "ValleyView",
595 .driver = &valleyview_gtt_driver
599 .name = "Haswell desktop GT1 IG",
600 .driver = &agp_i810_sb_driver
604 .name = "Haswell desktop GT2 IG",
605 .driver = &agp_i810_sb_driver
607 { 0x041e, "Haswell", &agp_i810_sb_driver },
608 { 0x0422, "Haswell", &agp_i810_sb_driver },
611 .name = "Haswell mobile GT1 IG",
612 .driver = &agp_i810_sb_driver
616 .name = "Haswell mobile GT2 IG",
617 .driver = &agp_i810_sb_driver
619 { 0x0426, "Haswell", &agp_i810_sb_driver },
622 .name = "Haswell server GT1 IG",
623 .driver = &agp_i810_sb_driver
627 .name = "Haswell server GT2 IG",
628 .driver = &agp_i810_sb_driver
630 { 0x042a, "Haswell", &agp_i810_sb_driver },
631 { 0x0c02, "Haswell", &agp_i810_sb_driver },
632 { 0x0c12, "Haswell", &agp_i810_sb_driver },
633 { 0x0c22, "Haswell", &agp_i810_sb_driver },
634 { 0x0c06, "Haswell", &agp_i810_sb_driver },
637 .name = "Haswell SDV",
638 .driver = &agp_i810_sb_driver
640 { 0x0c26, "Haswell", &agp_i810_sb_driver },
641 { 0x0c0a, "Haswell", &agp_i810_sb_driver },
642 { 0x0c1a, "Haswell", &agp_i810_sb_driver },
643 { 0x0c2a, "Haswell", &agp_i810_sb_driver },
644 { 0x0a02, "Haswell", &agp_i810_sb_driver },
645 { 0x0a12, "Haswell", &agp_i810_sb_driver },
646 { 0x0a22, "Haswell", &agp_i810_sb_driver },
647 { 0x0a06, "Haswell", &agp_i810_sb_driver },
648 { 0x0a16, "Haswell", &agp_i810_sb_driver },
649 { 0x0a26, "Haswell", &agp_i810_sb_driver },
650 { 0x0a0a, "Haswell", &agp_i810_sb_driver },
651 { 0x0a1a, "Haswell", &agp_i810_sb_driver },
652 { 0x0a2a, "Haswell", &agp_i810_sb_driver },
653 { 0x0d12, "Haswell", &agp_i810_sb_driver },
654 { 0x0d22, "Haswell", &agp_i810_sb_driver },
655 { 0x0d32, "Haswell", &agp_i810_sb_driver },
656 { 0x0d16, "Haswell", &agp_i810_sb_driver },
657 { 0x0d26, "Haswell", &agp_i810_sb_driver },
658 { 0x0d36, "Haswell", &agp_i810_sb_driver },
659 { 0x0d1a, "Haswell", &agp_i810_sb_driver },
660 { 0x0d2a, "Haswell", &agp_i810_sb_driver },
661 { 0x0d3a, "Haswell", &agp_i810_sb_driver },
663 { 0x1602, "Broadwell", &broadwell_gtt_driver }, /* m */
664 { 0x1606, "Broadwell", &broadwell_gtt_driver },
665 { 0x160B, "Broadwell", &broadwell_gtt_driver },
666 { 0x160E, "Broadwell", &broadwell_gtt_driver },
667 { 0x1616, "Broadwell", &broadwell_gtt_driver },
668 { 0x161E, "Broadwell", &broadwell_gtt_driver },
670 { 0x160A, "Broadwell", &broadwell_gtt_driver }, /* d */
671 { 0x160D, "Broadwell", &broadwell_gtt_driver },
677 static const struct agp_i810_match*
678 agp_i810_match(device_t dev)
682 if (pci_get_vendor(dev) != PCI_VENDOR_INTEL)
685 devid = pci_get_device(dev);
686 for (i = 0; agp_i810_matches[i].devid != 0; i++) {
687 if (agp_i810_matches[i].devid == devid)
690 if (agp_i810_matches[i].devid == 0)
693 return (&agp_i810_matches[i]);
697 * Find bridge device.
700 agp_i810_find_bridge(device_t dev)
703 return (pci_find_dbsf(0, 0, 0, 0));
707 agp_i810_identify(driver_t *driver, device_t parent)
710 if (device_find_child(parent, "agp", -1) == NULL &&
711 agp_i810_match(parent))
712 device_add_child(parent, "agp", -1);
716 agp_i915_check_active(device_t bridge_dev)
720 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
721 if ((deven & AGP_I915_DEVEN_D2F0) == AGP_I915_DEVEN_D2F0_DISABLED)
727 agp_sb_check_active(device_t bridge_dev)
731 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
732 if ((deven & AGP_SB_DEVEN_D2EN) == AGP_SB_DEVEN_D2EN_DISABLED)
738 agp_i810_set_desc(device_t dev, const struct agp_i810_match *match)
741 device_set_desc(dev, match->name);
745 agp_i810_probe(device_t dev)
748 const struct agp_i810_match *match;
751 if (resource_disabled("agp", device_get_unit(dev)))
753 match = agp_i810_match(dev);
757 bdev = agp_i810_find_bridge(dev);
760 kprintf("I810: can't find bridge device\n");
765 * checking whether internal graphics device has been activated.
767 err = match->driver->check_active(bdev);
770 kprintf("i810: disabled, not probing\n");
774 match->driver->set_desc(dev, match);
775 return (BUS_PROBE_DEFAULT);
779 agp_i915_dump_regs(device_t dev)
781 struct agp_i810_softc *sc = device_get_softc(dev);
783 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
784 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
785 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
786 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
787 device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
788 pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
792 agp_i965_dump_regs(device_t dev)
794 struct agp_i810_softc *sc = device_get_softc(dev);
796 device_printf(dev, "AGP_I965_PGTBL_CTL2: %08x\n",
797 bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2));
798 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
799 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
800 device_printf(dev, "AGP_I965_MSAC: 0x%02x\n",
801 pci_read_config(sc->bdev, AGP_I965_MSAC, 1));
805 agp_sb_dump_regs(device_t dev)
807 struct agp_i810_softc *sc = device_get_softc(dev);
809 device_printf(dev, "AGP_SNB_GFX_MODE: %08x\n",
810 bus_read_4(sc->sc_res[0], AGP_SNB_GFX_MODE));
811 device_printf(dev, "AGP_SNB_GCC1: 0x%04x\n",
812 pci_read_config(sc->bdev, AGP_SNB_GCC1, 2));
816 agp_i915_get_stolen_size(device_t dev)
818 struct agp_i810_softc *sc;
819 unsigned int gcc1, stolen, gtt_size;
821 sc = device_get_softc(dev);
824 * Stolen memory is set up at the beginning of the aperture by
825 * the BIOS, consisting of the GATT followed by 4kb for the
828 switch (sc->match->driver->chiptype) {
836 switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
837 AGP_I810_PGTBL_SIZE_MASK) {
838 case AGP_I810_PGTBL_SIZE_128KB:
841 case AGP_I810_PGTBL_SIZE_256KB:
844 case AGP_I810_PGTBL_SIZE_512KB:
847 case AGP_I965_PGTBL_SIZE_1MB:
850 case AGP_I965_PGTBL_SIZE_2MB:
853 case AGP_I965_PGTBL_SIZE_1_5MB:
854 gtt_size = 1024 + 512;
857 device_printf(dev, "Bad PGTBL size\n");
862 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2);
863 switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) {
864 case AGP_G33_MGGC_GGMS_SIZE_1M:
867 case AGP_G33_MGGC_GGMS_SIZE_2M:
871 device_printf(dev, "Bad PGTBL size\n");
880 device_printf(dev, "Bad chiptype\n");
884 /* GCC1 is called MGGC on i915+ */
885 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
886 switch (gcc1 & AGP_I855_GCC1_GMS) {
887 case AGP_I855_GCC1_GMS_STOLEN_1M:
890 case AGP_I855_GCC1_GMS_STOLEN_4M:
893 case AGP_I855_GCC1_GMS_STOLEN_8M:
896 case AGP_I855_GCC1_GMS_STOLEN_16M:
899 case AGP_I855_GCC1_GMS_STOLEN_32M:
902 case AGP_I915_GCC1_GMS_STOLEN_48M:
903 stolen = sc->match->driver->gen > 2 ? 48 * 1024 : 0;
905 case AGP_I915_GCC1_GMS_STOLEN_64M:
906 stolen = sc->match->driver->gen > 2 ? 64 * 1024 : 0;
908 case AGP_G33_GCC1_GMS_STOLEN_128M:
909 stolen = sc->match->driver->gen > 2 ? 128 * 1024 : 0;
911 case AGP_G33_GCC1_GMS_STOLEN_256M:
912 stolen = sc->match->driver->gen > 2 ? 256 * 1024 : 0;
914 case AGP_G4X_GCC1_GMS_STOLEN_96M:
915 if (sc->match->driver->chiptype == CHIP_I965 ||
916 sc->match->driver->chiptype == CHIP_G4X)
921 case AGP_G4X_GCC1_GMS_STOLEN_160M:
922 if (sc->match->driver->chiptype == CHIP_I965 ||
923 sc->match->driver->chiptype == CHIP_G4X)
928 case AGP_G4X_GCC1_GMS_STOLEN_224M:
929 if (sc->match->driver->chiptype == CHIP_I965 ||
930 sc->match->driver->chiptype == CHIP_G4X)
935 case AGP_G4X_GCC1_GMS_STOLEN_352M:
936 if (sc->match->driver->chiptype == CHIP_I965 ||
937 sc->match->driver->chiptype == CHIP_G4X)
944 "unknown memory configuration, disabling (GCC1 %x)\n",
950 sc->stolen_size = stolen * 1024;
951 sc->stolen = (stolen - gtt_size) * 1024 / 4096;
957 agp_sb_get_stolen_size(device_t dev)
959 struct agp_i810_softc *sc;
962 sc = device_get_softc(dev);
963 gmch_ctl = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
964 switch (gmch_ctl & AGP_SNB_GMCH_GMS_STOLEN_MASK) {
965 case AGP_SNB_GMCH_GMS_STOLEN_32M:
966 sc->stolen_size = 32 * 1024 * 1024;
968 case AGP_SNB_GMCH_GMS_STOLEN_64M:
969 sc->stolen_size = 64 * 1024 * 1024;
971 case AGP_SNB_GMCH_GMS_STOLEN_96M:
972 sc->stolen_size = 96 * 1024 * 1024;
974 case AGP_SNB_GMCH_GMS_STOLEN_128M:
975 sc->stolen_size = 128 * 1024 * 1024;
977 case AGP_SNB_GMCH_GMS_STOLEN_160M:
978 sc->stolen_size = 160 * 1024 * 1024;
980 case AGP_SNB_GMCH_GMS_STOLEN_192M:
981 sc->stolen_size = 192 * 1024 * 1024;
983 case AGP_SNB_GMCH_GMS_STOLEN_224M:
984 sc->stolen_size = 224 * 1024 * 1024;
986 case AGP_SNB_GMCH_GMS_STOLEN_256M:
987 sc->stolen_size = 256 * 1024 * 1024;
989 case AGP_SNB_GMCH_GMS_STOLEN_288M:
990 sc->stolen_size = 288 * 1024 * 1024;
992 case AGP_SNB_GMCH_GMS_STOLEN_320M:
993 sc->stolen_size = 320 * 1024 * 1024;
995 case AGP_SNB_GMCH_GMS_STOLEN_352M:
996 sc->stolen_size = 352 * 1024 * 1024;
998 case AGP_SNB_GMCH_GMS_STOLEN_384M:
999 sc->stolen_size = 384 * 1024 * 1024;
1001 case AGP_SNB_GMCH_GMS_STOLEN_416M:
1002 sc->stolen_size = 416 * 1024 * 1024;
1004 case AGP_SNB_GMCH_GMS_STOLEN_448M:
1005 sc->stolen_size = 448 * 1024 * 1024;
1007 case AGP_SNB_GMCH_GMS_STOLEN_480M:
1008 sc->stolen_size = 480 * 1024 * 1024;
1010 case AGP_SNB_GMCH_GMS_STOLEN_512M:
1011 sc->stolen_size = 512 * 1024 * 1024;
1014 sc->stolen = (sc->stolen_size - 4) / 4096;
1019 agp_gen8_get_stolen_size(device_t dev)
1021 struct agp_i810_softc *sc;
1025 sc = device_get_softc(dev);
1026 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1027 v = (gcc1 >> 8) & 0xFF;
1028 sc->stolen_size = v * (32L * 1024 * 1024); /* 32MB increments */
1029 kprintf("GTT STOLEN %ld\n", (long)sc->stolen_size);
1035 agp_i915_get_gtt_mappable_entries(device_t dev)
1037 struct agp_i810_softc *sc;
1040 sc = device_get_softc(dev);
1041 ap = AGP_GET_APERTURE(dev);
1042 sc->gtt_mappable_entries = ap >> AGP_PAGE_SHIFT;
1047 agp_i810_get_gtt_total_entries(device_t dev)
1049 struct agp_i810_softc *sc;
1051 sc = device_get_softc(dev);
1052 sc->gtt_total_entries = sc->gtt_mappable_entries;
1057 agp_i965_get_gtt_total_entries(device_t dev)
1059 struct agp_i810_softc *sc;
1060 uint32_t pgetbl_ctl;
1063 sc = device_get_softc(dev);
1065 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1066 switch (pgetbl_ctl & AGP_I810_PGTBL_SIZE_MASK) {
1067 case AGP_I810_PGTBL_SIZE_128KB:
1068 sc->gtt_total_entries = 128 * 1024 / 4;
1070 case AGP_I810_PGTBL_SIZE_256KB:
1071 sc->gtt_total_entries = 256 * 1024 / 4;
1073 case AGP_I810_PGTBL_SIZE_512KB:
1074 sc->gtt_total_entries = 512 * 1024 / 4;
1076 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
1077 case AGP_I810_PGTBL_SIZE_1MB:
1078 sc->gtt_total_entries = 1024 * 1024 / 4;
1080 case AGP_I810_PGTBL_SIZE_2MB:
1081 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
1083 case AGP_I810_PGTBL_SIZE_1_5MB:
1084 sc->gtt_total_entries = (1024 + 512) * 1024 / 4;
1087 device_printf(dev, "Unknown page table size\n");
1094 agp_gen5_adjust_pgtbl_size(device_t dev, uint32_t sz)
1096 struct agp_i810_softc *sc;
1097 uint32_t pgetbl_ctl, pgetbl_ctl2;
1099 sc = device_get_softc(dev);
1101 /* Disable per-process page table. */
1102 pgetbl_ctl2 = bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2);
1103 pgetbl_ctl2 &= ~AGP_I810_PGTBL_ENABLED;
1104 bus_write_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2, pgetbl_ctl2);
1106 /* Write the new ggtt size. */
1107 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1108 pgetbl_ctl &= ~AGP_I810_PGTBL_SIZE_MASK;
1110 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgetbl_ctl);
1114 agp_gen5_get_gtt_total_entries(device_t dev)
1116 struct agp_i810_softc *sc;
1119 sc = device_get_softc(dev);
1121 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
1122 switch (gcc1 & AGP_G4x_GCC1_SIZE_MASK) {
1123 case AGP_G4x_GCC1_SIZE_1M:
1124 case AGP_G4x_GCC1_SIZE_VT_1M:
1125 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1MB);
1127 case AGP_G4x_GCC1_SIZE_VT_1_5M:
1128 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1_5MB);
1130 case AGP_G4x_GCC1_SIZE_2M:
1131 case AGP_G4x_GCC1_SIZE_VT_2M:
1132 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_2MB);
1135 device_printf(dev, "Unknown page table size\n");
1139 return (agp_i965_get_gtt_total_entries(dev));
1143 agp_sb_get_gtt_total_entries(device_t dev)
1145 struct agp_i810_softc *sc;
1148 sc = device_get_softc(dev);
1150 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1151 switch (gcc1 & AGP_SNB_GTT_SIZE_MASK) {
1153 case AGP_SNB_GTT_SIZE_0M:
1154 kprintf("Bad GTT size mask: 0x%04x\n", gcc1);
1156 case AGP_SNB_GTT_SIZE_1M:
1157 sc->gtt_total_entries = 1024 * 1024 / 4;
1159 case AGP_SNB_GTT_SIZE_2M:
1160 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
1167 agp_gen8_get_gtt_total_entries(device_t dev)
1169 struct agp_i810_softc *sc;
1173 sc = device_get_softc(dev);
1175 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1176 v = (gcc1 >> 6) & 3;
1179 sc->gtt_total_entries = (v << 20) / 8;
1182 * XXX limit to 2GB due to misc integer overflows calculated on
1185 while ((long)sc->gtt_total_entries * PAGE_SIZE >= 4LL*1024*1024*1024)
1186 sc->gtt_total_entries >>= 1;
1188 kprintf("GTT SIZE %ld representing %ldM vmap\n",
1189 (long)sc->gtt_total_entries * 8,
1190 (long)sc->gtt_total_entries * PAGE_SIZE);
1196 agp_i830_install_gatt(device_t dev)
1198 struct agp_i810_softc *sc;
1201 sc = device_get_softc(dev);
1204 * The i830 automatically initializes the 128k gatt on boot.
1205 * GATT address is already in there, make sure it's enabled.
1207 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1209 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
1211 sc->gatt->ag_physical = pgtblctl & ~1;
1216 agp_i810_attach(device_t dev)
1218 struct agp_i810_softc *sc;
1221 sc = device_get_softc(dev);
1222 sc->bdev = agp_i810_find_bridge(dev);
1223 if (sc->bdev == NULL)
1226 sc->match = agp_i810_match(dev);
1228 agp_set_aperture_resource(dev, sc->match->driver->gen <= 2 ?
1229 AGP_APBASE : AGP_I915_GMADR);
1230 error = agp_generic_attach(dev);
1234 if (ptoa((vm_paddr_t)Maxmem) >
1235 (1ULL << sc->match->driver->busdma_addr_mask_sz) - 1) {
1236 device_printf(dev, "agp_i810 does not support physical "
1237 "memory above %ju.\n", (uintmax_t)(1ULL <<
1238 sc->match->driver->busdma_addr_mask_sz) - 1);
1242 if (bus_alloc_resources(dev, sc->match->driver->res_spec, sc->sc_res)) {
1243 agp_generic_detach(dev);
1247 sc->initial_aperture = AGP_GET_APERTURE(dev);
1248 sc->gatt = kmalloc(sizeof(struct agp_gatt), M_AGP, M_WAITOK);
1249 sc->gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
1251 if ((error = sc->match->driver->get_stolen_size(dev)) != 0 ||
1252 (error = sc->match->driver->install_gatt(dev)) != 0 ||
1253 (error = sc->match->driver->get_gtt_mappable_entries(dev)) != 0 ||
1254 (error = sc->match->driver->get_gtt_total_entries(dev)) != 0 ||
1255 (error = sc->match->driver->chipset_flush_setup(dev)) != 0) {
1256 bus_release_resources(dev, sc->match->driver->res_spec,
1258 kfree(sc->gatt, M_AGP);
1259 agp_generic_detach(dev);
1264 device_printf(dev, "aperture size is %dM",
1265 sc->initial_aperture / 1024 / 1024);
1267 kprintf(", detected %dk stolen memory\n", sc->stolen * 4);
1271 sc->match->driver->dump_regs(dev);
1272 device_printf(dev, "Mappable GTT entries: %d\n",
1273 sc->gtt_mappable_entries);
1274 device_printf(dev, "Total GTT entries: %d\n",
1275 sc->gtt_total_entries);
1281 agp_i830_deinstall_gatt(device_t dev)
1283 struct agp_i810_softc *sc;
1284 unsigned int pgtblctl;
1286 sc = device_get_softc(dev);
1287 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1289 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
1293 agp_i810_detach(device_t dev)
1295 struct agp_i810_softc *sc;
1297 sc = device_get_softc(dev);
1300 /* Clear the GATT base. */
1301 sc->match->driver->deinstall_gatt(dev);
1303 sc->match->driver->chipset_flush_teardown(dev);
1305 /* Put the aperture back the way it started. */
1306 AGP_SET_APERTURE(dev, sc->initial_aperture);
1308 kfree(sc->gatt, M_AGP);
1309 bus_release_resources(dev, sc->match->driver->res_spec, sc->sc_res);
1316 agp_i810_resume(device_t dev)
1318 struct agp_i810_softc *sc;
1319 sc = device_get_softc(dev);
1321 AGP_SET_APERTURE(dev, sc->initial_aperture);
1323 /* Install the GATT. */
1324 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
1325 sc->gatt->ag_physical | 1);
1327 return (bus_generic_resume(dev));
1331 * Sets the PCI resource size of the aperture on i830-class and below chipsets,
1332 * while returning failure on later chipsets when an actual change is
1335 * This whole function is likely bogus, as the kernel would probably need to
1336 * reconfigure the placement of the AGP aperture if a larger size is requested,
1337 * which doesn't happen currently.
1341 agp_i915_set_aperture(device_t dev, u_int32_t aperture)
1344 return (agp_generic_set_aperture(dev, aperture));
1348 agp_i810_method_set_aperture(device_t dev, u_int32_t aperture)
1350 struct agp_i810_softc *sc;
1352 sc = device_get_softc(dev);
1353 return (sc->match->driver->set_aperture(dev, aperture));
1357 * Writes a GTT entry mapping the page at the given offset from the
1358 * beginning of the aperture to the given physical address. Setup the
1359 * caching mode according to flags.
1361 * For gen 1, 2 and 3, GTT start is located at AGP_I810_GTT offset
1362 * from corresponding BAR start. For gen 4, offset is 512KB +
1363 * AGP_I810_GTT, for gen 5 and 6 it is 2MB + AGP_I810_GTT.
1365 * Also, the bits of the physical page address above 4GB needs to be
1366 * placed into bits 40-32 of PTE.
1369 agp_i915_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1374 pte = (u_int32_t)physical | I810_PTE_VALID;
1375 if (flags == AGP_USER_CACHED_MEMORY)
1376 pte |= I830_PTE_SYSTEM_CACHED;
1378 agp_i915_write_gtt(dev, index, pte);
1382 agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte)
1384 struct agp_i810_softc *sc;
1386 sc = device_get_softc(dev);
1387 bus_write_4(sc->sc_res[1], index * 4, pte);
1391 agp_i965_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1396 pte = (u_int32_t)physical | I810_PTE_VALID;
1397 if (flags == AGP_USER_CACHED_MEMORY)
1398 pte |= I830_PTE_SYSTEM_CACHED;
1400 pte |= (physical >> 28) & 0xf0;
1401 agp_i965_write_gtt(dev, index, pte);
1405 agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte)
1407 struct agp_i810_softc *sc;
1409 sc = device_get_softc(dev);
1410 bus_write_4(sc->sc_res[0], index * 4 + (512 * 1024), pte);
1414 agp_g4x_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1419 pte = (u_int32_t)physical | I810_PTE_VALID;
1420 if (flags == AGP_USER_CACHED_MEMORY)
1421 pte |= I830_PTE_SYSTEM_CACHED;
1423 pte |= (physical >> 28) & 0xf0;
1424 agp_g4x_write_gtt(dev, index, pte);
1428 agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte)
1430 struct agp_i810_softc *sc;
1432 sc = device_get_softc(dev);
1433 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1437 agp_sb_install_gtt_pte(device_t dev, u_int index,
1438 vm_offset_t physical, int flags)
1440 int type_mask, gfdt;
1443 pte = (u_int32_t)physical | I810_PTE_VALID;
1444 type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1445 gfdt = (flags & AGP_USER_CACHED_MEMORY_GFDT) != 0 ? GEN6_PTE_GFDT : 0;
1447 if (type_mask == AGP_USER_MEMORY)
1448 pte |= GEN6_PTE_UNCACHED;
1449 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
1450 pte |= GEN6_PTE_LLC_MLC | gfdt;
1452 pte |= GEN6_PTE_LLC | gfdt;
1454 pte |= (physical & 0x000000ff00000000ull) >> 28;
1455 agp_sb_write_gtt(dev, index, pte);
1459 agp_gen8_install_gtt_pte(device_t dev, u_int index,
1460 vm_offset_t physical, int flags)
1462 struct agp_i810_softc *sc;
1466 pte = (u_int64_t)physical | GEN8_PTE_PRESENT | GEN8_PTE_RW;
1467 type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1469 if (type_mask == AGP_USER_MEMORY)
1470 pte |= GEN8_PTE_PWT; /* XXX */
1471 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
1472 pte |= GEN8_PTE_PWT; /* XXX */
1474 pte |= GEN8_PTE_PWT; /* XXX */
1476 sc = device_get_softc(dev);
1477 bus_write_4(sc->sc_res[0], index * 8 + (2 * 1024 * 1024),
1479 bus_write_4(sc->sc_res[0], index * 8 + (2 * 1024 * 1024) + 4,
1480 (uint32_t)(pte >> 32));
1484 agp_sb_write_gtt(device_t dev, u_int index, uint32_t pte)
1486 struct agp_i810_softc *sc;
1488 sc = device_get_softc(dev);
1489 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1493 agp_i810_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
1495 struct agp_i810_softc *sc = device_get_softc(dev);
1498 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
1499 device_printf(dev, "failed: offset is 0x%08jx, "
1500 "shift is %d, entries is %d\n", (intmax_t)offset,
1501 AGP_PAGE_SHIFT, sc->gatt->ag_entries);
1504 index = offset >> AGP_PAGE_SHIFT;
1505 if (sc->stolen != 0 && index < sc->stolen) {
1506 device_printf(dev, "trying to bind into stolen memory\n");
1509 sc->match->driver->install_gtt_pte(dev, index, physical, 0);
1514 agp_i810_unbind_page(device_t dev, vm_offset_t offset)
1516 struct agp_i810_softc *sc;
1519 sc = device_get_softc(dev);
1520 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
1522 index = offset >> AGP_PAGE_SHIFT;
1523 if (sc->stolen != 0 && index < sc->stolen) {
1524 device_printf(dev, "trying to unbind from stolen memory\n");
1527 sc->match->driver->install_gtt_pte(dev, index, 0, 0);
1532 agp_i915_sync_gtt_pte(device_t dev, u_int index)
1534 struct agp_i810_softc *sc;
1536 sc = device_get_softc(dev);
1537 bus_read_4(sc->sc_res[1], index * 4);
1541 agp_i965_sync_gtt_pte(device_t dev, u_int index)
1543 struct agp_i810_softc *sc;
1545 sc = device_get_softc(dev);
1546 bus_read_4(sc->sc_res[0], index * 4 + (512 * 1024));
1550 agp_g4x_sync_gtt_pte(device_t dev, u_int index)
1552 struct agp_i810_softc *sc;
1554 sc = device_get_softc(dev);
1555 bus_read_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024));
1559 * Writing via memory mapped registers already flushes all TLBs.
1562 agp_i810_flush_tlb(device_t dev)
1567 agp_i810_enable(device_t dev, u_int32_t mode)
1573 static struct agp_memory *
1574 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
1576 struct agp_i810_softc *sc;
1577 struct agp_memory *mem;
1580 sc = device_get_softc(dev);
1582 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1583 sc->agp.as_allocated + size > sc->agp.as_maxmem)
1588 * Mapping local DRAM into GATT.
1590 if (sc->match->driver->chiptype != CHIP_I810)
1592 if (size != sc->dcache_size)
1594 } else if (type == 2) {
1596 * Type 2 is the contiguous physical memory type, that hands
1597 * back a physical address. This is used for cursors on i810.
1598 * Hand back as many single pages with physical as the user
1599 * wants, but only allow one larger allocation (ARGB cursor)
1602 if (size != AGP_PAGE_SIZE) {
1603 if (sc->argb_cursor != NULL)
1606 /* Allocate memory for ARGB cursor, if we can. */
1607 sc->argb_cursor = contigmalloc(size, M_AGP,
1608 0, 0, ~0, PAGE_SIZE, 0);
1609 if (sc->argb_cursor == NULL)
1614 mem = kmalloc(sizeof *mem, M_AGP, M_INTWAIT);
1615 mem->am_id = sc->agp.as_nextid++;
1616 mem->am_size = size;
1617 mem->am_type = type;
1618 if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
1619 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
1620 atop(round_page(size)));
1625 if (size == AGP_PAGE_SIZE) {
1627 * Allocate and wire down the page now so that we can
1628 * get its physical address.
1630 VM_OBJECT_LOCK(mem->am_obj);
1631 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NORMAL |
1635 VM_OBJECT_UNLOCK(mem->am_obj);
1636 mem->am_physical = VM_PAGE_TO_PHYS(m);
1639 /* Our allocation is already nicely wired down for us.
1640 * Just grab the physical address.
1642 mem->am_physical = vtophys(sc->argb_cursor);
1645 mem->am_physical = 0;
1648 mem->am_is_bound = 0;
1649 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
1650 sc->agp.as_allocated += size;
1656 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
1658 struct agp_i810_softc *sc;
1660 if (mem->am_is_bound)
1663 sc = device_get_softc(dev);
1665 if (mem->am_type == 2) {
1666 if (mem->am_size == AGP_PAGE_SIZE) {
1668 * Unwire the page which we wired in alloc_memory.
1672 vm_object_hold(mem->am_obj);
1673 m = vm_page_lookup_busy_wait(mem->am_obj, 0,
1675 vm_object_drop(mem->am_obj);
1676 vm_page_unwire(m, 0);
1679 contigfree(sc->argb_cursor, mem->am_size, M_AGP);
1680 sc->argb_cursor = NULL;
1684 sc->agp.as_allocated -= mem->am_size;
1685 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
1687 vm_object_deallocate(mem->am_obj);
1693 agp_i810_bind_memory(device_t dev, struct agp_memory *mem, vm_offset_t offset)
1695 struct agp_i810_softc *sc;
1698 /* Do some sanity checks first. */
1699 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
1700 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
1701 device_printf(dev, "binding memory at bad offset %#x\n",
1706 sc = device_get_softc(dev);
1707 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1708 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1709 if (mem->am_is_bound) {
1710 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1713 /* The memory's already wired down, just stick it in the GTT. */
1714 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1715 sc->match->driver->install_gtt_pte(dev, (offset + i) >>
1716 AGP_PAGE_SHIFT, mem->am_physical + i, 0);
1719 mem->am_offset = offset;
1720 mem->am_is_bound = 1;
1721 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1725 if (mem->am_type != 1)
1726 return (agp_generic_bind_memory(dev, mem, offset));
1729 * Mapping local DRAM into GATT.
1731 if (sc->match->driver->chiptype != CHIP_I810)
1733 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
1734 bus_write_4(sc->sc_res[0],
1735 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
1741 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
1743 struct agp_i810_softc *sc;
1746 sc = device_get_softc(dev);
1748 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1749 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1750 if (!mem->am_is_bound) {
1751 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1755 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1756 sc->match->driver->install_gtt_pte(dev,
1757 (mem->am_offset + i) >> AGP_PAGE_SHIFT, 0, 0);
1760 mem->am_is_bound = 0;
1761 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1765 if (mem->am_type != 1)
1766 return (agp_generic_unbind_memory(dev, mem));
1768 if (sc->match->driver->chiptype != CHIP_I810)
1770 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1771 sc->match->driver->install_gtt_pte(dev, i >> AGP_PAGE_SHIFT,
1777 static device_method_t agp_i810_methods[] = {
1778 /* Device interface */
1779 DEVMETHOD(device_identify, agp_i810_identify),
1780 DEVMETHOD(device_probe, agp_i810_probe),
1781 DEVMETHOD(device_attach, agp_i810_attach),
1782 DEVMETHOD(device_detach, agp_i810_detach),
1783 DEVMETHOD(device_suspend, bus_generic_suspend),
1784 DEVMETHOD(device_resume, agp_i810_resume),
1787 DEVMETHOD(agp_get_aperture, agp_generic_get_aperture),
1788 DEVMETHOD(agp_set_aperture, agp_i810_method_set_aperture),
1789 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
1790 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
1791 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
1792 DEVMETHOD(agp_enable, agp_i810_enable),
1793 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
1794 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
1795 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
1796 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
1797 DEVMETHOD(agp_chipset_flush, agp_intel_gtt_chipset_flush),
1802 static driver_t agp_i810_driver = {
1805 sizeof(struct agp_i810_softc),
1808 static devclass_t agp_devclass;
1810 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, NULL, NULL);
1811 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1812 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
1814 extern vm_page_t bogus_page;
1817 agp_intel_gtt_clear_range(device_t dev, u_int first_entry, u_int num_entries)
1819 struct agp_i810_softc *sc;
1822 sc = device_get_softc(dev);
1823 for (i = 0; i < num_entries; i++)
1824 sc->match->driver->install_gtt_pte(dev, first_entry + i,
1825 VM_PAGE_TO_PHYS(bogus_page), 0);
1826 sc->match->driver->sync_gtt_pte(dev, first_entry + num_entries - 1);
1830 agp_intel_gtt_insert_pages(device_t dev, u_int first_entry, u_int num_entries,
1831 vm_page_t *pages, u_int flags)
1833 struct agp_i810_softc *sc;
1836 sc = device_get_softc(dev);
1837 for (i = 0; i < num_entries; i++) {
1838 KKASSERT(pages[i]->valid == VM_PAGE_BITS_ALL);
1839 KKASSERT(pages[i]->wire_count > 0);
1840 sc->match->driver->install_gtt_pte(dev, first_entry + i,
1841 VM_PAGE_TO_PHYS(pages[i]), flags);
1843 sc->match->driver->sync_gtt_pte(dev, first_entry + num_entries - 1);
1847 agp_intel_gtt_get(device_t dev)
1849 struct agp_i810_softc *sc;
1850 struct intel_gtt res;
1852 sc = device_get_softc(dev);
1853 res.stolen_size = sc->stolen_size;
1854 res.gtt_total_entries = sc->gtt_total_entries;
1855 res.gtt_mappable_entries = sc->gtt_mappable_entries;
1856 res.do_idle_maps = 0;
1857 res.scratch_page_dma = VM_PAGE_TO_PHYS(bogus_page);
1862 agp_i810_chipset_flush_setup(device_t dev)
1869 agp_i810_chipset_flush_teardown(device_t dev)
1872 /* Nothing to do. */
1876 agp_i810_chipset_flush(device_t dev)
1879 /* Nothing to do. */
1883 agp_i915_chipset_flush_alloc_page(device_t dev, uint64_t start, uint64_t end)
1885 struct agp_i810_softc *sc;
1888 sc = device_get_softc(dev);
1889 vga = device_get_parent(dev);
1890 sc->sc_flush_page_rid = 100;
1891 sc->sc_flush_page_res = BUS_ALLOC_RESOURCE(device_get_parent(vga), dev,
1892 SYS_RES_MEMORY, &sc->sc_flush_page_rid, start, end, PAGE_SIZE,
1894 if (sc->sc_flush_page_res == NULL) {
1895 device_printf(dev, "Failed to allocate flush page at 0x%jx\n",
1899 sc->sc_flush_page_vaddr = rman_get_virtual(sc->sc_flush_page_res);
1901 device_printf(dev, "Allocated flush page phys 0x%jx virt %p\n",
1902 (uintmax_t)rman_get_start(sc->sc_flush_page_res),
1903 sc->sc_flush_page_vaddr);
1909 agp_i915_chipset_flush_free_page(device_t dev)
1911 struct agp_i810_softc *sc;
1914 sc = device_get_softc(dev);
1915 vga = device_get_parent(dev);
1916 if (sc->sc_flush_page_res == NULL)
1918 BUS_DEACTIVATE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
1919 sc->sc_flush_page_rid, sc->sc_flush_page_res);
1920 BUS_RELEASE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
1921 sc->sc_flush_page_rid, sc->sc_flush_page_res);
1925 agp_i915_chipset_flush_setup(device_t dev)
1927 struct agp_i810_softc *sc;
1931 sc = device_get_softc(dev);
1932 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
1933 if ((temp & 1) != 0) {
1937 "Found already configured flush page at 0x%jx\n",
1939 sc->sc_bios_allocated_flush_page = 1;
1941 * In the case BIOS initialized the flush pointer (?)
1942 * register, expect that BIOS also set up the resource
1945 error = agp_i915_chipset_flush_alloc_page(dev, temp,
1946 temp + PAGE_SIZE - 1);
1950 sc->sc_bios_allocated_flush_page = 0;
1951 error = agp_i915_chipset_flush_alloc_page(dev, 0, 0xffffffff);
1954 temp = rman_get_start(sc->sc_flush_page_res);
1955 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp | 1, 4);
1961 agp_i915_chipset_flush_teardown(device_t dev)
1963 struct agp_i810_softc *sc;
1966 sc = device_get_softc(dev);
1967 if (sc->sc_flush_page_res == NULL)
1969 if (!sc->sc_bios_allocated_flush_page) {
1970 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
1972 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp, 4);
1974 agp_i915_chipset_flush_free_page(dev);
1978 agp_i965_chipset_flush_setup(device_t dev)
1980 struct agp_i810_softc *sc;
1982 uint32_t temp_hi, temp_lo;
1985 sc = device_get_softc(dev);
1987 temp_hi = pci_read_config(sc->bdev, AGP_I965_IFPADDR + 4, 4);
1988 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
1990 if ((temp_lo & 1) != 0) {
1991 temp = ((uint64_t)temp_hi << 32) | (temp_lo & ~1);
1994 "Found already configured flush page at 0x%jx\n",
1996 sc->sc_bios_allocated_flush_page = 1;
1998 * In the case BIOS initialized the flush pointer (?)
1999 * register, expect that BIOS also set up the resource
2002 error = agp_i915_chipset_flush_alloc_page(dev, temp,
2003 temp + PAGE_SIZE - 1);
2007 sc->sc_bios_allocated_flush_page = 0;
2008 error = agp_i915_chipset_flush_alloc_page(dev, 0, ~0);
2011 temp = rman_get_start(sc->sc_flush_page_res);
2012 pci_write_config(sc->bdev, AGP_I965_IFPADDR + 4,
2013 (temp >> 32) & UINT32_MAX, 4);
2014 pci_write_config(sc->bdev, AGP_I965_IFPADDR,
2015 (temp & UINT32_MAX) | 1, 4);
2021 agp_i965_chipset_flush_teardown(device_t dev)
2023 struct agp_i810_softc *sc;
2026 sc = device_get_softc(dev);
2027 if (sc->sc_flush_page_res == NULL)
2029 if (!sc->sc_bios_allocated_flush_page) {
2030 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
2032 pci_write_config(sc->bdev, AGP_I965_IFPADDR, temp_lo, 4);
2034 agp_i915_chipset_flush_free_page(dev);
2038 agp_i915_chipset_flush(device_t dev)
2040 struct agp_i810_softc *sc;
2042 sc = device_get_softc(dev);
2043 *(uint32_t *)sc->sc_flush_page_vaddr = 1;
2047 agp_intel_gtt_chipset_flush(device_t dev)
2049 struct agp_i810_softc *sc;
2051 sc = device_get_softc(dev);
2052 sc->match->driver->chipset_flush(dev);
2057 agp_intel_gtt_unmap_memory(device_t dev, struct sglist *sg_list)
2062 agp_intel_gtt_map_memory(device_t dev, vm_page_t *pages, u_int num_entries,
2063 struct sglist **sg_list)
2066 struct agp_i810_softc *sc;
2075 if (*sg_list != NULL)
2078 sc = device_get_softc(dev);
2080 sg = sglist_alloc(num_entries, M_WAITOK /* XXXKIB */);
2081 for (i = 0; i < num_entries; i++) {
2082 sg->sg_segs[i].ss_paddr = VM_PAGE_TO_PHYS(pages[i]);
2083 sg->sg_segs[i].ss_len = PAGE_SIZE;
2087 error = bus_dma_tag_create(bus_get_dma_tag(dev),
2088 1 /* alignment */, 0 /* boundary */,
2089 1ULL << sc->match->busdma_addr_mask_sz /* lowaddr */,
2090 BUS_SPACE_MAXADDR /* highaddr */,
2091 NULL /* filtfunc */, NULL /* filtfuncarg */,
2092 BUS_SPACE_MAXADDR /* maxsize */,
2093 BUS_SPACE_UNRESTRICTED /* nsegments */,
2094 BUS_SPACE_MAXADDR /* maxsegsz */,
2095 0 /* flags */, NULL /* lockfunc */, NULL /* lockfuncarg */,
2108 agp_intel_gtt_insert_sg_entries(device_t dev, struct sglist *sg_list,
2109 u_int first_entry, u_int flags)
2111 struct agp_i810_softc *sc;
2116 sc = device_get_softc(dev);
2117 for (i = j = 0; j < sg_list->sg_nseg; j++) {
2118 spaddr = sg_list->sg_segs[i].ss_paddr;
2119 slen = sg_list->sg_segs[i].ss_len;
2120 for (; slen > 0; i++) {
2121 sc->match->driver->install_gtt_pte(dev, first_entry + i,
2123 spaddr += AGP_PAGE_SIZE;
2124 slen -= AGP_PAGE_SIZE;
2127 sc->match->driver->sync_gtt_pte(dev, first_entry + i - 1);
2131 intel_gtt_clear_range(u_int first_entry, u_int num_entries)
2134 agp_intel_gtt_clear_range(intel_agp, first_entry, num_entries);
2138 intel_gtt_insert_pages(u_int first_entry, u_int num_entries, vm_page_t *pages,
2142 agp_intel_gtt_insert_pages(intel_agp, first_entry, num_entries,
2146 void intel_gtt_get(size_t *gtt_total, size_t *stolen_size,
2147 phys_addr_t *mappable_base, unsigned long *mappable_end)
2149 intel_private.base = agp_intel_gtt_get(intel_agp);
2151 *gtt_total = intel_private.base.gtt_total_entries << PAGE_SHIFT;
2152 *stolen_size = intel_private.base.stolen_size;
2153 *mappable_base = intel_private.base.gma_bus_addr;
2154 *mappable_end = intel_private.base.gtt_mappable_entries << PAGE_SHIFT;
2158 intel_gtt_chipset_flush(void)
2161 return (agp_intel_gtt_chipset_flush(intel_agp));
2165 intel_gtt_unmap_memory(struct sglist *sg_list)
2168 agp_intel_gtt_unmap_memory(intel_agp, sg_list);
2172 intel_gtt_map_memory(vm_page_t *pages, u_int num_entries,
2173 struct sglist **sg_list)
2176 return (agp_intel_gtt_map_memory(intel_agp, pages, num_entries,
2181 intel_gtt_insert_sg_entries(struct sglist *sg_list, u_int first_entry,
2185 agp_intel_gtt_insert_sg_entries(intel_agp, sg_list, first_entry, flags);
2192 intel_gtt_sync_pte(u_int entry)
2194 struct agp_i810_softc *sc;
2196 sc = device_get_softc(intel_agp);
2197 sc->match->driver->sync_gtt_pte(intel_agp, entry);
2204 intel_gtt_write(u_int entry, uint32_t val)
2206 struct agp_i810_softc *sc;
2208 sc = device_get_softc(intel_agp);
2209 sc->match->driver->write_gtt(intel_agp, entry, val);