pgtable-radix.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  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. #define pr_fmt(fmt) "radix-mmu: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/sched/mm.h>
  14. #include <linux/memblock.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/mm.h>
  17. #include <linux/string_helpers.h>
  18. #include <linux/stop_machine.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/dma.h>
  23. #include <asm/machdep.h>
  24. #include <asm/mmu.h>
  25. #include <asm/firmware.h>
  26. #include <asm/powernv.h>
  27. #include <asm/sections.h>
  28. #include <asm/trace.h>
  29. #include <trace/events/thp.h>
  30. unsigned int mmu_pid_bits;
  31. unsigned int mmu_base_pid;
  32. static int native_register_process_table(unsigned long base, unsigned long pg_sz,
  33. unsigned long table_size)
  34. {
  35. unsigned long patb0, patb1;
  36. patb0 = be64_to_cpu(partition_tb[0].patb0);
  37. patb1 = base | table_size | PATB_GR;
  38. mmu_partition_table_set_entry(0, patb0, patb1);
  39. return 0;
  40. }
  41. static __ref void *early_alloc_pgtable(unsigned long size, int nid,
  42. unsigned long region_start, unsigned long region_end)
  43. {
  44. unsigned long pa = 0;
  45. void *pt;
  46. if (region_start || region_end) /* has region hint */
  47. pa = memblock_alloc_range(size, size, region_start, region_end,
  48. MEMBLOCK_NONE);
  49. else if (nid != -1) /* has node hint */
  50. pa = memblock_alloc_base_nid(size, size,
  51. MEMBLOCK_ALLOC_ANYWHERE,
  52. nid, MEMBLOCK_NONE);
  53. if (!pa)
  54. pa = memblock_alloc_base(size, size, MEMBLOCK_ALLOC_ANYWHERE);
  55. BUG_ON(!pa);
  56. pt = __va(pa);
  57. memset(pt, 0, size);
  58. return pt;
  59. }
  60. static int early_map_kernel_page(unsigned long ea, unsigned long pa,
  61. pgprot_t flags,
  62. unsigned int map_page_size,
  63. int nid,
  64. unsigned long region_start, unsigned long region_end)
  65. {
  66. unsigned long pfn = pa >> PAGE_SHIFT;
  67. pgd_t *pgdp;
  68. pud_t *pudp;
  69. pmd_t *pmdp;
  70. pte_t *ptep;
  71. pgdp = pgd_offset_k(ea);
  72. if (pgd_none(*pgdp)) {
  73. pudp = early_alloc_pgtable(PUD_TABLE_SIZE, nid,
  74. region_start, region_end);
  75. pgd_populate(&init_mm, pgdp, pudp);
  76. }
  77. pudp = pud_offset(pgdp, ea);
  78. if (map_page_size == PUD_SIZE) {
  79. ptep = (pte_t *)pudp;
  80. goto set_the_pte;
  81. }
  82. if (pud_none(*pudp)) {
  83. pmdp = early_alloc_pgtable(PMD_TABLE_SIZE, nid,
  84. region_start, region_end);
  85. pud_populate(&init_mm, pudp, pmdp);
  86. }
  87. pmdp = pmd_offset(pudp, ea);
  88. if (map_page_size == PMD_SIZE) {
  89. ptep = pmdp_ptep(pmdp);
  90. goto set_the_pte;
  91. }
  92. if (!pmd_present(*pmdp)) {
  93. ptep = early_alloc_pgtable(PAGE_SIZE, nid,
  94. region_start, region_end);
  95. pmd_populate_kernel(&init_mm, pmdp, ptep);
  96. }
  97. ptep = pte_offset_kernel(pmdp, ea);
  98. set_the_pte:
  99. set_pte_at(&init_mm, ea, ptep, pfn_pte(pfn, flags));
  100. asm volatile("ptesync": : :"memory");
  101. return 0;
  102. }
  103. /*
  104. * nid, region_start, and region_end are hints to try to place the page
  105. * table memory in the same node or region.
  106. */
  107. static int __map_kernel_page(unsigned long ea, unsigned long pa,
  108. pgprot_t flags,
  109. unsigned int map_page_size,
  110. int nid,
  111. unsigned long region_start, unsigned long region_end)
  112. {
  113. unsigned long pfn = pa >> PAGE_SHIFT;
  114. pgd_t *pgdp;
  115. pud_t *pudp;
  116. pmd_t *pmdp;
  117. pte_t *ptep;
  118. /*
  119. * Make sure task size is correct as per the max adddr
  120. */
  121. BUILD_BUG_ON(TASK_SIZE_USER64 > RADIX_PGTABLE_RANGE);
  122. if (unlikely(!slab_is_available()))
  123. return early_map_kernel_page(ea, pa, flags, map_page_size,
  124. nid, region_start, region_end);
  125. /*
  126. * Should make page table allocation functions be able to take a
  127. * node, so we can place kernel page tables on the right nodes after
  128. * boot.
  129. */
  130. pgdp = pgd_offset_k(ea);
  131. pudp = pud_alloc(&init_mm, pgdp, ea);
  132. if (!pudp)
  133. return -ENOMEM;
  134. if (map_page_size == PUD_SIZE) {
  135. ptep = (pte_t *)pudp;
  136. goto set_the_pte;
  137. }
  138. pmdp = pmd_alloc(&init_mm, pudp, ea);
  139. if (!pmdp)
  140. return -ENOMEM;
  141. if (map_page_size == PMD_SIZE) {
  142. ptep = pmdp_ptep(pmdp);
  143. goto set_the_pte;
  144. }
  145. ptep = pte_alloc_kernel(pmdp, ea);
  146. if (!ptep)
  147. return -ENOMEM;
  148. set_the_pte:
  149. set_pte_at(&init_mm, ea, ptep, pfn_pte(pfn, flags));
  150. asm volatile("ptesync": : :"memory");
  151. return 0;
  152. }
  153. int radix__map_kernel_page(unsigned long ea, unsigned long pa,
  154. pgprot_t flags,
  155. unsigned int map_page_size)
  156. {
  157. return __map_kernel_page(ea, pa, flags, map_page_size, -1, 0, 0);
  158. }
  159. #ifdef CONFIG_STRICT_KERNEL_RWX
  160. void radix__change_memory_range(unsigned long start, unsigned long end,
  161. unsigned long clear)
  162. {
  163. unsigned long idx;
  164. pgd_t *pgdp;
  165. pud_t *pudp;
  166. pmd_t *pmdp;
  167. pte_t *ptep;
  168. start = ALIGN_DOWN(start, PAGE_SIZE);
  169. end = PAGE_ALIGN(end); // aligns up
  170. pr_debug("Changing flags on range %lx-%lx removing 0x%lx\n",
  171. start, end, clear);
  172. for (idx = start; idx < end; idx += PAGE_SIZE) {
  173. pgdp = pgd_offset_k(idx);
  174. pudp = pud_alloc(&init_mm, pgdp, idx);
  175. if (!pudp)
  176. continue;
  177. if (pud_huge(*pudp)) {
  178. ptep = (pte_t *)pudp;
  179. goto update_the_pte;
  180. }
  181. pmdp = pmd_alloc(&init_mm, pudp, idx);
  182. if (!pmdp)
  183. continue;
  184. if (pmd_huge(*pmdp)) {
  185. ptep = pmdp_ptep(pmdp);
  186. goto update_the_pte;
  187. }
  188. ptep = pte_alloc_kernel(pmdp, idx);
  189. if (!ptep)
  190. continue;
  191. update_the_pte:
  192. radix__pte_update(&init_mm, idx, ptep, clear, 0, 0);
  193. }
  194. radix__flush_tlb_kernel_range(start, end);
  195. }
  196. void radix__mark_rodata_ro(void)
  197. {
  198. unsigned long start, end;
  199. start = (unsigned long)_stext;
  200. end = (unsigned long)__init_begin;
  201. radix__change_memory_range(start, end, _PAGE_WRITE);
  202. }
  203. void radix__mark_initmem_nx(void)
  204. {
  205. unsigned long start = (unsigned long)__init_begin;
  206. unsigned long end = (unsigned long)__init_end;
  207. radix__change_memory_range(start, end, _PAGE_EXEC);
  208. }
  209. #endif /* CONFIG_STRICT_KERNEL_RWX */
  210. static inline void __meminit print_mapping(unsigned long start,
  211. unsigned long end,
  212. unsigned long size)
  213. {
  214. char buf[10];
  215. if (end <= start)
  216. return;
  217. string_get_size(size, 1, STRING_UNITS_2, buf, sizeof(buf));
  218. pr_info("Mapped 0x%016lx-0x%016lx with %s pages\n", start, end, buf);
  219. }
  220. static int __meminit create_physical_mapping(unsigned long start,
  221. unsigned long end,
  222. int nid)
  223. {
  224. unsigned long vaddr, addr, mapping_size = 0;
  225. pgprot_t prot;
  226. unsigned long max_mapping_size;
  227. #ifdef CONFIG_STRICT_KERNEL_RWX
  228. int split_text_mapping = 1;
  229. #else
  230. int split_text_mapping = 0;
  231. #endif
  232. int psize;
  233. start = _ALIGN_UP(start, PAGE_SIZE);
  234. for (addr = start; addr < end; addr += mapping_size) {
  235. unsigned long gap, previous_size;
  236. int rc;
  237. gap = end - addr;
  238. previous_size = mapping_size;
  239. max_mapping_size = PUD_SIZE;
  240. retry:
  241. if (IS_ALIGNED(addr, PUD_SIZE) && gap >= PUD_SIZE &&
  242. mmu_psize_defs[MMU_PAGE_1G].shift &&
  243. PUD_SIZE <= max_mapping_size) {
  244. mapping_size = PUD_SIZE;
  245. psize = MMU_PAGE_1G;
  246. } else if (IS_ALIGNED(addr, PMD_SIZE) && gap >= PMD_SIZE &&
  247. mmu_psize_defs[MMU_PAGE_2M].shift) {
  248. mapping_size = PMD_SIZE;
  249. psize = MMU_PAGE_2M;
  250. } else {
  251. mapping_size = PAGE_SIZE;
  252. psize = mmu_virtual_psize;
  253. }
  254. if (split_text_mapping && (mapping_size == PUD_SIZE) &&
  255. (addr < __pa_symbol(__init_begin)) &&
  256. (addr + mapping_size) > __pa_symbol(__init_begin)) {
  257. max_mapping_size = PMD_SIZE;
  258. goto retry;
  259. }
  260. if (split_text_mapping && (mapping_size == PMD_SIZE) &&
  261. (addr < __pa_symbol(__init_begin)) &&
  262. (addr + mapping_size) > __pa_symbol(__init_begin)) {
  263. mapping_size = PAGE_SIZE;
  264. psize = mmu_virtual_psize;
  265. }
  266. if (mapping_size != previous_size) {
  267. print_mapping(start, addr, previous_size);
  268. start = addr;
  269. }
  270. vaddr = (unsigned long)__va(addr);
  271. if (overlaps_kernel_text(vaddr, vaddr + mapping_size) ||
  272. overlaps_interrupt_vector_text(vaddr, vaddr + mapping_size))
  273. prot = PAGE_KERNEL_X;
  274. else
  275. prot = PAGE_KERNEL;
  276. rc = __map_kernel_page(vaddr, addr, prot, mapping_size, nid, start, end);
  277. if (rc)
  278. return rc;
  279. update_page_count(psize, 1);
  280. }
  281. print_mapping(start, addr, mapping_size);
  282. return 0;
  283. }
  284. void __init radix_init_pgtable(void)
  285. {
  286. unsigned long rts_field;
  287. struct memblock_region *reg;
  288. /* We don't support slb for radix */
  289. mmu_slb_size = 0;
  290. /*
  291. * Create the linear mapping, using standard page size for now
  292. */
  293. for_each_memblock(memory, reg) {
  294. /*
  295. * The memblock allocator is up at this point, so the
  296. * page tables will be allocated within the range. No
  297. * need or a node (which we don't have yet).
  298. */
  299. WARN_ON(create_physical_mapping(reg->base,
  300. reg->base + reg->size,
  301. -1));
  302. }
  303. /* Find out how many PID bits are supported */
  304. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  305. if (!mmu_pid_bits)
  306. mmu_pid_bits = 20;
  307. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  308. /*
  309. * When KVM is possible, we only use the top half of the
  310. * PID space to avoid collisions between host and guest PIDs
  311. * which can cause problems due to prefetch when exiting the
  312. * guest with AIL=3
  313. */
  314. mmu_base_pid = 1 << (mmu_pid_bits - 1);
  315. #else
  316. mmu_base_pid = 1;
  317. #endif
  318. } else {
  319. /* The guest uses the bottom half of the PID space */
  320. if (!mmu_pid_bits)
  321. mmu_pid_bits = 19;
  322. mmu_base_pid = 1;
  323. }
  324. /*
  325. * Allocate Partition table and process table for the
  326. * host.
  327. */
  328. BUG_ON(PRTB_SIZE_SHIFT > 36);
  329. process_tb = early_alloc_pgtable(1UL << PRTB_SIZE_SHIFT, -1, 0, 0);
  330. /*
  331. * Fill in the process table.
  332. */
  333. rts_field = radix__get_tree_size();
  334. process_tb->prtb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE);
  335. /*
  336. * Fill in the partition table. We are suppose to use effective address
  337. * of process table here. But our linear mapping also enable us to use
  338. * physical address here.
  339. */
  340. register_process_table(__pa(process_tb), 0, PRTB_SIZE_SHIFT - 12);
  341. pr_info("Process table %p and radix root for kernel: %p\n", process_tb, init_mm.pgd);
  342. asm volatile("ptesync" : : : "memory");
  343. asm volatile(PPC_TLBIE_5(%0,%1,2,1,1) : :
  344. "r" (TLBIEL_INVAL_SET_LPID), "r" (0));
  345. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  346. trace_tlbie(0, 0, TLBIEL_INVAL_SET_LPID, 0, 2, 1, 1);
  347. /*
  348. * The init_mm context is given the first available (non-zero) PID,
  349. * which is the "guard PID" and contains no page table. PIDR should
  350. * never be set to zero because that duplicates the kernel address
  351. * space at the 0x0... offset (quadrant 0)!
  352. *
  353. * An arbitrary PID that may later be allocated by the PID allocator
  354. * for userspace processes must not be used either, because that
  355. * would cause stale user mappings for that PID on CPUs outside of
  356. * the TLB invalidation scheme (because it won't be in mm_cpumask).
  357. *
  358. * So permanently carve out one PID for the purpose of a guard PID.
  359. */
  360. init_mm.context.id = mmu_base_pid;
  361. mmu_base_pid++;
  362. }
  363. static void __init radix_init_partition_table(void)
  364. {
  365. unsigned long rts_field, dw0;
  366. mmu_partition_table_init();
  367. rts_field = radix__get_tree_size();
  368. dw0 = rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE | PATB_HR;
  369. mmu_partition_table_set_entry(0, dw0, 0);
  370. pr_info("Initializing Radix MMU\n");
  371. pr_info("Partition table %p\n", partition_tb);
  372. }
  373. void __init radix_init_native(void)
  374. {
  375. register_process_table = native_register_process_table;
  376. }
  377. static int __init get_idx_from_shift(unsigned int shift)
  378. {
  379. int idx = -1;
  380. switch (shift) {
  381. case 0xc:
  382. idx = MMU_PAGE_4K;
  383. break;
  384. case 0x10:
  385. idx = MMU_PAGE_64K;
  386. break;
  387. case 0x15:
  388. idx = MMU_PAGE_2M;
  389. break;
  390. case 0x1e:
  391. idx = MMU_PAGE_1G;
  392. break;
  393. }
  394. return idx;
  395. }
  396. static int __init radix_dt_scan_page_sizes(unsigned long node,
  397. const char *uname, int depth,
  398. void *data)
  399. {
  400. int size = 0;
  401. int shift, idx;
  402. unsigned int ap;
  403. const __be32 *prop;
  404. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  405. /* We are scanning "cpu" nodes only */
  406. if (type == NULL || strcmp(type, "cpu") != 0)
  407. return 0;
  408. /* Find MMU PID size */
  409. prop = of_get_flat_dt_prop(node, "ibm,mmu-pid-bits", &size);
  410. if (prop && size == 4)
  411. mmu_pid_bits = be32_to_cpup(prop);
  412. /* Grab page size encodings */
  413. prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
  414. if (!prop)
  415. return 0;
  416. pr_info("Page sizes from device-tree:\n");
  417. for (; size >= 4; size -= 4, ++prop) {
  418. struct mmu_psize_def *def;
  419. /* top 3 bit is AP encoding */
  420. shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
  421. ap = be32_to_cpu(prop[0]) >> 29;
  422. pr_info("Page size shift = %d AP=0x%x\n", shift, ap);
  423. idx = get_idx_from_shift(shift);
  424. if (idx < 0)
  425. continue;
  426. def = &mmu_psize_defs[idx];
  427. def->shift = shift;
  428. def->ap = ap;
  429. }
  430. /* needed ? */
  431. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  432. return 1;
  433. }
  434. void __init radix__early_init_devtree(void)
  435. {
  436. int rc;
  437. /*
  438. * Try to find the available page sizes in the device-tree
  439. */
  440. rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
  441. if (rc != 0) /* Found */
  442. goto found;
  443. /*
  444. * let's assume we have page 4k and 64k support
  445. */
  446. mmu_psize_defs[MMU_PAGE_4K].shift = 12;
  447. mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
  448. mmu_psize_defs[MMU_PAGE_64K].shift = 16;
  449. mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
  450. found:
  451. return;
  452. }
  453. static void radix_init_amor(void)
  454. {
  455. /*
  456. * In HV mode, we init AMOR (Authority Mask Override Register) so that
  457. * the hypervisor and guest can setup IAMR (Instruction Authority Mask
  458. * Register), enable key 0 and set it to 1.
  459. *
  460. * AMOR = 0b1100 .... 0000 (Mask for key 0 is 11)
  461. */
  462. mtspr(SPRN_AMOR, (3ul << 62));
  463. }
  464. static void radix_init_iamr(void)
  465. {
  466. /*
  467. * Radix always uses key0 of the IAMR to determine if an access is
  468. * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
  469. * fetch.
  470. */
  471. mtspr(SPRN_IAMR, (1ul << 62));
  472. }
  473. void __init radix__early_init_mmu(void)
  474. {
  475. unsigned long lpcr;
  476. #ifdef CONFIG_PPC_64K_PAGES
  477. /* PAGE_SIZE mappings */
  478. mmu_virtual_psize = MMU_PAGE_64K;
  479. #else
  480. mmu_virtual_psize = MMU_PAGE_4K;
  481. #endif
  482. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  483. /* vmemmap mapping */
  484. if (mmu_psize_defs[MMU_PAGE_2M].shift) {
  485. /*
  486. * map vmemmap using 2M if available
  487. */
  488. mmu_vmemmap_psize = MMU_PAGE_2M;
  489. } else
  490. mmu_vmemmap_psize = mmu_virtual_psize;
  491. #endif
  492. /*
  493. * initialize page table size
  494. */
  495. __pte_index_size = RADIX_PTE_INDEX_SIZE;
  496. __pmd_index_size = RADIX_PMD_INDEX_SIZE;
  497. __pud_index_size = RADIX_PUD_INDEX_SIZE;
  498. __pgd_index_size = RADIX_PGD_INDEX_SIZE;
  499. __pud_cache_index = RADIX_PUD_INDEX_SIZE;
  500. __pte_table_size = RADIX_PTE_TABLE_SIZE;
  501. __pmd_table_size = RADIX_PMD_TABLE_SIZE;
  502. __pud_table_size = RADIX_PUD_TABLE_SIZE;
  503. __pgd_table_size = RADIX_PGD_TABLE_SIZE;
  504. __pmd_val_bits = RADIX_PMD_VAL_BITS;
  505. __pud_val_bits = RADIX_PUD_VAL_BITS;
  506. __pgd_val_bits = RADIX_PGD_VAL_BITS;
  507. __kernel_virt_start = RADIX_KERN_VIRT_START;
  508. __kernel_virt_size = RADIX_KERN_VIRT_SIZE;
  509. __vmalloc_start = RADIX_VMALLOC_START;
  510. __vmalloc_end = RADIX_VMALLOC_END;
  511. __kernel_io_start = RADIX_KERN_IO_START;
  512. vmemmap = (struct page *)RADIX_VMEMMAP_BASE;
  513. ioremap_bot = IOREMAP_BASE;
  514. #ifdef CONFIG_PCI
  515. pci_io_base = ISA_IO_BASE;
  516. #endif
  517. __pte_frag_nr = RADIX_PTE_FRAG_NR;
  518. __pte_frag_size_shift = RADIX_PTE_FRAG_SIZE_SHIFT;
  519. __pmd_frag_nr = RADIX_PMD_FRAG_NR;
  520. __pmd_frag_size_shift = RADIX_PMD_FRAG_SIZE_SHIFT;
  521. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  522. radix_init_native();
  523. lpcr = mfspr(SPRN_LPCR);
  524. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  525. radix_init_partition_table();
  526. radix_init_amor();
  527. } else {
  528. radix_init_pseries();
  529. }
  530. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  531. radix_init_iamr();
  532. radix_init_pgtable();
  533. /* Switch to the guard PID before turning on MMU */
  534. radix__switch_mmu_context(NULL, &init_mm);
  535. if (cpu_has_feature(CPU_FTR_HVMODE))
  536. tlbiel_all();
  537. }
  538. void radix__early_init_mmu_secondary(void)
  539. {
  540. unsigned long lpcr;
  541. /*
  542. * update partition table control register and UPRT
  543. */
  544. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  545. lpcr = mfspr(SPRN_LPCR);
  546. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  547. mtspr(SPRN_PTCR,
  548. __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  549. radix_init_amor();
  550. }
  551. radix_init_iamr();
  552. radix__switch_mmu_context(NULL, &init_mm);
  553. if (cpu_has_feature(CPU_FTR_HVMODE))
  554. tlbiel_all();
  555. }
  556. void radix__mmu_cleanup_all(void)
  557. {
  558. unsigned long lpcr;
  559. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  560. lpcr = mfspr(SPRN_LPCR);
  561. mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
  562. mtspr(SPRN_PTCR, 0);
  563. powernv_set_nmmu_ptcr(0);
  564. radix__flush_tlb_all();
  565. }
  566. }
  567. void radix__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  568. phys_addr_t first_memblock_size)
  569. {
  570. /* We don't currently support the first MEMBLOCK not mapping 0
  571. * physical on those processors
  572. */
  573. BUG_ON(first_memblock_base != 0);
  574. /*
  575. * Radix mode is not limited by RMA / VRMA addressing.
  576. */
  577. ppc64_rma_size = ULONG_MAX;
  578. }
  579. #ifdef CONFIG_MEMORY_HOTPLUG
  580. static void free_pte_table(pte_t *pte_start, pmd_t *pmd)
  581. {
  582. pte_t *pte;
  583. int i;
  584. for (i = 0; i < PTRS_PER_PTE; i++) {
  585. pte = pte_start + i;
  586. if (!pte_none(*pte))
  587. return;
  588. }
  589. pte_free_kernel(&init_mm, pte_start);
  590. pmd_clear(pmd);
  591. }
  592. static void free_pmd_table(pmd_t *pmd_start, pud_t *pud)
  593. {
  594. pmd_t *pmd;
  595. int i;
  596. for (i = 0; i < PTRS_PER_PMD; i++) {
  597. pmd = pmd_start + i;
  598. if (!pmd_none(*pmd))
  599. return;
  600. }
  601. pmd_free(&init_mm, pmd_start);
  602. pud_clear(pud);
  603. }
  604. struct change_mapping_params {
  605. pte_t *pte;
  606. unsigned long start;
  607. unsigned long end;
  608. unsigned long aligned_start;
  609. unsigned long aligned_end;
  610. };
  611. static int __meminit stop_machine_change_mapping(void *data)
  612. {
  613. struct change_mapping_params *params =
  614. (struct change_mapping_params *)data;
  615. if (!data)
  616. return -1;
  617. spin_unlock(&init_mm.page_table_lock);
  618. pte_clear(&init_mm, params->aligned_start, params->pte);
  619. create_physical_mapping(__pa(params->aligned_start), __pa(params->start), -1);
  620. create_physical_mapping(__pa(params->end), __pa(params->aligned_end), -1);
  621. spin_lock(&init_mm.page_table_lock);
  622. return 0;
  623. }
  624. static void remove_pte_table(pte_t *pte_start, unsigned long addr,
  625. unsigned long end)
  626. {
  627. unsigned long next;
  628. pte_t *pte;
  629. pte = pte_start + pte_index(addr);
  630. for (; addr < end; addr = next, pte++) {
  631. next = (addr + PAGE_SIZE) & PAGE_MASK;
  632. if (next > end)
  633. next = end;
  634. if (!pte_present(*pte))
  635. continue;
  636. if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(next)) {
  637. /*
  638. * The vmemmap_free() and remove_section_mapping()
  639. * codepaths call us with aligned addresses.
  640. */
  641. WARN_ONCE(1, "%s: unaligned range\n", __func__);
  642. continue;
  643. }
  644. pte_clear(&init_mm, addr, pte);
  645. }
  646. }
  647. /*
  648. * clear the pte and potentially split the mapping helper
  649. */
  650. static void __meminit split_kernel_mapping(unsigned long addr, unsigned long end,
  651. unsigned long size, pte_t *pte)
  652. {
  653. unsigned long mask = ~(size - 1);
  654. unsigned long aligned_start = addr & mask;
  655. unsigned long aligned_end = addr + size;
  656. struct change_mapping_params params;
  657. bool split_region = false;
  658. if ((end - addr) < size) {
  659. /*
  660. * We're going to clear the PTE, but not flushed
  661. * the mapping, time to remap and flush. The
  662. * effects if visible outside the processor or
  663. * if we are running in code close to the
  664. * mapping we cleared, we are in trouble.
  665. */
  666. if (overlaps_kernel_text(aligned_start, addr) ||
  667. overlaps_kernel_text(end, aligned_end)) {
  668. /*
  669. * Hack, just return, don't pte_clear
  670. */
  671. WARN_ONCE(1, "Linear mapping %lx->%lx overlaps kernel "
  672. "text, not splitting\n", addr, end);
  673. return;
  674. }
  675. split_region = true;
  676. }
  677. if (split_region) {
  678. params.pte = pte;
  679. params.start = addr;
  680. params.end = end;
  681. params.aligned_start = addr & ~(size - 1);
  682. params.aligned_end = min_t(unsigned long, aligned_end,
  683. (unsigned long)__va(memblock_end_of_DRAM()));
  684. stop_machine(stop_machine_change_mapping, &params, NULL);
  685. return;
  686. }
  687. pte_clear(&init_mm, addr, pte);
  688. }
  689. static void remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
  690. unsigned long end)
  691. {
  692. unsigned long next;
  693. pte_t *pte_base;
  694. pmd_t *pmd;
  695. pmd = pmd_start + pmd_index(addr);
  696. for (; addr < end; addr = next, pmd++) {
  697. next = pmd_addr_end(addr, end);
  698. if (!pmd_present(*pmd))
  699. continue;
  700. if (pmd_huge(*pmd)) {
  701. split_kernel_mapping(addr, end, PMD_SIZE, (pte_t *)pmd);
  702. continue;
  703. }
  704. pte_base = (pte_t *)pmd_page_vaddr(*pmd);
  705. remove_pte_table(pte_base, addr, next);
  706. free_pte_table(pte_base, pmd);
  707. }
  708. }
  709. static void remove_pud_table(pud_t *pud_start, unsigned long addr,
  710. unsigned long end)
  711. {
  712. unsigned long next;
  713. pmd_t *pmd_base;
  714. pud_t *pud;
  715. pud = pud_start + pud_index(addr);
  716. for (; addr < end; addr = next, pud++) {
  717. next = pud_addr_end(addr, end);
  718. if (!pud_present(*pud))
  719. continue;
  720. if (pud_huge(*pud)) {
  721. split_kernel_mapping(addr, end, PUD_SIZE, (pte_t *)pud);
  722. continue;
  723. }
  724. pmd_base = (pmd_t *)pud_page_vaddr(*pud);
  725. remove_pmd_table(pmd_base, addr, next);
  726. free_pmd_table(pmd_base, pud);
  727. }
  728. }
  729. static void __meminit remove_pagetable(unsigned long start, unsigned long end)
  730. {
  731. unsigned long addr, next;
  732. pud_t *pud_base;
  733. pgd_t *pgd;
  734. spin_lock(&init_mm.page_table_lock);
  735. for (addr = start; addr < end; addr = next) {
  736. next = pgd_addr_end(addr, end);
  737. pgd = pgd_offset_k(addr);
  738. if (!pgd_present(*pgd))
  739. continue;
  740. if (pgd_huge(*pgd)) {
  741. split_kernel_mapping(addr, end, PGDIR_SIZE, (pte_t *)pgd);
  742. continue;
  743. }
  744. pud_base = (pud_t *)pgd_page_vaddr(*pgd);
  745. remove_pud_table(pud_base, addr, next);
  746. }
  747. spin_unlock(&init_mm.page_table_lock);
  748. radix__flush_tlb_kernel_range(start, end);
  749. }
  750. int __meminit radix__create_section_mapping(unsigned long start, unsigned long end, int nid)
  751. {
  752. return create_physical_mapping(start, end, nid);
  753. }
  754. int __meminit radix__remove_section_mapping(unsigned long start, unsigned long end)
  755. {
  756. remove_pagetable(start, end);
  757. return 0;
  758. }
  759. #endif /* CONFIG_MEMORY_HOTPLUG */
  760. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  761. static int __map_kernel_page_nid(unsigned long ea, unsigned long pa,
  762. pgprot_t flags, unsigned int map_page_size,
  763. int nid)
  764. {
  765. return __map_kernel_page(ea, pa, flags, map_page_size, nid, 0, 0);
  766. }
  767. int __meminit radix__vmemmap_create_mapping(unsigned long start,
  768. unsigned long page_size,
  769. unsigned long phys)
  770. {
  771. /* Create a PTE encoding */
  772. unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
  773. int nid = early_pfn_to_nid(phys >> PAGE_SHIFT);
  774. int ret;
  775. ret = __map_kernel_page_nid(start, phys, __pgprot(flags), page_size, nid);
  776. BUG_ON(ret);
  777. return 0;
  778. }
  779. #ifdef CONFIG_MEMORY_HOTPLUG
  780. void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
  781. {
  782. remove_pagetable(start, start + page_size);
  783. }
  784. #endif
  785. #endif
  786. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  787. unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  788. pmd_t *pmdp, unsigned long clr,
  789. unsigned long set)
  790. {
  791. unsigned long old;
  792. #ifdef CONFIG_DEBUG_VM
  793. WARN_ON(!radix__pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
  794. assert_spin_locked(pmd_lockptr(mm, pmdp));
  795. #endif
  796. old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
  797. trace_hugepage_update(addr, old, clr, set);
  798. return old;
  799. }
  800. pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  801. pmd_t *pmdp)
  802. {
  803. pmd_t pmd;
  804. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  805. VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
  806. VM_BUG_ON(pmd_devmap(*pmdp));
  807. /*
  808. * khugepaged calls this for normal pmd
  809. */
  810. pmd = *pmdp;
  811. pmd_clear(pmdp);
  812. /*FIXME!! Verify whether we need this kick below */
  813. serialize_against_pte_lookup(vma->vm_mm);
  814. radix__flush_tlb_collapsed_pmd(vma->vm_mm, address);
  815. return pmd;
  816. }
  817. /*
  818. * For us pgtable_t is pte_t *. Inorder to save the deposisted
  819. * page table, we consider the allocated page table as a list
  820. * head. On withdraw we need to make sure we zero out the used
  821. * list_head memory area.
  822. */
  823. void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  824. pgtable_t pgtable)
  825. {
  826. struct list_head *lh = (struct list_head *) pgtable;
  827. assert_spin_locked(pmd_lockptr(mm, pmdp));
  828. /* FIFO */
  829. if (!pmd_huge_pte(mm, pmdp))
  830. INIT_LIST_HEAD(lh);
  831. else
  832. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  833. pmd_huge_pte(mm, pmdp) = pgtable;
  834. }
  835. pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  836. {
  837. pte_t *ptep;
  838. pgtable_t pgtable;
  839. struct list_head *lh;
  840. assert_spin_locked(pmd_lockptr(mm, pmdp));
  841. /* FIFO */
  842. pgtable = pmd_huge_pte(mm, pmdp);
  843. lh = (struct list_head *) pgtable;
  844. if (list_empty(lh))
  845. pmd_huge_pte(mm, pmdp) = NULL;
  846. else {
  847. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  848. list_del(lh);
  849. }
  850. ptep = (pte_t *) pgtable;
  851. *ptep = __pte(0);
  852. ptep++;
  853. *ptep = __pte(0);
  854. return pgtable;
  855. }
  856. pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
  857. unsigned long addr, pmd_t *pmdp)
  858. {
  859. pmd_t old_pmd;
  860. unsigned long old;
  861. old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  862. old_pmd = __pmd(old);
  863. /*
  864. * Serialize against find_current_mm_pte which does lock-less
  865. * lookup in page tables with local interrupts disabled. For huge pages
  866. * it casts pmd_t to pte_t. Since format of pte_t is different from
  867. * pmd_t we want to prevent transit from pmd pointing to page table
  868. * to pmd pointing to huge page (and back) while interrupts are disabled.
  869. * We clear pmd to possibly replace it with page table pointer in
  870. * different code paths. So make sure we wait for the parallel
  871. * find_current_mm_pte to finish.
  872. */
  873. serialize_against_pte_lookup(mm);
  874. return old_pmd;
  875. }
  876. int radix__has_transparent_hugepage(void)
  877. {
  878. /* For radix 2M at PMD level means thp */
  879. if (mmu_psize_defs[MMU_PAGE_2M].shift == PMD_SHIFT)
  880. return 1;
  881. return 0;
  882. }
  883. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  884. void radix__ptep_set_access_flags(struct vm_area_struct *vma, pte_t *ptep,
  885. pte_t entry, unsigned long address, int psize)
  886. {
  887. struct mm_struct *mm = vma->vm_mm;
  888. unsigned long set = pte_val(entry) & (_PAGE_DIRTY | _PAGE_ACCESSED |
  889. _PAGE_RW | _PAGE_EXEC);
  890. unsigned long change = pte_val(entry) ^ pte_val(*ptep);
  891. /*
  892. * To avoid NMMU hang while relaxing access, we need mark
  893. * the pte invalid in between.
  894. */
  895. if ((change & _PAGE_RW) && atomic_read(&mm->context.copros) > 0) {
  896. unsigned long old_pte, new_pte;
  897. old_pte = __radix_pte_update(ptep, _PAGE_PRESENT, _PAGE_INVALID);
  898. /*
  899. * new value of pte
  900. */
  901. new_pte = old_pte | set;
  902. radix__flush_tlb_page_psize(mm, address, psize);
  903. __radix_pte_update(ptep, _PAGE_INVALID, new_pte);
  904. } else {
  905. __radix_pte_update(ptep, 0, set);
  906. /*
  907. * Book3S does not require a TLB flush when relaxing access
  908. * restrictions when the address space is not attached to a
  909. * NMMU, because the core MMU will reload the pte after taking
  910. * an access fault, which is defined by the architectue.
  911. */
  912. }
  913. /* See ptesync comment in radix__set_pte_at */
  914. }