FreeBSD/Linux Kernel Cross Reference
sys/pci/agp_amd64.c
1 /*-
2 * Copyright (c) 2004 Jung-uk Kim
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/5.3/sys/pci/agp_amd64.c 133852 2004-08-16 12:25:48Z obrien $");
29
30 #include "opt_bus.h"
31 #ifdef __i386__
32 #include "opt_agp.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <pci/agppriv.h>
48 #include <pci/agpreg.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_object.h>
52 #include <vm/pmap.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <sys/rman.h>
56
57 /* XXX */
58 extern void pci_cfgregwrite(int, int, int, int, uint32_t, int);
59 extern uint32_t pci_cfgregread(int, int, int, int, int);
60
61 MALLOC_DECLARE(M_AGP);
62
63 #define AMD64_MAX_MCTRL 8
64
65 struct agp_amd64_softc {
66 struct agp_softc agp;
67 uint32_t initial_aperture; /* aperture size at startup */
68 struct agp_gatt *gatt;
69 int mctrl[AMD64_MAX_MCTRL];
70 int n_mctrl;
71 };
72
73 static const char*
74 agp_amd64_match(device_t dev)
75 {
76 if (pci_get_class(dev) != PCIC_BRIDGE
77 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
78 return NULL;
79
80 if (agp_find_caps(dev) == 0)
81 return NULL;
82
83 switch (pci_get_devid(dev)) {
84 case 0x74541022:
85 return ("AMD 8151 AGP graphics tunnel");
86 case 0x10221039:
87 return ("SiS 755 host to AGP bridge");
88 case 0x02041106:
89 return ("VIA 8380 host to PCI bridge");
90 case 0x31881106:
91 return ("VIA 8385 host to PCI bridge");
92 };
93
94 return NULL;
95 }
96
97 static int
98 agp_amd64_probe(device_t dev)
99 {
100 const char *desc;
101
102 if (resource_disabled("agp", device_get_unit(dev)))
103 return ENXIO;
104 if ((desc = agp_amd64_match(dev))) {
105 device_verbose(dev);
106 device_set_desc(dev, desc);
107 return 0;
108 }
109
110 return ENXIO;
111 }
112
113 static int
114 agp_amd64_attach(device_t dev)
115 {
116 struct agp_amd64_softc *sc = device_get_softc(dev);
117 struct agp_gatt *gatt;
118 int i, n, error;
119
120 for (i = 0, n = 0; i < PCI_SLOTMAX && n < AMD64_MAX_MCTRL; i++)
121 if (pci_cfgregread(0, i, 3, 0, 4) == 0x11031022) {
122 sc->mctrl[n] = i;
123 n++;
124 }
125
126 if (n == 0)
127 return ENXIO;
128
129 sc->n_mctrl = n;
130
131 if (bootverbose)
132 printf("AMD64: %d Misc. Control unit(s) found.\n", sc->n_mctrl);
133
134 if ((error = agp_generic_attach(dev)))
135 return error;
136
137 sc->initial_aperture = AGP_GET_APERTURE(dev);
138
139 for (;;) {
140 gatt = agp_alloc_gatt(dev);
141 if (gatt)
142 break;
143
144 /*
145 * Probably contigmalloc failure. Try reducing the
146 * aperture so that the gatt size reduces.
147 */
148 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
149 agp_generic_detach(dev);
150 return ENOMEM;
151 }
152 }
153 sc->gatt = gatt;
154
155 /* Install the gatt and enable aperture. */
156 for (i = 0; i < sc->n_mctrl; i++) {
157 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_ATTBASE,
158 (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK,
159 4);
160 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL,
161 (pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) |
162 AGP_AMD64_APCTRL_GARTEN) &
163 ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO),
164 4);
165 }
166
167 agp_flush_cache();
168
169 return 0;
170 }
171
172 static int
173 agp_amd64_detach(device_t dev)
174 {
175 struct agp_amd64_softc *sc = device_get_softc(dev);
176 int i, error;
177
178 if ((error = agp_generic_detach(dev)))
179 return error;
180
181 for (i = 0; i < sc->n_mctrl; i++)
182 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL,
183 pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) &
184 ~AGP_AMD64_APCTRL_GARTEN, 4);
185
186 AGP_SET_APERTURE(dev, sc->initial_aperture);
187 agp_free_gatt(sc->gatt);
188
189 return 0;
190 }
191
192 static uint32_t agp_amd64_table[] = {
193 0x02000000, /* 32 MB */
194 0x04000000, /* 64 MB */
195 0x08000000, /* 128 MB */
196 0x10000000, /* 256 MB */
197 0x20000000, /* 512 MB */
198 0x40000000, /* 1024 MB */
199 0x80000000, /* 2048 MB */
200 };
201
202 #define AGP_AMD64_TABLE_SIZE \
203 (sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0]))
204
205 static uint32_t
206 agp_amd64_get_aperture(device_t dev)
207 {
208 struct agp_amd64_softc *sc = device_get_softc(dev);
209 uint32_t i;
210
211 i = (pci_cfgregread(0, sc->mctrl[0], 3, AGP_AMD64_APCTRL, 4) &
212 AGP_AMD64_APCTRL_SIZE_MASK) >> 1;
213
214 if (i >= AGP_AMD64_TABLE_SIZE)
215 return 0;
216
217 return (agp_amd64_table[i]);
218 }
219
220 static int
221 agp_amd64_set_aperture(device_t dev, uint32_t aperture)
222 {
223 struct agp_amd64_softc *sc = device_get_softc(dev);
224 uint32_t i;
225 int j;
226
227 for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++)
228 if (agp_amd64_table[i] == aperture)
229 break;
230 if (i == AGP_AMD64_TABLE_SIZE)
231 return EINVAL;
232
233 for (j = 0; j < sc->n_mctrl; j++)
234 pci_cfgregwrite(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL,
235 (pci_cfgregread(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 4) &
236 ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1), 4);
237
238 return 0;
239 }
240
241 static int
242 agp_amd64_bind_page(device_t dev, int offset, vm_offset_t physical)
243 {
244 struct agp_amd64_softc *sc = device_get_softc(dev);
245
246 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
247 return EINVAL;
248
249 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
250 return 0;
251 }
252
253 static int
254 agp_amd64_unbind_page(device_t dev, int offset)
255 {
256 struct agp_amd64_softc *sc = device_get_softc(dev);
257
258 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
259 return EINVAL;
260
261 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
262 return 0;
263 }
264
265 static void
266 agp_amd64_flush_tlb(device_t dev)
267 {
268 struct agp_amd64_softc *sc = device_get_softc(dev);
269 int i;
270
271 for (i = 0; i < sc->n_mctrl; i++)
272 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL,
273 pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 4) |
274 AGP_AMD64_CACHECTRL_INVGART, 4);
275 }
276
277 static device_method_t agp_amd64_methods[] = {
278 /* Device interface */
279 DEVMETHOD(device_probe, agp_amd64_probe),
280 DEVMETHOD(device_attach, agp_amd64_attach),
281 DEVMETHOD(device_detach, agp_amd64_detach),
282 DEVMETHOD(device_shutdown, bus_generic_shutdown),
283 DEVMETHOD(device_suspend, bus_generic_suspend),
284 DEVMETHOD(device_resume, bus_generic_resume),
285
286 /* AGP interface */
287 DEVMETHOD(agp_get_aperture, agp_amd64_get_aperture),
288 DEVMETHOD(agp_set_aperture, agp_amd64_set_aperture),
289 DEVMETHOD(agp_bind_page, agp_amd64_bind_page),
290 DEVMETHOD(agp_unbind_page, agp_amd64_unbind_page),
291 DEVMETHOD(agp_flush_tlb, agp_amd64_flush_tlb),
292 DEVMETHOD(agp_enable, agp_generic_enable),
293 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
294 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
295 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
296 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
297
298 { 0, 0 }
299 };
300
301 static driver_t agp_amd64_driver = {
302 "agp",
303 agp_amd64_methods,
304 sizeof(struct agp_amd64_softc),
305 };
306
307 static devclass_t agp_devclass;
308
309 DRIVER_MODULE(agp_amd64, pci, agp_amd64_driver, agp_devclass, 0, 0);
310 MODULE_DEPEND(agp_amd64, agp, 1, 1, 1);
311 MODULE_DEPEND(agp_amd64, pci, 1, 1, 1);
Cache object: f37a362b3052ce38c33903061d1fc1ef
|