idmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <linux/kernel.h>
  2. #include <asm/cputype.h>
  3. #include <asm/idmap.h>
  4. #include <asm/pgalloc.h>
  5. #include <asm/pgtable.h>
  6. #include <asm/sections.h>
  7. #include <asm/system_info.h>
  8. pgd_t *idmap_pgd;
  9. #ifdef CONFIG_ARM_LPAE
  10. static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
  11. unsigned long prot)
  12. {
  13. pmd_t *pmd;
  14. unsigned long next;
  15. if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
  16. pmd = pmd_alloc_one(&init_mm, addr);
  17. if (!pmd) {
  18. pr_warning("Failed to allocate identity pmd.\n");
  19. return;
  20. }
  21. pud_populate(&init_mm, pud, pmd);
  22. pmd += pmd_index(addr);
  23. } else
  24. pmd = pmd_offset(pud, addr);
  25. do {
  26. next = pmd_addr_end(addr, end);
  27. *pmd = __pmd((addr & PMD_MASK) | prot);
  28. flush_pmd_entry(pmd);
  29. } while (pmd++, addr = next, addr != end);
  30. }
  31. #else /* !CONFIG_ARM_LPAE */
  32. static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
  33. unsigned long prot)
  34. {
  35. pmd_t *pmd = pmd_offset(pud, addr);
  36. addr = (addr & PMD_MASK) | prot;
  37. pmd[0] = __pmd(addr);
  38. addr += SECTION_SIZE;
  39. pmd[1] = __pmd(addr);
  40. flush_pmd_entry(pmd);
  41. }
  42. #endif /* CONFIG_ARM_LPAE */
  43. static void idmap_add_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
  44. unsigned long prot)
  45. {
  46. pud_t *pud = pud_offset(pgd, addr);
  47. unsigned long next;
  48. do {
  49. next = pud_addr_end(addr, end);
  50. idmap_add_pmd(pud, addr, next, prot);
  51. } while (pud++, addr = next, addr != end);
  52. }
  53. static void identity_mapping_add(pgd_t *pgd, unsigned long addr, unsigned long end)
  54. {
  55. unsigned long prot, next;
  56. prot = PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
  57. if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
  58. prot |= PMD_BIT4;
  59. pgd += pgd_index(addr);
  60. do {
  61. next = pgd_addr_end(addr, end);
  62. idmap_add_pud(pgd, addr, next, prot);
  63. } while (pgd++, addr = next, addr != end);
  64. }
  65. extern char __idmap_text_start[], __idmap_text_end[];
  66. static int __init init_static_idmap(void)
  67. {
  68. phys_addr_t idmap_start, idmap_end;
  69. idmap_pgd = pgd_alloc(&init_mm);
  70. if (!idmap_pgd)
  71. return -ENOMEM;
  72. /* Add an identity mapping for the physical address of the section. */
  73. idmap_start = virt_to_phys((void *)__idmap_text_start);
  74. idmap_end = virt_to_phys((void *)__idmap_text_end);
  75. pr_info("Setting up static identity map for 0x%llx - 0x%llx\n",
  76. (long long)idmap_start, (long long)idmap_end);
  77. identity_mapping_add(idmap_pgd, idmap_start, idmap_end);
  78. return 0;
  79. }
  80. early_initcall(init_static_idmap);
  81. /*
  82. * In order to soft-boot, we need to switch to a 1:1 mapping for the
  83. * cpu_reset functions. This will then ensure that we have predictable
  84. * results when turning off the mmu.
  85. */
  86. void setup_mm_for_reboot(void)
  87. {
  88. /* Clean and invalidate L1. */
  89. flush_cache_all();
  90. /* Switch to the identity mapping. */
  91. cpu_switch_mm(idmap_pgd, &init_mm);
  92. /* Flush the TLB. */
  93. local_flush_tlb_all();
  94. }