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 263b6325f40SRussell King void etnaviv_gem_mapping_reference(struct etnaviv_vram_mapping *mapping) 264b6325f40SRussell King { 265b6325f40SRussell King struct etnaviv_gem_object *etnaviv_obj = mapping->object; 266b6325f40SRussell King 267b6325f40SRussell King drm_gem_object_reference(&etnaviv_obj->base); 268b6325f40SRussell King 269b6325f40SRussell King mutex_lock(&etnaviv_obj->lock); 270b6325f40SRussell King WARN_ON(mapping->use == 0); 271b6325f40SRussell King mapping->use += 1; 272b6325f40SRussell King mutex_unlock(&etnaviv_obj->lock); 273b6325f40SRussell King } 274b6325f40SRussell King 275b6325f40SRussell King void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping) 276b6325f40SRussell King { 277b6325f40SRussell King struct etnaviv_gem_object *etnaviv_obj = mapping->object; 278b6325f40SRussell King 279b6325f40SRussell King mutex_lock(&etnaviv_obj->lock); 280b6325f40SRussell King WARN_ON(mapping->use == 0); 281b6325f40SRussell King mapping->use -= 1; 282b6325f40SRussell King mutex_unlock(&etnaviv_obj->lock); 283b6325f40SRussell King 284b6325f40SRussell King drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 285b6325f40SRussell King } 286b6325f40SRussell King 287b6325f40SRussell King struct etnaviv_vram_mapping *etnaviv_gem_mapping_get( 288b6325f40SRussell King struct drm_gem_object *obj, struct etnaviv_gpu *gpu) 289a8c21a54SThe etnaviv authors { 290a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 291a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping; 292a8c21a54SThe etnaviv authors struct page **pages; 293a8c21a54SThe etnaviv authors int ret = 0; 294a8c21a54SThe etnaviv authors 295a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 296a8c21a54SThe etnaviv authors mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu); 297a8c21a54SThe etnaviv authors if (mapping) { 298a8c21a54SThe etnaviv authors /* 299a8c21a54SThe etnaviv authors * Holding the object lock prevents the use count changing 300a8c21a54SThe etnaviv authors * beneath us. If the use count is zero, the MMU might be 301a8c21a54SThe etnaviv authors * reaping this object, so take the lock and re-check that 302a8c21a54SThe etnaviv authors * the MMU owns this mapping to close this race. 303a8c21a54SThe etnaviv authors */ 304a8c21a54SThe etnaviv authors if (mapping->use == 0) { 305a8c21a54SThe etnaviv authors mutex_lock(&gpu->mmu->lock); 306a8c21a54SThe etnaviv authors if (mapping->mmu == gpu->mmu) 307a8c21a54SThe etnaviv authors mapping->use += 1; 308a8c21a54SThe etnaviv authors else 309a8c21a54SThe etnaviv authors mapping = NULL; 310a8c21a54SThe etnaviv authors mutex_unlock(&gpu->mmu->lock); 311a8c21a54SThe etnaviv authors if (mapping) 312a8c21a54SThe etnaviv authors goto out; 313a8c21a54SThe etnaviv authors } else { 314a8c21a54SThe etnaviv authors mapping->use += 1; 315a8c21a54SThe etnaviv authors goto out; 316a8c21a54SThe etnaviv authors } 317a8c21a54SThe etnaviv authors } 318a8c21a54SThe etnaviv authors 319a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(etnaviv_obj); 320a8c21a54SThe etnaviv authors if (IS_ERR(pages)) { 321a8c21a54SThe etnaviv authors ret = PTR_ERR(pages); 322a8c21a54SThe etnaviv authors goto out; 323a8c21a54SThe etnaviv authors } 324a8c21a54SThe etnaviv authors 325a8c21a54SThe etnaviv authors /* 326a8c21a54SThe etnaviv authors * See if we have a reaped vram mapping we can re-use before 327a8c21a54SThe etnaviv authors * allocating a fresh mapping. 328a8c21a54SThe etnaviv authors */ 329a8c21a54SThe etnaviv authors mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, NULL); 330a8c21a54SThe etnaviv authors if (!mapping) { 331a8c21a54SThe etnaviv authors mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 332ed94add0SDan Carpenter if (!mapping) { 333ed94add0SDan Carpenter ret = -ENOMEM; 334ed94add0SDan Carpenter goto out; 335ed94add0SDan Carpenter } 336a8c21a54SThe etnaviv authors 337a8c21a54SThe etnaviv authors INIT_LIST_HEAD(&mapping->scan_node); 338a8c21a54SThe etnaviv authors mapping->object = etnaviv_obj; 339a8c21a54SThe etnaviv authors } else { 340a8c21a54SThe etnaviv authors list_del(&mapping->obj_node); 341a8c21a54SThe etnaviv authors } 342a8c21a54SThe etnaviv authors 343a8c21a54SThe etnaviv authors mapping->mmu = gpu->mmu; 344a8c21a54SThe etnaviv authors mapping->use = 1; 345a8c21a54SThe etnaviv authors 346a8c21a54SThe etnaviv authors ret = etnaviv_iommu_map_gem(gpu->mmu, etnaviv_obj, gpu->memory_base, 347a8c21a54SThe etnaviv authors mapping); 348a8c21a54SThe etnaviv authors if (ret < 0) 349a8c21a54SThe etnaviv authors kfree(mapping); 350a8c21a54SThe etnaviv authors else 351a8c21a54SThe etnaviv authors list_add_tail(&mapping->obj_node, &etnaviv_obj->vram_list); 352a8c21a54SThe etnaviv authors 353a8c21a54SThe etnaviv authors out: 354a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 355a8c21a54SThe etnaviv authors 356b6325f40SRussell King if (ret) 357b6325f40SRussell King return ERR_PTR(ret); 358b6325f40SRussell King 359a8c21a54SThe etnaviv authors /* Take a reference on the object */ 360a8c21a54SThe etnaviv authors drm_gem_object_reference(obj); 361b6325f40SRussell King return mapping; 362a8c21a54SThe etnaviv authors } 363a8c21a54SThe etnaviv authors 364ce3088fdSLucas Stach void *etnaviv_gem_vmap(struct drm_gem_object *obj) 365a8c21a54SThe etnaviv authors { 366a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 367a8c21a54SThe etnaviv authors 368a0a5ab3eSLucas Stach if (etnaviv_obj->vaddr) 369a0a5ab3eSLucas Stach return etnaviv_obj->vaddr; 370a0a5ab3eSLucas Stach 371a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 372a0a5ab3eSLucas Stach /* 373a0a5ab3eSLucas Stach * Need to check again, as we might have raced with another thread 374a0a5ab3eSLucas Stach * while waiting for the mutex. 375a0a5ab3eSLucas Stach */ 376a0a5ab3eSLucas Stach if (!etnaviv_obj->vaddr) 377a0a5ab3eSLucas Stach etnaviv_obj->vaddr = etnaviv_obj->ops->vmap(etnaviv_obj); 378a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 379a8c21a54SThe etnaviv authors 380a8c21a54SThe etnaviv authors return etnaviv_obj->vaddr; 381a8c21a54SThe etnaviv authors } 382a8c21a54SThe etnaviv authors 383a0a5ab3eSLucas Stach static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj) 384a0a5ab3eSLucas Stach { 385a0a5ab3eSLucas Stach struct page **pages; 386a0a5ab3eSLucas Stach 387a0a5ab3eSLucas Stach lockdep_assert_held(&obj->lock); 388a0a5ab3eSLucas Stach 389a0a5ab3eSLucas Stach pages = etnaviv_gem_get_pages(obj); 390a0a5ab3eSLucas Stach if (IS_ERR(pages)) 391a0a5ab3eSLucas Stach return NULL; 392a0a5ab3eSLucas Stach 393a0a5ab3eSLucas Stach return vmap(pages, obj->base.size >> PAGE_SHIFT, 394a0a5ab3eSLucas Stach VM_MAP, pgprot_writecombine(PAGE_KERNEL)); 395a0a5ab3eSLucas Stach } 396a0a5ab3eSLucas Stach 397a8c21a54SThe etnaviv authors static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op) 398a8c21a54SThe etnaviv authors { 399a8c21a54SThe etnaviv authors if (op & ETNA_PREP_READ) 400a8c21a54SThe etnaviv authors return DMA_FROM_DEVICE; 401a8c21a54SThe etnaviv authors else if (op & ETNA_PREP_WRITE) 402a8c21a54SThe etnaviv authors return DMA_TO_DEVICE; 403a8c21a54SThe etnaviv authors else 404a8c21a54SThe etnaviv authors return DMA_BIDIRECTIONAL; 405a8c21a54SThe etnaviv authors } 406a8c21a54SThe etnaviv authors 407a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op, 408a8c21a54SThe etnaviv authors struct timespec *timeout) 409a8c21a54SThe etnaviv authors { 410a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 411a8c21a54SThe etnaviv authors struct drm_device *dev = obj->dev; 412a8c21a54SThe etnaviv authors bool write = !!(op & ETNA_PREP_WRITE); 413a8c21a54SThe etnaviv authors int ret; 414a8c21a54SThe etnaviv authors 415a8c21a54SThe etnaviv authors if (op & ETNA_PREP_NOSYNC) { 416a8c21a54SThe etnaviv authors if (!reservation_object_test_signaled_rcu(etnaviv_obj->resv, 417a8c21a54SThe etnaviv authors write)) 418a8c21a54SThe etnaviv authors return -EBUSY; 419a8c21a54SThe etnaviv authors } else { 420a8c21a54SThe etnaviv authors unsigned long remain = etnaviv_timeout_to_jiffies(timeout); 421a8c21a54SThe etnaviv authors 422a8c21a54SThe etnaviv authors ret = reservation_object_wait_timeout_rcu(etnaviv_obj->resv, 423a8c21a54SThe etnaviv authors write, true, remain); 424a8c21a54SThe etnaviv authors if (ret <= 0) 425a8c21a54SThe etnaviv authors return ret == 0 ? -ETIMEDOUT : ret; 426a8c21a54SThe etnaviv authors } 427a8c21a54SThe etnaviv authors 428a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHED) { 429a8c21a54SThe etnaviv authors if (!etnaviv_obj->sgt) { 430a8c21a54SThe etnaviv authors void *ret; 431a8c21a54SThe etnaviv authors 432a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 433a8c21a54SThe etnaviv authors ret = etnaviv_gem_get_pages(etnaviv_obj); 434a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 435a8c21a54SThe etnaviv authors if (IS_ERR(ret)) 436a8c21a54SThe etnaviv authors return PTR_ERR(ret); 437a8c21a54SThe etnaviv authors } 438a8c21a54SThe etnaviv authors 439a8c21a54SThe etnaviv authors dma_sync_sg_for_cpu(dev->dev, etnaviv_obj->sgt->sgl, 440a8c21a54SThe etnaviv authors etnaviv_obj->sgt->nents, 441a8c21a54SThe etnaviv authors etnaviv_op_to_dma_dir(op)); 442a8c21a54SThe etnaviv authors etnaviv_obj->last_cpu_prep_op = op; 443a8c21a54SThe etnaviv authors } 444a8c21a54SThe etnaviv authors 445a8c21a54SThe etnaviv authors return 0; 446a8c21a54SThe etnaviv authors } 447a8c21a54SThe etnaviv authors 448a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_fini(struct drm_gem_object *obj) 449a8c21a54SThe etnaviv authors { 450a8c21a54SThe etnaviv authors struct drm_device *dev = obj->dev; 451a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 452a8c21a54SThe etnaviv authors 453a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHED) { 454a8c21a54SThe etnaviv authors /* fini without a prep is almost certainly a userspace error */ 455a8c21a54SThe etnaviv authors WARN_ON(etnaviv_obj->last_cpu_prep_op == 0); 456a8c21a54SThe etnaviv authors dma_sync_sg_for_device(dev->dev, etnaviv_obj->sgt->sgl, 457a8c21a54SThe etnaviv authors etnaviv_obj->sgt->nents, 458a8c21a54SThe etnaviv authors etnaviv_op_to_dma_dir(etnaviv_obj->last_cpu_prep_op)); 459a8c21a54SThe etnaviv authors etnaviv_obj->last_cpu_prep_op = 0; 460a8c21a54SThe etnaviv authors } 461a8c21a54SThe etnaviv authors 462a8c21a54SThe etnaviv authors return 0; 463a8c21a54SThe etnaviv authors } 464a8c21a54SThe etnaviv authors 465a8c21a54SThe etnaviv authors int etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj, 466a8c21a54SThe etnaviv authors struct timespec *timeout) 467a8c21a54SThe etnaviv authors { 468a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 469a8c21a54SThe etnaviv authors 470a8c21a54SThe etnaviv authors return etnaviv_gpu_wait_obj_inactive(gpu, etnaviv_obj, timeout); 471a8c21a54SThe etnaviv authors } 472a8c21a54SThe etnaviv authors 473a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS 474a8c21a54SThe etnaviv authors static void etnaviv_gem_describe_fence(struct fence *fence, 475a8c21a54SThe etnaviv authors const char *type, struct seq_file *m) 476a8c21a54SThe etnaviv authors { 477a8c21a54SThe etnaviv authors if (!test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 478a8c21a54SThe etnaviv authors seq_printf(m, "\t%9s: %s %s seq %u\n", 479a8c21a54SThe etnaviv authors type, 480a8c21a54SThe etnaviv authors fence->ops->get_driver_name(fence), 481a8c21a54SThe etnaviv authors fence->ops->get_timeline_name(fence), 482a8c21a54SThe etnaviv authors fence->seqno); 483a8c21a54SThe etnaviv authors } 484a8c21a54SThe etnaviv authors 485a8c21a54SThe etnaviv authors static void etnaviv_gem_describe(struct drm_gem_object *obj, struct seq_file *m) 486a8c21a54SThe etnaviv authors { 487a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 488a8c21a54SThe etnaviv authors struct reservation_object *robj = etnaviv_obj->resv; 489a8c21a54SThe etnaviv authors struct reservation_object_list *fobj; 490a8c21a54SThe etnaviv authors struct fence *fence; 491a8c21a54SThe etnaviv authors unsigned long off = drm_vma_node_start(&obj->vma_node); 492a8c21a54SThe etnaviv authors 493a8c21a54SThe etnaviv authors seq_printf(m, "%08x: %c %2d (%2d) %08lx %p %zd\n", 494a8c21a54SThe etnaviv authors etnaviv_obj->flags, is_active(etnaviv_obj) ? 'A' : 'I', 495a8c21a54SThe etnaviv authors obj->name, obj->refcount.refcount.counter, 496a8c21a54SThe etnaviv authors off, etnaviv_obj->vaddr, obj->size); 497a8c21a54SThe etnaviv authors 498a8c21a54SThe etnaviv authors rcu_read_lock(); 499a8c21a54SThe etnaviv authors fobj = rcu_dereference(robj->fence); 500a8c21a54SThe etnaviv authors if (fobj) { 501a8c21a54SThe etnaviv authors unsigned int i, shared_count = fobj->shared_count; 502a8c21a54SThe etnaviv authors 503a8c21a54SThe etnaviv authors for (i = 0; i < shared_count; i++) { 504a8c21a54SThe etnaviv authors fence = rcu_dereference(fobj->shared[i]); 505a8c21a54SThe etnaviv authors etnaviv_gem_describe_fence(fence, "Shared", m); 506a8c21a54SThe etnaviv authors } 507a8c21a54SThe etnaviv authors } 508a8c21a54SThe etnaviv authors 509a8c21a54SThe etnaviv authors fence = rcu_dereference(robj->fence_excl); 510a8c21a54SThe etnaviv authors if (fence) 511a8c21a54SThe etnaviv authors etnaviv_gem_describe_fence(fence, "Exclusive", m); 512a8c21a54SThe etnaviv authors rcu_read_unlock(); 513a8c21a54SThe etnaviv authors } 514a8c21a54SThe etnaviv authors 515a8c21a54SThe etnaviv authors void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv, 516a8c21a54SThe etnaviv authors struct seq_file *m) 517a8c21a54SThe etnaviv authors { 518a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 519a8c21a54SThe etnaviv authors int count = 0; 520a8c21a54SThe etnaviv authors size_t size = 0; 521a8c21a54SThe etnaviv authors 522a8c21a54SThe etnaviv authors mutex_lock(&priv->gem_lock); 523a8c21a54SThe etnaviv authors list_for_each_entry(etnaviv_obj, &priv->gem_list, gem_node) { 524a8c21a54SThe etnaviv authors struct drm_gem_object *obj = &etnaviv_obj->base; 525a8c21a54SThe etnaviv authors 526a8c21a54SThe etnaviv authors seq_puts(m, " "); 527a8c21a54SThe etnaviv authors etnaviv_gem_describe(obj, m); 528a8c21a54SThe etnaviv authors count++; 529a8c21a54SThe etnaviv authors size += obj->size; 530a8c21a54SThe etnaviv authors } 531a8c21a54SThe etnaviv authors mutex_unlock(&priv->gem_lock); 532a8c21a54SThe etnaviv authors 533a8c21a54SThe etnaviv authors seq_printf(m, "Total %d objects, %zu bytes\n", count, size); 534a8c21a54SThe etnaviv authors } 535a8c21a54SThe etnaviv authors #endif 536a8c21a54SThe etnaviv authors 537a8c21a54SThe etnaviv authors static void etnaviv_gem_shmem_release(struct etnaviv_gem_object *etnaviv_obj) 538a8c21a54SThe etnaviv authors { 539a8c21a54SThe etnaviv authors if (etnaviv_obj->vaddr) 540a8c21a54SThe etnaviv authors vunmap(etnaviv_obj->vaddr); 541a8c21a54SThe etnaviv authors put_pages(etnaviv_obj); 542a8c21a54SThe etnaviv authors } 543a8c21a54SThe etnaviv authors 544a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_shmem_ops = { 545a8c21a54SThe etnaviv authors .get_pages = etnaviv_gem_shmem_get_pages, 546a8c21a54SThe etnaviv authors .release = etnaviv_gem_shmem_release, 547a0a5ab3eSLucas Stach .vmap = etnaviv_gem_vmap_impl, 548a8c21a54SThe etnaviv authors }; 549a8c21a54SThe etnaviv authors 550a8c21a54SThe etnaviv authors void etnaviv_gem_free_object(struct drm_gem_object *obj) 551a8c21a54SThe etnaviv authors { 552a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 553a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping, *tmp; 554a8c21a54SThe etnaviv authors 555a8c21a54SThe etnaviv authors /* object should not be active */ 556a8c21a54SThe etnaviv authors WARN_ON(is_active(etnaviv_obj)); 557a8c21a54SThe etnaviv authors 558a8c21a54SThe etnaviv authors list_del(&etnaviv_obj->gem_node); 559a8c21a54SThe etnaviv authors 560a8c21a54SThe etnaviv authors list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list, 561a8c21a54SThe etnaviv authors obj_node) { 562a8c21a54SThe etnaviv authors struct etnaviv_iommu *mmu = mapping->mmu; 563a8c21a54SThe etnaviv authors 564a8c21a54SThe etnaviv authors WARN_ON(mapping->use); 565a8c21a54SThe etnaviv authors 566a8c21a54SThe etnaviv authors if (mmu) 567a8c21a54SThe etnaviv authors etnaviv_iommu_unmap_gem(mmu, mapping); 568a8c21a54SThe etnaviv authors 569a8c21a54SThe etnaviv authors list_del(&mapping->obj_node); 570a8c21a54SThe etnaviv authors kfree(mapping); 571a8c21a54SThe etnaviv authors } 572a8c21a54SThe etnaviv authors 573a8c21a54SThe etnaviv authors drm_gem_free_mmap_offset(obj); 574a8c21a54SThe etnaviv authors etnaviv_obj->ops->release(etnaviv_obj); 575a8c21a54SThe etnaviv authors if (etnaviv_obj->resv == &etnaviv_obj->_resv) 576a8c21a54SThe etnaviv authors reservation_object_fini(&etnaviv_obj->_resv); 577a8c21a54SThe etnaviv authors drm_gem_object_release(obj); 578a8c21a54SThe etnaviv authors 579a8c21a54SThe etnaviv authors kfree(etnaviv_obj); 580a8c21a54SThe etnaviv authors } 581a8c21a54SThe etnaviv authors 582a8c21a54SThe etnaviv authors int etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj) 583a8c21a54SThe etnaviv authors { 584a8c21a54SThe etnaviv authors struct etnaviv_drm_private *priv = dev->dev_private; 585a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 586a8c21a54SThe etnaviv authors 587a8c21a54SThe etnaviv authors mutex_lock(&priv->gem_lock); 588a8c21a54SThe etnaviv authors list_add_tail(&etnaviv_obj->gem_node, &priv->gem_list); 589a8c21a54SThe etnaviv authors mutex_unlock(&priv->gem_lock); 590a8c21a54SThe etnaviv authors 591a8c21a54SThe etnaviv authors return 0; 592a8c21a54SThe etnaviv authors } 593a8c21a54SThe etnaviv authors 594a8c21a54SThe etnaviv authors static int etnaviv_gem_new_impl(struct drm_device *dev, u32 size, u32 flags, 595a8c21a54SThe etnaviv authors struct reservation_object *robj, const struct etnaviv_gem_ops *ops, 596a8c21a54SThe etnaviv authors struct drm_gem_object **obj) 597a8c21a54SThe etnaviv authors { 598a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 599a8c21a54SThe etnaviv authors unsigned sz = sizeof(*etnaviv_obj); 600a8c21a54SThe etnaviv authors bool valid = true; 601a8c21a54SThe etnaviv authors 602a8c21a54SThe etnaviv authors /* validate flags */ 603a8c21a54SThe etnaviv authors switch (flags & ETNA_BO_CACHE_MASK) { 604a8c21a54SThe etnaviv authors case ETNA_BO_UNCACHED: 605a8c21a54SThe etnaviv authors case ETNA_BO_CACHED: 606a8c21a54SThe etnaviv authors case ETNA_BO_WC: 607a8c21a54SThe etnaviv authors break; 608a8c21a54SThe etnaviv authors default: 609a8c21a54SThe etnaviv authors valid = false; 610a8c21a54SThe etnaviv authors } 611a8c21a54SThe etnaviv authors 612a8c21a54SThe etnaviv authors if (!valid) { 613a8c21a54SThe etnaviv authors dev_err(dev->dev, "invalid cache flag: %x\n", 614a8c21a54SThe etnaviv authors (flags & ETNA_BO_CACHE_MASK)); 615a8c21a54SThe etnaviv authors return -EINVAL; 616a8c21a54SThe etnaviv authors } 617a8c21a54SThe etnaviv authors 618a8c21a54SThe etnaviv authors etnaviv_obj = kzalloc(sz, GFP_KERNEL); 619a8c21a54SThe etnaviv authors if (!etnaviv_obj) 620a8c21a54SThe etnaviv authors return -ENOMEM; 621a8c21a54SThe etnaviv authors 622a8c21a54SThe etnaviv authors etnaviv_obj->flags = flags; 623a8c21a54SThe etnaviv authors etnaviv_obj->ops = ops; 624a8c21a54SThe etnaviv authors if (robj) { 625a8c21a54SThe etnaviv authors etnaviv_obj->resv = robj; 626a8c21a54SThe etnaviv authors } else { 627a8c21a54SThe etnaviv authors etnaviv_obj->resv = &etnaviv_obj->_resv; 628a8c21a54SThe etnaviv authors reservation_object_init(&etnaviv_obj->_resv); 629a8c21a54SThe etnaviv authors } 630a8c21a54SThe etnaviv authors 631a8c21a54SThe etnaviv authors mutex_init(&etnaviv_obj->lock); 632a8c21a54SThe etnaviv authors INIT_LIST_HEAD(&etnaviv_obj->vram_list); 633a8c21a54SThe etnaviv authors 634a8c21a54SThe etnaviv authors *obj = &etnaviv_obj->base; 635a8c21a54SThe etnaviv authors 636a8c21a54SThe etnaviv authors return 0; 637a8c21a54SThe etnaviv authors } 638a8c21a54SThe etnaviv authors 639a8c21a54SThe etnaviv authors static struct drm_gem_object *__etnaviv_gem_new(struct drm_device *dev, 640a8c21a54SThe etnaviv authors u32 size, u32 flags) 641a8c21a54SThe etnaviv authors { 642a8c21a54SThe etnaviv authors struct drm_gem_object *obj = NULL; 643a8c21a54SThe etnaviv authors int ret; 644a8c21a54SThe etnaviv authors 645a8c21a54SThe etnaviv authors size = PAGE_ALIGN(size); 646a8c21a54SThe etnaviv authors 647a8c21a54SThe etnaviv authors ret = etnaviv_gem_new_impl(dev, size, flags, NULL, 648a8c21a54SThe etnaviv authors &etnaviv_gem_shmem_ops, &obj); 649a8c21a54SThe etnaviv authors if (ret) 650a8c21a54SThe etnaviv authors goto fail; 651a8c21a54SThe etnaviv authors 652a8c21a54SThe etnaviv authors ret = drm_gem_object_init(dev, obj, size); 653a8c21a54SThe etnaviv authors if (ret == 0) { 654a8c21a54SThe etnaviv authors struct address_space *mapping; 655a8c21a54SThe etnaviv authors 656a8c21a54SThe etnaviv authors /* 657a8c21a54SThe etnaviv authors * Our buffers are kept pinned, so allocating them 658a8c21a54SThe etnaviv authors * from the MOVABLE zone is a really bad idea, and 659a8c21a54SThe etnaviv authors * conflicts with CMA. See coments above new_inode() 660a8c21a54SThe etnaviv authors * why this is required _and_ expected if you're 661a8c21a54SThe etnaviv authors * going to pin these pages. 662a8c21a54SThe etnaviv authors */ 663a8c21a54SThe etnaviv authors mapping = file_inode(obj->filp)->i_mapping; 664a8c21a54SThe etnaviv authors mapping_set_gfp_mask(mapping, GFP_HIGHUSER); 665a8c21a54SThe etnaviv authors } 666a8c21a54SThe etnaviv authors 667a8c21a54SThe etnaviv authors if (ret) 668a8c21a54SThe etnaviv authors goto fail; 669a8c21a54SThe etnaviv authors 670a8c21a54SThe etnaviv authors return obj; 671a8c21a54SThe etnaviv authors 672a8c21a54SThe etnaviv authors fail: 673a8c21a54SThe etnaviv authors if (obj) 674a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 675a8c21a54SThe etnaviv authors 676a8c21a54SThe etnaviv authors return ERR_PTR(ret); 677a8c21a54SThe etnaviv authors } 678a8c21a54SThe etnaviv authors 679a8c21a54SThe etnaviv authors /* convenience method to construct a GEM buffer object, and userspace handle */ 680a8c21a54SThe etnaviv authors int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file, 681a8c21a54SThe etnaviv authors u32 size, u32 flags, u32 *handle) 682a8c21a54SThe etnaviv authors { 683a8c21a54SThe etnaviv authors struct drm_gem_object *obj; 684a8c21a54SThe etnaviv authors int ret; 685a8c21a54SThe etnaviv authors 686a8c21a54SThe etnaviv authors obj = __etnaviv_gem_new(dev, size, flags); 687a8c21a54SThe etnaviv authors if (IS_ERR(obj)) 688a8c21a54SThe etnaviv authors return PTR_ERR(obj); 689a8c21a54SThe etnaviv authors 690a8c21a54SThe etnaviv authors ret = etnaviv_gem_obj_add(dev, obj); 691a8c21a54SThe etnaviv authors if (ret < 0) { 692a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 693a8c21a54SThe etnaviv authors return ret; 694a8c21a54SThe etnaviv authors } 695a8c21a54SThe etnaviv authors 696a8c21a54SThe etnaviv authors ret = drm_gem_handle_create(file, obj, handle); 697a8c21a54SThe etnaviv authors 698a8c21a54SThe etnaviv authors /* drop reference from allocate - handle holds it now */ 699a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 700a8c21a54SThe etnaviv authors 701a8c21a54SThe etnaviv authors return ret; 702a8c21a54SThe etnaviv authors } 703a8c21a54SThe etnaviv authors 704a8c21a54SThe etnaviv authors struct drm_gem_object *etnaviv_gem_new(struct drm_device *dev, 705a8c21a54SThe etnaviv authors u32 size, u32 flags) 706a8c21a54SThe etnaviv authors { 707a8c21a54SThe etnaviv authors struct drm_gem_object *obj; 708a8c21a54SThe etnaviv authors int ret; 709a8c21a54SThe etnaviv authors 710a8c21a54SThe etnaviv authors obj = __etnaviv_gem_new(dev, size, flags); 711a8c21a54SThe etnaviv authors if (IS_ERR(obj)) 712a8c21a54SThe etnaviv authors return obj; 713a8c21a54SThe etnaviv authors 714a8c21a54SThe etnaviv authors ret = etnaviv_gem_obj_add(dev, obj); 715a8c21a54SThe etnaviv authors if (ret < 0) { 716a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(obj); 717a8c21a54SThe etnaviv authors return ERR_PTR(ret); 718a8c21a54SThe etnaviv authors } 719a8c21a54SThe etnaviv authors 720a8c21a54SThe etnaviv authors return obj; 721a8c21a54SThe etnaviv authors } 722a8c21a54SThe etnaviv authors 723a8c21a54SThe etnaviv authors int etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags, 724a8c21a54SThe etnaviv authors struct reservation_object *robj, const struct etnaviv_gem_ops *ops, 725a8c21a54SThe etnaviv authors struct etnaviv_gem_object **res) 726a8c21a54SThe etnaviv authors { 727a8c21a54SThe etnaviv authors struct drm_gem_object *obj; 728a8c21a54SThe etnaviv authors int ret; 729a8c21a54SThe etnaviv authors 730a8c21a54SThe etnaviv authors ret = etnaviv_gem_new_impl(dev, size, flags, robj, ops, &obj); 731a8c21a54SThe etnaviv authors if (ret) 732a8c21a54SThe etnaviv authors return ret; 733a8c21a54SThe etnaviv authors 734a8c21a54SThe etnaviv authors drm_gem_private_object_init(dev, obj, size); 735a8c21a54SThe etnaviv authors 736a8c21a54SThe etnaviv authors *res = to_etnaviv_bo(obj); 737a8c21a54SThe etnaviv authors 738a8c21a54SThe etnaviv authors return 0; 739a8c21a54SThe etnaviv authors } 740a8c21a54SThe etnaviv authors 741a8c21a54SThe etnaviv authors struct get_pages_work { 742a8c21a54SThe etnaviv authors struct work_struct work; 743a8c21a54SThe etnaviv authors struct mm_struct *mm; 744a8c21a54SThe etnaviv authors struct task_struct *task; 745a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 746a8c21a54SThe etnaviv authors }; 747a8c21a54SThe etnaviv authors 748a8c21a54SThe etnaviv authors static struct page **etnaviv_gem_userptr_do_get_pages( 749a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj, struct mm_struct *mm, struct task_struct *task) 750a8c21a54SThe etnaviv authors { 751a8c21a54SThe etnaviv authors int ret = 0, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT; 752a8c21a54SThe etnaviv authors struct page **pvec; 753a8c21a54SThe etnaviv authors uintptr_t ptr; 754a8c21a54SThe etnaviv authors 755a8c21a54SThe etnaviv authors pvec = drm_malloc_ab(npages, sizeof(struct page *)); 756a8c21a54SThe etnaviv authors if (!pvec) 757a8c21a54SThe etnaviv authors return ERR_PTR(-ENOMEM); 758a8c21a54SThe etnaviv authors 759a8c21a54SThe etnaviv authors pinned = 0; 760a8c21a54SThe etnaviv authors ptr = etnaviv_obj->userptr.ptr; 761a8c21a54SThe etnaviv authors 762a8c21a54SThe etnaviv authors down_read(&mm->mmap_sem); 763a8c21a54SThe etnaviv authors while (pinned < npages) { 764a8c21a54SThe etnaviv authors ret = get_user_pages(task, mm, ptr, npages - pinned, 765a8c21a54SThe etnaviv authors !etnaviv_obj->userptr.ro, 0, 766a8c21a54SThe etnaviv authors pvec + pinned, NULL); 767a8c21a54SThe etnaviv authors if (ret < 0) 768a8c21a54SThe etnaviv authors break; 769a8c21a54SThe etnaviv authors 770a8c21a54SThe etnaviv authors ptr += ret * PAGE_SIZE; 771a8c21a54SThe etnaviv authors pinned += ret; 772a8c21a54SThe etnaviv authors } 773a8c21a54SThe etnaviv authors up_read(&mm->mmap_sem); 774a8c21a54SThe etnaviv authors 775a8c21a54SThe etnaviv authors if (ret < 0) { 776a8c21a54SThe etnaviv authors release_pages(pvec, pinned, 0); 777a8c21a54SThe etnaviv authors drm_free_large(pvec); 778a8c21a54SThe etnaviv authors return ERR_PTR(ret); 779a8c21a54SThe etnaviv authors } 780a8c21a54SThe etnaviv authors 781a8c21a54SThe etnaviv authors return pvec; 782a8c21a54SThe etnaviv authors } 783a8c21a54SThe etnaviv authors 784a8c21a54SThe etnaviv authors static void __etnaviv_gem_userptr_get_pages(struct work_struct *_work) 785a8c21a54SThe etnaviv authors { 786a8c21a54SThe etnaviv authors struct get_pages_work *work = container_of(_work, typeof(*work), work); 787a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = work->etnaviv_obj; 788a8c21a54SThe etnaviv authors struct page **pvec; 789a8c21a54SThe etnaviv authors 790a8c21a54SThe etnaviv authors pvec = etnaviv_gem_userptr_do_get_pages(etnaviv_obj, work->mm, work->task); 791a8c21a54SThe etnaviv authors 792a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock); 793a8c21a54SThe etnaviv authors if (IS_ERR(pvec)) { 794a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = ERR_CAST(pvec); 795a8c21a54SThe etnaviv authors } else { 796a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = NULL; 797a8c21a54SThe etnaviv authors etnaviv_obj->pages = pvec; 798a8c21a54SThe etnaviv authors } 799a8c21a54SThe etnaviv authors 800a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock); 801a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 802a8c21a54SThe etnaviv authors 803a8c21a54SThe etnaviv authors mmput(work->mm); 804a8c21a54SThe etnaviv authors put_task_struct(work->task); 805a8c21a54SThe etnaviv authors kfree(work); 806a8c21a54SThe etnaviv authors } 807a8c21a54SThe etnaviv authors 808a8c21a54SThe etnaviv authors static int etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object *etnaviv_obj) 809a8c21a54SThe etnaviv authors { 810a8c21a54SThe etnaviv authors struct page **pvec = NULL; 811a8c21a54SThe etnaviv authors struct get_pages_work *work; 812a8c21a54SThe etnaviv authors struct mm_struct *mm; 813a8c21a54SThe etnaviv authors int ret, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT; 814a8c21a54SThe etnaviv authors 815a8c21a54SThe etnaviv authors if (etnaviv_obj->userptr.work) { 816a8c21a54SThe etnaviv authors if (IS_ERR(etnaviv_obj->userptr.work)) { 817a8c21a54SThe etnaviv authors ret = PTR_ERR(etnaviv_obj->userptr.work); 818a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = NULL; 819a8c21a54SThe etnaviv authors } else { 820a8c21a54SThe etnaviv authors ret = -EAGAIN; 821a8c21a54SThe etnaviv authors } 822a8c21a54SThe etnaviv authors return ret; 823a8c21a54SThe etnaviv authors } 824a8c21a54SThe etnaviv authors 825a8c21a54SThe etnaviv authors mm = get_task_mm(etnaviv_obj->userptr.task); 826a8c21a54SThe etnaviv authors pinned = 0; 827a8c21a54SThe etnaviv authors if (mm == current->mm) { 828a8c21a54SThe etnaviv authors pvec = drm_malloc_ab(npages, sizeof(struct page *)); 829a8c21a54SThe etnaviv authors if (!pvec) { 830a8c21a54SThe etnaviv authors mmput(mm); 831a8c21a54SThe etnaviv authors return -ENOMEM; 832a8c21a54SThe etnaviv authors } 833a8c21a54SThe etnaviv authors 834a8c21a54SThe etnaviv authors pinned = __get_user_pages_fast(etnaviv_obj->userptr.ptr, npages, 835a8c21a54SThe etnaviv authors !etnaviv_obj->userptr.ro, pvec); 836a8c21a54SThe etnaviv authors if (pinned < 0) { 837a8c21a54SThe etnaviv authors drm_free_large(pvec); 838a8c21a54SThe etnaviv authors mmput(mm); 839a8c21a54SThe etnaviv authors return pinned; 840a8c21a54SThe etnaviv authors } 841a8c21a54SThe etnaviv authors 842a8c21a54SThe etnaviv authors if (pinned == npages) { 843a8c21a54SThe etnaviv authors etnaviv_obj->pages = pvec; 844a8c21a54SThe etnaviv authors mmput(mm); 845a8c21a54SThe etnaviv authors return 0; 846a8c21a54SThe etnaviv authors } 847a8c21a54SThe etnaviv authors } 848a8c21a54SThe etnaviv authors 849a8c21a54SThe etnaviv authors release_pages(pvec, pinned, 0); 850a8c21a54SThe etnaviv authors drm_free_large(pvec); 851a8c21a54SThe etnaviv authors 852a8c21a54SThe etnaviv authors work = kmalloc(sizeof(*work), GFP_KERNEL); 853a8c21a54SThe etnaviv authors if (!work) { 854a8c21a54SThe etnaviv authors mmput(mm); 855a8c21a54SThe etnaviv authors return -ENOMEM; 856a8c21a54SThe etnaviv authors } 857a8c21a54SThe etnaviv authors 858a8c21a54SThe etnaviv authors get_task_struct(current); 859a8c21a54SThe etnaviv authors drm_gem_object_reference(&etnaviv_obj->base); 860a8c21a54SThe etnaviv authors 861a8c21a54SThe etnaviv authors work->mm = mm; 862a8c21a54SThe etnaviv authors work->task = current; 863a8c21a54SThe etnaviv authors work->etnaviv_obj = etnaviv_obj; 864a8c21a54SThe etnaviv authors 865a8c21a54SThe etnaviv authors etnaviv_obj->userptr.work = &work->work; 866a8c21a54SThe etnaviv authors INIT_WORK(&work->work, __etnaviv_gem_userptr_get_pages); 867a8c21a54SThe etnaviv authors 868a8c21a54SThe etnaviv authors etnaviv_queue_work(etnaviv_obj->base.dev, &work->work); 869a8c21a54SThe etnaviv authors 870a8c21a54SThe etnaviv authors return -EAGAIN; 871a8c21a54SThe etnaviv authors } 872a8c21a54SThe etnaviv authors 873a8c21a54SThe etnaviv authors static void etnaviv_gem_userptr_release(struct etnaviv_gem_object *etnaviv_obj) 874a8c21a54SThe etnaviv authors { 875a8c21a54SThe etnaviv authors if (etnaviv_obj->sgt) { 876a8c21a54SThe etnaviv authors etnaviv_gem_scatterlist_unmap(etnaviv_obj); 877a8c21a54SThe etnaviv authors sg_free_table(etnaviv_obj->sgt); 878a8c21a54SThe etnaviv authors kfree(etnaviv_obj->sgt); 879a8c21a54SThe etnaviv authors } 880a8c21a54SThe etnaviv authors if (etnaviv_obj->pages) { 881a8c21a54SThe etnaviv authors int npages = etnaviv_obj->base.size >> PAGE_SHIFT; 882a8c21a54SThe etnaviv authors 883a8c21a54SThe etnaviv authors release_pages(etnaviv_obj->pages, npages, 0); 884a8c21a54SThe etnaviv authors drm_free_large(etnaviv_obj->pages); 885a8c21a54SThe etnaviv authors } 886a8c21a54SThe etnaviv authors put_task_struct(etnaviv_obj->userptr.task); 887a8c21a54SThe etnaviv authors } 888a8c21a54SThe etnaviv authors 889a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_userptr_ops = { 890a8c21a54SThe etnaviv authors .get_pages = etnaviv_gem_userptr_get_pages, 891a8c21a54SThe etnaviv authors .release = etnaviv_gem_userptr_release, 892a0a5ab3eSLucas Stach .vmap = etnaviv_gem_vmap_impl, 893a8c21a54SThe etnaviv authors }; 894a8c21a54SThe etnaviv authors 895a8c21a54SThe etnaviv authors int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file, 896a8c21a54SThe etnaviv authors uintptr_t ptr, u32 size, u32 flags, u32 *handle) 897a8c21a54SThe etnaviv authors { 898a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj; 899a8c21a54SThe etnaviv authors int ret; 900a8c21a54SThe etnaviv authors 901a8c21a54SThe etnaviv authors ret = etnaviv_gem_new_private(dev, size, ETNA_BO_CACHED, NULL, 902a8c21a54SThe etnaviv authors &etnaviv_gem_userptr_ops, &etnaviv_obj); 903a8c21a54SThe etnaviv authors if (ret) 904a8c21a54SThe etnaviv authors return ret; 905a8c21a54SThe etnaviv authors 906a8c21a54SThe etnaviv authors etnaviv_obj->userptr.ptr = ptr; 907a8c21a54SThe etnaviv authors etnaviv_obj->userptr.task = current; 908a8c21a54SThe etnaviv authors etnaviv_obj->userptr.ro = !(flags & ETNA_USERPTR_WRITE); 909a8c21a54SThe etnaviv authors get_task_struct(current); 910a8c21a54SThe etnaviv authors 911a8c21a54SThe etnaviv authors ret = etnaviv_gem_obj_add(dev, &etnaviv_obj->base); 912a8c21a54SThe etnaviv authors if (ret) { 913a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 914a8c21a54SThe etnaviv authors return ret; 915a8c21a54SThe etnaviv authors } 916a8c21a54SThe etnaviv authors 917a8c21a54SThe etnaviv authors ret = drm_gem_handle_create(file, &etnaviv_obj->base, handle); 918a8c21a54SThe etnaviv authors 919a8c21a54SThe etnaviv authors /* drop reference from allocate - handle holds it now */ 920a8c21a54SThe etnaviv authors drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 921a8c21a54SThe etnaviv authors 922a8c21a54SThe etnaviv authors return ret; 923a8c21a54SThe etnaviv authors } 924