summaryrefstree
path: root/kernel/include/linux/exynos_iovmm.h
blob: e93a0235814bc19dc0b698a81146aed1a191d447 (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
/* /include/linux/exynos_iovmm.h
 *
 * 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.
 */

#ifndef __ASM_PLAT_IOVMM_H
#define __ASM_PLAT_IOVMM_H

#include <linux/dma-direction.h>
#include <linux/iommu.h>

#include <asm/page.h>
#include <linux/mm_types.h>

#define IOMMU_PFNMAP	(1 << 5) /* VM_PFNMAP is set */

struct scatterlist;
struct device;

typedef u32 exynos_iova_t;

#define SYSMMU_FAULT_BITS	4
#define SYSMMU_FAULT_SHIFT	16
#define SYSMMU_FAULT_MASK	((1 << SYSMMU_FAULT_BITS) - 1)
#define SYSMMU_FAULT_FLAG(id) (((id) & SYSMMU_FAULT_MASK) << SYSMMU_FAULT_SHIFT)
#define SYSMMU_FAULT_ID(fg)   (((fg) >> SYSMMU_FAULT_SHIFT) & SYSMMU_FAULT_MASK)

#define SYSMMU_FAULT_PTW_ACCESS   0
#define SYSMMU_FAULT_PAGE_FAULT   1
#define SYSMMU_FAULT_TLB_MULTIHIT 2
#define SYSMMU_FAULT_ACCESS       3
#define SYSMMU_FAULT_SECURITY     4
#define SYSMMU_FAULT_UNKNOWN      5

#define IOMMU_FAULT_EXYNOS_PTW_ACCESS SYSMMU_FAULT_FLAG(SYSMMU_FAULT_PTW_ACCESS)
#define IOMMU_FAULT_EXYNOS_PAGE_FAULT SYSMMU_FAULT_FLAG(SYSMMU_FAULT_PAGE_FAULT)
#define IOMMU_FAULT_EXYNOS_TLB_MULTIHIT \
				SYSMMU_FAULT_FLAG(SYSMMU_FAULT_TLB_MULTIHIT)
#define IOMMU_FAULT_EXYNOS_ACCESS     SYSMMU_FAULT_FLAG(SYSMMU_FAULT_ACCESS)
#define IOMMU_FAULT_EXYNOS_SECURITY   SYSMMU_FAULT_FLAG(SYSMMU_FAULT_SECURITY)
#define IOMMU_FAULT_EXYNOS_UNKNOWN    SYSMMU_FAULT_FLAG(SYSMMU_FAULT_UNKOWN)

/* TODO: PB related for sysmmu v6, remove it later */
#define SYSMMU_PBUFCFG_TLB_UPDATE      (1 << 16)
#define SYSMMU_PBUFCFG_ASCENDING       (1 << 12)
#define SYSMMU_PBUFCFG_DESCENDING      (0 << 12)
#define SYSMMU_PBUFCFG_PREFETCH                (1 << 8)
#define SYSMMU_PBUFCFG_WRITE           (1 << 4)
#define SYSMMU_PBUFCFG_READ            (0 << 4)

#define SYSMMU_PBUFCFG_DEFAULT_INPUT   (SYSMMU_PBUFCFG_TLB_UPDATE | \
					SYSMMU_PBUFCFG_ASCENDING |  \
					SYSMMU_PBUFCFG_PREFETCH |   \
					SYSMMU_PBUFCFG_READ)
#define SYSMMU_PBUFCFG_DEFAULT_OUTPUT  (SYSMMU_PBUFCFG_TLB_UPDATE | \
					SYSMMU_PBUFCFG_ASCENDING |  \
					SYSMMU_PBUFCFG_PREFETCH |   \
					SYSMMU_PBUFCFG_WRITE)

#define SYSMMU_PBUFCFG_ASCENDING_INPUT   (SYSMMU_PBUFCFG_TLB_UPDATE | \
					SYSMMU_PBUFCFG_ASCENDING |  \
					SYSMMU_PBUFCFG_PREFETCH |   \
					SYSMMU_PBUFCFG_READ)

#define SYSMMU_PBUFCFG_DESCENDING_INPUT   (SYSMMU_PBUFCFG_TLB_UPDATE | \
					SYSMMU_PBUFCFG_DESCENDING |  \
					SYSMMU_PBUFCFG_PREFETCH |   \
					SYSMMU_PBUFCFG_READ)
/* SYSMMU PPC Event ID */
enum sysmmu_ppc_event {
	READ_TOTAL,
	READ_L1TLB_MISS,
	READ_L2TLB_MISS,
	READ_FLPD_MISS,
	READ_PB_LOOKUP,
	READ_PB_MISS,
	READ_BLOCK_NUM_BY_PREFETCH,
	READ_BLOCK_CYCLE_BRY_PREFETCH,
	READ_TLB_MISS,
	READ_FLPD_MISS_PREFETCH,
	WRITE_TOTAL = 0x10,
	WRITE_L1TLB_MISS,
	WRITE_L2TLB_MISS,
	WRITE_FLPD_MISS,
	WRITE_PB_LOOKUP,
	WRITE_PB_MISS,
	WRITE_BLOCK_NUM_BY_PREFETCH,
	WRITE_BLOCK_CYCLE_BY_PREFETCH,
	WRITE_TLB_MISS,
	WRITE_FLPD_MISS_PREFETCH,
	TOTAL_ID_NUM,
};

struct sysmmu_prefbuf {
	unsigned long base;
	unsigned long size;
	unsigned long config;
};

/* Fault information of SysMMU */
#define SYSMMU_FI_VALID_PGTABLE		(1 << 0)
#define SYSMMU_FI_VALID_TLB		(1 << 1)
#define SYSMMU_FI_VALID_SBB		(1 << 2)

struct _sysmmu_fi {
	struct device *sysmmu;
	bool is_secure;
	bool has_lv2;
	u32 fault_addr;
	u32 lv1_entry;
	u32 lv2_entry;
	u32 tlb_ppn;
	u32 tlb_attr;
	u32 sbb_link;
	u32 sbb_attr;
	u32 flags;
	u32 map_start;
	u32 map_end;
	u32 unmap_start;
	u32 unmap_end;
	struct timeval fault_time;
	struct timeval map_time;
	struct timeval unmap_time;
};

#if defined(CONFIG_EXYNOS_IOVMM)
int iovmm_activate(struct device *dev);
void iovmm_deactivate(struct device *dev);
struct iommu_domain *get_domain_from_dev(struct device *dev);

/* iovmm_map() - Maps a list of physical memory chunks
 * @dev: the owner of the IO address space where the mapping is created
 * @sg: list of physical memory chunks to map
 * @offset: length in bytes where the mapping starts
 * @size: how much memory to map in bytes. @offset + @size must not exceed
 *        total size of @sg
 * @direction: dma data direction for iova
 * @prot: iommu mapping property
 *
 * This function returns mapped IO address in the address space of @dev.
 * Returns minus error number if mapping fails.
 * Caller must check its return code with IS_ERROR_VALUE() if the function
 * succeeded.
 *
 * The caller of this function must ensure that iovmm_cleanup() is not called
 * while this function is called.
 *
 */
dma_addr_t iovmm_map(struct device *dev, struct scatterlist *sg, off_t offset,
		size_t size, enum dma_data_direction direction, int prot);

/* iovmm_unmap() - unmaps the given IO address
 * @dev: the owner of the IO address space where @iova belongs
 * @iova: IO address that needs to be unmapped and freed.
 *
 * The caller of this function must ensure that iovmm_cleanup() is not called
 * while this function is called.
 */
void iovmm_unmap(struct device *dev, dma_addr_t iova);

/*
 * flags to option_iplanes and option_oplanes.
 * inplanes and onplanes is 'input planes' and 'output planes', respectively.
 *
 * default value to option_iplanes:
 *    (TLB_UPDATE | ASCENDING | PREFETCH)
 * default value to option_oplanes:
 *    (TLB_UPDATE | ASCENDING | PREFETCH | WRITE)
 *
 * SYSMMU_PBUFCFG_READ and SYSMMU_PBUFCFG_WRITE are ignored because they are
 * implicitly set from 'inplanes' and 'onplanes' arguments to
 * iovmm_set_prefetch_buffer().
 *
 * Guide to setting flags:
 * - Clear SYSMMU_BUFCFG_TLB_UPDATE if a buffer is accessed by the device
 *   for rotation.
 * - Set SYSMMU_PBUFCFG_DESCENDING if the device access a buffer in reversed
 *   order
 * - Clear SYSMMU_PBUFCFG_PREFETCH if access to a buffer has poor locality.
 * - Otherwise, always set flags as default value.
 */
#else
#define iovmm_activate(dev)		(-ENOSYS)
#define iovmm_deactivate(dev)		do { } while (0)
#define iovmm_map(dev, sg, offset, size, direction, prot) (-ENOSYS)
#define iovmm_unmap(dev, iova)		do { } while (0)
#define get_domain_from_dev(dev)	NULL
static inline dma_addr_t exynos_iovmm_map_userptr(struct device *dev,
			unsigned long vaddr, size_t size, int prot)
{
	return 0;
}
#define exynos_iovmm_unmap_userptr(dev, iova) do { } while (0)

#endif /* CONFIG_EXYNOS_IOVMM */

#if defined(CONFIG_EXYNOS_IOMMU)
/**
 * exynos_sysmmu_map_user_pages() - maps all pages by fetching from
 * user page table entries.
 * @dev: The device whose System MMU is about to be disabled.
 * @mm: mm struct of user requested to map
 * @vaddr: start vaddr in valid vma
 * @iova: start io vaddr to be mapped
 * @size: size to map
 * @write: set if buffer may be written
 * @shareable: set shareable bit if true
 *
 * This function maps all user pages into sysmmu page table.
 */
int exynos_sysmmu_map_user_pages(struct device *dev,
					struct mm_struct *mm,
					unsigned long vaddr,
					exynos_iova_t iova,
					size_t size, bool write,
					bool shareable);

/**
 * exynos_sysmmu_unmap_user_pages() - unmaps all mapped pages
 * @dev: The device whose System MMU is about to be disabled.
 * @mm: mm struct of user requested to map
 * @vaddr: start vaddr in valid vma
 * @iova: start io vaddr to be unmapped
 * @size: size to map
 *
 * This function unmaps all user pages mapped in sysmmu page table.
 */
int exynos_sysmmu_unmap_user_pages(struct device *dev,
					struct mm_struct *mm,
					unsigned long vaddr,
					exynos_iova_t iova,
					size_t size);
/**
 * exynos_iommu_sync_for_device()
 *			- maintain cache lines on the given area before DMA
 * @dev:	The device that is about to see the area
 * @iova:	The start DMA address of @dev to maintain
 * @len:	The length of the area
 * @dir:	Indicate whether @dev read from or write to the area
 */
void exynos_iommu_sync_for_device(struct device *dev, dma_addr_t iova,
				  size_t len, enum dma_data_direction dir);

/**
 * exynos_iommu_sync_for_cpu()
 *			- maintain cache lines on the given area after DMA
 * @dev:	The device that is about to see the area
 * @iova:	The start DMA address of @dev to maintain
 * @len:	The length of the area
 * @dir:	Indicate whether @dev read from or write to the area
 */
void exynos_iommu_sync_for_cpu(struct device *dev, dma_addr_t iova, size_t len,
				enum dma_data_direction dir);

/**
 * TODO: description
 */
dma_addr_t exynos_iovmm_map_userptr(struct device *dev, unsigned long vaddr,
				    size_t size, int prot);
/**
 * TODO: description
 */
void exynos_iovmm_unmap_userptr(struct device *dev, dma_addr_t iova);

/*
 * The handle_pte_fault() is called by exynos_sysmmu_map_user_pages().
 * Driver cannot include include/linux/huge_mm.h because
 * CONFIG_TRANSPARENT_HUGEPAGE is disabled.
 */
extern int handle_pte_fault(struct mm_struct *mm,
			    struct vm_area_struct *vma, unsigned long address,
			    pte_t *pte, pmd_t *pmd, unsigned int flags);

/*
 * sysmmu_set_prefetch_buffer_by_region() - set prefetch buffer configuration
 *
 * @dev: device descriptor of master device
 * @pb_reg: array of regions where prefetch buffer contains.
 *
 * If @dev is NULL or @pb_reg is 0, prefetch buffers is disabled.
 *
 */
void sysmmu_set_prefetch_buffer_by_region(struct device *dev,
			struct sysmmu_prefbuf pb_reg[], unsigned int num_reg);

int sysmmu_set_prefetch_buffer_property(struct device *dev,
			unsigned int inplanes, unsigned int onplanes,
			unsigned int ipoption[], unsigned int opoption[]);
void exynos_sysmmu_show_status(struct device *dev);
void exynos_sysmmu_dump_pgtable(struct device *dev);

/*
 * exynos_sysmmu_set/clear/show_ppc_event() -
 *		set/clear/show system mmu ppc event
 *
 * @dev: device descriptor of master device.
 * @event: system mmu ppc event id.
 * Returns 0 if setting is successful. -EINVAL if the argument is invalid.
 *
 */
int exynos_sysmmu_set_ppc_event(struct device *dev, int event);
void exynos_sysmmu_clear_ppc_event(struct device *dev);
void exynos_sysmmu_show_ppc_event(struct device *dev);

/*
 * iovmm_set_fault_handler - register fault handler of dev to iommu controller
 * @dev: the device that wants to register fault handler
 * @handler: fault handler
 * @token: any data the device driver needs to get when fault occurred
 */
void iovmm_set_fault_handler(struct device *dev,
			     iommu_fault_handler_t handler, void *token);
bool iovmm_address_verifier(struct device *dev,
				dma_addr_t iova, struct _sysmmu_fi *fi);
#else
#define sysmmu_set_prefetch_buffer_property(dev, inplanes, onplnes, ipoption, opoption) \
					(0)
#define sysmmu_set_prefetch_buffer_by_region(dev, pb_reg, num_reg) \
					do { } while (0)
#define exynos_sysmmu_map_user_pages(dev, mm, vaddr, iova, size, write, sharable) \
					(-ENOSYS)
#define exynos_sysmmu_unmap_user_pages(dev, mm, vaddr, iova, size) \
					do { } while (0)
#define exynos_sysmmu_show_status(dev) do { } while (0)
#define exynos_sysmmu_dump_pgtable(dev) do { } while (0)

#define exynos_sysmmu_clear_ppc_event(dev) do { } while (0)
#define exynos_sysmmu_show_ppc_event(dev) do { } while (0)
#define exynos_sysmmu_set_ppc_event(dev, event) do { } while (0)
#define iovmm_set_fault_handler(dev, handler, token) do { } while(0)

#define exynos_iommu_sync_for_device(dev, iova, len, dir) do { } while (0)
#define exynos_iommu_sync_for_cpu(dev, iova, len, dir) do { } while (0)

#endif
#endif /*__ASM_PLAT_IOVMM_H*/