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