1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */ 3 4 #include <linux/err.h> 5 #include <linux/slab.h> 6 #include <linux/dma-buf.h> 7 #include <linux/dma-mapping.h> 8 9 #include <drm/panfrost_drm.h> 10 #include "panfrost_device.h" 11 #include "panfrost_gem.h" 12 #include "panfrost_mmu.h" 13 14 /* Called DRM core on the last userspace/kernel unreference of the 15 * BO. 16 */ 17 static void panfrost_gem_free_object(struct drm_gem_object *obj) 18 { 19 struct panfrost_gem_object *bo = to_panfrost_bo(obj); 20 struct panfrost_device *pfdev = obj->dev->dev_private; 21 22 if (bo->is_mapped) 23 panfrost_mmu_unmap(bo); 24 25 spin_lock(&pfdev->mm_lock); 26 drm_mm_remove_node(&bo->node); 27 spin_unlock(&pfdev->mm_lock); 28 29 drm_gem_shmem_free_object(obj); 30 } 31 32 static const struct drm_gem_object_funcs panfrost_gem_funcs = { 33 .free = panfrost_gem_free_object, 34 .print_info = drm_gem_shmem_print_info, 35 .pin = drm_gem_shmem_pin, 36 .unpin = drm_gem_shmem_unpin, 37 .get_sg_table = drm_gem_shmem_get_sg_table, 38 .vmap = drm_gem_shmem_vmap, 39 .vunmap = drm_gem_shmem_vunmap, 40 .vm_ops = &drm_gem_shmem_vm_ops, 41 }; 42 43 /** 44 * panfrost_gem_create_object - Implementation of driver->gem_create_object. 45 * @dev: DRM device 46 * @size: Size in bytes of the memory the object will reference 47 * 48 * This lets the GEM helpers allocate object structs for us, and keep 49 * our BO stats correct. 50 */ 51 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size) 52 { 53 int ret; 54 struct panfrost_device *pfdev = dev->dev_private; 55 struct panfrost_gem_object *obj; 56 u64 align; 57 58 obj = kzalloc(sizeof(*obj), GFP_KERNEL); 59 if (!obj) 60 return NULL; 61 62 obj->base.base.funcs = &panfrost_gem_funcs; 63 64 size = roundup(size, PAGE_SIZE); 65 align = size >= SZ_2M ? SZ_2M >> PAGE_SHIFT : 0; 66 67 spin_lock(&pfdev->mm_lock); 68 ret = drm_mm_insert_node_generic(&pfdev->mm, &obj->node, 69 size >> PAGE_SHIFT, align, 0, 0); 70 spin_unlock(&pfdev->mm_lock); 71 if (ret) 72 goto free_obj; 73 74 return &obj->base.base; 75 76 free_obj: 77 kfree(obj); 78 return ERR_PTR(ret); 79 } 80 81 struct drm_gem_object * 82 panfrost_gem_prime_import_sg_table(struct drm_device *dev, 83 struct dma_buf_attachment *attach, 84 struct sg_table *sgt) 85 { 86 struct drm_gem_object *obj; 87 struct panfrost_gem_object *pobj; 88 89 obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt); 90 if (IS_ERR(obj)) 91 return ERR_CAST(obj); 92 93 pobj = to_panfrost_bo(obj); 94 95 obj->resv = attach->dmabuf->resv; 96 97 panfrost_mmu_map(pobj); 98 99 return obj; 100 } 101