Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / agp / agp_amd.c
1 /*-
2  * Copyright (c) 2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/agp/agp_amd.c,v 1.25 2007/11/12 21:51:36 jhb Exp $
27  */
28
29 #include "opt_bus.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/lock.h>
37 #include <sys/rman.h>
38
39 #include <bus/pci/pcivar.h>
40 #include <bus/pci/pcireg.h>
41 #include "agppriv.h"
42 #include "agpreg.h"
43
44 #include <vm/vm.h>
45 #include <vm/vm_object.h>
46 #include <vm/pmap.h>
47
48 MALLOC_DECLARE(M_AGP);
49
50 #define READ2(off)      bus_space_read_2(sc->bst, sc->bsh, off)
51 #define READ4(off)      bus_space_read_4(sc->bst, sc->bsh, off)
52 #define WRITE2(off,v)   bus_space_write_2(sc->bst, sc->bsh, off, v)
53 #define WRITE4(off,v)   bus_space_write_4(sc->bst, sc->bsh, off, v)
54
55 struct agp_amd_gatt {
56         u_int32_t       ag_entries;
57         u_int32_t      *ag_virtual;     /* virtual address of gatt */
58         vm_offset_t     ag_physical;
59         u_int32_t      *ag_vdir;        /* virtual address of page dir */
60         vm_offset_t     ag_pdir;        /* physical address of page dir */
61 };
62
63 struct agp_amd_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         struct agp_amd_gatt    *gatt;
70 };
71
72 static struct agp_amd_gatt *
73 agp_amd_alloc_gatt(device_t dev)
74 {
75         u_int32_t apsize = AGP_GET_APERTURE(dev);
76         u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
77         struct agp_amd_gatt *gatt;
78         int i, npages, pdir_offset;
79
80         if (bootverbose)
81                 device_printf(dev,
82                               "allocating GATT for aperture of size %dM\n",
83                               apsize / (1024*1024));
84
85         gatt = kmalloc(sizeof(struct agp_amd_gatt), M_AGP, M_INTWAIT);
86
87         /*
88          * The AMD751 uses a page directory to map a non-contiguous
89          * gatt so we don't need to use contigmalloc.
90          * Malloc individual gatt pages and map them into the page
91          * directory.
92          */
93         gatt->ag_entries = entries;
94         gatt->ag_virtual = kmalloc(entries * sizeof(u_int32_t),
95                                   M_AGP, M_INTWAIT | M_ZERO);
96
97         /*
98          * Allocate the page directory.
99          */
100         gatt->ag_vdir = kmalloc(AGP_PAGE_SIZE, M_AGP, M_INTWAIT | M_ZERO);
101
102         gatt->ag_pdir = vtophys((vm_offset_t) gatt->ag_vdir);
103         if(bootverbose)
104                 device_printf(dev, "gatt -> ag_pdir %#lx\n",
105                     (u_long)gatt->ag_pdir);
106         /*
107          * Allocate the gatt pages
108          */
109         gatt->ag_entries = entries;
110         if(bootverbose)
111                 device_printf(dev, "allocating GATT for %d AGP page entries\n", 
112                         gatt->ag_entries);
113
114         gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
115
116         /*
117          * Map the pages of the GATT into the page directory.
118          *
119          * The GATT page addresses are mapped into the directory offset by
120          * an amount dependent on the base address of the aperture. This
121          * is and offset into the page directory, not an offset added to
122          * the addresses of the gatt pages.
123          */
124
125         pdir_offset = pci_read_config(dev, AGP_AMD751_APBASE, 4) >> 22;
126
127         npages = ((entries * sizeof(u_int32_t) + AGP_PAGE_SIZE - 1)
128                   >> AGP_PAGE_SHIFT);
129
130         for (i = 0; i < npages; i++) {
131                 vm_offset_t va;
132                 vm_offset_t pa;
133
134                 va = ((vm_offset_t) gatt->ag_virtual) + i * AGP_PAGE_SIZE;
135                 pa = vtophys(va);
136                 gatt->ag_vdir[i + pdir_offset] = pa | 1;
137         }
138
139         /*
140          * Make sure the chipset can see everything.
141          */
142         agp_flush_cache();
143
144         return gatt;
145 }
146
147 static void
148 agp_amd_free_gatt(struct agp_amd_gatt *gatt)
149 {
150         kfree(gatt->ag_virtual, M_AGP);
151         kfree(gatt->ag_vdir, M_AGP);
152         kfree(gatt, M_AGP);
153 }
154
155 static const char*
156 agp_amd_match(device_t dev)
157 {
158         if (pci_get_class(dev) != PCIC_BRIDGE
159             || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
160                 return NULL;
161
162         if (agp_find_caps(dev) == 0)
163                 return NULL;
164
165         switch (pci_get_devid(dev)) {
166         case 0x70061022:
167                 return ("AMD 751 host to AGP bridge");
168         case 0x700e1022:
169                 return ("AMD 761 host to AGP bridge");
170         case 0x700c1022:
171                 return ("AMD 762 host to AGP bridge");
172         };
173
174         return NULL;
175 }
176
177 static int
178 agp_amd_probe(device_t dev)
179 {
180         const char *desc;
181
182         if (resource_disabled("agp", device_get_unit(dev)))
183                 return (ENXIO);
184         desc = agp_amd_match(dev);
185         if (desc) {
186                 device_verbose(dev);
187                 device_set_desc(dev, desc);
188                 return BUS_PROBE_DEFAULT;
189         }
190
191         return ENXIO;
192 }
193
194 static int
195 agp_amd_attach(device_t dev)
196 {
197         struct agp_amd_softc *sc = device_get_softc(dev);
198         struct agp_amd_gatt *gatt;
199         int error, rid;
200
201         error = agp_generic_attach(dev);
202         if (error)
203                 return error;
204
205         rid = AGP_AMD751_REGISTERS;
206         sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
207                                           RF_ACTIVE);
208         if (!sc->regs) {
209                 agp_generic_detach(dev);
210                 return ENOMEM;
211         }
212
213         sc->bst = rman_get_bustag(sc->regs);
214         sc->bsh = rman_get_bushandle(sc->regs);
215
216         sc->initial_aperture = AGP_GET_APERTURE(dev);
217         if (sc->initial_aperture == 0) {
218                 device_printf(dev, "bad initial aperture size, disabling\n");
219                 return ENXIO;
220         }
221
222         for (;;) {
223                 gatt = agp_amd_alloc_gatt(dev);
224                 if (gatt)
225                         break;
226
227                 /*
228                  * Probably contigmalloc failure. Try reducing the
229                  * aperture so that the gatt size reduces.
230                  */
231                 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
232                         return ENOMEM;
233         }
234         sc->gatt = gatt;
235
236         /* Install the gatt. */
237         WRITE4(AGP_AMD751_ATTBASE, gatt->ag_pdir);
238         
239         /* Enable synchronisation between host and agp. */
240         pci_write_config(dev,
241                          AGP_AMD751_MODECTRL,
242                          AGP_AMD751_MODECTRL_SYNEN, 1);
243
244         /* Set indexing mode for two-level and enable page dir cache */
245         pci_write_config(dev,
246                          AGP_AMD751_MODECTRL2,
247                          AGP_AMD751_MODECTRL2_GPDCE, 1);
248
249         /* Enable the TLB and flush */
250         WRITE2(AGP_AMD751_STATUS,
251                READ2(AGP_AMD751_STATUS) | AGP_AMD751_STATUS_GCE);
252         AGP_FLUSH_TLB(dev);
253
254         return 0;
255 }
256
257 static int
258 agp_amd_detach(device_t dev)
259 {
260         struct agp_amd_softc *sc = device_get_softc(dev);
261
262         agp_free_cdev(dev);
263
264         /* Disable the TLB.. */
265         WRITE2(AGP_AMD751_STATUS,
266                READ2(AGP_AMD751_STATUS) & ~AGP_AMD751_STATUS_GCE);
267         
268         /* Disable host-agp sync */
269         pci_write_config(dev, AGP_AMD751_MODECTRL, 0x00, 1);
270         
271         /* Clear the GATT base */
272         WRITE4(AGP_AMD751_ATTBASE, 0);
273
274         /* Put the aperture back the way it started. */
275         AGP_SET_APERTURE(dev, sc->initial_aperture);
276
277         agp_amd_free_gatt(sc->gatt);
278         agp_free_res(dev);
279
280         bus_release_resource(dev, SYS_RES_MEMORY,
281                              AGP_AMD751_REGISTERS, sc->regs);
282
283         return 0;
284 }
285
286 static u_int32_t
287 agp_amd_get_aperture(device_t dev)
288 {
289         int vas;
290
291         /*
292          * The aperture size is equal to 32M<<vas.
293          */
294         vas = (pci_read_config(dev, AGP_AMD751_APCTRL, 1) & 0x06) >> 1;
295         return (32*1024*1024) << vas;
296 }
297
298 static int
299 agp_amd_set_aperture(device_t dev, u_int32_t aperture)
300 {
301         int vas;
302
303         /*
304          * Check for a power of two and make sure its within the
305          * programmable range.
306          */
307         if (aperture & (aperture - 1)
308             || aperture < 32*1024*1024
309             || aperture > 2U*1024*1024*1024)
310                 return EINVAL;
311
312         vas = ffs(aperture / 32*1024*1024) - 1;
313         
314         /* 
315          * While the size register is bits 1-3 of APCTRL, bit 0 must be
316          * set for the size value to be 'valid'
317          */
318         pci_write_config(dev, AGP_AMD751_APCTRL,
319                          (((pci_read_config(dev, AGP_AMD751_APCTRL, 1) & ~0x06)
320                           | ((vas << 1) | 1))), 1);
321
322         return 0;
323 }
324
325 static int
326 agp_amd_bind_page(device_t dev, int offset, vm_offset_t physical)
327 {
328         struct agp_amd_softc *sc = device_get_softc(dev);
329
330         if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
331                 return EINVAL;
332
333         sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
334
335         /* invalidate the cache */
336         AGP_FLUSH_TLB(dev);
337         return 0;
338 }
339
340 static int
341 agp_amd_unbind_page(device_t dev, int offset)
342 {
343         struct agp_amd_softc *sc = device_get_softc(dev);
344
345         if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
346                 return EINVAL;
347
348         sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
349         return 0;
350 }
351
352 static void
353 agp_amd_flush_tlb(device_t dev)
354 {
355         struct agp_amd_softc *sc = device_get_softc(dev);
356
357         /* Set the cache invalidate bit and wait for the chipset to clear */
358         WRITE4(AGP_AMD751_TLBCTRL, 1);
359         do {
360                 DELAY(1);
361         } while (READ4(AGP_AMD751_TLBCTRL));
362 }
363
364 static device_method_t agp_amd_methods[] = {
365         /* Device interface */
366         DEVMETHOD(device_probe,         agp_amd_probe),
367         DEVMETHOD(device_attach,        agp_amd_attach),
368         DEVMETHOD(device_detach,        agp_amd_detach),
369         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
370         DEVMETHOD(device_suspend,       bus_generic_suspend),
371         DEVMETHOD(device_resume,        bus_generic_resume),
372
373         /* AGP interface */
374         DEVMETHOD(agp_get_aperture,     agp_amd_get_aperture),
375         DEVMETHOD(agp_set_aperture,     agp_amd_set_aperture),
376         DEVMETHOD(agp_bind_page,        agp_amd_bind_page),
377         DEVMETHOD(agp_unbind_page,      agp_amd_unbind_page),
378         DEVMETHOD(agp_flush_tlb,        agp_amd_flush_tlb),
379         DEVMETHOD(agp_enable,           agp_generic_enable),
380         DEVMETHOD(agp_alloc_memory,     agp_generic_alloc_memory),
381         DEVMETHOD(agp_free_memory,      agp_generic_free_memory),
382         DEVMETHOD(agp_bind_memory,      agp_generic_bind_memory),
383         DEVMETHOD(agp_unbind_memory,    agp_generic_unbind_memory),
384
385         { 0, 0 }
386 };
387
388 static driver_t agp_amd_driver = {
389         "agp",
390         agp_amd_methods,
391         sizeof(struct agp_amd_softc),
392 };
393
394 static devclass_t agp_devclass;
395
396 DRIVER_MODULE(agp_amd, pci, agp_amd_driver, agp_devclass, NULL, NULL);
397 MODULE_DEPEND(agp_amd, agp, 1, 1, 1);
398 MODULE_DEPEND(agp_amd, pci, 1, 1, 1);