Merge from vendor branch SENDMAIL:
[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/pci/agp_amd.c,v 1.3.2.4 2002/04/25 23:41:36 cokane Exp $
27  *      $DragonFly: src/sys/dev/agp/agp_amd.c,v 1.5 2004/03/24 20:42:12 dillon Exp $
28  */
29
30 #include "opt_bus.h"
31 #include "opt_pci.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39
40 #include <bus/pci/pcivar.h>
41 #include <bus/pci/pcireg.h>
42 #include "agppriv.h"
43 #include "agpreg.h"
44
45 #include <vm/vm.h>
46 #include <vm/vm_object.h>
47 #include <vm/pmap.h>
48 #include <machine/clock.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <sys/rman.h>
52
53 MALLOC_DECLARE(M_AGP);
54
55 #define READ2(off)      bus_space_read_2(sc->bst, sc->bsh, off)
56 #define READ4(off)      bus_space_read_4(sc->bst, sc->bsh, off)
57 #define WRITE2(off,v)   bus_space_write_2(sc->bst, sc->bsh, off, v)
58 #define WRITE4(off,v)   bus_space_write_4(sc->bst, sc->bsh, off, v)
59
60 struct agp_amd_gatt {
61         u_int32_t       ag_entries;
62         u_int32_t      *ag_virtual;     /* virtual address of gatt */
63         vm_offset_t     ag_physical;
64         u_int32_t      *ag_vdir;        /* virtual address of page dir */
65         vm_offset_t     ag_pdir;        /* physical address of page dir */
66 };
67
68 struct agp_amd_softc {
69         struct agp_softc agp;
70         struct resource *regs;  /* memory mapped control registers */
71         bus_space_tag_t bst;    /* bus_space tag */
72         bus_space_handle_t bsh; /* bus_space handle */
73         u_int32_t       initial_aperture; /* aperture size at startup */
74         struct agp_amd_gatt *gatt;
75 };
76
77 static struct agp_amd_gatt *
78 agp_amd_alloc_gatt(device_t dev)
79 {
80         u_int32_t apsize = AGP_GET_APERTURE(dev);
81         u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
82         struct agp_amd_gatt *gatt;
83         int i, npages, pdir_offset;
84
85         if (bootverbose)
86                 device_printf(dev,
87                               "allocating GATT for aperture of size %dM\n",
88                               apsize / (1024*1024));
89
90         gatt = malloc(sizeof(struct agp_amd_gatt), M_AGP, M_INTWAIT);
91
92         /*
93          * The AMD751 uses a page directory to map a non-contiguous
94          * gatt so we don't need to use contigmalloc.
95          * Malloc individual gatt pages and map them into the page
96          * directory.
97          */
98         gatt->ag_entries = entries;
99         gatt->ag_virtual = malloc(entries * sizeof(u_int32_t),
100                                   M_AGP, M_INTWAIT | M_ZERO);
101
102         /*
103          * Allocate the page directory.
104          */
105         gatt->ag_vdir = malloc(AGP_PAGE_SIZE, M_AGP, M_INTWAIT | M_ZERO);
106         gatt->ag_pdir = vtophys((vm_offset_t) gatt->ag_vdir);
107         if(bootverbose)
108                 device_printf(dev, "gatt -> ag_pdir %8x\n",
109                                 (vm_offset_t)gatt->ag_pdir);
110         /*
111          * Allocate the gatt pages
112          */
113         gatt->ag_entries = entries;
114         if(bootverbose)
115                 device_printf(dev, "allocating GATT for %d AGP page entries\n", 
116                         gatt->ag_entries);
117         gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
118
119         /*
120          * Map the pages of the GATT into the page directory.
121          *
122          * The GATT page addresses are mapped into the directory offset by
123          * an amount dependent on the base address of the aperture. This
124          * is and offset into the page directory, not an offset added to
125          * the addresses of the gatt pages.
126          */
127
128         pdir_offset = pci_read_config(dev, AGP_AMD751_APBASE, 4) >> 22;
129
130         npages = ((entries * sizeof(u_int32_t) + AGP_PAGE_SIZE - 1)
131                   >> AGP_PAGE_SHIFT);
132
133         for (i = 0; i < npages; i++) {
134                 vm_offset_t va;
135                 vm_offset_t pa;
136
137                 va = ((vm_offset_t) gatt->ag_virtual) + i * AGP_PAGE_SIZE;
138                 pa = vtophys(va);
139                 gatt->ag_vdir[i + pdir_offset] = pa | 1;
140         }
141
142         /*
143          * Make sure the chipset can see everything.
144          */
145         agp_flush_cache();
146
147         return gatt;
148 }
149
150 static void
151 agp_amd_free_gatt(struct agp_amd_gatt *gatt)
152 {
153         free(gatt->ag_virtual, M_AGP);
154         free(gatt->ag_vdir, M_AGP);
155         free(gatt, M_AGP);
156 }
157
158 static const char*
159 agp_amd_match(device_t dev)
160 {
161         if (pci_get_class(dev) != PCIC_BRIDGE
162             || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
163                 return NULL;
164
165         if (agp_find_caps(dev) == 0)
166                 return NULL;
167
168         switch (pci_get_devid(dev)) {
169
170         case 0x70061022:
171                 return ("AMD 751 host to AGP bridge");
172         
173         case 0x700e1022:
174                 return ("AMD 761 host to AGP bridge");
175
176         case 0x700c1022:
177                 return ("AMD 762 host to AGP bridge");
178
179         };
180
181         return NULL;
182 }
183
184 static int
185 agp_amd_probe(device_t dev)
186 {
187         const char *desc;
188
189         desc = agp_amd_match(dev);
190         if (desc) {
191                 device_verbose(dev);
192                 device_set_desc(dev, desc);
193                 return 0;
194         }
195
196         return ENXIO;
197 }
198
199 static int
200 agp_amd_attach(device_t dev)
201 {
202         struct agp_amd_softc *sc = device_get_softc(dev);
203         struct agp_amd_gatt *gatt;
204         int error, rid;
205
206         error = agp_generic_attach(dev);
207         if (error)
208                 return error;
209
210         rid = AGP_AMD751_REGISTERS;
211         sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
212                                       0, ~0, 1, RF_ACTIVE);
213         if (!sc->regs) {
214                 agp_generic_detach(dev);
215                 return ENOMEM;
216         }
217
218         sc->bst = rman_get_bustag(sc->regs);
219         sc->bsh = rman_get_bushandle(sc->regs);
220
221         sc->initial_aperture = AGP_GET_APERTURE(dev);
222         if (sc->initial_aperture == 0) {
223                 device_printf(dev, "bad initial aperture size, disabling\n");
224                 return ENXIO;
225         }
226
227         for (;;) {
228                 gatt = agp_amd_alloc_gatt(dev);
229                 if (gatt)
230                         break;
231
232                 /*
233                  * Probably contigmalloc failure. Try reducing the
234                  * aperture so that the gatt size reduces.
235                  */
236                 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
237                         return ENOMEM;
238         }
239         sc->gatt = gatt;
240
241         /* Install the gatt. */
242         WRITE4(AGP_AMD751_ATTBASE, gatt->ag_pdir);
243         
244         /* Enable synchronisation between host and agp. */
245         pci_write_config(dev,
246                          AGP_AMD751_MODECTRL,
247                          AGP_AMD751_MODECTRL_SYNEN, 1);
248
249         /* Set indexing mode for two-level and enable page dir cache */
250         pci_write_config(dev,
251                          AGP_AMD751_MODECTRL2,
252                          AGP_AMD751_MODECTRL2_GPDCE, 1);
253
254         /* Enable the TLB and flush */
255         WRITE2(AGP_AMD751_STATUS,
256                READ2(AGP_AMD751_STATUS) | AGP_AMD751_STATUS_GCE);
257         AGP_FLUSH_TLB(dev);
258
259         return 0;
260 }
261
262 static int
263 agp_amd_detach(device_t dev)
264 {
265         struct agp_amd_softc *sc = device_get_softc(dev);
266         int error;
267
268         error = agp_generic_detach(dev);
269         if (error)
270                 return error;
271
272         /* Disable the TLB.. */
273         WRITE2(AGP_AMD751_STATUS,
274                READ2(AGP_AMD751_STATUS) & ~AGP_AMD751_STATUS_GCE);
275         
276         /* Disable host-agp sync */
277         pci_write_config(dev, AGP_AMD751_MODECTRL, 0x00, 1);
278         
279         /* Clear the GATT base */
280         WRITE4(AGP_AMD751_ATTBASE, 0);
281
282         /* Put the aperture back the way it started. */
283         AGP_SET_APERTURE(dev, sc->initial_aperture);
284
285         agp_amd_free_gatt(sc->gatt);
286
287         bus_release_resource(dev, SYS_RES_MEMORY,
288                              AGP_AMD751_REGISTERS, sc->regs);
289
290         return 0;
291 }
292
293 static u_int32_t
294 agp_amd_get_aperture(device_t dev)
295 {
296         int vas;
297
298         /*
299          * The aperture size is equal to 32M<<vas.
300          */
301         vas = (pci_read_config(dev, AGP_AMD751_APCTRL, 1) & 0x06) >> 1;
302         return (32*1024*1024) << vas;
303 }
304
305 static int
306 agp_amd_set_aperture(device_t dev, u_int32_t aperture)
307 {
308         int vas;
309
310         /*
311          * Check for a power of two and make sure its within the
312          * programmable range.
313          */
314         if (aperture & (aperture - 1)
315             || aperture < 32*1024*1024
316             || aperture > 2U*1024*1024*1024)
317                 return EINVAL;
318
319         vas = ffs(aperture / 32*1024*1024) - 1;
320         
321         /* 
322          * While the size register is bits 1-3 of APCTRL, bit 0 must be
323          * set for the size value to be 'valid'
324          */
325         pci_write_config(dev, AGP_AMD751_APCTRL,
326                          (((pci_read_config(dev, AGP_AMD751_APCTRL, 1) & ~0x06)
327                           | ((vas << 1) | 1))), 1);
328
329         return 0;
330 }
331
332 static int
333 agp_amd_bind_page(device_t dev, int offset, vm_offset_t physical)
334 {
335         struct agp_amd_softc *sc = device_get_softc(dev);
336
337         if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
338                 return EINVAL;
339
340         sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
341
342         /* invalidate the cache */
343         AGP_FLUSH_TLB(dev);
344         return 0;
345 }
346
347 static int
348 agp_amd_unbind_page(device_t dev, int offset)
349 {
350         struct agp_amd_softc *sc = device_get_softc(dev);
351
352         if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
353                 return EINVAL;
354
355         sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
356         return 0;
357 }
358
359 static void
360 agp_amd_flush_tlb(device_t dev)
361 {
362         struct agp_amd_softc *sc = device_get_softc(dev);
363
364         /* Set the cache invalidate bit and wait for the chipset to clear */
365         WRITE4(AGP_AMD751_TLBCTRL, 1);
366         do {
367                 DELAY(1);
368         } while (READ4(AGP_AMD751_TLBCTRL));
369 }
370
371 static device_method_t agp_amd_methods[] = {
372         /* Device interface */
373         DEVMETHOD(device_probe,         agp_amd_probe),
374         DEVMETHOD(device_attach,        agp_amd_attach),
375         DEVMETHOD(device_detach,        agp_amd_detach),
376         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
377         DEVMETHOD(device_suspend,       bus_generic_suspend),
378         DEVMETHOD(device_resume,        bus_generic_resume),
379
380         /* AGP interface */
381         DEVMETHOD(agp_get_aperture,     agp_amd_get_aperture),
382         DEVMETHOD(agp_set_aperture,     agp_amd_set_aperture),
383         DEVMETHOD(agp_bind_page,        agp_amd_bind_page),
384         DEVMETHOD(agp_unbind_page,      agp_amd_unbind_page),
385         DEVMETHOD(agp_flush_tlb,        agp_amd_flush_tlb),
386         DEVMETHOD(agp_enable,           agp_generic_enable),
387         DEVMETHOD(agp_alloc_memory,     agp_generic_alloc_memory),
388         DEVMETHOD(agp_free_memory,      agp_generic_free_memory),
389         DEVMETHOD(agp_bind_memory,      agp_generic_bind_memory),
390         DEVMETHOD(agp_unbind_memory,    agp_generic_unbind_memory),
391
392         { 0, 0 }
393 };
394
395 static driver_t agp_amd_driver = {
396         "agp",
397         agp_amd_methods,
398         sizeof(struct agp_amd_softc),
399 };
400
401 static devclass_t agp_devclass;
402
403 DRIVER_MODULE(agp_amd, pci, agp_amd_driver, agp_devclass, 0, 0);
404 MODULE_DEPEND(agp_amd, agp, 1, 1, 1);
405 MODULE_DEPEND(agp_amd, pci, 1, 1, 1);