init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1994 - 2000 Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Kevin D. Kissell, [email protected] and Carsten Langgaard, [email protected]
  9. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/init.h>
  13. #include <linux/export.h>
  14. #include <linux/signal.h>
  15. #include <linux/sched.h>
  16. #include <linux/smp.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/mman.h>
  24. #include <linux/mm.h>
  25. #include <linux/memblock.h>
  26. #include <linux/highmem.h>
  27. #include <linux/swap.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/pfn.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/gfp.h>
  32. #include <linux/kcore.h>
  33. #include <linux/initrd.h>
  34. #include <asm/bootinfo.h>
  35. #include <asm/cachectl.h>
  36. #include <asm/cpu.h>
  37. #include <asm/dma.h>
  38. #include <asm/maar.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/sections.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/tlb.h>
  43. #include <asm/fixmap.h>
  44. /*
  45. * We have up to 8 empty zeroed pages so we can map one of the right colour
  46. * when needed. This is necessary only on R4000 / R4400 SC and MC versions
  47. * where we have to avoid VCED / VECI exceptions for good performance at
  48. * any price. Since page is never written to after the initialization we
  49. * don't have to care about aliases on other CPUs.
  50. */
  51. unsigned long empty_zero_page, zero_page_mask;
  52. EXPORT_SYMBOL_GPL(empty_zero_page);
  53. EXPORT_SYMBOL(zero_page_mask);
  54. /*
  55. * Not static inline because used by IP27 special magic initialization code
  56. */
  57. void setup_zero_pages(void)
  58. {
  59. unsigned int order, i;
  60. struct page *page;
  61. if (cpu_has_vce)
  62. order = 3;
  63. else
  64. order = 0;
  65. empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  66. if (!empty_zero_page)
  67. panic("Oh boy, that early out of memory?");
  68. page = virt_to_page((void *)empty_zero_page);
  69. split_page(page, order);
  70. for (i = 0; i < (1 << order); i++, page++)
  71. mark_page_reserved(page);
  72. zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK;
  73. }
  74. static void *__kmap_pgprot(struct page *page, unsigned long addr, pgprot_t prot)
  75. {
  76. enum fixed_addresses idx;
  77. unsigned int old_mmid;
  78. unsigned long vaddr, flags, entrylo;
  79. unsigned long old_ctx;
  80. pte_t pte;
  81. int tlbidx;
  82. BUG_ON(Page_dcache_dirty(page));
  83. preempt_disable();
  84. pagefault_disable();
  85. idx = (addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1);
  86. idx += in_interrupt() ? FIX_N_COLOURS : 0;
  87. vaddr = __fix_to_virt(FIX_CMAP_END - idx);
  88. pte = mk_pte(page, prot);
  89. #if defined(CONFIG_XPA)
  90. entrylo = pte_to_entrylo(pte.pte_high);
  91. #elif defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
  92. entrylo = pte.pte_high;
  93. #else
  94. entrylo = pte_to_entrylo(pte_val(pte));
  95. #endif
  96. local_irq_save(flags);
  97. old_ctx = read_c0_entryhi();
  98. write_c0_entryhi(vaddr & (PAGE_MASK << 1));
  99. write_c0_entrylo0(entrylo);
  100. write_c0_entrylo1(entrylo);
  101. if (cpu_has_mmid) {
  102. old_mmid = read_c0_memorymapid();
  103. write_c0_memorymapid(MMID_KERNEL_WIRED);
  104. }
  105. #ifdef CONFIG_XPA
  106. if (cpu_has_xpa) {
  107. entrylo = (pte.pte_low & _PFNX_MASK);
  108. writex_c0_entrylo0(entrylo);
  109. writex_c0_entrylo1(entrylo);
  110. }
  111. #endif
  112. tlbidx = num_wired_entries();
  113. write_c0_wired(tlbidx + 1);
  114. write_c0_index(tlbidx);
  115. mtc0_tlbw_hazard();
  116. tlb_write_indexed();
  117. tlbw_use_hazard();
  118. write_c0_entryhi(old_ctx);
  119. if (cpu_has_mmid)
  120. write_c0_memorymapid(old_mmid);
  121. local_irq_restore(flags);
  122. return (void*) vaddr;
  123. }
  124. void *kmap_coherent(struct page *page, unsigned long addr)
  125. {
  126. return __kmap_pgprot(page, addr, PAGE_KERNEL);
  127. }
  128. void *kmap_noncoherent(struct page *page, unsigned long addr)
  129. {
  130. return __kmap_pgprot(page, addr, PAGE_KERNEL_NC);
  131. }
  132. void kunmap_coherent(void)
  133. {
  134. unsigned int wired;
  135. unsigned long flags, old_ctx;
  136. local_irq_save(flags);
  137. old_ctx = read_c0_entryhi();
  138. wired = num_wired_entries() - 1;
  139. write_c0_wired(wired);
  140. write_c0_index(wired);
  141. write_c0_entryhi(UNIQUE_ENTRYHI(wired));
  142. write_c0_entrylo0(0);
  143. write_c0_entrylo1(0);
  144. mtc0_tlbw_hazard();
  145. tlb_write_indexed();
  146. tlbw_use_hazard();
  147. write_c0_entryhi(old_ctx);
  148. local_irq_restore(flags);
  149. pagefault_enable();
  150. preempt_enable();
  151. }
  152. void copy_user_highpage(struct page *to, struct page *from,
  153. unsigned long vaddr, struct vm_area_struct *vma)
  154. {
  155. void *vfrom, *vto;
  156. vto = kmap_atomic(to);
  157. if (cpu_has_dc_aliases &&
  158. page_mapcount(from) && !Page_dcache_dirty(from)) {
  159. vfrom = kmap_coherent(from, vaddr);
  160. copy_page(vto, vfrom);
  161. kunmap_coherent();
  162. } else {
  163. vfrom = kmap_atomic(from);
  164. copy_page(vto, vfrom);
  165. kunmap_atomic(vfrom);
  166. }
  167. if ((!cpu_has_ic_fills_f_dc) ||
  168. pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK))
  169. flush_data_cache_page((unsigned long)vto);
  170. kunmap_atomic(vto);
  171. /* Make sure this page is cleared on other CPU's too before using it */
  172. smp_wmb();
  173. }
  174. void copy_to_user_page(struct vm_area_struct *vma,
  175. struct page *page, unsigned long vaddr, void *dst, const void *src,
  176. unsigned long len)
  177. {
  178. if (cpu_has_dc_aliases &&
  179. page_mapcount(page) && !Page_dcache_dirty(page)) {
  180. void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  181. memcpy(vto, src, len);
  182. kunmap_coherent();
  183. } else {
  184. memcpy(dst, src, len);
  185. if (cpu_has_dc_aliases)
  186. SetPageDcacheDirty(page);
  187. }
  188. if (vma->vm_flags & VM_EXEC)
  189. flush_cache_page(vma, vaddr, page_to_pfn(page));
  190. }
  191. void copy_from_user_page(struct vm_area_struct *vma,
  192. struct page *page, unsigned long vaddr, void *dst, const void *src,
  193. unsigned long len)
  194. {
  195. if (cpu_has_dc_aliases &&
  196. page_mapcount(page) && !Page_dcache_dirty(page)) {
  197. void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  198. memcpy(dst, vfrom, len);
  199. kunmap_coherent();
  200. } else {
  201. memcpy(dst, src, len);
  202. if (cpu_has_dc_aliases)
  203. SetPageDcacheDirty(page);
  204. }
  205. }
  206. EXPORT_SYMBOL_GPL(copy_from_user_page);
  207. void __init fixrange_init(unsigned long start, unsigned long end,
  208. pgd_t *pgd_base)
  209. {
  210. #ifdef CONFIG_HIGHMEM
  211. pgd_t *pgd;
  212. pud_t *pud;
  213. pmd_t *pmd;
  214. pte_t *pte;
  215. int i, j, k;
  216. unsigned long vaddr;
  217. vaddr = start;
  218. i = pgd_index(vaddr);
  219. j = pud_index(vaddr);
  220. k = pmd_index(vaddr);
  221. pgd = pgd_base + i;
  222. for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
  223. pud = (pud_t *)pgd;
  224. for ( ; (j < PTRS_PER_PUD) && (vaddr < end); pud++, j++) {
  225. pmd = (pmd_t *)pud;
  226. for (; (k < PTRS_PER_PMD) && (vaddr < end); pmd++, k++) {
  227. if (pmd_none(*pmd)) {
  228. pte = (pte_t *) memblock_alloc_low(PAGE_SIZE,
  229. PAGE_SIZE);
  230. if (!pte)
  231. panic("%s: Failed to allocate %lu bytes align=%lx\n",
  232. __func__, PAGE_SIZE,
  233. PAGE_SIZE);
  234. set_pmd(pmd, __pmd((unsigned long)pte));
  235. BUG_ON(pte != pte_offset_kernel(pmd, 0));
  236. }
  237. vaddr += PMD_SIZE;
  238. }
  239. k = 0;
  240. }
  241. j = 0;
  242. }
  243. #endif
  244. }
  245. struct maar_walk_info {
  246. struct maar_config cfg[16];
  247. unsigned int num_cfg;
  248. };
  249. static int maar_res_walk(unsigned long start_pfn, unsigned long nr_pages,
  250. void *data)
  251. {
  252. struct maar_walk_info *wi = data;
  253. struct maar_config *cfg = &wi->cfg[wi->num_cfg];
  254. unsigned int maar_align;
  255. /* MAAR registers hold physical addresses right shifted by 4 bits */
  256. maar_align = BIT(MIPS_MAAR_ADDR_SHIFT + 4);
  257. /* Fill in the MAAR config entry */
  258. cfg->lower = ALIGN(PFN_PHYS(start_pfn), maar_align);
  259. cfg->upper = ALIGN_DOWN(PFN_PHYS(start_pfn + nr_pages), maar_align) - 1;
  260. cfg->attrs = MIPS_MAAR_S;
  261. /* Ensure we don't overflow the cfg array */
  262. if (!WARN_ON(wi->num_cfg >= ARRAY_SIZE(wi->cfg)))
  263. wi->num_cfg++;
  264. return 0;
  265. }
  266. unsigned __weak platform_maar_init(unsigned num_pairs)
  267. {
  268. unsigned int num_configured;
  269. struct maar_walk_info wi;
  270. wi.num_cfg = 0;
  271. walk_system_ram_range(0, max_pfn, &wi, maar_res_walk);
  272. num_configured = maar_config(wi.cfg, wi.num_cfg, num_pairs);
  273. if (num_configured < wi.num_cfg)
  274. pr_warn("Not enough MAAR pairs (%u) for all memory regions (%u)\n",
  275. num_pairs, wi.num_cfg);
  276. return num_configured;
  277. }
  278. void maar_init(void)
  279. {
  280. unsigned num_maars, used, i;
  281. phys_addr_t lower, upper, attr;
  282. static struct {
  283. struct maar_config cfgs[3];
  284. unsigned used;
  285. } recorded = { { { 0 } }, 0 };
  286. if (!cpu_has_maar)
  287. return;
  288. /* Detect the number of MAARs */
  289. write_c0_maari(~0);
  290. back_to_back_c0_hazard();
  291. num_maars = read_c0_maari() + 1;
  292. /* MAARs should be in pairs */
  293. WARN_ON(num_maars % 2);
  294. /* Set MAARs using values we recorded already */
  295. if (recorded.used) {
  296. used = maar_config(recorded.cfgs, recorded.used, num_maars / 2);
  297. BUG_ON(used != recorded.used);
  298. } else {
  299. /* Configure the required MAARs */
  300. used = platform_maar_init(num_maars / 2);
  301. }
  302. /* Disable any further MAARs */
  303. for (i = (used * 2); i < num_maars; i++) {
  304. write_c0_maari(i);
  305. back_to_back_c0_hazard();
  306. write_c0_maar(0);
  307. back_to_back_c0_hazard();
  308. }
  309. if (recorded.used)
  310. return;
  311. pr_info("MAAR configuration:\n");
  312. for (i = 0; i < num_maars; i += 2) {
  313. write_c0_maari(i);
  314. back_to_back_c0_hazard();
  315. upper = read_c0_maar();
  316. #ifdef CONFIG_XPA
  317. upper |= (phys_addr_t)readx_c0_maar() << MIPS_MAARX_ADDR_SHIFT;
  318. #endif
  319. write_c0_maari(i + 1);
  320. back_to_back_c0_hazard();
  321. lower = read_c0_maar();
  322. #ifdef CONFIG_XPA
  323. lower |= (phys_addr_t)readx_c0_maar() << MIPS_MAARX_ADDR_SHIFT;
  324. #endif
  325. attr = lower & upper;
  326. lower = (lower & MIPS_MAAR_ADDR) << 4;
  327. upper = ((upper & MIPS_MAAR_ADDR) << 4) | 0xffff;
  328. pr_info(" [%d]: ", i / 2);
  329. if ((attr & MIPS_MAAR_V) != MIPS_MAAR_V) {
  330. pr_cont("disabled\n");
  331. continue;
  332. }
  333. pr_cont("%pa-%pa", &lower, &upper);
  334. if (attr & MIPS_MAAR_S)
  335. pr_cont(" speculate");
  336. pr_cont("\n");
  337. /* Record the setup for use on secondary CPUs */
  338. if (used <= ARRAY_SIZE(recorded.cfgs)) {
  339. recorded.cfgs[recorded.used].lower = lower;
  340. recorded.cfgs[recorded.used].upper = upper;
  341. recorded.cfgs[recorded.used].attrs = attr;
  342. recorded.used++;
  343. }
  344. }
  345. }
  346. #ifndef CONFIG_NUMA
  347. void __init paging_init(void)
  348. {
  349. unsigned long max_zone_pfns[MAX_NR_ZONES];
  350. pagetable_init();
  351. #ifdef CONFIG_ZONE_DMA
  352. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  353. #endif
  354. #ifdef CONFIG_ZONE_DMA32
  355. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  356. #endif
  357. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  358. #ifdef CONFIG_HIGHMEM
  359. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  360. if (cpu_has_dc_aliases && max_low_pfn != highend_pfn) {
  361. printk(KERN_WARNING "This processor doesn't support highmem."
  362. " %ldk highmem ignored\n",
  363. (highend_pfn - max_low_pfn) << (PAGE_SHIFT - 10));
  364. max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
  365. }
  366. #endif
  367. free_area_init(max_zone_pfns);
  368. }
  369. #ifdef CONFIG_64BIT
  370. static struct kcore_list kcore_kseg0;
  371. #endif
  372. static inline void __init mem_init_free_highmem(void)
  373. {
  374. #ifdef CONFIG_HIGHMEM
  375. unsigned long tmp;
  376. if (cpu_has_dc_aliases)
  377. return;
  378. for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
  379. struct page *page = pfn_to_page(tmp);
  380. if (!memblock_is_memory(PFN_PHYS(tmp)))
  381. SetPageReserved(page);
  382. else
  383. free_highmem_page(page);
  384. }
  385. #endif
  386. }
  387. void __init mem_init(void)
  388. {
  389. /*
  390. * When _PFN_SHIFT is greater than PAGE_SHIFT we won't have enough PTE
  391. * bits to hold a full 32b physical address on MIPS32 systems.
  392. */
  393. BUILD_BUG_ON(IS_ENABLED(CONFIG_32BIT) && (_PFN_SHIFT > PAGE_SHIFT));
  394. #ifdef CONFIG_HIGHMEM
  395. max_mapnr = highend_pfn ? highend_pfn : max_low_pfn;
  396. #else
  397. max_mapnr = max_low_pfn;
  398. #endif
  399. high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
  400. maar_init();
  401. memblock_free_all();
  402. setup_zero_pages(); /* Setup zeroed pages. */
  403. mem_init_free_highmem();
  404. #ifdef CONFIG_64BIT
  405. if ((unsigned long) &_text > (unsigned long) CKSEG0)
  406. /* The -4 is a hack so that user tools don't have to handle
  407. the overflow. */
  408. kclist_add(&kcore_kseg0, (void *) CKSEG0,
  409. 0x80000000 - 4, KCORE_TEXT);
  410. #endif
  411. }
  412. #endif /* !CONFIG_NUMA */
  413. void free_init_pages(const char *what, unsigned long begin, unsigned long end)
  414. {
  415. unsigned long pfn;
  416. for (pfn = PFN_UP(begin); pfn < PFN_DOWN(end); pfn++) {
  417. struct page *page = pfn_to_page(pfn);
  418. void *addr = phys_to_virt(PFN_PHYS(pfn));
  419. memset(addr, POISON_FREE_INITMEM, PAGE_SIZE);
  420. free_reserved_page(page);
  421. }
  422. printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
  423. }
  424. void (*free_init_pages_eva)(void *begin, void *end) = NULL;
  425. void __weak __init prom_free_prom_memory(void)
  426. {
  427. /* nothing to do */
  428. }
  429. void __ref free_initmem(void)
  430. {
  431. prom_free_prom_memory();
  432. /*
  433. * Let the platform define a specific function to free the
  434. * init section since EVA may have used any possible mapping
  435. * between virtual and physical addresses.
  436. */
  437. if (free_init_pages_eva)
  438. free_init_pages_eva((void *)&__init_begin, (void *)&__init_end);
  439. else
  440. free_initmem_default(POISON_FREE_INITMEM);
  441. }
  442. #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
  443. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  444. EXPORT_SYMBOL(__per_cpu_offset);
  445. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  446. {
  447. return node_distance(cpu_to_node(from), cpu_to_node(to));
  448. }
  449. static int __init pcpu_cpu_to_node(int cpu)
  450. {
  451. return cpu_to_node(cpu);
  452. }
  453. void __init setup_per_cpu_areas(void)
  454. {
  455. unsigned long delta;
  456. unsigned int cpu;
  457. int rc;
  458. /*
  459. * Always reserve area for module percpu variables. That's
  460. * what the legacy allocator did.
  461. */
  462. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  463. PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
  464. pcpu_cpu_distance,
  465. pcpu_cpu_to_node);
  466. if (rc < 0)
  467. panic("Failed to initialize percpu areas.");
  468. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  469. for_each_possible_cpu(cpu)
  470. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  471. }
  472. #endif
  473. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  474. unsigned long pgd_current[NR_CPUS];
  475. #endif
  476. /*
  477. * Align swapper_pg_dir in to 64K, allows its address to be loaded
  478. * with a single LUI instruction in the TLB handlers. If we used
  479. * __aligned(64K), its size would get rounded up to the alignment
  480. * size, and waste space. So we place it in its own section and align
  481. * it in the linker script.
  482. */
  483. pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".bss..swapper_pg_dir");
  484. #ifndef __PAGETABLE_PUD_FOLDED
  485. pud_t invalid_pud_table[PTRS_PER_PUD] __page_aligned_bss;
  486. #endif
  487. #ifndef __PAGETABLE_PMD_FOLDED
  488. pmd_t invalid_pmd_table[PTRS_PER_PMD] __page_aligned_bss;
  489. EXPORT_SYMBOL_GPL(invalid_pmd_table);
  490. #endif
  491. pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss;
  492. EXPORT_SYMBOL(invalid_pte_table);