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 25 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj, 26 bool alloc_kmap) 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 34 if (!alloc_kmap) 35 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs); 36 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; 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 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 79 int rockchip_gem_mmap_buf(struct drm_gem_object *obj, 80 struct vm_area_struct *vma) 81 { 82 struct drm_device *drm = obj->dev; 83 int ret; 84 85 mutex_lock(&drm->struct_mutex); 86 ret = drm_gem_mmap_obj(obj, obj->size, vma); 87 mutex_unlock(&drm->struct_mutex); 88 if (ret) 89 return ret; 90 91 return rockchip_drm_gem_object_mmap(obj, vma); 92 } 93 94 /* drm driver mmap file operations */ 95 int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma) 96 { 97 struct drm_gem_object *obj; 98 int ret; 99 100 ret = drm_gem_mmap(filp, vma); 101 if (ret) 102 return ret; 103 104 obj = vma->vm_private_data; 105 106 return rockchip_drm_gem_object_mmap(obj, vma); 107 } 108 109 struct rockchip_gem_object * 110 rockchip_gem_create_object(struct drm_device *drm, unsigned int size, 111 bool alloc_kmap) 112 { 113 struct rockchip_gem_object *rk_obj; 114 struct drm_gem_object *obj; 115 int ret; 116 117 size = round_up(size, PAGE_SIZE); 118 119 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL); 120 if (!rk_obj) 121 return ERR_PTR(-ENOMEM); 122 123 obj = &rk_obj->base; 124 125 drm_gem_private_object_init(drm, obj, size); 126 127 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap); 128 if (ret) 129 goto err_free_rk_obj; 130 131 return rk_obj; 132 133 err_free_rk_obj: 134 kfree(rk_obj); 135 return ERR_PTR(ret); 136 } 137 138 /* 139 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback 140 * function 141 */ 142 void rockchip_gem_free_object(struct drm_gem_object *obj) 143 { 144 struct rockchip_gem_object *rk_obj; 145 146 drm_gem_free_mmap_offset(obj); 147 148 rk_obj = to_rockchip_obj(obj); 149 150 rockchip_gem_free_buf(rk_obj); 151 152 kfree(rk_obj); 153 } 154 155 /* 156 * rockchip_gem_create_with_handle - allocate an object with the given 157 * size and create a gem handle on it 158 * 159 * returns a struct rockchip_gem_object* on success or ERR_PTR values 160 * on failure. 161 */ 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) 166 { 167 struct rockchip_gem_object *rk_obj; 168 struct drm_gem_object *obj; 169 int ret; 170 171 rk_obj = rockchip_gem_create_object(drm, size, false); 172 if (IS_ERR(rk_obj)) 173 return ERR_CAST(rk_obj); 174 175 obj = &rk_obj->base; 176 177 /* 178 * allocate a id of idr table where the obj is registered 179 * and handle has the id what user can see. 180 */ 181 ret = drm_gem_handle_create(file_priv, obj, handle); 182 if (ret) 183 goto err_handle_create; 184 185 /* drop reference from allocate - handle holds it now. */ 186 drm_gem_object_unreference_unlocked(obj); 187 188 return rk_obj; 189 190 err_handle_create: 191 rockchip_gem_free_object(obj); 192 193 return ERR_PTR(ret); 194 } 195 196 int rockchip_gem_dumb_map_offset(struct drm_file *file_priv, 197 struct drm_device *dev, uint32_t handle, 198 uint64_t *offset) 199 { 200 struct drm_gem_object *obj; 201 int ret; 202 203 obj = drm_gem_object_lookup(dev, file_priv, handle); 204 if (!obj) { 205 DRM_ERROR("failed to lookup gem object.\n"); 206 return -EINVAL; 207 } 208 209 ret = drm_gem_create_mmap_offset(obj); 210 if (ret) 211 goto out; 212 213 *offset = drm_vma_node_offset_addr(&obj->vma_node); 214 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset); 215 216 out: 217 drm_gem_object_unreference_unlocked(obj); 218 219 return 0; 220 } 221 222 /* 223 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback 224 * function 225 * 226 * This aligns the pitch and size arguments to the minimum required. wrap 227 * this into your own function if you need bigger alignment. 228 */ 229 int rockchip_gem_dumb_create(struct drm_file *file_priv, 230 struct drm_device *dev, 231 struct drm_mode_create_dumb *args) 232 { 233 struct rockchip_gem_object *rk_obj; 234 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); 235 236 /* 237 * align to 64 bytes since Mali requires it. 238 */ 239 min_pitch = ALIGN(min_pitch, 64); 240 241 if (args->pitch < min_pitch) 242 args->pitch = min_pitch; 243 244 if (args->size < args->pitch * args->height) 245 args->size = args->pitch * args->height; 246 247 rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size, 248 &args->handle); 249 250 return PTR_ERR_OR_ZERO(rk_obj); 251 } 252 253 /* 254 * Allocate a sg_table for this GEM object. 255 * Note: Both the table's contents, and the sg_table itself must be freed by 256 * the caller. 257 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error. 258 */ 259 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj) 260 { 261 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); 262 struct drm_device *drm = obj->dev; 263 struct sg_table *sgt; 264 int ret; 265 266 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); 267 if (!sgt) 268 return ERR_PTR(-ENOMEM); 269 270 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr, 271 rk_obj->dma_addr, obj->size, 272 &rk_obj->dma_attrs); 273 if (ret) { 274 DRM_ERROR("failed to allocate sgt, %d\n", ret); 275 kfree(sgt); 276 return ERR_PTR(ret); 277 } 278 279 return sgt; 280 } 281 282 void *rockchip_gem_prime_vmap(struct drm_gem_object *obj) 283 { 284 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj); 285 286 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs)) 287 return NULL; 288 289 return rk_obj->kvaddr; 290 } 291 292 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) 293 { 294 /* Nothing to do */ 295 } 296