]>
Commit | Line | Data |
---|---|---|
a9dcad5e HD |
1 | /* |
2 | * omap iommu: tlb and pagetable primitives | |
3 | * | |
c127c7dc | 4 | * Copyright (C) 2008-2010 Nokia Corporation |
a9dcad5e HD |
5 | * |
6 | * Written by Hiroshi DOYU <[email protected]>, | |
7 | * Paul Mundt and Toshihiro Kobayashi | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/err.h> | |
15 | #include <linux/module.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
a9dcad5e HD |
17 | #include <linux/interrupt.h> |
18 | #include <linux/ioport.h> | |
19 | #include <linux/clk.h> | |
20 | #include <linux/platform_device.h> | |
f626b52d | 21 | #include <linux/iommu.h> |
c8d35c84 | 22 | #include <linux/omap-iommu.h> |
f626b52d OBC |
23 | #include <linux/mutex.h> |
24 | #include <linux/spinlock.h> | |
a9dcad5e HD |
25 | |
26 | #include <asm/cacheflush.h> | |
27 | ||
ce491cf8 | 28 | #include <plat/iommu.h> |
a9dcad5e | 29 | |
2f7702af | 30 | #include "omap-iopgtable.h" |
a9dcad5e | 31 | |
37c2836c HD |
32 | #define for_each_iotlb_cr(obj, n, __i, cr) \ |
33 | for (__i = 0; \ | |
34 | (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \ | |
35 | __i++) | |
36 | ||
66bc8cf3 OBC |
37 | /* bitmap of the page sizes currently supported */ |
38 | #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) | |
39 | ||
f626b52d OBC |
40 | /** |
41 | * struct omap_iommu_domain - omap iommu domain | |
42 | * @pgtable: the page table | |
43 | * @iommu_dev: an omap iommu device attached to this domain. only a single | |
44 | * iommu device can be attached for now. | |
803b5277 | 45 | * @dev: Device using this domain. |
f626b52d OBC |
46 | * @lock: domain lock, should be taken when attaching/detaching |
47 | */ | |
48 | struct omap_iommu_domain { | |
49 | u32 *pgtable; | |
6c32df43 | 50 | struct omap_iommu *iommu_dev; |
803b5277 | 51 | struct device *dev; |
f626b52d OBC |
52 | spinlock_t lock; |
53 | }; | |
54 | ||
a9dcad5e HD |
55 | /* accommodate the difference between omap1 and omap2/3 */ |
56 | static const struct iommu_functions *arch_iommu; | |
57 | ||
58 | static struct platform_driver omap_iommu_driver; | |
59 | static struct kmem_cache *iopte_cachep; | |
60 | ||
61 | /** | |
6c32df43 | 62 | * omap_install_iommu_arch - Install archtecure specific iommu functions |
a9dcad5e HD |
63 | * @ops: a pointer to architecture specific iommu functions |
64 | * | |
65 | * There are several kind of iommu algorithm(tlb, pagetable) among | |
66 | * omap series. This interface installs such an iommu algorighm. | |
67 | **/ | |
6c32df43 | 68 | int omap_install_iommu_arch(const struct iommu_functions *ops) |
a9dcad5e HD |
69 | { |
70 | if (arch_iommu) | |
71 | return -EBUSY; | |
72 | ||
73 | arch_iommu = ops; | |
74 | return 0; | |
75 | } | |
6c32df43 | 76 | EXPORT_SYMBOL_GPL(omap_install_iommu_arch); |
a9dcad5e HD |
77 | |
78 | /** | |
6c32df43 | 79 | * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions |
a9dcad5e HD |
80 | * @ops: a pointer to architecture specific iommu functions |
81 | * | |
82 | * This interface uninstalls the iommu algorighm installed previously. | |
83 | **/ | |
6c32df43 | 84 | void omap_uninstall_iommu_arch(const struct iommu_functions *ops) |
a9dcad5e HD |
85 | { |
86 | if (arch_iommu != ops) | |
87 | pr_err("%s: not your arch\n", __func__); | |
88 | ||
89 | arch_iommu = NULL; | |
90 | } | |
6c32df43 | 91 | EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch); |
a9dcad5e HD |
92 | |
93 | /** | |
6c32df43 | 94 | * omap_iommu_save_ctx - Save registers for pm off-mode support |
fabdbca8 | 95 | * @dev: client device |
a9dcad5e | 96 | **/ |
fabdbca8 | 97 | void omap_iommu_save_ctx(struct device *dev) |
a9dcad5e | 98 | { |
fabdbca8 OBC |
99 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
100 | ||
a9dcad5e HD |
101 | arch_iommu->save_ctx(obj); |
102 | } | |
6c32df43 | 103 | EXPORT_SYMBOL_GPL(omap_iommu_save_ctx); |
a9dcad5e HD |
104 | |
105 | /** | |
6c32df43 | 106 | * omap_iommu_restore_ctx - Restore registers for pm off-mode support |
fabdbca8 | 107 | * @dev: client device |
a9dcad5e | 108 | **/ |
fabdbca8 | 109 | void omap_iommu_restore_ctx(struct device *dev) |
a9dcad5e | 110 | { |
fabdbca8 OBC |
111 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
112 | ||
a9dcad5e HD |
113 | arch_iommu->restore_ctx(obj); |
114 | } | |
6c32df43 | 115 | EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx); |
a9dcad5e HD |
116 | |
117 | /** | |
6c32df43 | 118 | * omap_iommu_arch_version - Return running iommu arch version |
a9dcad5e | 119 | **/ |
6c32df43 | 120 | u32 omap_iommu_arch_version(void) |
a9dcad5e HD |
121 | { |
122 | return arch_iommu->version; | |
123 | } | |
6c32df43 | 124 | EXPORT_SYMBOL_GPL(omap_iommu_arch_version); |
a9dcad5e | 125 | |
6c32df43 | 126 | static int iommu_enable(struct omap_iommu *obj) |
a9dcad5e HD |
127 | { |
128 | int err; | |
129 | ||
130 | if (!obj) | |
131 | return -EINVAL; | |
132 | ||
ef4815ab MH |
133 | if (!arch_iommu) |
134 | return -ENODEV; | |
135 | ||
a9dcad5e HD |
136 | clk_enable(obj->clk); |
137 | ||
138 | err = arch_iommu->enable(obj); | |
139 | ||
140 | clk_disable(obj->clk); | |
141 | return err; | |
142 | } | |
143 | ||
6c32df43 | 144 | static void iommu_disable(struct omap_iommu *obj) |
a9dcad5e HD |
145 | { |
146 | if (!obj) | |
147 | return; | |
148 | ||
149 | clk_enable(obj->clk); | |
150 | ||
151 | arch_iommu->disable(obj); | |
152 | ||
153 | clk_disable(obj->clk); | |
154 | } | |
155 | ||
156 | /* | |
157 | * TLB operations | |
158 | */ | |
6c32df43 | 159 | void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e) |
a9dcad5e HD |
160 | { |
161 | BUG_ON(!cr || !e); | |
162 | ||
163 | arch_iommu->cr_to_e(cr, e); | |
164 | } | |
6c32df43 | 165 | EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e); |
a9dcad5e HD |
166 | |
167 | static inline int iotlb_cr_valid(struct cr_regs *cr) | |
168 | { | |
169 | if (!cr) | |
170 | return -EINVAL; | |
171 | ||
172 | return arch_iommu->cr_valid(cr); | |
173 | } | |
174 | ||
6c32df43 | 175 | static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj, |
a9dcad5e HD |
176 | struct iotlb_entry *e) |
177 | { | |
178 | if (!e) | |
179 | return NULL; | |
180 | ||
181 | return arch_iommu->alloc_cr(obj, e); | |
182 | } | |
183 | ||
e1f23813 | 184 | static u32 iotlb_cr_to_virt(struct cr_regs *cr) |
a9dcad5e HD |
185 | { |
186 | return arch_iommu->cr_to_virt(cr); | |
187 | } | |
a9dcad5e HD |
188 | |
189 | static u32 get_iopte_attr(struct iotlb_entry *e) | |
190 | { | |
191 | return arch_iommu->get_pte_attr(e); | |
192 | } | |
193 | ||
6c32df43 | 194 | static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da) |
a9dcad5e HD |
195 | { |
196 | return arch_iommu->fault_isr(obj, da); | |
197 | } | |
198 | ||
6c32df43 | 199 | static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l) |
a9dcad5e HD |
200 | { |
201 | u32 val; | |
202 | ||
203 | val = iommu_read_reg(obj, MMU_LOCK); | |
204 | ||
205 | l->base = MMU_LOCK_BASE(val); | |
206 | l->vict = MMU_LOCK_VICT(val); | |
207 | ||
a9dcad5e HD |
208 | } |
209 | ||
6c32df43 | 210 | static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l) |
a9dcad5e HD |
211 | { |
212 | u32 val; | |
213 | ||
a9dcad5e HD |
214 | val = (l->base << MMU_LOCK_BASE_SHIFT); |
215 | val |= (l->vict << MMU_LOCK_VICT_SHIFT); | |
216 | ||
217 | iommu_write_reg(obj, val, MMU_LOCK); | |
218 | } | |
219 | ||
6c32df43 | 220 | static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr) |
a9dcad5e HD |
221 | { |
222 | arch_iommu->tlb_read_cr(obj, cr); | |
223 | } | |
224 | ||
6c32df43 | 225 | static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr) |
a9dcad5e HD |
226 | { |
227 | arch_iommu->tlb_load_cr(obj, cr); | |
228 | ||
229 | iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY); | |
230 | iommu_write_reg(obj, 1, MMU_LD_TLB); | |
231 | } | |
232 | ||
233 | /** | |
234 | * iotlb_dump_cr - Dump an iommu tlb entry into buf | |
235 | * @obj: target iommu | |
236 | * @cr: contents of cam and ram register | |
237 | * @buf: output buffer | |
238 | **/ | |
6c32df43 | 239 | static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr, |
a9dcad5e HD |
240 | char *buf) |
241 | { | |
242 | BUG_ON(!cr || !buf); | |
243 | ||
244 | return arch_iommu->dump_cr(obj, cr, buf); | |
245 | } | |
246 | ||
37c2836c | 247 | /* only used in iotlb iteration for-loop */ |
6c32df43 | 248 | static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n) |
37c2836c HD |
249 | { |
250 | struct cr_regs cr; | |
251 | struct iotlb_lock l; | |
252 | ||
253 | iotlb_lock_get(obj, &l); | |
254 | l.vict = n; | |
255 | iotlb_lock_set(obj, &l); | |
256 | iotlb_read_cr(obj, &cr); | |
257 | ||
258 | return cr; | |
259 | } | |
260 | ||
a9dcad5e HD |
261 | /** |
262 | * load_iotlb_entry - Set an iommu tlb entry | |
263 | * @obj: target iommu | |
264 | * @e: an iommu tlb entry info | |
265 | **/ | |
5da14a47 | 266 | #ifdef PREFETCH_IOTLB |
6c32df43 | 267 | static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
a9dcad5e | 268 | { |
a9dcad5e HD |
269 | int err = 0; |
270 | struct iotlb_lock l; | |
271 | struct cr_regs *cr; | |
272 | ||
273 | if (!obj || !obj->nr_tlb_entries || !e) | |
274 | return -EINVAL; | |
275 | ||
276 | clk_enable(obj->clk); | |
277 | ||
be6d8026 KH |
278 | iotlb_lock_get(obj, &l); |
279 | if (l.base == obj->nr_tlb_entries) { | |
280 | dev_warn(obj->dev, "%s: preserve entries full\n", __func__); | |
a9dcad5e HD |
281 | err = -EBUSY; |
282 | goto out; | |
283 | } | |
be6d8026 | 284 | if (!e->prsvd) { |
37c2836c HD |
285 | int i; |
286 | struct cr_regs tmp; | |
be6d8026 | 287 | |
37c2836c | 288 | for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp) |
be6d8026 KH |
289 | if (!iotlb_cr_valid(&tmp)) |
290 | break; | |
37c2836c | 291 | |
be6d8026 KH |
292 | if (i == obj->nr_tlb_entries) { |
293 | dev_dbg(obj->dev, "%s: full: no entry\n", __func__); | |
294 | err = -EBUSY; | |
295 | goto out; | |
296 | } | |
37c2836c HD |
297 | |
298 | iotlb_lock_get(obj, &l); | |
be6d8026 KH |
299 | } else { |
300 | l.vict = l.base; | |
301 | iotlb_lock_set(obj, &l); | |
302 | } | |
a9dcad5e HD |
303 | |
304 | cr = iotlb_alloc_cr(obj, e); | |
305 | if (IS_ERR(cr)) { | |
306 | clk_disable(obj->clk); | |
307 | return PTR_ERR(cr); | |
308 | } | |
309 | ||
310 | iotlb_load_cr(obj, cr); | |
311 | kfree(cr); | |
312 | ||
be6d8026 KH |
313 | if (e->prsvd) |
314 | l.base++; | |
a9dcad5e HD |
315 | /* increment victim for next tlb load */ |
316 | if (++l.vict == obj->nr_tlb_entries) | |
be6d8026 | 317 | l.vict = l.base; |
a9dcad5e HD |
318 | iotlb_lock_set(obj, &l); |
319 | out: | |
320 | clk_disable(obj->clk); | |
321 | return err; | |
322 | } | |
a9dcad5e | 323 | |
5da14a47 OBC |
324 | #else /* !PREFETCH_IOTLB */ |
325 | ||
6c32df43 | 326 | static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
5da14a47 OBC |
327 | { |
328 | return 0; | |
329 | } | |
330 | ||
331 | #endif /* !PREFETCH_IOTLB */ | |
332 | ||
6c32df43 | 333 | static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
5da14a47 OBC |
334 | { |
335 | return load_iotlb_entry(obj, e); | |
336 | } | |
a9dcad5e HD |
337 | |
338 | /** | |
339 | * flush_iotlb_page - Clear an iommu tlb entry | |
340 | * @obj: target iommu | |
341 | * @da: iommu device virtual address | |
342 | * | |
343 | * Clear an iommu tlb entry which includes 'da' address. | |
344 | **/ | |
6c32df43 | 345 | static void flush_iotlb_page(struct omap_iommu *obj, u32 da) |
a9dcad5e | 346 | { |
a9dcad5e | 347 | int i; |
37c2836c | 348 | struct cr_regs cr; |
a9dcad5e HD |
349 | |
350 | clk_enable(obj->clk); | |
351 | ||
37c2836c | 352 | for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) { |
a9dcad5e HD |
353 | u32 start; |
354 | size_t bytes; | |
355 | ||
a9dcad5e HD |
356 | if (!iotlb_cr_valid(&cr)) |
357 | continue; | |
358 | ||
359 | start = iotlb_cr_to_virt(&cr); | |
360 | bytes = iopgsz_to_bytes(cr.cam & 3); | |
361 | ||
362 | if ((start <= da) && (da < start + bytes)) { | |
363 | dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n", | |
364 | __func__, start, da, bytes); | |
0fa035e5 | 365 | iotlb_load_cr(obj, &cr); |
a9dcad5e HD |
366 | iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY); |
367 | } | |
368 | } | |
369 | clk_disable(obj->clk); | |
370 | ||
371 | if (i == obj->nr_tlb_entries) | |
372 | dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da); | |
373 | } | |
a9dcad5e HD |
374 | |
375 | /** | |
376 | * flush_iotlb_all - Clear all iommu tlb entries | |
377 | * @obj: target iommu | |
378 | **/ | |
6c32df43 | 379 | static void flush_iotlb_all(struct omap_iommu *obj) |
a9dcad5e HD |
380 | { |
381 | struct iotlb_lock l; | |
382 | ||
383 | clk_enable(obj->clk); | |
384 | ||
385 | l.base = 0; | |
386 | l.vict = 0; | |
387 | iotlb_lock_set(obj, &l); | |
388 | ||
389 | iommu_write_reg(obj, 1, MMU_GFLUSH); | |
390 | ||
391 | clk_disable(obj->clk); | |
392 | } | |
ddfa975a | 393 | |
e4efd94b | 394 | #if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE) |
a9dcad5e | 395 | |
6c32df43 | 396 | ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes) |
a9dcad5e | 397 | { |
a9dcad5e HD |
398 | if (!obj || !buf) |
399 | return -EINVAL; | |
400 | ||
401 | clk_enable(obj->clk); | |
402 | ||
14e0e679 | 403 | bytes = arch_iommu->dump_ctx(obj, buf, bytes); |
a9dcad5e HD |
404 | |
405 | clk_disable(obj->clk); | |
406 | ||
407 | return bytes; | |
408 | } | |
6c32df43 | 409 | EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx); |
a9dcad5e | 410 | |
6c32df43 OBC |
411 | static int |
412 | __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num) | |
a9dcad5e HD |
413 | { |
414 | int i; | |
37c2836c HD |
415 | struct iotlb_lock saved; |
416 | struct cr_regs tmp; | |
a9dcad5e HD |
417 | struct cr_regs *p = crs; |
418 | ||
419 | clk_enable(obj->clk); | |
a9dcad5e | 420 | iotlb_lock_get(obj, &saved); |
a9dcad5e | 421 | |
37c2836c | 422 | for_each_iotlb_cr(obj, num, i, tmp) { |
a9dcad5e HD |
423 | if (!iotlb_cr_valid(&tmp)) |
424 | continue; | |
a9dcad5e HD |
425 | *p++ = tmp; |
426 | } | |
37c2836c | 427 | |
a9dcad5e HD |
428 | iotlb_lock_set(obj, &saved); |
429 | clk_disable(obj->clk); | |
430 | ||
431 | return p - crs; | |
432 | } | |
433 | ||
434 | /** | |
6c32df43 | 435 | * omap_dump_tlb_entries - dump cr arrays to given buffer |
a9dcad5e HD |
436 | * @obj: target iommu |
437 | * @buf: output buffer | |
438 | **/ | |
6c32df43 | 439 | size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes) |
a9dcad5e | 440 | { |
14e0e679 | 441 | int i, num; |
a9dcad5e HD |
442 | struct cr_regs *cr; |
443 | char *p = buf; | |
444 | ||
14e0e679 HD |
445 | num = bytes / sizeof(*cr); |
446 | num = min(obj->nr_tlb_entries, num); | |
447 | ||
448 | cr = kcalloc(num, sizeof(*cr), GFP_KERNEL); | |
a9dcad5e HD |
449 | if (!cr) |
450 | return 0; | |
451 | ||
14e0e679 HD |
452 | num = __dump_tlb_entries(obj, cr, num); |
453 | for (i = 0; i < num; i++) | |
a9dcad5e HD |
454 | p += iotlb_dump_cr(obj, cr + i, p); |
455 | kfree(cr); | |
456 | ||
457 | return p - buf; | |
458 | } | |
6c32df43 | 459 | EXPORT_SYMBOL_GPL(omap_dump_tlb_entries); |
a9dcad5e | 460 | |
6c32df43 | 461 | int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *)) |
a9dcad5e HD |
462 | { |
463 | return driver_for_each_device(&omap_iommu_driver.driver, | |
464 | NULL, data, fn); | |
465 | } | |
6c32df43 | 466 | EXPORT_SYMBOL_GPL(omap_foreach_iommu_device); |
a9dcad5e HD |
467 | |
468 | #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */ | |
469 | ||
470 | /* | |
471 | * H/W pagetable operations | |
472 | */ | |
473 | static void flush_iopgd_range(u32 *first, u32 *last) | |
474 | { | |
475 | /* FIXME: L2 cache should be taken care of if it exists */ | |
476 | do { | |
477 | asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd" | |
478 | : : "r" (first)); | |
479 | first += L1_CACHE_BYTES / sizeof(*first); | |
480 | } while (first <= last); | |
481 | } | |
482 | ||
483 | static void flush_iopte_range(u32 *first, u32 *last) | |
484 | { | |
485 | /* FIXME: L2 cache should be taken care of if it exists */ | |
486 | do { | |
487 | asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte" | |
488 | : : "r" (first)); | |
489 | first += L1_CACHE_BYTES / sizeof(*first); | |
490 | } while (first <= last); | |
491 | } | |
492 | ||
493 | static void iopte_free(u32 *iopte) | |
494 | { | |
495 | /* Note: freed iopte's must be clean ready for re-use */ | |
496 | kmem_cache_free(iopte_cachep, iopte); | |
497 | } | |
498 | ||
6c32df43 | 499 | static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da) |
a9dcad5e HD |
500 | { |
501 | u32 *iopte; | |
502 | ||
503 | /* a table has already existed */ | |
504 | if (*iopgd) | |
505 | goto pte_ready; | |
506 | ||
507 | /* | |
508 | * do the allocation outside the page table lock | |
509 | */ | |
510 | spin_unlock(&obj->page_table_lock); | |
511 | iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL); | |
512 | spin_lock(&obj->page_table_lock); | |
513 | ||
514 | if (!*iopgd) { | |
515 | if (!iopte) | |
516 | return ERR_PTR(-ENOMEM); | |
517 | ||
518 | *iopgd = virt_to_phys(iopte) | IOPGD_TABLE; | |
519 | flush_iopgd_range(iopgd, iopgd); | |
520 | ||
521 | dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte); | |
522 | } else { | |
523 | /* We raced, free the reduniovant table */ | |
524 | iopte_free(iopte); | |
525 | } | |
526 | ||
527 | pte_ready: | |
528 | iopte = iopte_offset(iopgd, da); | |
529 | ||
530 | dev_vdbg(obj->dev, | |
531 | "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n", | |
532 | __func__, da, iopgd, *iopgd, iopte, *iopte); | |
533 | ||
534 | return iopte; | |
535 | } | |
536 | ||
6c32df43 | 537 | static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
a9dcad5e HD |
538 | { |
539 | u32 *iopgd = iopgd_offset(obj, da); | |
540 | ||
4abb7617 HD |
541 | if ((da | pa) & ~IOSECTION_MASK) { |
542 | dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", | |
543 | __func__, da, pa, IOSECTION_SIZE); | |
544 | return -EINVAL; | |
545 | } | |
546 | ||
a9dcad5e HD |
547 | *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION; |
548 | flush_iopgd_range(iopgd, iopgd); | |
549 | return 0; | |
550 | } | |
551 | ||
6c32df43 | 552 | static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
a9dcad5e HD |
553 | { |
554 | u32 *iopgd = iopgd_offset(obj, da); | |
555 | int i; | |
556 | ||
4abb7617 HD |
557 | if ((da | pa) & ~IOSUPER_MASK) { |
558 | dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", | |
559 | __func__, da, pa, IOSUPER_SIZE); | |
560 | return -EINVAL; | |
561 | } | |
562 | ||
a9dcad5e HD |
563 | for (i = 0; i < 16; i++) |
564 | *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER; | |
565 | flush_iopgd_range(iopgd, iopgd + 15); | |
566 | return 0; | |
567 | } | |
568 | ||
6c32df43 | 569 | static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
a9dcad5e HD |
570 | { |
571 | u32 *iopgd = iopgd_offset(obj, da); | |
572 | u32 *iopte = iopte_alloc(obj, iopgd, da); | |
573 | ||
574 | if (IS_ERR(iopte)) | |
575 | return PTR_ERR(iopte); | |
576 | ||
577 | *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL; | |
578 | flush_iopte_range(iopte, iopte); | |
579 | ||
580 | dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n", | |
581 | __func__, da, pa, iopte, *iopte); | |
582 | ||
583 | return 0; | |
584 | } | |
585 | ||
6c32df43 | 586 | static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) |
a9dcad5e HD |
587 | { |
588 | u32 *iopgd = iopgd_offset(obj, da); | |
589 | u32 *iopte = iopte_alloc(obj, iopgd, da); | |
590 | int i; | |
591 | ||
4abb7617 HD |
592 | if ((da | pa) & ~IOLARGE_MASK) { |
593 | dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", | |
594 | __func__, da, pa, IOLARGE_SIZE); | |
595 | return -EINVAL; | |
596 | } | |
597 | ||
a9dcad5e HD |
598 | if (IS_ERR(iopte)) |
599 | return PTR_ERR(iopte); | |
600 | ||
601 | for (i = 0; i < 16; i++) | |
602 | *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE; | |
603 | flush_iopte_range(iopte, iopte + 15); | |
604 | return 0; | |
605 | } | |
606 | ||
6c32df43 OBC |
607 | static int |
608 | iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e) | |
a9dcad5e | 609 | { |
6c32df43 | 610 | int (*fn)(struct omap_iommu *, u32, u32, u32); |
a9dcad5e HD |
611 | u32 prot; |
612 | int err; | |
613 | ||
614 | if (!obj || !e) | |
615 | return -EINVAL; | |
616 | ||
617 | switch (e->pgsz) { | |
618 | case MMU_CAM_PGSZ_16M: | |
619 | fn = iopgd_alloc_super; | |
620 | break; | |
621 | case MMU_CAM_PGSZ_1M: | |
622 | fn = iopgd_alloc_section; | |
623 | break; | |
624 | case MMU_CAM_PGSZ_64K: | |
625 | fn = iopte_alloc_large; | |
626 | break; | |
627 | case MMU_CAM_PGSZ_4K: | |
628 | fn = iopte_alloc_page; | |
629 | break; | |
630 | default: | |
631 | fn = NULL; | |
632 | BUG(); | |
633 | break; | |
634 | } | |
635 | ||
636 | prot = get_iopte_attr(e); | |
637 | ||
638 | spin_lock(&obj->page_table_lock); | |
639 | err = fn(obj, e->da, e->pa, prot); | |
640 | spin_unlock(&obj->page_table_lock); | |
641 | ||
642 | return err; | |
643 | } | |
644 | ||
645 | /** | |
6c32df43 | 646 | * omap_iopgtable_store_entry - Make an iommu pte entry |
a9dcad5e HD |
647 | * @obj: target iommu |
648 | * @e: an iommu tlb entry info | |
649 | **/ | |
6c32df43 | 650 | int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e) |
a9dcad5e HD |
651 | { |
652 | int err; | |
653 | ||
654 | flush_iotlb_page(obj, e->da); | |
655 | err = iopgtable_store_entry_core(obj, e); | |
a9dcad5e | 656 | if (!err) |
5da14a47 | 657 | prefetch_iotlb_entry(obj, e); |
a9dcad5e HD |
658 | return err; |
659 | } | |
6c32df43 | 660 | EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry); |
a9dcad5e HD |
661 | |
662 | /** | |
663 | * iopgtable_lookup_entry - Lookup an iommu pte entry | |
664 | * @obj: target iommu | |
665 | * @da: iommu device virtual address | |
666 | * @ppgd: iommu pgd entry pointer to be returned | |
667 | * @ppte: iommu pte entry pointer to be returned | |
668 | **/ | |
e1f23813 OBC |
669 | static void |
670 | iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte) | |
a9dcad5e HD |
671 | { |
672 | u32 *iopgd, *iopte = NULL; | |
673 | ||
674 | iopgd = iopgd_offset(obj, da); | |
675 | if (!*iopgd) | |
676 | goto out; | |
677 | ||
a1a54456 | 678 | if (iopgd_is_table(*iopgd)) |
a9dcad5e HD |
679 | iopte = iopte_offset(iopgd, da); |
680 | out: | |
681 | *ppgd = iopgd; | |
682 | *ppte = iopte; | |
683 | } | |
a9dcad5e | 684 | |
6c32df43 | 685 | static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da) |
a9dcad5e HD |
686 | { |
687 | size_t bytes; | |
688 | u32 *iopgd = iopgd_offset(obj, da); | |
689 | int nent = 1; | |
690 | ||
691 | if (!*iopgd) | |
692 | return 0; | |
693 | ||
a1a54456 | 694 | if (iopgd_is_table(*iopgd)) { |
a9dcad5e HD |
695 | int i; |
696 | u32 *iopte = iopte_offset(iopgd, da); | |
697 | ||
698 | bytes = IOPTE_SIZE; | |
699 | if (*iopte & IOPTE_LARGE) { | |
700 | nent *= 16; | |
701 | /* rewind to the 1st entry */ | |
c127c7dc | 702 | iopte = iopte_offset(iopgd, (da & IOLARGE_MASK)); |
a9dcad5e HD |
703 | } |
704 | bytes *= nent; | |
705 | memset(iopte, 0, nent * sizeof(*iopte)); | |
706 | flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte)); | |
707 | ||
708 | /* | |
709 | * do table walk to check if this table is necessary or not | |
710 | */ | |
711 | iopte = iopte_offset(iopgd, 0); | |
712 | for (i = 0; i < PTRS_PER_IOPTE; i++) | |
713 | if (iopte[i]) | |
714 | goto out; | |
715 | ||
716 | iopte_free(iopte); | |
717 | nent = 1; /* for the next L1 entry */ | |
718 | } else { | |
719 | bytes = IOPGD_SIZE; | |
dcc730dc | 720 | if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) { |
a9dcad5e HD |
721 | nent *= 16; |
722 | /* rewind to the 1st entry */ | |
8d33ea58 | 723 | iopgd = iopgd_offset(obj, (da & IOSUPER_MASK)); |
a9dcad5e HD |
724 | } |
725 | bytes *= nent; | |
726 | } | |
727 | memset(iopgd, 0, nent * sizeof(*iopgd)); | |
728 | flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd)); | |
729 | out: | |
730 | return bytes; | |
731 | } | |
732 | ||
733 | /** | |
734 | * iopgtable_clear_entry - Remove an iommu pte entry | |
735 | * @obj: target iommu | |
736 | * @da: iommu device virtual address | |
737 | **/ | |
6c32df43 | 738 | static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da) |
a9dcad5e HD |
739 | { |
740 | size_t bytes; | |
741 | ||
742 | spin_lock(&obj->page_table_lock); | |
743 | ||
744 | bytes = iopgtable_clear_entry_core(obj, da); | |
745 | flush_iotlb_page(obj, da); | |
746 | ||
747 | spin_unlock(&obj->page_table_lock); | |
748 | ||
749 | return bytes; | |
750 | } | |
a9dcad5e | 751 | |
6c32df43 | 752 | static void iopgtable_clear_entry_all(struct omap_iommu *obj) |
a9dcad5e HD |
753 | { |
754 | int i; | |
755 | ||
756 | spin_lock(&obj->page_table_lock); | |
757 | ||
758 | for (i = 0; i < PTRS_PER_IOPGD; i++) { | |
759 | u32 da; | |
760 | u32 *iopgd; | |
761 | ||
762 | da = i << IOPGD_SHIFT; | |
763 | iopgd = iopgd_offset(obj, da); | |
764 | ||
765 | if (!*iopgd) | |
766 | continue; | |
767 | ||
a1a54456 | 768 | if (iopgd_is_table(*iopgd)) |
a9dcad5e HD |
769 | iopte_free(iopte_offset(iopgd, 0)); |
770 | ||
771 | *iopgd = 0; | |
772 | flush_iopgd_range(iopgd, iopgd); | |
773 | } | |
774 | ||
775 | flush_iotlb_all(obj); | |
776 | ||
777 | spin_unlock(&obj->page_table_lock); | |
778 | } | |
779 | ||
780 | /* | |
781 | * Device IOMMU generic operations | |
782 | */ | |
783 | static irqreturn_t iommu_fault_handler(int irq, void *data) | |
784 | { | |
d594f1f3 | 785 | u32 da, errs; |
a9dcad5e | 786 | u32 *iopgd, *iopte; |
6c32df43 | 787 | struct omap_iommu *obj = data; |
e7f10f02 | 788 | struct iommu_domain *domain = obj->domain; |
a9dcad5e HD |
789 | |
790 | if (!obj->refcount) | |
791 | return IRQ_NONE; | |
792 | ||
a9dcad5e | 793 | clk_enable(obj->clk); |
d594f1f3 | 794 | errs = iommu_report_fault(obj, &da); |
a9dcad5e | 795 | clk_disable(obj->clk); |
c56b2ddd LP |
796 | if (errs == 0) |
797 | return IRQ_HANDLED; | |
d594f1f3 DC |
798 | |
799 | /* Fault callback or TLB/PTE Dynamic loading */ | |
e7f10f02 | 800 | if (!report_iommu_fault(domain, obj->dev, da, 0)) |
a9dcad5e HD |
801 | return IRQ_HANDLED; |
802 | ||
37b29810 HD |
803 | iommu_disable(obj); |
804 | ||
a9dcad5e HD |
805 | iopgd = iopgd_offset(obj, da); |
806 | ||
a1a54456 | 807 | if (!iopgd_is_table(*iopgd)) { |
d594f1f3 DC |
808 | dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p " |
809 | "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd); | |
a9dcad5e HD |
810 | return IRQ_NONE; |
811 | } | |
812 | ||
813 | iopte = iopte_offset(iopgd, da); | |
814 | ||
d594f1f3 DC |
815 | dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x " |
816 | "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd, | |
817 | iopte, *iopte); | |
a9dcad5e HD |
818 | |
819 | return IRQ_NONE; | |
820 | } | |
821 | ||
822 | static int device_match_by_alias(struct device *dev, void *data) | |
823 | { | |
6c32df43 | 824 | struct omap_iommu *obj = to_iommu(dev); |
a9dcad5e HD |
825 | const char *name = data; |
826 | ||
827 | pr_debug("%s: %s %s\n", __func__, obj->name, name); | |
828 | ||
829 | return strcmp(obj->name, name) == 0; | |
830 | } | |
831 | ||
832 | /** | |
f626b52d | 833 | * omap_iommu_attach() - attach iommu device to an iommu domain |
fabdbca8 | 834 | * @name: name of target omap iommu device |
f626b52d | 835 | * @iopgd: page table |
a9dcad5e | 836 | **/ |
fabdbca8 | 837 | static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd) |
a9dcad5e HD |
838 | { |
839 | int err = -ENOMEM; | |
fabdbca8 OBC |
840 | struct device *dev; |
841 | struct omap_iommu *obj; | |
842 | ||
843 | dev = driver_find_device(&omap_iommu_driver.driver, NULL, | |
844 | (void *)name, | |
845 | device_match_by_alias); | |
846 | if (!dev) | |
847 | return NULL; | |
848 | ||
849 | obj = to_iommu(dev); | |
a9dcad5e | 850 | |
f626b52d | 851 | spin_lock(&obj->iommu_lock); |
a9dcad5e | 852 | |
f626b52d OBC |
853 | /* an iommu device can only be attached once */ |
854 | if (++obj->refcount > 1) { | |
855 | dev_err(dev, "%s: already attached!\n", obj->name); | |
856 | err = -EBUSY; | |
857 | goto err_enable; | |
a9dcad5e HD |
858 | } |
859 | ||
f626b52d OBC |
860 | obj->iopgd = iopgd; |
861 | err = iommu_enable(obj); | |
862 | if (err) | |
863 | goto err_enable; | |
864 | flush_iotlb_all(obj); | |
865 | ||
a9dcad5e HD |
866 | if (!try_module_get(obj->owner)) |
867 | goto err_module; | |
868 | ||
f626b52d | 869 | spin_unlock(&obj->iommu_lock); |
a9dcad5e HD |
870 | |
871 | dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name); | |
872 | return obj; | |
873 | ||
874 | err_module: | |
875 | if (obj->refcount == 1) | |
876 | iommu_disable(obj); | |
877 | err_enable: | |
878 | obj->refcount--; | |
f626b52d | 879 | spin_unlock(&obj->iommu_lock); |
a9dcad5e HD |
880 | return ERR_PTR(err); |
881 | } | |
a9dcad5e HD |
882 | |
883 | /** | |
f626b52d | 884 | * omap_iommu_detach - release iommu device |
a9dcad5e HD |
885 | * @obj: target iommu |
886 | **/ | |
6c32df43 | 887 | static void omap_iommu_detach(struct omap_iommu *obj) |
a9dcad5e | 888 | { |
acf9d467 | 889 | if (!obj || IS_ERR(obj)) |
a9dcad5e HD |
890 | return; |
891 | ||
f626b52d | 892 | spin_lock(&obj->iommu_lock); |
a9dcad5e HD |
893 | |
894 | if (--obj->refcount == 0) | |
895 | iommu_disable(obj); | |
896 | ||
897 | module_put(obj->owner); | |
898 | ||
f626b52d | 899 | obj->iopgd = NULL; |
d594f1f3 | 900 | |
f626b52d | 901 | spin_unlock(&obj->iommu_lock); |
d594f1f3 | 902 | |
a9dcad5e | 903 | dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name); |
d594f1f3 | 904 | } |
d594f1f3 | 905 | |
a9dcad5e HD |
906 | /* |
907 | * OMAP Device MMU(IOMMU) detection | |
908 | */ | |
909 | static int __devinit omap_iommu_probe(struct platform_device *pdev) | |
910 | { | |
911 | int err = -ENODEV; | |
a9dcad5e | 912 | int irq; |
6c32df43 | 913 | struct omap_iommu *obj; |
a9dcad5e HD |
914 | struct resource *res; |
915 | struct iommu_platform_data *pdata = pdev->dev.platform_data; | |
916 | ||
917 | if (pdev->num_resources != 2) | |
918 | return -EINVAL; | |
919 | ||
920 | obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL); | |
921 | if (!obj) | |
922 | return -ENOMEM; | |
923 | ||
924 | obj->clk = clk_get(&pdev->dev, pdata->clk_name); | |
925 | if (IS_ERR(obj->clk)) | |
926 | goto err_clk; | |
927 | ||
928 | obj->nr_tlb_entries = pdata->nr_tlb_entries; | |
929 | obj->name = pdata->name; | |
930 | obj->dev = &pdev->dev; | |
931 | obj->ctx = (void *)obj + sizeof(*obj); | |
c7f4ab26 GLF |
932 | obj->da_start = pdata->da_start; |
933 | obj->da_end = pdata->da_end; | |
a9dcad5e | 934 | |
f626b52d | 935 | spin_lock_init(&obj->iommu_lock); |
a9dcad5e HD |
936 | mutex_init(&obj->mmap_lock); |
937 | spin_lock_init(&obj->page_table_lock); | |
938 | INIT_LIST_HEAD(&obj->mmap); | |
939 | ||
940 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
941 | if (!res) { | |
942 | err = -ENODEV; | |
943 | goto err_mem; | |
944 | } | |
a9dcad5e HD |
945 | |
946 | res = request_mem_region(res->start, resource_size(res), | |
947 | dev_name(&pdev->dev)); | |
948 | if (!res) { | |
949 | err = -EIO; | |
950 | goto err_mem; | |
951 | } | |
952 | ||
da4a0f76 AK |
953 | obj->regbase = ioremap(res->start, resource_size(res)); |
954 | if (!obj->regbase) { | |
955 | err = -ENOMEM; | |
956 | goto err_ioremap; | |
957 | } | |
958 | ||
a9dcad5e HD |
959 | irq = platform_get_irq(pdev, 0); |
960 | if (irq < 0) { | |
961 | err = -ENODEV; | |
962 | goto err_irq; | |
963 | } | |
964 | err = request_irq(irq, iommu_fault_handler, IRQF_SHARED, | |
965 | dev_name(&pdev->dev), obj); | |
966 | if (err < 0) | |
967 | goto err_irq; | |
968 | platform_set_drvdata(pdev, obj); | |
969 | ||
a9dcad5e HD |
970 | dev_info(&pdev->dev, "%s registered\n", obj->name); |
971 | return 0; | |
972 | ||
a9dcad5e | 973 | err_irq: |
a9dcad5e | 974 | iounmap(obj->regbase); |
da4a0f76 AK |
975 | err_ioremap: |
976 | release_mem_region(res->start, resource_size(res)); | |
a9dcad5e HD |
977 | err_mem: |
978 | clk_put(obj->clk); | |
979 | err_clk: | |
980 | kfree(obj); | |
981 | return err; | |
982 | } | |
983 | ||
984 | static int __devexit omap_iommu_remove(struct platform_device *pdev) | |
985 | { | |
986 | int irq; | |
987 | struct resource *res; | |
6c32df43 | 988 | struct omap_iommu *obj = platform_get_drvdata(pdev); |
a9dcad5e HD |
989 | |
990 | platform_set_drvdata(pdev, NULL); | |
991 | ||
992 | iopgtable_clear_entry_all(obj); | |
a9dcad5e HD |
993 | |
994 | irq = platform_get_irq(pdev, 0); | |
995 | free_irq(irq, obj); | |
996 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
997 | release_mem_region(res->start, resource_size(res)); | |
998 | iounmap(obj->regbase); | |
999 | ||
1000 | clk_put(obj->clk); | |
1001 | dev_info(&pdev->dev, "%s removed\n", obj->name); | |
1002 | kfree(obj); | |
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | static struct platform_driver omap_iommu_driver = { | |
1007 | .probe = omap_iommu_probe, | |
1008 | .remove = __devexit_p(omap_iommu_remove), | |
1009 | .driver = { | |
1010 | .name = "omap-iommu", | |
1011 | }, | |
1012 | }; | |
1013 | ||
1014 | static void iopte_cachep_ctor(void *iopte) | |
1015 | { | |
1016 | clean_dcache_area(iopte, IOPTE_TABLE_SIZE); | |
1017 | } | |
1018 | ||
f626b52d | 1019 | static int omap_iommu_map(struct iommu_domain *domain, unsigned long da, |
5009065d | 1020 | phys_addr_t pa, size_t bytes, int prot) |
f626b52d OBC |
1021 | { |
1022 | struct omap_iommu_domain *omap_domain = domain->priv; | |
6c32df43 | 1023 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
f626b52d | 1024 | struct device *dev = oiommu->dev; |
f626b52d OBC |
1025 | struct iotlb_entry e; |
1026 | int omap_pgsz; | |
1027 | u32 ret, flags; | |
1028 | ||
1029 | /* we only support mapping a single iommu page for now */ | |
1030 | omap_pgsz = bytes_to_iopgsz(bytes); | |
1031 | if (omap_pgsz < 0) { | |
1032 | dev_err(dev, "invalid size to map: %d\n", bytes); | |
1033 | return -EINVAL; | |
1034 | } | |
1035 | ||
1036 | dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes); | |
1037 | ||
1038 | flags = omap_pgsz | prot; | |
1039 | ||
1040 | iotlb_init_entry(&e, da, pa, flags); | |
1041 | ||
6c32df43 | 1042 | ret = omap_iopgtable_store_entry(oiommu, &e); |
b4550d41 | 1043 | if (ret) |
6c32df43 | 1044 | dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret); |
f626b52d | 1045 | |
b4550d41 | 1046 | return ret; |
f626b52d OBC |
1047 | } |
1048 | ||
5009065d OBC |
1049 | static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da, |
1050 | size_t size) | |
f626b52d OBC |
1051 | { |
1052 | struct omap_iommu_domain *omap_domain = domain->priv; | |
6c32df43 | 1053 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
f626b52d | 1054 | struct device *dev = oiommu->dev; |
f626b52d | 1055 | |
5009065d | 1056 | dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size); |
f626b52d | 1057 | |
5009065d | 1058 | return iopgtable_clear_entry(oiommu, da); |
f626b52d OBC |
1059 | } |
1060 | ||
1061 | static int | |
1062 | omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) | |
1063 | { | |
1064 | struct omap_iommu_domain *omap_domain = domain->priv; | |
6c32df43 | 1065 | struct omap_iommu *oiommu; |
fabdbca8 | 1066 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
f626b52d OBC |
1067 | int ret = 0; |
1068 | ||
1069 | spin_lock(&omap_domain->lock); | |
1070 | ||
1071 | /* only a single device is supported per domain for now */ | |
1072 | if (omap_domain->iommu_dev) { | |
1073 | dev_err(dev, "iommu domain is already attached\n"); | |
1074 | ret = -EBUSY; | |
1075 | goto out; | |
1076 | } | |
1077 | ||
1078 | /* get a handle to and enable the omap iommu */ | |
fabdbca8 | 1079 | oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable); |
f626b52d OBC |
1080 | if (IS_ERR(oiommu)) { |
1081 | ret = PTR_ERR(oiommu); | |
1082 | dev_err(dev, "can't get omap iommu: %d\n", ret); | |
1083 | goto out; | |
1084 | } | |
1085 | ||
fabdbca8 | 1086 | omap_domain->iommu_dev = arch_data->iommu_dev = oiommu; |
803b5277 | 1087 | omap_domain->dev = dev; |
e7f10f02 | 1088 | oiommu->domain = domain; |
f626b52d OBC |
1089 | |
1090 | out: | |
1091 | spin_unlock(&omap_domain->lock); | |
1092 | return ret; | |
1093 | } | |
1094 | ||
803b5277 ORL |
1095 | static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain, |
1096 | struct device *dev) | |
f626b52d | 1097 | { |
fabdbca8 | 1098 | struct omap_iommu *oiommu = dev_to_omap_iommu(dev); |
803b5277 | 1099 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
f626b52d OBC |
1100 | |
1101 | /* only a single device is supported per domain for now */ | |
1102 | if (omap_domain->iommu_dev != oiommu) { | |
1103 | dev_err(dev, "invalid iommu device\n"); | |
803b5277 | 1104 | return; |
f626b52d OBC |
1105 | } |
1106 | ||
1107 | iopgtable_clear_entry_all(oiommu); | |
1108 | ||
1109 | omap_iommu_detach(oiommu); | |
1110 | ||
fabdbca8 | 1111 | omap_domain->iommu_dev = arch_data->iommu_dev = NULL; |
803b5277 ORL |
1112 | omap_domain->dev = NULL; |
1113 | } | |
f626b52d | 1114 | |
803b5277 ORL |
1115 | static void omap_iommu_detach_dev(struct iommu_domain *domain, |
1116 | struct device *dev) | |
1117 | { | |
1118 | struct omap_iommu_domain *omap_domain = domain->priv; | |
1119 | ||
1120 | spin_lock(&omap_domain->lock); | |
1121 | _omap_iommu_detach_dev(omap_domain, dev); | |
f626b52d OBC |
1122 | spin_unlock(&omap_domain->lock); |
1123 | } | |
1124 | ||
1125 | static int omap_iommu_domain_init(struct iommu_domain *domain) | |
1126 | { | |
1127 | struct omap_iommu_domain *omap_domain; | |
1128 | ||
1129 | omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL); | |
1130 | if (!omap_domain) { | |
1131 | pr_err("kzalloc failed\n"); | |
1132 | goto out; | |
1133 | } | |
1134 | ||
1135 | omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL); | |
1136 | if (!omap_domain->pgtable) { | |
1137 | pr_err("kzalloc failed\n"); | |
1138 | goto fail_nomem; | |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * should never fail, but please keep this around to ensure | |
1143 | * we keep the hardware happy | |
1144 | */ | |
1145 | BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE)); | |
1146 | ||
1147 | clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE); | |
1148 | spin_lock_init(&omap_domain->lock); | |
1149 | ||
1150 | domain->priv = omap_domain; | |
1151 | ||
2c6edb0c JR |
1152 | domain->geometry.aperture_start = 0; |
1153 | domain->geometry.aperture_end = (1ULL << 32) - 1; | |
1154 | domain->geometry.force_aperture = true; | |
1155 | ||
f626b52d OBC |
1156 | return 0; |
1157 | ||
1158 | fail_nomem: | |
1159 | kfree(omap_domain); | |
1160 | out: | |
1161 | return -ENOMEM; | |
1162 | } | |
1163 | ||
f626b52d OBC |
1164 | static void omap_iommu_domain_destroy(struct iommu_domain *domain) |
1165 | { | |
1166 | struct omap_iommu_domain *omap_domain = domain->priv; | |
1167 | ||
1168 | domain->priv = NULL; | |
1169 | ||
803b5277 ORL |
1170 | /* |
1171 | * An iommu device is still attached | |
1172 | * (currently, only one device can be attached) ? | |
1173 | */ | |
1174 | if (omap_domain->iommu_dev) | |
1175 | _omap_iommu_detach_dev(omap_domain, omap_domain->dev); | |
1176 | ||
f626b52d OBC |
1177 | kfree(omap_domain->pgtable); |
1178 | kfree(omap_domain); | |
1179 | } | |
1180 | ||
1181 | static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain, | |
1182 | unsigned long da) | |
1183 | { | |
1184 | struct omap_iommu_domain *omap_domain = domain->priv; | |
6c32df43 | 1185 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
f626b52d OBC |
1186 | struct device *dev = oiommu->dev; |
1187 | u32 *pgd, *pte; | |
1188 | phys_addr_t ret = 0; | |
1189 | ||
1190 | iopgtable_lookup_entry(oiommu, da, &pgd, &pte); | |
1191 | ||
1192 | if (pte) { | |
1193 | if (iopte_is_small(*pte)) | |
1194 | ret = omap_iommu_translate(*pte, da, IOPTE_MASK); | |
1195 | else if (iopte_is_large(*pte)) | |
1196 | ret = omap_iommu_translate(*pte, da, IOLARGE_MASK); | |
1197 | else | |
1a36ea81 | 1198 | dev_err(dev, "bogus pte 0x%x, da 0x%lx", *pte, da); |
f626b52d OBC |
1199 | } else { |
1200 | if (iopgd_is_section(*pgd)) | |
1201 | ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK); | |
1202 | else if (iopgd_is_super(*pgd)) | |
1203 | ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK); | |
1204 | else | |
1a36ea81 | 1205 | dev_err(dev, "bogus pgd 0x%x, da 0x%lx", *pgd, da); |
f626b52d OBC |
1206 | } |
1207 | ||
1208 | return ret; | |
1209 | } | |
1210 | ||
1211 | static int omap_iommu_domain_has_cap(struct iommu_domain *domain, | |
1212 | unsigned long cap) | |
1213 | { | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | static struct iommu_ops omap_iommu_ops = { | |
1218 | .domain_init = omap_iommu_domain_init, | |
1219 | .domain_destroy = omap_iommu_domain_destroy, | |
1220 | .attach_dev = omap_iommu_attach_dev, | |
1221 | .detach_dev = omap_iommu_detach_dev, | |
1222 | .map = omap_iommu_map, | |
1223 | .unmap = omap_iommu_unmap, | |
1224 | .iova_to_phys = omap_iommu_iova_to_phys, | |
1225 | .domain_has_cap = omap_iommu_domain_has_cap, | |
66bc8cf3 | 1226 | .pgsize_bitmap = OMAP_IOMMU_PGSIZES, |
f626b52d OBC |
1227 | }; |
1228 | ||
a9dcad5e HD |
1229 | static int __init omap_iommu_init(void) |
1230 | { | |
1231 | struct kmem_cache *p; | |
1232 | const unsigned long flags = SLAB_HWCACHE_ALIGN; | |
1233 | size_t align = 1 << 10; /* L2 pagetable alignement */ | |
1234 | ||
1235 | p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags, | |
1236 | iopte_cachep_ctor); | |
1237 | if (!p) | |
1238 | return -ENOMEM; | |
1239 | iopte_cachep = p; | |
1240 | ||
a65bc64f | 1241 | bus_set_iommu(&platform_bus_type, &omap_iommu_ops); |
f626b52d | 1242 | |
a9dcad5e HD |
1243 | return platform_driver_register(&omap_iommu_driver); |
1244 | } | |
435792d9 OBC |
1245 | /* must be ready before omap3isp is probed */ |
1246 | subsys_initcall(omap_iommu_init); | |
a9dcad5e HD |
1247 | |
1248 | static void __exit omap_iommu_exit(void) | |
1249 | { | |
1250 | kmem_cache_destroy(iopte_cachep); | |
1251 | ||
1252 | platform_driver_unregister(&omap_iommu_driver); | |
1253 | } | |
1254 | module_exit(omap_iommu_exit); | |
1255 | ||
1256 | MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives"); | |
1257 | MODULE_ALIAS("platform:omap-iommu"); | |
1258 | MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi"); | |
1259 | MODULE_LICENSE("GPL v2"); |