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