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 },
669 { 0x160A, "Broadwell", &broadwell_gtt_driver }, /* d */
670 { 0x160D, "Broadwell", &broadwell_gtt_driver },
676 static const struct agp_i810_match*
677 agp_i810_match(device_t dev)
681 if (pci_get_vendor(dev) != PCI_VENDOR_INTEL)
684 devid = pci_get_device(dev);
685 for (i = 0; agp_i810_matches[i].devid != 0; i++) {
686 if (agp_i810_matches[i].devid == devid)
689 if (agp_i810_matches[i].devid == 0)
692 return (&agp_i810_matches[i]);
696 * Find bridge device.
699 agp_i810_find_bridge(device_t dev)
702 return (pci_find_dbsf(0, 0, 0, 0));
706 agp_i810_identify(driver_t *driver, device_t parent)
709 if (device_find_child(parent, "agp", -1) == NULL &&
710 agp_i810_match(parent))
711 device_add_child(parent, "agp", -1);
715 agp_i915_check_active(device_t bridge_dev)
719 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
720 if ((deven & AGP_I915_DEVEN_D2F0) == AGP_I915_DEVEN_D2F0_DISABLED)
726 agp_sb_check_active(device_t bridge_dev)
730 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
731 if ((deven & AGP_SB_DEVEN_D2EN) == AGP_SB_DEVEN_D2EN_DISABLED)
737 agp_i810_set_desc(device_t dev, const struct agp_i810_match *match)
740 device_set_desc(dev, match->name);
744 agp_i810_probe(device_t dev)
747 const struct agp_i810_match *match;
750 if (resource_disabled("agp", device_get_unit(dev)))
752 match = agp_i810_match(dev);
756 bdev = agp_i810_find_bridge(dev);
759 kprintf("I810: can't find bridge device\n");
764 * checking whether internal graphics device has been activated.
766 err = match->driver->check_active(bdev);
769 kprintf("i810: disabled, not probing\n");
773 match->driver->set_desc(dev, match);
774 return (BUS_PROBE_DEFAULT);
778 agp_i915_dump_regs(device_t dev)
780 struct agp_i810_softc *sc = device_get_softc(dev);
782 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
783 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
784 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
785 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
786 device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
787 pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
791 agp_i965_dump_regs(device_t dev)
793 struct agp_i810_softc *sc = device_get_softc(dev);
795 device_printf(dev, "AGP_I965_PGTBL_CTL2: %08x\n",
796 bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2));
797 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
798 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
799 device_printf(dev, "AGP_I965_MSAC: 0x%02x\n",
800 pci_read_config(sc->bdev, AGP_I965_MSAC, 1));
804 agp_sb_dump_regs(device_t dev)
806 struct agp_i810_softc *sc = device_get_softc(dev);
808 device_printf(dev, "AGP_SNB_GFX_MODE: %08x\n",
809 bus_read_4(sc->sc_res[0], AGP_SNB_GFX_MODE));
810 device_printf(dev, "AGP_SNB_GCC1: 0x%04x\n",
811 pci_read_config(sc->bdev, AGP_SNB_GCC1, 2));
815 agp_i915_get_stolen_size(device_t dev)
817 struct agp_i810_softc *sc;
818 unsigned int gcc1, stolen, gtt_size;
820 sc = device_get_softc(dev);
823 * Stolen memory is set up at the beginning of the aperture by
824 * the BIOS, consisting of the GATT followed by 4kb for the
827 switch (sc->match->driver->chiptype) {
835 switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
836 AGP_I810_PGTBL_SIZE_MASK) {
837 case AGP_I810_PGTBL_SIZE_128KB:
840 case AGP_I810_PGTBL_SIZE_256KB:
843 case AGP_I810_PGTBL_SIZE_512KB:
846 case AGP_I965_PGTBL_SIZE_1MB:
849 case AGP_I965_PGTBL_SIZE_2MB:
852 case AGP_I965_PGTBL_SIZE_1_5MB:
853 gtt_size = 1024 + 512;
856 device_printf(dev, "Bad PGTBL size\n");
861 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2);
862 switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) {
863 case AGP_G33_MGGC_GGMS_SIZE_1M:
866 case AGP_G33_MGGC_GGMS_SIZE_2M:
870 device_printf(dev, "Bad PGTBL size\n");
879 device_printf(dev, "Bad chiptype\n");
883 /* GCC1 is called MGGC on i915+ */
884 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
885 switch (gcc1 & AGP_I855_GCC1_GMS) {
886 case AGP_I855_GCC1_GMS_STOLEN_1M:
889 case AGP_I855_GCC1_GMS_STOLEN_4M:
892 case AGP_I855_GCC1_GMS_STOLEN_8M:
895 case AGP_I855_GCC1_GMS_STOLEN_16M:
898 case AGP_I855_GCC1_GMS_STOLEN_32M:
901 case AGP_I915_GCC1_GMS_STOLEN_48M:
902 stolen = sc->match->driver->gen > 2 ? 48 * 1024 : 0;
904 case AGP_I915_GCC1_GMS_STOLEN_64M:
905 stolen = sc->match->driver->gen > 2 ? 64 * 1024 : 0;
907 case AGP_G33_GCC1_GMS_STOLEN_128M:
908 stolen = sc->match->driver->gen > 2 ? 128 * 1024 : 0;
910 case AGP_G33_GCC1_GMS_STOLEN_256M:
911 stolen = sc->match->driver->gen > 2 ? 256 * 1024 : 0;
913 case AGP_G4X_GCC1_GMS_STOLEN_96M:
914 if (sc->match->driver->chiptype == CHIP_I965 ||
915 sc->match->driver->chiptype == CHIP_G4X)
920 case AGP_G4X_GCC1_GMS_STOLEN_160M:
921 if (sc->match->driver->chiptype == CHIP_I965 ||
922 sc->match->driver->chiptype == CHIP_G4X)
927 case AGP_G4X_GCC1_GMS_STOLEN_224M:
928 if (sc->match->driver->chiptype == CHIP_I965 ||
929 sc->match->driver->chiptype == CHIP_G4X)
934 case AGP_G4X_GCC1_GMS_STOLEN_352M:
935 if (sc->match->driver->chiptype == CHIP_I965 ||
936 sc->match->driver->chiptype == CHIP_G4X)
943 "unknown memory configuration, disabling (GCC1 %x)\n",
949 sc->stolen_size = stolen * 1024;
950 sc->stolen = (stolen - gtt_size) * 1024 / 4096;
956 agp_sb_get_stolen_size(device_t dev)
958 struct agp_i810_softc *sc;
961 sc = device_get_softc(dev);
962 gmch_ctl = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
963 switch (gmch_ctl & AGP_SNB_GMCH_GMS_STOLEN_MASK) {
964 case AGP_SNB_GMCH_GMS_STOLEN_32M:
965 sc->stolen_size = 32 * 1024 * 1024;
967 case AGP_SNB_GMCH_GMS_STOLEN_64M:
968 sc->stolen_size = 64 * 1024 * 1024;
970 case AGP_SNB_GMCH_GMS_STOLEN_96M:
971 sc->stolen_size = 96 * 1024 * 1024;
973 case AGP_SNB_GMCH_GMS_STOLEN_128M:
974 sc->stolen_size = 128 * 1024 * 1024;
976 case AGP_SNB_GMCH_GMS_STOLEN_160M:
977 sc->stolen_size = 160 * 1024 * 1024;
979 case AGP_SNB_GMCH_GMS_STOLEN_192M:
980 sc->stolen_size = 192 * 1024 * 1024;
982 case AGP_SNB_GMCH_GMS_STOLEN_224M:
983 sc->stolen_size = 224 * 1024 * 1024;
985 case AGP_SNB_GMCH_GMS_STOLEN_256M:
986 sc->stolen_size = 256 * 1024 * 1024;
988 case AGP_SNB_GMCH_GMS_STOLEN_288M:
989 sc->stolen_size = 288 * 1024 * 1024;
991 case AGP_SNB_GMCH_GMS_STOLEN_320M:
992 sc->stolen_size = 320 * 1024 * 1024;
994 case AGP_SNB_GMCH_GMS_STOLEN_352M:
995 sc->stolen_size = 352 * 1024 * 1024;
997 case AGP_SNB_GMCH_GMS_STOLEN_384M:
998 sc->stolen_size = 384 * 1024 * 1024;
1000 case AGP_SNB_GMCH_GMS_STOLEN_416M:
1001 sc->stolen_size = 416 * 1024 * 1024;
1003 case AGP_SNB_GMCH_GMS_STOLEN_448M:
1004 sc->stolen_size = 448 * 1024 * 1024;
1006 case AGP_SNB_GMCH_GMS_STOLEN_480M:
1007 sc->stolen_size = 480 * 1024 * 1024;
1009 case AGP_SNB_GMCH_GMS_STOLEN_512M:
1010 sc->stolen_size = 512 * 1024 * 1024;
1013 sc->stolen = (sc->stolen_size - 4) / 4096;
1018 agp_gen8_get_stolen_size(device_t dev)
1020 struct agp_i810_softc *sc;
1024 sc = device_get_softc(dev);
1025 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1026 v = (gcc1 >> 8) & 0xFF;
1027 sc->stolen_size = v * (32L * 1024 * 1024); /* 32MB increments */
1028 kprintf("GTT STOLEN %ld\n", (long)sc->stolen_size);
1034 agp_i915_get_gtt_mappable_entries(device_t dev)
1036 struct agp_i810_softc *sc;
1039 sc = device_get_softc(dev);
1040 ap = AGP_GET_APERTURE(dev);
1041 sc->gtt_mappable_entries = ap >> AGP_PAGE_SHIFT;
1046 agp_i810_get_gtt_total_entries(device_t dev)
1048 struct agp_i810_softc *sc;
1050 sc = device_get_softc(dev);
1051 sc->gtt_total_entries = sc->gtt_mappable_entries;
1056 agp_i965_get_gtt_total_entries(device_t dev)
1058 struct agp_i810_softc *sc;
1059 uint32_t pgetbl_ctl;
1062 sc = device_get_softc(dev);
1064 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1065 switch (pgetbl_ctl & AGP_I810_PGTBL_SIZE_MASK) {
1066 case AGP_I810_PGTBL_SIZE_128KB:
1067 sc->gtt_total_entries = 128 * 1024 / 4;
1069 case AGP_I810_PGTBL_SIZE_256KB:
1070 sc->gtt_total_entries = 256 * 1024 / 4;
1072 case AGP_I810_PGTBL_SIZE_512KB:
1073 sc->gtt_total_entries = 512 * 1024 / 4;
1075 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
1076 case AGP_I810_PGTBL_SIZE_1MB:
1077 sc->gtt_total_entries = 1024 * 1024 / 4;
1079 case AGP_I810_PGTBL_SIZE_2MB:
1080 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
1082 case AGP_I810_PGTBL_SIZE_1_5MB:
1083 sc->gtt_total_entries = (1024 + 512) * 1024 / 4;
1086 device_printf(dev, "Unknown page table size\n");
1093 agp_gen5_adjust_pgtbl_size(device_t dev, uint32_t sz)
1095 struct agp_i810_softc *sc;
1096 uint32_t pgetbl_ctl, pgetbl_ctl2;
1098 sc = device_get_softc(dev);
1100 /* Disable per-process page table. */
1101 pgetbl_ctl2 = bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2);
1102 pgetbl_ctl2 &= ~AGP_I810_PGTBL_ENABLED;
1103 bus_write_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2, pgetbl_ctl2);
1105 /* Write the new ggtt size. */
1106 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1107 pgetbl_ctl &= ~AGP_I810_PGTBL_SIZE_MASK;
1109 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgetbl_ctl);
1113 agp_gen5_get_gtt_total_entries(device_t dev)
1115 struct agp_i810_softc *sc;
1118 sc = device_get_softc(dev);
1120 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
1121 switch (gcc1 & AGP_G4x_GCC1_SIZE_MASK) {
1122 case AGP_G4x_GCC1_SIZE_1M:
1123 case AGP_G4x_GCC1_SIZE_VT_1M:
1124 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1MB);
1126 case AGP_G4x_GCC1_SIZE_VT_1_5M:
1127 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1_5MB);
1129 case AGP_G4x_GCC1_SIZE_2M:
1130 case AGP_G4x_GCC1_SIZE_VT_2M:
1131 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_2MB);
1134 device_printf(dev, "Unknown page table size\n");
1138 return (agp_i965_get_gtt_total_entries(dev));
1142 agp_sb_get_gtt_total_entries(device_t dev)
1144 struct agp_i810_softc *sc;
1147 sc = device_get_softc(dev);
1149 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1150 switch (gcc1 & AGP_SNB_GTT_SIZE_MASK) {
1152 case AGP_SNB_GTT_SIZE_0M:
1153 kprintf("Bad GTT size mask: 0x%04x\n", gcc1);
1155 case AGP_SNB_GTT_SIZE_1M:
1156 sc->gtt_total_entries = 1024 * 1024 / 4;
1158 case AGP_SNB_GTT_SIZE_2M:
1159 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
1166 agp_gen8_get_gtt_total_entries(device_t dev)
1168 struct agp_i810_softc *sc;
1172 sc = device_get_softc(dev);
1174 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1175 v = (gcc1 >> 6) & 3;
1178 sc->gtt_total_entries = (v << 20) / 8;
1181 * XXX limit to 2GB due to misc integer overflows calculated on
1184 while ((long)sc->gtt_total_entries * PAGE_SIZE >= 4LL*1024*1024*1024)
1185 sc->gtt_total_entries >>= 1;
1187 kprintf("GTT SIZE %ld representing %ldM vmap\n",
1188 (long)sc->gtt_total_entries * 8,
1189 (long)sc->gtt_total_entries * PAGE_SIZE);
1195 agp_i830_install_gatt(device_t dev)
1197 struct agp_i810_softc *sc;
1200 sc = device_get_softc(dev);
1203 * The i830 automatically initializes the 128k gatt on boot.
1204 * GATT address is already in there, make sure it's enabled.
1206 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1208 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
1210 sc->gatt->ag_physical = pgtblctl & ~1;
1215 agp_i810_attach(device_t dev)
1217 struct agp_i810_softc *sc;
1220 sc = device_get_softc(dev);
1221 sc->bdev = agp_i810_find_bridge(dev);
1222 if (sc->bdev == NULL)
1225 sc->match = agp_i810_match(dev);
1227 agp_set_aperture_resource(dev, sc->match->driver->gen <= 2 ?
1228 AGP_APBASE : AGP_I915_GMADR);
1229 error = agp_generic_attach(dev);
1233 if (ptoa((vm_paddr_t)Maxmem) >
1234 (1ULL << sc->match->driver->busdma_addr_mask_sz) - 1) {
1235 device_printf(dev, "agp_i810 does not support physical "
1236 "memory above %ju.\n", (uintmax_t)(1ULL <<
1237 sc->match->driver->busdma_addr_mask_sz) - 1);
1241 if (bus_alloc_resources(dev, sc->match->driver->res_spec, sc->sc_res)) {
1242 agp_generic_detach(dev);
1246 sc->initial_aperture = AGP_GET_APERTURE(dev);
1247 sc->gatt = kmalloc(sizeof(struct agp_gatt), M_AGP, M_WAITOK);
1248 sc->gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
1250 if ((error = sc->match->driver->get_stolen_size(dev)) != 0 ||
1251 (error = sc->match->driver->install_gatt(dev)) != 0 ||
1252 (error = sc->match->driver->get_gtt_mappable_entries(dev)) != 0 ||
1253 (error = sc->match->driver->get_gtt_total_entries(dev)) != 0 ||
1254 (error = sc->match->driver->chipset_flush_setup(dev)) != 0) {
1255 bus_release_resources(dev, sc->match->driver->res_spec,
1257 kfree(sc->gatt, M_AGP);
1258 agp_generic_detach(dev);
1263 device_printf(dev, "aperture size is %dM",
1264 sc->initial_aperture / 1024 / 1024);
1266 kprintf(", detected %dk stolen memory\n", sc->stolen * 4);
1270 sc->match->driver->dump_regs(dev);
1271 device_printf(dev, "Mappable GTT entries: %d\n",
1272 sc->gtt_mappable_entries);
1273 device_printf(dev, "Total GTT entries: %d\n",
1274 sc->gtt_total_entries);
1280 agp_i830_deinstall_gatt(device_t dev)
1282 struct agp_i810_softc *sc;
1283 unsigned int pgtblctl;
1285 sc = device_get_softc(dev);
1286 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1288 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
1292 agp_i810_detach(device_t dev)
1294 struct agp_i810_softc *sc;
1296 sc = device_get_softc(dev);
1299 /* Clear the GATT base. */
1300 sc->match->driver->deinstall_gatt(dev);
1302 sc->match->driver->chipset_flush_teardown(dev);
1304 /* Put the aperture back the way it started. */
1305 AGP_SET_APERTURE(dev, sc->initial_aperture);
1307 kfree(sc->gatt, M_AGP);
1308 bus_release_resources(dev, sc->match->driver->res_spec, sc->sc_res);
1315 agp_i810_resume(device_t dev)
1317 struct agp_i810_softc *sc;
1318 sc = device_get_softc(dev);
1320 AGP_SET_APERTURE(dev, sc->initial_aperture);
1322 /* Install the GATT. */
1323 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
1324 sc->gatt->ag_physical | 1);
1326 return (bus_generic_resume(dev));
1330 * Sets the PCI resource size of the aperture on i830-class and below chipsets,
1331 * while returning failure on later chipsets when an actual change is
1334 * This whole function is likely bogus, as the kernel would probably need to
1335 * reconfigure the placement of the AGP aperture if a larger size is requested,
1336 * which doesn't happen currently.
1340 agp_i915_set_aperture(device_t dev, u_int32_t aperture)
1343 return (agp_generic_set_aperture(dev, aperture));
1347 agp_i810_method_set_aperture(device_t dev, u_int32_t aperture)
1349 struct agp_i810_softc *sc;
1351 sc = device_get_softc(dev);
1352 return (sc->match->driver->set_aperture(dev, aperture));
1356 * Writes a GTT entry mapping the page at the given offset from the
1357 * beginning of the aperture to the given physical address. Setup the
1358 * caching mode according to flags.
1360 * For gen 1, 2 and 3, GTT start is located at AGP_I810_GTT offset
1361 * from corresponding BAR start. For gen 4, offset is 512KB +
1362 * AGP_I810_GTT, for gen 5 and 6 it is 2MB + AGP_I810_GTT.
1364 * Also, the bits of the physical page address above 4GB needs to be
1365 * placed into bits 40-32 of PTE.
1368 agp_i915_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1373 pte = (u_int32_t)physical | I810_PTE_VALID;
1374 if (flags == AGP_USER_CACHED_MEMORY)
1375 pte |= I830_PTE_SYSTEM_CACHED;
1377 agp_i915_write_gtt(dev, index, pte);
1381 agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte)
1383 struct agp_i810_softc *sc;
1385 sc = device_get_softc(dev);
1386 bus_write_4(sc->sc_res[1], index * 4, pte);
1390 agp_i965_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1395 pte = (u_int32_t)physical | I810_PTE_VALID;
1396 if (flags == AGP_USER_CACHED_MEMORY)
1397 pte |= I830_PTE_SYSTEM_CACHED;
1399 pte |= (physical >> 28) & 0xf0;
1400 agp_i965_write_gtt(dev, index, pte);
1404 agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte)
1406 struct agp_i810_softc *sc;
1408 sc = device_get_softc(dev);
1409 bus_write_4(sc->sc_res[0], index * 4 + (512 * 1024), pte);
1413 agp_g4x_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1418 pte = (u_int32_t)physical | I810_PTE_VALID;
1419 if (flags == AGP_USER_CACHED_MEMORY)
1420 pte |= I830_PTE_SYSTEM_CACHED;
1422 pte |= (physical >> 28) & 0xf0;
1423 agp_g4x_write_gtt(dev, index, pte);
1427 agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte)
1429 struct agp_i810_softc *sc;
1431 sc = device_get_softc(dev);
1432 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1436 agp_sb_install_gtt_pte(device_t dev, u_int index,
1437 vm_offset_t physical, int flags)
1439 int type_mask, gfdt;
1442 pte = (u_int32_t)physical | I810_PTE_VALID;
1443 type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1444 gfdt = (flags & AGP_USER_CACHED_MEMORY_GFDT) != 0 ? GEN6_PTE_GFDT : 0;
1446 if (type_mask == AGP_USER_MEMORY)
1447 pte |= GEN6_PTE_UNCACHED;
1448 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
1449 pte |= GEN6_PTE_LLC_MLC | gfdt;
1451 pte |= GEN6_PTE_LLC | gfdt;
1453 pte |= (physical & 0x000000ff00000000ull) >> 28;
1454 agp_sb_write_gtt(dev, index, pte);
1458 agp_gen8_install_gtt_pte(device_t dev, u_int index,
1459 vm_offset_t physical, int flags)
1461 struct agp_i810_softc *sc;
1465 pte = (u_int64_t)physical | GEN8_PTE_PRESENT | GEN8_PTE_RW;
1466 type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1468 if (type_mask == AGP_USER_MEMORY)
1469 pte |= GEN8_PTE_PWT; /* XXX */
1470 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
1471 pte |= GEN8_PTE_PWT; /* XXX */
1473 pte |= GEN8_PTE_PWT; /* XXX */
1475 sc = device_get_softc(dev);
1476 bus_write_4(sc->sc_res[0], index * 8 + (2 * 1024 * 1024),
1478 bus_write_4(sc->sc_res[0], index * 8 + (2 * 1024 * 1024) + 4,
1479 (uint32_t)(pte >> 32));
1483 agp_sb_write_gtt(device_t dev, u_int index, uint32_t pte)
1485 struct agp_i810_softc *sc;
1487 sc = device_get_softc(dev);
1488 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1492 agp_i810_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
1494 struct agp_i810_softc *sc = device_get_softc(dev);
1497 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
1498 device_printf(dev, "failed: offset is 0x%08jx, "
1499 "shift is %d, entries is %d\n", (intmax_t)offset,
1500 AGP_PAGE_SHIFT, sc->gatt->ag_entries);
1503 index = offset >> AGP_PAGE_SHIFT;
1504 if (sc->stolen != 0 && index < sc->stolen) {
1505 device_printf(dev, "trying to bind into stolen memory\n");
1508 sc->match->driver->install_gtt_pte(dev, index, physical, 0);
1513 agp_i810_unbind_page(device_t dev, vm_offset_t offset)
1515 struct agp_i810_softc *sc;
1518 sc = device_get_softc(dev);
1519 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
1521 index = offset >> AGP_PAGE_SHIFT;
1522 if (sc->stolen != 0 && index < sc->stolen) {
1523 device_printf(dev, "trying to unbind from stolen memory\n");
1526 sc->match->driver->install_gtt_pte(dev, index, 0, 0);
1531 agp_i915_sync_gtt_pte(device_t dev, u_int index)
1533 struct agp_i810_softc *sc;
1535 sc = device_get_softc(dev);
1536 bus_read_4(sc->sc_res[1], index * 4);
1540 agp_i965_sync_gtt_pte(device_t dev, u_int index)
1542 struct agp_i810_softc *sc;
1544 sc = device_get_softc(dev);
1545 bus_read_4(sc->sc_res[0], index * 4 + (512 * 1024));
1549 agp_g4x_sync_gtt_pte(device_t dev, u_int index)
1551 struct agp_i810_softc *sc;
1553 sc = device_get_softc(dev);
1554 bus_read_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024));
1558 * Writing via memory mapped registers already flushes all TLBs.
1561 agp_i810_flush_tlb(device_t dev)
1566 agp_i810_enable(device_t dev, u_int32_t mode)
1572 static struct agp_memory *
1573 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
1575 struct agp_i810_softc *sc;
1576 struct agp_memory *mem;
1579 sc = device_get_softc(dev);
1581 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1582 sc->agp.as_allocated + size > sc->agp.as_maxmem)
1587 * Mapping local DRAM into GATT.
1589 if (sc->match->driver->chiptype != CHIP_I810)
1591 if (size != sc->dcache_size)
1593 } else if (type == 2) {
1595 * Type 2 is the contiguous physical memory type, that hands
1596 * back a physical address. This is used for cursors on i810.
1597 * Hand back as many single pages with physical as the user
1598 * wants, but only allow one larger allocation (ARGB cursor)
1601 if (size != AGP_PAGE_SIZE) {
1602 if (sc->argb_cursor != NULL)
1605 /* Allocate memory for ARGB cursor, if we can. */
1606 sc->argb_cursor = contigmalloc(size, M_AGP,
1607 0, 0, ~0, PAGE_SIZE, 0);
1608 if (sc->argb_cursor == NULL)
1613 mem = kmalloc(sizeof *mem, M_AGP, M_INTWAIT);
1614 mem->am_id = sc->agp.as_nextid++;
1615 mem->am_size = size;
1616 mem->am_type = type;
1617 if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
1618 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
1619 atop(round_page(size)));
1624 if (size == AGP_PAGE_SIZE) {
1626 * Allocate and wire down the page now so that we can
1627 * get its physical address.
1629 VM_OBJECT_LOCK(mem->am_obj);
1630 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NORMAL |
1634 VM_OBJECT_UNLOCK(mem->am_obj);
1635 mem->am_physical = VM_PAGE_TO_PHYS(m);
1638 /* Our allocation is already nicely wired down for us.
1639 * Just grab the physical address.
1641 mem->am_physical = vtophys(sc->argb_cursor);
1644 mem->am_physical = 0;
1647 mem->am_is_bound = 0;
1648 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
1649 sc->agp.as_allocated += size;
1655 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
1657 struct agp_i810_softc *sc;
1659 if (mem->am_is_bound)
1662 sc = device_get_softc(dev);
1664 if (mem->am_type == 2) {
1665 if (mem->am_size == AGP_PAGE_SIZE) {
1667 * Unwire the page which we wired in alloc_memory.
1671 vm_object_hold(mem->am_obj);
1672 m = vm_page_lookup_busy_wait(mem->am_obj, 0,
1674 vm_object_drop(mem->am_obj);
1675 vm_page_unwire(m, 0);
1678 contigfree(sc->argb_cursor, mem->am_size, M_AGP);
1679 sc->argb_cursor = NULL;
1683 sc->agp.as_allocated -= mem->am_size;
1684 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
1686 vm_object_deallocate(mem->am_obj);
1692 agp_i810_bind_memory(device_t dev, struct agp_memory *mem, vm_offset_t offset)
1694 struct agp_i810_softc *sc;
1697 /* Do some sanity checks first. */
1698 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
1699 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
1700 device_printf(dev, "binding memory at bad offset %#x\n",
1705 sc = device_get_softc(dev);
1706 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1707 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1708 if (mem->am_is_bound) {
1709 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1712 /* The memory's already wired down, just stick it in the GTT. */
1713 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1714 sc->match->driver->install_gtt_pte(dev, (offset + i) >>
1715 AGP_PAGE_SHIFT, mem->am_physical + i, 0);
1718 mem->am_offset = offset;
1719 mem->am_is_bound = 1;
1720 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1724 if (mem->am_type != 1)
1725 return (agp_generic_bind_memory(dev, mem, offset));
1728 * Mapping local DRAM into GATT.
1730 if (sc->match->driver->chiptype != CHIP_I810)
1732 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
1733 bus_write_4(sc->sc_res[0],
1734 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
1740 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
1742 struct agp_i810_softc *sc;
1745 sc = device_get_softc(dev);
1747 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
1748 lockmgr(&sc->agp.as_lock, LK_EXCLUSIVE);
1749 if (!mem->am_is_bound) {
1750 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1754 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1755 sc->match->driver->install_gtt_pte(dev,
1756 (mem->am_offset + i) >> AGP_PAGE_SHIFT, 0, 0);
1759 mem->am_is_bound = 0;
1760 lockmgr(&sc->agp.as_lock, LK_RELEASE);
1764 if (mem->am_type != 1)
1765 return (agp_generic_unbind_memory(dev, mem));
1767 if (sc->match->driver->chiptype != CHIP_I810)
1769 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
1770 sc->match->driver->install_gtt_pte(dev, i >> AGP_PAGE_SHIFT,
1776 static device_method_t agp_i810_methods[] = {
1777 /* Device interface */
1778 DEVMETHOD(device_identify, agp_i810_identify),
1779 DEVMETHOD(device_probe, agp_i810_probe),
1780 DEVMETHOD(device_attach, agp_i810_attach),
1781 DEVMETHOD(device_detach, agp_i810_detach),
1782 DEVMETHOD(device_suspend, bus_generic_suspend),
1783 DEVMETHOD(device_resume, agp_i810_resume),
1786 DEVMETHOD(agp_get_aperture, agp_generic_get_aperture),
1787 DEVMETHOD(agp_set_aperture, agp_i810_method_set_aperture),
1788 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
1789 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
1790 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
1791 DEVMETHOD(agp_enable, agp_i810_enable),
1792 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
1793 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
1794 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
1795 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
1796 DEVMETHOD(agp_chipset_flush, agp_intel_gtt_chipset_flush),
1801 static driver_t agp_i810_driver = {
1804 sizeof(struct agp_i810_softc),
1807 static devclass_t agp_devclass;
1809 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, NULL, NULL);
1810 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1811 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
1813 extern vm_page_t bogus_page;
1816 agp_intel_gtt_clear_range(device_t dev, u_int first_entry, u_int num_entries)
1818 struct agp_i810_softc *sc;
1821 sc = device_get_softc(dev);
1822 for (i = 0; i < num_entries; i++)
1823 sc->match->driver->install_gtt_pte(dev, first_entry + i,
1824 VM_PAGE_TO_PHYS(bogus_page), 0);
1825 sc->match->driver->sync_gtt_pte(dev, first_entry + num_entries - 1);
1829 agp_intel_gtt_insert_pages(device_t dev, u_int first_entry, u_int num_entries,
1830 vm_page_t *pages, u_int flags)
1832 struct agp_i810_softc *sc;
1835 sc = device_get_softc(dev);
1836 for (i = 0; i < num_entries; i++) {
1837 KKASSERT(pages[i]->valid == VM_PAGE_BITS_ALL);
1838 KKASSERT(pages[i]->wire_count > 0);
1839 sc->match->driver->install_gtt_pte(dev, first_entry + i,
1840 VM_PAGE_TO_PHYS(pages[i]), flags);
1842 sc->match->driver->sync_gtt_pte(dev, first_entry + num_entries - 1);
1846 agp_intel_gtt_get(device_t dev)
1848 struct agp_i810_softc *sc;
1849 struct intel_gtt res;
1851 sc = device_get_softc(dev);
1852 res.stolen_size = sc->stolen_size;
1853 res.gtt_total_entries = sc->gtt_total_entries;
1854 res.gtt_mappable_entries = sc->gtt_mappable_entries;
1855 res.do_idle_maps = 0;
1856 res.scratch_page_dma = VM_PAGE_TO_PHYS(bogus_page);
1861 agp_i810_chipset_flush_setup(device_t dev)
1868 agp_i810_chipset_flush_teardown(device_t dev)
1871 /* Nothing to do. */
1875 agp_i810_chipset_flush(device_t dev)
1878 /* Nothing to do. */
1882 agp_i915_chipset_flush_alloc_page(device_t dev, uint64_t start, uint64_t end)
1884 struct agp_i810_softc *sc;
1887 sc = device_get_softc(dev);
1888 vga = device_get_parent(dev);
1889 sc->sc_flush_page_rid = 100;
1890 sc->sc_flush_page_res = BUS_ALLOC_RESOURCE(device_get_parent(vga), dev,
1891 SYS_RES_MEMORY, &sc->sc_flush_page_rid, start, end, PAGE_SIZE,
1893 if (sc->sc_flush_page_res == NULL) {
1894 device_printf(dev, "Failed to allocate flush page at 0x%jx\n",
1898 sc->sc_flush_page_vaddr = rman_get_virtual(sc->sc_flush_page_res);
1900 device_printf(dev, "Allocated flush page phys 0x%jx virt %p\n",
1901 (uintmax_t)rman_get_start(sc->sc_flush_page_res),
1902 sc->sc_flush_page_vaddr);
1908 agp_i915_chipset_flush_free_page(device_t dev)
1910 struct agp_i810_softc *sc;
1913 sc = device_get_softc(dev);
1914 vga = device_get_parent(dev);
1915 if (sc->sc_flush_page_res == NULL)
1917 BUS_DEACTIVATE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
1918 sc->sc_flush_page_rid, sc->sc_flush_page_res);
1919 BUS_RELEASE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
1920 sc->sc_flush_page_rid, sc->sc_flush_page_res);
1924 agp_i915_chipset_flush_setup(device_t dev)
1926 struct agp_i810_softc *sc;
1930 sc = device_get_softc(dev);
1931 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
1932 if ((temp & 1) != 0) {
1936 "Found already configured flush page at 0x%jx\n",
1938 sc->sc_bios_allocated_flush_page = 1;
1940 * In the case BIOS initialized the flush pointer (?)
1941 * register, expect that BIOS also set up the resource
1944 error = agp_i915_chipset_flush_alloc_page(dev, temp,
1945 temp + PAGE_SIZE - 1);
1949 sc->sc_bios_allocated_flush_page = 0;
1950 error = agp_i915_chipset_flush_alloc_page(dev, 0, 0xffffffff);
1953 temp = rman_get_start(sc->sc_flush_page_res);
1954 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp | 1, 4);
1960 agp_i915_chipset_flush_teardown(device_t dev)
1962 struct agp_i810_softc *sc;
1965 sc = device_get_softc(dev);
1966 if (sc->sc_flush_page_res == NULL)
1968 if (!sc->sc_bios_allocated_flush_page) {
1969 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
1971 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp, 4);
1973 agp_i915_chipset_flush_free_page(dev);
1977 agp_i965_chipset_flush_setup(device_t dev)
1979 struct agp_i810_softc *sc;
1981 uint32_t temp_hi, temp_lo;
1984 sc = device_get_softc(dev);
1986 temp_hi = pci_read_config(sc->bdev, AGP_I965_IFPADDR + 4, 4);
1987 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
1989 if ((temp_lo & 1) != 0) {
1990 temp = ((uint64_t)temp_hi << 32) | (temp_lo & ~1);
1993 "Found already configured flush page at 0x%jx\n",
1995 sc->sc_bios_allocated_flush_page = 1;
1997 * In the case BIOS initialized the flush pointer (?)
1998 * register, expect that BIOS also set up the resource
2001 error = agp_i915_chipset_flush_alloc_page(dev, temp,
2002 temp + PAGE_SIZE - 1);
2006 sc->sc_bios_allocated_flush_page = 0;
2007 error = agp_i915_chipset_flush_alloc_page(dev, 0, ~0);
2010 temp = rman_get_start(sc->sc_flush_page_res);
2011 pci_write_config(sc->bdev, AGP_I965_IFPADDR + 4,
2012 (temp >> 32) & UINT32_MAX, 4);
2013 pci_write_config(sc->bdev, AGP_I965_IFPADDR,
2014 (temp & UINT32_MAX) | 1, 4);
2020 agp_i965_chipset_flush_teardown(device_t dev)
2022 struct agp_i810_softc *sc;
2025 sc = device_get_softc(dev);
2026 if (sc->sc_flush_page_res == NULL)
2028 if (!sc->sc_bios_allocated_flush_page) {
2029 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
2031 pci_write_config(sc->bdev, AGP_I965_IFPADDR, temp_lo, 4);
2033 agp_i915_chipset_flush_free_page(dev);
2037 agp_i915_chipset_flush(device_t dev)
2039 struct agp_i810_softc *sc;
2041 sc = device_get_softc(dev);
2042 *(uint32_t *)sc->sc_flush_page_vaddr = 1;
2046 agp_intel_gtt_chipset_flush(device_t dev)
2048 struct agp_i810_softc *sc;
2050 sc = device_get_softc(dev);
2051 sc->match->driver->chipset_flush(dev);
2056 agp_intel_gtt_unmap_memory(device_t dev, struct sglist *sg_list)
2061 agp_intel_gtt_map_memory(device_t dev, vm_page_t *pages, u_int num_entries,
2062 struct sglist **sg_list)
2065 struct agp_i810_softc *sc;
2074 if (*sg_list != NULL)
2077 sc = device_get_softc(dev);
2079 sg = sglist_alloc(num_entries, M_WAITOK /* XXXKIB */);
2080 for (i = 0; i < num_entries; i++) {
2081 sg->sg_segs[i].ss_paddr = VM_PAGE_TO_PHYS(pages[i]);
2082 sg->sg_segs[i].ss_len = PAGE_SIZE;
2086 error = bus_dma_tag_create(bus_get_dma_tag(dev),
2087 1 /* alignment */, 0 /* boundary */,
2088 1ULL << sc->match->busdma_addr_mask_sz /* lowaddr */,
2089 BUS_SPACE_MAXADDR /* highaddr */,
2090 NULL /* filtfunc */, NULL /* filtfuncarg */,
2091 BUS_SPACE_MAXADDR /* maxsize */,
2092 BUS_SPACE_UNRESTRICTED /* nsegments */,
2093 BUS_SPACE_MAXADDR /* maxsegsz */,
2094 0 /* flags */, NULL /* lockfunc */, NULL /* lockfuncarg */,
2107 agp_intel_gtt_insert_sg_entries(device_t dev, struct sglist *sg_list,
2108 u_int first_entry, u_int flags)
2110 struct agp_i810_softc *sc;
2115 sc = device_get_softc(dev);
2116 for (i = j = 0; j < sg_list->sg_nseg; j++) {
2117 spaddr = sg_list->sg_segs[i].ss_paddr;
2118 slen = sg_list->sg_segs[i].ss_len;
2119 for (; slen > 0; i++) {
2120 sc->match->driver->install_gtt_pte(dev, first_entry + i,
2122 spaddr += AGP_PAGE_SIZE;
2123 slen -= AGP_PAGE_SIZE;
2126 sc->match->driver->sync_gtt_pte(dev, first_entry + i - 1);
2130 intel_gtt_clear_range(u_int first_entry, u_int num_entries)
2133 agp_intel_gtt_clear_range(intel_agp, first_entry, num_entries);
2137 intel_gtt_insert_pages(u_int first_entry, u_int num_entries, vm_page_t *pages,
2141 agp_intel_gtt_insert_pages(intel_agp, first_entry, num_entries,
2145 struct intel_gtt *intel_gtt_get(void)
2147 intel_private.base = agp_intel_gtt_get(intel_agp);
2148 return &intel_private.base;
2152 intel_gtt_chipset_flush(void)
2155 return (agp_intel_gtt_chipset_flush(intel_agp));
2159 intel_gtt_unmap_memory(struct sglist *sg_list)
2162 agp_intel_gtt_unmap_memory(intel_agp, sg_list);
2166 intel_gtt_map_memory(vm_page_t *pages, u_int num_entries,
2167 struct sglist **sg_list)
2170 return (agp_intel_gtt_map_memory(intel_agp, pages, num_entries,
2175 intel_gtt_insert_sg_entries(struct sglist *sg_list, u_int first_entry,
2179 agp_intel_gtt_insert_sg_entries(intel_agp, sg_list, first_entry, flags);
2186 intel_gtt_sync_pte(u_int entry)
2188 struct agp_i810_softc *sc;
2190 sc = device_get_softc(intel_agp);
2191 sc->match->driver->sync_gtt_pte(intel_agp, entry);
2198 intel_gtt_write(u_int entry, uint32_t val)
2200 struct agp_i810_softc *sc;
2202 sc = device_get_softc(intel_agp);
2203 sc->match->driver->write_gtt(intel_agp, entry, val);