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 | * |
| 6 | * Copyright (C) 2005 Paul Mundt |
| 7 | * |
| 8 | * P1/P2 Section mapping definitions from map32.h, which was: |
| 9 | * |
| 10 | * Copyright 2003 (c) Lineo Solutions,Inc. |
| 11 | * |
| 12 | * This file is subject to the terms and conditions of the GNU General Public |
| 13 | * License. See the file "COPYING" in the main directory of this archive |
| 14 | * for more details. |
| 15 | */ |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/debugfs.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <asm/system.h> |
| 26 | #include <asm/uaccess.h> |
| 27 | #include <asm/mmu.h> |
| 28 | #include <asm/io.h> |
| 29 | |
| 30 | #define NR_PMB_ENTRIES 16 |
| 31 | |
| 32 | static kmem_cache_t *pmb_cache; |
| 33 | static unsigned long pmb_map; |
| 34 | |
| 35 | static struct pmb_entry pmb_init_map[] = { |
| 36 | /* vpn ppn flags (ub/sz/c/wt) */ |
| 37 | |
| 38 | /* P1 Section Mappings */ |
| 39 | { 0x80000000, 0x00000000, PMB_SZ_64M | PMB_C, }, |
| 40 | { 0x84000000, 0x04000000, PMB_SZ_64M | PMB_C, }, |
| 41 | { 0x88000000, 0x08000000, PMB_SZ_128M | PMB_C, }, |
| 42 | { 0x90000000, 0x10000000, PMB_SZ_64M | PMB_C, }, |
| 43 | { 0x94000000, 0x14000000, PMB_SZ_64M | PMB_C, }, |
| 44 | { 0x98000000, 0x18000000, PMB_SZ_64M | PMB_C, }, |
| 45 | |
| 46 | /* P2 Section Mappings */ |
| 47 | { 0xa0000000, 0x00000000, PMB_UB | PMB_SZ_64M | PMB_WT, }, |
| 48 | { 0xa4000000, 0x04000000, PMB_UB | PMB_SZ_64M | PMB_WT, }, |
| 49 | { 0xa8000000, 0x08000000, PMB_UB | PMB_SZ_128M | PMB_WT, }, |
| 50 | { 0xb0000000, 0x10000000, PMB_UB | PMB_SZ_64M | PMB_WT, }, |
| 51 | { 0xb4000000, 0x14000000, PMB_UB | PMB_SZ_64M | PMB_WT, }, |
| 52 | { 0xb8000000, 0x18000000, PMB_UB | PMB_SZ_64M | PMB_WT, }, |
| 53 | }; |
| 54 | |
| 55 | static inline unsigned long mk_pmb_entry(unsigned int entry) |
| 56 | { |
| 57 | return (entry & PMB_E_MASK) << PMB_E_SHIFT; |
| 58 | } |
| 59 | |
| 60 | static inline unsigned long mk_pmb_addr(unsigned int entry) |
| 61 | { |
| 62 | return mk_pmb_entry(entry) | PMB_ADDR; |
| 63 | } |
| 64 | |
| 65 | static inline unsigned long mk_pmb_data(unsigned int entry) |
| 66 | { |
| 67 | return mk_pmb_entry(entry) | PMB_DATA; |
| 68 | } |
| 69 | |
| 70 | struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn, |
| 71 | unsigned long flags) |
| 72 | { |
| 73 | struct pmb_entry *pmbe; |
| 74 | |
| 75 | pmbe = kmem_cache_alloc(pmb_cache, GFP_KERNEL); |
| 76 | if (!pmbe) |
| 77 | return ERR_PTR(-ENOMEM); |
| 78 | |
| 79 | pmbe->vpn = vpn; |
| 80 | pmbe->ppn = ppn; |
| 81 | pmbe->flags = flags; |
| 82 | |
| 83 | return pmbe; |
| 84 | } |
| 85 | |
| 86 | void pmb_free(struct pmb_entry *pmbe) |
| 87 | { |
| 88 | kmem_cache_free(pmb_cache, pmbe); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Must be in P2 for __set_pmb_entry() |
| 93 | */ |
| 94 | int __set_pmb_entry(unsigned long vpn, unsigned long ppn, |
| 95 | unsigned long flags, int *entry) |
| 96 | { |
| 97 | unsigned int pos = *entry; |
| 98 | |
| 99 | if (unlikely(pos == PMB_NO_ENTRY)) |
| 100 | pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES); |
| 101 | |
| 102 | repeat: |
| 103 | if (unlikely(pos > NR_PMB_ENTRIES)) |
| 104 | return -ENOSPC; |
| 105 | |
| 106 | if (test_and_set_bit(pos, &pmb_map)) { |
| 107 | pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES); |
| 108 | goto repeat; |
| 109 | } |
| 110 | |
| 111 | ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos)); |
| 112 | |
| 113 | #ifdef CONFIG_SH_WRITETHROUGH |
| 114 | /* |
| 115 | * When we are in 32-bit address extended mode, CCR.CB becomes |
| 116 | * invalid, so care must be taken to manually adjust cacheable |
| 117 | * translations. |
| 118 | */ |
| 119 | if (likely(flags & PMB_C)) |
| 120 | flags |= PMB_WT; |
| 121 | #endif |
| 122 | |
| 123 | ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos)); |
| 124 | |
| 125 | *entry = pos; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void set_pmb_entry(struct pmb_entry *pmbe) |
| 131 | { |
| 132 | jump_to_P2(); |
| 133 | __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, &pmbe->entry); |
| 134 | back_to_P1(); |
| 135 | } |
| 136 | |
| 137 | void clear_pmb_entry(struct pmb_entry *pmbe) |
| 138 | { |
| 139 | unsigned int entry = pmbe->entry; |
| 140 | unsigned long addr; |
| 141 | |
| 142 | /* |
| 143 | * Don't allow clearing of wired init entries, P1 or P2 access |
| 144 | * without a corresponding mapping in the PMB will lead to reset |
| 145 | * by the TLB. |
| 146 | */ |
| 147 | if (unlikely(entry < ARRAY_SIZE(pmb_init_map) || |
| 148 | entry >= NR_PMB_ENTRIES)) |
| 149 | return; |
| 150 | |
| 151 | jump_to_P2(); |
| 152 | |
| 153 | /* Clear V-bit */ |
| 154 | addr = mk_pmb_addr(entry); |
| 155 | ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr); |
| 156 | |
| 157 | addr = mk_pmb_data(entry); |
| 158 | ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr); |
| 159 | |
| 160 | back_to_P1(); |
| 161 | |
| 162 | clear_bit(entry, &pmb_map); |
| 163 | } |
| 164 | |
| 165 | static void pmb_cache_ctor(void *pmb, kmem_cache_t *cachep, unsigned long flags) |
| 166 | { |
| 167 | memset(pmb, 0, sizeof(struct pmb_entry)); |
| 168 | |
| 169 | ((struct pmb_entry *)pmb)->entry = PMB_NO_ENTRY; |
| 170 | } |
| 171 | |
| 172 | static int __init pmb_init(void) |
| 173 | { |
| 174 | unsigned int nr_entries = ARRAY_SIZE(pmb_init_map); |
| 175 | unsigned int entry; |
| 176 | |
| 177 | BUG_ON(unlikely(nr_entries >= NR_PMB_ENTRIES)); |
| 178 | |
| 179 | pmb_cache = kmem_cache_create("pmb", sizeof(struct pmb_entry), |
| 180 | 0, 0, pmb_cache_ctor, NULL); |
| 181 | BUG_ON(!pmb_cache); |
| 182 | |
| 183 | jump_to_P2(); |
| 184 | |
| 185 | /* |
| 186 | * Ordering is important, P2 must be mapped in the PMB before we |
| 187 | * can set PMB.SE, and P1 must be mapped before we jump back to |
| 188 | * P1 space. |
| 189 | */ |
| 190 | for (entry = 0; entry < nr_entries; entry++) { |
| 191 | struct pmb_entry *pmbe = pmb_init_map + entry; |
| 192 | |
| 193 | __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, &entry); |
| 194 | } |
| 195 | |
| 196 | ctrl_outl(0, PMB_IRMCR); |
| 197 | |
| 198 | /* PMB.SE and UB[7] */ |
| 199 | ctrl_outl((1 << 31) | (1 << 7), PMB_PASCR); |
| 200 | |
| 201 | back_to_P1(); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | arch_initcall(pmb_init); |
| 207 | |
| 208 | #ifdef CONFIG_DEBUG_FS |
| 209 | static int pmb_seq_show(struct seq_file *file, void *iter) |
| 210 | { |
| 211 | int i; |
| 212 | |
| 213 | seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n" |
| 214 | "CB: Copy-Back, B: Buffered, UB: Unbuffered\n"); |
| 215 | seq_printf(file, "ety vpn ppn size flags\n"); |
| 216 | |
| 217 | for (i = 0; i < NR_PMB_ENTRIES; i++) { |
| 218 | unsigned long addr, data; |
| 219 | unsigned int size; |
| 220 | char *sz_str = NULL; |
| 221 | |
| 222 | addr = ctrl_inl(mk_pmb_addr(i)); |
| 223 | data = ctrl_inl(mk_pmb_data(i)); |
| 224 | |
| 225 | size = data & PMB_SZ_MASK; |
| 226 | sz_str = (size == PMB_SZ_16M) ? " 16MB": |
| 227 | (size == PMB_SZ_64M) ? " 64MB": |
| 228 | (size == PMB_SZ_128M) ? "128MB": |
| 229 | "512MB"; |
| 230 | |
| 231 | /* 02: V 0x88 0x08 128MB C CB B */ |
| 232 | seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n", |
| 233 | i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ', |
| 234 | (addr >> 24) & 0xff, (data >> 24) & 0xff, |
| 235 | sz_str, (data & PMB_C) ? 'C' : ' ', |
| 236 | (data & PMB_WT) ? "WT" : "CB", |
| 237 | (data & PMB_UB) ? "UB" : " B"); |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int pmb_debugfs_open(struct inode *inode, struct file *file) |
| 244 | { |
| 245 | return single_open(file, pmb_seq_show, NULL); |
| 246 | } |
| 247 | |
| 248 | static struct file_operations pmb_debugfs_fops = { |
| 249 | .owner = THIS_MODULE, |
| 250 | .open = pmb_debugfs_open, |
| 251 | .read = seq_read, |
| 252 | .llseek = seq_lseek, |
| 253 | .release = seq_release, |
| 254 | }; |
| 255 | |
| 256 | static int __init pmb_debugfs_init(void) |
| 257 | { |
| 258 | struct dentry *dentry; |
| 259 | |
| 260 | dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO, |
| 261 | NULL, NULL, &pmb_debugfs_fops); |
| 262 | if (IS_ERR(dentry)) |
| 263 | return PTR_ERR(dentry); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | postcore_initcall(pmb_debugfs_init); |
| 269 | #endif |