init.c 13 KB

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