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