init_32.c 14 KB

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