pgtable-radix.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. smp_wmb();
  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. smp_wmb();
  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
  211. print_mapping(unsigned long start, unsigned long end, unsigned long size, bool exec)
  212. {
  213. char buf[10];
  214. if (end <= start)
  215. return;
  216. string_get_size(size, 1, STRING_UNITS_2, buf, sizeof(buf));
  217. pr_info("Mapped 0x%016lx-0x%016lx with %s pages%s\n", start, end, buf,
  218. exec ? " (exec)" : "");
  219. }
  220. static unsigned long next_boundary(unsigned long addr, unsigned long end)
  221. {
  222. #ifdef CONFIG_STRICT_KERNEL_RWX
  223. if (addr < __pa_symbol(__init_begin))
  224. return __pa_symbol(__init_begin);
  225. #endif
  226. return end;
  227. }
  228. static int __meminit create_physical_mapping(unsigned long start,
  229. unsigned long end,
  230. int nid)
  231. {
  232. unsigned long vaddr, addr, mapping_size = 0;
  233. bool prev_exec, exec = false;
  234. pgprot_t prot;
  235. int psize;
  236. start = _ALIGN_UP(start, PAGE_SIZE);
  237. for (addr = start; addr < end; addr += mapping_size) {
  238. unsigned long gap, previous_size;
  239. int rc;
  240. gap = next_boundary(addr, end) - addr;
  241. previous_size = mapping_size;
  242. prev_exec = exec;
  243. if (IS_ALIGNED(addr, PUD_SIZE) && gap >= PUD_SIZE &&
  244. mmu_psize_defs[MMU_PAGE_1G].shift) {
  245. mapping_size = PUD_SIZE;
  246. psize = MMU_PAGE_1G;
  247. } else if (IS_ALIGNED(addr, PMD_SIZE) && gap >= PMD_SIZE &&
  248. mmu_psize_defs[MMU_PAGE_2M].shift) {
  249. mapping_size = PMD_SIZE;
  250. psize = MMU_PAGE_2M;
  251. } else {
  252. mapping_size = PAGE_SIZE;
  253. psize = mmu_virtual_psize;
  254. }
  255. vaddr = (unsigned long)__va(addr);
  256. if (overlaps_kernel_text(vaddr, vaddr + mapping_size) ||
  257. overlaps_interrupt_vector_text(vaddr, vaddr + mapping_size)) {
  258. prot = PAGE_KERNEL_X;
  259. exec = true;
  260. } else {
  261. prot = PAGE_KERNEL;
  262. exec = false;
  263. }
  264. if (mapping_size != previous_size || exec != prev_exec) {
  265. print_mapping(start, addr, previous_size, prev_exec);
  266. start = addr;
  267. }
  268. rc = __map_kernel_page(vaddr, addr, prot, mapping_size, nid, start, end);
  269. if (rc)
  270. return rc;
  271. update_page_count(psize, 1);
  272. }
  273. print_mapping(start, addr, mapping_size, exec);
  274. return 0;
  275. }
  276. void __init radix_init_pgtable(void)
  277. {
  278. unsigned long rts_field;
  279. struct memblock_region *reg;
  280. /* We don't support slb for radix */
  281. mmu_slb_size = 0;
  282. /*
  283. * Create the linear mapping, using standard page size for now
  284. */
  285. for_each_memblock(memory, reg) {
  286. /*
  287. * The memblock allocator is up at this point, so the
  288. * page tables will be allocated within the range. No
  289. * need or a node (which we don't have yet).
  290. */
  291. WARN_ON(create_physical_mapping(reg->base,
  292. reg->base + reg->size,
  293. -1));
  294. }
  295. /* Find out how many PID bits are supported */
  296. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  297. if (!mmu_pid_bits)
  298. mmu_pid_bits = 20;
  299. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  300. /*
  301. * When KVM is possible, we only use the top half of the
  302. * PID space to avoid collisions between host and guest PIDs
  303. * which can cause problems due to prefetch when exiting the
  304. * guest with AIL=3
  305. */
  306. mmu_base_pid = 1 << (mmu_pid_bits - 1);
  307. #else
  308. mmu_base_pid = 1;
  309. #endif
  310. } else {
  311. /* The guest uses the bottom half of the PID space */
  312. if (!mmu_pid_bits)
  313. mmu_pid_bits = 19;
  314. mmu_base_pid = 1;
  315. }
  316. /*
  317. * Allocate Partition table and process table for the
  318. * host.
  319. */
  320. BUG_ON(PRTB_SIZE_SHIFT > 36);
  321. process_tb = early_alloc_pgtable(1UL << PRTB_SIZE_SHIFT, -1, 0, 0);
  322. /*
  323. * Fill in the process table.
  324. */
  325. rts_field = radix__get_tree_size();
  326. process_tb->prtb0 = cpu_to_be64(rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE);
  327. /*
  328. * Fill in the partition table. We are suppose to use effective address
  329. * of process table here. But our linear mapping also enable us to use
  330. * physical address here.
  331. */
  332. register_process_table(__pa(process_tb), 0, PRTB_SIZE_SHIFT - 12);
  333. pr_info("Process table %p and radix root for kernel: %p\n", process_tb, init_mm.pgd);
  334. asm volatile("ptesync" : : : "memory");
  335. asm volatile(PPC_TLBIE_5(%0,%1,2,1,1) : :
  336. "r" (TLBIEL_INVAL_SET_LPID), "r" (0));
  337. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  338. trace_tlbie(0, 0, TLBIEL_INVAL_SET_LPID, 0, 2, 1, 1);
  339. /*
  340. * The init_mm context is given the first available (non-zero) PID,
  341. * which is the "guard PID" and contains no page table. PIDR should
  342. * never be set to zero because that duplicates the kernel address
  343. * space at the 0x0... offset (quadrant 0)!
  344. *
  345. * An arbitrary PID that may later be allocated by the PID allocator
  346. * for userspace processes must not be used either, because that
  347. * would cause stale user mappings for that PID on CPUs outside of
  348. * the TLB invalidation scheme (because it won't be in mm_cpumask).
  349. *
  350. * So permanently carve out one PID for the purpose of a guard PID.
  351. */
  352. init_mm.context.id = mmu_base_pid;
  353. mmu_base_pid++;
  354. }
  355. static void __init radix_init_partition_table(void)
  356. {
  357. unsigned long rts_field, dw0;
  358. mmu_partition_table_init();
  359. rts_field = radix__get_tree_size();
  360. dw0 = rts_field | __pa(init_mm.pgd) | RADIX_PGD_INDEX_SIZE | PATB_HR;
  361. mmu_partition_table_set_entry(0, dw0, 0);
  362. pr_info("Initializing Radix MMU\n");
  363. pr_info("Partition table %p\n", partition_tb);
  364. }
  365. void __init radix_init_native(void)
  366. {
  367. register_process_table = native_register_process_table;
  368. }
  369. static int __init get_idx_from_shift(unsigned int shift)
  370. {
  371. int idx = -1;
  372. switch (shift) {
  373. case 0xc:
  374. idx = MMU_PAGE_4K;
  375. break;
  376. case 0x10:
  377. idx = MMU_PAGE_64K;
  378. break;
  379. case 0x15:
  380. idx = MMU_PAGE_2M;
  381. break;
  382. case 0x1e:
  383. idx = MMU_PAGE_1G;
  384. break;
  385. }
  386. return idx;
  387. }
  388. static int __init radix_dt_scan_page_sizes(unsigned long node,
  389. const char *uname, int depth,
  390. void *data)
  391. {
  392. int size = 0;
  393. int shift, idx;
  394. unsigned int ap;
  395. const __be32 *prop;
  396. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  397. /* We are scanning "cpu" nodes only */
  398. if (type == NULL || strcmp(type, "cpu") != 0)
  399. return 0;
  400. /* Find MMU PID size */
  401. prop = of_get_flat_dt_prop(node, "ibm,mmu-pid-bits", &size);
  402. if (prop && size == 4)
  403. mmu_pid_bits = be32_to_cpup(prop);
  404. /* Grab page size encodings */
  405. prop = of_get_flat_dt_prop(node, "ibm,processor-radix-AP-encodings", &size);
  406. if (!prop)
  407. return 0;
  408. pr_info("Page sizes from device-tree:\n");
  409. for (; size >= 4; size -= 4, ++prop) {
  410. struct mmu_psize_def *def;
  411. /* top 3 bit is AP encoding */
  412. shift = be32_to_cpu(prop[0]) & ~(0xe << 28);
  413. ap = be32_to_cpu(prop[0]) >> 29;
  414. pr_info("Page size shift = %d AP=0x%x\n", shift, ap);
  415. idx = get_idx_from_shift(shift);
  416. if (idx < 0)
  417. continue;
  418. def = &mmu_psize_defs[idx];
  419. def->shift = shift;
  420. def->ap = ap;
  421. }
  422. /* needed ? */
  423. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  424. return 1;
  425. }
  426. void __init radix__early_init_devtree(void)
  427. {
  428. int rc;
  429. /*
  430. * Try to find the available page sizes in the device-tree
  431. */
  432. rc = of_scan_flat_dt(radix_dt_scan_page_sizes, NULL);
  433. if (rc != 0) /* Found */
  434. goto found;
  435. /*
  436. * let's assume we have page 4k and 64k support
  437. */
  438. mmu_psize_defs[MMU_PAGE_4K].shift = 12;
  439. mmu_psize_defs[MMU_PAGE_4K].ap = 0x0;
  440. mmu_psize_defs[MMU_PAGE_64K].shift = 16;
  441. mmu_psize_defs[MMU_PAGE_64K].ap = 0x5;
  442. found:
  443. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  444. if (mmu_psize_defs[MMU_PAGE_2M].shift) {
  445. /*
  446. * map vmemmap using 2M if available
  447. */
  448. mmu_vmemmap_psize = MMU_PAGE_2M;
  449. }
  450. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  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. mmu_vmemmap_psize = mmu_virtual_psize;
  485. #endif
  486. /*
  487. * initialize page table size
  488. */
  489. __pte_index_size = RADIX_PTE_INDEX_SIZE;
  490. __pmd_index_size = RADIX_PMD_INDEX_SIZE;
  491. __pud_index_size = RADIX_PUD_INDEX_SIZE;
  492. __pgd_index_size = RADIX_PGD_INDEX_SIZE;
  493. __pud_cache_index = RADIX_PUD_INDEX_SIZE;
  494. __pte_table_size = RADIX_PTE_TABLE_SIZE;
  495. __pmd_table_size = RADIX_PMD_TABLE_SIZE;
  496. __pud_table_size = RADIX_PUD_TABLE_SIZE;
  497. __pgd_table_size = RADIX_PGD_TABLE_SIZE;
  498. __pmd_val_bits = RADIX_PMD_VAL_BITS;
  499. __pud_val_bits = RADIX_PUD_VAL_BITS;
  500. __pgd_val_bits = RADIX_PGD_VAL_BITS;
  501. __kernel_virt_start = RADIX_KERN_VIRT_START;
  502. __kernel_virt_size = RADIX_KERN_VIRT_SIZE;
  503. __vmalloc_start = RADIX_VMALLOC_START;
  504. __vmalloc_end = RADIX_VMALLOC_END;
  505. __kernel_io_start = RADIX_KERN_IO_START;
  506. vmemmap = (struct page *)RADIX_VMEMMAP_BASE;
  507. ioremap_bot = IOREMAP_BASE;
  508. #ifdef CONFIG_PCI
  509. pci_io_base = ISA_IO_BASE;
  510. #endif
  511. __pte_frag_nr = RADIX_PTE_FRAG_NR;
  512. __pte_frag_size_shift = RADIX_PTE_FRAG_SIZE_SHIFT;
  513. __pmd_frag_nr = RADIX_PMD_FRAG_NR;
  514. __pmd_frag_size_shift = RADIX_PMD_FRAG_SIZE_SHIFT;
  515. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  516. radix_init_native();
  517. lpcr = mfspr(SPRN_LPCR);
  518. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  519. radix_init_partition_table();
  520. radix_init_amor();
  521. } else {
  522. radix_init_pseries();
  523. }
  524. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  525. radix_init_iamr();
  526. radix_init_pgtable();
  527. /* Switch to the guard PID before turning on MMU */
  528. radix__switch_mmu_context(NULL, &init_mm);
  529. if (cpu_has_feature(CPU_FTR_HVMODE))
  530. tlbiel_all();
  531. }
  532. void radix__early_init_mmu_secondary(void)
  533. {
  534. unsigned long lpcr;
  535. /*
  536. * update partition table control register and UPRT
  537. */
  538. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  539. lpcr = mfspr(SPRN_LPCR);
  540. mtspr(SPRN_LPCR, lpcr | LPCR_UPRT | LPCR_HR);
  541. mtspr(SPRN_PTCR,
  542. __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
  543. radix_init_amor();
  544. }
  545. radix_init_iamr();
  546. radix__switch_mmu_context(NULL, &init_mm);
  547. if (cpu_has_feature(CPU_FTR_HVMODE))
  548. tlbiel_all();
  549. }
  550. void radix__mmu_cleanup_all(void)
  551. {
  552. unsigned long lpcr;
  553. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  554. lpcr = mfspr(SPRN_LPCR);
  555. mtspr(SPRN_LPCR, lpcr & ~LPCR_UPRT);
  556. mtspr(SPRN_PTCR, 0);
  557. powernv_set_nmmu_ptcr(0);
  558. radix__flush_tlb_all();
  559. }
  560. }
  561. void radix__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  562. phys_addr_t first_memblock_size)
  563. {
  564. /* We don't currently support the first MEMBLOCK not mapping 0
  565. * physical on those processors
  566. */
  567. BUG_ON(first_memblock_base != 0);
  568. /*
  569. * Radix mode is not limited by RMA / VRMA addressing.
  570. */
  571. ppc64_rma_size = ULONG_MAX;
  572. }
  573. #ifdef CONFIG_MEMORY_HOTPLUG
  574. static void free_pte_table(pte_t *pte_start, pmd_t *pmd)
  575. {
  576. pte_t *pte;
  577. int i;
  578. for (i = 0; i < PTRS_PER_PTE; i++) {
  579. pte = pte_start + i;
  580. if (!pte_none(*pte))
  581. return;
  582. }
  583. pte_free_kernel(&init_mm, pte_start);
  584. pmd_clear(pmd);
  585. }
  586. static void free_pmd_table(pmd_t *pmd_start, pud_t *pud)
  587. {
  588. pmd_t *pmd;
  589. int i;
  590. for (i = 0; i < PTRS_PER_PMD; i++) {
  591. pmd = pmd_start + i;
  592. if (!pmd_none(*pmd))
  593. return;
  594. }
  595. pmd_free(&init_mm, pmd_start);
  596. pud_clear(pud);
  597. }
  598. struct change_mapping_params {
  599. pte_t *pte;
  600. unsigned long start;
  601. unsigned long end;
  602. unsigned long aligned_start;
  603. unsigned long aligned_end;
  604. };
  605. static int __meminit stop_machine_change_mapping(void *data)
  606. {
  607. struct change_mapping_params *params =
  608. (struct change_mapping_params *)data;
  609. if (!data)
  610. return -1;
  611. spin_unlock(&init_mm.page_table_lock);
  612. pte_clear(&init_mm, params->aligned_start, params->pte);
  613. create_physical_mapping(params->aligned_start, params->start, -1);
  614. create_physical_mapping(params->end, params->aligned_end, -1);
  615. spin_lock(&init_mm.page_table_lock);
  616. return 0;
  617. }
  618. static void remove_pte_table(pte_t *pte_start, unsigned long addr,
  619. unsigned long end)
  620. {
  621. unsigned long next;
  622. pte_t *pte;
  623. pte = pte_start + pte_index(addr);
  624. for (; addr < end; addr = next, pte++) {
  625. next = (addr + PAGE_SIZE) & PAGE_MASK;
  626. if (next > end)
  627. next = end;
  628. if (!pte_present(*pte))
  629. continue;
  630. if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(next)) {
  631. /*
  632. * The vmemmap_free() and remove_section_mapping()
  633. * codepaths call us with aligned addresses.
  634. */
  635. WARN_ONCE(1, "%s: unaligned range\n", __func__);
  636. continue;
  637. }
  638. pte_clear(&init_mm, addr, pte);
  639. }
  640. }
  641. /*
  642. * clear the pte and potentially split the mapping helper
  643. */
  644. static void __meminit split_kernel_mapping(unsigned long addr, unsigned long end,
  645. unsigned long size, pte_t *pte)
  646. {
  647. unsigned long mask = ~(size - 1);
  648. unsigned long aligned_start = addr & mask;
  649. unsigned long aligned_end = addr + size;
  650. struct change_mapping_params params;
  651. bool split_region = false;
  652. if ((end - addr) < size) {
  653. /*
  654. * We're going to clear the PTE, but not flushed
  655. * the mapping, time to remap and flush. The
  656. * effects if visible outside the processor or
  657. * if we are running in code close to the
  658. * mapping we cleared, we are in trouble.
  659. */
  660. if (overlaps_kernel_text(aligned_start, addr) ||
  661. overlaps_kernel_text(end, aligned_end)) {
  662. /*
  663. * Hack, just return, don't pte_clear
  664. */
  665. WARN_ONCE(1, "Linear mapping %lx->%lx overlaps kernel "
  666. "text, not splitting\n", addr, end);
  667. return;
  668. }
  669. split_region = true;
  670. }
  671. if (split_region) {
  672. params.pte = pte;
  673. params.start = addr;
  674. params.end = end;
  675. params.aligned_start = addr & ~(size - 1);
  676. params.aligned_end = min_t(unsigned long, aligned_end,
  677. (unsigned long)__va(memblock_end_of_DRAM()));
  678. stop_machine(stop_machine_change_mapping, &params, NULL);
  679. return;
  680. }
  681. pte_clear(&init_mm, addr, pte);
  682. }
  683. static void remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
  684. unsigned long end)
  685. {
  686. unsigned long next;
  687. pte_t *pte_base;
  688. pmd_t *pmd;
  689. pmd = pmd_start + pmd_index(addr);
  690. for (; addr < end; addr = next, pmd++) {
  691. next = pmd_addr_end(addr, end);
  692. if (!pmd_present(*pmd))
  693. continue;
  694. if (pmd_huge(*pmd)) {
  695. split_kernel_mapping(addr, end, PMD_SIZE, (pte_t *)pmd);
  696. continue;
  697. }
  698. pte_base = (pte_t *)pmd_page_vaddr(*pmd);
  699. remove_pte_table(pte_base, addr, next);
  700. free_pte_table(pte_base, pmd);
  701. }
  702. }
  703. static void remove_pud_table(pud_t *pud_start, unsigned long addr,
  704. unsigned long end)
  705. {
  706. unsigned long next;
  707. pmd_t *pmd_base;
  708. pud_t *pud;
  709. pud = pud_start + pud_index(addr);
  710. for (; addr < end; addr = next, pud++) {
  711. next = pud_addr_end(addr, end);
  712. if (!pud_present(*pud))
  713. continue;
  714. if (pud_huge(*pud)) {
  715. split_kernel_mapping(addr, end, PUD_SIZE, (pte_t *)pud);
  716. continue;
  717. }
  718. pmd_base = (pmd_t *)pud_page_vaddr(*pud);
  719. remove_pmd_table(pmd_base, addr, next);
  720. free_pmd_table(pmd_base, pud);
  721. }
  722. }
  723. static void __meminit remove_pagetable(unsigned long start, unsigned long end)
  724. {
  725. unsigned long addr, next;
  726. pud_t *pud_base;
  727. pgd_t *pgd;
  728. spin_lock(&init_mm.page_table_lock);
  729. for (addr = start; addr < end; addr = next) {
  730. next = pgd_addr_end(addr, end);
  731. pgd = pgd_offset_k(addr);
  732. if (!pgd_present(*pgd))
  733. continue;
  734. if (pgd_huge(*pgd)) {
  735. split_kernel_mapping(addr, end, PGDIR_SIZE, (pte_t *)pgd);
  736. continue;
  737. }
  738. pud_base = (pud_t *)pgd_page_vaddr(*pgd);
  739. remove_pud_table(pud_base, addr, next);
  740. }
  741. spin_unlock(&init_mm.page_table_lock);
  742. radix__flush_tlb_kernel_range(start, end);
  743. }
  744. int __meminit radix__create_section_mapping(unsigned long start, unsigned long end, int nid)
  745. {
  746. return create_physical_mapping(start, end, nid);
  747. }
  748. int __meminit radix__remove_section_mapping(unsigned long start, unsigned long end)
  749. {
  750. remove_pagetable(start, end);
  751. return 0;
  752. }
  753. #endif /* CONFIG_MEMORY_HOTPLUG */
  754. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  755. static int __map_kernel_page_nid(unsigned long ea, unsigned long pa,
  756. pgprot_t flags, unsigned int map_page_size,
  757. int nid)
  758. {
  759. return __map_kernel_page(ea, pa, flags, map_page_size, nid, 0, 0);
  760. }
  761. int __meminit radix__vmemmap_create_mapping(unsigned long start,
  762. unsigned long page_size,
  763. unsigned long phys)
  764. {
  765. /* Create a PTE encoding */
  766. unsigned long flags = _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_KERNEL_RW;
  767. int nid = early_pfn_to_nid(phys >> PAGE_SHIFT);
  768. int ret;
  769. ret = __map_kernel_page_nid(start, phys, __pgprot(flags), page_size, nid);
  770. BUG_ON(ret);
  771. return 0;
  772. }
  773. #ifdef CONFIG_MEMORY_HOTPLUG
  774. void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
  775. {
  776. remove_pagetable(start, start + page_size);
  777. }
  778. #endif
  779. #endif
  780. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  781. unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,
  782. pmd_t *pmdp, unsigned long clr,
  783. unsigned long set)
  784. {
  785. unsigned long old;
  786. #ifdef CONFIG_DEBUG_VM
  787. WARN_ON(!radix__pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp));
  788. assert_spin_locked(pmd_lockptr(mm, pmdp));
  789. #endif
  790. old = radix__pte_update(mm, addr, (pte_t *)pmdp, clr, set, 1);
  791. trace_hugepage_update(addr, old, clr, set);
  792. return old;
  793. }
  794. pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,
  795. pmd_t *pmdp)
  796. {
  797. pmd_t pmd;
  798. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  799. VM_BUG_ON(radix__pmd_trans_huge(*pmdp));
  800. VM_BUG_ON(pmd_devmap(*pmdp));
  801. /*
  802. * khugepaged calls this for normal pmd
  803. */
  804. pmd = *pmdp;
  805. pmd_clear(pmdp);
  806. /*FIXME!! Verify whether we need this kick below */
  807. serialize_against_pte_lookup(vma->vm_mm);
  808. radix__flush_tlb_collapsed_pmd(vma->vm_mm, address);
  809. return pmd;
  810. }
  811. /*
  812. * For us pgtable_t is pte_t *. Inorder to save the deposisted
  813. * page table, we consider the allocated page table as a list
  814. * head. On withdraw we need to make sure we zero out the used
  815. * list_head memory area.
  816. */
  817. void radix__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  818. pgtable_t pgtable)
  819. {
  820. struct list_head *lh = (struct list_head *) pgtable;
  821. assert_spin_locked(pmd_lockptr(mm, pmdp));
  822. /* FIFO */
  823. if (!pmd_huge_pte(mm, pmdp))
  824. INIT_LIST_HEAD(lh);
  825. else
  826. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  827. pmd_huge_pte(mm, pmdp) = pgtable;
  828. }
  829. pgtable_t radix__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  830. {
  831. pte_t *ptep;
  832. pgtable_t pgtable;
  833. struct list_head *lh;
  834. assert_spin_locked(pmd_lockptr(mm, pmdp));
  835. /* FIFO */
  836. pgtable = pmd_huge_pte(mm, pmdp);
  837. lh = (struct list_head *) pgtable;
  838. if (list_empty(lh))
  839. pmd_huge_pte(mm, pmdp) = NULL;
  840. else {
  841. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  842. list_del(lh);
  843. }
  844. ptep = (pte_t *) pgtable;
  845. *ptep = __pte(0);
  846. ptep++;
  847. *ptep = __pte(0);
  848. return pgtable;
  849. }
  850. pmd_t radix__pmdp_huge_get_and_clear(struct mm_struct *mm,
  851. unsigned long addr, pmd_t *pmdp)
  852. {
  853. pmd_t old_pmd;
  854. unsigned long old;
  855. old = radix__pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);
  856. old_pmd = __pmd(old);
  857. /*
  858. * Serialize against find_current_mm_pte which does lock-less
  859. * lookup in page tables with local interrupts disabled. For huge pages
  860. * it casts pmd_t to pte_t. Since format of pte_t is different from
  861. * pmd_t we want to prevent transit from pmd pointing to page table
  862. * to pmd pointing to huge page (and back) while interrupts are disabled.
  863. * We clear pmd to possibly replace it with page table pointer in
  864. * different code paths. So make sure we wait for the parallel
  865. * find_current_mm_pte to finish.
  866. */
  867. serialize_against_pte_lookup(mm);
  868. return old_pmd;
  869. }
  870. int radix__has_transparent_hugepage(void)
  871. {
  872. /* For radix 2M at PMD level means thp */
  873. if (mmu_psize_defs[MMU_PAGE_2M].shift == PMD_SHIFT)
  874. return 1;
  875. return 0;
  876. }
  877. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  878. void radix__ptep_set_access_flags(struct vm_area_struct *vma, pte_t *ptep,
  879. pte_t entry, unsigned long address, int psize)
  880. {
  881. struct mm_struct *mm = vma->vm_mm;
  882. unsigned long set = pte_val(entry) & (_PAGE_DIRTY | _PAGE_ACCESSED |
  883. _PAGE_RW | _PAGE_EXEC);
  884. unsigned long change = pte_val(entry) ^ pte_val(*ptep);
  885. /*
  886. * To avoid NMMU hang while relaxing access, we need mark
  887. * the pte invalid in between.
  888. */
  889. if ((change & _PAGE_RW) && atomic_read(&mm->context.copros) > 0) {
  890. unsigned long old_pte, new_pte;
  891. old_pte = __radix_pte_update(ptep, _PAGE_PRESENT, _PAGE_INVALID);
  892. /*
  893. * new value of pte
  894. */
  895. new_pte = old_pte | set;
  896. radix__flush_tlb_page_psize(mm, address, psize);
  897. __radix_pte_update(ptep, _PAGE_INVALID, new_pte);
  898. } else {
  899. __radix_pte_update(ptep, 0, set);
  900. /*
  901. * Book3S does not require a TLB flush when relaxing access
  902. * restrictions when the address space is not attached to a
  903. * NMMU, because the core MMU will reload the pte after taking
  904. * an access fault, which is defined by the architectue.
  905. */
  906. }
  907. /* See ptesync comment in radix__set_pte_at */
  908. }