-
Notifications
You must be signed in to change notification settings - Fork 5
/
vcodec_iommu_dma.c
348 lines (275 loc) · 7.53 KB
/
vcodec_iommu_dma.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (C) 2016 - 2017 Fuzhou Rockchip Electronics Co., Ltd
* Randy Li, <ayaka@soulik.info>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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.
*
*/
#include <linux/dma-buf.h>
#include <linux/dma-iommu.h>
#include <linux/kref.h>
/* For kzalloc issues */
#include <linux/slab.h>
/* For iommu_*_device */
#include <linux/iommu.h>
#include "vcodec_iommu_dma.h"
/* pixel buffer, stream buffer and video codec buffer */
#define BUFFER_LIST_MAX_NUMS 26
struct vcodec_dma_buffer {
struct list_head list;
/* dma-buf */
struct dma_buf *dma_buf;
struct dma_buf_attachment *attach;
struct sg_table *sgt;
dma_addr_t iova;
unsigned long size;
/* Only be used for identifying the buffer */
int fd;
int index;
struct kref ref;
};
struct vcodec_iommu_info {
struct iommu_domain *domain;
bool attached;
struct device *dev;
};
struct vcodec_dma_session {
int buffer_nums;
struct list_head buffer_list;
/* Mutex for the above buffer list */
struct mutex list_mutex;
struct device *dev;
};
static struct vcodec_dma_buffer *
vcodec_dma_get_buffer(struct vcodec_dma_session *session, int fd)
{
struct vcodec_dma_buffer *buffer = NULL, *n;
mutex_lock(&session->list_mutex);
list_for_each_entry_safe(buffer, n, &session->buffer_list, list) {
/*
* As long as the last reference is hold by the buffer pool,
* the same fd won't be assigned to the other application.
*/
if (buffer->fd == fd) {
mutex_unlock(&session->list_mutex);
return buffer;
}
}
mutex_unlock(&session->list_mutex);
return NULL;
}
static void vcodec_dma_buffer_release(struct kref *ref)
{
struct vcodec_dma_buffer *buffer =
container_of(ref, struct vcodec_dma_buffer, ref);
dma_buf_unmap_attachment(buffer->attach, buffer->sgt,
DMA_BIDIRECTIONAL);
dma_buf_detach(buffer->dma_buf, buffer->attach);
dma_buf_put(buffer->dma_buf);
list_del(&buffer->list);
kfree(buffer);
}
int vcodec_dma_release_fd(struct vcodec_dma_session *session, int fd)
{
struct device *dev = session->dev;
struct vcodec_dma_buffer *buffer = NULL;
buffer = vcodec_dma_get_buffer(session, fd);
if (IS_ERR_OR_NULL(buffer)) {
dev_err(dev, "can not find %d buffer in list to release\n", fd);
return -EINVAL;
}
kref_put(&buffer->ref, vcodec_dma_buffer_release);
return 0;
}
dma_addr_t vcodec_dma_import_fd(struct vcodec_dma_session *session, int fd)
{
struct vcodec_dma_buffer *buffer = NULL;
struct dma_buf_attachment *attach;
struct sg_table *sgt;
struct dma_buf *dma_buf;
int ret = 0;
if (!session)
return -EINVAL;
buffer = vcodec_dma_get_buffer(session, fd);
if (!IS_ERR_OR_NULL(buffer)) {
kref_get(&buffer->ref);
return buffer->iova;
}
dma_buf = dma_buf_get(fd);
if (IS_ERR(dma_buf)) {
ret = PTR_ERR(dma_buf);
return ret;
}
buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
if (!buffer) {
ret = -ENOMEM;
goto err;
}
buffer->dma_buf = dma_buf;
buffer->fd = fd;
kref_init(&buffer->ref);
attach = dma_buf_attach(buffer->dma_buf, session->dev);
if (IS_ERR(attach)) {
ret = PTR_ERR(attach);
goto fail_out;
}
sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto fail_detach;
}
buffer->iova = sg_dma_address(sgt->sgl);
buffer->size = sg_dma_len(sgt->sgl);
buffer->attach = attach;
buffer->sgt = sgt;
/* Increase the reference for used outside the buffer pool */
kref_get(&buffer->ref);
INIT_LIST_HEAD(&buffer->list);
mutex_lock(&session->list_mutex);
if (session->buffer_nums >= BUFFER_LIST_MAX_NUMS - 1) {
struct vcodec_dma_buffer *loop_buffer = NULL, *n;
/*
* Clear those buffer not be referenced beyond the buffer pool
* 1. unreference all the buffer in the list
* 2. reference count callback clean and remove it from the list
* 3. Put back the reference from the list
*/
list_for_each_entry_safe(loop_buffer, n,
&session->buffer_list, list) {
kref_put(&loop_buffer->ref, vcodec_dma_buffer_release);
}
session->buffer_nums = 0;
list_for_each_entry(loop_buffer, &session->buffer_list, list) {
kref_get(&loop_buffer->ref);
session->buffer_nums++;
}
}
buffer->index = session->buffer_nums++;
list_add_tail(&buffer->list, &session->buffer_list);
mutex_unlock(&session->list_mutex);
return buffer->iova;
fail_detach:
dma_buf_detach(buffer->dma_buf, attach);
dma_buf_put(buffer->dma_buf);
fail_out:
kfree(buffer);
err:
dma_buf_put(dma_buf);
return ret;
}
void vcodec_dma_destroy_session(struct vcodec_dma_session *session)
{
struct vcodec_dma_buffer *buffer = NULL, *n;
if (!session)
return;
list_for_each_entry_safe(buffer, n, &session->buffer_list, list) {
if (buffer->attach) {
dma_buf_unmap_attachment(buffer->attach, buffer->sgt,
DMA_BIDIRECTIONAL);
dma_buf_detach(buffer->dma_buf, buffer->attach);
dma_buf_put(buffer->dma_buf);
buffer->attach = NULL;
}
dma_buf_put(buffer->dma_buf);
list_del(&buffer->list);
kfree(buffer);
}
kfree(session);
}
struct vcodec_dma_session *vcodec_dma_session_create(struct device *dev)
{
struct vcodec_dma_session *session = NULL;
session = kzalloc(sizeof(*session), GFP_KERNEL);
if (!session)
return session;
INIT_LIST_HEAD(&session->buffer_list);
mutex_init(&session->list_mutex);
session->buffer_nums = 0;
session->dev = dev;
return session;
}
void vcodec_iommu_detach(struct vcodec_iommu_info *info)
{
struct device *dev = info->dev;
struct iommu_domain *domain = info->domain;
if (!info->attached)
return;
iommu_detach_device(domain, dev);
info->attached = false;
}
int vcodec_iommu_attach(struct vcodec_iommu_info *info)
{
struct device *dev = info->dev;
struct iommu_domain *domain = info->domain;
int ret;
if (info->attached)
return 0;
ret = iommu_attach_device(domain, dev);
if (ret)
return ret;
info->attached = true;
return ret;
}
struct vcodec_iommu_info *vcodec_iommu_probe(struct device *dev)
{
struct vcodec_iommu_info *info = NULL;
int ret = 0;
dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms), GFP_KERNEL);
if (!dev->dma_parms) {
ret = -ENOMEM;
goto err_free_parms;
}
info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info) {
ret = -ENOMEM;
goto err_free_parms;
}
info->dev = dev;
info->domain = iommu_domain_alloc(dev->bus);
if (!info->domain) {
ret = -ENOMEM;
goto err_free_info;
}
ret = iommu_get_dma_cookie(info->domain);
if (ret)
goto err_free_domain;
ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
if (ret)
goto err_put_cookie;
dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
ret = iommu_attach_device(info->domain, dev);
if (ret)
goto err_put_cookie;
/*
* You may use the arch_setup_dma_ops() instead in the future kernel
* version, but the iommu_domain_alloc() will create an unnamed domain.
*/
/* ??? - Myy */
arch_setup_dma_ops(dev, 0x0, dev->coherent_dma_mask + 1,
info->domain->ops, true);
return info;
err_put_cookie:
iommu_put_dma_cookie(info->domain);
err_free_domain:
iommu_domain_free(info->domain);
err_free_info:
kfree(info);
err_free_parms:
return ERR_PTR(ret);
}
int vcodec_iommu_remove(struct vcodec_iommu_info *info)
{
iommu_detach_device(info->domain, info->dev);
info->attached = false;
iommu_put_dma_cookie(info->domain);
iommu_domain_free(info->domain);
kfree(info);
return 0;
}