blob: 8c3378d99b81dfaff29a4d0faf67d3303ae93949 [file] [log] [blame]
Tony Lindgrened1c7de2012-11-02 12:24:06 -07001/*
2 * omap iommu: main structures
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#if defined(CONFIG_ARCH_OMAP1)
14#error "iommu for this processor not implemented yet"
15#endif
16
17struct iotlb_entry {
18 u32 da;
19 u32 pa;
20 u32 pgsz, prsvd, valid;
21 union {
22 u16 ap;
23 struct {
24 u32 endian, elsz, mixed;
25 };
26 };
27};
28
29struct omap_iommu {
30 const char *name;
31 struct module *owner;
32 struct clk *clk;
33 void __iomem *regbase;
34 struct device *dev;
35 void *isr_priv;
36 struct iommu_domain *domain;
37
38 unsigned int refcount;
39 spinlock_t iommu_lock; /* global for this whole object */
40
41 /*
42 * We don't change iopgd for a situation like pgd for a task,
43 * but share it globally for each iommu.
44 */
45 u32 *iopgd;
46 spinlock_t page_table_lock; /* protect iopgd */
47
48 int nr_tlb_entries;
49
50 struct list_head mmap;
51 struct mutex mmap_lock; /* protect mmap */
52
53 void *ctx; /* iommu context: registres saved area */
54 u32 da_start;
55 u32 da_end;
56};
57
58struct cr_regs {
59 union {
60 struct {
61 u16 cam_l;
62 u16 cam_h;
63 };
64 u32 cam;
65 };
66 union {
67 struct {
68 u16 ram_l;
69 u16 ram_h;
70 };
71 u32 ram;
72 };
73};
74
75struct iotlb_lock {
76 short base;
77 short vict;
78};
79
80/* architecture specific functions */
81struct iommu_functions {
82 unsigned long version;
83
84 int (*enable)(struct omap_iommu *obj);
85 void (*disable)(struct omap_iommu *obj);
86 void (*set_twl)(struct omap_iommu *obj, bool on);
87 u32 (*fault_isr)(struct omap_iommu *obj, u32 *ra);
88
89 void (*tlb_read_cr)(struct omap_iommu *obj, struct cr_regs *cr);
90 void (*tlb_load_cr)(struct omap_iommu *obj, struct cr_regs *cr);
91
92 struct cr_regs *(*alloc_cr)(struct omap_iommu *obj,
93 struct iotlb_entry *e);
94 int (*cr_valid)(struct cr_regs *cr);
95 u32 (*cr_to_virt)(struct cr_regs *cr);
96 void (*cr_to_e)(struct cr_regs *cr, struct iotlb_entry *e);
97 ssize_t (*dump_cr)(struct omap_iommu *obj, struct cr_regs *cr,
98 char *buf);
99
100 u32 (*get_pte_attr)(struct iotlb_entry *e);
101
102 void (*save_ctx)(struct omap_iommu *obj);
103 void (*restore_ctx)(struct omap_iommu *obj);
104 ssize_t (*dump_ctx)(struct omap_iommu *obj, char *buf, ssize_t len);
105};
106
107#ifdef CONFIG_IOMMU_API
108/**
109 * dev_to_omap_iommu() - retrieves an omap iommu object from a user device
110 * @dev: iommu client device
111 */
112static inline struct omap_iommu *dev_to_omap_iommu(struct device *dev)
113{
114 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
115
116 return arch_data->iommu_dev;
117}
118#endif
119
120/* IOMMU errors */
121#define OMAP_IOMMU_ERR_TLB_MISS (1 << 0)
122#define OMAP_IOMMU_ERR_TRANS_FAULT (1 << 1)
123#define OMAP_IOMMU_ERR_EMU_MISS (1 << 2)
124#define OMAP_IOMMU_ERR_TBLWALK_FAULT (1 << 3)
125#define OMAP_IOMMU_ERR_MULTIHIT_FAULT (1 << 4)
126
127/*
128 * MMU Register offsets
129 */
130#define MMU_REVISION 0x00
131#define MMU_SYSCONFIG 0x10
132#define MMU_SYSSTATUS 0x14
133#define MMU_IRQSTATUS 0x18
134#define MMU_IRQENABLE 0x1c
135#define MMU_WALKING_ST 0x40
136#define MMU_CNTL 0x44
137#define MMU_FAULT_AD 0x48
138#define MMU_TTB 0x4c
139#define MMU_LOCK 0x50
140#define MMU_LD_TLB 0x54
141#define MMU_CAM 0x58
142#define MMU_RAM 0x5c
143#define MMU_GFLUSH 0x60
144#define MMU_FLUSH_ENTRY 0x64
145#define MMU_READ_CAM 0x68
146#define MMU_READ_RAM 0x6c
147#define MMU_EMU_FAULT_AD 0x70
148
149#define MMU_REG_SIZE 256
150
151/*
152 * MMU Register bit definitions
153 */
154#define MMU_LOCK_BASE_SHIFT 10
155#define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
156#define MMU_LOCK_BASE(x) \
157 ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
158
159#define MMU_LOCK_VICT_SHIFT 4
160#define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
161#define MMU_LOCK_VICT(x) \
162 ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
163
164#define MMU_CAM_VATAG_SHIFT 12
165#define MMU_CAM_VATAG_MASK \
166 ((~0UL >> MMU_CAM_VATAG_SHIFT) << MMU_CAM_VATAG_SHIFT)
167#define MMU_CAM_P (1 << 3)
168#define MMU_CAM_V (1 << 2)
169#define MMU_CAM_PGSZ_MASK 3
170#define MMU_CAM_PGSZ_1M (0 << 0)
171#define MMU_CAM_PGSZ_64K (1 << 0)
172#define MMU_CAM_PGSZ_4K (2 << 0)
173#define MMU_CAM_PGSZ_16M (3 << 0)
174
175#define MMU_RAM_PADDR_SHIFT 12
176#define MMU_RAM_PADDR_MASK \
177 ((~0UL >> MMU_RAM_PADDR_SHIFT) << MMU_RAM_PADDR_SHIFT)
178
179#define MMU_RAM_ENDIAN_MASK (1 << MMU_RAM_ENDIAN_SHIFT)
180#define MMU_RAM_ENDIAN_BIG (1 << MMU_RAM_ENDIAN_SHIFT)
181
182#define MMU_RAM_ELSZ_MASK (3 << MMU_RAM_ELSZ_SHIFT)
183#define MMU_RAM_ELSZ_8 (0 << MMU_RAM_ELSZ_SHIFT)
184#define MMU_RAM_ELSZ_16 (1 << MMU_RAM_ELSZ_SHIFT)
185#define MMU_RAM_ELSZ_32 (2 << MMU_RAM_ELSZ_SHIFT)
186#define MMU_RAM_ELSZ_NONE (3 << MMU_RAM_ELSZ_SHIFT)
187#define MMU_RAM_MIXED_SHIFT 6
188#define MMU_RAM_MIXED_MASK (1 << MMU_RAM_MIXED_SHIFT)
189#define MMU_RAM_MIXED MMU_RAM_MIXED_MASK
190
191/*
192 * utilities for super page(16MB, 1MB, 64KB and 4KB)
193 */
194
195#define iopgsz_max(bytes) \
196 (((bytes) >= SZ_16M) ? SZ_16M : \
197 ((bytes) >= SZ_1M) ? SZ_1M : \
198 ((bytes) >= SZ_64K) ? SZ_64K : \
199 ((bytes) >= SZ_4K) ? SZ_4K : 0)
200
201#define bytes_to_iopgsz(bytes) \
202 (((bytes) == SZ_16M) ? MMU_CAM_PGSZ_16M : \
203 ((bytes) == SZ_1M) ? MMU_CAM_PGSZ_1M : \
204 ((bytes) == SZ_64K) ? MMU_CAM_PGSZ_64K : \
205 ((bytes) == SZ_4K) ? MMU_CAM_PGSZ_4K : -1)
206
207#define iopgsz_to_bytes(iopgsz) \
208 (((iopgsz) == MMU_CAM_PGSZ_16M) ? SZ_16M : \
209 ((iopgsz) == MMU_CAM_PGSZ_1M) ? SZ_1M : \
210 ((iopgsz) == MMU_CAM_PGSZ_64K) ? SZ_64K : \
211 ((iopgsz) == MMU_CAM_PGSZ_4K) ? SZ_4K : 0)
212
213#define iopgsz_ok(bytes) (bytes_to_iopgsz(bytes) >= 0)
214
215/*
216 * global functions
217 */
218extern u32 omap_iommu_arch_version(void);
219
220extern void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e);
221
222extern int
223omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e);
224
225extern int omap_iommu_set_isr(const char *name,
226 int (*isr)(struct omap_iommu *obj, u32 da, u32 iommu_errs,
227 void *priv),
228 void *isr_priv);
229
230extern void omap_iommu_save_ctx(struct device *dev);
231extern void omap_iommu_restore_ctx(struct device *dev);
232
233extern int omap_install_iommu_arch(const struct iommu_functions *ops);
234extern void omap_uninstall_iommu_arch(const struct iommu_functions *ops);
235
236extern int omap_foreach_iommu_device(void *data,
237 int (*fn)(struct device *, void *));
238
239extern ssize_t
240omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len);
241extern size_t
242omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t len);
243
244/*
245 * register accessors
246 */
247static inline u32 iommu_read_reg(struct omap_iommu *obj, size_t offs)
248{
249 return __raw_readl(obj->regbase + offs);
250}
251
252static inline void iommu_write_reg(struct omap_iommu *obj, u32 val, size_t offs)
253{
254 __raw_writel(val, obj->regbase + offs);
255}