agp: Sync with FreeBSD
[dragonfly.git] / sys / dev / agp / agp_ati.c
CommitLineData
fdc3c5be
HT
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 *
aa31142a 28 * $FreeBSD: src/sys/dev/agp/agp_ati.c,v 1.5 2007/11/12 21:51:36 jhb Exp $
fdc3c5be
HT
29 */
30
fdc3c5be
HT
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/bus.h>
49#include <sys/rman.h>
50
51MALLOC_DECLARE(M_AGP);
52
53#define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
54#define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
55
56struct agp_ati_softc {
57 struct agp_softc agp;
58 struct resource *regs; /* memory mapped control registers */
59 bus_space_tag_t bst; /* bus_space tag */
60 bus_space_handle_t bsh; /* bus_space handle */
61 u_int32_t initial_aperture; /* aperture size at startup */
62 char is_rs300;
63
64 /* The GATT */
65 u_int32_t ag_entries;
66 u_int32_t *ag_virtual; /* virtual address of gatt */
67 u_int32_t *ag_vdir; /* virtual address of page dir */
68 vm_offset_t ag_pdir; /* physical address of page dir */
69};
70
71
72static const char*
73agp_ati_match(device_t dev)
74{
75 if (pci_get_class(dev) != PCIC_BRIDGE ||
76 pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
77 return NULL;
78
79 if (agp_find_caps(dev) == 0)
80 return NULL;
81
82 switch (pci_get_devid(dev)) {
83 case 0xcab01002:
84 return ("ATI RS100 AGP bridge");
85 case 0xcab21002:
86 return ("ATI RS200 AGP bridge");
87 case 0xcbb21002:
88 return ("ATI RS200M AGP bridge");
89 case 0xcab31002:
90 return ("ATI RS250 AGP bridge");
91 case 0x58301002:
92 return ("ATI RS300_100 AGP bridge");
93 case 0x58311002:
94 return ("ATI RS300_133 AGP bridge");
95 case 0x58321002:
96 return ("ATI RS300_166 AGP bridge");
97 case 0x58331002:
98 return ("ATI RS300_200 AGP bridge");
99 };
100
101 return NULL;
102}
103
104static int
105agp_ati_probe(device_t dev)
106{
107 const char *desc;
108
109 desc = agp_ati_match(dev);
110 if (desc) {
111 device_verbose(dev);
112 device_set_desc(dev, desc);
113 return 0;
114 }
115
116 return ENXIO;
117}
118
119static int
120agp_ati_alloc_gatt(device_t dev)
121{
122 struct agp_ati_softc *sc = device_get_softc(dev);
123 u_int32_t apsize = AGP_GET_APERTURE(dev);
124 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
125 u_int32_t apbase_offset;
126 int i;
127
128 /* Alloc the GATT -- pointers to pages of AGP memory */
129 sc->ag_entries = entries;
130 sc->ag_virtual = kmalloc(entries * sizeof(u_int32_t), M_AGP,
131 M_NOWAIT | M_ZERO);
132 if (sc->ag_virtual == NULL) {
133 if (bootverbose)
134 device_printf(dev, "aperture allocation failed\n");
135 return ENOMEM;
136 }
137
138 /* Alloc the page directory -- pointers to each page of the GATT */
139 sc->ag_vdir = kmalloc(AGP_PAGE_SIZE, M_AGP, M_NOWAIT | M_ZERO);
140 if (sc->ag_vdir == NULL) {
141 if (bootverbose)
142 device_printf(dev, "pagedir allocation failed\n");
143 kfree(sc->ag_virtual, M_AGP);
144 return ENOMEM;
145 }
146 sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
147
148 apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
149 /* Fill in the pagedir's pointers to GATT pages */
150 for (i = 0; i < sc->ag_entries / 1024; i++) {
151 vm_offset_t va;
152 vm_offset_t pa;
153
154 va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
155 pa = vtophys(va);
156 sc->ag_vdir[apbase_offset + i] = pa | 1;
157 }
158
159 /*
160 * Make sure the chipset can see everything.
161 */
162 agp_flush_cache();
163
164 return 0;
165}
166
167
168static int
169agp_ati_attach(device_t dev)
170{
171 struct agp_ati_softc *sc = device_get_softc(dev);
172 int error, rid;
173 u_int32_t temp;
174 u_int32_t apsize_reg, agpmode_reg;
175
176 error = agp_generic_attach(dev);
177 if (error)
178 return error;
179
180 switch (pci_get_devid(dev)) {
181 case 0xcab01002: /* ATI RS100 AGP bridge */
182 case 0xcab21002: /* ATI RS200 AGP bridge */
183 case 0xcbb21002: /* ATI RS200M AGP bridge */
184 case 0xcab31002: /* ATI RS250 AGP bridge */
185 sc->is_rs300 = 0;
186 apsize_reg = ATI_RS100_APSIZE;
187 agpmode_reg = ATI_RS100_IG_AGPMODE;
188 break;
189 case 0x58301002: /* ATI RS300_100 AGP bridge */
190 case 0x58311002: /* ATI RS300_133 AGP bridge */
191 case 0x58321002: /* ATI RS300_166 AGP bridge */
192 case 0x58331002: /* ATI RS300_200 AGP bridge */
193 sc->is_rs300 = 1;
194 apsize_reg = ATI_RS300_APSIZE;
195 agpmode_reg = ATI_RS300_IG_AGPMODE;
196 break;
197 default:
198 /* Unknown chipset */
199 return EINVAL;
200 };
201
202 rid = ATI_GART_MMADDR;
203 sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
204 if (!sc->regs) {
205 agp_generic_detach(dev);
206 return ENOMEM;
207 }
208
209 sc->bst = rman_get_bustag(sc->regs);
210 sc->bsh = rman_get_bushandle(sc->regs);
211
212 sc->initial_aperture = AGP_GET_APERTURE(dev);
213 if (sc->initial_aperture == 0) {
214 device_printf(dev, "bad initial aperture size, disabling\n");
215 return ENXIO;
216 }
217
218 for (;;) {
219 if (agp_ati_alloc_gatt(dev) == 0)
220 break;
221
222 /*
223 * Probably contigmalloc failure. Try reducing the
224 * aperture so that the gatt size reduces.
225 */
226 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
227 return ENOMEM;
228 }
229
230 temp = pci_read_config(dev, apsize_reg, 4);
231 pci_write_config(dev, apsize_reg, temp | 1, 4);
232
233 pci_write_config(dev, agpmode_reg, 0x20000, 4);
234
235 WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
236
237 temp = pci_read_config(dev, 4, 4); /* XXX: Magic reg# */
238 pci_write_config(dev, 4, temp | (1 << 14), 4);
239
240 WRITE4(ATI_GART_BASE, sc->ag_pdir);
241
242 AGP_FLUSH_TLB(dev);
243
244 return 0;
245}
246
247static int
248agp_ati_detach(device_t dev)
249{
250 struct agp_ati_softc *sc = device_get_softc(dev);
fdc3c5be
HT
251 u_int32_t apsize_reg, temp;
252
67e2dc5d
SS
253 agp_free_cdev(dev);
254
fdc3c5be
HT
255 if (sc->is_rs300)
256 apsize_reg = ATI_RS300_APSIZE;
257 else
258 apsize_reg = ATI_RS100_APSIZE;
259
fdc3c5be
HT
260 /* Clear the GATT base */
261 WRITE4(ATI_GART_BASE, 0);
262
263 /* Put the aperture back the way it started. */
264 AGP_SET_APERTURE(dev, sc->initial_aperture);
265
266 temp = pci_read_config(dev, apsize_reg, 4);
267 pci_write_config(dev, apsize_reg, temp & ~1, 4);
268
269 kfree(sc->ag_vdir, M_AGP);
270 kfree(sc->ag_virtual, M_AGP);
271
272 bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
67e2dc5d 273 agp_free_res(dev);
fdc3c5be
HT
274
275 return 0;
276}
277
278static u_int32_t
279agp_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
295static int
296agp_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
317static int
7f9ec87c 318agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
fdc3c5be
HT
319{
320 struct agp_ati_softc *sc = device_get_softc(dev);
321
7f9ec87c 322 if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
fdc3c5be
HT
323 return EINVAL;
324
325 sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
326
327 return 0;
328}
329
330static int
7f9ec87c 331agp_ati_unbind_page(device_t dev, vm_offset_t offset)
fdc3c5be
HT
332{
333 struct agp_ati_softc *sc = device_get_softc(dev);
334
7f9ec87c 335 if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
fdc3c5be
HT
336 return EINVAL;
337
338 sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
339 return 0;
340}
341
342static void
343agp_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
352static 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
376static driver_t agp_ati_driver = {
377 "agp",
378 agp_ati_methods,
379 sizeof(struct agp_ati_softc),
380};
381
382static devclass_t agp_devclass;
383
aa2b9d05 384DRIVER_MODULE(agp_ati, pci, agp_ati_driver, agp_devclass, NULL, NULL);
fdc3c5be
HT
385MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
386MODULE_DEPEND(agp_ati, pci, 1, 1, 1);