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