bna: remove oper_state_cbfn from struct bna_rxf
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / rockchip_drm_gem.c
blobeb2282cc4a56507819a5ab638af504499157157a
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 <linux/dma-attrs.h>
22 #include "rockchip_drm_drv.h"
23 #include "rockchip_drm_gem.h"
25 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
26 bool alloc_kmap)
28 struct drm_gem_object *obj = &rk_obj->base;
29 struct drm_device *drm = obj->dev;
31 init_dma_attrs(&rk_obj->dma_attrs);
32 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &rk_obj->dma_attrs);
34 if (!alloc_kmap)
35 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs);
37 rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
38 &rk_obj->dma_addr, GFP_KERNEL,
39 &rk_obj->dma_attrs);
40 if (!rk_obj->kvaddr) {
41 DRM_ERROR("failed to allocate %#x byte dma buffer", obj->size);
42 return -ENOMEM;
45 return 0;
48 static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
50 struct drm_gem_object *obj = &rk_obj->base;
51 struct drm_device *drm = obj->dev;
53 dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
54 &rk_obj->dma_attrs);
57 int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
58 struct vm_area_struct *vma)
60 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
61 struct drm_device *drm = obj->dev;
62 unsigned long vm_size;
64 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
65 vm_size = vma->vm_end - vma->vm_start;
67 if (vm_size > obj->size)
68 return -EINVAL;
70 return dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
71 obj->size, &rk_obj->dma_attrs);
74 /* drm driver mmap file operations */
75 int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
77 struct drm_file *priv = filp->private_data;
78 struct drm_device *dev = priv->minor->dev;
79 struct drm_gem_object *obj;
80 struct drm_vma_offset_node *node;
81 int ret;
83 if (drm_device_is_unplugged(dev))
84 return -ENODEV;
86 mutex_lock(&dev->struct_mutex);
88 node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
89 vma->vm_pgoff,
90 vma_pages(vma));
91 if (!node) {
92 mutex_unlock(&dev->struct_mutex);
93 DRM_ERROR("failed to find vma node.\n");
94 return -EINVAL;
95 } else if (!drm_vma_node_is_allowed(node, filp)) {
96 mutex_unlock(&dev->struct_mutex);
97 return -EACCES;
100 obj = container_of(node, struct drm_gem_object, vma_node);
101 ret = rockchip_gem_mmap_buf(obj, vma);
103 mutex_unlock(&dev->struct_mutex);
105 return ret;
108 struct rockchip_gem_object *
109 rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
110 bool alloc_kmap)
112 struct rockchip_gem_object *rk_obj;
113 struct drm_gem_object *obj;
114 int ret;
116 size = round_up(size, PAGE_SIZE);
118 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
119 if (!rk_obj)
120 return ERR_PTR(-ENOMEM);
122 obj = &rk_obj->base;
124 drm_gem_private_object_init(drm, obj, size);
126 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
127 if (ret)
128 goto err_free_rk_obj;
130 return rk_obj;
132 err_free_rk_obj:
133 kfree(rk_obj);
134 return ERR_PTR(ret);
138 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
139 * function
141 void rockchip_gem_free_object(struct drm_gem_object *obj)
143 struct rockchip_gem_object *rk_obj;
145 drm_gem_free_mmap_offset(obj);
147 rk_obj = to_rockchip_obj(obj);
149 rockchip_gem_free_buf(rk_obj);
151 kfree(rk_obj);
155 * rockchip_gem_create_with_handle - allocate an object with the given
156 * size and create a gem handle on it
158 * returns a struct rockchip_gem_object* on success or ERR_PTR values
159 * on failure.
161 static struct rockchip_gem_object *
162 rockchip_gem_create_with_handle(struct drm_file *file_priv,
163 struct drm_device *drm, unsigned int size,
164 unsigned int *handle)
166 struct rockchip_gem_object *rk_obj;
167 struct drm_gem_object *obj;
168 int ret;
170 rk_obj = rockchip_gem_create_object(drm, size, false);
171 if (IS_ERR(rk_obj))
172 return ERR_CAST(rk_obj);
174 obj = &rk_obj->base;
177 * allocate a id of idr table where the obj is registered
178 * and handle has the id what user can see.
180 ret = drm_gem_handle_create(file_priv, obj, handle);
181 if (ret)
182 goto err_handle_create;
184 /* drop reference from allocate - handle holds it now. */
185 drm_gem_object_unreference_unlocked(obj);
187 return rk_obj;
189 err_handle_create:
190 rockchip_gem_free_object(obj);
192 return ERR_PTR(ret);
195 int rockchip_gem_dumb_map_offset(struct drm_file *file_priv,
196 struct drm_device *dev, uint32_t handle,
197 uint64_t *offset)
199 struct drm_gem_object *obj;
200 int ret;
202 mutex_lock(&dev->struct_mutex);
204 obj = drm_gem_object_lookup(dev, file_priv, handle);
205 if (!obj) {
206 DRM_ERROR("failed to lookup gem object.\n");
207 ret = -EINVAL;
208 goto unlock;
211 ret = drm_gem_create_mmap_offset(obj);
212 if (ret)
213 goto out;
215 *offset = drm_vma_node_offset_addr(&obj->vma_node);
216 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset);
218 out:
219 drm_gem_object_unreference(obj);
220 unlock:
221 mutex_unlock(&dev->struct_mutex);
222 return ret;
226 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
227 * function
229 * This aligns the pitch and size arguments to the minimum required. wrap
230 * this into your own function if you need bigger alignment.
232 int rockchip_gem_dumb_create(struct drm_file *file_priv,
233 struct drm_device *dev,
234 struct drm_mode_create_dumb *args)
236 struct rockchip_gem_object *rk_obj;
237 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
240 * align to 64 bytes since Mali requires it.
242 min_pitch = ALIGN(min_pitch, 64);
244 if (args->pitch < min_pitch)
245 args->pitch = min_pitch;
247 if (args->size < args->pitch * args->height)
248 args->size = args->pitch * args->height;
250 rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
251 &args->handle);
253 return PTR_ERR_OR_ZERO(rk_obj);
257 * Allocate a sg_table for this GEM object.
258 * Note: Both the table's contents, and the sg_table itself must be freed by
259 * the caller.
260 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
262 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
264 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
265 struct drm_device *drm = obj->dev;
266 struct sg_table *sgt;
267 int ret;
269 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
270 if (!sgt)
271 return ERR_PTR(-ENOMEM);
273 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
274 rk_obj->dma_addr, obj->size,
275 &rk_obj->dma_attrs);
276 if (ret) {
277 DRM_ERROR("failed to allocate sgt, %d\n", ret);
278 kfree(sgt);
279 return ERR_PTR(ret);
282 return sgt;
285 void *rockchip_gem_prime_vmap(struct drm_gem_object *obj)
287 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
289 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs))
290 return NULL;
292 return rk_obj->kvaddr;
295 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
297 /* Nothing to do */