init.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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 & (HV_L2_ENTRIES-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. #if HV_L1_SIZE != HV_L2_SIZE
  123. # error Rework assumption that L1 and L2 page tables are same size.
  124. #endif
  125. /* Since pmd_t arrays and pte_t arrays are the same size, just use casts. */
  126. static inline pmd_t *alloc_pmd(void)
  127. {
  128. return (pmd_t *)alloc_pte();
  129. }
  130. static inline void assign_pmd(pud_t *pud, pmd_t *pmd)
  131. {
  132. assign_pte((pmd_t *)pud, (pte_t *)pmd);
  133. }
  134. #endif /* __tilegx__ */
  135. /* Replace the given pmd with a full PTE table. */
  136. void __init shatter_pmd(pmd_t *pmd)
  137. {
  138. pte_t *pte = get_prealloc_pte(pte_pfn(*(pte_t *)pmd));
  139. assign_pte(pmd, pte);
  140. }
  141. #ifdef CONFIG_HIGHMEM
  142. /*
  143. * This function initializes a certain range of kernel virtual memory
  144. * with new bootmem page tables, everywhere page tables are missing in
  145. * the given range.
  146. */
  147. /*
  148. * NOTE: The pagetables are allocated contiguous on the physical space
  149. * so we can cache the place of the first one and move around without
  150. * checking the pgd every time.
  151. */
  152. static void __init page_table_range_init(unsigned long start,
  153. unsigned long end, pgd_t *pgd_base)
  154. {
  155. pgd_t *pgd;
  156. int pgd_idx;
  157. unsigned long vaddr;
  158. vaddr = start;
  159. pgd_idx = pgd_index(vaddr);
  160. pgd = pgd_base + pgd_idx;
  161. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  162. pmd_t *pmd = pmd_offset(pud_offset(pgd, vaddr), vaddr);
  163. if (pmd_none(*pmd))
  164. assign_pte(pmd, alloc_pte());
  165. vaddr += PMD_SIZE;
  166. }
  167. }
  168. #endif /* CONFIG_HIGHMEM */
  169. #if CHIP_HAS_CBOX_HOME_MAP()
  170. static int __initdata ktext_hash = 1; /* .text pages */
  171. static int __initdata kdata_hash = 1; /* .data and .bss pages */
  172. int __write_once hash_default = 1; /* kernel allocator pages */
  173. EXPORT_SYMBOL(hash_default);
  174. int __write_once kstack_hash = 1; /* if no homecaching, use h4h */
  175. #endif /* CHIP_HAS_CBOX_HOME_MAP */
  176. /*
  177. * CPUs to use to for striping the pages of kernel data. If hash-for-home
  178. * is available, this is only relevant if kcache_hash sets up the
  179. * .data and .bss to be page-homed, and we don't want the default mode
  180. * of using the full set of kernel cpus for the striping.
  181. */
  182. static __initdata struct cpumask kdata_mask;
  183. static __initdata int kdata_arg_seen;
  184. int __write_once kdata_huge; /* if no homecaching, small pages */
  185. /* Combine a generic pgprot_t with cache home to get a cache-aware pgprot. */
  186. static pgprot_t __init construct_pgprot(pgprot_t prot, int home)
  187. {
  188. prot = pte_set_home(prot, home);
  189. #if CHIP_HAS_CBOX_HOME_MAP()
  190. if (home == PAGE_HOME_IMMUTABLE) {
  191. if (ktext_hash)
  192. prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_HASH_L3);
  193. else
  194. prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_NO_L3);
  195. }
  196. #endif
  197. return prot;
  198. }
  199. /*
  200. * For a given kernel data VA, how should it be cached?
  201. * We return the complete pgprot_t with caching bits set.
  202. */
  203. static pgprot_t __init init_pgprot(ulong address)
  204. {
  205. int cpu;
  206. unsigned long page;
  207. enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
  208. #if CHIP_HAS_CBOX_HOME_MAP()
  209. /* For kdata=huge, everything is just hash-for-home. */
  210. if (kdata_huge)
  211. return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
  212. #endif
  213. /* We map the aliased pages of permanent text inaccessible. */
  214. if (address < (ulong) _sinittext - CODE_DELTA)
  215. return PAGE_NONE;
  216. /*
  217. * We map read-only data non-coherent for performance. We could
  218. * use neighborhood caching on TILE64, but it's not clear it's a win.
  219. */
  220. if ((address >= (ulong) __start_rodata &&
  221. address < (ulong) __end_rodata) ||
  222. address == (ulong) empty_zero_page) {
  223. return construct_pgprot(PAGE_KERNEL_RO, PAGE_HOME_IMMUTABLE);
  224. }
  225. #ifndef __tilegx__
  226. #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
  227. /* Force the atomic_locks[] array page to be hash-for-home. */
  228. if (address == (ulong) atomic_locks)
  229. return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
  230. #endif
  231. #endif
  232. /*
  233. * Everything else that isn't data or bss is heap, so mark it
  234. * with the initial heap home (hash-for-home, or this cpu). This
  235. * includes any addresses after the loaded image and any address before
  236. * _einitdata, since we already captured the case of text before
  237. * _sinittext, and __pa(einittext) is approximately __pa(sinitdata).
  238. *
  239. * All the LOWMEM pages that we mark this way will get their
  240. * struct page homecache properly marked later, in set_page_homes().
  241. * The HIGHMEM pages we leave with a default zero for their
  242. * homes, but with a zero free_time we don't have to actually
  243. * do a flush action the first time we use them, either.
  244. */
  245. if (address >= (ulong) _end || address < (ulong) _einitdata)
  246. return construct_pgprot(PAGE_KERNEL, initial_heap_home());
  247. #if CHIP_HAS_CBOX_HOME_MAP()
  248. /* Use hash-for-home if requested for data/bss. */
  249. if (kdata_hash)
  250. return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
  251. #endif
  252. /*
  253. * Make the w1data homed like heap to start with, to avoid
  254. * making it part of the page-striped data area when we're just
  255. * going to convert it to read-only soon anyway.
  256. */
  257. if (address >= (ulong)__w1data_begin && address < (ulong)__w1data_end)
  258. return construct_pgprot(PAGE_KERNEL, initial_heap_home());
  259. /*
  260. * Otherwise we just hand out consecutive cpus. To avoid
  261. * requiring this function to hold state, we just walk forward from
  262. * _sdata by PAGE_SIZE, skipping the readonly and init data, to reach
  263. * the requested address, while walking cpu home around kdata_mask.
  264. * This is typically no more than a dozen or so iterations.
  265. */
  266. page = (((ulong)__w1data_end) + PAGE_SIZE - 1) & PAGE_MASK;
  267. BUG_ON(address < page || address >= (ulong)_end);
  268. cpu = cpumask_first(&kdata_mask);
  269. for (; page < address; page += PAGE_SIZE) {
  270. if (page >= (ulong)&init_thread_union &&
  271. page < (ulong)&init_thread_union + THREAD_SIZE)
  272. continue;
  273. if (page == (ulong)empty_zero_page)
  274. continue;
  275. #ifndef __tilegx__
  276. #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
  277. if (page == (ulong)atomic_locks)
  278. continue;
  279. #endif
  280. #endif
  281. cpu = cpumask_next(cpu, &kdata_mask);
  282. if (cpu == NR_CPUS)
  283. cpu = cpumask_first(&kdata_mask);
  284. }
  285. return construct_pgprot(PAGE_KERNEL, cpu);
  286. }
  287. /*
  288. * This function sets up how we cache the kernel text. If we have
  289. * hash-for-home support, normally that is used instead (see the
  290. * kcache_hash boot flag for more information). But if we end up
  291. * using a page-based caching technique, this option sets up the
  292. * details of that. In addition, the "ktext=nocache" option may
  293. * always be used to disable local caching of text pages, if desired.
  294. */
  295. static int __initdata ktext_arg_seen;
  296. static int __initdata ktext_small;
  297. static int __initdata ktext_local;
  298. static int __initdata ktext_all;
  299. static int __initdata ktext_nondataplane;
  300. static int __initdata ktext_nocache;
  301. static struct cpumask __initdata ktext_mask;
  302. static int __init setup_ktext(char *str)
  303. {
  304. if (str == NULL)
  305. return -EINVAL;
  306. /* If you have a leading "nocache", turn off ktext caching */
  307. if (strncmp(str, "nocache", 7) == 0) {
  308. ktext_nocache = 1;
  309. pr_info("ktext: disabling local caching of kernel text\n");
  310. str += 7;
  311. if (*str == ',')
  312. ++str;
  313. if (*str == '\0')
  314. return 0;
  315. }
  316. ktext_arg_seen = 1;
  317. /* Default setting on Tile64: use a huge page */
  318. if (strcmp(str, "huge") == 0)
  319. pr_info("ktext: using one huge locally cached page\n");
  320. /* Pay TLB cost but get no cache benefit: cache small pages locally */
  321. else if (strcmp(str, "local") == 0) {
  322. ktext_small = 1;
  323. ktext_local = 1;
  324. pr_info("ktext: using small pages with local caching\n");
  325. }
  326. /* Neighborhood cache ktext pages on all cpus. */
  327. else if (strcmp(str, "all") == 0) {
  328. ktext_small = 1;
  329. ktext_all = 1;
  330. pr_info("ktext: using maximal caching neighborhood\n");
  331. }
  332. /* Neighborhood ktext pages on specified mask */
  333. else if (cpulist_parse(str, &ktext_mask) == 0) {
  334. char buf[NR_CPUS * 5];
  335. cpulist_scnprintf(buf, sizeof(buf), &ktext_mask);
  336. if (cpumask_weight(&ktext_mask) > 1) {
  337. ktext_small = 1;
  338. pr_info("ktext: using caching neighborhood %s "
  339. "with small pages\n", buf);
  340. } else {
  341. pr_info("ktext: caching on cpu %s with one huge page\n",
  342. buf);
  343. }
  344. }
  345. else if (*str)
  346. return -EINVAL;
  347. return 0;
  348. }
  349. early_param("ktext", setup_ktext);
  350. static inline pgprot_t ktext_set_nocache(pgprot_t prot)
  351. {
  352. if (!ktext_nocache)
  353. prot = hv_pte_set_nc(prot);
  354. #if CHIP_HAS_NC_AND_NOALLOC_BITS()
  355. else
  356. prot = hv_pte_set_no_alloc_l2(prot);
  357. #endif
  358. return prot;
  359. }
  360. #ifndef __tilegx__
  361. static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
  362. {
  363. return pmd_offset(pud_offset(&pgtables[pgd_index(va)], va), va);
  364. }
  365. #else
  366. static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
  367. {
  368. pud_t *pud = pud_offset(&pgtables[pgd_index(va)], va);
  369. if (pud_none(*pud))
  370. assign_pmd(pud, alloc_pmd());
  371. return pmd_offset(pud, va);
  372. }
  373. #endif
  374. /* Temporary page table we use for staging. */
  375. static pgd_t pgtables[PTRS_PER_PGD]
  376. __attribute__((aligned(HV_PAGE_TABLE_ALIGN)));
  377. /*
  378. * This maps the physical memory to kernel virtual address space, a total
  379. * of max_low_pfn pages, by creating page tables starting from address
  380. * PAGE_OFFSET.
  381. *
  382. * This routine transitions us from using a set of compiled-in large
  383. * pages to using some more precise caching, including removing access
  384. * to code pages mapped at PAGE_OFFSET (executed only at MEM_SV_START)
  385. * marking read-only data as locally cacheable, striping the remaining
  386. * .data and .bss across all the available tiles, and removing access
  387. * to pages above the top of RAM (thus ensuring a page fault from a bad
  388. * virtual address rather than a hypervisor shoot down for accessing
  389. * memory outside the assigned limits).
  390. */
  391. static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
  392. {
  393. unsigned long long irqmask;
  394. unsigned long address, pfn;
  395. pmd_t *pmd;
  396. pte_t *pte;
  397. int pte_ofs;
  398. const struct cpumask *my_cpu_mask = cpumask_of(smp_processor_id());
  399. struct cpumask kstripe_mask;
  400. int rc, i;
  401. #if CHIP_HAS_CBOX_HOME_MAP()
  402. if (ktext_arg_seen && ktext_hash) {
  403. pr_warning("warning: \"ktext\" boot argument ignored"
  404. " if \"kcache_hash\" sets up text hash-for-home\n");
  405. ktext_small = 0;
  406. }
  407. if (kdata_arg_seen && kdata_hash) {
  408. pr_warning("warning: \"kdata\" boot argument ignored"
  409. " if \"kcache_hash\" sets up data hash-for-home\n");
  410. }
  411. if (kdata_huge && !hash_default) {
  412. pr_warning("warning: disabling \"kdata=huge\"; requires"
  413. " kcache_hash=all or =allbutstack\n");
  414. kdata_huge = 0;
  415. }
  416. #endif
  417. /*
  418. * Set up a mask for cpus to use for kernel striping.
  419. * This is normally all cpus, but minus dataplane cpus if any.
  420. * If the dataplane covers the whole chip, we stripe over
  421. * the whole chip too.
  422. */
  423. cpumask_copy(&kstripe_mask, cpu_possible_mask);
  424. if (!kdata_arg_seen)
  425. kdata_mask = kstripe_mask;
  426. /* Allocate and fill in L2 page tables */
  427. for (i = 0; i < MAX_NUMNODES; ++i) {
  428. #ifdef CONFIG_HIGHMEM
  429. unsigned long end_pfn = node_lowmem_end_pfn[i];
  430. #else
  431. unsigned long end_pfn = node_end_pfn[i];
  432. #endif
  433. unsigned long end_huge_pfn = 0;
  434. /* Pre-shatter the last huge page to allow per-cpu pages. */
  435. if (kdata_huge)
  436. end_huge_pfn = end_pfn - (HPAGE_SIZE >> PAGE_SHIFT);
  437. pfn = node_start_pfn[i];
  438. /* Allocate enough memory to hold L2 page tables for node. */
  439. init_prealloc_ptes(i, end_pfn - pfn);
  440. address = (unsigned long) pfn_to_kaddr(pfn);
  441. while (pfn < end_pfn) {
  442. BUG_ON(address & (HPAGE_SIZE-1));
  443. pmd = get_pmd(pgtables, address);
  444. pte = get_prealloc_pte(pfn);
  445. if (pfn < end_huge_pfn) {
  446. pgprot_t prot = init_pgprot(address);
  447. *(pte_t *)pmd = pte_mkhuge(pfn_pte(pfn, prot));
  448. for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
  449. pfn++, pte_ofs++, address += PAGE_SIZE)
  450. pte[pte_ofs] = pfn_pte(pfn, prot);
  451. } else {
  452. if (kdata_huge)
  453. printk(KERN_DEBUG "pre-shattered huge"
  454. " page at %#lx\n", address);
  455. for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
  456. pfn++, pte_ofs++, address += PAGE_SIZE) {
  457. pgprot_t prot = init_pgprot(address);
  458. pte[pte_ofs] = pfn_pte(pfn, prot);
  459. }
  460. assign_pte(pmd, pte);
  461. }
  462. }
  463. }
  464. /*
  465. * Set or check ktext_map now that we have cpu_possible_mask
  466. * and kstripe_mask to work with.
  467. */
  468. if (ktext_all)
  469. cpumask_copy(&ktext_mask, cpu_possible_mask);
  470. else if (ktext_nondataplane)
  471. ktext_mask = kstripe_mask;
  472. else if (!cpumask_empty(&ktext_mask)) {
  473. /* Sanity-check any mask that was requested */
  474. struct cpumask bad;
  475. cpumask_andnot(&bad, &ktext_mask, cpu_possible_mask);
  476. cpumask_and(&ktext_mask, &ktext_mask, cpu_possible_mask);
  477. if (!cpumask_empty(&bad)) {
  478. char buf[NR_CPUS * 5];
  479. cpulist_scnprintf(buf, sizeof(buf), &bad);
  480. pr_info("ktext: not using unavailable cpus %s\n", buf);
  481. }
  482. if (cpumask_empty(&ktext_mask)) {
  483. pr_warning("ktext: no valid cpus; caching on %d.\n",
  484. smp_processor_id());
  485. cpumask_copy(&ktext_mask,
  486. cpumask_of(smp_processor_id()));
  487. }
  488. }
  489. address = MEM_SV_INTRPT;
  490. pmd = get_pmd(pgtables, address);
  491. pfn = 0; /* code starts at PA 0 */
  492. if (ktext_small) {
  493. /* Allocate an L2 PTE for the kernel text */
  494. int cpu = 0;
  495. pgprot_t prot = construct_pgprot(PAGE_KERNEL_EXEC,
  496. PAGE_HOME_IMMUTABLE);
  497. if (ktext_local) {
  498. if (ktext_nocache)
  499. prot = hv_pte_set_mode(prot,
  500. HV_PTE_MODE_UNCACHED);
  501. else
  502. prot = hv_pte_set_mode(prot,
  503. HV_PTE_MODE_CACHE_NO_L3);
  504. } else {
  505. prot = hv_pte_set_mode(prot,
  506. HV_PTE_MODE_CACHE_TILE_L3);
  507. cpu = cpumask_first(&ktext_mask);
  508. prot = ktext_set_nocache(prot);
  509. }
  510. BUG_ON(address != (unsigned long)_stext);
  511. pte = NULL;
  512. for (; address < (unsigned long)_einittext;
  513. pfn++, address += PAGE_SIZE) {
  514. pte_ofs = pte_index(address);
  515. if (pte_ofs == 0) {
  516. if (pte)
  517. assign_pte(pmd++, pte);
  518. pte = alloc_pte();
  519. }
  520. if (!ktext_local) {
  521. prot = set_remote_cache_cpu(prot, cpu);
  522. cpu = cpumask_next(cpu, &ktext_mask);
  523. if (cpu == NR_CPUS)
  524. cpu = cpumask_first(&ktext_mask);
  525. }
  526. pte[pte_ofs] = pfn_pte(pfn, prot);
  527. }
  528. if (pte)
  529. assign_pte(pmd, pte);
  530. } else {
  531. pte_t pteval = pfn_pte(0, PAGE_KERNEL_EXEC);
  532. pteval = pte_mkhuge(pteval);
  533. #if CHIP_HAS_CBOX_HOME_MAP()
  534. if (ktext_hash) {
  535. pteval = hv_pte_set_mode(pteval,
  536. HV_PTE_MODE_CACHE_HASH_L3);
  537. pteval = ktext_set_nocache(pteval);
  538. } else
  539. #endif /* CHIP_HAS_CBOX_HOME_MAP() */
  540. if (cpumask_weight(&ktext_mask) == 1) {
  541. pteval = set_remote_cache_cpu(pteval,
  542. cpumask_first(&ktext_mask));
  543. pteval = hv_pte_set_mode(pteval,
  544. HV_PTE_MODE_CACHE_TILE_L3);
  545. pteval = ktext_set_nocache(pteval);
  546. } else if (ktext_nocache)
  547. pteval = hv_pte_set_mode(pteval,
  548. HV_PTE_MODE_UNCACHED);
  549. else
  550. pteval = hv_pte_set_mode(pteval,
  551. HV_PTE_MODE_CACHE_NO_L3);
  552. for (; address < (unsigned long)_einittext;
  553. pfn += PFN_DOWN(HPAGE_SIZE), address += HPAGE_SIZE)
  554. *(pte_t *)(pmd++) = pfn_pte(pfn, pteval);
  555. }
  556. /* Set swapper_pgprot here so it is flushed to memory right away. */
  557. swapper_pgprot = init_pgprot((unsigned long)swapper_pg_dir);
  558. /*
  559. * Since we may be changing the caching of the stack and page
  560. * table itself, we invoke an assembly helper to do the
  561. * following steps:
  562. *
  563. * - flush the cache so we start with an empty slate
  564. * - install pgtables[] as the real page table
  565. * - flush the TLB so the new page table takes effect
  566. */
  567. irqmask = interrupt_mask_save_mask();
  568. interrupt_mask_set_mask(-1ULL);
  569. rc = flush_and_install_context(__pa(pgtables),
  570. init_pgprot((unsigned long)pgtables),
  571. __get_cpu_var(current_asid),
  572. cpumask_bits(my_cpu_mask));
  573. interrupt_mask_restore_mask(irqmask);
  574. BUG_ON(rc != 0);
  575. /* Copy the page table back to the normal swapper_pg_dir. */
  576. memcpy(pgd_base, pgtables, sizeof(pgtables));
  577. __install_page_table(pgd_base, __get_cpu_var(current_asid),
  578. swapper_pgprot);
  579. /*
  580. * We just read swapper_pgprot and thus brought it into the cache,
  581. * with its new home & caching mode. When we start the other CPUs,
  582. * they're going to reference swapper_pgprot via their initial fake
  583. * VA-is-PA mappings, which cache everything locally. At that
  584. * time, if it's in our cache with a conflicting home, the
  585. * simulator's coherence checker will complain. So, flush it out
  586. * of our cache; we're not going to ever use it again anyway.
  587. */
  588. __insn_finv(&swapper_pgprot);
  589. }
  590. /*
  591. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  592. * is valid. The argument is a physical page number.
  593. *
  594. * On Tile, the only valid things for which we can just hand out unchecked
  595. * PTEs are the kernel code and data. Anything else might change its
  596. * homing with time, and we wouldn't know to adjust the /dev/mem PTEs.
  597. * Note that init_thread_union is released to heap soon after boot,
  598. * so we include it in the init data.
  599. *
  600. * For TILE-Gx, we might want to consider allowing access to PA
  601. * regions corresponding to PCI space, etc.
  602. */
  603. int devmem_is_allowed(unsigned long pagenr)
  604. {
  605. return pagenr < kaddr_to_pfn(_end) &&
  606. !(pagenr >= kaddr_to_pfn(&init_thread_union) ||
  607. pagenr < kaddr_to_pfn(_einitdata)) &&
  608. !(pagenr >= kaddr_to_pfn(_sinittext) ||
  609. pagenr <= kaddr_to_pfn(_einittext-1));
  610. }
  611. #ifdef CONFIG_HIGHMEM
  612. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  613. {
  614. pgd_t *pgd;
  615. pud_t *pud;
  616. pmd_t *pmd;
  617. pte_t *pte;
  618. unsigned long vaddr;
  619. vaddr = PKMAP_BASE;
  620. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  621. pgd = swapper_pg_dir + pgd_index(vaddr);
  622. pud = pud_offset(pgd, vaddr);
  623. pmd = pmd_offset(pud, vaddr);
  624. pte = pte_offset_kernel(pmd, vaddr);
  625. pkmap_page_table = pte;
  626. }
  627. #endif /* CONFIG_HIGHMEM */
  628. static void __init init_free_pfn_range(unsigned long start, unsigned long end)
  629. {
  630. unsigned long pfn;
  631. struct page *page = pfn_to_page(start);
  632. for (pfn = start; pfn < end; ) {
  633. /* Optimize by freeing pages in large batches */
  634. int order = __ffs(pfn);
  635. int count, i;
  636. struct page *p;
  637. if (order >= MAX_ORDER)
  638. order = MAX_ORDER-1;
  639. count = 1 << order;
  640. while (pfn + count > end) {
  641. count >>= 1;
  642. --order;
  643. }
  644. for (p = page, i = 0; i < count; ++i, ++p) {
  645. __ClearPageReserved(p);
  646. /*
  647. * Hacky direct set to avoid unnecessary
  648. * lock take/release for EVERY page here.
  649. */
  650. p->_count.counter = 0;
  651. p->_mapcount.counter = -1;
  652. }
  653. init_page_count(page);
  654. __free_pages(page, order);
  655. totalram_pages += count;
  656. page += count;
  657. pfn += count;
  658. }
  659. }
  660. static void __init set_non_bootmem_pages_init(void)
  661. {
  662. struct zone *z;
  663. for_each_zone(z) {
  664. unsigned long start, end;
  665. int nid = z->zone_pgdat->node_id;
  666. int idx = zone_idx(z);
  667. start = z->zone_start_pfn;
  668. if (start == 0)
  669. continue; /* bootmem */
  670. end = start + z->spanned_pages;
  671. if (idx == ZONE_NORMAL) {
  672. BUG_ON(start != node_start_pfn[nid]);
  673. start = node_free_pfn[nid];
  674. }
  675. #ifdef CONFIG_HIGHMEM
  676. if (idx == ZONE_HIGHMEM)
  677. totalhigh_pages += z->spanned_pages;
  678. #endif
  679. if (kdata_huge) {
  680. unsigned long percpu_pfn = node_percpu_pfn[nid];
  681. if (start < percpu_pfn && end > percpu_pfn)
  682. end = percpu_pfn;
  683. }
  684. #ifdef CONFIG_PCI
  685. if (start <= pci_reserve_start_pfn &&
  686. end > pci_reserve_start_pfn) {
  687. if (end > pci_reserve_end_pfn)
  688. init_free_pfn_range(pci_reserve_end_pfn, end);
  689. end = pci_reserve_start_pfn;
  690. }
  691. #endif
  692. init_free_pfn_range(start, end);
  693. }
  694. }
  695. /*
  696. * paging_init() sets up the page tables - note that all of lowmem is
  697. * already mapped by head.S.
  698. */
  699. void __init paging_init(void)
  700. {
  701. #ifdef CONFIG_HIGHMEM
  702. unsigned long vaddr, end;
  703. #endif
  704. #ifdef __tilegx__
  705. pud_t *pud;
  706. #endif
  707. pgd_t *pgd_base = swapper_pg_dir;
  708. kernel_physical_mapping_init(pgd_base);
  709. #ifdef CONFIG_HIGHMEM
  710. /*
  711. * Fixed mappings, only the page table structure has to be
  712. * created - mappings will be set by set_fixmap():
  713. */
  714. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  715. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  716. page_table_range_init(vaddr, end, pgd_base);
  717. permanent_kmaps_init(pgd_base);
  718. #endif
  719. #ifdef __tilegx__
  720. /*
  721. * Since GX allocates just one pmd_t array worth of vmalloc space,
  722. * we go ahead and allocate it statically here, then share it
  723. * globally. As a result we don't have to worry about any task
  724. * changing init_mm once we get up and running, and there's no
  725. * need for e.g. vmalloc_sync_all().
  726. */
  727. BUILD_BUG_ON(pgd_index(VMALLOC_START) != pgd_index(VMALLOC_END));
  728. pud = pud_offset(pgd_base + pgd_index(VMALLOC_START), VMALLOC_START);
  729. assign_pmd(pud, alloc_pmd());
  730. #endif
  731. }
  732. /*
  733. * Walk the kernel page tables and derive the page_home() from
  734. * the PTEs, so that set_pte() can properly validate the caching
  735. * of all PTEs it sees.
  736. */
  737. void __init set_page_homes(void)
  738. {
  739. }
  740. static void __init set_max_mapnr_init(void)
  741. {
  742. #ifdef CONFIG_FLATMEM
  743. max_mapnr = max_low_pfn;
  744. #endif
  745. }
  746. void __init mem_init(void)
  747. {
  748. int codesize, datasize, initsize;
  749. int i;
  750. #ifndef __tilegx__
  751. void *last;
  752. #endif
  753. #ifdef CONFIG_FLATMEM
  754. BUG_ON(!mem_map);
  755. #endif
  756. #ifdef CONFIG_HIGHMEM
  757. /* check that fixmap and pkmap do not overlap */
  758. if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) {
  759. pr_err("fixmap and kmap areas overlap"
  760. " - this will crash\n");
  761. pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n",
  762. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1),
  763. FIXADDR_START);
  764. BUG();
  765. }
  766. #endif
  767. set_max_mapnr_init();
  768. /* this will put all bootmem onto the freelists */
  769. totalram_pages += free_all_bootmem();
  770. /* count all remaining LOWMEM and give all HIGHMEM to page allocator */
  771. set_non_bootmem_pages_init();
  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. }