agp_intel.c revision 1.8 1 1.8 ichiro /* $NetBSD: agp_intel.c,v 1.8 2003/06/14 11:40:20 ichiro Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*-
4 1.1 fvdl * Copyright (c) 2000 Doug Rabson
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * Redistribution and use in source and binary forms, with or without
8 1.1 fvdl * modification, are permitted provided that the following conditions
9 1.1 fvdl * are met:
10 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
11 1.1 fvdl * notice, this list of conditions and the following disclaimer.
12 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
14 1.1 fvdl * documentation and/or other materials provided with the distribution.
15 1.1 fvdl *
16 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 fvdl * SUCH DAMAGE.
27 1.1 fvdl *
28 1.1 fvdl * $FreeBSD: src/sys/pci/agp_intel.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
29 1.1 fvdl */
30 1.4 lukem
31 1.4 lukem #include <sys/cdefs.h>
32 1.8 ichiro __KERNEL_RCSID(0, "$NetBSD: agp_intel.c,v 1.8 2003/06/14 11:40:20 ichiro Exp $");
33 1.1 fvdl
34 1.1 fvdl #include <sys/param.h>
35 1.1 fvdl #include <sys/systm.h>
36 1.1 fvdl #include <sys/malloc.h>
37 1.1 fvdl #include <sys/kernel.h>
38 1.1 fvdl #include <sys/lock.h>
39 1.1 fvdl #include <sys/proc.h>
40 1.1 fvdl #include <sys/agpio.h>
41 1.1 fvdl #include <sys/device.h>
42 1.1 fvdl #include <sys/agpio.h>
43 1.1 fvdl
44 1.1 fvdl #include <uvm/uvm_extern.h>
45 1.1 fvdl
46 1.1 fvdl #include <dev/pci/pcivar.h>
47 1.1 fvdl #include <dev/pci/pcireg.h>
48 1.7 ichiro #include <dev/pci/pcidevs.h>
49 1.1 fvdl #include <dev/pci/agpvar.h>
50 1.1 fvdl #include <dev/pci/agpreg.h>
51 1.1 fvdl
52 1.1 fvdl #include <machine/bus.h>
53 1.1 fvdl
54 1.1 fvdl struct agp_intel_softc {
55 1.7 ichiro u_int32_t initial_aperture;
56 1.7 ichiro /* aperture size at startup */
57 1.7 ichiro struct agp_gatt *gatt;
58 1.7 ichiro struct pci_attach_args vga_pa; /* child device */
59 1.7 ichiro int chiptype;
60 1.8 ichiro #define CHIP_INTEL 0x0
61 1.8 ichiro #define CHIP_I443 0x1
62 1.8 ichiro #define CHIP_I840 0x2
63 1.8 ichiro #define CHIP_I845 0x3
64 1.8 ichiro #define CHIP_I850 0x4
65 1.7 ichiro /* Chip type */
66 1.1 fvdl };
67 1.1 fvdl
68 1.1 fvdl static u_int32_t agp_intel_get_aperture(struct agp_softc *);
69 1.1 fvdl static int agp_intel_set_aperture(struct agp_softc *, u_int32_t);
70 1.1 fvdl static int agp_intel_bind_page(struct agp_softc *, off_t, bus_addr_t);
71 1.1 fvdl static int agp_intel_unbind_page(struct agp_softc *, off_t);
72 1.1 fvdl static void agp_intel_flush_tlb(struct agp_softc *);
73 1.1 fvdl
74 1.1 fvdl struct agp_methods agp_intel_methods = {
75 1.1 fvdl agp_intel_get_aperture,
76 1.1 fvdl agp_intel_set_aperture,
77 1.1 fvdl agp_intel_bind_page,
78 1.1 fvdl agp_intel_unbind_page,
79 1.1 fvdl agp_intel_flush_tlb,
80 1.1 fvdl agp_generic_enable,
81 1.1 fvdl agp_generic_alloc_memory,
82 1.1 fvdl agp_generic_free_memory,
83 1.1 fvdl agp_generic_bind_memory,
84 1.1 fvdl agp_generic_unbind_memory,
85 1.1 fvdl };
86 1.1 fvdl
87 1.7 ichiro static int
88 1.7 ichiro agp_intel_vgamatch(struct pci_attach_args *pa)
89 1.7 ichiro {
90 1.7 ichiro switch (PCI_PRODUCT(pa->pa_id)) {
91 1.7 ichiro case PCI_PRODUCT_INTEL_82855PM_AGP:
92 1.7 ichiro case PCI_PRODUCT_INTEL_82443LX_AGP:
93 1.7 ichiro case PCI_PRODUCT_INTEL_82443BX_AGP:
94 1.7 ichiro case PCI_PRODUCT_INTEL_82443GX_AGP:
95 1.7 ichiro case PCI_PRODUCT_INTEL_82850_AGP:
96 1.7 ichiro case PCI_PRODUCT_INTEL_82845_AGP:
97 1.7 ichiro case PCI_PRODUCT_INTEL_82840_AGP:
98 1.7 ichiro return (1);
99 1.7 ichiro }
100 1.7 ichiro
101 1.7 ichiro return (0);
102 1.7 ichiro }
103 1.7 ichiro
104 1.1 fvdl int
105 1.1 fvdl agp_intel_attach(struct device *parent, struct device *self, void *aux)
106 1.1 fvdl {
107 1.1 fvdl struct agp_softc *sc = (struct agp_softc *)self;
108 1.1 fvdl struct pci_attach_args *pa= aux;
109 1.1 fvdl struct agp_intel_softc *isc;
110 1.1 fvdl struct agp_gatt *gatt;
111 1.1 fvdl pcireg_t reg;
112 1.1 fvdl
113 1.5 tsutsui isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
114 1.1 fvdl if (isc == NULL) {
115 1.6 thorpej aprint_error(": can't allocate chipset-specific softc\n");
116 1.1 fvdl return ENOMEM;
117 1.1 fvdl }
118 1.2 fvdl
119 1.1 fvdl sc->as_methods = &agp_intel_methods;
120 1.2 fvdl sc->as_chipc = isc;
121 1.2 fvdl
122 1.7 ichiro if (pci_find_device(&isc->vga_pa, agp_intel_vgamatch) == 0) {
123 1.7 ichiro aprint_error(": can't find internal VGA device config space\n");
124 1.7 ichiro free(isc, M_AGP);
125 1.7 ichiro return ENOENT;
126 1.7 ichiro }
127 1.7 ichiro
128 1.1 fvdl pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
129 1.1 fvdl NULL);
130 1.1 fvdl
131 1.1 fvdl if (agp_map_aperture(pa, sc) != 0) {
132 1.6 thorpej aprint_error(": can't map aperture\n");
133 1.1 fvdl free(isc, M_AGP);
134 1.2 fvdl sc->as_chipc = NULL;
135 1.1 fvdl return ENXIO;
136 1.1 fvdl }
137 1.1 fvdl
138 1.7 ichiro switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
139 1.7 ichiro case PCI_PRODUCT_INTEL_82855PM_AGP:
140 1.7 ichiro case PCI_PRODUCT_INTEL_82845_AGP:
141 1.7 ichiro isc->chiptype = CHIP_I845;
142 1.7 ichiro break;
143 1.7 ichiro case PCI_PRODUCT_INTEL_82840_AGP:
144 1.8 ichiro isc->chiptype = CHIP_I840;
145 1.8 ichiro break;
146 1.7 ichiro case PCI_PRODUCT_INTEL_82850_AGP:
147 1.8 ichiro isc->chiptype = CHIP_I850;
148 1.7 ichiro break;
149 1.7 ichiro case PCI_PRODUCT_INTEL_82443LX_AGP:
150 1.7 ichiro case PCI_PRODUCT_INTEL_82443BX_AGP:
151 1.7 ichiro case PCI_PRODUCT_INTEL_82443GX_AGP:
152 1.7 ichiro isc->chiptype = CHIP_I443;
153 1.7 ichiro break;
154 1.8 ichiro default:
155 1.8 ichiro isc->chiptype = CHIP_INTEL;
156 1.7 ichiro }
157 1.7 ichiro
158 1.1 fvdl isc->initial_aperture = AGP_GET_APERTURE(sc);
159 1.1 fvdl
160 1.1 fvdl for (;;) {
161 1.1 fvdl gatt = agp_alloc_gatt(sc);
162 1.1 fvdl if (gatt)
163 1.1 fvdl break;
164 1.1 fvdl
165 1.1 fvdl /*
166 1.1 fvdl * Probably contigmalloc failure. Try reducing the
167 1.1 fvdl * aperture so that the gatt size reduces.
168 1.1 fvdl */
169 1.1 fvdl if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
170 1.1 fvdl agp_generic_detach(sc);
171 1.6 thorpej aprint_error(": failed to set aperture\n");
172 1.1 fvdl return ENOMEM;
173 1.1 fvdl }
174 1.1 fvdl }
175 1.1 fvdl isc->gatt = gatt;
176 1.1 fvdl
177 1.1 fvdl /* Install the gatt. */
178 1.1 fvdl pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE,
179 1.1 fvdl gatt->ag_physical);
180 1.8 ichiro
181 1.8 ichiro /* Enable the GLTB and setup the control register. */
182 1.8 ichiro switch (isc->chiptype) {
183 1.8 ichiro case CHIP_I443:
184 1.8 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL,
185 1.8 ichiro AGPCTRL_AGPRSE | AGPCTRL_GTLB);
186 1.8 ichiro
187 1.8 ichiro default:
188 1.8 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL,
189 1.8 ichiro pci_read_config(dev, AGP_INTEL_AGPCTRL) | AGPCTRL_GTLB);
190 1.8 ichiro }
191 1.1 fvdl
192 1.1 fvdl /* Enable things, clear errors etc. */
193 1.7 ichiro switch (isc->chiptype) {
194 1.7 ichiro case CHIP_I845:
195 1.7 ichiro {
196 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCMD,
197 1.7 ichiro AGPCMD_SBA | AGPCMD_AGPEN | AGPCMD_RATE_4X);
198 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_I845_AGPMISC, AGPMISC_AAGN);
199 1.7 ichiro break;
200 1.7 ichiro }
201 1.7 ichiro case CHIP_I840:
202 1.8 ichiro case CHIP_I850:
203 1.7 ichiro {
204 1.8 ichiro #if XXX
205 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCMD,
206 1.7 ichiro AGPCMD_SBA | AGPCMD_AGPEN | AGPCMD_RATE_4X);
207 1.8 ichiro #endif
208 1.8 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_I840_MCHCFG,
209 1.8 ichiro MCHCFG_AAGN);
210 1.7 ichiro break;
211 1.7 ichiro }
212 1.8 ichiro default:
213 1.7 ichiro {
214 1.7 ichiro reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
215 1.7 ichiro reg &= ~NBXCFG_APAE;
216 1.7 ichiro reg |= NBXCFG_AAGN;
217 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
218 1.8 ichiro }
219 1.8 ichiro }
220 1.7 ichiro
221 1.8 ichiro /* Clear Error status */
222 1.8 ichiro switch (type) {
223 1.8 ichiro case CHIP_I840:
224 1.8 ichiro pci_write_config(dev, AGP_INTEL_I8XX_ERRSTS, 0xc000);
225 1.8 ichiro break;
226 1.1 fvdl
227 1.8 ichiro case CHIP_I845:
228 1.8 ichiro case CHIP_I850:
229 1.8 ichiro pci_write_config(dev, AGP_INTEL_I8XX_ERRSTS, 0x00ff);
230 1.7 ichiro break;
231 1.8 ichiro
232 1.8 ichiro default:
233 1.8 ichiro pci_write_config(dev, AGP_INTEL_ERRSTS, 0x70);
234 1.7 ichiro }
235 1.1 fvdl
236 1.1 fvdl return 0;
237 1.1 fvdl }
238 1.1 fvdl
239 1.1 fvdl #if 0
240 1.1 fvdl static int
241 1.1 fvdl agp_intel_detach(struct agp_softc *sc)
242 1.1 fvdl {
243 1.1 fvdl int error;
244 1.1 fvdl pcireg_t reg;
245 1.1 fvdl struct agp_intel_softc *isc = sc->as_chipc;
246 1.1 fvdl
247 1.1 fvdl error = agp_generic_detach(sc);
248 1.1 fvdl if (error)
249 1.1 fvdl return error;
250 1.1 fvdl
251 1.7 ichiro /* XXX i845/i855PM/i840/i850E */
252 1.1 fvdl reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
253 1.1 fvdl reg &= ~(1 << 9);
254 1.1 fvdl printf("%s: set NBXCFG to %x\n", __FUNCTION__, reg);
255 1.1 fvdl pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
256 1.1 fvdl pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE, 0);
257 1.1 fvdl AGP_SET_APERTURE(sc, isc->initial_aperture);
258 1.1 fvdl agp_free_gatt(sc, isc->gatt);
259 1.1 fvdl
260 1.1 fvdl return 0;
261 1.1 fvdl }
262 1.1 fvdl #endif
263 1.1 fvdl
264 1.1 fvdl static u_int32_t
265 1.1 fvdl agp_intel_get_aperture(struct agp_softc *sc)
266 1.1 fvdl {
267 1.1 fvdl u_int32_t apsize;
268 1.1 fvdl
269 1.7 ichiro apsize = pci_conf_read(sc->as_pc, sc->as_tag,
270 1.7 ichiro AGP_INTEL_APSIZE) & APSIZE_MASK;
271 1.1 fvdl
272 1.1 fvdl /*
273 1.1 fvdl * The size is determined by the number of low bits of
274 1.1 fvdl * register APBASE which are forced to zero. The low 22 bits
275 1.1 fvdl * are always forced to zero and each zero bit in the apsize
276 1.1 fvdl * field just read forces the corresponding bit in the 27:22
277 1.1 fvdl * to be zero. We calculate the aperture size accordingly.
278 1.1 fvdl */
279 1.7 ichiro return (((apsize ^ APSIZE_MASK) << 22) | ((1 << 22) - 1)) + 1;
280 1.1 fvdl }
281 1.1 fvdl
282 1.1 fvdl static int
283 1.1 fvdl agp_intel_set_aperture(struct agp_softc *sc, u_int32_t aperture)
284 1.1 fvdl {
285 1.1 fvdl u_int32_t apsize;
286 1.1 fvdl pcireg_t reg;
287 1.1 fvdl
288 1.1 fvdl /*
289 1.1 fvdl * Reverse the magic from get_aperture.
290 1.1 fvdl */
291 1.7 ichiro apsize = ((aperture - 1) >> 22) ^ APSIZE_MASK;
292 1.1 fvdl
293 1.1 fvdl /*
294 1.1 fvdl * Double check for sanity.
295 1.1 fvdl */
296 1.7 ichiro if ((((apsize ^ APSIZE_MASK) << 22) |
297 1.7 ichiro ((1 << 22) - 1)) + 1 != aperture)
298 1.1 fvdl return EINVAL;
299 1.1 fvdl
300 1.1 fvdl reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE);
301 1.1 fvdl reg = (reg & 0xffffff00) | apsize;
302 1.1 fvdl pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE, reg);
303 1.1 fvdl
304 1.1 fvdl return 0;
305 1.1 fvdl }
306 1.1 fvdl
307 1.1 fvdl static int
308 1.1 fvdl agp_intel_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
309 1.1 fvdl {
310 1.1 fvdl struct agp_intel_softc *isc = sc->as_chipc;
311 1.1 fvdl
312 1.1 fvdl if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
313 1.1 fvdl return EINVAL;
314 1.1 fvdl
315 1.1 fvdl isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 0x17;
316 1.1 fvdl return 0;
317 1.1 fvdl }
318 1.1 fvdl
319 1.1 fvdl static int
320 1.1 fvdl agp_intel_unbind_page(struct agp_softc *sc, off_t offset)
321 1.1 fvdl {
322 1.1 fvdl struct agp_intel_softc *isc = sc->as_chipc;
323 1.1 fvdl
324 1.1 fvdl if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
325 1.1 fvdl return EINVAL;
326 1.1 fvdl
327 1.1 fvdl isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
328 1.1 fvdl return 0;
329 1.1 fvdl }
330 1.1 fvdl
331 1.1 fvdl static void
332 1.1 fvdl agp_intel_flush_tlb(struct agp_softc *sc)
333 1.1 fvdl {
334 1.7 ichiro struct agp_intel_softc *isc = sc->as_chipc;
335 1.7 ichiro
336 1.7 ichiro switch (isc->chiptype) {
337 1.8 ichiro case CHIP_I850:
338 1.7 ichiro case CHIP_I845:
339 1.7 ichiro case CHIP_I840:
340 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL,
341 1.7 ichiro 0x0);
342 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL,
343 1.7 ichiro AGPCTRL_GTLB);
344 1.7 ichiro break;
345 1.7 ichiro case CHIP_I443:
346 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL,
347 1.7 ichiro AGPCTRL_AGPRSE);
348 1.7 ichiro pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL,
349 1.7 ichiro AGPCTRL_AGPRSE | AGPCTRL_GTLB);
350 1.7 ichiro break;
351 1.7 ichiro }
352 1.1 fvdl }
353