Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * NVIDIA Tegra DRM GEM helper functions |
| 3 | * |
| 4 | * Copyright (C) 2012 Sascha Hauer, Pengutronix |
| 5 | * Copyright (C) 2013 NVIDIA CORPORATION, All rights reserved. |
| 6 | * |
| 7 | * Based on the GEM/CMA helpers |
| 8 | * |
| 9 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version 2 |
| 14 | * of the License, or (at your option) any later version. |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/export.h> |
| 25 | #include <linux/dma-mapping.h> |
| 26 | |
| 27 | #include <drm/drmP.h> |
| 28 | #include <drm/drm.h> |
| 29 | |
| 30 | #include "gem.h" |
| 31 | |
| 32 | static inline struct tegra_bo *host1x_to_drm_bo(struct host1x_bo *bo) |
| 33 | { |
| 34 | return container_of(bo, struct tegra_bo, base); |
| 35 | } |
| 36 | |
| 37 | static void tegra_bo_put(struct host1x_bo *bo) |
| 38 | { |
| 39 | struct tegra_bo *obj = host1x_to_drm_bo(bo); |
| 40 | struct drm_device *drm = obj->gem.dev; |
| 41 | |
| 42 | mutex_lock(&drm->struct_mutex); |
| 43 | drm_gem_object_unreference(&obj->gem); |
| 44 | mutex_unlock(&drm->struct_mutex); |
| 45 | } |
| 46 | |
| 47 | static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt) |
| 48 | { |
| 49 | struct tegra_bo *obj = host1x_to_drm_bo(bo); |
| 50 | |
| 51 | return obj->paddr; |
| 52 | } |
| 53 | |
| 54 | static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | static void *tegra_bo_mmap(struct host1x_bo *bo) |
| 59 | { |
| 60 | struct tegra_bo *obj = host1x_to_drm_bo(bo); |
| 61 | |
| 62 | return obj->vaddr; |
| 63 | } |
| 64 | |
| 65 | static void tegra_bo_munmap(struct host1x_bo *bo, void *addr) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page) |
| 70 | { |
| 71 | struct tegra_bo *obj = host1x_to_drm_bo(bo); |
| 72 | |
| 73 | return obj->vaddr + page * PAGE_SIZE; |
| 74 | } |
| 75 | |
| 76 | static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page, |
| 77 | void *addr) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo) |
| 82 | { |
| 83 | struct tegra_bo *obj = host1x_to_drm_bo(bo); |
| 84 | struct drm_device *drm = obj->gem.dev; |
| 85 | |
| 86 | mutex_lock(&drm->struct_mutex); |
| 87 | drm_gem_object_reference(&obj->gem); |
| 88 | mutex_unlock(&drm->struct_mutex); |
| 89 | |
| 90 | return bo; |
| 91 | } |
| 92 | |
| 93 | const struct host1x_bo_ops tegra_bo_ops = { |
| 94 | .get = tegra_bo_get, |
| 95 | .put = tegra_bo_put, |
| 96 | .pin = tegra_bo_pin, |
| 97 | .unpin = tegra_bo_unpin, |
| 98 | .mmap = tegra_bo_mmap, |
| 99 | .munmap = tegra_bo_munmap, |
| 100 | .kmap = tegra_bo_kmap, |
| 101 | .kunmap = tegra_bo_kunmap, |
| 102 | }; |
| 103 | |
| 104 | static void tegra_bo_destroy(struct drm_device *drm, struct tegra_bo *bo) |
| 105 | { |
| 106 | dma_free_writecombine(drm->dev, bo->gem.size, bo->vaddr, bo->paddr); |
| 107 | } |
| 108 | |
Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 109 | struct tegra_bo *tegra_bo_create(struct drm_device *drm, unsigned int size) |
| 110 | { |
| 111 | struct tegra_bo *bo; |
| 112 | int err; |
| 113 | |
| 114 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
| 115 | if (!bo) |
| 116 | return ERR_PTR(-ENOMEM); |
| 117 | |
| 118 | host1x_bo_init(&bo->base, &tegra_bo_ops); |
| 119 | size = round_up(size, PAGE_SIZE); |
| 120 | |
| 121 | bo->vaddr = dma_alloc_writecombine(drm->dev, size, &bo->paddr, |
| 122 | GFP_KERNEL | __GFP_NOWARN); |
| 123 | if (!bo->vaddr) { |
| 124 | dev_err(drm->dev, "failed to allocate buffer with size %u\n", |
| 125 | size); |
| 126 | err = -ENOMEM; |
| 127 | goto err_dma; |
| 128 | } |
| 129 | |
| 130 | err = drm_gem_object_init(drm, &bo->gem, size); |
| 131 | if (err) |
| 132 | goto err_init; |
| 133 | |
| 134 | err = drm_gem_create_mmap_offset(&bo->gem); |
| 135 | if (err) |
| 136 | goto err_mmap; |
| 137 | |
| 138 | return bo; |
| 139 | |
| 140 | err_mmap: |
| 141 | drm_gem_object_release(&bo->gem); |
| 142 | err_init: |
| 143 | tegra_bo_destroy(drm, bo); |
| 144 | err_dma: |
| 145 | kfree(bo); |
| 146 | |
| 147 | return ERR_PTR(err); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file, |
| 152 | struct drm_device *drm, |
| 153 | unsigned int size, |
| 154 | unsigned int *handle) |
| 155 | { |
| 156 | struct tegra_bo *bo; |
| 157 | int ret; |
| 158 | |
| 159 | bo = tegra_bo_create(drm, size); |
| 160 | if (IS_ERR(bo)) |
| 161 | return bo; |
| 162 | |
| 163 | ret = drm_gem_handle_create(file, &bo->gem, handle); |
| 164 | if (ret) |
| 165 | goto err; |
| 166 | |
| 167 | drm_gem_object_unreference_unlocked(&bo->gem); |
| 168 | |
| 169 | return bo; |
| 170 | |
| 171 | err: |
| 172 | tegra_bo_free_object(&bo->gem); |
| 173 | return ERR_PTR(ret); |
| 174 | } |
| 175 | |
| 176 | void tegra_bo_free_object(struct drm_gem_object *gem) |
| 177 | { |
| 178 | struct tegra_bo *bo = to_tegra_bo(gem); |
| 179 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 180 | drm_gem_free_mmap_offset(gem); |
Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 181 | |
| 182 | drm_gem_object_release(gem); |
| 183 | tegra_bo_destroy(gem->dev, bo); |
| 184 | |
| 185 | kfree(bo); |
| 186 | } |
| 187 | |
| 188 | int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm, |
| 189 | struct drm_mode_create_dumb *args) |
| 190 | { |
| 191 | int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 192 | struct tegra_bo *bo; |
| 193 | |
| 194 | if (args->pitch < min_pitch) |
| 195 | args->pitch = min_pitch; |
| 196 | |
| 197 | if (args->size < args->pitch * args->height) |
| 198 | args->size = args->pitch * args->height; |
| 199 | |
| 200 | bo = tegra_bo_create_with_handle(file, drm, args->size, |
| 201 | &args->handle); |
| 202 | if (IS_ERR(bo)) |
| 203 | return PTR_ERR(bo); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm, |
| 209 | uint32_t handle, uint64_t *offset) |
| 210 | { |
| 211 | struct drm_gem_object *gem; |
| 212 | struct tegra_bo *bo; |
| 213 | |
| 214 | mutex_lock(&drm->struct_mutex); |
| 215 | |
| 216 | gem = drm_gem_object_lookup(drm, file, handle); |
| 217 | if (!gem) { |
| 218 | dev_err(drm->dev, "failed to lookup GEM object\n"); |
| 219 | mutex_unlock(&drm->struct_mutex); |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | bo = to_tegra_bo(gem); |
| 224 | |
David Herrmann | 2bc7b0c | 2013-08-13 14:19:58 +0200 | [diff] [blame^] | 225 | *offset = drm_vma_node_offset_addr(&bo->gem.vma_node); |
Arto Merilainen | de2ba66 | 2013-03-22 16:34:08 +0200 | [diff] [blame] | 226 | |
| 227 | drm_gem_object_unreference(gem); |
| 228 | |
| 229 | mutex_unlock(&drm->struct_mutex); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | const struct vm_operations_struct tegra_bo_vm_ops = { |
| 235 | .open = drm_gem_vm_open, |
| 236 | .close = drm_gem_vm_close, |
| 237 | }; |
| 238 | |
| 239 | int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma) |
| 240 | { |
| 241 | struct drm_gem_object *gem; |
| 242 | struct tegra_bo *bo; |
| 243 | int ret; |
| 244 | |
| 245 | ret = drm_gem_mmap(file, vma); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | gem = vma->vm_private_data; |
| 250 | bo = to_tegra_bo(gem); |
| 251 | |
| 252 | ret = remap_pfn_range(vma, vma->vm_start, bo->paddr >> PAGE_SHIFT, |
| 253 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 254 | if (ret) |
| 255 | drm_gem_vm_close(vma); |
| 256 | |
| 257 | return ret; |
| 258 | } |