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