agp.c revision 1.7 1 /* $NetBSD: agp.c,v 1.7 2001/09/15 01:32:10 thorpej 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
78 #include <uvm/uvm_extern.h>
79
80 #include <dev/pci/pcireg.h>
81 #include <dev/pci/pcivar.h>
82 #include <dev/pci/agpvar.h>
83 #include <dev/pci/agpreg.h>
84 #include <dev/pci/pcidevs.h>
85
86 #include <machine/bus.h>
87
88 /* Helper functions for implementing chipset mini drivers. */
89 /* XXXfvdl get rid of this one. */
90
91 extern struct cfdriver agp_cd;
92 cdev_decl(agp);
93
94 int agpmatch(struct device *, struct cfdata *, void *);
95 void agpattach(struct device *, struct device *, void *);
96
97 struct cfattach agp_ca = {
98 sizeof(struct agp_softc), agpmatch, agpattach
99 };
100
101 static int agp_info_user(struct agp_softc *, agp_info *);
102 static int agp_setup_user(struct agp_softc *, agp_setup *);
103 static int agp_allocate_user(struct agp_softc *, agp_allocate *);
104 static int agp_deallocate_user(struct agp_softc *, int);
105 static int agp_bind_user(struct agp_softc *, agp_bind *);
106 static int agp_unbind_user(struct agp_softc *, agp_unbind *);
107 static int agpdev_match(struct pci_attach_args *);
108
109 #include "agp_ali.h"
110 #include "agp_amd.h"
111 #include "agp_i810.h"
112 #include "agp_intel.h"
113 #include "agp_sis.h"
114 #include "agp_via.h"
115
116 const struct agp_product {
117 uint32_t ap_vendor;
118 uint32_t ap_product;
119 int (*ap_match)(const struct pci_attach_args *);
120 int (*ap_attach)(struct device *, struct device *, void *);
121 } agp_products[] = {
122 #if NAGP_ALI > 0
123 { PCI_VENDOR_ALI, -1,
124 NULL, agp_ali_attach },
125 #endif
126
127 #if NAGP_AMD > 0
128 { PCI_VENDOR_AMD, -1,
129 agp_amd_match, agp_amd_attach },
130 #endif
131
132 #if NAGP_I810 > 0
133 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_MCH,
134 NULL, agp_i810_attach },
135 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_DC100_MCH,
136 NULL, agp_i810_attach },
137 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810E_MCH,
138 NULL, agp_i810_attach },
139 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82815_FULL_HUB,
140 NULL, agp_i810_attach },
141 #endif
142
143 #if NAGP_INTEL > 0
144 { PCI_VENDOR_INTEL, -1,
145 NULL, agp_intel_attach },
146 #endif
147
148 #if NAGP_SIS > 0
149 { PCI_VENDOR_SIS, -1,
150 NULL, agp_sis_attach },
151 #endif
152
153 #if NAGP_VIA > 0
154 { PCI_VENDOR_VIATECH, -1,
155 NULL, agp_via_attach },
156 #endif
157
158 { 0, 0,
159 NULL, NULL },
160 };
161
162 static const struct agp_product *
163 agp_lookup(const struct pci_attach_args *pa)
164 {
165 const struct agp_product *ap;
166
167 /* First find the vendor. */
168 for (ap = agp_products; ap->ap_attach != NULL; ap++) {
169 if (PCI_VENDOR(pa->pa_id) == ap->ap_vendor)
170 break;
171 }
172
173 if (ap->ap_attach == NULL)
174 return (NULL);
175
176 /* Now find the product within the vendor's domain. */
177 for (; ap->ap_attach != NULL; ap++) {
178 if (PCI_VENDOR(pa->pa_id) != ap->ap_vendor) {
179 /* Ran out of this vendor's section of the table. */
180 return (NULL);
181 }
182 if (ap->ap_product == PCI_PRODUCT(pa->pa_id)) {
183 /* Exact match. */
184 break;
185 }
186 if (ap->ap_product == (uint32_t) -1) {
187 /* Wildcard match. */
188 break;
189 }
190 }
191
192 if (ap->ap_attach == NULL)
193 return (NULL);
194
195 /* Now let the product-specific driver filter the match. */
196 if (ap->ap_match != NULL && (*ap->ap_match)(pa) == 0)
197 return (NULL);
198
199 return (ap);
200 }
201
202 int
203 agpmatch(struct device *parent, struct cfdata *match, void *aux)
204 {
205 struct agpbus_attach_args *apa = aux;
206 struct pci_attach_args *pa = &apa->apa_pci_args;
207
208 if (strcmp(apa->apa_busname, "agp") != 0)
209 return (0);
210
211 if (agp_lookup(pa) == NULL)
212 return (0);
213
214 return (1);
215 }
216
217 static int agp_max[][2] = {
218 {0, 0},
219 {32, 4},
220 {64, 28},
221 {128, 96},
222 {256, 204},
223 {512, 440},
224 {1024, 942},
225 {2048, 1920},
226 {4096, 3932}
227 };
228 #define agp_max_size (sizeof(agp_max) / sizeof(agp_max[0]))
229
230 void
231 agpattach(struct device *parent, struct device *self, void *aux)
232 {
233 struct agpbus_attach_args *apa = aux;
234 struct pci_attach_args *pa = &apa->apa_pci_args;
235 struct agp_softc *sc = (void *)self;
236 const struct agp_product *ap;
237 int memsize, i, ret;
238
239 ap = agp_lookup(pa);
240 if (ap == NULL) {
241 printf("\n");
242 panic("agpattach: impossible");
243 }
244
245 sc->as_dmat = pa->pa_dmat;
246 sc->as_pc = pa->pa_pc;
247 sc->as_tag = pa->pa_tag;
248 sc->as_id = pa->pa_id;
249
250 /*
251 * Work out an upper bound for agp memory allocation. This
252 * uses a heurisitc table from the Linux driver.
253 */
254 memsize = ptoa(physmem) >> 20;
255 for (i = 0; i < agp_max_size; i++) {
256 if (memsize <= agp_max[i][0])
257 break;
258 }
259 if (i == agp_max_size)
260 i = agp_max_size - 1;
261 sc->as_maxmem = agp_max[i][1] << 20U;
262
263 /*
264 * The lock is used to prevent re-entry to
265 * agp_generic_bind_memory() since that function can sleep.
266 */
267 lockinit(&sc->as_lock, PZERO|PCATCH, "agplk", 0, 0);
268
269 TAILQ_INIT(&sc->as_memory);
270
271 ret = (*ap->ap_attach)(parent, self, pa);
272 if (ret == 0)
273 printf(": aperture at 0x%lx, size 0x%lx\n",
274 (unsigned long)sc->as_apaddr,
275 (unsigned long)AGP_GET_APERTURE(sc));
276 else
277 sc->as_chipc = NULL;
278 }
279 int
280 agp_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc)
281 {
282 /*
283 * Find and map the aperture.
284 */
285 if (pci_mapreg_map(pa, AGP_APBASE, PCI_MAPREG_TYPE_MEM,
286 BUS_SPACE_MAP_LINEAR,
287 &sc->as_apt, &sc->as_aph, &sc->as_apaddr, &sc->as_apsize) != 0) {
288 printf("%s: can't map aperture space\n", sc->as_dev.dv_xname);
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 NULL;
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(dev_t dev, int oflags, int devtype, struct proc *p)
767 {
768 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
769
770 if (sc->as_chipc == NULL)
771 return ENXIO;
772
773 if (!sc->as_isopen)
774 sc->as_isopen = 1;
775 else
776 return EBUSY;
777
778 return 0;
779 }
780
781 int
782 agpclose(dev_t dev, int fflag, int devtype, struct proc *p)
783 {
784 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
785
786 /*
787 * Clear the GATT and force release on last close
788 */
789 if (sc->as_state == AGP_ACQUIRE_USER)
790 agp_release_helper(sc, AGP_ACQUIRE_USER);
791 sc->as_isopen = 0;
792
793 return 0;
794 }
795
796 int
797 agpioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
798 {
799 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
800
801 if (sc == NULL)
802 return ENODEV;
803
804 if ((fflag & FWRITE) == 0 && cmd != AGPIOC_INFO)
805 return EPERM;
806
807 switch (cmd) {
808 case AGPIOC_INFO:
809 return agp_info_user(sc, (agp_info *) data);
810
811 case AGPIOC_ACQUIRE:
812 return agp_acquire_helper(sc, AGP_ACQUIRE_USER);
813
814 case AGPIOC_RELEASE:
815 return agp_release_helper(sc, AGP_ACQUIRE_USER);
816
817 case AGPIOC_SETUP:
818 return agp_setup_user(sc, (agp_setup *)data);
819
820 case AGPIOC_ALLOCATE:
821 return agp_allocate_user(sc, (agp_allocate *)data);
822
823 case AGPIOC_DEALLOCATE:
824 return agp_deallocate_user(sc, *(int *) data);
825
826 case AGPIOC_BIND:
827 return agp_bind_user(sc, (agp_bind *)data);
828
829 case AGPIOC_UNBIND:
830 return agp_unbind_user(sc, (agp_unbind *)data);
831
832 }
833
834 return EINVAL;
835 }
836
837 paddr_t
838 agpmmap(dev_t dev, off_t offset, int prot)
839 {
840 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
841
842 if (offset > AGP_GET_APERTURE(sc))
843 return -1;
844
845 return (bus_space_mmap(sc->as_apt, sc->as_apaddr, offset, prot,
846 BUS_SPACE_MAP_LINEAR));
847 }
848
849 /* Implementation of the kernel api */
850
851 void *
852 agp_find_device(int unit)
853 {
854 return device_lookup(&agp_cd, unit);
855 }
856
857 enum agp_acquire_state
858 agp_state(void *devcookie)
859 {
860 struct agp_softc *sc = devcookie;
861 return sc->as_state;
862 }
863
864 void
865 agp_get_info(void *devcookie, struct agp_info *info)
866 {
867 struct agp_softc *sc = devcookie;
868
869 info->ai_mode = pci_conf_read(sc->as_pc, sc->as_tag,
870 sc->as_capoff + AGP_STATUS);
871 info->ai_aperture_base = sc->as_apaddr;
872 info->ai_aperture_size = sc->as_apsize; /* XXXfvdl inconsistent */
873 info->ai_aperture_vaddr = bus_space_vaddr(sc->as_apt, sc->as_aph);
874 info->ai_memory_allowed = sc->as_maxmem;
875 info->ai_memory_used = sc->as_allocated;
876 }
877
878 int
879 agp_acquire(void *dev)
880 {
881 return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
882 }
883
884 int
885 agp_release(void *dev)
886 {
887 return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
888 }
889
890 int
891 agp_enable(void *dev, u_int32_t mode)
892 {
893 struct agp_softc *sc = dev;
894
895 return AGP_ENABLE(sc, mode);
896 }
897
898 void *agp_alloc_memory(void *dev, int type, vsize_t bytes)
899 {
900 struct agp_softc *sc = dev;
901
902 return (void *)AGP_ALLOC_MEMORY(sc, type, bytes);
903 }
904
905 void agp_free_memory(void *dev, void *handle)
906 {
907 struct agp_softc *sc = dev;
908 struct agp_memory *mem = (struct agp_memory *) handle;
909 AGP_FREE_MEMORY(sc, mem);
910 }
911
912 int agp_bind_memory(void *dev, void *handle, off_t offset)
913 {
914 struct agp_softc *sc = dev;
915 struct agp_memory *mem = (struct agp_memory *) handle;
916
917 return AGP_BIND_MEMORY(sc, mem, offset);
918 }
919
920 int agp_unbind_memory(void *dev, void *handle)
921 {
922 struct agp_softc *sc = dev;
923 struct agp_memory *mem = (struct agp_memory *) handle;
924
925 return AGP_UNBIND_MEMORY(sc, mem);
926 }
927
928 void agp_memory_info(void *dev, void *handle, struct agp_memory_info *mi)
929 {
930 struct agp_memory *mem = (struct agp_memory *) handle;
931
932 mi->ami_size = mem->am_size;
933 mi->ami_physical = mem->am_physical;
934 mi->ami_offset = mem->am_offset;
935 mi->ami_is_bound = mem->am_is_bound;
936 }
937
938 int
939 agp_alloc_dmamem(bus_dma_tag_t tag, size_t size, int flags,
940 bus_dmamap_t *mapp, caddr_t *vaddr, bus_addr_t *baddr,
941 bus_dma_segment_t *seg, int nseg, int *rseg)
942
943 {
944 int error, level = 0;
945
946 if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
947 seg, nseg, rseg, BUS_DMA_NOWAIT)) != 0)
948 goto out;
949 level++;
950
951 if ((error = bus_dmamem_map(tag, seg, *rseg, size, vaddr,
952 BUS_DMA_NOWAIT | flags)) != 0)
953 goto out;
954 level++;
955
956 if ((error = bus_dmamap_create(tag, size, *rseg, size, 0,
957 BUS_DMA_NOWAIT, mapp)) != 0)
958 goto out;
959 level++;
960
961 if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
962 BUS_DMA_NOWAIT)) != 0)
963 goto out;
964
965 *baddr = (*mapp)->dm_segs[0].ds_addr;
966
967 return 0;
968 out:
969 switch (level) {
970 case 3:
971 bus_dmamap_destroy(tag, *mapp);
972 /* FALLTHROUGH */
973 case 2:
974 bus_dmamem_unmap(tag, *vaddr, size);
975 /* FALLTHROUGH */
976 case 1:
977 bus_dmamem_free(tag, seg, *rseg);
978 break;
979 default:
980 break;
981 }
982
983 return error;
984 }
985
986 void
987 agp_free_dmamem(bus_dma_tag_t tag, size_t size, bus_dmamap_t map,
988 caddr_t vaddr, bus_dma_segment_t *seg, int nseg)
989 {
990
991 bus_dmamap_unload(tag, map);
992 bus_dmamap_destroy(tag, map);
993 bus_dmamem_unmap(tag, vaddr, size);
994 bus_dmamem_free(tag, seg, nseg);
995 }
996