agp.c revision 1.34.2.1 1 /* $NetBSD: agp.c,v 1.34.2.1 2006/02/14 20:39:35 riz Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/pci/agp.c,v 1.12 2001/05/19 01:28:07 alfred Exp $
29 */
30
31 /*
32 * Copyright (c) 2001 Wasabi Systems, Inc.
33 * All rights reserved.
34 *
35 * Written by Frank van der Linden for Wasabi Systems, Inc.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed for the NetBSD Project by
48 * Wasabi Systems, Inc.
49 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
50 * or promote products derived from this software without specific prior
51 * written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
64 */
65
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: agp.c,v 1.34.2.1 2006/02/14 20:39:35 riz Exp $");
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <sys/kernel.h>
74 #include <sys/device.h>
75 #include <sys/conf.h>
76 #include <sys/ioctl.h>
77 #include <sys/fcntl.h>
78 #include <sys/agpio.h>
79 #include <sys/proc.h>
80
81 #include <uvm/uvm_extern.h>
82
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcivar.h>
85 #include <dev/pci/agpvar.h>
86 #include <dev/pci/agpreg.h>
87 #include <dev/pci/pcidevs.h>
88
89 #include <machine/bus.h>
90
91 MALLOC_DEFINE(M_AGP, "AGP", "AGP memory");
92
93 /* Helper functions for implementing chipset mini drivers. */
94 /* XXXfvdl get rid of this one. */
95
96 extern struct cfdriver agp_cd;
97
98 dev_type_open(agpopen);
99 dev_type_close(agpclose);
100 dev_type_ioctl(agpioctl);
101 dev_type_mmap(agpmmap);
102
103 const struct cdevsw agp_cdevsw = {
104 agpopen, agpclose, noread, nowrite, agpioctl,
105 nostop, notty, nopoll, agpmmap, nokqfilter,
106 };
107
108 int agpmatch(struct device *, struct cfdata *, void *);
109 void agpattach(struct device *, struct device *, void *);
110
111 CFATTACH_DECL(agp, sizeof(struct agp_softc),
112 agpmatch, agpattach, NULL, NULL);
113
114 static int agp_info_user(struct agp_softc *, agp_info *);
115 static int agp_setup_user(struct agp_softc *, agp_setup *);
116 static int agp_allocate_user(struct agp_softc *, agp_allocate *);
117 static int agp_deallocate_user(struct agp_softc *, int);
118 static int agp_bind_user(struct agp_softc *, agp_bind *);
119 static int agp_unbind_user(struct agp_softc *, agp_unbind *);
120 static int agpdev_match(struct pci_attach_args *);
121
122 #include "agp_ali.h"
123 #include "agp_amd.h"
124 #include "agp_i810.h"
125 #include "agp_intel.h"
126 #include "agp_sis.h"
127 #include "agp_via.h"
128
129 const struct agp_product {
130 uint32_t ap_vendor;
131 uint32_t ap_product;
132 int (*ap_match)(const struct pci_attach_args *);
133 int (*ap_attach)(struct device *, struct device *, void *);
134 } agp_products[] = {
135 #if NAGP_ALI > 0
136 { PCI_VENDOR_ALI, -1,
137 NULL, agp_ali_attach },
138 #endif
139
140 #if NAGP_AMD > 0
141 { PCI_VENDOR_AMD, -1,
142 agp_amd_match, agp_amd_attach },
143 #endif
144
145 #if NAGP_I810 > 0
146 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_MCH,
147 NULL, agp_i810_attach },
148 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_DC100_MCH,
149 NULL, agp_i810_attach },
150 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810E_MCH,
151 NULL, agp_i810_attach },
152 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82815_FULL_HUB,
153 NULL, agp_i810_attach },
154 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82840_HB,
155 NULL, agp_i810_attach },
156 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82830MP_IO_1,
157 NULL, agp_i810_attach },
158 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82845G_DRAM,
159 NULL, agp_i810_attach },
160 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82855GM_MCH,
161 NULL, agp_i810_attach },
162 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82865_HB,
163 NULL, agp_i810_attach },
164 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82915G_HB,
165 NULL, agp_i810_attach },
166 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82915GM_HB,
167 NULL, agp_i810_attach },
168 #endif
169
170 #if NAGP_INTEL > 0
171 { PCI_VENDOR_INTEL, -1,
172 NULL, agp_intel_attach },
173 #endif
174
175 #if NAGP_SIS > 0
176 { PCI_VENDOR_SIS, -1,
177 NULL, agp_sis_attach },
178 #endif
179
180 #if NAGP_VIA > 0
181 { PCI_VENDOR_VIATECH, -1,
182 NULL, agp_via_attach },
183 #endif
184
185 { 0, 0,
186 NULL, NULL },
187 };
188
189 static const struct agp_product *
190 agp_lookup(const struct pci_attach_args *pa)
191 {
192 const struct agp_product *ap;
193
194 /* First find the vendor. */
195 for (ap = agp_products; ap->ap_attach != NULL; ap++) {
196 if (PCI_VENDOR(pa->pa_id) == ap->ap_vendor)
197 break;
198 }
199
200 if (ap->ap_attach == NULL)
201 return (NULL);
202
203 /* Now find the product within the vendor's domain. */
204 for (; ap->ap_attach != NULL; ap++) {
205 if (PCI_VENDOR(pa->pa_id) != ap->ap_vendor) {
206 /* Ran out of this vendor's section of the table. */
207 return (NULL);
208 }
209 if (ap->ap_product == PCI_PRODUCT(pa->pa_id)) {
210 /* Exact match. */
211 break;
212 }
213 if (ap->ap_product == (uint32_t) -1) {
214 /* Wildcard match. */
215 break;
216 }
217 }
218
219 if (ap->ap_attach == NULL)
220 return (NULL);
221
222 /* Now let the product-specific driver filter the match. */
223 if (ap->ap_match != NULL && (*ap->ap_match)(pa) == 0)
224 return (NULL);
225
226 return (ap);
227 }
228
229 int
230 agpmatch(struct device *parent, struct cfdata *match, void *aux)
231 {
232 struct agpbus_attach_args *apa = aux;
233 struct pci_attach_args *pa = &apa->apa_pci_args;
234
235 if (agp_lookup(pa) == NULL)
236 return (0);
237
238 return (1);
239 }
240
241 static int agp_max[][2] = {
242 {0, 0},
243 {32, 4},
244 {64, 28},
245 {128, 96},
246 {256, 204},
247 {512, 440},
248 {1024, 942},
249 {2048, 1920},
250 {4096, 3932}
251 };
252 #define agp_max_size (sizeof(agp_max) / sizeof(agp_max[0]))
253
254 void
255 agpattach(struct device *parent, struct device *self, void *aux)
256 {
257 struct agpbus_attach_args *apa = aux;
258 struct pci_attach_args *pa = &apa->apa_pci_args;
259 struct agp_softc *sc = (void *)self;
260 const struct agp_product *ap;
261 int memsize, i, ret;
262
263 ap = agp_lookup(pa);
264 if (ap == NULL) {
265 printf("\n");
266 panic("agpattach: impossible");
267 }
268
269 aprint_naive(": AGP controller\n");
270
271 sc->as_dmat = pa->pa_dmat;
272 sc->as_pc = pa->pa_pc;
273 sc->as_tag = pa->pa_tag;
274 sc->as_id = pa->pa_id;
275
276 /*
277 * Work out an upper bound for agp memory allocation. This
278 * uses a heurisitc table from the Linux driver.
279 */
280 memsize = ptoa(physmem) >> 20;
281 for (i = 0; i < agp_max_size; i++) {
282 if (memsize <= agp_max[i][0])
283 break;
284 }
285 if (i == agp_max_size)
286 i = agp_max_size - 1;
287 sc->as_maxmem = agp_max[i][1] << 20U;
288
289 /*
290 * The lock is used to prevent re-entry to
291 * agp_generic_bind_memory() since that function can sleep.
292 */
293 lockinit(&sc->as_lock, PZERO|PCATCH, "agplk", 0, 0);
294
295 TAILQ_INIT(&sc->as_memory);
296
297 ret = (*ap->ap_attach)(parent, self, pa);
298 if (ret == 0)
299 aprint_normal(": aperture at 0x%lx, size 0x%lx\n",
300 (unsigned long)sc->as_apaddr,
301 (unsigned long)AGP_GET_APERTURE(sc));
302 else
303 sc->as_chipc = NULL;
304 }
305
306 int
307 agp_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc, int reg)
308 {
309 /*
310 * Find the aperture. Don't map it (yet), this would
311 * eat KVA.
312 */
313 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
314 PCI_MAPREG_TYPE_MEM, &sc->as_apaddr, &sc->as_apsize,
315 &sc->as_apflags) != 0)
316 return ENXIO;
317
318 sc->as_apt = pa->pa_memt;
319
320 return 0;
321 }
322
323 struct agp_gatt *
324 agp_alloc_gatt(struct agp_softc *sc)
325 {
326 u_int32_t apsize = AGP_GET_APERTURE(sc);
327 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
328 struct agp_gatt *gatt;
329 int dummyseg;
330
331 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
332 if (!gatt)
333 return NULL;
334 gatt->ag_entries = entries;
335
336 if (agp_alloc_dmamem(sc->as_dmat, entries * sizeof(u_int32_t),
337 0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
338 &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0)
339 return NULL;
340
341 gatt->ag_size = entries * sizeof(u_int32_t);
342 memset(gatt->ag_virtual, 0, gatt->ag_size);
343 agp_flush_cache();
344
345 return gatt;
346 }
347
348 void
349 agp_free_gatt(struct agp_softc *sc, struct agp_gatt *gatt)
350 {
351 agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
352 (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
353 free(gatt, M_AGP);
354 }
355
356
357 int
358 agp_generic_detach(struct agp_softc *sc)
359 {
360 lockmgr(&sc->as_lock, LK_DRAIN, 0);
361 agp_flush_cache();
362 return 0;
363 }
364
365 static int
366 agpdev_match(struct pci_attach_args *pa)
367 {
368 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
369 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
370 if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP,
371 NULL, NULL))
372 return 1;
373
374 return 0;
375 }
376
377 int
378 agp_generic_enable(struct agp_softc *sc, u_int32_t mode)
379 {
380 struct pci_attach_args pa;
381 pcireg_t tstatus, mstatus;
382 pcireg_t command;
383 int rq, sba, fw, rate, capoff;
384
385 if (pci_find_device(&pa, agpdev_match) == 0 ||
386 pci_get_capability(pa.pa_pc, pa.pa_tag, PCI_CAP_AGP,
387 &capoff, NULL) == 0) {
388 printf("%s: can't find display\n", sc->as_dev.dv_xname);
389 return ENXIO;
390 }
391
392 tstatus = pci_conf_read(sc->as_pc, sc->as_tag,
393 sc->as_capoff + AGP_STATUS);
394 mstatus = pci_conf_read(pa.pa_pc, pa.pa_tag,
395 capoff + AGP_STATUS);
396
397 /* Set RQ to the min of mode, tstatus and mstatus */
398 rq = AGP_MODE_GET_RQ(mode);
399 if (AGP_MODE_GET_RQ(tstatus) < rq)
400 rq = AGP_MODE_GET_RQ(tstatus);
401 if (AGP_MODE_GET_RQ(mstatus) < rq)
402 rq = AGP_MODE_GET_RQ(mstatus);
403
404 /* Set SBA if all three can deal with SBA */
405 sba = (AGP_MODE_GET_SBA(tstatus)
406 & AGP_MODE_GET_SBA(mstatus)
407 & AGP_MODE_GET_SBA(mode));
408
409 /* Similar for FW */
410 fw = (AGP_MODE_GET_FW(tstatus)
411 & AGP_MODE_GET_FW(mstatus)
412 & AGP_MODE_GET_FW(mode));
413
414 /* Figure out the max rate */
415 rate = (AGP_MODE_GET_RATE(tstatus)
416 & AGP_MODE_GET_RATE(mstatus)
417 & AGP_MODE_GET_RATE(mode));
418 if (rate & AGP_MODE_RATE_4x)
419 rate = AGP_MODE_RATE_4x;
420 else if (rate & AGP_MODE_RATE_2x)
421 rate = AGP_MODE_RATE_2x;
422 else
423 rate = AGP_MODE_RATE_1x;
424
425 /* Construct the new mode word and tell the hardware */
426 command = AGP_MODE_SET_RQ(0, rq);
427 command = AGP_MODE_SET_SBA(command, sba);
428 command = AGP_MODE_SET_FW(command, fw);
429 command = AGP_MODE_SET_RATE(command, rate);
430 command = AGP_MODE_SET_AGP(command, 1);
431 pci_conf_write(sc->as_pc, sc->as_tag,
432 sc->as_capoff + AGP_COMMAND, command);
433 pci_conf_write(pa.pa_pc, pa.pa_tag, capoff + AGP_COMMAND, command);
434
435 return 0;
436 }
437
438 struct agp_memory *
439 agp_generic_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
440 {
441 struct agp_memory *mem;
442
443 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
444 return 0;
445
446 if (sc->as_allocated + size > sc->as_maxmem)
447 return 0;
448
449 if (type != 0) {
450 printf("agp_generic_alloc_memory: unsupported type %d\n",
451 type);
452 return 0;
453 }
454
455 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
456 if (mem == NULL)
457 return NULL;
458
459 if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
460 size, 0, BUS_DMA_NOWAIT, &mem->am_dmamap) != 0) {
461 free(mem, M_AGP);
462 return NULL;
463 }
464
465 mem->am_id = sc->as_nextid++;
466 mem->am_size = size;
467 mem->am_type = 0;
468 mem->am_physical = 0;
469 mem->am_offset = 0;
470 mem->am_is_bound = 0;
471 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
472 sc->as_allocated += size;
473
474 return mem;
475 }
476
477 int
478 agp_generic_free_memory(struct agp_softc *sc, struct agp_memory *mem)
479 {
480 if (mem->am_is_bound)
481 return EBUSY;
482
483 sc->as_allocated -= mem->am_size;
484 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
485 bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
486 free(mem, M_AGP);
487 return 0;
488 }
489
490 int
491 agp_generic_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
492 off_t offset)
493 {
494 off_t i, k;
495 bus_size_t done, j;
496 int error;
497 bus_dma_segment_t *segs, *seg;
498 bus_addr_t pa;
499 int contigpages, nseg;
500
501 lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
502
503 if (mem->am_is_bound) {
504 printf("%s: memory already bound\n", sc->as_dev.dv_xname);
505 lockmgr(&sc->as_lock, LK_RELEASE, 0);
506 return EINVAL;
507 }
508
509 if (offset < 0
510 || (offset & (AGP_PAGE_SIZE - 1)) != 0
511 || offset + mem->am_size > AGP_GET_APERTURE(sc)) {
512 printf("%s: binding memory at bad offset %#lx\n",
513 sc->as_dev.dv_xname, (unsigned long) offset);
514 lockmgr(&sc->as_lock, LK_RELEASE, 0);
515 return EINVAL;
516 }
517
518 /*
519 * XXXfvdl
520 * The memory here needs to be directly accessable from the
521 * AGP video card, so it should be allocated using bus_dma.
522 * However, it need not be contiguous, since individual pages
523 * are translated using the GATT.
524 *
525 * Using a large chunk of contiguous memory may get in the way
526 * of other subsystems that may need one, so we try to be friendly
527 * and ask for allocation in chunks of a minimum of 8 pages
528 * of contiguous memory on average, falling back to 4, 2 and 1
529 * if really needed. Larger chunks are preferred, since allocating
530 * a bus_dma_segment per page would be overkill.
531 */
532
533 for (contigpages = 8; contigpages > 0; contigpages >>= 1) {
534 nseg = (mem->am_size / (contigpages * PAGE_SIZE)) + 1;
535 segs = malloc(nseg * sizeof *segs, M_AGP, M_WAITOK);
536 if (segs == NULL) {
537 lockmgr(&sc->as_lock, LK_RELEASE, 0);
538 return ENOMEM;
539 }
540 if (bus_dmamem_alloc(sc->as_dmat, mem->am_size, PAGE_SIZE, 0,
541 segs, nseg, &mem->am_nseg,
542 contigpages > 1 ?
543 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) != 0) {
544 free(segs, M_AGP);
545 continue;
546 }
547 if (bus_dmamem_map(sc->as_dmat, segs, mem->am_nseg,
548 mem->am_size, &mem->am_virtual, BUS_DMA_WAITOK) != 0) {
549 bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
550 free(segs, M_AGP);
551 continue;
552 }
553 if (bus_dmamap_load(sc->as_dmat, mem->am_dmamap,
554 mem->am_virtual, mem->am_size, NULL, BUS_DMA_WAITOK) != 0) {
555 bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
556 mem->am_size);
557 bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
558 free(segs, M_AGP);
559 continue;
560 }
561 mem->am_dmaseg = segs;
562 break;
563 }
564
565 if (contigpages == 0) {
566 lockmgr(&sc->as_lock, LK_RELEASE, 0);
567 return ENOMEM;
568 }
569
570
571 /*
572 * Bind the individual pages and flush the chipset's
573 * TLB.
574 */
575 done = 0;
576 for (i = 0; i < mem->am_dmamap->dm_nsegs; i++) {
577 seg = &mem->am_dmamap->dm_segs[i];
578 /*
579 * Install entries in the GATT, making sure that if
580 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
581 * aligned to PAGE_SIZE, we don't modify too many GATT
582 * entries.
583 */
584 for (j = 0; j < seg->ds_len && (done + j) < mem->am_size;
585 j += AGP_PAGE_SIZE) {
586 pa = seg->ds_addr + j;
587 AGP_DPF("binding offset %#lx to pa %#lx\n",
588 (unsigned long)(offset + done + j),
589 (unsigned long)pa);
590 error = AGP_BIND_PAGE(sc, offset + done + j, pa);
591 if (error) {
592 /*
593 * Bail out. Reverse all the mappings
594 * and unwire the pages.
595 */
596 for (k = 0; k < done + j; k += AGP_PAGE_SIZE)
597 AGP_UNBIND_PAGE(sc, offset + k);
598
599 bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
600 bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
601 mem->am_size);
602 bus_dmamem_free(sc->as_dmat, mem->am_dmaseg,
603 mem->am_nseg);
604 free(mem->am_dmaseg, M_AGP);
605 lockmgr(&sc->as_lock, LK_RELEASE, 0);
606 return error;
607 }
608 }
609 done += seg->ds_len;
610 }
611
612 /*
613 * Flush the CPU cache since we are providing a new mapping
614 * for these pages.
615 */
616 agp_flush_cache();
617
618 /*
619 * Make sure the chipset gets the new mappings.
620 */
621 AGP_FLUSH_TLB(sc);
622
623 mem->am_offset = offset;
624 mem->am_is_bound = 1;
625
626 lockmgr(&sc->as_lock, LK_RELEASE, 0);
627
628 return 0;
629 }
630
631 int
632 agp_generic_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
633 {
634 int i;
635
636 lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
637
638 if (!mem->am_is_bound) {
639 printf("%s: memory is not bound\n", sc->as_dev.dv_xname);
640 lockmgr(&sc->as_lock, LK_RELEASE, 0);
641 return EINVAL;
642 }
643
644
645 /*
646 * Unbind the individual pages and flush the chipset's
647 * TLB. Unwire the pages so they can be swapped.
648 */
649 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
650 AGP_UNBIND_PAGE(sc, mem->am_offset + i);
651
652 agp_flush_cache();
653 AGP_FLUSH_TLB(sc);
654
655 bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
656 bus_dmamem_unmap(sc->as_dmat, mem->am_virtual, mem->am_size);
657 bus_dmamem_free(sc->as_dmat, mem->am_dmaseg, mem->am_nseg);
658
659 free(mem->am_dmaseg, M_AGP);
660
661 mem->am_offset = 0;
662 mem->am_is_bound = 0;
663
664 lockmgr(&sc->as_lock, LK_RELEASE, 0);
665
666 return 0;
667 }
668
669 /* Helper functions for implementing user/kernel api */
670
671 static int
672 agp_acquire_helper(struct agp_softc *sc, enum agp_acquire_state state)
673 {
674 if (sc->as_state != AGP_ACQUIRE_FREE)
675 return EBUSY;
676 sc->as_state = state;
677
678 return 0;
679 }
680
681 static int
682 agp_release_helper(struct agp_softc *sc, enum agp_acquire_state state)
683 {
684 struct agp_memory *mem;
685
686 if (sc->as_state == AGP_ACQUIRE_FREE)
687 return 0;
688
689 if (sc->as_state != state)
690 return EBUSY;
691
692 /*
693 * Clear out outstanding aperture mappings.
694 * (should not be necessary, done by caller)
695 */
696 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
697 if (mem->am_is_bound) {
698 printf("agp_release_helper: mem %d is bound\n",
699 mem->am_id);
700 AGP_UNBIND_MEMORY(sc, mem);
701 }
702 }
703
704 sc->as_state = AGP_ACQUIRE_FREE;
705 return 0;
706 }
707
708 static struct agp_memory *
709 agp_find_memory(struct agp_softc *sc, int id)
710 {
711 struct agp_memory *mem;
712
713 AGP_DPF("searching for memory block %d\n", id);
714 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
715 AGP_DPF("considering memory block %d\n", mem->am_id);
716 if (mem->am_id == id)
717 return mem;
718 }
719 return 0;
720 }
721
722 /* Implementation of the userland ioctl api */
723
724 static int
725 agp_info_user(struct agp_softc *sc, agp_info *info)
726 {
727 memset(info, 0, sizeof *info);
728 info->bridge_id = sc->as_id;
729 if (sc->as_capoff != 0)
730 info->agp_mode = pci_conf_read(sc->as_pc, sc->as_tag,
731 sc->as_capoff + AGP_STATUS);
732 else
733 info->agp_mode = 0; /* i810 doesn't have real AGP */
734 info->aper_base = sc->as_apaddr;
735 info->aper_size = AGP_GET_APERTURE(sc) >> 20;
736 info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
737 info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
738
739 return 0;
740 }
741
742 static int
743 agp_setup_user(struct agp_softc *sc, agp_setup *setup)
744 {
745 return AGP_ENABLE(sc, setup->agp_mode);
746 }
747
748 static int
749 agp_allocate_user(struct agp_softc *sc, agp_allocate *alloc)
750 {
751 struct agp_memory *mem;
752
753 mem = AGP_ALLOC_MEMORY(sc,
754 alloc->type,
755 alloc->pg_count << AGP_PAGE_SHIFT);
756 if (mem) {
757 alloc->key = mem->am_id;
758 alloc->physical = mem->am_physical;
759 return 0;
760 } else {
761 return ENOMEM;
762 }
763 }
764
765 static int
766 agp_deallocate_user(struct agp_softc *sc, int id)
767 {
768 struct agp_memory *mem = agp_find_memory(sc, id);
769
770 if (mem) {
771 AGP_FREE_MEMORY(sc, mem);
772 return 0;
773 } else {
774 return ENOENT;
775 }
776 }
777
778 static int
779 agp_bind_user(struct agp_softc *sc, agp_bind *bind)
780 {
781 struct agp_memory *mem = agp_find_memory(sc, bind->key);
782
783 if (!mem)
784 return ENOENT;
785
786 return AGP_BIND_MEMORY(sc, mem, bind->pg_start << AGP_PAGE_SHIFT);
787 }
788
789 static int
790 agp_unbind_user(struct agp_softc *sc, agp_unbind *unbind)
791 {
792 struct agp_memory *mem = agp_find_memory(sc, unbind->key);
793
794 if (!mem)
795 return ENOENT;
796
797 return AGP_UNBIND_MEMORY(sc, mem);
798 }
799
800 int
801 agpopen(dev_t dev, int oflags, int devtype, struct proc *p)
802 {
803 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
804
805 if (sc == NULL)
806 return ENXIO;
807
808 if (sc->as_chipc == NULL)
809 return ENXIO;
810
811 if (!sc->as_isopen)
812 sc->as_isopen = 1;
813 else
814 return EBUSY;
815
816 return 0;
817 }
818
819 int
820 agpclose(dev_t dev, int fflag, int devtype, struct proc *p)
821 {
822 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
823 struct agp_memory *mem;
824
825 /*
826 * Clear the GATT and force release on last close
827 */
828 if (sc->as_state == AGP_ACQUIRE_USER) {
829 while ((mem = TAILQ_FIRST(&sc->as_memory))) {
830 if (mem->am_is_bound) {
831 printf("agpclose: mem %d is bound\n",
832 mem->am_id);
833 AGP_UNBIND_MEMORY(sc, mem);
834 }
835 /*
836 * XXX it is not documented, but if the protocol allows
837 * allocate->acquire->bind, it would be possible that
838 * memory ranges are allocated by the kernel here,
839 * which we shouldn't free. We'd have to keep track of
840 * the memory range's owner.
841 * The kernel API is unsed yet, so we get away with
842 * freeing all.
843 */
844 AGP_FREE_MEMORY(sc, mem);
845 }
846 agp_release_helper(sc, AGP_ACQUIRE_USER);
847 }
848 sc->as_isopen = 0;
849
850 return 0;
851 }
852
853 int
854 agpioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
855 {
856 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
857
858 if (sc == NULL)
859 return ENODEV;
860
861 if ((fflag & FWRITE) == 0 && cmd != AGPIOC_INFO)
862 return EPERM;
863
864 switch (cmd) {
865 case AGPIOC_INFO:
866 return agp_info_user(sc, (agp_info *) data);
867
868 case AGPIOC_ACQUIRE:
869 return agp_acquire_helper(sc, AGP_ACQUIRE_USER);
870
871 case AGPIOC_RELEASE:
872 return agp_release_helper(sc, AGP_ACQUIRE_USER);
873
874 case AGPIOC_SETUP:
875 return agp_setup_user(sc, (agp_setup *)data);
876
877 case AGPIOC_ALLOCATE:
878 return agp_allocate_user(sc, (agp_allocate *)data);
879
880 case AGPIOC_DEALLOCATE:
881 return agp_deallocate_user(sc, *(int *) data);
882
883 case AGPIOC_BIND:
884 return agp_bind_user(sc, (agp_bind *)data);
885
886 case AGPIOC_UNBIND:
887 return agp_unbind_user(sc, (agp_unbind *)data);
888
889 }
890
891 return EINVAL;
892 }
893
894 paddr_t
895 agpmmap(dev_t dev, off_t offset, int prot)
896 {
897 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
898
899 if (offset > AGP_GET_APERTURE(sc))
900 return -1;
901
902 return (bus_space_mmap(sc->as_apt, sc->as_apaddr, offset, prot,
903 BUS_SPACE_MAP_LINEAR));
904 }
905
906 /* Implementation of the kernel api */
907
908 void *
909 agp_find_device(int unit)
910 {
911 return device_lookup(&agp_cd, unit);
912 }
913
914 enum agp_acquire_state
915 agp_state(void *devcookie)
916 {
917 struct agp_softc *sc = devcookie;
918 return sc->as_state;
919 }
920
921 void
922 agp_get_info(void *devcookie, struct agp_info *info)
923 {
924 struct agp_softc *sc = devcookie;
925
926 info->ai_mode = pci_conf_read(sc->as_pc, sc->as_tag,
927 sc->as_capoff + AGP_STATUS);
928 info->ai_aperture_base = sc->as_apaddr;
929 info->ai_aperture_size = sc->as_apsize; /* XXXfvdl inconsistent */
930 info->ai_memory_allowed = sc->as_maxmem;
931 info->ai_memory_used = sc->as_allocated;
932 }
933
934 int
935 agp_acquire(void *dev)
936 {
937 return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
938 }
939
940 int
941 agp_release(void *dev)
942 {
943 return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
944 }
945
946 int
947 agp_enable(void *dev, u_int32_t mode)
948 {
949 struct agp_softc *sc = dev;
950
951 return AGP_ENABLE(sc, mode);
952 }
953
954 void *agp_alloc_memory(void *dev, int type, vsize_t bytes)
955 {
956 struct agp_softc *sc = dev;
957
958 return (void *)AGP_ALLOC_MEMORY(sc, type, bytes);
959 }
960
961 void agp_free_memory(void *dev, void *handle)
962 {
963 struct agp_softc *sc = dev;
964 struct agp_memory *mem = (struct agp_memory *) handle;
965 AGP_FREE_MEMORY(sc, mem);
966 }
967
968 int agp_bind_memory(void *dev, void *handle, off_t offset)
969 {
970 struct agp_softc *sc = dev;
971 struct agp_memory *mem = (struct agp_memory *) handle;
972
973 return AGP_BIND_MEMORY(sc, mem, offset);
974 }
975
976 int agp_unbind_memory(void *dev, void *handle)
977 {
978 struct agp_softc *sc = dev;
979 struct agp_memory *mem = (struct agp_memory *) handle;
980
981 return AGP_UNBIND_MEMORY(sc, mem);
982 }
983
984 void agp_memory_info(void *dev, void *handle, struct agp_memory_info *mi)
985 {
986 struct agp_memory *mem = (struct agp_memory *) handle;
987
988 mi->ami_size = mem->am_size;
989 mi->ami_physical = mem->am_physical;
990 mi->ami_offset = mem->am_offset;
991 mi->ami_is_bound = mem->am_is_bound;
992 }
993
994 int
995 agp_alloc_dmamem(bus_dma_tag_t tag, size_t size, int flags,
996 bus_dmamap_t *mapp, caddr_t *vaddr, bus_addr_t *baddr,
997 bus_dma_segment_t *seg, int nseg, int *rseg)
998
999 {
1000 int error, level = 0;
1001
1002 if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
1003 seg, nseg, rseg, BUS_DMA_NOWAIT)) != 0)
1004 goto out;
1005 level++;
1006
1007 if ((error = bus_dmamem_map(tag, seg, *rseg, size, vaddr,
1008 BUS_DMA_NOWAIT | flags)) != 0)
1009 goto out;
1010 level++;
1011
1012 if ((error = bus_dmamap_create(tag, size, *rseg, size, 0,
1013 BUS_DMA_NOWAIT, mapp)) != 0)
1014 goto out;
1015 level++;
1016
1017 if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
1018 BUS_DMA_NOWAIT)) != 0)
1019 goto out;
1020
1021 *baddr = (*mapp)->dm_segs[0].ds_addr;
1022
1023 return 0;
1024 out:
1025 switch (level) {
1026 case 3:
1027 bus_dmamap_destroy(tag, *mapp);
1028 /* FALLTHROUGH */
1029 case 2:
1030 bus_dmamem_unmap(tag, *vaddr, size);
1031 /* FALLTHROUGH */
1032 case 1:
1033 bus_dmamem_free(tag, seg, *rseg);
1034 break;
1035 default:
1036 break;
1037 }
1038
1039 return error;
1040 }
1041
1042 void
1043 agp_free_dmamem(bus_dma_tag_t tag, size_t size, bus_dmamap_t map,
1044 caddr_t vaddr, bus_dma_segment_t *seg, int nseg)
1045 {
1046
1047 bus_dmamap_unload(tag, map);
1048 bus_dmamap_destroy(tag, map);
1049 bus_dmamem_unmap(tag, vaddr, size);
1050 bus_dmamem_free(tag, seg, nseg);
1051 }
1052