init.c 11 KB

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