1 /* 2 * drivers/gpu/drm/omapdrm/omap_gem.c 3 * 4 * Copyright (C) 2011 Texas Instruments 5 * Author: Rob Clark <rob.clark@linaro.org> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 as published by 9 * the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include <linux/seq_file.h> 21 #include <linux/shmem_fs.h> 22 #include <linux/spinlock.h> 23 #include <linux/pfn_t.h> 24 25 #include <drm/drm_vma_manager.h> 26 27 #include "omap_drv.h" 28 #include "omap_dmm_tiler.h" 29 30 /* 31 * GEM buffer object implementation. 32 */ 33 34 /* note: we use upper 8 bits of flags for driver-internal flags: */ 35 #define OMAP_BO_MEM_DMA_API 0x01000000 /* memory allocated with the dma_alloc_* API */ 36 #define OMAP_BO_MEM_SHMEM 0x02000000 /* memory allocated through shmem backing */ 37 #define OMAP_BO_MEM_DMABUF 0x08000000 /* memory imported from a dmabuf */ 38 39 struct omap_gem_object { 40 struct drm_gem_object base; 41 42 struct list_head mm_list; 43 44 uint32_t flags; 45 46 /** width/height for tiled formats (rounded up to slot boundaries) */ 47 uint16_t width, height; 48 49 /** roll applied when mapping to DMM */ 50 uint32_t roll; 51 52 /** 53 * paddr contains the buffer DMA address. It is valid for 54 * 55 * - buffers allocated through the DMA mapping API (with the 56 * OMAP_BO_MEM_DMA_API flag set) 57 * 58 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set) 59 * if they are physically contiguous (when sgt->orig_nents == 1) 60 * 61 * - buffers mapped through the TILER when paddr_cnt is not zero, in 62 * which case the DMA address points to the TILER aperture 63 * 64 * Physically contiguous buffers have their DMA address equal to the 65 * physical address as we don't remap those buffers through the TILER. 66 * 67 * Buffers mapped to the TILER have their DMA address pointing to the 68 * TILER aperture. As TILER mappings are refcounted (through paddr_cnt) 69 * the DMA address must be accessed through omap_get_get_paddr() to 70 * ensure that the mapping won't disappear unexpectedly. References must 71 * be released with omap_gem_put_paddr(). 72 */ 73 dma_addr_t paddr; 74 75 /** 76 * # of users of paddr 77 */ 78 uint32_t paddr_cnt; 79 80 /** 81 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag 82 * is set and the sgt field is valid. 83 */ 84 struct sg_table *sgt; 85 86 /** 87 * tiler block used when buffer is remapped in DMM/TILER. 88 */ 89 struct tiler_block *block; 90 91 /** 92 * Array of backing pages, if allocated. Note that pages are never 93 * allocated for buffers originally allocated from contiguous memory 94 */ 95 struct page **pages; 96 97 /** addresses corresponding to pages in above array */ 98 dma_addr_t *addrs; 99 100 /** 101 * Virtual address, if mapped. 102 */ 103 void *vaddr; 104 105 /** 106 * sync-object allocated on demand (if needed) 107 * 108 * Per-buffer sync-object for tracking pending and completed hw/dma 109 * read and write operations. 110 */ 111 struct { 112 uint32_t write_pending; 113 uint32_t write_complete; 114 uint32_t read_pending; 115 uint32_t read_complete; 116 } *sync; 117 }; 118 119 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base) 120 121 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are 122 * not necessarily pinned in TILER all the time, and (b) when they are 123 * they are not necessarily page aligned, we reserve one or more small 124 * regions in each of the 2d containers to use as a user-GART where we 125 * can create a second page-aligned mapping of parts of the buffer 126 * being accessed from userspace. 127 * 128 * Note that we could optimize slightly when we know that multiple 129 * tiler containers are backed by the same PAT.. but I'll leave that 130 * for later.. 131 */ 132 #define NUM_USERGART_ENTRIES 2 133 struct omap_drm_usergart_entry { 134 struct tiler_block *block; /* the reserved tiler block */ 135 dma_addr_t paddr; 136 struct drm_gem_object *obj; /* the current pinned obj */ 137 pgoff_t obj_pgoff; /* page offset of obj currently 138 mapped in */ 139 }; 140 141 struct omap_drm_usergart { 142 struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES]; 143 int height; /* height in rows */ 144 int height_shift; /* ilog2(height in rows) */ 145 int slot_shift; /* ilog2(width per slot) */ 146 int stride_pfn; /* stride in pages */ 147 int last; /* index of last used entry */ 148 }; 149 150 /* ----------------------------------------------------------------------------- 151 * Helpers 152 */ 153 154 /** get mmap offset */ 155 static uint64_t mmap_offset(struct drm_gem_object *obj) 156 { 157 struct drm_device *dev = obj->dev; 158 int ret; 159 size_t size; 160 161 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 162 163 /* Make it mmapable */ 164 size = omap_gem_mmap_size(obj); 165 ret = drm_gem_create_mmap_offset_size(obj, size); 166 if (ret) { 167 dev_err(dev->dev, "could not allocate mmap offset\n"); 168 return 0; 169 } 170 171 return drm_vma_node_offset_addr(&obj->vma_node); 172 } 173 174 static bool is_contiguous(struct omap_gem_object *omap_obj) 175 { 176 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) 177 return true; 178 179 if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1) 180 return true; 181 182 return false; 183 } 184 185 /* ----------------------------------------------------------------------------- 186 * Eviction 187 */ 188 189 static void evict_entry(struct drm_gem_object *obj, 190 enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry) 191 { 192 struct omap_gem_object *omap_obj = to_omap_bo(obj); 193 struct omap_drm_private *priv = obj->dev->dev_private; 194 int n = priv->usergart[fmt].height; 195 size_t size = PAGE_SIZE * n; 196 loff_t off = mmap_offset(obj) + 197 (entry->obj_pgoff << PAGE_SHIFT); 198 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE); 199 200 if (m > 1) { 201 int i; 202 /* if stride > than PAGE_SIZE then sparse mapping: */ 203 for (i = n; i > 0; i--) { 204 unmap_mapping_range(obj->dev->anon_inode->i_mapping, 205 off, PAGE_SIZE, 1); 206 off += PAGE_SIZE * m; 207 } 208 } else { 209 unmap_mapping_range(obj->dev->anon_inode->i_mapping, 210 off, size, 1); 211 } 212 213 entry->obj = NULL; 214 } 215 216 /* Evict a buffer from usergart, if it is mapped there */ 217 static void evict(struct drm_gem_object *obj) 218 { 219 struct omap_gem_object *omap_obj = to_omap_bo(obj); 220 struct omap_drm_private *priv = obj->dev->dev_private; 221 222 if (omap_obj->flags & OMAP_BO_TILED) { 223 enum tiler_fmt fmt = gem2fmt(omap_obj->flags); 224 int i; 225 226 for (i = 0; i < NUM_USERGART_ENTRIES; i++) { 227 struct omap_drm_usergart_entry *entry = 228 &priv->usergart[fmt].entry[i]; 229 230 if (entry->obj == obj) 231 evict_entry(obj, fmt, entry); 232 } 233 } 234 } 235 236 /* ----------------------------------------------------------------------------- 237 * Page Management 238 */ 239 240 /** ensure backing pages are allocated */ 241 static int omap_gem_attach_pages(struct drm_gem_object *obj) 242 { 243 struct drm_device *dev = obj->dev; 244 struct omap_gem_object *omap_obj = to_omap_bo(obj); 245 struct page **pages; 246 int npages = obj->size >> PAGE_SHIFT; 247 int i, ret; 248 dma_addr_t *addrs; 249 250 WARN_ON(omap_obj->pages); 251 252 pages = drm_gem_get_pages(obj); 253 if (IS_ERR(pages)) { 254 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages)); 255 return PTR_ERR(pages); 256 } 257 258 /* for non-cached buffers, ensure the new pages are clean because 259 * DSS, GPU, etc. are not cache coherent: 260 */ 261 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) { 262 addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL); 263 if (!addrs) { 264 ret = -ENOMEM; 265 goto free_pages; 266 } 267 268 for (i = 0; i < npages; i++) { 269 addrs[i] = dma_map_page(dev->dev, pages[i], 270 0, PAGE_SIZE, DMA_BIDIRECTIONAL); 271 272 if (dma_mapping_error(dev->dev, addrs[i])) { 273 dev_warn(dev->dev, 274 "%s: failed to map page\n", __func__); 275 276 for (i = i - 1; i >= 0; --i) { 277 dma_unmap_page(dev->dev, addrs[i], 278 PAGE_SIZE, DMA_BIDIRECTIONAL); 279 } 280 281 ret = -ENOMEM; 282 goto free_addrs; 283 } 284 } 285 } else { 286 addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL); 287 if (!addrs) { 288 ret = -ENOMEM; 289 goto free_pages; 290 } 291 } 292 293 omap_obj->addrs = addrs; 294 omap_obj->pages = pages; 295 296 return 0; 297 298 free_addrs: 299 kfree(addrs); 300 free_pages: 301 drm_gem_put_pages(obj, pages, true, false); 302 303 return ret; 304 } 305 306 /* acquire pages when needed (for example, for DMA where physically 307 * contiguous buffer is not required 308 */ 309 static int get_pages(struct drm_gem_object *obj, struct page ***pages) 310 { 311 struct omap_gem_object *omap_obj = to_omap_bo(obj); 312 int ret = 0; 313 314 if ((omap_obj->flags & OMAP_BO_MEM_SHMEM) && !omap_obj->pages) { 315 ret = omap_gem_attach_pages(obj); 316 if (ret) { 317 dev_err(obj->dev->dev, "could not attach pages\n"); 318 return ret; 319 } 320 } 321 322 /* TODO: even phys-contig.. we should have a list of pages? */ 323 *pages = omap_obj->pages; 324 325 return 0; 326 } 327 328 /** release backing pages */ 329 static void omap_gem_detach_pages(struct drm_gem_object *obj) 330 { 331 struct omap_gem_object *omap_obj = to_omap_bo(obj); 332 333 /* for non-cached buffers, ensure the new pages are clean because 334 * DSS, GPU, etc. are not cache coherent: 335 */ 336 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) { 337 int i, npages = obj->size >> PAGE_SHIFT; 338 for (i = 0; i < npages; i++) { 339 dma_unmap_page(obj->dev->dev, omap_obj->addrs[i], 340 PAGE_SIZE, DMA_BIDIRECTIONAL); 341 } 342 } 343 344 kfree(omap_obj->addrs); 345 omap_obj->addrs = NULL; 346 347 drm_gem_put_pages(obj, omap_obj->pages, true, false); 348 omap_obj->pages = NULL; 349 } 350 351 /* get buffer flags */ 352 uint32_t omap_gem_flags(struct drm_gem_object *obj) 353 { 354 return to_omap_bo(obj)->flags; 355 } 356 357 uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj) 358 { 359 uint64_t offset; 360 mutex_lock(&obj->dev->struct_mutex); 361 offset = mmap_offset(obj); 362 mutex_unlock(&obj->dev->struct_mutex); 363 return offset; 364 } 365 366 /** get mmap size */ 367 size_t omap_gem_mmap_size(struct drm_gem_object *obj) 368 { 369 struct omap_gem_object *omap_obj = to_omap_bo(obj); 370 size_t size = obj->size; 371 372 if (omap_obj->flags & OMAP_BO_TILED) { 373 /* for tiled buffers, the virtual size has stride rounded up 374 * to 4kb.. (to hide the fact that row n+1 might start 16kb or 375 * 32kb later!). But we don't back the entire buffer with 376 * pages, only the valid picture part.. so need to adjust for 377 * this in the size used to mmap and generate mmap offset 378 */ 379 size = tiler_vsize(gem2fmt(omap_obj->flags), 380 omap_obj->width, omap_obj->height); 381 } 382 383 return size; 384 } 385 386 /* get tiled size, returns -EINVAL if not tiled buffer */ 387 int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h) 388 { 389 struct omap_gem_object *omap_obj = to_omap_bo(obj); 390 if (omap_obj->flags & OMAP_BO_TILED) { 391 *w = omap_obj->width; 392 *h = omap_obj->height; 393 return 0; 394 } 395 return -EINVAL; 396 } 397 398 /* ----------------------------------------------------------------------------- 399 * Fault Handling 400 */ 401 402 /* Normal handling for the case of faulting in non-tiled buffers */ 403 static int fault_1d(struct drm_gem_object *obj, 404 struct vm_area_struct *vma, struct vm_fault *vmf) 405 { 406 struct omap_gem_object *omap_obj = to_omap_bo(obj); 407 unsigned long pfn; 408 pgoff_t pgoff; 409 410 /* We don't use vmf->pgoff since that has the fake offset: */ 411 pgoff = ((unsigned long)vmf->virtual_address - 412 vma->vm_start) >> PAGE_SHIFT; 413 414 if (omap_obj->pages) { 415 omap_gem_cpu_sync(obj, pgoff); 416 pfn = page_to_pfn(omap_obj->pages[pgoff]); 417 } else { 418 BUG_ON(!is_contiguous(omap_obj)); 419 pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff; 420 } 421 422 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, 423 pfn, pfn << PAGE_SHIFT); 424 425 return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, 426 __pfn_to_pfn_t(pfn, PFN_DEV)); 427 } 428 429 /* Special handling for the case of faulting in 2d tiled buffers */ 430 static int fault_2d(struct drm_gem_object *obj, 431 struct vm_area_struct *vma, struct vm_fault *vmf) 432 { 433 struct omap_gem_object *omap_obj = to_omap_bo(obj); 434 struct omap_drm_private *priv = obj->dev->dev_private; 435 struct omap_drm_usergart_entry *entry; 436 enum tiler_fmt fmt = gem2fmt(omap_obj->flags); 437 struct page *pages[64]; /* XXX is this too much to have on stack? */ 438 unsigned long pfn; 439 pgoff_t pgoff, base_pgoff; 440 void __user *vaddr; 441 int i, ret, slots; 442 443 /* 444 * Note the height of the slot is also equal to the number of pages 445 * that need to be mapped in to fill 4kb wide CPU page. If the slot 446 * height is 64, then 64 pages fill a 4kb wide by 64 row region. 447 */ 448 const int n = priv->usergart[fmt].height; 449 const int n_shift = priv->usergart[fmt].height_shift; 450 451 /* 452 * If buffer width in bytes > PAGE_SIZE then the virtual stride is 453 * rounded up to next multiple of PAGE_SIZE.. this need to be taken 454 * into account in some of the math, so figure out virtual stride 455 * in pages 456 */ 457 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE); 458 459 /* We don't use vmf->pgoff since that has the fake offset: */ 460 pgoff = ((unsigned long)vmf->virtual_address - 461 vma->vm_start) >> PAGE_SHIFT; 462 463 /* 464 * Actual address we start mapping at is rounded down to previous slot 465 * boundary in the y direction: 466 */ 467 base_pgoff = round_down(pgoff, m << n_shift); 468 469 /* figure out buffer width in slots */ 470 slots = omap_obj->width >> priv->usergart[fmt].slot_shift; 471 472 vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT); 473 474 entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last]; 475 476 /* evict previous buffer using this usergart entry, if any: */ 477 if (entry->obj) 478 evict_entry(entry->obj, fmt, entry); 479 480 entry->obj = obj; 481 entry->obj_pgoff = base_pgoff; 482 483 /* now convert base_pgoff to phys offset from virt offset: */ 484 base_pgoff = (base_pgoff >> n_shift) * slots; 485 486 /* for wider-than 4k.. figure out which part of the slot-row we want: */ 487 if (m > 1) { 488 int off = pgoff % m; 489 entry->obj_pgoff += off; 490 base_pgoff /= m; 491 slots = min(slots - (off << n_shift), n); 492 base_pgoff += off << n_shift; 493 vaddr += off << PAGE_SHIFT; 494 } 495 496 /* 497 * Map in pages. Beyond the valid pixel part of the buffer, we set 498 * pages[i] to NULL to get a dummy page mapped in.. if someone 499 * reads/writes it they will get random/undefined content, but at 500 * least it won't be corrupting whatever other random page used to 501 * be mapped in, or other undefined behavior. 502 */ 503 memcpy(pages, &omap_obj->pages[base_pgoff], 504 sizeof(struct page *) * slots); 505 memset(pages + slots, 0, 506 sizeof(struct page *) * (n - slots)); 507 508 ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true); 509 if (ret) { 510 dev_err(obj->dev->dev, "failed to pin: %d\n", ret); 511 return ret; 512 } 513 514 pfn = entry->paddr >> PAGE_SHIFT; 515 516 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, 517 pfn, pfn << PAGE_SHIFT); 518 519 for (i = n; i > 0; i--) { 520 vm_insert_mixed(vma, (unsigned long)vaddr, 521 __pfn_to_pfn_t(pfn, PFN_DEV)); 522 pfn += priv->usergart[fmt].stride_pfn; 523 vaddr += PAGE_SIZE * m; 524 } 525 526 /* simple round-robin: */ 527 priv->usergart[fmt].last = (priv->usergart[fmt].last + 1) 528 % NUM_USERGART_ENTRIES; 529 530 return 0; 531 } 532 533 /** 534 * omap_gem_fault - pagefault handler for GEM objects 535 * @vma: the VMA of the GEM object 536 * @vmf: fault detail 537 * 538 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM 539 * does most of the work for us including the actual map/unmap calls 540 * but we need to do the actual page work. 541 * 542 * The VMA was set up by GEM. In doing so it also ensured that the 543 * vma->vm_private_data points to the GEM object that is backing this 544 * mapping. 545 */ 546 int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 547 { 548 struct drm_gem_object *obj = vma->vm_private_data; 549 struct omap_gem_object *omap_obj = to_omap_bo(obj); 550 struct drm_device *dev = obj->dev; 551 struct page **pages; 552 int ret; 553 554 /* Make sure we don't parallel update on a fault, nor move or remove 555 * something from beneath our feet 556 */ 557 mutex_lock(&dev->struct_mutex); 558 559 /* if a shmem backed object, make sure we have pages attached now */ 560 ret = get_pages(obj, &pages); 561 if (ret) 562 goto fail; 563 564 /* where should we do corresponding put_pages().. we are mapping 565 * the original page, rather than thru a GART, so we can't rely 566 * on eviction to trigger this. But munmap() or all mappings should 567 * probably trigger put_pages()? 568 */ 569 570 if (omap_obj->flags & OMAP_BO_TILED) 571 ret = fault_2d(obj, vma, vmf); 572 else 573 ret = fault_1d(obj, vma, vmf); 574 575 576 fail: 577 mutex_unlock(&dev->struct_mutex); 578 switch (ret) { 579 case 0: 580 case -ERESTARTSYS: 581 case -EINTR: 582 case -EBUSY: 583 /* 584 * EBUSY is ok: this just means that another thread 585 * already did the job. 586 */ 587 return VM_FAULT_NOPAGE; 588 case -ENOMEM: 589 return VM_FAULT_OOM; 590 default: 591 return VM_FAULT_SIGBUS; 592 } 593 } 594 595 /** We override mainly to fix up some of the vm mapping flags.. */ 596 int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma) 597 { 598 int ret; 599 600 ret = drm_gem_mmap(filp, vma); 601 if (ret) { 602 DBG("mmap failed: %d", ret); 603 return ret; 604 } 605 606 return omap_gem_mmap_obj(vma->vm_private_data, vma); 607 } 608 609 int omap_gem_mmap_obj(struct drm_gem_object *obj, 610 struct vm_area_struct *vma) 611 { 612 struct omap_gem_object *omap_obj = to_omap_bo(obj); 613 614 vma->vm_flags &= ~VM_PFNMAP; 615 vma->vm_flags |= VM_MIXEDMAP; 616 617 if (omap_obj->flags & OMAP_BO_WC) { 618 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 619 } else if (omap_obj->flags & OMAP_BO_UNCACHED) { 620 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); 621 } else { 622 /* 623 * We do have some private objects, at least for scanout buffers 624 * on hardware without DMM/TILER. But these are allocated write- 625 * combine 626 */ 627 if (WARN_ON(!obj->filp)) 628 return -EINVAL; 629 630 /* 631 * Shunt off cached objs to shmem file so they have their own 632 * address_space (so unmap_mapping_range does what we want, 633 * in particular in the case of mmap'd dmabufs) 634 */ 635 fput(vma->vm_file); 636 vma->vm_pgoff = 0; 637 vma->vm_file = get_file(obj->filp); 638 639 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 640 } 641 642 return 0; 643 } 644 645 /* ----------------------------------------------------------------------------- 646 * Dumb Buffers 647 */ 648 649 /** 650 * omap_gem_dumb_create - create a dumb buffer 651 * @drm_file: our client file 652 * @dev: our device 653 * @args: the requested arguments copied from userspace 654 * 655 * Allocate a buffer suitable for use for a frame buffer of the 656 * form described by user space. Give userspace a handle by which 657 * to reference it. 658 */ 659 int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev, 660 struct drm_mode_create_dumb *args) 661 { 662 union omap_gem_size gsize; 663 664 args->pitch = align_pitch(0, args->width, args->bpp); 665 args->size = PAGE_ALIGN(args->pitch * args->height); 666 667 gsize = (union omap_gem_size){ 668 .bytes = args->size, 669 }; 670 671 return omap_gem_new_handle(dev, file, gsize, 672 OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle); 673 } 674 675 /** 676 * omap_gem_dumb_map - buffer mapping for dumb interface 677 * @file: our drm client file 678 * @dev: drm device 679 * @handle: GEM handle to the object (from dumb_create) 680 * 681 * Do the necessary setup to allow the mapping of the frame buffer 682 * into user memory. We don't have to do much here at the moment. 683 */ 684 int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 685 uint32_t handle, uint64_t *offset) 686 { 687 struct drm_gem_object *obj; 688 int ret = 0; 689 690 /* GEM does all our handle to object mapping */ 691 obj = drm_gem_object_lookup(file, handle); 692 if (obj == NULL) { 693 ret = -ENOENT; 694 goto fail; 695 } 696 697 *offset = omap_gem_mmap_offset(obj); 698 699 drm_gem_object_unreference_unlocked(obj); 700 701 fail: 702 return ret; 703 } 704 705 #ifdef CONFIG_DRM_FBDEV_EMULATION 706 /* Set scrolling position. This allows us to implement fast scrolling 707 * for console. 708 * 709 * Call only from non-atomic contexts. 710 */ 711 int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll) 712 { 713 struct omap_gem_object *omap_obj = to_omap_bo(obj); 714 uint32_t npages = obj->size >> PAGE_SHIFT; 715 int ret = 0; 716 717 if (roll > npages) { 718 dev_err(obj->dev->dev, "invalid roll: %d\n", roll); 719 return -EINVAL; 720 } 721 722 omap_obj->roll = roll; 723 724 mutex_lock(&obj->dev->struct_mutex); 725 726 /* if we aren't mapped yet, we don't need to do anything */ 727 if (omap_obj->block) { 728 struct page **pages; 729 ret = get_pages(obj, &pages); 730 if (ret) 731 goto fail; 732 ret = tiler_pin(omap_obj->block, pages, npages, roll, true); 733 if (ret) 734 dev_err(obj->dev->dev, "could not repin: %d\n", ret); 735 } 736 737 fail: 738 mutex_unlock(&obj->dev->struct_mutex); 739 740 return ret; 741 } 742 #endif 743 744 /* ----------------------------------------------------------------------------- 745 * Memory Management & DMA Sync 746 */ 747 748 /** 749 * shmem buffers that are mapped cached can simulate coherency via using 750 * page faulting to keep track of dirty pages 751 */ 752 static inline bool is_cached_coherent(struct drm_gem_object *obj) 753 { 754 struct omap_gem_object *omap_obj = to_omap_bo(obj); 755 756 return (omap_obj->flags & OMAP_BO_MEM_SHMEM) && 757 ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED); 758 } 759 760 /* Sync the buffer for CPU access.. note pages should already be 761 * attached, ie. omap_gem_get_pages() 762 */ 763 void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff) 764 { 765 struct drm_device *dev = obj->dev; 766 struct omap_gem_object *omap_obj = to_omap_bo(obj); 767 768 if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) { 769 dma_unmap_page(dev->dev, omap_obj->addrs[pgoff], 770 PAGE_SIZE, DMA_BIDIRECTIONAL); 771 omap_obj->addrs[pgoff] = 0; 772 } 773 } 774 775 /* sync the buffer for DMA access */ 776 void omap_gem_dma_sync(struct drm_gem_object *obj, 777 enum dma_data_direction dir) 778 { 779 struct drm_device *dev = obj->dev; 780 struct omap_gem_object *omap_obj = to_omap_bo(obj); 781 782 if (is_cached_coherent(obj)) { 783 int i, npages = obj->size >> PAGE_SHIFT; 784 struct page **pages = omap_obj->pages; 785 bool dirty = false; 786 787 for (i = 0; i < npages; i++) { 788 if (!omap_obj->addrs[i]) { 789 dma_addr_t addr; 790 791 addr = dma_map_page(dev->dev, pages[i], 0, 792 PAGE_SIZE, DMA_BIDIRECTIONAL); 793 794 if (dma_mapping_error(dev->dev, addr)) { 795 dev_warn(dev->dev, 796 "%s: failed to map page\n", 797 __func__); 798 break; 799 } 800 801 dirty = true; 802 omap_obj->addrs[i] = addr; 803 } 804 } 805 806 if (dirty) { 807 unmap_mapping_range(obj->filp->f_mapping, 0, 808 omap_gem_mmap_size(obj), 1); 809 } 810 } 811 } 812 813 /* Get physical address for DMA.. if 'remap' is true, and the buffer is not 814 * already contiguous, remap it to pin in physically contiguous memory.. (ie. 815 * map in TILER) 816 */ 817 int omap_gem_get_paddr(struct drm_gem_object *obj, 818 dma_addr_t *paddr, bool remap) 819 { 820 struct omap_drm_private *priv = obj->dev->dev_private; 821 struct omap_gem_object *omap_obj = to_omap_bo(obj); 822 int ret = 0; 823 824 mutex_lock(&obj->dev->struct_mutex); 825 826 if (!is_contiguous(omap_obj) && remap && priv->has_dmm) { 827 if (omap_obj->paddr_cnt == 0) { 828 struct page **pages; 829 uint32_t npages = obj->size >> PAGE_SHIFT; 830 enum tiler_fmt fmt = gem2fmt(omap_obj->flags); 831 struct tiler_block *block; 832 833 BUG_ON(omap_obj->block); 834 835 ret = get_pages(obj, &pages); 836 if (ret) 837 goto fail; 838 839 if (omap_obj->flags & OMAP_BO_TILED) { 840 block = tiler_reserve_2d(fmt, 841 omap_obj->width, 842 omap_obj->height, 0); 843 } else { 844 block = tiler_reserve_1d(obj->size); 845 } 846 847 if (IS_ERR(block)) { 848 ret = PTR_ERR(block); 849 dev_err(obj->dev->dev, 850 "could not remap: %d (%d)\n", ret, fmt); 851 goto fail; 852 } 853 854 /* TODO: enable async refill.. */ 855 ret = tiler_pin(block, pages, npages, 856 omap_obj->roll, true); 857 if (ret) { 858 tiler_release(block); 859 dev_err(obj->dev->dev, 860 "could not pin: %d\n", ret); 861 goto fail; 862 } 863 864 omap_obj->paddr = tiler_ssptr(block); 865 omap_obj->block = block; 866 867 DBG("got paddr: %pad", &omap_obj->paddr); 868 } 869 870 omap_obj->paddr_cnt++; 871 872 *paddr = omap_obj->paddr; 873 } else if (is_contiguous(omap_obj)) { 874 *paddr = omap_obj->paddr; 875 } else { 876 ret = -EINVAL; 877 goto fail; 878 } 879 880 fail: 881 mutex_unlock(&obj->dev->struct_mutex); 882 883 return ret; 884 } 885 886 /* Release physical address, when DMA is no longer being performed.. this 887 * could potentially unpin and unmap buffers from TILER 888 */ 889 void omap_gem_put_paddr(struct drm_gem_object *obj) 890 { 891 struct omap_gem_object *omap_obj = to_omap_bo(obj); 892 int ret; 893 894 mutex_lock(&obj->dev->struct_mutex); 895 if (omap_obj->paddr_cnt > 0) { 896 omap_obj->paddr_cnt--; 897 if (omap_obj->paddr_cnt == 0) { 898 ret = tiler_unpin(omap_obj->block); 899 if (ret) { 900 dev_err(obj->dev->dev, 901 "could not unpin pages: %d\n", ret); 902 } 903 ret = tiler_release(omap_obj->block); 904 if (ret) { 905 dev_err(obj->dev->dev, 906 "could not release unmap: %d\n", ret); 907 } 908 omap_obj->paddr = 0; 909 omap_obj->block = NULL; 910 } 911 } 912 913 mutex_unlock(&obj->dev->struct_mutex); 914 } 915 916 /* Get rotated scanout address (only valid if already pinned), at the 917 * specified orientation and x,y offset from top-left corner of buffer 918 * (only valid for tiled 2d buffers) 919 */ 920 int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient, 921 int x, int y, dma_addr_t *paddr) 922 { 923 struct omap_gem_object *omap_obj = to_omap_bo(obj); 924 int ret = -EINVAL; 925 926 mutex_lock(&obj->dev->struct_mutex); 927 if ((omap_obj->paddr_cnt > 0) && omap_obj->block && 928 (omap_obj->flags & OMAP_BO_TILED)) { 929 *paddr = tiler_tsptr(omap_obj->block, orient, x, y); 930 ret = 0; 931 } 932 mutex_unlock(&obj->dev->struct_mutex); 933 return ret; 934 } 935 936 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */ 937 int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient) 938 { 939 struct omap_gem_object *omap_obj = to_omap_bo(obj); 940 int ret = -EINVAL; 941 if (omap_obj->flags & OMAP_BO_TILED) 942 ret = tiler_stride(gem2fmt(omap_obj->flags), orient); 943 return ret; 944 } 945 946 /* if !remap, and we don't have pages backing, then fail, rather than 947 * increasing the pin count (which we don't really do yet anyways, 948 * because we don't support swapping pages back out). And 'remap' 949 * might not be quite the right name, but I wanted to keep it working 950 * similarly to omap_gem_get_paddr(). Note though that mutex is not 951 * aquired if !remap (because this can be called in atomic ctxt), 952 * but probably omap_gem_get_paddr() should be changed to work in the 953 * same way. If !remap, a matching omap_gem_put_pages() call is not 954 * required (and should not be made). 955 */ 956 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages, 957 bool remap) 958 { 959 int ret; 960 if (!remap) { 961 struct omap_gem_object *omap_obj = to_omap_bo(obj); 962 if (!omap_obj->pages) 963 return -ENOMEM; 964 *pages = omap_obj->pages; 965 return 0; 966 } 967 mutex_lock(&obj->dev->struct_mutex); 968 ret = get_pages(obj, pages); 969 mutex_unlock(&obj->dev->struct_mutex); 970 return ret; 971 } 972 973 /* release pages when DMA no longer being performed */ 974 int omap_gem_put_pages(struct drm_gem_object *obj) 975 { 976 /* do something here if we dynamically attach/detach pages.. at 977 * least they would no longer need to be pinned if everyone has 978 * released the pages.. 979 */ 980 return 0; 981 } 982 983 #ifdef CONFIG_DRM_FBDEV_EMULATION 984 /* Get kernel virtual address for CPU access.. this more or less only 985 * exists for omap_fbdev. This should be called with struct_mutex 986 * held. 987 */ 988 void *omap_gem_vaddr(struct drm_gem_object *obj) 989 { 990 struct omap_gem_object *omap_obj = to_omap_bo(obj); 991 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 992 if (!omap_obj->vaddr) { 993 struct page **pages; 994 int ret = get_pages(obj, &pages); 995 if (ret) 996 return ERR_PTR(ret); 997 omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, 998 VM_MAP, pgprot_writecombine(PAGE_KERNEL)); 999 } 1000 return omap_obj->vaddr; 1001 } 1002 #endif 1003 1004 /* ----------------------------------------------------------------------------- 1005 * Power Management 1006 */ 1007 1008 #ifdef CONFIG_PM 1009 /* re-pin objects in DMM in resume path: */ 1010 int omap_gem_resume(struct device *dev) 1011 { 1012 struct drm_device *drm_dev = dev_get_drvdata(dev); 1013 struct omap_drm_private *priv = drm_dev->dev_private; 1014 struct omap_gem_object *omap_obj; 1015 int ret = 0; 1016 1017 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) { 1018 if (omap_obj->block) { 1019 struct drm_gem_object *obj = &omap_obj->base; 1020 uint32_t npages = obj->size >> PAGE_SHIFT; 1021 WARN_ON(!omap_obj->pages); /* this can't happen */ 1022 ret = tiler_pin(omap_obj->block, 1023 omap_obj->pages, npages, 1024 omap_obj->roll, true); 1025 if (ret) { 1026 dev_err(dev, "could not repin: %d\n", ret); 1027 return ret; 1028 } 1029 } 1030 } 1031 1032 return 0; 1033 } 1034 #endif 1035 1036 /* ----------------------------------------------------------------------------- 1037 * DebugFS 1038 */ 1039 1040 #ifdef CONFIG_DEBUG_FS 1041 void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m) 1042 { 1043 struct omap_gem_object *omap_obj = to_omap_bo(obj); 1044 uint64_t off; 1045 1046 off = drm_vma_node_start(&obj->vma_node); 1047 1048 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d", 1049 omap_obj->flags, obj->name, obj->refcount.refcount.counter, 1050 off, &omap_obj->paddr, omap_obj->paddr_cnt, 1051 omap_obj->vaddr, omap_obj->roll); 1052 1053 if (omap_obj->flags & OMAP_BO_TILED) { 1054 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height); 1055 if (omap_obj->block) { 1056 struct tcm_area *area = &omap_obj->block->area; 1057 seq_printf(m, " (%dx%d, %dx%d)", 1058 area->p0.x, area->p0.y, 1059 area->p1.x, area->p1.y); 1060 } 1061 } else { 1062 seq_printf(m, " %d", obj->size); 1063 } 1064 1065 seq_printf(m, "\n"); 1066 } 1067 1068 void omap_gem_describe_objects(struct list_head *list, struct seq_file *m) 1069 { 1070 struct omap_gem_object *omap_obj; 1071 int count = 0; 1072 size_t size = 0; 1073 1074 list_for_each_entry(omap_obj, list, mm_list) { 1075 struct drm_gem_object *obj = &omap_obj->base; 1076 seq_printf(m, " "); 1077 omap_gem_describe(obj, m); 1078 count++; 1079 size += obj->size; 1080 } 1081 1082 seq_printf(m, "Total %d objects, %zu bytes\n", count, size); 1083 } 1084 #endif 1085 1086 /* ----------------------------------------------------------------------------- 1087 * Buffer Synchronization 1088 */ 1089 1090 static DEFINE_SPINLOCK(sync_lock); 1091 1092 struct omap_gem_sync_waiter { 1093 struct list_head list; 1094 struct omap_gem_object *omap_obj; 1095 enum omap_gem_op op; 1096 uint32_t read_target, write_target; 1097 /* notify called w/ sync_lock held */ 1098 void (*notify)(void *arg); 1099 void *arg; 1100 }; 1101 1102 /* list of omap_gem_sync_waiter.. the notify fxn gets called back when 1103 * the read and/or write target count is achieved which can call a user 1104 * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for 1105 * cpu access), etc. 1106 */ 1107 static LIST_HEAD(waiters); 1108 1109 static inline bool is_waiting(struct omap_gem_sync_waiter *waiter) 1110 { 1111 struct omap_gem_object *omap_obj = waiter->omap_obj; 1112 if ((waiter->op & OMAP_GEM_READ) && 1113 (omap_obj->sync->write_complete < waiter->write_target)) 1114 return true; 1115 if ((waiter->op & OMAP_GEM_WRITE) && 1116 (omap_obj->sync->read_complete < waiter->read_target)) 1117 return true; 1118 return false; 1119 } 1120 1121 /* macro for sync debug.. */ 1122 #define SYNCDBG 0 1123 #define SYNC(fmt, ...) do { if (SYNCDBG) \ 1124 printk(KERN_ERR "%s:%d: "fmt"\n", \ 1125 __func__, __LINE__, ##__VA_ARGS__); \ 1126 } while (0) 1127 1128 1129 static void sync_op_update(void) 1130 { 1131 struct omap_gem_sync_waiter *waiter, *n; 1132 list_for_each_entry_safe(waiter, n, &waiters, list) { 1133 if (!is_waiting(waiter)) { 1134 list_del(&waiter->list); 1135 SYNC("notify: %p", waiter); 1136 waiter->notify(waiter->arg); 1137 kfree(waiter); 1138 } 1139 } 1140 } 1141 1142 static inline int sync_op(struct drm_gem_object *obj, 1143 enum omap_gem_op op, bool start) 1144 { 1145 struct omap_gem_object *omap_obj = to_omap_bo(obj); 1146 int ret = 0; 1147 1148 spin_lock(&sync_lock); 1149 1150 if (!omap_obj->sync) { 1151 omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC); 1152 if (!omap_obj->sync) { 1153 ret = -ENOMEM; 1154 goto unlock; 1155 } 1156 } 1157 1158 if (start) { 1159 if (op & OMAP_GEM_READ) 1160 omap_obj->sync->read_pending++; 1161 if (op & OMAP_GEM_WRITE) 1162 omap_obj->sync->write_pending++; 1163 } else { 1164 if (op & OMAP_GEM_READ) 1165 omap_obj->sync->read_complete++; 1166 if (op & OMAP_GEM_WRITE) 1167 omap_obj->sync->write_complete++; 1168 sync_op_update(); 1169 } 1170 1171 unlock: 1172 spin_unlock(&sync_lock); 1173 1174 return ret; 1175 } 1176 1177 /* mark the start of read and/or write operation */ 1178 int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op) 1179 { 1180 return sync_op(obj, op, true); 1181 } 1182 1183 int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op) 1184 { 1185 return sync_op(obj, op, false); 1186 } 1187 1188 static DECLARE_WAIT_QUEUE_HEAD(sync_event); 1189 1190 static void sync_notify(void *arg) 1191 { 1192 struct task_struct **waiter_task = arg; 1193 *waiter_task = NULL; 1194 wake_up_all(&sync_event); 1195 } 1196 1197 int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op) 1198 { 1199 struct omap_gem_object *omap_obj = to_omap_bo(obj); 1200 int ret = 0; 1201 if (omap_obj->sync) { 1202 struct task_struct *waiter_task = current; 1203 struct omap_gem_sync_waiter *waiter = 1204 kzalloc(sizeof(*waiter), GFP_KERNEL); 1205 1206 if (!waiter) 1207 return -ENOMEM; 1208 1209 waiter->omap_obj = omap_obj; 1210 waiter->op = op; 1211 waiter->read_target = omap_obj->sync->read_pending; 1212 waiter->write_target = omap_obj->sync->write_pending; 1213 waiter->notify = sync_notify; 1214 waiter->arg = &waiter_task; 1215 1216 spin_lock(&sync_lock); 1217 if (is_waiting(waiter)) { 1218 SYNC("waited: %p", waiter); 1219 list_add_tail(&waiter->list, &waiters); 1220 spin_unlock(&sync_lock); 1221 ret = wait_event_interruptible(sync_event, 1222 (waiter_task == NULL)); 1223 spin_lock(&sync_lock); 1224 if (waiter_task) { 1225 SYNC("interrupted: %p", waiter); 1226 /* we were interrupted */ 1227 list_del(&waiter->list); 1228 waiter_task = NULL; 1229 } else { 1230 /* freed in sync_op_update() */ 1231 waiter = NULL; 1232 } 1233 } 1234 spin_unlock(&sync_lock); 1235 kfree(waiter); 1236 } 1237 return ret; 1238 } 1239 1240 /* call fxn(arg), either synchronously or asynchronously if the op 1241 * is currently blocked.. fxn() can be called from any context 1242 * 1243 * (TODO for now fxn is called back from whichever context calls 1244 * omap_gem_op_finish().. but this could be better defined later 1245 * if needed) 1246 * 1247 * TODO more code in common w/ _sync().. 1248 */ 1249 int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op, 1250 void (*fxn)(void *arg), void *arg) 1251 { 1252 struct omap_gem_object *omap_obj = to_omap_bo(obj); 1253 if (omap_obj->sync) { 1254 struct omap_gem_sync_waiter *waiter = 1255 kzalloc(sizeof(*waiter), GFP_ATOMIC); 1256 1257 if (!waiter) 1258 return -ENOMEM; 1259 1260 waiter->omap_obj = omap_obj; 1261 waiter->op = op; 1262 waiter->read_target = omap_obj->sync->read_pending; 1263 waiter->write_target = omap_obj->sync->write_pending; 1264 waiter->notify = fxn; 1265 waiter->arg = arg; 1266 1267 spin_lock(&sync_lock); 1268 if (is_waiting(waiter)) { 1269 SYNC("waited: %p", waiter); 1270 list_add_tail(&waiter->list, &waiters); 1271 spin_unlock(&sync_lock); 1272 return 0; 1273 } 1274 1275 spin_unlock(&sync_lock); 1276 1277 kfree(waiter); 1278 } 1279 1280 /* no waiting.. */ 1281 fxn(arg); 1282 1283 return 0; 1284 } 1285 1286 /* ----------------------------------------------------------------------------- 1287 * Constructor & Destructor 1288 */ 1289 1290 void omap_gem_free_object(struct drm_gem_object *obj) 1291 { 1292 struct drm_device *dev = obj->dev; 1293 struct omap_drm_private *priv = dev->dev_private; 1294 struct omap_gem_object *omap_obj = to_omap_bo(obj); 1295 1296 evict(obj); 1297 1298 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 1299 1300 spin_lock(&priv->list_lock); 1301 list_del(&omap_obj->mm_list); 1302 spin_unlock(&priv->list_lock); 1303 1304 /* this means the object is still pinned.. which really should 1305 * not happen. I think.. 1306 */ 1307 WARN_ON(omap_obj->paddr_cnt > 0); 1308 1309 if (omap_obj->pages) { 1310 if (omap_obj->flags & OMAP_BO_MEM_DMABUF) 1311 kfree(omap_obj->pages); 1312 else 1313 omap_gem_detach_pages(obj); 1314 } 1315 1316 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) { 1317 dma_free_wc(dev->dev, obj->size, omap_obj->vaddr, 1318 omap_obj->paddr); 1319 } else if (omap_obj->vaddr) { 1320 vunmap(omap_obj->vaddr); 1321 } else if (obj->import_attach) { 1322 drm_prime_gem_destroy(obj, omap_obj->sgt); 1323 } 1324 1325 kfree(omap_obj->sync); 1326 1327 drm_gem_object_release(obj); 1328 1329 kfree(omap_obj); 1330 } 1331 1332 /* GEM buffer object constructor */ 1333 struct drm_gem_object *omap_gem_new(struct drm_device *dev, 1334 union omap_gem_size gsize, uint32_t flags) 1335 { 1336 struct omap_drm_private *priv = dev->dev_private; 1337 struct omap_gem_object *omap_obj; 1338 struct drm_gem_object *obj; 1339 struct address_space *mapping; 1340 size_t size; 1341 int ret; 1342 1343 /* Validate the flags and compute the memory and cache flags. */ 1344 if (flags & OMAP_BO_TILED) { 1345 if (!priv->usergart) { 1346 dev_err(dev->dev, "Tiled buffers require DMM\n"); 1347 return NULL; 1348 } 1349 1350 /* 1351 * Tiled buffers are always shmem paged backed. When they are 1352 * scanned out, they are remapped into DMM/TILER. 1353 */ 1354 flags &= ~OMAP_BO_SCANOUT; 1355 flags |= OMAP_BO_MEM_SHMEM; 1356 1357 /* 1358 * Currently don't allow cached buffers. There is some caching 1359 * stuff that needs to be handled better. 1360 */ 1361 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED); 1362 flags |= tiler_get_cpu_cache_flags(); 1363 } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) { 1364 /* 1365 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be 1366 * tiled. However, to lower the pressure on memory allocation, 1367 * use contiguous memory only if no TILER is available. 1368 */ 1369 flags |= OMAP_BO_MEM_DMA_API; 1370 } else if (!(flags & OMAP_BO_MEM_DMABUF)) { 1371 /* 1372 * All other buffers not backed by dma_buf are shmem-backed. 1373 */ 1374 flags |= OMAP_BO_MEM_SHMEM; 1375 } 1376 1377 /* Allocate the initialize the OMAP GEM object. */ 1378 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL); 1379 if (!omap_obj) 1380 return NULL; 1381 1382 obj = &omap_obj->base; 1383 omap_obj->flags = flags; 1384 1385 if (flags & OMAP_BO_TILED) { 1386 /* 1387 * For tiled buffers align dimensions to slot boundaries and 1388 * calculate size based on aligned dimensions. 1389 */ 1390 tiler_align(gem2fmt(flags), &gsize.tiled.width, 1391 &gsize.tiled.height); 1392 1393 size = tiler_size(gem2fmt(flags), gsize.tiled.width, 1394 gsize.tiled.height); 1395 1396 omap_obj->width = gsize.tiled.width; 1397 omap_obj->height = gsize.tiled.height; 1398 } else { 1399 size = PAGE_ALIGN(gsize.bytes); 1400 } 1401 1402 /* Initialize the GEM object. */ 1403 if (!(flags & OMAP_BO_MEM_SHMEM)) { 1404 drm_gem_private_object_init(dev, obj, size); 1405 } else { 1406 ret = drm_gem_object_init(dev, obj, size); 1407 if (ret) 1408 goto err_free; 1409 1410 mapping = file_inode(obj->filp)->i_mapping; 1411 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32); 1412 } 1413 1414 /* Allocate memory if needed. */ 1415 if (flags & OMAP_BO_MEM_DMA_API) { 1416 omap_obj->vaddr = dma_alloc_wc(dev->dev, size, 1417 &omap_obj->paddr, 1418 GFP_KERNEL); 1419 if (!omap_obj->vaddr) 1420 goto err_release; 1421 } 1422 1423 spin_lock(&priv->list_lock); 1424 list_add(&omap_obj->mm_list, &priv->obj_list); 1425 spin_unlock(&priv->list_lock); 1426 1427 return obj; 1428 1429 err_release: 1430 drm_gem_object_release(obj); 1431 err_free: 1432 kfree(omap_obj); 1433 return NULL; 1434 } 1435 1436 struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size, 1437 struct sg_table *sgt) 1438 { 1439 struct omap_drm_private *priv = dev->dev_private; 1440 struct omap_gem_object *omap_obj; 1441 struct drm_gem_object *obj; 1442 union omap_gem_size gsize; 1443 1444 /* Without a DMM only physically contiguous buffers can be supported. */ 1445 if (sgt->orig_nents != 1 && !priv->has_dmm) 1446 return ERR_PTR(-EINVAL); 1447 1448 mutex_lock(&dev->struct_mutex); 1449 1450 gsize.bytes = PAGE_ALIGN(size); 1451 obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC); 1452 if (!obj) { 1453 obj = ERR_PTR(-ENOMEM); 1454 goto done; 1455 } 1456 1457 omap_obj = to_omap_bo(obj); 1458 omap_obj->sgt = sgt; 1459 1460 if (sgt->orig_nents == 1) { 1461 omap_obj->paddr = sg_dma_address(sgt->sgl); 1462 } else { 1463 /* Create pages list from sgt */ 1464 struct sg_page_iter iter; 1465 struct page **pages; 1466 unsigned int npages; 1467 unsigned int i = 0; 1468 1469 npages = DIV_ROUND_UP(size, PAGE_SIZE); 1470 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL); 1471 if (!pages) { 1472 omap_gem_free_object(obj); 1473 obj = ERR_PTR(-ENOMEM); 1474 goto done; 1475 } 1476 1477 omap_obj->pages = pages; 1478 1479 for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) { 1480 pages[i++] = sg_page_iter_page(&iter); 1481 if (i > npages) 1482 break; 1483 } 1484 1485 if (WARN_ON(i != npages)) { 1486 omap_gem_free_object(obj); 1487 obj = ERR_PTR(-ENOMEM); 1488 goto done; 1489 } 1490 } 1491 1492 done: 1493 mutex_unlock(&dev->struct_mutex); 1494 return obj; 1495 } 1496 1497 /* convenience method to construct a GEM buffer object, and userspace handle */ 1498 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file, 1499 union omap_gem_size gsize, uint32_t flags, uint32_t *handle) 1500 { 1501 struct drm_gem_object *obj; 1502 int ret; 1503 1504 obj = omap_gem_new(dev, gsize, flags); 1505 if (!obj) 1506 return -ENOMEM; 1507 1508 ret = drm_gem_handle_create(file, obj, handle); 1509 if (ret) { 1510 omap_gem_free_object(obj); 1511 return ret; 1512 } 1513 1514 /* drop reference from allocate - handle holds it now */ 1515 drm_gem_object_unreference_unlocked(obj); 1516 1517 return 0; 1518 } 1519 1520 /* ----------------------------------------------------------------------------- 1521 * Init & Cleanup 1522 */ 1523 1524 /* If DMM is used, we need to set some stuff up.. */ 1525 void omap_gem_init(struct drm_device *dev) 1526 { 1527 struct omap_drm_private *priv = dev->dev_private; 1528 struct omap_drm_usergart *usergart; 1529 const enum tiler_fmt fmts[] = { 1530 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT 1531 }; 1532 int i, j; 1533 1534 if (!dmm_is_available()) { 1535 /* DMM only supported on OMAP4 and later, so this isn't fatal */ 1536 dev_warn(dev->dev, "DMM not available, disable DMM support\n"); 1537 return; 1538 } 1539 1540 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL); 1541 if (!usergart) 1542 return; 1543 1544 /* reserve 4k aligned/wide regions for userspace mappings: */ 1545 for (i = 0; i < ARRAY_SIZE(fmts); i++) { 1546 uint16_t h = 1, w = PAGE_SIZE >> i; 1547 tiler_align(fmts[i], &w, &h); 1548 /* note: since each region is 1 4kb page wide, and minimum 1549 * number of rows, the height ends up being the same as the 1550 * # of pages in the region 1551 */ 1552 usergart[i].height = h; 1553 usergart[i].height_shift = ilog2(h); 1554 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT; 1555 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i); 1556 for (j = 0; j < NUM_USERGART_ENTRIES; j++) { 1557 struct omap_drm_usergart_entry *entry; 1558 struct tiler_block *block; 1559 1560 entry = &usergart[i].entry[j]; 1561 block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE); 1562 if (IS_ERR(block)) { 1563 dev_err(dev->dev, 1564 "reserve failed: %d, %d, %ld\n", 1565 i, j, PTR_ERR(block)); 1566 return; 1567 } 1568 entry->paddr = tiler_ssptr(block); 1569 entry->block = block; 1570 1571 DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h, 1572 &entry->paddr, 1573 usergart[i].stride_pfn << PAGE_SHIFT); 1574 } 1575 } 1576 1577 priv->usergart = usergart; 1578 priv->has_dmm = true; 1579 } 1580 1581 void omap_gem_deinit(struct drm_device *dev) 1582 { 1583 struct omap_drm_private *priv = dev->dev_private; 1584 1585 /* I believe we can rely on there being no more outstanding GEM 1586 * objects which could depend on usergart/dmm at this point. 1587 */ 1588 kfree(priv->usergart); 1589 } 1590