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
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 #define KTR_AGP_I810 KTR_DEV
46 #define KTR_AGP_I810 0
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
54 #include <sys/module.h>
57 #include <sys/mutex.h>
59 #include <sys/rwlock.h>
61 #include <dev/agp/agppriv.h>
62 #include <dev/agp/agpreg.h>
63 #include <dev/agp/agp_i810.h>
64 #include <dev/pci/pcivar.h>
65 #include <dev/pci/pcireg.h>
66 #include <dev/pci/pci_private.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_param.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
77 #include <machine/bus.h>
78 #include <machine/resource.h>
79 #include <machine/md_var.h>
82 MALLOC_DECLARE(M_AGP);
84 struct agp_i810_match;
86 static int agp_i810_check_active(device_t bridge_dev);
87 static int agp_i830_check_active(device_t bridge_dev);
88 static int agp_i915_check_active(device_t bridge_dev);
89 static int agp_sb_check_active(device_t bridge_dev);
91 static void agp_82852_set_desc(device_t dev,
92 const struct agp_i810_match *match);
93 static void agp_i810_set_desc(device_t dev, const struct agp_i810_match *match);
95 static void agp_i810_dump_regs(device_t dev);
96 static void agp_i830_dump_regs(device_t dev);
97 static void agp_i855_dump_regs(device_t dev);
98 static void agp_i915_dump_regs(device_t dev);
99 static void agp_i965_dump_regs(device_t dev);
100 static void agp_sb_dump_regs(device_t dev);
102 static int agp_i810_get_stolen_size(device_t dev);
103 static int agp_i830_get_stolen_size(device_t dev);
104 static int agp_i915_get_stolen_size(device_t dev);
105 static int agp_sb_get_stolen_size(device_t dev);
107 static int agp_i810_get_gtt_mappable_entries(device_t dev);
108 static int agp_i830_get_gtt_mappable_entries(device_t dev);
109 static int agp_i915_get_gtt_mappable_entries(device_t dev);
111 static int agp_i810_get_gtt_total_entries(device_t dev);
112 static int agp_i965_get_gtt_total_entries(device_t dev);
113 static int agp_gen5_get_gtt_total_entries(device_t dev);
114 static int agp_sb_get_gtt_total_entries(device_t dev);
116 static int agp_i810_install_gatt(device_t dev);
117 static int agp_i830_install_gatt(device_t dev);
118 static int agp_i965_install_gatt(device_t dev);
119 static int agp_g4x_install_gatt(device_t dev);
121 static void agp_i810_deinstall_gatt(device_t dev);
122 static void agp_i830_deinstall_gatt(device_t dev);
124 static void agp_i810_install_gtt_pte(device_t dev, u_int index,
125 vm_offset_t physical, int flags);
126 static void agp_i830_install_gtt_pte(device_t dev, u_int index,
127 vm_offset_t physical, int flags);
128 static void agp_i915_install_gtt_pte(device_t dev, u_int index,
129 vm_offset_t physical, int flags);
130 static void agp_i965_install_gtt_pte(device_t dev, u_int index,
131 vm_offset_t physical, int flags);
132 static void agp_g4x_install_gtt_pte(device_t dev, u_int index,
133 vm_offset_t physical, int flags);
134 static void agp_sb_install_gtt_pte(device_t dev, u_int index,
135 vm_offset_t physical, int flags);
137 static void agp_i810_write_gtt(device_t dev, u_int index, uint32_t pte);
138 static void agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte);
139 static void agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte);
140 static void agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte);
141 static void agp_sb_write_gtt(device_t dev, u_int index, uint32_t pte);
143 static u_int32_t agp_i810_read_gtt_pte(device_t dev, u_int index);
144 static u_int32_t agp_i915_read_gtt_pte(device_t dev, u_int index);
145 static u_int32_t agp_i965_read_gtt_pte(device_t dev, u_int index);
146 static u_int32_t agp_g4x_read_gtt_pte(device_t dev, u_int index);
148 static vm_paddr_t agp_i810_read_gtt_pte_paddr(device_t dev, u_int index);
149 static vm_paddr_t agp_i915_read_gtt_pte_paddr(device_t dev, u_int index);
150 static vm_paddr_t agp_sb_read_gtt_pte_paddr(device_t dev, u_int index);
152 static int agp_i810_set_aperture(device_t dev, u_int32_t aperture);
153 static int agp_i830_set_aperture(device_t dev, u_int32_t aperture);
154 static int agp_i915_set_aperture(device_t dev, u_int32_t aperture);
156 static int agp_i810_chipset_flush_setup(device_t dev);
157 static int agp_i915_chipset_flush_setup(device_t dev);
158 static int agp_i965_chipset_flush_setup(device_t dev);
160 static void agp_i810_chipset_flush_teardown(device_t dev);
161 static void agp_i915_chipset_flush_teardown(device_t dev);
162 static void agp_i965_chipset_flush_teardown(device_t dev);
164 static void agp_i810_chipset_flush(device_t dev);
165 static void agp_i830_chipset_flush(device_t dev);
166 static void agp_i915_chipset_flush(device_t dev);
169 CHIP_I810, /* i810/i815 */
170 CHIP_I830, /* 830M/845G */
171 CHIP_I855, /* 852GM/855GM/865G */
172 CHIP_I915, /* 915G/915GM */
173 CHIP_I965, /* G965 */
174 CHIP_G33, /* G33/Q33/Q35 */
175 CHIP_IGD, /* Pineview */
176 CHIP_G4X, /* G45/Q45 */
177 CHIP_SB, /* SandyBridge */
180 /* The i810 through i855 have the registers at BAR 1, and the GATT gets
181 * allocated by us. The i915 has registers in BAR 0 and the GATT is at the
182 * start of the stolen memory, and should only be accessed by the OS through
183 * BAR 3. The G965 has registers and GATT in the same BAR (0) -- first 512KB
184 * is registers, second 512KB is GATT.
186 static struct resource_spec agp_i810_res_spec[] = {
187 { SYS_RES_MEMORY, AGP_I810_MMADR, RF_ACTIVE | RF_SHAREABLE },
191 static struct resource_spec agp_i915_res_spec[] = {
192 { SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
193 { SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
197 static struct resource_spec agp_i965_res_spec[] = {
198 { SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
202 static struct resource_spec agp_g4x_res_spec[] = {
203 { SYS_RES_MEMORY, AGP_G4X_MMADR, RF_ACTIVE | RF_SHAREABLE },
204 { SYS_RES_MEMORY, AGP_G4X_GTTADR, RF_ACTIVE | RF_SHAREABLE },
208 struct agp_i810_softc {
209 struct agp_softc agp;
210 u_int32_t initial_aperture; /* aperture size at startup */
211 struct agp_gatt *gatt;
212 u_int32_t dcache_size; /* i810 only */
213 u_int32_t stolen; /* number of i830/845 gtt
214 entries for stolen memory */
215 u_int stolen_size; /* BIOS-reserved graphics memory */
216 u_int gtt_total_entries; /* Total number of gtt ptes */
217 u_int gtt_mappable_entries; /* Number of gtt ptes mappable by CPU */
218 device_t bdev; /* bridge device */
219 void *argb_cursor; /* contigmalloc area for ARGB cursor */
220 struct resource *sc_res[2];
221 const struct agp_i810_match *match;
222 int sc_flush_page_rid;
223 struct resource *sc_flush_page_res;
224 void *sc_flush_page_vaddr;
225 int sc_bios_allocated_flush_page;
228 static device_t intel_agp;
230 struct agp_i810_driver {
233 int busdma_addr_mask_sz;
234 struct resource_spec *res_spec;
235 int (*check_active)(device_t);
236 void (*set_desc)(device_t, const struct agp_i810_match *);
237 void (*dump_regs)(device_t);
238 int (*get_stolen_size)(device_t);
239 int (*get_gtt_total_entries)(device_t);
240 int (*get_gtt_mappable_entries)(device_t);
241 int (*install_gatt)(device_t);
242 void (*deinstall_gatt)(device_t);
243 void (*write_gtt)(device_t, u_int, uint32_t);
244 void (*install_gtt_pte)(device_t, u_int, vm_offset_t, int);
245 u_int32_t (*read_gtt_pte)(device_t, u_int);
246 vm_paddr_t (*read_gtt_pte_paddr)(device_t , u_int);
247 int (*set_aperture)(device_t, u_int32_t);
248 int (*chipset_flush_setup)(device_t);
249 void (*chipset_flush_teardown)(device_t);
250 void (*chipset_flush)(device_t);
253 static const struct agp_i810_driver agp_i810_i810_driver = {
254 .chiptype = CHIP_I810,
256 .busdma_addr_mask_sz = 32,
257 .res_spec = agp_i810_res_spec,
258 .check_active = agp_i810_check_active,
259 .set_desc = agp_i810_set_desc,
260 .dump_regs = agp_i810_dump_regs,
261 .get_stolen_size = agp_i810_get_stolen_size,
262 .get_gtt_mappable_entries = agp_i810_get_gtt_mappable_entries,
263 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
264 .install_gatt = agp_i810_install_gatt,
265 .deinstall_gatt = agp_i810_deinstall_gatt,
266 .write_gtt = agp_i810_write_gtt,
267 .install_gtt_pte = agp_i810_install_gtt_pte,
268 .read_gtt_pte = agp_i810_read_gtt_pte,
269 .read_gtt_pte_paddr = agp_i810_read_gtt_pte_paddr,
270 .set_aperture = agp_i810_set_aperture,
271 .chipset_flush_setup = agp_i810_chipset_flush_setup,
272 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
273 .chipset_flush = agp_i810_chipset_flush,
276 static const struct agp_i810_driver agp_i810_i815_driver = {
277 .chiptype = CHIP_I810,
279 .busdma_addr_mask_sz = 32,
280 .res_spec = agp_i810_res_spec,
281 .check_active = agp_i810_check_active,
282 .set_desc = agp_i810_set_desc,
283 .dump_regs = agp_i810_dump_regs,
284 .get_stolen_size = agp_i810_get_stolen_size,
285 .get_gtt_mappable_entries = agp_i830_get_gtt_mappable_entries,
286 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
287 .install_gatt = agp_i810_install_gatt,
288 .deinstall_gatt = agp_i810_deinstall_gatt,
289 .write_gtt = agp_i810_write_gtt,
290 .install_gtt_pte = agp_i810_install_gtt_pte,
291 .read_gtt_pte = agp_i810_read_gtt_pte,
292 .read_gtt_pte_paddr = agp_i810_read_gtt_pte_paddr,
293 .set_aperture = agp_i810_set_aperture,
294 .chipset_flush_setup = agp_i810_chipset_flush_setup,
295 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
296 .chipset_flush = agp_i830_chipset_flush,
299 static const struct agp_i810_driver agp_i810_i830_driver = {
300 .chiptype = CHIP_I830,
302 .busdma_addr_mask_sz = 32,
303 .res_spec = agp_i810_res_spec,
304 .check_active = agp_i830_check_active,
305 .set_desc = agp_i810_set_desc,
306 .dump_regs = agp_i830_dump_regs,
307 .get_stolen_size = agp_i830_get_stolen_size,
308 .get_gtt_mappable_entries = agp_i830_get_gtt_mappable_entries,
309 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
310 .install_gatt = agp_i830_install_gatt,
311 .deinstall_gatt = agp_i830_deinstall_gatt,
312 .write_gtt = agp_i810_write_gtt,
313 .install_gtt_pte = agp_i830_install_gtt_pte,
314 .read_gtt_pte = agp_i810_read_gtt_pte,
315 .read_gtt_pte_paddr = agp_i810_read_gtt_pte_paddr,
316 .set_aperture = agp_i830_set_aperture,
317 .chipset_flush_setup = agp_i810_chipset_flush_setup,
318 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
319 .chipset_flush = agp_i830_chipset_flush,
322 static const struct agp_i810_driver agp_i810_i855_driver = {
323 .chiptype = CHIP_I855,
325 .busdma_addr_mask_sz = 32,
326 .res_spec = agp_i810_res_spec,
327 .check_active = agp_i830_check_active,
328 .set_desc = agp_82852_set_desc,
329 .dump_regs = agp_i855_dump_regs,
330 .get_stolen_size = agp_i915_get_stolen_size,
331 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
332 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
333 .install_gatt = agp_i830_install_gatt,
334 .deinstall_gatt = agp_i830_deinstall_gatt,
335 .write_gtt = agp_i810_write_gtt,
336 .install_gtt_pte = agp_i830_install_gtt_pte,
337 .read_gtt_pte = agp_i810_read_gtt_pte,
338 .read_gtt_pte_paddr = agp_i810_read_gtt_pte_paddr,
339 .set_aperture = agp_i830_set_aperture,
340 .chipset_flush_setup = agp_i810_chipset_flush_setup,
341 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
342 .chipset_flush = agp_i830_chipset_flush,
345 static const struct agp_i810_driver agp_i810_i865_driver = {
346 .chiptype = CHIP_I855,
348 .busdma_addr_mask_sz = 32,
349 .res_spec = agp_i810_res_spec,
350 .check_active = agp_i830_check_active,
351 .set_desc = agp_i810_set_desc,
352 .dump_regs = agp_i855_dump_regs,
353 .get_stolen_size = agp_i915_get_stolen_size,
354 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
355 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
356 .install_gatt = agp_i830_install_gatt,
357 .deinstall_gatt = agp_i830_deinstall_gatt,
358 .write_gtt = agp_i810_write_gtt,
359 .install_gtt_pte = agp_i830_install_gtt_pte,
360 .read_gtt_pte = agp_i810_read_gtt_pte,
361 .read_gtt_pte_paddr = agp_i810_read_gtt_pte_paddr,
362 .set_aperture = agp_i915_set_aperture,
363 .chipset_flush_setup = agp_i810_chipset_flush_setup,
364 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
365 .chipset_flush = agp_i830_chipset_flush,
368 static const struct agp_i810_driver agp_i810_i915_driver = {
369 .chiptype = CHIP_I915,
371 .busdma_addr_mask_sz = 32,
372 .res_spec = agp_i915_res_spec,
373 .check_active = agp_i915_check_active,
374 .set_desc = agp_i810_set_desc,
375 .dump_regs = agp_i915_dump_regs,
376 .get_stolen_size = agp_i915_get_stolen_size,
377 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
378 .get_gtt_total_entries = agp_i810_get_gtt_total_entries,
379 .install_gatt = agp_i830_install_gatt,
380 .deinstall_gatt = agp_i830_deinstall_gatt,
381 .write_gtt = agp_i915_write_gtt,
382 .install_gtt_pte = agp_i915_install_gtt_pte,
383 .read_gtt_pte = agp_i915_read_gtt_pte,
384 .read_gtt_pte_paddr = agp_i915_read_gtt_pte_paddr,
385 .set_aperture = agp_i915_set_aperture,
386 .chipset_flush_setup = agp_i915_chipset_flush_setup,
387 .chipset_flush_teardown = agp_i915_chipset_flush_teardown,
388 .chipset_flush = agp_i915_chipset_flush,
391 static const struct agp_i810_driver agp_i810_g965_driver = {
392 .chiptype = CHIP_I965,
394 .busdma_addr_mask_sz = 36,
395 .res_spec = agp_i965_res_spec,
396 .check_active = agp_i915_check_active,
397 .set_desc = agp_i810_set_desc,
398 .dump_regs = agp_i965_dump_regs,
399 .get_stolen_size = agp_i915_get_stolen_size,
400 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
401 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
402 .install_gatt = agp_i965_install_gatt,
403 .deinstall_gatt = agp_i830_deinstall_gatt,
404 .write_gtt = agp_i965_write_gtt,
405 .install_gtt_pte = agp_i965_install_gtt_pte,
406 .read_gtt_pte = agp_i965_read_gtt_pte,
407 .read_gtt_pte_paddr = agp_i915_read_gtt_pte_paddr,
408 .set_aperture = agp_i915_set_aperture,
409 .chipset_flush_setup = agp_i965_chipset_flush_setup,
410 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
411 .chipset_flush = agp_i915_chipset_flush,
414 static const struct agp_i810_driver agp_i810_g33_driver = {
415 .chiptype = CHIP_G33,
417 .busdma_addr_mask_sz = 36,
418 .res_spec = agp_i915_res_spec,
419 .check_active = agp_i915_check_active,
420 .set_desc = agp_i810_set_desc,
421 .dump_regs = agp_i965_dump_regs,
422 .get_stolen_size = agp_i915_get_stolen_size,
423 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
424 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
425 .install_gatt = agp_i830_install_gatt,
426 .deinstall_gatt = agp_i830_deinstall_gatt,
427 .write_gtt = agp_i915_write_gtt,
428 .install_gtt_pte = agp_i915_install_gtt_pte,
429 .read_gtt_pte = agp_i915_read_gtt_pte,
430 .read_gtt_pte_paddr = agp_i915_read_gtt_pte_paddr,
431 .set_aperture = agp_i915_set_aperture,
432 .chipset_flush_setup = agp_i965_chipset_flush_setup,
433 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
434 .chipset_flush = agp_i915_chipset_flush,
437 static const struct agp_i810_driver agp_i810_igd_driver = {
438 .chiptype = CHIP_IGD,
440 .busdma_addr_mask_sz = 36,
441 .res_spec = agp_i915_res_spec,
442 .check_active = agp_i915_check_active,
443 .set_desc = agp_i810_set_desc,
444 .dump_regs = agp_i915_dump_regs,
445 .get_stolen_size = agp_i915_get_stolen_size,
446 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
447 .get_gtt_total_entries = agp_i965_get_gtt_total_entries,
448 .install_gatt = agp_i830_install_gatt,
449 .deinstall_gatt = agp_i830_deinstall_gatt,
450 .write_gtt = agp_i915_write_gtt,
451 .install_gtt_pte = agp_i915_install_gtt_pte,
452 .read_gtt_pte = agp_i915_read_gtt_pte,
453 .read_gtt_pte_paddr = agp_i915_read_gtt_pte_paddr,
454 .set_aperture = agp_i915_set_aperture,
455 .chipset_flush_setup = agp_i965_chipset_flush_setup,
456 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
457 .chipset_flush = agp_i915_chipset_flush,
460 static const struct agp_i810_driver agp_i810_g4x_driver = {
461 .chiptype = CHIP_G4X,
463 .busdma_addr_mask_sz = 36,
464 .res_spec = agp_i965_res_spec,
465 .check_active = agp_i915_check_active,
466 .set_desc = agp_i810_set_desc,
467 .dump_regs = agp_i965_dump_regs,
468 .get_stolen_size = agp_i915_get_stolen_size,
469 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
470 .get_gtt_total_entries = agp_gen5_get_gtt_total_entries,
471 .install_gatt = agp_g4x_install_gatt,
472 .deinstall_gatt = agp_i830_deinstall_gatt,
473 .write_gtt = agp_g4x_write_gtt,
474 .install_gtt_pte = agp_g4x_install_gtt_pte,
475 .read_gtt_pte = agp_g4x_read_gtt_pte,
476 .read_gtt_pte_paddr = agp_i915_read_gtt_pte_paddr,
477 .set_aperture = agp_i915_set_aperture,
478 .chipset_flush_setup = agp_i965_chipset_flush_setup,
479 .chipset_flush_teardown = agp_i965_chipset_flush_teardown,
480 .chipset_flush = agp_i915_chipset_flush,
483 static const struct agp_i810_driver agp_i810_sb_driver = {
486 .busdma_addr_mask_sz = 40,
487 .res_spec = agp_g4x_res_spec,
488 .check_active = agp_sb_check_active,
489 .set_desc = agp_i810_set_desc,
490 .dump_regs = agp_sb_dump_regs,
491 .get_stolen_size = agp_sb_get_stolen_size,
492 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
493 .get_gtt_total_entries = agp_sb_get_gtt_total_entries,
494 .install_gatt = agp_g4x_install_gatt,
495 .deinstall_gatt = agp_i830_deinstall_gatt,
496 .write_gtt = agp_sb_write_gtt,
497 .install_gtt_pte = agp_sb_install_gtt_pte,
498 .read_gtt_pte = agp_g4x_read_gtt_pte,
499 .read_gtt_pte_paddr = agp_sb_read_gtt_pte_paddr,
500 .set_aperture = agp_i915_set_aperture,
501 .chipset_flush_setup = agp_i810_chipset_flush_setup,
502 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
503 .chipset_flush = agp_i810_chipset_flush,
506 static const struct agp_i810_driver agp_i810_hsw_driver = {
509 .busdma_addr_mask_sz = 40,
510 .res_spec = agp_g4x_res_spec,
511 .check_active = agp_sb_check_active,
512 .set_desc = agp_i810_set_desc,
513 .dump_regs = agp_sb_dump_regs,
514 .get_stolen_size = agp_sb_get_stolen_size,
515 .get_gtt_mappable_entries = agp_i915_get_gtt_mappable_entries,
516 .get_gtt_total_entries = agp_sb_get_gtt_total_entries,
517 .install_gatt = agp_g4x_install_gatt,
518 .deinstall_gatt = agp_i830_deinstall_gatt,
519 .write_gtt = agp_sb_write_gtt,
520 .install_gtt_pte = agp_sb_install_gtt_pte,
521 .read_gtt_pte = agp_g4x_read_gtt_pte,
522 .read_gtt_pte_paddr = agp_sb_read_gtt_pte_paddr,
523 .set_aperture = agp_i915_set_aperture,
524 .chipset_flush_setup = agp_i810_chipset_flush_setup,
525 .chipset_flush_teardown = agp_i810_chipset_flush_teardown,
526 .chipset_flush = agp_i810_chipset_flush,
529 /* For adding new devices, devid is the id of the graphics controller
530 * (pci:0:2:0, for example). The placeholder (usually at pci:0:2:1) for the
531 * second head should never be added. The bridge_offset is the offset to
532 * subtract from devid to get the id of the hostb that the device is on.
534 static const struct agp_i810_match {
537 const struct agp_i810_driver *driver;
538 } agp_i810_matches[] = {
541 .name = "Intel 82810 (i810 GMCH) SVGA controller",
542 .driver = &agp_i810_i810_driver
546 .name = "Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller",
547 .driver = &agp_i810_i810_driver
551 .name = "Intel 82810E (i810E GMCH) SVGA controller",
552 .driver = &agp_i810_i810_driver
556 .name = "Intel 82815 (i815 GMCH) SVGA controller",
557 .driver = &agp_i810_i815_driver
561 .name = "Intel 82830M (830M GMCH) SVGA controller",
562 .driver = &agp_i810_i830_driver
566 .name = "Intel 82845M (845M GMCH) SVGA controller",
567 .driver = &agp_i810_i830_driver
571 .name = "Intel 82852/855GM SVGA controller",
572 .driver = &agp_i810_i855_driver
576 .name = "Intel 82865G (865G GMCH) SVGA controller",
577 .driver = &agp_i810_i865_driver
581 .name = "Intel 82915G (915G GMCH) SVGA controller",
582 .driver = &agp_i810_i915_driver
586 .name = "Intel E7221 SVGA controller",
587 .driver = &agp_i810_i915_driver
591 .name = "Intel 82915GM (915GM GMCH) SVGA controller",
592 .driver = &agp_i810_i915_driver
596 .name = "Intel 82945G (945G GMCH) SVGA controller",
597 .driver = &agp_i810_i915_driver
601 .name = "Intel 82945GM (945GM GMCH) SVGA controller",
602 .driver = &agp_i810_i915_driver
606 .name = "Intel 945GME SVGA controller",
607 .driver = &agp_i810_i915_driver
611 .name = "Intel 946GZ SVGA controller",
612 .driver = &agp_i810_g965_driver
616 .name = "Intel G965 SVGA controller",
617 .driver = &agp_i810_g965_driver
621 .name = "Intel Q965 SVGA controller",
622 .driver = &agp_i810_g965_driver
626 .name = "Intel G965 SVGA controller",
627 .driver = &agp_i810_g965_driver
631 .name = "Intel Q35 SVGA controller",
632 .driver = &agp_i810_g33_driver
636 .name = "Intel G33 SVGA controller",
637 .driver = &agp_i810_g33_driver
641 .name = "Intel Q33 SVGA controller",
642 .driver = &agp_i810_g33_driver
646 .name = "Intel Pineview SVGA controller",
647 .driver = &agp_i810_igd_driver
651 .name = "Intel Pineview (M) SVGA controller",
652 .driver = &agp_i810_igd_driver
656 .name = "Intel GM965 SVGA controller",
657 .driver = &agp_i810_g965_driver
661 .name = "Intel GME965 SVGA controller",
662 .driver = &agp_i810_g965_driver
666 .name = "Intel GM45 SVGA controller",
667 .driver = &agp_i810_g4x_driver
671 .name = "Intel Eaglelake SVGA controller",
672 .driver = &agp_i810_g4x_driver
676 .name = "Intel Q45 SVGA controller",
677 .driver = &agp_i810_g4x_driver
681 .name = "Intel G45 SVGA controller",
682 .driver = &agp_i810_g4x_driver
686 .name = "Intel G41 SVGA controller",
687 .driver = &agp_i810_g4x_driver
691 .name = "Intel Ironlake (D) SVGA controller",
692 .driver = &agp_i810_g4x_driver
696 .name = "Intel Ironlake (M) SVGA controller",
697 .driver = &agp_i810_g4x_driver
701 .name = "SandyBridge desktop GT1 IG",
702 .driver = &agp_i810_sb_driver
706 .name = "SandyBridge desktop GT2 IG",
707 .driver = &agp_i810_sb_driver
711 .name = "SandyBridge desktop GT2+ IG",
712 .driver = &agp_i810_sb_driver
716 .name = "SandyBridge mobile GT1 IG",
717 .driver = &agp_i810_sb_driver
721 .name = "SandyBridge mobile GT2 IG",
722 .driver = &agp_i810_sb_driver
726 .name = "SandyBridge mobile GT2+ IG",
727 .driver = &agp_i810_sb_driver
731 .name = "SandyBridge server IG",
732 .driver = &agp_i810_sb_driver
736 .name = "IvyBridge desktop GT1 IG",
737 .driver = &agp_i810_sb_driver
741 .name = "IvyBridge desktop GT2 IG",
742 .driver = &agp_i810_sb_driver
746 .name = "IvyBridge mobile GT1 IG",
747 .driver = &agp_i810_sb_driver
751 .name = "IvyBridge mobile GT2 IG",
752 .driver = &agp_i810_sb_driver
756 .name = "IvyBridge server GT1 IG",
757 .driver = &agp_i810_sb_driver
761 .name = "IvyBridge server GT2 IG",
762 .driver = &agp_i810_sb_driver
766 .name = "Haswell desktop GT1",
767 .driver = &agp_i810_hsw_driver
771 .name = "Haswell desktop GT2",
772 .driver = &agp_i810_hsw_driver
776 .name = "Haswell server GT1",
777 .driver = &agp_i810_hsw_driver
781 .name = "Haswell server GT2",
782 .driver = &agp_i810_hsw_driver
786 .name = "Haswell mobile GT1",
787 .driver = &agp_i810_hsw_driver
791 .name = "Haswell mobile GT2",
792 .driver = &agp_i810_hsw_driver
796 .name = "Haswell SDV",
797 .driver = &agp_i810_hsw_driver
804 static const struct agp_i810_match*
805 agp_i810_match(device_t dev)
809 if (pci_get_class(dev) != PCIC_DISPLAY
810 || (pci_get_subclass(dev) != PCIS_DISPLAY_VGA &&
811 pci_get_subclass(dev) != PCIS_DISPLAY_OTHER))
814 devid = pci_get_devid(dev);
815 for (i = 0; agp_i810_matches[i].devid != 0; i++) {
816 if (agp_i810_matches[i].devid == devid)
819 if (agp_i810_matches[i].devid == 0)
822 return (&agp_i810_matches[i]);
826 * Find bridge device.
829 agp_i810_find_bridge(device_t dev)
832 return (pci_find_dbsf(0, 0, 0, 0));
836 agp_i810_identify(driver_t *driver, device_t parent)
839 if (device_find_child(parent, "agp", -1) == NULL &&
840 agp_i810_match(parent))
841 device_add_child(parent, "agp", -1);
845 agp_i810_check_active(device_t bridge_dev)
849 smram = pci_read_config(bridge_dev, AGP_I810_SMRAM, 1);
850 if ((smram & AGP_I810_SMRAM_GMS) == AGP_I810_SMRAM_GMS_DISABLED)
856 agp_i830_check_active(device_t bridge_dev)
860 gcc1 = pci_read_config(bridge_dev, AGP_I830_GCC1, 1);
861 if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED)
867 agp_i915_check_active(device_t bridge_dev)
871 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
872 if ((deven & AGP_I915_DEVEN_D2F0) == AGP_I915_DEVEN_D2F0_DISABLED)
878 agp_sb_check_active(device_t bridge_dev)
882 deven = pci_read_config(bridge_dev, AGP_I915_DEVEN, 4);
883 if ((deven & AGP_SB_DEVEN_D2EN) == AGP_SB_DEVEN_D2EN_DISABLED)
889 agp_82852_set_desc(device_t dev, const struct agp_i810_match *match)
892 switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
895 "Intel 82855GME (855GME GMCH) SVGA controller");
899 "Intel 82855GM (855GM GMCH) SVGA controller");
903 "Intel 82852GME (852GME GMCH) SVGA controller");
907 "Intel 82852GM (852GM GMCH) SVGA controller");
911 "Intel 8285xM (85xGM GMCH) SVGA controller");
917 agp_i810_set_desc(device_t dev, const struct agp_i810_match *match)
920 device_set_desc(dev, match->name);
924 agp_i810_probe(device_t dev)
927 const struct agp_i810_match *match;
930 if (resource_disabled("agp", device_get_unit(dev)))
932 match = agp_i810_match(dev);
936 bdev = agp_i810_find_bridge(dev);
939 printf("I810: can't find bridge device\n");
944 * checking whether internal graphics device has been activated.
946 err = match->driver->check_active(bdev);
949 printf("i810: disabled, not probing\n");
953 match->driver->set_desc(dev, match);
954 return (BUS_PROBE_DEFAULT);
958 agp_i810_dump_regs(device_t dev)
960 struct agp_i810_softc *sc = device_get_softc(dev);
962 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
963 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
964 device_printf(dev, "AGP_I810_MISCC: 0x%04x\n",
965 pci_read_config(sc->bdev, AGP_I810_MISCC, 2));
969 agp_i830_dump_regs(device_t dev)
971 struct agp_i810_softc *sc = device_get_softc(dev);
973 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
974 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
975 device_printf(dev, "AGP_I830_GCC1: 0x%02x\n",
976 pci_read_config(sc->bdev, AGP_I830_GCC1, 1));
980 agp_i855_dump_regs(device_t dev)
982 struct agp_i810_softc *sc = device_get_softc(dev);
984 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
985 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
986 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
987 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
991 agp_i915_dump_regs(device_t dev)
993 struct agp_i810_softc *sc = device_get_softc(dev);
995 device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
996 bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
997 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
998 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
999 device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
1000 pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
1004 agp_i965_dump_regs(device_t dev)
1006 struct agp_i810_softc *sc = device_get_softc(dev);
1008 device_printf(dev, "AGP_I965_PGTBL_CTL2: %08x\n",
1009 bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2));
1010 device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
1011 pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
1012 device_printf(dev, "AGP_I965_MSAC: 0x%02x\n",
1013 pci_read_config(sc->bdev, AGP_I965_MSAC, 1));
1017 agp_sb_dump_regs(device_t dev)
1019 struct agp_i810_softc *sc = device_get_softc(dev);
1021 device_printf(dev, "AGP_SNB_GFX_MODE: %08x\n",
1022 bus_read_4(sc->sc_res[0], AGP_SNB_GFX_MODE));
1023 device_printf(dev, "AGP_SNB_GCC1: 0x%04x\n",
1024 pci_read_config(sc->bdev, AGP_SNB_GCC1, 2));
1028 agp_i810_get_stolen_size(device_t dev)
1030 struct agp_i810_softc *sc;
1032 sc = device_get_softc(dev);
1034 sc->stolen_size = 0;
1039 agp_i830_get_stolen_size(device_t dev)
1041 struct agp_i810_softc *sc;
1044 sc = device_get_softc(dev);
1046 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
1047 switch (gcc1 & AGP_I830_GCC1_GMS) {
1048 case AGP_I830_GCC1_GMS_STOLEN_512:
1049 sc->stolen = (512 - 132) * 1024 / 4096;
1050 sc->stolen_size = 512 * 1024;
1052 case AGP_I830_GCC1_GMS_STOLEN_1024:
1053 sc->stolen = (1024 - 132) * 1024 / 4096;
1054 sc->stolen_size = 1024 * 1024;
1056 case AGP_I830_GCC1_GMS_STOLEN_8192:
1057 sc->stolen = (8192 - 132) * 1024 / 4096;
1058 sc->stolen_size = 8192 * 1024;
1063 "unknown memory configuration, disabling (GCC1 %x)\n",
1071 agp_i915_get_stolen_size(device_t dev)
1073 struct agp_i810_softc *sc;
1074 unsigned int gcc1, stolen, gtt_size;
1076 sc = device_get_softc(dev);
1079 * Stolen memory is set up at the beginning of the aperture by
1080 * the BIOS, consisting of the GATT followed by 4kb for the
1083 switch (sc->match->driver->chiptype) {
1091 switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
1092 AGP_I810_PGTBL_SIZE_MASK) {
1093 case AGP_I810_PGTBL_SIZE_128KB:
1096 case AGP_I810_PGTBL_SIZE_256KB:
1099 case AGP_I810_PGTBL_SIZE_512KB:
1102 case AGP_I965_PGTBL_SIZE_1MB:
1105 case AGP_I965_PGTBL_SIZE_2MB:
1108 case AGP_I965_PGTBL_SIZE_1_5MB:
1109 gtt_size = 1024 + 512;
1112 device_printf(dev, "Bad PGTBL size\n");
1117 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 2);
1118 switch (gcc1 & AGP_G33_MGGC_GGMS_MASK) {
1119 case AGP_G33_MGGC_GGMS_SIZE_1M:
1122 case AGP_G33_MGGC_GGMS_SIZE_2M:
1126 device_printf(dev, "Bad PGTBL size\n");
1135 device_printf(dev, "Bad chiptype\n");
1139 /* GCC1 is called MGGC on i915+ */
1140 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
1141 switch (gcc1 & AGP_I855_GCC1_GMS) {
1142 case AGP_I855_GCC1_GMS_STOLEN_1M:
1145 case AGP_I855_GCC1_GMS_STOLEN_4M:
1148 case AGP_I855_GCC1_GMS_STOLEN_8M:
1151 case AGP_I855_GCC1_GMS_STOLEN_16M:
1154 case AGP_I855_GCC1_GMS_STOLEN_32M:
1157 case AGP_I915_GCC1_GMS_STOLEN_48M:
1158 stolen = sc->match->driver->gen > 2 ? 48 * 1024 : 0;
1160 case AGP_I915_GCC1_GMS_STOLEN_64M:
1161 stolen = sc->match->driver->gen > 2 ? 64 * 1024 : 0;
1163 case AGP_G33_GCC1_GMS_STOLEN_128M:
1164 stolen = sc->match->driver->gen > 2 ? 128 * 1024 : 0;
1166 case AGP_G33_GCC1_GMS_STOLEN_256M:
1167 stolen = sc->match->driver->gen > 2 ? 256 * 1024 : 0;
1169 case AGP_G4X_GCC1_GMS_STOLEN_96M:
1170 if (sc->match->driver->chiptype == CHIP_I965 ||
1171 sc->match->driver->chiptype == CHIP_G4X)
1176 case AGP_G4X_GCC1_GMS_STOLEN_160M:
1177 if (sc->match->driver->chiptype == CHIP_I965 ||
1178 sc->match->driver->chiptype == CHIP_G4X)
1179 stolen = 160 * 1024;
1183 case AGP_G4X_GCC1_GMS_STOLEN_224M:
1184 if (sc->match->driver->chiptype == CHIP_I965 ||
1185 sc->match->driver->chiptype == CHIP_G4X)
1186 stolen = 224 * 1024;
1190 case AGP_G4X_GCC1_GMS_STOLEN_352M:
1191 if (sc->match->driver->chiptype == CHIP_I965 ||
1192 sc->match->driver->chiptype == CHIP_G4X)
1193 stolen = 352 * 1024;
1199 "unknown memory configuration, disabling (GCC1 %x)\n",
1205 sc->stolen_size = stolen * 1024;
1206 sc->stolen = (stolen - gtt_size) * 1024 / 4096;
1212 agp_sb_get_stolen_size(device_t dev)
1214 struct agp_i810_softc *sc;
1217 sc = device_get_softc(dev);
1218 gmch_ctl = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1219 switch (gmch_ctl & AGP_SNB_GMCH_GMS_STOLEN_MASK) {
1220 case AGP_SNB_GMCH_GMS_STOLEN_32M:
1221 sc->stolen_size = 32 * 1024 * 1024;
1223 case AGP_SNB_GMCH_GMS_STOLEN_64M:
1224 sc->stolen_size = 64 * 1024 * 1024;
1226 case AGP_SNB_GMCH_GMS_STOLEN_96M:
1227 sc->stolen_size = 96 * 1024 * 1024;
1229 case AGP_SNB_GMCH_GMS_STOLEN_128M:
1230 sc->stolen_size = 128 * 1024 * 1024;
1232 case AGP_SNB_GMCH_GMS_STOLEN_160M:
1233 sc->stolen_size = 160 * 1024 * 1024;
1235 case AGP_SNB_GMCH_GMS_STOLEN_192M:
1236 sc->stolen_size = 192 * 1024 * 1024;
1238 case AGP_SNB_GMCH_GMS_STOLEN_224M:
1239 sc->stolen_size = 224 * 1024 * 1024;
1241 case AGP_SNB_GMCH_GMS_STOLEN_256M:
1242 sc->stolen_size = 256 * 1024 * 1024;
1244 case AGP_SNB_GMCH_GMS_STOLEN_288M:
1245 sc->stolen_size = 288 * 1024 * 1024;
1247 case AGP_SNB_GMCH_GMS_STOLEN_320M:
1248 sc->stolen_size = 320 * 1024 * 1024;
1250 case AGP_SNB_GMCH_GMS_STOLEN_352M:
1251 sc->stolen_size = 352 * 1024 * 1024;
1253 case AGP_SNB_GMCH_GMS_STOLEN_384M:
1254 sc->stolen_size = 384 * 1024 * 1024;
1256 case AGP_SNB_GMCH_GMS_STOLEN_416M:
1257 sc->stolen_size = 416 * 1024 * 1024;
1259 case AGP_SNB_GMCH_GMS_STOLEN_448M:
1260 sc->stolen_size = 448 * 1024 * 1024;
1262 case AGP_SNB_GMCH_GMS_STOLEN_480M:
1263 sc->stolen_size = 480 * 1024 * 1024;
1265 case AGP_SNB_GMCH_GMS_STOLEN_512M:
1266 sc->stolen_size = 512 * 1024 * 1024;
1269 sc->stolen = (sc->stolen_size - 4) / 4096;
1274 agp_i810_get_gtt_mappable_entries(device_t dev)
1276 struct agp_i810_softc *sc;
1280 sc = device_get_softc(dev);
1281 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
1282 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
1286 sc->gtt_mappable_entries = (ap * 1024 * 1024) >> AGP_PAGE_SHIFT;
1291 agp_i830_get_gtt_mappable_entries(device_t dev)
1293 struct agp_i810_softc *sc;
1297 sc = device_get_softc(dev);
1298 gmch_ctl = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
1299 if ((gmch_ctl & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
1303 sc->gtt_mappable_entries = (ap * 1024 * 1024) >> AGP_PAGE_SHIFT;
1308 agp_i915_get_gtt_mappable_entries(device_t dev)
1310 struct agp_i810_softc *sc;
1313 sc = device_get_softc(dev);
1314 ap = AGP_GET_APERTURE(dev);
1315 sc->gtt_mappable_entries = ap >> AGP_PAGE_SHIFT;
1320 agp_i810_get_gtt_total_entries(device_t dev)
1322 struct agp_i810_softc *sc;
1324 sc = device_get_softc(dev);
1325 sc->gtt_total_entries = sc->gtt_mappable_entries;
1330 agp_i965_get_gtt_total_entries(device_t dev)
1332 struct agp_i810_softc *sc;
1333 uint32_t pgetbl_ctl;
1336 sc = device_get_softc(dev);
1338 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1339 switch (pgetbl_ctl & AGP_I810_PGTBL_SIZE_MASK) {
1340 case AGP_I810_PGTBL_SIZE_128KB:
1341 sc->gtt_total_entries = 128 * 1024 / 4;
1343 case AGP_I810_PGTBL_SIZE_256KB:
1344 sc->gtt_total_entries = 256 * 1024 / 4;
1346 case AGP_I810_PGTBL_SIZE_512KB:
1347 sc->gtt_total_entries = 512 * 1024 / 4;
1349 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
1350 case AGP_I810_PGTBL_SIZE_1MB:
1351 sc->gtt_total_entries = 1024 * 1024 / 4;
1353 case AGP_I810_PGTBL_SIZE_2MB:
1354 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
1356 case AGP_I810_PGTBL_SIZE_1_5MB:
1357 sc->gtt_total_entries = (1024 + 512) * 1024 / 4;
1360 device_printf(dev, "Unknown page table size\n");
1367 agp_gen5_adjust_pgtbl_size(device_t dev, uint32_t sz)
1369 struct agp_i810_softc *sc;
1370 uint32_t pgetbl_ctl, pgetbl_ctl2;
1372 sc = device_get_softc(dev);
1374 /* Disable per-process page table. */
1375 pgetbl_ctl2 = bus_read_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2);
1376 pgetbl_ctl2 &= ~AGP_I810_PGTBL_ENABLED;
1377 bus_write_4(sc->sc_res[0], AGP_I965_PGTBL_CTL2, pgetbl_ctl2);
1379 /* Write the new ggtt size. */
1380 pgetbl_ctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1381 pgetbl_ctl &= ~AGP_I810_PGTBL_SIZE_MASK;
1383 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgetbl_ctl);
1387 agp_gen5_get_gtt_total_entries(device_t dev)
1389 struct agp_i810_softc *sc;
1392 sc = device_get_softc(dev);
1394 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
1395 switch (gcc1 & AGP_G4x_GCC1_SIZE_MASK) {
1396 case AGP_G4x_GCC1_SIZE_1M:
1397 case AGP_G4x_GCC1_SIZE_VT_1M:
1398 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1MB);
1400 case AGP_G4x_GCC1_SIZE_VT_1_5M:
1401 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_1_5MB);
1403 case AGP_G4x_GCC1_SIZE_2M:
1404 case AGP_G4x_GCC1_SIZE_VT_2M:
1405 agp_gen5_adjust_pgtbl_size(dev, AGP_I810_PGTBL_SIZE_2MB);
1408 device_printf(dev, "Unknown page table size\n");
1412 return (agp_i965_get_gtt_total_entries(dev));
1416 agp_sb_get_gtt_total_entries(device_t dev)
1418 struct agp_i810_softc *sc;
1421 sc = device_get_softc(dev);
1423 gcc1 = pci_read_config(sc->bdev, AGP_SNB_GCC1, 2);
1424 switch (gcc1 & AGP_SNB_GTT_SIZE_MASK) {
1426 case AGP_SNB_GTT_SIZE_0M:
1427 printf("Bad GTT size mask: 0x%04x\n", gcc1);
1429 case AGP_SNB_GTT_SIZE_1M:
1430 sc->gtt_total_entries = 1024 * 1024 / 4;
1432 case AGP_SNB_GTT_SIZE_2M:
1433 sc->gtt_total_entries = 2 * 1024 * 1024 / 4;
1440 agp_i810_install_gatt(device_t dev)
1442 struct agp_i810_softc *sc;
1444 sc = device_get_softc(dev);
1446 /* Some i810s have on-chip memory called dcache. */
1447 if ((bus_read_1(sc->sc_res[0], AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
1449 sc->dcache_size = 4 * 1024 * 1024;
1451 sc->dcache_size = 0;
1453 /* According to the specs the gatt on the i810 must be 64k. */
1454 sc->gatt->ag_virtual = (void *)kmem_alloc_contig(kernel_arena,
1455 64 * 1024, M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE,
1456 0, VM_MEMATTR_WRITE_COMBINING);
1457 if (sc->gatt->ag_virtual == NULL) {
1459 device_printf(dev, "contiguous allocation failed\n");
1463 sc->gatt->ag_physical = vtophys((vm_offset_t)sc->gatt->ag_virtual);
1464 /* Install the GATT. */
1465 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
1466 sc->gatt->ag_physical | 1);
1471 agp_i830_install_gatt_init(struct agp_i810_softc *sc)
1476 * The i830 automatically initializes the 128k gatt on boot.
1477 * GATT address is already in there, make sure it's enabled.
1479 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1481 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
1483 sc->gatt->ag_physical = pgtblctl & ~1;
1487 agp_i830_install_gatt(device_t dev)
1489 struct agp_i810_softc *sc;
1491 sc = device_get_softc(dev);
1492 agp_i830_install_gatt_init(sc);
1497 agp_gen4_install_gatt(device_t dev, const vm_size_t gtt_offset)
1499 struct agp_i810_softc *sc;
1501 sc = device_get_softc(dev);
1502 pmap_change_attr((vm_offset_t)rman_get_virtual(sc->sc_res[0]) +
1503 gtt_offset, rman_get_size(sc->sc_res[0]) - gtt_offset,
1504 VM_MEMATTR_WRITE_COMBINING);
1505 agp_i830_install_gatt_init(sc);
1510 agp_i965_install_gatt(device_t dev)
1513 return (agp_gen4_install_gatt(dev, 512 * 1024));
1517 agp_g4x_install_gatt(device_t dev)
1520 return (agp_gen4_install_gatt(dev, 2 * 1024 * 1024));
1524 agp_i810_attach(device_t dev)
1526 struct agp_i810_softc *sc;
1529 sc = device_get_softc(dev);
1530 sc->bdev = agp_i810_find_bridge(dev);
1531 if (sc->bdev == NULL)
1534 sc->match = agp_i810_match(dev);
1536 agp_set_aperture_resource(dev, sc->match->driver->gen <= 2 ?
1537 AGP_APBASE : AGP_I915_GMADR);
1538 error = agp_generic_attach(dev);
1542 if (ptoa((vm_paddr_t)Maxmem) >
1543 (1ULL << sc->match->driver->busdma_addr_mask_sz) - 1) {
1544 device_printf(dev, "agp_i810 does not support physical "
1545 "memory above %ju.\n", (uintmax_t)(1ULL <<
1546 sc->match->driver->busdma_addr_mask_sz) - 1);
1550 if (bus_alloc_resources(dev, sc->match->driver->res_spec, sc->sc_res)) {
1551 agp_generic_detach(dev);
1555 sc->initial_aperture = AGP_GET_APERTURE(dev);
1556 sc->gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_WAITOK);
1557 sc->gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
1559 if ((error = sc->match->driver->get_stolen_size(dev)) != 0 ||
1560 (error = sc->match->driver->install_gatt(dev)) != 0 ||
1561 (error = sc->match->driver->get_gtt_mappable_entries(dev)) != 0 ||
1562 (error = sc->match->driver->get_gtt_total_entries(dev)) != 0 ||
1563 (error = sc->match->driver->chipset_flush_setup(dev)) != 0) {
1564 bus_release_resources(dev, sc->match->driver->res_spec,
1566 free(sc->gatt, M_AGP);
1567 agp_generic_detach(dev);
1572 device_printf(dev, "aperture size is %dM",
1573 sc->initial_aperture / 1024 / 1024);
1575 printf(", detected %dk stolen memory\n", sc->stolen * 4);
1579 sc->match->driver->dump_regs(dev);
1580 device_printf(dev, "Mappable GTT entries: %d\n",
1581 sc->gtt_mappable_entries);
1582 device_printf(dev, "Total GTT entries: %d\n",
1583 sc->gtt_total_entries);
1589 agp_i810_deinstall_gatt(device_t dev)
1591 struct agp_i810_softc *sc;
1593 sc = device_get_softc(dev);
1594 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 0);
1595 kmem_free(kernel_arena, (vm_offset_t)sc->gatt->ag_virtual, 64 * 1024);
1599 agp_i830_deinstall_gatt(device_t dev)
1601 struct agp_i810_softc *sc;
1602 unsigned int pgtblctl;
1604 sc = device_get_softc(dev);
1605 pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
1607 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
1611 agp_i810_detach(device_t dev)
1613 struct agp_i810_softc *sc;
1615 sc = device_get_softc(dev);
1618 /* Clear the GATT base. */
1619 sc->match->driver->deinstall_gatt(dev);
1621 sc->match->driver->chipset_flush_teardown(dev);
1623 /* Put the aperture back the way it started. */
1624 AGP_SET_APERTURE(dev, sc->initial_aperture);
1626 free(sc->gatt, M_AGP);
1627 bus_release_resources(dev, sc->match->driver->res_spec, sc->sc_res);
1634 agp_i810_resume(device_t dev)
1636 struct agp_i810_softc *sc;
1637 sc = device_get_softc(dev);
1639 AGP_SET_APERTURE(dev, sc->initial_aperture);
1641 /* Install the GATT. */
1642 bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
1643 sc->gatt->ag_physical | 1);
1645 return (bus_generic_resume(dev));
1649 * Sets the PCI resource size of the aperture on i830-class and below chipsets,
1650 * while returning failure on later chipsets when an actual change is
1653 * This whole function is likely bogus, as the kernel would probably need to
1654 * reconfigure the placement of the AGP aperture if a larger size is requested,
1655 * which doesn't happen currently.
1658 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
1660 struct agp_i810_softc *sc;
1663 sc = device_get_softc(dev);
1665 * Double check for sanity.
1667 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
1668 device_printf(dev, "bad aperture size %d\n", aperture);
1672 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
1673 miscc &= ~AGP_I810_MISCC_WINSIZE;
1674 if (aperture == 32 * 1024 * 1024)
1675 miscc |= AGP_I810_MISCC_WINSIZE_32;
1677 miscc |= AGP_I810_MISCC_WINSIZE_64;
1679 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
1684 agp_i830_set_aperture(device_t dev, u_int32_t aperture)
1686 struct agp_i810_softc *sc;
1689 sc = device_get_softc(dev);
1691 if (aperture != 64 * 1024 * 1024 &&
1692 aperture != 128 * 1024 * 1024) {
1693 device_printf(dev, "bad aperture size %d\n", aperture);
1696 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
1697 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
1698 if (aperture == 64 * 1024 * 1024)
1699 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
1701 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
1703 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
1708 agp_i915_set_aperture(device_t dev, u_int32_t aperture)
1711 return (agp_generic_set_aperture(dev, aperture));
1715 agp_i810_method_set_aperture(device_t dev, u_int32_t aperture)
1717 struct agp_i810_softc *sc;
1719 sc = device_get_softc(dev);
1720 return (sc->match->driver->set_aperture(dev, aperture));
1724 * Writes a GTT entry mapping the page at the given offset from the
1725 * beginning of the aperture to the given physical address. Setup the
1726 * caching mode according to flags.
1728 * For gen 1, 2 and 3, GTT start is located at AGP_I810_GTT offset
1729 * from corresponding BAR start. For gen 4, offset is 512KB +
1730 * AGP_I810_GTT, for gen 5 and 6 it is 2MB + AGP_I810_GTT.
1732 * Also, the bits of the physical page address above 4GB needs to be
1733 * placed into bits 40-32 of PTE.
1736 agp_i810_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1741 pte = (u_int32_t)physical | I810_PTE_VALID;
1742 if (flags == AGP_DCACHE_MEMORY)
1743 pte |= I810_PTE_LOCAL;
1744 else if (flags == AGP_USER_CACHED_MEMORY)
1745 pte |= I830_PTE_SYSTEM_CACHED;
1746 agp_i810_write_gtt(dev, index, pte);
1750 agp_i810_write_gtt(device_t dev, u_int index, uint32_t pte)
1752 struct agp_i810_softc *sc;
1754 sc = device_get_softc(dev);
1755 bus_write_4(sc->sc_res[0], AGP_I810_GTT + index * 4, pte);
1756 CTR2(KTR_AGP_I810, "810_pte %x %x", index, pte);
1760 agp_i830_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1765 pte = (u_int32_t)physical | I810_PTE_VALID;
1766 if (flags == AGP_USER_CACHED_MEMORY)
1767 pte |= I830_PTE_SYSTEM_CACHED;
1768 agp_i810_write_gtt(dev, index, pte);
1772 agp_i915_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1777 pte = (u_int32_t)physical | I810_PTE_VALID;
1778 if (flags == AGP_USER_CACHED_MEMORY)
1779 pte |= I830_PTE_SYSTEM_CACHED;
1780 pte |= (physical & 0x0000000f00000000ull) >> 28;
1781 agp_i915_write_gtt(dev, index, pte);
1785 agp_i915_write_gtt(device_t dev, u_int index, uint32_t pte)
1787 struct agp_i810_softc *sc;
1789 sc = device_get_softc(dev);
1790 bus_write_4(sc->sc_res[1], index * 4, pte);
1791 CTR2(KTR_AGP_I810, "915_pte %x %x", index, pte);
1795 agp_i965_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1800 pte = (u_int32_t)physical | I810_PTE_VALID;
1801 if (flags == AGP_USER_CACHED_MEMORY)
1802 pte |= I830_PTE_SYSTEM_CACHED;
1803 pte |= (physical & 0x0000000f00000000ull) >> 28;
1804 agp_i965_write_gtt(dev, index, pte);
1808 agp_i965_write_gtt(device_t dev, u_int index, uint32_t pte)
1810 struct agp_i810_softc *sc;
1812 sc = device_get_softc(dev);
1813 bus_write_4(sc->sc_res[0], index * 4 + (512 * 1024), pte);
1814 CTR2(KTR_AGP_I810, "965_pte %x %x", index, pte);
1818 agp_g4x_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1823 pte = (u_int32_t)physical | I810_PTE_VALID;
1824 if (flags == AGP_USER_CACHED_MEMORY)
1825 pte |= I830_PTE_SYSTEM_CACHED;
1826 pte |= (physical & 0x0000000f00000000ull) >> 28;
1827 agp_g4x_write_gtt(dev, index, pte);
1831 agp_g4x_write_gtt(device_t dev, u_int index, uint32_t pte)
1833 struct agp_i810_softc *sc;
1835 sc = device_get_softc(dev);
1836 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1837 CTR2(KTR_AGP_I810, "g4x_pte %x %x", index, pte);
1841 agp_sb_install_gtt_pte(device_t dev, u_int index, vm_offset_t physical,
1844 int type_mask, gfdt;
1847 pte = (u_int32_t)physical | I810_PTE_VALID;
1848 type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1849 gfdt = (flags & AGP_USER_CACHED_MEMORY_GFDT) != 0 ? GEN6_PTE_GFDT : 0;
1851 if (type_mask == AGP_USER_MEMORY)
1852 pte |= GEN6_PTE_UNCACHED;
1853 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
1854 pte |= GEN6_PTE_LLC_MLC | gfdt;
1856 pte |= GEN6_PTE_LLC | gfdt;
1858 pte |= (physical & 0x000000ff00000000ull) >> 28;
1859 agp_sb_write_gtt(dev, index, pte);
1863 agp_sb_write_gtt(device_t dev, u_int index, uint32_t pte)
1865 struct agp_i810_softc *sc;
1867 sc = device_get_softc(dev);
1868 bus_write_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024), pte);
1869 CTR2(KTR_AGP_I810, "sb_pte %x %x", index, pte);
1873 agp_i810_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
1875 struct agp_i810_softc *sc = device_get_softc(dev);
1878 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
1879 device_printf(dev, "failed: offset is 0x%08jx, "
1880 "shift is %d, entries is %d\n", (intmax_t)offset,
1881 AGP_PAGE_SHIFT, sc->gatt->ag_entries);
1884 index = offset >> AGP_PAGE_SHIFT;
1885 if (sc->stolen != 0 && index < sc->stolen) {
1886 device_printf(dev, "trying to bind into stolen memory\n");
1889 sc->match->driver->install_gtt_pte(dev, index, physical, 0);
1894 agp_i810_unbind_page(device_t dev, vm_offset_t offset)
1896 struct agp_i810_softc *sc;
1899 sc = device_get_softc(dev);
1900 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
1902 index = offset >> AGP_PAGE_SHIFT;
1903 if (sc->stolen != 0 && index < sc->stolen) {
1904 device_printf(dev, "trying to unbind from stolen memory\n");
1907 sc->match->driver->install_gtt_pte(dev, index, 0, 0);
1912 agp_i810_read_gtt_pte(device_t dev, u_int index)
1914 struct agp_i810_softc *sc;
1917 sc = device_get_softc(dev);
1918 pte = bus_read_4(sc->sc_res[0], AGP_I810_GTT + index * 4);
1923 agp_i915_read_gtt_pte(device_t dev, u_int index)
1925 struct agp_i810_softc *sc;
1928 sc = device_get_softc(dev);
1929 pte = bus_read_4(sc->sc_res[1], index * 4);
1934 agp_i965_read_gtt_pte(device_t dev, u_int index)
1936 struct agp_i810_softc *sc;
1939 sc = device_get_softc(dev);
1940 pte = bus_read_4(sc->sc_res[0], index * 4 + (512 * 1024));
1945 agp_g4x_read_gtt_pte(device_t dev, u_int index)
1947 struct agp_i810_softc *sc;
1950 sc = device_get_softc(dev);
1951 pte = bus_read_4(sc->sc_res[0], index * 4 + (2 * 1024 * 1024));
1956 agp_i810_read_gtt_pte_paddr(device_t dev, u_int index)
1958 struct agp_i810_softc *sc;
1962 sc = device_get_softc(dev);
1963 pte = sc->match->driver->read_gtt_pte(dev, index);
1964 res = pte & ~PAGE_MASK;
1969 agp_i915_read_gtt_pte_paddr(device_t dev, u_int index)
1971 struct agp_i810_softc *sc;
1975 sc = device_get_softc(dev);
1976 pte = sc->match->driver->read_gtt_pte(dev, index);
1977 res = (pte & ~PAGE_MASK) | ((pte & 0xf0) << 28);
1982 agp_sb_read_gtt_pte_paddr(device_t dev, u_int index)
1984 struct agp_i810_softc *sc;
1988 sc = device_get_softc(dev);
1989 pte = sc->match->driver->read_gtt_pte(dev, index);
1990 res = (pte & ~PAGE_MASK) | ((pte & 0xff0) << 28);
1995 * Writing via memory mapped registers already flushes all TLBs.
1998 agp_i810_flush_tlb(device_t dev)
2003 agp_i810_enable(device_t dev, u_int32_t mode)
2009 static struct agp_memory *
2010 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
2012 struct agp_i810_softc *sc;
2013 struct agp_memory *mem;
2016 sc = device_get_softc(dev);
2018 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
2019 sc->agp.as_allocated + size > sc->agp.as_maxmem)
2024 * Mapping local DRAM into GATT.
2026 if (sc->match->driver->chiptype != CHIP_I810)
2028 if (size != sc->dcache_size)
2030 } else if (type == 2) {
2032 * Type 2 is the contiguous physical memory type, that hands
2033 * back a physical address. This is used for cursors on i810.
2034 * Hand back as many single pages with physical as the user
2035 * wants, but only allow one larger allocation (ARGB cursor)
2038 if (size != AGP_PAGE_SIZE) {
2039 if (sc->argb_cursor != NULL)
2042 /* Allocate memory for ARGB cursor, if we can. */
2043 sc->argb_cursor = contigmalloc(size, M_AGP,
2044 0, 0, ~0, PAGE_SIZE, 0);
2045 if (sc->argb_cursor == NULL)
2050 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
2051 mem->am_id = sc->agp.as_nextid++;
2052 mem->am_size = size;
2053 mem->am_type = type;
2054 if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
2055 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
2056 atop(round_page(size)));
2061 if (size == AGP_PAGE_SIZE) {
2063 * Allocate and wire down the page now so that we can
2064 * get its physical address.
2066 VM_OBJECT_WLOCK(mem->am_obj);
2067 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY |
2068 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2069 VM_OBJECT_WUNLOCK(mem->am_obj);
2070 mem->am_physical = VM_PAGE_TO_PHYS(m);
2072 /* Our allocation is already nicely wired down for us.
2073 * Just grab the physical address.
2075 mem->am_physical = vtophys(sc->argb_cursor);
2078 mem->am_physical = 0;
2081 mem->am_is_bound = 0;
2082 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
2083 sc->agp.as_allocated += size;
2089 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
2091 struct agp_i810_softc *sc;
2094 if (mem->am_is_bound)
2097 sc = device_get_softc(dev);
2099 if (mem->am_type == 2) {
2100 if (mem->am_size == AGP_PAGE_SIZE) {
2102 * Unwire the page which we wired in alloc_memory.
2104 VM_OBJECT_WLOCK(mem->am_obj);
2105 m = vm_page_lookup(mem->am_obj, 0);
2107 vm_page_unwire(m, 0);
2109 VM_OBJECT_WUNLOCK(mem->am_obj);
2111 contigfree(sc->argb_cursor, mem->am_size, M_AGP);
2112 sc->argb_cursor = NULL;
2116 sc->agp.as_allocated -= mem->am_size;
2117 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
2119 vm_object_deallocate(mem->am_obj);
2125 agp_i810_bind_memory(device_t dev, struct agp_memory *mem, vm_offset_t offset)
2127 struct agp_i810_softc *sc;
2130 /* Do some sanity checks first. */
2131 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
2132 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
2133 device_printf(dev, "binding memory at bad offset %#x\n",
2138 sc = device_get_softc(dev);
2139 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
2140 mtx_lock(&sc->agp.as_lock);
2141 if (mem->am_is_bound) {
2142 mtx_unlock(&sc->agp.as_lock);
2145 /* The memory's already wired down, just stick it in the GTT. */
2146 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
2147 sc->match->driver->install_gtt_pte(dev, (offset + i) >>
2148 AGP_PAGE_SHIFT, mem->am_physical + i, 0);
2150 mem->am_offset = offset;
2151 mem->am_is_bound = 1;
2152 mtx_unlock(&sc->agp.as_lock);
2156 if (mem->am_type != 1)
2157 return (agp_generic_bind_memory(dev, mem, offset));
2160 * Mapping local DRAM into GATT.
2162 if (sc->match->driver->chiptype != CHIP_I810)
2164 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
2165 bus_write_4(sc->sc_res[0],
2166 AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
2172 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
2174 struct agp_i810_softc *sc;
2177 sc = device_get_softc(dev);
2179 if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
2180 mtx_lock(&sc->agp.as_lock);
2181 if (!mem->am_is_bound) {
2182 mtx_unlock(&sc->agp.as_lock);
2186 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
2187 sc->match->driver->install_gtt_pte(dev,
2188 (mem->am_offset + i) >> AGP_PAGE_SHIFT, 0, 0);
2190 mem->am_is_bound = 0;
2191 mtx_unlock(&sc->agp.as_lock);
2195 if (mem->am_type != 1)
2196 return (agp_generic_unbind_memory(dev, mem));
2198 if (sc->match->driver->chiptype != CHIP_I810)
2200 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
2201 sc->match->driver->install_gtt_pte(dev, i >> AGP_PAGE_SHIFT,
2207 static device_method_t agp_i810_methods[] = {
2208 /* Device interface */
2209 DEVMETHOD(device_identify, agp_i810_identify),
2210 DEVMETHOD(device_probe, agp_i810_probe),
2211 DEVMETHOD(device_attach, agp_i810_attach),
2212 DEVMETHOD(device_detach, agp_i810_detach),
2213 DEVMETHOD(device_suspend, bus_generic_suspend),
2214 DEVMETHOD(device_resume, agp_i810_resume),
2217 DEVMETHOD(agp_get_aperture, agp_generic_get_aperture),
2218 DEVMETHOD(agp_set_aperture, agp_i810_method_set_aperture),
2219 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
2220 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
2221 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
2222 DEVMETHOD(agp_enable, agp_i810_enable),
2223 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
2224 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
2225 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
2226 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
2227 DEVMETHOD(agp_chipset_flush, agp_intel_gtt_chipset_flush),
2232 static driver_t agp_i810_driver = {
2235 sizeof(struct agp_i810_softc),
2238 static devclass_t agp_devclass;
2240 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, 0, 0);
2241 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
2242 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
2244 extern vm_page_t bogus_page;
2247 agp_intel_gtt_clear_range(device_t dev, u_int first_entry, u_int num_entries)
2249 struct agp_i810_softc *sc;
2252 sc = device_get_softc(dev);
2253 for (i = 0; i < num_entries; i++)
2254 sc->match->driver->install_gtt_pte(dev, first_entry + i,
2255 VM_PAGE_TO_PHYS(bogus_page), 0);
2256 sc->match->driver->read_gtt_pte(dev, first_entry + num_entries - 1);
2260 agp_intel_gtt_insert_pages(device_t dev, u_int first_entry, u_int num_entries,
2261 vm_page_t *pages, u_int flags)
2263 struct agp_i810_softc *sc;
2266 sc = device_get_softc(dev);
2267 for (i = 0; i < num_entries; i++) {
2268 MPASS(pages[i]->valid == VM_PAGE_BITS_ALL);
2269 MPASS(pages[i]->wire_count > 0);
2270 sc->match->driver->install_gtt_pte(dev, first_entry + i,
2271 VM_PAGE_TO_PHYS(pages[i]), flags);
2273 sc->match->driver->read_gtt_pte(dev, first_entry + num_entries - 1);
2277 agp_intel_gtt_get(device_t dev)
2279 struct agp_i810_softc *sc;
2280 struct intel_gtt res;
2282 sc = device_get_softc(dev);
2283 res.stolen_size = sc->stolen_size;
2284 res.gtt_total_entries = sc->gtt_total_entries;
2285 res.gtt_mappable_entries = sc->gtt_mappable_entries;
2286 res.do_idle_maps = 0;
2287 res.scratch_page_dma = VM_PAGE_TO_PHYS(bogus_page);
2292 agp_i810_chipset_flush_setup(device_t dev)
2299 agp_i810_chipset_flush_teardown(device_t dev)
2302 /* Nothing to do. */
2306 agp_i810_chipset_flush(device_t dev)
2309 /* Nothing to do. */
2313 agp_i830_chipset_flush(device_t dev)
2315 struct agp_i810_softc *sc;
2319 sc = device_get_softc(dev);
2320 pmap_invalidate_cache();
2321 hic = bus_read_4(sc->sc_res[0], AGP_I830_HIC);
2322 bus_write_4(sc->sc_res[0], AGP_I830_HIC, hic | (1U << 31));
2323 for (i = 0; i < 20000 /* 1 sec */; i++) {
2324 hic = bus_read_4(sc->sc_res[0], AGP_I830_HIC);
2325 if ((hic & (1U << 31)) == 0)
2332 agp_i915_chipset_flush_alloc_page(device_t dev, uint64_t start, uint64_t end)
2334 struct agp_i810_softc *sc;
2337 sc = device_get_softc(dev);
2338 vga = device_get_parent(dev);
2339 sc->sc_flush_page_rid = 100;
2340 sc->sc_flush_page_res = BUS_ALLOC_RESOURCE(device_get_parent(vga), dev,
2341 SYS_RES_MEMORY, &sc->sc_flush_page_rid, start, end, PAGE_SIZE,
2343 if (sc->sc_flush_page_res == NULL) {
2344 device_printf(dev, "Failed to allocate flush page at 0x%jx\n",
2348 sc->sc_flush_page_vaddr = rman_get_virtual(sc->sc_flush_page_res);
2350 device_printf(dev, "Allocated flush page phys 0x%jx virt %p\n",
2351 (uintmax_t)rman_get_start(sc->sc_flush_page_res),
2352 sc->sc_flush_page_vaddr);
2358 agp_i915_chipset_flush_free_page(device_t dev)
2360 struct agp_i810_softc *sc;
2363 sc = device_get_softc(dev);
2364 vga = device_get_parent(dev);
2365 if (sc->sc_flush_page_res == NULL)
2367 BUS_DEACTIVATE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
2368 sc->sc_flush_page_rid, sc->sc_flush_page_res);
2369 BUS_RELEASE_RESOURCE(device_get_parent(vga), dev, SYS_RES_MEMORY,
2370 sc->sc_flush_page_rid, sc->sc_flush_page_res);
2374 agp_i915_chipset_flush_setup(device_t dev)
2376 struct agp_i810_softc *sc;
2380 sc = device_get_softc(dev);
2381 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
2382 if ((temp & 1) != 0) {
2386 "Found already configured flush page at 0x%jx\n",
2388 sc->sc_bios_allocated_flush_page = 1;
2390 * In the case BIOS initialized the flush pointer (?)
2391 * register, expect that BIOS also set up the resource
2394 error = agp_i915_chipset_flush_alloc_page(dev, temp,
2395 temp + PAGE_SIZE - 1);
2399 sc->sc_bios_allocated_flush_page = 0;
2400 error = agp_i915_chipset_flush_alloc_page(dev, 0, 0xffffffff);
2403 temp = rman_get_start(sc->sc_flush_page_res);
2404 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp | 1, 4);
2410 agp_i915_chipset_flush_teardown(device_t dev)
2412 struct agp_i810_softc *sc;
2415 sc = device_get_softc(dev);
2416 if (sc->sc_flush_page_res == NULL)
2418 if (!sc->sc_bios_allocated_flush_page) {
2419 temp = pci_read_config(sc->bdev, AGP_I915_IFPADDR, 4);
2421 pci_write_config(sc->bdev, AGP_I915_IFPADDR, temp, 4);
2423 agp_i915_chipset_flush_free_page(dev);
2427 agp_i965_chipset_flush_setup(device_t dev)
2429 struct agp_i810_softc *sc;
2431 uint32_t temp_hi, temp_lo;
2434 sc = device_get_softc(dev);
2436 temp_hi = pci_read_config(sc->bdev, AGP_I965_IFPADDR + 4, 4);
2437 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
2439 if ((temp_lo & 1) != 0) {
2440 temp = ((uint64_t)temp_hi << 32) | (temp_lo & ~1);
2443 "Found already configured flush page at 0x%jx\n",
2445 sc->sc_bios_allocated_flush_page = 1;
2447 * In the case BIOS initialized the flush pointer (?)
2448 * register, expect that BIOS also set up the resource
2451 error = agp_i915_chipset_flush_alloc_page(dev, temp,
2452 temp + PAGE_SIZE - 1);
2456 sc->sc_bios_allocated_flush_page = 0;
2457 error = agp_i915_chipset_flush_alloc_page(dev, 0, ~0);
2460 temp = rman_get_start(sc->sc_flush_page_res);
2461 pci_write_config(sc->bdev, AGP_I965_IFPADDR + 4,
2462 (temp >> 32) & UINT32_MAX, 4);
2463 pci_write_config(sc->bdev, AGP_I965_IFPADDR,
2464 (temp & UINT32_MAX) | 1, 4);
2470 agp_i965_chipset_flush_teardown(device_t dev)
2472 struct agp_i810_softc *sc;
2475 sc = device_get_softc(dev);
2476 if (sc->sc_flush_page_res == NULL)
2478 if (!sc->sc_bios_allocated_flush_page) {
2479 temp_lo = pci_read_config(sc->bdev, AGP_I965_IFPADDR, 4);
2481 pci_write_config(sc->bdev, AGP_I965_IFPADDR, temp_lo, 4);
2483 agp_i915_chipset_flush_free_page(dev);
2487 agp_i915_chipset_flush(device_t dev)
2489 struct agp_i810_softc *sc;
2491 sc = device_get_softc(dev);
2492 *(uint32_t *)sc->sc_flush_page_vaddr = 1;
2496 agp_intel_gtt_chipset_flush(device_t dev)
2498 struct agp_i810_softc *sc;
2500 sc = device_get_softc(dev);
2501 sc->match->driver->chipset_flush(dev);
2506 agp_intel_gtt_unmap_memory(device_t dev, struct sglist *sg_list)
2511 agp_intel_gtt_map_memory(device_t dev, vm_page_t *pages, u_int num_entries,
2512 struct sglist **sg_list)
2514 struct agp_i810_softc *sc;
2522 if (*sg_list != NULL)
2524 sc = device_get_softc(dev);
2525 sg = sglist_alloc(num_entries, M_WAITOK /* XXXKIB */);
2526 for (i = 0; i < num_entries; i++) {
2527 sg->sg_segs[i].ss_paddr = VM_PAGE_TO_PHYS(pages[i]);
2528 sg->sg_segs[i].ss_len = PAGE_SIZE;
2532 error = bus_dma_tag_create(bus_get_dma_tag(dev),
2533 1 /* alignment */, 0 /* boundary */,
2534 1ULL << sc->match->busdma_addr_mask_sz /* lowaddr */,
2535 BUS_SPACE_MAXADDR /* highaddr */,
2536 NULL /* filtfunc */, NULL /* filtfuncarg */,
2537 BUS_SPACE_MAXADDR /* maxsize */,
2538 BUS_SPACE_UNRESTRICTED /* nsegments */,
2539 BUS_SPACE_MAXADDR /* maxsegsz */,
2540 0 /* flags */, NULL /* lockfunc */, NULL /* lockfuncarg */,
2553 agp_intel_gtt_insert_sg_entries(device_t dev, struct sglist *sg_list,
2554 u_int first_entry, u_int flags)
2556 struct agp_i810_softc *sc;
2561 sc = device_get_softc(dev);
2562 for (i = j = 0; j < sg_list->sg_nseg; j++) {
2563 spaddr = sg_list->sg_segs[i].ss_paddr;
2564 slen = sg_list->sg_segs[i].ss_len;
2565 for (; slen > 0; i++) {
2566 sc->match->driver->install_gtt_pte(dev, first_entry + i,
2568 spaddr += AGP_PAGE_SIZE;
2569 slen -= AGP_PAGE_SIZE;
2572 sc->match->driver->read_gtt_pte(dev, first_entry + i - 1);
2576 intel_gtt_clear_range(u_int first_entry, u_int num_entries)
2579 agp_intel_gtt_clear_range(intel_agp, first_entry, num_entries);
2583 intel_gtt_insert_pages(u_int first_entry, u_int num_entries, vm_page_t *pages,
2587 agp_intel_gtt_insert_pages(intel_agp, first_entry, num_entries,
2595 return (agp_intel_gtt_get(intel_agp));
2599 intel_gtt_chipset_flush(void)
2602 return (agp_intel_gtt_chipset_flush(intel_agp));
2606 intel_gtt_unmap_memory(struct sglist *sg_list)
2609 agp_intel_gtt_unmap_memory(intel_agp, sg_list);
2613 intel_gtt_map_memory(vm_page_t *pages, u_int num_entries,
2614 struct sglist **sg_list)
2617 return (agp_intel_gtt_map_memory(intel_agp, pages, num_entries,
2622 intel_gtt_insert_sg_entries(struct sglist *sg_list, u_int first_entry,
2626 agp_intel_gtt_insert_sg_entries(intel_agp, sg_list, first_entry, flags);
2630 intel_gtt_get_bridge_device(void)
2632 struct agp_i810_softc *sc;
2634 sc = device_get_softc(intel_agp);
2639 intel_gtt_read_pte_paddr(u_int entry)
2641 struct agp_i810_softc *sc;
2643 sc = device_get_softc(intel_agp);
2644 return (sc->match->driver->read_gtt_pte_paddr(intel_agp, entry));
2648 intel_gtt_read_pte(u_int entry)
2650 struct agp_i810_softc *sc;
2652 sc = device_get_softc(intel_agp);
2653 return (sc->match->driver->read_gtt_pte(intel_agp, entry));
2657 intel_gtt_write(u_int entry, uint32_t val)
2659 struct agp_i810_softc *sc;
2661 sc = device_get_softc(intel_agp);
2662 return (sc->match->driver->write_gtt(intel_agp, entry, val));