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