blob: 086522b7c60f488ac599a6d8a50ed4abd568855f [file] [log] [blame]
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +10001/*
2 * Page table handling routines for radix page table.
3 *
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 <linux/of_fdt.h>
14
15#include <asm/pgtable.h>
16#include <asm/pgalloc.h>
17#include <asm/dma.h>
18#include <asm/machdep.h>
19#include <asm/mmu.h>
20#include <asm/firmware.h>
Alistair Popple1d0761d2016-12-14 13:36:51 +110021#include <asm/powernv.h>
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +100022
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +100023#include <trace/events/thp.h>
24
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +053025static int native_register_process_table(unsigned long base, unsigned long pg_sz,
26 unsigned long table_size)
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +100027{
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +053028 unsigned long patb1 = base | table_size | PATB_GR;
29
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +100030 partition_tb->patb1 = cpu_to_be64(patb1);
31 return 0;
32}
33
34static __ref void *early_alloc_pgtable(unsigned long size)
35{
36 void *pt;
37
38 pt = __va(memblock_alloc_base(size, size, MEMBLOCK_ALLOC_ANYWHERE));
39 memset(pt, 0, size);
40
41 return pt;
42}
43
44int radix__map_kernel_page(unsigned long ea, unsigned long pa,
45 pgprot_t flags,
46 unsigned int map_page_size)
47{
48 pgd_t *pgdp;
49 pud_t *pudp;
50 pmd_t *pmdp;
51 pte_t *ptep;
52 /*
53 * Make sure task size is correct as per the max adddr
54 */
55 BUILD_BUG_ON(TASK_SIZE_USER64 > RADIX_PGTABLE_RANGE);
56 if (slab_is_available()) {
57 pgdp = pgd_offset_k(ea);
58 pudp = pud_alloc(&init_mm, pgdp, ea);
59 if (!pudp)
60 return -ENOMEM;
61 if (map_page_size == PUD_SIZE) {
62 ptep = (pte_t *)pudp;
63 goto set_the_pte;
64 }
65 pmdp = pmd_alloc(&init_mm, pudp, ea);
66 if (!pmdp)
67 return -ENOMEM;
68 if (map_page_size == PMD_SIZE) {
69 ptep = (pte_t *)pudp;
70 goto set_the_pte;
71 }
72 ptep = pte_alloc_kernel(pmdp, ea);
73 if (!ptep)
74 return -ENOMEM;
75 } else {
76 pgdp = pgd_offset_k(ea);
77 if (pgd_none(*pgdp)) {
78 pudp = early_alloc_pgtable(PUD_TABLE_SIZE);
79 BUG_ON(pudp == NULL);
80 pgd_populate(&init_mm, pgdp, pudp);
81 }
82 pudp = pud_offset(pgdp, ea);
83 if (map_page_size == PUD_SIZE) {
84 ptep = (pte_t *)pudp;
85 goto set_the_pte;
86 }
87 if (pud_none(*pudp)) {
88 pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
89 BUG_ON(pmdp == NULL);
90 pud_populate(&init_mm, pudp, pmdp);
91 }
92 pmdp = pmd_offset(pudp, ea);
93 if (map_page_size == PMD_SIZE) {
94 ptep = (pte_t *)pudp;
95 goto set_the_pte;
96 }
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 }
104
105set_the_pte:
106 set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, flags));
107 smp_wmb();
108 return 0;
109}
110
111static void __init radix_init_pgtable(void)
112{
113 int loop_count;
114 u64 base, end, start_addr;
115 unsigned long rts_field;
116 struct memblock_region *reg;
117 unsigned long linear_page_size;
118
119 /* We don't support slb for radix */
120 mmu_slb_size = 0;
121 /*
122 * Create the linear mapping, using standard page size for now
123 */
124 loop_count = 0;
125 for_each_memblock(memory, reg) {
126
127 start_addr = reg->base;
128
129redo:
130 if (loop_count < 1 && mmu_psize_defs[MMU_PAGE_1G].shift)
131 linear_page_size = PUD_SIZE;
132 else if (loop_count < 2 && mmu_psize_defs[MMU_PAGE_2M].shift)
133 linear_page_size = PMD_SIZE;
134 else
135 linear_page_size = PAGE_SIZE;
136
137 base = _ALIGN_UP(start_addr, linear_page_size);
138 end = _ALIGN_DOWN(reg->base + reg->size, linear_page_size);
139
140 pr_info("Mapping range 0x%lx - 0x%lx with 0x%lx\n",
141 (unsigned long)base, (unsigned long)end,
142 linear_page_size);
143
144 while (base < end) {
145 radix__map_kernel_page((unsigned long)__va(base),
146 base, PAGE_KERNEL_X,
147 linear_page_size);
148 base += linear_page_size;
149 }
150 /*
151 * map the rest using lower page size
152 */
153 if (end < reg->base + reg->size) {
154 start_addr = end;
155 loop_count++;
156 goto redo;
157 }
158 }
159 /*
160 * Allocate Partition table and process table for the
161 * host.
162 */
Suraj Jitindar Singh555c1632016-11-09 16:36:33 +1100163 BUILD_BUG_ON_MSG((PRTB_SIZE_SHIFT > 36), "Process table size too large.");
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000164 process_tb = early_alloc_pgtable(1UL << PRTB_SIZE_SHIFT);
165 /*
166 * Fill in the process table.
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000167 */
Aneesh Kumar K.Vb23d9c52016-06-17 11:40:36 +0530168 rts_field = radix__get_tree_size();
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000169 process_tb->prtb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE);
170 /*
171 * Fill in the partition table. We are suppose to use effective address
172 * of process table here. But our linear mapping also enable us to use
173 * physical address here.
174 */
Michael Ellermaneea81482016-08-04 15:32:06 +1000175 register_process_table(__pa(process_tb), 0, PRTB_SIZE_SHIFT - 12);
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000176 pr_info("Process table %p and radix root for kernel: %p\n", process_tb, init_mm.pgd);
177}
178
179static void __init radix_init_partition_table(void)
180{
Paul Mackerras9d661952016-11-21 16:00:58 +1100181 unsigned long rts_field, dw0;
Aneesh Kumar K.Vb23d9c52016-06-17 11:40:36 +0530182
Paul Mackerras9d661952016-11-21 16:00:58 +1100183 mmu_partition_table_init();
Aneesh Kumar K.Vb23d9c52016-06-17 11:40:36 +0530184 rts_field = radix__get_tree_size();
Paul Mackerras9d661952016-11-21 16:00:58 +1100185 dw0 = rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE | PATB_HR;
186 mmu_partition_table_set_entry(0, dw0, 0);
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000187
Aneesh Kumar K.V56547412016-07-13 15:05:25 +0530188 pr_info("Initializing Radix MMU\n");
189 pr_info("Partition table %p\n", partition_tb);
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000190}
191
192void __init radix_init_native(void)
193{
Michael Ellermaneea81482016-08-04 15:32:06 +1000194 register_process_table = native_register_process_table;
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000195}
196
197static int __init get_idx_from_shift(unsigned int shift)
198{
199 int idx = -1;
200
201 switch (shift) {
202 case 0xc:
203 idx = MMU_PAGE_4K;
204 break;
205 case 0x10:
206 idx = MMU_PAGE_64K;
207 break;
208 case 0x15:
209 idx = MMU_PAGE_2M;
210 break;
211 case 0x1e:
212 idx = MMU_PAGE_1G;
213 break;
214 }
215 return idx;
216}
217
218static int __init radix_dt_scan_page_sizes(unsigned long node,
219 const char *uname, int depth,
220 void *data)
221{
222 int size = 0;
223 int shift, idx;
224 unsigned int ap;
225 const __be32 *prop;
226 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
227
228 /* We are scanning "cpu" nodes only */
229 if (type == NULL || strcmp(type, "cpu") != 0)
230 return 0;
231
232 prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
233 if (!prop)
234 return 0;
235
236 pr_info("Page sizes from device-tree:\n");
237 for (; size >= 4; size -= 4, ++prop) {
238
239 struct mmu_psize_def *def;
240
241 /* top 3 bit is AP encoding */
242 shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
243 ap = be32_to_cpu(prop[0]) >> 29;
Balbir Singhac8d3812016-11-05 15:24:22 +1100244 pr_info("Page size shift = %d AP=0x%x\n", shift, ap);
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000245
246 idx = get_idx_from_shift(shift);
247 if (idx < 0)
248 continue;
249
250 def = &mmu_psize_defs[idx];
251 def->shift = shift;
252 def->ap = ap;
253 }
254
255 /* needed ? */
256 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
257 return 1;
258}
259
Michael Ellerman2537b092016-07-26 21:55:27 +1000260void __init radix__early_init_devtree(void)
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000261{
262 int rc;
263
264 /*
265 * Try to find the available page sizes in the device-tree
266 */
267 rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
268 if (rc != 0) /* Found */
269 goto found;
270 /*
271 * let's assume we have page 4k and 64k support
272 */
273 mmu_psize_defs[MMU_PAGE_4K].shift = 12;
274 mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
275
276 mmu_psize_defs[MMU_PAGE_64K].shift = 16;
277 mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
278found:
279#ifdef CONFIG_SPARSEMEM_VMEMMAP
280 if (mmu_psize_defs[MMU_PAGE_2M].shift) {
281 /*
282 * map vmemmap using 2M if available
283 */
284 mmu_vmemmap_psize = MMU_PAGE_2M;
285 }
286#endif /* CONFIG_SPARSEMEM_VMEMMAP */
287 return;
288}
289
Aneesh Kumar K.Vad410672016-08-24 15:03:39 +0530290static void update_hid_for_radix(void)
291{
292 unsigned long hid0;
293 unsigned long rb = 3UL << PPC_BITLSHIFT(53); /* IS = 3 */
294
295 asm volatile("ptesync": : :"memory");
296 /* prs = 0, ric = 2, rs = 0, r = 1 is = 3 */
297 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
298 : : "r"(rb), "i"(1), "i"(0), "i"(2), "r"(0) : "memory");
299 /* prs = 1, ric = 2, rs = 0, r = 1 is = 3 */
300 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
301 : : "r"(rb), "i"(1), "i"(1), "i"(2), "r"(0) : "memory");
302 asm volatile("eieio; tlbsync; ptesync; isync; slbia": : :"memory");
303 /*
304 * now switch the HID
305 */
306 hid0 = mfspr(SPRN_HID0);
307 hid0 |= HID0_POWER9_RADIX;
308 mtspr(SPRN_HID0, hid0);
309 asm volatile("isync": : :"memory");
310
311 /* Wait for it to happen */
312 while (!(mfspr(SPRN_HID0) & HID0_POWER9_RADIX))
313 cpu_relax();
314}
315
Balbir Singhee97b6b2016-11-15 17:56:14 +1100316static void radix_init_amor(void)
317{
318 /*
319 * In HV mode, we init AMOR (Authority Mask Override Register) so that
320 * the hypervisor and guest can setup IAMR (Instruction Authority Mask
321 * Register), enable key 0 and set it to 1.
322 *
323 * AMOR = 0b1100 .... 0000 (Mask for key 0 is 11)
324 */
325 mtspr(SPRN_AMOR, (3ul << 62));
326}
327
Balbir Singh3b10d002016-11-15 17:56:16 +1100328static void radix_init_iamr(void)
329{
330 unsigned long iamr;
331
332 /*
333 * The IAMR should set to 0 on DD1.
334 */
335 if (cpu_has_feature(CPU_FTR_POWER9_DD1))
336 iamr = 0;
337 else
338 iamr = (1ul << 62);
339
340 /*
341 * Radix always uses key0 of the IAMR to determine if an access is
342 * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
343 * fetch.
344 */
345 mtspr(SPRN_IAMR, iamr);
346}
347
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000348void __init radix__early_init_mmu(void)
349{
350 unsigned long lpcr;
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000351
352#ifdef CONFIG_PPC_64K_PAGES
353 /* PAGE_SIZE mappings */
354 mmu_virtual_psize = MMU_PAGE_64K;
355#else
356 mmu_virtual_psize = MMU_PAGE_4K;
357#endif
358
359#ifdef CONFIG_SPARSEMEM_VMEMMAP
360 /* vmemmap mapping */
361 mmu_vmemmap_psize = mmu_virtual_psize;
362#endif
363 /*
364 * initialize page table size
365 */
366 __pte_index_size = RADIX_PTE_INDEX_SIZE;
367 __pmd_index_size = RADIX_PMD_INDEX_SIZE;
368 __pud_index_size = RADIX_PUD_INDEX_SIZE;
369 __pgd_index_size = RADIX_PGD_INDEX_SIZE;
370 __pmd_cache_index = RADIX_PMD_INDEX_SIZE;
371 __pte_table_size = RADIX_PTE_TABLE_SIZE;
372 __pmd_table_size = RADIX_PMD_TABLE_SIZE;
373 __pud_table_size = RADIX_PUD_TABLE_SIZE;
374 __pgd_table_size = RADIX_PGD_TABLE_SIZE;
375
Aneesh Kumar K.Va2f41eb2016-04-29 23:26:19 +1000376 __pmd_val_bits = RADIX_PMD_VAL_BITS;
377 __pud_val_bits = RADIX_PUD_VAL_BITS;
378 __pgd_val_bits = RADIX_PGD_VAL_BITS;
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000379
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +1000380 __kernel_virt_start = RADIX_KERN_VIRT_START;
381 __kernel_virt_size = RADIX_KERN_VIRT_SIZE;
382 __vmalloc_start = RADIX_VMALLOC_START;
383 __vmalloc_end = RADIX_VMALLOC_END;
384 vmemmap = (struct page *)RADIX_VMEMMAP_BASE;
385 ioremap_bot = IOREMAP_BASE;
Darren Stevensbfa37082016-06-29 21:06:28 +0100386
387#ifdef CONFIG_PCI
388 pci_io_base = ISA_IO_BASE;
389#endif
390
Aneesh Kumar K.V5ed7ecd2016-04-29 23:26:23 +1000391 /*
392 * For now radix also use the same frag size
393 */
394 __pte_frag_nr = H_PTE_FRAG_NR;
395 __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +1000396
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530397 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
Benjamin Herrenschmidt166dd7d2016-07-05 15:03:51 +1000398 radix_init_native();
Aneesh Kumar K.Vad410672016-08-24 15:03:39 +0530399 if (cpu_has_feature(CPU_FTR_POWER9_DD1))
400 update_hid_for_radix();
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530401 lpcr = mfspr(SPRN_LPCR);
Aneesh Kumar K.Vbf16cdf2016-07-13 15:05:21 +0530402 mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000403 radix_init_partition_table();
Balbir Singhee97b6b2016-11-15 17:56:14 +1100404 radix_init_amor();
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530405 }
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000406
Paul Mackerras9d661952016-11-21 16:00:58 +1100407 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
408
Balbir Singh3b10d002016-11-15 17:56:16 +1100409 radix_init_iamr();
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000410 radix_init_pgtable();
411}
412
413void radix__early_init_mmu_secondary(void)
414{
415 unsigned long lpcr;
416 /*
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530417 * update partition table control register and UPRT
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000418 */
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530419 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
Aneesh Kumar K.Vcac4a182016-11-17 15:46:23 +0530420
421 if (cpu_has_feature(CPU_FTR_POWER9_DD1))
422 update_hid_for_radix();
423
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530424 lpcr = mfspr(SPRN_LPCR);
Aneesh Kumar K.Vbf16cdf2016-07-13 15:05:21 +0530425 mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530426
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000427 mtspr(SPRN_PTCR,
428 __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
Balbir Singhee97b6b2016-11-15 17:56:14 +1100429 radix_init_amor();
Aneesh Kumar K.Vd6c88602016-05-31 11:56:29 +0530430 }
Balbir Singh3b10d002016-11-15 17:56:16 +1100431 radix_init_iamr();
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000432}
433
Benjamin Herrenschmidtfe036a02016-08-19 14:22:37 +0530434void radix__mmu_cleanup_all(void)
435{
436 unsigned long lpcr;
437
438 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
439 lpcr = mfspr(SPRN_LPCR);
440 mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
441 mtspr(SPRN_PTCR, 0);
Alistair Popple1d0761d2016-12-14 13:36:51 +1100442 powernv_set_nmmu_ptcr(0);
Benjamin Herrenschmidtfe036a02016-08-19 14:22:37 +0530443 radix__flush_tlb_all();
444 }
445}
446
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000447void radix__setup_initial_memory_limit(phys_addr_t first_memblock_base,
448 phys_addr_t first_memblock_size)
449{
Aneesh Kumar K.V177ba7c2016-04-29 23:26:10 +1000450 /* We don't currently support the first MEMBLOCK not mapping 0
451 * physical on those processors
452 */
453 BUG_ON(first_memblock_base != 0);
454 /*
455 * We limit the allocation that depend on ppc64_rma_size
456 * to first_memblock_size. We also clamp it to 1GB to
457 * avoid some funky things such as RTAS bugs.
458 *
459 * On radix config we really don't have a limitation
460 * on real mode access. But keeping it as above works
461 * well enough.
462 */
463 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
464 /*
465 * Finally limit subsequent allocations. We really don't want
466 * to limit the memblock allocations to rma_size. FIXME!! should
467 * we even limit at all ?
468 */
Aneesh Kumar K.V2bfd65e2016-04-29 23:25:58 +1000469 memblock_set_current_limit(first_memblock_base + first_memblock_size);
470}
Aneesh Kumar K.Vd9225ad2016-04-29 23:26:00 +1000471
472#ifdef CONFIG_SPARSEMEM_VMEMMAP
473int __meminit radix__vmemmap_create_mapping(unsigned long start,
474 unsigned long page_size,
475 unsigned long phys)
476{
477 /* Create a PTE encoding */
478 unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
479
480 BUG_ON(radix__map_kernel_page(start, phys, __pgprot(flags), page_size));
481 return 0;
482}
483
484#ifdef CONFIG_MEMORY_HOTPLUG
485void radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
486{
487 /* FIXME!! intel does more. We should free page tables mapping vmemmap ? */
488}
489#endif
490#endif
Aneesh Kumar K.Vbde3eb62016-04-29 23:26:30 +1000491
492#ifdef CONFIG_TRANSPARENT_HUGEPAGE
493
494unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
495 pmd_t *pmdp, unsigned long clr,
496 unsigned long set)
497{
498 unsigned long old;
499
500#ifdef CONFIG_DEBUG_VM
501 WARN_ON(!radix__pmd_trans_huge(*pmdp));
502 assert_spin_locked(&mm->page_table_lock);
503#endif
504
505 old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
506 trace_hugepage_update(addr, old, clr, set);
507
508 return old;
509}
510
511pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
512 pmd_t *pmdp)
513
514{
515 pmd_t pmd;
516
517 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
518 VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
519 /*
520 * khugepaged calls this for normal pmd
521 */
522 pmd = *pmdp;
523 pmd_clear(pmdp);
524 /*FIXME!! Verify whether we need this kick below */
525 kick_all_cpus_sync();
526 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
527 return pmd;
528}
529
530/*
531 * For us pgtable_t is pte_t *. Inorder to save the deposisted
532 * page table, we consider the allocated page table as a list
533 * head. On withdraw we need to make sure we zero out the used
534 * list_head memory area.
535 */
536void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
537 pgtable_t pgtable)
538{
539 struct list_head *lh = (struct list_head *) pgtable;
540
541 assert_spin_locked(pmd_lockptr(mm, pmdp));
542
543 /* FIFO */
544 if (!pmd_huge_pte(mm, pmdp))
545 INIT_LIST_HEAD(lh);
546 else
547 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
548 pmd_huge_pte(mm, pmdp) = pgtable;
549}
550
551pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
552{
553 pte_t *ptep;
554 pgtable_t pgtable;
555 struct list_head *lh;
556
557 assert_spin_locked(pmd_lockptr(mm, pmdp));
558
559 /* FIFO */
560 pgtable = pmd_huge_pte(mm, pmdp);
561 lh = (struct list_head *) pgtable;
562 if (list_empty(lh))
563 pmd_huge_pte(mm, pmdp) = NULL;
564 else {
565 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
566 list_del(lh);
567 }
568 ptep = (pte_t *) pgtable;
569 *ptep = __pte(0);
570 ptep++;
571 *ptep = __pte(0);
572 return pgtable;
573}
574
575
576pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
577 unsigned long addr, pmd_t *pmdp)
578{
579 pmd_t old_pmd;
580 unsigned long old;
581
582 old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
583 old_pmd = __pmd(old);
584 /*
585 * Serialize against find_linux_pte_or_hugepte which does lock-less
586 * lookup in page tables with local interrupts disabled. For huge pages
587 * it casts pmd_t to pte_t. Since format of pte_t is different from
588 * pmd_t we want to prevent transit from pmd pointing to page table
589 * to pmd pointing to huge page (and back) while interrupts are disabled.
590 * We clear pmd to possibly replace it with page table pointer in
591 * different code paths. So make sure we wait for the parallel
592 * find_linux_pte_or_hugepage to finish.
593 */
594 kick_all_cpus_sync();
595 return old_pmd;
596}
597
598int radix__has_transparent_hugepage(void)
599{
600 /* For radix 2M at PMD level means thp */
601 if (mmu_psize_defs[MMU_PAGE_2M].shift == PMD_SHIFT)
602 return 1;
603 return 0;
604}
605#endif /* CONFIG_TRANSPARENT_HUGEPAGE */