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