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