The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/agp/agp_ati.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2005 Eric Anholt
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  * Based on reading the Linux 2.6.8.1 driver by Dave Jones.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD$");
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/malloc.h>
   37 #include <sys/kernel.h>
   38 #include <sys/module.h>
   39 #include <sys/bus.h>
   40 #include <sys/lock.h>
   41 #include <sys/mutex.h>
   42 #include <sys/proc.h>
   43 
   44 #include <dev/agp/agppriv.h>
   45 #include <dev/agp/agpreg.h>
   46 #include <dev/pci/pcivar.h>
   47 #include <dev/pci/pcireg.h>
   48 
   49 #include <vm/vm.h>
   50 #include <vm/vm_extern.h>
   51 #include <vm/vm_kern.h>
   52 #include <vm/vm_object.h>
   53 #include <vm/pmap.h>
   54 #include <machine/bus.h>
   55 #include <machine/resource.h>
   56 #include <sys/rman.h>
   57 
   58 MALLOC_DECLARE(M_AGP);
   59 
   60 #define READ4(off)      bus_space_read_4(sc->bst, sc->bsh, off)
   61 #define WRITE4(off,v)   bus_space_write_4(sc->bst, sc->bsh, off, v)
   62 
   63 struct agp_ati_softc {
   64         struct agp_softc agp;
   65         struct resource *regs;  /* memory mapped control registers */
   66         bus_space_tag_t bst;    /* bus_space tag */
   67         bus_space_handle_t bsh; /* bus_space handle */
   68         u_int32_t       initial_aperture; /* aperture size at startup */
   69         char            is_rs300;
   70 
   71         /* The GATT */
   72         u_int32_t       ag_entries;
   73         u_int32_t      *ag_virtual;     /* virtual address of gatt */
   74         u_int32_t      *ag_vdir;        /* virtual address of page dir */
   75         vm_offset_t     ag_pdir;        /* physical address of page dir */
   76 };
   77 
   78 
   79 static const char*
   80 agp_ati_match(device_t dev)
   81 {
   82         if (pci_get_class(dev) != PCIC_BRIDGE ||
   83             pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
   84                 return NULL;
   85 
   86         if (agp_find_caps(dev) == 0)
   87                 return NULL;
   88 
   89         switch (pci_get_devid(dev)) {
   90         case 0xcab01002:
   91                 return ("ATI RS100 AGP bridge");
   92         case 0xcab21002:
   93                 return ("ATI RS200 AGP bridge");
   94         case 0xcbb21002:
   95                 return ("ATI RS200M AGP bridge");
   96         case 0xcab31002:
   97                 return ("ATI RS250 AGP bridge");
   98         case 0x58301002:
   99                 return ("ATI RS300_100 AGP bridge");
  100         case 0x58311002:
  101                 return ("ATI RS300_133 AGP bridge");
  102         case 0x58321002:
  103                 return ("ATI RS300_166 AGP bridge");
  104         case 0x58331002:
  105                 return ("ATI RS300_200 AGP bridge");
  106         }
  107 
  108         return NULL;
  109 }
  110 
  111 static int
  112 agp_ati_probe(device_t dev)
  113 {
  114         const char *desc;
  115 
  116         desc = agp_ati_match(dev);
  117         if (desc) {
  118                 device_set_desc(dev, desc);
  119                 return 0;
  120         }
  121 
  122         return ENXIO;
  123 }
  124 
  125 static int
  126 agp_ati_alloc_gatt(device_t dev)
  127 {
  128         struct agp_ati_softc *sc = device_get_softc(dev);
  129         u_int32_t apsize = AGP_GET_APERTURE(dev);
  130         u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
  131         u_int32_t apbase_offset;
  132         int i;
  133 
  134         /* Alloc the GATT -- pointers to pages of AGP memory */
  135         sc->ag_entries = entries;
  136         sc->ag_virtual = (void *)kmem_alloc_attr(entries * sizeof(u_int32_t),
  137             M_NOWAIT | M_ZERO, 0, ~0, VM_MEMATTR_WRITE_COMBINING);
  138         if (sc->ag_virtual == NULL) {
  139                 if (bootverbose)
  140                         device_printf(dev, "GATT allocation failed\n");
  141                 return ENOMEM;
  142         }
  143 
  144         /* Alloc the page directory -- pointers to each page of the GATT */
  145         sc->ag_vdir = (void *)kmem_alloc_attr(AGP_PAGE_SIZE, M_NOWAIT | M_ZERO,
  146             0, ~0, VM_MEMATTR_WRITE_COMBINING);
  147         if (sc->ag_vdir == NULL) {
  148                 if (bootverbose)
  149                         device_printf(dev, "pagedir allocation failed\n");
  150                 kmem_free((vm_offset_t)sc->ag_virtual, entries *
  151                     sizeof(u_int32_t));
  152                 return ENOMEM;
  153         }
  154         sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
  155 
  156         apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
  157         /* Fill in the pagedir's pointers to GATT pages */
  158         for (i = 0; i < sc->ag_entries / 1024; i++) {
  159                 vm_offset_t va;
  160                 vm_offset_t pa;
  161 
  162                 va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
  163                 pa = vtophys(va);
  164                 sc->ag_vdir[apbase_offset + i] = pa | 1;
  165         }
  166 
  167         return 0;
  168 }
  169 
  170 
  171 static int
  172 agp_ati_attach(device_t dev)
  173 {
  174         struct agp_ati_softc *sc = device_get_softc(dev);
  175         int error, rid;
  176         u_int32_t temp;
  177         u_int32_t apsize_reg, agpmode_reg;
  178 
  179         error = agp_generic_attach(dev);
  180         if (error)
  181                 return error;
  182 
  183         switch (pci_get_devid(dev)) {
  184         case 0xcab01002: /* ATI RS100 AGP bridge */
  185         case 0xcab21002: /* ATI RS200 AGP bridge */
  186         case 0xcbb21002: /* ATI RS200M AGP bridge */
  187         case 0xcab31002: /* ATI RS250 AGP bridge */
  188                 sc->is_rs300 = 0;
  189                 apsize_reg = ATI_RS100_APSIZE;
  190                 agpmode_reg = ATI_RS100_IG_AGPMODE;
  191                 break;
  192         case 0x58301002: /* ATI RS300_100 AGP bridge */
  193         case 0x58311002: /* ATI RS300_133 AGP bridge */
  194         case 0x58321002: /* ATI RS300_166 AGP bridge */
  195         case 0x58331002: /* ATI RS300_200 AGP bridge */
  196                 sc->is_rs300 = 1;
  197                 apsize_reg = ATI_RS300_APSIZE;
  198                 agpmode_reg = ATI_RS300_IG_AGPMODE;
  199                 break;
  200         default:
  201                 /* Unknown chipset */
  202                 return EINVAL;
  203         }
  204 
  205         rid = ATI_GART_MMADDR;
  206         sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
  207         if (!sc->regs) {
  208                 agp_generic_detach(dev);
  209                 return ENOMEM;
  210         }
  211 
  212         sc->bst = rman_get_bustag(sc->regs);
  213         sc->bsh = rman_get_bushandle(sc->regs);
  214 
  215         sc->initial_aperture = AGP_GET_APERTURE(dev);
  216 
  217         for (;;) {
  218                 if (agp_ati_alloc_gatt(dev) == 0)
  219                         break;
  220 
  221                 /*
  222                  * Probably contigmalloc failure. Try reducing the
  223                  * aperture so that the gatt size reduces.
  224                  */
  225                 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
  226                         return ENOMEM;
  227         }
  228 
  229         temp = pci_read_config(dev, apsize_reg, 4);
  230         pci_write_config(dev, apsize_reg, temp | 1, 4);
  231 
  232         pci_write_config(dev, agpmode_reg, 0x20000, 4);
  233 
  234         WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
  235 
  236         temp = pci_read_config(dev, 4, 4);      /* XXX: Magic reg# */
  237         pci_write_config(dev, 4, temp | (1 << 14), 4);
  238 
  239         WRITE4(ATI_GART_BASE, sc->ag_pdir);
  240 
  241         AGP_FLUSH_TLB(dev);
  242 
  243         return 0;
  244 }
  245 
  246 static int
  247 agp_ati_detach(device_t dev)
  248 {
  249         struct agp_ati_softc *sc = device_get_softc(dev);
  250         u_int32_t apsize_reg, temp;
  251 
  252         agp_free_cdev(dev);
  253 
  254         if (sc->is_rs300)
  255                 apsize_reg = ATI_RS300_APSIZE;
  256         else
  257                 apsize_reg = ATI_RS100_APSIZE;
  258 
  259         /* Clear the GATT base */
  260         WRITE4(ATI_GART_BASE, 0);
  261 
  262         /* Put the aperture back the way it started. */
  263         AGP_SET_APERTURE(dev, sc->initial_aperture);
  264 
  265         temp = pci_read_config(dev, apsize_reg, 4);
  266         pci_write_config(dev, apsize_reg, temp & ~1, 4);
  267 
  268         kmem_free((vm_offset_t)sc->ag_vdir, AGP_PAGE_SIZE);
  269         kmem_free((vm_offset_t)sc->ag_virtual, sc->ag_entries *
  270             sizeof(u_int32_t));
  271 
  272         bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
  273         agp_free_res(dev);
  274 
  275         return 0;
  276 }
  277 
  278 static u_int32_t
  279 agp_ati_get_aperture(device_t dev)
  280 {
  281         struct agp_ati_softc *sc = device_get_softc(dev);
  282         int size_value;
  283 
  284         if (sc->is_rs300)
  285                 size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
  286         else
  287                 size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
  288 
  289         size_value = (size_value & 0x0000000e) >> 1;
  290         size_value = (32 * 1024 * 1024) << size_value;
  291 
  292         return size_value;
  293 }
  294 
  295 static int
  296 agp_ati_set_aperture(device_t dev, u_int32_t aperture)
  297 {
  298         struct agp_ati_softc *sc = device_get_softc(dev);
  299         int size_value;
  300         u_int32_t apsize_reg;
  301 
  302         if (sc->is_rs300)
  303                 apsize_reg = ATI_RS300_APSIZE;
  304         else
  305                 apsize_reg = ATI_RS100_APSIZE;
  306 
  307         size_value = pci_read_config(dev, apsize_reg, 4);
  308 
  309         size_value &= ~0x0000000e;
  310         size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
  311 
  312         pci_write_config(dev, apsize_reg, size_value, 4);
  313 
  314         return 0;
  315 }
  316 
  317 static int
  318 agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
  319 {
  320         struct agp_ati_softc *sc = device_get_softc(dev);
  321 
  322         if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
  323                 return EINVAL;
  324 
  325         sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
  326 
  327         return 0;
  328 }
  329 
  330 static int
  331 agp_ati_unbind_page(device_t dev, vm_offset_t offset)
  332 {
  333         struct agp_ati_softc *sc = device_get_softc(dev);
  334 
  335         if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
  336                 return EINVAL;
  337 
  338         sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
  339         return 0;
  340 }
  341 
  342 static void
  343 agp_ati_flush_tlb(device_t dev)
  344 {
  345         struct agp_ati_softc *sc = device_get_softc(dev);
  346 
  347         /* Set the cache invalidate bit and wait for the chipset to clear */
  348         WRITE4(ATI_GART_CACHE_CNTRL, 1);
  349         (void)READ4(ATI_GART_CACHE_CNTRL);
  350 }
  351 
  352 static device_method_t agp_ati_methods[] = {
  353         /* Device interface */
  354         DEVMETHOD(device_probe,         agp_ati_probe),
  355         DEVMETHOD(device_attach,        agp_ati_attach),
  356         DEVMETHOD(device_detach,        agp_ati_detach),
  357         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  358         DEVMETHOD(device_suspend,       bus_generic_suspend),
  359         DEVMETHOD(device_resume,        bus_generic_resume),
  360 
  361         /* AGP interface */
  362         DEVMETHOD(agp_get_aperture,     agp_ati_get_aperture),
  363         DEVMETHOD(agp_set_aperture,     agp_ati_set_aperture),
  364         DEVMETHOD(agp_bind_page,        agp_ati_bind_page),
  365         DEVMETHOD(agp_unbind_page,      agp_ati_unbind_page),
  366         DEVMETHOD(agp_flush_tlb,        agp_ati_flush_tlb),
  367         DEVMETHOD(agp_enable,           agp_generic_enable),
  368         DEVMETHOD(agp_alloc_memory,     agp_generic_alloc_memory),
  369         DEVMETHOD(agp_free_memory,      agp_generic_free_memory),
  370         DEVMETHOD(agp_bind_memory,      agp_generic_bind_memory),
  371         DEVMETHOD(agp_unbind_memory,    agp_generic_unbind_memory),
  372 
  373         { 0, 0 }
  374 };
  375 
  376 static driver_t agp_ati_driver = {
  377         "agp",
  378         agp_ati_methods,
  379         sizeof(struct agp_ati_softc),
  380 };
  381 
  382 static devclass_t agp_devclass;
  383 
  384 DRIVER_MODULE(agp_ati, hostb, agp_ati_driver, agp_devclass, 0, 0);
  385 MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
  386 MODULE_DEPEND(agp_ati, pci, 1, 1, 1);

Cache object: 7f921def2f3a01c691ab5b7e3432c23e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.