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.
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
)
27 struct drm_gem_object
*obj
= &rk_obj
->base
;
28 struct drm_device
*drm
= obj
->dev
;
30 init_dma_attrs(&rk_obj
->dma_attrs
);
31 dma_set_attr(DMA_ATTR_WRITE_COMBINE
, &rk_obj
->dma_attrs
);
33 /* TODO(djkurtz): Use DMA_ATTR_NO_KERNEL_MAPPING except for fbdev */
34 rk_obj
->kvaddr
= dma_alloc_attrs(drm
->dev
, obj
->size
,
35 &rk_obj
->dma_addr
, GFP_KERNEL
,
37 if (!rk_obj
->kvaddr
) {
38 DRM_ERROR("failed to allocate %#x byte dma buffer", obj
->size
);
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
,
54 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
55 struct vm_area_struct
*vma
)
57 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
58 struct drm_device
*drm
= obj
->dev
;
59 unsigned long vm_size
;
61 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
62 vm_size
= vma
->vm_end
- vma
->vm_start
;
64 if (vm_size
> obj
->size
)
67 return dma_mmap_attrs(drm
->dev
, vma
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
68 obj
->size
, &rk_obj
->dma_attrs
);
71 /* drm driver mmap file operations */
72 int rockchip_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
74 struct drm_file
*priv
= filp
->private_data
;
75 struct drm_device
*dev
= priv
->minor
->dev
;
76 struct drm_gem_object
*obj
;
77 struct drm_vma_offset_node
*node
;
80 if (drm_device_is_unplugged(dev
))
83 mutex_lock(&dev
->struct_mutex
);
85 node
= drm_vma_offset_exact_lookup(dev
->vma_offset_manager
,
89 mutex_unlock(&dev
->struct_mutex
);
90 DRM_ERROR("failed to find vma node.\n");
92 } else if (!drm_vma_node_is_allowed(node
, filp
)) {
93 mutex_unlock(&dev
->struct_mutex
);
97 obj
= container_of(node
, struct drm_gem_object
, vma_node
);
98 ret
= rockchip_gem_mmap_buf(obj
, vma
);
100 mutex_unlock(&dev
->struct_mutex
);
105 struct rockchip_gem_object
*
106 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
)
108 struct rockchip_gem_object
*rk_obj
;
109 struct drm_gem_object
*obj
;
112 size
= round_up(size
, PAGE_SIZE
);
114 rk_obj
= kzalloc(sizeof(*rk_obj
), GFP_KERNEL
);
116 return ERR_PTR(-ENOMEM
);
120 drm_gem_private_object_init(drm
, obj
, size
);
122 ret
= rockchip_gem_alloc_buf(rk_obj
);
124 goto err_free_rk_obj
;
134 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
137 void rockchip_gem_free_object(struct drm_gem_object
*obj
)
139 struct rockchip_gem_object
*rk_obj
;
141 drm_gem_free_mmap_offset(obj
);
143 rk_obj
= to_rockchip_obj(obj
);
145 rockchip_gem_free_buf(rk_obj
);
151 * rockchip_gem_create_with_handle - allocate an object with the given
152 * size and create a gem handle on it
154 * returns a struct rockchip_gem_object* on success or ERR_PTR values
157 static struct rockchip_gem_object
*
158 rockchip_gem_create_with_handle(struct drm_file
*file_priv
,
159 struct drm_device
*drm
, unsigned int size
,
160 unsigned int *handle
)
162 struct rockchip_gem_object
*rk_obj
;
163 struct drm_gem_object
*obj
;
166 rk_obj
= rockchip_gem_create_object(drm
, size
);
168 return ERR_CAST(rk_obj
);
173 * allocate a id of idr table where the obj is registered
174 * and handle has the id what user can see.
176 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
178 goto err_handle_create
;
180 /* drop reference from allocate - handle holds it now. */
181 drm_gem_object_unreference_unlocked(obj
);
186 rockchip_gem_free_object(obj
);
191 int rockchip_gem_dumb_map_offset(struct drm_file
*file_priv
,
192 struct drm_device
*dev
, uint32_t handle
,
195 struct drm_gem_object
*obj
;
198 mutex_lock(&dev
->struct_mutex
);
200 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
202 DRM_ERROR("failed to lookup gem object.\n");
207 ret
= drm_gem_create_mmap_offset(obj
);
211 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
212 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset
);
215 drm_gem_object_unreference(obj
);
217 mutex_unlock(&dev
->struct_mutex
);
222 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
225 * This aligns the pitch and size arguments to the minimum required. wrap
226 * this into your own function if you need bigger alignment.
228 int rockchip_gem_dumb_create(struct drm_file
*file_priv
,
229 struct drm_device
*dev
,
230 struct drm_mode_create_dumb
*args
)
232 struct rockchip_gem_object
*rk_obj
;
233 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
236 * align to 64 bytes since Mali requires it.
238 min_pitch
= ALIGN(min_pitch
, 64);
240 if (args
->pitch
< min_pitch
)
241 args
->pitch
= min_pitch
;
243 if (args
->size
< args
->pitch
* args
->height
)
244 args
->size
= args
->pitch
* args
->height
;
246 rk_obj
= rockchip_gem_create_with_handle(file_priv
, dev
, args
->size
,
249 return PTR_ERR_OR_ZERO(rk_obj
);
253 * Allocate a sg_table for this GEM object.
254 * Note: Both the table's contents, and the sg_table itself must be freed by
256 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
258 struct sg_table
*rockchip_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
260 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
261 struct drm_device
*drm
= obj
->dev
;
262 struct sg_table
*sgt
;
265 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
267 return ERR_PTR(-ENOMEM
);
269 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
270 rk_obj
->dma_addr
, obj
->size
,
273 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
281 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
283 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
285 return rk_obj
->kvaddr
;
288 void rockchip_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)