1a8c21a54SThe etnaviv authors /* 2a8c21a54SThe etnaviv authors * Copyright (C) 2015 Etnaviv Project 3a8c21a54SThe etnaviv authors * 4a8c21a54SThe etnaviv authors * This program is free software; you can redistribute it and/or modify it 5a8c21a54SThe etnaviv authors * under the terms of the GNU General Public License version 2 as published by 6a8c21a54SThe etnaviv authors * the Free Software Foundation. 7a8c21a54SThe etnaviv authors * 8a8c21a54SThe etnaviv authors * This program is distributed in the hope that it will be useful, but WITHOUT 9a8c21a54SThe etnaviv authors * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10a8c21a54SThe etnaviv authors * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11a8c21a54SThe etnaviv authors * more details. 12a8c21a54SThe etnaviv authors * 13a8c21a54SThe etnaviv authors * You should have received a copy of the GNU General Public License along with 14a8c21a54SThe etnaviv authors * this program. If not, see <http://www.gnu.org/licenses/>. 15a8c21a54SThe etnaviv authors */ 16a8c21a54SThe etnaviv authors 17a8c21a54SThe etnaviv authors #include <linux/spinlock.h> 18a8c21a54SThe etnaviv authors #include <linux/shmem_fs.h> 19a8c21a54SThe etnaviv authors 20a8c21a54SThe etnaviv authors #include "etnaviv_drv.h" 21a8c21a54SThe etnaviv authors #include "etnaviv_gem.h" 22a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h" 23a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h" 24a8c21a54SThe etnaviv authors 25a8c21a54SThe etnaviv authors static void etnaviv_gem_scatter_map(struct etnaviv_gem_object *etnaviv_obj) 26a8c21a54SThe etnaviv authors { 27a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev; 28a8c21a54SThe etnaviv authors struct sg_table *sgt = etnaviv_obj->sgt; 29a8c21a54SThe etnaviv authors 30a8c21a54SThe etnaviv authors /* 31a8c21a54SThe etnaviv authors * For non-cached buffers, ensure the new pages are clean 32a8c21a54SThe etnaviv authors * because display controller, GPU, etc. are not coherent. 33a8c21a54SThe etnaviv authors */ 34a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK) 35a8c21a54SThe etnaviv authors dma_map_sg(dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL); 36a8c21a54SThe etnaviv authors } 37a8c21a54SThe etnaviv authors 38a8c21a54SThe etnaviv authors static void etnaviv_gem_scatterlist_unmap(struct etnaviv_gem_object *etnaviv_obj) 39a8c21a54SThe etnaviv authors { 40a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev; 41a8c21a54SThe etnaviv authors struct sg_table *sgt = etnaviv_obj->sgt; 42a8c21a54SThe etnaviv authors 43a8c21a54SThe etnaviv authors /* 44a8c21a54SThe etnaviv authors * For non-cached buffers, ensure the new pages are clean 45a8c21a54SThe etnaviv authors * because display controller, GPU, etc. are not coherent: 46a8c21a54SThe etnaviv authors * 47a8c21a54SThe etnaviv authors * WARNING: The DMA API does not support concurrent CPU 48a8c21a54SThe etnaviv authors * and device access to the memory area. With BIDIRECTIONAL, 49a8c21a54SThe etnaviv authors * we will clean the cache lines which overlap the region, 50a8c21a54SThe etnaviv authors * and invalidate all cache lines (partially) contained in 51a8c21a54SThe etnaviv authors * the region. 52a8c21a54SThe etnaviv authors * 53a8c21a54SThe etnaviv authors * If you have dirty data in the overlapping cache lines, 54a8c21a54SThe etnaviv authors * that will corrupt the GPU-written data. If you have 55a8c21a54SThe etnaviv authors * written into the remainder of the region, this can 56a8c21a54SThe etnaviv authors * discard those writes. 57a8c21a54SThe etnaviv authors */ 58a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK) 59a8c21a54SThe etnaviv authors dma_unmap_sg(dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL); 60a8c21a54SThe etnaviv authors } 61a8c21a54SThe etnaviv authors 62a8c21a54SThe etnaviv authors /* called with etnaviv_obj->lock held */ 63a8c21a54SThe etnaviv authors static int etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object *etnaviv_obj) 64a8c21a54SThe etnaviv authors { 65a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev; 66a8c21a54SThe etnaviv authors struct page **p = drm_gem_get_pages(&etnaviv_obj->base); 67a8c21a54SThe etnaviv authors 68a8c21a54SThe etnaviv authors if (IS_ERR(p)) { 69a8c21a54SThe etnaviv authors dev_err(dev->dev, "could not get pages: %ld\n", PTR_ERR(p)); 70a8c21a54SThe etnaviv authors return PTR_ERR(p); 71a8c21a54SThe etnaviv authors } 72a8c21a54SThe etnaviv authors 73a8c21a54SThe etnaviv authors etnaviv_obj->pages = p; 74a8c21a54SThe etnaviv authors 75a8c21a54SThe etnaviv authors return 0; 76a8c21a54SThe etnaviv authors } 77a8c21a54SThe etnaviv authors 78a8c21a54SThe etnaviv authors static void put_pages(struct etnaviv_gem_object *etnaviv_obj) 79a8c21a54SThe etnaviv authors { 80a8c21a54SThe etnaviv authors if (etnaviv_obj->sgt) { 81a8c21a54SThe etnaviv authors etnaviv_gem_scatterlist_unmap(etnaviv_obj); 82a8c21a54SThe etnaviv authors sg_free_table(etnaviv_obj->sgt); 83a8c21a54SThe etnaviv authors kfree(etnaviv_obj->sgt); 84a8c21a54SThe etnaviv authors etnaviv_obj->sgt = NULL; 85a8c21a54SThe etnaviv authors } 86a8c21a54SThe etnaviv authors if (etnaviv_obj->pages) { 87a8c21a54SThe etnaviv authors drm_gem_put_pages(&etnaviv_obj->base, etnaviv_obj->pages, 88a8c21a54SThe etnaviv authors true, false); 89a8c21a54SThe etnaviv authors 90a8c21a54SThe etnaviv authors etnaviv_obj->pages = NULL; 91a8c21a54SThe etnaviv authors } 92a8c21a54SThe etnaviv authors } 93a8c21a54SThe etnaviv authors 94a8c21a54SThe etnaviv authors struct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *etnaviv_obj) 95a8c21a54SThe etnaviv authors { 96a8c21a54SThe etnaviv authors int ret; 97a8c21a54SThe etnaviv authors 98a8c21a54SThe etnaviv authors lockdep_assert_held(&etnaviv_obj->lock); 99a8c21a54SThe etnaviv authors 100a8c21a54SThe etnaviv authors if (!etnaviv_obj->pages) { 101a8c21a54SThe etnaviv authors ret = etnaviv_obj->ops->get_pages(etnaviv_obj); 102a8c21a54SThe etnaviv authors if (ret < 0) 103a8c21a54SThe etnaviv authors return ERR_PTR(ret); 104a8c21a54SThe etnaviv authors } 105a8c21a54SThe etnaviv authors 106a8c21a54SThe etnaviv authors if (!etnaviv_obj->sgt) { 107a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev; 108a8c21a54SThe etnaviv authors int npages = etnaviv_obj->base.size >> PAGE_SHIFT; 109a8c21a54SThe etnaviv authors struct sg_table *sgt; 110a8c21a54SThe etnaviv authors 111a8c21a54SThe etnaviv authors sgt = drm_prime_pages_to_sg(etnaviv_obj->pages, npages); 112a8c21a54SThe etnaviv authors if (IS_ERR(sgt)) { 113a8c21a54SThe etnaviv authors dev_err(dev->dev, "failed to allocate sgt: %ld\n", 114a8c21a54SThe etnaviv authors PTR_ERR(sgt)); 115a8c21a54SThe etnaviv authors return ERR_CAST(sgt); 116a8c21a54SThe etnaviv authors } 117a8c21a54SThe etnaviv authors 118a8c21a54SThe etnaviv authors etnaviv_obj->sgt = sgt; 119a8c21a54SThe etnaviv authors 120a8c21a54SThe etnaviv authors etnaviv_gem_scatter_map(etnaviv_obj); 121a8c21a54SThe etnaviv authors } 122a8c21a54SThe etnaviv authors 123a8c21a54SThe etnaviv authors return etnaviv_obj->pages; 124a8c21a54SThe etnaviv authors } 125a8c21a54SThe etnaviv authors 126a8c21a54SThe etnaviv authors void etnaviv_gem_put_pages(struct etnaviv_gem_object *etnaviv_obj) 127a8c21a54SThe etnaviv authors { 128a8c21a54SThe etnaviv authors lockdep_assert_held(&etnaviv_obj->lock); 129a8c21a54SThe etnaviv authors /* when we start tracking the pin count, then do something here */ 130a8c21a54SThe etnaviv authors } 131a8c21a54SThe etnaviv authors 132a8c21a54SThe etnaviv authors static int etnaviv_gem_mmap_obj(struct drm_gem_object *obj, 133a8c21a54SThe etnaviv authors struct vm_area_struct *vma) 134a8c21a54SThe etnaviv authors { 135a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 136a8c21a54SThe etnaviv authors pgprot_t vm_page_prot; 137a8c21a54SThe etnaviv authors 138a8c21a54SThe etnaviv authors vma->vm_flags &= ~VM_PFNMAP; 139a8c21a54SThe etnaviv authors vma->vm_flags |= VM_MIXEDMAP; 140a8c21a54SThe etnaviv authors 141a8c21a54SThe etnaviv authors vm_page_prot = vm_get_page_prot(vma->vm_flags); 142a8c21a54SThe etnaviv authors 143a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_WC) { 144a8c21a54SThe etnaviv authors vma->vm_page_prot = pgprot_writecombine(vm_page_prot); 145a8c21a54SThe etnaviv authors } else if (etnaviv_obj->flags & ETNA_BO_UNCACHED) { 146a8c21a54SThe etnaviv authors vma->vm_page_prot = pgprot_noncached(vm_page_prot); 147a8c21a54SThe etnaviv authors } else { 148a8c21a54SThe etnaviv authors /* 149a8c21a54SThe etnaviv authors * Shunt off cached objs to shmem file so they have their own 150a8c21a54SThe etnaviv authors * address_space (so unmap_mapping_range does what we want, 151a8c21a54SThe etnaviv authors * in particular in the case of mmap'd dmabufs) 152a8c21a54SThe etnaviv authors */ 153a8c21a54SThe etnaviv authors fput(vma->vm_file); 154a8c21a54SThe etnaviv authors get_file(obj->filp); 155a8c21a54SThe etnaviv authors vma->vm_pgoff = 0; 156a8c21a54SThe etnaviv authors vma->vm_file = obj->filp; 157a8c21a54SThe etnaviv authors 158a8c21a54SThe etnaviv authors vma->vm_page_prot = vm_page_prot; 159a8c21a54SThe etnaviv authors } 160a8c21a54SThe etnaviv authors 161a8c21a54SThe etnaviv authors return 0; 162a8c21a54SThe etnaviv authors } 163a8c21a54SThe etnaviv authors 164a8c21a54SThe etnaviv authors int etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma) 165a8c21a54SThe etnaviv authors { 166a8c21a54SThe etnaviv authors struct etnaviv_gem_object *obj; 167a8c21a54SThe etnaviv authors int ret; 168a8c21a54SThe etnaviv authors 169a8c21a54SThe etnaviv authors ret = drm_gem_mmap(filp, vma); 170a8c21a54SThe etnaviv authors if (ret) { 171a8c21a54SThe etnaviv authors DBG("mmap failed: %d", ret); 172a8c21a54SThe etnaviv authors return ret; 173a8c21a54SThe etnaviv authors } 174a8c21a54SThe etnaviv authors 175a8c21a54SThe etnaviv authors obj = to_etnaviv_bo(vma->vm_private_data); 176a8c21a54SThe etnaviv authors return etnaviv_gem_mmap_obj(vma->vm_private_data, vma); 177a8c21a54SThe etnaviv authors } 178a8c21a54SThe etnaviv authors 179a8c21a54SThe etnaviv authors int etnaviv_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 180a8c21a54SThe etnaviv authors { 181a8c21a54SThe etnaviv authors struct drm_gem_object *obj = vma->vm_private_data; 182a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 183a8c21a54SThe etnaviv authors struct page **pages, *page; 184a8c21a54SThe etnaviv authors pgoff_t pgoff; 185a8c21a54SThe etnaviv authors int ret; 186a8c21a54SThe etnaviv authors 187a8c21a54SThe etnaviv authors /* 188a8c21a54SThe etnaviv authors * Make sure we don't parallel update on a fault, nor move or remove 189a8c21a54SThe etnaviv authors * something from beneath our feet. Note that vm_insert_page() is 190a8c21a54SThe etnaviv authors * specifically coded to take care of this, so we don't have to. 191a8c21a54SThe etnaviv authors */ 192a8c21a54SThe etnaviv authors ret = mutex_lock_interruptible(&etnaviv_obj->lock); 193a8c21a54SThe etnaviv authors if (ret) 194a8c21a54SThe etnaviv authors goto out; 195a8c21a54SThe etnaviv authors 196a8c21a54SThe etnaviv authors /* make sure we have pages attached now */ 197a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(etnaviv_obj); 198a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 199a8c21a54SThe etnaviv authors 200a8c21a54SThe etnaviv authors if (IS_ERR(pages)) { 201a8c21a54SThe etnaviv authors ret = PTR_ERR(pages); 202a8c21a54SThe etnaviv authors goto out; 203a8c21a54SThe etnaviv authors } 204a8c21a54SThe etnaviv authors 205a8c21a54SThe etnaviv authors /* We don't use vmf->pgoff since that has the fake offset: */ 206a8c21a54SThe etnaviv authors pgoff = ((unsigned long)vmf->virtual_address - 207a8c21a54SThe etnaviv authors vma->vm_start) >> PAGE_SHIFT; 208a8c21a54SThe etnaviv authors 209a8c21a54SThe etnaviv authors page = pages[pgoff]; 210a8c21a54SThe etnaviv authors 211a8c21a54SThe etnaviv authors VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, 212a8c21a54SThe etnaviv authors page_to_pfn(page), page_to_pfn(page) << PAGE_SHIFT); 213a8c21a54SThe etnaviv authors 214a8c21a54SThe etnaviv authors ret = vm_insert_page(vma, (unsigned long)vmf->virtual_address, page); 215a8c21a54SThe etnaviv authors 216a8c21a54SThe etnaviv authors out: 217a8c21a54SThe etnaviv authors switch (ret) { 218a8c21a54SThe etnaviv authors case -EAGAIN: 219a8c21a54SThe etnaviv authors case 0: 220a8c21a54SThe etnaviv authors case -ERESTARTSYS: 221a8c21a54SThe etnaviv authors case -EINTR: 222a8c21a54SThe etnaviv authors case -EBUSY: 223a8c21a54SThe etnaviv authors /* 224a8c21a54SThe etnaviv authors * EBUSY is ok: this just means that another thread 225a8c21a54SThe etnaviv authors * already did the job. 226a8c21a54SThe etnaviv authors */ 227a8c21a54SThe etnaviv authors return VM_FAULT_NOPAGE; 228a8c21a54SThe etnaviv authors case -ENOMEM: 229a8c21a54SThe etnaviv authors return VM_FAULT_OOM; 230a8c21a54SThe etnaviv authors default: 231a8c21a54SThe etnaviv authors return VM_FAULT_SIGBUS; 232a8c21a54SThe etnaviv authors } 233a8c21a54SThe etnaviv authors } 234a8c21a54SThe etnaviv authors 235a8c21a54SThe etnaviv authors int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset) 236a8c21a54SThe etnaviv authors { 237a8c21a54SThe etnaviv authors int ret; 238a8c21a54SThe etnaviv authors 239a8c21a54SThe etnaviv authors /* Make it mmapable */ 240a8c21a54SThe etnaviv authors ret = drm_gem_create_mmap_offset(obj); 241a8c21a54SThe etnaviv authors if (ret) 242a8c21a54SThe etnaviv authors dev_err(obj->dev->dev, "could not allocate mmap offset\n"); 243a8c21a54SThe etnaviv authors else 244a8c21a54SThe etnaviv authors *offset = drm_vma_node_offset_addr(&obj->vma_node); 245a8c21a54SThe etnaviv authors 246a8c21a54SThe etnaviv authors return ret; 247a8c21a54SThe etnaviv authors } 248a8c21a54SThe etnaviv authors 249a8c21a54SThe etnaviv authors static struct etnaviv_vram_mapping * 250a8c21a54SThe etnaviv authors etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object *obj, 251a8c21a54SThe etnaviv authors struct etnaviv_iommu *mmu) 252a8c21a54SThe etnaviv authors { 253a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping; 254a8c21a54SThe etnaviv authors 255a8c21a54SThe etnaviv authors list_for_each_entry(mapping, &obj->vram_list, obj_node) { 256a8c21a54SThe etnaviv authors if (mapping->mmu == mmu) 257a8c21a54SThe etnaviv authors return mapping; 258a8c21a54SThe etnaviv authors } 259a8c21a54SThe etnaviv authors 260a8c21a54SThe etnaviv authors return NULL; 261a8c21a54SThe etnaviv authors } 262a8c21a54SThe etnaviv authors 263a8c21a54SThe etnaviv authors int etnaviv_gem_get_iova(struct etnaviv_gpu *gpu, 264a8c21a54SThe etnaviv authors struct drm_gem_object *obj, u32 *iova) 265a8c21a54SThe etnaviv authors { 266a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 267a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping; 268a8c21a54SThe etnaviv authors struct page **pages; 269a8c21a54SThe etnaviv authors int ret = 0; 270a8c21a54SThe etnaviv authors 271a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 272a8c21a54SThe etnaviv authors mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu); 273a8c21a54SThe etnaviv authors if (mapping) { 274a8c21a54SThe etnaviv authors /* 275a8c21a54SThe etnaviv authors * Holding the object lock prevents the use count changing 276a8c21a54SThe etnaviv authors * beneath us. If the use count is zero, the MMU might be 277a8c21a54SThe etnaviv authors * reaping this object, so take the lock and re-check that 278a8c21a54SThe etnaviv authors * the MMU owns this mapping to close this race. 279a8c21a54SThe etnaviv authors */ 280a8c21a54SThe etnaviv authors if (mapping->use == 0) { 281a8c21a54SThe etnaviv authors mutex_lock(&gpu->mmu->lock); 282a8c21a54SThe etnaviv authors if (mapping->mmu == gpu->mmu) 283a8c21a54SThe etnaviv authors mapping->use += 1; 284a8c21a54SThe etnaviv authors else 285a8c21a54SThe etnaviv authors mapping = NULL; 286a8c21a54SThe etnaviv authors mutex_unlock(&gpu->mmu->lock); 287a8c21a54SThe etnaviv authors if (mapping) 288a8c21a54SThe etnaviv authors goto out; 289a8c21a54SThe etnaviv authors } else { 290a8c21a54SThe etnaviv authors mapping->use += 1; 291a8c21a54SThe etnaviv authors goto out; 292a8c21a54SThe etnaviv authors } 293a8c21a54SThe etnaviv authors } 294a8c21a54SThe etnaviv authors 295a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(etnaviv_obj); 296a8c21a54SThe etnaviv authors if (IS_ERR(pages)) { 297a8c21a54SThe etnaviv authors ret = PTR_ERR(pages); 298a8c21a54SThe etnaviv authors goto out; 299a8c21a54SThe etnaviv authors } 300a8c21a54SThe etnaviv authors 301a8c21a54SThe etnaviv authors /* 302a8c21a54SThe etnaviv authors * See if we have a reaped vram mapping we can re-use before 303a8c21a54SThe etnaviv authors * allocating a fresh mapping. 304a8c21a54SThe etnaviv authors */ 305a8c21a54SThe etnaviv authors mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, NULL); 306a8c21a54SThe etnaviv authors if (!mapping) { 307a8c21a54SThe etnaviv authors mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 308ed94add0SDan Carpenter if (!mapping) { 309ed94add0SDan Carpenter ret = -ENOMEM; 310ed94add0SDan Carpenter goto out; 311ed94add0SDan Carpenter } 312a8c21a54SThe etnaviv authors 313a8c21a54SThe etnaviv authors INIT_LIST_HEAD(&mapping->scan_node); 314a8c21a54SThe etnaviv authors mapping->object = etnaviv_obj; 315a8c21a54SThe etnaviv authors } else { 316a8c21a54SThe etnaviv authors list_del(&mapping->obj_node); 317a8c21a54SThe etnaviv authors } 318a8c21a54SThe etnaviv authors 319a8c21a54SThe etnaviv authors mapping->mmu = gpu->mmu; 320a8c21a54SThe etnaviv authors mapping->use = 1; 321a8c21a54SThe etnaviv authors 322a8c21a54SThe etnaviv authors ret = etnaviv_iommu_map_gem(gpu->mmu, etnaviv_obj, gpu->memory_base, 323a8c21a54SThe etnaviv authors mapping); 324a8c21a54SThe etnaviv authors if (ret < 0) 325a8c21a54SThe etnaviv authors kfree(mapping); 326a8c21a54SThe etnaviv authors else 327a8c21a54SThe etnaviv authors list_add_tail(&mapping->obj_node, &etnaviv_obj->vram_list); 328a8c21a54SThe etnaviv authors 329a8c21a54SThe etnaviv authors out: 330a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 331a8c21a54SThe etnaviv authors 332a8c21a54SThe etnaviv authors if (!ret) { 333a8c21a54SThe etnaviv authors /* Take a reference on the object */ 334a8c21a54SThe etnaviv authors drm_gem_object_reference(obj); 335a8c21a54SThe etnaviv authors *iova = mapping->iova; 336a8c21a54SThe etnaviv authors } 337a8c21a54SThe etnaviv authors 338a8c21a54SThe etnaviv authors return ret; 339a8c21a54SThe etnaviv authors } 340a8c21a54SThe etnaviv authors 341a8c21a54SThe etnaviv authors void etnaviv_gem_put_iova(struct etnaviv_gpu *gpu, struct drm_gem_object *obj) 342a8c21a54SThe etnaviv authors { 343a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 344a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping; 345a8c21a54SThe etnaviv authors 346a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 347a8c21a54SThe etnaviv authors mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu); 348a8c21a54SThe etnaviv authors 349a8c21a54SThe etnaviv authors WARN_ON(mapping->use == 0); 350a8c21a54SThe etnaviv authors mapping->use -= 1; 351a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 352a8c21a54SThe etnaviv authors 353a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 354a8c21a54SThe etnaviv authors } 355a8c21a54SThe etnaviv authors 356ce3088fdSLucas Stach void *etnaviv_gem_vmap(struct drm_gem_object *obj) 357a8c21a54SThe etnaviv authors { 358a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 359a8c21a54SThe etnaviv authors 360a0a5ab3eSLucas Stach if (etnaviv_obj->vaddr) 361a0a5ab3eSLucas Stach return etnaviv_obj->vaddr; 362a0a5ab3eSLucas Stach 363a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 364a0a5ab3eSLucas Stach /* 365a0a5ab3eSLucas Stach * Need to check again, as we might have raced with another thread 366a0a5ab3eSLucas Stach * while waiting for the mutex. 367a0a5ab3eSLucas Stach */ 368a0a5ab3eSLucas Stach if (!etnaviv_obj->vaddr) 369a0a5ab3eSLucas Stach etnaviv_obj->vaddr = etnaviv_obj->ops->vmap(etnaviv_obj); 370a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 371a8c21a54SThe etnaviv authors 372a8c21a54SThe etnaviv authors return etnaviv_obj->vaddr; 373a8c21a54SThe etnaviv authors } 374a8c21a54SThe etnaviv authors 375a0a5ab3eSLucas Stach static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj) 376a0a5ab3eSLucas Stach { 377a0a5ab3eSLucas Stach struct page **pages; 378a0a5ab3eSLucas Stach 379a0a5ab3eSLucas Stach lockdep_assert_held(&obj->lock); 380a0a5ab3eSLucas Stach 381a0a5ab3eSLucas Stach pages = etnaviv_gem_get_pages(obj); 382a0a5ab3eSLucas Stach if (IS_ERR(pages)) 383a0a5ab3eSLucas Stach return NULL; 384a0a5ab3eSLucas Stach 385a0a5ab3eSLucas Stach return vmap(pages, obj->base.size >> PAGE_SHIFT, 386a0a5ab3eSLucas Stach VM_MAP, pgprot_writecombine(PAGE_KERNEL)); 387a0a5ab3eSLucas Stach } 388a0a5ab3eSLucas Stach 389a8c21a54SThe etnaviv authors static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op) 390a8c21a54SThe etnaviv authors { 391a8c21a54SThe etnaviv authors if (op & ETNA_PREP_READ) 392a8c21a54SThe etnaviv authors return DMA_FROM_DEVICE; 393a8c21a54SThe etnaviv authors else if (op & ETNA_PREP_WRITE) 394a8c21a54SThe etnaviv authors return DMA_TO_DEVICE; 395a8c21a54SThe etnaviv authors else 396a8c21a54SThe etnaviv authors return DMA_BIDIRECTIONAL; 397a8c21a54SThe etnaviv authors } 398a8c21a54SThe etnaviv authors 399a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op, 400a8c21a54SThe etnaviv authors struct timespec *timeout) 401a8c21a54SThe etnaviv authors { 402a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 403a8c21a54SThe etnaviv authors struct drm_device *dev = obj->dev; 404a8c21a54SThe etnaviv authors bool write = !!(op & ETNA_PREP_WRITE); 405a8c21a54SThe etnaviv authors int ret; 406a8c21a54SThe etnaviv authors 407a8c21a54SThe etnaviv authors if (op & ETNA_PREP_NOSYNC) { 408a8c21a54SThe etnaviv authors if (!reservation_object_test_signaled_rcu(etnaviv_obj->resv, 409a8c21a54SThe etnaviv authors write)) 410a8c21a54SThe etnaviv authors return -EBUSY; 411a8c21a54SThe etnaviv authors } else { 412a8c21a54SThe etnaviv authors unsigned long remain = etnaviv_timeout_to_jiffies(timeout); 413a8c21a54SThe etnaviv authors 414a8c21a54SThe etnaviv authors ret = reservation_object_wait_timeout_rcu(etnaviv_obj->resv, 415a8c21a54SThe etnaviv authors write, true, remain); 416a8c21a54SThe etnaviv authors if (ret <= 0) 417a8c21a54SThe etnaviv authors return ret == 0 ? -ETIMEDOUT : ret; 418a8c21a54SThe etnaviv authors } 419a8c21a54SThe etnaviv authors 420a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHED) { 421a8c21a54SThe etnaviv authors if (!etnaviv_obj->sgt) { 422a8c21a54SThe etnaviv authors void *ret; 423a8c21a54SThe etnaviv authors 424a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 425a8c21a54SThe etnaviv authors ret = etnaviv_gem_get_pages(etnaviv_obj); 426a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 427a8c21a54SThe etnaviv authors if (IS_ERR(ret)) 428a8c21a54SThe etnaviv authors return PTR_ERR(ret); 429a8c21a54SThe etnaviv authors } 430a8c21a54SThe etnaviv authors 431a8c21a54SThe etnaviv authors dma_sync_sg_for_cpu(dev->dev, etnaviv_obj->sgt->sgl, 432a8c21a54SThe etnaviv authors etnaviv_obj->sgt->nents, 433a8c21a54SThe etnaviv authors etnaviv_op_to_dma_dir(op)); 434a8c21a54SThe etnaviv authors etnaviv_obj->last_cpu_prep_op = op; 435a8c21a54SThe etnaviv authors } 436a8c21a54SThe etnaviv authors 437a8c21a54SThe etnaviv authors return 0; 438a8c21a54SThe etnaviv authors } 439a8c21a54SThe etnaviv authors 440a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_fini(struct drm_gem_object *obj) 441a8c21a54SThe etnaviv authors { 442a8c21a54SThe etnaviv authors struct drm_device *dev = obj->dev; 443a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 444a8c21a54SThe etnaviv authors 445a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHED) { 446a8c21a54SThe etnaviv authors /* fini without a prep is almost certainly a userspace error */ 447a8c21a54SThe etnaviv authors WARN_ON(etnaviv_obj->last_cpu_prep_op == 0); 448a8c21a54SThe etnaviv authors dma_sync_sg_for_device(dev->dev, etnaviv_obj->sgt->sgl, 449a8c21a54SThe etnaviv authors etnaviv_obj->sgt->nents, 450a8c21a54SThe etnaviv authors etnaviv_op_to_dma_dir(etnaviv_obj->last_cpu_prep_op)); 451a8c21a54SThe etnaviv authors etnaviv_obj->last_cpu_prep_op = 0; 452a8c21a54SThe etnaviv authors } 453a8c21a54SThe etnaviv authors 454a8c21a54SThe etnaviv authors return 0; 455a8c21a54SThe etnaviv authors } 456a8c21a54SThe etnaviv authors 457a8c21a54SThe etnaviv authors int etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj, 458a8c21a54SThe etnaviv authors struct timespec *timeout) 459a8c21a54SThe etnaviv authors { 460a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 461a8c21a54SThe etnaviv authors 462a8c21a54SThe etnaviv authors return etnaviv_gpu_wait_obj_inactive(gpu, etnaviv_obj, timeout); 463a8c21a54SThe etnaviv authors } 464a8c21a54SThe etnaviv authors 465a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS 466a8c21a54SThe etnaviv authors static void etnaviv_gem_describe_fence(struct fence *fence, 467a8c21a54SThe etnaviv authors const char *type, struct seq_file *m) 468a8c21a54SThe etnaviv authors { 469a8c21a54SThe etnaviv authors if (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 470a8c21a54SThe etnaviv authors seq_printf(m, "\t%9s: %s %s seq %u\n", 471a8c21a54SThe etnaviv authors type, 472a8c21a54SThe etnaviv authors fence->ops->get_driver_name(fence), 473a8c21a54SThe etnaviv authors fence->ops->get_timeline_name(fence), 474a8c21a54SThe etnaviv authors fence->seqno); 475a8c21a54SThe etnaviv authors } 476a8c21a54SThe etnaviv authors 477a8c21a54SThe etnaviv authors static void etnaviv_gem_describe(struct drm_gem_object *obj, struct seq_file *m) 478a8c21a54SThe etnaviv authors { 479a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 480a8c21a54SThe etnaviv authors struct reservation_object *robj = etnaviv_obj->resv; 481a8c21a54SThe etnaviv authors struct reservation_object_list *fobj; 482a8c21a54SThe etnaviv authors struct fence *fence; 483a8c21a54SThe etnaviv authors unsigned long off = drm_vma_node_start(&obj->vma_node); 484a8c21a54SThe etnaviv authors 485a8c21a54SThe etnaviv authors seq_printf(m, "%08x: %c %2d (%2d) %08lx %p %zd\n", 486a8c21a54SThe etnaviv authors etnaviv_obj->flags, is_active(etnaviv_obj) ? 'A' : 'I', 487a8c21a54SThe etnaviv authors obj->name, obj->refcount.refcount.counter, 488a8c21a54SThe etnaviv authors off, etnaviv_obj->vaddr, obj->size); 489a8c21a54SThe etnaviv authors 490a8c21a54SThe etnaviv authors rcu_read_lock(); 491a8c21a54SThe etnaviv authors fobj = rcu_dereference(robj->fence); 492a8c21a54SThe etnaviv authors if (fobj) { 493a8c21a54SThe etnaviv authors unsigned int i, shared_count = fobj->shared_count; 494a8c21a54SThe etnaviv authors 495a8c21a54SThe etnaviv authors for (i = 0; i < shared_count; i++) { 496a8c21a54SThe etnaviv authors fence = rcu_dereference(fobj->shared[i]); 497a8c21a54SThe etnaviv authors etnaviv_gem_describe_fence(fence, "Shared", m); 498a8c21a54SThe etnaviv authors } 499a8c21a54SThe etnaviv authors } 500a8c21a54SThe etnaviv authors 501a8c21a54SThe etnaviv authors fence = rcu_dereference(robj->fence_excl); 502a8c21a54SThe etnaviv authors if (fence) 503a8c21a54SThe etnaviv authors etnaviv_gem_describe_fence(fence, "Exclusive", m); 504a8c21a54SThe etnaviv authors rcu_read_unlock(); 505a8c21a54SThe etnaviv authors } 506a8c21a54SThe etnaviv authors 507a8c21a54SThe etnaviv authors void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv, 508a8c21a54SThe etnaviv authors struct seq_file *m) 509a8c21a54SThe etnaviv authors { 510a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 511a8c21a54SThe etnaviv authors int count = 0; 512a8c21a54SThe etnaviv authors size_t size = 0; 513a8c21a54SThe etnaviv authors 514a8c21a54SThe etnaviv authors mutex_lock(&priv->gem_lock); 515a8c21a54SThe etnaviv authors list_for_each_entry(etnaviv_obj, &priv->gem_list, gem_node) { 516a8c21a54SThe etnaviv authors struct drm_gem_object *obj = &etnaviv_obj->base; 517a8c21a54SThe etnaviv authors 518a8c21a54SThe etnaviv authors seq_puts(m, " "); 519a8c21a54SThe etnaviv authors etnaviv_gem_describe(obj, m); 520a8c21a54SThe etnaviv authors count++; 521a8c21a54SThe etnaviv authors size += obj->size; 522a8c21a54SThe etnaviv authors } 523a8c21a54SThe etnaviv authors mutex_unlock(&priv->gem_lock); 524a8c21a54SThe etnaviv authors 525a8c21a54SThe etnaviv authors seq_printf(m, "Total %d objects, %zu bytes\n", count, size); 526a8c21a54SThe etnaviv authors } 527a8c21a54SThe etnaviv authors #endif 528a8c21a54SThe etnaviv authors 529a8c21a54SThe etnaviv authors static void etnaviv_gem_shmem_release(struct etnaviv_gem_object *etnaviv_obj) 530a8c21a54SThe etnaviv authors { 531a8c21a54SThe etnaviv authors if (etnaviv_obj->vaddr) 532a8c21a54SThe etnaviv authors vunmap(etnaviv_obj->vaddr); 533a8c21a54SThe etnaviv authors put_pages(etnaviv_obj); 534a8c21a54SThe etnaviv authors } 535a8c21a54SThe etnaviv authors 536a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_shmem_ops = { 537a8c21a54SThe etnaviv authors .get_pages = etnaviv_gem_shmem_get_pages, 538a8c21a54SThe etnaviv authors .release = etnaviv_gem_shmem_release, 539a0a5ab3eSLucas Stach .vmap = etnaviv_gem_vmap_impl, 540a8c21a54SThe etnaviv authors }; 541a8c21a54SThe etnaviv authors 542a8c21a54SThe etnaviv authors void etnaviv_gem_free_object(struct drm_gem_object *obj) 543a8c21a54SThe etnaviv authors { 544a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 545a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping, *tmp; 546a8c21a54SThe etnaviv authors 547a8c21a54SThe etnaviv authors /* object should not be active */ 548a8c21a54SThe etnaviv authors WARN_ON(is_active(etnaviv_obj)); 549a8c21a54SThe etnaviv authors 550a8c21a54SThe etnaviv authors list_del(&etnaviv_obj->gem_node); 551a8c21a54SThe etnaviv authors 552a8c21a54SThe etnaviv authors list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list, 553a8c21a54SThe etnaviv authors obj_node) { 554a8c21a54SThe etnaviv authors struct etnaviv_iommu *mmu = mapping->mmu; 555a8c21a54SThe etnaviv authors 556a8c21a54SThe etnaviv authors WARN_ON(mapping->use); 557a8c21a54SThe etnaviv authors 558a8c21a54SThe etnaviv authors if (mmu) 559a8c21a54SThe etnaviv authors etnaviv_iommu_unmap_gem(mmu, mapping); 560a8c21a54SThe etnaviv authors 561a8c21a54SThe etnaviv authors list_del(&mapping->obj_node); 562a8c21a54SThe etnaviv authors kfree(mapping); 563a8c21a54SThe etnaviv authors } 564a8c21a54SThe etnaviv authors 565a8c21a54SThe etnaviv authors drm_gem_free_mmap_offset(obj); 566a8c21a54SThe etnaviv authors etnaviv_obj->ops->release(etnaviv_obj); 567a8c21a54SThe etnaviv authors if (etnaviv_obj->resv == &etnaviv_obj->_resv) 568a8c21a54SThe etnaviv authors reservation_object_fini(&etnaviv_obj->_resv); 569a8c21a54SThe etnaviv authors drm_gem_object_release(obj); 570a8c21a54SThe etnaviv authors 571a8c21a54SThe etnaviv authors kfree(etnaviv_obj); 572a8c21a54SThe etnaviv authors } 573a8c21a54SThe etnaviv authors 574a8c21a54SThe etnaviv authors int etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj) 575a8c21a54SThe etnaviv authors { 576a8c21a54SThe etnaviv authors struct etnaviv_drm_private *priv = dev->dev_private; 577a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 578a8c21a54SThe etnaviv authors 579a8c21a54SThe etnaviv authors mutex_lock(&priv->gem_lock); 580a8c21a54SThe etnaviv authors list_add_tail(&etnaviv_obj->gem_node, &priv->gem_list); 581a8c21a54SThe etnaviv authors mutex_unlock(&priv->gem_lock); 582a8c21a54SThe etnaviv authors 583a8c21a54SThe etnaviv authors return 0; 584a8c21a54SThe etnaviv authors } 585a8c21a54SThe etnaviv authors 586a8c21a54SThe etnaviv authors static int etnaviv_gem_new_impl(struct drm_device *dev, u32 size, u32 flags, 587a8c21a54SThe etnaviv authors struct reservation_object *robj, const struct etnaviv_gem_ops *ops, 588a8c21a54SThe etnaviv authors struct drm_gem_object **obj) 589a8c21a54SThe etnaviv authors { 590a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 591a8c21a54SThe etnaviv authors unsigned sz = sizeof(*etnaviv_obj); 592a8c21a54SThe etnaviv authors bool valid = true; 593a8c21a54SThe etnaviv authors 594a8c21a54SThe etnaviv authors /* validate flags */ 595a8c21a54SThe etnaviv authors switch (flags & ETNA_BO_CACHE_MASK) { 596a8c21a54SThe etnaviv authors case ETNA_BO_UNCACHED: 597a8c21a54SThe etnaviv authors case ETNA_BO_CACHED: 598a8c21a54SThe etnaviv authors case ETNA_BO_WC: 599a8c21a54SThe etnaviv authors break; 600a8c21a54SThe etnaviv authors default: 601a8c21a54SThe etnaviv authors valid = false; 602a8c21a54SThe etnaviv authors } 603a8c21a54SThe etnaviv authors 604a8c21a54SThe etnaviv authors if (!valid) { 605a8c21a54SThe etnaviv authors dev_err(dev->dev, "invalid cache flag: %x\n", 606a8c21a54SThe etnaviv authors (flags & ETNA_BO_CACHE_MASK)); 607a8c21a54SThe etnaviv authors return -EINVAL; 608a8c21a54SThe etnaviv authors } 609a8c21a54SThe etnaviv authors 610a8c21a54SThe etnaviv authors etnaviv_obj = kzalloc(sz, GFP_KERNEL); 611a8c21a54SThe etnaviv authors if (!etnaviv_obj) 612a8c21a54SThe etnaviv authors return -ENOMEM; 613a8c21a54SThe etnaviv authors 614a8c21a54SThe etnaviv authors etnaviv_obj->flags = flags; 615a8c21a54SThe etnaviv authors etnaviv_obj->ops = ops; 616a8c21a54SThe etnaviv authors if (robj) { 617a8c21a54SThe etnaviv authors etnaviv_obj->resv = robj; 618a8c21a54SThe etnaviv authors } else { 619a8c21a54SThe etnaviv authors etnaviv_obj->resv = &etnaviv_obj->_resv; 620a8c21a54SThe etnaviv authors reservation_object_init(&etnaviv_obj->_resv); 621a8c21a54SThe etnaviv authors } 622a8c21a54SThe etnaviv authors 623a8c21a54SThe etnaviv authors mutex_init(&etnaviv_obj->lock); 624a8c21a54SThe etnaviv authors INIT_LIST_HEAD(&etnaviv_obj->vram_list); 625a8c21a54SThe etnaviv authors 626a8c21a54SThe etnaviv authors *obj = &etnaviv_obj->base; 627a8c21a54SThe etnaviv authors 628a8c21a54SThe etnaviv authors return 0; 629a8c21a54SThe etnaviv authors } 630a8c21a54SThe etnaviv authors 631a8c21a54SThe etnaviv authors static struct drm_gem_object *__etnaviv_gem_new(struct drm_device *dev, 632a8c21a54SThe etnaviv authors u32 size, u32 flags) 633a8c21a54SThe etnaviv authors { 634a8c21a54SThe etnaviv authors struct drm_gem_object *obj = NULL; 635a8c21a54SThe etnaviv authors int ret; 636a8c21a54SThe etnaviv authors 637a8c21a54SThe etnaviv authors size = PAGE_ALIGN(size); 638a8c21a54SThe etnaviv authors 639a8c21a54SThe etnaviv authors ret = etnaviv_gem_new_impl(dev, size, flags, NULL, 640a8c21a54SThe etnaviv authors &etnaviv_gem_shmem_ops, &obj); 641a8c21a54SThe etnaviv authors if (ret) 642a8c21a54SThe etnaviv authors goto fail; 643a8c21a54SThe etnaviv authors 644a8c21a54SThe etnaviv authors ret = drm_gem_object_init(dev, obj, size); 645a8c21a54SThe etnaviv authors if (ret == 0) { 646a8c21a54SThe etnaviv authors struct address_space *mapping; 647a8c21a54SThe etnaviv authors 648a8c21a54SThe etnaviv authors /* 649a8c21a54SThe etnaviv authors * Our buffers are kept pinned, so allocating them 650a8c21a54SThe etnaviv authors * from the MOVABLE zone is a really bad idea, and 651a8c21a54SThe etnaviv authors * conflicts with CMA. See coments above new_inode() 652a8c21a54SThe etnaviv authors * why this is required _and_ expected if you're 653a8c21a54SThe etnaviv authors * going to pin these pages. 654a8c21a54SThe etnaviv authors */ 655a8c21a54SThe etnaviv authors mapping = file_inode(obj->filp)->i_mapping; 656a8c21a54SThe etnaviv authors mapping_set_gfp_mask(mapping, GFP_HIGHUSER); 657a8c21a54SThe etnaviv authors } 658a8c21a54SThe etnaviv authors 659a8c21a54SThe etnaviv authors if (ret) 660a8c21a54SThe etnaviv authors goto fail; 661a8c21a54SThe etnaviv authors 662a8c21a54SThe etnaviv authors return obj; 663a8c21a54SThe etnaviv authors 664a8c21a54SThe etnaviv authors fail: 665a8c21a54SThe etnaviv authors if (obj) 666a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 667a8c21a54SThe etnaviv authors 668a8c21a54SThe etnaviv authors return ERR_PTR(ret); 669a8c21a54SThe etnaviv authors } 670a8c21a54SThe etnaviv authors 671a8c21a54SThe etnaviv authors /* convenience method to construct a GEM buffer object, and userspace handle */ 672a8c21a54SThe etnaviv authors int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file, 673a8c21a54SThe etnaviv authors u32 size, u32 flags, u32 *handle) 674a8c21a54SThe etnaviv authors { 675a8c21a54SThe etnaviv authors struct drm_gem_object *obj; 676a8c21a54SThe etnaviv authors int ret; 677a8c21a54SThe etnaviv authors 678a8c21a54SThe etnaviv authors obj = __etnaviv_gem_new(dev, size, flags); 679a8c21a54SThe etnaviv authors if (IS_ERR(obj)) 680a8c21a54SThe etnaviv authors return PTR_ERR(obj); 681a8c21a54SThe etnaviv authors 682a8c21a54SThe etnaviv authors ret = etnaviv_gem_obj_add(dev, obj); 683a8c21a54SThe etnaviv authors if (ret < 0) { 684a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 685a8c21a54SThe etnaviv authors return ret; 686a8c21a54SThe etnaviv authors } 687a8c21a54SThe etnaviv authors 688a8c21a54SThe etnaviv authors ret = drm_gem_handle_create(file, obj, handle); 689a8c21a54SThe etnaviv authors 690a8c21a54SThe etnaviv authors /* drop reference from allocate - handle holds it now */ 691a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 692a8c21a54SThe etnaviv authors 693a8c21a54SThe etnaviv authors return ret; 694a8c21a54SThe etnaviv authors } 695a8c21a54SThe etnaviv authors 696a8c21a54SThe etnaviv authors struct drm_gem_object *etnaviv_gem_new(struct drm_device *dev, 697a8c21a54SThe etnaviv authors u32 size, u32 flags) 698a8c21a54SThe etnaviv authors { 699a8c21a54SThe etnaviv authors struct drm_gem_object *obj; 700a8c21a54SThe etnaviv authors int ret; 701a8c21a54SThe etnaviv authors 702a8c21a54SThe etnaviv authors obj = __etnaviv_gem_new(dev, size, flags); 703a8c21a54SThe etnaviv authors if (IS_ERR(obj)) 704a8c21a54SThe etnaviv authors return obj; 705a8c21a54SThe etnaviv authors 706a8c21a54SThe etnaviv authors ret = etnaviv_gem_obj_add(dev, obj); 707a8c21a54SThe etnaviv authors if (ret < 0) { 708a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 709a8c21a54SThe etnaviv authors return ERR_PTR(ret); 710a8c21a54SThe etnaviv authors } 711a8c21a54SThe etnaviv authors 712a8c21a54SThe etnaviv authors return obj; 713a8c21a54SThe etnaviv authors } 714a8c21a54SThe etnaviv authors 715a8c21a54SThe etnaviv authors int etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags, 716a8c21a54SThe etnaviv authors struct reservation_object *robj, const struct etnaviv_gem_ops *ops, 717a8c21a54SThe etnaviv authors struct etnaviv_gem_object **res) 718a8c21a54SThe etnaviv authors { 719a8c21a54SThe etnaviv authors struct drm_gem_object *obj; 720a8c21a54SThe etnaviv authors int ret; 721a8c21a54SThe etnaviv authors 722a8c21a54SThe etnaviv authors ret = etnaviv_gem_new_impl(dev, size, flags, robj, ops, &obj); 723a8c21a54SThe etnaviv authors if (ret) 724a8c21a54SThe etnaviv authors return ret; 725a8c21a54SThe etnaviv authors 726a8c21a54SThe etnaviv authors drm_gem_private_object_init(dev, obj, size); 727a8c21a54SThe etnaviv authors 728a8c21a54SThe etnaviv authors *res = to_etnaviv_bo(obj); 729a8c21a54SThe etnaviv authors 730a8c21a54SThe etnaviv authors return 0; 731a8c21a54SThe etnaviv authors } 732a8c21a54SThe etnaviv authors 733a8c21a54SThe etnaviv authors struct get_pages_work { 734a8c21a54SThe etnaviv authors struct work_struct work; 735a8c21a54SThe etnaviv authors struct mm_struct *mm; 736a8c21a54SThe etnaviv authors struct task_struct *task; 737a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 738a8c21a54SThe etnaviv authors }; 739a8c21a54SThe etnaviv authors 740a8c21a54SThe etnaviv authors static struct page **etnaviv_gem_userptr_do_get_pages( 741a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj, struct mm_struct *mm, struct task_struct *task) 742a8c21a54SThe etnaviv authors { 743a8c21a54SThe etnaviv authors int ret = 0, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT; 744a8c21a54SThe etnaviv authors struct page **pvec; 745a8c21a54SThe etnaviv authors uintptr_t ptr; 746a8c21a54SThe etnaviv authors 747a8c21a54SThe etnaviv authors pvec = drm_malloc_ab(npages, sizeof(struct page *)); 748a8c21a54SThe etnaviv authors if (!pvec) 749a8c21a54SThe etnaviv authors return ERR_PTR(-ENOMEM); 750a8c21a54SThe etnaviv authors 751a8c21a54SThe etnaviv authors pinned = 0; 752a8c21a54SThe etnaviv authors ptr = etnaviv_obj->userptr.ptr; 753a8c21a54SThe etnaviv authors 754a8c21a54SThe etnaviv authors down_read(&mm->mmap_sem); 755a8c21a54SThe etnaviv authors while (pinned < npages) { 7561e987790SDave Hansen ret = get_user_pages_remote(task, mm, ptr, npages - pinned, 757a8c21a54SThe etnaviv authors !etnaviv_obj->userptr.ro, 0, 758a8c21a54SThe etnaviv authors pvec + pinned, NULL); 759a8c21a54SThe etnaviv authors if (ret < 0) 760a8c21a54SThe etnaviv authors break; 761a8c21a54SThe etnaviv authors 762a8c21a54SThe etnaviv authors ptr += ret * PAGE_SIZE; 763a8c21a54SThe etnaviv authors pinned += ret; 764a8c21a54SThe etnaviv authors } 765a8c21a54SThe etnaviv authors up_read(&mm->mmap_sem); 766a8c21a54SThe etnaviv authors 767a8c21a54SThe etnaviv authors if (ret < 0) { 768a8c21a54SThe etnaviv authors release_pages(pvec, pinned, 0); 769a8c21a54SThe etnaviv authors drm_free_large(pvec); 770a8c21a54SThe etnaviv authors return ERR_PTR(ret); 771a8c21a54SThe etnaviv authors } 772a8c21a54SThe etnaviv authors 773a8c21a54SThe etnaviv authors return pvec; 774a8c21a54SThe etnaviv authors } 775a8c21a54SThe etnaviv authors 776a8c21a54SThe etnaviv authors static void __etnaviv_gem_userptr_get_pages(struct work_struct *_work) 777a8c21a54SThe etnaviv authors { 778a8c21a54SThe etnaviv authors struct get_pages_work *work = container_of(_work, typeof(*work), work); 779a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = work->etnaviv_obj; 780a8c21a54SThe etnaviv authors struct page **pvec; 781a8c21a54SThe etnaviv authors 782a8c21a54SThe etnaviv authors pvec = etnaviv_gem_userptr_do_get_pages(etnaviv_obj, work->mm, work->task); 783a8c21a54SThe etnaviv authors 784a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 785a8c21a54SThe etnaviv authors if (IS_ERR(pvec)) { 786a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = ERR_CAST(pvec); 787a8c21a54SThe etnaviv authors } else { 788a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = NULL; 789a8c21a54SThe etnaviv authors etnaviv_obj->pages = pvec; 790a8c21a54SThe etnaviv authors } 791a8c21a54SThe etnaviv authors 792a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 793a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 794a8c21a54SThe etnaviv authors 795a8c21a54SThe etnaviv authors mmput(work->mm); 796a8c21a54SThe etnaviv authors put_task_struct(work->task); 797a8c21a54SThe etnaviv authors kfree(work); 798a8c21a54SThe etnaviv authors } 799a8c21a54SThe etnaviv authors 800a8c21a54SThe etnaviv authors static int etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object *etnaviv_obj) 801a8c21a54SThe etnaviv authors { 802a8c21a54SThe etnaviv authors struct page **pvec = NULL; 803a8c21a54SThe etnaviv authors struct get_pages_work *work; 804a8c21a54SThe etnaviv authors struct mm_struct *mm; 805a8c21a54SThe etnaviv authors int ret, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT; 806a8c21a54SThe etnaviv authors 807a8c21a54SThe etnaviv authors if (etnaviv_obj->userptr.work) { 808a8c21a54SThe etnaviv authors if (IS_ERR(etnaviv_obj->userptr.work)) { 809a8c21a54SThe etnaviv authors ret = PTR_ERR(etnaviv_obj->userptr.work); 810a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = NULL; 811a8c21a54SThe etnaviv authors } else { 812a8c21a54SThe etnaviv authors ret = -EAGAIN; 813a8c21a54SThe etnaviv authors } 814a8c21a54SThe etnaviv authors return ret; 815a8c21a54SThe etnaviv authors } 816a8c21a54SThe etnaviv authors 817a8c21a54SThe etnaviv authors mm = get_task_mm(etnaviv_obj->userptr.task); 818a8c21a54SThe etnaviv authors pinned = 0; 819a8c21a54SThe etnaviv authors if (mm == current->mm) { 820a8c21a54SThe etnaviv authors pvec = drm_malloc_ab(npages, sizeof(struct page *)); 821a8c21a54SThe etnaviv authors if (!pvec) { 822a8c21a54SThe etnaviv authors mmput(mm); 823a8c21a54SThe etnaviv authors return -ENOMEM; 824a8c21a54SThe etnaviv authors } 825a8c21a54SThe etnaviv authors 826a8c21a54SThe etnaviv authors pinned = __get_user_pages_fast(etnaviv_obj->userptr.ptr, npages, 827a8c21a54SThe etnaviv authors !etnaviv_obj->userptr.ro, pvec); 828a8c21a54SThe etnaviv authors if (pinned < 0) { 829a8c21a54SThe etnaviv authors drm_free_large(pvec); 830a8c21a54SThe etnaviv authors mmput(mm); 831a8c21a54SThe etnaviv authors return pinned; 832a8c21a54SThe etnaviv authors } 833a8c21a54SThe etnaviv authors 834a8c21a54SThe etnaviv authors if (pinned == npages) { 835a8c21a54SThe etnaviv authors etnaviv_obj->pages = pvec; 836a8c21a54SThe etnaviv authors mmput(mm); 837a8c21a54SThe etnaviv authors return 0; 838a8c21a54SThe etnaviv authors } 839a8c21a54SThe etnaviv authors } 840a8c21a54SThe etnaviv authors 841a8c21a54SThe etnaviv authors release_pages(pvec, pinned, 0); 842a8c21a54SThe etnaviv authors drm_free_large(pvec); 843a8c21a54SThe etnaviv authors 844a8c21a54SThe etnaviv authors work = kmalloc(sizeof(*work), GFP_KERNEL); 845a8c21a54SThe etnaviv authors if (!work) { 846a8c21a54SThe etnaviv authors mmput(mm); 847a8c21a54SThe etnaviv authors return -ENOMEM; 848a8c21a54SThe etnaviv authors } 849a8c21a54SThe etnaviv authors 850a8c21a54SThe etnaviv authors get_task_struct(current); 851a8c21a54SThe etnaviv authors drm_gem_object_reference(&etnaviv_obj->base); 852a8c21a54SThe etnaviv authors 853a8c21a54SThe etnaviv authors work->mm = mm; 854a8c21a54SThe etnaviv authors work->task = current; 855a8c21a54SThe etnaviv authors work->etnaviv_obj = etnaviv_obj; 856a8c21a54SThe etnaviv authors 857a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = &work->work; 858a8c21a54SThe etnaviv authors INIT_WORK(&work->work, __etnaviv_gem_userptr_get_pages); 859a8c21a54SThe etnaviv authors 860a8c21a54SThe etnaviv authors etnaviv_queue_work(etnaviv_obj->base.dev, &work->work); 861a8c21a54SThe etnaviv authors 862a8c21a54SThe etnaviv authors return -EAGAIN; 863a8c21a54SThe etnaviv authors } 864a8c21a54SThe etnaviv authors 865a8c21a54SThe etnaviv authors static void etnaviv_gem_userptr_release(struct etnaviv_gem_object *etnaviv_obj) 866a8c21a54SThe etnaviv authors { 867a8c21a54SThe etnaviv authors if (etnaviv_obj->sgt) { 868a8c21a54SThe etnaviv authors etnaviv_gem_scatterlist_unmap(etnaviv_obj); 869a8c21a54SThe etnaviv authors sg_free_table(etnaviv_obj->sgt); 870a8c21a54SThe etnaviv authors kfree(etnaviv_obj->sgt); 871a8c21a54SThe etnaviv authors } 872a8c21a54SThe etnaviv authors if (etnaviv_obj->pages) { 873a8c21a54SThe etnaviv authors int npages = etnaviv_obj->base.size >> PAGE_SHIFT; 874a8c21a54SThe etnaviv authors 875a8c21a54SThe etnaviv authors release_pages(etnaviv_obj->pages, npages, 0); 876a8c21a54SThe etnaviv authors drm_free_large(etnaviv_obj->pages); 877a8c21a54SThe etnaviv authors } 878a8c21a54SThe etnaviv authors put_task_struct(etnaviv_obj->userptr.task); 879a8c21a54SThe etnaviv authors } 880a8c21a54SThe etnaviv authors 881a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_userptr_ops = { 882a8c21a54SThe etnaviv authors .get_pages = etnaviv_gem_userptr_get_pages, 883a8c21a54SThe etnaviv authors .release = etnaviv_gem_userptr_release, 884a0a5ab3eSLucas Stach .vmap = etnaviv_gem_vmap_impl, 885a8c21a54SThe etnaviv authors }; 886a8c21a54SThe etnaviv authors 887a8c21a54SThe etnaviv authors int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file, 888a8c21a54SThe etnaviv authors uintptr_t ptr, u32 size, u32 flags, u32 *handle) 889a8c21a54SThe etnaviv authors { 890a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 891a8c21a54SThe etnaviv authors int ret; 892a8c21a54SThe etnaviv authors 893a8c21a54SThe etnaviv authors ret = etnaviv_gem_new_private(dev, size, ETNA_BO_CACHED, NULL, 894a8c21a54SThe etnaviv authors &etnaviv_gem_userptr_ops, &etnaviv_obj); 895a8c21a54SThe etnaviv authors if (ret) 896a8c21a54SThe etnaviv authors return ret; 897a8c21a54SThe etnaviv authors 898a8c21a54SThe etnaviv authors etnaviv_obj->userptr.ptr = ptr; 899a8c21a54SThe etnaviv authors etnaviv_obj->userptr.task = current; 900a8c21a54SThe etnaviv authors etnaviv_obj->userptr.ro = !(flags & ETNA_USERPTR_WRITE); 901a8c21a54SThe etnaviv authors get_task_struct(current); 902a8c21a54SThe etnaviv authors 903a8c21a54SThe etnaviv authors ret = etnaviv_gem_obj_add(dev, &etnaviv_obj->base); 904a8c21a54SThe etnaviv authors if (ret) { 905a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 906a8c21a54SThe etnaviv authors return ret; 907a8c21a54SThe etnaviv authors } 908a8c21a54SThe etnaviv authors 909a8c21a54SThe etnaviv authors ret = drm_gem_handle_create(file, &etnaviv_obj->base, handle); 910a8c21a54SThe etnaviv authors 911a8c21a54SThe etnaviv authors /* drop reference from allocate - handle holds it now */ 912a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 913a8c21a54SThe etnaviv authors 914a8c21a54SThe etnaviv authors return ret; 915a8c21a54SThe etnaviv authors } 916