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