hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
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
349
350
351
352
353
354
355
356
357
358
359
// SPDX-License-Identifier: GPL-2.0
/*
 * ION Memory Allocator - dmabuf interface
 *
 * Copyright (c) 2019, Google, Inc.
 */
 
#include <linux/device.h>
#include <linux/mm.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
 
#include "ion_private.h"
 
static struct sg_table *dup_sg_table(struct sg_table *table)
{
   struct sg_table *new_table;
   int ret, i;
   struct scatterlist *sg, *new_sg;
 
   new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
   if (!new_table)
       return ERR_PTR(-ENOMEM);
 
   ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL);
   if (ret) {
       kfree(new_table);
       return ERR_PTR(-ENOMEM);
   }
 
   new_sg = new_table->sgl;
   for_each_sg(table->sgl, sg, table->nents, i) {
       memcpy(new_sg, sg, sizeof(*sg));
       new_sg->dma_address = 0;
       new_sg = sg_next(new_sg);
   }
 
   return new_table;
}
 
static void free_duped_table(struct sg_table *table)
{
   sg_free_table(table);
   kfree(table);
}
 
static int ion_dma_buf_attach(struct dma_buf *dmabuf,
                 struct dma_buf_attachment *attachment)
{
   struct ion_dma_buf_attachment *a;
   struct sg_table *table;
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   if (heap->buf_ops.attach)
       return heap->buf_ops.attach(dmabuf, attachment);
 
   a = kzalloc(sizeof(*a), GFP_KERNEL);
   if (!a)
       return -ENOMEM;
 
   table = dup_sg_table(buffer->sg_table);
   if (IS_ERR(table)) {
       kfree(a);
       return -ENOMEM;
   }
 
   a->table = table;
   a->dev = attachment->dev;
   INIT_LIST_HEAD(&a->list);
   a->mapped = false;
 
   attachment->priv = a;
 
   mutex_lock(&buffer->lock);
   list_add(&a->list, &buffer->attachments);
   mutex_unlock(&buffer->lock);
 
   return 0;
}
 
static void ion_dma_buf_detatch(struct dma_buf *dmabuf,
               struct dma_buf_attachment *attachment)
{
   struct ion_dma_buf_attachment *a = attachment->priv;
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   if (heap->buf_ops.detach)
       return heap->buf_ops.detach(dmabuf, attachment);
 
   mutex_lock(&buffer->lock);
   list_del(&a->list);
   mutex_unlock(&buffer->lock);
   free_duped_table(a->table);
 
   kfree(a);
}
 
static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
                   enum dma_data_direction direction)
{
   struct ion_buffer *buffer = attachment->dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
   struct ion_dma_buf_attachment *a;
   struct sg_table *table;
   unsigned long attrs = attachment->dma_map_attrs;
 
   if (heap->buf_ops.map_dma_buf)
       return heap->buf_ops.map_dma_buf(attachment, direction);
 
   a = attachment->priv;
   table = a->table;
 
   if (!(buffer->flags & ION_FLAG_CACHED))
       attrs |= DMA_ATTR_SKIP_CPU_SYNC;
 
   if (!dma_map_sg_attrs(attachment->dev, table->sgl, table->nents,
                 direction, attrs))
       return ERR_PTR(-ENOMEM);
 
   a->mapped = true;
 
   return table;
}
 
static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
                 struct sg_table *table,
                 enum dma_data_direction direction)
{
   struct ion_buffer *buffer = attachment->dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
   struct ion_dma_buf_attachment *a = attachment->priv;
   unsigned long attrs = attachment->dma_map_attrs;
 
   if (heap->buf_ops.unmap_dma_buf)
       return heap->buf_ops.unmap_dma_buf(attachment, table,
                          direction);
 
   a->mapped = false;
 
   if (!(buffer->flags & ION_FLAG_CACHED))
       attrs |= DMA_ATTR_SKIP_CPU_SYNC;
 
   dma_unmap_sg_attrs(attachment->dev, table->sgl, table->nents,
              direction, attrs);
}
 
static void ion_dma_buf_release(struct dma_buf *dmabuf)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   if (heap->buf_ops.release)
       return heap->buf_ops.release(dmabuf);
 
   ion_free(buffer);
}
 
static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
                   enum dma_data_direction direction)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
   struct ion_dma_buf_attachment *a;
 
   if (heap->buf_ops.begin_cpu_access)
       return heap->buf_ops.begin_cpu_access(dmabuf, direction);
 
   mutex_lock(&buffer->lock);
   if (!(buffer->flags & ION_FLAG_CACHED))
       goto unlock;
 
   list_for_each_entry(a, &buffer->attachments, list) {
       if (!a->mapped)
           continue;
       dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents,
                   direction);
   }
 
unlock:
   mutex_unlock(&buffer->lock);
   return 0;
}
 
static int
ion_dma_buf_begin_cpu_access_partial(struct dma_buf *dmabuf,
                    enum dma_data_direction direction,
                    unsigned int offset, unsigned int len)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   /* This is done to make sure partial buffer cache flush / invalidate is
    * allowed. The implementation may be vendor specific in this case, so
    * ion core does not provide a default implementation
    */
   if (!heap->buf_ops.begin_cpu_access_partial)
       return -EOPNOTSUPP;
 
   return heap->buf_ops.begin_cpu_access_partial(dmabuf, direction, offset,
                             len);
}
 
static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
                     enum dma_data_direction direction)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
   struct ion_dma_buf_attachment *a;
 
   if (heap->buf_ops.end_cpu_access)
       return heap->buf_ops.end_cpu_access(dmabuf, direction);
 
   mutex_lock(&buffer->lock);
   if (!(buffer->flags & ION_FLAG_CACHED))
       goto unlock;
 
   list_for_each_entry(a, &buffer->attachments, list) {
       if (!a->mapped)
           continue;
       dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents,
                      direction);
   }
unlock:
   mutex_unlock(&buffer->lock);
 
   return 0;
}
 
static int ion_dma_buf_end_cpu_access_partial(struct dma_buf *dmabuf,
                         enum dma_data_direction direction,
                         unsigned int offset,
                         unsigned int len)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   /* This is done to make sure partial buffer cache flush / invalidate is
    * allowed. The implementation may be vendor specific in this case, so
    * ion core does not provide a default implementation
    */
   if (!heap->buf_ops.end_cpu_access_partial)
       return -EOPNOTSUPP;
 
   return heap->buf_ops.end_cpu_access_partial(dmabuf, direction, offset,
                           len);
}
 
static int ion_dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
   int ret;
 
   /* now map it to userspace */
   if (heap->buf_ops.mmap) {
       ret = heap->buf_ops.mmap(dmabuf, vma);
   } else {
       mutex_lock(&buffer->lock);
       if (!(buffer->flags & ION_FLAG_CACHED))
           vma->vm_page_prot =
               pgprot_writecombine(vma->vm_page_prot);
 
       ret = ion_heap_map_user(heap, buffer, vma);
       mutex_unlock(&buffer->lock);
   }
 
   if (ret)
       pr_err("%s: failure mapping buffer to userspace\n", __func__);
 
   return ret;
}
 
static void *ion_dma_buf_vmap(struct dma_buf *dmabuf)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
   void *vaddr;
 
   if (heap->buf_ops.vmap)
       return heap->buf_ops.vmap(dmabuf);
 
   mutex_lock(&buffer->lock);
   vaddr = ion_buffer_kmap_get(buffer);
   mutex_unlock(&buffer->lock);
 
   return vaddr;
}
 
static void ion_dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   if (heap->buf_ops.vunmap) {
       heap->buf_ops.vunmap(dmabuf, vaddr);
       return;
   }
 
   mutex_lock(&buffer->lock);
   ion_buffer_kmap_put(buffer);
   mutex_unlock(&buffer->lock);
}
 
static int ion_dma_buf_get_flags(struct dma_buf *dmabuf, unsigned long *flags)
{
   struct ion_buffer *buffer = dmabuf->priv;
   struct ion_heap *heap = buffer->heap;
 
   if (!heap->buf_ops.get_flags)
       return -EOPNOTSUPP;
 
   return heap->buf_ops.get_flags(dmabuf, flags);
}
 
static const struct dma_buf_ops dma_buf_ops = {
   .attach = ion_dma_buf_attach,
   .detach = ion_dma_buf_detatch,
   .map_dma_buf = ion_map_dma_buf,
   .unmap_dma_buf = ion_unmap_dma_buf,
   .release = ion_dma_buf_release,
   .begin_cpu_access = ion_dma_buf_begin_cpu_access,
   .begin_cpu_access_partial = ion_dma_buf_begin_cpu_access_partial,
   .end_cpu_access = ion_dma_buf_end_cpu_access,
   .end_cpu_access_partial = ion_dma_buf_end_cpu_access_partial,
   .mmap = ion_dma_buf_mmap,
   .vmap = ion_dma_buf_vmap,
   .vunmap = ion_dma_buf_vunmap,
   .get_flags = ion_dma_buf_get_flags,
};
 
struct dma_buf *ion_dmabuf_alloc(struct ion_device *dev, size_t len,
                unsigned int heap_id_mask,
                unsigned int flags)
{
   struct ion_buffer *buffer;
   DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
   struct dma_buf *dmabuf;
 
   pr_debug("%s: len %zu heap_id_mask %u flags %x\n", __func__,
        len, heap_id_mask, flags);
 
   buffer = ion_buffer_alloc(dev, len, heap_id_mask, flags);
   if (IS_ERR(buffer))
       return ERR_CAST(buffer);
 
   exp_info.ops = &dma_buf_ops;
   exp_info.size = buffer->size;
   exp_info.flags = O_RDWR;
   exp_info.priv = buffer;
 
   dmabuf = dma_buf_export(&exp_info);
   if (IS_ERR(dmabuf))
       ion_buffer_destroy(dev, buffer);
 
   return dmabuf;
}