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