agp_via.c revision 1.12 1 /* $NetBSD: agp_via.c,v 1.12 2007/03/27 00:34:16 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/pci/agp_via.c,v 1.3 2001/07/05 21:28:47 jhb Exp $
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: agp_via.c,v 1.12 2007/03/27 00:34:16 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/proc.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/agpio.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/agpvar.h>
49 #include <dev/pci/agpreg.h>
50 #include <dev/pci/pcidevs.h>
51
52 #include <machine/bus.h>
53
54 static u_int32_t agp_via_get_aperture(struct agp_softc *);
55 static int agp_via_set_aperture(struct agp_softc *, u_int32_t);
56 static int agp_via_bind_page(struct agp_softc *, off_t, bus_addr_t);
57 static int agp_via_unbind_page(struct agp_softc *, off_t);
58 static void agp_via_flush_tlb(struct agp_softc *);
59
60 static struct agp_methods agp_via_methods = {
61 agp_via_get_aperture,
62 agp_via_set_aperture,
63 agp_via_bind_page,
64 agp_via_unbind_page,
65 agp_via_flush_tlb,
66 agp_generic_enable,
67 agp_generic_alloc_memory,
68 agp_generic_free_memory,
69 agp_generic_bind_memory,
70 agp_generic_unbind_memory,
71 };
72
73 struct agp_via_softc {
74 u_int32_t initial_aperture; /* aperture size at startup */
75 struct agp_gatt *gatt;
76 int *regs;
77 };
78
79 #define REG_GARTCTRL 0
80 #define REG_APSIZE 1
81 #define REG_ATTBASE 2
82
83 static int via_v2_regs[] =
84 { AGP_VIA_GARTCTRL, AGP_VIA_APSIZE, AGP_VIA_ATTBASE };
85 static int via_v3_regs[] =
86 { AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE, AGP3_VIA_ATTBASE };
87
88 static pci_product_id_t via_v3_devices[] = {
89 0x0314,
90 0xffff,
91 };
92
93 int
94 agp_via_attach(struct device *parent, struct device *self, void *aux)
95 {
96 struct pci_attach_args *pa = aux;
97 struct agp_softc *sc = (void *)self;
98 struct agp_via_softc *asc;
99 struct agp_gatt *gatt;
100 pcireg_t agpsel;
101 int i;
102
103 asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
104 if (asc == NULL) {
105 aprint_error(": can't allocate chipset-specific softc\n");
106 return ENOMEM;
107 }
108 sc->as_chipc = asc;
109 sc->as_methods = &agp_via_methods;
110 pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
111 NULL);
112
113 asc->regs = via_v2_regs;
114 for (i = 0; via_v3_devices[i] != 0xffff; i++) {
115 if (PCI_PRODUCT(pa->pa_id) == via_v3_devices[i]) {
116 agpsel = pci_conf_read(pa->pa_pc, pa->pa_tag,
117 AGP_VIA_AGPSEL);
118 if ((agpsel & (1 << 1)) == 0) {
119 asc->regs = via_v3_regs;
120 printf(" (v3)");
121 } else {
122 asc->regs = via_v2_regs;
123 printf(" (v2 compat mode)");
124 }
125 break;
126 }
127 }
128
129 if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
130 aprint_error(": can't map aperture\n");
131 free(asc, M_AGP);
132 return ENXIO;
133 }
134
135 asc->initial_aperture = AGP_GET_APERTURE(sc);
136
137 for (;;) {
138 gatt = agp_alloc_gatt(sc);
139 if (gatt)
140 break;
141
142 /*
143 * Probably contigmalloc failure. Try reducing the
144 * aperture so that the gatt size reduces.
145 */
146 if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
147 agp_generic_detach(sc);
148 aprint_error(": can't set aperture size\n");
149 return ENOMEM;
150 }
151 }
152 asc->gatt = gatt;
153
154 if (asc->regs == via_v2_regs) {
155 /* Install the gatt. */
156 pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_ATTBASE],
157 gatt->ag_physical | 3);
158 /* Enable the aperture. */
159 pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_GARTCTRL],
160 0x0000000f);
161 } else {
162 pcireg_t gartctrl;
163 /* Install the gatt. */
164 pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_ATTBASE],
165 gatt->ag_physical);
166 /* Enable the aperture. */
167 gartctrl = pci_conf_read(pa->pa_pc, pa->pa_tag,
168 asc->regs[REG_ATTBASE]);
169 pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_GARTCTRL],
170 gartctrl | (3 << 7));
171 }
172
173 return 0;
174 }
175
176 #if 0
177 static int
178 agp_via_detach(struct agp_softc *sc)
179 {
180 struct agp_via_softc *asc = sc->as_chipc;
181 int error;
182
183 error = agp_generic_detach(sc);
184 if (error)
185 return error;
186
187 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL], 0);
188 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_ATTBASE], 0);
189 AGP_SET_APERTURE(sc, asc->initial_aperture);
190 agp_free_gatt(sc, asc->gatt);
191
192 return 0;
193 }
194 #endif
195
196 static u_int32_t
197 agp_via_get_aperture(struct agp_softc *sc)
198 {
199 struct agp_via_softc *asc = sc->as_chipc;
200 u_int32_t apsize;
201
202 apsize = pci_conf_read(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE])
203 & 0x1f;
204
205 /*
206 * The size is determined by the number of low bits of
207 * register APBASE which are forced to zero. The low 20 bits
208 * are always forced to zero and each zero bit in the apsize
209 * field just read forces the corresponding bit in the 27:20
210 * to be zero. We calculate the aperture size accordingly.
211 */
212 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1;
213 }
214
215 static int
216 agp_via_set_aperture(struct agp_softc *sc, u_int32_t aperture)
217 {
218 struct agp_via_softc *asc = sc->as_chipc;
219 u_int32_t apsize;
220 pcireg_t reg;
221
222 /*
223 * Reverse the magic from get_aperture.
224 */
225 apsize = ((aperture - 1) >> 20) ^ 0xff;
226
227 /*
228 * Double check for sanity.
229 */
230 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
231 return EINVAL;
232
233 reg = pci_conf_read(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE]);
234 reg &= ~0xff;
235 reg |= apsize;
236 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE], reg);
237
238 return 0;
239 }
240
241 static int
242 agp_via_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
243 {
244 struct agp_via_softc *asc = sc->as_chipc;
245
246 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
247 return EINVAL;
248
249 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
250 return 0;
251 }
252
253 static int
254 agp_via_unbind_page(struct agp_softc *sc, off_t offset)
255 {
256 struct agp_via_softc *asc = sc->as_chipc;
257
258 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
259 return EINVAL;
260
261 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
262 return 0;
263 }
264
265 static void
266 agp_via_flush_tlb(struct agp_softc *sc)
267 {
268 struct agp_via_softc *asc = sc->as_chipc;
269 pcireg_t gartctrl;
270
271 if (asc->regs == via_v2_regs) {
272 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
273 0x8f);
274 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
275 0x0f);
276 } else {
277 gartctrl = pci_conf_read(sc->as_pc, sc->as_tag,
278 asc->regs[REG_GARTCTRL]);
279 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
280 gartctrl & ~(1 << 7));
281 pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
282 gartctrl);
283 }
284 }
285