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.2 2003/06/17 04:28:56 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 <pci/pcivar.h>
46 #include <pci/pcireg.h>
47 #include <pci/agppriv.h>
48 #include <pci/agpreg.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");
113 * Find bridge device.
116 agp_i810_find_bridge(device_t dev)
118 device_t *children, child;
123 * Calculate bridge device's ID.
125 devid = pci_get_devid(dev);
139 if (device_get_children(device_get_parent(dev), &children, &nchildren))
142 for (i = 0; i < nchildren; i++) {
145 if (pci_get_devid(child) == devid) {
146 free(children, M_TEMP);
150 free(children, M_TEMP);
155 agp_i810_probe(device_t dev)
159 desc = agp_i810_match(dev);
163 int devid = pci_get_devid(dev);
165 bdev = agp_i810_find_bridge(dev);
168 printf("I810: can't find bridge device\n");
173 * checking whether internal graphics device has been activated.
175 if ( (devid != 0x35778086 ) &&
176 (devid != 0x25628086 ) ) {
177 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
178 if ((smram & AGP_I810_SMRAM_GMS)
179 == AGP_I810_SMRAM_GMS_DISABLED) {
181 printf("I810: disabled, not probing\n");
184 } else { /* I830MG */
186 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
187 if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
189 printf("I830: disabled, not probing\n");
195 device_set_desc(dev, desc);
203 agp_i810_attach(device_t dev)
205 struct agp_i810_softc *sc = device_get_softc(dev);
206 struct agp_gatt *gatt;
209 sc->bdev = agp_i810_find_bridge(dev);
213 error = agp_generic_attach(dev);
217 switch (pci_get_devid(dev)) {
222 sc->chiptype = CHIP_I810;
226 sc->chiptype = CHIP_I830;
230 /* Same for i810 and i830 */
231 rid = AGP_I810_MMADR;
232 sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
233 0, ~0, 1, RF_ACTIVE);
235 agp_generic_detach(dev);
238 sc->bst = rman_get_bustag(sc->regs);
239 sc->bsh = rman_get_bushandle(sc->regs);
241 sc->initial_aperture = AGP_GET_APERTURE(dev);
243 gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
245 agp_generic_detach(dev);
250 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
252 if ( sc->chiptype == CHIP_I810 ) {
253 /* Some i810s have on-chip memory called dcache */
254 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
255 sc->dcache_size = 4 * 1024 * 1024;
259 /* According to the specs the gatt on the i810 must be 64k */
260 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
261 0, ~0, PAGE_SIZE, 0);
262 if (!gatt->ag_virtual) {
264 device_printf(dev, "contiguous allocation failed\n");
266 agp_generic_detach(dev);
269 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
271 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
273 /* Install the GATT. */
274 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
276 /* The i830 automatically initializes the 128k gatt on boot. */
277 unsigned int gcc1, pgtblctl;
279 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
280 switch (gcc1 & AGP_I830_GCC1_GMS) {
281 case AGP_I830_GCC1_GMS_STOLEN_512:
282 sc->stolen = (512 - 132) * 1024 / 4096;
284 case AGP_I830_GCC1_GMS_STOLEN_1024:
285 sc->stolen = (1024 - 132) * 1024 / 4096;
287 case AGP_I830_GCC1_GMS_STOLEN_8192:
288 sc->stolen = (8192 - 132) * 1024 / 4096;
292 device_printf(dev, "unknown memory configuration, disabling\n");
293 agp_generic_detach(dev);
297 device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
298 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
300 /* GATT address is already in there, make sure it's enabled */
301 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
303 device_printf(dev, "PGTBL_CTL is 0x%08x\n", pgtblctl);
306 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
308 gatt->ag_physical = pgtblctl & ~1;
312 * Make sure the chipset can see everything.
320 agp_i810_detach(device_t dev)
322 struct agp_i810_softc *sc = device_get_softc(dev);
325 error = agp_generic_detach(dev);
329 /* Clear the GATT base. */
330 if ( sc->chiptype == CHIP_I810 ) {
331 WRITE4(AGP_I810_PGTBL_CTL, 0);
333 unsigned int pgtblctl;
334 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
336 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
339 /* Put the aperture back the way it started. */
340 AGP_SET_APERTURE(dev, sc->initial_aperture);
342 if ( sc->chiptype == CHIP_I810 ) {
343 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
345 free(sc->gatt, M_AGP);
347 bus_release_resource(dev, SYS_RES_MEMORY,
348 AGP_I810_MMADR, sc->regs);
354 agp_i810_get_aperture(device_t dev)
356 struct agp_i810_softc *sc = device_get_softc(dev);
358 if ( sc->chiptype == CHIP_I810 ) {
360 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
361 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
362 return 32 * 1024 * 1024;
364 return 64 * 1024 * 1024;
368 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
369 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
370 return 64 * 1024 * 1024;
372 return 128 * 1024 * 1024;
377 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
379 struct agp_i810_softc *sc = device_get_softc(dev);
382 if ( sc->chiptype == CHIP_I810 ) {
384 * Double check for sanity.
386 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
387 device_printf(dev, "bad aperture size %d\n", aperture);
391 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
392 miscc &= ~AGP_I810_MISCC_WINSIZE;
393 if (aperture == 32 * 1024 * 1024)
394 miscc |= AGP_I810_MISCC_WINSIZE_32;
396 miscc |= AGP_I810_MISCC_WINSIZE_64;
398 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
402 if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
403 device_printf(dev, "bad aperture size %d\n", aperture);
406 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
407 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
408 if (aperture == 64 * 1024 * 1024)
409 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
411 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
413 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
420 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
422 struct agp_i810_softc *sc = device_get_softc(dev);
424 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
425 device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
429 if ( sc->chiptype == CHIP_I830 ) {
430 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
431 device_printf(dev, "trying to bind into stolen memory");
436 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
441 agp_i810_unbind_page(device_t dev, int offset)
443 struct agp_i810_softc *sc = device_get_softc(dev);
445 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
448 if ( sc->chiptype == CHIP_I830 ) {
449 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
450 device_printf(dev, "trying to unbind from stolen memory");
455 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
460 * Writing via memory mapped registers already flushes all TLBs.
463 agp_i810_flush_tlb(device_t dev)
468 agp_i810_enable(device_t dev, u_int32_t mode)
474 static struct agp_memory *
475 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
477 struct agp_i810_softc *sc = device_get_softc(dev);
478 struct agp_memory *mem;
480 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
483 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
488 * Mapping local DRAM into GATT.
490 if ( sc->chiptype == CHIP_I830 )
492 if (size != sc->dcache_size)
494 } else if (type == 2) {
496 * Bogus mapping of a single page for the hardware cursor.
498 if (size != AGP_PAGE_SIZE)
502 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
503 mem->am_id = sc->agp.as_nextid++;
507 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
508 atop(round_page(size)));
514 * Allocate and wire down the page now so that we can
515 * get its physical address.
518 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_ZERO|VM_ALLOC_RETRY);
519 if ((m->flags & PG_ZERO) == 0)
520 vm_page_zero_fill(m);
522 mem->am_physical = VM_PAGE_TO_PHYS(m);
525 mem->am_physical = 0;
529 mem->am_is_bound = 0;
530 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
531 sc->agp.as_allocated += size;
537 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
539 struct agp_i810_softc *sc = device_get_softc(dev);
541 if (mem->am_is_bound)
544 if (mem->am_type == 2) {
546 * Unwire the page which we wired in alloc_memory.
548 vm_page_t m = vm_page_lookup(mem->am_obj, 0);
549 vm_page_unwire(m, 0);
552 sc->agp.as_allocated -= mem->am_size;
553 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
555 vm_object_deallocate(mem->am_obj);
561 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
564 struct agp_i810_softc *sc = device_get_softc(dev);
567 if (mem->am_type != 1)
568 return agp_generic_bind_memory(dev, mem, offset);
570 if ( sc->chiptype == CHIP_I830 )
573 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
574 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
582 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
584 struct agp_i810_softc *sc = device_get_softc(dev);
587 if (mem->am_type != 1)
588 return agp_generic_unbind_memory(dev, mem);
590 if ( sc->chiptype == CHIP_I830 )
593 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
594 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
599 static device_method_t agp_i810_methods[] = {
600 /* Device interface */
601 DEVMETHOD(device_probe, agp_i810_probe),
602 DEVMETHOD(device_attach, agp_i810_attach),
603 DEVMETHOD(device_detach, agp_i810_detach),
604 DEVMETHOD(device_shutdown, bus_generic_shutdown),
605 DEVMETHOD(device_suspend, bus_generic_suspend),
606 DEVMETHOD(device_resume, bus_generic_resume),
609 DEVMETHOD(agp_get_aperture, agp_i810_get_aperture),
610 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
611 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
612 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
613 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
614 DEVMETHOD(agp_enable, agp_i810_enable),
615 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
616 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
617 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
618 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
623 static driver_t agp_i810_driver = {
626 sizeof(struct agp_i810_softc),
629 static devclass_t agp_devclass;
631 DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);