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