init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  9. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/init.h>
  13. #include <linux/module.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/bootmem.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 <asm/asm-offsets.h>
  34. #include <asm/bootinfo.h>
  35. #include <asm/cachectl.h>
  36. #include <asm/cpu.h>
  37. #include <asm/dma.h>
  38. #include <asm/kmap_types.h>
  39. #include <asm/maar.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/sections.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/tlb.h>
  45. #include <asm/fixmap.h>
  46. #include <asm/maar.h>
  47. /*
  48. * We have up to 8 empty zeroed pages so we can map one of the right colour
  49. * when needed. This is necessary only on R4000 / R4400 SC and MC versions
  50. * where we have to avoid VCED / VECI exceptions for good performance at
  51. * any price. Since page is never written to after the initialization we
  52. * don't have to care about aliases on other CPUs.
  53. */
  54. unsigned long empty_zero_page, zero_page_mask;
  55. EXPORT_SYMBOL_GPL(empty_zero_page);
  56. EXPORT_SYMBOL(zero_page_mask);
  57. /*
  58. * Not static inline because used by IP27 special magic initialization code
  59. */
  60. void setup_zero_pages(void)
  61. {
  62. unsigned int order, i;
  63. struct page *page;
  64. if (cpu_has_vce)
  65. order = 3;
  66. else
  67. order = 0;
  68. empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  69. if (!empty_zero_page)
  70. panic("Oh boy, that early out of memory?");
  71. page = virt_to_page((void *)empty_zero_page);
  72. split_page(page, order);
  73. for (i = 0; i < (1 << order); i++, page++)
  74. mark_page_reserved(page);
  75. zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK;
  76. }
  77. static void *__kmap_pgprot(struct page *page, unsigned long addr, pgprot_t prot)
  78. {
  79. enum fixed_addresses idx;
  80. unsigned long vaddr, flags, entrylo;
  81. unsigned long old_ctx;
  82. pte_t pte;
  83. int tlbidx;
  84. BUG_ON(Page_dcache_dirty(page));
  85. preempt_disable();
  86. pagefault_disable();
  87. idx = (addr >> PAGE_SHIFT) & (FIX_N_COLOURS - 1);
  88. idx += in_interrupt() ? FIX_N_COLOURS : 0;
  89. vaddr = __fix_to_virt(FIX_CMAP_END - idx);
  90. pte = mk_pte(page, prot);
  91. #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
  92. entrylo = pte_to_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. #ifdef CONFIG_XPA
  102. entrylo = (pte.pte_low & _PFNX_MASK);
  103. writex_c0_entrylo0(entrylo);
  104. writex_c0_entrylo1(entrylo);
  105. #endif
  106. tlbidx = read_c0_wired();
  107. write_c0_wired(tlbidx + 1);
  108. write_c0_index(tlbidx);
  109. mtc0_tlbw_hazard();
  110. tlb_write_indexed();
  111. tlbw_use_hazard();
  112. write_c0_entryhi(old_ctx);
  113. local_irq_restore(flags);
  114. return (void*) vaddr;
  115. }
  116. void *kmap_coherent(struct page *page, unsigned long addr)
  117. {
  118. return __kmap_pgprot(page, addr, PAGE_KERNEL);
  119. }
  120. void *kmap_noncoherent(struct page *page, unsigned long addr)
  121. {
  122. return __kmap_pgprot(page, addr, PAGE_KERNEL_NC);
  123. }
  124. void kunmap_coherent(void)
  125. {
  126. unsigned int wired;
  127. unsigned long flags, old_ctx;
  128. local_irq_save(flags);
  129. old_ctx = read_c0_entryhi();
  130. wired = read_c0_wired() - 1;
  131. write_c0_wired(wired);
  132. write_c0_index(wired);
  133. write_c0_entryhi(UNIQUE_ENTRYHI(wired));
  134. write_c0_entrylo0(0);
  135. write_c0_entrylo1(0);
  136. mtc0_tlbw_hazard();
  137. tlb_write_indexed();
  138. tlbw_use_hazard();
  139. write_c0_entryhi(old_ctx);
  140. local_irq_restore(flags);
  141. pagefault_enable();
  142. preempt_enable();
  143. }
  144. void copy_user_highpage(struct page *to, struct page *from,
  145. unsigned long vaddr, struct vm_area_struct *vma)
  146. {
  147. void *vfrom, *vto;
  148. vto = kmap_atomic(to);
  149. if (cpu_has_dc_aliases &&
  150. page_mapped(from) && !Page_dcache_dirty(from)) {
  151. vfrom = kmap_coherent(from, vaddr);
  152. copy_page(vto, vfrom);
  153. kunmap_coherent();
  154. } else {
  155. vfrom = kmap_atomic(from);
  156. copy_page(vto, vfrom);
  157. kunmap_atomic(vfrom);
  158. }
  159. if ((!cpu_has_ic_fills_f_dc) ||
  160. pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK))
  161. flush_data_cache_page((unsigned long)vto);
  162. kunmap_atomic(vto);
  163. /* Make sure this page is cleared on other CPU's too before using it */
  164. smp_wmb();
  165. }
  166. void copy_to_user_page(struct vm_area_struct *vma,
  167. struct page *page, unsigned long vaddr, void *dst, const void *src,
  168. unsigned long len)
  169. {
  170. if (cpu_has_dc_aliases &&
  171. page_mapped(page) && !Page_dcache_dirty(page)) {
  172. void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  173. memcpy(vto, src, len);
  174. kunmap_coherent();
  175. } else {
  176. memcpy(dst, src, len);
  177. if (cpu_has_dc_aliases)
  178. SetPageDcacheDirty(page);
  179. }
  180. if ((vma->vm_flags & VM_EXEC) && !cpu_has_ic_fills_f_dc)
  181. flush_cache_page(vma, vaddr, page_to_pfn(page));
  182. }
  183. void copy_from_user_page(struct vm_area_struct *vma,
  184. struct page *page, unsigned long vaddr, void *dst, const void *src,
  185. unsigned long len)
  186. {
  187. if (cpu_has_dc_aliases &&
  188. page_mapped(page) && !Page_dcache_dirty(page)) {
  189. void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  190. memcpy(dst, vfrom, len);
  191. kunmap_coherent();
  192. } else {
  193. memcpy(dst, src, len);
  194. if (cpu_has_dc_aliases)
  195. SetPageDcacheDirty(page);
  196. }
  197. }
  198. EXPORT_SYMBOL_GPL(copy_from_user_page);
  199. void __init fixrange_init(unsigned long start, unsigned long end,
  200. pgd_t *pgd_base)
  201. {
  202. #ifdef CONFIG_HIGHMEM
  203. pgd_t *pgd;
  204. pud_t *pud;
  205. pmd_t *pmd;
  206. pte_t *pte;
  207. int i, j, k;
  208. unsigned long vaddr;
  209. vaddr = start;
  210. i = __pgd_offset(vaddr);
  211. j = __pud_offset(vaddr);
  212. k = __pmd_offset(vaddr);
  213. pgd = pgd_base + i;
  214. for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
  215. pud = (pud_t *)pgd;
  216. for ( ; (j < PTRS_PER_PUD) && (vaddr < end); pud++, j++) {
  217. pmd = (pmd_t *)pud;
  218. for (; (k < PTRS_PER_PMD) && (vaddr < end); pmd++, k++) {
  219. if (pmd_none(*pmd)) {
  220. pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  221. set_pmd(pmd, __pmd((unsigned long)pte));
  222. BUG_ON(pte != pte_offset_kernel(pmd, 0));
  223. }
  224. vaddr += PMD_SIZE;
  225. }
  226. k = 0;
  227. }
  228. j = 0;
  229. }
  230. #endif
  231. }
  232. unsigned __weak platform_maar_init(unsigned num_pairs)
  233. {
  234. struct maar_config cfg[BOOT_MEM_MAP_MAX];
  235. unsigned i, num_configured, num_cfg = 0;
  236. phys_addr_t skip;
  237. for (i = 0; i < boot_mem_map.nr_map; i++) {
  238. switch (boot_mem_map.map[i].type) {
  239. case BOOT_MEM_RAM:
  240. case BOOT_MEM_INIT_RAM:
  241. break;
  242. default:
  243. continue;
  244. }
  245. skip = 0x10000 - (boot_mem_map.map[i].addr & 0xffff);
  246. cfg[num_cfg].lower = boot_mem_map.map[i].addr;
  247. cfg[num_cfg].lower += skip;
  248. cfg[num_cfg].upper = cfg[num_cfg].lower;
  249. cfg[num_cfg].upper += boot_mem_map.map[i].size - 1;
  250. cfg[num_cfg].upper -= skip;
  251. cfg[num_cfg].attrs = MIPS_MAAR_S;
  252. num_cfg++;
  253. }
  254. num_configured = maar_config(cfg, num_cfg, num_pairs);
  255. if (num_configured < num_cfg)
  256. pr_warn("Not enough MAAR pairs (%u) for all bootmem regions (%u)\n",
  257. num_pairs, num_cfg);
  258. return num_configured;
  259. }
  260. void maar_init(void)
  261. {
  262. unsigned num_maars, used, i;
  263. phys_addr_t lower, upper, attr;
  264. static struct {
  265. struct maar_config cfgs[3];
  266. unsigned used;
  267. } recorded = { { { 0 } }, 0 };
  268. if (!cpu_has_maar)
  269. return;
  270. /* Detect the number of MAARs */
  271. write_c0_maari(~0);
  272. back_to_back_c0_hazard();
  273. num_maars = read_c0_maari() + 1;
  274. /* MAARs should be in pairs */
  275. WARN_ON(num_maars % 2);
  276. /* Set MAARs using values we recorded already */
  277. if (recorded.used) {
  278. used = maar_config(recorded.cfgs, recorded.used, num_maars / 2);
  279. BUG_ON(used != recorded.used);
  280. } else {
  281. /* Configure the required MAARs */
  282. used = platform_maar_init(num_maars / 2);
  283. }
  284. /* Disable any further MAARs */
  285. for (i = (used * 2); i < num_maars; i++) {
  286. write_c0_maari(i);
  287. back_to_back_c0_hazard();
  288. write_c0_maar(0);
  289. back_to_back_c0_hazard();
  290. }
  291. if (recorded.used)
  292. return;
  293. pr_info("MAAR configuration:\n");
  294. for (i = 0; i < num_maars; i += 2) {
  295. write_c0_maari(i);
  296. back_to_back_c0_hazard();
  297. upper = read_c0_maar();
  298. write_c0_maari(i + 1);
  299. back_to_back_c0_hazard();
  300. lower = read_c0_maar();
  301. attr = lower & upper;
  302. lower = (lower & MIPS_MAAR_ADDR) << 4;
  303. upper = ((upper & MIPS_MAAR_ADDR) << 4) | 0xffff;
  304. pr_info(" [%d]: ", i / 2);
  305. if (!(attr & MIPS_MAAR_V)) {
  306. pr_cont("disabled\n");
  307. continue;
  308. }
  309. pr_cont("%pa-%pa", &lower, &upper);
  310. if (attr & MIPS_MAAR_S)
  311. pr_cont(" speculate");
  312. pr_cont("\n");
  313. /* Record the setup for use on secondary CPUs */
  314. if (used <= ARRAY_SIZE(recorded.cfgs)) {
  315. recorded.cfgs[recorded.used].lower = lower;
  316. recorded.cfgs[recorded.used].upper = upper;
  317. recorded.cfgs[recorded.used].attrs = attr;
  318. recorded.used++;
  319. }
  320. }
  321. }
  322. #ifndef CONFIG_NEED_MULTIPLE_NODES
  323. int page_is_ram(unsigned long pagenr)
  324. {
  325. int i;
  326. for (i = 0; i < boot_mem_map.nr_map; i++) {
  327. unsigned long addr, end;
  328. switch (boot_mem_map.map[i].type) {
  329. case BOOT_MEM_RAM:
  330. case BOOT_MEM_INIT_RAM:
  331. break;
  332. default:
  333. /* not usable memory */
  334. continue;
  335. }
  336. addr = PFN_UP(boot_mem_map.map[i].addr);
  337. end = PFN_DOWN(boot_mem_map.map[i].addr +
  338. boot_mem_map.map[i].size);
  339. if (pagenr >= addr && pagenr < end)
  340. return 1;
  341. }
  342. return 0;
  343. }
  344. void __init paging_init(void)
  345. {
  346. unsigned long max_zone_pfns[MAX_NR_ZONES];
  347. unsigned long lastpfn __maybe_unused;
  348. pagetable_init();
  349. #ifdef CONFIG_HIGHMEM
  350. kmap_init();
  351. #endif
  352. #ifdef CONFIG_ZONE_DMA
  353. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  354. #endif
  355. #ifdef CONFIG_ZONE_DMA32
  356. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  357. #endif
  358. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  359. lastpfn = max_low_pfn;
  360. #ifdef CONFIG_HIGHMEM
  361. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  362. lastpfn = highend_pfn;
  363. if (cpu_has_dc_aliases && max_low_pfn != highend_pfn) {
  364. printk(KERN_WARNING "This processor doesn't support highmem."
  365. " %ldk highmem ignored\n",
  366. (highend_pfn - max_low_pfn) << (PAGE_SHIFT - 10));
  367. max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
  368. lastpfn = max_low_pfn;
  369. }
  370. #endif
  371. free_area_init_nodes(max_zone_pfns);
  372. }
  373. #ifdef CONFIG_64BIT
  374. static struct kcore_list kcore_kseg0;
  375. #endif
  376. static inline void mem_init_free_highmem(void)
  377. {
  378. #ifdef CONFIG_HIGHMEM
  379. unsigned long tmp;
  380. for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) {
  381. struct page *page = pfn_to_page(tmp);
  382. if (!page_is_ram(tmp))
  383. SetPageReserved(page);
  384. else
  385. free_highmem_page(page);
  386. }
  387. #endif
  388. }
  389. void __init mem_init(void)
  390. {
  391. #ifdef CONFIG_HIGHMEM
  392. #ifdef CONFIG_DISCONTIGMEM
  393. #error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet"
  394. #endif
  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. free_all_bootmem();
  402. setup_zero_pages(); /* Setup zeroed pages. */
  403. mem_init_free_highmem();
  404. mem_init_print_info(NULL);
  405. #ifdef CONFIG_64BIT
  406. if ((unsigned long) &_text > (unsigned long) CKSEG0)
  407. /* The -4 is a hack so that user tools don't have to handle
  408. the overflow. */
  409. kclist_add(&kcore_kseg0, (void *) CKSEG0,
  410. 0x80000000 - 4, KCORE_TEXT);
  411. #endif
  412. }
  413. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  414. void free_init_pages(const char *what, unsigned long begin, unsigned long end)
  415. {
  416. unsigned long pfn;
  417. for (pfn = PFN_UP(begin); pfn < PFN_DOWN(end); pfn++) {
  418. struct page *page = pfn_to_page(pfn);
  419. void *addr = phys_to_virt(PFN_PHYS(pfn));
  420. memset(addr, POISON_FREE_INITMEM, PAGE_SIZE);
  421. free_reserved_page(page);
  422. }
  423. printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
  424. }
  425. #ifdef CONFIG_BLK_DEV_INITRD
  426. void free_initrd_mem(unsigned long start, unsigned long end)
  427. {
  428. free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
  429. "initrd");
  430. }
  431. #endif
  432. void (*free_init_pages_eva)(void *begin, void *end) = NULL;
  433. void __init_refok free_initmem(void)
  434. {
  435. prom_free_prom_memory();
  436. /*
  437. * Let the platform define a specific function to free the
  438. * init section since EVA may have used any possible mapping
  439. * between virtual and physical addresses.
  440. */
  441. if (free_init_pages_eva)
  442. free_init_pages_eva((void *)&__init_begin, (void *)&__init_end);
  443. else
  444. free_initmem_default(POISON_FREE_INITMEM);
  445. }
  446. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  447. unsigned long pgd_current[NR_CPUS];
  448. #endif
  449. /*
  450. * gcc 3.3 and older have trouble determining that PTRS_PER_PGD and PGD_ORDER
  451. * are constants. So we use the variants from asm-offset.h until that gcc
  452. * will officially be retired.
  453. *
  454. * Align swapper_pg_dir in to 64K, allows its address to be loaded
  455. * with a single LUI instruction in the TLB handlers. If we used
  456. * __aligned(64K), its size would get rounded up to the alignment
  457. * size, and waste space. So we place it in its own section and align
  458. * it in the linker script.
  459. */
  460. pgd_t swapper_pg_dir[_PTRS_PER_PGD] __section(.bss..swapper_pg_dir);
  461. #ifndef __PAGETABLE_PMD_FOLDED
  462. pmd_t invalid_pmd_table[PTRS_PER_PMD] __page_aligned_bss;
  463. #endif
  464. pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss;