omap_gem_dmabuf.c 5.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Author: Rob Clark <rob.clark@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/dma-buf.h>

20 21
#include "omap_drv.h"

22 23 24 25
/* -----------------------------------------------------------------------------
 * DMABUF Export
 */

26 27 28 29 30 31
static struct sg_table *omap_gem_map_dma_buf(
		struct dma_buf_attachment *attachment,
		enum dma_data_direction dir)
{
	struct drm_gem_object *obj = attachment->dmabuf->priv;
	struct sg_table *sg;
32
	dma_addr_t dma_addr;
33 34 35 36 37 38 39 40 41
	int ret;

	sg = kzalloc(sizeof(*sg), GFP_KERNEL);
	if (!sg)
		return ERR_PTR(-ENOMEM);

	/* camera, etc, need physically contiguous.. but we need a
	 * better way to know this..
	 */
42
	ret = omap_gem_pin(obj, &dma_addr);
43 44 45 46 47 48 49 50 51
	if (ret)
		goto out;

	ret = sg_alloc_table(sg, 1, GFP_KERNEL);
	if (ret)
		goto out;

	sg_init_table(sg->sgl, 1);
	sg_dma_len(sg->sgl) = obj->size;
52 53
	sg_set_page(sg->sgl, pfn_to_page(PFN_DOWN(dma_addr)), obj->size, 0);
	sg_dma_address(sg->sgl) = dma_addr;
54

55
	/* this must be after omap_gem_pin() to ensure we have pages attached */
56
	omap_gem_dma_sync_buffer(obj, dir);
57

58
	return sg;
59 60 61
out:
	kfree(sg);
	return ERR_PTR(ret);
62 63 64 65 66 67
}

static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
		struct sg_table *sg, enum dma_data_direction dir)
{
	struct drm_gem_object *obj = attachment->dmabuf->priv;
68
	omap_gem_unpin(obj);
69 70 71 72 73
	sg_free_table(sg);
	kfree(sg);
}

static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer,
74
		enum dma_data_direction dir)
75 76 77 78 79 80 81 82 83 84 85 86 87
{
	struct drm_gem_object *obj = buffer->priv;
	struct page **pages;
	if (omap_gem_flags(obj) & OMAP_BO_TILED) {
		/* TODO we would need to pin at least part of the buffer to
		 * get de-tiled view.  For now just reject it.
		 */
		return -ENOMEM;
	}
	/* make sure we have the pages: */
	return omap_gem_get_pages(obj, &pages, true);
}

88 89
static int omap_gem_dmabuf_end_cpu_access(struct dma_buf *buffer,
					  enum dma_data_direction dir)
90 91 92
{
	struct drm_gem_object *obj = buffer->priv;
	omap_gem_put_pages(obj);
93
	return 0;
94 95 96 97 98 99 100 101
}

static void *omap_gem_dmabuf_kmap(struct dma_buf *buffer,
		unsigned long page_num)
{
	struct drm_gem_object *obj = buffer->priv;
	struct page **pages;
	omap_gem_get_pages(obj, &pages, false);
102
	omap_gem_cpu_sync_page(obj, page_num);
103 104 105 106 107 108 109 110 111 112 113 114
	return kmap(pages[page_num]);
}

static void omap_gem_dmabuf_kunmap(struct dma_buf *buffer,
		unsigned long page_num, void *addr)
{
	struct drm_gem_object *obj = buffer->priv;
	struct page **pages;
	omap_gem_get_pages(obj, &pages, false);
	kunmap(pages[page_num]);
}

115 116 117 118 119 120
static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
		struct vm_area_struct *vma)
{
	struct drm_gem_object *obj = buffer->priv;
	int ret = 0;

121 122 123
	ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
	if (ret < 0)
		return ret;
124 125 126 127

	return omap_gem_mmap_obj(obj, vma);
}

128
static const struct dma_buf_ops omap_dmabuf_ops = {
129 130
	.map_dma_buf = omap_gem_map_dma_buf,
	.unmap_dma_buf = omap_gem_unmap_dma_buf,
131
	.release = drm_gem_dmabuf_release,
132 133
	.begin_cpu_access = omap_gem_dmabuf_begin_cpu_access,
	.end_cpu_access = omap_gem_dmabuf_end_cpu_access,
134 135
	.map = omap_gem_dmabuf_kmap,
	.unmap = omap_gem_dmabuf_kunmap,
136
	.mmap = omap_gem_dmabuf_mmap,
137 138
};

139
struct dma_buf *omap_gem_prime_export(struct drm_device *dev,
140 141
		struct drm_gem_object *obj, int flags)
{
142 143 144 145 146 147 148
	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);

	exp_info.ops = &omap_dmabuf_ops;
	exp_info.size = obj->size;
	exp_info.flags = flags;
	exp_info.priv = obj;

149
	return drm_gem_dmabuf_export(dev, &exp_info);
150
}
151

152 153 154 155
/* -----------------------------------------------------------------------------
 * DMABUF Import
 */

156
struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
157
					     struct dma_buf *dma_buf)
158
{
159
	struct dma_buf_attachment *attach;
160
	struct drm_gem_object *obj;
161 162
	struct sg_table *sgt;
	int ret;
163

164 165
	if (dma_buf->ops == &omap_dmabuf_ops) {
		obj = dma_buf->priv;
166
		if (obj->dev == dev) {
167 168 169 170
			/*
			 * Importing dmabuf exported from out own gem increases
			 * refcount on gem itself instead of f_count of dmabuf.
			 */
171 172 173 174 175
			drm_gem_object_reference(obj);
			return obj;
		}
	}

176 177 178 179 180 181
	attach = dma_buf_attach(dma_buf, dev->dev);
	if (IS_ERR(attach))
		return ERR_CAST(attach);

	get_dma_buf(dma_buf);

182
	sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	if (IS_ERR(sgt)) {
		ret = PTR_ERR(sgt);
		goto fail_detach;
	}

	obj = omap_gem_new_dmabuf(dev, dma_buf->size, sgt);
	if (IS_ERR(obj)) {
		ret = PTR_ERR(obj);
		goto fail_unmap;
	}

	obj->import_attach = attach;

	return obj;

fail_unmap:
199
	dma_buf_unmap_attachment(attach, sgt, DMA_TO_DEVICE);
200 201 202 203 204
fail_detach:
	dma_buf_detach(dma_buf, attach);
	dma_buf_put(dma_buf);

	return ERR_PTR(ret);
205
}