agp.c revision 1.10 1 /* $NetBSD: agp.c,v 1.10 2001/09/16 18:33:08 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 return ENXIO;
289
290 return 0;
291 }
292
293 struct agp_gatt *
294 agp_alloc_gatt(struct agp_softc *sc)
295 {
296 u_int32_t apsize = AGP_GET_APERTURE(sc);
297 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
298 struct agp_gatt *gatt;
299 int dummyseg;
300
301 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
302 if (!gatt)
303 return NULL;
304 gatt->ag_entries = entries;
305
306 if (agp_alloc_dmamem(sc->as_dmat, entries * sizeof(u_int32_t),
307 0, &gatt->ag_dmamap, (caddr_t *)&gatt->ag_virtual,
308 &gatt->ag_physical, &gatt->ag_dmaseg, 1, &dummyseg) != 0)
309 return NULL;
310
311 gatt->ag_size = entries * sizeof(u_int32_t);
312 memset(gatt->ag_virtual, 0, gatt->ag_size);
313 agp_flush_cache();
314
315 return gatt;
316 }
317
318 void
319 agp_free_gatt(struct agp_softc *sc, struct agp_gatt *gatt)
320 {
321 agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
322 (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
323 free(gatt, M_AGP);
324 }
325
326
327 int
328 agp_generic_detach(struct agp_softc *sc)
329 {
330 lockmgr(&sc->as_lock, LK_DRAIN, 0);
331 agp_flush_cache();
332 return 0;
333 }
334
335 static int
336 agpdev_match(struct pci_attach_args *pa)
337 {
338 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
339 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
340 return 1;
341
342 return 0;
343 }
344
345 int
346 agp_generic_enable(struct agp_softc *sc, u_int32_t mode)
347 {
348 struct pci_attach_args pa;
349 pcireg_t tstatus, mstatus;
350 pcireg_t command;
351 int rq, sba, fw, rate, capoff;
352
353 if (pci_find_device(&pa, agpdev_match) == 0 ||
354 pci_get_capability(pa.pa_pc, pa.pa_tag, PCI_CAP_AGP,
355 &capoff, NULL) == 0) {
356 printf("%s: can't find display\n", sc->as_dev.dv_xname);
357 return ENXIO;
358 }
359
360 tstatus = pci_conf_read(sc->as_pc, sc->as_tag,
361 sc->as_capoff + AGP_STATUS);
362 mstatus = pci_conf_read(pa.pa_pc, pa.pa_tag,
363 capoff + AGP_STATUS);
364
365 /* Set RQ to the min of mode, tstatus and mstatus */
366 rq = AGP_MODE_GET_RQ(mode);
367 if (AGP_MODE_GET_RQ(tstatus) < rq)
368 rq = AGP_MODE_GET_RQ(tstatus);
369 if (AGP_MODE_GET_RQ(mstatus) < rq)
370 rq = AGP_MODE_GET_RQ(mstatus);
371
372 /* Set SBA if all three can deal with SBA */
373 sba = (AGP_MODE_GET_SBA(tstatus)
374 & AGP_MODE_GET_SBA(mstatus)
375 & AGP_MODE_GET_SBA(mode));
376
377 /* Similar for FW */
378 fw = (AGP_MODE_GET_FW(tstatus)
379 & AGP_MODE_GET_FW(mstatus)
380 & AGP_MODE_GET_FW(mode));
381
382 /* Figure out the max rate */
383 rate = (AGP_MODE_GET_RATE(tstatus)
384 & AGP_MODE_GET_RATE(mstatus)
385 & AGP_MODE_GET_RATE(mode));
386 if (rate & AGP_MODE_RATE_4x)
387 rate = AGP_MODE_RATE_4x;
388 else if (rate & AGP_MODE_RATE_2x)
389 rate = AGP_MODE_RATE_2x;
390 else
391 rate = AGP_MODE_RATE_1x;
392
393 /* Construct the new mode word and tell the hardware */
394 command = AGP_MODE_SET_RQ(0, rq);
395 command = AGP_MODE_SET_SBA(command, sba);
396 command = AGP_MODE_SET_FW(command, fw);
397 command = AGP_MODE_SET_RATE(command, rate);
398 command = AGP_MODE_SET_AGP(command, 1);
399 pci_conf_write(sc->as_pc, sc->as_tag,
400 sc->as_capoff + AGP_COMMAND, command);
401 pci_conf_write(pa.pa_pc, pa.pa_tag, capoff + AGP_COMMAND, command);
402
403 return 0;
404 }
405
406 struct agp_memory *
407 agp_generic_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
408 {
409 struct agp_memory *mem;
410
411 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
412 return 0;
413
414 if (sc->as_allocated + size > sc->as_maxmem)
415 return 0;
416
417 if (type != 0) {
418 printf("agp_generic_alloc_memory: unsupported type %d\n",
419 type);
420 return 0;
421 }
422
423 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
424 if (mem == NULL)
425 return NULL;
426
427 if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
428 size, 0, BUS_DMA_NOWAIT, &mem->am_dmamap) != 0) {
429 free(mem, M_AGP);
430 return NULL;
431 }
432
433 mem->am_id = sc->as_nextid++;
434 mem->am_size = size;
435 mem->am_type = 0;
436 mem->am_physical = 0;
437 mem->am_offset = 0;
438 mem->am_is_bound = 0;
439 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
440 sc->as_allocated += size;
441
442 return mem;
443 }
444
445 int
446 agp_generic_free_memory(struct agp_softc *sc, struct agp_memory *mem)
447 {
448 if (mem->am_is_bound)
449 return EBUSY;
450
451 sc->as_allocated -= mem->am_size;
452 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
453 bus_dmamap_destroy(sc->as_dmat, mem->am_dmamap);
454 free(mem, M_AGP);
455 return 0;
456 }
457
458 int
459 agp_generic_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
460 off_t offset)
461 {
462 off_t i, k;
463 bus_size_t done, j;
464 int error;
465 bus_dma_segment_t *segs, *seg;
466 bus_addr_t pa;
467 int contigpages, nseg;
468
469 lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
470
471 if (mem->am_is_bound) {
472 printf("%s: memory already bound\n", sc->as_dev.dv_xname);
473 lockmgr(&sc->as_lock, LK_RELEASE, 0);
474 return EINVAL;
475 }
476
477 if (offset < 0
478 || (offset & (AGP_PAGE_SIZE - 1)) != 0
479 || offset + mem->am_size > AGP_GET_APERTURE(sc)) {
480 printf("%s: binding memory at bad offset %#lx\n",
481 sc->as_dev.dv_xname, (unsigned long) offset);
482 lockmgr(&sc->as_lock, LK_RELEASE, 0);
483 return EINVAL;
484 }
485
486 /*
487 * XXXfvdl
488 * The memory here needs to be directly accessable from the
489 * AGP video card, so it should be allocated using bus_dma.
490 * However, it need not be contiguous, since individual pages
491 * are translated using the GATT.
492 *
493 * Using a large chunk of contiguous memory may get in the way
494 * of other subsystems that may need one, so we try to be friendly
495 * and ask for allocation in chunks of a minimum of 8 pages
496 * of contiguous memory on average, falling back to 4, 2 and 1
497 * if really needed. Larger chunks are preferred, since allocating
498 * a bus_dma_segment per page would be overkill.
499 */
500
501 for (contigpages = 8; contigpages > 0; contigpages >>= 1) {
502 nseg = (mem->am_size / (contigpages * PAGE_SIZE)) + 1;
503 segs = malloc(nseg * sizeof *segs, M_AGP, M_WAITOK);
504 if (segs == NULL)
505 return ENOMEM;
506 if (bus_dmamem_alloc(sc->as_dmat, mem->am_size, PAGE_SIZE, 0,
507 segs, nseg, &mem->am_nseg,
508 BUS_DMA_WAITOK) != 0) {
509 free(segs, M_AGP);
510 continue;
511 }
512 if (bus_dmamem_map(sc->as_dmat, segs, mem->am_nseg,
513 mem->am_size, &mem->am_virtual, BUS_DMA_WAITOK) != 0) {
514 bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
515 free(segs, M_AGP);
516 continue;
517 }
518 if (bus_dmamap_load(sc->as_dmat, mem->am_dmamap,
519 mem->am_virtual, mem->am_size, NULL, BUS_DMA_WAITOK) != 0) {
520 bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
521 mem->am_size);
522 bus_dmamem_free(sc->as_dmat, segs, mem->am_nseg);
523 free(segs, M_AGP);
524 continue;
525 }
526 mem->am_dmaseg = segs;
527 break;
528 }
529
530 if (contigpages == 0) {
531 lockmgr(&sc->as_lock, LK_RELEASE, 0);
532 return ENOMEM;
533 }
534
535
536 /*
537 * Bind the individual pages and flush the chipset's
538 * TLB.
539 */
540 done = 0;
541 for (i = 0; i < mem->am_dmamap->dm_nsegs; i++) {
542 seg = &mem->am_dmamap->dm_segs[i];
543 /*
544 * Install entries in the GATT, making sure that if
545 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
546 * aligned to PAGE_SIZE, we don't modify too many GATT
547 * entries.
548 */
549 for (j = 0; j < seg->ds_len && (done + j) < mem->am_size;
550 j += AGP_PAGE_SIZE) {
551 pa = seg->ds_addr + j;
552 AGP_DPF("binding offset %#lx to pa %#lx\n",
553 (unsigned long)(offset + done + j),
554 (unsigned long)pa);
555 error = AGP_BIND_PAGE(sc, offset + done + j, pa);
556 if (error) {
557 /*
558 * Bail out. Reverse all the mappings
559 * and unwire the pages.
560 */
561 for (k = 0; k < done + j; k += AGP_PAGE_SIZE)
562 AGP_UNBIND_PAGE(sc, offset + k);
563
564 bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
565 bus_dmamem_unmap(sc->as_dmat, mem->am_virtual,
566 mem->am_size);
567 bus_dmamem_free(sc->as_dmat, mem->am_dmaseg,
568 mem->am_nseg);
569 free(mem->am_dmaseg, M_AGP);
570 lockmgr(&sc->as_lock, LK_RELEASE, 0);
571 return error;
572 }
573 }
574 done += seg->ds_len;
575 }
576
577 /*
578 * Flush the cpu cache since we are providing a new mapping
579 * for these pages.
580 */
581 agp_flush_cache();
582
583 /*
584 * Make sure the chipset gets the new mappings.
585 */
586 AGP_FLUSH_TLB(sc);
587
588 mem->am_offset = offset;
589 mem->am_is_bound = 1;
590
591 lockmgr(&sc->as_lock, LK_RELEASE, 0);
592
593 return 0;
594 }
595
596 int
597 agp_generic_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
598 {
599 int i;
600
601 lockmgr(&sc->as_lock, LK_EXCLUSIVE, 0);
602
603 if (!mem->am_is_bound) {
604 printf("%s: memory is not bound\n", sc->as_dev.dv_xname);
605 lockmgr(&sc->as_lock, LK_RELEASE, 0);
606 return EINVAL;
607 }
608
609
610 /*
611 * Unbind the individual pages and flush the chipset's
612 * TLB. Unwire the pages so they can be swapped.
613 */
614 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
615 AGP_UNBIND_PAGE(sc, mem->am_offset + i);
616
617 agp_flush_cache();
618 AGP_FLUSH_TLB(sc);
619
620 bus_dmamap_unload(sc->as_dmat, mem->am_dmamap);
621 bus_dmamem_unmap(sc->as_dmat, mem->am_virtual, mem->am_size);
622 bus_dmamem_free(sc->as_dmat, mem->am_dmaseg, mem->am_nseg);
623
624 free(mem->am_dmaseg, M_AGP);
625
626 mem->am_offset = 0;
627 mem->am_is_bound = 0;
628
629 lockmgr(&sc->as_lock, LK_RELEASE, 0);
630
631 return 0;
632 }
633
634 /* Helper functions for implementing user/kernel api */
635
636 static int
637 agp_acquire_helper(struct agp_softc *sc, enum agp_acquire_state state)
638 {
639 if (sc->as_state != AGP_ACQUIRE_FREE)
640 return EBUSY;
641 sc->as_state = state;
642
643 return 0;
644 }
645
646 static int
647 agp_release_helper(struct agp_softc *sc, enum agp_acquire_state state)
648 {
649 struct agp_memory *mem;
650
651 if (sc->as_state == AGP_ACQUIRE_FREE)
652 return 0;
653
654 if (sc->as_state != state)
655 return EBUSY;
656
657 /*
658 * Clear out the aperture and free any outstanding memory blocks.
659 */
660 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
661 if (mem->am_is_bound) {
662 printf("agp_release_helper: mem %d is bound\n",
663 mem->am_id);
664 AGP_UNBIND_MEMORY(sc, mem);
665 }
666 }
667
668 sc->as_state = AGP_ACQUIRE_FREE;
669 return 0;
670 }
671
672 static struct agp_memory *
673 agp_find_memory(struct agp_softc *sc, int id)
674 {
675 struct agp_memory *mem;
676
677 AGP_DPF("searching for memory block %d\n", id);
678 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
679 AGP_DPF("considering memory block %d\n", mem->am_id);
680 if (mem->am_id == id)
681 return mem;
682 }
683 return 0;
684 }
685
686 /* Implementation of the userland ioctl api */
687
688 static int
689 agp_info_user(struct agp_softc *sc, agp_info *info)
690 {
691 memset(info, 0, sizeof *info);
692 info->bridge_id = sc->as_id;
693 if (sc->as_capoff != 0)
694 info->agp_mode = pci_conf_read(sc->as_pc, sc->as_tag,
695 sc->as_capoff + AGP_STATUS);
696 else
697 info->agp_mode = 0; /* i810 doesn't have real AGP */
698 info->aper_base = sc->as_apaddr;
699 info->aper_size = AGP_GET_APERTURE(sc) >> 20;
700 info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
701 info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
702
703 return 0;
704 }
705
706 static int
707 agp_setup_user(struct agp_softc *sc, agp_setup *setup)
708 {
709 return AGP_ENABLE(sc, setup->agp_mode);
710 }
711
712 static int
713 agp_allocate_user(struct agp_softc *sc, agp_allocate *alloc)
714 {
715 struct agp_memory *mem;
716
717 mem = AGP_ALLOC_MEMORY(sc,
718 alloc->type,
719 alloc->pg_count << AGP_PAGE_SHIFT);
720 if (mem) {
721 alloc->key = mem->am_id;
722 alloc->physical = mem->am_physical;
723 return 0;
724 } else {
725 return ENOMEM;
726 }
727 }
728
729 static int
730 agp_deallocate_user(struct agp_softc *sc, int id)
731 {
732 struct agp_memory *mem = agp_find_memory(sc, id);
733
734 if (mem) {
735 AGP_FREE_MEMORY(sc, mem);
736 return 0;
737 } else {
738 return ENOENT;
739 }
740 }
741
742 static int
743 agp_bind_user(struct agp_softc *sc, agp_bind *bind)
744 {
745 struct agp_memory *mem = agp_find_memory(sc, bind->key);
746
747 if (!mem)
748 return ENOENT;
749
750 return AGP_BIND_MEMORY(sc, mem, bind->pg_start << AGP_PAGE_SHIFT);
751 }
752
753 static int
754 agp_unbind_user(struct agp_softc *sc, agp_unbind *unbind)
755 {
756 struct agp_memory *mem = agp_find_memory(sc, unbind->key);
757
758 if (!mem)
759 return ENOENT;
760
761 return AGP_UNBIND_MEMORY(sc, mem);
762 }
763
764 int
765 agpopen(dev_t dev, int oflags, int devtype, struct proc *p)
766 {
767 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
768
769 if (sc == NULL)
770 return ENXIO;
771
772 if (sc->as_chipc == NULL)
773 return ENXIO;
774
775 if (!sc->as_isopen)
776 sc->as_isopen = 1;
777 else
778 return EBUSY;
779
780 return 0;
781 }
782
783 int
784 agpclose(dev_t dev, int fflag, int devtype, struct proc *p)
785 {
786 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
787
788 /*
789 * Clear the GATT and force release on last close
790 */
791 if (sc->as_state == AGP_ACQUIRE_USER)
792 agp_release_helper(sc, AGP_ACQUIRE_USER);
793 sc->as_isopen = 0;
794
795 return 0;
796 }
797
798 int
799 agpioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
800 {
801 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
802
803 if (sc == NULL)
804 return ENODEV;
805
806 if ((fflag & FWRITE) == 0 && cmd != AGPIOC_INFO)
807 return EPERM;
808
809 switch (cmd) {
810 case AGPIOC_INFO:
811 return agp_info_user(sc, (agp_info *) data);
812
813 case AGPIOC_ACQUIRE:
814 return agp_acquire_helper(sc, AGP_ACQUIRE_USER);
815
816 case AGPIOC_RELEASE:
817 return agp_release_helper(sc, AGP_ACQUIRE_USER);
818
819 case AGPIOC_SETUP:
820 return agp_setup_user(sc, (agp_setup *)data);
821
822 case AGPIOC_ALLOCATE:
823 return agp_allocate_user(sc, (agp_allocate *)data);
824
825 case AGPIOC_DEALLOCATE:
826 return agp_deallocate_user(sc, *(int *) data);
827
828 case AGPIOC_BIND:
829 return agp_bind_user(sc, (agp_bind *)data);
830
831 case AGPIOC_UNBIND:
832 return agp_unbind_user(sc, (agp_unbind *)data);
833
834 }
835
836 return EINVAL;
837 }
838
839 paddr_t
840 agpmmap(dev_t dev, off_t offset, int prot)
841 {
842 struct agp_softc *sc = device_lookup(&agp_cd, AGPUNIT(dev));
843
844 if (offset > AGP_GET_APERTURE(sc))
845 return -1;
846
847 return (bus_space_mmap(sc->as_apt, sc->as_apaddr, offset, prot,
848 BUS_SPACE_MAP_LINEAR));
849 }
850
851 /* Implementation of the kernel api */
852
853 void *
854 agp_find_device(int unit)
855 {
856 return device_lookup(&agp_cd, unit);
857 }
858
859 enum agp_acquire_state
860 agp_state(void *devcookie)
861 {
862 struct agp_softc *sc = devcookie;
863 return sc->as_state;
864 }
865
866 void
867 agp_get_info(void *devcookie, struct agp_info *info)
868 {
869 struct agp_softc *sc = devcookie;
870
871 info->ai_mode = pci_conf_read(sc->as_pc, sc->as_tag,
872 sc->as_capoff + AGP_STATUS);
873 info->ai_aperture_base = sc->as_apaddr;
874 info->ai_aperture_size = sc->as_apsize; /* XXXfvdl inconsistent */
875 info->ai_aperture_vaddr = bus_space_vaddr(sc->as_apt, sc->as_aph);
876 info->ai_memory_allowed = sc->as_maxmem;
877 info->ai_memory_used = sc->as_allocated;
878 }
879
880 int
881 agp_acquire(void *dev)
882 {
883 return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
884 }
885
886 int
887 agp_release(void *dev)
888 {
889 return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
890 }
891
892 int
893 agp_enable(void *dev, u_int32_t mode)
894 {
895 struct agp_softc *sc = dev;
896
897 return AGP_ENABLE(sc, mode);
898 }
899
900 void *agp_alloc_memory(void *dev, int type, vsize_t bytes)
901 {
902 struct agp_softc *sc = dev;
903
904 return (void *)AGP_ALLOC_MEMORY(sc, type, bytes);
905 }
906
907 void agp_free_memory(void *dev, void *handle)
908 {
909 struct agp_softc *sc = dev;
910 struct agp_memory *mem = (struct agp_memory *) handle;
911 AGP_FREE_MEMORY(sc, mem);
912 }
913
914 int agp_bind_memory(void *dev, void *handle, off_t offset)
915 {
916 struct agp_softc *sc = dev;
917 struct agp_memory *mem = (struct agp_memory *) handle;
918
919 return AGP_BIND_MEMORY(sc, mem, offset);
920 }
921
922 int agp_unbind_memory(void *dev, void *handle)
923 {
924 struct agp_softc *sc = dev;
925 struct agp_memory *mem = (struct agp_memory *) handle;
926
927 return AGP_UNBIND_MEMORY(sc, mem);
928 }
929
930 void agp_memory_info(void *dev, void *handle, struct agp_memory_info *mi)
931 {
932 struct agp_memory *mem = (struct agp_memory *) handle;
933
934 mi->ami_size = mem->am_size;
935 mi->ami_physical = mem->am_physical;
936 mi->ami_offset = mem->am_offset;
937 mi->ami_is_bound = mem->am_is_bound;
938 }
939
940 int
941 agp_alloc_dmamem(bus_dma_tag_t tag, size_t size, int flags,
942 bus_dmamap_t *mapp, caddr_t *vaddr, bus_addr_t *baddr,
943 bus_dma_segment_t *seg, int nseg, int *rseg)
944
945 {
946 int error, level = 0;
947
948 if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
949 seg, nseg, rseg, BUS_DMA_NOWAIT)) != 0)
950 goto out;
951 level++;
952
953 if ((error = bus_dmamem_map(tag, seg, *rseg, size, vaddr,
954 BUS_DMA_NOWAIT | flags)) != 0)
955 goto out;
956 level++;
957
958 if ((error = bus_dmamap_create(tag, size, *rseg, size, 0,
959 BUS_DMA_NOWAIT, mapp)) != 0)
960 goto out;
961 level++;
962
963 if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
964 BUS_DMA_NOWAIT)) != 0)
965 goto out;
966
967 *baddr = (*mapp)->dm_segs[0].ds_addr;
968
969 return 0;
970 out:
971 switch (level) {
972 case 3:
973 bus_dmamap_destroy(tag, *mapp);
974 /* FALLTHROUGH */
975 case 2:
976 bus_dmamem_unmap(tag, *vaddr, size);
977 /* FALLTHROUGH */
978 case 1:
979 bus_dmamem_free(tag, seg, *rseg);
980 break;
981 default:
982 break;
983 }
984
985 return error;
986 }
987
988 void
989 agp_free_dmamem(bus_dma_tag_t tag, size_t size, bus_dmamap_t map,
990 caddr_t vaddr, bus_dma_segment_t *seg, int nseg)
991 {
992
993 bus_dmamap_unload(tag, map);
994 bus_dmamap_destroy(tag, map);
995 bus_dmamem_unmap(tag, vaddr, size);
996 bus_dmamem_free(tag, seg, nseg);
997 }
998