summaryrefslogtreecommitdiff
path: root/ion_physical_heap.c
blob: 30c8250ea8d4add7dddcc677f7d33798b6e2bd48 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2011,2020 Google LLC
 */
#include <linux/spinlock.h>
#include <linux/dma-map-ops.h>
#include <linux/err.h>
#include <linux/genalloc.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/samsung-dma-heap.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

#include <asm/cacheflush.h>

#include "ion_physical_heap.h"

#define ION_PHYSICAL_ALLOCATE_FAIL -1

struct ion_physical_heap {
	struct gen_pool *pool;
	phys_addr_t base;
	size_t size;

	ion_physical_heap_allocate_callback *allocate_cb;
	void *allocate_ctx;

	ion_physical_heap_free_callback *free_cb;
	void *free_ctx;
};

static int _clear_pages(struct page **pages, int num, pgprot_t pgprot)
{
	void *addr = vmap(pages, num, VM_MAP, pgprot);

	if (!addr)
		return -ENOMEM;
	memset(addr, 0, PAGE_SIZE * num);
	vunmap(addr);

	return 0;
}

static int _sglist_zero(struct scatterlist *sgl, unsigned int nents,
			pgprot_t pgprot)
{
	int p = 0;
	int ret = 0;
	struct sg_page_iter piter;
	struct page *pages[32];

	for_each_sg_page(sgl, &piter, nents, 0) {
		pages[p++] = sg_page_iter_page(&piter);
		if (p == ARRAY_SIZE(pages)) {
			ret = _clear_pages(pages, p, pgprot);
			if (ret)
				return ret;
			p = 0;
		}
	}
	if (p)
		ret = _clear_pages(pages, p, pgprot);

	return ret;
}

static int _buffer_zero(struct samsung_dma_buffer *buffer)
{
	pgprot_t pgprot = pgprot_writecombine(PAGE_KERNEL);

	return _sglist_zero(buffer->sg_table.sgl, buffer->sg_table.orig_nents, pgprot);
}

static int _pages_zero(struct page *page, size_t size, pgprot_t pgprot)
{
	struct scatterlist sg;

	sg_init_table(&sg, 1);
	sg_set_page(&sg, page, size, 0);
	return _sglist_zero(&sg, 1, pgprot);
}

static int ion_physical_heap_do_allocate(struct dma_heap *heap,
					 struct samsung_dma_buffer *buffer,
					 unsigned long size)
{
	struct samsung_dma_heap *samsung_dma_heap = dma_heap_get_drvdata(heap);
	struct ion_physical_heap *physical_heap = samsung_dma_heap->priv;
	unsigned long aligned_size = ALIGN(size, samsung_dma_heap->alignment);
	phys_addr_t paddr;

	paddr = gen_pool_alloc(physical_heap->pool, aligned_size);
	if (!paddr) {
		pr_err("%s: failed to allocate from AOC physical heap size %lu",
		       __func__, size);
		return -ENOMEM;
	}

	sg_set_page(buffer->sg_table.sgl, pfn_to_page(PFN_DOWN(paddr)), size, 0);
	buffer->priv = (void *)(uintptr_t)hash_long(paddr, 32);

	if (physical_heap->allocate_cb)
		physical_heap->allocate_cb(buffer, physical_heap->allocate_ctx);

	return 0;
}

static void ion_physical_heap_buffer_free(struct samsung_dma_buffer *buffer)
{
	struct ion_physical_heap *physical_heap = buffer->heap->priv;
	struct page *page;
	phys_addr_t paddr;

	page = sg_page(buffer->sg_table.sgl);
	paddr = PFN_PHYS(page_to_pfn(page));

	if (physical_heap->free_cb)
		physical_heap->free_cb(buffer, physical_heap->free_ctx);

	_buffer_zero(buffer);
	if (paddr)
		gen_pool_free(physical_heap->pool, paddr,
			      ALIGN(buffer->len, buffer->heap->alignment));

	samsung_dma_buffer_free(buffer);
}

static struct dma_buf *ion_physical_heap_allocate(struct dma_heap *heap,
						  unsigned long len,
						  unsigned long fd_flags,
						  unsigned long heap_flags)
{
	int ret;
	struct dma_buf *dmabuf;
	struct samsung_dma_buffer *buffer;
	struct samsung_dma_heap *samsung_dma_heap = dma_heap_get_drvdata(heap);

	buffer = samsung_dma_buffer_alloc(samsung_dma_heap, len, 1);
	if (IS_ERR(buffer))
		return ERR_CAST(buffer);

	ret = ion_physical_heap_do_allocate(heap, buffer, len);
	if (ret) {
		samsung_dma_buffer_free(buffer);
		return ERR_PTR(ret);
	}

	dmabuf = samsung_export_dmabuf(buffer, fd_flags);
	if (IS_ERR(dmabuf))
		ion_physical_heap_buffer_free(buffer);

	return dmabuf;
}

static long ion_physical_get_pool_size(struct dma_heap *heap)
{
	struct samsung_dma_heap *samsung_dma_heap = dma_heap_get_drvdata(heap);
	struct ion_physical_heap *physical_heap = samsung_dma_heap->priv;

	return physical_heap->size / PAGE_SIZE;
}

static struct dma_heap_ops physical_heap_ops = {
	.allocate = ion_physical_heap_allocate,
	.get_pool_size = ion_physical_get_pool_size,
};

static struct dma_heap *samsung_dma_heap_create_helper(const char *name,
						       size_t align,
						       void *priv)
{
	struct samsung_dma_heap *samsung_dma_heap;
	struct dma_heap *heap;
	struct dma_heap_export_info exp_info;

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

	samsung_dma_heap->name = name;
	samsung_dma_heap->release = ion_physical_heap_buffer_free;
	samsung_dma_heap->priv = priv;
	samsung_dma_heap->flags = DMA_HEAP_FLAG_UNCACHED;
	samsung_dma_heap->alignment = align;

	exp_info.name = name;
	exp_info.ops = &physical_heap_ops;
	exp_info.priv = samsung_dma_heap;

	heap = dma_heap_add(&exp_info);
	if (IS_ERR(heap))
		kfree(samsung_dma_heap);

	return heap;
}

struct dma_heap *ion_physical_heap_create(phys_addr_t base, size_t size,
					  size_t align, const char *name,
					  ion_physical_heap_allocate_callback alloc_cb,
					  ion_physical_heap_free_callback free_cb,
					  void *ctx)
{
	struct ion_physical_heap *physical_heap;
	int ret;
	struct page *page;
	struct dma_heap *physical_dmabuf_heap;

	page = pfn_to_page(PFN_DOWN(base));

	ret = _pages_zero(page, size, pgprot_writecombine(PAGE_KERNEL));
	if (ret)
		return ERR_PTR(ret);

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

	physical_heap->pool = gen_pool_create(get_order(align) + PAGE_SHIFT, -1);
	if (!physical_heap->pool) {
		kfree(physical_heap);
		return ERR_PTR(-ENOMEM);
	}

	physical_heap->base = base;
	gen_pool_add(physical_heap->pool, physical_heap->base, size, -1);

	physical_heap->size = size;

	physical_heap->allocate_cb = alloc_cb;
	physical_heap->allocate_ctx = ctx;

	physical_heap->free_cb = free_cb;
	physical_heap->free_ctx = ctx;

	physical_dmabuf_heap = samsung_dma_heap_create_helper(name, align,
							      (void *)physical_heap);
	if (IS_ERR(physical_dmabuf_heap)) {
		gen_pool_destroy(physical_heap->pool);
		kfree(physical_heap);
	}

	return physical_dmabuf_heap;
}