mmu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xtensa mmu stuff
  4. *
  5. * Extracted from init.c
  6. */
  7. #include <linux/memblock.h>
  8. #include <linux/percpu.h>
  9. #include <linux/init.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/cache.h>
  13. #include <asm/tlb.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/page.h>
  17. #include <asm/initialize_mmu.h>
  18. #include <asm/io.h>
  19. DEFINE_PER_CPU(unsigned long, asid_cache) = ASID_USER_FIRST;
  20. #if defined(CONFIG_HIGHMEM)
  21. static void * __init init_pmd(unsigned long vaddr, unsigned long n_pages)
  22. {
  23. pmd_t *pmd = pmd_off_k(vaddr);
  24. pte_t *pte;
  25. unsigned long i;
  26. n_pages = ALIGN(n_pages, PTRS_PER_PTE);
  27. pr_debug("%s: vaddr: 0x%08lx, n_pages: %ld\n",
  28. __func__, vaddr, n_pages);
  29. pte = memblock_alloc_low(n_pages * sizeof(pte_t), PAGE_SIZE);
  30. if (!pte)
  31. panic("%s: Failed to allocate %lu bytes align=%lx\n",
  32. __func__, n_pages * sizeof(pte_t), PAGE_SIZE);
  33. for (i = 0; i < n_pages; ++i)
  34. pte_clear(NULL, 0, pte + i);
  35. for (i = 0; i < n_pages; i += PTRS_PER_PTE, ++pmd) {
  36. pte_t *cur_pte = pte + i;
  37. BUG_ON(!pmd_none(*pmd));
  38. set_pmd(pmd, __pmd(((unsigned long)cur_pte) & PAGE_MASK));
  39. BUG_ON(cur_pte != pte_offset_kernel(pmd, 0));
  40. pr_debug("%s: pmd: 0x%p, pte: 0x%p\n",
  41. __func__, pmd, cur_pte);
  42. }
  43. return pte;
  44. }
  45. static void __init fixedrange_init(void)
  46. {
  47. BUILD_BUG_ON(FIXADDR_START < TLBTEMP_BASE_1 + TLBTEMP_SIZE);
  48. init_pmd(FIXADDR_START, __end_of_fixed_addresses);
  49. }
  50. #endif
  51. void __init paging_init(void)
  52. {
  53. #ifdef CONFIG_HIGHMEM
  54. fixedrange_init();
  55. pkmap_page_table = init_pmd(PKMAP_BASE, LAST_PKMAP);
  56. kmap_init();
  57. #endif
  58. }
  59. /*
  60. * Flush the mmu and reset associated register to default values.
  61. */
  62. void init_mmu(void)
  63. {
  64. #if !(XCHAL_HAVE_PTP_MMU && XCHAL_HAVE_SPANNING_WAY)
  65. /*
  66. * Writing zeros to the instruction and data TLBCFG special
  67. * registers ensure that valid values exist in the register.
  68. *
  69. * For existing PGSZID<w> fields, zero selects the first element
  70. * of the page-size array. For nonexistent PGSZID<w> fields,
  71. * zero is the best value to write. Also, when changing PGSZID<w>
  72. * fields, the corresponding TLB must be flushed.
  73. */
  74. set_itlbcfg_register(0);
  75. set_dtlbcfg_register(0);
  76. #endif
  77. init_kio();
  78. local_flush_tlb_all();
  79. /* Set rasid register to a known value. */
  80. set_rasid_register(ASID_INSERT(ASID_USER_FIRST));
  81. /* Set PTEVADDR special register to the start of the page
  82. * table, which is in kernel mappable space (ie. not
  83. * statically mapped). This register's value is undefined on
  84. * reset.
  85. */
  86. set_ptevaddr_register(XCHAL_PAGE_TABLE_VADDR);
  87. }
  88. void init_kio(void)
  89. {
  90. #if XCHAL_HAVE_PTP_MMU && XCHAL_HAVE_SPANNING_WAY && defined(CONFIG_USE_OF)
  91. /*
  92. * Update the IO area mapping in case xtensa_kio_paddr has changed
  93. */
  94. write_dtlb_entry(__pte(xtensa_kio_paddr + CA_WRITEBACK),
  95. XCHAL_KIO_CACHED_VADDR + 6);
  96. write_itlb_entry(__pte(xtensa_kio_paddr + CA_WRITEBACK),
  97. XCHAL_KIO_CACHED_VADDR + 6);
  98. write_dtlb_entry(__pte(xtensa_kio_paddr + CA_BYPASS),
  99. XCHAL_KIO_BYPASS_VADDR + 6);
  100. write_itlb_entry(__pte(xtensa_kio_paddr + CA_BYPASS),
  101. XCHAL_KIO_BYPASS_VADDR + 6);
  102. #endif
  103. }