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