1 /*
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 #ifndef __MACH_IOMMU_H
14 #define __MACH_IOMMU_H
15
16 struct iotlb_entry {
17 u32 da;
18 u32 pa;
19 u32 pgsz, prsvd, valid;
20 union {
21 u16 ap;
22 struct {
23 u32 endian, elsz, mixed;
24 };
25 };
26 };
27
28 struct omap_iommu {
29 const char *name;
30 struct module *owner;
31 struct clk *clk;
32 void __iomem *regbase;
33 struct device *dev;
34 void *isr_priv;
35 struct iommu_domain *domain;
36
37 unsigned int refcount;
38 spinlock_t iommu_lock; /* global for this whole object */
39
40 /*
41 * We don't change iopgd for a situation like pgd for a task,
42 * but share it globally for each iommu.
43 */
44 u32 *iopgd;
45 spinlock_t page_table_lock; /* protect iopgd */
46
47 int nr_tlb_entries;
48
49 struct list_head mmap;
50 struct mutex mmap_lock; /* protect mmap */
51
52 void *ctx; /* iommu context: registres saved area */
53 u32 da_start;
54 u32 da_end;
55 };
56
57 struct cr_regs {
58 union {
59 struct {
60 u16 cam_l;
61 u16 cam_h;
62 };
63 u32 cam;
64 };
65 union {
66 struct {
67 u16 ram_l;
68 u16 ram_h;
69 };
70 u32 ram;
71 };
72 };
73
74 struct iotlb_lock {
75 short base;
76 short vict;
77 };
78
79 /* architecture specific functions */
80 struct iommu_functions {
81 unsigned long version;
82
83 int (*enable)(struct omap_iommu *obj);
84 void (*disable)(struct omap_iommu *obj);
85 void (*set_twl)(struct omap_iommu *obj, bool on);
86 u32 (*fault_isr)(struct omap_iommu *obj, u32 *ra);
87
88 void (*tlb_read_cr)(struct omap_iommu *obj, struct cr_regs *cr);
89 void (*tlb_load_cr)(struct omap_iommu *obj, struct cr_regs *cr);
90
91 struct cr_regs *(*alloc_cr)(struct omap_iommu *obj,
92 struct iotlb_entry *e);
93 int (*cr_valid)(struct cr_regs *cr);
94 u32 (*cr_to_virt)(struct cr_regs *cr);
95 void (*cr_to_e)(struct cr_regs *cr, struct iotlb_entry *e);
96 ssize_t (*dump_cr)(struct omap_iommu *obj, struct cr_regs *cr,
97 char *buf);
98
99 u32 (*get_pte_attr)(struct iotlb_entry *e);
100
101 void (*save_ctx)(struct omap_iommu *obj);
102 void (*restore_ctx)(struct omap_iommu *obj);
103 ssize_t (*dump_ctx)(struct omap_iommu *obj, char *buf, ssize_t len);
104 };
105
106 struct iommu_platform_data {
107 const char *name;
108 const char *clk_name;
109 const int nr_tlb_entries;
110 u32 da_start;
111 u32 da_end;
112 };
113
114 /**
115 * struct iommu_arch_data - omap iommu private data
116 * @name: name of the iommu device
117 * @iommu_dev: handle of the iommu device
118 *
119 * This is an omap iommu private data object, which binds an iommu user
120 * to its iommu device. This object should be placed at the iommu user's
121 * dev_archdata so generic IOMMU API can be used without having to
122 * utilize omap-specific plumbing anymore.
123 */
124 struct omap_iommu_arch_data {
125 const char *name;
126 struct omap_iommu *iommu_dev;
127 };
128
129 /**
130 * dev_to_omap_iommu() - retrieves an omap iommu object from a user device
131 * @dev: iommu client device
132 */
dev_to_omap_iommu(struct device * dev)133 static inline struct omap_iommu *dev_to_omap_iommu(struct device *dev)
134 {
135 struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
136
137 return arch_data->iommu_dev;
138 }
139
140 /* IOMMU errors */
141 #define OMAP_IOMMU_ERR_TLB_MISS (1 << 0)
142 #define OMAP_IOMMU_ERR_TRANS_FAULT (1 << 1)
143 #define OMAP_IOMMU_ERR_EMU_MISS (1 << 2)
144 #define OMAP_IOMMU_ERR_TBLWALK_FAULT (1 << 3)
145 #define OMAP_IOMMU_ERR_MULTIHIT_FAULT (1 << 4)
146
147 #if defined(CONFIG_ARCH_OMAP1)
148 #error "iommu for this processor not implemented yet"
149 #else
150 #include <plat/iommu2.h>
151 #endif
152
153 /*
154 * utilities for super page(16MB, 1MB, 64KB and 4KB)
155 */
156
157 #define iopgsz_max(bytes) \
158 (((bytes) >= SZ_16M) ? SZ_16M : \
159 ((bytes) >= SZ_1M) ? SZ_1M : \
160 ((bytes) >= SZ_64K) ? SZ_64K : \
161 ((bytes) >= SZ_4K) ? SZ_4K : 0)
162
163 #define bytes_to_iopgsz(bytes) \
164 (((bytes) == SZ_16M) ? MMU_CAM_PGSZ_16M : \
165 ((bytes) == SZ_1M) ? MMU_CAM_PGSZ_1M : \
166 ((bytes) == SZ_64K) ? MMU_CAM_PGSZ_64K : \
167 ((bytes) == SZ_4K) ? MMU_CAM_PGSZ_4K : -1)
168
169 #define iopgsz_to_bytes(iopgsz) \
170 (((iopgsz) == MMU_CAM_PGSZ_16M) ? SZ_16M : \
171 ((iopgsz) == MMU_CAM_PGSZ_1M) ? SZ_1M : \
172 ((iopgsz) == MMU_CAM_PGSZ_64K) ? SZ_64K : \
173 ((iopgsz) == MMU_CAM_PGSZ_4K) ? SZ_4K : 0)
174
175 #define iopgsz_ok(bytes) (bytes_to_iopgsz(bytes) >= 0)
176
177 /*
178 * global functions
179 */
180 extern u32 omap_iommu_arch_version(void);
181
182 extern void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e);
183
184 extern int
185 omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e);
186
187 extern int omap_iommu_set_isr(const char *name,
188 int (*isr)(struct omap_iommu *obj, u32 da, u32 iommu_errs,
189 void *priv),
190 void *isr_priv);
191
192 extern void omap_iommu_save_ctx(struct device *dev);
193 extern void omap_iommu_restore_ctx(struct device *dev);
194
195 extern int omap_install_iommu_arch(const struct iommu_functions *ops);
196 extern void omap_uninstall_iommu_arch(const struct iommu_functions *ops);
197
198 extern int omap_foreach_iommu_device(void *data,
199 int (*fn)(struct device *, void *));
200
201 extern ssize_t
202 omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len);
203 extern size_t
204 omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t len);
205
206 #endif /* __MACH_IOMMU_H */
207