pmb.c 19 KB

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