pmb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 <asm/sizes.h>
  26. #include <asm/system.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/page.h>
  30. #include <asm/mmu.h>
  31. #include <asm/mmu_context.h>
  32. static void pmb_unmap_entry(struct pmb_entry *);
  33. static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
  34. static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES);
  35. static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
  36. {
  37. return (entry & PMB_E_MASK) << PMB_E_SHIFT;
  38. }
  39. static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
  40. {
  41. return mk_pmb_entry(entry) | PMB_ADDR;
  42. }
  43. static __always_inline unsigned long mk_pmb_data(unsigned int entry)
  44. {
  45. return mk_pmb_entry(entry) | PMB_DATA;
  46. }
  47. static int pmb_alloc_entry(void)
  48. {
  49. unsigned int pos;
  50. repeat:
  51. pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
  52. if (unlikely(pos > NR_PMB_ENTRIES))
  53. return -ENOSPC;
  54. if (test_and_set_bit(pos, pmb_map))
  55. goto repeat;
  56. return pos;
  57. }
  58. static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
  59. unsigned long flags, int entry)
  60. {
  61. struct pmb_entry *pmbe;
  62. int pos;
  63. if (entry == PMB_NO_ENTRY) {
  64. pos = pmb_alloc_entry();
  65. if (pos < 0)
  66. return ERR_PTR(pos);
  67. } else {
  68. if (test_and_set_bit(entry, pmb_map))
  69. return ERR_PTR(-ENOSPC);
  70. pos = entry;
  71. }
  72. pmbe = &pmb_entry_list[pos];
  73. if (!pmbe)
  74. return ERR_PTR(-ENOMEM);
  75. pmbe->vpn = vpn;
  76. pmbe->ppn = ppn;
  77. pmbe->flags = flags;
  78. pmbe->entry = pos;
  79. return pmbe;
  80. }
  81. static void pmb_free(struct pmb_entry *pmbe)
  82. {
  83. int pos = pmbe->entry;
  84. pmbe->vpn = 0;
  85. pmbe->ppn = 0;
  86. pmbe->flags = 0;
  87. pmbe->entry = 0;
  88. clear_bit(pos, pmb_map);
  89. }
  90. /*
  91. * Must be run uncached.
  92. */
  93. static void set_pmb_entry(struct pmb_entry *pmbe)
  94. {
  95. jump_to_uncached();
  96. __raw_writel(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry));
  97. #ifdef CONFIG_CACHE_WRITETHROUGH
  98. /*
  99. * When we are in 32-bit address extended mode, CCR.CB becomes
  100. * invalid, so care must be taken to manually adjust cacheable
  101. * translations.
  102. */
  103. if (likely(pmbe->flags & PMB_C))
  104. pmbe->flags |= PMB_WT;
  105. #endif
  106. __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, mk_pmb_data(pmbe->entry));
  107. back_to_cached();
  108. }
  109. static void clear_pmb_entry(struct pmb_entry *pmbe)
  110. {
  111. unsigned int entry = pmbe->entry;
  112. unsigned long addr;
  113. jump_to_uncached();
  114. /* Clear V-bit */
  115. addr = mk_pmb_addr(entry);
  116. __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
  117. addr = mk_pmb_data(entry);
  118. __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
  119. back_to_cached();
  120. }
  121. static struct {
  122. unsigned long size;
  123. int flag;
  124. } pmb_sizes[] = {
  125. { .size = SZ_512M, .flag = PMB_SZ_512M, },
  126. { .size = SZ_128M, .flag = PMB_SZ_128M, },
  127. { .size = SZ_64M, .flag = PMB_SZ_64M, },
  128. { .size = SZ_16M, .flag = PMB_SZ_16M, },
  129. };
  130. long pmb_remap(unsigned long vaddr, unsigned long phys,
  131. unsigned long size, pgprot_t prot)
  132. {
  133. struct pmb_entry *pmbp, *pmbe;
  134. unsigned long wanted;
  135. int pmb_flags, i;
  136. long err;
  137. u64 flags;
  138. flags = pgprot_val(prot);
  139. /* Convert typical pgprot value to the PMB equivalent */
  140. if (flags & _PAGE_CACHABLE) {
  141. if (flags & _PAGE_WT)
  142. pmb_flags = PMB_WT;
  143. else
  144. pmb_flags = PMB_C;
  145. } else
  146. pmb_flags = PMB_WT | PMB_UB;
  147. pmbp = NULL;
  148. wanted = size;
  149. again:
  150. for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
  151. if (size < pmb_sizes[i].size)
  152. continue;
  153. pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
  154. PMB_NO_ENTRY);
  155. if (IS_ERR(pmbe)) {
  156. err = PTR_ERR(pmbe);
  157. goto out;
  158. }
  159. set_pmb_entry(pmbe);
  160. phys += pmb_sizes[i].size;
  161. vaddr += pmb_sizes[i].size;
  162. size -= pmb_sizes[i].size;
  163. /*
  164. * Link adjacent entries that span multiple PMB entries
  165. * for easier tear-down.
  166. */
  167. if (likely(pmbp))
  168. pmbp->link = pmbe;
  169. pmbp = pmbe;
  170. /*
  171. * Instead of trying smaller sizes on every iteration
  172. * (even if we succeed in allocating space), try using
  173. * pmb_sizes[i].size again.
  174. */
  175. i--;
  176. }
  177. if (size >= 0x1000000)
  178. goto again;
  179. return wanted - size;
  180. out:
  181. pmb_unmap_entry(pmbp);
  182. return err;
  183. }
  184. void pmb_unmap(unsigned long addr)
  185. {
  186. struct pmb_entry *pmbe;
  187. int i;
  188. for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
  189. if (test_bit(i, pmb_map)) {
  190. pmbe = &pmb_entry_list[i];
  191. if (pmbe->vpn == addr) {
  192. pmb_unmap_entry(pmbe);
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. static void pmb_unmap_entry(struct pmb_entry *pmbe)
  199. {
  200. if (unlikely(!pmbe))
  201. return;
  202. if (!test_bit(pmbe->entry, pmb_map)) {
  203. WARN_ON(1);
  204. return;
  205. }
  206. do {
  207. struct pmb_entry *pmblink = pmbe;
  208. /*
  209. * We may be called before this pmb_entry has been
  210. * entered into the PMB table via set_pmb_entry(), but
  211. * that's OK because we've allocated a unique slot for
  212. * this entry in pmb_alloc() (even if we haven't filled
  213. * it yet).
  214. *
  215. * Therefore, calling clear_pmb_entry() is safe as no
  216. * other mapping can be using that slot.
  217. */
  218. clear_pmb_entry(pmbe);
  219. pmbe = pmblink->link;
  220. pmb_free(pmblink);
  221. } while (pmbe);
  222. }
  223. static inline void
  224. pmb_log_mapping(unsigned long data_val, unsigned long vpn, unsigned long ppn)
  225. {
  226. unsigned int size;
  227. const char *sz_str;
  228. size = data_val & PMB_SZ_MASK;
  229. sz_str = (size == PMB_SZ_16M) ? " 16MB":
  230. (size == PMB_SZ_64M) ? " 64MB":
  231. (size == PMB_SZ_128M) ? "128MB":
  232. "512MB";
  233. pr_info("\t0x%08lx -> 0x%08lx [ %s %scached ]\n",
  234. vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, sz_str,
  235. (data_val & PMB_C) ? "" : "un");
  236. }
  237. static inline unsigned int pmb_ppn_in_range(unsigned long ppn)
  238. {
  239. return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
  240. }
  241. static int pmb_synchronize_mappings(void)
  242. {
  243. unsigned int applied = 0;
  244. int i;
  245. pr_info("PMB: boot mappings:\n");
  246. /*
  247. * Run through the initial boot mappings, log the established
  248. * ones, and blow away anything that falls outside of the valid
  249. * PPN range. Specifically, we only care about existing mappings
  250. * that impact the cached/uncached sections.
  251. *
  252. * Note that touching these can be a bit of a minefield; the boot
  253. * loader can establish multi-page mappings with the same caching
  254. * attributes, so we need to ensure that we aren't modifying a
  255. * mapping that we're presently executing from, or may execute
  256. * from in the case of straddling page boundaries.
  257. *
  258. * In the future we will have to tidy up after the boot loader by
  259. * jumping between the cached and uncached mappings and tearing
  260. * down alternating mappings while executing from the other.
  261. */
  262. for (i = 0; i < NR_PMB_ENTRIES; i++) {
  263. unsigned long addr, data;
  264. unsigned long addr_val, data_val;
  265. unsigned long ppn, vpn, flags;
  266. struct pmb_entry *pmbe;
  267. addr = mk_pmb_addr(i);
  268. data = mk_pmb_data(i);
  269. addr_val = __raw_readl(addr);
  270. data_val = __raw_readl(data);
  271. /*
  272. * Skip over any bogus entries
  273. */
  274. if (!(data_val & PMB_V) || !(addr_val & PMB_V))
  275. continue;
  276. ppn = data_val & PMB_PFN_MASK;
  277. vpn = addr_val & PMB_PFN_MASK;
  278. /*
  279. * Only preserve in-range mappings.
  280. */
  281. if (!pmb_ppn_in_range(ppn)) {
  282. /*
  283. * Invalidate anything out of bounds.
  284. */
  285. __raw_writel(addr_val & ~PMB_V, addr);
  286. __raw_writel(data_val & ~PMB_V, data);
  287. continue;
  288. }
  289. /*
  290. * Update the caching attributes if necessary
  291. */
  292. if (data_val & PMB_C) {
  293. #if defined(CONFIG_CACHE_WRITETHROUGH)
  294. data_val |= PMB_WT;
  295. #elif defined(CONFIG_CACHE_WRITEBACK)
  296. data_val &= ~PMB_WT;
  297. #else
  298. data_val &= ~(PMB_C | PMB_WT);
  299. #endif
  300. __raw_writel(data_val, data);
  301. }
  302. flags = data_val & (PMB_SZ_MASK | PMB_CACHE_MASK);
  303. pmbe = pmb_alloc(vpn, ppn, flags, i);
  304. if (IS_ERR(pmbe)) {
  305. WARN_ON_ONCE(1);
  306. continue;
  307. }
  308. pmb_log_mapping(data_val, vpn, ppn);
  309. applied++;
  310. }
  311. return (applied == 0);
  312. }
  313. int pmb_init(void)
  314. {
  315. int ret;
  316. jump_to_uncached();
  317. /*
  318. * Sync our software copy of the PMB mappings with those in
  319. * hardware. The mappings in the hardware PMB were either set up
  320. * by the bootloader or very early on by the kernel.
  321. */
  322. ret = pmb_synchronize_mappings();
  323. if (unlikely(ret == 0)) {
  324. back_to_cached();
  325. return 0;
  326. }
  327. __raw_writel(0, PMB_IRMCR);
  328. /* Flush out the TLB */
  329. __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
  330. back_to_cached();
  331. return 0;
  332. }
  333. bool __in_29bit_mode(void)
  334. {
  335. return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
  336. }
  337. static int pmb_seq_show(struct seq_file *file, void *iter)
  338. {
  339. int i;
  340. seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
  341. "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
  342. seq_printf(file, "ety vpn ppn size flags\n");
  343. for (i = 0; i < NR_PMB_ENTRIES; i++) {
  344. unsigned long addr, data;
  345. unsigned int size;
  346. char *sz_str = NULL;
  347. addr = __raw_readl(mk_pmb_addr(i));
  348. data = __raw_readl(mk_pmb_data(i));
  349. size = data & PMB_SZ_MASK;
  350. sz_str = (size == PMB_SZ_16M) ? " 16MB":
  351. (size == PMB_SZ_64M) ? " 64MB":
  352. (size == PMB_SZ_128M) ? "128MB":
  353. "512MB";
  354. /* 02: V 0x88 0x08 128MB C CB B */
  355. seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
  356. i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
  357. (addr >> 24) & 0xff, (data >> 24) & 0xff,
  358. sz_str, (data & PMB_C) ? 'C' : ' ',
  359. (data & PMB_WT) ? "WT" : "CB",
  360. (data & PMB_UB) ? "UB" : " B");
  361. }
  362. return 0;
  363. }
  364. static int pmb_debugfs_open(struct inode *inode, struct file *file)
  365. {
  366. return single_open(file, pmb_seq_show, NULL);
  367. }
  368. static const struct file_operations pmb_debugfs_fops = {
  369. .owner = THIS_MODULE,
  370. .open = pmb_debugfs_open,
  371. .read = seq_read,
  372. .llseek = seq_lseek,
  373. .release = single_release,
  374. };
  375. static int __init pmb_debugfs_init(void)
  376. {
  377. struct dentry *dentry;
  378. dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
  379. sh_debugfs_root, NULL, &pmb_debugfs_fops);
  380. if (!dentry)
  381. return -ENOMEM;
  382. if (IS_ERR(dentry))
  383. return PTR_ERR(dentry);
  384. return 0;
  385. }
  386. postcore_initcall(pmb_debugfs_init);
  387. #ifdef CONFIG_PM
  388. static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
  389. {
  390. static pm_message_t prev_state;
  391. int i;
  392. /* Restore the PMB after a resume from hibernation */
  393. if (state.event == PM_EVENT_ON &&
  394. prev_state.event == PM_EVENT_FREEZE) {
  395. struct pmb_entry *pmbe;
  396. for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
  397. if (test_bit(i, pmb_map)) {
  398. pmbe = &pmb_entry_list[i];
  399. set_pmb_entry(pmbe);
  400. }
  401. }
  402. }
  403. prev_state = state;
  404. return 0;
  405. }
  406. static int pmb_sysdev_resume(struct sys_device *dev)
  407. {
  408. return pmb_sysdev_suspend(dev, PMSG_ON);
  409. }
  410. static struct sysdev_driver pmb_sysdev_driver = {
  411. .suspend = pmb_sysdev_suspend,
  412. .resume = pmb_sysdev_resume,
  413. };
  414. static int __init pmb_sysdev_init(void)
  415. {
  416. return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
  417. }
  418. subsys_initcall(pmb_sysdev_init);
  419. #endif