init.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation, version 2.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/signal.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/mman.h>
  24. #include <linux/mm.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/swap.h>
  27. #include <linux/smp.h>
  28. #include <linux/init.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/poison.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/slab.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/efi.h>
  36. #include <linux/memory_hotplug.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/processor.h>
  40. #include <asm/system.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/pgalloc.h>
  43. #include <asm/dma.h>
  44. #include <asm/fixmap.h>
  45. #include <asm/tlb.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/sections.h>
  48. #include <asm/setup.h>
  49. #include <asm/homecache.h>
  50. #include <hv/hypervisor.h>
  51. #include <arch/chip.h>
  52. #include "migrate.h"
  53. #define clear_pgd(pmdptr) (*(pmdptr) = hv_pte(0))
  54. #ifndef __tilegx__
  55. unsigned long VMALLOC_RESERVE = CONFIG_VMALLOC_RESERVE;
  56. EXPORT_SYMBOL(VMALLOC_RESERVE);
  57. #endif
  58. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  59. /* Create an L2 page table */
  60. static pte_t * __init alloc_pte(void)
  61. {
  62. return __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0);
  63. }
  64. /*
  65. * L2 page tables per controller. We allocate these all at once from
  66. * the bootmem allocator and store them here. This saves on kernel L2
  67. * page table memory, compared to allocating a full 64K page per L2
  68. * page table, and also means that in cases where we use huge pages,
  69. * we are guaranteed to later be able to shatter those huge pages and
  70. * switch to using these page tables instead, without requiring
  71. * further allocation. Each l2_ptes[] entry points to the first page
  72. * table for the first hugepage-size piece of memory on the
  73. * controller; other page tables are just indexed directly, i.e. the
  74. * L2 page tables are contiguous in memory for each controller.
  75. */
  76. static pte_t *l2_ptes[MAX_NUMNODES];
  77. static int num_l2_ptes[MAX_NUMNODES];
  78. static void init_prealloc_ptes(int node, int pages)
  79. {
  80. BUG_ON(pages & (HV_L2_ENTRIES-1));
  81. if (pages) {
  82. num_l2_ptes[node] = pages;
  83. l2_ptes[node] = __alloc_bootmem(pages * sizeof(pte_t),
  84. HV_PAGE_TABLE_ALIGN, 0);
  85. }
  86. }
  87. pte_t *get_prealloc_pte(unsigned long pfn)
  88. {
  89. int node = pfn_to_nid(pfn);
  90. pfn &= ~(-1UL << (NR_PA_HIGHBIT_SHIFT - PAGE_SHIFT));
  91. BUG_ON(node >= MAX_NUMNODES);
  92. BUG_ON(pfn >= num_l2_ptes[node]);
  93. return &l2_ptes[node][pfn];
  94. }
  95. /*
  96. * What caching do we expect pages from the heap to have when
  97. * they are allocated during bootup? (Once we've installed the
  98. * "real" swapper_pg_dir.)
  99. */
  100. static int initial_heap_home(void)
  101. {
  102. #if CHIP_HAS_CBOX_HOME_MAP()
  103. if (hash_default)
  104. return PAGE_HOME_HASH;
  105. #endif
  106. return smp_processor_id();
  107. }
  108. /*
  109. * Place a pointer to an L2 page table in a middle page
  110. * directory entry.
  111. */
  112. static void __init assign_pte(pmd_t *pmd, pte_t *page_table)
  113. {
  114. phys_addr_t pa = __pa(page_table);
  115. unsigned long l2_ptfn = pa >> HV_LOG2_PAGE_TABLE_ALIGN;
  116. pte_t pteval = hv_pte_set_ptfn(__pgprot(_PAGE_TABLE), l2_ptfn);
  117. BUG_ON((pa & (HV_PAGE_TABLE_ALIGN-1)) != 0);
  118. pteval = pte_set_home(pteval, initial_heap_home());
  119. *(pte_t *)pmd = pteval;
  120. if (page_table != (pte_t *)pmd_page_vaddr(*pmd))
  121. BUG();
  122. }
  123. #ifdef __tilegx__
  124. #if HV_L1_SIZE != HV_L2_SIZE
  125. # error Rework assumption that L1 and L2 page tables are same size.
  126. #endif
  127. /* Since pmd_t arrays and pte_t arrays are the same size, just use casts. */
  128. static inline pmd_t *alloc_pmd(void)
  129. {
  130. return (pmd_t *)alloc_pte();
  131. }
  132. static inline void assign_pmd(pud_t *pud, pmd_t *pmd)
  133. {
  134. assign_pte((pmd_t *)pud, (pte_t *)pmd);
  135. }
  136. #endif /* __tilegx__ */
  137. /* Replace the given pmd with a full PTE table. */
  138. void __init shatter_pmd(pmd_t *pmd)
  139. {
  140. pte_t *pte = get_prealloc_pte(pte_pfn(*(pte_t *)pmd));
  141. assign_pte(pmd, pte);
  142. }
  143. #ifdef CONFIG_HIGHMEM
  144. /*
  145. * This function initializes a certain range of kernel virtual memory
  146. * with new bootmem page tables, everywhere page tables are missing in
  147. * the given range.
  148. */
  149. /*
  150. * NOTE: The pagetables are allocated contiguous on the physical space
  151. * so we can cache the place of the first one and move around without
  152. * checking the pgd every time.
  153. */
  154. static void __init page_table_range_init(unsigned long start,
  155. unsigned long end, pgd_t *pgd_base)
  156. {
  157. pgd_t *pgd;
  158. int pgd_idx;
  159. unsigned long vaddr;
  160. vaddr = start;
  161. pgd_idx = pgd_index(vaddr);
  162. pgd = pgd_base + pgd_idx;
  163. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  164. pmd_t *pmd = pmd_offset(pud_offset(pgd, vaddr), vaddr);
  165. if (pmd_none(*pmd))
  166. assign_pte(pmd, alloc_pte());
  167. vaddr += PMD_SIZE;
  168. }
  169. }
  170. #endif /* CONFIG_HIGHMEM */
  171. #if CHIP_HAS_CBOX_HOME_MAP()
  172. static int __initdata ktext_hash = 1; /* .text pages */
  173. static int __initdata kdata_hash = 1; /* .data and .bss pages */
  174. int __write_once hash_default = 1; /* kernel allocator pages */
  175. EXPORT_SYMBOL(hash_default);
  176. int __write_once kstack_hash = 1; /* if no homecaching, use h4h */
  177. #endif /* CHIP_HAS_CBOX_HOME_MAP */
  178. /*
  179. * CPUs to use to for striping the pages of kernel data. If hash-for-home
  180. * is available, this is only relevant if kcache_hash sets up the
  181. * .data and .bss to be page-homed, and we don't want the default mode
  182. * of using the full set of kernel cpus for the striping.
  183. */
  184. static __initdata struct cpumask kdata_mask;
  185. static __initdata int kdata_arg_seen;
  186. int __write_once kdata_huge; /* if no homecaching, small pages */
  187. /* Combine a generic pgprot_t with cache home to get a cache-aware pgprot. */
  188. static pgprot_t __init construct_pgprot(pgprot_t prot, int home)
  189. {
  190. prot = pte_set_home(prot, home);
  191. #if CHIP_HAS_CBOX_HOME_MAP()
  192. if (home == PAGE_HOME_IMMUTABLE) {
  193. if (ktext_hash)
  194. prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_HASH_L3);
  195. else
  196. prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_NO_L3);
  197. }
  198. #endif
  199. return prot;
  200. }
  201. /*
  202. * For a given kernel data VA, how should it be cached?
  203. * We return the complete pgprot_t with caching bits set.
  204. */
  205. static pgprot_t __init init_pgprot(ulong address)
  206. {
  207. int cpu;
  208. unsigned long page;
  209. enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
  210. #if CHIP_HAS_CBOX_HOME_MAP()
  211. /* For kdata=huge, everything is just hash-for-home. */
  212. if (kdata_huge)
  213. return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
  214. #endif
  215. /* We map the aliased pages of permanent text inaccessible. */
  216. if (address < (ulong) _sinittext - CODE_DELTA)
  217. return PAGE_NONE;
  218. /*
  219. * We map read-only data non-coherent for performance. We could
  220. * use neighborhood caching on TILE64, but it's not clear it's a win.
  221. */
  222. if ((address >= (ulong) __start_rodata &&
  223. address < (ulong) __end_rodata) ||
  224. address == (ulong) empty_zero_page) {
  225. return construct_pgprot(PAGE_KERNEL_RO, PAGE_HOME_IMMUTABLE);
  226. }
  227. /* As a performance optimization, keep the boot init stack here. */
  228. if (address >= (ulong)&init_thread_union &&
  229. address < (ulong)&init_thread_union + THREAD_SIZE)
  230. return construct_pgprot(PAGE_KERNEL, smp_processor_id());
  231. #ifndef __tilegx__
  232. #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
  233. /* Force the atomic_locks[] array page to be hash-for-home. */
  234. if (address == (ulong) atomic_locks)
  235. return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
  236. #endif
  237. #endif
  238. /*
  239. * Everything else that isn't data or bss is heap, so mark it
  240. * with the initial heap home (hash-for-home, or this cpu). This
  241. * includes any addresses after the loaded image and any address before
  242. * _einitdata, since we already captured the case of text before
  243. * _sinittext, and __pa(einittext) is approximately __pa(sinitdata).
  244. *
  245. * All the LOWMEM pages that we mark this way will get their
  246. * struct page homecache properly marked later, in set_page_homes().
  247. * The HIGHMEM pages we leave with a default zero for their
  248. * homes, but with a zero free_time we don't have to actually
  249. * do a flush action the first time we use them, either.
  250. */
  251. if (address >= (ulong) _end || address < (ulong) _einitdata)
  252. return construct_pgprot(PAGE_KERNEL, initial_heap_home());
  253. #if CHIP_HAS_CBOX_HOME_MAP()
  254. /* Use hash-for-home if requested for data/bss. */
  255. if (kdata_hash)
  256. return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
  257. #endif
  258. /*
  259. * Make the w1data homed like heap to start with, to avoid
  260. * making it part of the page-striped data area when we're just
  261. * going to convert it to read-only soon anyway.
  262. */
  263. if (address >= (ulong)__w1data_begin && address < (ulong)__w1data_end)
  264. return construct_pgprot(PAGE_KERNEL, initial_heap_home());
  265. /*
  266. * Otherwise we just hand out consecutive cpus. To avoid
  267. * requiring this function to hold state, we just walk forward from
  268. * _sdata by PAGE_SIZE, skipping the readonly and init data, to reach
  269. * the requested address, while walking cpu home around kdata_mask.
  270. * This is typically no more than a dozen or so iterations.
  271. */
  272. page = (((ulong)__w1data_end) + PAGE_SIZE - 1) & PAGE_MASK;
  273. BUG_ON(address < page || address >= (ulong)_end);
  274. cpu = cpumask_first(&kdata_mask);
  275. for (; page < address; page += PAGE_SIZE) {
  276. if (page >= (ulong)&init_thread_union &&
  277. page < (ulong)&init_thread_union + THREAD_SIZE)
  278. continue;
  279. if (page == (ulong)empty_zero_page)
  280. continue;
  281. #ifndef __tilegx__
  282. #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
  283. if (page == (ulong)atomic_locks)
  284. continue;
  285. #endif
  286. #endif
  287. cpu = cpumask_next(cpu, &kdata_mask);
  288. if (cpu == NR_CPUS)
  289. cpu = cpumask_first(&kdata_mask);
  290. }
  291. return construct_pgprot(PAGE_KERNEL, cpu);
  292. }
  293. /*
  294. * This function sets up how we cache the kernel text. If we have
  295. * hash-for-home support, normally that is used instead (see the
  296. * kcache_hash boot flag for more information). But if we end up
  297. * using a page-based caching technique, this option sets up the
  298. * details of that. In addition, the "ktext=nocache" option may
  299. * always be used to disable local caching of text pages, if desired.
  300. */
  301. static int __initdata ktext_arg_seen;
  302. static int __initdata ktext_small;
  303. static int __initdata ktext_local;
  304. static int __initdata ktext_all;
  305. static int __initdata ktext_nondataplane;
  306. static int __initdata ktext_nocache;
  307. static struct cpumask __initdata ktext_mask;
  308. static int __init setup_ktext(char *str)
  309. {
  310. if (str == NULL)
  311. return -EINVAL;
  312. /* If you have a leading "nocache", turn off ktext caching */
  313. if (strncmp(str, "nocache", 7) == 0) {
  314. ktext_nocache = 1;
  315. pr_info("ktext: disabling local caching of kernel text\n");
  316. str += 7;
  317. if (*str == ',')
  318. ++str;
  319. if (*str == '\0')
  320. return 0;
  321. }
  322. ktext_arg_seen = 1;
  323. /* Default setting on Tile64: use a huge page */
  324. if (strcmp(str, "huge") == 0)
  325. pr_info("ktext: using one huge locally cached page\n");
  326. /* Pay TLB cost but get no cache benefit: cache small pages locally */
  327. else if (strcmp(str, "local") == 0) {
  328. ktext_small = 1;
  329. ktext_local = 1;
  330. pr_info("ktext: using small pages with local caching\n");
  331. }
  332. /* Neighborhood cache ktext pages on all cpus. */
  333. else if (strcmp(str, "all") == 0) {
  334. ktext_small = 1;
  335. ktext_all = 1;
  336. pr_info("ktext: using maximal caching neighborhood\n");
  337. }
  338. /* Neighborhood ktext pages on specified mask */
  339. else if (cpulist_parse(str, &ktext_mask) == 0) {
  340. char buf[NR_CPUS * 5];
  341. cpulist_scnprintf(buf, sizeof(buf), &ktext_mask);
  342. if (cpumask_weight(&ktext_mask) > 1) {
  343. ktext_small = 1;
  344. pr_info("ktext: using caching neighborhood %s "
  345. "with small pages\n", buf);
  346. } else {
  347. pr_info("ktext: caching on cpu %s with one huge page\n",
  348. buf);
  349. }
  350. }
  351. else if (*str)
  352. return -EINVAL;
  353. return 0;
  354. }
  355. early_param("ktext", setup_ktext);
  356. static inline pgprot_t ktext_set_nocache(pgprot_t prot)
  357. {
  358. if (!ktext_nocache)
  359. prot = hv_pte_set_nc(prot);
  360. #if CHIP_HAS_NC_AND_NOALLOC_BITS()
  361. else
  362. prot = hv_pte_set_no_alloc_l2(prot);
  363. #endif
  364. return prot;
  365. }
  366. #ifndef __tilegx__
  367. static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
  368. {
  369. return pmd_offset(pud_offset(&pgtables[pgd_index(va)], va), va);
  370. }
  371. #else
  372. static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
  373. {
  374. pud_t *pud = pud_offset(&pgtables[pgd_index(va)], va);
  375. if (pud_none(*pud))
  376. assign_pmd(pud, alloc_pmd());
  377. return pmd_offset(pud, va);
  378. }
  379. #endif
  380. /* Temporary page table we use for staging. */
  381. static pgd_t pgtables[PTRS_PER_PGD]
  382. __attribute__((aligned(HV_PAGE_TABLE_ALIGN)));
  383. /*
  384. * This maps the physical memory to kernel virtual address space, a total
  385. * of max_low_pfn pages, by creating page tables starting from address
  386. * PAGE_OFFSET.
  387. *
  388. * This routine transitions us from using a set of compiled-in large
  389. * pages to using some more precise caching, including removing access
  390. * to code pages mapped at PAGE_OFFSET (executed only at MEM_SV_START)
  391. * marking read-only data as locally cacheable, striping the remaining
  392. * .data and .bss across all the available tiles, and removing access
  393. * to pages above the top of RAM (thus ensuring a page fault from a bad
  394. * virtual address rather than a hypervisor shoot down for accessing
  395. * memory outside the assigned limits).
  396. */
  397. static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
  398. {
  399. unsigned long address, pfn;
  400. pmd_t *pmd;
  401. pte_t *pte;
  402. int pte_ofs;
  403. const struct cpumask *my_cpu_mask = cpumask_of(smp_processor_id());
  404. struct cpumask kstripe_mask;
  405. int rc, i;
  406. #if CHIP_HAS_CBOX_HOME_MAP()
  407. if (ktext_arg_seen && ktext_hash) {
  408. pr_warning("warning: \"ktext\" boot argument ignored"
  409. " if \"kcache_hash\" sets up text hash-for-home\n");
  410. ktext_small = 0;
  411. }
  412. if (kdata_arg_seen && kdata_hash) {
  413. pr_warning("warning: \"kdata\" boot argument ignored"
  414. " if \"kcache_hash\" sets up data hash-for-home\n");
  415. }
  416. if (kdata_huge && !hash_default) {
  417. pr_warning("warning: disabling \"kdata=huge\"; requires"
  418. " kcache_hash=all or =allbutstack\n");
  419. kdata_huge = 0;
  420. }
  421. #endif
  422. /*
  423. * Set up a mask for cpus to use for kernel striping.
  424. * This is normally all cpus, but minus dataplane cpus if any.
  425. * If the dataplane covers the whole chip, we stripe over
  426. * the whole chip too.
  427. */
  428. cpumask_copy(&kstripe_mask, cpu_possible_mask);
  429. if (!kdata_arg_seen)
  430. kdata_mask = kstripe_mask;
  431. /* Allocate and fill in L2 page tables */
  432. for (i = 0; i < MAX_NUMNODES; ++i) {
  433. #ifdef CONFIG_HIGHMEM
  434. unsigned long end_pfn = node_lowmem_end_pfn[i];
  435. #else
  436. unsigned long end_pfn = node_end_pfn[i];
  437. #endif
  438. unsigned long end_huge_pfn = 0;
  439. /* Pre-shatter the last huge page to allow per-cpu pages. */
  440. if (kdata_huge)
  441. end_huge_pfn = end_pfn - (HPAGE_SIZE >> PAGE_SHIFT);
  442. pfn = node_start_pfn[i];
  443. /* Allocate enough memory to hold L2 page tables for node. */
  444. init_prealloc_ptes(i, end_pfn - pfn);
  445. address = (unsigned long) pfn_to_kaddr(pfn);
  446. while (pfn < end_pfn) {
  447. BUG_ON(address & (HPAGE_SIZE-1));
  448. pmd = get_pmd(pgtables, address);
  449. pte = get_prealloc_pte(pfn);
  450. if (pfn < end_huge_pfn) {
  451. pgprot_t prot = init_pgprot(address);
  452. *(pte_t *)pmd = pte_mkhuge(pfn_pte(pfn, prot));
  453. for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
  454. pfn++, pte_ofs++, address += PAGE_SIZE)
  455. pte[pte_ofs] = pfn_pte(pfn, prot);
  456. } else {
  457. if (kdata_huge)
  458. printk(KERN_DEBUG "pre-shattered huge"
  459. " page at %#lx\n", address);
  460. for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
  461. pfn++, pte_ofs++, address += PAGE_SIZE) {
  462. pgprot_t prot = init_pgprot(address);
  463. pte[pte_ofs] = pfn_pte(pfn, prot);
  464. }
  465. assign_pte(pmd, pte);
  466. }
  467. }
  468. }
  469. /*
  470. * Set or check ktext_map now that we have cpu_possible_mask
  471. * and kstripe_mask to work with.
  472. */
  473. if (ktext_all)
  474. cpumask_copy(&ktext_mask, cpu_possible_mask);
  475. else if (ktext_nondataplane)
  476. ktext_mask = kstripe_mask;
  477. else if (!cpumask_empty(&ktext_mask)) {
  478. /* Sanity-check any mask that was requested */
  479. struct cpumask bad;
  480. cpumask_andnot(&bad, &ktext_mask, cpu_possible_mask);
  481. cpumask_and(&ktext_mask, &ktext_mask, cpu_possible_mask);
  482. if (!cpumask_empty(&bad)) {
  483. char buf[NR_CPUS * 5];
  484. cpulist_scnprintf(buf, sizeof(buf), &bad);
  485. pr_info("ktext: not using unavailable cpus %s\n", buf);
  486. }
  487. if (cpumask_empty(&ktext_mask)) {
  488. pr_warning("ktext: no valid cpus; caching on %d.\n",
  489. smp_processor_id());
  490. cpumask_copy(&ktext_mask,
  491. cpumask_of(smp_processor_id()));
  492. }
  493. }
  494. address = MEM_SV_INTRPT;
  495. pmd = get_pmd(pgtables, address);
  496. if (ktext_small) {
  497. /* Allocate an L2 PTE for the kernel text */
  498. int cpu = 0;
  499. pgprot_t prot = construct_pgprot(PAGE_KERNEL_EXEC,
  500. PAGE_HOME_IMMUTABLE);
  501. if (ktext_local) {
  502. if (ktext_nocache)
  503. prot = hv_pte_set_mode(prot,
  504. HV_PTE_MODE_UNCACHED);
  505. else
  506. prot = hv_pte_set_mode(prot,
  507. HV_PTE_MODE_CACHE_NO_L3);
  508. } else {
  509. prot = hv_pte_set_mode(prot,
  510. HV_PTE_MODE_CACHE_TILE_L3);
  511. cpu = cpumask_first(&ktext_mask);
  512. prot = ktext_set_nocache(prot);
  513. }
  514. BUG_ON(address != (unsigned long)_stext);
  515. pfn = 0; /* code starts at PA 0 */
  516. pte = alloc_pte();
  517. for (pte_ofs = 0; address < (unsigned long)_einittext;
  518. pfn++, pte_ofs++, address += PAGE_SIZE) {
  519. if (!ktext_local) {
  520. prot = set_remote_cache_cpu(prot, cpu);
  521. cpu = cpumask_next(cpu, &ktext_mask);
  522. if (cpu == NR_CPUS)
  523. cpu = cpumask_first(&ktext_mask);
  524. }
  525. pte[pte_ofs] = pfn_pte(pfn, prot);
  526. }
  527. assign_pte(pmd, pte);
  528. } else {
  529. pte_t pteval = pfn_pte(0, PAGE_KERNEL_EXEC);
  530. pteval = pte_mkhuge(pteval);
  531. #if CHIP_HAS_CBOX_HOME_MAP()
  532. if (ktext_hash) {
  533. pteval = hv_pte_set_mode(pteval,
  534. HV_PTE_MODE_CACHE_HASH_L3);
  535. pteval = ktext_set_nocache(pteval);
  536. } else
  537. #endif /* CHIP_HAS_CBOX_HOME_MAP() */
  538. if (cpumask_weight(&ktext_mask) == 1) {
  539. pteval = set_remote_cache_cpu(pteval,
  540. cpumask_first(&ktext_mask));
  541. pteval = hv_pte_set_mode(pteval,
  542. HV_PTE_MODE_CACHE_TILE_L3);
  543. pteval = ktext_set_nocache(pteval);
  544. } else if (ktext_nocache)
  545. pteval = hv_pte_set_mode(pteval,
  546. HV_PTE_MODE_UNCACHED);
  547. else
  548. pteval = hv_pte_set_mode(pteval,
  549. HV_PTE_MODE_CACHE_NO_L3);
  550. *(pte_t *)pmd = pteval;
  551. }
  552. /* Set swapper_pgprot here so it is flushed to memory right away. */
  553. swapper_pgprot = init_pgprot((unsigned long)swapper_pg_dir);
  554. /*
  555. * Since we may be changing the caching of the stack and page
  556. * table itself, we invoke an assembly helper to do the
  557. * following steps:
  558. *
  559. * - flush the cache so we start with an empty slate
  560. * - install pgtables[] as the real page table
  561. * - flush the TLB so the new page table takes effect
  562. */
  563. rc = flush_and_install_context(__pa(pgtables),
  564. init_pgprot((unsigned long)pgtables),
  565. __get_cpu_var(current_asid),
  566. cpumask_bits(my_cpu_mask));
  567. BUG_ON(rc != 0);
  568. /* Copy the page table back to the normal swapper_pg_dir. */
  569. memcpy(pgd_base, pgtables, sizeof(pgtables));
  570. __install_page_table(pgd_base, __get_cpu_var(current_asid),
  571. swapper_pgprot);
  572. /*
  573. * We just read swapper_pgprot and thus brought it into the cache,
  574. * with its new home & caching mode. When we start the other CPUs,
  575. * they're going to reference swapper_pgprot via their initial fake
  576. * VA-is-PA mappings, which cache everything locally. At that
  577. * time, if it's in our cache with a conflicting home, the
  578. * simulator's coherence checker will complain. So, flush it out
  579. * of our cache; we're not going to ever use it again anyway.
  580. */
  581. __insn_finv(&swapper_pgprot);
  582. }
  583. /*
  584. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  585. * is valid. The argument is a physical page number.
  586. *
  587. * On Tile, the only valid things for which we can just hand out unchecked
  588. * PTEs are the kernel code and data. Anything else might change its
  589. * homing with time, and we wouldn't know to adjust the /dev/mem PTEs.
  590. * Note that init_thread_union is released to heap soon after boot,
  591. * so we include it in the init data.
  592. *
  593. * For TILE-Gx, we might want to consider allowing access to PA
  594. * regions corresponding to PCI space, etc.
  595. */
  596. int devmem_is_allowed(unsigned long pagenr)
  597. {
  598. return pagenr < kaddr_to_pfn(_end) &&
  599. !(pagenr >= kaddr_to_pfn(&init_thread_union) ||
  600. pagenr < kaddr_to_pfn(_einitdata)) &&
  601. !(pagenr >= kaddr_to_pfn(_sinittext) ||
  602. pagenr <= kaddr_to_pfn(_einittext-1));
  603. }
  604. #ifdef CONFIG_HIGHMEM
  605. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  606. {
  607. pgd_t *pgd;
  608. pud_t *pud;
  609. pmd_t *pmd;
  610. pte_t *pte;
  611. unsigned long vaddr;
  612. vaddr = PKMAP_BASE;
  613. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  614. pgd = swapper_pg_dir + pgd_index(vaddr);
  615. pud = pud_offset(pgd, vaddr);
  616. pmd = pmd_offset(pud, vaddr);
  617. pte = pte_offset_kernel(pmd, vaddr);
  618. pkmap_page_table = pte;
  619. }
  620. #endif /* CONFIG_HIGHMEM */
  621. static void __init init_free_pfn_range(unsigned long start, unsigned long end)
  622. {
  623. unsigned long pfn;
  624. struct page *page = pfn_to_page(start);
  625. for (pfn = start; pfn < end; ) {
  626. /* Optimize by freeing pages in large batches */
  627. int order = __ffs(pfn);
  628. int count, i;
  629. struct page *p;
  630. if (order >= MAX_ORDER)
  631. order = MAX_ORDER-1;
  632. count = 1 << order;
  633. while (pfn + count > end) {
  634. count >>= 1;
  635. --order;
  636. }
  637. for (p = page, i = 0; i < count; ++i, ++p) {
  638. __ClearPageReserved(p);
  639. /*
  640. * Hacky direct set to avoid unnecessary
  641. * lock take/release for EVERY page here.
  642. */
  643. p->_count.counter = 0;
  644. p->_mapcount.counter = -1;
  645. }
  646. init_page_count(page);
  647. __free_pages(page, order);
  648. totalram_pages += count;
  649. page += count;
  650. pfn += count;
  651. }
  652. }
  653. static void __init set_non_bootmem_pages_init(void)
  654. {
  655. struct zone *z;
  656. for_each_zone(z) {
  657. unsigned long start, end;
  658. int nid = z->zone_pgdat->node_id;
  659. int idx = zone_idx(z);
  660. start = z->zone_start_pfn;
  661. if (start == 0)
  662. continue; /* bootmem */
  663. end = start + z->spanned_pages;
  664. if (idx == ZONE_NORMAL) {
  665. BUG_ON(start != node_start_pfn[nid]);
  666. start = node_free_pfn[nid];
  667. }
  668. #ifdef CONFIG_HIGHMEM
  669. if (idx == ZONE_HIGHMEM)
  670. totalhigh_pages += z->spanned_pages;
  671. #endif
  672. if (kdata_huge) {
  673. unsigned long percpu_pfn = node_percpu_pfn[nid];
  674. if (start < percpu_pfn && end > percpu_pfn)
  675. end = percpu_pfn;
  676. }
  677. #ifdef CONFIG_PCI
  678. if (start <= pci_reserve_start_pfn &&
  679. end > pci_reserve_start_pfn) {
  680. if (end > pci_reserve_end_pfn)
  681. init_free_pfn_range(pci_reserve_end_pfn, end);
  682. end = pci_reserve_start_pfn;
  683. }
  684. #endif
  685. init_free_pfn_range(start, end);
  686. }
  687. }
  688. /*
  689. * paging_init() sets up the page tables - note that all of lowmem is
  690. * already mapped by head.S.
  691. */
  692. void __init paging_init(void)
  693. {
  694. #ifdef CONFIG_HIGHMEM
  695. unsigned long vaddr, end;
  696. #endif
  697. #ifdef __tilegx__
  698. pud_t *pud;
  699. #endif
  700. pgd_t *pgd_base = swapper_pg_dir;
  701. kernel_physical_mapping_init(pgd_base);
  702. #ifdef CONFIG_HIGHMEM
  703. /*
  704. * Fixed mappings, only the page table structure has to be
  705. * created - mappings will be set by set_fixmap():
  706. */
  707. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  708. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  709. page_table_range_init(vaddr, end, pgd_base);
  710. permanent_kmaps_init(pgd_base);
  711. #endif
  712. #ifdef __tilegx__
  713. /*
  714. * Since GX allocates just one pmd_t array worth of vmalloc space,
  715. * we go ahead and allocate it statically here, then share it
  716. * globally. As a result we don't have to worry about any task
  717. * changing init_mm once we get up and running, and there's no
  718. * need for e.g. vmalloc_sync_all().
  719. */
  720. BUILD_BUG_ON(pgd_index(VMALLOC_START) != pgd_index(VMALLOC_END));
  721. pud = pud_offset(pgd_base + pgd_index(VMALLOC_START), VMALLOC_START);
  722. assign_pmd(pud, alloc_pmd());
  723. #endif
  724. }
  725. /*
  726. * Walk the kernel page tables and derive the page_home() from
  727. * the PTEs, so that set_pte() can properly validate the caching
  728. * of all PTEs it sees.
  729. */
  730. void __init set_page_homes(void)
  731. {
  732. }
  733. static void __init set_max_mapnr_init(void)
  734. {
  735. #ifdef CONFIG_FLATMEM
  736. max_mapnr = max_low_pfn;
  737. #endif
  738. }
  739. void __init mem_init(void)
  740. {
  741. int codesize, datasize, initsize;
  742. int i;
  743. #ifndef __tilegx__
  744. void *last;
  745. #endif
  746. #ifdef CONFIG_FLATMEM
  747. if (!mem_map)
  748. BUG();
  749. #endif
  750. #ifdef CONFIG_HIGHMEM
  751. /* check that fixmap and pkmap do not overlap */
  752. if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) {
  753. pr_err("fixmap and kmap areas overlap"
  754. " - this will crash\n");
  755. pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n",
  756. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1),
  757. FIXADDR_START);
  758. BUG();
  759. }
  760. #endif
  761. set_max_mapnr_init();
  762. /* this will put all bootmem onto the freelists */
  763. totalram_pages += free_all_bootmem();
  764. /* count all remaining LOWMEM and give all HIGHMEM to page allocator */
  765. set_non_bootmem_pages_init();
  766. codesize = (unsigned long)&_etext - (unsigned long)&_text;
  767. datasize = (unsigned long)&_end - (unsigned long)&_sdata;
  768. initsize = (unsigned long)&_einittext - (unsigned long)&_sinittext;
  769. initsize += (unsigned long)&_einitdata - (unsigned long)&_sinitdata;
  770. pr_info("Memory: %luk/%luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
  771. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  772. num_physpages << (PAGE_SHIFT-10),
  773. codesize >> 10,
  774. datasize >> 10,
  775. initsize >> 10,
  776. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  777. );
  778. /*
  779. * In debug mode, dump some interesting memory mappings.
  780. */
  781. #ifdef CONFIG_HIGHMEM
  782. printk(KERN_DEBUG " KMAP %#lx - %#lx\n",
  783. FIXADDR_START, FIXADDR_TOP + PAGE_SIZE - 1);
  784. printk(KERN_DEBUG " PKMAP %#lx - %#lx\n",
  785. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP) - 1);
  786. #endif
  787. #ifdef CONFIG_HUGEVMAP
  788. printk(KERN_DEBUG " HUGEMAP %#lx - %#lx\n",
  789. HUGE_VMAP_BASE, HUGE_VMAP_END - 1);
  790. #endif
  791. printk(KERN_DEBUG " VMALLOC %#lx - %#lx\n",
  792. _VMALLOC_START, _VMALLOC_END - 1);
  793. #ifdef __tilegx__
  794. for (i = MAX_NUMNODES-1; i >= 0; --i) {
  795. struct pglist_data *node = &node_data[i];
  796. if (node->node_present_pages) {
  797. unsigned long start = (unsigned long)
  798. pfn_to_kaddr(node->node_start_pfn);
  799. unsigned long end = start +
  800. (node->node_present_pages << PAGE_SHIFT);
  801. printk(KERN_DEBUG " MEM%d %#lx - %#lx\n",
  802. i, start, end - 1);
  803. }
  804. }
  805. #else
  806. last = high_memory;
  807. for (i = MAX_NUMNODES-1; i >= 0; --i) {
  808. if ((unsigned long)vbase_map[i] != -1UL) {
  809. printk(KERN_DEBUG " LOWMEM%d %#lx - %#lx\n",
  810. i, (unsigned long) (vbase_map[i]),
  811. (unsigned long) (last-1));
  812. last = vbase_map[i];
  813. }
  814. }
  815. #endif
  816. #ifndef __tilegx__
  817. /*
  818. * Convert from using one lock for all atomic operations to
  819. * one per cpu.
  820. */
  821. __init_atomic_per_cpu();
  822. #endif
  823. }
  824. /*
  825. * this is for the non-NUMA, single node SMP system case.
  826. * Specifically, in the case of x86, we will always add
  827. * memory to the highmem for now.
  828. */
  829. #ifndef CONFIG_NEED_MULTIPLE_NODES
  830. int arch_add_memory(u64 start, u64 size)
  831. {
  832. struct pglist_data *pgdata = &contig_page_data;
  833. struct zone *zone = pgdata->node_zones + MAX_NR_ZONES-1;
  834. unsigned long start_pfn = start >> PAGE_SHIFT;
  835. unsigned long nr_pages = size >> PAGE_SHIFT;
  836. return __add_pages(zone, start_pfn, nr_pages);
  837. }
  838. int remove_memory(u64 start, u64 size)
  839. {
  840. return -EINVAL;
  841. }
  842. #endif
  843. struct kmem_cache *pgd_cache;
  844. void __init pgtable_cache_init(void)
  845. {
  846. pgd_cache = kmem_cache_create("pgd", SIZEOF_PGD, SIZEOF_PGD, 0, NULL);
  847. if (!pgd_cache)
  848. panic("pgtable_cache_init(): Cannot create pgd cache");
  849. }
  850. #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
  851. /*
  852. * The __w1data area holds data that is only written during initialization,
  853. * and is read-only and thus freely cacheable thereafter. Fix the page
  854. * table entries that cover that region accordingly.
  855. */
  856. static void mark_w1data_ro(void)
  857. {
  858. /* Loop over page table entries */
  859. unsigned long addr = (unsigned long)__w1data_begin;
  860. BUG_ON((addr & (PAGE_SIZE-1)) != 0);
  861. for (; addr <= (unsigned long)__w1data_end - 1; addr += PAGE_SIZE) {
  862. unsigned long pfn = kaddr_to_pfn((void *)addr);
  863. pte_t *ptep = virt_to_pte(NULL, addr);
  864. BUG_ON(pte_huge(*ptep)); /* not relevant for kdata_huge */
  865. set_pte_at(&init_mm, addr, ptep, pfn_pte(pfn, PAGE_KERNEL_RO));
  866. }
  867. }
  868. #endif
  869. #ifdef CONFIG_DEBUG_PAGEALLOC
  870. static long __write_once initfree;
  871. #else
  872. static long __write_once initfree = 1;
  873. #endif
  874. /* Select whether to free (1) or mark unusable (0) the __init pages. */
  875. static int __init set_initfree(char *str)
  876. {
  877. long val;
  878. if (strict_strtol(str, 0, &val) == 0) {
  879. initfree = val;
  880. pr_info("initfree: %s free init pages\n",
  881. initfree ? "will" : "won't");
  882. }
  883. return 1;
  884. }
  885. __setup("initfree=", set_initfree);
  886. static void free_init_pages(char *what, unsigned long begin, unsigned long end)
  887. {
  888. unsigned long addr = (unsigned long) begin;
  889. if (kdata_huge && !initfree) {
  890. pr_warning("Warning: ignoring initfree=0:"
  891. " incompatible with kdata=huge\n");
  892. initfree = 1;
  893. }
  894. end = (end + PAGE_SIZE - 1) & PAGE_MASK;
  895. local_flush_tlb_pages(NULL, begin, PAGE_SIZE, end - begin);
  896. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  897. /*
  898. * Note we just reset the home here directly in the
  899. * page table. We know this is safe because our caller
  900. * just flushed the caches on all the other cpus,
  901. * and they won't be touching any of these pages.
  902. */
  903. int pfn = kaddr_to_pfn((void *)addr);
  904. struct page *page = pfn_to_page(pfn);
  905. pte_t *ptep = virt_to_pte(NULL, addr);
  906. if (!initfree) {
  907. /*
  908. * If debugging page accesses then do not free
  909. * this memory but mark them not present - any
  910. * buggy init-section access will create a
  911. * kernel page fault:
  912. */
  913. pte_clear(&init_mm, addr, ptep);
  914. continue;
  915. }
  916. __ClearPageReserved(page);
  917. init_page_count(page);
  918. if (pte_huge(*ptep))
  919. BUG_ON(!kdata_huge);
  920. else
  921. set_pte_at(&init_mm, addr, ptep,
  922. pfn_pte(pfn, PAGE_KERNEL));
  923. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  924. free_page(addr);
  925. totalram_pages++;
  926. }
  927. pr_info("Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
  928. }
  929. void free_initmem(void)
  930. {
  931. const unsigned long text_delta = MEM_SV_INTRPT - PAGE_OFFSET;
  932. /*
  933. * Evict the dirty initdata on the boot cpu, evict the w1data
  934. * wherever it's homed, and evict all the init code everywhere.
  935. * We are guaranteed that no one will touch the init pages any
  936. * more, and although other cpus may be touching the w1data,
  937. * we only actually change the caching on tile64, which won't
  938. * be keeping local copies in the other tiles' caches anyway.
  939. */
  940. homecache_evict(&cpu_cacheable_map);
  941. /* Free the data pages that we won't use again after init. */
  942. free_init_pages("unused kernel data",
  943. (unsigned long)_sinitdata,
  944. (unsigned long)_einitdata);
  945. /*
  946. * Free the pages mapped from 0xc0000000 that correspond to code
  947. * pages from MEM_SV_INTRPT that we won't use again after init.
  948. */
  949. free_init_pages("unused kernel text",
  950. (unsigned long)_sinittext - text_delta,
  951. (unsigned long)_einittext - text_delta);
  952. #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
  953. /*
  954. * Upgrade the .w1data section to globally cached.
  955. * We don't do this on tilepro, since the cache architecture
  956. * pretty much makes it irrelevant, and in any case we end
  957. * up having racing issues with other tiles that may touch
  958. * the data after we flush the cache but before we update
  959. * the PTEs and flush the TLBs, causing sharer shootdowns
  960. * later. Even though this is to clean data, it seems like
  961. * an unnecessary complication.
  962. */
  963. mark_w1data_ro();
  964. #endif
  965. /* Do a global TLB flush so everyone sees the changes. */
  966. flush_tlb_all();
  967. }