Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 1 | /* |
Rob Clark | 8bb0daf | 2013-02-11 12:43:09 -0500 | [diff] [blame] | 2 | * drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2011 Texas Instruments |
| 5 | * Author: Rob Clark <rob.clark@linaro.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published by |
| 9 | * the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 20 | #include <linux/dma-buf.h> |
| 21 | |
Laurent Pinchart | 2d278f5 | 2015-03-05 21:31:37 +0200 | [diff] [blame] | 22 | #include "omap_drv.h" |
| 23 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 24 | /* ----------------------------------------------------------------------------- |
| 25 | * DMABUF Export |
| 26 | */ |
| 27 | |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 28 | static struct sg_table *omap_gem_map_dma_buf( |
| 29 | struct dma_buf_attachment *attachment, |
| 30 | enum dma_data_direction dir) |
| 31 | { |
| 32 | struct drm_gem_object *obj = attachment->dmabuf->priv; |
| 33 | struct sg_table *sg; |
Laurent Pinchart | 1686908 | 2017-04-21 00:33:51 +0300 | [diff] [blame] | 34 | dma_addr_t dma_addr; |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 35 | int ret; |
| 36 | |
| 37 | sg = kzalloc(sizeof(*sg), GFP_KERNEL); |
| 38 | if (!sg) |
| 39 | return ERR_PTR(-ENOMEM); |
| 40 | |
| 41 | /* camera, etc, need physically contiguous.. but we need a |
| 42 | * better way to know this.. |
| 43 | */ |
Laurent Pinchart | bc20c85 | 2017-04-21 00:33:52 +0300 | [diff] [blame] | 44 | ret = omap_gem_pin(obj, &dma_addr); |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 45 | if (ret) |
| 46 | goto out; |
| 47 | |
| 48 | ret = sg_alloc_table(sg, 1, GFP_KERNEL); |
| 49 | if (ret) |
| 50 | goto out; |
| 51 | |
| 52 | sg_init_table(sg->sgl, 1); |
| 53 | sg_dma_len(sg->sgl) = obj->size; |
Laurent Pinchart | 1686908 | 2017-04-21 00:33:51 +0300 | [diff] [blame] | 54 | sg_set_page(sg->sgl, pfn_to_page(PFN_DOWN(dma_addr)), obj->size, 0); |
| 55 | sg_dma_address(sg->sgl) = dma_addr; |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 56 | |
Laurent Pinchart | bc20c85 | 2017-04-21 00:33:52 +0300 | [diff] [blame] | 57 | /* this must be after omap_gem_pin() to ensure we have pages attached */ |
Laurent Pinchart | d61ce7d | 2017-04-21 00:33:55 +0300 | [diff] [blame] | 58 | omap_gem_dma_sync_buffer(obj, dir); |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 59 | |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 60 | return sg; |
Cong Ding | 32ac1a5 | 2013-01-15 20:46:50 +0100 | [diff] [blame] | 61 | out: |
| 62 | kfree(sg); |
| 63 | return ERR_PTR(ret); |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, |
| 67 | struct sg_table *sg, enum dma_data_direction dir) |
| 68 | { |
| 69 | struct drm_gem_object *obj = attachment->dmabuf->priv; |
Laurent Pinchart | bc20c85 | 2017-04-21 00:33:52 +0300 | [diff] [blame] | 70 | omap_gem_unpin(obj); |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 71 | sg_free_table(sg); |
| 72 | kfree(sg); |
| 73 | } |
| 74 | |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 75 | static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer, |
Tiago Vignatti | 831e9da | 2015-12-22 19:36:45 -0200 | [diff] [blame] | 76 | enum dma_data_direction dir) |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 77 | { |
| 78 | struct drm_gem_object *obj = buffer->priv; |
| 79 | struct page **pages; |
| 80 | if (omap_gem_flags(obj) & OMAP_BO_TILED) { |
| 81 | /* TODO we would need to pin at least part of the buffer to |
| 82 | * get de-tiled view. For now just reject it. |
| 83 | */ |
| 84 | return -ENOMEM; |
| 85 | } |
| 86 | /* make sure we have the pages: */ |
| 87 | return omap_gem_get_pages(obj, &pages, true); |
| 88 | } |
| 89 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 90 | static int omap_gem_dmabuf_end_cpu_access(struct dma_buf *buffer, |
| 91 | enum dma_data_direction dir) |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 92 | { |
| 93 | struct drm_gem_object *obj = buffer->priv; |
| 94 | omap_gem_put_pages(obj); |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 95 | return 0; |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | static void *omap_gem_dmabuf_kmap_atomic(struct dma_buf *buffer, |
| 100 | unsigned long page_num) |
| 101 | { |
| 102 | struct drm_gem_object *obj = buffer->priv; |
| 103 | struct page **pages; |
| 104 | omap_gem_get_pages(obj, &pages, false); |
Laurent Pinchart | d61ce7d | 2017-04-21 00:33:55 +0300 | [diff] [blame] | 105 | omap_gem_cpu_sync_page(obj, page_num); |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 106 | return kmap_atomic(pages[page_num]); |
| 107 | } |
| 108 | |
| 109 | static void omap_gem_dmabuf_kunmap_atomic(struct dma_buf *buffer, |
| 110 | unsigned long page_num, void *addr) |
| 111 | { |
| 112 | kunmap_atomic(addr); |
| 113 | } |
| 114 | |
| 115 | static void *omap_gem_dmabuf_kmap(struct dma_buf *buffer, |
| 116 | unsigned long page_num) |
| 117 | { |
| 118 | struct drm_gem_object *obj = buffer->priv; |
| 119 | struct page **pages; |
| 120 | omap_gem_get_pages(obj, &pages, false); |
Laurent Pinchart | d61ce7d | 2017-04-21 00:33:55 +0300 | [diff] [blame] | 121 | omap_gem_cpu_sync_page(obj, page_num); |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 122 | return kmap(pages[page_num]); |
| 123 | } |
| 124 | |
| 125 | static void omap_gem_dmabuf_kunmap(struct dma_buf *buffer, |
| 126 | unsigned long page_num, void *addr) |
| 127 | { |
| 128 | struct drm_gem_object *obj = buffer->priv; |
| 129 | struct page **pages; |
| 130 | omap_gem_get_pages(obj, &pages, false); |
| 131 | kunmap(pages[page_num]); |
| 132 | } |
| 133 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 134 | static int omap_gem_dmabuf_mmap(struct dma_buf *buffer, |
| 135 | struct vm_area_struct *vma) |
| 136 | { |
| 137 | struct drm_gem_object *obj = buffer->priv; |
| 138 | int ret = 0; |
| 139 | |
Laurent Pinchart | bda3fda | 2013-04-16 14:21:23 +0200 | [diff] [blame] | 140 | ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma); |
| 141 | if (ret < 0) |
| 142 | return ret; |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 143 | |
| 144 | return omap_gem_mmap_obj(obj, vma); |
| 145 | } |
| 146 | |
Arvind Yadav | 815ed41 | 2017-07-02 11:49:40 +0530 | [diff] [blame] | 147 | static const struct dma_buf_ops omap_dmabuf_ops = { |
Laurent Pinchart | 222025e | 2015-01-11 00:02:07 +0200 | [diff] [blame] | 148 | .map_dma_buf = omap_gem_map_dma_buf, |
| 149 | .unmap_dma_buf = omap_gem_unmap_dma_buf, |
Laurent Pinchart | c488dd2 | 2017-05-08 21:59:24 +0300 | [diff] [blame] | 150 | .release = drm_gem_dmabuf_release, |
Laurent Pinchart | 222025e | 2015-01-11 00:02:07 +0200 | [diff] [blame] | 151 | .begin_cpu_access = omap_gem_dmabuf_begin_cpu_access, |
| 152 | .end_cpu_access = omap_gem_dmabuf_end_cpu_access, |
Logan Gunthorpe | f9b67f0 | 2017-04-19 13:36:10 -0600 | [diff] [blame] | 153 | .map_atomic = omap_gem_dmabuf_kmap_atomic, |
| 154 | .unmap_atomic = omap_gem_dmabuf_kunmap_atomic, |
| 155 | .map = omap_gem_dmabuf_kmap, |
| 156 | .unmap = omap_gem_dmabuf_kunmap, |
Laurent Pinchart | 222025e | 2015-01-11 00:02:07 +0200 | [diff] [blame] | 157 | .mmap = omap_gem_dmabuf_mmap, |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 158 | }; |
| 159 | |
YAMANE Toshiaki | 11d3d27 | 2012-11-14 19:40:14 +0900 | [diff] [blame] | 160 | struct dma_buf *omap_gem_prime_export(struct drm_device *dev, |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 161 | struct drm_gem_object *obj, int flags) |
| 162 | { |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 163 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 164 | |
| 165 | exp_info.ops = &omap_dmabuf_ops; |
| 166 | exp_info.size = obj->size; |
| 167 | exp_info.flags = flags; |
| 168 | exp_info.priv = obj; |
| 169 | |
Laurent Pinchart | c488dd2 | 2017-05-08 21:59:24 +0300 | [diff] [blame] | 170 | return drm_gem_dmabuf_export(dev, &exp_info); |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 171 | } |
Rob Clark | 3080b83 | 2012-05-17 02:37:26 -0600 | [diff] [blame] | 172 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 173 | /* ----------------------------------------------------------------------------- |
| 174 | * DMABUF Import |
| 175 | */ |
Rob Clark | 3080b83 | 2012-05-17 02:37:26 -0600 | [diff] [blame] | 176 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 177 | struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev, |
| 178 | struct dma_buf *dma_buf) |
| 179 | { |
| 180 | struct dma_buf_attachment *attach; |
| 181 | struct drm_gem_object *obj; |
| 182 | struct sg_table *sgt; |
| 183 | int ret; |
| 184 | |
| 185 | if (dma_buf->ops == &omap_dmabuf_ops) { |
| 186 | obj = dma_buf->priv; |
Rob Clark | 3080b83 | 2012-05-17 02:37:26 -0600 | [diff] [blame] | 187 | if (obj->dev == dev) { |
Seung-Woo Kim | be8a42a | 2012-09-27 15:30:06 +0900 | [diff] [blame] | 188 | /* |
| 189 | * Importing dmabuf exported from out own gem increases |
| 190 | * refcount on gem itself instead of f_count of dmabuf. |
| 191 | */ |
Rob Clark | 3080b83 | 2012-05-17 02:37:26 -0600 | [diff] [blame] | 192 | drm_gem_object_reference(obj); |
| 193 | return obj; |
| 194 | } |
| 195 | } |
| 196 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 197 | attach = dma_buf_attach(dma_buf, dev->dev); |
| 198 | if (IS_ERR(attach)) |
| 199 | return ERR_CAST(attach); |
| 200 | |
| 201 | get_dma_buf(dma_buf); |
| 202 | |
Laurent Pinchart | 97817fd | 2017-04-21 00:33:58 +0300 | [diff] [blame] | 203 | sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE); |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 204 | if (IS_ERR(sgt)) { |
| 205 | ret = PTR_ERR(sgt); |
| 206 | goto fail_detach; |
| 207 | } |
| 208 | |
| 209 | obj = omap_gem_new_dmabuf(dev, dma_buf->size, sgt); |
| 210 | if (IS_ERR(obj)) { |
| 211 | ret = PTR_ERR(obj); |
| 212 | goto fail_unmap; |
| 213 | } |
| 214 | |
| 215 | obj->import_attach = attach; |
| 216 | |
| 217 | return obj; |
| 218 | |
| 219 | fail_unmap: |
Laurent Pinchart | 97817fd | 2017-04-21 00:33:58 +0300 | [diff] [blame] | 220 | dma_buf_unmap_attachment(attach, sgt, DMA_TO_DEVICE); |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 221 | fail_detach: |
| 222 | dma_buf_detach(dma_buf, attach); |
| 223 | dma_buf_put(dma_buf); |
| 224 | |
| 225 | return ERR_PTR(ret); |
Rob Clark | 3080b83 | 2012-05-17 02:37:26 -0600 | [diff] [blame] | 226 | } |