agp_i810.c revision 1.14 1 /* $NetBSD: agp_i810.c,v 1.14 2002/12/13 11:32:51 scw 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/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.14 2002/12/13 11:32:51 scw Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42 #include <sys/conf.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/pcidevs.h>
49 #include <dev/pci/agpvar.h>
50 #include <dev/pci/agpreg.h>
51
52 #include <sys/agpio.h>
53
54 #include <machine/bus.h>
55
56 #define READ1(off) bus_space_read_1(isc->bst, isc->bsh, off)
57 #define READ4(off) bus_space_read_4(isc->bst, isc->bsh, off)
58 #define WRITE4(off,v) bus_space_write_4(isc->bst, isc->bsh, off, v)
59
60 #define CHIP_I810 0 /* i810/i815 */
61 #define CHIP_I830 1 /* i830/i845 */
62
63 struct agp_i810_softc {
64 u_int32_t initial_aperture; /* aperture size at startup */
65 struct agp_gatt *gatt;
66 int chiptype; /* i810-like or i830 */
67 u_int32_t dcache_size; /* i810 only */
68 u_int32_t stolen; /* number of i830/845 gtt entries
69 for stolen memory */
70 bus_space_tag_t bst; /* bus_space tag */
71 bus_space_handle_t bsh; /* bus_space handle */
72 struct pci_attach_args vga_pa;
73 };
74
75 static u_int32_t agp_i810_get_aperture(struct agp_softc *);
76 static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
77 static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
78 static int agp_i810_unbind_page(struct agp_softc *, off_t);
79 static void agp_i810_flush_tlb(struct agp_softc *);
80 static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
81 static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
82 vsize_t);
83 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
84 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
85 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
86
87 struct agp_methods agp_i810_methods = {
88 agp_i810_get_aperture,
89 agp_i810_set_aperture,
90 agp_i810_bind_page,
91 agp_i810_unbind_page,
92 agp_i810_flush_tlb,
93 agp_i810_enable,
94 agp_i810_alloc_memory,
95 agp_i810_free_memory,
96 agp_i810_bind_memory,
97 agp_i810_unbind_memory,
98 };
99
100 /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
101 static int
102 agp_i810_vgamatch(struct pci_attach_args *pa)
103 {
104
105 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
106 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
107 return (0);
108
109 switch (PCI_PRODUCT(pa->pa_id)) {
110 case PCI_PRODUCT_INTEL_82810_GC:
111 case PCI_PRODUCT_INTEL_82810_DC100_GC:
112 case PCI_PRODUCT_INTEL_82810E_GC:
113 case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
114 case PCI_PRODUCT_INTEL_82830MP_IV:
115 case PCI_PRODUCT_INTEL_82845G_IGD:
116 return (1);
117 }
118
119 return (0);
120 }
121
122 int
123 agp_i810_attach(struct device *parent, struct device *self, void *aux)
124 {
125 struct agp_softc *sc = (void *)self;
126 struct agp_i810_softc *isc;
127 struct agp_gatt *gatt;
128 int error;
129
130 isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
131 if (isc == NULL) {
132 printf(": can't allocate chipset-specific softc\n");
133 return ENOMEM;
134 }
135 sc->as_chipc = isc;
136 sc->as_methods = &agp_i810_methods;
137
138 if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
139 printf(": can't find internal VGA device config space\n");
140 free(isc, M_AGP);
141 return ENOENT;
142 }
143
144 /* XXXfvdl */
145 sc->as_dmat = isc->vga_pa.pa_dmat;
146
147 error = agp_map_aperture(&isc->vga_pa, sc);
148 if (error != 0) {
149 printf(": can't map aperture\n");
150 free(isc, M_AGP);
151 return error;
152 }
153
154 switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
155 case PCI_PRODUCT_INTEL_82810_GC:
156 case PCI_PRODUCT_INTEL_82810_DC100_GC:
157 case PCI_PRODUCT_INTEL_82810E_GC:
158 case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
159 isc->chiptype = CHIP_I810;
160 break;
161 case PCI_PRODUCT_INTEL_82830MP_IV:
162 case PCI_PRODUCT_INTEL_82845G_IGD:
163 isc->chiptype = CHIP_I830;
164 break;
165 }
166
167 error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
168 PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
169 if (error != 0) {
170 printf(": can't map mmadr registers\n");
171 return error;
172 }
173
174 isc->initial_aperture = AGP_GET_APERTURE(sc);
175
176 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
177 if (!gatt) {
178 agp_generic_detach(sc);
179 return ENOMEM;
180 }
181 isc->gatt = gatt;
182
183 gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
184
185 if (isc->chiptype == CHIP_I810) {
186 int dummyseg;
187 /* Some i810s have on-chip memory called dcache */
188 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
189 isc->dcache_size = 4 * 1024 * 1024;
190 else
191 isc->dcache_size = 0;
192
193 /* According to the specs the gatt on the i810 must be 64k */
194 if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
195 0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
196 &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
197 free(gatt, M_AGP);
198 agp_generic_detach(sc);
199 return ENOMEM;
200 }
201
202 gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
203 memset(gatt->ag_virtual, 0, gatt->ag_size);
204
205 agp_flush_cache();
206 /* Install the GATT. */
207 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
208 } else {
209 /* The i830 automatically initializes the 128k gatt on boot. */
210 pcireg_t reg;
211 u_int32_t pgtblctl;
212 u_int16_t gcc1;
213
214 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
215 gcc1 = (u_int16_t)(reg >> 16);
216 switch (gcc1 & AGP_I830_GCC1_GMS) {
217 case AGP_I830_GCC1_GMS_STOLEN_512:
218 isc->stolen = (512 - 132) * 1024 / 4096;
219 break;
220 case AGP_I830_GCC1_GMS_STOLEN_1024:
221 isc->stolen = (1024 - 132) * 1024 / 4096;
222 break;
223 case AGP_I830_GCC1_GMS_STOLEN_8192:
224 isc->stolen = (8192 - 132) * 1024 / 4096;
225 break;
226 default:
227 isc->stolen = 0;
228 printf(": unknown memory configuration, disabling\n");
229 agp_generic_detach(sc);
230 return EINVAL;
231 }
232 if (isc->stolen > 0) {
233 printf(": detected %dk stolen memory\n",
234 isc->stolen * 4);
235 }
236 printf("%s: aperture size is %dM\n", sc->as_dev.dv_xname,
237 isc->initial_aperture / 1024 / 1024);
238
239 /* GATT address is already in there, make sure it's enabled */
240 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
241 pgtblctl |= 1;
242 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
243
244 gatt->ag_physical = pgtblctl & ~1;
245 }
246
247 /*
248 * Make sure the chipset can see everything.
249 */
250 agp_flush_cache();
251
252 printf("%s", sc->as_dev.dv_xname);
253
254 return 0;
255 }
256
257 #if 0
258 static int
259 agp_i810_detach(struct agp_softc *sc)
260 {
261 int error;
262 struct agp_i810_softc *isc = sc->as_chipc;
263
264 error = agp_generic_detach(sc);
265 if (error)
266 return error;
267
268 /* Clear the GATT base. */
269 if (sc->chiptype == CHIP_I810) {
270 WRITE4(AGP_I810_PGTBL_CTL, 0);
271 } else {
272 unsigned int pgtblctl;
273 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
274 pgtblctl &= ~1;
275 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
276 }
277
278 /* Put the aperture back the way it started. */
279 AGP_SET_APERTURE(sc, isc->initial_aperture);
280
281 if (sc->chiptype == CHIP_I810) {
282 agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
283 (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
284 }
285 free(sc->gatt, M_AGP);
286
287 return 0;
288 }
289 #endif
290
291 static u_int32_t
292 agp_i810_get_aperture(struct agp_softc *sc)
293 {
294 struct agp_i810_softc *isc = sc->as_chipc;
295 pcireg_t reg;
296
297 if (isc->chiptype == CHIP_I810) {
298 u_int16_t miscc;
299
300 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
301 miscc = (u_int16_t)(reg >> 16);
302 if ((miscc & AGP_I810_MISCC_WINSIZE) ==
303 AGP_I810_MISCC_WINSIZE_32)
304 return 32 * 1024 * 1024;
305 else
306 return 64 * 1024 * 1024;
307 } else { /* I830 */
308 u_int16_t gcc1;
309
310 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
311 gcc1 = (u_int16_t)(reg >> 16);
312 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
313 return 64 * 1024 * 1024;
314 else
315 return 128 * 1024 * 1024;
316 }
317 }
318
319 static int
320 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
321 {
322 struct agp_i810_softc *isc = sc->as_chipc;
323 pcireg_t reg;
324
325 if (isc->chiptype == CHIP_I810) {
326 u_int16_t miscc;
327
328 /*
329 * Double check for sanity.
330 */
331 if (aperture != (32 * 1024 * 1024) &&
332 aperture != (64 * 1024 * 1024)) {
333 printf("%s: bad aperture size %d\n",
334 sc->as_dev.dv_xname, aperture);
335 return EINVAL;
336 }
337
338 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
339 miscc = (u_int16_t)(reg >> 16);
340 miscc &= ~AGP_I810_MISCC_WINSIZE;
341 if (aperture == 32 * 1024 * 1024)
342 miscc |= AGP_I810_MISCC_WINSIZE_32;
343 else
344 miscc |= AGP_I810_MISCC_WINSIZE_64;
345
346 reg &= 0x0000ffff;
347 reg |= ((pcireg_t)miscc) << 16;
348 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
349 } else { /* I830 */
350 u_int16_t gcc1;
351
352 if (aperture != (64 * 1024 * 1024) &&
353 aperture != (128 * 1024 * 1024)) {
354 printf("%s: bad aperture size %d\n",
355 sc->as_dev.dv_xname, aperture);
356 return EINVAL;
357 }
358 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
359 gcc1 = (u_int16_t)(reg >> 16);
360 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
361 if (aperture == 64 * 1024 * 1024)
362 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
363 else
364 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
365
366 reg &= 0x0000ffff;
367 reg |= ((pcireg_t)gcc1) << 16;
368 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
369 }
370
371 return 0;
372 }
373
374 static int
375 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
376 {
377 struct agp_i810_softc *isc = sc->as_chipc;
378
379 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
380 #ifdef DEBUG
381 printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
382 sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
383 isc->gatt->ag_entries);
384 #endif
385 return EINVAL;
386 }
387
388 if (isc->chiptype == CHIP_I810) {
389 if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
390 #ifdef DEBUG
391 printf("%s: trying to bind into stolen memory",
392 sc->as_dev.dv_xname);
393 #endif
394 return EINVAL;
395 }
396 }
397
398 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
399 physical | 1);
400 return 0;
401 }
402
403 static int
404 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
405 {
406 struct agp_i810_softc *isc = sc->as_chipc;
407
408 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
409 return EINVAL;
410
411 if (isc->chiptype == CHIP_I830 ) {
412 if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
413 #ifdef DEBUG
414 printf("%s: trying to unbind from stolen memory",
415 sc->as_dev.dv_xname);
416 #endif
417 return EINVAL;
418 }
419 }
420
421 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
422 return 0;
423 }
424
425 /*
426 * Writing via memory mapped registers already flushes all TLBs.
427 */
428 static void
429 agp_i810_flush_tlb(struct agp_softc *sc)
430 {
431 }
432
433 static int
434 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
435 {
436
437 return 0;
438 }
439
440 static struct agp_memory *
441 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
442 {
443 struct agp_i810_softc *isc = sc->as_chipc;
444 struct agp_memory *mem;
445
446 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
447 return 0;
448
449 if (sc->as_allocated + size > sc->as_maxmem)
450 return 0;
451
452 if (type == 1) {
453 /*
454 * Mapping local DRAM into GATT.
455 */
456 if (isc->chiptype == CHIP_I830 )
457 return 0;
458 if (size != isc->dcache_size)
459 return 0;
460 } else if (type == 2) {
461 /*
462 * Bogus mapping of a single page for the hardware cursor.
463 */
464 if (size != AGP_PAGE_SIZE)
465 return 0;
466 }
467
468 mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
469 if (mem == NULL)
470 return NULL;
471 mem->am_id = sc->as_nextid++;
472 mem->am_size = size;
473 mem->am_type = type;
474
475 if (type == 2) {
476 /*
477 * Allocate and wire down the page now so that we can
478 * get its physical address.
479 */
480 mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
481 M_WAITOK);
482 if (mem->am_dmaseg == NULL) {
483 free(mem, M_AGP);
484 return NULL;
485 }
486 if (agp_alloc_dmamem(sc->as_dmat, size, 0,
487 &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
488 mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
489 free(mem->am_dmaseg, M_AGP);
490 free(mem, M_AGP);
491 return NULL;
492 }
493 } else if (type != 1) {
494 if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
495 size, 0, BUS_DMA_NOWAIT,
496 &mem->am_dmamap) != 0) {
497 free(mem, M_AGP);
498 return NULL;
499 }
500 }
501
502 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
503 sc->as_allocated += size;
504
505 return mem;
506 }
507
508 static int
509 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
510 {
511 if (mem->am_is_bound)
512 return EBUSY;
513
514 if (mem->am_type == 2) {
515 agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
516 mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
517 free(mem->am_dmaseg, M_AGP);
518 }
519
520 sc->as_allocated -= mem->am_size;
521 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
522 free(mem, M_AGP);
523 return 0;
524 }
525
526 static int
527 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
528 off_t offset)
529 {
530 struct agp_i810_softc *isc = sc->as_chipc;
531 u_int32_t regval, i;
532
533 /*
534 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
535 * X server for mysterious reasons which leads to crashes if we write
536 * to the GTT through the MMIO window.
537 * Until the issue is solved, simply restore it.
538 */
539 regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
540 if (regval != (isc->gatt->ag_physical | 1)) {
541 printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
542 regval);
543 bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
544 isc->gatt->ag_physical | 1);
545 }
546
547 if (mem->am_type == 2) {
548 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
549 mem->am_physical | 1);
550 mem->am_offset = offset;
551 mem->am_is_bound = 1;
552 return 0;
553 }
554
555 if (mem->am_type != 1)
556 return agp_generic_bind_memory(sc, mem, offset);
557
558 if (isc->chiptype == CHIP_I830)
559 return EINVAL;
560
561 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
562 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
563 i | 3);
564 }
565 mem->am_is_bound = 1;
566 return 0;
567 }
568
569 static int
570 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
571 {
572 struct agp_i810_softc *isc = sc->as_chipc;
573 u_int32_t i;
574
575 if (mem->am_type == 2) {
576 WRITE4(AGP_I810_GTT +
577 (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
578 0);
579 mem->am_offset = 0;
580 mem->am_is_bound = 0;
581 return 0;
582 }
583
584 if (mem->am_type != 1)
585 return agp_generic_unbind_memory(sc, mem);
586
587 if (isc->chiptype == CHIP_I830)
588 return EINVAL;
589
590 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
591 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
592 mem->am_is_bound = 0;
593 return 0;
594 }
595