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.8 2004/07/04 00:24:52 dillon 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 /* 830M/845G */
68 #define CHIP_I855 2 /* 852GM/855GM/865G */
70 struct agp_i810_softc {
72 u_int32_t initial_aperture; /* aperture size at startup */
73 struct agp_gatt *gatt;
74 int chiptype; /* i810-like or i830 */
75 u_int32_t dcache_size; /* i810 only */
76 u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */
77 device_t bdev; /* bridge device */
78 struct resource *regs; /* memory mapped GC registers */
79 bus_space_tag_t bst; /* bus_space tag */
80 bus_space_handle_t bsh; /* bus_space handle */
84 agp_i810_match(device_t dev)
86 if (pci_get_class(dev) != PCIC_DISPLAY
87 || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
90 switch (pci_get_devid(dev)) {
92 return ("Intel 82810 (i810 GMCH) SVGA controller");
95 return ("Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller");
98 return ("Intel 82810E (i810E GMCH) SVGA controller");
101 return ("Intel 82815 (i815 GMCH) SVGA controller");
104 return ("Intel 82830M (i830M GMCH) SVGA controller");
107 return ("Intel 82845G (i845 GMCH) SVGA controller");
110 return ("Intel 82865G (i865 GMCH) SVGA controller");
113 switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
115 return ("Intel 82855GME (855GME GMCH) SVGA controller");
118 return ("Intel 82855GM (855GM GMCH) SVGA controller");
121 return ("Intel 82852GME (852GME GMCH) SVGA controller");
124 return ("Intel 82852GM (852GM GMCH) SVGA controller");
127 return ("Intel 8285xM (85xGM GMCH) SVGA controller");
136 * Find bridge device.
139 agp_i810_find_bridge(device_t dev)
141 device_t *children, child;
146 * Calculate bridge device's ID.
148 devid = pci_get_devid(dev);
163 if (device_get_children(device_get_parent(dev), &children, &nchildren))
166 for (i = 0; i < nchildren; i++) {
169 if (pci_get_devid(child) == devid) {
170 free(children, M_TEMP);
174 free(children, M_TEMP);
179 agp_i810_probe(device_t dev)
183 desc = agp_i810_match(dev);
188 int devid = pci_get_devid(dev);
190 bdev = agp_i810_find_bridge(dev);
193 printf("I810: can't find bridge device\n");
198 * checking whether internal graphics device has been activated.
206 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
207 if ((smram & AGP_I810_SMRAM_GMS)
208 == AGP_I810_SMRAM_GMS_DISABLED) {
210 printf("I810: disabled, not probing\n");
219 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
220 if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
222 printf("I830: disabled, not probing\n");
231 device_set_desc(dev, desc);
239 agp_i810_attach(device_t dev)
241 struct agp_i810_softc *sc = device_get_softc(dev);
242 struct agp_gatt *gatt;
245 sc->bdev = agp_i810_find_bridge(dev);
249 error = agp_generic_attach(dev);
253 switch (pci_get_devid(dev)) {
258 sc->chiptype = CHIP_I810;
262 sc->chiptype = CHIP_I830;
266 sc->chiptype = CHIP_I855;
270 /* Same for i810 and i830 */
271 rid = AGP_I810_MMADR;
272 sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
273 0, ~0, 1, RF_ACTIVE);
275 agp_generic_detach(dev);
278 sc->bst = rman_get_bustag(sc->regs);
279 sc->bsh = rman_get_bushandle(sc->regs);
281 sc->initial_aperture = AGP_GET_APERTURE(dev);
282 if (sc->initial_aperture == 0) {
283 device_printf(dev, "bad initial aperture size, disabling\n");
287 gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_INTWAIT);
290 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
292 if ( sc->chiptype == CHIP_I810 ) {
293 /* Some i810s have on-chip memory called dcache */
294 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
295 sc->dcache_size = 4 * 1024 * 1024;
299 /* According to the specs the gatt on the i810 must be 64k */
300 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
301 0, ~0, PAGE_SIZE, 0);
302 if (!gatt->ag_virtual) {
304 device_printf(dev, "contiguous allocation failed\n");
306 agp_generic_detach(dev);
309 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
311 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
313 /* Install the GATT. */
314 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
315 } else if (sc->chiptype == CHIP_I830) {
316 /* The i830 automatically initializes the 128k gatt on boot. */
317 unsigned int gcc1, pgtblctl;
319 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
320 switch (gcc1 & AGP_I830_GCC1_GMS) {
321 case AGP_I830_GCC1_GMS_STOLEN_512:
322 sc->stolen = (512 - 132) * 1024 / 4096;
324 case AGP_I830_GCC1_GMS_STOLEN_1024:
325 sc->stolen = (1024 - 132) * 1024 / 4096;
327 case AGP_I830_GCC1_GMS_STOLEN_8192:
328 sc->stolen = (8192 - 132) * 1024 / 4096;
332 device_printf(dev, "unknown memory configuration, disabling\n");
333 agp_generic_detach(dev);
336 if (sc->stolen > 0) {
338 "detected %dk stolen memory\n", sc->stolen * 4);
340 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
342 /* GATT address is already in there, make sure it's enabled */
343 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
345 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
347 gatt->ag_physical = pgtblctl & ~1;
348 } else { /* CHIP_I855 */
349 /* The i855 automatically initializes the 128k gatt on boot. */
350 unsigned int gcc1, pgtblctl;
352 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
353 switch (gcc1 & AGP_I855_GCC1_GMS) {
354 case AGP_I855_GCC1_GMS_STOLEN_1M:
355 sc->stolen = (1024 - 132) * 1024 / 4096;
357 case AGP_I855_GCC1_GMS_STOLEN_4M:
358 sc->stolen = (4096 - 132) * 1024 / 4096;
360 case AGP_I855_GCC1_GMS_STOLEN_8M:
361 sc->stolen = (8192 - 132) * 1024 / 4096;
363 case AGP_I855_GCC1_GMS_STOLEN_16M:
364 sc->stolen = (16384 - 132) * 1024 / 4096;
366 case AGP_I855_GCC1_GMS_STOLEN_32M:
367 sc->stolen = (32768 - 132) * 1024 / 4096;
372 "unknown memory configuration, disabling\n");
373 agp_generic_detach(dev);
376 if (sc->stolen > 0) {
377 device_printf(dev, "detected %dk stolen memory\n",
380 device_printf(dev, "aperture size is %dM\n",
381 sc->initial_aperture / 1024 / 1024);
383 /* GATT address is already in there, make sure it's enabled */
384 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
386 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
388 gatt->ag_physical = pgtblctl & ~1;
394 agp_i810_detach(device_t dev)
396 struct agp_i810_softc *sc = device_get_softc(dev);
399 error = agp_generic_detach(dev);
403 /* Clear the GATT base. */
404 if ( sc->chiptype == CHIP_I810 ) {
405 WRITE4(AGP_I810_PGTBL_CTL, 0);
407 unsigned int pgtblctl;
408 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
410 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
413 /* Put the aperture back the way it started. */
414 AGP_SET_APERTURE(dev, sc->initial_aperture);
416 if ( sc->chiptype == CHIP_I810 ) {
417 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
419 free(sc->gatt, M_AGP);
421 bus_release_resource(dev, SYS_RES_MEMORY,
422 AGP_I810_MMADR, sc->regs);
428 agp_i810_get_aperture(device_t dev)
430 struct agp_i810_softc *sc = device_get_softc(dev);
432 if ( sc->chiptype == CHIP_I810 ) {
434 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
435 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
436 return 32 * 1024 * 1024;
438 return 64 * 1024 * 1024;
442 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
443 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
444 return 64 * 1024 * 1024;
446 return 128 * 1024 * 1024;
451 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
453 struct agp_i810_softc *sc = device_get_softc(dev);
456 if ( sc->chiptype == CHIP_I810 ) {
458 * Double check for sanity.
460 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
461 device_printf(dev, "bad aperture size %d\n", aperture);
465 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
466 miscc &= ~AGP_I810_MISCC_WINSIZE;
467 if (aperture == 32 * 1024 * 1024)
468 miscc |= AGP_I810_MISCC_WINSIZE_32;
470 miscc |= AGP_I810_MISCC_WINSIZE_64;
472 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
476 if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
477 device_printf(dev, "bad aperture size %d\n", aperture);
480 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
481 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
482 if (aperture == 64 * 1024 * 1024)
483 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
485 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
487 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
494 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
496 struct agp_i810_softc *sc = device_get_softc(dev);
498 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
499 device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
503 if ( sc->chiptype == CHIP_I830 ) {
504 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
505 device_printf(dev, "trying to bind into stolen memory");
510 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
515 agp_i810_unbind_page(device_t dev, int offset)
517 struct agp_i810_softc *sc = device_get_softc(dev);
519 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
522 if ( sc->chiptype == CHIP_I830 ) {
523 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
524 device_printf(dev, "trying to unbind from stolen memory");
529 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
534 * Writing via memory mapped registers already flushes all TLBs.
537 agp_i810_flush_tlb(device_t dev)
542 agp_i810_enable(device_t dev, u_int32_t mode)
548 static struct agp_memory *
549 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
551 struct agp_i810_softc *sc = device_get_softc(dev);
552 struct agp_memory *mem;
554 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
557 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
562 * Mapping local DRAM into GATT.
564 if ( sc->chiptype == CHIP_I830 )
566 if (size != sc->dcache_size)
568 } else if (type == 2) {
570 * Bogus mapping of a single page for the hardware cursor.
572 if (size != AGP_PAGE_SIZE)
576 mem = malloc(sizeof *mem, M_AGP, M_INTWAIT);
577 mem->am_id = sc->agp.as_nextid++;
581 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
582 atop(round_page(size)));
588 * Allocate and wire down the page now so that we can
589 * get its physical address.
592 m = vm_page_grab(mem->am_obj, 0,
593 VM_ALLOC_NORMAL|VM_ALLOC_ZERO|VM_ALLOC_RETRY);
594 if ((m->flags & PG_ZERO) == 0)
595 vm_page_zero_fill(m);
597 mem->am_physical = VM_PAGE_TO_PHYS(m);
600 mem->am_physical = 0;
604 mem->am_is_bound = 0;
605 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
606 sc->agp.as_allocated += size;
612 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
614 struct agp_i810_softc *sc = device_get_softc(dev);
616 if (mem->am_is_bound)
619 if (mem->am_type == 2) {
621 * Unwire the page which we wired in alloc_memory.
623 vm_page_t m = vm_page_lookup(mem->am_obj, 0);
624 vm_page_unwire(m, 0);
627 sc->agp.as_allocated -= mem->am_size;
628 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
630 vm_object_deallocate(mem->am_obj);
636 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
639 struct agp_i810_softc *sc = device_get_softc(dev);
642 if (mem->am_type != 1)
643 return agp_generic_bind_memory(dev, mem, offset);
645 if ( sc->chiptype == CHIP_I830 )
648 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
649 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
657 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
659 struct agp_i810_softc *sc = device_get_softc(dev);
662 if (mem->am_type != 1)
663 return agp_generic_unbind_memory(dev, mem);
665 if ( sc->chiptype == CHIP_I830 )
668 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
669 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
674 static device_method_t agp_i810_methods[] = {
675 /* Device interface */
676 DEVMETHOD(device_probe, agp_i810_probe),
677 DEVMETHOD(device_attach, agp_i810_attach),
678 DEVMETHOD(device_detach, agp_i810_detach),
679 DEVMETHOD(device_shutdown, bus_generic_shutdown),
680 DEVMETHOD(device_suspend, bus_generic_suspend),
681 DEVMETHOD(device_resume, bus_generic_resume),
684 DEVMETHOD(agp_get_aperture, agp_i810_get_aperture),
685 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
686 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
687 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
688 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
689 DEVMETHOD(agp_enable, agp_i810_enable),
690 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
691 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
692 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
693 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
698 static driver_t agp_i810_driver = {
701 sizeof(struct agp_i810_softc),
704 static devclass_t agp_devclass;
706 DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);
707 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
708 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);