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