1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* include/media/videobuf2-ion.h
*
* Copyright 2011-2012 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*
* Definition of Android ION memory allocator for videobuf2
*
* 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.
*/
#ifndef _MEDIA_VIDEOBUF2_ION_H
#define _MEDIA_VIDEOBUF2_ION_H
#include <linux/scatterlist.h>
#include <linux/dma-mapping.h>
#include <linux/ion.h>
#include <linux/exynos_ion.h>
#include <linux/err.h>
#include <linux/dma-buf.h>
/* flags to vb2_ion_create_context
* These flags are dependet upon heap flags in ION.
*
* bit 0 ~ ION_NUM_HEAPS: ion heap flags
* bit ION_NUM_HEAPS+1 ~ 20: non-ion flags (cached, iommu)
* bit 21 ~ BITS_PER_INT - 1: ion specific flags
*/
/* DMA of the client device is coherent with CPU */
#define VB2ION_CTX_COHERENT_DMA (1 << (ION_NUM_HEAPS + 3))
/* DMA should read from memory instead of CPU cache even if DMA is coherent */
#define VB2ION_CTX_UNCACHED_READ_DMA (1 << (ION_NUM_HEAPS + 4))
#define VB2ION_CONTIG_ID_NUM 16
#define VB2ION_NUM_HEAPS 8
/* below 6 is the above vb2-ion flags (ION_NUM_HEAPS + 1 ~ 6) */
#if (BITS_PER_INT <= (VB2ION_CONTIG_ID_NUM + VB2ION_NUM_HEAPS + 6))
#error "Bits are too small to express all flags"
#endif
struct device;
struct vb2_buffer;
/* - vb2_ion_set_noncoherent_dma_read(): Forces the DMA to read from system
* memory even though it is capable of snooping CPU caches.
*/
void vb2_ion_set_noncoherent_dma_read(struct device *dev, bool noncoherent);
/* Data type of the cookie returned by vb2_plane_cookie() function call.
* The drivers do not need the definition of this structure. The only reason
* why it is defined outside of videobuf2-ion.c is to make some functions
* inline.
*/
struct vb2_ion_cookie {
dma_addr_t ioaddr;
dma_addr_t paddr;
struct sg_table *sgt;
off_t offset;
};
/* vb2_ion_buffer_offset - return the mapped offset of the buffer
* - cookie: pointer returned by vb2_plane_cookie()
*
* Returns offset value that the mapping starts from.
*/
static inline off_t vb2_ion_buffer_offset(void *cookie)
{
return IS_ERR_OR_NULL(cookie) ?
-EINVAL : ((struct vb2_ion_cookie *)cookie)->offset;
}
/* vb2_ion_phys_address - returns the physical address of the given buffer
* - cookie: pointer returned by vb2_plane_cookie()
* - phys_addr: pointer to the store of the physical address of the buffer
* specified by cookie.
*
* Returns -EINVAL if the buffer does not have nor physically contiguous memory.
*/
static inline int vb2_ion_phys_address(void *cookie, phys_addr_t *phys_addr)
{
struct vb2_ion_cookie *vb2cookie = cookie;
if (WARN_ON(!phys_addr || IS_ERR_OR_NULL(cookie)))
return -EINVAL;
if (vb2cookie->paddr) {
*phys_addr = vb2cookie->paddr;
} else {
if (vb2cookie->sgt && vb2cookie->sgt->nents == 1) {
*phys_addr = sg_phys(vb2cookie->sgt->sgl) +
vb2cookie->offset;
} else {
*phys_addr = 0;
return -EINVAL;
}
}
return 0;
}
/* vb2_ion_dma_address - returns the DMA address that device can see
* - cookie: pointer returned by vb2_plane_cookie()
* - dma_addr: pointer to the store of the address of the buffer specified
* by cookie. It can be either IO virtual address or physical address
* depending on the specification of allocation context which allocated
* the buffer.
*
* Returns -EINVAL if the buffer has neither IO virtual address nor physically
* contiguous memory
*/
static inline int vb2_ion_dma_address(void *cookie, dma_addr_t *dma_addr)
{
struct vb2_ion_cookie *vb2cookie = cookie;
if (WARN_ON(!dma_addr || IS_ERR_OR_NULL(cookie)))
return -EINVAL;
if (vb2cookie->ioaddr == 0)
return vb2_ion_phys_address(cookie, (phys_addr_t *)dma_addr);
*dma_addr = vb2cookie->ioaddr;
return 0;
}
/* vb2_ion_get_sg - returns scatterlist of the given cookie.
* - cookie: pointer returned by vb2_plane_cookie()
* - nents: pointer to the store of number of elements in the returned
* scatterlist
*
* Returns the scatterlist of the buffer specified by cookie.
* If the arguments are not correct, returns NULL.
*/
static inline struct scatterlist *vb2_ion_get_sg(void *cookie, int *nents)
{
struct vb2_ion_cookie *vb2cookie = cookie;
if (WARN_ON(!nents || IS_ERR_OR_NULL(cookie)))
return NULL;
*nents = vb2cookie->sgt->nents;
return vb2cookie->sgt->sgl;
}
/* vb2_ion_get_dmabuf - returns dmabuf of the given cookie
* - cookie: pointer returned by vb2_plane_cookie()
*
* Returns dma-buf descriptor with a reference held.
* NULL if the cookie is invalid or the buffer is not dmabuf.
* The caller must put the dmabuf when it is no longer need the dmabuf
*/
struct dma_buf *vb2_ion_get_dmabuf(void *cookie);
/***** Cache mainatenance operations *****/
void vb2_ion_sync_for_device(void *cookie, off_t offset, size_t size,
enum dma_data_direction dir);
void vb2_ion_sync_for_cpu(void *cookie, off_t offset, size_t size,
enum dma_data_direction dir);
int vb2_ion_buf_prepare(struct vb2_buffer *vb);
void vb2_ion_buf_finish(struct vb2_buffer *vb);
int vb2_ion_buf_prepare_exact(struct vb2_buffer *vb);
int vb2_ion_buf_finish_exact(struct vb2_buffer *vb);
extern const struct vb2_mem_ops vb2_ion_memops;
extern struct ion_device *ion_exynos; /* drivers/gpu/ion/exynos/exynos-ion.c */
#endif /* _MEDIA_VIDEOBUF2_ION_H */
|