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