1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
4 * Author: Rob Clark <rob.clark@linaro.org>
7 #include <linux/dma-buf.h>
8 #include <linux/highmem.h>
10 #include <drm/drm_prime.h>
14 /* -----------------------------------------------------------------------------
18 static struct sg_table
*omap_gem_map_dma_buf(
19 struct dma_buf_attachment
*attachment
,
20 enum dma_data_direction dir
)
22 struct drm_gem_object
*obj
= attachment
->dmabuf
->priv
;
27 sg
= kzalloc(sizeof(*sg
), GFP_KERNEL
);
29 return ERR_PTR(-ENOMEM
);
31 /* camera, etc, need physically contiguous.. but we need a
32 * better way to know this..
34 ret
= omap_gem_pin(obj
, &dma_addr
);
38 ret
= sg_alloc_table(sg
, 1, GFP_KERNEL
);
42 sg_init_table(sg
->sgl
, 1);
43 sg_dma_len(sg
->sgl
) = obj
->size
;
44 sg_set_page(sg
->sgl
, pfn_to_page(PFN_DOWN(dma_addr
)), obj
->size
, 0);
45 sg_dma_address(sg
->sgl
) = dma_addr
;
47 /* this must be after omap_gem_pin() to ensure we have pages attached */
48 omap_gem_dma_sync_buffer(obj
, dir
);
56 static void omap_gem_unmap_dma_buf(struct dma_buf_attachment
*attachment
,
57 struct sg_table
*sg
, enum dma_data_direction dir
)
59 struct drm_gem_object
*obj
= attachment
->dmabuf
->priv
;
65 static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf
*buffer
,
66 enum dma_data_direction dir
)
68 struct drm_gem_object
*obj
= buffer
->priv
;
70 if (omap_gem_flags(obj
) & OMAP_BO_TILED_MASK
) {
71 /* TODO we would need to pin at least part of the buffer to
72 * get de-tiled view. For now just reject it.
76 /* make sure we have the pages: */
77 return omap_gem_get_pages(obj
, &pages
, true);
80 static int omap_gem_dmabuf_end_cpu_access(struct dma_buf
*buffer
,
81 enum dma_data_direction dir
)
83 struct drm_gem_object
*obj
= buffer
->priv
;
84 omap_gem_put_pages(obj
);
88 static int omap_gem_dmabuf_mmap(struct dma_buf
*buffer
,
89 struct vm_area_struct
*vma
)
91 struct drm_gem_object
*obj
= buffer
->priv
;
94 ret
= drm_gem_mmap_obj(obj
, omap_gem_mmap_size(obj
), vma
);
98 return omap_gem_mmap_obj(obj
, vma
);
101 static const struct dma_buf_ops omap_dmabuf_ops
= {
102 .map_dma_buf
= omap_gem_map_dma_buf
,
103 .unmap_dma_buf
= omap_gem_unmap_dma_buf
,
104 .release
= drm_gem_dmabuf_release
,
105 .begin_cpu_access
= omap_gem_dmabuf_begin_cpu_access
,
106 .end_cpu_access
= omap_gem_dmabuf_end_cpu_access
,
107 .mmap
= omap_gem_dmabuf_mmap
,
110 struct dma_buf
*omap_gem_prime_export(struct drm_gem_object
*obj
, int flags
)
112 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
114 exp_info
.ops
= &omap_dmabuf_ops
;
115 exp_info
.size
= obj
->size
;
116 exp_info
.flags
= flags
;
119 return drm_gem_dmabuf_export(obj
->dev
, &exp_info
);
122 /* -----------------------------------------------------------------------------
126 struct drm_gem_object
*omap_gem_prime_import(struct drm_device
*dev
,
127 struct dma_buf
*dma_buf
)
129 struct dma_buf_attachment
*attach
;
130 struct drm_gem_object
*obj
;
131 struct sg_table
*sgt
;
134 if (dma_buf
->ops
== &omap_dmabuf_ops
) {
136 if (obj
->dev
== dev
) {
138 * Importing dmabuf exported from out own gem increases
139 * refcount on gem itself instead of f_count of dmabuf.
141 drm_gem_object_get(obj
);
146 attach
= dma_buf_attach(dma_buf
, dev
->dev
);
148 return ERR_CAST(attach
);
150 get_dma_buf(dma_buf
);
152 sgt
= dma_buf_map_attachment(attach
, DMA_TO_DEVICE
);
158 obj
= omap_gem_new_dmabuf(dev
, dma_buf
->size
, sgt
);
164 obj
->import_attach
= attach
;
169 dma_buf_unmap_attachment(attach
, sgt
, DMA_TO_DEVICE
);
171 dma_buf_detach(dma_buf
, attach
);
172 dma_buf_put(dma_buf
);