init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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/config.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/string.h>
  26. #include <linux/types.h>
  27. #include <linux/mm.h>
  28. #include <linux/stddef.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/highmem.h>
  32. #include <linux/initrd.h>
  33. #include <linux/pagemap.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/prom.h>
  36. #include <asm/io.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/mmu.h>
  40. #include <asm/smp.h>
  41. #include <asm/machdep.h>
  42. #include <asm/btext.h>
  43. #include <asm/tlb.h>
  44. #include <asm/bootinfo.h>
  45. #include <asm/prom.h>
  46. #include "mem_pieces.h"
  47. #include "mmu_decl.h"
  48. #if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
  49. /* The ammount of lowmem must be within 0xF0000000 - KERNELBASE. */
  50. #if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
  51. #error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
  52. #endif
  53. #endif
  54. #define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
  55. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  56. unsigned long total_memory;
  57. unsigned long total_lowmem;
  58. unsigned long ppc_memstart;
  59. unsigned long ppc_memoffset = PAGE_OFFSET;
  60. int mem_init_done;
  61. int init_bootmem_done;
  62. int boot_mapsize;
  63. #ifdef CONFIG_PPC_PMAC
  64. unsigned long agp_special_page;
  65. #endif
  66. extern char _end[];
  67. extern char etext[], _stext[];
  68. extern char __init_begin, __init_end;
  69. #ifdef CONFIG_HIGHMEM
  70. pte_t *kmap_pte;
  71. pgprot_t kmap_prot;
  72. EXPORT_SYMBOL(kmap_prot);
  73. EXPORT_SYMBOL(kmap_pte);
  74. #endif
  75. void MMU_init(void);
  76. void set_phys_avail(unsigned long total_ram);
  77. /* XXX should be in current.h -- paulus */
  78. extern struct task_struct *current_set[NR_CPUS];
  79. char *klimit = _end;
  80. struct mem_pieces phys_avail;
  81. struct device_node *memory_node;
  82. /*
  83. * this tells the system to map all of ram with the segregs
  84. * (i.e. page tables) instead of the bats.
  85. * -- Cort
  86. */
  87. int __map_without_bats;
  88. int __map_without_ltlbs;
  89. /* max amount of RAM to use */
  90. unsigned long __max_memory;
  91. /* max amount of low RAM to map in */
  92. unsigned long __max_low_memory = MAX_LOW_MEM;
  93. /*
  94. * Read in a property describing some pieces of memory.
  95. */
  96. static int __init get_mem_prop(char *name, struct mem_pieces *mp)
  97. {
  98. struct reg_property *rp;
  99. int i, s;
  100. unsigned int *ip;
  101. int nac = prom_n_addr_cells(memory_node);
  102. int nsc = prom_n_size_cells(memory_node);
  103. ip = (unsigned int *) get_property(memory_node, name, &s);
  104. if (ip == NULL) {
  105. printk(KERN_ERR "error: couldn't get %s property on /memory\n",
  106. name);
  107. return 0;
  108. }
  109. s /= (nsc + nac) * 4;
  110. rp = mp->regions;
  111. for (i = 0; i < s; ++i, ip += nac+nsc) {
  112. if (nac >= 2 && ip[nac-2] != 0)
  113. continue;
  114. rp->address = ip[nac-1];
  115. if (nsc >= 2 && ip[nac+nsc-2] != 0)
  116. rp->size = ~0U;
  117. else
  118. rp->size = ip[nac+nsc-1];
  119. ++rp;
  120. }
  121. mp->n_regions = rp - mp->regions;
  122. /* Make sure the pieces are sorted. */
  123. mem_pieces_sort(mp);
  124. mem_pieces_coalesce(mp);
  125. return 1;
  126. }
  127. /*
  128. * Collect information about physical RAM and which pieces are
  129. * already in use from the device tree.
  130. */
  131. unsigned long __init find_end_of_memory(void)
  132. {
  133. unsigned long a, total;
  134. struct mem_pieces phys_mem;
  135. /*
  136. * Find out where physical memory is, and check that it
  137. * starts at 0 and is contiguous. It seems that RAM is
  138. * always physically contiguous on Power Macintoshes.
  139. *
  140. * Supporting discontiguous physical memory isn't hard,
  141. * it just makes the virtual <-> physical mapping functions
  142. * more complicated (or else you end up wasting space
  143. * in mem_map).
  144. */
  145. memory_node = find_devices("memory");
  146. if (memory_node == NULL || !get_mem_prop("reg", &phys_mem)
  147. || phys_mem.n_regions == 0)
  148. panic("No RAM??");
  149. a = phys_mem.regions[0].address;
  150. if (a != 0)
  151. panic("RAM doesn't start at physical address 0");
  152. total = phys_mem.regions[0].size;
  153. if (phys_mem.n_regions > 1) {
  154. printk("RAM starting at 0x%x is not contiguous\n",
  155. phys_mem.regions[1].address);
  156. printk("Using RAM from 0 to 0x%lx\n", total-1);
  157. }
  158. return total;
  159. }
  160. /*
  161. * Check for command-line options that affect what MMU_init will do.
  162. */
  163. void MMU_setup(void)
  164. {
  165. /* Check for nobats option (used in mapin_ram). */
  166. if (strstr(cmd_line, "nobats")) {
  167. __map_without_bats = 1;
  168. }
  169. if (strstr(cmd_line, "noltlbs")) {
  170. __map_without_ltlbs = 1;
  171. }
  172. /* Look for mem= option on command line */
  173. if (strstr(cmd_line, "mem=")) {
  174. char *p, *q;
  175. unsigned long maxmem = 0;
  176. for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
  177. q = p + 4;
  178. if (p > cmd_line && p[-1] != ' ')
  179. continue;
  180. maxmem = simple_strtoul(q, &q, 0);
  181. if (*q == 'k' || *q == 'K') {
  182. maxmem <<= 10;
  183. ++q;
  184. } else if (*q == 'm' || *q == 'M') {
  185. maxmem <<= 20;
  186. ++q;
  187. }
  188. }
  189. __max_memory = maxmem;
  190. }
  191. }
  192. /*
  193. * MMU_init sets up the basic memory mappings for the kernel,
  194. * including both RAM and possibly some I/O regions,
  195. * and sets up the page tables and the MMU hardware ready to go.
  196. */
  197. void __init MMU_init(void)
  198. {
  199. if (ppc_md.progress)
  200. ppc_md.progress("MMU:enter", 0x111);
  201. /* parse args from command line */
  202. MMU_setup();
  203. /*
  204. * Figure out how much memory we have, how much
  205. * is lowmem, and how much is highmem. If we were
  206. * passed the total memory size from the bootloader,
  207. * just use it.
  208. */
  209. if (boot_mem_size)
  210. total_memory = boot_mem_size;
  211. else
  212. total_memory = ppc_md.find_end_of_memory();
  213. if (__max_memory && total_memory > __max_memory)
  214. total_memory = __max_memory;
  215. total_lowmem = total_memory;
  216. #ifdef CONFIG_FSL_BOOKE
  217. /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
  218. * entries, so we need to adjust lowmem to match the amount we can map
  219. * in the fixed entries */
  220. adjust_total_lowmem();
  221. #endif /* CONFIG_FSL_BOOKE */
  222. if (total_lowmem > __max_low_memory) {
  223. total_lowmem = __max_low_memory;
  224. #ifndef CONFIG_HIGHMEM
  225. total_memory = total_lowmem;
  226. #endif /* CONFIG_HIGHMEM */
  227. }
  228. set_phys_avail(total_lowmem);
  229. /* Initialize the MMU hardware */
  230. if (ppc_md.progress)
  231. ppc_md.progress("MMU:hw init", 0x300);
  232. MMU_init_hw();
  233. /* Map in all of RAM starting at KERNELBASE */
  234. if (ppc_md.progress)
  235. ppc_md.progress("MMU:mapin", 0x301);
  236. mapin_ram();
  237. #ifdef CONFIG_HIGHMEM
  238. ioremap_base = PKMAP_BASE;
  239. #else
  240. ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
  241. #endif /* CONFIG_HIGHMEM */
  242. ioremap_bot = ioremap_base;
  243. /* Map in I/O resources */
  244. if (ppc_md.progress)
  245. ppc_md.progress("MMU:setio", 0x302);
  246. if (ppc_md.setup_io_mappings)
  247. ppc_md.setup_io_mappings();
  248. /* Initialize the context management stuff */
  249. mmu_context_init();
  250. if (ppc_md.progress)
  251. ppc_md.progress("MMU:exit", 0x211);
  252. #ifdef CONFIG_BOOTX_TEXT
  253. /* By default, we are no longer mapped */
  254. boot_text_mapped = 0;
  255. /* Must be done last, or ppc_md.progress will die. */
  256. map_boot_text();
  257. #endif
  258. }
  259. /* This is only called until mem_init is done. */
  260. void __init *early_get_page(void)
  261. {
  262. void *p;
  263. if (init_bootmem_done) {
  264. p = alloc_bootmem_pages(PAGE_SIZE);
  265. } else {
  266. p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
  267. }
  268. return p;
  269. }
  270. /* Free up now-unused memory */
  271. static void free_sec(unsigned long start, unsigned long end, const char *name)
  272. {
  273. unsigned long cnt = 0;
  274. while (start < end) {
  275. ClearPageReserved(virt_to_page(start));
  276. set_page_count(virt_to_page(start), 1);
  277. free_page(start);
  278. cnt++;
  279. start += PAGE_SIZE;
  280. }
  281. if (cnt) {
  282. printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
  283. totalram_pages += cnt;
  284. }
  285. }
  286. void free_initmem(void)
  287. {
  288. #define FREESEC(TYPE) \
  289. free_sec((unsigned long)(&__ ## TYPE ## _begin), \
  290. (unsigned long)(&__ ## TYPE ## _end), \
  291. #TYPE);
  292. printk ("Freeing unused kernel memory:");
  293. FREESEC(init);
  294. printk("\n");
  295. ppc_md.progress = NULL;
  296. #undef FREESEC
  297. }
  298. #ifdef CONFIG_BLK_DEV_INITRD
  299. void free_initrd_mem(unsigned long start, unsigned long end)
  300. {
  301. if (start < end)
  302. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  303. for (; start < end; start += PAGE_SIZE) {
  304. ClearPageReserved(virt_to_page(start));
  305. set_page_count(virt_to_page(start), 1);
  306. free_page(start);
  307. totalram_pages++;
  308. }
  309. }
  310. #endif
  311. /*
  312. * Initialize the bootmem system and give it all the memory we
  313. * have available.
  314. */
  315. void __init do_init_bootmem(void)
  316. {
  317. unsigned long start, size;
  318. int i;
  319. /*
  320. * Find an area to use for the bootmem bitmap.
  321. * We look for the first area which is at least
  322. * 128kB in length (128kB is enough for a bitmap
  323. * for 4GB of memory, using 4kB pages), plus 1 page
  324. * (in case the address isn't page-aligned).
  325. */
  326. start = 0;
  327. size = 0;
  328. for (i = 0; i < phys_avail.n_regions; ++i) {
  329. unsigned long a = phys_avail.regions[i].address;
  330. unsigned long s = phys_avail.regions[i].size;
  331. if (s <= size)
  332. continue;
  333. start = a;
  334. size = s;
  335. if (s >= 33 * PAGE_SIZE)
  336. break;
  337. }
  338. start = PAGE_ALIGN(start);
  339. min_low_pfn = start >> PAGE_SHIFT;
  340. max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
  341. max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
  342. boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
  343. PPC_MEMSTART >> PAGE_SHIFT,
  344. max_low_pfn);
  345. /* remove the bootmem bitmap from the available memory */
  346. mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
  347. /* add everything in phys_avail into the bootmem map */
  348. for (i = 0; i < phys_avail.n_regions; ++i)
  349. free_bootmem(phys_avail.regions[i].address,
  350. phys_avail.regions[i].size);
  351. init_bootmem_done = 1;
  352. }
  353. /*
  354. * paging_init() sets up the page tables - in fact we've already done this.
  355. */
  356. void __init paging_init(void)
  357. {
  358. unsigned long zones_size[MAX_NR_ZONES], i;
  359. #ifdef CONFIG_HIGHMEM
  360. map_page(PKMAP_BASE, 0, 0); /* XXX gross */
  361. pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
  362. (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
  363. map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
  364. kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
  365. (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
  366. kmap_prot = PAGE_KERNEL;
  367. #endif /* CONFIG_HIGHMEM */
  368. /*
  369. * All pages are DMA-able so we put them all in the DMA zone.
  370. */
  371. zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
  372. for (i = 1; i < MAX_NR_ZONES; i++)
  373. zones_size[i] = 0;
  374. #ifdef CONFIG_HIGHMEM
  375. zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
  376. #endif /* CONFIG_HIGHMEM */
  377. free_area_init(zones_size);
  378. }
  379. void __init mem_init(void)
  380. {
  381. unsigned long addr;
  382. int codepages = 0;
  383. int datapages = 0;
  384. int initpages = 0;
  385. #ifdef CONFIG_HIGHMEM
  386. unsigned long highmem_mapnr;
  387. highmem_mapnr = total_lowmem >> PAGE_SHIFT;
  388. #endif /* CONFIG_HIGHMEM */
  389. max_mapnr = total_memory >> PAGE_SHIFT;
  390. high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
  391. num_physpages = max_mapnr; /* RAM is assumed contiguous */
  392. totalram_pages += free_all_bootmem();
  393. #ifdef CONFIG_BLK_DEV_INITRD
  394. /* if we are booted from BootX with an initial ramdisk,
  395. make sure the ramdisk pages aren't reserved. */
  396. if (initrd_start) {
  397. for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
  398. ClearPageReserved(virt_to_page(addr));
  399. }
  400. #endif /* CONFIG_BLK_DEV_INITRD */
  401. #ifdef CONFIG_PPC_OF
  402. /* mark the RTAS pages as reserved */
  403. if ( rtas_data )
  404. for (addr = (ulong)__va(rtas_data);
  405. addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
  406. addr += PAGE_SIZE)
  407. SetPageReserved(virt_to_page(addr));
  408. #endif
  409. #ifdef CONFIG_PPC_PMAC
  410. if (agp_special_page)
  411. SetPageReserved(virt_to_page(agp_special_page));
  412. #endif
  413. for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
  414. addr += PAGE_SIZE) {
  415. if (!PageReserved(virt_to_page(addr)))
  416. continue;
  417. if (addr < (ulong) etext)
  418. codepages++;
  419. else if (addr >= (unsigned long)&__init_begin
  420. && addr < (unsigned long)&__init_end)
  421. initpages++;
  422. else if (addr < (ulong) klimit)
  423. datapages++;
  424. }
  425. #ifdef CONFIG_HIGHMEM
  426. {
  427. unsigned long pfn;
  428. for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
  429. struct page *page = mem_map + pfn;
  430. ClearPageReserved(page);
  431. set_page_count(page, 1);
  432. __free_page(page);
  433. totalhigh_pages++;
  434. }
  435. totalram_pages += totalhigh_pages;
  436. }
  437. #endif /* CONFIG_HIGHMEM */
  438. printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
  439. (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
  440. codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
  441. initpages<< (PAGE_SHIFT-10),
  442. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
  443. #ifdef CONFIG_PPC_PMAC
  444. if (agp_special_page)
  445. printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
  446. #endif
  447. mem_init_done = 1;
  448. }
  449. /*
  450. * Set phys_avail to the amount of physical memory,
  451. * less the kernel text/data/bss.
  452. */
  453. void __init
  454. set_phys_avail(unsigned long total_memory)
  455. {
  456. unsigned long kstart, ksize;
  457. /*
  458. * Initially, available physical memory is equivalent to all
  459. * physical memory.
  460. */
  461. phys_avail.regions[0].address = PPC_MEMSTART;
  462. phys_avail.regions[0].size = total_memory;
  463. phys_avail.n_regions = 1;
  464. /*
  465. * Map out the kernel text/data/bss from the available physical
  466. * memory.
  467. */
  468. kstart = __pa(_stext); /* should be 0 */
  469. ksize = PAGE_ALIGN(klimit - _stext);
  470. mem_pieces_remove(&phys_avail, kstart, ksize, 0);
  471. mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
  472. #if defined(CONFIG_BLK_DEV_INITRD)
  473. /* Remove the init RAM disk from the available memory. */
  474. if (initrd_start) {
  475. mem_pieces_remove(&phys_avail, __pa(initrd_start),
  476. initrd_end - initrd_start, 1);
  477. }
  478. #endif /* CONFIG_BLK_DEV_INITRD */
  479. #ifdef CONFIG_PPC_OF
  480. /* remove the RTAS pages from the available memory */
  481. if (rtas_data)
  482. mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
  483. #endif
  484. #ifdef CONFIG_PPC_PMAC
  485. /* Because of some uninorth weirdness, we need a page of
  486. * memory as high as possible (it must be outside of the
  487. * bus address seen as the AGP aperture). It will be used
  488. * by the r128 DRM driver
  489. *
  490. * FIXME: We need to make sure that page doesn't overlap any of the\
  491. * above. This could be done by improving mem_pieces_find to be able
  492. * to do a backward search from the end of the list.
  493. */
  494. if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
  495. agp_special_page = (total_memory - PAGE_SIZE);
  496. mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
  497. agp_special_page = (unsigned long)__va(agp_special_page);
  498. }
  499. #endif /* CONFIG_PPC_PMAC */
  500. }
  501. /* Mark some memory as reserved by removing it from phys_avail. */
  502. void __init reserve_phys_mem(unsigned long start, unsigned long size)
  503. {
  504. mem_pieces_remove(&phys_avail, start, size, 1);
  505. }