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