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