Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/mm/pmb.c |
| 3 | * |
| 4 | * Privileged Space Mapping Buffer (PMB) Support. |
| 5 | * |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 6 | * Copyright (C) 2005 - 2010 Paul Mundt |
| 7 | * Copyright (C) 2010 Matt Fleming |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
| 12 | */ |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 15 | #include <linux/sysdev.h> |
| 16 | #include <linux/cpu.h> |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <linux/debugfs.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/err.h> |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 24 | #include <linux/io.h> |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/rwlock.h> |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 27 | #include <asm/sizes.h> |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 28 | #include <asm/system.h> |
| 29 | #include <asm/uaccess.h> |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 30 | #include <asm/pgtable.h> |
Paul Mundt | 7bdda62 | 2010-02-17 13:23:00 +0900 | [diff] [blame] | 31 | #include <asm/page.h> |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 32 | #include <asm/mmu.h> |
Stuart Menefy | eddeeb3 | 2007-11-26 21:32:40 +0900 | [diff] [blame] | 33 | #include <asm/mmu_context.h> |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 34 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 35 | struct pmb_entry; |
| 36 | |
| 37 | struct pmb_entry { |
| 38 | unsigned long vpn; |
| 39 | unsigned long ppn; |
| 40 | unsigned long flags; |
| 41 | unsigned long size; |
| 42 | |
| 43 | spinlock_t lock; |
| 44 | |
| 45 | /* |
| 46 | * 0 .. NR_PMB_ENTRIES for specific entry selection, or |
| 47 | * PMB_NO_ENTRY to search for a free one |
| 48 | */ |
| 49 | int entry; |
| 50 | |
| 51 | /* Adjacent entry link for contiguous multi-entry mappings */ |
| 52 | struct pmb_entry *link; |
| 53 | }; |
| 54 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 55 | static void pmb_unmap_entry(struct pmb_entry *); |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 56 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 57 | static DEFINE_RWLOCK(pmb_rwlock); |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 58 | static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES]; |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 59 | static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 60 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 61 | static __always_inline unsigned long mk_pmb_entry(unsigned int entry) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 62 | { |
| 63 | return (entry & PMB_E_MASK) << PMB_E_SHIFT; |
| 64 | } |
| 65 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 66 | static __always_inline unsigned long mk_pmb_addr(unsigned int entry) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 67 | { |
| 68 | return mk_pmb_entry(entry) | PMB_ADDR; |
| 69 | } |
| 70 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 71 | static __always_inline unsigned long mk_pmb_data(unsigned int entry) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 72 | { |
| 73 | return mk_pmb_entry(entry) | PMB_DATA; |
| 74 | } |
| 75 | |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 76 | static int pmb_alloc_entry(void) |
| 77 | { |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 78 | int pos; |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 79 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 80 | pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES); |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 81 | if (pos >= 0 && pos < NR_PMB_ENTRIES) |
| 82 | __set_bit(pos, pmb_map); |
| 83 | else |
| 84 | pos = -ENOSPC; |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 85 | |
| 86 | return pos; |
| 87 | } |
| 88 | |
Matt Fleming | 8386aeb | 2009-10-06 21:22:28 +0000 | [diff] [blame] | 89 | static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn, |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 90 | unsigned long flags, int entry) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 91 | { |
| 92 | struct pmb_entry *pmbe; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 93 | unsigned long irqflags; |
| 94 | void *ret = NULL; |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 95 | int pos; |
| 96 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 97 | write_lock_irqsave(&pmb_rwlock, irqflags); |
| 98 | |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 99 | if (entry == PMB_NO_ENTRY) { |
| 100 | pos = pmb_alloc_entry(); |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 101 | if (unlikely(pos < 0)) { |
| 102 | ret = ERR_PTR(pos); |
| 103 | goto out; |
| 104 | } |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 105 | } else { |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 106 | if (__test_and_set_bit(entry, pmb_map)) { |
| 107 | ret = ERR_PTR(-ENOSPC); |
| 108 | goto out; |
| 109 | } |
| 110 | |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 111 | pos = entry; |
| 112 | } |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 113 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 114 | write_unlock_irqrestore(&pmb_rwlock, irqflags); |
| 115 | |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 116 | pmbe = &pmb_entry_list[pos]; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 117 | |
| 118 | spin_lock_init(&pmbe->lock); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 119 | |
| 120 | pmbe->vpn = vpn; |
| 121 | pmbe->ppn = ppn; |
| 122 | pmbe->flags = flags; |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 123 | pmbe->entry = pos; |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 124 | pmbe->size = 0; |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 125 | |
| 126 | return pmbe; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 127 | |
| 128 | out: |
| 129 | write_unlock_irqrestore(&pmb_rwlock, irqflags); |
| 130 | return ret; |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Matt Fleming | 8386aeb | 2009-10-06 21:22:28 +0000 | [diff] [blame] | 133 | static void pmb_free(struct pmb_entry *pmbe) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 134 | { |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 135 | __clear_bit(pmbe->entry, pmb_map); |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 136 | pmbe->entry = PMB_NO_ENTRY; |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
Paul Mundt | 0065b96 | 2010-02-17 18:05:23 +0900 | [diff] [blame] | 140 | * Ensure that the PMB entries match our cache configuration. |
| 141 | * |
| 142 | * When we are in 32-bit address extended mode, CCR.CB becomes |
| 143 | * invalid, so care must be taken to manually adjust cacheable |
| 144 | * translations. |
| 145 | */ |
| 146 | static __always_inline unsigned long pmb_cache_flags(void) |
| 147 | { |
| 148 | unsigned long flags = 0; |
| 149 | |
| 150 | #if defined(CONFIG_CACHE_WRITETHROUGH) |
| 151 | flags |= PMB_C | PMB_WT | PMB_UB; |
| 152 | #elif defined(CONFIG_CACHE_WRITEBACK) |
| 153 | flags |= PMB_C; |
| 154 | #endif |
| 155 | |
| 156 | return flags; |
| 157 | } |
| 158 | |
| 159 | /* |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 160 | * Must be run uncached. |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 161 | */ |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 162 | static void __set_pmb_entry(struct pmb_entry *pmbe) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 163 | { |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 164 | jump_to_uncached(); |
| 165 | |
Paul Mundt | 0065b96 | 2010-02-17 18:05:23 +0900 | [diff] [blame] | 166 | pmbe->flags &= ~PMB_CACHE_MASK; |
| 167 | pmbe->flags |= pmb_cache_flags(); |
| 168 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 169 | __raw_writel(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry)); |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 170 | __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, mk_pmb_data(pmbe->entry)); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 171 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 172 | back_to_cached(); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 173 | } |
| 174 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 175 | static void __clear_pmb_entry(struct pmb_entry *pmbe) |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 176 | { |
| 177 | unsigned int entry = pmbe->entry; |
| 178 | unsigned long addr; |
| 179 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 180 | jump_to_uncached(); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 181 | |
| 182 | /* Clear V-bit */ |
| 183 | addr = mk_pmb_addr(entry); |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 184 | __raw_writel(__raw_readl(addr) & ~PMB_V, addr); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 185 | |
| 186 | addr = mk_pmb_data(entry); |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 187 | __raw_writel(__raw_readl(addr) & ~PMB_V, addr); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 188 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 189 | back_to_cached(); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 192 | static void set_pmb_entry(struct pmb_entry *pmbe) |
| 193 | { |
| 194 | unsigned long flags; |
| 195 | |
| 196 | spin_lock_irqsave(&pmbe->lock, flags); |
| 197 | __set_pmb_entry(pmbe); |
| 198 | spin_unlock_irqrestore(&pmbe->lock, flags); |
| 199 | } |
| 200 | |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 201 | static struct { |
| 202 | unsigned long size; |
| 203 | int flag; |
| 204 | } pmb_sizes[] = { |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 205 | { .size = SZ_512M, .flag = PMB_SZ_512M, }, |
| 206 | { .size = SZ_128M, .flag = PMB_SZ_128M, }, |
| 207 | { .size = SZ_64M, .flag = PMB_SZ_64M, }, |
| 208 | { .size = SZ_16M, .flag = PMB_SZ_16M, }, |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | long pmb_remap(unsigned long vaddr, unsigned long phys, |
Paul Mundt | 7bdda62 | 2010-02-17 13:23:00 +0900 | [diff] [blame] | 212 | unsigned long size, pgprot_t prot) |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 213 | { |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 214 | struct pmb_entry *pmbp, *pmbe; |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 215 | unsigned long wanted; |
| 216 | int pmb_flags, i; |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 217 | long err; |
Paul Mundt | 7bdda62 | 2010-02-17 13:23:00 +0900 | [diff] [blame] | 218 | u64 flags; |
| 219 | |
| 220 | flags = pgprot_val(prot); |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 221 | |
Paul Mundt | 0065b96 | 2010-02-17 18:05:23 +0900 | [diff] [blame] | 222 | pmb_flags = PMB_WT | PMB_UB; |
| 223 | |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 224 | /* Convert typical pgprot value to the PMB equivalent */ |
| 225 | if (flags & _PAGE_CACHABLE) { |
Paul Mundt | 0065b96 | 2010-02-17 18:05:23 +0900 | [diff] [blame] | 226 | pmb_flags |= PMB_C; |
| 227 | |
| 228 | if ((flags & _PAGE_WT) == 0) |
| 229 | pmb_flags &= ~(PMB_WT | PMB_UB); |
| 230 | } |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 231 | |
| 232 | pmbp = NULL; |
| 233 | wanted = size; |
| 234 | |
| 235 | again: |
| 236 | for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) { |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 237 | unsigned long flags; |
| 238 | |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 239 | if (size < pmb_sizes[i].size) |
| 240 | continue; |
| 241 | |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 242 | pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag, |
| 243 | PMB_NO_ENTRY); |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 244 | if (IS_ERR(pmbe)) { |
| 245 | err = PTR_ERR(pmbe); |
| 246 | goto out; |
| 247 | } |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 248 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 249 | spin_lock_irqsave(&pmbe->lock, flags); |
| 250 | |
| 251 | __set_pmb_entry(pmbe); |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 252 | |
| 253 | phys += pmb_sizes[i].size; |
| 254 | vaddr += pmb_sizes[i].size; |
| 255 | size -= pmb_sizes[i].size; |
| 256 | |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 257 | pmbe->size = pmb_sizes[i].size; |
| 258 | |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 259 | /* |
| 260 | * Link adjacent entries that span multiple PMB entries |
| 261 | * for easier tear-down. |
| 262 | */ |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 263 | if (likely(pmbp)) { |
| 264 | spin_lock(&pmbp->lock); |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 265 | pmbp->link = pmbe; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 266 | spin_unlock(&pmbp->lock); |
| 267 | } |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 268 | |
| 269 | pmbp = pmbe; |
Matt Fleming | a2767cf | 2009-10-06 21:22:34 +0000 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * Instead of trying smaller sizes on every iteration |
| 273 | * (even if we succeed in allocating space), try using |
| 274 | * pmb_sizes[i].size again. |
| 275 | */ |
| 276 | i--; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 277 | |
| 278 | spin_unlock_irqrestore(&pmbe->lock, flags); |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 279 | } |
| 280 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 281 | if (size >= SZ_16M) |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 282 | goto again; |
| 283 | |
| 284 | return wanted - size; |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 285 | |
| 286 | out: |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 287 | pmb_unmap_entry(pmbp); |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 288 | |
| 289 | return err; |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void pmb_unmap(unsigned long addr) |
| 293 | { |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 294 | struct pmb_entry *pmbe = NULL; |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 295 | int i; |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 296 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 297 | read_lock(&pmb_rwlock); |
| 298 | |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 299 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 300 | if (test_bit(i, pmb_map)) { |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 301 | pmbe = &pmb_entry_list[i]; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 302 | if (pmbe->vpn == addr) |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | } |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 306 | |
| 307 | read_unlock(&pmb_rwlock); |
| 308 | |
| 309 | pmb_unmap_entry(pmbe); |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 310 | } |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 311 | |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 312 | static void pmb_unmap_entry(struct pmb_entry *pmbe) |
| 313 | { |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 314 | unsigned long flags; |
| 315 | |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 316 | if (unlikely(!pmbe)) |
| 317 | return; |
| 318 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 319 | write_lock_irqsave(&pmb_rwlock, flags); |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 320 | |
| 321 | do { |
| 322 | struct pmb_entry *pmblink = pmbe; |
| 323 | |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 324 | /* |
| 325 | * We may be called before this pmb_entry has been |
| 326 | * entered into the PMB table via set_pmb_entry(), but |
| 327 | * that's OK because we've allocated a unique slot for |
| 328 | * this entry in pmb_alloc() (even if we haven't filled |
| 329 | * it yet). |
| 330 | * |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 331 | * Therefore, calling __clear_pmb_entry() is safe as no |
Matt Fleming | 067784f | 2009-10-06 21:22:23 +0000 | [diff] [blame] | 332 | * other mapping can be using that slot. |
| 333 | */ |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 334 | __clear_pmb_entry(pmbe); |
Matt Fleming | fc2bdef | 2009-10-06 21:22:22 +0000 | [diff] [blame] | 335 | |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 336 | pmbe = pmblink->link; |
| 337 | |
| 338 | pmb_free(pmblink); |
| 339 | } while (pmbe); |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 340 | |
| 341 | write_unlock_irqrestore(&pmb_rwlock, flags); |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 342 | } |
| 343 | |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 344 | static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn) |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 345 | { |
| 346 | return ppn >= __pa(memory_start) && ppn < __pa(memory_end); |
| 347 | } |
| 348 | |
| 349 | static int pmb_synchronize_mappings(void) |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 350 | { |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 351 | unsigned int applied = 0; |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 352 | struct pmb_entry *pmbp = NULL; |
| 353 | int i, j; |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 354 | |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 355 | pr_info("PMB: boot mappings:\n"); |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 356 | |
| 357 | /* |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 358 | * Run through the initial boot mappings, log the established |
| 359 | * ones, and blow away anything that falls outside of the valid |
| 360 | * PPN range. Specifically, we only care about existing mappings |
| 361 | * that impact the cached/uncached sections. |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 362 | * |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 363 | * Note that touching these can be a bit of a minefield; the boot |
| 364 | * loader can establish multi-page mappings with the same caching |
| 365 | * attributes, so we need to ensure that we aren't modifying a |
| 366 | * mapping that we're presently executing from, or may execute |
| 367 | * from in the case of straddling page boundaries. |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 368 | * |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 369 | * In the future we will have to tidy up after the boot loader by |
| 370 | * jumping between the cached and uncached mappings and tearing |
| 371 | * down alternating mappings while executing from the other. |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 372 | */ |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 373 | for (i = 0; i < NR_PMB_ENTRIES; i++) { |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 374 | unsigned long addr, data; |
| 375 | unsigned long addr_val, data_val; |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 376 | unsigned long ppn, vpn, flags; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 377 | unsigned long irqflags; |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 378 | unsigned int size; |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 379 | struct pmb_entry *pmbe; |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 380 | |
| 381 | addr = mk_pmb_addr(i); |
| 382 | data = mk_pmb_data(i); |
| 383 | |
| 384 | addr_val = __raw_readl(addr); |
| 385 | data_val = __raw_readl(data); |
| 386 | |
| 387 | /* |
| 388 | * Skip over any bogus entries |
| 389 | */ |
| 390 | if (!(data_val & PMB_V) || !(addr_val & PMB_V)) |
| 391 | continue; |
| 392 | |
| 393 | ppn = data_val & PMB_PFN_MASK; |
| 394 | vpn = addr_val & PMB_PFN_MASK; |
| 395 | |
| 396 | /* |
| 397 | * Only preserve in-range mappings. |
| 398 | */ |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 399 | if (!pmb_ppn_in_range(ppn)) { |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 400 | /* |
| 401 | * Invalidate anything out of bounds. |
| 402 | */ |
| 403 | __raw_writel(addr_val & ~PMB_V, addr); |
| 404 | __raw_writel(data_val & ~PMB_V, data); |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 405 | continue; |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 406 | } |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * Update the caching attributes if necessary |
| 410 | */ |
| 411 | if (data_val & PMB_C) { |
Paul Mundt | 0065b96 | 2010-02-17 18:05:23 +0900 | [diff] [blame] | 412 | data_val &= ~PMB_CACHE_MASK; |
| 413 | data_val |= pmb_cache_flags(); |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 414 | __raw_writel(data_val, data); |
| 415 | } |
| 416 | |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 417 | size = data_val & PMB_SZ_MASK; |
| 418 | flags = size | (data_val & PMB_CACHE_MASK); |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 419 | |
| 420 | pmbe = pmb_alloc(vpn, ppn, flags, i); |
| 421 | if (IS_ERR(pmbe)) { |
| 422 | WARN_ON_ONCE(1); |
| 423 | continue; |
| 424 | } |
| 425 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 426 | spin_lock_irqsave(&pmbe->lock, irqflags); |
| 427 | |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 428 | for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++) |
| 429 | if (pmb_sizes[j].flag == size) |
| 430 | pmbe->size = pmb_sizes[j].size; |
| 431 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 432 | if (pmbp) { |
| 433 | spin_lock(&pmbp->lock); |
| 434 | |
| 435 | /* |
| 436 | * Compare the previous entry against the current one to |
| 437 | * see if the entries span a contiguous mapping. If so, |
| 438 | * setup the entry links accordingly. |
| 439 | */ |
| 440 | if ((pmbe->vpn == (pmbp->vpn + pmbp->size)) && |
| 441 | (pmbe->ppn == (pmbp->ppn + pmbp->size))) |
| 442 | pmbp->link = pmbe; |
| 443 | |
| 444 | spin_unlock(&pmbp->lock); |
| 445 | } |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 446 | |
| 447 | pmbp = pmbe; |
| 448 | |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 449 | spin_unlock_irqrestore(&pmbe->lock, irqflags); |
| 450 | |
Paul Mundt | d7813bc | 2010-02-17 17:56:38 +0900 | [diff] [blame] | 451 | pr_info("\t0x%08lx -> 0x%08lx [ %ldMB %scached ]\n", |
| 452 | vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, pmbe->size >> 20, |
| 453 | (data_val & PMB_C) ? "" : "un"); |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 454 | |
| 455 | applied++; |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return (applied == 0); |
| 459 | } |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 460 | |
Paul Mundt | 2dc2f8e | 2010-01-21 16:05:25 +0900 | [diff] [blame] | 461 | int pmb_init(void) |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 462 | { |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 463 | int ret; |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 464 | |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 465 | jump_to_uncached(); |
| 466 | |
| 467 | /* |
Matt Fleming | 3d46767 | 2010-01-18 19:33:10 +0900 | [diff] [blame] | 468 | * Sync our software copy of the PMB mappings with those in |
| 469 | * hardware. The mappings in the hardware PMB were either set up |
| 470 | * by the bootloader or very early on by the kernel. |
| 471 | */ |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 472 | ret = pmb_synchronize_mappings(); |
| 473 | if (unlikely(ret == 0)) { |
| 474 | back_to_cached(); |
| 475 | return 0; |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 478 | __raw_writel(0, PMB_IRMCR); |
Paul Mundt | a0ab366 | 2010-01-13 18:31:48 +0900 | [diff] [blame] | 479 | |
Paul Mundt | a0ab366 | 2010-01-13 18:31:48 +0900 | [diff] [blame] | 480 | /* Flush out the TLB */ |
Paul Mundt | efd54ea | 2010-02-16 18:39:30 +0900 | [diff] [blame] | 481 | __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR); |
Paul Mundt | a0ab366 | 2010-01-13 18:31:48 +0900 | [diff] [blame] | 482 | |
Matt Fleming | 20b5014 | 2009-10-06 21:22:33 +0000 | [diff] [blame] | 483 | back_to_cached(); |
| 484 | |
| 485 | return 0; |
| 486 | } |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 487 | |
Paul Mundt | 2efa53b | 2010-01-20 16:40:48 +0900 | [diff] [blame] | 488 | bool __in_29bit_mode(void) |
| 489 | { |
| 490 | return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0; |
| 491 | } |
| 492 | |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 493 | static int pmb_seq_show(struct seq_file *file, void *iter) |
| 494 | { |
| 495 | int i; |
| 496 | |
| 497 | seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n" |
| 498 | "CB: Copy-Back, B: Buffered, UB: Unbuffered\n"); |
| 499 | seq_printf(file, "ety vpn ppn size flags\n"); |
| 500 | |
| 501 | for (i = 0; i < NR_PMB_ENTRIES; i++) { |
| 502 | unsigned long addr, data; |
| 503 | unsigned int size; |
| 504 | char *sz_str = NULL; |
| 505 | |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 506 | addr = __raw_readl(mk_pmb_addr(i)); |
| 507 | data = __raw_readl(mk_pmb_data(i)); |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 508 | |
| 509 | size = data & PMB_SZ_MASK; |
| 510 | sz_str = (size == PMB_SZ_16M) ? " 16MB": |
| 511 | (size == PMB_SZ_64M) ? " 64MB": |
| 512 | (size == PMB_SZ_128M) ? "128MB": |
| 513 | "512MB"; |
| 514 | |
| 515 | /* 02: V 0x88 0x08 128MB C CB B */ |
| 516 | seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n", |
| 517 | i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ', |
| 518 | (addr >> 24) & 0xff, (data >> 24) & 0xff, |
| 519 | sz_str, (data & PMB_C) ? 'C' : ' ', |
| 520 | (data & PMB_WT) ? "WT" : "CB", |
| 521 | (data & PMB_UB) ? "UB" : " B"); |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int pmb_debugfs_open(struct inode *inode, struct file *file) |
| 528 | { |
| 529 | return single_open(file, pmb_seq_show, NULL); |
| 530 | } |
| 531 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 532 | static const struct file_operations pmb_debugfs_fops = { |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 533 | .owner = THIS_MODULE, |
| 534 | .open = pmb_debugfs_open, |
| 535 | .read = seq_read, |
| 536 | .llseek = seq_lseek, |
Li Zefan | 45dabf1 | 2008-06-24 13:30:23 +0800 | [diff] [blame] | 537 | .release = single_release, |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | static int __init pmb_debugfs_init(void) |
| 541 | { |
| 542 | struct dentry *dentry; |
| 543 | |
| 544 | dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO, |
Paul Mundt | b9e393c | 2008-03-07 17:19:58 +0900 | [diff] [blame] | 545 | sh_debugfs_root, NULL, &pmb_debugfs_fops); |
Zhaolei | 25627c7 | 2008-10-17 19:25:09 +0800 | [diff] [blame] | 546 | if (!dentry) |
| 547 | return -ENOMEM; |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 548 | if (IS_ERR(dentry)) |
| 549 | return PTR_ERR(dentry); |
| 550 | |
| 551 | return 0; |
| 552 | } |
Paul Mundt | 0c7b1df | 2006-09-27 15:08:07 +0900 | [diff] [blame] | 553 | postcore_initcall(pmb_debugfs_init); |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 554 | |
| 555 | #ifdef CONFIG_PM |
| 556 | static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state) |
| 557 | { |
| 558 | static pm_message_t prev_state; |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 559 | int i; |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 560 | |
| 561 | /* Restore the PMB after a resume from hibernation */ |
| 562 | if (state.event == PM_EVENT_ON && |
| 563 | prev_state.event == PM_EVENT_FREEZE) { |
| 564 | struct pmb_entry *pmbe; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 565 | |
| 566 | read_lock(&pmb_rwlock); |
| 567 | |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 568 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
Paul Mundt | 51becfd | 2010-02-17 15:33:30 +0900 | [diff] [blame] | 569 | if (test_bit(i, pmb_map)) { |
Matt Fleming | edd7de8 | 2009-10-06 21:22:29 +0000 | [diff] [blame] | 570 | pmbe = &pmb_entry_list[i]; |
| 571 | set_pmb_entry(pmbe); |
| 572 | } |
| 573 | } |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 574 | |
| 575 | read_unlock(&pmb_rwlock); |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 576 | } |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 577 | |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 578 | prev_state = state; |
Paul Mundt | d53a0d3 | 2010-02-17 21:17:02 +0900 | [diff] [blame^] | 579 | |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int pmb_sysdev_resume(struct sys_device *dev) |
| 584 | { |
| 585 | return pmb_sysdev_suspend(dev, PMSG_ON); |
| 586 | } |
| 587 | |
| 588 | static struct sysdev_driver pmb_sysdev_driver = { |
| 589 | .suspend = pmb_sysdev_suspend, |
| 590 | .resume = pmb_sysdev_resume, |
| 591 | }; |
| 592 | |
| 593 | static int __init pmb_sysdev_init(void) |
| 594 | { |
| 595 | return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver); |
| 596 | } |
Francesco VIRLINZI | a83c0b7 | 2009-03-11 10:39:02 +0000 | [diff] [blame] | 597 | subsys_initcall(pmb_sysdev_init); |
| 598 | #endif |