book3e_pgtable.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2005, Paul Mackerras, IBM Corporation.
  4. * Copyright 2009, Benjamin Herrenschmidt, IBM Corporation.
  5. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/memblock.h>
  9. #include <asm/pgalloc.h>
  10. #include <asm/tlb.h>
  11. #include <asm/dma.h>
  12. #include <asm/code-patching.h>
  13. #include <mm/mmu_decl.h>
  14. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  15. /*
  16. * On Book3E CPUs, the vmemmap is currently mapped in the top half of
  17. * the vmalloc space using normal page tables, though the size of
  18. * pages encoded in the PTEs can be different
  19. */
  20. int __meminit vmemmap_create_mapping(unsigned long start,
  21. unsigned long page_size,
  22. unsigned long phys)
  23. {
  24. /* Create a PTE encoding without page size */
  25. unsigned long i, flags = _PAGE_PRESENT | _PAGE_ACCESSED |
  26. _PAGE_KERNEL_RW;
  27. /* PTEs only contain page size encodings up to 32M */
  28. BUG_ON(mmu_psize_defs[mmu_vmemmap_psize].enc > 0xf);
  29. /* Encode the size in the PTE */
  30. flags |= mmu_psize_defs[mmu_vmemmap_psize].enc << 8;
  31. /* For each PTE for that area, map things. Note that we don't
  32. * increment phys because all PTEs are of the large size and
  33. * thus must have the low bits clear
  34. */
  35. for (i = 0; i < page_size; i += PAGE_SIZE)
  36. BUG_ON(map_kernel_page(start + i, phys, __pgprot(flags)));
  37. return 0;
  38. }
  39. #ifdef CONFIG_MEMORY_HOTPLUG
  40. void vmemmap_remove_mapping(unsigned long start,
  41. unsigned long page_size)
  42. {
  43. }
  44. #endif
  45. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  46. static void __init *early_alloc_pgtable(unsigned long size)
  47. {
  48. void *ptr;
  49. ptr = memblock_alloc_try_nid(size, size, MEMBLOCK_LOW_LIMIT,
  50. __pa(MAX_DMA_ADDRESS), NUMA_NO_NODE);
  51. if (!ptr)
  52. panic("%s: Failed to allocate %lu bytes align=0x%lx max_addr=%lx\n",
  53. __func__, size, size, __pa(MAX_DMA_ADDRESS));
  54. return ptr;
  55. }
  56. /*
  57. * map_kernel_page currently only called by __ioremap
  58. * map_kernel_page adds an entry to the ioremap page table
  59. * and adds an entry to the HPT, possibly bolting it
  60. */
  61. int __ref map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot)
  62. {
  63. pgd_t *pgdp;
  64. p4d_t *p4dp;
  65. pud_t *pudp;
  66. pmd_t *pmdp;
  67. pte_t *ptep;
  68. BUILD_BUG_ON(TASK_SIZE_USER64 > PGTABLE_RANGE);
  69. if (slab_is_available()) {
  70. pgdp = pgd_offset_k(ea);
  71. p4dp = p4d_offset(pgdp, ea);
  72. pudp = pud_alloc(&init_mm, p4dp, ea);
  73. if (!pudp)
  74. return -ENOMEM;
  75. pmdp = pmd_alloc(&init_mm, pudp, ea);
  76. if (!pmdp)
  77. return -ENOMEM;
  78. ptep = pte_alloc_kernel(pmdp, ea);
  79. if (!ptep)
  80. return -ENOMEM;
  81. } else {
  82. pgdp = pgd_offset_k(ea);
  83. p4dp = p4d_offset(pgdp, ea);
  84. if (p4d_none(*p4dp)) {
  85. pudp = early_alloc_pgtable(PUD_TABLE_SIZE);
  86. p4d_populate(&init_mm, p4dp, pudp);
  87. }
  88. pudp = pud_offset(p4dp, ea);
  89. if (pud_none(*pudp)) {
  90. pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
  91. pud_populate(&init_mm, pudp, pmdp);
  92. }
  93. pmdp = pmd_offset(pudp, ea);
  94. if (!pmd_present(*pmdp)) {
  95. ptep = early_alloc_pgtable(PTE_TABLE_SIZE);
  96. pmd_populate_kernel(&init_mm, pmdp, ptep);
  97. }
  98. ptep = pte_offset_kernel(pmdp, ea);
  99. }
  100. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, prot));
  101. smp_wmb();
  102. return 0;
  103. }
  104. void __patch_exception(int exc, unsigned long addr)
  105. {
  106. unsigned int *ibase = &interrupt_base_book3e;
  107. /*
  108. * Our exceptions vectors start with a NOP and -then- a branch
  109. * to deal with single stepping from userspace which stops on
  110. * the second instruction. Thus we need to patch the second
  111. * instruction of the exception, not the first one.
  112. */
  113. patch_branch(ibase + (exc / 4) + 1, addr, 0);
  114. }