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