463514a526a7c14972b7a67e341abe608bcbe870
[dragonfly.git] / sys / dev / agp / agp_ati.c
1 /*-
2  * Copyright (c) 2005 Eric Anholt
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  * Based on reading the Linux 2.6.8.1 driver by Dave Jones.
27  *
28  * $FreeBSD: src/sys/dev/agp/agp_ati.c,v 1.5 2007/11/12 21:51:36 jhb Exp $
29  */
30
31 #include "opt_bus.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/module.h>
38 #include <sys/bus.h>
39 #include <sys/lock.h>
40 #include <sys/proc.h>
41
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44 #include "agppriv.h"
45 #include "agpreg.h"
46
47 #include <vm/vm.h>
48 #include <vm/vm_object.h>
49 #include <vm/pmap.h>
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52
53 MALLOC_DECLARE(M_AGP);
54
55 #define READ4(off)      bus_space_read_4(sc->bst, sc->bsh, off)
56 #define WRITE4(off,v)   bus_space_write_4(sc->bst, sc->bsh, off, v)
57
58 struct agp_ati_softc {
59         struct agp_softc agp;
60         struct resource *regs;  /* memory mapped control registers */
61         bus_space_tag_t bst;    /* bus_space tag */
62         bus_space_handle_t bsh; /* bus_space handle */
63         u_int32_t       initial_aperture; /* aperture size at startup */
64         char            is_rs300;
65
66         /* The GATT */
67         u_int32_t       ag_entries;
68         u_int32_t      *ag_virtual;     /* virtual address of gatt */
69         u_int32_t      *ag_vdir;        /* virtual address of page dir */
70         vm_offset_t     ag_pdir;        /* physical address of page dir */
71 };
72
73
74 static const char*
75 agp_ati_match(device_t dev)
76 {
77         if (pci_get_class(dev) != PCIC_BRIDGE ||
78             pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
79                 return NULL;
80
81         if (agp_find_caps(dev) == 0)
82                 return NULL;
83
84         switch (pci_get_devid(dev)) {
85         case 0xcab01002:
86                 return ("ATI RS100 AGP bridge");
87         case 0xcab21002:
88                 return ("ATI RS200 AGP bridge");
89         case 0xcbb21002:
90                 return ("ATI RS200M AGP bridge");
91         case 0xcab31002:
92                 return ("ATI RS250 AGP bridge");
93         case 0x58301002:
94                 return ("ATI RS300_100 AGP bridge");
95         case 0x58311002:
96                 return ("ATI RS300_133 AGP bridge");
97         case 0x58321002:
98                 return ("ATI RS300_166 AGP bridge");
99         case 0x58331002:
100                 return ("ATI RS300_200 AGP bridge");
101         };
102
103         return NULL;
104 }
105
106 static int
107 agp_ati_probe(device_t dev)
108 {
109         const char *desc;
110
111         desc = agp_ati_match(dev);
112         if (desc) {
113                 device_verbose(dev);
114                 device_set_desc(dev, desc);
115                 return 0;
116         }
117
118         return ENXIO;
119 }
120
121 static int
122 agp_ati_alloc_gatt(device_t dev)
123 {
124         struct agp_ati_softc *sc = device_get_softc(dev);
125         u_int32_t apsize = AGP_GET_APERTURE(dev);
126         u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
127         u_int32_t apbase_offset;
128         int i;
129
130         /* Alloc the GATT -- pointers to pages of AGP memory */
131         sc->ag_entries = entries;
132         sc->ag_virtual = kmalloc(entries * sizeof(u_int32_t), M_AGP,
133             M_NOWAIT | M_ZERO);
134         if (sc->ag_virtual == NULL) {
135                 if (bootverbose)
136                         device_printf(dev, "aperture allocation failed\n");
137                 return ENOMEM;
138         }
139
140         /* Alloc the page directory -- pointers to each page of the GATT */
141         sc->ag_vdir = kmalloc(AGP_PAGE_SIZE, M_AGP, M_NOWAIT | M_ZERO);
142         if (sc->ag_vdir == NULL) {
143                 if (bootverbose)
144                         device_printf(dev, "pagedir allocation failed\n");
145                 kfree(sc->ag_virtual, M_AGP);
146                 return ENOMEM;
147         }
148         sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
149
150         apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
151         /* Fill in the pagedir's pointers to GATT pages */
152         for (i = 0; i < sc->ag_entries / 1024; i++) {
153                 vm_offset_t va;
154                 vm_offset_t pa;
155
156                 va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
157                 pa = vtophys(va);
158                 sc->ag_vdir[apbase_offset + i] = pa | 1;
159         }
160
161         /*
162          * Make sure the chipset can see everything.
163          */
164         agp_flush_cache();
165
166         return 0;
167 }
168
169
170 static int
171 agp_ati_attach(device_t dev)
172 {
173         struct agp_ati_softc *sc = device_get_softc(dev);
174         int error, rid;
175         u_int32_t temp;
176         u_int32_t apsize_reg, agpmode_reg;
177
178         error = agp_generic_attach(dev);
179         if (error)
180                 return error;
181
182         switch (pci_get_devid(dev)) {
183         case 0xcab01002: /* ATI RS100 AGP bridge */
184         case 0xcab21002: /* ATI RS200 AGP bridge */
185         case 0xcbb21002: /* ATI RS200M AGP bridge */
186         case 0xcab31002: /* ATI RS250 AGP bridge */
187                 sc->is_rs300 = 0;
188                 apsize_reg = ATI_RS100_APSIZE;
189                 agpmode_reg = ATI_RS100_IG_AGPMODE;
190                 break;
191         case 0x58301002: /* ATI RS300_100 AGP bridge */
192         case 0x58311002: /* ATI RS300_133 AGP bridge */
193         case 0x58321002: /* ATI RS300_166 AGP bridge */
194         case 0x58331002: /* ATI RS300_200 AGP bridge */
195                 sc->is_rs300 = 1;
196                 apsize_reg = ATI_RS300_APSIZE;
197                 agpmode_reg = ATI_RS300_IG_AGPMODE;
198                 break;
199         default:
200                 /* Unknown chipset */
201                 return EINVAL;
202         };
203
204         rid = ATI_GART_MMADDR;
205         sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
206         if (!sc->regs) {
207                 agp_generic_detach(dev);
208                 return ENOMEM;
209         }
210
211         sc->bst = rman_get_bustag(sc->regs);
212         sc->bsh = rman_get_bushandle(sc->regs);
213
214         sc->initial_aperture = AGP_GET_APERTURE(dev);
215         if (sc->initial_aperture == 0) {
216                 device_printf(dev, "bad initial aperture size, disabling\n");
217                 return ENXIO;
218         }
219
220         for (;;) {
221                 if (agp_ati_alloc_gatt(dev) == 0)
222                         break;
223
224                 /*
225                  * Probably contigmalloc failure. Try reducing the
226                  * aperture so that the gatt size reduces.
227                  */
228                 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
229                         return ENOMEM;
230         }
231
232         temp = pci_read_config(dev, apsize_reg, 4);
233         pci_write_config(dev, apsize_reg, temp | 1, 4);
234
235         pci_write_config(dev, agpmode_reg, 0x20000, 4);
236
237         WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
238
239         temp = pci_read_config(dev, 4, 4);      /* XXX: Magic reg# */
240         pci_write_config(dev, 4, temp | (1 << 14), 4);
241
242         WRITE4(ATI_GART_BASE, sc->ag_pdir);
243
244         AGP_FLUSH_TLB(dev);
245
246         return 0;
247 }
248
249 static int
250 agp_ati_detach(device_t dev)
251 {
252         struct agp_ati_softc *sc = device_get_softc(dev);
253         u_int32_t apsize_reg, temp;
254
255         agp_free_cdev(dev);
256
257         if (sc->is_rs300)
258                 apsize_reg = ATI_RS300_APSIZE;
259         else
260                 apsize_reg = ATI_RS100_APSIZE;
261
262         /* Clear the GATT base */
263         WRITE4(ATI_GART_BASE, 0);
264
265         /* Put the aperture back the way it started. */
266         AGP_SET_APERTURE(dev, sc->initial_aperture);
267
268         temp = pci_read_config(dev, apsize_reg, 4);
269         pci_write_config(dev, apsize_reg, temp & ~1, 4);
270
271         kfree(sc->ag_vdir, M_AGP);
272         kfree(sc->ag_virtual, M_AGP);
273
274         bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
275         agp_free_res(dev);
276
277         return 0;
278 }
279
280 static u_int32_t
281 agp_ati_get_aperture(device_t dev)
282 {
283         struct agp_ati_softc *sc = device_get_softc(dev);
284         int size_value;
285
286         if (sc->is_rs300)
287                 size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
288         else
289                 size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
290
291         size_value = (size_value & 0x0000000e) >> 1;
292         size_value = (32 * 1024 * 1024) << size_value;
293
294         return size_value;
295 }
296
297 static int
298 agp_ati_set_aperture(device_t dev, u_int32_t aperture)
299 {
300         struct agp_ati_softc *sc = device_get_softc(dev);
301         int size_value;
302         u_int32_t apsize_reg;
303
304         if (sc->is_rs300)
305                 apsize_reg = ATI_RS300_APSIZE;
306         else
307                 apsize_reg = ATI_RS100_APSIZE;
308
309         size_value = pci_read_config(dev, apsize_reg, 4);
310
311         size_value &= ~0x0000000e;
312         size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
313
314         pci_write_config(dev, apsize_reg, size_value, 4);
315
316         return 0;
317 }
318
319 static int
320 agp_ati_bind_page(device_t dev, int offset, vm_offset_t physical)
321 {
322         struct agp_ati_softc *sc = device_get_softc(dev);
323
324         if (offset < 0 || offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
325                 return EINVAL;
326
327         sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
328
329         return 0;
330 }
331
332 static int
333 agp_ati_unbind_page(device_t dev, int offset)
334 {
335         struct agp_ati_softc *sc = device_get_softc(dev);
336
337         if (offset < 0 || offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
338                 return EINVAL;
339
340         sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
341         return 0;
342 }
343
344 static void
345 agp_ati_flush_tlb(device_t dev)
346 {
347         struct agp_ati_softc *sc = device_get_softc(dev);
348
349         /* Set the cache invalidate bit and wait for the chipset to clear */
350         WRITE4(ATI_GART_CACHE_CNTRL, 1);
351         (void)READ4(ATI_GART_CACHE_CNTRL);
352 }
353
354 static device_method_t agp_ati_methods[] = {
355         /* Device interface */
356         DEVMETHOD(device_probe,         agp_ati_probe),
357         DEVMETHOD(device_attach,        agp_ati_attach),
358         DEVMETHOD(device_detach,        agp_ati_detach),
359         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
360         DEVMETHOD(device_suspend,       bus_generic_suspend),
361         DEVMETHOD(device_resume,        bus_generic_resume),
362
363         /* AGP interface */
364         DEVMETHOD(agp_get_aperture,     agp_ati_get_aperture),
365         DEVMETHOD(agp_set_aperture,     agp_ati_set_aperture),
366         DEVMETHOD(agp_bind_page,        agp_ati_bind_page),
367         DEVMETHOD(agp_unbind_page,      agp_ati_unbind_page),
368         DEVMETHOD(agp_flush_tlb,        agp_ati_flush_tlb),
369         DEVMETHOD(agp_enable,           agp_generic_enable),
370         DEVMETHOD(agp_alloc_memory,     agp_generic_alloc_memory),
371         DEVMETHOD(agp_free_memory,      agp_generic_free_memory),
372         DEVMETHOD(agp_bind_memory,      agp_generic_bind_memory),
373         DEVMETHOD(agp_unbind_memory,    agp_generic_unbind_memory),
374
375         { 0, 0 }
376 };
377
378 static driver_t agp_ati_driver = {
379         "agp",
380         agp_ati_methods,
381         sizeof(struct agp_ati_softc),
382 };
383
384 static devclass_t agp_devclass;
385
386 DRIVER_MODULE(agp_ati, pci, agp_ati_driver, agp_devclass, 0, 0);
387 MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
388 MODULE_DEPEND(agp_ati, pci, 1, 1, 1);