agp_amd64.c revision 1.1.6.2 1 1.1.6.2 ad /*-
2 1.1.6.2 ad * Copyright (c) 2004, 2005 Jung-uk Kim <jkim (at) FreeBSD.org>
3 1.1.6.2 ad * All rights reserved.
4 1.1.6.2 ad *
5 1.1.6.2 ad * Redistribution and use in source and binary forms, with or without
6 1.1.6.2 ad * modification, are permitted provided that the following conditions
7 1.1.6.2 ad * are met:
8 1.1.6.2 ad * 1. Redistributions of source code must retain the above copyright
9 1.1.6.2 ad * notice, this list of conditions and the following disclaimer.
10 1.1.6.2 ad * 2. Redistributions in binary form must reproduce the above copyright
11 1.1.6.2 ad * notice, this list of conditions and the following disclaimer in the
12 1.1.6.2 ad * documentation and/or other materials provided with the distribution.
13 1.1.6.2 ad *
14 1.1.6.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 1.1.6.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 1.1.6.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 1.1.6.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 1.1.6.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 1.1.6.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 1.1.6.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 1.1.6.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 1.1.6.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 1.1.6.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 1.1.6.2 ad * SUCH DAMAGE.
25 1.1.6.2 ad */
26 1.1.6.2 ad
27 1.1.6.2 ad #include <sys/cdefs.h>
28 1.1.6.2 ad __KERNEL_RCSID(0, "$NetBSD: agp_amd64.c,v 1.1.6.2 2007/08/20 22:07:03 ad Exp $");
29 1.1.6.2 ad
30 1.1.6.2 ad #include <sys/param.h>
31 1.1.6.2 ad #include <sys/systm.h>
32 1.1.6.2 ad #include <sys/malloc.h>
33 1.1.6.2 ad #include <sys/kernel.h>
34 1.1.6.2 ad #include <sys/proc.h>
35 1.1.6.2 ad #include <sys/conf.h>
36 1.1.6.2 ad #include <sys/device.h>
37 1.1.6.2 ad #include <sys/lock.h>
38 1.1.6.2 ad #include <sys/agpio.h>
39 1.1.6.2 ad
40 1.1.6.2 ad #include <uvm/uvm_extern.h>
41 1.1.6.2 ad
42 1.1.6.2 ad #include <dev/pci/pcivar.h>
43 1.1.6.2 ad #include <dev/pci/pcireg.h>
44 1.1.6.2 ad #include <dev/pci/agpvar.h>
45 1.1.6.2 ad #include <dev/pci/agpreg.h>
46 1.1.6.2 ad
47 1.1.6.2 ad #include <dev/pci/pcidevs.h>
48 1.1.6.2 ad
49 1.1.6.2 ad #include <machine/bus.h>
50 1.1.6.2 ad
51 1.1.6.2 ad
52 1.1.6.2 ad #define AMD64_MAX_MCTRL 8
53 1.1.6.2 ad
54 1.1.6.2 ad /* XXX nForce3 requires secondary AGP bridge at 0:11:0. */
55 1.1.6.2 ad #define AGP_AMD64_NVIDIA_PCITAG(pc) pci_make_tag(pc, 0, 11, 0)
56 1.1.6.2 ad /* XXX Some VIA bridge requires secondary AGP bridge at 0:1:0. */
57 1.1.6.2 ad #define AGP_AMD64_VIA_PCITAG(pc) pci_make_tag(pc, 0, 1, 0)
58 1.1.6.2 ad
59 1.1.6.2 ad
60 1.1.6.2 ad static uint32_t agp_amd64_get_aperture(struct agp_softc *);
61 1.1.6.2 ad static int agp_amd64_set_aperture(struct agp_softc *, uint32_t);
62 1.1.6.2 ad static int agp_amd64_bind_page(struct agp_softc *, off_t, bus_addr_t);
63 1.1.6.2 ad static int agp_amd64_unbind_page(struct agp_softc *, off_t);
64 1.1.6.2 ad static void agp_amd64_flush_tlb(struct agp_softc *);
65 1.1.6.2 ad
66 1.1.6.2 ad static void agp_amd64_apbase_fixup(struct agp_softc *);
67 1.1.6.2 ad
68 1.1.6.2 ad static void agp_amd64_uli_init(struct agp_softc *);
69 1.1.6.2 ad static int agp_amd64_uli_set_aperture(struct agp_softc *, uint32_t);
70 1.1.6.2 ad
71 1.1.6.2 ad static int agp_amd64_nvidia_match(const struct pci_attach_args *, uint16_t);
72 1.1.6.2 ad static void agp_amd64_nvidia_init(struct agp_softc *);
73 1.1.6.2 ad static int agp_amd64_nvidia_set_aperture(struct agp_softc *, uint32_t);
74 1.1.6.2 ad
75 1.1.6.2 ad static int agp_amd64_via_match(const struct pci_attach_args *);
76 1.1.6.2 ad static void agp_amd64_via_init(struct agp_softc *);
77 1.1.6.2 ad static int agp_amd64_via_set_aperture(struct agp_softc *, uint32_t);
78 1.1.6.2 ad
79 1.1.6.2 ad
80 1.1.6.2 ad struct agp_amd64_softc {
81 1.1.6.2 ad uint32_t initial_aperture;
82 1.1.6.2 ad struct agp_gatt *gatt;
83 1.1.6.2 ad uint32_t apbase;
84 1.1.6.2 ad pcitag_t ctrl_tag; /* use NVIDIA and VIA */
85 1.1.6.2 ad pcitag_t mctrl_tag[AMD64_MAX_MCTRL];
86 1.1.6.2 ad int n_mctrl;
87 1.1.6.2 ad int via_agp;
88 1.1.6.2 ad };
89 1.1.6.2 ad
90 1.1.6.2 ad static struct agp_methods agp_amd64_methods = {
91 1.1.6.2 ad agp_amd64_get_aperture,
92 1.1.6.2 ad agp_amd64_set_aperture,
93 1.1.6.2 ad agp_amd64_bind_page,
94 1.1.6.2 ad agp_amd64_unbind_page,
95 1.1.6.2 ad agp_amd64_flush_tlb,
96 1.1.6.2 ad agp_generic_enable,
97 1.1.6.2 ad agp_generic_alloc_memory,
98 1.1.6.2 ad agp_generic_free_memory,
99 1.1.6.2 ad agp_generic_bind_memory,
100 1.1.6.2 ad agp_generic_unbind_memory,
101 1.1.6.2 ad };
102 1.1.6.2 ad
103 1.1.6.2 ad
104 1.1.6.2 ad int
105 1.1.6.2 ad agp_amd64_match(const struct pci_attach_args *pa)
106 1.1.6.2 ad {
107 1.1.6.2 ad
108 1.1.6.2 ad switch (PCI_VENDOR(pa->pa_id)) {
109 1.1.6.2 ad case PCI_VENDOR_AMD:
110 1.1.6.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
111 1.1.6.2 ad case PCI_PRODUCT_AMD_AGP8151_DEV:
112 1.1.6.2 ad return 1;
113 1.1.6.2 ad }
114 1.1.6.2 ad break;
115 1.1.6.2 ad
116 1.1.6.2 ad case PCI_VENDOR_SIS:
117 1.1.6.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
118 1.1.6.2 ad case PCI_PRODUCT_SIS_755:
119 1.1.6.2 ad case PCI_PRODUCT_SIS_760:
120 1.1.6.2 ad return 1;
121 1.1.6.2 ad }
122 1.1.6.2 ad break;
123 1.1.6.2 ad
124 1.1.6.2 ad case PCI_VENDOR_ALI:
125 1.1.6.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
126 1.1.6.2 ad case PCI_PRODUCT_ALI_M1689:
127 1.1.6.2 ad return 1;
128 1.1.6.2 ad }
129 1.1.6.2 ad break;
130 1.1.6.2 ad
131 1.1.6.2 ad case PCI_VENDOR_NVIDIA:
132 1.1.6.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
133 1.1.6.2 ad case PCI_PRODUCT_NVIDIA_NFORCE3_PCHB:
134 1.1.6.2 ad return agp_amd64_nvidia_match(pa,
135 1.1.6.2 ad PCI_PRODUCT_NVIDIA_NFORCE3_PPB2);
136 1.1.6.2 ad
137 1.1.6.2 ad /* NOTREACHED */
138 1.1.6.2 ad
139 1.1.6.2 ad case PCI_PRODUCT_NVIDIA_NFORCE3_250_PCHB:
140 1.1.6.2 ad return agp_amd64_nvidia_match(pa,
141 1.1.6.2 ad PCI_PRODUCT_NVIDIA_NFORCE3_250_AGP);
142 1.1.6.2 ad
143 1.1.6.2 ad /* NOTREACHED */
144 1.1.6.2 ad }
145 1.1.6.2 ad break;
146 1.1.6.2 ad
147 1.1.6.2 ad case PCI_VENDOR_VIATECH:
148 1.1.6.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
149 1.1.6.2 ad case PCI_PRODUCT_VIATECH_K8M800_0:
150 1.1.6.2 ad case PCI_PRODUCT_VIATECH_K8T890_0:
151 1.1.6.2 ad case PCI_PRODUCT_VIATECH_K8HTB_0:
152 1.1.6.2 ad case PCI_PRODUCT_VIATECH_K8HTB:
153 1.1.6.2 ad return 1;
154 1.1.6.2 ad }
155 1.1.6.2 ad break;
156 1.1.6.2 ad }
157 1.1.6.2 ad
158 1.1.6.2 ad return 0;
159 1.1.6.2 ad }
160 1.1.6.2 ad
161 1.1.6.2 ad static int
162 1.1.6.2 ad agp_amd64_nvidia_match(const struct pci_attach_args *pa, uint16_t devid)
163 1.1.6.2 ad {
164 1.1.6.2 ad pcitag_t tag;
165 1.1.6.2 ad pcireg_t reg;
166 1.1.6.2 ad
167 1.1.6.2 ad tag = AGP_AMD64_NVIDIA_PCITAG(pa->pa_pc);
168 1.1.6.2 ad
169 1.1.6.2 ad reg = pci_conf_read(pa->pa_pc, tag, PCI_CLASS_REG);
170 1.1.6.2 ad if (PCI_CLASS(reg) != PCI_CLASS_BRIDGE ||
171 1.1.6.2 ad PCI_SUBCLASS(reg) != PCI_SUBCLASS_BRIDGE_PCI)
172 1.1.6.2 ad return 0;
173 1.1.6.2 ad
174 1.1.6.2 ad reg = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
175 1.1.6.2 ad if (PCI_VENDOR(reg) != PCI_VENDOR_NVIDIA || PCI_PRODUCT(reg) != devid)
176 1.1.6.2 ad return 0;
177 1.1.6.2 ad
178 1.1.6.2 ad return 1;
179 1.1.6.2 ad }
180 1.1.6.2 ad
181 1.1.6.2 ad static int
182 1.1.6.2 ad agp_amd64_via_match(const struct pci_attach_args *pa)
183 1.1.6.2 ad {
184 1.1.6.2 ad pcitag_t tag;
185 1.1.6.2 ad pcireg_t reg;
186 1.1.6.2 ad
187 1.1.6.2 ad tag = AGP_AMD64_VIA_PCITAG(pa->pa_pc);
188 1.1.6.2 ad
189 1.1.6.2 ad reg = pci_conf_read(pa->pa_pc, tag, PCI_CLASS_REG);
190 1.1.6.2 ad if (PCI_CLASS(reg) != PCI_CLASS_BRIDGE ||
191 1.1.6.2 ad PCI_SUBCLASS(reg) != PCI_SUBCLASS_BRIDGE_PCI)
192 1.1.6.2 ad return 0;
193 1.1.6.2 ad
194 1.1.6.2 ad reg = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
195 1.1.6.2 ad if (PCI_VENDOR(reg) != PCI_VENDOR_VIATECH ||
196 1.1.6.2 ad PCI_PRODUCT(reg) != PCI_PRODUCT_VIATECH_K8HTB_AGP)
197 1.1.6.2 ad return 0;
198 1.1.6.2 ad
199 1.1.6.2 ad return 1;
200 1.1.6.2 ad }
201 1.1.6.2 ad
202 1.1.6.2 ad int
203 1.1.6.2 ad agp_amd64_attach(struct device *parent, struct device *self, void *aux)
204 1.1.6.2 ad {
205 1.1.6.2 ad struct agp_softc *sc = (void *)self;
206 1.1.6.2 ad struct agp_amd64_softc *asc;
207 1.1.6.2 ad struct pci_attach_args *pa = aux;
208 1.1.6.2 ad struct agp_gatt *gatt;
209 1.1.6.2 ad pcitag_t tag;
210 1.1.6.2 ad pcireg_t id, attbase, apctrl;
211 1.1.6.2 ad int maxdevs, i, n;
212 1.1.6.2 ad
213 1.1.6.2 ad asc = malloc(sizeof(struct agp_amd64_softc), M_AGP, M_NOWAIT | M_ZERO);
214 1.1.6.2 ad if (asc == NULL) {
215 1.1.6.2 ad aprint_error(": can't allocate softc\n");
216 1.1.6.2 ad return ENOMEM;
217 1.1.6.2 ad }
218 1.1.6.2 ad
219 1.1.6.2 ad if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
220 1.1.6.2 ad aprint_error(": can't map aperture\n");
221 1.1.6.2 ad free(asc, M_AGP);
222 1.1.6.2 ad return ENXIO;
223 1.1.6.2 ad }
224 1.1.6.2 ad
225 1.1.6.2 ad maxdevs = pci_bus_maxdevs(pa->pa_pc, 0);
226 1.1.6.2 ad for (i = 0, n = 0; i < maxdevs && n < AMD64_MAX_MCTRL; i++) {
227 1.1.6.2 ad tag = pci_make_tag(pa->pa_pc, 0, i, 3);
228 1.1.6.2 ad id = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
229 1.1.6.2 ad if (PCI_VENDOR(id) == PCI_VENDOR_AMD &&
230 1.1.6.2 ad PCI_PRODUCT(id) == PCI_PRODUCT_AMD_AMD64_MISC) {
231 1.1.6.2 ad asc->mctrl_tag[n] = tag;
232 1.1.6.2 ad n++;
233 1.1.6.2 ad }
234 1.1.6.2 ad }
235 1.1.6.2 ad if (n == 0)
236 1.1.6.2 ad return ENXIO;
237 1.1.6.2 ad asc->n_mctrl = n;
238 1.1.6.2 ad
239 1.1.6.2 ad aprint_normal(": %d Miscellaneous Control unit(s) found.\n",
240 1.1.6.2 ad asc->n_mctrl);
241 1.1.6.2 ad aprint_normal("%s", sc->as_dev.dv_xname);
242 1.1.6.2 ad
243 1.1.6.2 ad sc->as_chipc = asc;
244 1.1.6.2 ad sc->as_methods = &agp_amd64_methods;
245 1.1.6.2 ad pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
246 1.1.6.2 ad NULL);
247 1.1.6.2 ad asc->initial_aperture = AGP_GET_APERTURE(sc);
248 1.1.6.2 ad
249 1.1.6.2 ad for (;;) {
250 1.1.6.2 ad gatt = agp_alloc_gatt(sc);
251 1.1.6.2 ad if (gatt)
252 1.1.6.2 ad break;
253 1.1.6.2 ad
254 1.1.6.2 ad /*
255 1.1.6.2 ad * Probably contigmalloc failure. Try reducing the
256 1.1.6.2 ad * aperture so that the gatt size reduces.
257 1.1.6.2 ad */
258 1.1.6.2 ad if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
259 1.1.6.2 ad agp_generic_detach(sc);
260 1.1.6.2 ad return ENOMEM;
261 1.1.6.2 ad }
262 1.1.6.2 ad }
263 1.1.6.2 ad asc->gatt = gatt;
264 1.1.6.2 ad
265 1.1.6.2 ad switch (PCI_VENDOR(sc->as_id)) {
266 1.1.6.2 ad case PCI_VENDOR_ALI:
267 1.1.6.2 ad agp_amd64_uli_init(sc);
268 1.1.6.2 ad if (agp_amd64_uli_set_aperture(sc, asc->initial_aperture))
269 1.1.6.2 ad return ENXIO;
270 1.1.6.2 ad break;
271 1.1.6.2 ad
272 1.1.6.2 ad case PCI_VENDOR_NVIDIA:
273 1.1.6.2 ad asc->ctrl_tag = AGP_AMD64_NVIDIA_PCITAG(pa->pa_pc);
274 1.1.6.2 ad agp_amd64_nvidia_init(sc);
275 1.1.6.2 ad if (agp_amd64_nvidia_set_aperture(sc, asc->initial_aperture))
276 1.1.6.2 ad return ENXIO;
277 1.1.6.2 ad break;
278 1.1.6.2 ad
279 1.1.6.2 ad case PCI_VENDOR_VIATECH:
280 1.1.6.2 ad asc->via_agp = agp_amd64_via_match(pa);
281 1.1.6.2 ad if (asc->via_agp) {
282 1.1.6.2 ad asc->ctrl_tag = AGP_AMD64_VIA_PCITAG(pa->pa_pc);
283 1.1.6.2 ad agp_amd64_via_init(sc);
284 1.1.6.2 ad if (agp_amd64_via_set_aperture(sc,
285 1.1.6.2 ad asc->initial_aperture))
286 1.1.6.2 ad return ENXIO;
287 1.1.6.2 ad }
288 1.1.6.2 ad break;
289 1.1.6.2 ad }
290 1.1.6.2 ad
291 1.1.6.2 ad /* Install the gatt and enable aperture. */
292 1.1.6.2 ad attbase = (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK;
293 1.1.6.2 ad for (i = 0; i < asc->n_mctrl; i++) {
294 1.1.6.2 ad pci_conf_write(pa->pa_pc, asc->mctrl_tag[i], AGP_AMD64_ATTBASE,
295 1.1.6.2 ad attbase);
296 1.1.6.2 ad apctrl = pci_conf_read(pa->pa_pc, asc->mctrl_tag[i],
297 1.1.6.2 ad AGP_AMD64_APCTRL);
298 1.1.6.2 ad apctrl |= AGP_AMD64_APCTRL_GARTEN;
299 1.1.6.2 ad apctrl &=
300 1.1.6.2 ad ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO);
301 1.1.6.2 ad pci_conf_write(pa->pa_pc, asc->mctrl_tag[i], AGP_AMD64_APCTRL,
302 1.1.6.2 ad apctrl);
303 1.1.6.2 ad }
304 1.1.6.2 ad
305 1.1.6.2 ad agp_flush_cache();
306 1.1.6.2 ad
307 1.1.6.2 ad return 0;
308 1.1.6.2 ad }
309 1.1.6.2 ad
310 1.1.6.2 ad
311 1.1.6.2 ad static uint32_t agp_amd64_table[] = {
312 1.1.6.2 ad 0x02000000, /* 32 MB */
313 1.1.6.2 ad 0x04000000, /* 64 MB */
314 1.1.6.2 ad 0x08000000, /* 128 MB */
315 1.1.6.2 ad 0x10000000, /* 256 MB */
316 1.1.6.2 ad 0x20000000, /* 512 MB */
317 1.1.6.2 ad 0x40000000, /* 1024 MB */
318 1.1.6.2 ad 0x80000000, /* 2048 MB */
319 1.1.6.2 ad };
320 1.1.6.2 ad
321 1.1.6.2 ad #define AGP_AMD64_TABLE_SIZE \
322 1.1.6.2 ad (sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0]))
323 1.1.6.2 ad
324 1.1.6.2 ad static uint32_t
325 1.1.6.2 ad agp_amd64_get_aperture(struct agp_softc *sc)
326 1.1.6.2 ad {
327 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
328 1.1.6.2 ad uint32_t i;
329 1.1.6.2 ad
330 1.1.6.2 ad i = (pci_conf_read(sc->as_pc, asc->mctrl_tag[0], AGP_AMD64_APCTRL) &
331 1.1.6.2 ad AGP_AMD64_APCTRL_SIZE_MASK) >> 1;
332 1.1.6.2 ad
333 1.1.6.2 ad if (i >= AGP_AMD64_TABLE_SIZE)
334 1.1.6.2 ad return 0;
335 1.1.6.2 ad
336 1.1.6.2 ad return agp_amd64_table[i];
337 1.1.6.2 ad }
338 1.1.6.2 ad
339 1.1.6.2 ad static int
340 1.1.6.2 ad agp_amd64_set_aperture(struct agp_softc *sc, uint32_t aperture)
341 1.1.6.2 ad {
342 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
343 1.1.6.2 ad uint32_t i;
344 1.1.6.2 ad pcireg_t apctrl;
345 1.1.6.2 ad int j;
346 1.1.6.2 ad
347 1.1.6.2 ad for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++)
348 1.1.6.2 ad if (agp_amd64_table[i] == aperture)
349 1.1.6.2 ad break;
350 1.1.6.2 ad if (i >= AGP_AMD64_TABLE_SIZE)
351 1.1.6.2 ad return EINVAL;
352 1.1.6.2 ad
353 1.1.6.2 ad for (j = 0; j < asc->n_mctrl; j++) {
354 1.1.6.2 ad apctrl = pci_conf_read(sc->as_pc, asc->mctrl_tag[0],
355 1.1.6.2 ad AGP_AMD64_APCTRL);
356 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->mctrl_tag[0], AGP_AMD64_APCTRL,
357 1.1.6.2 ad (apctrl & ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1));
358 1.1.6.2 ad }
359 1.1.6.2 ad
360 1.1.6.2 ad switch (PCI_VENDOR(sc->as_id)) {
361 1.1.6.2 ad case PCI_VENDOR_ALI:
362 1.1.6.2 ad return agp_amd64_uli_set_aperture(sc, aperture);
363 1.1.6.2 ad break;
364 1.1.6.2 ad
365 1.1.6.2 ad case PCI_VENDOR_NVIDIA:
366 1.1.6.2 ad return agp_amd64_nvidia_set_aperture(sc, aperture);
367 1.1.6.2 ad break;
368 1.1.6.2 ad
369 1.1.6.2 ad case PCI_VENDOR_VIATECH:
370 1.1.6.2 ad if (asc->via_agp)
371 1.1.6.2 ad return agp_amd64_via_set_aperture(sc, aperture);
372 1.1.6.2 ad break;
373 1.1.6.2 ad }
374 1.1.6.2 ad
375 1.1.6.2 ad return 0;
376 1.1.6.2 ad }
377 1.1.6.2 ad
378 1.1.6.2 ad static int
379 1.1.6.2 ad agp_amd64_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
380 1.1.6.2 ad {
381 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
382 1.1.6.2 ad
383 1.1.6.2 ad if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
384 1.1.6.2 ad return EINVAL;
385 1.1.6.2 ad
386 1.1.6.2 ad asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] =
387 1.1.6.2 ad (physical & 0xfffff000) | ((physical >> 28) & 0x00000ff0) | 3;
388 1.1.6.2 ad
389 1.1.6.2 ad return 0;
390 1.1.6.2 ad }
391 1.1.6.2 ad
392 1.1.6.2 ad static int
393 1.1.6.2 ad agp_amd64_unbind_page(struct agp_softc *sc, off_t offset)
394 1.1.6.2 ad {
395 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
396 1.1.6.2 ad
397 1.1.6.2 ad if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
398 1.1.6.2 ad return EINVAL;
399 1.1.6.2 ad
400 1.1.6.2 ad asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
401 1.1.6.2 ad
402 1.1.6.2 ad return 0;
403 1.1.6.2 ad }
404 1.1.6.2 ad
405 1.1.6.2 ad static void
406 1.1.6.2 ad agp_amd64_flush_tlb(struct agp_softc *sc)
407 1.1.6.2 ad {
408 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
409 1.1.6.2 ad pcireg_t cachectrl;
410 1.1.6.2 ad int i;
411 1.1.6.2 ad
412 1.1.6.2 ad for (i = 0; i < asc->n_mctrl; i++) {
413 1.1.6.2 ad cachectrl = pci_conf_read(sc->as_pc, asc->mctrl_tag[i],
414 1.1.6.2 ad AGP_AMD64_CACHECTRL);
415 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->mctrl_tag[i],
416 1.1.6.2 ad AGP_AMD64_CACHECTRL,
417 1.1.6.2 ad cachectrl | AGP_AMD64_CACHECTRL_INVGART);
418 1.1.6.2 ad }
419 1.1.6.2 ad }
420 1.1.6.2 ad
421 1.1.6.2 ad static void
422 1.1.6.2 ad agp_amd64_apbase_fixup(struct agp_softc *sc)
423 1.1.6.2 ad {
424 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
425 1.1.6.2 ad uint32_t apbase;
426 1.1.6.2 ad int i;
427 1.1.6.2 ad
428 1.1.6.2 ad apbase = pci_conf_read(sc->as_pc, sc->as_tag, AGP_APBASE);
429 1.1.6.2 ad asc->apbase = PCI_MAPREG_MEM_ADDR(apbase);
430 1.1.6.2 ad apbase = (asc->apbase >> 25) & AGP_AMD64_APBASE_MASK;
431 1.1.6.2 ad for (i = 0; i < asc->n_mctrl; i++)
432 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->mctrl_tag[i], AGP_AMD64_APBASE,
433 1.1.6.2 ad apbase);
434 1.1.6.2 ad }
435 1.1.6.2 ad
436 1.1.6.2 ad static void
437 1.1.6.2 ad agp_amd64_uli_init(struct agp_softc *sc)
438 1.1.6.2 ad {
439 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
440 1.1.6.2 ad pcireg_t apbase;
441 1.1.6.2 ad
442 1.1.6.2 ad agp_amd64_apbase_fixup(sc);
443 1.1.6.2 ad apbase = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_APBASE);
444 1.1.6.2 ad pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_APBASE,
445 1.1.6.2 ad (apbase & 0x0000000f) | asc->apbase);
446 1.1.6.2 ad pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_HTT_FEATURE,
447 1.1.6.2 ad asc->apbase);
448 1.1.6.2 ad }
449 1.1.6.2 ad
450 1.1.6.2 ad static int
451 1.1.6.2 ad agp_amd64_uli_set_aperture(struct agp_softc *sc, uint32_t aperture)
452 1.1.6.2 ad {
453 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
454 1.1.6.2 ad
455 1.1.6.2 ad switch (aperture) {
456 1.1.6.2 ad case 0x02000000: /* 32 MB */
457 1.1.6.2 ad case 0x04000000: /* 64 MB */
458 1.1.6.2 ad case 0x08000000: /* 128 MB */
459 1.1.6.2 ad case 0x10000000: /* 256 MB */
460 1.1.6.2 ad break;
461 1.1.6.2 ad default:
462 1.1.6.2 ad return EINVAL;
463 1.1.6.2 ad }
464 1.1.6.2 ad
465 1.1.6.2 ad pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_ENU_SCR,
466 1.1.6.2 ad asc->apbase + aperture - 1);
467 1.1.6.2 ad
468 1.1.6.2 ad return 0;
469 1.1.6.2 ad }
470 1.1.6.2 ad
471 1.1.6.2 ad static void
472 1.1.6.2 ad agp_amd64_nvidia_init(struct agp_softc *sc)
473 1.1.6.2 ad {
474 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
475 1.1.6.2 ad pcireg_t apbase;
476 1.1.6.2 ad
477 1.1.6.2 ad agp_amd64_apbase_fixup(sc);
478 1.1.6.2 ad apbase =
479 1.1.6.2 ad pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD64_NVIDIA_0_APBASE);
480 1.1.6.2 ad pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_NVIDIA_0_APBASE,
481 1.1.6.2 ad (apbase & 0x0000000f) | asc->apbase);
482 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APBASE1,
483 1.1.6.2 ad asc->apbase);
484 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APBASE2,
485 1.1.6.2 ad asc->apbase);
486 1.1.6.2 ad }
487 1.1.6.2 ad
488 1.1.6.2 ad static int
489 1.1.6.2 ad agp_amd64_nvidia_set_aperture(struct agp_softc *sc, uint32_t aperture)
490 1.1.6.2 ad {
491 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
492 1.1.6.2 ad uint32_t apsize;
493 1.1.6.2 ad
494 1.1.6.2 ad switch (aperture) {
495 1.1.6.2 ad case 0x02000000: apsize = 0x0f; break; /* 32 MB */
496 1.1.6.2 ad case 0x04000000: apsize = 0x0e; break; /* 64 MB */
497 1.1.6.2 ad case 0x08000000: apsize = 0x0c; break; /* 128 MB */
498 1.1.6.2 ad case 0x10000000: apsize = 0x08; break; /* 256 MB */
499 1.1.6.2 ad case 0x20000000: apsize = 0x00; break; /* 512 MB */
500 1.1.6.2 ad default:
501 1.1.6.2 ad return EINVAL;
502 1.1.6.2 ad }
503 1.1.6.2 ad
504 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APSIZE,
505 1.1.6.2 ad (pci_conf_read(sc->as_pc, asc->ctrl_tag,
506 1.1.6.2 ad AGP_AMD64_NVIDIA_1_APSIZE) & 0xfffffff0) | apsize);
507 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APLIMIT1,
508 1.1.6.2 ad asc->apbase + aperture - 1);
509 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APLIMIT2,
510 1.1.6.2 ad asc->apbase + aperture - 1);
511 1.1.6.2 ad
512 1.1.6.2 ad return 0;
513 1.1.6.2 ad }
514 1.1.6.2 ad
515 1.1.6.2 ad static void
516 1.1.6.2 ad agp_amd64_via_init(struct agp_softc *sc)
517 1.1.6.2 ad {
518 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
519 1.1.6.2 ad
520 1.1.6.2 ad agp_amd64_apbase_fixup(sc);
521 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_ATTBASE,
522 1.1.6.2 ad asc->gatt->ag_physical);
523 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_GARTCTRL,
524 1.1.6.2 ad pci_conf_read(sc->as_pc, asc->ctrl_tag, AGP3_VIA_ATTBASE) | 0x180);
525 1.1.6.2 ad }
526 1.1.6.2 ad
527 1.1.6.2 ad static int
528 1.1.6.2 ad agp_amd64_via_set_aperture(struct agp_softc *sc, uint32_t aperture)
529 1.1.6.2 ad {
530 1.1.6.2 ad struct agp_amd64_softc *asc = sc->as_chipc;
531 1.1.6.2 ad uint32_t apsize;
532 1.1.6.2 ad
533 1.1.6.2 ad apsize = ((aperture - 1) >> 20) ^ 0xff;
534 1.1.6.2 ad if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
535 1.1.6.2 ad return EINVAL;
536 1.1.6.2 ad pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_APSIZE,
537 1.1.6.2 ad (pci_conf_read(sc->as_pc, asc->ctrl_tag, AGP3_VIA_APSIZE) & ~0xff) |
538 1.1.6.2 ad apsize);
539 1.1.6.2 ad
540 1.1.6.2 ad return 0;
541 1.1.6.2 ad }
542