Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Xen dma-buf functionality for gntdev. |
| 5 | * |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 6 | * DMA buffer implementation is based on drivers/gpu/drm/drm_prime.c. |
| 7 | * |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 8 | * Copyright (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 13 | #include <linux/dma-buf.h> |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/uaccess.h> |
| 17 | |
| 18 | #include <xen/xen.h> |
| 19 | #include <xen/grant_table.h> |
| 20 | |
| 21 | #include "gntdev-common.h" |
| 22 | #include "gntdev-dmabuf.h" |
| 23 | |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 24 | #ifndef GRANT_INVALID_REF |
| 25 | /* |
| 26 | * Note on usage of grant reference 0 as invalid grant reference: |
| 27 | * grant reference 0 is valid, but never exposed to a driver, |
| 28 | * because of the fact it is already in use/reserved by the PV console. |
| 29 | */ |
| 30 | #define GRANT_INVALID_REF 0 |
| 31 | #endif |
| 32 | |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 33 | struct gntdev_dmabuf { |
| 34 | struct gntdev_dmabuf_priv *priv; |
| 35 | struct dma_buf *dmabuf; |
| 36 | struct list_head next; |
| 37 | int fd; |
| 38 | |
| 39 | union { |
| 40 | struct { |
| 41 | /* Exported buffers are reference counted. */ |
| 42 | struct kref refcount; |
| 43 | |
| 44 | struct gntdev_priv *priv; |
| 45 | struct gntdev_grant_map *map; |
| 46 | } exp; |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 47 | struct { |
| 48 | /* Granted references of the imported buffer. */ |
| 49 | grant_ref_t *refs; |
| 50 | /* Scatter-gather table of the imported buffer. */ |
| 51 | struct sg_table *sgt; |
| 52 | /* dma-buf attachment of the imported buffer. */ |
| 53 | struct dma_buf_attachment *attach; |
| 54 | } imp; |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 55 | } u; |
| 56 | |
| 57 | /* Number of pages this buffer has. */ |
| 58 | int nr_pages; |
| 59 | /* Pages of this buffer. */ |
| 60 | struct page **pages; |
| 61 | }; |
| 62 | |
| 63 | struct gntdev_dmabuf_wait_obj { |
| 64 | struct list_head next; |
| 65 | struct gntdev_dmabuf *gntdev_dmabuf; |
| 66 | struct completion completion; |
| 67 | }; |
| 68 | |
| 69 | struct gntdev_dmabuf_attachment { |
| 70 | struct sg_table *sgt; |
| 71 | enum dma_data_direction dir; |
| 72 | }; |
| 73 | |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 74 | struct gntdev_dmabuf_priv { |
| 75 | /* List of exported DMA buffers. */ |
| 76 | struct list_head exp_list; |
| 77 | /* List of wait objects. */ |
| 78 | struct list_head exp_wait_list; |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 79 | /* List of imported DMA buffers. */ |
| 80 | struct list_head imp_list; |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 81 | /* This is the lock which protects dma_buf_xxx lists. */ |
| 82 | struct mutex lock; |
| 83 | }; |
| 84 | |
| 85 | /* DMA buffer export support. */ |
| 86 | |
| 87 | /* Implementation of wait for exported DMA buffer to be released. */ |
| 88 | |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 89 | static void dmabuf_exp_release(struct kref *kref); |
| 90 | |
| 91 | static struct gntdev_dmabuf_wait_obj * |
| 92 | dmabuf_exp_wait_obj_new(struct gntdev_dmabuf_priv *priv, |
| 93 | struct gntdev_dmabuf *gntdev_dmabuf) |
| 94 | { |
| 95 | struct gntdev_dmabuf_wait_obj *obj; |
| 96 | |
| 97 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 98 | if (!obj) |
| 99 | return ERR_PTR(-ENOMEM); |
| 100 | |
| 101 | init_completion(&obj->completion); |
| 102 | obj->gntdev_dmabuf = gntdev_dmabuf; |
| 103 | |
| 104 | mutex_lock(&priv->lock); |
| 105 | list_add(&obj->next, &priv->exp_wait_list); |
| 106 | /* Put our reference and wait for gntdev_dmabuf's release to fire. */ |
| 107 | kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release); |
| 108 | mutex_unlock(&priv->lock); |
| 109 | return obj; |
| 110 | } |
| 111 | |
| 112 | static void dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv *priv, |
| 113 | struct gntdev_dmabuf_wait_obj *obj) |
| 114 | { |
| 115 | mutex_lock(&priv->lock); |
| 116 | list_del(&obj->next); |
| 117 | mutex_unlock(&priv->lock); |
| 118 | kfree(obj); |
| 119 | } |
| 120 | |
| 121 | static int dmabuf_exp_wait_obj_wait(struct gntdev_dmabuf_wait_obj *obj, |
| 122 | u32 wait_to_ms) |
| 123 | { |
| 124 | if (wait_for_completion_timeout(&obj->completion, |
| 125 | msecs_to_jiffies(wait_to_ms)) <= 0) |
| 126 | return -ETIMEDOUT; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static void dmabuf_exp_wait_obj_signal(struct gntdev_dmabuf_priv *priv, |
| 132 | struct gntdev_dmabuf *gntdev_dmabuf) |
| 133 | { |
| 134 | struct gntdev_dmabuf_wait_obj *obj; |
| 135 | |
| 136 | list_for_each_entry(obj, &priv->exp_wait_list, next) |
| 137 | if (obj->gntdev_dmabuf == gntdev_dmabuf) { |
| 138 | pr_debug("Found gntdev_dmabuf in the wait list, wake\n"); |
| 139 | complete_all(&obj->completion); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static struct gntdev_dmabuf * |
| 145 | dmabuf_exp_wait_obj_get_dmabuf(struct gntdev_dmabuf_priv *priv, int fd) |
| 146 | { |
| 147 | struct gntdev_dmabuf *gntdev_dmabuf, *ret = ERR_PTR(-ENOENT); |
| 148 | |
| 149 | mutex_lock(&priv->lock); |
| 150 | list_for_each_entry(gntdev_dmabuf, &priv->exp_list, next) |
| 151 | if (gntdev_dmabuf->fd == fd) { |
| 152 | pr_debug("Found gntdev_dmabuf in the wait list\n"); |
| 153 | kref_get(&gntdev_dmabuf->u.exp.refcount); |
| 154 | ret = gntdev_dmabuf; |
| 155 | break; |
| 156 | } |
| 157 | mutex_unlock(&priv->lock); |
| 158 | return ret; |
| 159 | } |
| 160 | |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 161 | static int dmabuf_exp_wait_released(struct gntdev_dmabuf_priv *priv, int fd, |
| 162 | int wait_to_ms) |
| 163 | { |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 164 | struct gntdev_dmabuf *gntdev_dmabuf; |
| 165 | struct gntdev_dmabuf_wait_obj *obj; |
| 166 | int ret; |
| 167 | |
| 168 | pr_debug("Will wait for dma-buf with fd %d\n", fd); |
| 169 | /* |
| 170 | * Try to find the DMA buffer: if not found means that |
| 171 | * either the buffer has already been released or file descriptor |
| 172 | * provided is wrong. |
| 173 | */ |
| 174 | gntdev_dmabuf = dmabuf_exp_wait_obj_get_dmabuf(priv, fd); |
| 175 | if (IS_ERR(gntdev_dmabuf)) |
| 176 | return PTR_ERR(gntdev_dmabuf); |
| 177 | |
| 178 | /* |
| 179 | * gntdev_dmabuf still exists and is reference count locked by us now, |
| 180 | * so prepare to wait: allocate wait object and add it to the wait list, |
| 181 | * so we can find it on release. |
| 182 | */ |
| 183 | obj = dmabuf_exp_wait_obj_new(priv, gntdev_dmabuf); |
| 184 | if (IS_ERR(obj)) |
| 185 | return PTR_ERR(obj); |
| 186 | |
| 187 | ret = dmabuf_exp_wait_obj_wait(obj, wait_to_ms); |
| 188 | dmabuf_exp_wait_obj_free(priv, obj); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* DMA buffer export support. */ |
| 193 | |
| 194 | static struct sg_table * |
| 195 | dmabuf_pages_to_sgt(struct page **pages, unsigned int nr_pages) |
| 196 | { |
| 197 | struct sg_table *sgt; |
| 198 | int ret; |
| 199 | |
| 200 | sgt = kmalloc(sizeof(*sgt), GFP_KERNEL); |
| 201 | if (!sgt) { |
| 202 | ret = -ENOMEM; |
| 203 | goto out; |
| 204 | } |
| 205 | |
| 206 | ret = sg_alloc_table_from_pages(sgt, pages, nr_pages, 0, |
| 207 | nr_pages << PAGE_SHIFT, |
| 208 | GFP_KERNEL); |
| 209 | if (ret) |
| 210 | goto out; |
| 211 | |
| 212 | return sgt; |
| 213 | |
| 214 | out: |
| 215 | kfree(sgt); |
| 216 | return ERR_PTR(ret); |
| 217 | } |
| 218 | |
| 219 | static int dmabuf_exp_ops_attach(struct dma_buf *dma_buf, |
| 220 | struct device *target_dev, |
| 221 | struct dma_buf_attachment *attach) |
| 222 | { |
| 223 | struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach; |
| 224 | |
| 225 | gntdev_dmabuf_attach = kzalloc(sizeof(*gntdev_dmabuf_attach), |
| 226 | GFP_KERNEL); |
| 227 | if (!gntdev_dmabuf_attach) |
| 228 | return -ENOMEM; |
| 229 | |
| 230 | gntdev_dmabuf_attach->dir = DMA_NONE; |
| 231 | attach->priv = gntdev_dmabuf_attach; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static void dmabuf_exp_ops_detach(struct dma_buf *dma_buf, |
| 236 | struct dma_buf_attachment *attach) |
| 237 | { |
| 238 | struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv; |
| 239 | |
| 240 | if (gntdev_dmabuf_attach) { |
| 241 | struct sg_table *sgt = gntdev_dmabuf_attach->sgt; |
| 242 | |
| 243 | if (sgt) { |
| 244 | if (gntdev_dmabuf_attach->dir != DMA_NONE) |
| 245 | dma_unmap_sg_attrs(attach->dev, sgt->sgl, |
| 246 | sgt->nents, |
| 247 | gntdev_dmabuf_attach->dir, |
| 248 | DMA_ATTR_SKIP_CPU_SYNC); |
| 249 | sg_free_table(sgt); |
| 250 | } |
| 251 | |
| 252 | kfree(sgt); |
| 253 | kfree(gntdev_dmabuf_attach); |
| 254 | attach->priv = NULL; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static struct sg_table * |
| 259 | dmabuf_exp_ops_map_dma_buf(struct dma_buf_attachment *attach, |
| 260 | enum dma_data_direction dir) |
| 261 | { |
| 262 | struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv; |
| 263 | struct gntdev_dmabuf *gntdev_dmabuf = attach->dmabuf->priv; |
| 264 | struct sg_table *sgt; |
| 265 | |
| 266 | pr_debug("Mapping %d pages for dev %p\n", gntdev_dmabuf->nr_pages, |
| 267 | attach->dev); |
| 268 | |
| 269 | if (dir == DMA_NONE || !gntdev_dmabuf_attach) |
| 270 | return ERR_PTR(-EINVAL); |
| 271 | |
| 272 | /* Return the cached mapping when possible. */ |
| 273 | if (gntdev_dmabuf_attach->dir == dir) |
| 274 | return gntdev_dmabuf_attach->sgt; |
| 275 | |
| 276 | /* |
| 277 | * Two mappings with different directions for the same attachment are |
| 278 | * not allowed. |
| 279 | */ |
| 280 | if (gntdev_dmabuf_attach->dir != DMA_NONE) |
| 281 | return ERR_PTR(-EBUSY); |
| 282 | |
| 283 | sgt = dmabuf_pages_to_sgt(gntdev_dmabuf->pages, |
| 284 | gntdev_dmabuf->nr_pages); |
| 285 | if (!IS_ERR(sgt)) { |
| 286 | if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir, |
| 287 | DMA_ATTR_SKIP_CPU_SYNC)) { |
| 288 | sg_free_table(sgt); |
| 289 | kfree(sgt); |
| 290 | sgt = ERR_PTR(-ENOMEM); |
| 291 | } else { |
| 292 | gntdev_dmabuf_attach->sgt = sgt; |
| 293 | gntdev_dmabuf_attach->dir = dir; |
| 294 | } |
| 295 | } |
| 296 | if (IS_ERR(sgt)) |
| 297 | pr_debug("Failed to map sg table for dev %p\n", attach->dev); |
| 298 | return sgt; |
| 299 | } |
| 300 | |
| 301 | static void dmabuf_exp_ops_unmap_dma_buf(struct dma_buf_attachment *attach, |
| 302 | struct sg_table *sgt, |
| 303 | enum dma_data_direction dir) |
| 304 | { |
| 305 | /* Not implemented. The unmap is done at dmabuf_exp_ops_detach(). */ |
| 306 | } |
| 307 | |
| 308 | static void dmabuf_exp_release(struct kref *kref) |
| 309 | { |
| 310 | struct gntdev_dmabuf *gntdev_dmabuf = |
| 311 | container_of(kref, struct gntdev_dmabuf, u.exp.refcount); |
| 312 | |
| 313 | dmabuf_exp_wait_obj_signal(gntdev_dmabuf->priv, gntdev_dmabuf); |
| 314 | list_del(&gntdev_dmabuf->next); |
| 315 | kfree(gntdev_dmabuf); |
| 316 | } |
| 317 | |
| 318 | static void dmabuf_exp_remove_map(struct gntdev_priv *priv, |
| 319 | struct gntdev_grant_map *map) |
| 320 | { |
| 321 | mutex_lock(&priv->lock); |
| 322 | list_del(&map->next); |
| 323 | gntdev_put_map(NULL /* already removed */, map); |
| 324 | mutex_unlock(&priv->lock); |
| 325 | } |
| 326 | |
| 327 | static void dmabuf_exp_ops_release(struct dma_buf *dma_buf) |
| 328 | { |
| 329 | struct gntdev_dmabuf *gntdev_dmabuf = dma_buf->priv; |
| 330 | struct gntdev_dmabuf_priv *priv = gntdev_dmabuf->priv; |
| 331 | |
| 332 | dmabuf_exp_remove_map(gntdev_dmabuf->u.exp.priv, |
| 333 | gntdev_dmabuf->u.exp.map); |
| 334 | mutex_lock(&priv->lock); |
| 335 | kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release); |
| 336 | mutex_unlock(&priv->lock); |
| 337 | } |
| 338 | |
| 339 | static void *dmabuf_exp_ops_kmap(struct dma_buf *dma_buf, |
| 340 | unsigned long page_num) |
| 341 | { |
| 342 | /* Not implemented. */ |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | static void dmabuf_exp_ops_kunmap(struct dma_buf *dma_buf, |
| 347 | unsigned long page_num, void *addr) |
| 348 | { |
| 349 | /* Not implemented. */ |
| 350 | } |
| 351 | |
| 352 | static int dmabuf_exp_ops_mmap(struct dma_buf *dma_buf, |
| 353 | struct vm_area_struct *vma) |
| 354 | { |
| 355 | /* Not implemented. */ |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static const struct dma_buf_ops dmabuf_exp_ops = { |
| 360 | .attach = dmabuf_exp_ops_attach, |
| 361 | .detach = dmabuf_exp_ops_detach, |
| 362 | .map_dma_buf = dmabuf_exp_ops_map_dma_buf, |
| 363 | .unmap_dma_buf = dmabuf_exp_ops_unmap_dma_buf, |
| 364 | .release = dmabuf_exp_ops_release, |
| 365 | .map = dmabuf_exp_ops_kmap, |
| 366 | .unmap = dmabuf_exp_ops_kunmap, |
| 367 | .mmap = dmabuf_exp_ops_mmap, |
| 368 | }; |
| 369 | |
| 370 | struct gntdev_dmabuf_export_args { |
| 371 | struct gntdev_priv *priv; |
| 372 | struct gntdev_grant_map *map; |
| 373 | struct gntdev_dmabuf_priv *dmabuf_priv; |
| 374 | struct device *dev; |
| 375 | int count; |
| 376 | struct page **pages; |
| 377 | u32 fd; |
| 378 | }; |
| 379 | |
| 380 | static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args) |
| 381 | { |
| 382 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 383 | struct gntdev_dmabuf *gntdev_dmabuf; |
| 384 | int ret; |
| 385 | |
| 386 | gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL); |
| 387 | if (!gntdev_dmabuf) |
| 388 | return -ENOMEM; |
| 389 | |
| 390 | kref_init(&gntdev_dmabuf->u.exp.refcount); |
| 391 | |
| 392 | gntdev_dmabuf->priv = args->dmabuf_priv; |
| 393 | gntdev_dmabuf->nr_pages = args->count; |
| 394 | gntdev_dmabuf->pages = args->pages; |
| 395 | gntdev_dmabuf->u.exp.priv = args->priv; |
| 396 | gntdev_dmabuf->u.exp.map = args->map; |
| 397 | |
| 398 | exp_info.exp_name = KBUILD_MODNAME; |
| 399 | if (args->dev->driver && args->dev->driver->owner) |
| 400 | exp_info.owner = args->dev->driver->owner; |
| 401 | else |
| 402 | exp_info.owner = THIS_MODULE; |
| 403 | exp_info.ops = &dmabuf_exp_ops; |
| 404 | exp_info.size = args->count << PAGE_SHIFT; |
| 405 | exp_info.flags = O_RDWR; |
| 406 | exp_info.priv = gntdev_dmabuf; |
| 407 | |
| 408 | gntdev_dmabuf->dmabuf = dma_buf_export(&exp_info); |
| 409 | if (IS_ERR(gntdev_dmabuf->dmabuf)) { |
| 410 | ret = PTR_ERR(gntdev_dmabuf->dmabuf); |
| 411 | gntdev_dmabuf->dmabuf = NULL; |
| 412 | goto fail; |
| 413 | } |
| 414 | |
| 415 | ret = dma_buf_fd(gntdev_dmabuf->dmabuf, O_CLOEXEC); |
| 416 | if (ret < 0) |
| 417 | goto fail; |
| 418 | |
| 419 | gntdev_dmabuf->fd = ret; |
| 420 | args->fd = ret; |
| 421 | |
| 422 | pr_debug("Exporting DMA buffer with fd %d\n", ret); |
| 423 | |
| 424 | mutex_lock(&args->dmabuf_priv->lock); |
| 425 | list_add(&gntdev_dmabuf->next, &args->dmabuf_priv->exp_list); |
| 426 | mutex_unlock(&args->dmabuf_priv->lock); |
| 427 | return 0; |
| 428 | |
| 429 | fail: |
| 430 | if (gntdev_dmabuf->dmabuf) |
| 431 | dma_buf_put(gntdev_dmabuf->dmabuf); |
| 432 | kfree(gntdev_dmabuf); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static struct gntdev_grant_map * |
| 437 | dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags, |
| 438 | int count) |
| 439 | { |
| 440 | struct gntdev_grant_map *map; |
| 441 | |
| 442 | if (unlikely(count <= 0)) |
| 443 | return ERR_PTR(-EINVAL); |
| 444 | |
| 445 | if ((dmabuf_flags & GNTDEV_DMA_FLAG_WC) && |
| 446 | (dmabuf_flags & GNTDEV_DMA_FLAG_COHERENT)) { |
| 447 | pr_debug("Wrong dma-buf flags: 0x%x\n", dmabuf_flags); |
| 448 | return ERR_PTR(-EINVAL); |
| 449 | } |
| 450 | |
| 451 | map = gntdev_alloc_map(priv, count, dmabuf_flags); |
| 452 | if (!map) |
| 453 | return ERR_PTR(-ENOMEM); |
| 454 | |
| 455 | if (unlikely(gntdev_account_mapped_pages(count))) { |
| 456 | pr_debug("can't map %d pages: over limit\n", count); |
| 457 | gntdev_put_map(NULL, map); |
| 458 | return ERR_PTR(-ENOMEM); |
| 459 | } |
| 460 | return map; |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, |
| 464 | int count, u32 domid, u32 *refs, u32 *fd) |
| 465 | { |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 466 | struct gntdev_grant_map *map; |
| 467 | struct gntdev_dmabuf_export_args args; |
| 468 | int i, ret; |
| 469 | |
| 470 | map = dmabuf_exp_alloc_backing_storage(priv, flags, count); |
| 471 | if (IS_ERR(map)) |
| 472 | return PTR_ERR(map); |
| 473 | |
| 474 | for (i = 0; i < count; i++) { |
| 475 | map->grants[i].domid = domid; |
| 476 | map->grants[i].ref = refs[i]; |
| 477 | } |
| 478 | |
| 479 | mutex_lock(&priv->lock); |
| 480 | gntdev_add_map(priv, map); |
| 481 | mutex_unlock(&priv->lock); |
| 482 | |
| 483 | map->flags |= GNTMAP_host_map; |
| 484 | #if defined(CONFIG_X86) |
| 485 | map->flags |= GNTMAP_device_map; |
| 486 | #endif |
| 487 | |
| 488 | ret = gntdev_map_grant_pages(map); |
| 489 | if (ret < 0) |
| 490 | goto out; |
| 491 | |
| 492 | args.priv = priv; |
| 493 | args.map = map; |
| 494 | args.dev = priv->dma_dev; |
| 495 | args.dmabuf_priv = priv->dmabuf_priv; |
| 496 | args.count = map->count; |
| 497 | args.pages = map->pages; |
| 498 | args.fd = -1; /* Shut up unnecessary gcc warning for i386 */ |
| 499 | |
| 500 | ret = dmabuf_exp_from_pages(&args); |
| 501 | if (ret < 0) |
| 502 | goto out; |
| 503 | |
| 504 | *fd = args.fd; |
| 505 | return 0; |
| 506 | |
| 507 | out: |
| 508 | dmabuf_exp_remove_map(priv, map); |
| 509 | return ret; |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* DMA buffer import support. */ |
| 513 | |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 514 | static int |
| 515 | dmabuf_imp_grant_foreign_access(struct page **pages, u32 *refs, |
| 516 | int count, int domid) |
| 517 | { |
| 518 | grant_ref_t priv_gref_head; |
| 519 | int i, ret; |
| 520 | |
| 521 | ret = gnttab_alloc_grant_references(count, &priv_gref_head); |
| 522 | if (ret < 0) { |
| 523 | pr_debug("Cannot allocate grant references, ret %d\n", ret); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | for (i = 0; i < count; i++) { |
| 528 | int cur_ref; |
| 529 | |
| 530 | cur_ref = gnttab_claim_grant_reference(&priv_gref_head); |
| 531 | if (cur_ref < 0) { |
| 532 | ret = cur_ref; |
| 533 | pr_debug("Cannot claim grant reference, ret %d\n", ret); |
| 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | gnttab_grant_foreign_access_ref(cur_ref, domid, |
| 538 | xen_page_to_gfn(pages[i]), 0); |
| 539 | refs[i] = cur_ref; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | |
| 544 | out: |
| 545 | gnttab_free_grant_references(priv_gref_head); |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | static void dmabuf_imp_end_foreign_access(u32 *refs, int count) |
| 550 | { |
| 551 | int i; |
| 552 | |
| 553 | for (i = 0; i < count; i++) |
| 554 | if (refs[i] != GRANT_INVALID_REF) |
| 555 | gnttab_end_foreign_access(refs[i], 0, 0UL); |
| 556 | } |
| 557 | |
| 558 | static void dmabuf_imp_free_storage(struct gntdev_dmabuf *gntdev_dmabuf) |
| 559 | { |
| 560 | kfree(gntdev_dmabuf->pages); |
| 561 | kfree(gntdev_dmabuf->u.imp.refs); |
| 562 | kfree(gntdev_dmabuf); |
| 563 | } |
| 564 | |
| 565 | static struct gntdev_dmabuf *dmabuf_imp_alloc_storage(int count) |
| 566 | { |
| 567 | struct gntdev_dmabuf *gntdev_dmabuf; |
| 568 | int i; |
| 569 | |
| 570 | gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL); |
| 571 | if (!gntdev_dmabuf) |
| 572 | goto fail; |
| 573 | |
| 574 | gntdev_dmabuf->u.imp.refs = kcalloc(count, |
| 575 | sizeof(gntdev_dmabuf->u.imp.refs[0]), |
| 576 | GFP_KERNEL); |
| 577 | if (!gntdev_dmabuf->u.imp.refs) |
| 578 | goto fail; |
| 579 | |
| 580 | gntdev_dmabuf->pages = kcalloc(count, |
| 581 | sizeof(gntdev_dmabuf->pages[0]), |
| 582 | GFP_KERNEL); |
| 583 | if (!gntdev_dmabuf->pages) |
| 584 | goto fail; |
| 585 | |
| 586 | gntdev_dmabuf->nr_pages = count; |
| 587 | |
| 588 | for (i = 0; i < count; i++) |
| 589 | gntdev_dmabuf->u.imp.refs[i] = GRANT_INVALID_REF; |
| 590 | |
| 591 | return gntdev_dmabuf; |
| 592 | |
| 593 | fail: |
| 594 | dmabuf_imp_free_storage(gntdev_dmabuf); |
| 595 | return ERR_PTR(-ENOMEM); |
| 596 | } |
| 597 | |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 598 | static struct gntdev_dmabuf * |
| 599 | dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev, |
| 600 | int fd, int count, int domid) |
| 601 | { |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 602 | struct gntdev_dmabuf *gntdev_dmabuf, *ret; |
| 603 | struct dma_buf *dma_buf; |
| 604 | struct dma_buf_attachment *attach; |
| 605 | struct sg_table *sgt; |
| 606 | struct sg_page_iter sg_iter; |
| 607 | int i; |
| 608 | |
| 609 | dma_buf = dma_buf_get(fd); |
| 610 | if (IS_ERR(dma_buf)) |
| 611 | return ERR_CAST(dma_buf); |
| 612 | |
| 613 | gntdev_dmabuf = dmabuf_imp_alloc_storage(count); |
| 614 | if (IS_ERR(gntdev_dmabuf)) { |
| 615 | ret = gntdev_dmabuf; |
| 616 | goto fail_put; |
| 617 | } |
| 618 | |
| 619 | gntdev_dmabuf->priv = priv; |
| 620 | gntdev_dmabuf->fd = fd; |
| 621 | |
| 622 | attach = dma_buf_attach(dma_buf, dev); |
| 623 | if (IS_ERR(attach)) { |
| 624 | ret = ERR_CAST(attach); |
| 625 | goto fail_free_obj; |
| 626 | } |
| 627 | |
| 628 | gntdev_dmabuf->u.imp.attach = attach; |
| 629 | |
| 630 | sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); |
| 631 | if (IS_ERR(sgt)) { |
| 632 | ret = ERR_CAST(sgt); |
| 633 | goto fail_detach; |
| 634 | } |
| 635 | |
| 636 | /* Check number of pages that imported buffer has. */ |
| 637 | if (attach->dmabuf->size != gntdev_dmabuf->nr_pages << PAGE_SHIFT) { |
| 638 | ret = ERR_PTR(-EINVAL); |
| 639 | pr_debug("DMA buffer has %zu pages, user-space expects %d\n", |
| 640 | attach->dmabuf->size, gntdev_dmabuf->nr_pages); |
| 641 | goto fail_unmap; |
| 642 | } |
| 643 | |
| 644 | gntdev_dmabuf->u.imp.sgt = sgt; |
| 645 | |
| 646 | /* Now convert sgt to array of pages and check for page validity. */ |
| 647 | i = 0; |
| 648 | for_each_sg_page(sgt->sgl, &sg_iter, sgt->nents, 0) { |
| 649 | struct page *page = sg_page_iter_page(&sg_iter); |
| 650 | /* |
| 651 | * Check if page is valid: this can happen if we are given |
| 652 | * a page from VRAM or other resources which are not backed |
| 653 | * by a struct page. |
| 654 | */ |
| 655 | if (!pfn_valid(page_to_pfn(page))) { |
| 656 | ret = ERR_PTR(-EINVAL); |
| 657 | goto fail_unmap; |
| 658 | } |
| 659 | |
| 660 | gntdev_dmabuf->pages[i++] = page; |
| 661 | } |
| 662 | |
| 663 | ret = ERR_PTR(dmabuf_imp_grant_foreign_access(gntdev_dmabuf->pages, |
| 664 | gntdev_dmabuf->u.imp.refs, |
| 665 | count, domid)); |
| 666 | if (IS_ERR(ret)) |
| 667 | goto fail_end_access; |
| 668 | |
| 669 | pr_debug("Imported DMA buffer with fd %d\n", fd); |
| 670 | |
| 671 | mutex_lock(&priv->lock); |
| 672 | list_add(&gntdev_dmabuf->next, &priv->imp_list); |
| 673 | mutex_unlock(&priv->lock); |
| 674 | |
| 675 | return gntdev_dmabuf; |
| 676 | |
| 677 | fail_end_access: |
| 678 | dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs, count); |
| 679 | fail_unmap: |
| 680 | dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); |
| 681 | fail_detach: |
| 682 | dma_buf_detach(dma_buf, attach); |
| 683 | fail_free_obj: |
| 684 | dmabuf_imp_free_storage(gntdev_dmabuf); |
| 685 | fail_put: |
| 686 | dma_buf_put(dma_buf); |
| 687 | return ret; |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 688 | } |
| 689 | |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 690 | /* |
| 691 | * Find the hyper dma-buf by its file descriptor and remove |
| 692 | * it from the buffer's list. |
| 693 | */ |
| 694 | static struct gntdev_dmabuf * |
| 695 | dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv *priv, int fd) |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 696 | { |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 697 | struct gntdev_dmabuf *q, *gntdev_dmabuf, *ret = ERR_PTR(-ENOENT); |
| 698 | |
| 699 | mutex_lock(&priv->lock); |
| 700 | list_for_each_entry_safe(gntdev_dmabuf, q, &priv->imp_list, next) { |
| 701 | if (gntdev_dmabuf->fd == fd) { |
| 702 | pr_debug("Found gntdev_dmabuf in the import list\n"); |
| 703 | ret = gntdev_dmabuf; |
| 704 | list_del(&gntdev_dmabuf->next); |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | mutex_unlock(&priv->lock); |
| 709 | return ret; |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd) |
| 713 | { |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 714 | struct gntdev_dmabuf *gntdev_dmabuf; |
| 715 | struct dma_buf_attachment *attach; |
| 716 | struct dma_buf *dma_buf; |
| 717 | |
| 718 | gntdev_dmabuf = dmabuf_imp_find_unlink(priv, fd); |
| 719 | if (IS_ERR(gntdev_dmabuf)) |
| 720 | return PTR_ERR(gntdev_dmabuf); |
| 721 | |
| 722 | pr_debug("Releasing DMA buffer with fd %d\n", fd); |
| 723 | |
| 724 | dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs, |
| 725 | gntdev_dmabuf->nr_pages); |
| 726 | |
| 727 | attach = gntdev_dmabuf->u.imp.attach; |
| 728 | |
| 729 | if (gntdev_dmabuf->u.imp.sgt) |
| 730 | dma_buf_unmap_attachment(attach, gntdev_dmabuf->u.imp.sgt, |
| 731 | DMA_BIDIRECTIONAL); |
| 732 | dma_buf = attach->dmabuf; |
| 733 | dma_buf_detach(attach->dmabuf, attach); |
| 734 | dma_buf_put(dma_buf); |
| 735 | |
| 736 | dmabuf_imp_free_storage(gntdev_dmabuf); |
| 737 | return 0; |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* DMA buffer IOCTL support. */ |
| 741 | |
| 742 | long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_ptemod, |
| 743 | struct ioctl_gntdev_dmabuf_exp_from_refs __user *u) |
| 744 | { |
| 745 | struct ioctl_gntdev_dmabuf_exp_from_refs op; |
| 746 | u32 *refs; |
| 747 | long ret; |
| 748 | |
| 749 | if (use_ptemod) { |
| 750 | pr_debug("Cannot provide dma-buf: use_ptemode %d\n", |
| 751 | use_ptemod); |
| 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 756 | return -EFAULT; |
| 757 | |
| 758 | if (unlikely(op.count <= 0)) |
| 759 | return -EINVAL; |
| 760 | |
| 761 | refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL); |
| 762 | if (!refs) |
| 763 | return -ENOMEM; |
| 764 | |
| 765 | if (copy_from_user(refs, u->refs, sizeof(*refs) * op.count) != 0) { |
| 766 | ret = -EFAULT; |
| 767 | goto out; |
| 768 | } |
| 769 | |
| 770 | ret = dmabuf_exp_from_refs(priv, op.flags, op.count, |
| 771 | op.domid, refs, &op.fd); |
| 772 | if (ret) |
| 773 | goto out; |
| 774 | |
| 775 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 776 | ret = -EFAULT; |
| 777 | |
| 778 | out: |
| 779 | kfree(refs); |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv, |
| 784 | struct ioctl_gntdev_dmabuf_exp_wait_released __user *u) |
| 785 | { |
| 786 | struct ioctl_gntdev_dmabuf_exp_wait_released op; |
| 787 | |
| 788 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 789 | return -EFAULT; |
| 790 | |
| 791 | return dmabuf_exp_wait_released(priv->dmabuf_priv, op.fd, |
| 792 | op.wait_to_ms); |
| 793 | } |
| 794 | |
| 795 | long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *priv, |
| 796 | struct ioctl_gntdev_dmabuf_imp_to_refs __user *u) |
| 797 | { |
| 798 | struct ioctl_gntdev_dmabuf_imp_to_refs op; |
| 799 | struct gntdev_dmabuf *gntdev_dmabuf; |
| 800 | long ret; |
| 801 | |
| 802 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 803 | return -EFAULT; |
| 804 | |
| 805 | if (unlikely(op.count <= 0)) |
| 806 | return -EINVAL; |
| 807 | |
| 808 | gntdev_dmabuf = dmabuf_imp_to_refs(priv->dmabuf_priv, |
| 809 | priv->dma_dev, op.fd, |
| 810 | op.count, op.domid); |
| 811 | if (IS_ERR(gntdev_dmabuf)) |
| 812 | return PTR_ERR(gntdev_dmabuf); |
| 813 | |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 814 | if (copy_to_user(u->refs, gntdev_dmabuf->u.imp.refs, |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 815 | sizeof(*u->refs) * op.count) != 0) { |
| 816 | ret = -EFAULT; |
| 817 | goto out_release; |
| 818 | } |
| 819 | return 0; |
| 820 | |
| 821 | out_release: |
| 822 | dmabuf_imp_release(priv->dmabuf_priv, op.fd); |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | long gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv *priv, |
| 827 | struct ioctl_gntdev_dmabuf_imp_release __user *u) |
| 828 | { |
| 829 | struct ioctl_gntdev_dmabuf_imp_release op; |
| 830 | |
| 831 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 832 | return -EFAULT; |
| 833 | |
| 834 | return dmabuf_imp_release(priv->dmabuf_priv, op.fd); |
| 835 | } |
| 836 | |
| 837 | struct gntdev_dmabuf_priv *gntdev_dmabuf_init(void) |
| 838 | { |
| 839 | struct gntdev_dmabuf_priv *priv; |
| 840 | |
| 841 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 842 | if (!priv) |
| 843 | return ERR_PTR(-ENOMEM); |
| 844 | |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 845 | mutex_init(&priv->lock); |
| 846 | INIT_LIST_HEAD(&priv->exp_list); |
| 847 | INIT_LIST_HEAD(&priv->exp_wait_list); |
Oleksandr Andrushchenko | bf8dc55 | 2018-07-20 12:01:50 +0300 | [diff] [blame^] | 848 | INIT_LIST_HEAD(&priv->imp_list); |
Oleksandr Andrushchenko | a240d6e | 2018-07-20 12:01:49 +0300 | [diff] [blame] | 849 | |
Oleksandr Andrushchenko | 932d656 | 2018-07-20 12:01:48 +0300 | [diff] [blame] | 850 | return priv; |
| 851 | } |
| 852 | |
| 853 | void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv) |
| 854 | { |
| 855 | kfree(priv); |
| 856 | } |