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