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