2 * Copyright (c) 2000 Doug Rabson
3 * Copyright (c) 2000 Ruslan Ermilov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/pci/agp_i810.c,v 1.1.2.5 2002/09/15 08:45:41 anholt Exp $
28 * $DragonFly: src/sys/dev/agp/agp_i810.c,v 1.7 2004/05/13 14:33:14 joerg Exp $
32 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
45 #include <bus/pci/pcivar.h>
46 #include <bus/pci/pcireg.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pageout.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
60 MALLOC_DECLARE(M_AGP);
62 #define READ1(off) bus_space_read_1(sc->bst, sc->bsh, off)
63 #define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
64 #define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
66 #define CHIP_I810 0 /* i810/i815 */
67 #define CHIP_I830 1 /* i830/i845 */
69 struct agp_i810_softc {
71 u_int32_t initial_aperture; /* aperture size at startup */
72 struct agp_gatt *gatt;
73 int chiptype; /* i810-like or i830 */
74 u_int32_t dcache_size; /* i810 only */
75 u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */
76 device_t bdev; /* bridge device */
77 struct resource *regs; /* memory mapped GC registers */
78 bus_space_tag_t bst; /* bus_space tag */
79 bus_space_handle_t bsh; /* bus_space handle */
83 agp_i810_match(device_t dev)
85 if (pci_get_class(dev) != PCIC_DISPLAY
86 || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
89 switch (pci_get_devid(dev)) {
91 return ("Intel 82810 (i810 GMCH) SVGA controller");
94 return ("Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller");
97 return ("Intel 82810E (i810E GMCH) SVGA controller");
100 return ("Intel 82815 (i815 GMCH) SVGA controller");
103 return ("Intel 82830 (i830M GMCH) SVGA controller");
106 return ("Intel 82845 (i845 GMCH) SVGA controller");
109 return ("Intel 82865 (i865 GMCH) SVGA controller");
116 * Find bridge device.
119 agp_i810_find_bridge(device_t dev)
121 device_t *children, child;
126 * Calculate bridge device's ID.
128 devid = pci_get_devid(dev);
143 if (device_get_children(device_get_parent(dev), &children, &nchildren))
146 for (i = 0; i < nchildren; i++) {
149 if (pci_get_devid(child) == devid) {
150 free(children, M_TEMP);
154 free(children, M_TEMP);
159 agp_i810_probe(device_t dev)
163 desc = agp_i810_match(dev);
167 int devid = pci_get_devid(dev);
169 bdev = agp_i810_find_bridge(dev);
172 printf("I810: can't find bridge device\n");
177 * checking whether internal graphics device has been activated.
179 if ( (devid != 0x35778086 ) &&
180 (devid != 0x25628086 ) &&
181 (devid != 0x25728086 ) ) {
182 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
183 if ((smram & AGP_I810_SMRAM_GMS)
184 == AGP_I810_SMRAM_GMS_DISABLED) {
186 printf("I810: disabled, not probing\n");
189 } else { /* I830MG */
191 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
192 if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
194 printf("I830: disabled, not probing\n");
200 device_set_desc(dev, desc);
208 agp_i810_attach(device_t dev)
210 struct agp_i810_softc *sc = device_get_softc(dev);
211 struct agp_gatt *gatt;
214 sc->bdev = agp_i810_find_bridge(dev);
218 error = agp_generic_attach(dev);
222 switch (pci_get_devid(dev)) {
227 sc->chiptype = CHIP_I810;
232 sc->chiptype = CHIP_I830;
236 /* Same for i810 and i830 */
237 rid = AGP_I810_MMADR;
238 sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
239 0, ~0, 1, RF_ACTIVE);
241 agp_generic_detach(dev);
244 sc->bst = rman_get_bustag(sc->regs);
245 sc->bsh = rman_get_bushandle(sc->regs);
247 sc->initial_aperture = AGP_GET_APERTURE(dev);
248 if (sc->initial_aperture == 0) {
249 device_printf(dev, "bad initial aperture size, disabling\n");
253 gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_INTWAIT);
256 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
258 if ( sc->chiptype == CHIP_I810 ) {
259 /* Some i810s have on-chip memory called dcache */
260 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
261 sc->dcache_size = 4 * 1024 * 1024;
265 /* According to the specs the gatt on the i810 must be 64k */
266 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
267 0, ~0, PAGE_SIZE, 0);
268 if (!gatt->ag_virtual) {
270 device_printf(dev, "contiguous allocation failed\n");
272 agp_generic_detach(dev);
275 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
277 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
279 /* Install the GATT. */
280 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
282 /* The i830 automatically initializes the 128k gatt on boot. */
283 unsigned int gcc1, pgtblctl;
285 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
286 switch (gcc1 & AGP_I830_GCC1_GMS) {
287 case AGP_I830_GCC1_GMS_STOLEN_512:
288 sc->stolen = (512 - 132) * 1024 / 4096;
290 case AGP_I830_GCC1_GMS_STOLEN_1024:
291 sc->stolen = (1024 - 132) * 1024 / 4096;
293 case AGP_I830_GCC1_GMS_STOLEN_8192:
294 sc->stolen = (8192 - 132) * 1024 / 4096;
298 device_printf(dev, "unknown memory configuration, disabling\n");
299 agp_generic_detach(dev);
303 device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
304 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
306 /* GATT address is already in there, make sure it's enabled */
307 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
309 device_printf(dev, "PGTBL_CTL is 0x%08x\n", pgtblctl);
312 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
314 gatt->ag_physical = pgtblctl & ~1;
318 * Make sure the chipset can see everything.
326 agp_i810_detach(device_t dev)
328 struct agp_i810_softc *sc = device_get_softc(dev);
331 error = agp_generic_detach(dev);
335 /* Clear the GATT base. */
336 if ( sc->chiptype == CHIP_I810 ) {
337 WRITE4(AGP_I810_PGTBL_CTL, 0);
339 unsigned int pgtblctl;
340 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
342 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
345 /* Put the aperture back the way it started. */
346 AGP_SET_APERTURE(dev, sc->initial_aperture);
348 if ( sc->chiptype == CHIP_I810 ) {
349 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
351 free(sc->gatt, M_AGP);
353 bus_release_resource(dev, SYS_RES_MEMORY,
354 AGP_I810_MMADR, sc->regs);
360 agp_i810_get_aperture(device_t dev)
362 struct agp_i810_softc *sc = device_get_softc(dev);
364 if ( sc->chiptype == CHIP_I810 ) {
366 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
367 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
368 return 32 * 1024 * 1024;
370 return 64 * 1024 * 1024;
374 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
375 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
376 return 64 * 1024 * 1024;
378 return 128 * 1024 * 1024;
383 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
385 struct agp_i810_softc *sc = device_get_softc(dev);
388 if ( sc->chiptype == CHIP_I810 ) {
390 * Double check for sanity.
392 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
393 device_printf(dev, "bad aperture size %d\n", aperture);
397 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
398 miscc &= ~AGP_I810_MISCC_WINSIZE;
399 if (aperture == 32 * 1024 * 1024)
400 miscc |= AGP_I810_MISCC_WINSIZE_32;
402 miscc |= AGP_I810_MISCC_WINSIZE_64;
404 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
408 if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
409 device_printf(dev, "bad aperture size %d\n", aperture);
412 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
413 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
414 if (aperture == 64 * 1024 * 1024)
415 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
417 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
419 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
426 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
428 struct agp_i810_softc *sc = device_get_softc(dev);
430 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
431 device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
435 if ( sc->chiptype == CHIP_I830 ) {
436 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
437 device_printf(dev, "trying to bind into stolen memory");
442 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
447 agp_i810_unbind_page(device_t dev, int offset)
449 struct agp_i810_softc *sc = device_get_softc(dev);
451 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
454 if ( sc->chiptype == CHIP_I830 ) {
455 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
456 device_printf(dev, "trying to unbind from stolen memory");
461 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
466 * Writing via memory mapped registers already flushes all TLBs.
469 agp_i810_flush_tlb(device_t dev)
474 agp_i810_enable(device_t dev, u_int32_t mode)
480 static struct agp_memory *
481 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
483 struct agp_i810_softc *sc = device_get_softc(dev);
484 struct agp_memory *mem;
486 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
489 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
494 * Mapping local DRAM into GATT.
496 if ( sc->chiptype == CHIP_I830 )
498 if (size != sc->dcache_size)
500 } else if (type == 2) {
502 * Bogus mapping of a single page for the hardware cursor.
504 if (size != AGP_PAGE_SIZE)
508 mem = malloc(sizeof *mem, M_AGP, M_INTWAIT);
509 mem->am_id = sc->agp.as_nextid++;
513 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
514 atop(round_page(size)));
520 * Allocate and wire down the page now so that we can
521 * get its physical address.
524 m = vm_page_grab(mem->am_obj, 0,
525 VM_ALLOC_NORMAL|VM_ALLOC_ZERO|VM_ALLOC_RETRY);
526 if ((m->flags & PG_ZERO) == 0)
527 vm_page_zero_fill(m);
529 mem->am_physical = VM_PAGE_TO_PHYS(m);
532 mem->am_physical = 0;
536 mem->am_is_bound = 0;
537 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
538 sc->agp.as_allocated += size;
544 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
546 struct agp_i810_softc *sc = device_get_softc(dev);
548 if (mem->am_is_bound)
551 if (mem->am_type == 2) {
553 * Unwire the page which we wired in alloc_memory.
555 vm_page_t m = vm_page_lookup(mem->am_obj, 0);
556 vm_page_unwire(m, 0);
559 sc->agp.as_allocated -= mem->am_size;
560 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
562 vm_object_deallocate(mem->am_obj);
568 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
571 struct agp_i810_softc *sc = device_get_softc(dev);
574 if (mem->am_type != 1)
575 return agp_generic_bind_memory(dev, mem, offset);
577 if ( sc->chiptype == CHIP_I830 )
580 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
581 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
589 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
591 struct agp_i810_softc *sc = device_get_softc(dev);
594 if (mem->am_type != 1)
595 return agp_generic_unbind_memory(dev, mem);
597 if ( sc->chiptype == CHIP_I830 )
600 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
601 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
606 static device_method_t agp_i810_methods[] = {
607 /* Device interface */
608 DEVMETHOD(device_probe, agp_i810_probe),
609 DEVMETHOD(device_attach, agp_i810_attach),
610 DEVMETHOD(device_detach, agp_i810_detach),
611 DEVMETHOD(device_shutdown, bus_generic_shutdown),
612 DEVMETHOD(device_suspend, bus_generic_suspend),
613 DEVMETHOD(device_resume, bus_generic_resume),
616 DEVMETHOD(agp_get_aperture, agp_i810_get_aperture),
617 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
618 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
619 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
620 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
621 DEVMETHOD(agp_enable, agp_i810_enable),
622 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
623 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
624 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
625 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
630 static driver_t agp_i810_driver = {
633 sizeof(struct agp_i810_softc),
636 static devclass_t agp_devclass;
638 DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);
639 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
640 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);