Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 1 | /* |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 2 | * Copyright IBM Corp. 2006 |
| 3 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/bootmem.h> |
| 7 | #include <linux/pfn.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/list.h> |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 11 | #include <linux/hugetlb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 13 | #include <linux/memblock.h> |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 14 | #include <asm/cacheflush.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 15 | #include <asm/pgalloc.h> |
| 16 | #include <asm/pgtable.h> |
| 17 | #include <asm/setup.h> |
| 18 | #include <asm/tlbflush.h> |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 19 | #include <asm/sections.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 20 | |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 21 | static DEFINE_MUTEX(vmem_mutex); |
| 22 | |
| 23 | struct memory_segment { |
| 24 | struct list_head list; |
| 25 | unsigned long start; |
| 26 | unsigned long size; |
| 27 | }; |
| 28 | |
| 29 | static LIST_HEAD(mem_segs); |
| 30 | |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 31 | static void __ref *vmem_alloc_pages(unsigned int order) |
| 32 | { |
Heiko Carstens | 2e9996f | 2016-05-13 11:10:09 +0200 | [diff] [blame] | 33 | unsigned long size = PAGE_SIZE << order; |
| 34 | |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 35 | if (slab_is_available()) |
| 36 | return (void *)__get_free_pages(GFP_KERNEL, order); |
Heiko Carstens | 2e9996f | 2016-05-13 11:10:09 +0200 | [diff] [blame] | 37 | return alloc_bootmem_align(size, size); |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline pud_t *vmem_pud_alloc(void) |
Martin Schwidefsky | 5a216a2 | 2008-02-09 18:24:36 +0100 | [diff] [blame] | 41 | { |
| 42 | pud_t *pud = NULL; |
| 43 | |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 44 | pud = vmem_alloc_pages(2); |
Martin Schwidefsky | 5a216a2 | 2008-02-09 18:24:36 +0100 | [diff] [blame] | 45 | if (!pud) |
| 46 | return NULL; |
Heiko Carstens | 8fc6365 | 2008-04-30 13:38:44 +0200 | [diff] [blame] | 47 | clear_table((unsigned long *) pud, _REGION3_ENTRY_EMPTY, PAGE_SIZE * 4); |
Martin Schwidefsky | 5a216a2 | 2008-02-09 18:24:36 +0100 | [diff] [blame] | 48 | return pud; |
| 49 | } |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 50 | |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 51 | pmd_t *vmem_pmd_alloc(void) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 52 | { |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 53 | pmd_t *pmd = NULL; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 54 | |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 55 | pmd = vmem_alloc_pages(2); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 56 | if (!pmd) |
| 57 | return NULL; |
Heiko Carstens | 8fc6365 | 2008-04-30 13:38:44 +0200 | [diff] [blame] | 58 | clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 59 | return pmd; |
| 60 | } |
| 61 | |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 62 | pte_t __ref *vmem_pte_alloc(void) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 63 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 64 | pte_t *pte; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 65 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 66 | if (slab_is_available()) |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 67 | pte = (pte_t *) page_table_alloc(&init_mm); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 68 | else |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 69 | pte = alloc_bootmem_align(PTRS_PER_PTE * sizeof(pte_t), |
| 70 | PTRS_PER_PTE * sizeof(pte_t)); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 71 | if (!pte) |
| 72 | return NULL; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 73 | clear_table((unsigned long *) pte, _PAGE_INVALID, |
Christian Borntraeger | 6af7eea | 2010-04-09 13:43:01 +0200 | [diff] [blame] | 74 | PTRS_PER_PTE * sizeof(pte_t)); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 75 | return pte; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Add a physical memory range to the 1:1 mapping. |
| 80 | */ |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 81 | static int vmem_add_mem(unsigned long start, unsigned long size) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 82 | { |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 83 | unsigned long pages4k, pages1m, pages2g; |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 84 | unsigned long end = start + size; |
| 85 | unsigned long address = start; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 86 | pgd_t *pg_dir; |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 87 | pud_t *pu_dir; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 88 | pmd_t *pm_dir; |
| 89 | pte_t *pt_dir; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 90 | int ret = -ENOMEM; |
| 91 | |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 92 | pages4k = pages1m = pages2g = 0; |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 93 | while (address < end) { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 94 | pg_dir = pgd_offset_k(address); |
| 95 | if (pgd_none(*pg_dir)) { |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 96 | pu_dir = vmem_pud_alloc(); |
| 97 | if (!pu_dir) |
| 98 | goto out; |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 99 | pgd_populate(&init_mm, pg_dir, pu_dir); |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 100 | } |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 101 | pu_dir = pud_offset(pg_dir, address); |
Heiko Carstens | 18da236 | 2012-10-08 09:18:26 +0200 | [diff] [blame] | 102 | if (MACHINE_HAS_EDAT2 && pud_none(*pu_dir) && address && |
Christian Borntraeger | 10917b8 | 2016-03-15 14:57:36 -0700 | [diff] [blame] | 103 | !(address & ~PUD_MASK) && (address + PUD_SIZE <= end) && |
| 104 | !debug_pagealloc_enabled()) { |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 105 | pud_val(*pu_dir) = address | pgprot_val(REGION3_KERNEL); |
Heiko Carstens | 18da236 | 2012-10-08 09:18:26 +0200 | [diff] [blame] | 106 | address += PUD_SIZE; |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 107 | pages2g++; |
Heiko Carstens | 18da236 | 2012-10-08 09:18:26 +0200 | [diff] [blame] | 108 | continue; |
| 109 | } |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 110 | if (pud_none(*pu_dir)) { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 111 | pm_dir = vmem_pmd_alloc(); |
| 112 | if (!pm_dir) |
| 113 | goto out; |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 114 | pud_populate(&init_mm, pu_dir, pm_dir); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 115 | } |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 116 | pm_dir = pmd_offset(pu_dir, address); |
Heiko Carstens | fc7e48aa | 2012-10-08 07:54:32 +0200 | [diff] [blame] | 117 | if (MACHINE_HAS_EDAT1 && pmd_none(*pm_dir) && address && |
Christian Borntraeger | 10917b8 | 2016-03-15 14:57:36 -0700 | [diff] [blame] | 118 | !(address & ~PMD_MASK) && (address + PMD_SIZE <= end) && |
| 119 | !debug_pagealloc_enabled()) { |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 120 | pmd_val(*pm_dir) = address | pgprot_val(SEGMENT_KERNEL); |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 121 | address += PMD_SIZE; |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 122 | pages1m++; |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 123 | continue; |
| 124 | } |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 125 | if (pmd_none(*pm_dir)) { |
Heiko Carstens | c53db52 | 2016-05-09 15:52:28 +0200 | [diff] [blame] | 126 | pt_dir = vmem_pte_alloc(); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 127 | if (!pt_dir) |
| 128 | goto out; |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 129 | pmd_populate(&init_mm, pm_dir, pt_dir); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | pt_dir = pte_offset_kernel(pm_dir, address); |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 133 | pte_val(*pt_dir) = address | pgprot_val(PAGE_KERNEL); |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 134 | address += PAGE_SIZE; |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 135 | pages4k++; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 136 | } |
| 137 | ret = 0; |
| 138 | out: |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 139 | update_page_count(PG_DIRECT_MAP_4K, pages4k); |
| 140 | update_page_count(PG_DIRECT_MAP_1M, pages1m); |
| 141 | update_page_count(PG_DIRECT_MAP_2G, pages2g); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Remove a physical memory range from the 1:1 mapping. |
| 147 | * Currently only invalidates page table entries. |
| 148 | */ |
| 149 | static void vmem_remove_range(unsigned long start, unsigned long size) |
| 150 | { |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 151 | unsigned long pages4k, pages1m, pages2g; |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 152 | unsigned long end = start + size; |
| 153 | unsigned long address = start; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 154 | pgd_t *pg_dir; |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 155 | pud_t *pu_dir; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 156 | pmd_t *pm_dir; |
| 157 | pte_t *pt_dir; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 158 | |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 159 | pages4k = pages1m = pages2g = 0; |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 160 | while (address < end) { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 161 | pg_dir = pgd_offset_k(address); |
Heiko Carstens | fc7e48aa | 2012-10-08 07:54:32 +0200 | [diff] [blame] | 162 | if (pgd_none(*pg_dir)) { |
| 163 | address += PGDIR_SIZE; |
| 164 | continue; |
| 165 | } |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 166 | pu_dir = pud_offset(pg_dir, address); |
Heiko Carstens | fc7e48aa | 2012-10-08 07:54:32 +0200 | [diff] [blame] | 167 | if (pud_none(*pu_dir)) { |
| 168 | address += PUD_SIZE; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 169 | continue; |
Heiko Carstens | fc7e48aa | 2012-10-08 07:54:32 +0200 | [diff] [blame] | 170 | } |
Heiko Carstens | 18da236 | 2012-10-08 09:18:26 +0200 | [diff] [blame] | 171 | if (pud_large(*pu_dir)) { |
| 172 | pud_clear(pu_dir); |
| 173 | address += PUD_SIZE; |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 174 | pages2g++; |
Heiko Carstens | 18da236 | 2012-10-08 09:18:26 +0200 | [diff] [blame] | 175 | continue; |
| 176 | } |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 177 | pm_dir = pmd_offset(pu_dir, address); |
Heiko Carstens | fc7e48aa | 2012-10-08 07:54:32 +0200 | [diff] [blame] | 178 | if (pmd_none(*pm_dir)) { |
| 179 | address += PMD_SIZE; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 180 | continue; |
Heiko Carstens | fc7e48aa | 2012-10-08 07:54:32 +0200 | [diff] [blame] | 181 | } |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 182 | if (pmd_large(*pm_dir)) { |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 183 | pmd_clear(pm_dir); |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 184 | address += PMD_SIZE; |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 185 | pages1m++; |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 186 | continue; |
| 187 | } |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 188 | pt_dir = pte_offset_kernel(pm_dir, address); |
Heiko Carstens | 5aa2997 | 2016-05-17 12:17:51 +0200 | [diff] [blame] | 189 | pte_clear(&init_mm, address, pt_dir); |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 190 | address += PAGE_SIZE; |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 191 | pages4k++; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 192 | } |
Heiko Carstens | 378b1e7 | 2012-10-01 12:58:34 +0200 | [diff] [blame] | 193 | flush_tlb_kernel_range(start, end); |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 194 | update_page_count(PG_DIRECT_MAP_4K, -pages4k); |
| 195 | update_page_count(PG_DIRECT_MAP_1M, -pages1m); |
| 196 | update_page_count(PG_DIRECT_MAP_2G, -pages2g); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Add a backed mem_map array to the virtual mem_map array. |
| 201 | */ |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 202 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 203 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 204 | unsigned long address = start; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 205 | pgd_t *pg_dir; |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 206 | pud_t *pu_dir; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 207 | pmd_t *pm_dir; |
| 208 | pte_t *pt_dir; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 209 | int ret = -ENOMEM; |
| 210 | |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 211 | for (address = start; address < end;) { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 212 | pg_dir = pgd_offset_k(address); |
| 213 | if (pgd_none(*pg_dir)) { |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 214 | pu_dir = vmem_pud_alloc(); |
| 215 | if (!pu_dir) |
| 216 | goto out; |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 217 | pgd_populate(&init_mm, pg_dir, pu_dir); |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | pu_dir = pud_offset(pg_dir, address); |
| 221 | if (pud_none(*pu_dir)) { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 222 | pm_dir = vmem_pmd_alloc(); |
| 223 | if (!pm_dir) |
| 224 | goto out; |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 225 | pud_populate(&init_mm, pu_dir, pm_dir); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 228 | pm_dir = pmd_offset(pu_dir, address); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 229 | if (pmd_none(*pm_dir)) { |
Heiko Carstens | f781796 | 2012-10-17 12:18:05 +0200 | [diff] [blame] | 230 | /* Use 1MB frames for vmemmap if available. We always |
| 231 | * use large frames even if they are only partially |
| 232 | * used. |
| 233 | * Otherwise we would have also page tables since |
| 234 | * vmemmap_populate gets called for each section |
| 235 | * separately. */ |
| 236 | if (MACHINE_HAS_EDAT1) { |
| 237 | void *new_page; |
| 238 | |
| 239 | new_page = vmemmap_alloc_block(PMD_SIZE, node); |
| 240 | if (!new_page) |
| 241 | goto out; |
Martin Schwidefsky | abf09be | 2012-11-07 13:17:37 +0100 | [diff] [blame] | 242 | pmd_val(*pm_dir) = __pa(new_page) | |
Heiko Carstens | 6a5c148 | 2014-09-22 08:50:51 +0200 | [diff] [blame] | 243 | _SEGMENT_ENTRY | _SEGMENT_ENTRY_LARGE; |
Heiko Carstens | f781796 | 2012-10-17 12:18:05 +0200 | [diff] [blame] | 244 | address = (address + PMD_SIZE) & PMD_MASK; |
| 245 | continue; |
| 246 | } |
Heiko Carstens | c53db52 | 2016-05-09 15:52:28 +0200 | [diff] [blame] | 247 | pt_dir = vmem_pte_alloc(); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 248 | if (!pt_dir) |
| 249 | goto out; |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 250 | pmd_populate(&init_mm, pm_dir, pt_dir); |
Heiko Carstens | f781796 | 2012-10-17 12:18:05 +0200 | [diff] [blame] | 251 | } else if (pmd_large(*pm_dir)) { |
| 252 | address = (address + PMD_SIZE) & PMD_MASK; |
| 253 | continue; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | pt_dir = pte_offset_kernel(pm_dir, address); |
| 257 | if (pte_none(*pt_dir)) { |
Heiko Carstens | 70c9d29 | 2014-09-20 11:12:08 +0200 | [diff] [blame] | 258 | void *new_page; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 259 | |
Heiko Carstens | 70c9d29 | 2014-09-20 11:12:08 +0200 | [diff] [blame] | 260 | new_page = vmemmap_alloc_block(PAGE_SIZE, node); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 261 | if (!new_page) |
| 262 | goto out; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 263 | pte_val(*pt_dir) = |
| 264 | __pa(new_page) | pgprot_val(PAGE_KERNEL); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 265 | } |
Heiko Carstens | f781796 | 2012-10-17 12:18:05 +0200 | [diff] [blame] | 266 | address += PAGE_SIZE; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 267 | } |
| 268 | ret = 0; |
| 269 | out: |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 270 | return ret; |
| 271 | } |
| 272 | |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 273 | void vmemmap_free(unsigned long start, unsigned long end) |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 274 | { |
| 275 | } |
| 276 | |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Add memory segment to the segment list if it doesn't overlap with |
| 279 | * an already present segment. |
| 280 | */ |
| 281 | static int insert_memory_segment(struct memory_segment *seg) |
| 282 | { |
| 283 | struct memory_segment *tmp; |
| 284 | |
Heiko Carstens | ee0ddad | 2008-06-10 10:03:20 +0200 | [diff] [blame] | 285 | if (seg->start + seg->size > VMEM_MAX_PHYS || |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 286 | seg->start + seg->size < seg->start) |
| 287 | return -ERANGE; |
| 288 | |
| 289 | list_for_each_entry(tmp, &mem_segs, list) { |
| 290 | if (seg->start >= tmp->start + tmp->size) |
| 291 | continue; |
| 292 | if (seg->start + seg->size <= tmp->start) |
| 293 | continue; |
| 294 | return -ENOSPC; |
| 295 | } |
| 296 | list_add(&seg->list, &mem_segs); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Remove memory segment from the segment list. |
| 302 | */ |
| 303 | static void remove_memory_segment(struct memory_segment *seg) |
| 304 | { |
| 305 | list_del(&seg->list); |
| 306 | } |
| 307 | |
| 308 | static void __remove_shared_memory(struct memory_segment *seg) |
| 309 | { |
| 310 | remove_memory_segment(seg); |
| 311 | vmem_remove_range(seg->start, seg->size); |
| 312 | } |
| 313 | |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 314 | int vmem_remove_mapping(unsigned long start, unsigned long size) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 315 | { |
| 316 | struct memory_segment *seg; |
| 317 | int ret; |
| 318 | |
| 319 | mutex_lock(&vmem_mutex); |
| 320 | |
| 321 | ret = -ENOENT; |
| 322 | list_for_each_entry(seg, &mem_segs, list) { |
| 323 | if (seg->start == start && seg->size == size) |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | if (seg->start != start || seg->size != size) |
| 328 | goto out; |
| 329 | |
| 330 | ret = 0; |
| 331 | __remove_shared_memory(seg); |
| 332 | kfree(seg); |
| 333 | out: |
| 334 | mutex_unlock(&vmem_mutex); |
| 335 | return ret; |
| 336 | } |
| 337 | |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 338 | int vmem_add_mapping(unsigned long start, unsigned long size) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 339 | { |
| 340 | struct memory_segment *seg; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 341 | int ret; |
| 342 | |
| 343 | mutex_lock(&vmem_mutex); |
| 344 | ret = -ENOMEM; |
| 345 | seg = kzalloc(sizeof(*seg), GFP_KERNEL); |
| 346 | if (!seg) |
| 347 | goto out; |
| 348 | seg->start = start; |
| 349 | seg->size = size; |
| 350 | |
| 351 | ret = insert_memory_segment(seg); |
| 352 | if (ret) |
| 353 | goto out_free; |
| 354 | |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 355 | ret = vmem_add_mem(start, size); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 356 | if (ret) |
| 357 | goto out_remove; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 358 | goto out; |
| 359 | |
| 360 | out_remove: |
| 361 | __remove_shared_memory(seg); |
| 362 | out_free: |
| 363 | kfree(seg); |
| 364 | out: |
| 365 | mutex_unlock(&vmem_mutex); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * map whole physical memory to virtual memory (identity mapping) |
Christian Borntraeger | 5fd9c6e | 2008-01-26 14:11:00 +0100 | [diff] [blame] | 371 | * we reserve enough space in the vmalloc area for vmemmap to hotplug |
| 372 | * additional memory segments. |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 373 | */ |
| 374 | void __init vmem_map_init(void) |
| 375 | { |
Heiko Carstens | d07a980 | 2016-06-07 10:12:55 +0200 | [diff] [blame] | 376 | unsigned long size = _eshared - _stext; |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 377 | struct memblock_region *reg; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 378 | |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 379 | for_each_memblock(memory, reg) |
| 380 | vmem_add_mem(reg->base, reg->size); |
Heiko Carstens | d07a980 | 2016-06-07 10:12:55 +0200 | [diff] [blame] | 381 | set_memory_ro((unsigned long)_stext, size >> PAGE_SHIFT); |
| 382 | pr_info("Write protected kernel read-only data: %luk\n", size >> 10); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 386 | * Convert memblock.memory to a memory segment list so there is a single |
| 387 | * list that contains all memory segments. |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 388 | */ |
| 389 | static int __init vmem_convert_memory_chunk(void) |
| 390 | { |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 391 | struct memblock_region *reg; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 392 | struct memory_segment *seg; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 393 | |
| 394 | mutex_lock(&vmem_mutex); |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 395 | for_each_memblock(memory, reg) { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 396 | seg = kzalloc(sizeof(*seg), GFP_KERNEL); |
| 397 | if (!seg) |
| 398 | panic("Out of memory...\n"); |
Philipp Hachtmann | 50be634 | 2014-01-29 18:16:01 +0100 | [diff] [blame] | 399 | seg->start = reg->base; |
| 400 | seg->size = reg->size; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 401 | insert_memory_segment(seg); |
| 402 | } |
| 403 | mutex_unlock(&vmem_mutex); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | core_initcall(vmem_convert_memory_chunk); |