agp_i810.c revision 1.41.6.2 1 /* $NetBSD: agp_i810.c,v 1.41.6.2 2007/08/06 19:49:35 jmcneill 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.41.6.2 2007/08/06 19:49:35 jmcneill 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 #include "agp_intel.h"
57
58 #define READ1(off) bus_space_read_1(isc->bst, isc->bsh, off)
59 #define READ4(off) bus_space_read_4(isc->bst, isc->bsh, off)
60 #define WRITE4(off,v) bus_space_write_4(isc->bst, isc->bsh, off, v)
61 #define WRITEGTT(off, v) \
62 do { \
63 if (isc->chiptype == CHIP_I915) { \
64 bus_space_write_4(isc->gtt_bst, isc->gtt_bsh, \
65 (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4, \
66 (v)); \
67 } else { \
68 WRITE4(AGP_I810_GTT + \
69 (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4, \
70 (v)); \
71 } \
72 } while (0)
73
74 #define CHIP_I810 0 /* i810/i815 */
75 #define CHIP_I830 1 /* 830M/845G */
76 #define CHIP_I855 2 /* 852GM/855GM/865G */
77 #define CHIP_I915 3 /* 915G/915GM/945G/945GM */
78
79 struct agp_i810_softc {
80 u_int32_t initial_aperture; /* aperture size at startup */
81 struct agp_gatt *gatt;
82 int chiptype; /* i810-like or i830 */
83 u_int32_t dcache_size; /* i810 only */
84 u_int32_t stolen; /* number of i830/845 gtt entries
85 for stolen memory */
86 bus_space_tag_t bst; /* register bus_space tag */
87 bus_space_handle_t bsh; /* register bus_space handle */
88 bus_space_tag_t gtt_bst; /* GTT bus_space tag */
89 bus_space_handle_t gtt_bsh; /* GTT bus_space handle */
90 struct pci_attach_args vga_pa;
91
92 struct pci_conf_state sc_pciconf;
93 };
94
95 static u_int32_t agp_i810_get_aperture(struct agp_softc *);
96 static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
97 static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
98 static int agp_i810_unbind_page(struct agp_softc *, off_t);
99 static void agp_i810_flush_tlb(struct agp_softc *);
100 static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
101 static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
102 vsize_t);
103 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
104 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
105 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
106
107 static struct agp_methods agp_i810_methods = {
108 agp_i810_get_aperture,
109 agp_i810_set_aperture,
110 agp_i810_bind_page,
111 agp_i810_unbind_page,
112 agp_i810_flush_tlb,
113 agp_i810_enable,
114 agp_i810_alloc_memory,
115 agp_i810_free_memory,
116 agp_i810_bind_memory,
117 agp_i810_unbind_memory,
118 };
119
120 /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
121 static int
122 agp_i810_vgamatch(struct pci_attach_args *pa)
123 {
124
125 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
126 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
127 return (0);
128
129 switch (PCI_PRODUCT(pa->pa_id)) {
130 case PCI_PRODUCT_INTEL_82810_GC:
131 case PCI_PRODUCT_INTEL_82810_DC100_GC:
132 case PCI_PRODUCT_INTEL_82810E_GC:
133 case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
134 case PCI_PRODUCT_INTEL_82830MP_IV:
135 case PCI_PRODUCT_INTEL_82845G_IGD:
136 case PCI_PRODUCT_INTEL_82855GM_IGD:
137 case PCI_PRODUCT_INTEL_82865_IGD:
138 case PCI_PRODUCT_INTEL_82915G_IGD:
139 case PCI_PRODUCT_INTEL_82915GM_IGD:
140 case PCI_PRODUCT_INTEL_82945P_IGD:
141 case PCI_PRODUCT_INTEL_82945GM_IGD:
142 case PCI_PRODUCT_INTEL_82945GM_IGD_1:
143 return (1);
144 }
145
146 return (0);
147 }
148
149 int
150 agp_i810_attach(struct device *parent, struct device *self, void *aux)
151 {
152 struct agp_softc *sc = (void *)self;
153 struct agp_i810_softc *isc;
154 struct agp_gatt *gatt;
155 int error, apbase;
156 bus_size_t mmadrsize;
157
158 isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
159 if (isc == NULL) {
160 aprint_error(": can't allocate chipset-specific softc\n");
161 return ENOMEM;
162 }
163 sc->as_chipc = isc;
164 sc->as_methods = &agp_i810_methods;
165
166 if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
167 #if NAGP_INTEL > 0
168 const struct pci_attach_args *pa = aux;
169
170 switch (PCI_PRODUCT(pa->pa_id)) {
171 case PCI_PRODUCT_INTEL_82840_HB:
172 case PCI_PRODUCT_INTEL_82865_HB:
173 case PCI_PRODUCT_INTEL_82845G_DRAM:
174 case PCI_PRODUCT_INTEL_82815_FULL_HUB:
175 return agp_intel_attach(parent, self, aux);
176 }
177 #endif
178 aprint_error(": can't find internal VGA device config space\n");
179 free(isc, M_AGP);
180 return ENOENT;
181 }
182
183 /* XXXfvdl */
184 sc->as_dmat = isc->vga_pa.pa_dmat;
185
186 switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
187 case PCI_PRODUCT_INTEL_82810_GC:
188 case PCI_PRODUCT_INTEL_82810_DC100_GC:
189 case PCI_PRODUCT_INTEL_82810E_GC:
190 case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
191 isc->chiptype = CHIP_I810;
192 break;
193 case PCI_PRODUCT_INTEL_82830MP_IV:
194 case PCI_PRODUCT_INTEL_82845G_IGD:
195 isc->chiptype = CHIP_I830;
196 break;
197 case PCI_PRODUCT_INTEL_82855GM_IGD:
198 case PCI_PRODUCT_INTEL_82865_IGD:
199 isc->chiptype = CHIP_I855;
200 break;
201 case PCI_PRODUCT_INTEL_82915G_IGD:
202 case PCI_PRODUCT_INTEL_82915GM_IGD:
203 case PCI_PRODUCT_INTEL_82945P_IGD:
204 case PCI_PRODUCT_INTEL_82945GM_IGD:
205 case PCI_PRODUCT_INTEL_82945GM_IGD_1:
206 isc->chiptype = CHIP_I915;
207 break;
208 }
209
210 apbase = isc->chiptype == CHIP_I915 ? AGP_I915_GMADR : AGP_I810_GMADR;
211 error = agp_map_aperture(&isc->vga_pa, sc, apbase);
212 if (error != 0) {
213 aprint_error(": can't map aperture\n");
214 free(isc, M_AGP);
215 return error;
216 }
217
218 if (isc->chiptype == CHIP_I915) {
219 error = pci_mapreg_map(&isc->vga_pa, AGP_I915_MMADR,
220 PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
221 NULL, &mmadrsize);
222 if (error != 0) {
223 aprint_error(": can't map mmadr registers\n");
224 agp_generic_detach(sc);
225 return error;
226 }
227 error = pci_mapreg_map(&isc->vga_pa, AGP_I915_GTTADR,
228 PCI_MAPREG_TYPE_MEM, 0, &isc->gtt_bst, &isc->gtt_bsh,
229 NULL, NULL);
230 if (error != 0) {
231 aprint_error(": can't map gttadr registers\n");
232 /* XXX we should release mmadr here */
233 agp_generic_detach(sc);
234 return error;
235 }
236 } else {
237 error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
238 PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
239 NULL, &mmadrsize);
240 if (error != 0) {
241 aprint_error(": can't map mmadr registers\n");
242 agp_generic_detach(sc);
243 return error;
244 }
245 }
246
247 isc->initial_aperture = AGP_GET_APERTURE(sc);
248
249 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
250 if (!gatt) {
251 agp_generic_detach(sc);
252 return ENOMEM;
253 }
254 isc->gatt = gatt;
255
256 gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
257
258 if (isc->chiptype == CHIP_I810) {
259 void *virtual;
260 int dummyseg;
261
262 /* Some i810s have on-chip memory called dcache */
263 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
264 isc->dcache_size = 4 * 1024 * 1024;
265 else
266 isc->dcache_size = 0;
267
268 /* According to the specs the gatt on the i810 must be 64k */
269 if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
270 0, &gatt->ag_dmamap, &virtual, &gatt->ag_physical,
271 &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
272 free(gatt, M_AGP);
273 agp_generic_detach(sc);
274 return ENOMEM;
275 }
276 gatt->ag_virtual = (uint32_t *)virtual;
277
278 gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
279 memset(gatt->ag_virtual, 0, gatt->ag_size);
280
281 agp_flush_cache();
282 /* Install the GATT. */
283 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
284 } else if (isc->chiptype == CHIP_I830) {
285 /* The i830 automatically initializes the 128k gatt on boot. */
286 pcireg_t reg;
287 u_int32_t pgtblctl;
288 u_int16_t gcc1;
289
290 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
291 gcc1 = (u_int16_t)(reg >> 16);
292 switch (gcc1 & AGP_I830_GCC1_GMS) {
293 case AGP_I830_GCC1_GMS_STOLEN_512:
294 isc->stolen = (512 - 132) * 1024 / 4096;
295 break;
296 case AGP_I830_GCC1_GMS_STOLEN_1024:
297 isc->stolen = (1024 - 132) * 1024 / 4096;
298 break;
299 case AGP_I830_GCC1_GMS_STOLEN_8192:
300 isc->stolen = (8192 - 132) * 1024 / 4096;
301 break;
302 default:
303 isc->stolen = 0;
304 aprint_error(
305 ": unknown memory configuration, disabling\n");
306 agp_generic_detach(sc);
307 return EINVAL;
308 }
309 if (isc->stolen > 0) {
310 aprint_error(": detected %dk stolen memory\n%s",
311 isc->stolen * 4, sc->as_dev.dv_xname);
312 }
313
314 /* GATT address is already in there, make sure it's enabled */
315 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
316 pgtblctl |= 1;
317 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
318
319 gatt->ag_physical = pgtblctl & ~1;
320 } else if (isc->chiptype == CHIP_I855) {
321 /* The 855GM automatically initializes the 128k gatt on boot. */
322 pcireg_t reg;
323 u_int32_t pgtblctl;
324 u_int16_t gcc1;
325
326 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
327 gcc1 = (u_int16_t)(reg >> 16);
328 switch (gcc1 & AGP_I855_GCC1_GMS) {
329 case AGP_I855_GCC1_GMS_STOLEN_1M:
330 isc->stolen = (1024 - 132) * 1024 / 4096;
331 break;
332 case AGP_I855_GCC1_GMS_STOLEN_4M:
333 isc->stolen = (4096 - 132) * 1024 / 4096;
334 break;
335 case AGP_I855_GCC1_GMS_STOLEN_8M:
336 isc->stolen = (8192 - 132) * 1024 / 4096;
337 break;
338 case AGP_I855_GCC1_GMS_STOLEN_16M:
339 isc->stolen = (16384 - 132) * 1024 / 4096;
340 break;
341 case AGP_I855_GCC1_GMS_STOLEN_32M:
342 isc->stolen = (32768 - 132) * 1024 / 4096;
343 break;
344 default:
345 isc->stolen = 0;
346 aprint_error(
347 ": unknown memory configuration, disabling\n");
348 agp_generic_detach(sc);
349 return EINVAL;
350 }
351 if (isc->stolen > 0) {
352 aprint_error(": detected %dk stolen memory\n%s",
353 isc->stolen * 4, sc->as_dev.dv_xname);
354 }
355
356 /* GATT address is already in there, make sure it's enabled */
357 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
358 pgtblctl |= 1;
359 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
360
361 gatt->ag_physical = pgtblctl & ~1;
362 } else { /* CHIP_I915 */
363 /* The 915G automatically initializes the 256k gatt on boot. */
364 pcireg_t reg;
365 u_int32_t pgtblctl;
366 u_int16_t gcc1;
367
368 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I915_GCC1);
369 gcc1 = (u_int16_t)(reg >> 16);
370 switch (gcc1 & AGP_I915_GCC1_GMS) {
371 case AGP_I915_GCC1_GMS_STOLEN_0M:
372 isc->stolen = 0;
373 break;
374 case AGP_I915_GCC1_GMS_STOLEN_1M:
375 isc->stolen = (1024 - 260) * 1024 / 4096;
376 break;
377 case AGP_I915_GCC1_GMS_STOLEN_8M:
378 isc->stolen = (8192 - 260) * 1024 / 4096;
379 break;
380 case AGP_I915_GCC1_GMS_STOLEN_16M:
381 isc->stolen = (16384 - 260) * 1024 / 4096;
382 break;
383 case AGP_I915_GCC1_GMS_STOLEN_32M:
384 isc->stolen = (32768 - 260) * 1024 / 4096;
385 break;
386 case AGP_I915_GCC1_GMS_STOLEN_48M:
387 isc->stolen = (49152 - 260) * 1024 / 4096;
388 break;
389 case AGP_I915_GCC1_GMS_STOLEN_64M:
390 isc->stolen = (65536 - 260) * 1024 / 4096;
391 break;
392 default:
393 isc->stolen = 0;
394 aprint_error(
395 ": unknown memory configuration, disabling\n");
396 agp_generic_detach(sc);
397 return EINVAL;
398 }
399 if (isc->stolen > 0) {
400 aprint_error(": detected %dk stolen memory\n%s",
401 isc->stolen * 4, sc->as_dev.dv_xname);
402 }
403
404 /* GATT address is already in there, make sure it's enabled */
405 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
406 pgtblctl |= 1;
407 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
408
409 gatt->ag_physical = pgtblctl & ~1;
410 }
411
412 /*
413 * Make sure the chipset can see everything.
414 */
415 agp_flush_cache();
416
417 #if 0
418 /*
419 * another device (drm) may need access to this region
420 * we do not need it anymore
421 */
422 bus_space_unmap(isc->bst, isc->bsh, mmadrsize);
423 #endif
424
425 return 0;
426 }
427
428 #if 0
429 static int
430 agp_i810_detach(struct agp_softc *sc)
431 {
432 int error;
433 struct agp_i810_softc *isc = sc->as_chipc;
434
435 error = agp_generic_detach(sc);
436 if (error)
437 return error;
438
439 /* Clear the GATT base. */
440 if (sc->chiptype == CHIP_I810) {
441 WRITE4(AGP_I810_PGTBL_CTL, 0);
442 } else {
443 unsigned int pgtblctl;
444 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
445 pgtblctl &= ~1;
446 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
447 }
448
449 /* Put the aperture back the way it started. */
450 AGP_SET_APERTURE(sc, isc->initial_aperture);
451
452 if (sc->chiptype == CHIP_I810) {
453 agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
454 (void *)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
455 }
456 free(sc->gatt, M_AGP);
457
458 return 0;
459 }
460 #endif
461
462 static u_int32_t
463 agp_i810_get_aperture(struct agp_softc *sc)
464 {
465 struct agp_i810_softc *isc = sc->as_chipc;
466 pcireg_t reg;
467
468 if (isc->chiptype == CHIP_I810) {
469 u_int16_t miscc;
470
471 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
472 miscc = (u_int16_t)(reg >> 16);
473 if ((miscc & AGP_I810_MISCC_WINSIZE) ==
474 AGP_I810_MISCC_WINSIZE_32)
475 return 32 * 1024 * 1024;
476 else
477 return 64 * 1024 * 1024;
478 } else if (isc->chiptype == CHIP_I830) {
479 u_int16_t gcc1;
480
481 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
482 gcc1 = (u_int16_t)(reg >> 16);
483 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
484 return 64 * 1024 * 1024;
485 else
486 return 128 * 1024 * 1024;
487 } else if (isc->chiptype == CHIP_I855) {
488 return 128 * 1024 * 1024;
489 } else { /* CHIP_I915 */
490 u_int16_t msac;
491
492 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I915_MSAC);
493 msac = (u_int16_t)(reg >> 16);
494 if (msac & AGP_I915_MSAC_APER_128M)
495 return 128 * 1024 * 1024;
496 else
497 return 256 * 1024 * 1024;
498 }
499 }
500
501 static int
502 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
503 {
504 struct agp_i810_softc *isc = sc->as_chipc;
505 pcireg_t reg;
506
507 if (isc->chiptype == CHIP_I810) {
508 u_int16_t miscc;
509
510 /*
511 * Double check for sanity.
512 */
513 if (aperture != (32 * 1024 * 1024) &&
514 aperture != (64 * 1024 * 1024)) {
515 printf("%s: bad aperture size %d\n",
516 sc->as_dev.dv_xname, aperture);
517 return EINVAL;
518 }
519
520 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
521 miscc = (u_int16_t)(reg >> 16);
522 miscc &= ~AGP_I810_MISCC_WINSIZE;
523 if (aperture == 32 * 1024 * 1024)
524 miscc |= AGP_I810_MISCC_WINSIZE_32;
525 else
526 miscc |= AGP_I810_MISCC_WINSIZE_64;
527
528 reg &= 0x0000ffff;
529 reg |= ((pcireg_t)miscc) << 16;
530 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
531 } else if (isc->chiptype == CHIP_I830) {
532 u_int16_t gcc1;
533
534 if (aperture != (64 * 1024 * 1024) &&
535 aperture != (128 * 1024 * 1024)) {
536 printf("%s: bad aperture size %d\n",
537 sc->as_dev.dv_xname, aperture);
538 return EINVAL;
539 }
540 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
541 gcc1 = (u_int16_t)(reg >> 16);
542 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
543 if (aperture == 64 * 1024 * 1024)
544 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
545 else
546 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
547
548 reg &= 0x0000ffff;
549 reg |= ((pcireg_t)gcc1) << 16;
550 pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
551 } else { /* CHIP_I855 or CHIP_I915 */
552 if (aperture != agp_i810_get_aperture(sc)) {
553 printf("%s: bad aperture size %d\n",
554 sc->as_dev.dv_xname, aperture);
555 return EINVAL;
556 }
557 }
558
559 return 0;
560 }
561
562 static int
563 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
564 {
565 struct agp_i810_softc *isc = sc->as_chipc;
566
567 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
568 #ifdef AGP_DEBUG
569 printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
570 sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
571 isc->gatt->ag_entries);
572 #endif
573 return EINVAL;
574 }
575
576 if (isc->chiptype != CHIP_I830) {
577 if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
578 #ifdef AGP_DEBUG
579 printf("%s: trying to bind into stolen memory",
580 sc->as_dev.dv_xname);
581 #endif
582 return EINVAL;
583 }
584 }
585
586 WRITEGTT(offset, physical | 1);
587 return 0;
588 }
589
590 static int
591 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
592 {
593 struct agp_i810_softc *isc = sc->as_chipc;
594
595 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
596 return EINVAL;
597
598 if (isc->chiptype != CHIP_I810 ) {
599 if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
600 #ifdef AGP_DEBUG
601 printf("%s: trying to unbind from stolen memory",
602 sc->as_dev.dv_xname);
603 #endif
604 return EINVAL;
605 }
606 }
607
608 WRITEGTT(offset, 0);
609 return 0;
610 }
611
612 /*
613 * Writing via memory mapped registers already flushes all TLBs.
614 */
615 static void
616 agp_i810_flush_tlb(struct agp_softc *sc)
617 {
618 }
619
620 static int
621 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
622 {
623
624 return 0;
625 }
626
627 static struct agp_memory *
628 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
629 {
630 struct agp_i810_softc *isc = sc->as_chipc;
631 struct agp_memory *mem;
632
633 #ifdef AGP_DEBUG
634 printf("AGP: alloc(%d, 0x%x)\n", type, (int) size);
635 #endif
636
637 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
638 return 0;
639
640 if (sc->as_allocated + size > sc->as_maxmem)
641 return 0;
642
643 if (type == 1) {
644 /*
645 * Mapping local DRAM into GATT.
646 */
647 if (isc->chiptype != CHIP_I810 )
648 return 0;
649 if (size != isc->dcache_size)
650 return 0;
651 } else if (type == 2) {
652 /*
653 * Bogus mapping for the hardware cursor.
654 */
655 if (size != AGP_PAGE_SIZE && size != 4 * AGP_PAGE_SIZE)
656 return 0;
657 }
658
659 mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
660 if (mem == NULL)
661 return NULL;
662 mem->am_id = sc->as_nextid++;
663 mem->am_size = size;
664 mem->am_type = type;
665
666 if (type == 2) {
667 /*
668 * Allocate and wire down the memory now so that we can
669 * get its physical address.
670 */
671 mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
672 M_WAITOK);
673 if (mem->am_dmaseg == NULL) {
674 free(mem, M_AGP);
675 return NULL;
676 }
677 if (agp_alloc_dmamem(sc->as_dmat, size, 0,
678 &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
679 mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
680 free(mem->am_dmaseg, M_AGP);
681 free(mem, M_AGP);
682 return NULL;
683 }
684 memset(mem->am_virtual, 0, size);
685 } else if (type != 1) {
686 if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
687 size, 0, BUS_DMA_NOWAIT,
688 &mem->am_dmamap) != 0) {
689 free(mem, M_AGP);
690 return NULL;
691 }
692 }
693
694 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
695 sc->as_allocated += size;
696
697 return mem;
698 }
699
700 static int
701 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
702 {
703 if (mem->am_is_bound)
704 return EBUSY;
705
706 if (mem->am_type == 2) {
707 agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
708 mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
709 free(mem->am_dmaseg, M_AGP);
710 }
711
712 sc->as_allocated -= mem->am_size;
713 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
714 free(mem, M_AGP);
715 return 0;
716 }
717
718 static int
719 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
720 off_t offset)
721 {
722 struct agp_i810_softc *isc = sc->as_chipc;
723 u_int32_t regval, i;
724
725 /*
726 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
727 * X server for mysterious reasons which leads to crashes if we write
728 * to the GTT through the MMIO window.
729 * Until the issue is solved, simply restore it.
730 */
731
732 #if 0
733 regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
734 if (regval != (isc->gatt->ag_physical | 1)) {
735 printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
736 regval);
737 bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
738 isc->gatt->ag_physical | 1);
739 }
740 #endif
741 regval = 0;
742
743 if (mem->am_type == 2) {
744 WRITEGTT(offset, mem->am_physical | 1);
745 mem->am_offset = offset;
746 mem->am_is_bound = 1;
747 return 0;
748 }
749
750 if (mem->am_type != 1)
751 return agp_generic_bind_memory(sc, mem, offset);
752
753 if (isc->chiptype != CHIP_I810)
754 return EINVAL;
755
756 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
757 WRITEGTT(offset, i | 3);
758 mem->am_is_bound = 1;
759 return 0;
760 }
761
762 static int
763 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
764 {
765 struct agp_i810_softc *isc = sc->as_chipc;
766 u_int32_t i;
767
768 if (mem->am_type == 2) {
769 WRITEGTT(mem->am_offset, 0);
770 mem->am_offset = 0;
771 mem->am_is_bound = 0;
772 return 0;
773 }
774
775 if (mem->am_type != 1)
776 return agp_generic_unbind_memory(sc, mem);
777
778 if (isc->chiptype != CHIP_I810)
779 return EINVAL;
780
781 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
782 WRITEGTT(i, 0);
783 mem->am_is_bound = 0;
784 return 0;
785 }
786