agp_i810.c revision 1.6 1 /* $NetBSD: agp_i810.c,v 1.6 2001/09/15 00:25:00 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Doug Rabson
5 * Copyright (c) 2000 Ruslan Ermilov
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/proc.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcidevs.h>
46 #include <dev/pci/agpvar.h>
47 #include <dev/pci/agpreg.h>
48
49 #include <sys/agpio.h>
50
51 #include <machine/bus.h>
52
53 #define READ1(off) bus_space_read_1(isc->bst, isc->bsh, off)
54 #define WRITE4(off,v) bus_space_write_4(isc->bst, isc->bsh, off, v)
55
56 struct agp_i810_softc {
57 u_int32_t initial_aperture; /* aperture size at startup */
58 struct agp_gatt *gatt;
59 u_int32_t dcache_size;
60 bus_space_tag_t bst; /* bus_space tag */
61 bus_space_handle_t bsh; /* bus_space handle */
62 struct pci_attach_args vga_pa;
63 };
64
65 static u_int32_t agp_i810_get_aperture(struct agp_softc *);
66 static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
67 static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
68 static int agp_i810_unbind_page(struct agp_softc *, off_t);
69 static void agp_i810_flush_tlb(struct agp_softc *);
70 static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
71 static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
72 vsize_t);
73 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
74 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
75 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
76
77 struct agp_methods agp_i810_methods = {
78 agp_i810_get_aperture,
79 agp_i810_set_aperture,
80 agp_i810_bind_page,
81 agp_i810_unbind_page,
82 agp_i810_flush_tlb,
83 agp_i810_enable,
84 agp_i810_alloc_memory,
85 agp_i810_free_memory,
86 agp_i810_bind_memory,
87 agp_i810_unbind_memory,
88 };
89
90 /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
91 static int
92 agp_i810_vgamatch(struct pci_attach_args *pa)
93 {
94
95 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
96 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
97 return (0);
98
99 switch (PCI_PRODUCT(pa->pa_id)) {
100 case PCI_PRODUCT_INTEL_82810_GC:
101 case PCI_PRODUCT_INTEL_82810_DC100_GC:
102 case PCI_PRODUCT_INTEL_82810E_GC:
103 case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
104 return (1);
105 }
106
107 return (0);
108 }
109
110 int
111 agp_i810_attach(struct device *parent, struct device *self, void *aux)
112 {
113 struct agp_softc *sc = (void *)self;
114 struct agp_i810_softc *isc;
115 struct agp_gatt *gatt;
116 int error;
117
118 isc = malloc(sizeof *isc, M_AGP, M_NOWAIT);
119 if (isc == NULL) {
120 printf(": can't allocate chipset-specific softc\n");
121 return ENOMEM;
122 }
123 memset(isc, 0, sizeof *isc);
124 sc->as_chipc = isc;
125 sc->as_methods = &agp_i810_methods;
126
127 if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
128 free(isc, M_AGP);
129 return ENOENT;
130 }
131
132 /* XXXfvdl */
133 sc->as_dmat = isc->vga_pa.pa_dmat;
134
135 error = agp_map_aperture(&isc->vga_pa, sc);
136 if (error != 0) {
137 free(isc, M_AGP);
138 return error;
139 }
140
141 error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
142 PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
143 if (error != 0) {
144 printf(": can't map mmadr registers\n");
145 return error;
146 }
147
148 isc->initial_aperture = AGP_GET_APERTURE(sc);
149
150 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
151 isc->dcache_size = 4 * 1024 * 1024;
152 else
153 isc->dcache_size = 0;
154
155 for (;;) {
156 gatt = agp_alloc_gatt(sc);
157 if (gatt)
158 break;
159
160 /*
161 * Probably contigmalloc failure. Try reducing the
162 * aperture so that the gatt size reduces.
163 */
164 if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
165 agp_generic_detach(sc);
166 return ENOMEM;
167 }
168 }
169 isc->gatt = gatt;
170
171 /* Install the GATT. */
172 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
173
174 /*
175 * Make sure the chipset can see everything.
176 */
177 agp_flush_cache();
178
179 return 0;
180 }
181
182 #if 0
183 static int
184 agp_i810_detach(struct agp_softc *sc)
185 {
186 int error;
187 struct agp_i810_softc *isc = sc->as_chipc;
188
189 error = agp_generic_detach(sc);
190 if (error)
191 return error;
192
193 /* Clear the GATT base. */
194 WRITE4(AGP_I810_PGTBL_CTL, 0);
195
196 /* Put the aperture back the way it started. */
197 AGP_SET_APERTURE(sc, isc->initial_aperture);
198
199 agp_free_gatt(sc, isc->gatt);
200
201 return 0;
202 }
203 #endif
204
205 static u_int32_t
206 agp_i810_get_aperture(struct agp_softc *sc)
207 {
208 u_int16_t miscc;
209
210 miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
211 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
212 return 32 * 1024 * 1024;
213 else
214 return 64 * 1024 * 1024;
215 }
216
217 static int
218 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
219 {
220 pcireg_t reg, miscc;
221
222 /*
223 * Double check for sanity.
224 */
225 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
226 printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
227 aperture);
228 return EINVAL;
229 }
230
231 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
232 miscc = reg >> 16;
233 miscc &= ~AGP_I810_MISCC_WINSIZE;
234 if (aperture == 32 * 1024 * 1024)
235 miscc |= AGP_I810_MISCC_WINSIZE_32;
236 else
237 miscc |= AGP_I810_MISCC_WINSIZE_64;
238
239 reg &= 0x0000ffff;
240 reg |= (miscc << 16);
241 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, miscc);
242
243 return 0;
244 }
245
246 static int
247 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
248 {
249 struct agp_i810_softc *isc = sc->as_chipc;
250
251 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
252 return EINVAL;
253
254 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
255 physical | 1);
256 return 0;
257 }
258
259 static int
260 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
261 {
262 struct agp_i810_softc *isc = sc->as_chipc;
263
264 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
265 return EINVAL;
266
267 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
268 return 0;
269 }
270
271 /*
272 * Writing via memory mapped registers already flushes all TLBs.
273 */
274 static void
275 agp_i810_flush_tlb(struct agp_softc *sc)
276 {
277 }
278
279 static int
280 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
281 {
282
283 return 0;
284 }
285
286 static struct agp_memory *
287 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
288 {
289 struct agp_i810_softc *isc = sc->as_chipc;
290 struct agp_memory *mem;
291
292 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
293 return 0;
294
295 if (sc->as_allocated + size > sc->as_maxmem)
296 return 0;
297
298 if (type == 1) {
299 /*
300 * Mapping local DRAM into GATT.
301 */
302 if (size != isc->dcache_size)
303 return 0;
304 } else if (type == 2) {
305 /*
306 * Bogus mapping of a single page for the hardware cursor.
307 */
308 if (size != AGP_PAGE_SIZE)
309 return 0;
310 }
311
312 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
313 if (mem == NULL)
314 return NULL;
315 memset(mem, 0, sizeof *mem);
316 mem->am_id = sc->as_nextid++;
317 mem->am_size = size;
318 mem->am_type = type;
319
320 if (type == 2) {
321 /*
322 * Allocate and wire down the page now so that we can
323 * get its physical address.
324 */
325 mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
326 M_WAITOK);
327 if (mem->am_dmaseg == NULL) {
328 free(mem, M_AGP);
329 return NULL;
330 }
331 if (agp_alloc_dmamem(sc->as_dmat, size, 0,
332 &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
333 mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
334 free(mem->am_dmaseg, M_AGP);
335 free(mem, M_AGP);
336 return NULL;
337 }
338 } else if (type != 1) {
339 if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
340 size, 0, BUS_DMA_NOWAIT,
341 &mem->am_dmamap) != 0) {
342 free(mem, M_AGP);
343 return NULL;
344 }
345 }
346
347 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
348 sc->as_allocated += size;
349
350 return mem;
351 }
352
353 static int
354 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
355 {
356 if (mem->am_is_bound)
357 return EBUSY;
358
359 if (mem->am_type == 2) {
360 agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
361 mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
362 free(mem->am_dmaseg, M_AGP);
363 }
364
365 sc->as_allocated -= mem->am_size;
366 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
367 free(mem, M_AGP);
368 return 0;
369 }
370
371 static int
372 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
373 off_t offset)
374 {
375 struct agp_i810_softc *isc = sc->as_chipc;
376 u_int32_t regval, i;
377
378 /*
379 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
380 * X server for mysterious reasons which leads to crashes if we write
381 * to the GTT through the MMIO window.
382 * Until the issue is solved, simply restore it.
383 */
384 regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
385 if (regval != (isc->gatt->ag_physical | 1)) {
386 printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
387 regval);
388 bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
389 isc->gatt->ag_physical | 1);
390 }
391
392 if (mem->am_type == 2) {
393 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
394 mem->am_physical | 1);
395 mem->am_offset = offset;
396 mem->am_is_bound = 1;
397 return 0;
398 }
399
400 if (mem->am_type != 1)
401 return agp_generic_bind_memory(sc, mem, offset);
402
403 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
404 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
405 i | 3);
406 }
407
408 return 0;
409 }
410
411 static int
412 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
413 {
414 struct agp_i810_softc *isc = sc->as_chipc;
415 u_int32_t i;
416
417 if (mem->am_type == 2) {
418 WRITE4(AGP_I810_GTT +
419 (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
420 0);
421 mem->am_offset = 0;
422 mem->am_is_bound = 0;
423 return 0;
424 }
425
426 if (mem->am_type != 1)
427 return agp_generic_unbind_memory(sc, mem);
428
429 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
430 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
431
432 return 0;
433 }
434