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> |
Tony Lindgren | c8d35c8 | 2012-11-02 12:24:03 -0700 | [diff] [blame^] | 22 | #include <linux/omap-iommu.h> |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 23 | #include <linux/mutex.h> |
| 24 | #include <linux/spinlock.h> |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 25 | |
| 26 | #include <asm/cacheflush.h> |
| 27 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 28 | #include <plat/iommu.h> |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 29 | |
Ido Yariv | 2f7702a | 2012-11-02 12:24:00 -0700 | [diff] [blame] | 30 | #include "omap-iopgtable.h" |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 31 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 66bc8cf | 2011-11-10 11:32:27 +0200 | [diff] [blame] | 37 | /* bitmap of the page sizes currently supported */ |
| 38 | #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M) |
| 39 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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. |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 45 | * @dev: Device using this domain. |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 46 | * @lock: domain lock, should be taken when attaching/detaching |
| 47 | */ |
| 48 | struct omap_iommu_domain { |
| 49 | u32 *pgtable; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 50 | struct omap_iommu *iommu_dev; |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 51 | struct device *dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 52 | spinlock_t lock; |
| 53 | }; |
| 54 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 62 | * omap_install_iommu_arch - Install archtecure specific iommu functions |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 68 | int omap_install_iommu_arch(const struct iommu_functions *ops) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 69 | { |
| 70 | if (arch_iommu) |
| 71 | return -EBUSY; |
| 72 | |
| 73 | arch_iommu = ops; |
| 74 | return 0; |
| 75 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(omap_install_iommu_arch); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 77 | |
| 78 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 79 | * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 80 | * @ops: a pointer to architecture specific iommu functions |
| 81 | * |
| 82 | * This interface uninstalls the iommu algorighm installed previously. |
| 83 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 84 | void omap_uninstall_iommu_arch(const struct iommu_functions *ops) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 85 | { |
| 86 | if (arch_iommu != ops) |
| 87 | pr_err("%s: not your arch\n", __func__); |
| 88 | |
| 89 | arch_iommu = NULL; |
| 90 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 91 | EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 94 | * omap_iommu_save_ctx - Save registers for pm off-mode support |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 95 | * @dev: client device |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 96 | **/ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 97 | void omap_iommu_save_ctx(struct device *dev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 98 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 99 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
| 100 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 101 | arch_iommu->save_ctx(obj); |
| 102 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 103 | EXPORT_SYMBOL_GPL(omap_iommu_save_ctx); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 106 | * omap_iommu_restore_ctx - Restore registers for pm off-mode support |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 107 | * @dev: client device |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 108 | **/ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 109 | void omap_iommu_restore_ctx(struct device *dev) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 110 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 111 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
| 112 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 113 | arch_iommu->restore_ctx(obj); |
| 114 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 115 | EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 118 | * omap_iommu_arch_version - Return running iommu arch version |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 119 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 120 | u32 omap_iommu_arch_version(void) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 121 | { |
| 122 | return arch_iommu->version; |
| 123 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(omap_iommu_arch_version); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 125 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 126 | static int iommu_enable(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 127 | { |
| 128 | int err; |
| 129 | |
| 130 | if (!obj) |
| 131 | return -EINVAL; |
| 132 | |
Martin Hostettler | ef4815a | 2011-02-24 12:51:31 -0800 | [diff] [blame] | 133 | if (!arch_iommu) |
| 134 | return -ENODEV; |
| 135 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 136 | clk_enable(obj->clk); |
| 137 | |
| 138 | err = arch_iommu->enable(obj); |
| 139 | |
| 140 | clk_disable(obj->clk); |
| 141 | return err; |
| 142 | } |
| 143 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 144 | static void iommu_disable(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 159 | 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] | 160 | { |
| 161 | BUG_ON(!cr || !e); |
| 162 | |
| 163 | arch_iommu->cr_to_e(cr, e); |
| 164 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 165 | EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 175 | static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj, |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 176 | struct iotlb_entry *e) |
| 177 | { |
| 178 | if (!e) |
| 179 | return NULL; |
| 180 | |
| 181 | return arch_iommu->alloc_cr(obj, e); |
| 182 | } |
| 183 | |
Ohad Ben-Cohen | e1f2381 | 2011-08-16 14:58:14 +0300 | [diff] [blame] | 184 | static u32 iotlb_cr_to_virt(struct cr_regs *cr) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 185 | { |
| 186 | return arch_iommu->cr_to_virt(cr); |
| 187 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 188 | |
| 189 | static u32 get_iopte_attr(struct iotlb_entry *e) |
| 190 | { |
| 191 | return arch_iommu->get_pte_attr(e); |
| 192 | } |
| 193 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 194 | static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 195 | { |
| 196 | return arch_iommu->fault_isr(obj, da); |
| 197 | } |
| 198 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 199 | 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] | 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 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | u32 val; |
| 213 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 220 | 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] | 221 | { |
| 222 | arch_iommu->tlb_read_cr(obj, cr); |
| 223 | } |
| 224 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 225 | 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] | 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 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 239 | 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] | 240 | char *buf) |
| 241 | { |
| 242 | BUG_ON(!cr || !buf); |
| 243 | |
| 244 | return arch_iommu->dump_cr(obj, cr, buf); |
| 245 | } |
| 246 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 247 | /* only used in iotlb iteration for-loop */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 248 | 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] | 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 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 261 | /** |
| 262 | * load_iotlb_entry - Set an iommu tlb entry |
| 263 | * @obj: target iommu |
| 264 | * @e: an iommu tlb entry info |
| 265 | **/ |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 266 | #ifdef PREFETCH_IOTLB |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 267 | 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] | 268 | { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 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__); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 281 | err = -EBUSY; |
| 282 | goto out; |
| 283 | } |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 284 | if (!e->prsvd) { |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 285 | int i; |
| 286 | struct cr_regs tmp; |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 287 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 288 | for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp) |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 289 | if (!iotlb_cr_valid(&tmp)) |
| 290 | break; |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 291 | |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 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 | } |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 297 | |
| 298 | iotlb_lock_get(obj, &l); |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 299 | } else { |
| 300 | l.vict = l.base; |
| 301 | iotlb_lock_set(obj, &l); |
| 302 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 313 | if (e->prsvd) |
| 314 | l.base++; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 315 | /* increment victim for next tlb load */ |
| 316 | if (++l.vict == obj->nr_tlb_entries) |
Kanigeri, Hari | be6d802 | 2010-04-22 23:26:11 +0000 | [diff] [blame] | 317 | l.vict = l.base; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 318 | iotlb_lock_set(obj, &l); |
| 319 | out: |
| 320 | clk_disable(obj->clk); |
| 321 | return err; |
| 322 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 323 | |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 324 | #else /* !PREFETCH_IOTLB */ |
| 325 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 326 | 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] | 327 | { |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | #endif /* !PREFETCH_IOTLB */ |
| 332 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 333 | 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] | 334 | { |
| 335 | return load_iotlb_entry(obj, e); |
| 336 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 345 | static void flush_iotlb_page(struct omap_iommu *obj, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 346 | { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 347 | int i; |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 348 | struct cr_regs cr; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 349 | |
| 350 | clk_enable(obj->clk); |
| 351 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 352 | for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 353 | u32 start; |
| 354 | size_t bytes; |
| 355 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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); |
Hari Kanigeri | 0fa035e | 2010-08-20 13:50:18 +0000 | [diff] [blame] | 365 | iotlb_load_cr(obj, &cr); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * flush_iotlb_all - Clear all iommu tlb entries |
| 377 | * @obj: target iommu |
| 378 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 379 | static void flush_iotlb_all(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 393 | |
Arnd Bergmann | e4efd94 | 2011-10-02 14:34:05 -0400 | [diff] [blame] | 394 | #if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE) |
Kanigeri, Hari | ddfa975 | 2010-05-24 02:01:51 +0000 | [diff] [blame] | 395 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 396 | 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] | 397 | { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 398 | if (!obj || !buf) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | clk_enable(obj->clk); |
| 402 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 403 | bytes = arch_iommu->dump_ctx(obj, buf, bytes); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 404 | |
| 405 | clk_disable(obj->clk); |
| 406 | |
| 407 | return bytes; |
| 408 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 409 | EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 410 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 411 | static int |
| 412 | __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] | 413 | { |
| 414 | int i; |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 415 | struct iotlb_lock saved; |
| 416 | struct cr_regs tmp; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 417 | struct cr_regs *p = crs; |
| 418 | |
| 419 | clk_enable(obj->clk); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 420 | iotlb_lock_get(obj, &saved); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 421 | |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 422 | for_each_iotlb_cr(obj, num, i, tmp) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 423 | if (!iotlb_cr_valid(&tmp)) |
| 424 | continue; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 425 | *p++ = tmp; |
| 426 | } |
Hiroshi DOYU | 37c2836 | 2010-04-27 05:37:12 +0000 | [diff] [blame] | 427 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 428 | iotlb_lock_set(obj, &saved); |
| 429 | clk_disable(obj->clk); |
| 430 | |
| 431 | return p - crs; |
| 432 | } |
| 433 | |
| 434 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 435 | * omap_dump_tlb_entries - dump cr arrays to given buffer |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 436 | * @obj: target iommu |
| 437 | * @buf: output buffer |
| 438 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 439 | 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] | 440 | { |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 441 | int i, num; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 442 | struct cr_regs *cr; |
| 443 | char *p = buf; |
| 444 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 445 | num = bytes / sizeof(*cr); |
| 446 | num = min(obj->nr_tlb_entries, num); |
| 447 | |
| 448 | cr = kcalloc(num, sizeof(*cr), GFP_KERNEL); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 449 | if (!cr) |
| 450 | return 0; |
| 451 | |
Hiroshi DOYU | 14e0e67 | 2009-08-28 10:54:41 -0700 | [diff] [blame] | 452 | num = __dump_tlb_entries(obj, cr, num); |
| 453 | for (i = 0; i < num; i++) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 454 | p += iotlb_dump_cr(obj, cr + i, p); |
| 455 | kfree(cr); |
| 456 | |
| 457 | return p - buf; |
| 458 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 459 | EXPORT_SYMBOL_GPL(omap_dump_tlb_entries); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 460 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 461 | int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 462 | { |
| 463 | return driver_for_each_device(&omap_iommu_driver.driver, |
| 464 | NULL, data, fn); |
| 465 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 466 | EXPORT_SYMBOL_GPL(omap_foreach_iommu_device); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 499 | static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 537 | 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] | 538 | { |
| 539 | u32 *iopgd = iopgd_offset(obj, da); |
| 540 | |
Hiroshi DOYU | 4abb761 | 2010-05-06 18:24:04 +0300 | [diff] [blame] | 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 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 547 | *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION; |
| 548 | flush_iopgd_range(iopgd, iopgd); |
| 549 | return 0; |
| 550 | } |
| 551 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 552 | 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] | 553 | { |
| 554 | u32 *iopgd = iopgd_offset(obj, da); |
| 555 | int i; |
| 556 | |
Hiroshi DOYU | 4abb761 | 2010-05-06 18:24:04 +0300 | [diff] [blame] | 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 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 569 | 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] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 586 | 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] | 587 | { |
| 588 | u32 *iopgd = iopgd_offset(obj, da); |
| 589 | u32 *iopte = iopte_alloc(obj, iopgd, da); |
| 590 | int i; |
| 591 | |
Hiroshi DOYU | 4abb761 | 2010-05-06 18:24:04 +0300 | [diff] [blame] | 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 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 607 | static int |
| 608 | iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 609 | { |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 610 | int (*fn)(struct omap_iommu *, u32, u32, u32); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 646 | * omap_iopgtable_store_entry - Make an iommu pte entry |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 647 | * @obj: target iommu |
| 648 | * @e: an iommu tlb entry info |
| 649 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 650 | 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] | 651 | { |
| 652 | int err; |
| 653 | |
| 654 | flush_iotlb_page(obj, e->da); |
| 655 | err = iopgtable_store_entry_core(obj, e); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 656 | if (!err) |
Ohad Ben-Cohen | 5da14a4 | 2011-08-16 15:19:10 +0300 | [diff] [blame] | 657 | prefetch_iotlb_entry(obj, e); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 658 | return err; |
| 659 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 660 | EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | **/ |
Ohad Ben-Cohen | e1f2381 | 2011-08-16 14:58:14 +0300 | [diff] [blame] | 669 | static void |
| 670 | 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] | 671 | { |
| 672 | u32 *iopgd, *iopte = NULL; |
| 673 | |
| 674 | iopgd = iopgd_offset(obj, da); |
| 675 | if (!*iopgd) |
| 676 | goto out; |
| 677 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 678 | if (iopgd_is_table(*iopgd)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 679 | iopte = iopte_offset(iopgd, da); |
| 680 | out: |
| 681 | *ppgd = iopgd; |
| 682 | *ppte = iopte; |
| 683 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 684 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 685 | 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] | 686 | { |
| 687 | size_t bytes; |
| 688 | u32 *iopgd = iopgd_offset(obj, da); |
| 689 | int nent = 1; |
| 690 | |
| 691 | if (!*iopgd) |
| 692 | return 0; |
| 693 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 694 | if (iopgd_is_table(*iopgd)) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 */ |
Hiroshi DOYU | c127c7d | 2010-02-15 10:03:32 -0800 | [diff] [blame] | 702 | iopte = iopte_offset(iopgd, (da & IOLARGE_MASK)); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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; |
Hiroshi DOYU | dcc730d | 2009-10-22 14:46:32 -0700 | [diff] [blame] | 720 | if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) { |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 721 | nent *= 16; |
| 722 | /* rewind to the 1st entry */ |
Hiroshi DOYU | 8d33ea5 | 2010-02-15 10:03:32 -0800 | [diff] [blame] | 723 | iopgd = iopgd_offset(obj, (da & IOSUPER_MASK)); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 738 | static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 751 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 752 | static void iopgtable_clear_entry_all(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 768 | if (iopgd_is_table(*iopgd)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | { |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 785 | u32 da, errs; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 786 | u32 *iopgd, *iopte; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 787 | struct omap_iommu *obj = data; |
Ohad Ben-Cohen | e7f10f0 | 2011-09-13 15:26:29 -0400 | [diff] [blame] | 788 | struct iommu_domain *domain = obj->domain; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 789 | |
| 790 | if (!obj->refcount) |
| 791 | return IRQ_NONE; |
| 792 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 793 | clk_enable(obj->clk); |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 794 | errs = iommu_report_fault(obj, &da); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 795 | clk_disable(obj->clk); |
Laurent Pinchart | c56b2dd | 2011-05-10 16:56:46 +0200 | [diff] [blame] | 796 | if (errs == 0) |
| 797 | return IRQ_HANDLED; |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 798 | |
| 799 | /* Fault callback or TLB/PTE Dynamic loading */ |
Ohad Ben-Cohen | e7f10f0 | 2011-09-13 15:26:29 -0400 | [diff] [blame] | 800 | if (!report_iommu_fault(domain, obj->dev, da, 0)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 801 | return IRQ_HANDLED; |
| 802 | |
Hiroshi DOYU | 37b2981 | 2010-05-24 02:01:52 +0000 | [diff] [blame] | 803 | iommu_disable(obj); |
| 804 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 805 | iopgd = iopgd_offset(obj, da); |
| 806 | |
Hiroshi DOYU | a1a5445 | 2010-05-13 09:45:35 +0300 | [diff] [blame] | 807 | if (!iopgd_is_table(*iopgd)) { |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 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); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 810 | return IRQ_NONE; |
| 811 | } |
| 812 | |
| 813 | iopte = iopte_offset(iopgd, da); |
| 814 | |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 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); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 818 | |
| 819 | return IRQ_NONE; |
| 820 | } |
| 821 | |
| 822 | static int device_match_by_alias(struct device *dev, void *data) |
| 823 | { |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 824 | struct omap_iommu *obj = to_iommu(dev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | /** |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 833 | * omap_iommu_attach() - attach iommu device to an iommu domain |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 834 | * @name: name of target omap iommu device |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 835 | * @iopgd: page table |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 836 | **/ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 837 | static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 838 | { |
| 839 | int err = -ENOMEM; |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 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); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 850 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 851 | spin_lock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 852 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 860 | obj->iopgd = iopgd; |
| 861 | err = iommu_enable(obj); |
| 862 | if (err) |
| 863 | goto err_enable; |
| 864 | flush_iotlb_all(obj); |
| 865 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 866 | if (!try_module_get(obj->owner)) |
| 867 | goto err_module; |
| 868 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 869 | spin_unlock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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--; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 879 | spin_unlock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 880 | return ERR_PTR(err); |
| 881 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 882 | |
| 883 | /** |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 884 | * omap_iommu_detach - release iommu device |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 885 | * @obj: target iommu |
| 886 | **/ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 887 | static void omap_iommu_detach(struct omap_iommu *obj) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 888 | { |
Roel Kluin | acf9d46 | 2010-01-08 10:29:05 -0800 | [diff] [blame] | 889 | if (!obj || IS_ERR(obj)) |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 890 | return; |
| 891 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 892 | spin_lock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 893 | |
| 894 | if (--obj->refcount == 0) |
| 895 | iommu_disable(obj); |
| 896 | |
| 897 | module_put(obj->owner); |
| 898 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 899 | obj->iopgd = NULL; |
| 900 | |
| 901 | spin_unlock(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 902 | |
| 903 | dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name); |
| 904 | } |
David Cohen | d594f1f | 2011-02-16 19:35:51 +0000 | [diff] [blame] | 905 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 906 | /* |
| 907 | * OMAP Device MMU(IOMMU) detection |
| 908 | */ |
| 909 | static int __devinit omap_iommu_probe(struct platform_device *pdev) |
| 910 | { |
| 911 | int err = -ENODEV; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 912 | int irq; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 913 | struct omap_iommu *obj; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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); |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 932 | obj->da_start = pdata->da_start; |
| 933 | obj->da_end = pdata->da_end; |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 934 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 935 | spin_lock_init(&obj->iommu_lock); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | } |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Aaro Koskinen | da4a0f7 | 2011-03-14 12:28:32 +0000 | [diff] [blame] | 953 | obj->regbase = ioremap(res->start, resource_size(res)); |
| 954 | if (!obj->regbase) { |
| 955 | err = -ENOMEM; |
| 956 | goto err_ioremap; |
| 957 | } |
| 958 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 970 | dev_info(&pdev->dev, "%s registered\n", obj->name); |
| 971 | return 0; |
| 972 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 973 | err_irq: |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 974 | iounmap(obj->regbase); |
Aaro Koskinen | da4a0f7 | 2011-03-14 12:28:32 +0000 | [diff] [blame] | 975 | err_ioremap: |
| 976 | release_mem_region(res->start, resource_size(res)); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 988 | struct omap_iommu *obj = platform_get_drvdata(pdev); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 989 | |
| 990 | platform_set_drvdata(pdev, NULL); |
| 991 | |
| 992 | iopgtable_clear_entry_all(obj); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1019 | 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] | 1020 | phys_addr_t pa, size_t bytes, int prot) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1021 | { |
| 1022 | struct omap_iommu_domain *omap_domain = domain->priv; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1023 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1024 | struct device *dev = oiommu->dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1042 | ret = omap_iopgtable_store_entry(oiommu, &e); |
Ohad Ben-Cohen | b4550d4 | 2011-09-02 13:32:31 -0400 | [diff] [blame] | 1043 | if (ret) |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1044 | dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1045 | |
Ohad Ben-Cohen | b4550d4 | 2011-09-02 13:32:31 -0400 | [diff] [blame] | 1046 | return ret; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1047 | } |
| 1048 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1049 | static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da, |
| 1050 | size_t size) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1051 | { |
| 1052 | struct omap_iommu_domain *omap_domain = domain->priv; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1053 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1054 | struct device *dev = oiommu->dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1055 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1056 | 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] | 1057 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1058 | return iopgtable_clear_entry(oiommu, da); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1065 | struct omap_iommu *oiommu; |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1066 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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 */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1079 | oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1086 | omap_domain->iommu_dev = arch_data->iommu_dev = oiommu; |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1087 | omap_domain->dev = dev; |
Ohad Ben-Cohen | e7f10f0 | 2011-09-13 15:26:29 -0400 | [diff] [blame] | 1088 | oiommu->domain = domain; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1089 | |
| 1090 | out: |
| 1091 | spin_unlock(&omap_domain->lock); |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1095 | static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain, |
| 1096 | struct device *dev) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1097 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1098 | struct omap_iommu *oiommu = dev_to_omap_iommu(dev); |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1099 | struct omap_iommu_arch_data *arch_data = dev->archdata.iommu; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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"); |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1104 | return; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | iopgtable_clear_entry_all(oiommu); |
| 1108 | |
| 1109 | omap_iommu_detach(oiommu); |
| 1110 | |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 1111 | omap_domain->iommu_dev = arch_data->iommu_dev = NULL; |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 1112 | omap_domain->dev = NULL; |
| 1113 | } |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1114 | |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 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); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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 | |
Joerg Roedel | 2c6edb0 | 2012-01-26 19:40:55 +0100 | [diff] [blame] | 1152 | domain->geometry.aperture_start = 0; |
| 1153 | domain->geometry.aperture_end = (1ULL << 32) - 1; |
| 1154 | domain->geometry.force_aperture = true; |
| 1155 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1156 | return 0; |
| 1157 | |
| 1158 | fail_nomem: |
| 1159 | kfree(omap_domain); |
| 1160 | out: |
| 1161 | return -ENOMEM; |
| 1162 | } |
| 1163 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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 | |
Omar Ramirez Luna | 803b527 | 2012-04-18 13:09:41 -0500 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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; |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 1185 | struct omap_iommu *oiommu = omap_domain->iommu_dev; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 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 |
Ohad Ben-Cohen | 1a36ea8 | 2011-12-06 15:22:10 +0200 | [diff] [blame] | 1198 | 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] | 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 |
Ohad Ben-Cohen | 1a36ea8 | 2011-12-06 15:22:10 +0200 | [diff] [blame] | 1205 | 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] | 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, |
Ohad Ben-Cohen | 66bc8cf | 2011-11-10 11:32:27 +0200 | [diff] [blame] | 1226 | .pgsize_bitmap = OMAP_IOMMU_PGSIZES, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1227 | }; |
| 1228 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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 | |
Joerg Roedel | a65bc64 | 2011-09-06 17:56:07 +0200 | [diff] [blame] | 1241 | bus_set_iommu(&platform_bus_type, &omap_iommu_ops); |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 1242 | |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 1243 | return platform_driver_register(&omap_iommu_driver); |
| 1244 | } |
Ohad Ben-Cohen | 435792d | 2012-02-26 12:14:14 +0200 | [diff] [blame] | 1245 | /* must be ready before omap3isp is probed */ |
| 1246 | subsys_initcall(omap_iommu_init); |
Hiroshi DOYU | a9dcad5 | 2009-01-26 15:13:40 +0200 | [diff] [blame] | 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"); |