init_32.c 14 KB

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