init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* $Id: init.c,v 1.103 2001/11/19 19:03:08 davem Exp $
  2. * linux/arch/sparc/mm/init.c
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  7. * Copyright (C) 2000 Anton Blanchard (anton@samba.org)
  8. */
  9. #include <linux/config.h>
  10. #include <linux/module.h>
  11. #include <linux/signal.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/mman.h>
  19. #include <linux/mm.h>
  20. #include <linux/swap.h>
  21. #include <linux/initrd.h>
  22. #include <linux/init.h>
  23. #include <linux/highmem.h>
  24. #include <linux/bootmem.h>
  25. #include <asm/system.h>
  26. #include <asm/vac-ops.h>
  27. #include <asm/page.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/vaddrs.h>
  30. #include <asm/pgalloc.h> /* bug in asm-generic/tlb.h: check_pgt_cache */
  31. #include <asm/tlb.h>
  32. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  33. unsigned long *sparc_valid_addr_bitmap;
  34. unsigned long phys_base;
  35. unsigned long pfn_base;
  36. unsigned long page_kernel;
  37. struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
  38. unsigned long sparc_unmapped_base;
  39. struct pgtable_cache_struct pgt_quicklists;
  40. /* References to section boundaries */
  41. extern char __init_begin, __init_end, _start, _end, etext , edata;
  42. /* Initial ramdisk setup */
  43. extern unsigned int sparc_ramdisk_image;
  44. extern unsigned int sparc_ramdisk_size;
  45. unsigned long highstart_pfn, highend_pfn;
  46. pte_t *kmap_pte;
  47. pgprot_t kmap_prot;
  48. #define kmap_get_fixmap_pte(vaddr) \
  49. pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
  50. void __init kmap_init(void)
  51. {
  52. /* cache the first kmap pte */
  53. kmap_pte = kmap_get_fixmap_pte(__fix_to_virt(FIX_KMAP_BEGIN));
  54. kmap_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE);
  55. }
  56. void show_mem(void)
  57. {
  58. printk("Mem-info:\n");
  59. show_free_areas();
  60. printk("Free swap: %6ldkB\n",
  61. nr_swap_pages << (PAGE_SHIFT-10));
  62. printk("%ld pages of RAM\n", totalram_pages);
  63. printk("%d free pages\n", nr_free_pages());
  64. #if 0 /* undefined pgtable_cache_size, pgd_cache_size */
  65. printk("%ld pages in page table cache\n",pgtable_cache_size);
  66. #ifndef CONFIG_SMP
  67. if (sparc_cpu_model == sun4m || sparc_cpu_model == sun4d)
  68. printk("%ld entries in page dir cache\n",pgd_cache_size);
  69. #endif
  70. #endif
  71. }
  72. void __init sparc_context_init(int numctx)
  73. {
  74. int ctx;
  75. ctx_list_pool = __alloc_bootmem(numctx * sizeof(struct ctx_list), SMP_CACHE_BYTES, 0UL);
  76. for(ctx = 0; ctx < numctx; ctx++) {
  77. struct ctx_list *clist;
  78. clist = (ctx_list_pool + ctx);
  79. clist->ctx_number = ctx;
  80. clist->ctx_mm = NULL;
  81. }
  82. ctx_free.next = ctx_free.prev = &ctx_free;
  83. ctx_used.next = ctx_used.prev = &ctx_used;
  84. for(ctx = 0; ctx < numctx; ctx++)
  85. add_to_free_ctxlist(ctx_list_pool + ctx);
  86. }
  87. extern unsigned long cmdline_memory_size;
  88. unsigned long last_valid_pfn;
  89. unsigned long calc_highpages(void)
  90. {
  91. int i;
  92. int nr = 0;
  93. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  94. unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  95. unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  96. if (end_pfn <= max_low_pfn)
  97. continue;
  98. if (start_pfn < max_low_pfn)
  99. start_pfn = max_low_pfn;
  100. nr += end_pfn - start_pfn;
  101. }
  102. return nr;
  103. }
  104. unsigned long calc_max_low_pfn(void)
  105. {
  106. int i;
  107. unsigned long tmp = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
  108. unsigned long curr_pfn, last_pfn;
  109. last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
  110. for (i = 1; sp_banks[i].num_bytes != 0; i++) {
  111. curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  112. if (curr_pfn >= tmp) {
  113. if (last_pfn < tmp)
  114. tmp = last_pfn;
  115. break;
  116. }
  117. last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  118. }
  119. return tmp;
  120. }
  121. unsigned long __init bootmem_init(unsigned long *pages_avail)
  122. {
  123. unsigned long bootmap_size, start_pfn;
  124. unsigned long end_of_phys_memory = 0UL;
  125. unsigned long bootmap_pfn, bytes_avail, size;
  126. int i;
  127. bytes_avail = 0UL;
  128. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  129. end_of_phys_memory = sp_banks[i].base_addr +
  130. sp_banks[i].num_bytes;
  131. bytes_avail += sp_banks[i].num_bytes;
  132. if (cmdline_memory_size) {
  133. if (bytes_avail > cmdline_memory_size) {
  134. unsigned long slack = bytes_avail - cmdline_memory_size;
  135. bytes_avail -= slack;
  136. end_of_phys_memory -= slack;
  137. sp_banks[i].num_bytes -= slack;
  138. if (sp_banks[i].num_bytes == 0) {
  139. sp_banks[i].base_addr = 0xdeadbeef;
  140. } else {
  141. sp_banks[i+1].num_bytes = 0;
  142. sp_banks[i+1].base_addr = 0xdeadbeef;
  143. }
  144. break;
  145. }
  146. }
  147. }
  148. /* Start with page aligned address of last symbol in kernel
  149. * image.
  150. */
  151. start_pfn = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
  152. /* Now shift down to get the real physical page frame number. */
  153. start_pfn >>= PAGE_SHIFT;
  154. bootmap_pfn = start_pfn;
  155. max_pfn = end_of_phys_memory >> PAGE_SHIFT;
  156. max_low_pfn = max_pfn;
  157. highstart_pfn = highend_pfn = max_pfn;
  158. if (max_low_pfn > pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT)) {
  159. highstart_pfn = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
  160. max_low_pfn = calc_max_low_pfn();
  161. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  162. calc_highpages() >> (20 - PAGE_SHIFT));
  163. }
  164. #ifdef CONFIG_BLK_DEV_INITRD
  165. /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
  166. if (sparc_ramdisk_image) {
  167. if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
  168. sparc_ramdisk_image -= KERNBASE;
  169. initrd_start = sparc_ramdisk_image + phys_base;
  170. initrd_end = initrd_start + sparc_ramdisk_size;
  171. if (initrd_end > end_of_phys_memory) {
  172. printk(KERN_CRIT "initrd extends beyond end of memory "
  173. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  174. initrd_end, end_of_phys_memory);
  175. initrd_start = 0;
  176. }
  177. if (initrd_start) {
  178. if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
  179. initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
  180. bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
  181. }
  182. }
  183. #endif
  184. /* Initialize the boot-time allocator. */
  185. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base,
  186. max_low_pfn);
  187. /* Now register the available physical memory with the
  188. * allocator.
  189. */
  190. *pages_avail = 0;
  191. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  192. unsigned long curr_pfn, last_pfn;
  193. curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  194. if (curr_pfn >= max_low_pfn)
  195. break;
  196. last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  197. if (last_pfn > max_low_pfn)
  198. last_pfn = max_low_pfn;
  199. /*
  200. * .. finally, did all the rounding and playing
  201. * around just make the area go away?
  202. */
  203. if (last_pfn <= curr_pfn)
  204. continue;
  205. size = (last_pfn - curr_pfn) << PAGE_SHIFT;
  206. *pages_avail += last_pfn - curr_pfn;
  207. free_bootmem(sp_banks[i].base_addr, size);
  208. }
  209. #ifdef CONFIG_BLK_DEV_INITRD
  210. if (initrd_start) {
  211. /* Reserve the initrd image area. */
  212. size = initrd_end - initrd_start;
  213. reserve_bootmem(initrd_start, size);
  214. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  215. initrd_start = (initrd_start - phys_base) + PAGE_OFFSET;
  216. initrd_end = (initrd_end - phys_base) + PAGE_OFFSET;
  217. }
  218. #endif
  219. /* Reserve the kernel text/data/bss. */
  220. size = (start_pfn << PAGE_SHIFT) - phys_base;
  221. reserve_bootmem(phys_base, size);
  222. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  223. /* Reserve the bootmem map. We do not account for it
  224. * in pages_avail because we will release that memory
  225. * in free_all_bootmem.
  226. */
  227. size = bootmap_size;
  228. reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
  229. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  230. return max_pfn;
  231. }
  232. /*
  233. * check_pgt_cache
  234. *
  235. * This is called at the end of unmapping of VMA (zap_page_range),
  236. * to rescan the page cache for architecture specific things,
  237. * presumably something like sun4/sun4c PMEGs. Most architectures
  238. * define check_pgt_cache empty.
  239. *
  240. * We simply copy the 2.4 implementation for now.
  241. */
  242. int pgt_cache_water[2] = { 25, 50 };
  243. void check_pgt_cache(void)
  244. {
  245. do_check_pgt_cache(pgt_cache_water[0], pgt_cache_water[1]);
  246. }
  247. /*
  248. * paging_init() sets up the page tables: We call the MMU specific
  249. * init routine based upon the Sun model type on the Sparc.
  250. *
  251. */
  252. extern void sun4c_paging_init(void);
  253. extern void srmmu_paging_init(void);
  254. extern void device_scan(void);
  255. void __init paging_init(void)
  256. {
  257. switch(sparc_cpu_model) {
  258. case sun4c:
  259. case sun4e:
  260. case sun4:
  261. sun4c_paging_init();
  262. sparc_unmapped_base = 0xe0000000;
  263. BTFIXUPSET_SETHI(sparc_unmapped_base, 0xe0000000);
  264. break;
  265. case sun4m:
  266. case sun4d:
  267. srmmu_paging_init();
  268. sparc_unmapped_base = 0x50000000;
  269. BTFIXUPSET_SETHI(sparc_unmapped_base, 0x50000000);
  270. break;
  271. default:
  272. prom_printf("paging_init: Cannot init paging on this Sparc\n");
  273. prom_printf("paging_init: sparc_cpu_model = %d\n", sparc_cpu_model);
  274. prom_printf("paging_init: Halting...\n");
  275. prom_halt();
  276. };
  277. /* Initialize the protection map with non-constant, MMU dependent values. */
  278. protection_map[0] = PAGE_NONE;
  279. protection_map[1] = PAGE_READONLY;
  280. protection_map[2] = PAGE_COPY;
  281. protection_map[3] = PAGE_COPY;
  282. protection_map[4] = PAGE_READONLY;
  283. protection_map[5] = PAGE_READONLY;
  284. protection_map[6] = PAGE_COPY;
  285. protection_map[7] = PAGE_COPY;
  286. protection_map[8] = PAGE_NONE;
  287. protection_map[9] = PAGE_READONLY;
  288. protection_map[10] = PAGE_SHARED;
  289. protection_map[11] = PAGE_SHARED;
  290. protection_map[12] = PAGE_READONLY;
  291. protection_map[13] = PAGE_READONLY;
  292. protection_map[14] = PAGE_SHARED;
  293. protection_map[15] = PAGE_SHARED;
  294. btfixup();
  295. device_scan();
  296. }
  297. struct cache_palias *sparc_aliases;
  298. static void __init taint_real_pages(void)
  299. {
  300. int i;
  301. for (i = 0; sp_banks[i].num_bytes; i++) {
  302. unsigned long start, end;
  303. start = sp_banks[i].base_addr;
  304. end = start + sp_banks[i].num_bytes;
  305. while (start < end) {
  306. set_bit(start >> 20, sparc_valid_addr_bitmap);
  307. start += PAGE_SIZE;
  308. }
  309. }
  310. }
  311. void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
  312. {
  313. unsigned long tmp;
  314. #ifdef CONFIG_DEBUG_HIGHMEM
  315. printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
  316. #endif
  317. for (tmp = start_pfn; tmp < end_pfn; tmp++) {
  318. struct page *page = pfn_to_page(tmp);
  319. ClearPageReserved(page);
  320. set_page_count(page, 1);
  321. __free_page(page);
  322. totalhigh_pages++;
  323. }
  324. }
  325. void __init mem_init(void)
  326. {
  327. int codepages = 0;
  328. int datapages = 0;
  329. int initpages = 0;
  330. int reservedpages = 0;
  331. int i;
  332. if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
  333. prom_printf("BUG: fixmap and pkmap areas overlap\n");
  334. prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
  335. PKMAP_BASE,
  336. (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  337. FIXADDR_START);
  338. prom_printf("Please mail sparclinux@vger.kernel.org.\n");
  339. prom_halt();
  340. }
  341. /* Saves us work later. */
  342. memset((void *)&empty_zero_page, 0, PAGE_SIZE);
  343. i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
  344. i += 1;
  345. sparc_valid_addr_bitmap = (unsigned long *)
  346. __alloc_bootmem(i << 2, SMP_CACHE_BYTES, 0UL);
  347. if (sparc_valid_addr_bitmap == NULL) {
  348. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  349. prom_halt();
  350. }
  351. memset(sparc_valid_addr_bitmap, 0, i << 2);
  352. taint_real_pages();
  353. max_mapnr = last_valid_pfn - pfn_base;
  354. high_memory = __va(max_low_pfn << PAGE_SHIFT);
  355. totalram_pages = free_all_bootmem();
  356. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  357. unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  358. unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  359. num_physpages += sp_banks[i].num_bytes >> PAGE_SHIFT;
  360. if (end_pfn <= highstart_pfn)
  361. continue;
  362. if (start_pfn < highstart_pfn)
  363. start_pfn = highstart_pfn;
  364. map_high_region(start_pfn, end_pfn);
  365. }
  366. totalram_pages += totalhigh_pages;
  367. codepages = (((unsigned long) &etext) - ((unsigned long)&_start));
  368. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  369. datapages = (((unsigned long) &edata) - ((unsigned long)&etext));
  370. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  371. initpages = (((unsigned long) &__init_end) - ((unsigned long) &__init_begin));
  372. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  373. /* Ignore memory holes for the purpose of counting reserved pages */
  374. for (i=0; i < max_low_pfn; i++)
  375. if (test_bit(i >> (20 - PAGE_SHIFT), sparc_valid_addr_bitmap)
  376. && PageReserved(pfn_to_page(i)))
  377. reservedpages++;
  378. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
  379. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  380. num_physpages << (PAGE_SHIFT - 10),
  381. codepages << (PAGE_SHIFT-10),
  382. reservedpages << (PAGE_SHIFT - 10),
  383. datapages << (PAGE_SHIFT-10),
  384. initpages << (PAGE_SHIFT-10),
  385. totalhigh_pages << (PAGE_SHIFT-10));
  386. }
  387. void free_initmem (void)
  388. {
  389. unsigned long addr;
  390. addr = (unsigned long)(&__init_begin);
  391. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  392. struct page *p;
  393. p = virt_to_page(addr);
  394. ClearPageReserved(p);
  395. set_page_count(p, 1);
  396. __free_page(p);
  397. totalram_pages++;
  398. num_physpages++;
  399. }
  400. printk (KERN_INFO "Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
  401. }
  402. #ifdef CONFIG_BLK_DEV_INITRD
  403. void free_initrd_mem(unsigned long start, unsigned long end)
  404. {
  405. if (start < end)
  406. printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  407. for (; start < end; start += PAGE_SIZE) {
  408. struct page *p = virt_to_page(start);
  409. ClearPageReserved(p);
  410. set_page_count(p, 1);
  411. __free_page(p);
  412. num_physpages++;
  413. }
  414. }
  415. #endif
  416. void sparc_flush_page_to_ram(struct page *page)
  417. {
  418. unsigned long vaddr = (unsigned long)page_address(page);
  419. if (vaddr)
  420. __flush_page_to_ram(vaddr);
  421. }