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