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