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
29 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
30 * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <pci/agppriv.h>
51 #include <pci/agpreg.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pageout.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
63 MALLOC_DECLARE(M_AGP);
65 #define READ1(off) bus_space_read_1(sc->bst, sc->bsh, off)
66 #define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
67 #define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
69 #define CHIP_I810 0 /* i810/i815 */
70 #define CHIP_I830 1 /* 830M/845G */
71 #define CHIP_I855 2 /* 852GM/855GM/865G */
73 struct agp_i810_softc {
75 u_int32_t initial_aperture; /* aperture size at startup */
76 struct agp_gatt *gatt;
77 int chiptype; /* i810-like or i830 */
78 u_int32_t dcache_size; /* i810 only */
79 u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */
80 device_t bdev; /* bridge device */
81 struct resource *regs; /* memory mapped GC registers */
82 bus_space_tag_t bst; /* bus_space tag */
83 bus_space_handle_t bsh; /* bus_space handle */
87 agp_i810_match(device_t dev)
89 if (pci_get_class(dev) != PCIC_DISPLAY
90 || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
93 switch (pci_get_devid(dev)) {
95 return ("Intel 82810 (i810 GMCH) SVGA controller");
98 return ("Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller");
101 return ("Intel 82810E (i810E GMCH) SVGA controller");
104 return ("Intel 82815 (i815 GMCH) SVGA controller");
107 return ("Intel 82830M (830M GMCH) SVGA controller");
110 return ("Intel 82845G (845G 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");
131 return ("Intel 82865G (865G GMCH) SVGA controller");
138 * Find bridge device.
141 agp_i810_find_bridge(device_t dev)
143 device_t *children, child;
148 * Calculate bridge device's ID.
150 devid = pci_get_devid(dev);
166 if (device_get_children(device_get_parent(dev), &children, &nchildren))
169 for (i = 0; i < nchildren; i++) {
172 if (pci_get_devid(child) == devid) {
173 free(children, M_TEMP);
177 free(children, M_TEMP);
182 agp_i810_probe(device_t dev)
186 if (resource_disabled("agp", device_get_unit(dev)))
188 desc = agp_i810_match(dev);
193 int devid = pci_get_devid(dev);
195 bdev = agp_i810_find_bridge(dev);
198 printf("I810: can't find bridge device\n");
203 * checking whether internal graphics device has been activated.
211 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
212 if ((smram & AGP_I810_SMRAM_GMS)
213 == AGP_I810_SMRAM_GMS_DISABLED) {
215 printf("I810: disabled, not probing\n");
225 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
226 if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
228 printf("I830: disabled, not probing\n");
238 device_set_desc(dev, desc);
239 return BUS_PROBE_DEFAULT;
246 agp_i810_attach(device_t dev)
248 struct agp_i810_softc *sc = device_get_softc(dev);
249 struct agp_gatt *gatt;
252 sc->bdev = agp_i810_find_bridge(dev);
256 error = agp_generic_attach(dev);
260 switch (pci_get_devid(dev)) {
265 sc->chiptype = CHIP_I810;
269 sc->chiptype = CHIP_I830;
273 sc->chiptype = CHIP_I855;
277 /* Same for i810 and i830 */
278 rid = AGP_I810_MMADR;
279 sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
282 agp_generic_detach(dev);
285 sc->bst = rman_get_bustag(sc->regs);
286 sc->bsh = rman_get_bushandle(sc->regs);
288 sc->initial_aperture = AGP_GET_APERTURE(dev);
290 gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
292 agp_generic_detach(dev);
297 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
299 if ( sc->chiptype == CHIP_I810 ) {
300 /* Some i810s have on-chip memory called dcache */
301 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
302 sc->dcache_size = 4 * 1024 * 1024;
306 /* According to the specs the gatt on the i810 must be 64k */
307 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
308 0, ~0, PAGE_SIZE, 0);
309 if (!gatt->ag_virtual) {
311 device_printf(dev, "contiguous allocation failed\n");
313 agp_generic_detach(dev);
316 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
318 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
320 /* Install the GATT. */
321 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
322 } else if ( sc->chiptype == CHIP_I830 ) {
323 /* The i830 automatically initializes the 128k gatt on boot. */
324 unsigned int gcc1, pgtblctl;
326 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
327 switch (gcc1 & AGP_I830_GCC1_GMS) {
328 case AGP_I830_GCC1_GMS_STOLEN_512:
329 sc->stolen = (512 - 132) * 1024 / 4096;
331 case AGP_I830_GCC1_GMS_STOLEN_1024:
332 sc->stolen = (1024 - 132) * 1024 / 4096;
334 case AGP_I830_GCC1_GMS_STOLEN_8192:
335 sc->stolen = (8192 - 132) * 1024 / 4096;
339 device_printf(dev, "unknown memory configuration, disabling\n");
340 agp_generic_detach(dev);
344 device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
345 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
347 /* GATT address is already in there, make sure it's enabled */
348 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
350 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
352 gatt->ag_physical = pgtblctl & ~1;
353 } else { /* CHIP_I855 */
354 /* The i855 automatically initializes the 128k gatt on boot. */
355 unsigned int gcc1, pgtblctl;
357 gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
358 switch (gcc1 & AGP_I855_GCC1_GMS) {
359 case AGP_I855_GCC1_GMS_STOLEN_1M:
360 sc->stolen = (1024 - 132) * 1024 / 4096;
362 case AGP_I855_GCC1_GMS_STOLEN_4M:
363 sc->stolen = (4096 - 132) * 1024 / 4096;
365 case AGP_I855_GCC1_GMS_STOLEN_8M:
366 sc->stolen = (8192 - 132) * 1024 / 4096;
368 case AGP_I855_GCC1_GMS_STOLEN_16M:
369 sc->stolen = (16384 - 132) * 1024 / 4096;
371 case AGP_I855_GCC1_GMS_STOLEN_32M:
372 sc->stolen = (32768 - 132) * 1024 / 4096;
376 device_printf(dev, "unknown memory configuration, disabling\n");
377 agp_generic_detach(dev);
381 device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
382 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
384 /* GATT address is already in there, make sure it's enabled */
385 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
387 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
389 gatt->ag_physical = pgtblctl & ~1;
392 /* Add a device for the drm to attach to */
393 if (!device_add_child( dev, "drmsub", -1 ))
394 printf("out of memory...\n");
396 return bus_generic_attach(dev);
400 agp_i810_detach(device_t dev)
402 struct agp_i810_softc *sc = device_get_softc(dev);
406 error = agp_generic_detach(dev);
410 /* Clear the GATT base. */
411 if ( sc->chiptype == CHIP_I810 ) {
412 WRITE4(AGP_I810_PGTBL_CTL, 0);
414 unsigned int pgtblctl;
415 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
417 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
420 /* Put the aperture back the way it started. */
421 AGP_SET_APERTURE(dev, sc->initial_aperture);
423 if ( sc->chiptype == CHIP_I810 ) {
424 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
426 free(sc->gatt, M_AGP);
428 bus_release_resource(dev, SYS_RES_MEMORY,
429 AGP_I810_MMADR, sc->regs);
431 child = device_find_child( dev, "drmsub", 0 );
433 device_delete_child( dev, child );
439 agp_i810_get_aperture(device_t dev)
441 struct agp_i810_softc *sc = device_get_softc(dev);
443 if ( sc->chiptype == CHIP_I810 ) {
445 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
446 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
447 return 32 * 1024 * 1024;
449 return 64 * 1024 * 1024;
450 } else if ( sc->chiptype == CHIP_I830 ) {
453 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
454 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
455 return 64 * 1024 * 1024;
457 return 128 * 1024 * 1024;
458 } else { /* CHIP_I855 */
459 return 128 * 1024 * 1024;
464 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
466 struct agp_i810_softc *sc = device_get_softc(dev);
469 if ( sc->chiptype == CHIP_I810 ) {
471 * Double check for sanity.
473 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
474 device_printf(dev, "bad aperture size %d\n", aperture);
478 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
479 miscc &= ~AGP_I810_MISCC_WINSIZE;
480 if (aperture == 32 * 1024 * 1024)
481 miscc |= AGP_I810_MISCC_WINSIZE_32;
483 miscc |= AGP_I810_MISCC_WINSIZE_64;
485 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
486 } else if ( sc->chiptype == CHIP_I830 ) {
489 if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
490 device_printf(dev, "bad aperture size %d\n", aperture);
493 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
494 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
495 if (aperture == 64 * 1024 * 1024)
496 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
498 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
500 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
501 } else { /* CHIP_I855 */
502 if (aperture != 128 * 1024 * 1024) {
503 device_printf(dev, "bad aperture size %d\n", aperture);
512 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
514 struct agp_i810_softc *sc = device_get_softc(dev);
516 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
517 device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
521 if ( sc->chiptype != CHIP_I810 ) {
522 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
523 device_printf(dev, "trying to bind into stolen memory");
528 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
533 agp_i810_unbind_page(device_t dev, int offset)
535 struct agp_i810_softc *sc = device_get_softc(dev);
537 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
540 if ( sc->chiptype != CHIP_I810 ) {
541 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
542 device_printf(dev, "trying to unbind from stolen memory");
547 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
552 * Writing via memory mapped registers already flushes all TLBs.
555 agp_i810_flush_tlb(device_t dev)
560 agp_i810_enable(device_t dev, u_int32_t mode)
566 static struct agp_memory *
567 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
569 struct agp_i810_softc *sc = device_get_softc(dev);
570 struct agp_memory *mem;
572 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
575 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
580 * Mapping local DRAM into GATT.
582 if ( sc->chiptype != CHIP_I810 )
584 if (size != sc->dcache_size)
586 } else if (type == 2) {
588 * Bogus mapping of a single page for the hardware cursor.
590 if (size != AGP_PAGE_SIZE)
594 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
595 mem->am_id = sc->agp.as_nextid++;
599 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
600 atop(round_page(size)));
606 * Allocate and wire down the page now so that we can
607 * get its physical address.
611 VM_OBJECT_LOCK(mem->am_obj);
612 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY |
613 VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
614 VM_OBJECT_UNLOCK(mem->am_obj);
615 mem->am_physical = VM_PAGE_TO_PHYS(m);
617 mem->am_physical = 0;
621 mem->am_is_bound = 0;
622 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
623 sc->agp.as_allocated += size;
629 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
631 struct agp_i810_softc *sc = device_get_softc(dev);
633 if (mem->am_is_bound)
636 if (mem->am_type == 2) {
638 * Unwire the page which we wired in alloc_memory.
642 VM_OBJECT_LOCK(mem->am_obj);
643 m = vm_page_lookup(mem->am_obj, 0);
644 VM_OBJECT_UNLOCK(mem->am_obj);
645 vm_page_lock_queues();
646 vm_page_unwire(m, 0);
647 vm_page_unlock_queues();
650 sc->agp.as_allocated -= mem->am_size;
651 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
653 vm_object_deallocate(mem->am_obj);
659 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
662 struct agp_i810_softc *sc = device_get_softc(dev);
665 if (mem->am_type != 1)
666 return agp_generic_bind_memory(dev, mem, offset);
668 if ( sc->chiptype != CHIP_I810 )
671 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
672 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
680 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
682 struct agp_i810_softc *sc = device_get_softc(dev);
685 if (mem->am_type != 1)
686 return agp_generic_unbind_memory(dev, mem);
688 if ( sc->chiptype != CHIP_I810 )
691 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
692 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
698 agp_i810_print_child(device_t dev, device_t child)
702 retval += bus_print_child_header(dev, child);
703 retval += printf(": (child of agp_i810.c)");
704 retval += bus_print_child_footer(dev, child);
709 static device_method_t agp_i810_methods[] = {
710 /* Device interface */
711 DEVMETHOD(device_probe, agp_i810_probe),
712 DEVMETHOD(device_attach, agp_i810_attach),
713 DEVMETHOD(device_detach, agp_i810_detach),
714 DEVMETHOD(device_shutdown, bus_generic_shutdown),
715 DEVMETHOD(device_suspend, bus_generic_suspend),
716 DEVMETHOD(device_resume, bus_generic_resume),
719 DEVMETHOD(agp_get_aperture, agp_i810_get_aperture),
720 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
721 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
722 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
723 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
724 DEVMETHOD(agp_enable, agp_i810_enable),
725 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
726 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
727 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
728 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
731 DEVMETHOD(bus_print_child, agp_i810_print_child),
732 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
733 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
734 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
735 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
736 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
737 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
741 static driver_t agp_i810_driver = {
744 sizeof(struct agp_i810_softc),
747 static devclass_t agp_devclass;
749 DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);
750 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
751 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);