init.c 31 KB

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