Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd |
| 3 | * Author:Mark Yao <mark.yao@rock-chips.com> |
| 4 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | */ |
| 14 | |
| 15 | #include <drm/drm.h> |
| 16 | #include <drm/drmP.h> |
| 17 | #include <drm/drm_gem.h> |
| 18 | #include <drm/drm_vma_manager.h> |
| 19 | |
| 20 | #include <linux/dma-attrs.h> |
| 21 | |
| 22 | #include "rockchip_drm_drv.h" |
| 23 | #include "rockchip_drm_gem.h" |
| 24 | |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 25 | static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj, |
| 26 | bool alloc_kmap) |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 27 | { |
| 28 | struct drm_gem_object *obj = &rk_obj->base; |
| 29 | struct drm_device *drm = obj->dev; |
| 30 | |
| 31 | init_dma_attrs(&rk_obj->dma_attrs); |
| 32 | dma_set_attr(DMA_ATTR_WRITE_COMBINE, &rk_obj->dma_attrs); |
| 33 | |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 34 | if (!alloc_kmap) |
| 35 | dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs); |
| 36 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 37 | rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size, |
| 38 | &rk_obj->dma_addr, GFP_KERNEL, |
| 39 | &rk_obj->dma_attrs); |
Daniel Kurtz | 4b9a90c | 2015-01-07 17:27:06 +0800 | [diff] [blame] | 40 | if (!rk_obj->kvaddr) { |
| 41 | DRM_ERROR("failed to allocate %#x byte dma buffer", obj->size); |
| 42 | return -ENOMEM; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj) |
| 49 | { |
| 50 | struct drm_gem_object *obj = &rk_obj->base; |
| 51 | struct drm_device *drm = obj->dev; |
| 52 | |
| 53 | dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr, |
| 54 | &rk_obj->dma_attrs); |
| 55 | } |
| 56 | |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 57 | static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj, |
| 58 | struct vm_area_struct *vma) |
| 59 | |
| 60 | { |
| 61 | int ret; |
| 62 | struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); |
| 63 | struct drm_device *drm = obj->dev; |
| 64 | |
| 65 | /* |
| 66 | * dma_alloc_attrs() allocated a struct page table for rk_obj, so clear |
| 67 | * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap(). |
| 68 | */ |
| 69 | vma->vm_flags &= ~VM_PFNMAP; |
| 70 | |
| 71 | ret = dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr, |
| 72 | obj->size, &rk_obj->dma_attrs); |
| 73 | if (ret) |
| 74 | drm_gem_vm_close(vma); |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 79 | int rockchip_gem_mmap_buf(struct drm_gem_object *obj, |
| 80 | struct vm_area_struct *vma) |
| 81 | { |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 82 | int ret; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 83 | |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 84 | ret = drm_gem_mmap_obj(obj, obj->size, vma); |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 85 | if (ret) |
| 86 | return ret; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 87 | |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 88 | return rockchip_drm_gem_object_mmap(obj, vma); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* drm driver mmap file operations */ |
| 92 | int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 93 | { |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 94 | struct drm_gem_object *obj; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 95 | int ret; |
| 96 | |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 97 | ret = drm_gem_mmap(filp, vma); |
| 98 | if (ret) |
| 99 | return ret; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 100 | |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 101 | obj = vma->vm_private_data; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 102 | |
Daniel Kurtz | 41315b7 | 2015-07-07 17:03:36 +0800 | [diff] [blame] | 103 | return rockchip_drm_gem_object_mmap(obj, vma); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | struct rockchip_gem_object * |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 107 | rockchip_gem_create_object(struct drm_device *drm, unsigned int size, |
| 108 | bool alloc_kmap) |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 109 | { |
| 110 | struct rockchip_gem_object *rk_obj; |
| 111 | struct drm_gem_object *obj; |
| 112 | int ret; |
| 113 | |
| 114 | size = round_up(size, PAGE_SIZE); |
| 115 | |
| 116 | rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL); |
| 117 | if (!rk_obj) |
| 118 | return ERR_PTR(-ENOMEM); |
| 119 | |
| 120 | obj = &rk_obj->base; |
| 121 | |
| 122 | drm_gem_private_object_init(drm, obj, size); |
| 123 | |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 124 | ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 125 | if (ret) |
| 126 | goto err_free_rk_obj; |
| 127 | |
| 128 | return rk_obj; |
| 129 | |
| 130 | err_free_rk_obj: |
| 131 | kfree(rk_obj); |
| 132 | return ERR_PTR(ret); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback |
| 137 | * function |
| 138 | */ |
| 139 | void rockchip_gem_free_object(struct drm_gem_object *obj) |
| 140 | { |
| 141 | struct rockchip_gem_object *rk_obj; |
| 142 | |
| 143 | drm_gem_free_mmap_offset(obj); |
| 144 | |
| 145 | rk_obj = to_rockchip_obj(obj); |
| 146 | |
| 147 | rockchip_gem_free_buf(rk_obj); |
| 148 | |
| 149 | kfree(rk_obj); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * rockchip_gem_create_with_handle - allocate an object with the given |
| 154 | * size and create a gem handle on it |
| 155 | * |
| 156 | * returns a struct rockchip_gem_object* on success or ERR_PTR values |
| 157 | * on failure. |
| 158 | */ |
| 159 | static struct rockchip_gem_object * |
| 160 | rockchip_gem_create_with_handle(struct drm_file *file_priv, |
| 161 | struct drm_device *drm, unsigned int size, |
| 162 | unsigned int *handle) |
| 163 | { |
| 164 | struct rockchip_gem_object *rk_obj; |
| 165 | struct drm_gem_object *obj; |
| 166 | int ret; |
| 167 | |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 168 | rk_obj = rockchip_gem_create_object(drm, size, false); |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 169 | if (IS_ERR(rk_obj)) |
| 170 | return ERR_CAST(rk_obj); |
| 171 | |
| 172 | obj = &rk_obj->base; |
| 173 | |
| 174 | /* |
| 175 | * allocate a id of idr table where the obj is registered |
| 176 | * and handle has the id what user can see. |
| 177 | */ |
| 178 | ret = drm_gem_handle_create(file_priv, obj, handle); |
| 179 | if (ret) |
| 180 | goto err_handle_create; |
| 181 | |
| 182 | /* drop reference from allocate - handle holds it now. */ |
| 183 | drm_gem_object_unreference_unlocked(obj); |
| 184 | |
| 185 | return rk_obj; |
| 186 | |
| 187 | err_handle_create: |
| 188 | rockchip_gem_free_object(obj); |
| 189 | |
| 190 | return ERR_PTR(ret); |
| 191 | } |
| 192 | |
| 193 | int rockchip_gem_dumb_map_offset(struct drm_file *file_priv, |
| 194 | struct drm_device *dev, uint32_t handle, |
| 195 | uint64_t *offset) |
| 196 | { |
| 197 | struct drm_gem_object *obj; |
| 198 | int ret; |
| 199 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 200 | obj = drm_gem_object_lookup(dev, file_priv, handle); |
| 201 | if (!obj) { |
| 202 | DRM_ERROR("failed to lookup gem object.\n"); |
Daniel Vetter | 648a4ce | 2015-07-09 23:32:42 +0200 | [diff] [blame] | 203 | return -EINVAL; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | ret = drm_gem_create_mmap_offset(obj); |
| 207 | if (ret) |
| 208 | goto out; |
| 209 | |
| 210 | *offset = drm_vma_node_offset_addr(&obj->vma_node); |
| 211 | DRM_DEBUG_KMS("offset = 0x%llx\n", *offset); |
| 212 | |
| 213 | out: |
Daniel Vetter | 648a4ce | 2015-07-09 23:32:42 +0200 | [diff] [blame] | 214 | drm_gem_object_unreference_unlocked(obj); |
| 215 | |
| 216 | return 0; |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback |
| 221 | * function |
| 222 | * |
| 223 | * This aligns the pitch and size arguments to the minimum required. wrap |
| 224 | * this into your own function if you need bigger alignment. |
| 225 | */ |
| 226 | int rockchip_gem_dumb_create(struct drm_file *file_priv, |
| 227 | struct drm_device *dev, |
| 228 | struct drm_mode_create_dumb *args) |
| 229 | { |
| 230 | struct rockchip_gem_object *rk_obj; |
| 231 | int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 232 | |
| 233 | /* |
| 234 | * align to 64 bytes since Mali requires it. |
| 235 | */ |
| 236 | min_pitch = ALIGN(min_pitch, 64); |
| 237 | |
| 238 | if (args->pitch < min_pitch) |
| 239 | args->pitch = min_pitch; |
| 240 | |
| 241 | if (args->size < args->pitch * args->height) |
| 242 | args->size = args->pitch * args->height; |
| 243 | |
| 244 | rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size, |
| 245 | &args->handle); |
| 246 | |
| 247 | return PTR_ERR_OR_ZERO(rk_obj); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Allocate a sg_table for this GEM object. |
| 252 | * Note: Both the table's contents, and the sg_table itself must be freed by |
| 253 | * the caller. |
| 254 | * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error. |
| 255 | */ |
| 256 | struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj) |
| 257 | { |
| 258 | struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); |
| 259 | struct drm_device *drm = obj->dev; |
| 260 | struct sg_table *sgt; |
| 261 | int ret; |
| 262 | |
| 263 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 264 | if (!sgt) |
| 265 | return ERR_PTR(-ENOMEM); |
| 266 | |
| 267 | ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr, |
| 268 | rk_obj->dma_addr, obj->size, |
| 269 | &rk_obj->dma_attrs); |
| 270 | if (ret) { |
| 271 | DRM_ERROR("failed to allocate sgt, %d\n", ret); |
| 272 | kfree(sgt); |
| 273 | return ERR_PTR(ret); |
| 274 | } |
| 275 | |
| 276 | return sgt; |
| 277 | } |
| 278 | |
| 279 | void *rockchip_gem_prime_vmap(struct drm_gem_object *obj) |
| 280 | { |
| 281 | struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); |
| 282 | |
Daniel Kurtz | f76c83b | 2015-01-12 14:58:23 +0800 | [diff] [blame] | 283 | if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs)) |
| 284 | return NULL; |
| 285 | |
Mark Yao | 2048e32 | 2014-08-22 18:36:26 +0800 | [diff] [blame] | 286 | return rk_obj->kvaddr; |
| 287 | } |
| 288 | |
| 289 | void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 290 | { |
| 291 | /* Nothing to do */ |
| 292 | } |