aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/plat-s5p/s5p_iommu.c
blob: a900c84a7ddba8b82cba781d9a7822c055651b4a (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* linux/drivers/iommu/exynos_iommu.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * 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.
 */

#ifdef CONFIG_S5P_SYSTEM_MMU_DEBUG
#define DEBUG
#endif

#include <linux/io.h>
#include <linux/mm.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/iommu.h>
#include <linux/errno.h>
#include <linux/list.h>

#include <asm/cacheflush.h>

#include <plat/s5p-sysmmu.h>

#ifdef CONFIG_S5P_SYSTEM_MMU_DEBUG
#define DEBUG /* for dev_dbg() */
#endif

/* We does not consider super section mapping (16MB) */
#define S5P_SPAGE_SHIFT		12
#define S5P_LPAGE_SHIFT		16
#define S5P_SECTION_SHIFT	20

#define S5P_SPAGE_SIZE		(1 << S5P_SPAGE_SHIFT)
#define S5P_LPAGE_SIZE		(1 << S5P_LPAGE_SHIFT)
#define S5P_SECTION_SIZE	(1 << S5P_SECTION_SHIFT)

#define S5P_SPAGE_MASK		(~(S5P_SPAGE_SIZE - 1))
#define S5P_LPAGE_MASK		(~(S5P_LPAGE_SIZE - 1))
#define S5P_SECTION_MASK	(~(S5P_SECTION_SIZE - 1))

#define S5P_SPAGE_ORDER		(S5P_SPAGE_SHIFT - PAGE_SHIFT)
#define S5P_LPAGE_ORDER		(S5P_LPAGE_SHIFT - S5P_SPAGE_SHIFT)
#define S5P_SECTION_ORDER	(S5P_SECTION_SHIFT - S5P_SPAGE_SHIFT)

#define S5P_LV1TABLE_ENTRIES	(1 << (BITS_PER_LONG - S5P_SECTION_SHIFT))
#define S5P_LV1TABLE_ORDER	2 /* get_order(S5P_LV1TABLE_ENTRIES) */

#define S5P_LV2TABLE_ENTRIES	(1 << S5P_SECTION_ORDER)
#define S5P_LV2TABLE_SIZE	(S5P_LV2TABLE_ENTRIES * sizeof(long))
#define S5P_LV2TABLE_MASK	(~(S5P_LV2TABLE_SIZE - 1)) /* 0xFFFFFC00 */

#define S5P_SECTION_LV1_ENTRY(entry)	((entry & 0x40003) == 2)
#define S5P_SUPSECT_LV1_ENTRY(entry)	((entry & 0x40003) == 0x40002)
#define S5P_PAGE_LV1_ENTRY(entry)	((entry & 3) == 1)
#define S5P_FAULT_LV1_ENTRY(entry) (((entry & 3) == 0) || (entry & 3) == 3)

#define S5P_LPAGE_LV2_ENTRY(entry)	((entry & 3) == 1)
#define S5P_SPAGE_LV2_ENTRY(entry)	((entry & 2) == 2)
#define S5P_FAULT_LV2_ENTRY(entry)	((entry & 3) == 0)

#define MAKE_FAULT_ENTRY(entry)		do { entry = 0; } while (0)
#define MAKE_SECTION_ENTRY(entry, pa)	do { entry = pa | 2; } while (0)
#define MAKE_SUPSECT_ENTRY(entry, pa)	do { entry = pa | 0x40002; } while (0)
#define MAKE_LV2TABLE_ENTRY(entry, pa)	do { entry = pa | 1; } while (0)

#define MAKE_LPAGE_ENTRY(entry, pa)	do { entry = pa | 1; } while (0)
#define MAKE_SPAGE_ENTRY(entry, pa)	do { entry = pa | 3; } while (0)

#define GET_LV2ENTRY(entry, iova) (\
	(unsigned long *)phys_to_virt(entry & S5P_LV2TABLE_MASK) +\
	((iova & (~S5P_SECTION_MASK)) >> S5P_SPAGE_SHIFT))

struct s5p_iommu_domain {
	struct device *dev;
	unsigned long *pgtable;
	struct mutex lock;
};

/* slab cache for level 2 page tables */
static struct kmem_cache *l2table_cachep;

static inline void pgtable_flush(void *vastart, void *vaend)
{
	dmac_flush_range(vastart, vaend);
	outer_flush_range(virt_to_phys(vastart),
				virt_to_phys(vaend));
}

static int s5p_iommu_domain_init(struct iommu_domain *domain)
{
	struct s5p_iommu_domain *priv;

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

	priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
							S5P_LV1TABLE_ORDER);
	if (!priv->pgtable) {
		kfree(priv);
		return -ENOMEM;
	}

	memset(priv->pgtable, 0, S5P_LV1TABLE_ENTRIES * sizeof(unsigned long));
	pgtable_flush(priv->pgtable, priv->pgtable + S5P_LV1TABLE_ENTRIES);

	mutex_init(&priv->lock);

	domain->priv = priv;
	pr_debug("%s: Allocated IOMMU domain %p with pgtable @ %#lx\n",
			__func__, domain, __pa(priv->pgtable));
	return 0;
}

static void s5p_iommu_domain_destroy(struct iommu_domain *domain)
{
	struct s5p_iommu_domain *priv = domain->priv;

	free_pages((unsigned long)priv->pgtable, S5P_LV1TABLE_ORDER);
	kfree(domain->priv);
	domain->priv = NULL;
}

static int s5p_iommu_attach_device(struct iommu_domain *domain,
				   struct device *dev)
{
	int ret;
	struct s5p_iommu_domain *s5p_domain = domain->priv;

	if (s5p_domain->dev) {
		pr_debug("%s: %s is already attached to doamin %p\n", __func__,
				dev_name(s5p_domain->dev), domain);
		BUG_ON(s5p_domain->dev != dev);
		return -EBUSY;
	}

	ret = s5p_sysmmu_enable(dev, virt_to_phys(s5p_domain->pgtable));
	if (ret)
		return ret;

	mutex_lock(&s5p_domain->lock);
	s5p_domain->dev = dev;
	mutex_unlock(&s5p_domain->lock);

	return 0;
}

static void s5p_iommu_detach_device(struct iommu_domain *domain,
				    struct device *dev)
{
	struct s5p_iommu_domain *s5p_domain = domain->priv;

	mutex_lock(&s5p_domain->lock);

	if (s5p_domain->dev == dev) {
		mutex_unlock(&s5p_domain->lock);

		s5p_sysmmu_disable(s5p_domain->dev);

		s5p_domain->dev = NULL;
	} else {
		pr_debug("%s: %s is not attached to domain of pgtable @ %#lx\n",
			__func__, dev_name(dev), __pa(s5p_domain->pgtable));
		mutex_unlock(&s5p_domain->lock);
	}

}

static bool section_available(struct iommu_domain *domain,
			      unsigned long *lv1entry)
{
	struct s5p_iommu_domain *s5p_domain = domain->priv;

	if (S5P_SECTION_LV1_ENTRY(*lv1entry)) {
		pr_err("1MB entry alread exists at %#x // pgtable %#lx\n",
				(lv1entry - s5p_domain->pgtable) * SZ_1M,
				__pa(s5p_domain->pgtable));
		return false;
	}

	if (S5P_PAGE_LV1_ENTRY(*lv1entry)) {
		unsigned long *lv2end, *lv2base;

		lv2base = phys_to_virt(*lv1entry & S5P_LV2TABLE_MASK);
		lv2end = lv2base + S5P_LV2TABLE_ENTRIES;
		while (lv2base != lv2end) {
			if (!S5P_FAULT_LV2_ENTRY(*lv2base)) {
				pr_err("Failed to free L2 page table for"
					"section mapping. // pgtalle %#lx\n",
					__pa(s5p_domain->pgtable));
				return false;
			}
			lv2base++;
		}

		kmem_cache_free(l2table_cachep,
				phys_to_virt(*lv1entry & S5P_LV2TABLE_MASK));

		MAKE_FAULT_ENTRY(*lv1entry);
	}

	return true;
}

static bool write_lpage(unsigned long *head_entry, unsigned long phys_addr)
{
	unsigned long *entry, *end;

	entry = head_entry;
	end = entry + (1 << S5P_LPAGE_ORDER);

	while (entry != end) {
		if (!S5P_FAULT_LV2_ENTRY(*entry))
			break;

		MAKE_LPAGE_ENTRY(*entry, phys_addr);

		entry++;
	}

	if (entry != end) {
		end = entry;
		while (entry != head_entry)
			MAKE_FAULT_ENTRY(*(--entry));

		return false;
	}

	return true;
}

static int s5p_iommu_map(struct iommu_domain *domain, unsigned long iova,
			 phys_addr_t paddr, int gfp_order, int prot)
{
	struct s5p_iommu_domain *s5p_domain = domain->priv;
	unsigned long *start_entry, *entry, *end_entry;
	int num_entry;
	int ret = 0;

	BUG_ON(s5p_domain->pgtable== NULL);

	mutex_lock(&s5p_domain->lock);

	start_entry = entry = s5p_domain->pgtable + (iova >> S5P_SECTION_SHIFT);

	if (gfp_order >= S5P_SECTION_ORDER) {
		BUG_ON((paddr | iova) & ~S5P_SECTION_MASK);
		/* 1MiB mapping */

		num_entry = 1 << (gfp_order - S5P_SECTION_ORDER);
		end_entry = entry + num_entry;

		while (entry != end_entry) {
			if (!section_available(domain, entry))
				break;

			MAKE_SECTION_ENTRY(*entry, paddr);

			paddr += S5P_SECTION_SIZE;
			entry++;
		}

		if (entry != end_entry)
			goto mapping_error;

		pgtable_flush(start_entry, entry);
		goto mapping_done;
	}

	if (S5P_FAULT_LV1_ENTRY(*entry)) {
		unsigned long *l2table;

		l2table = kmem_cache_zalloc(l2table_cachep, GFP_KERNEL);
		if (!l2table) {
			ret = -ENOMEM;
			goto nomem_error;
		}

		pgtable_flush(l2table, l2table + S5P_LV2TABLE_ENTRIES);

		MAKE_LV2TABLE_ENTRY(*entry, virt_to_phys(l2table));
		pgtable_flush(entry, entry + 1);
	}

	/* 'entry' points level 2 entries, hereafter */
	entry = GET_LV2ENTRY(*entry, iova);

	start_entry = entry;
	num_entry = 1 << gfp_order;
	end_entry = entry + num_entry;

	if (gfp_order >= S5P_LPAGE_ORDER) {
		/* large page(64KiB) mapping */
		BUG_ON((paddr | iova) & ~S5P_LPAGE_MASK);

		while (entry != end_entry) {
			if (!write_lpage(entry, paddr)) {
				pr_err("%s: Failed to allocate large page"
						"for IOVA %#lx entry.\n",
						__func__, iova);
				ret = -EADDRINUSE;
				break;
			}

			paddr += S5P_LPAGE_SIZE;
			entry += (1 << S5P_LPAGE_ORDER);
		}

		if (entry != end_entry) {
			entry -= 1 << S5P_LPAGE_ORDER;
			goto mapping_error;
		}
	} else {
		/* page (4KiB) mapping */
		while (entry != end_entry && S5P_FAULT_LV2_ENTRY(*entry)) {

			MAKE_SPAGE_ENTRY(*entry, paddr);

			entry++;
			paddr += S5P_SPAGE_SIZE;
		}

		if (entry != end_entry) {
			pr_err("%s: Failed to allocate small page entry"
					" for IOVA %#lx.\n", __func__, iova);
			ret = -EADDRINUSE;

			goto mapping_error;
		}
	}

	pgtable_flush(start_entry, entry);
mapping_error:
	if (entry != end_entry) {
		unsigned long *current_entry = entry;
		while (entry != start_entry)
			MAKE_FAULT_ENTRY(*(--entry));
		pgtable_flush(start_entry, current_entry);
		ret = -EADDRINUSE;
	}

nomem_error:
mapping_done:
	mutex_unlock(&s5p_domain->lock);

	return ret;
}

static int s5p_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
			   int gfp_order)
{
	struct s5p_iommu_domain *s5p_domain = domain->priv;
	unsigned long *entry;
	int num_entry;

	BUG_ON(s5p_domain->pgtable == NULL);

	mutex_lock(&s5p_domain->lock);

	entry = s5p_domain->pgtable + (iova >> S5P_SECTION_SHIFT);

	if (gfp_order >= S5P_SECTION_ORDER) {
		num_entry = 1 << (gfp_order - S5P_SECTION_ORDER);
		while (num_entry--) {
			if (S5P_SECTION_LV1_ENTRY(*entry)) {
				MAKE_FAULT_ENTRY(*entry);
			} else if (S5P_PAGE_LV1_ENTRY(*entry)) {
				unsigned long *lv2beg, *lv2end;
				lv2beg = phys_to_virt(
						*entry & S5P_LV2TABLE_MASK);
				lv2end = lv2beg + S5P_LV2TABLE_ENTRIES;
				while (lv2beg != lv2end) {
					MAKE_FAULT_ENTRY(*lv2beg);
					lv2beg++;
				}
			}
			entry++;
		}
	} else {
		entry = GET_LV2ENTRY(*entry, iova);

		BUG_ON(S5P_LPAGE_LV2_ENTRY(*entry) &&
						(gfp_order < S5P_LPAGE_ORDER));

		num_entry = 1 << gfp_order;

		while (num_entry--) {
			MAKE_FAULT_ENTRY(*entry);
			entry++;
		}
	}

	mutex_unlock(&s5p_domain->lock);

	if (s5p_domain->dev)
		s5p_sysmmu_tlb_invalidate(s5p_domain->dev);

	return 0;
}

static phys_addr_t s5p_iommu_iova_to_phys(struct iommu_domain *domain,
					  unsigned long iova)
{
	struct s5p_iommu_domain *s5p_domain = domain->priv;
	unsigned long *entry;
	unsigned long offset;

	entry = s5p_domain->pgtable + (iova >> S5P_SECTION_SHIFT);

	if (S5P_FAULT_LV1_ENTRY(*entry))
		return 0;

	offset = iova & ~S5P_SECTION_MASK;

	if (S5P_SECTION_LV1_ENTRY(*entry))
		return (*entry & S5P_SECTION_MASK) + offset;

	entry = GET_LV2ENTRY(*entry, iova);

	if (S5P_SPAGE_LV2_ENTRY(*entry))
		return (*entry & S5P_SPAGE_MASK) + (iova & ~S5P_SPAGE_MASK);

	if (S5P_LPAGE_LV2_ENTRY(*entry))
		return (*entry & S5P_LPAGE_MASK) + (iova & ~S5P_LPAGE_MASK);

	return 0;
}

static int s5p_iommu_domain_has_cap(struct iommu_domain *domain,
				    unsigned long cap)
{
	return 0;
}

static struct iommu_ops s5p_iommu_ops = {
	.domain_init = &s5p_iommu_domain_init,
	.domain_destroy = &s5p_iommu_domain_destroy,
	.attach_dev = &s5p_iommu_attach_device,
	.detach_dev = &s5p_iommu_detach_device,
	.map = &s5p_iommu_map,
	.unmap = &s5p_iommu_unmap,
	.iova_to_phys = &s5p_iommu_iova_to_phys,
	.domain_has_cap = &s5p_iommu_domain_has_cap,
};

static int __init s5p_iommu_init(void)
{
	l2table_cachep = kmem_cache_create("SysMMU Lv2 Tables",
				S5P_LV2TABLE_SIZE, S5P_LV2TABLE_SIZE, 0, NULL);
	if (!l2table_cachep)
		return -ENOMEM;

	register_iommu(&s5p_iommu_ops);
	return 0;
}
arch_initcall(s5p_iommu_init);