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