1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
5 #include <linux/slab.h>
6 #include <linux/dma-buf.h>
7 #include <linux/dma-mapping.h>
9 #include <drm/panfrost_drm.h>
10 #include "panfrost_device.h"
11 #include "panfrost_gem.h"
12 #include "panfrost_mmu.h"
14 /* Called DRM core on the last userspace/kernel unreference of the
17 static void panfrost_gem_free_object(struct drm_gem_object *obj)
19 struct panfrost_gem_object *bo = to_panfrost_bo(obj);
20 struct panfrost_device *pfdev = obj->dev->dev_private;
23 panfrost_mmu_unmap(bo);
25 spin_lock(&pfdev->mm_lock);
26 drm_mm_remove_node(&bo->node);
27 spin_unlock(&pfdev->mm_lock);
29 drm_gem_shmem_free_object(obj);
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,
44 * panfrost_gem_create_object - Implementation of driver->gem_create_object.
46 * @size: Size in bytes of the memory the object will reference
48 * This lets the GEM helpers allocate object structs for us, and keep
49 * our BO stats correct.
51 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size)
54 struct panfrost_device *pfdev = dev->dev_private;
55 struct panfrost_gem_object *obj;
58 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
62 obj->base.base.funcs = &panfrost_gem_funcs;
64 size = roundup(size, PAGE_SIZE);
65 align = size >= SZ_2M ? SZ_2M >> PAGE_SHIFT : 0;
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);
74 return &obj->base.base;
81 struct drm_gem_object *
82 panfrost_gem_prime_import_sg_table(struct drm_device *dev,
83 struct dma_buf_attachment *attach,
86 struct drm_gem_object *obj;
87 struct panfrost_gem_object *pobj;
89 obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
93 pobj = to_panfrost_bo(obj);
95 obj->resv = attach->dmabuf->resv;
97 panfrost_mmu_map(pobj);