init.c 31 KB

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