init.c 32 KB

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