summaryrefslogtreecommitdiff
path: root/drivers/gpu/ion/ion_iommu_heap.c
blob: b75bb3132a2d18de1192d8c0451059d0549ca2c7 (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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * drivers/gpu/ion/ion_iommu_heap.c
 *
 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

#define pr_fmt(fmt)	"%s(): " fmt, __func__

#include <linux/spinlock.h>
#include <linux/kernel.h>
#include <linux/genalloc.h>
#include <linux/io.h>
#include <linux/ion.h>
#include <linux/mm.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/iommu.h>
#include <linux/highmem.h>

#include <asm/cacheflush.h>

#include "ion_priv.h"

#define NUM_PAGES(buf)	(PAGE_ALIGN((buf)->size) >> PAGE_SHIFT)

#define GFP_ION		(GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN)

struct ion_iommu_heap {
	struct ion_heap		heap;
	struct gen_pool		*pool;
	struct iommu_domain	*domain;
	struct device		*dev;
};

static struct scatterlist *iommu_heap_map_dma(struct ion_heap *heap,
					      struct ion_buffer *buf)
{
	struct ion_iommu_heap *h =
		container_of(heap, struct ion_iommu_heap, heap);
	int err, npages = NUM_PAGES(buf);
	unsigned int i;
	struct scatterlist *sg;
	unsigned long da = (unsigned long)buf->priv_virt;

	for_each_sg(buf->sglist, sg, npages, i) {
		phys_addr_t pa;

		pa = sg_phys(sg);
		BUG_ON(!IS_ALIGNED(sg->length, PAGE_SIZE));
		err = iommu_map(h->domain, da, pa, 0, 0);
		if (err)
			goto err_out;

		sg->dma_address = da;
		da += PAGE_SIZE;
	}

	pr_debug("da:%p pa:%08x va:%p\n",
		 buf->priv_virt, sg_phys(buf->sglist), buf->vaddr);

	return buf->sglist;

err_out:
	if (i-- > 0) {
		unsigned int j;
		for_each_sg(buf->sglist, sg, i, j)
			iommu_unmap(h->domain, sg_dma_address(sg), 0);
	}
	return ERR_PTR(err);
}

static void iommu_heap_unmap_dma(struct ion_heap *heap, struct ion_buffer *buf)
{
	struct ion_iommu_heap *h =
		container_of(heap, struct ion_iommu_heap, heap);
	unsigned int i;
	struct scatterlist *sg;
	int npages = NUM_PAGES(buf);

	for_each_sg(buf->sglist, sg, npages, i)
		iommu_unmap(h->domain, sg_dma_address(sg), 0);

	pr_debug("da:%p\n", buf->priv_virt);
}

struct scatterlist *iommu_heap_remap_dma(struct ion_heap *heap,
					      struct ion_buffer *buf,
					      unsigned long addr)
{
	struct ion_iommu_heap *h =
		container_of(heap, struct ion_iommu_heap, heap);
	int err;
	unsigned int i;
	unsigned long da, da_to_free = (unsigned long)buf->priv_virt;
	int npages = NUM_PAGES(buf);

	BUG_ON(!buf->priv_virt);

	da = gen_pool_alloc_addr(h->pool, buf->size, addr);
	if (da == 0) {
		pr_err("dma address alloc failed, addr=0x%lx", addr);
		return ERR_PTR(-ENOMEM);
	} else {
		pr_err("iommu_heap_remap_dma passed, addr=0x%lx",
			addr);
		iommu_heap_unmap_dma(heap, buf);
		gen_pool_free(h->pool, da_to_free, buf->size);
		buf->priv_virt = (void *)da;
	}
	for (i = 0; i < npages; i++) {
		phys_addr_t pa;

		pa = page_to_phys(buf->pages[i]);
		err = iommu_map(h->domain, da, pa, 0, 0);
		if (err)
			goto err_out;
		da += PAGE_SIZE;
	}

	pr_debug("da:%p pa:%08x va:%p\n",
		 buf->priv_virt, page_to_phys(buf->pages[0]), buf->vaddr);

	return (struct scatterlist *)buf->pages;

err_out:
	if (i-- > 0) {
		da = (unsigned long)buf->priv_virt;
		iommu_unmap(h->domain, da + (i << PAGE_SHIFT), 0);
	}
	return ERR_PTR(err);
}

static int ion_buffer_allocate(struct ion_buffer *buf)
{
	int i, npages = NUM_PAGES(buf);

	buf->pages = kmalloc(npages * sizeof(*buf->pages), GFP_KERNEL);
	if (!buf->pages)
		goto err_pages;

	buf->sglist = vzalloc(npages * sizeof(*buf->sglist));
	if (!buf->sglist)
		goto err_sgl;

	sg_init_table(buf->sglist, npages);

	for (i = 0; i < npages; i++) {
		struct page *page;
		phys_addr_t pa;

		page = alloc_page(GFP_ION);
		if (!page)
			goto err_pgalloc;
		pa = page_to_phys(page);

		sg_set_page(&buf->sglist[i], page, PAGE_SIZE, 0);

		flush_dcache_page(page);
		outer_flush_range(pa, pa + PAGE_SIZE);

		buf->pages[i] = page;

		pr_debug_once("pa:%08x\n", pa);
	}
	return 0;

err_pgalloc:
	while (i-- > 0)
		__free_page(buf->pages[i]);
	vfree(buf->sglist);
err_sgl:
	kfree(buf->pages);
err_pages:
	return -ENOMEM;
}

static void ion_buffer_free(struct ion_buffer *buf)
{
	int i, npages = NUM_PAGES(buf);

	for (i = 0; i < npages; i++)
		__free_page(buf->pages[i]);
	vfree(buf->sglist);
	kfree(buf->pages);
}

static int iommu_heap_allocate(struct ion_heap *heap, struct ion_buffer *buf,
			       unsigned long len, unsigned long align,
			       unsigned long flags)
{
	int err;
	struct ion_iommu_heap *h =
		container_of(heap, struct ion_iommu_heap, heap);
	unsigned long da;
	struct scatterlist *sgl;

	len = round_up(len, PAGE_SIZE);

	da = gen_pool_alloc(h->pool, len);
	if (!da)
		return -ENOMEM;

	buf->priv_virt = (void *)da;
	buf->size = len;

	WARN_ON(!IS_ALIGNED(da, PAGE_SIZE));

	err = ion_buffer_allocate(buf);
	if (err)
		goto err_alloc_buf;

	sgl = iommu_heap_map_dma(heap, buf);
	if (IS_ERR_OR_NULL(sgl))
		goto err_heap_map_dma;
	buf->vaddr = 0;
	return 0;

err_heap_map_dma:
	ion_buffer_free(buf);
err_alloc_buf:
	gen_pool_free(h->pool, da, len);
	buf->size = 0;
	buf->pages = NULL;
	buf->priv_virt = NULL;
	return err;
}

static void iommu_heap_free(struct ion_buffer *buf)
{
	struct ion_heap *heap = buf->heap;
	struct ion_iommu_heap *h =
		container_of(heap, struct ion_iommu_heap, heap);
	void *da = buf->priv_virt;

	iommu_heap_unmap_dma(heap, buf);
	ion_buffer_free(buf);
	gen_pool_free(h->pool, (unsigned long)da, buf->size);

	buf->pages = NULL;
	buf->priv_virt = NULL;
	pr_debug("da:%p\n", da);
}

static int iommu_heap_phys(struct ion_heap *heap, struct ion_buffer *buf,
			   ion_phys_addr_t *addr, size_t *len)
{
	*addr = (unsigned long)buf->priv_virt;
	*len = buf->size;
	pr_debug("da:%08lx(%x)\n", *addr, *len);
	return 0;
}

static void *iommu_heap_map_kernel(struct ion_heap *heap,
				   struct ion_buffer *buf)
{
	int npages = NUM_PAGES(buf);

	BUG_ON(!buf->pages);
	buf->vaddr = vm_map_ram(buf->pages, npages, -1,
				pgprot_noncached(pgprot_kernel));
	pr_debug("va:%p\n", buf->vaddr);
	WARN_ON(!buf->vaddr);
	return buf->vaddr;
}

static void iommu_heap_unmap_kernel(struct ion_heap *heap,
				    struct ion_buffer *buf)
{
	int npages = NUM_PAGES(buf);

	BUG_ON(!buf->pages);
	WARN_ON(!buf->vaddr);
	vm_unmap_ram(buf->vaddr, npages);
	buf->vaddr = NULL;
	pr_debug("va:%p\n", buf->vaddr);
}

static int iommu_heap_map_user(struct ion_heap *mapper,
			       struct ion_buffer *buf,
			       struct vm_area_struct *vma)
{
	int i = vma->vm_pgoff >> PAGE_SHIFT;
	unsigned long uaddr = vma->vm_start;
	unsigned long usize = vma->vm_end - vma->vm_start;

	pr_debug("vma:%08lx-%08lx\n", vma->vm_start, vma->vm_end);
	BUG_ON(!buf->pages);

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	do {
		int ret;
		struct page *page = buf->pages[i++];

		ret = vm_insert_page(vma, uaddr, page);
		if (ret)
			return ret;

		uaddr += PAGE_SIZE;
		usize -= PAGE_SIZE;
	} while (usize > 0);

	return 0;
}

static struct ion_heap_ops iommu_heap_ops = {
	.allocate	= iommu_heap_allocate,
	.free		= iommu_heap_free,
	.phys		= iommu_heap_phys,
	.map_dma	= iommu_heap_map_dma,
	.unmap_dma	= iommu_heap_unmap_dma,
	.map_kernel	= iommu_heap_map_kernel,
	.unmap_kernel	= iommu_heap_unmap_kernel,
	.map_user	= iommu_heap_map_user,
};

struct ion_heap *ion_iommu_heap_create(struct ion_platform_heap *data)
{
	struct ion_iommu_heap *h;
	int err;

	h = kzalloc(sizeof(*h), GFP_KERNEL);
	if (!h) {
		err = -ENOMEM;
		goto err_heap;
	}

	h->pool = gen_pool_create(12, -1);
	if (!h->pool) {
		err = -ENOMEM;
		goto err_genpool;
	}
	gen_pool_add(h->pool, data->base, data->size, -1);

	h->heap.ops = &iommu_heap_ops;
	h->domain = iommu_domain_alloc();
	h->dev = data->priv;
	if (!h->domain) {
		err = -ENOMEM;
		goto err_iommu_alloc;
	}

	err = iommu_attach_device(h->domain, h->dev);
	if (err)
		goto err_iommu_attach;

	return &h->heap;

err_iommu_attach:
	iommu_domain_free(h->domain);
err_iommu_alloc:
	gen_pool_destroy(h->pool);
err_genpool:
	kfree(h);
err_heap:
	return ERR_PTR(err);
}

void ion_iommu_heap_destroy(struct ion_heap *heap)
{
	struct ion_iommu_heap *h =
		container_of(heap, struct  ion_iommu_heap, heap);

	iommu_detach_device(h->domain, h->dev);
	gen_pool_destroy(h->pool);
	iommu_domain_free(h->domain);
	kfree(h);
}