1 /** 2 * \file drm_bufs.c 3 * Generic buffer template 4 * 5 * \author Rickard E. (Rik) Faith <faith@valinux.com> 6 * \author Gareth Hughes <gareth@valinux.com> 7 */ 8 9 /* 10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com 11 * 12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. 13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 14 * All Rights Reserved. 15 * 16 * Permission is hereby granted, free of charge, to any person obtaining a 17 * copy of this software and associated documentation files (the "Software"), 18 * to deal in the Software without restriction, including without limitation 19 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 20 * and/or sell copies of the Software, and to permit persons to whom the 21 * Software is furnished to do so, subject to the following conditions: 22 * 23 * The above copyright notice and this permission notice (including the next 24 * paragraph) shall be included in all copies or substantial portions of the 25 * Software. 26 * 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 33 * OTHER DEALINGS IN THE SOFTWARE. 34 */ 35 36 #include <sys/cdefs.h> 37 #include <sys/param.h> 38 #include <sys/shm.h> 39 40 #include <dev/pci/pcireg.h> 41 42 #include <dev/drm2/drmP.h> 43 44 /* Allocation of PCI memory resources (framebuffer, registers, etc.) for 45 * drm_get_resource_*. Note that they are not RF_ACTIVE, so there's no virtual 46 * address for accessing them. Cleaned up at unload. 47 */ 48 static int drm_alloc_resource(struct drm_device *dev, int resource) 49 { 50 struct resource *res; 51 int rid; 52 53 if (resource >= DRM_MAX_PCI_RESOURCE) { 54 DRM_ERROR("Resource %d too large\n", resource); 55 return 1; 56 } 57 58 if (dev->pcir[resource] != NULL) { 59 return 0; 60 } 61 62 rid = PCIR_BAR(resource); 63 res = bus_alloc_resource_any(dev->dev, SYS_RES_MEMORY, &rid, 64 RF_SHAREABLE); 65 if (res == NULL) { 66 DRM_ERROR("Couldn't find resource 0x%x\n", resource); 67 return 1; 68 } 69 70 if (dev->pcir[resource] == NULL) { 71 dev->pcirid[resource] = rid; 72 dev->pcir[resource] = res; 73 } 74 75 return 0; 76 } 77 78 unsigned long drm_get_resource_start(struct drm_device *dev, 79 unsigned int resource) 80 { 81 unsigned long start; 82 83 mtx_lock(&dev->pcir_lock); 84 85 if (drm_alloc_resource(dev, resource) != 0) 86 return 0; 87 88 start = rman_get_start(dev->pcir[resource]); 89 90 mtx_unlock(&dev->pcir_lock); 91 92 return (start); 93 } 94 95 unsigned long drm_get_resource_len(struct drm_device *dev, 96 unsigned int resource) 97 { 98 unsigned long len; 99 100 mtx_lock(&dev->pcir_lock); 101 102 if (drm_alloc_resource(dev, resource) != 0) 103 return 0; 104 105 len = rman_get_size(dev->pcir[resource]); 106 107 mtx_unlock(&dev->pcir_lock); 108 109 return (len); 110 } 111 112 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev, 113 struct drm_local_map *map) 114 { 115 struct drm_map_list *entry; 116 list_for_each_entry(entry, &dev->maplist, head) { 117 /* 118 * Because the kernel-userspace ABI is fixed at a 32-bit offset 119 * while PCI resources may live above that, we only compare the 120 * lower 32 bits of the map offset for maps of type 121 * _DRM_FRAMEBUFFER or _DRM_REGISTERS. 122 * It is assumed that if a driver have more than one resource 123 * of each type, the lower 32 bits are different. 124 */ 125 if (!entry->map || 126 map->type != entry->map->type || 127 entry->master != dev->primary->master) 128 continue; 129 switch (map->type) { 130 case _DRM_SHM: 131 if (map->flags != _DRM_CONTAINS_LOCK) 132 break; 133 return entry; 134 case _DRM_REGISTERS: 135 case _DRM_FRAME_BUFFER: 136 if ((entry->map->offset & 0xffffffff) == 137 (map->offset & 0xffffffff)) 138 return entry; 139 default: /* Make gcc happy */ 140 ; 141 } 142 if (entry->map->offset == map->offset) 143 return entry; 144 } 145 146 return NULL; 147 } 148 149 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash, 150 unsigned long user_token, int hashed_handle, int shm) 151 { 152 int use_hashed_handle, shift; 153 unsigned long add; 154 155 #if (BITS_PER_LONG == 64) 156 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle); 157 #elif (BITS_PER_LONG == 32) 158 use_hashed_handle = hashed_handle; 159 #else 160 #error Unsupported long size. Neither 64 nor 32 bits. 161 #endif 162 163 if (!use_hashed_handle) { 164 int ret; 165 hash->key = user_token >> PAGE_SHIFT; 166 ret = drm_ht_insert_item(&dev->map_hash, hash); 167 if (ret != -EINVAL) 168 return ret; 169 } 170 171 shift = 0; 172 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT; 173 if (shm && (SHMLBA > PAGE_SIZE)) { 174 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1; 175 176 /* For shared memory, we have to preserve the SHMLBA 177 * bits of the eventual vma->vm_pgoff value during 178 * mmap(). Otherwise we run into cache aliasing problems 179 * on some platforms. On these platforms, the pgoff of 180 * a mmap() request is used to pick a suitable virtual 181 * address for the mmap() region such that it will not 182 * cause cache aliasing problems. 183 * 184 * Therefore, make sure the SHMLBA relevant bits of the 185 * hash value we use are equal to those in the original 186 * kernel virtual address. 187 */ 188 shift = bits; 189 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL)); 190 } 191 192 return drm_ht_just_insert_please(&dev->map_hash, hash, 193 user_token, 32 - PAGE_SHIFT - 3, 194 shift, add); 195 } 196 197 /** 198 * Core function to create a range of memory available for mapping by a 199 * non-root process. 200 * 201 * Adjusts the memory offset to its absolute value according to the mapping 202 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where 203 * applicable and if supported by the kernel. 204 */ 205 static int drm_addmap_core(struct drm_device * dev, resource_size_t offset, 206 unsigned int size, enum drm_map_type type, 207 enum drm_map_flags flags, 208 struct drm_map_list ** maplist) 209 { 210 struct drm_local_map *map; 211 struct drm_map_list *list; 212 drm_dma_handle_t *dmah; 213 unsigned long user_token; 214 int ret; 215 int align; 216 217 map = malloc(sizeof(*map), DRM_MEM_MAPS, M_NOWAIT); 218 if (!map) 219 return -ENOMEM; 220 221 map->offset = offset; 222 map->size = size; 223 map->flags = flags; 224 map->type = type; 225 226 /* Only allow shared memory to be removable since we only keep enough 227 * book keeping information about shared memory to allow for removal 228 * when processes fork. 229 */ 230 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) { 231 free(map, DRM_MEM_MAPS); 232 return -EINVAL; 233 } 234 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n", 235 (unsigned long long)map->offset, map->size, map->type); 236 237 /* page-align _DRM_SHM maps. They are allocated here so there is no security 238 * hole created by that and it works around various broken drivers that use 239 * a non-aligned quantity to map the SAREA. --BenH 240 */ 241 if (map->type == _DRM_SHM) 242 map->size = PAGE_ALIGN(map->size); 243 244 /* 245 * FreeBSD port note: FreeBSD's PAGE_MASK is the inverse of 246 * Linux's one. That's why the test below doesn't inverse the 247 * constant. 248 */ 249 if ((map->offset & ((resource_size_t)PAGE_MASK)) || (map->size & (PAGE_MASK))) { 250 free(map, DRM_MEM_MAPS); 251 return -EINVAL; 252 } 253 map->mtrr = -1; 254 map->handle = NULL; 255 256 switch (map->type) { 257 case _DRM_REGISTERS: 258 case _DRM_FRAME_BUFFER: 259 #ifdef __linux__ 260 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__) 261 if (map->offset + (map->size-1) < map->offset || 262 map->offset < virt_to_phys(high_memory)) { 263 kfree(map); 264 return -EINVAL; 265 } 266 #endif 267 #endif 268 /* Some drivers preinitialize some maps, without the X Server 269 * needing to be aware of it. Therefore, we just return success 270 * when the server tries to create a duplicate map. 271 */ 272 list = drm_find_matching_map(dev, map); 273 if (list != NULL) { 274 if (list->map->size != map->size) { 275 DRM_DEBUG("Matching maps of type %d with " 276 "mismatched sizes, (%ld vs %ld)\n", 277 map->type, map->size, 278 list->map->size); 279 list->map->size = map->size; 280 } 281 282 free(map, DRM_MEM_MAPS); 283 *maplist = list; 284 return 0; 285 } 286 287 if (drm_core_has_MTRR(dev)) { 288 if (map->type == _DRM_FRAME_BUFFER || 289 (map->flags & _DRM_WRITE_COMBINING)) { 290 if (drm_mtrr_add( 291 map->offset, map->size, 292 DRM_MTRR_WC) == 0) 293 map->mtrr = 1; 294 } 295 } 296 if (map->type == _DRM_REGISTERS) { 297 drm_core_ioremap(map, dev); 298 if (!map->handle) { 299 free(map, DRM_MEM_MAPS); 300 return -ENOMEM; 301 } 302 } 303 304 break; 305 case _DRM_SHM: 306 list = drm_find_matching_map(dev, map); 307 if (list != NULL) { 308 if(list->map->size != map->size) { 309 DRM_DEBUG("Matching maps of type %d with " 310 "mismatched sizes, (%ld vs %ld)\n", 311 map->type, map->size, list->map->size); 312 list->map->size = map->size; 313 } 314 315 free(map, DRM_MEM_MAPS); 316 *maplist = list; 317 return 0; 318 } 319 map->handle = malloc(map->size, DRM_MEM_MAPS, M_NOWAIT); 320 DRM_DEBUG("%lu %d %p\n", 321 map->size, drm_order(map->size), map->handle); 322 if (!map->handle) { 323 free(map, DRM_MEM_MAPS); 324 return -ENOMEM; 325 } 326 map->offset = (unsigned long)map->handle; 327 if (map->flags & _DRM_CONTAINS_LOCK) { 328 /* Prevent a 2nd X Server from creating a 2nd lock */ 329 if (dev->primary->master->lock.hw_lock != NULL) { 330 free(map->handle, DRM_MEM_MAPS); 331 free(map, DRM_MEM_MAPS); 332 return -EBUSY; 333 } 334 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */ 335 } 336 break; 337 case _DRM_AGP: { 338 struct drm_agp_mem *entry; 339 int valid = 0; 340 341 if (!drm_core_has_AGP(dev)) { 342 free(map, DRM_MEM_MAPS); 343 return -EINVAL; 344 } 345 #ifdef __linux__ 346 #ifdef __alpha__ 347 map->offset += dev->hose->mem_space->start; 348 #endif 349 #endif 350 /* In some cases (i810 driver), user space may have already 351 * added the AGP base itself, because dev->agp->base previously 352 * only got set during AGP enable. So, only add the base 353 * address if the map's offset isn't already within the 354 * aperture. 355 */ 356 if (map->offset < dev->agp->base || 357 map->offset > dev->agp->base + 358 dev->agp->agp_info.ai_aperture_size * 1024 * 1024 - 1) { 359 map->offset += dev->agp->base; 360 } 361 map->mtrr = dev->agp->agp_mtrr; /* for getmap */ 362 363 /* This assumes the DRM is in total control of AGP space. 364 * It's not always the case as AGP can be in the control 365 * of user space (i.e. i810 driver). So this loop will get 366 * skipped and we double check that dev->agp->memory is 367 * actually set as well as being invalid before EPERM'ing 368 */ 369 list_for_each_entry(entry, &dev->agp->memory, head) { 370 if ((map->offset >= entry->bound) && 371 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) { 372 valid = 1; 373 break; 374 } 375 } 376 if (!list_empty(&dev->agp->memory) && !valid) { 377 free(map, DRM_MEM_MAPS); 378 return -EPERM; 379 } 380 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n", 381 (unsigned long long)map->offset, map->size); 382 383 break; 384 } 385 case _DRM_GEM: 386 DRM_ERROR("tried to addmap GEM object\n"); 387 break; 388 case _DRM_SCATTER_GATHER: 389 if (!dev->sg) { 390 free(map, DRM_MEM_MAPS); 391 return -EINVAL; 392 } 393 map->handle = (char *)dev->sg->vaddr + offset; 394 map->offset += (uintptr_t)dev->sg->vaddr; 395 break; 396 case _DRM_CONSISTENT: 397 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G, 398 * As we're limiting the address to 2^32-1 (or less), 399 * casting it down to 32 bits is no problem, but we 400 * need to point to a 64bit variable first. */ 401 align = map->size; 402 if ((align & (align - 1)) != 0) 403 align = PAGE_SIZE; 404 dmah = drm_pci_alloc(dev, map->size, align, BUS_SPACE_MAXADDR); 405 if (!dmah) { 406 free(map, DRM_MEM_MAPS); 407 return -ENOMEM; 408 } 409 map->handle = dmah->vaddr; 410 map->offset = dmah->busaddr; 411 map->dmah = dmah; 412 break; 413 default: 414 free(map, DRM_MEM_MAPS); 415 return -EINVAL; 416 } 417 418 list = malloc(sizeof(*list), DRM_MEM_MAPS, M_ZERO | M_NOWAIT); 419 if (!list) { 420 if (map->type == _DRM_REGISTERS) 421 drm_core_ioremapfree(map, dev); 422 free(map, DRM_MEM_MAPS); 423 return -EINVAL; 424 } 425 list->map = map; 426 427 DRM_LOCK(dev); 428 list_add(&list->head, &dev->maplist); 429 430 /* Assign a 32-bit handle */ 431 /* We do it here so that dev->struct_mutex protects the increment */ 432 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle : 433 map->offset; 434 ret = drm_map_handle(dev, &list->hash, user_token, 0, 435 (map->type == _DRM_SHM)); 436 if (ret) { 437 if (map->type == _DRM_REGISTERS) 438 drm_core_ioremapfree(map, dev); 439 free(map, DRM_MEM_MAPS); 440 free(list, DRM_MEM_MAPS); 441 DRM_UNLOCK(dev); 442 return ret; 443 } 444 445 list->user_token = list->hash.key << PAGE_SHIFT; 446 DRM_UNLOCK(dev); 447 448 if (!(map->flags & _DRM_DRIVER)) 449 list->master = dev->primary->master; 450 *maplist = list; 451 return 0; 452 } 453 454 int drm_addmap(struct drm_device * dev, resource_size_t offset, 455 unsigned int size, enum drm_map_type type, 456 enum drm_map_flags flags, struct drm_local_map ** map_ptr) 457 { 458 struct drm_map_list *list; 459 int rc; 460 461 rc = drm_addmap_core(dev, offset, size, type, flags, &list); 462 if (!rc) 463 *map_ptr = list->map; 464 return rc; 465 } 466 467 EXPORT_SYMBOL(drm_addmap); 468 469 /** 470 * Ioctl to specify a range of memory that is available for mapping by a 471 * non-root process. 472 * 473 * \param inode device inode. 474 * \param file_priv DRM file private. 475 * \param cmd command. 476 * \param arg pointer to a drm_map structure. 477 * \return zero on success or a negative value on error. 478 * 479 */ 480 int drm_addmap_ioctl(struct drm_device *dev, void *data, 481 struct drm_file *file_priv) 482 { 483 struct drm_map *map = data; 484 struct drm_map_list *maplist; 485 int err; 486 487 if (!(DRM_SUSER(DRM_CURPROC) || map->type == _DRM_AGP || map->type == _DRM_SHM)) 488 return -EPERM; 489 490 err = drm_addmap_core(dev, map->offset, map->size, map->type, 491 map->flags, &maplist); 492 493 if (err) 494 return err; 495 496 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */ 497 map->handle = (void *)(unsigned long)maplist->user_token; 498 return 0; 499 } 500 501 /** 502 * Remove a map private from list and deallocate resources if the mapping 503 * isn't in use. 504 * 505 * Searches the map on drm_device::maplist, removes it from the list, see if 506 * its being used, and free any associate resource (such as MTRR's) if it's not 507 * being on use. 508 * 509 * \sa drm_addmap 510 */ 511 int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map) 512 { 513 struct drm_map_list *r_list = NULL, *list_t; 514 int found = 0; 515 struct drm_master *master; 516 517 /* Find the list entry for the map and remove it */ 518 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) { 519 if (r_list->map == map) { 520 master = r_list->master; 521 list_del(&r_list->head); 522 drm_ht_remove_key(&dev->map_hash, 523 r_list->user_token >> PAGE_SHIFT); 524 free(r_list, DRM_MEM_MAPS); 525 found = 1; 526 break; 527 } 528 } 529 530 if (!found) 531 return -EINVAL; 532 533 switch (map->type) { 534 case _DRM_REGISTERS: 535 drm_core_ioremapfree(map, dev); 536 /* FALLTHROUGH */ 537 case _DRM_FRAME_BUFFER: 538 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) { 539 int retcode; 540 retcode = drm_mtrr_del(map->mtrr, map->offset, 541 map->size, DRM_MTRR_WC); 542 DRM_DEBUG("mtrr_del=%d\n", retcode); 543 } 544 break; 545 case _DRM_SHM: 546 free(map->handle, DRM_MEM_MAPS); 547 if (master) { 548 if (dev->sigdata.lock == master->lock.hw_lock) 549 dev->sigdata.lock = NULL; 550 master->lock.hw_lock = NULL; /* SHM removed */ 551 master->lock.file_priv = NULL; 552 DRM_WAKEUP_INT((void *)&master->lock.lock_queue); 553 } 554 break; 555 case _DRM_AGP: 556 case _DRM_SCATTER_GATHER: 557 break; 558 case _DRM_CONSISTENT: 559 drm_pci_free(dev, map->dmah); 560 break; 561 case _DRM_GEM: 562 DRM_ERROR("tried to rmmap GEM object\n"); 563 break; 564 } 565 free(map, DRM_MEM_MAPS); 566 567 return 0; 568 } 569 EXPORT_SYMBOL(drm_rmmap_locked); 570 571 int drm_rmmap(struct drm_device *dev, struct drm_local_map *map) 572 { 573 int ret; 574 575 DRM_LOCK(dev); 576 ret = drm_rmmap_locked(dev, map); 577 DRM_UNLOCK(dev); 578 579 return ret; 580 } 581 EXPORT_SYMBOL(drm_rmmap); 582 583 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on 584 * the last close of the device, and this is necessary for cleanup when things 585 * exit uncleanly. Therefore, having userland manually remove mappings seems 586 * like a pointless exercise since they're going away anyway. 587 * 588 * One use case might be after addmap is allowed for normal users for SHM and 589 * gets used by drivers that the server doesn't need to care about. This seems 590 * unlikely. 591 * 592 * \param inode device inode. 593 * \param file_priv DRM file private. 594 * \param cmd command. 595 * \param arg pointer to a struct drm_map structure. 596 * \return zero on success or a negative value on error. 597 */ 598 int drm_rmmap_ioctl(struct drm_device *dev, void *data, 599 struct drm_file *file_priv) 600 { 601 struct drm_map *request = data; 602 struct drm_local_map *map = NULL; 603 struct drm_map_list *r_list; 604 int ret; 605 606 DRM_LOCK(dev); 607 list_for_each_entry(r_list, &dev->maplist, head) { 608 if (r_list->map && 609 r_list->user_token == (unsigned long)request->handle && 610 r_list->map->flags & _DRM_REMOVABLE) { 611 map = r_list->map; 612 break; 613 } 614 } 615 616 /* List has wrapped around to the head pointer, or its empty we didn't 617 * find anything. 618 */ 619 if (list_empty(&dev->maplist) || !map) { 620 DRM_UNLOCK(dev); 621 return -EINVAL; 622 } 623 624 /* Register and framebuffer maps are permanent */ 625 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) { 626 DRM_UNLOCK(dev); 627 return 0; 628 } 629 630 ret = drm_rmmap_locked(dev, map); 631 632 DRM_UNLOCK(dev); 633 634 return ret; 635 } 636 637 /** 638 * Cleanup after an error on one of the addbufs() functions. 639 * 640 * \param dev DRM device. 641 * \param entry buffer entry where the error occurred. 642 * 643 * Frees any pages and buffers associated with the given entry. 644 */ 645 static void drm_cleanup_buf_error(struct drm_device * dev, 646 struct drm_buf_entry * entry) 647 { 648 int i; 649 650 if (entry->seg_count) { 651 for (i = 0; i < entry->seg_count; i++) { 652 if (entry->seglist[i]) { 653 drm_pci_free(dev, entry->seglist[i]); 654 } 655 } 656 free(entry->seglist, DRM_MEM_SEGS); 657 658 entry->seg_count = 0; 659 } 660 661 if (entry->buf_count) { 662 for (i = 0; i < entry->buf_count; i++) { 663 free(entry->buflist[i].dev_private, DRM_MEM_BUFS); 664 } 665 free(entry->buflist, DRM_MEM_BUFS); 666 667 entry->buf_count = 0; 668 } 669 } 670 671 #if __OS_HAS_AGP 672 /** 673 * Add AGP buffers for DMA transfers. 674 * 675 * \param dev struct drm_device to which the buffers are to be added. 676 * \param request pointer to a struct drm_buf_desc describing the request. 677 * \return zero on success or a negative number on failure. 678 * 679 * After some sanity checks creates a drm_buf structure for each buffer and 680 * reallocates the buffer list of the same size order to accommodate the new 681 * buffers. 682 */ 683 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request) 684 { 685 struct drm_device_dma *dma = dev->dma; 686 struct drm_buf_entry *entry; 687 struct drm_agp_mem *agp_entry; 688 struct drm_buf *buf; 689 unsigned long offset; 690 unsigned long agp_offset; 691 int count; 692 int order; 693 int size; 694 int alignment; 695 int page_order; 696 int total; 697 int byte_count; 698 int i, valid; 699 struct drm_buf **temp_buflist; 700 701 if (!dma) 702 return -EINVAL; 703 704 count = request->count; 705 order = drm_order(request->size); 706 size = 1 << order; 707 708 alignment = (request->flags & _DRM_PAGE_ALIGN) 709 ? PAGE_ALIGN(size) : size; 710 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; 711 total = PAGE_SIZE << page_order; 712 713 byte_count = 0; 714 agp_offset = dev->agp->base + request->agp_start; 715 716 DRM_DEBUG("count: %d\n", count); 717 DRM_DEBUG("order: %d\n", order); 718 DRM_DEBUG("size: %d\n", size); 719 DRM_DEBUG("agp_offset: %lx\n", agp_offset); 720 DRM_DEBUG("alignment: %d\n", alignment); 721 DRM_DEBUG("page_order: %d\n", page_order); 722 DRM_DEBUG("total: %d\n", total); 723 724 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) 725 return -EINVAL; 726 727 /* Make sure buffers are located in AGP memory that we own */ 728 valid = 0; 729 list_for_each_entry(agp_entry, &dev->agp->memory, head) { 730 if ((agp_offset >= agp_entry->bound) && 731 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) { 732 valid = 1; 733 break; 734 } 735 } 736 if (!list_empty(&dev->agp->memory) && !valid) { 737 DRM_DEBUG("zone invalid\n"); 738 return -EINVAL; 739 } 740 mtx_lock(&dev->count_lock); 741 if (dev->buf_use) { 742 mtx_unlock(&dev->count_lock); 743 return -EBUSY; 744 } 745 atomic_inc(&dev->buf_alloc); 746 mtx_unlock(&dev->count_lock); 747 748 DRM_LOCK(dev); 749 entry = &dma->bufs[order]; 750 if (entry->buf_count) { 751 DRM_UNLOCK(dev); 752 atomic_dec(&dev->buf_alloc); 753 return -ENOMEM; /* May only call once for each order */ 754 } 755 756 if (count < 0 || count > 4096) { 757 DRM_UNLOCK(dev); 758 atomic_dec(&dev->buf_alloc); 759 return -EINVAL; 760 } 761 762 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS, 763 M_NOWAIT | M_ZERO); 764 if (!entry->buflist) { 765 DRM_UNLOCK(dev); 766 atomic_dec(&dev->buf_alloc); 767 return -ENOMEM; 768 } 769 770 entry->buf_size = size; 771 entry->page_order = page_order; 772 773 offset = 0; 774 775 while (entry->buf_count < count) { 776 buf = &entry->buflist[entry->buf_count]; 777 buf->idx = dma->buf_count + entry->buf_count; 778 buf->total = alignment; 779 buf->order = order; 780 buf->used = 0; 781 782 buf->offset = (dma->byte_count + offset); 783 buf->bus_address = agp_offset + offset; 784 buf->address = (void *)(agp_offset + offset); 785 buf->next = NULL; 786 buf->waiting = 0; 787 buf->pending = 0; 788 buf->file_priv = NULL; 789 790 buf->dev_priv_size = dev->driver->dev_priv_size; 791 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS, 792 M_NOWAIT | M_ZERO); 793 if (!buf->dev_private) { 794 /* Set count correctly so we free the proper amount. */ 795 entry->buf_count = count; 796 drm_cleanup_buf_error(dev, entry); 797 DRM_UNLOCK(dev); 798 atomic_dec(&dev->buf_alloc); 799 return -ENOMEM; 800 } 801 802 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); 803 804 offset += alignment; 805 entry->buf_count++; 806 byte_count += PAGE_SIZE << page_order; 807 } 808 809 DRM_DEBUG("byte_count: %d\n", byte_count); 810 811 temp_buflist = realloc(dma->buflist, 812 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), 813 DRM_MEM_BUFS, M_NOWAIT); 814 if (!temp_buflist) { 815 /* Free the entry because it isn't valid */ 816 drm_cleanup_buf_error(dev, entry); 817 DRM_UNLOCK(dev); 818 atomic_dec(&dev->buf_alloc); 819 return -ENOMEM; 820 } 821 dma->buflist = temp_buflist; 822 823 for (i = 0; i < entry->buf_count; i++) { 824 dma->buflist[i + dma->buf_count] = &entry->buflist[i]; 825 } 826 827 dma->buf_count += entry->buf_count; 828 dma->seg_count += entry->seg_count; 829 dma->page_count += byte_count >> PAGE_SHIFT; 830 dma->byte_count += byte_count; 831 832 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); 833 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); 834 835 DRM_UNLOCK(dev); 836 837 request->count = entry->buf_count; 838 request->size = size; 839 840 dma->flags = _DRM_DMA_USE_AGP; 841 842 atomic_dec(&dev->buf_alloc); 843 return 0; 844 } 845 EXPORT_SYMBOL(drm_addbufs_agp); 846 #endif /* __OS_HAS_AGP */ 847 848 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request) 849 { 850 struct drm_device_dma *dma = dev->dma; 851 int count; 852 int order; 853 int size; 854 int total; 855 int page_order; 856 struct drm_buf_entry *entry; 857 drm_dma_handle_t *dmah; 858 struct drm_buf *buf; 859 int alignment; 860 unsigned long offset; 861 int i; 862 int byte_count; 863 int page_count; 864 unsigned long *temp_pagelist; 865 struct drm_buf **temp_buflist; 866 867 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) 868 return -EINVAL; 869 870 if (!dma) 871 return -EINVAL; 872 873 if (!DRM_SUSER(DRM_CURPROC)) 874 return -EPERM; 875 876 count = request->count; 877 order = drm_order(request->size); 878 size = 1 << order; 879 880 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n", 881 request->count, request->size, size, order); 882 883 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) 884 return -EINVAL; 885 886 alignment = (request->flags & _DRM_PAGE_ALIGN) 887 ? PAGE_ALIGN(size) : size; 888 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; 889 total = PAGE_SIZE << page_order; 890 891 mtx_lock(&dev->count_lock); 892 if (dev->buf_use) { 893 mtx_unlock(&dev->count_lock); 894 return -EBUSY; 895 } 896 atomic_inc(&dev->buf_alloc); 897 mtx_unlock(&dev->count_lock); 898 899 DRM_LOCK(dev); 900 entry = &dma->bufs[order]; 901 if (entry->buf_count) { 902 DRM_UNLOCK(dev); 903 atomic_dec(&dev->buf_alloc); 904 return -ENOMEM; /* May only call once for each order */ 905 } 906 907 if (count < 0 || count > 4096) { 908 DRM_UNLOCK(dev); 909 atomic_dec(&dev->buf_alloc); 910 return -EINVAL; 911 } 912 913 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS, 914 M_NOWAIT | M_ZERO); 915 if (!entry->buflist) { 916 DRM_UNLOCK(dev); 917 atomic_dec(&dev->buf_alloc); 918 return -ENOMEM; 919 } 920 921 entry->seglist = malloc(count * sizeof(*entry->seglist), DRM_MEM_SEGS, 922 M_NOWAIT | M_ZERO); 923 if (!entry->seglist) { 924 free(entry->buflist, DRM_MEM_BUFS); 925 DRM_UNLOCK(dev); 926 atomic_dec(&dev->buf_alloc); 927 return -ENOMEM; 928 } 929 930 /* Keep the original pagelist until we know all the allocations 931 * have succeeded 932 */ 933 temp_pagelist = malloc((dma->page_count + (count << page_order)) * 934 sizeof(*dma->pagelist), DRM_MEM_PAGES, M_NOWAIT); 935 if (!temp_pagelist) { 936 free(entry->buflist, DRM_MEM_BUFS); 937 free(entry->seglist, DRM_MEM_SEGS); 938 DRM_UNLOCK(dev); 939 atomic_dec(&dev->buf_alloc); 940 return -ENOMEM; 941 } 942 memcpy(temp_pagelist, 943 dma->pagelist, dma->page_count * sizeof(*dma->pagelist)); 944 DRM_DEBUG("pagelist: %d entries\n", 945 dma->page_count + (count << page_order)); 946 947 entry->buf_size = size; 948 entry->page_order = page_order; 949 byte_count = 0; 950 page_count = 0; 951 952 while (entry->buf_count < count) { 953 954 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, BUS_SPACE_MAXADDR); 955 956 if (!dmah) { 957 /* Set count correctly so we free the proper amount. */ 958 entry->buf_count = count; 959 entry->seg_count = count; 960 drm_cleanup_buf_error(dev, entry); 961 free(temp_pagelist, DRM_MEM_PAGES); 962 DRM_UNLOCK(dev); 963 atomic_dec(&dev->buf_alloc); 964 return -ENOMEM; 965 } 966 entry->seglist[entry->seg_count++] = dmah; 967 for (i = 0; i < (1 << page_order); i++) { 968 DRM_DEBUG("page %d @ 0x%08lx\n", 969 dma->page_count + page_count, 970 (unsigned long)dmah->vaddr + PAGE_SIZE * i); 971 temp_pagelist[dma->page_count + page_count++] 972 = (unsigned long)dmah->vaddr + PAGE_SIZE * i; 973 } 974 for (offset = 0; 975 offset + size <= total && entry->buf_count < count; 976 offset += alignment, ++entry->buf_count) { 977 buf = &entry->buflist[entry->buf_count]; 978 buf->idx = dma->buf_count + entry->buf_count; 979 buf->total = alignment; 980 buf->order = order; 981 buf->used = 0; 982 buf->offset = (dma->byte_count + byte_count + offset); 983 buf->address = (void *)((char *)dmah->vaddr + offset); 984 buf->bus_address = dmah->busaddr + offset; 985 buf->next = NULL; 986 buf->waiting = 0; 987 buf->pending = 0; 988 buf->file_priv = NULL; 989 990 buf->dev_priv_size = dev->driver->dev_priv_size; 991 buf->dev_private = malloc(buf->dev_priv_size, 992 DRM_MEM_BUFS, M_NOWAIT | M_ZERO); 993 if (!buf->dev_private) { 994 /* Set count correctly so we free the proper amount. */ 995 entry->buf_count = count; 996 entry->seg_count = count; 997 drm_cleanup_buf_error(dev, entry); 998 free(temp_pagelist, DRM_MEM_PAGES); 999 DRM_UNLOCK(dev); 1000 atomic_dec(&dev->buf_alloc); 1001 return -ENOMEM; 1002 } 1003 1004 DRM_DEBUG("buffer %d @ %p\n", 1005 entry->buf_count, buf->address); 1006 } 1007 byte_count += PAGE_SIZE << page_order; 1008 } 1009 1010 temp_buflist = realloc(dma->buflist, 1011 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), 1012 DRM_MEM_BUFS, M_NOWAIT); 1013 if (!temp_buflist) { 1014 /* Free the entry because it isn't valid */ 1015 drm_cleanup_buf_error(dev, entry); 1016 free(temp_pagelist, DRM_MEM_PAGES); 1017 DRM_UNLOCK(dev); 1018 atomic_dec(&dev->buf_alloc); 1019 return -ENOMEM; 1020 } 1021 dma->buflist = temp_buflist; 1022 1023 for (i = 0; i < entry->buf_count; i++) { 1024 dma->buflist[i + dma->buf_count] = &entry->buflist[i]; 1025 } 1026 1027 /* No allocations failed, so now we can replace the original pagelist 1028 * with the new one. 1029 */ 1030 if (dma->page_count) { 1031 free(dma->pagelist, DRM_MEM_PAGES); 1032 } 1033 dma->pagelist = temp_pagelist; 1034 1035 dma->buf_count += entry->buf_count; 1036 dma->seg_count += entry->seg_count; 1037 dma->page_count += entry->seg_count << page_order; 1038 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order); 1039 1040 DRM_UNLOCK(dev); 1041 1042 request->count = entry->buf_count; 1043 request->size = size; 1044 1045 if (request->flags & _DRM_PCI_BUFFER_RO) 1046 dma->flags = _DRM_DMA_USE_PCI_RO; 1047 1048 atomic_dec(&dev->buf_alloc); 1049 return 0; 1050 1051 } 1052 EXPORT_SYMBOL(drm_addbufs_pci); 1053 1054 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request) 1055 { 1056 struct drm_device_dma *dma = dev->dma; 1057 struct drm_buf_entry *entry; 1058 struct drm_buf *buf; 1059 unsigned long offset; 1060 unsigned long agp_offset; 1061 int count; 1062 int order; 1063 int size; 1064 int alignment; 1065 int page_order; 1066 int total; 1067 int byte_count; 1068 int i; 1069 struct drm_buf **temp_buflist; 1070 1071 if (!drm_core_check_feature(dev, DRIVER_SG)) 1072 return -EINVAL; 1073 1074 if (!dma) 1075 return -EINVAL; 1076 1077 if (!DRM_SUSER(DRM_CURPROC)) 1078 return -EPERM; 1079 1080 count = request->count; 1081 order = drm_order(request->size); 1082 size = 1 << order; 1083 1084 alignment = (request->flags & _DRM_PAGE_ALIGN) 1085 ? PAGE_ALIGN(size) : size; 1086 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; 1087 total = PAGE_SIZE << page_order; 1088 1089 byte_count = 0; 1090 agp_offset = request->agp_start; 1091 1092 DRM_DEBUG("count: %d\n", count); 1093 DRM_DEBUG("order: %d\n", order); 1094 DRM_DEBUG("size: %d\n", size); 1095 DRM_DEBUG("agp_offset: %lu\n", agp_offset); 1096 DRM_DEBUG("alignment: %d\n", alignment); 1097 DRM_DEBUG("page_order: %d\n", page_order); 1098 DRM_DEBUG("total: %d\n", total); 1099 1100 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) 1101 return -EINVAL; 1102 1103 mtx_lock(&dev->count_lock); 1104 if (dev->buf_use) { 1105 mtx_unlock(&dev->count_lock); 1106 return -EBUSY; 1107 } 1108 atomic_inc(&dev->buf_alloc); 1109 mtx_unlock(&dev->count_lock); 1110 1111 DRM_LOCK(dev); 1112 entry = &dma->bufs[order]; 1113 if (entry->buf_count) { 1114 DRM_UNLOCK(dev); 1115 atomic_dec(&dev->buf_alloc); 1116 return -ENOMEM; /* May only call once for each order */ 1117 } 1118 1119 if (count < 0 || count > 4096) { 1120 DRM_UNLOCK(dev); 1121 atomic_dec(&dev->buf_alloc); 1122 return -EINVAL; 1123 } 1124 1125 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS, 1126 M_NOWAIT | M_ZERO); 1127 if (!entry->buflist) { 1128 DRM_UNLOCK(dev); 1129 atomic_dec(&dev->buf_alloc); 1130 return -ENOMEM; 1131 } 1132 1133 entry->buf_size = size; 1134 entry->page_order = page_order; 1135 1136 offset = 0; 1137 1138 while (entry->buf_count < count) { 1139 buf = &entry->buflist[entry->buf_count]; 1140 buf->idx = dma->buf_count + entry->buf_count; 1141 buf->total = alignment; 1142 buf->order = order; 1143 buf->used = 0; 1144 1145 buf->offset = (dma->byte_count + offset); 1146 buf->bus_address = agp_offset + offset; 1147 buf->address = (void *)(agp_offset + offset 1148 + (unsigned long)dev->sg->vaddr); 1149 buf->next = NULL; 1150 buf->waiting = 0; 1151 buf->pending = 0; 1152 buf->file_priv = NULL; 1153 1154 buf->dev_priv_size = dev->driver->dev_priv_size; 1155 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS, 1156 M_NOWAIT | M_ZERO); 1157 if (!buf->dev_private) { 1158 /* Set count correctly so we free the proper amount. */ 1159 entry->buf_count = count; 1160 drm_cleanup_buf_error(dev, entry); 1161 DRM_UNLOCK(dev); 1162 atomic_dec(&dev->buf_alloc); 1163 return -ENOMEM; 1164 } 1165 1166 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); 1167 1168 offset += alignment; 1169 entry->buf_count++; 1170 byte_count += PAGE_SIZE << page_order; 1171 } 1172 1173 DRM_DEBUG("byte_count: %d\n", byte_count); 1174 1175 temp_buflist = realloc(dma->buflist, 1176 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), 1177 DRM_MEM_BUFS, M_NOWAIT); 1178 if (!temp_buflist) { 1179 /* Free the entry because it isn't valid */ 1180 drm_cleanup_buf_error(dev, entry); 1181 DRM_UNLOCK(dev); 1182 atomic_dec(&dev->buf_alloc); 1183 return -ENOMEM; 1184 } 1185 dma->buflist = temp_buflist; 1186 1187 for (i = 0; i < entry->buf_count; i++) { 1188 dma->buflist[i + dma->buf_count] = &entry->buflist[i]; 1189 } 1190 1191 dma->buf_count += entry->buf_count; 1192 dma->seg_count += entry->seg_count; 1193 dma->page_count += byte_count >> PAGE_SHIFT; 1194 dma->byte_count += byte_count; 1195 1196 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); 1197 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); 1198 1199 DRM_UNLOCK(dev); 1200 1201 request->count = entry->buf_count; 1202 request->size = size; 1203 1204 dma->flags = _DRM_DMA_USE_SG; 1205 1206 atomic_dec(&dev->buf_alloc); 1207 return 0; 1208 } 1209 1210 static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request) 1211 { 1212 struct drm_device_dma *dma = dev->dma; 1213 struct drm_buf_entry *entry; 1214 struct drm_buf *buf; 1215 unsigned long offset; 1216 unsigned long agp_offset; 1217 int count; 1218 int order; 1219 int size; 1220 int alignment; 1221 int page_order; 1222 int total; 1223 int byte_count; 1224 int i; 1225 struct drm_buf **temp_buflist; 1226 1227 if (!drm_core_check_feature(dev, DRIVER_FB_DMA)) 1228 return -EINVAL; 1229 1230 if (!dma) 1231 return -EINVAL; 1232 1233 if (!DRM_SUSER(DRM_CURPROC)) 1234 return -EPERM; 1235 1236 count = request->count; 1237 order = drm_order(request->size); 1238 size = 1 << order; 1239 1240 alignment = (request->flags & _DRM_PAGE_ALIGN) 1241 ? PAGE_ALIGN(size) : size; 1242 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; 1243 total = PAGE_SIZE << page_order; 1244 1245 byte_count = 0; 1246 agp_offset = request->agp_start; 1247 1248 DRM_DEBUG("count: %d\n", count); 1249 DRM_DEBUG("order: %d\n", order); 1250 DRM_DEBUG("size: %d\n", size); 1251 DRM_DEBUG("agp_offset: %lu\n", agp_offset); 1252 DRM_DEBUG("alignment: %d\n", alignment); 1253 DRM_DEBUG("page_order: %d\n", page_order); 1254 DRM_DEBUG("total: %d\n", total); 1255 1256 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) 1257 return -EINVAL; 1258 1259 mtx_lock(&dev->count_lock); 1260 if (dev->buf_use) { 1261 mtx_unlock(&dev->count_lock); 1262 return -EBUSY; 1263 } 1264 atomic_inc(&dev->buf_alloc); 1265 mtx_unlock(&dev->count_lock); 1266 1267 DRM_LOCK(dev); 1268 entry = &dma->bufs[order]; 1269 if (entry->buf_count) { 1270 DRM_UNLOCK(dev); 1271 atomic_dec(&dev->buf_alloc); 1272 return -ENOMEM; /* May only call once for each order */ 1273 } 1274 1275 if (count < 0 || count > 4096) { 1276 DRM_UNLOCK(dev); 1277 atomic_dec(&dev->buf_alloc); 1278 return -EINVAL; 1279 } 1280 1281 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS, 1282 M_NOWAIT | M_ZERO); 1283 if (!entry->buflist) { 1284 DRM_UNLOCK(dev); 1285 atomic_dec(&dev->buf_alloc); 1286 return -ENOMEM; 1287 } 1288 1289 entry->buf_size = size; 1290 entry->page_order = page_order; 1291 1292 offset = 0; 1293 1294 while (entry->buf_count < count) { 1295 buf = &entry->buflist[entry->buf_count]; 1296 buf->idx = dma->buf_count + entry->buf_count; 1297 buf->total = alignment; 1298 buf->order = order; 1299 buf->used = 0; 1300 1301 buf->offset = (dma->byte_count + offset); 1302 buf->bus_address = agp_offset + offset; 1303 buf->address = (void *)(agp_offset + offset); 1304 buf->next = NULL; 1305 buf->waiting = 0; 1306 buf->pending = 0; 1307 buf->file_priv = NULL; 1308 1309 buf->dev_priv_size = dev->driver->dev_priv_size; 1310 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS, 1311 M_NOWAIT | M_ZERO); 1312 if (!buf->dev_private) { 1313 /* Set count correctly so we free the proper amount. */ 1314 entry->buf_count = count; 1315 drm_cleanup_buf_error(dev, entry); 1316 DRM_UNLOCK(dev); 1317 atomic_dec(&dev->buf_alloc); 1318 return -ENOMEM; 1319 } 1320 1321 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address); 1322 1323 offset += alignment; 1324 entry->buf_count++; 1325 byte_count += PAGE_SIZE << page_order; 1326 } 1327 1328 DRM_DEBUG("byte_count: %d\n", byte_count); 1329 1330 temp_buflist = realloc(dma->buflist, 1331 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), 1332 DRM_MEM_BUFS, M_NOWAIT); 1333 if (!temp_buflist) { 1334 /* Free the entry because it isn't valid */ 1335 drm_cleanup_buf_error(dev, entry); 1336 DRM_UNLOCK(dev); 1337 atomic_dec(&dev->buf_alloc); 1338 return -ENOMEM; 1339 } 1340 dma->buflist = temp_buflist; 1341 1342 for (i = 0; i < entry->buf_count; i++) { 1343 dma->buflist[i + dma->buf_count] = &entry->buflist[i]; 1344 } 1345 1346 dma->buf_count += entry->buf_count; 1347 dma->seg_count += entry->seg_count; 1348 dma->page_count += byte_count >> PAGE_SHIFT; 1349 dma->byte_count += byte_count; 1350 1351 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count); 1352 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count); 1353 1354 DRM_UNLOCK(dev); 1355 1356 request->count = entry->buf_count; 1357 request->size = size; 1358 1359 dma->flags = _DRM_DMA_USE_FB; 1360 1361 atomic_dec(&dev->buf_alloc); 1362 return 0; 1363 } 1364 1365 1366 /** 1367 * Add buffers for DMA transfers (ioctl). 1368 * 1369 * \param inode device inode. 1370 * \param file_priv DRM file private. 1371 * \param cmd command. 1372 * \param arg pointer to a struct drm_buf_desc request. 1373 * \return zero on success or a negative number on failure. 1374 * 1375 * According with the memory type specified in drm_buf_desc::flags and the 1376 * build options, it dispatches the call either to addbufs_agp(), 1377 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent 1378 * PCI memory respectively. 1379 */ 1380 int drm_addbufs(struct drm_device *dev, void *data, 1381 struct drm_file *file_priv) 1382 { 1383 struct drm_buf_desc *request = data; 1384 int ret; 1385 1386 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1387 return -EINVAL; 1388 1389 #if __OS_HAS_AGP 1390 if (request->flags & _DRM_AGP_BUFFER) 1391 ret = drm_addbufs_agp(dev, request); 1392 else 1393 #endif 1394 if (request->flags & _DRM_SG_BUFFER) 1395 ret = drm_addbufs_sg(dev, request); 1396 else if (request->flags & _DRM_FB_BUFFER) 1397 ret = drm_addbufs_fb(dev, request); 1398 else 1399 ret = drm_addbufs_pci(dev, request); 1400 1401 return ret; 1402 } 1403 1404 /** 1405 * Get information about the buffer mappings. 1406 * 1407 * This was originally mean for debugging purposes, or by a sophisticated 1408 * client library to determine how best to use the available buffers (e.g., 1409 * large buffers can be used for image transfer). 1410 * 1411 * \param inode device inode. 1412 * \param file_priv DRM file private. 1413 * \param cmd command. 1414 * \param arg pointer to a drm_buf_info structure. 1415 * \return zero on success or a negative number on failure. 1416 * 1417 * Increments drm_device::buf_use while holding the drm_device::count_lock 1418 * lock, preventing of allocating more buffers after this call. Information 1419 * about each requested buffer is then copied into user space. 1420 */ 1421 int drm_infobufs(struct drm_device *dev, void *data, 1422 struct drm_file *file_priv) 1423 { 1424 struct drm_device_dma *dma = dev->dma; 1425 struct drm_buf_info *request = data; 1426 int i; 1427 int count; 1428 1429 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1430 return -EINVAL; 1431 1432 if (!dma) 1433 return -EINVAL; 1434 1435 mtx_lock(&dev->count_lock); 1436 if (atomic_read(&dev->buf_alloc)) { 1437 mtx_unlock(&dev->count_lock); 1438 return -EBUSY; 1439 } 1440 ++dev->buf_use; /* Can't allocate more after this call */ 1441 mtx_unlock(&dev->count_lock); 1442 1443 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { 1444 if (dma->bufs[i].buf_count) 1445 ++count; 1446 } 1447 1448 DRM_DEBUG("count = %d\n", count); 1449 1450 if (request->count >= count) { 1451 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) { 1452 if (dma->bufs[i].buf_count) { 1453 struct drm_buf_desc __user *to = 1454 &request->list[count]; 1455 struct drm_buf_entry *from = &dma->bufs[i]; 1456 struct drm_freelist *list = &dma->bufs[i].freelist; 1457 if (copy_to_user(&to->count, 1458 &from->buf_count, 1459 sizeof(from->buf_count)) || 1460 copy_to_user(&to->size, 1461 &from->buf_size, 1462 sizeof(from->buf_size)) || 1463 copy_to_user(&to->low_mark, 1464 &list->low_mark, 1465 sizeof(list->low_mark)) || 1466 copy_to_user(&to->high_mark, 1467 &list->high_mark, 1468 sizeof(list->high_mark))) 1469 return -EFAULT; 1470 1471 DRM_DEBUG("%d %d %d %d %d\n", 1472 i, 1473 dma->bufs[i].buf_count, 1474 dma->bufs[i].buf_size, 1475 dma->bufs[i].freelist.low_mark, 1476 dma->bufs[i].freelist.high_mark); 1477 ++count; 1478 } 1479 } 1480 } 1481 request->count = count; 1482 1483 return 0; 1484 } 1485 1486 /** 1487 * Specifies a low and high water mark for buffer allocation 1488 * 1489 * \param inode device inode. 1490 * \param file_priv DRM file private. 1491 * \param cmd command. 1492 * \param arg a pointer to a drm_buf_desc structure. 1493 * \return zero on success or a negative number on failure. 1494 * 1495 * Verifies that the size order is bounded between the admissible orders and 1496 * updates the respective drm_device_dma::bufs entry low and high water mark. 1497 * 1498 * \note This ioctl is deprecated and mostly never used. 1499 */ 1500 int drm_markbufs(struct drm_device *dev, void *data, 1501 struct drm_file *file_priv) 1502 { 1503 struct drm_device_dma *dma = dev->dma; 1504 struct drm_buf_desc *request = data; 1505 int order; 1506 struct drm_buf_entry *entry; 1507 1508 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1509 return -EINVAL; 1510 1511 if (!dma) 1512 return -EINVAL; 1513 1514 DRM_DEBUG("%d, %d, %d\n", 1515 request->size, request->low_mark, request->high_mark); 1516 order = drm_order(request->size); 1517 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) 1518 return -EINVAL; 1519 entry = &dma->bufs[order]; 1520 1521 if (request->low_mark < 0 || request->low_mark > entry->buf_count) 1522 return -EINVAL; 1523 if (request->high_mark < 0 || request->high_mark > entry->buf_count) 1524 return -EINVAL; 1525 1526 entry->freelist.low_mark = request->low_mark; 1527 entry->freelist.high_mark = request->high_mark; 1528 1529 return 0; 1530 } 1531 1532 /** 1533 * Unreserve the buffers in list, previously reserved using drmDMA. 1534 * 1535 * \param inode device inode. 1536 * \param file_priv DRM file private. 1537 * \param cmd command. 1538 * \param arg pointer to a drm_buf_free structure. 1539 * \return zero on success or a negative number on failure. 1540 * 1541 * Calls free_buffer() for each used buffer. 1542 * This function is primarily used for debugging. 1543 */ 1544 int drm_freebufs(struct drm_device *dev, void *data, 1545 struct drm_file *file_priv) 1546 { 1547 struct drm_device_dma *dma = dev->dma; 1548 struct drm_buf_free *request = data; 1549 int i; 1550 int idx; 1551 struct drm_buf *buf; 1552 1553 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1554 return -EINVAL; 1555 1556 if (!dma) 1557 return -EINVAL; 1558 1559 DRM_DEBUG("%d\n", request->count); 1560 for (i = 0; i < request->count; i++) { 1561 if (copy_from_user(&idx, &request->list[i], sizeof(idx))) 1562 return -EFAULT; 1563 if (idx < 0 || idx >= dma->buf_count) { 1564 DRM_ERROR("Index %d (of %d max)\n", 1565 idx, dma->buf_count - 1); 1566 return -EINVAL; 1567 } 1568 buf = dma->buflist[idx]; 1569 if (buf->file_priv != file_priv) { 1570 DRM_ERROR("Process %d freeing buffer not owned\n", 1571 DRM_CURRENTPID); 1572 return -EINVAL; 1573 } 1574 drm_free_buffer(dev, buf); 1575 } 1576 1577 return 0; 1578 } 1579 1580 /** 1581 * Maps all of the DMA buffers into client-virtual space (ioctl). 1582 * 1583 * \param inode device inode. 1584 * \param file_priv DRM file private. 1585 * \param cmd command. 1586 * \param arg pointer to a drm_buf_map structure. 1587 * \return zero on success or a negative number on failure. 1588 * 1589 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information 1590 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with 1591 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls 1592 * drm_mmap_dma(). 1593 */ 1594 int drm_mapbufs(struct drm_device *dev, void *data, 1595 struct drm_file *file_priv) 1596 { 1597 struct drm_device_dma *dma = dev->dma; 1598 int retcode = 0; 1599 const int zero = 0; 1600 vm_offset_t virtual; 1601 vm_offset_t address; 1602 struct vmspace *vms; 1603 struct drm_buf_map *request = data; 1604 int i; 1605 1606 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA)) 1607 return -EINVAL; 1608 1609 if (!dma) 1610 return -EINVAL; 1611 1612 mtx_lock(&dev->count_lock); 1613 if (atomic_read(&dev->buf_alloc)) { 1614 mtx_unlock(&dev->count_lock); 1615 return -EBUSY; 1616 } 1617 dev->buf_use++; /* Can't allocate more after this call */ 1618 mtx_unlock(&dev->count_lock); 1619 1620 vms = DRM_CURPROC->td_proc->p_vmspace; 1621 1622 if (request->count >= dma->buf_count) { 1623 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) 1624 || (drm_core_check_feature(dev, DRIVER_SG) 1625 && (dma->flags & _DRM_DMA_USE_SG)) 1626 || (drm_core_check_feature(dev, DRIVER_FB_DMA) 1627 && (dma->flags & _DRM_DMA_USE_FB))) { 1628 struct drm_local_map *map = dev->agp_buffer_map; 1629 vm_ooffset_t token = dev->agp_buffer_token; 1630 1631 if (!map) { 1632 retcode = -EINVAL; 1633 goto done; 1634 } 1635 retcode = vm_mmap(&vms->vm_map, &virtual, map->size, 1636 VM_PROT_RW, VM_PROT_RW, MAP_SHARED | MAP_NOSYNC, 1637 OBJT_DEVICE, file_priv->minor->device, token); 1638 } else { 1639 retcode = vm_mmap(&vms->vm_map, &virtual, dma->byte_count, 1640 VM_PROT_RW, VM_PROT_RW, MAP_SHARED | MAP_NOSYNC, 1641 OBJT_DEVICE, file_priv->minor->device, 0); 1642 } 1643 if (retcode) { 1644 /* Real error */ 1645 retcode = -retcode; 1646 goto done; 1647 } 1648 request->virtual = (void __user *)virtual; 1649 1650 for (i = 0; i < dma->buf_count; i++) { 1651 if (copy_to_user(&request->list[i].idx, 1652 &dma->buflist[i]->idx, 1653 sizeof(request->list[0].idx))) { 1654 retcode = -EFAULT; 1655 goto done; 1656 } 1657 if (copy_to_user(&request->list[i].total, 1658 &dma->buflist[i]->total, 1659 sizeof(request->list[0].total))) { 1660 retcode = -EFAULT; 1661 goto done; 1662 } 1663 if (copy_to_user(&request->list[i].used, 1664 &zero, sizeof(zero))) { 1665 retcode = -EFAULT; 1666 goto done; 1667 } 1668 address = virtual + dma->buflist[i]->offset; /* *** */ 1669 if (copy_to_user(&request->list[i].address, 1670 &address, sizeof(address))) { 1671 retcode = -EFAULT; 1672 goto done; 1673 } 1674 } 1675 } 1676 done: 1677 request->count = dma->buf_count; 1678 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode); 1679 1680 return retcode; 1681 } 1682 1683 /** 1684 * Compute size order. Returns the exponent of the smaller power of two which 1685 * is greater or equal to given number. 1686 * 1687 * \param size size. 1688 * \return order. 1689 * 1690 * \todo Can be made faster. 1691 */ 1692 int drm_order(unsigned long size) 1693 { 1694 int order; 1695 unsigned long tmp; 1696 1697 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ; 1698 1699 if (size & (size - 1)) 1700 ++order; 1701 1702 return order; 1703 } 1704 EXPORT_SYMBOL(drm_order); 1705