| 1 | /* |
| 2 | * arch/sh/mm/pmb.c |
| 3 | * |
| 4 | * Privileged Space Mapping Buffer (PMB) Support. |
| 5 | * |
| 6 | * Copyright (C) 2005 - 2010 Paul Mundt |
| 7 | * Copyright (C) 2010 Matt Fleming |
| 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> |
| 15 | #include <linux/sysdev.h> |
| 16 | #include <linux/cpu.h> |
| 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 <linux/io.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <asm/sizes.h> |
| 28 | #include <asm/system.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/pgtable.h> |
| 31 | #include <asm/page.h> |
| 32 | #include <asm/mmu.h> |
| 33 | #include <asm/mmu_context.h> |
| 34 | |
| 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 | |
| 55 | static struct { |
| 56 | unsigned long size; |
| 57 | int flag; |
| 58 | } pmb_sizes[] = { |
| 59 | { .size = SZ_512M, .flag = PMB_SZ_512M, }, |
| 60 | { .size = SZ_128M, .flag = PMB_SZ_128M, }, |
| 61 | { .size = SZ_64M, .flag = PMB_SZ_64M, }, |
| 62 | { .size = SZ_16M, .flag = PMB_SZ_16M, }, |
| 63 | }; |
| 64 | |
| 65 | static void pmb_unmap_entry(struct pmb_entry *, int depth); |
| 66 | |
| 67 | static DEFINE_RWLOCK(pmb_rwlock); |
| 68 | static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES]; |
| 69 | static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES); |
| 70 | |
| 71 | static __always_inline unsigned long mk_pmb_entry(unsigned int entry) |
| 72 | { |
| 73 | return (entry & PMB_E_MASK) << PMB_E_SHIFT; |
| 74 | } |
| 75 | |
| 76 | static __always_inline unsigned long mk_pmb_addr(unsigned int entry) |
| 77 | { |
| 78 | return mk_pmb_entry(entry) | PMB_ADDR; |
| 79 | } |
| 80 | |
| 81 | static __always_inline unsigned long mk_pmb_data(unsigned int entry) |
| 82 | { |
| 83 | return mk_pmb_entry(entry) | PMB_DATA; |
| 84 | } |
| 85 | |
| 86 | static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn) |
| 87 | { |
| 88 | return ppn >= __pa(memory_start) && ppn < __pa(memory_end); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Ensure that the PMB entries match our cache configuration. |
| 93 | * |
| 94 | * When we are in 32-bit address extended mode, CCR.CB becomes |
| 95 | * invalid, so care must be taken to manually adjust cacheable |
| 96 | * translations. |
| 97 | */ |
| 98 | static __always_inline unsigned long pmb_cache_flags(void) |
| 99 | { |
| 100 | unsigned long flags = 0; |
| 101 | |
| 102 | #if defined(CONFIG_CACHE_OFF) |
| 103 | flags |= PMB_WT | PMB_UB; |
| 104 | #elif defined(CONFIG_CACHE_WRITETHROUGH) |
| 105 | flags |= PMB_C | PMB_WT | PMB_UB; |
| 106 | #elif defined(CONFIG_CACHE_WRITEBACK) |
| 107 | flags |= PMB_C; |
| 108 | #endif |
| 109 | |
| 110 | return flags; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Convert typical pgprot value to the PMB equivalent |
| 115 | */ |
| 116 | static inline unsigned long pgprot_to_pmb_flags(pgprot_t prot) |
| 117 | { |
| 118 | unsigned long pmb_flags = 0; |
| 119 | u64 flags = pgprot_val(prot); |
| 120 | |
| 121 | if (flags & _PAGE_CACHABLE) |
| 122 | pmb_flags |= PMB_C; |
| 123 | if (flags & _PAGE_WT) |
| 124 | pmb_flags |= PMB_WT | PMB_UB; |
| 125 | |
| 126 | return pmb_flags; |
| 127 | } |
| 128 | |
| 129 | static bool pmb_can_merge(struct pmb_entry *a, struct pmb_entry *b) |
| 130 | { |
| 131 | return (b->vpn == (a->vpn + a->size)) && |
| 132 | (b->ppn == (a->ppn + a->size)) && |
| 133 | (b->flags == a->flags); |
| 134 | } |
| 135 | |
| 136 | static bool pmb_size_valid(unsigned long size) |
| 137 | { |
| 138 | int i; |
| 139 | |
| 140 | for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) |
| 141 | if (pmb_sizes[i].size == size) |
| 142 | return true; |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | static inline bool pmb_addr_valid(unsigned long addr, unsigned long size) |
| 148 | { |
| 149 | return (addr >= P1SEG && (addr + size - 1) < P3SEG); |
| 150 | } |
| 151 | |
| 152 | static inline bool pmb_prot_valid(pgprot_t prot) |
| 153 | { |
| 154 | return (pgprot_val(prot) & _PAGE_USER) == 0; |
| 155 | } |
| 156 | |
| 157 | static int pmb_size_to_flags(unsigned long size) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) |
| 162 | if (pmb_sizes[i].size == size) |
| 163 | return pmb_sizes[i].flag; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int pmb_alloc_entry(void) |
| 169 | { |
| 170 | int pos; |
| 171 | |
| 172 | pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES); |
| 173 | if (pos >= 0 && pos < NR_PMB_ENTRIES) |
| 174 | __set_bit(pos, pmb_map); |
| 175 | else |
| 176 | pos = -ENOSPC; |
| 177 | |
| 178 | return pos; |
| 179 | } |
| 180 | |
| 181 | static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn, |
| 182 | unsigned long flags, int entry) |
| 183 | { |
| 184 | struct pmb_entry *pmbe; |
| 185 | unsigned long irqflags; |
| 186 | void *ret = NULL; |
| 187 | int pos; |
| 188 | |
| 189 | write_lock_irqsave(&pmb_rwlock, irqflags); |
| 190 | |
| 191 | if (entry == PMB_NO_ENTRY) { |
| 192 | pos = pmb_alloc_entry(); |
| 193 | if (unlikely(pos < 0)) { |
| 194 | ret = ERR_PTR(pos); |
| 195 | goto out; |
| 196 | } |
| 197 | } else { |
| 198 | if (__test_and_set_bit(entry, pmb_map)) { |
| 199 | ret = ERR_PTR(-ENOSPC); |
| 200 | goto out; |
| 201 | } |
| 202 | |
| 203 | pos = entry; |
| 204 | } |
| 205 | |
| 206 | write_unlock_irqrestore(&pmb_rwlock, irqflags); |
| 207 | |
| 208 | pmbe = &pmb_entry_list[pos]; |
| 209 | |
| 210 | memset(pmbe, 0, sizeof(struct pmb_entry)); |
| 211 | |
| 212 | spin_lock_init(&pmbe->lock); |
| 213 | |
| 214 | pmbe->vpn = vpn; |
| 215 | pmbe->ppn = ppn; |
| 216 | pmbe->flags = flags; |
| 217 | pmbe->entry = pos; |
| 218 | |
| 219 | return pmbe; |
| 220 | |
| 221 | out: |
| 222 | write_unlock_irqrestore(&pmb_rwlock, irqflags); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | static void pmb_free(struct pmb_entry *pmbe) |
| 227 | { |
| 228 | __clear_bit(pmbe->entry, pmb_map); |
| 229 | |
| 230 | pmbe->entry = PMB_NO_ENTRY; |
| 231 | pmbe->link = NULL; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Must be run uncached. |
| 236 | */ |
| 237 | static void __set_pmb_entry(struct pmb_entry *pmbe) |
| 238 | { |
| 239 | /* Set V-bit */ |
| 240 | __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, mk_pmb_data(pmbe->entry)); |
| 241 | __raw_writel(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry)); |
| 242 | } |
| 243 | |
| 244 | static void __clear_pmb_entry(struct pmb_entry *pmbe) |
| 245 | { |
| 246 | unsigned long addr, data; |
| 247 | unsigned long addr_val, data_val; |
| 248 | |
| 249 | addr = mk_pmb_addr(pmbe->entry); |
| 250 | data = mk_pmb_data(pmbe->entry); |
| 251 | |
| 252 | addr_val = __raw_readl(addr); |
| 253 | data_val = __raw_readl(data); |
| 254 | |
| 255 | /* Clear V-bit */ |
| 256 | writel_uncached(addr_val & ~PMB_V, addr); |
| 257 | writel_uncached(data_val & ~PMB_V, data); |
| 258 | } |
| 259 | |
| 260 | static void set_pmb_entry(struct pmb_entry *pmbe) |
| 261 | { |
| 262 | unsigned long flags; |
| 263 | |
| 264 | spin_lock_irqsave(&pmbe->lock, flags); |
| 265 | __set_pmb_entry(pmbe); |
| 266 | spin_unlock_irqrestore(&pmbe->lock, flags); |
| 267 | } |
| 268 | |
| 269 | int pmb_bolt_mapping(unsigned long vaddr, phys_addr_t phys, |
| 270 | unsigned long size, pgprot_t prot) |
| 271 | { |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | void __iomem *pmb_remap_caller(phys_addr_t phys, unsigned long size, |
| 276 | pgprot_t prot, void *caller) |
| 277 | { |
| 278 | struct pmb_entry *pmbp, *pmbe; |
| 279 | unsigned long pmb_flags; |
| 280 | int i, mapped; |
| 281 | unsigned long orig_addr, vaddr; |
| 282 | phys_addr_t offset, last_addr; |
| 283 | phys_addr_t align_mask; |
| 284 | unsigned long aligned; |
| 285 | struct vm_struct *area; |
| 286 | |
| 287 | /* |
| 288 | * Small mappings need to go through the TLB. |
| 289 | */ |
| 290 | if (size < SZ_16M) |
| 291 | return ERR_PTR(-EINVAL); |
| 292 | if (!pmb_prot_valid(prot)) |
| 293 | return ERR_PTR(-EINVAL); |
| 294 | |
| 295 | pmbp = NULL; |
| 296 | pmb_flags = pgprot_to_pmb_flags(prot); |
| 297 | mapped = 0; |
| 298 | |
| 299 | for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) |
| 300 | if (size >= pmb_sizes[i].size) |
| 301 | break; |
| 302 | |
| 303 | last_addr = phys + size; |
| 304 | align_mask = ~(pmb_sizes[i].size - 1); |
| 305 | offset = phys & ~align_mask; |
| 306 | phys &= align_mask; |
| 307 | aligned = ALIGN(last_addr, pmb_sizes[i].size) - phys; |
| 308 | |
| 309 | area = __get_vm_area_caller(aligned, VM_IOREMAP, uncached_end, |
| 310 | P3SEG, caller); |
| 311 | if (!area) |
| 312 | return NULL; |
| 313 | |
| 314 | area->phys_addr = phys; |
| 315 | orig_addr = vaddr = (unsigned long)area->addr; |
| 316 | |
| 317 | if (!pmb_addr_valid(vaddr, aligned)) |
| 318 | return ERR_PTR(-EFAULT); |
| 319 | |
| 320 | again: |
| 321 | for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) { |
| 322 | unsigned long flags; |
| 323 | |
| 324 | if (size < pmb_sizes[i].size) |
| 325 | continue; |
| 326 | |
| 327 | pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag, |
| 328 | PMB_NO_ENTRY); |
| 329 | if (IS_ERR(pmbe)) { |
| 330 | pmb_unmap_entry(pmbp, mapped); |
| 331 | return pmbe; |
| 332 | } |
| 333 | |
| 334 | spin_lock_irqsave(&pmbe->lock, flags); |
| 335 | |
| 336 | pmbe->size = pmb_sizes[i].size; |
| 337 | |
| 338 | __set_pmb_entry(pmbe); |
| 339 | |
| 340 | phys += pmbe->size; |
| 341 | vaddr += pmbe->size; |
| 342 | size -= pmbe->size; |
| 343 | |
| 344 | /* |
| 345 | * Link adjacent entries that span multiple PMB entries |
| 346 | * for easier tear-down. |
| 347 | */ |
| 348 | if (likely(pmbp)) { |
| 349 | spin_lock(&pmbp->lock); |
| 350 | pmbp->link = pmbe; |
| 351 | spin_unlock(&pmbp->lock); |
| 352 | } |
| 353 | |
| 354 | pmbp = pmbe; |
| 355 | |
| 356 | /* |
| 357 | * Instead of trying smaller sizes on every iteration |
| 358 | * (even if we succeed in allocating space), try using |
| 359 | * pmb_sizes[i].size again. |
| 360 | */ |
| 361 | i--; |
| 362 | mapped++; |
| 363 | |
| 364 | spin_unlock_irqrestore(&pmbe->lock, flags); |
| 365 | } |
| 366 | |
| 367 | if (size >= SZ_16M) |
| 368 | goto again; |
| 369 | |
| 370 | return (void __iomem *)(offset + (char *)orig_addr); |
| 371 | } |
| 372 | |
| 373 | int pmb_unmap(void __iomem *addr) |
| 374 | { |
| 375 | struct pmb_entry *pmbe = NULL; |
| 376 | unsigned long vaddr = (unsigned long __force)addr; |
| 377 | int i, found = 0; |
| 378 | |
| 379 | read_lock(&pmb_rwlock); |
| 380 | |
| 381 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
| 382 | if (test_bit(i, pmb_map)) { |
| 383 | pmbe = &pmb_entry_list[i]; |
| 384 | if (pmbe->vpn == vaddr) { |
| 385 | found = 1; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | read_unlock(&pmb_rwlock); |
| 392 | |
| 393 | if (found) { |
| 394 | pmb_unmap_entry(pmbe, NR_PMB_ENTRIES); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | return -EINVAL; |
| 399 | } |
| 400 | |
| 401 | static void __pmb_unmap_entry(struct pmb_entry *pmbe, int depth) |
| 402 | { |
| 403 | do { |
| 404 | struct pmb_entry *pmblink = pmbe; |
| 405 | |
| 406 | /* |
| 407 | * We may be called before this pmb_entry has been |
| 408 | * entered into the PMB table via set_pmb_entry(), but |
| 409 | * that's OK because we've allocated a unique slot for |
| 410 | * this entry in pmb_alloc() (even if we haven't filled |
| 411 | * it yet). |
| 412 | * |
| 413 | * Therefore, calling __clear_pmb_entry() is safe as no |
| 414 | * other mapping can be using that slot. |
| 415 | */ |
| 416 | __clear_pmb_entry(pmbe); |
| 417 | |
| 418 | pmbe = pmblink->link; |
| 419 | |
| 420 | pmb_free(pmblink); |
| 421 | } while (pmbe && --depth); |
| 422 | } |
| 423 | |
| 424 | static void pmb_unmap_entry(struct pmb_entry *pmbe, int depth) |
| 425 | { |
| 426 | unsigned long flags; |
| 427 | |
| 428 | if (unlikely(!pmbe)) |
| 429 | return; |
| 430 | |
| 431 | write_lock_irqsave(&pmb_rwlock, flags); |
| 432 | __pmb_unmap_entry(pmbe, depth); |
| 433 | write_unlock_irqrestore(&pmb_rwlock, flags); |
| 434 | } |
| 435 | |
| 436 | static void __init pmb_notify(void) |
| 437 | { |
| 438 | int i; |
| 439 | |
| 440 | pr_info("PMB: boot mappings:\n"); |
| 441 | |
| 442 | read_lock(&pmb_rwlock); |
| 443 | |
| 444 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
| 445 | struct pmb_entry *pmbe; |
| 446 | |
| 447 | if (!test_bit(i, pmb_map)) |
| 448 | continue; |
| 449 | |
| 450 | pmbe = &pmb_entry_list[i]; |
| 451 | |
| 452 | pr_info(" 0x%08lx -> 0x%08lx [ %4ldMB %2scached ]\n", |
| 453 | pmbe->vpn >> PAGE_SHIFT, pmbe->ppn >> PAGE_SHIFT, |
| 454 | pmbe->size >> 20, (pmbe->flags & PMB_C) ? "" : "un"); |
| 455 | } |
| 456 | |
| 457 | read_unlock(&pmb_rwlock); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Sync our software copy of the PMB mappings with those in hardware. The |
| 462 | * mappings in the hardware PMB were either set up by the bootloader or |
| 463 | * very early on by the kernel. |
| 464 | */ |
| 465 | static void __init pmb_synchronize(void) |
| 466 | { |
| 467 | struct pmb_entry *pmbp = NULL; |
| 468 | int i, j; |
| 469 | |
| 470 | /* |
| 471 | * Run through the initial boot mappings, log the established |
| 472 | * ones, and blow away anything that falls outside of the valid |
| 473 | * PPN range. Specifically, we only care about existing mappings |
| 474 | * that impact the cached/uncached sections. |
| 475 | * |
| 476 | * Note that touching these can be a bit of a minefield; the boot |
| 477 | * loader can establish multi-page mappings with the same caching |
| 478 | * attributes, so we need to ensure that we aren't modifying a |
| 479 | * mapping that we're presently executing from, or may execute |
| 480 | * from in the case of straddling page boundaries. |
| 481 | * |
| 482 | * In the future we will have to tidy up after the boot loader by |
| 483 | * jumping between the cached and uncached mappings and tearing |
| 484 | * down alternating mappings while executing from the other. |
| 485 | */ |
| 486 | for (i = 0; i < NR_PMB_ENTRIES; i++) { |
| 487 | unsigned long addr, data; |
| 488 | unsigned long addr_val, data_val; |
| 489 | unsigned long ppn, vpn, flags; |
| 490 | unsigned long irqflags; |
| 491 | unsigned int size; |
| 492 | struct pmb_entry *pmbe; |
| 493 | |
| 494 | addr = mk_pmb_addr(i); |
| 495 | data = mk_pmb_data(i); |
| 496 | |
| 497 | addr_val = __raw_readl(addr); |
| 498 | data_val = __raw_readl(data); |
| 499 | |
| 500 | /* |
| 501 | * Skip over any bogus entries |
| 502 | */ |
| 503 | if (!(data_val & PMB_V) || !(addr_val & PMB_V)) |
| 504 | continue; |
| 505 | |
| 506 | ppn = data_val & PMB_PFN_MASK; |
| 507 | vpn = addr_val & PMB_PFN_MASK; |
| 508 | |
| 509 | /* |
| 510 | * Only preserve in-range mappings. |
| 511 | */ |
| 512 | if (!pmb_ppn_in_range(ppn)) { |
| 513 | /* |
| 514 | * Invalidate anything out of bounds. |
| 515 | */ |
| 516 | writel_uncached(addr_val & ~PMB_V, addr); |
| 517 | writel_uncached(data_val & ~PMB_V, data); |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Update the caching attributes if necessary |
| 523 | */ |
| 524 | if (data_val & PMB_C) { |
| 525 | data_val &= ~PMB_CACHE_MASK; |
| 526 | data_val |= pmb_cache_flags(); |
| 527 | |
| 528 | writel_uncached(data_val, data); |
| 529 | } |
| 530 | |
| 531 | size = data_val & PMB_SZ_MASK; |
| 532 | flags = size | (data_val & PMB_CACHE_MASK); |
| 533 | |
| 534 | pmbe = pmb_alloc(vpn, ppn, flags, i); |
| 535 | if (IS_ERR(pmbe)) { |
| 536 | WARN_ON_ONCE(1); |
| 537 | continue; |
| 538 | } |
| 539 | |
| 540 | spin_lock_irqsave(&pmbe->lock, irqflags); |
| 541 | |
| 542 | for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++) |
| 543 | if (pmb_sizes[j].flag == size) |
| 544 | pmbe->size = pmb_sizes[j].size; |
| 545 | |
| 546 | if (pmbp) { |
| 547 | spin_lock(&pmbp->lock); |
| 548 | |
| 549 | /* |
| 550 | * Compare the previous entry against the current one to |
| 551 | * see if the entries span a contiguous mapping. If so, |
| 552 | * setup the entry links accordingly. Compound mappings |
| 553 | * are later coalesced. |
| 554 | */ |
| 555 | if (pmb_can_merge(pmbp, pmbe)) |
| 556 | pmbp->link = pmbe; |
| 557 | |
| 558 | spin_unlock(&pmbp->lock); |
| 559 | } |
| 560 | |
| 561 | pmbp = pmbe; |
| 562 | |
| 563 | spin_unlock_irqrestore(&pmbe->lock, irqflags); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | static void __init pmb_merge(struct pmb_entry *head) |
| 568 | { |
| 569 | unsigned long span, newsize; |
| 570 | struct pmb_entry *tail; |
| 571 | int i = 1, depth = 0; |
| 572 | |
| 573 | span = newsize = head->size; |
| 574 | |
| 575 | tail = head->link; |
| 576 | while (tail) { |
| 577 | span += tail->size; |
| 578 | |
| 579 | if (pmb_size_valid(span)) { |
| 580 | newsize = span; |
| 581 | depth = i; |
| 582 | } |
| 583 | |
| 584 | /* This is the end of the line.. */ |
| 585 | if (!tail->link) |
| 586 | break; |
| 587 | |
| 588 | tail = tail->link; |
| 589 | i++; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * The merged page size must be valid. |
| 594 | */ |
| 595 | if (!pmb_size_valid(newsize)) |
| 596 | return; |
| 597 | |
| 598 | head->flags &= ~PMB_SZ_MASK; |
| 599 | head->flags |= pmb_size_to_flags(newsize); |
| 600 | |
| 601 | head->size = newsize; |
| 602 | |
| 603 | __pmb_unmap_entry(head->link, depth); |
| 604 | __set_pmb_entry(head); |
| 605 | } |
| 606 | |
| 607 | static void __init pmb_coalesce(void) |
| 608 | { |
| 609 | unsigned long flags; |
| 610 | int i; |
| 611 | |
| 612 | write_lock_irqsave(&pmb_rwlock, flags); |
| 613 | |
| 614 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
| 615 | struct pmb_entry *pmbe; |
| 616 | |
| 617 | if (!test_bit(i, pmb_map)) |
| 618 | continue; |
| 619 | |
| 620 | pmbe = &pmb_entry_list[i]; |
| 621 | |
| 622 | /* |
| 623 | * We're only interested in compound mappings |
| 624 | */ |
| 625 | if (!pmbe->link) |
| 626 | continue; |
| 627 | |
| 628 | /* |
| 629 | * Nothing to do if it already uses the largest possible |
| 630 | * page size. |
| 631 | */ |
| 632 | if (pmbe->size == SZ_512M) |
| 633 | continue; |
| 634 | |
| 635 | pmb_merge(pmbe); |
| 636 | } |
| 637 | |
| 638 | write_unlock_irqrestore(&pmb_rwlock, flags); |
| 639 | } |
| 640 | |
| 641 | #ifdef CONFIG_UNCACHED_MAPPING |
| 642 | static void __init pmb_resize(void) |
| 643 | { |
| 644 | int i; |
| 645 | |
| 646 | /* |
| 647 | * If the uncached mapping was constructed by the kernel, it will |
| 648 | * already be a reasonable size. |
| 649 | */ |
| 650 | if (uncached_size == SZ_16M) |
| 651 | return; |
| 652 | |
| 653 | read_lock(&pmb_rwlock); |
| 654 | |
| 655 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
| 656 | struct pmb_entry *pmbe; |
| 657 | unsigned long flags; |
| 658 | |
| 659 | if (!test_bit(i, pmb_map)) |
| 660 | continue; |
| 661 | |
| 662 | pmbe = &pmb_entry_list[i]; |
| 663 | |
| 664 | if (pmbe->vpn != uncached_start) |
| 665 | continue; |
| 666 | |
| 667 | /* |
| 668 | * Found it, now resize it. |
| 669 | */ |
| 670 | spin_lock_irqsave(&pmbe->lock, flags); |
| 671 | |
| 672 | pmbe->size = SZ_16M; |
| 673 | pmbe->flags &= ~PMB_SZ_MASK; |
| 674 | pmbe->flags |= pmb_size_to_flags(pmbe->size); |
| 675 | |
| 676 | uncached_resize(pmbe->size); |
| 677 | |
| 678 | __set_pmb_entry(pmbe); |
| 679 | |
| 680 | spin_unlock_irqrestore(&pmbe->lock, flags); |
| 681 | } |
| 682 | |
| 683 | read_lock(&pmb_rwlock); |
| 684 | } |
| 685 | #endif |
| 686 | |
| 687 | void __init pmb_init(void) |
| 688 | { |
| 689 | /* Synchronize software state */ |
| 690 | pmb_synchronize(); |
| 691 | |
| 692 | /* Attempt to combine compound mappings */ |
| 693 | pmb_coalesce(); |
| 694 | |
| 695 | #ifdef CONFIG_UNCACHED_MAPPING |
| 696 | /* Resize initial mappings, if necessary */ |
| 697 | pmb_resize(); |
| 698 | #endif |
| 699 | |
| 700 | /* Log them */ |
| 701 | pmb_notify(); |
| 702 | |
| 703 | writel_uncached(0, PMB_IRMCR); |
| 704 | |
| 705 | /* Flush out the TLB */ |
| 706 | __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR); |
| 707 | ctrl_barrier(); |
| 708 | } |
| 709 | |
| 710 | bool __in_29bit_mode(void) |
| 711 | { |
| 712 | return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0; |
| 713 | } |
| 714 | |
| 715 | static int pmb_seq_show(struct seq_file *file, void *iter) |
| 716 | { |
| 717 | int i; |
| 718 | |
| 719 | seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n" |
| 720 | "CB: Copy-Back, B: Buffered, UB: Unbuffered\n"); |
| 721 | seq_printf(file, "ety vpn ppn size flags\n"); |
| 722 | |
| 723 | for (i = 0; i < NR_PMB_ENTRIES; i++) { |
| 724 | unsigned long addr, data; |
| 725 | unsigned int size; |
| 726 | char *sz_str = NULL; |
| 727 | |
| 728 | addr = __raw_readl(mk_pmb_addr(i)); |
| 729 | data = __raw_readl(mk_pmb_data(i)); |
| 730 | |
| 731 | size = data & PMB_SZ_MASK; |
| 732 | sz_str = (size == PMB_SZ_16M) ? " 16MB": |
| 733 | (size == PMB_SZ_64M) ? " 64MB": |
| 734 | (size == PMB_SZ_128M) ? "128MB": |
| 735 | "512MB"; |
| 736 | |
| 737 | /* 02: V 0x88 0x08 128MB C CB B */ |
| 738 | seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n", |
| 739 | i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ', |
| 740 | (addr >> 24) & 0xff, (data >> 24) & 0xff, |
| 741 | sz_str, (data & PMB_C) ? 'C' : ' ', |
| 742 | (data & PMB_WT) ? "WT" : "CB", |
| 743 | (data & PMB_UB) ? "UB" : " B"); |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int pmb_debugfs_open(struct inode *inode, struct file *file) |
| 750 | { |
| 751 | return single_open(file, pmb_seq_show, NULL); |
| 752 | } |
| 753 | |
| 754 | static const struct file_operations pmb_debugfs_fops = { |
| 755 | .owner = THIS_MODULE, |
| 756 | .open = pmb_debugfs_open, |
| 757 | .read = seq_read, |
| 758 | .llseek = seq_lseek, |
| 759 | .release = single_release, |
| 760 | }; |
| 761 | |
| 762 | static int __init pmb_debugfs_init(void) |
| 763 | { |
| 764 | struct dentry *dentry; |
| 765 | |
| 766 | dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO, |
| 767 | sh_debugfs_root, NULL, &pmb_debugfs_fops); |
| 768 | if (!dentry) |
| 769 | return -ENOMEM; |
| 770 | if (IS_ERR(dentry)) |
| 771 | return PTR_ERR(dentry); |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | postcore_initcall(pmb_debugfs_init); |
| 776 | |
| 777 | #ifdef CONFIG_PM |
| 778 | static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state) |
| 779 | { |
| 780 | static pm_message_t prev_state; |
| 781 | int i; |
| 782 | |
| 783 | /* Restore the PMB after a resume from hibernation */ |
| 784 | if (state.event == PM_EVENT_ON && |
| 785 | prev_state.event == PM_EVENT_FREEZE) { |
| 786 | struct pmb_entry *pmbe; |
| 787 | |
| 788 | read_lock(&pmb_rwlock); |
| 789 | |
| 790 | for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) { |
| 791 | if (test_bit(i, pmb_map)) { |
| 792 | pmbe = &pmb_entry_list[i]; |
| 793 | set_pmb_entry(pmbe); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | read_unlock(&pmb_rwlock); |
| 798 | } |
| 799 | |
| 800 | prev_state = state; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static int pmb_sysdev_resume(struct sys_device *dev) |
| 806 | { |
| 807 | return pmb_sysdev_suspend(dev, PMSG_ON); |
| 808 | } |
| 809 | |
| 810 | static struct sysdev_driver pmb_sysdev_driver = { |
| 811 | .suspend = pmb_sysdev_suspend, |
| 812 | .resume = pmb_sysdev_resume, |
| 813 | }; |
| 814 | |
| 815 | static int __init pmb_sysdev_init(void) |
| 816 | { |
| 817 | return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver); |
| 818 | } |
| 819 | subsys_initcall(pmb_sysdev_init); |
| 820 | #endif |