1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 MediaTek Inc. 4 */ 5 6 #include <linux/dma-buf.h> 7 #include <linux/vmalloc.h> 8 9 #include <drm/drm.h> 10 #include <drm/drm_device.h> 11 #include <drm/drm_gem.h> 12 #include <drm/drm_gem_dma_helper.h> 13 #include <drm/drm_prime.h> 14 15 #include "mtk_drm_drv.h" 16 #include "mtk_gem.h" 17 18 static int mtk_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma); 19 20 static const struct vm_operations_struct vm_ops = { 21 .open = drm_gem_vm_open, 22 .close = drm_gem_vm_close, 23 }; 24 25 static const struct drm_gem_object_funcs mtk_gem_object_funcs = { 26 .free = mtk_gem_free_object, 27 .get_sg_table = mtk_gem_prime_get_sg_table, 28 .vmap = mtk_gem_prime_vmap, 29 .vunmap = mtk_gem_prime_vunmap, 30 .mmap = mtk_gem_object_mmap, 31 .vm_ops = &vm_ops, 32 }; 33 34 static struct mtk_gem_obj *mtk_gem_init(struct drm_device *dev, 35 unsigned long size) 36 { 37 struct mtk_gem_obj *mtk_gem_obj; 38 int ret; 39 40 size = round_up(size, PAGE_SIZE); 41 42 if (size == 0) 43 return ERR_PTR(-EINVAL); 44 45 mtk_gem_obj = kzalloc(sizeof(*mtk_gem_obj), GFP_KERNEL); 46 if (!mtk_gem_obj) 47 return ERR_PTR(-ENOMEM); 48 49 mtk_gem_obj->base.funcs = &mtk_gem_object_funcs; 50 51 ret = drm_gem_object_init(dev, &mtk_gem_obj->base, size); 52 if (ret < 0) { 53 DRM_ERROR("failed to initialize gem object\n"); 54 kfree(mtk_gem_obj); 55 return ERR_PTR(ret); 56 } 57 58 return mtk_gem_obj; 59 } 60 61 struct mtk_gem_obj *mtk_gem_create(struct drm_device *dev, 62 size_t size, bool alloc_kmap) 63 { 64 struct mtk_drm_private *priv = dev->dev_private; 65 struct mtk_gem_obj *mtk_gem; 66 struct drm_gem_object *obj; 67 int ret; 68 69 mtk_gem = mtk_gem_init(dev, size); 70 if (IS_ERR(mtk_gem)) 71 return ERR_CAST(mtk_gem); 72 73 obj = &mtk_gem->base; 74 75 mtk_gem->dma_attrs = DMA_ATTR_WRITE_COMBINE; 76 77 if (!alloc_kmap) 78 mtk_gem->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING; 79 80 mtk_gem->cookie = dma_alloc_attrs(priv->dma_dev, obj->size, 81 &mtk_gem->dma_addr, GFP_KERNEL, 82 mtk_gem->dma_attrs); 83 if (!mtk_gem->cookie) { 84 DRM_ERROR("failed to allocate %zx byte dma buffer", obj->size); 85 ret = -ENOMEM; 86 goto err_gem_free; 87 } 88 89 if (alloc_kmap) 90 mtk_gem->kvaddr = mtk_gem->cookie; 91 92 DRM_DEBUG_DRIVER("cookie = %p dma_addr = %pad size = %zu\n", 93 mtk_gem->cookie, &mtk_gem->dma_addr, 94 size); 95 96 return mtk_gem; 97 98 err_gem_free: 99 drm_gem_object_release(obj); 100 kfree(mtk_gem); 101 return ERR_PTR(ret); 102 } 103 104 void mtk_gem_free_object(struct drm_gem_object *obj) 105 { 106 struct mtk_gem_obj *mtk_gem = to_mtk_gem_obj(obj); 107 struct mtk_drm_private *priv = obj->dev->dev_private; 108 109 if (mtk_gem->sg) 110 drm_prime_gem_destroy(obj, mtk_gem->sg); 111 else 112 dma_free_attrs(priv->dma_dev, obj->size, mtk_gem->cookie, 113 mtk_gem->dma_addr, mtk_gem->dma_attrs); 114 115 /* release file pointer to gem object. */ 116 drm_gem_object_release(obj); 117 118 kfree(mtk_gem); 119 } 120 121 int mtk_gem_dumb_create(struct drm_file *file_priv, struct drm_device *dev, 122 struct drm_mode_create_dumb *args) 123 { 124 struct mtk_gem_obj *mtk_gem; 125 int ret; 126 127 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8); 128 129 /* 130 * Multiply 2 variables of different types, 131 * for example: args->size = args->spacing * args->height; 132 * may cause coverity issue with unintentional overflow. 133 */ 134 args->size = args->pitch; 135 args->size *= args->height; 136 137 mtk_gem = mtk_gem_create(dev, args->size, false); 138 if (IS_ERR(mtk_gem)) 139 return PTR_ERR(mtk_gem); 140 141 /* 142 * allocate a id of idr table where the obj is registered 143 * and handle has the id what user can see. 144 */ 145 ret = drm_gem_handle_create(file_priv, &mtk_gem->base, &args->handle); 146 if (ret) 147 goto err_handle_create; 148 149 /* drop reference from allocate - handle holds it now. */ 150 drm_gem_object_put(&mtk_gem->base); 151 152 return 0; 153 154 err_handle_create: 155 mtk_gem_free_object(&mtk_gem->base); 156 return ret; 157 } 158 159 static int mtk_gem_object_mmap(struct drm_gem_object *obj, 160 struct vm_area_struct *vma) 161 162 { 163 int ret; 164 struct mtk_gem_obj *mtk_gem = to_mtk_gem_obj(obj); 165 struct mtk_drm_private *priv = obj->dev->dev_private; 166 167 /* 168 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the 169 * whole buffer from the start. 170 */ 171 vma->vm_pgoff = 0; 172 173 /* 174 * dma_alloc_attrs() allocated a struct page table for mtk_gem, so clear 175 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap(). 176 */ 177 vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP); 178 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 179 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 180 181 ret = dma_mmap_attrs(priv->dma_dev, vma, mtk_gem->cookie, 182 mtk_gem->dma_addr, obj->size, mtk_gem->dma_attrs); 183 184 return ret; 185 } 186 187 /* 188 * Allocate a sg_table for this GEM object. 189 * Note: Both the table's contents, and the sg_table itself must be freed by 190 * the caller. 191 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error. 192 */ 193 struct sg_table *mtk_gem_prime_get_sg_table(struct drm_gem_object *obj) 194 { 195 struct mtk_gem_obj *mtk_gem = to_mtk_gem_obj(obj); 196 struct mtk_drm_private *priv = obj->dev->dev_private; 197 struct sg_table *sgt; 198 int ret; 199 200 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); 201 if (!sgt) 202 return ERR_PTR(-ENOMEM); 203 204 ret = dma_get_sgtable_attrs(priv->dma_dev, sgt, mtk_gem->cookie, 205 mtk_gem->dma_addr, obj->size, 206 mtk_gem->dma_attrs); 207 if (ret) { 208 DRM_ERROR("failed to allocate sgt, %d\n", ret); 209 kfree(sgt); 210 return ERR_PTR(ret); 211 } 212 213 return sgt; 214 } 215 216 struct drm_gem_object *mtk_gem_prime_import_sg_table(struct drm_device *dev, 217 struct dma_buf_attachment *attach, struct sg_table *sg) 218 { 219 struct mtk_gem_obj *mtk_gem; 220 221 /* check if the entries in the sg_table are contiguous */ 222 if (drm_prime_get_contiguous_size(sg) < attach->dmabuf->size) { 223 DRM_ERROR("sg_table is not contiguous"); 224 return ERR_PTR(-EINVAL); 225 } 226 227 mtk_gem = mtk_gem_init(dev, attach->dmabuf->size); 228 if (IS_ERR(mtk_gem)) 229 return ERR_CAST(mtk_gem); 230 231 mtk_gem->dma_addr = sg_dma_address(sg->sgl); 232 mtk_gem->sg = sg; 233 234 return &mtk_gem->base; 235 } 236 237 int mtk_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map) 238 { 239 struct mtk_gem_obj *mtk_gem = to_mtk_gem_obj(obj); 240 struct sg_table *sgt = NULL; 241 unsigned int npages; 242 243 if (mtk_gem->kvaddr) 244 goto out; 245 246 sgt = mtk_gem_prime_get_sg_table(obj); 247 if (IS_ERR(sgt)) 248 return PTR_ERR(sgt); 249 250 npages = obj->size >> PAGE_SHIFT; 251 mtk_gem->pages = kcalloc(npages, sizeof(*mtk_gem->pages), GFP_KERNEL); 252 if (!mtk_gem->pages) { 253 sg_free_table(sgt); 254 kfree(sgt); 255 return -ENOMEM; 256 } 257 258 drm_prime_sg_to_page_array(sgt, mtk_gem->pages, npages); 259 260 mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP, 261 pgprot_writecombine(PAGE_KERNEL)); 262 if (!mtk_gem->kvaddr) { 263 sg_free_table(sgt); 264 kfree(sgt); 265 kfree(mtk_gem->pages); 266 return -ENOMEM; 267 } 268 sg_free_table(sgt); 269 kfree(sgt); 270 271 out: 272 iosys_map_set_vaddr(map, mtk_gem->kvaddr); 273 274 return 0; 275 } 276 277 void mtk_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map) 278 { 279 struct mtk_gem_obj *mtk_gem = to_mtk_gem_obj(obj); 280 void *vaddr = map->vaddr; 281 282 if (!mtk_gem->pages) 283 return; 284 285 vunmap(vaddr); 286 mtk_gem->kvaddr = NULL; 287 kfree(mtk_gem->pages); 288 } 289