agp_i810.c revision 1.15 1 /* $NetBSD: agp_i810.c,v 1.15 2003/01/31 00:07:39 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/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.15 2003/01/31 00:07:39 thorpej 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 aprint_error(": 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 aprint_error(": 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 aprint_error(": 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 aprint_error(": 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 aprint_error(
229 ": unknown memory configuration, disabling\n");
230 agp_generic_detach(sc);
231 return EINVAL;
232 }
233 if (isc->stolen > 0) {
234 aprint_error(": detected %dk stolen memory\n",
235 isc->stolen * 4);
236 }
237 aprint_error("%s: aperture size is %dM\n", sc->as_dev.dv_xname,
238 isc->initial_aperture / 1024 / 1024);
239
240 /* GATT address is already in there, make sure it's enabled */
241 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
242 pgtblctl |= 1;
243 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
244
245 gatt->ag_physical = pgtblctl & ~1;
246 }
247
248 /*
249 * Make sure the chipset can see everything.
250 */
251 agp_flush_cache();
252
253 aprint_normal("%s", sc->as_dev.dv_xname);
254
255 return 0;
256 }
257
258 #if 0
259 static int
260 agp_i810_detach(struct agp_softc *sc)
261 {
262 int error;
263 struct agp_i810_softc *isc = sc->as_chipc;
264
265 error = agp_generic_detach(sc);
266 if (error)
267 return error;
268
269 /* Clear the GATT base. */
270 if (sc->chiptype == CHIP_I810) {
271 WRITE4(AGP_I810_PGTBL_CTL, 0);
272 } else {
273 unsigned int pgtblctl;
274 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
275 pgtblctl &= ~1;
276 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
277 }
278
279 /* Put the aperture back the way it started. */
280 AGP_SET_APERTURE(sc, isc->initial_aperture);
281
282 if (sc->chiptype == CHIP_I810) {
283 agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
284 (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
285 }
286 free(sc->gatt, M_AGP);
287
288 return 0;
289 }
290 #endif
291
292 static u_int32_t
293 agp_i810_get_aperture(struct agp_softc *sc)
294 {
295 struct agp_i810_softc *isc = sc->as_chipc;
296 pcireg_t reg;
297
298 if (isc->chiptype == CHIP_I810) {
299 u_int16_t miscc;
300
301 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
302 miscc = (u_int16_t)(reg >> 16);
303 if ((miscc & AGP_I810_MISCC_WINSIZE) ==
304 AGP_I810_MISCC_WINSIZE_32)
305 return 32 * 1024 * 1024;
306 else
307 return 64 * 1024 * 1024;
308 } else { /* I830 */
309 u_int16_t gcc1;
310
311 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
312 gcc1 = (u_int16_t)(reg >> 16);
313 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
314 return 64 * 1024 * 1024;
315 else
316 return 128 * 1024 * 1024;
317 }
318 }
319
320 static int
321 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
322 {
323 struct agp_i810_softc *isc = sc->as_chipc;
324 pcireg_t reg;
325
326 if (isc->chiptype == CHIP_I810) {
327 u_int16_t miscc;
328
329 /*
330 * Double check for sanity.
331 */
332 if (aperture != (32 * 1024 * 1024) &&
333 aperture != (64 * 1024 * 1024)) {
334 printf("%s: bad aperture size %d\n",
335 sc->as_dev.dv_xname, aperture);
336 return EINVAL;
337 }
338
339 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
340 miscc = (u_int16_t)(reg >> 16);
341 miscc &= ~AGP_I810_MISCC_WINSIZE;
342 if (aperture == 32 * 1024 * 1024)
343 miscc |= AGP_I810_MISCC_WINSIZE_32;
344 else
345 miscc |= AGP_I810_MISCC_WINSIZE_64;
346
347 reg &= 0x0000ffff;
348 reg |= ((pcireg_t)miscc) << 16;
349 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
350 } else { /* I830 */
351 u_int16_t gcc1;
352
353 if (aperture != (64 * 1024 * 1024) &&
354 aperture != (128 * 1024 * 1024)) {
355 printf("%s: bad aperture size %d\n",
356 sc->as_dev.dv_xname, aperture);
357 return EINVAL;
358 }
359 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
360 gcc1 = (u_int16_t)(reg >> 16);
361 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
362 if (aperture == 64 * 1024 * 1024)
363 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
364 else
365 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
366
367 reg &= 0x0000ffff;
368 reg |= ((pcireg_t)gcc1) << 16;
369 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
370 }
371
372 return 0;
373 }
374
375 static int
376 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
377 {
378 struct agp_i810_softc *isc = sc->as_chipc;
379
380 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
381 #ifdef DEBUG
382 printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
383 sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
384 isc->gatt->ag_entries);
385 #endif
386 return EINVAL;
387 }
388
389 if (isc->chiptype == CHIP_I810) {
390 if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
391 #ifdef DEBUG
392 printf("%s: trying to bind into stolen memory",
393 sc->as_dev.dv_xname);
394 #endif
395 return EINVAL;
396 }
397 }
398
399 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
400 physical | 1);
401 return 0;
402 }
403
404 static int
405 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
406 {
407 struct agp_i810_softc *isc = sc->as_chipc;
408
409 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
410 return EINVAL;
411
412 if (isc->chiptype == CHIP_I830 ) {
413 if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
414 #ifdef DEBUG
415 printf("%s: trying to unbind from stolen memory",
416 sc->as_dev.dv_xname);
417 #endif
418 return EINVAL;
419 }
420 }
421
422 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
423 return 0;
424 }
425
426 /*
427 * Writing via memory mapped registers already flushes all TLBs.
428 */
429 static void
430 agp_i810_flush_tlb(struct agp_softc *sc)
431 {
432 }
433
434 static int
435 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
436 {
437
438 return 0;
439 }
440
441 static struct agp_memory *
442 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
443 {
444 struct agp_i810_softc *isc = sc->as_chipc;
445 struct agp_memory *mem;
446
447 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
448 return 0;
449
450 if (sc->as_allocated + size > sc->as_maxmem)
451 return 0;
452
453 if (type == 1) {
454 /*
455 * Mapping local DRAM into GATT.
456 */
457 if (isc->chiptype == CHIP_I830 )
458 return 0;
459 if (size != isc->dcache_size)
460 return 0;
461 } else if (type == 2) {
462 /*
463 * Bogus mapping of a single page for the hardware cursor.
464 */
465 if (size != AGP_PAGE_SIZE)
466 return 0;
467 }
468
469 mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
470 if (mem == NULL)
471 return NULL;
472 mem->am_id = sc->as_nextid++;
473 mem->am_size = size;
474 mem->am_type = type;
475
476 if (type == 2) {
477 /*
478 * Allocate and wire down the page now so that we can
479 * get its physical address.
480 */
481 mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
482 M_WAITOK);
483 if (mem->am_dmaseg == NULL) {
484 free(mem, M_AGP);
485 return NULL;
486 }
487 if (agp_alloc_dmamem(sc->as_dmat, size, 0,
488 &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
489 mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
490 free(mem->am_dmaseg, M_AGP);
491 free(mem, M_AGP);
492 return NULL;
493 }
494 } else if (type != 1) {
495 if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
496 size, 0, BUS_DMA_NOWAIT,
497 &mem->am_dmamap) != 0) {
498 free(mem, M_AGP);
499 return NULL;
500 }
501 }
502
503 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
504 sc->as_allocated += size;
505
506 return mem;
507 }
508
509 static int
510 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
511 {
512 if (mem->am_is_bound)
513 return EBUSY;
514
515 if (mem->am_type == 2) {
516 agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
517 mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
518 free(mem->am_dmaseg, M_AGP);
519 }
520
521 sc->as_allocated -= mem->am_size;
522 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
523 free(mem, M_AGP);
524 return 0;
525 }
526
527 static int
528 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
529 off_t offset)
530 {
531 struct agp_i810_softc *isc = sc->as_chipc;
532 u_int32_t regval, i;
533
534 /*
535 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
536 * X server for mysterious reasons which leads to crashes if we write
537 * to the GTT through the MMIO window.
538 * Until the issue is solved, simply restore it.
539 */
540 regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
541 if (regval != (isc->gatt->ag_physical | 1)) {
542 printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
543 regval);
544 bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
545 isc->gatt->ag_physical | 1);
546 }
547
548 if (mem->am_type == 2) {
549 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
550 mem->am_physical | 1);
551 mem->am_offset = offset;
552 mem->am_is_bound = 1;
553 return 0;
554 }
555
556 if (mem->am_type != 1)
557 return agp_generic_bind_memory(sc, mem, offset);
558
559 if (isc->chiptype == CHIP_I830)
560 return EINVAL;
561
562 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
563 WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
564 i | 3);
565 }
566 mem->am_is_bound = 1;
567 return 0;
568 }
569
570 static int
571 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
572 {
573 struct agp_i810_softc *isc = sc->as_chipc;
574 u_int32_t i;
575
576 if (mem->am_type == 2) {
577 WRITE4(AGP_I810_GTT +
578 (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
579 0);
580 mem->am_offset = 0;
581 mem->am_is_bound = 0;
582 return 0;
583 }
584
585 if (mem->am_type != 1)
586 return agp_generic_unbind_memory(sc, mem);
587
588 if (isc->chiptype == CHIP_I830)
589 return EINVAL;
590
591 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
592 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
593 mem->am_is_bound = 0;
594 return 0;
595 }
596