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