ARC: [plat-hsdk]: unify memory apertures configuration
[linux-2.6-microblaze.git] / drivers / gpu / drm / panfrost / panfrost_gem.c
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         panfrost_mmu_unmap(bo);
23
24         spin_lock(&pfdev->mm_lock);
25         drm_mm_remove_node(&bo->node);
26         spin_unlock(&pfdev->mm_lock);
27
28         drm_gem_shmem_free_object(obj);
29 }
30
31 static const struct drm_gem_object_funcs panfrost_gem_funcs = {
32         .free = panfrost_gem_free_object,
33         .print_info = drm_gem_shmem_print_info,
34         .pin = drm_gem_shmem_pin,
35         .unpin = drm_gem_shmem_unpin,
36         .get_sg_table = drm_gem_shmem_get_sg_table,
37         .vmap = drm_gem_shmem_vmap,
38         .vunmap = drm_gem_shmem_vunmap,
39         .vm_ops = &drm_gem_shmem_vm_ops,
40 };
41
42 /**
43  * panfrost_gem_create_object - Implementation of driver->gem_create_object.
44  * @dev: DRM device
45  * @size: Size in bytes of the memory the object will reference
46  *
47  * This lets the GEM helpers allocate object structs for us, and keep
48  * our BO stats correct.
49  */
50 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size)
51 {
52         int ret;
53         struct panfrost_device *pfdev = dev->dev_private;
54         struct panfrost_gem_object *obj;
55
56         obj = kzalloc(sizeof(*obj), GFP_KERNEL);
57         if (!obj)
58                 return NULL;
59
60         obj->base.base.funcs = &panfrost_gem_funcs;
61
62         spin_lock(&pfdev->mm_lock);
63         ret = drm_mm_insert_node(&pfdev->mm, &obj->node,
64                                  roundup(size, PAGE_SIZE) >> PAGE_SHIFT);
65         spin_unlock(&pfdev->mm_lock);
66         if (ret)
67                 goto free_obj;
68
69         return &obj->base.base;
70
71 free_obj:
72         kfree(obj);
73         return ERR_PTR(ret);
74 }
75
76 struct drm_gem_object *
77 panfrost_gem_prime_import_sg_table(struct drm_device *dev,
78                                    struct dma_buf_attachment *attach,
79                                    struct sg_table *sgt)
80 {
81         struct drm_gem_object *obj;
82         struct panfrost_gem_object *pobj;
83
84         obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
85         if (IS_ERR(obj))
86                 return ERR_CAST(obj);
87
88         pobj = to_panfrost_bo(obj);
89
90         obj->resv = attach->dmabuf->resv;
91
92         panfrost_mmu_map(pobj);
93
94         return obj;
95 }