init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  6. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  7. * Copyright (C) 1996 Paul Mackerras
  8. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  9. * PPC44x/36-bit changes by Matt Porter (mporter@mvista.com)
  10. *
  11. * Derived from "arch/i386/mm/init.c"
  12. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/types.h>
  26. #include <linux/mm.h>
  27. #include <linux/stddef.h>
  28. #include <linux/init.h>
  29. #include <linux/bootmem.h>
  30. #include <linux/highmem.h>
  31. #include <linux/initrd.h>
  32. #include <linux/pagemap.h>
  33. #include <asm/pgalloc.h>
  34. #include <asm/prom.h>
  35. #include <asm/io.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/mmu.h>
  39. #include <asm/smp.h>
  40. #include <asm/machdep.h>
  41. #include <asm/btext.h>
  42. #include <asm/tlb.h>
  43. #include <asm/bootinfo.h>
  44. #include "mem_pieces.h"
  45. #include "mmu_decl.h"
  46. #if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
  47. /* The amount of lowmem must be within 0xF0000000 - KERNELBASE. */
  48. #if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
  49. #error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
  50. #endif
  51. #endif
  52. #define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
  53. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  54. unsigned long total_memory;
  55. unsigned long total_lowmem;
  56. unsigned long ppc_memstart;
  57. unsigned long ppc_memoffset = PAGE_OFFSET;
  58. int mem_init_done;
  59. int init_bootmem_done;
  60. int boot_mapsize;
  61. extern char _end[];
  62. extern char etext[], _stext[];
  63. extern char __init_begin, __init_end;
  64. #ifdef CONFIG_HIGHMEM
  65. pte_t *kmap_pte;
  66. pgprot_t kmap_prot;
  67. EXPORT_SYMBOL(kmap_prot);
  68. EXPORT_SYMBOL(kmap_pte);
  69. #endif
  70. void MMU_init(void);
  71. void set_phys_avail(unsigned long total_ram);
  72. /* XXX should be in current.h -- paulus */
  73. extern struct task_struct *current_set[NR_CPUS];
  74. char *klimit = _end;
  75. struct mem_pieces phys_avail;
  76. /*
  77. * this tells the system to map all of ram with the segregs
  78. * (i.e. page tables) instead of the bats.
  79. * -- Cort
  80. */
  81. int __map_without_bats;
  82. int __map_without_ltlbs;
  83. /* max amount of RAM to use */
  84. unsigned long __max_memory;
  85. /* max amount of low RAM to map in */
  86. unsigned long __max_low_memory = MAX_LOW_MEM;
  87. void show_mem(void)
  88. {
  89. int i,free = 0,total = 0,reserved = 0;
  90. int shared = 0, cached = 0;
  91. int highmem = 0;
  92. printk("Mem-info:\n");
  93. show_free_areas();
  94. i = max_mapnr;
  95. while (i-- > 0) {
  96. total++;
  97. if (PageHighMem(mem_map+i))
  98. highmem++;
  99. if (PageReserved(mem_map+i))
  100. reserved++;
  101. else if (PageSwapCache(mem_map+i))
  102. cached++;
  103. else if (!page_count(mem_map+i))
  104. free++;
  105. else
  106. shared += page_count(mem_map+i) - 1;
  107. }
  108. printk("%d pages of RAM\n",total);
  109. printk("%d pages of HIGHMEM\n", highmem);
  110. printk("%d free pages\n",free);
  111. printk("%d reserved pages\n",reserved);
  112. printk("%d pages shared\n",shared);
  113. printk("%d pages swap cached\n",cached);
  114. }
  115. /* Free up now-unused memory */
  116. static void free_sec(unsigned long start, unsigned long end, const char *name)
  117. {
  118. unsigned long cnt = 0;
  119. while (start < end) {
  120. ClearPageReserved(virt_to_page(start));
  121. init_page_count(virt_to_page(start));
  122. free_page(start);
  123. cnt++;
  124. start += PAGE_SIZE;
  125. }
  126. if (cnt) {
  127. printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
  128. totalram_pages += cnt;
  129. }
  130. }
  131. void free_initmem(void)
  132. {
  133. #define FREESEC(TYPE) \
  134. free_sec((unsigned long)(&__ ## TYPE ## _begin), \
  135. (unsigned long)(&__ ## TYPE ## _end), \
  136. #TYPE);
  137. printk ("Freeing unused kernel memory:");
  138. FREESEC(init);
  139. printk("\n");
  140. ppc_md.progress = NULL;
  141. #undef FREESEC
  142. }
  143. #ifdef CONFIG_BLK_DEV_INITRD
  144. void free_initrd_mem(unsigned long start, unsigned long end)
  145. {
  146. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  147. for (; start < end; start += PAGE_SIZE) {
  148. ClearPageReserved(virt_to_page(start));
  149. init_page_count(virt_to_page(start));
  150. free_page(start);
  151. totalram_pages++;
  152. }
  153. }
  154. #endif
  155. /*
  156. * Check for command-line options that affect what MMU_init will do.
  157. */
  158. void MMU_setup(void)
  159. {
  160. /* Check for nobats option (used in mapin_ram). */
  161. if (strstr(cmd_line, "nobats")) {
  162. __map_without_bats = 1;
  163. }
  164. if (strstr(cmd_line, "noltlbs")) {
  165. __map_without_ltlbs = 1;
  166. }
  167. /* Look for mem= option on command line */
  168. if (strstr(cmd_line, "mem=")) {
  169. char *p, *q;
  170. unsigned long maxmem = 0;
  171. for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
  172. q = p + 4;
  173. if (p > cmd_line && p[-1] != ' ')
  174. continue;
  175. maxmem = simple_strtoul(q, &q, 0);
  176. if (*q == 'k' || *q == 'K') {
  177. maxmem <<= 10;
  178. ++q;
  179. } else if (*q == 'm' || *q == 'M') {
  180. maxmem <<= 20;
  181. ++q;
  182. }
  183. }
  184. __max_memory = maxmem;
  185. }
  186. }
  187. /*
  188. * MMU_init sets up the basic memory mappings for the kernel,
  189. * including both RAM and possibly some I/O regions,
  190. * and sets up the page tables and the MMU hardware ready to go.
  191. */
  192. void __init MMU_init(void)
  193. {
  194. if (ppc_md.progress)
  195. ppc_md.progress("MMU:enter", 0x111);
  196. /* parse args from command line */
  197. MMU_setup();
  198. /*
  199. * Figure out how much memory we have, how much
  200. * is lowmem, and how much is highmem. If we were
  201. * passed the total memory size from the bootloader,
  202. * just use it.
  203. */
  204. if (boot_mem_size)
  205. total_memory = boot_mem_size;
  206. else
  207. total_memory = ppc_md.find_end_of_memory();
  208. if (__max_memory && total_memory > __max_memory)
  209. total_memory = __max_memory;
  210. total_lowmem = total_memory;
  211. if (total_lowmem > __max_low_memory) {
  212. total_lowmem = __max_low_memory;
  213. #ifndef CONFIG_HIGHMEM
  214. total_memory = total_lowmem;
  215. #endif /* CONFIG_HIGHMEM */
  216. }
  217. set_phys_avail(total_lowmem);
  218. /* Initialize the MMU hardware */
  219. if (ppc_md.progress)
  220. ppc_md.progress("MMU:hw init", 0x300);
  221. MMU_init_hw();
  222. /* Map in all of RAM starting at KERNELBASE */
  223. if (ppc_md.progress)
  224. ppc_md.progress("MMU:mapin", 0x301);
  225. mapin_ram();
  226. #ifdef CONFIG_HIGHMEM
  227. ioremap_base = PKMAP_BASE;
  228. #else
  229. ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
  230. #endif /* CONFIG_HIGHMEM */
  231. ioremap_bot = ioremap_base;
  232. /* Map in I/O resources */
  233. if (ppc_md.progress)
  234. ppc_md.progress("MMU:setio", 0x302);
  235. if (ppc_md.setup_io_mappings)
  236. ppc_md.setup_io_mappings();
  237. /* Initialize the context management stuff */
  238. mmu_context_init();
  239. if (ppc_md.progress)
  240. ppc_md.progress("MMU:exit", 0x211);
  241. #ifdef CONFIG_BOOTX_TEXT
  242. /* By default, we are no longer mapped */
  243. boot_text_mapped = 0;
  244. /* Must be done last, or ppc_md.progress will die. */
  245. map_boot_text();
  246. #endif
  247. }
  248. /* This is only called until mem_init is done. */
  249. void __init *early_get_page(void)
  250. {
  251. void *p;
  252. if (init_bootmem_done) {
  253. p = alloc_bootmem_pages(PAGE_SIZE);
  254. } else {
  255. p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
  256. }
  257. return p;
  258. }
  259. /*
  260. * Initialize the bootmem system and give it all the memory we
  261. * have available.
  262. */
  263. void __init do_init_bootmem(void)
  264. {
  265. unsigned long start, size;
  266. int i;
  267. /*
  268. * Find an area to use for the bootmem bitmap.
  269. * We look for the first area which is at least
  270. * 128kB in length (128kB is enough for a bitmap
  271. * for 4GB of memory, using 4kB pages), plus 1 page
  272. * (in case the address isn't page-aligned).
  273. */
  274. start = 0;
  275. size = 0;
  276. for (i = 0; i < phys_avail.n_regions; ++i) {
  277. unsigned long a = phys_avail.regions[i].address;
  278. unsigned long s = phys_avail.regions[i].size;
  279. if (s <= size)
  280. continue;
  281. start = a;
  282. size = s;
  283. if (s >= 33 * PAGE_SIZE)
  284. break;
  285. }
  286. start = PAGE_ALIGN(start);
  287. min_low_pfn = start >> PAGE_SHIFT;
  288. max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
  289. max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
  290. boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
  291. PPC_MEMSTART >> PAGE_SHIFT,
  292. max_low_pfn);
  293. /* remove the bootmem bitmap from the available memory */
  294. mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
  295. /* add everything in phys_avail into the bootmem map */
  296. for (i = 0; i < phys_avail.n_regions; ++i)
  297. free_bootmem(phys_avail.regions[i].address,
  298. phys_avail.regions[i].size);
  299. init_bootmem_done = 1;
  300. }
  301. /*
  302. * paging_init() sets up the page tables - in fact we've already done this.
  303. */
  304. void __init paging_init(void)
  305. {
  306. unsigned long start_pfn, end_pfn;
  307. unsigned long max_zone_pfns[MAX_NR_ZONES];
  308. #ifdef CONFIG_HIGHMEM
  309. map_page(PKMAP_BASE, 0, 0); /* XXX gross */
  310. pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
  311. (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
  312. map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
  313. kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
  314. (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
  315. kmap_prot = PAGE_KERNEL;
  316. #endif /* CONFIG_HIGHMEM */
  317. /* All pages are DMA-able so we put them all in the DMA zone. */
  318. start_pfn = __pa(PAGE_OFFSET) >> PAGE_SHIFT;
  319. end_pfn = start_pfn + (total_memory >> PAGE_SHIFT);
  320. add_active_range(0, start_pfn, end_pfn);
  321. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  322. #ifdef CONFIG_HIGHMEM
  323. max_zone_pfns[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
  324. max_zone_pfns[ZONE_HIGHMEM] = total_memory >> PAGE_SHIFT;
  325. #else
  326. max_zone_pfns[ZONE_DMA] = total_memory >> PAGE_SHIFT;
  327. #endif /* CONFIG_HIGHMEM */
  328. free_area_init_nodes(max_zone_pfns);
  329. }
  330. void __init mem_init(void)
  331. {
  332. unsigned long addr;
  333. int codepages = 0;
  334. int datapages = 0;
  335. int initpages = 0;
  336. #ifdef CONFIG_HIGHMEM
  337. unsigned long highmem_mapnr;
  338. highmem_mapnr = total_lowmem >> PAGE_SHIFT;
  339. #endif /* CONFIG_HIGHMEM */
  340. max_mapnr = total_memory >> PAGE_SHIFT;
  341. high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
  342. num_physpages = max_mapnr; /* RAM is assumed contiguous */
  343. totalram_pages += free_all_bootmem();
  344. #ifdef CONFIG_BLK_DEV_INITRD
  345. /* if we are booted from BootX with an initial ramdisk,
  346. make sure the ramdisk pages aren't reserved. */
  347. if (initrd_start) {
  348. for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
  349. ClearPageReserved(virt_to_page(addr));
  350. }
  351. #endif /* CONFIG_BLK_DEV_INITRD */
  352. for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
  353. addr += PAGE_SIZE) {
  354. if (!PageReserved(virt_to_page(addr)))
  355. continue;
  356. if (addr < (ulong) etext)
  357. codepages++;
  358. else if (addr >= (unsigned long)&__init_begin
  359. && addr < (unsigned long)&__init_end)
  360. initpages++;
  361. else if (addr < (ulong) klimit)
  362. datapages++;
  363. }
  364. #ifdef CONFIG_HIGHMEM
  365. {
  366. unsigned long pfn;
  367. for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
  368. struct page *page = mem_map + pfn;
  369. ClearPageReserved(page);
  370. init_page_count(page);
  371. __free_page(page);
  372. totalhigh_pages++;
  373. }
  374. totalram_pages += totalhigh_pages;
  375. }
  376. #endif /* CONFIG_HIGHMEM */
  377. printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
  378. (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
  379. codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
  380. initpages<< (PAGE_SHIFT-10),
  381. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
  382. mem_init_done = 1;
  383. }
  384. /*
  385. * Set phys_avail to the amount of physical memory,
  386. * less the kernel text/data/bss.
  387. */
  388. void __init
  389. set_phys_avail(unsigned long total_memory)
  390. {
  391. unsigned long kstart, ksize;
  392. /*
  393. * Initially, available physical memory is equivalent to all
  394. * physical memory.
  395. */
  396. phys_avail.regions[0].address = PPC_MEMSTART;
  397. phys_avail.regions[0].size = total_memory;
  398. phys_avail.n_regions = 1;
  399. /*
  400. * Map out the kernel text/data/bss from the available physical
  401. * memory.
  402. */
  403. kstart = __pa(_stext); /* should be 0 */
  404. ksize = PAGE_ALIGN(klimit - _stext);
  405. mem_pieces_remove(&phys_avail, kstart, ksize, 0);
  406. mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
  407. #if defined(CONFIG_BLK_DEV_INITRD)
  408. /* Remove the init RAM disk from the available memory. */
  409. if (initrd_start) {
  410. mem_pieces_remove(&phys_avail, __pa(initrd_start),
  411. initrd_end - initrd_start, 1);
  412. }
  413. #endif /* CONFIG_BLK_DEV_INITRD */
  414. }
  415. /* Mark some memory as reserved by removing it from phys_avail. */
  416. void __init reserve_phys_mem(unsigned long start, unsigned long size)
  417. {
  418. mem_pieces_remove(&phys_avail, start, size, 1);
  419. }
  420. /*
  421. * This is called when a page has been modified by the kernel.
  422. * It just marks the page as not i-cache clean. We do the i-cache
  423. * flush later when the page is given to a user process, if necessary.
  424. */
  425. void flush_dcache_page(struct page *page)
  426. {
  427. clear_bit(PG_arch_1, &page->flags);
  428. }
  429. void flush_dcache_icache_page(struct page *page)
  430. {
  431. #ifdef CONFIG_BOOKE
  432. void *start = kmap_atomic(page, KM_PPC_SYNC_ICACHE);
  433. __flush_dcache_icache(start);
  434. kunmap_atomic(start, KM_PPC_SYNC_ICACHE);
  435. #elif defined(CONFIG_8xx)
  436. /* On 8xx there is no need to kmap since highmem is not supported */
  437. __flush_dcache_icache(page_address(page));
  438. #else
  439. __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
  440. #endif
  441. }
  442. void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
  443. {
  444. clear_page(page);
  445. clear_bit(PG_arch_1, &pg->flags);
  446. }
  447. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  448. struct page *pg)
  449. {
  450. copy_page(vto, vfrom);
  451. clear_bit(PG_arch_1, &pg->flags);
  452. }
  453. void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  454. unsigned long addr, int len)
  455. {
  456. unsigned long maddr;
  457. maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
  458. flush_icache_range(maddr, maddr + len);
  459. kunmap(page);
  460. }
  461. /*
  462. * This is called at the end of handling a user page fault, when the
  463. * fault has been handled by updating a PTE in the linux page tables.
  464. * We use it to preload an HPTE into the hash table corresponding to
  465. * the updated linux PTE.
  466. */
  467. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
  468. pte_t pte)
  469. {
  470. /* handle i-cache coherency */
  471. unsigned long pfn = pte_pfn(pte);
  472. if (pfn_valid(pfn)) {
  473. struct page *page = pfn_to_page(pfn);
  474. #ifdef CONFIG_8xx
  475. /* On 8xx, the TLB handlers work in 2 stages:
  476. * First, a zeroed entry is loaded by TLBMiss handler,
  477. * which causes the TLBError handler to be triggered.
  478. * That means the zeroed TLB has to be invalidated
  479. * whenever a page miss occurs.
  480. */
  481. _tlbie(address, 0 /* 8xx doesn't care about PID */);
  482. #endif
  483. if (!PageReserved(page)
  484. && !test_bit(PG_arch_1, &page->flags)) {
  485. if (vma->vm_mm == current->active_mm)
  486. __flush_dcache_icache((void *) address);
  487. else
  488. flush_dcache_icache_page(page);
  489. set_bit(PG_arch_1, &page->flags);
  490. }
  491. }
  492. #ifdef CONFIG_PPC_STD_MMU
  493. /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
  494. if (Hash != 0 && pte_young(pte)) {
  495. struct mm_struct *mm;
  496. pmd_t *pmd;
  497. mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
  498. pmd = pmd_offset(pgd_offset(mm, address), address);
  499. if (!pmd_none(*pmd))
  500. add_hash_page(mm->context.id, address, pmd_val(*pmd));
  501. }
  502. #endif
  503. }
  504. /*
  505. * This is called by /dev/mem to know if a given address has to
  506. * be mapped non-cacheable or not
  507. */
  508. int page_is_ram(unsigned long pfn)
  509. {
  510. return pfn < max_pfn;
  511. }
  512. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  513. unsigned long size, pgprot_t vma_prot)
  514. {
  515. if (ppc_md.phys_mem_access_prot)
  516. return ppc_md.phys_mem_access_prot(file, pfn, size, vma_prot);
  517. if (!page_is_ram(pfn))
  518. vma_prot = __pgprot(pgprot_val(vma_prot)
  519. | _PAGE_GUARDED | _PAGE_NO_CACHE);
  520. return vma_prot;
  521. }
  522. EXPORT_SYMBOL(phys_mem_access_prot);