init.c 31 KB

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