idmap.c 2.7 KB

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