pmb.c 19 KB

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