init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Initialize MMU support.
  4. *
  5. * Copyright (C) 1998-2003 Hewlett-Packard Co
  6. * David Mosberger-Tang <davidm@hpl.hp.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/dma-map-ops.h>
  11. #include <linux/dmar.h>
  12. #include <linux/efi.h>
  13. #include <linux/elf.h>
  14. #include <linux/memblock.h>
  15. #include <linux/mm.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/mmzone.h>
  18. #include <linux/module.h>
  19. #include <linux/personality.h>
  20. #include <linux/reboot.h>
  21. #include <linux/slab.h>
  22. #include <linux/swap.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/bitops.h>
  25. #include <linux/kexec.h>
  26. #include <linux/swiotlb.h>
  27. #include <asm/dma.h>
  28. #include <asm/io.h>
  29. #include <asm/numa.h>
  30. #include <asm/patch.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/sal.h>
  33. #include <asm/sections.h>
  34. #include <asm/tlb.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/unistd.h>
  37. #include <asm/mca.h>
  38. extern void ia64_tlb_init (void);
  39. unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
  40. #ifdef CONFIG_VIRTUAL_MEM_MAP
  41. unsigned long VMALLOC_END = VMALLOC_END_INIT;
  42. EXPORT_SYMBOL(VMALLOC_END);
  43. struct page *vmem_map;
  44. EXPORT_SYMBOL(vmem_map);
  45. #endif
  46. struct page *zero_page_memmap_ptr; /* map entry for zero page */
  47. EXPORT_SYMBOL(zero_page_memmap_ptr);
  48. void
  49. __ia64_sync_icache_dcache (pte_t pte)
  50. {
  51. unsigned long addr;
  52. struct page *page;
  53. page = pte_page(pte);
  54. addr = (unsigned long) page_address(page);
  55. if (test_bit(PG_arch_1, &page->flags))
  56. return; /* i-cache is already coherent with d-cache */
  57. flush_icache_range(addr, addr + page_size(page));
  58. set_bit(PG_arch_1, &page->flags); /* mark page as clean */
  59. }
  60. /*
  61. * Since DMA is i-cache coherent, any (complete) pages that were written via
  62. * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
  63. * flush them when they get mapped into an executable vm-area.
  64. */
  65. void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
  66. {
  67. unsigned long pfn = PHYS_PFN(paddr);
  68. do {
  69. set_bit(PG_arch_1, &pfn_to_page(pfn)->flags);
  70. } while (++pfn <= PHYS_PFN(paddr + size - 1));
  71. }
  72. inline void
  73. ia64_set_rbs_bot (void)
  74. {
  75. unsigned long stack_size = rlimit_max(RLIMIT_STACK) & -16;
  76. if (stack_size > MAX_USER_STACK_SIZE)
  77. stack_size = MAX_USER_STACK_SIZE;
  78. current->thread.rbs_bot = PAGE_ALIGN(current->mm->start_stack - stack_size);
  79. }
  80. /*
  81. * This performs some platform-dependent address space initialization.
  82. * On IA-64, we want to setup the VM area for the register backing
  83. * store (which grows upwards) and install the gateway page which is
  84. * used for signal trampolines, etc.
  85. */
  86. void
  87. ia64_init_addr_space (void)
  88. {
  89. struct vm_area_struct *vma;
  90. ia64_set_rbs_bot();
  91. /*
  92. * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
  93. * the problem. When the process attempts to write to the register backing store
  94. * for the first time, it will get a SEGFAULT in this case.
  95. */
  96. vma = vm_area_alloc(current->mm);
  97. if (vma) {
  98. vma_set_anonymous(vma);
  99. vma->vm_start = current->thread.rbs_bot & PAGE_MASK;
  100. vma->vm_end = vma->vm_start + PAGE_SIZE;
  101. vma->vm_flags = VM_DATA_DEFAULT_FLAGS|VM_GROWSUP|VM_ACCOUNT;
  102. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  103. mmap_write_lock(current->mm);
  104. if (insert_vm_struct(current->mm, vma)) {
  105. mmap_write_unlock(current->mm);
  106. vm_area_free(vma);
  107. return;
  108. }
  109. mmap_write_unlock(current->mm);
  110. }
  111. /* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
  112. if (!(current->personality & MMAP_PAGE_ZERO)) {
  113. vma = vm_area_alloc(current->mm);
  114. if (vma) {
  115. vma_set_anonymous(vma);
  116. vma->vm_end = PAGE_SIZE;
  117. vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
  118. vma->vm_flags = VM_READ | VM_MAYREAD | VM_IO |
  119. VM_DONTEXPAND | VM_DONTDUMP;
  120. mmap_write_lock(current->mm);
  121. if (insert_vm_struct(current->mm, vma)) {
  122. mmap_write_unlock(current->mm);
  123. vm_area_free(vma);
  124. return;
  125. }
  126. mmap_write_unlock(current->mm);
  127. }
  128. }
  129. }
  130. void
  131. free_initmem (void)
  132. {
  133. free_reserved_area(ia64_imva(__init_begin), ia64_imva(__init_end),
  134. -1, "unused kernel");
  135. }
  136. void __init
  137. free_initrd_mem (unsigned long start, unsigned long end)
  138. {
  139. /*
  140. * EFI uses 4KB pages while the kernel can use 4KB or bigger.
  141. * Thus EFI and the kernel may have different page sizes. It is
  142. * therefore possible to have the initrd share the same page as
  143. * the end of the kernel (given current setup).
  144. *
  145. * To avoid freeing/using the wrong page (kernel sized) we:
  146. * - align up the beginning of initrd
  147. * - align down the end of initrd
  148. *
  149. * | |
  150. * |=============| a000
  151. * | |
  152. * | |
  153. * | | 9000
  154. * |/////////////|
  155. * |/////////////|
  156. * |=============| 8000
  157. * |///INITRD////|
  158. * |/////////////|
  159. * |/////////////| 7000
  160. * | |
  161. * |KKKKKKKKKKKKK|
  162. * |=============| 6000
  163. * |KKKKKKKKKKKKK|
  164. * |KKKKKKKKKKKKK|
  165. * K=kernel using 8KB pages
  166. *
  167. * In this example, we must free page 8000 ONLY. So we must align up
  168. * initrd_start and keep initrd_end as is.
  169. */
  170. start = PAGE_ALIGN(start);
  171. end = end & PAGE_MASK;
  172. if (start < end)
  173. printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
  174. for (; start < end; start += PAGE_SIZE) {
  175. if (!virt_addr_valid(start))
  176. continue;
  177. free_reserved_page(virt_to_page(start));
  178. }
  179. }
  180. /*
  181. * This installs a clean page in the kernel's page table.
  182. */
  183. static struct page * __init
  184. put_kernel_page (struct page *page, unsigned long address, pgprot_t pgprot)
  185. {
  186. pgd_t *pgd;
  187. p4d_t *p4d;
  188. pud_t *pud;
  189. pmd_t *pmd;
  190. pte_t *pte;
  191. pgd = pgd_offset_k(address); /* note: this is NOT pgd_offset()! */
  192. {
  193. p4d = p4d_alloc(&init_mm, pgd, address);
  194. if (!p4d)
  195. goto out;
  196. pud = pud_alloc(&init_mm, p4d, address);
  197. if (!pud)
  198. goto out;
  199. pmd = pmd_alloc(&init_mm, pud, address);
  200. if (!pmd)
  201. goto out;
  202. pte = pte_alloc_kernel(pmd, address);
  203. if (!pte)
  204. goto out;
  205. if (!pte_none(*pte))
  206. goto out;
  207. set_pte(pte, mk_pte(page, pgprot));
  208. }
  209. out:
  210. /* no need for flush_tlb */
  211. return page;
  212. }
  213. static void __init
  214. setup_gate (void)
  215. {
  216. struct page *page;
  217. /*
  218. * Map the gate page twice: once read-only to export the ELF
  219. * headers etc. and once execute-only page to enable
  220. * privilege-promotion via "epc":
  221. */
  222. page = virt_to_page(ia64_imva(__start_gate_section));
  223. put_kernel_page(page, GATE_ADDR, PAGE_READONLY);
  224. #ifdef HAVE_BUGGY_SEGREL
  225. page = virt_to_page(ia64_imva(__start_gate_section + PAGE_SIZE));
  226. put_kernel_page(page, GATE_ADDR + PAGE_SIZE, PAGE_GATE);
  227. #else
  228. put_kernel_page(page, GATE_ADDR + PERCPU_PAGE_SIZE, PAGE_GATE);
  229. /* Fill in the holes (if any) with read-only zero pages: */
  230. {
  231. unsigned long addr;
  232. for (addr = GATE_ADDR + PAGE_SIZE;
  233. addr < GATE_ADDR + PERCPU_PAGE_SIZE;
  234. addr += PAGE_SIZE)
  235. {
  236. put_kernel_page(ZERO_PAGE(0), addr,
  237. PAGE_READONLY);
  238. put_kernel_page(ZERO_PAGE(0), addr + PERCPU_PAGE_SIZE,
  239. PAGE_READONLY);
  240. }
  241. }
  242. #endif
  243. ia64_patch_gate();
  244. }
  245. static struct vm_area_struct gate_vma;
  246. static int __init gate_vma_init(void)
  247. {
  248. vma_init(&gate_vma, NULL);
  249. gate_vma.vm_start = FIXADDR_USER_START;
  250. gate_vma.vm_end = FIXADDR_USER_END;
  251. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  252. gate_vma.vm_page_prot = __P101;
  253. return 0;
  254. }
  255. __initcall(gate_vma_init);
  256. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  257. {
  258. return &gate_vma;
  259. }
  260. int in_gate_area_no_mm(unsigned long addr)
  261. {
  262. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  263. return 1;
  264. return 0;
  265. }
  266. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  267. {
  268. return in_gate_area_no_mm(addr);
  269. }
  270. void ia64_mmu_init(void *my_cpu_data)
  271. {
  272. unsigned long pta, impl_va_bits;
  273. extern void tlb_init(void);
  274. #ifdef CONFIG_DISABLE_VHPT
  275. # define VHPT_ENABLE_BIT 0
  276. #else
  277. # define VHPT_ENABLE_BIT 1
  278. #endif
  279. /*
  280. * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
  281. * address space. The IA-64 architecture guarantees that at least 50 bits of
  282. * virtual address space are implemented but if we pick a large enough page size
  283. * (e.g., 64KB), the mapped address space is big enough that it will overlap with
  284. * VMLPT. I assume that once we run on machines big enough to warrant 64KB pages,
  285. * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
  286. * problem in practice. Alternatively, we could truncate the top of the mapped
  287. * address space to not permit mappings that would overlap with the VMLPT.
  288. * --davidm 00/12/06
  289. */
  290. # define pte_bits 3
  291. # define mapped_space_bits (3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
  292. /*
  293. * The virtual page table has to cover the entire implemented address space within
  294. * a region even though not all of this space may be mappable. The reason for
  295. * this is that the Access bit and Dirty bit fault handlers perform
  296. * non-speculative accesses to the virtual page table, so the address range of the
  297. * virtual page table itself needs to be covered by virtual page table.
  298. */
  299. # define vmlpt_bits (impl_va_bits - PAGE_SHIFT + pte_bits)
  300. # define POW2(n) (1ULL << (n))
  301. impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
  302. if (impl_va_bits < 51 || impl_va_bits > 61)
  303. panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
  304. /*
  305. * mapped_space_bits - PAGE_SHIFT is the total number of ptes we need,
  306. * which must fit into "vmlpt_bits - pte_bits" slots. Second half of
  307. * the test makes sure that our mapped space doesn't overlap the
  308. * unimplemented hole in the middle of the region.
  309. */
  310. if ((mapped_space_bits - PAGE_SHIFT > vmlpt_bits - pte_bits) ||
  311. (mapped_space_bits > impl_va_bits - 1))
  312. panic("Cannot build a big enough virtual-linear page table"
  313. " to cover mapped address space.\n"
  314. " Try using a smaller page size.\n");
  315. /* place the VMLPT at the end of each page-table mapped region: */
  316. pta = POW2(61) - POW2(vmlpt_bits);
  317. /*
  318. * Set the (virtually mapped linear) page table address. Bit
  319. * 8 selects between the short and long format, bits 2-7 the
  320. * size of the table, and bit 0 whether the VHPT walker is
  321. * enabled.
  322. */
  323. ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
  324. ia64_tlb_init();
  325. #ifdef CONFIG_HUGETLB_PAGE
  326. ia64_set_rr(HPAGE_REGION_BASE, HPAGE_SHIFT << 2);
  327. ia64_srlz_d();
  328. #endif
  329. }
  330. #ifdef CONFIG_VIRTUAL_MEM_MAP
  331. int vmemmap_find_next_valid_pfn(int node, int i)
  332. {
  333. unsigned long end_address, hole_next_pfn;
  334. unsigned long stop_address;
  335. pg_data_t *pgdat = NODE_DATA(node);
  336. end_address = (unsigned long) &vmem_map[pgdat->node_start_pfn + i];
  337. end_address = PAGE_ALIGN(end_address);
  338. stop_address = (unsigned long) &vmem_map[pgdat_end_pfn(pgdat)];
  339. do {
  340. pgd_t *pgd;
  341. p4d_t *p4d;
  342. pud_t *pud;
  343. pmd_t *pmd;
  344. pte_t *pte;
  345. pgd = pgd_offset_k(end_address);
  346. if (pgd_none(*pgd)) {
  347. end_address += PGDIR_SIZE;
  348. continue;
  349. }
  350. p4d = p4d_offset(pgd, end_address);
  351. if (p4d_none(*p4d)) {
  352. end_address += P4D_SIZE;
  353. continue;
  354. }
  355. pud = pud_offset(p4d, end_address);
  356. if (pud_none(*pud)) {
  357. end_address += PUD_SIZE;
  358. continue;
  359. }
  360. pmd = pmd_offset(pud, end_address);
  361. if (pmd_none(*pmd)) {
  362. end_address += PMD_SIZE;
  363. continue;
  364. }
  365. pte = pte_offset_kernel(pmd, end_address);
  366. retry_pte:
  367. if (pte_none(*pte)) {
  368. end_address += PAGE_SIZE;
  369. pte++;
  370. if ((end_address < stop_address) &&
  371. (end_address != ALIGN(end_address, 1UL << PMD_SHIFT)))
  372. goto retry_pte;
  373. continue;
  374. }
  375. /* Found next valid vmem_map page */
  376. break;
  377. } while (end_address < stop_address);
  378. end_address = min(end_address, stop_address);
  379. end_address = end_address - (unsigned long) vmem_map + sizeof(struct page) - 1;
  380. hole_next_pfn = end_address / sizeof(struct page);
  381. return hole_next_pfn - pgdat->node_start_pfn;
  382. }
  383. int __init create_mem_map_page_table(u64 start, u64 end, void *arg)
  384. {
  385. unsigned long address, start_page, end_page;
  386. struct page *map_start, *map_end;
  387. int node;
  388. pgd_t *pgd;
  389. p4d_t *p4d;
  390. pud_t *pud;
  391. pmd_t *pmd;
  392. pte_t *pte;
  393. map_start = vmem_map + (__pa(start) >> PAGE_SHIFT);
  394. map_end = vmem_map + (__pa(end) >> PAGE_SHIFT);
  395. start_page = (unsigned long) map_start & PAGE_MASK;
  396. end_page = PAGE_ALIGN((unsigned long) map_end);
  397. node = paddr_to_nid(__pa(start));
  398. for (address = start_page; address < end_page; address += PAGE_SIZE) {
  399. pgd = pgd_offset_k(address);
  400. if (pgd_none(*pgd)) {
  401. p4d = memblock_alloc_node(PAGE_SIZE, PAGE_SIZE, node);
  402. if (!p4d)
  403. goto err_alloc;
  404. pgd_populate(&init_mm, pgd, p4d);
  405. }
  406. p4d = p4d_offset(pgd, address);
  407. if (p4d_none(*p4d)) {
  408. pud = memblock_alloc_node(PAGE_SIZE, PAGE_SIZE, node);
  409. if (!pud)
  410. goto err_alloc;
  411. p4d_populate(&init_mm, p4d, pud);
  412. }
  413. pud = pud_offset(p4d, address);
  414. if (pud_none(*pud)) {
  415. pmd = memblock_alloc_node(PAGE_SIZE, PAGE_SIZE, node);
  416. if (!pmd)
  417. goto err_alloc;
  418. pud_populate(&init_mm, pud, pmd);
  419. }
  420. pmd = pmd_offset(pud, address);
  421. if (pmd_none(*pmd)) {
  422. pte = memblock_alloc_node(PAGE_SIZE, PAGE_SIZE, node);
  423. if (!pte)
  424. goto err_alloc;
  425. pmd_populate_kernel(&init_mm, pmd, pte);
  426. }
  427. pte = pte_offset_kernel(pmd, address);
  428. if (pte_none(*pte)) {
  429. void *page = memblock_alloc_node(PAGE_SIZE, PAGE_SIZE,
  430. node);
  431. if (!page)
  432. goto err_alloc;
  433. set_pte(pte, pfn_pte(__pa(page) >> PAGE_SHIFT,
  434. PAGE_KERNEL));
  435. }
  436. }
  437. return 0;
  438. err_alloc:
  439. panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d\n",
  440. __func__, PAGE_SIZE, PAGE_SIZE, node);
  441. return -ENOMEM;
  442. }
  443. struct memmap_init_callback_data {
  444. struct page *start;
  445. struct page *end;
  446. int nid;
  447. unsigned long zone;
  448. };
  449. static int __meminit
  450. virtual_memmap_init(u64 start, u64 end, void *arg)
  451. {
  452. struct memmap_init_callback_data *args;
  453. struct page *map_start, *map_end;
  454. args = (struct memmap_init_callback_data *) arg;
  455. map_start = vmem_map + (__pa(start) >> PAGE_SHIFT);
  456. map_end = vmem_map + (__pa(end) >> PAGE_SHIFT);
  457. if (map_start < args->start)
  458. map_start = args->start;
  459. if (map_end > args->end)
  460. map_end = args->end;
  461. /*
  462. * We have to initialize "out of bounds" struct page elements that fit completely
  463. * on the same pages that were allocated for the "in bounds" elements because they
  464. * may be referenced later (and found to be "reserved").
  465. */
  466. map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1)) / sizeof(struct page);
  467. map_end += ((PAGE_ALIGN((unsigned long) map_end) - (unsigned long) map_end)
  468. / sizeof(struct page));
  469. if (map_start < map_end)
  470. memmap_init_zone((unsigned long)(map_end - map_start),
  471. args->nid, args->zone, page_to_pfn(map_start), page_to_pfn(map_end),
  472. MEMINIT_EARLY, NULL, MIGRATE_MOVABLE);
  473. return 0;
  474. }
  475. void __meminit
  476. arch_memmap_init (unsigned long size, int nid, unsigned long zone,
  477. unsigned long start_pfn)
  478. {
  479. if (!vmem_map) {
  480. memmap_init_zone(size, nid, zone, start_pfn, start_pfn + size,
  481. MEMINIT_EARLY, NULL, MIGRATE_MOVABLE);
  482. } else {
  483. struct page *start;
  484. struct memmap_init_callback_data args;
  485. start = pfn_to_page(start_pfn);
  486. args.start = start;
  487. args.end = start + size;
  488. args.nid = nid;
  489. args.zone = zone;
  490. efi_memmap_walk(virtual_memmap_init, &args);
  491. }
  492. }
  493. void __init memmap_init(void)
  494. {
  495. }
  496. int
  497. ia64_pfn_valid (unsigned long pfn)
  498. {
  499. char byte;
  500. struct page *pg = pfn_to_page(pfn);
  501. return (__get_user(byte, (char __user *) pg) == 0)
  502. && ((((u64)pg & PAGE_MASK) == (((u64)(pg + 1) - 1) & PAGE_MASK))
  503. || (__get_user(byte, (char __user *) (pg + 1) - 1) == 0));
  504. }
  505. EXPORT_SYMBOL(ia64_pfn_valid);
  506. int __init find_largest_hole(u64 start, u64 end, void *arg)
  507. {
  508. u64 *max_gap = arg;
  509. static u64 last_end = PAGE_OFFSET;
  510. /* NOTE: this algorithm assumes efi memmap table is ordered */
  511. if (*max_gap < (start - last_end))
  512. *max_gap = start - last_end;
  513. last_end = end;
  514. return 0;
  515. }
  516. #endif /* CONFIG_VIRTUAL_MEM_MAP */
  517. int __init register_active_ranges(u64 start, u64 len, int nid)
  518. {
  519. u64 end = start + len;
  520. #ifdef CONFIG_KEXEC
  521. if (start > crashk_res.start && start < crashk_res.end)
  522. start = crashk_res.end;
  523. if (end > crashk_res.start && end < crashk_res.end)
  524. end = crashk_res.start;
  525. #endif
  526. if (start < end)
  527. memblock_add_node(__pa(start), end - start, nid);
  528. return 0;
  529. }
  530. int
  531. find_max_min_low_pfn (u64 start, u64 end, void *arg)
  532. {
  533. unsigned long pfn_start, pfn_end;
  534. #ifdef CONFIG_FLATMEM
  535. pfn_start = (PAGE_ALIGN(__pa(start))) >> PAGE_SHIFT;
  536. pfn_end = (PAGE_ALIGN(__pa(end - 1))) >> PAGE_SHIFT;
  537. #else
  538. pfn_start = GRANULEROUNDDOWN(__pa(start)) >> PAGE_SHIFT;
  539. pfn_end = GRANULEROUNDUP(__pa(end - 1)) >> PAGE_SHIFT;
  540. #endif
  541. min_low_pfn = min(min_low_pfn, pfn_start);
  542. max_low_pfn = max(max_low_pfn, pfn_end);
  543. return 0;
  544. }
  545. /*
  546. * Boot command-line option "nolwsys" can be used to disable the use of any light-weight
  547. * system call handler. When this option is in effect, all fsyscalls will end up bubbling
  548. * down into the kernel and calling the normal (heavy-weight) syscall handler. This is
  549. * useful for performance testing, but conceivably could also come in handy for debugging
  550. * purposes.
  551. */
  552. static int nolwsys __initdata;
  553. static int __init
  554. nolwsys_setup (char *s)
  555. {
  556. nolwsys = 1;
  557. return 1;
  558. }
  559. __setup("nolwsys", nolwsys_setup);
  560. void __init
  561. mem_init (void)
  562. {
  563. int i;
  564. BUG_ON(PTRS_PER_PGD * sizeof(pgd_t) != PAGE_SIZE);
  565. BUG_ON(PTRS_PER_PMD * sizeof(pmd_t) != PAGE_SIZE);
  566. BUG_ON(PTRS_PER_PTE * sizeof(pte_t) != PAGE_SIZE);
  567. /*
  568. * This needs to be called _after_ the command line has been parsed but
  569. * _before_ any drivers that may need the PCI DMA interface are
  570. * initialized or bootmem has been freed.
  571. */
  572. #ifdef CONFIG_INTEL_IOMMU
  573. detect_intel_iommu();
  574. if (!iommu_detected)
  575. #endif
  576. #ifdef CONFIG_SWIOTLB
  577. swiotlb_init(1);
  578. #endif
  579. #ifdef CONFIG_FLATMEM
  580. BUG_ON(!mem_map);
  581. #endif
  582. set_max_mapnr(max_low_pfn);
  583. high_memory = __va(max_low_pfn * PAGE_SIZE);
  584. memblock_free_all();
  585. mem_init_print_info(NULL);
  586. /*
  587. * For fsyscall entrpoints with no light-weight handler, use the ordinary
  588. * (heavy-weight) handler, but mark it by setting bit 0, so the fsyscall entry
  589. * code can tell them apart.
  590. */
  591. for (i = 0; i < NR_syscalls; ++i) {
  592. extern unsigned long fsyscall_table[NR_syscalls];
  593. extern unsigned long sys_call_table[NR_syscalls];
  594. if (!fsyscall_table[i] || nolwsys)
  595. fsyscall_table[i] = sys_call_table[i] | 1;
  596. }
  597. setup_gate();
  598. }
  599. #ifdef CONFIG_MEMORY_HOTPLUG
  600. int arch_add_memory(int nid, u64 start, u64 size,
  601. struct mhp_params *params)
  602. {
  603. unsigned long start_pfn = start >> PAGE_SHIFT;
  604. unsigned long nr_pages = size >> PAGE_SHIFT;
  605. int ret;
  606. if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
  607. return -EINVAL;
  608. ret = __add_pages(nid, start_pfn, nr_pages, params);
  609. if (ret)
  610. printk("%s: Problem encountered in __add_pages() as ret=%d\n",
  611. __func__, ret);
  612. return ret;
  613. }
  614. void arch_remove_memory(int nid, u64 start, u64 size,
  615. struct vmem_altmap *altmap)
  616. {
  617. unsigned long start_pfn = start >> PAGE_SHIFT;
  618. unsigned long nr_pages = size >> PAGE_SHIFT;
  619. __remove_pages(start_pfn, nr_pages, altmap);
  620. }
  621. #endif