pgtable-book3e.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2005, Paul Mackerras, IBM Corporation.
  3. * Copyright 2009, Benjamin Herrenschmidt, IBM Corporation.
  4. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/memblock.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/tlb.h>
  15. #include <asm/dma.h>
  16. #include "mmu_decl.h"
  17. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  18. /*
  19. * On Book3E CPUs, the vmemmap is currently mapped in the top half of
  20. * the vmalloc space using normal page tables, though the size of
  21. * pages encoded in the PTEs can be different
  22. */
  23. int __meminit vmemmap_create_mapping(unsigned long start,
  24. unsigned long page_size,
  25. unsigned long phys)
  26. {
  27. /* Create a PTE encoding without page size */
  28. unsigned long i, flags = _PAGE_PRESENT | _PAGE_ACCESSED |
  29. _PAGE_KERNEL_RW;
  30. /* PTEs only contain page size encodings up to 32M */
  31. BUG_ON(mmu_psize_defs[mmu_vmemmap_psize].enc > 0xf);
  32. /* Encode the size in the PTE */
  33. flags |= mmu_psize_defs[mmu_vmemmap_psize].enc << 8;
  34. /* For each PTE for that area, map things. Note that we don't
  35. * increment phys because all PTEs are of the large size and
  36. * thus must have the low bits clear
  37. */
  38. for (i = 0; i < page_size; i += PAGE_SIZE)
  39. BUG_ON(map_kernel_page(start + i, phys, flags));
  40. return 0;
  41. }
  42. #ifdef CONFIG_MEMORY_HOTPLUG
  43. void vmemmap_remove_mapping(unsigned long start,
  44. unsigned long page_size)
  45. {
  46. }
  47. #endif
  48. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  49. static __ref void *early_alloc_pgtable(unsigned long size)
  50. {
  51. void *pt;
  52. pt = __va(memblock_alloc_base(size, size, __pa(MAX_DMA_ADDRESS)));
  53. memset(pt, 0, size);
  54. return pt;
  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 map_kernel_page(unsigned long ea, unsigned long pa, unsigned long flags)
  62. {
  63. pgd_t *pgdp;
  64. pud_t *pudp;
  65. pmd_t *pmdp;
  66. pte_t *ptep;
  67. BUILD_BUG_ON(TASK_SIZE_USER64 > PGTABLE_RANGE);
  68. if (slab_is_available()) {
  69. pgdp = pgd_offset_k(ea);
  70. pudp = pud_alloc(&init_mm, pgdp, ea);
  71. if (!pudp)
  72. return -ENOMEM;
  73. pmdp = pmd_alloc(&init_mm, pudp, ea);
  74. if (!pmdp)
  75. return -ENOMEM;
  76. ptep = pte_alloc_kernel(pmdp, ea);
  77. if (!ptep)
  78. return -ENOMEM;
  79. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  80. __pgprot(flags)));
  81. } else {
  82. pgdp = pgd_offset_k(ea);
  83. #ifndef __PAGETABLE_PUD_FOLDED
  84. if (pgd_none(*pgdp)) {
  85. pudp = early_alloc_pgtable(PUD_TABLE_SIZE);
  86. BUG_ON(pudp == NULL);
  87. pgd_populate(&init_mm, pgdp, pudp);
  88. }
  89. #endif /* !__PAGETABLE_PUD_FOLDED */
  90. pudp = pud_offset(pgdp, ea);
  91. if (pud_none(*pudp)) {
  92. pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
  93. BUG_ON(pmdp == NULL);
  94. pud_populate(&init_mm, pudp, pmdp);
  95. }
  96. pmdp = pmd_offset(pudp, ea);
  97. if (!pmd_present(*pmdp)) {
  98. ptep = early_alloc_pgtable(PAGE_SIZE);
  99. BUG_ON(ptep == NULL);
  100. pmd_populate_kernel(&init_mm, pmdp, ptep);
  101. }
  102. ptep = pte_offset_kernel(pmdp, ea);
  103. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  104. __pgprot(flags)));
  105. }
  106. smp_wmb();
  107. return 0;
  108. }