pmb.c 19 KB

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