init.c 31 KB

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