Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_gem.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Author: Inki Dae <inki.dae@samsung.com> |
| 5 | * |
Inki Dae | d81aecb | 2012-12-18 02:30:17 +0900 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 10 | */ |
| 11 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 12 | #include <drm/drmP.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 13 | #include <drm/drm_vma_manager.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 14 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 15 | #include <linux/shmem_fs.h> |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 16 | #include <linux/dma-buf.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 17 | #include <drm/exynos_drm.h> |
| 18 | |
| 19 | #include "exynos_drm_drv.h" |
| 20 | #include "exynos_drm_gem.h" |
Vikas Sajjan | 3fec453 | 2013-08-23 12:05:06 +0530 | [diff] [blame] | 21 | #include "exynos_drm_iommu.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 22 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 23 | static int exynos_drm_alloc_buf(struct exynos_drm_gem *exynos_gem) |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 24 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 25 | struct drm_device *dev = exynos_gem->base.dev; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 26 | enum dma_attr attr; |
| 27 | unsigned int nr_pages; |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 28 | struct sg_table sgt; |
| 29 | int ret = -ENOMEM; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 30 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 31 | if (exynos_gem->dma_addr) { |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 32 | DRM_DEBUG_KMS("already allocated.\n"); |
| 33 | return 0; |
| 34 | } |
| 35 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 36 | init_dma_attrs(&exynos_gem->dma_attrs); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * if EXYNOS_BO_CONTIG, fully physically contiguous memory |
| 40 | * region will be allocated else physically contiguous |
| 41 | * as possible. |
| 42 | */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 43 | if (!(exynos_gem->flags & EXYNOS_BO_NONCONTIG)) |
| 44 | dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &exynos_gem->dma_attrs); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping |
| 48 | * else cachable mapping. |
| 49 | */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 50 | if (exynos_gem->flags & EXYNOS_BO_WC || |
| 51 | !(exynos_gem->flags & EXYNOS_BO_CACHABLE)) |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 52 | attr = DMA_ATTR_WRITE_COMBINE; |
| 53 | else |
| 54 | attr = DMA_ATTR_NON_CONSISTENT; |
| 55 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 56 | dma_set_attr(attr, &exynos_gem->dma_attrs); |
| 57 | dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &exynos_gem->dma_attrs); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 58 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 59 | nr_pages = exynos_gem->size >> PAGE_SHIFT; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 60 | |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 61 | exynos_gem->pages = drm_calloc_large(nr_pages, sizeof(struct page *)); |
| 62 | if (!exynos_gem->pages) { |
| 63 | DRM_ERROR("failed to allocate pages.\n"); |
| 64 | return -ENOMEM; |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame] | 65 | } |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 66 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 67 | exynos_gem->cookie = dma_alloc_attrs(dev->dev, exynos_gem->size, |
| 68 | &exynos_gem->dma_addr, GFP_KERNEL, |
| 69 | &exynos_gem->dma_attrs); |
| 70 | if (!exynos_gem->cookie) { |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame] | 71 | DRM_ERROR("failed to allocate buffer.\n"); |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 72 | goto err_free; |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 75 | ret = dma_get_sgtable_attrs(dev->dev, &sgt, exynos_gem->cookie, |
| 76 | exynos_gem->dma_addr, exynos_gem->size, |
| 77 | &exynos_gem->dma_attrs); |
| 78 | if (ret < 0) { |
| 79 | DRM_ERROR("failed to get sgtable.\n"); |
| 80 | goto err_dma_free; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 83 | if (drm_prime_sg_to_page_addr_arrays(&sgt, exynos_gem->pages, NULL, |
| 84 | nr_pages)) { |
| 85 | DRM_ERROR("invalid sgtable.\n"); |
| 86 | ret = -EINVAL; |
| 87 | goto err_sgt_free; |
| 88 | } |
| 89 | |
| 90 | sg_free_table(&sgt); |
| 91 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 92 | DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n", |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 93 | (unsigned long)exynos_gem->dma_addr, exynos_gem->size); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 94 | |
| 95 | return 0; |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 96 | |
| 97 | err_sgt_free: |
| 98 | sg_free_table(&sgt); |
| 99 | err_dma_free: |
| 100 | dma_free_attrs(dev->dev, exynos_gem->size, exynos_gem->cookie, |
| 101 | exynos_gem->dma_addr, &exynos_gem->dma_attrs); |
| 102 | err_free: |
| 103 | drm_free_large(exynos_gem->pages); |
| 104 | |
| 105 | return ret; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 108 | static void exynos_drm_free_buf(struct exynos_drm_gem *exynos_gem) |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 109 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 110 | struct drm_device *dev = exynos_gem->base.dev; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 111 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 112 | if (!exynos_gem->dma_addr) { |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 113 | DRM_DEBUG_KMS("dma_addr is invalid.\n"); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n", |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 118 | (unsigned long)exynos_gem->dma_addr, exynos_gem->size); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 119 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 120 | dma_free_attrs(dev->dev, exynos_gem->size, exynos_gem->cookie, |
| 121 | (dma_addr_t)exynos_gem->dma_addr, |
| 122 | &exynos_gem->dma_attrs); |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame] | 123 | |
Marek Szyprowski | df547bf7 | 2015-10-13 13:47:20 +0200 | [diff] [blame] | 124 | drm_free_large(exynos_gem->pages); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 127 | static int exynos_drm_gem_handle_create(struct drm_gem_object *obj, |
| 128 | struct drm_file *file_priv, |
| 129 | unsigned int *handle) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 130 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 131 | int ret; |
| 132 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 133 | /* |
| 134 | * allocate a id of idr table where the obj is registered |
| 135 | * and handle has the id what user can see. |
| 136 | */ |
| 137 | ret = drm_gem_handle_create(file_priv, obj, handle); |
| 138 | if (ret) |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 139 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 140 | |
| 141 | DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle); |
| 142 | |
| 143 | /* drop reference from allocate - handle holds it now. */ |
| 144 | drm_gem_object_unreference_unlocked(obj); |
| 145 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 146 | return 0; |
| 147 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 148 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 149 | void exynos_drm_gem_destroy(struct exynos_drm_gem *exynos_gem) |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 150 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 151 | struct drm_gem_object *obj = &exynos_gem->base; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 152 | |
Daniel Vetter | a8e11d1 | 2013-08-15 00:02:37 +0200 | [diff] [blame] | 153 | DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 154 | |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 155 | /* |
| 156 | * do not release memory region from exporter. |
| 157 | * |
| 158 | * the region will be released by exporter |
| 159 | * once dmabuf's refcount becomes 0. |
| 160 | */ |
| 161 | if (obj->import_attach) |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 162 | drm_prime_gem_destroy(obj, exynos_gem->sgt); |
Joonyoung Shim | 7c93537 | 2015-09-16 14:14:54 +0900 | [diff] [blame] | 163 | else |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 164 | exynos_drm_free_buf(exynos_gem); |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 165 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 166 | /* release file pointer to gem object. */ |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 167 | drm_gem_object_release(obj); |
| 168 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 169 | kfree(exynos_gem); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 172 | unsigned long exynos_drm_gem_get_size(struct drm_device *dev, |
| 173 | unsigned int gem_handle, |
| 174 | struct drm_file *file_priv) |
| 175 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 176 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 177 | struct drm_gem_object *obj; |
| 178 | |
| 179 | obj = drm_gem_object_lookup(dev, file_priv, gem_handle); |
| 180 | if (!obj) { |
| 181 | DRM_ERROR("failed to lookup gem object.\n"); |
| 182 | return 0; |
| 183 | } |
| 184 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 185 | exynos_gem = to_exynos_gem(obj); |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 186 | |
| 187 | drm_gem_object_unreference_unlocked(obj); |
| 188 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 189 | return exynos_gem->size; |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 192 | static struct exynos_drm_gem *exynos_drm_gem_init(struct drm_device *dev, |
| 193 | unsigned long size) |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 194 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 195 | struct exynos_drm_gem *exynos_gem; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 196 | struct drm_gem_object *obj; |
| 197 | int ret; |
| 198 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 199 | exynos_gem = kzalloc(sizeof(*exynos_gem), GFP_KERNEL); |
| 200 | if (!exynos_gem) |
Joonyoung Shim | 5f3f426 | 2015-07-28 17:53:22 +0900 | [diff] [blame] | 201 | return ERR_PTR(-ENOMEM); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 202 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 203 | exynos_gem->size = size; |
| 204 | obj = &exynos_gem->base; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 205 | |
| 206 | ret = drm_gem_object_init(dev, obj, size); |
| 207 | if (ret < 0) { |
| 208 | DRM_ERROR("failed to initialize gem object\n"); |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 209 | kfree(exynos_gem); |
Joonyoung Shim | 5f3f426 | 2015-07-28 17:53:22 +0900 | [diff] [blame] | 210 | return ERR_PTR(ret); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 211 | } |
| 212 | |
Joonyoung Shim | 48cf53f | 2015-07-28 17:53:23 +0900 | [diff] [blame] | 213 | ret = drm_gem_create_mmap_offset(obj); |
| 214 | if (ret < 0) { |
| 215 | drm_gem_object_release(obj); |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 216 | kfree(exynos_gem); |
Joonyoung Shim | 48cf53f | 2015-07-28 17:53:23 +0900 | [diff] [blame] | 217 | return ERR_PTR(ret); |
| 218 | } |
| 219 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 220 | DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp); |
| 221 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 222 | return exynos_gem; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 223 | } |
| 224 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 225 | struct exynos_drm_gem *exynos_drm_gem_create(struct drm_device *dev, |
| 226 | unsigned int flags, |
| 227 | unsigned long size) |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 228 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 229 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 230 | int ret; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 231 | |
Joonyoung Shim | c4130bc | 2015-08-16 14:15:06 +0900 | [diff] [blame] | 232 | if (flags & ~(EXYNOS_BO_MASK)) { |
| 233 | DRM_ERROR("invalid flags.\n"); |
| 234 | return ERR_PTR(-EINVAL); |
| 235 | } |
| 236 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 237 | if (!size) { |
| 238 | DRM_ERROR("invalid size.\n"); |
| 239 | return ERR_PTR(-EINVAL); |
| 240 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 241 | |
Joonyoung Shim | eb57da8 | 2015-07-28 17:53:27 +0900 | [diff] [blame] | 242 | size = roundup(size, PAGE_SIZE); |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 243 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 244 | exynos_gem = exynos_drm_gem_init(dev, size); |
| 245 | if (IS_ERR(exynos_gem)) |
| 246 | return exynos_gem; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 247 | |
| 248 | /* set memory type and cache attribute from user side. */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 249 | exynos_gem->flags = flags; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 250 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 251 | ret = exynos_drm_alloc_buf(exynos_gem); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 252 | if (ret < 0) { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 253 | drm_gem_object_release(&exynos_gem->base); |
| 254 | kfree(exynos_gem); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 255 | return ERR_PTR(ret); |
| 256 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 257 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 258 | return exynos_gem; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 261 | int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 262 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 263 | { |
| 264 | struct drm_exynos_gem_create *args = data; |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 265 | struct exynos_drm_gem *exynos_gem; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 266 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 267 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 268 | exynos_gem = exynos_drm_gem_create(dev, args->flags, args->size); |
| 269 | if (IS_ERR(exynos_gem)) |
| 270 | return PTR_ERR(exynos_gem); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 271 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 272 | ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv, |
| 273 | &args->handle); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 274 | if (ret) { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 275 | exynos_drm_gem_destroy(exynos_gem); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 276 | return ret; |
| 277 | } |
| 278 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 282 | dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev, |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 283 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 284 | struct drm_file *filp) |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 285 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 286 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 287 | struct drm_gem_object *obj; |
| 288 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 289 | obj = drm_gem_object_lookup(dev, filp, gem_handle); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 290 | if (!obj) { |
| 291 | DRM_ERROR("failed to lookup gem object.\n"); |
| 292 | return ERR_PTR(-EINVAL); |
| 293 | } |
| 294 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 295 | exynos_gem = to_exynos_gem(obj); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 296 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 297 | return &exynos_gem->dma_addr; |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void exynos_drm_gem_put_dma_addr(struct drm_device *dev, |
| 301 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 302 | struct drm_file *filp) |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 303 | { |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 304 | struct drm_gem_object *obj; |
| 305 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 306 | obj = drm_gem_object_lookup(dev, filp, gem_handle); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 307 | if (!obj) { |
| 308 | DRM_ERROR("failed to lookup gem object.\n"); |
| 309 | return; |
| 310 | } |
| 311 | |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 312 | drm_gem_object_unreference_unlocked(obj); |
| 313 | |
| 314 | /* |
| 315 | * decrease obj->refcount one more time because we has already |
| 316 | * increased it at exynos_drm_gem_get_dma_addr(). |
| 317 | */ |
| 318 | drm_gem_object_unreference_unlocked(obj); |
| 319 | } |
| 320 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 321 | static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem *exynos_gem, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 322 | struct vm_area_struct *vma) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 323 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 324 | struct drm_device *drm_dev = exynos_gem->base.dev; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 325 | unsigned long vm_size; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 326 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 327 | |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 328 | vma->vm_flags &= ~VM_PFNMAP; |
| 329 | vma->vm_pgoff = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 330 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 331 | vm_size = vma->vm_end - vma->vm_start; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 332 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 333 | /* check if user-requested size is valid. */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 334 | if (vm_size > exynos_gem->size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 337 | ret = dma_mmap_attrs(drm_dev->dev, vma, exynos_gem->pages, |
| 338 | exynos_gem->dma_addr, exynos_gem->size, |
| 339 | &exynos_gem->dma_attrs); |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 340 | if (ret < 0) { |
| 341 | DRM_ERROR("failed to mmap.\n"); |
| 342 | return ret; |
| 343 | } |
| 344 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 348 | int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data, |
| 349 | struct drm_file *file_priv) |
Joonyoung Shim | b4cfd4d | 2015-09-16 14:29:35 +0900 | [diff] [blame] | 350 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 351 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 352 | struct drm_exynos_gem_info *args = data; |
| 353 | struct drm_gem_object *obj; |
| 354 | |
| 355 | mutex_lock(&dev->struct_mutex); |
| 356 | |
| 357 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 358 | if (!obj) { |
| 359 | DRM_ERROR("failed to lookup gem object.\n"); |
| 360 | mutex_unlock(&dev->struct_mutex); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 364 | exynos_gem = to_exynos_gem(obj); |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 365 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 366 | args->flags = exynos_gem->flags; |
| 367 | args->size = exynos_gem->size; |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 368 | |
| 369 | drm_gem_object_unreference(obj); |
| 370 | mutex_unlock(&dev->struct_mutex); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 375 | int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev, |
| 376 | struct sg_table *sgt, |
| 377 | enum dma_data_direction dir) |
| 378 | { |
| 379 | int nents; |
| 380 | |
| 381 | mutex_lock(&drm_dev->struct_mutex); |
| 382 | |
| 383 | nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir); |
| 384 | if (!nents) { |
| 385 | DRM_ERROR("failed to map sgl with dma.\n"); |
| 386 | mutex_unlock(&drm_dev->struct_mutex); |
| 387 | return nents; |
| 388 | } |
| 389 | |
| 390 | mutex_unlock(&drm_dev->struct_mutex); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev, |
| 395 | struct sg_table *sgt, |
| 396 | enum dma_data_direction dir) |
| 397 | { |
| 398 | dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir); |
| 399 | } |
| 400 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 401 | void exynos_drm_gem_free_object(struct drm_gem_object *obj) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 402 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 403 | exynos_drm_gem_destroy(to_exynos_gem(obj)); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int exynos_drm_gem_dumb_create(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 407 | struct drm_device *dev, |
| 408 | struct drm_mode_create_dumb *args) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 409 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 410 | struct exynos_drm_gem *exynos_gem; |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame] | 411 | unsigned int flags; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 412 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 413 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 414 | /* |
Masanari Iida | c6b78bc | 2013-10-24 16:02:57 +0900 | [diff] [blame] | 415 | * allocate memory to be used for framebuffer. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 416 | * - this callback would be called by user application |
| 417 | * with DRM_IOCTL_MODE_CREATE_DUMB command. |
| 418 | */ |
| 419 | |
Cooper Yuan | 3fd6b69 | 2012-06-29 11:49:45 +0900 | [diff] [blame] | 420 | args->pitch = args->width * ((args->bpp + 7) / 8); |
Inki Dae | 7da5907 | 2012-08-17 15:24:03 +0900 | [diff] [blame] | 421 | args->size = args->pitch * args->height; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 422 | |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame] | 423 | if (is_drm_iommu_supported(dev)) |
| 424 | flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC; |
| 425 | else |
| 426 | flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC; |
Vikas Sajjan | 3fec453 | 2013-08-23 12:05:06 +0530 | [diff] [blame] | 427 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 428 | exynos_gem = exynos_drm_gem_create(dev, flags, args->size); |
| 429 | if (IS_ERR(exynos_gem)) { |
Rahul Sharma | 122beea | 2014-05-07 17:21:29 +0530 | [diff] [blame] | 430 | dev_warn(dev->dev, "FB allocation failed.\n"); |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 431 | return PTR_ERR(exynos_gem); |
Rahul Sharma | 122beea | 2014-05-07 17:21:29 +0530 | [diff] [blame] | 432 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 433 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 434 | ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv, |
| 435 | &args->handle); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 436 | if (ret) { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 437 | exynos_drm_gem_destroy(exynos_gem); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 438 | return ret; |
| 439 | } |
| 440 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 445 | struct drm_device *dev, uint32_t handle, |
| 446 | uint64_t *offset) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 447 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 448 | struct drm_gem_object *obj; |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 449 | int ret = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 450 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 451 | mutex_lock(&dev->struct_mutex); |
| 452 | |
| 453 | /* |
| 454 | * get offset of memory allocated for drm framebuffer. |
| 455 | * - this callback would be called by user application |
| 456 | * with DRM_IOCTL_MODE_MAP_DUMB command. |
| 457 | */ |
| 458 | |
| 459 | obj = drm_gem_object_lookup(dev, file_priv, handle); |
| 460 | if (!obj) { |
| 461 | DRM_ERROR("failed to lookup gem object.\n"); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 462 | ret = -EINVAL; |
| 463 | goto unlock; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 464 | } |
| 465 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 466 | *offset = drm_vma_node_offset_addr(&obj->vma_node); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 467 | DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset); |
| 468 | |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 469 | drm_gem_object_unreference(obj); |
| 470 | unlock: |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 471 | mutex_unlock(&dev->struct_mutex); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 472 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 476 | { |
| 477 | struct drm_gem_object *obj = vma->vm_private_data; |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 478 | struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj); |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 479 | unsigned long pfn; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 480 | pgoff_t page_offset; |
| 481 | int ret; |
| 482 | |
| 483 | page_offset = ((unsigned long)vmf->virtual_address - |
| 484 | vma->vm_start) >> PAGE_SHIFT; |
| 485 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 486 | if (page_offset >= (exynos_gem->size >> PAGE_SHIFT)) { |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 487 | DRM_ERROR("invalid page offset\n"); |
| 488 | ret = -EINVAL; |
| 489 | goto out; |
| 490 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 491 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 492 | pfn = page_to_pfn(exynos_gem->pages[page_offset]); |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 493 | ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn); |
| 494 | |
| 495 | out: |
Joonyoung Shim | 23597e2 | 2015-07-28 17:53:17 +0900 | [diff] [blame] | 496 | switch (ret) { |
| 497 | case 0: |
| 498 | case -ERESTARTSYS: |
| 499 | case -EINTR: |
| 500 | return VM_FAULT_NOPAGE; |
| 501 | case -ENOMEM: |
| 502 | return VM_FAULT_OOM; |
| 503 | default: |
| 504 | return VM_FAULT_SIGBUS; |
| 505 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 509 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 510 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 511 | struct drm_gem_object *obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 512 | int ret; |
| 513 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 514 | /* set vm_area_struct. */ |
| 515 | ret = drm_gem_mmap(filp, vma); |
| 516 | if (ret < 0) { |
| 517 | DRM_ERROR("failed to mmap.\n"); |
| 518 | return ret; |
| 519 | } |
| 520 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 521 | obj = vma->vm_private_data; |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 522 | exynos_gem = to_exynos_gem(obj); |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 523 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 524 | DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem->flags); |
Joonyoung Shim | 211b887 | 2015-08-16 14:16:49 +0900 | [diff] [blame] | 525 | |
| 526 | /* non-cachable as default. */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 527 | if (exynos_gem->flags & EXYNOS_BO_CACHABLE) |
Joonyoung Shim | 211b887 | 2015-08-16 14:16:49 +0900 | [diff] [blame] | 528 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 529 | else if (exynos_gem->flags & EXYNOS_BO_WC) |
Joonyoung Shim | 211b887 | 2015-08-16 14:16:49 +0900 | [diff] [blame] | 530 | vma->vm_page_prot = |
| 531 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 532 | else |
| 533 | vma->vm_page_prot = |
| 534 | pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 535 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 536 | ret = exynos_drm_gem_mmap_buffer(exynos_gem, vma); |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 537 | if (ret) |
| 538 | goto err_close_vm; |
| 539 | |
| 540 | return ret; |
| 541 | |
| 542 | err_close_vm: |
| 543 | drm_gem_vm_close(vma); |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 544 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 545 | return ret; |
| 546 | } |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 547 | |
| 548 | /* low-level interface prime helpers */ |
| 549 | struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj) |
| 550 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 551 | struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 552 | int npages; |
| 553 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 554 | npages = exynos_gem->size >> PAGE_SHIFT; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 555 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 556 | return drm_prime_pages_to_sg(exynos_gem->pages, npages); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | struct drm_gem_object * |
| 560 | exynos_drm_gem_prime_import_sg_table(struct drm_device *dev, |
| 561 | struct dma_buf_attachment *attach, |
| 562 | struct sg_table *sgt) |
| 563 | { |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 564 | struct exynos_drm_gem *exynos_gem; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 565 | int npages; |
| 566 | int ret; |
| 567 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 568 | exynos_gem = exynos_drm_gem_init(dev, attach->dmabuf->size); |
| 569 | if (IS_ERR(exynos_gem)) { |
| 570 | ret = PTR_ERR(exynos_gem); |
Inki Dae | 50002d4 | 2015-08-31 01:11:53 +0900 | [diff] [blame] | 571 | return ERR_PTR(ret); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 572 | } |
| 573 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 574 | exynos_gem->dma_addr = sg_dma_address(sgt->sgl); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 575 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 576 | npages = exynos_gem->size >> PAGE_SHIFT; |
| 577 | exynos_gem->pages = drm_malloc_ab(npages, sizeof(struct page *)); |
| 578 | if (!exynos_gem->pages) { |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 579 | ret = -ENOMEM; |
| 580 | goto err; |
| 581 | } |
| 582 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 583 | ret = drm_prime_sg_to_page_addr_arrays(sgt, exynos_gem->pages, NULL, |
| 584 | npages); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 585 | if (ret < 0) |
| 586 | goto err_free_large; |
| 587 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 588 | exynos_gem->sgt = sgt; |
Joonyoung Shim | 7c93537 | 2015-09-16 14:14:54 +0900 | [diff] [blame] | 589 | |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 590 | if (sgt->nents == 1) { |
| 591 | /* always physically continuous memory if sgt->nents is 1. */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 592 | exynos_gem->flags |= EXYNOS_BO_CONTIG; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 593 | } else { |
| 594 | /* |
| 595 | * this case could be CONTIG or NONCONTIG type but for now |
| 596 | * sets NONCONTIG. |
| 597 | * TODO. we have to find a way that exporter can notify |
| 598 | * the type of its own buffer to importer. |
| 599 | */ |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 600 | exynos_gem->flags |= EXYNOS_BO_NONCONTIG; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 601 | } |
| 602 | |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 603 | return &exynos_gem->base; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 604 | |
| 605 | err_free_large: |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 606 | drm_free_large(exynos_gem->pages); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 607 | err: |
Joonyoung Shim | 813fd67 | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 608 | drm_gem_object_release(&exynos_gem->base); |
| 609 | kfree(exynos_gem); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 610 | return ERR_PTR(ret); |
| 611 | } |
| 612 | |
| 613 | void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj) |
| 614 | { |
| 615 | return NULL; |
| 616 | } |
| 617 | |
| 618 | void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 619 | { |
| 620 | /* Nothing to do */ |
| 621 | } |