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