Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / rockchip_drm_gem.c
blobcab4d60060a0051e9268376a5bcfd52550f0850a
1 /*
2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <drm/drm.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_vma_manager.h>
20 #include "rockchip_drm_drv.h"
21 #include "rockchip_drm_gem.h"
23 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
24 bool alloc_kmap)
26 struct drm_gem_object *obj = &rk_obj->base;
27 struct drm_device *drm = obj->dev;
29 rk_obj->dma_attrs = DMA_ATTR_WRITE_COMBINE;
31 if (!alloc_kmap)
32 rk_obj->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
34 rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
35 &rk_obj->dma_addr, GFP_KERNEL,
36 rk_obj->dma_attrs);
37 if (!rk_obj->kvaddr) {
38 DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
39 return -ENOMEM;
42 return 0;
45 static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
47 struct drm_gem_object *obj = &rk_obj->base;
48 struct drm_device *drm = obj->dev;
50 dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
51 rk_obj->dma_attrs);
54 static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
55 struct vm_area_struct *vma)
58 int ret;
59 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
60 struct drm_device *drm = obj->dev;
63 * dma_alloc_attrs() allocated a struct page table for rk_obj, so clear
64 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
66 vma->vm_flags &= ~VM_PFNMAP;
68 ret = dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
69 obj->size, rk_obj->dma_attrs);
70 if (ret)
71 drm_gem_vm_close(vma);
73 return ret;
76 int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
77 struct vm_area_struct *vma)
79 int ret;
81 ret = drm_gem_mmap_obj(obj, obj->size, vma);
82 if (ret)
83 return ret;
85 return rockchip_drm_gem_object_mmap(obj, vma);
88 /* drm driver mmap file operations */
89 int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
91 struct drm_gem_object *obj;
92 int ret;
94 ret = drm_gem_mmap(filp, vma);
95 if (ret)
96 return ret;
99 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
100 * whole buffer from the start.
102 vma->vm_pgoff = 0;
104 obj = vma->vm_private_data;
106 return rockchip_drm_gem_object_mmap(obj, vma);
109 struct rockchip_gem_object *
110 rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
111 bool alloc_kmap)
113 struct rockchip_gem_object *rk_obj;
114 struct drm_gem_object *obj;
115 int ret;
117 size = round_up(size, PAGE_SIZE);
119 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
120 if (!rk_obj)
121 return ERR_PTR(-ENOMEM);
123 obj = &rk_obj->base;
125 drm_gem_private_object_init(drm, obj, size);
127 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
128 if (ret)
129 goto err_free_rk_obj;
131 return rk_obj;
133 err_free_rk_obj:
134 kfree(rk_obj);
135 return ERR_PTR(ret);
139 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
140 * function
142 void rockchip_gem_free_object(struct drm_gem_object *obj)
144 struct rockchip_gem_object *rk_obj;
146 drm_gem_free_mmap_offset(obj);
148 rk_obj = to_rockchip_obj(obj);
150 rockchip_gem_free_buf(rk_obj);
152 kfree(rk_obj);
156 * rockchip_gem_create_with_handle - allocate an object with the given
157 * size and create a gem handle on it
159 * returns a struct rockchip_gem_object* on success or ERR_PTR values
160 * on failure.
162 static struct rockchip_gem_object *
163 rockchip_gem_create_with_handle(struct drm_file *file_priv,
164 struct drm_device *drm, unsigned int size,
165 unsigned int *handle)
167 struct rockchip_gem_object *rk_obj;
168 struct drm_gem_object *obj;
169 int ret;
171 rk_obj = rockchip_gem_create_object(drm, size, false);
172 if (IS_ERR(rk_obj))
173 return ERR_CAST(rk_obj);
175 obj = &rk_obj->base;
178 * allocate a id of idr table where the obj is registered
179 * and handle has the id what user can see.
181 ret = drm_gem_handle_create(file_priv, obj, handle);
182 if (ret)
183 goto err_handle_create;
185 /* drop reference from allocate - handle holds it now. */
186 drm_gem_object_unreference_unlocked(obj);
188 return rk_obj;
190 err_handle_create:
191 rockchip_gem_free_object(obj);
193 return ERR_PTR(ret);
196 int rockchip_gem_dumb_map_offset(struct drm_file *file_priv,
197 struct drm_device *dev, uint32_t handle,
198 uint64_t *offset)
200 struct drm_gem_object *obj;
201 int ret;
203 obj = drm_gem_object_lookup(file_priv, handle);
204 if (!obj) {
205 DRM_ERROR("failed to lookup gem object.\n");
206 return -EINVAL;
209 ret = drm_gem_create_mmap_offset(obj);
210 if (ret)
211 goto out;
213 *offset = drm_vma_node_offset_addr(&obj->vma_node);
214 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset);
216 out:
217 drm_gem_object_unreference_unlocked(obj);
219 return 0;
223 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
224 * function
226 * This aligns the pitch and size arguments to the minimum required. wrap
227 * this into your own function if you need bigger alignment.
229 int rockchip_gem_dumb_create(struct drm_file *file_priv,
230 struct drm_device *dev,
231 struct drm_mode_create_dumb *args)
233 struct rockchip_gem_object *rk_obj;
234 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
237 * align to 64 bytes since Mali requires it.
239 args->pitch = ALIGN(min_pitch, 64);
240 args->size = args->pitch * args->height;
242 rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
243 &args->handle);
245 return PTR_ERR_OR_ZERO(rk_obj);
249 * Allocate a sg_table for this GEM object.
250 * Note: Both the table's contents, and the sg_table itself must be freed by
251 * the caller.
252 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
254 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
256 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
257 struct drm_device *drm = obj->dev;
258 struct sg_table *sgt;
259 int ret;
261 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
262 if (!sgt)
263 return ERR_PTR(-ENOMEM);
265 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
266 rk_obj->dma_addr, obj->size,
267 rk_obj->dma_attrs);
268 if (ret) {
269 DRM_ERROR("failed to allocate sgt, %d\n", ret);
270 kfree(sgt);
271 return ERR_PTR(ret);
274 return sgt;
277 void *rockchip_gem_prime_vmap(struct drm_gem_object *obj)
279 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
281 if (rk_obj->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING)
282 return NULL;
284 return rk_obj->kvaddr;
287 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
289 /* Nothing to do */