FreeBSD/Linux Kernel Cross Reference
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 <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/lock.h>
38 #include <sys/proc.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 <sys/rman.h>
49
50 MALLOC_DECLARE(M_AGP);
51
52 #define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
53 #define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
54
55 struct agp_ati_softc {
56 struct agp_softc agp;
57 struct resource *regs; /* memory mapped control registers */
58 bus_space_tag_t bst; /* bus_space tag */
59 bus_space_handle_t bsh; /* bus_space handle */
60 u_int32_t initial_aperture; /* aperture size at startup */
61 char is_rs300;
62
63 /* The GATT */
64 u_int32_t ag_entries;
65 u_int32_t *ag_virtual; /* virtual address of gatt */
66 u_int32_t *ag_vdir; /* virtual address of page dir */
67 vm_offset_t ag_pdir; /* physical address of page dir */
68 };
69
70
71 static const char*
72 agp_ati_match(device_t dev)
73 {
74 if (pci_get_class(dev) != PCIC_BRIDGE ||
75 pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
76 return NULL;
77
78 if (agp_find_caps(dev) == 0)
79 return NULL;
80
81 switch (pci_get_devid(dev)) {
82 case 0xcab01002:
83 return ("ATI RS100 AGP bridge");
84 case 0xcab21002:
85 return ("ATI RS200 AGP bridge");
86 case 0xcbb21002:
87 return ("ATI RS200M AGP bridge");
88 case 0xcab31002:
89 return ("ATI RS250 AGP bridge");
90 case 0x58301002:
91 return ("ATI RS300_100 AGP bridge");
92 case 0x58311002:
93 return ("ATI RS300_133 AGP bridge");
94 case 0x58321002:
95 return ("ATI RS300_166 AGP bridge");
96 case 0x58331002:
97 return ("ATI RS300_200 AGP bridge");
98 };
99
100 return NULL;
101 }
102
103 static int
104 agp_ati_probe(device_t dev)
105 {
106 const char *desc;
107
108 desc = agp_ati_match(dev);
109 if (desc) {
110 device_verbose(dev);
111 device_set_desc(dev, desc);
112 return 0;
113 }
114
115 return ENXIO;
116 }
117
118 static int
119 agp_ati_alloc_gatt(device_t dev)
120 {
121 struct agp_ati_softc *sc = device_get_softc(dev);
122 u_int32_t apsize = AGP_GET_APERTURE(dev);
123 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
124 u_int32_t apbase_offset;
125 int i;
126
127 /* Alloc the GATT -- pointers to pages of AGP memory */
128 sc->ag_entries = entries;
129 sc->ag_virtual = kmalloc(entries * sizeof(u_int32_t), M_AGP,
130 M_NOWAIT | M_ZERO);
131 if (sc->ag_virtual == NULL) {
132 if (bootverbose)
133 device_printf(dev, "aperture allocation failed\n");
134 return ENOMEM;
135 }
136
137 /* Alloc the page directory -- pointers to each page of the GATT */
138 sc->ag_vdir = kmalloc(AGP_PAGE_SIZE, M_AGP, M_NOWAIT | M_ZERO);
139 if (sc->ag_vdir == NULL) {
140 if (bootverbose)
141 device_printf(dev, "pagedir allocation failed\n");
142 kfree(sc->ag_virtual, M_AGP);
143 return ENOMEM;
144 }
145 sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
146
147 apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
148 /* Fill in the pagedir's pointers to GATT pages */
149 for (i = 0; i < sc->ag_entries / 1024; i++) {
150 vm_offset_t va;
151 vm_offset_t pa;
152
153 va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
154 pa = vtophys(va);
155 sc->ag_vdir[apbase_offset + i] = pa | 1;
156 }
157
158 /*
159 * Make sure the chipset can see everything.
160 */
161 agp_flush_cache();
162
163 return 0;
164 }
165
166
167 static int
168 agp_ati_attach(device_t dev)
169 {
170 struct agp_ati_softc *sc = device_get_softc(dev);
171 int error, rid;
172 u_int32_t temp;
173 u_int32_t apsize_reg, agpmode_reg;
174
175 error = agp_generic_attach(dev);
176 if (error)
177 return error;
178
179 switch (pci_get_devid(dev)) {
180 case 0xcab01002: /* ATI RS100 AGP bridge */
181 case 0xcab21002: /* ATI RS200 AGP bridge */
182 case 0xcbb21002: /* ATI RS200M AGP bridge */
183 case 0xcab31002: /* ATI RS250 AGP bridge */
184 sc->is_rs300 = 0;
185 apsize_reg = ATI_RS100_APSIZE;
186 agpmode_reg = ATI_RS100_IG_AGPMODE;
187 break;
188 case 0x58301002: /* ATI RS300_100 AGP bridge */
189 case 0x58311002: /* ATI RS300_133 AGP bridge */
190 case 0x58321002: /* ATI RS300_166 AGP bridge */
191 case 0x58331002: /* ATI RS300_200 AGP bridge */
192 sc->is_rs300 = 1;
193 apsize_reg = ATI_RS300_APSIZE;
194 agpmode_reg = ATI_RS300_IG_AGPMODE;
195 break;
196 default:
197 /* Unknown chipset */
198 return EINVAL;
199 };
200
201 rid = ATI_GART_MMADDR;
202 sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
203 if (!sc->regs) {
204 agp_generic_detach(dev);
205 return ENOMEM;
206 }
207
208 sc->bst = rman_get_bustag(sc->regs);
209 sc->bsh = rman_get_bushandle(sc->regs);
210
211 sc->initial_aperture = AGP_GET_APERTURE(dev);
212 if (sc->initial_aperture == 0) {
213 device_printf(dev, "bad initial aperture size, disabling\n");
214 return ENXIO;
215 }
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 kfree(sc->ag_vdir, M_AGP);
269 kfree(sc->ag_virtual, M_AGP);
270
271 bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
272 agp_free_res(dev);
273
274 return 0;
275 }
276
277 static u_int32_t
278 agp_ati_get_aperture(device_t dev)
279 {
280 struct agp_ati_softc *sc = device_get_softc(dev);
281 int size_value;
282
283 if (sc->is_rs300)
284 size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
285 else
286 size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
287
288 size_value = (size_value & 0x0000000e) >> 1;
289 size_value = (32 * 1024 * 1024) << size_value;
290
291 return size_value;
292 }
293
294 static int
295 agp_ati_set_aperture(device_t dev, u_int32_t aperture)
296 {
297 struct agp_ati_softc *sc = device_get_softc(dev);
298 int size_value;
299 u_int32_t apsize_reg;
300
301 if (sc->is_rs300)
302 apsize_reg = ATI_RS300_APSIZE;
303 else
304 apsize_reg = ATI_RS100_APSIZE;
305
306 size_value = pci_read_config(dev, apsize_reg, 4);
307
308 size_value &= ~0x0000000e;
309 size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
310
311 pci_write_config(dev, apsize_reg, size_value, 4);
312
313 return 0;
314 }
315
316 static int
317 agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
318 {
319 struct agp_ati_softc *sc = device_get_softc(dev);
320
321 if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
322 return EINVAL;
323
324 sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
325
326 return 0;
327 }
328
329 static int
330 agp_ati_unbind_page(device_t dev, vm_offset_t offset)
331 {
332 struct agp_ati_softc *sc = device_get_softc(dev);
333
334 if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
335 return EINVAL;
336
337 sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
338 return 0;
339 }
340
341 static void
342 agp_ati_flush_tlb(device_t dev)
343 {
344 struct agp_ati_softc *sc = device_get_softc(dev);
345
346 /* Set the cache invalidate bit and wait for the chipset to clear */
347 WRITE4(ATI_GART_CACHE_CNTRL, 1);
348 (void)READ4(ATI_GART_CACHE_CNTRL);
349 }
350
351 static device_method_t agp_ati_methods[] = {
352 /* Device interface */
353 DEVMETHOD(device_probe, agp_ati_probe),
354 DEVMETHOD(device_attach, agp_ati_attach),
355 DEVMETHOD(device_detach, agp_ati_detach),
356 DEVMETHOD(device_shutdown, bus_generic_shutdown),
357 DEVMETHOD(device_suspend, bus_generic_suspend),
358 DEVMETHOD(device_resume, bus_generic_resume),
359
360 /* AGP interface */
361 DEVMETHOD(agp_get_aperture, agp_ati_get_aperture),
362 DEVMETHOD(agp_set_aperture, agp_ati_set_aperture),
363 DEVMETHOD(agp_bind_page, agp_ati_bind_page),
364 DEVMETHOD(agp_unbind_page, agp_ati_unbind_page),
365 DEVMETHOD(agp_flush_tlb, agp_ati_flush_tlb),
366 DEVMETHOD(agp_enable, agp_generic_enable),
367 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
368 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
369 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
370 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
371
372 DEVMETHOD_END
373 };
374
375 static driver_t agp_ati_driver = {
376 "agp",
377 agp_ati_methods,
378 sizeof(struct agp_ati_softc),
379 };
380
381 static devclass_t agp_devclass;
382
383 DRIVER_MODULE(agp_ati, pci, agp_ati_driver, agp_devclass, NULL, NULL);
384 MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
385 MODULE_DEPEND(agp_ati, pci, 1, 1, 1);
Cache object: 35f227131241a8e4c7896949c43bdcee
|