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