Zach Reizner | 502e95c | 2015-03-04 16:33:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * Copyright © 2014 The Chromium OS Authors |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | * IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Ben Widawsky <ben@bwidawsk.net> |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <linux/dma-buf.h> |
| 30 | #include "vgem_drv.h" |
| 31 | |
| 32 | struct sg_table *vgem_gem_prime_get_sg_table(struct drm_gem_object *gobj) |
| 33 | { |
| 34 | struct drm_vgem_gem_object *obj = to_vgem_bo(gobj); |
| 35 | BUG_ON(obj->pages == NULL); |
| 36 | |
| 37 | return drm_prime_pages_to_sg(obj->pages, obj->base.size / PAGE_SIZE); |
| 38 | } |
| 39 | |
| 40 | int vgem_gem_prime_pin(struct drm_gem_object *gobj) |
| 41 | { |
| 42 | struct drm_vgem_gem_object *obj = to_vgem_bo(gobj); |
| 43 | return vgem_gem_get_pages(obj); |
| 44 | } |
| 45 | |
| 46 | void vgem_gem_prime_unpin(struct drm_gem_object *gobj) |
| 47 | { |
| 48 | struct drm_vgem_gem_object *obj = to_vgem_bo(gobj); |
| 49 | vgem_gem_put_pages(obj); |
| 50 | } |
| 51 | |
| 52 | void *vgem_gem_prime_vmap(struct drm_gem_object *gobj) |
| 53 | { |
| 54 | struct drm_vgem_gem_object *obj = to_vgem_bo(gobj); |
| 55 | BUG_ON(obj->pages == NULL); |
| 56 | |
| 57 | return vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL); |
| 58 | } |
| 59 | |
| 60 | void vgem_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 61 | { |
| 62 | vunmap(vaddr); |
| 63 | } |
| 64 | |
| 65 | struct drm_gem_object *vgem_gem_prime_import(struct drm_device *dev, |
| 66 | struct dma_buf *dma_buf) |
| 67 | { |
| 68 | struct drm_vgem_gem_object *obj = NULL; |
| 69 | int ret; |
| 70 | |
| 71 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 72 | if (obj == NULL) { |
| 73 | ret = -ENOMEM; |
| 74 | goto fail; |
| 75 | } |
| 76 | |
| 77 | ret = drm_gem_object_init(dev, &obj->base, dma_buf->size); |
| 78 | if (ret) { |
| 79 | ret = -ENOMEM; |
| 80 | goto fail_free; |
| 81 | } |
| 82 | |
| 83 | get_dma_buf(dma_buf); |
| 84 | |
| 85 | obj->base.dma_buf = dma_buf; |
| 86 | obj->use_dma_buf = true; |
| 87 | |
| 88 | return &obj->base; |
| 89 | |
| 90 | fail_free: |
| 91 | kfree(obj); |
| 92 | fail: |
| 93 | return ERR_PTR(ret); |
| 94 | } |