init_32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <asm/sections.h>
  27. #include <asm/system.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. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  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(void)
  62. {
  63. printk("Mem-info:\n");
  64. show_free_areas();
  65. printk("Free swap: %6ldkB\n",
  66. 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 sun4m:
  273. case sun4d:
  274. srmmu_paging_init();
  275. sparc_unmapped_base = 0x50000000;
  276. BTFIXUPSET_SETHI(sparc_unmapped_base, 0x50000000);
  277. break;
  278. default:
  279. prom_printf("paging_init: Cannot init paging on this Sparc\n");
  280. prom_printf("paging_init: sparc_cpu_model = %d\n", sparc_cpu_model);
  281. prom_printf("paging_init: Halting...\n");
  282. prom_halt();
  283. };
  284. /* Initialize the protection map with non-constant, MMU dependent values. */
  285. protection_map[0] = PAGE_NONE;
  286. protection_map[1] = PAGE_READONLY;
  287. protection_map[2] = PAGE_COPY;
  288. protection_map[3] = PAGE_COPY;
  289. protection_map[4] = PAGE_READONLY;
  290. protection_map[5] = PAGE_READONLY;
  291. protection_map[6] = PAGE_COPY;
  292. protection_map[7] = PAGE_COPY;
  293. protection_map[8] = PAGE_NONE;
  294. protection_map[9] = PAGE_READONLY;
  295. protection_map[10] = PAGE_SHARED;
  296. protection_map[11] = PAGE_SHARED;
  297. protection_map[12] = PAGE_READONLY;
  298. protection_map[13] = PAGE_READONLY;
  299. protection_map[14] = PAGE_SHARED;
  300. protection_map[15] = PAGE_SHARED;
  301. btfixup();
  302. prom_build_devicetree();
  303. of_fill_in_cpu_data();
  304. device_scan();
  305. }
  306. static void __init taint_real_pages(void)
  307. {
  308. int i;
  309. for (i = 0; sp_banks[i].num_bytes; i++) {
  310. unsigned long start, end;
  311. start = sp_banks[i].base_addr;
  312. end = start + sp_banks[i].num_bytes;
  313. while (start < end) {
  314. set_bit(start >> 20, sparc_valid_addr_bitmap);
  315. start += PAGE_SIZE;
  316. }
  317. }
  318. }
  319. static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
  320. {
  321. unsigned long tmp;
  322. #ifdef CONFIG_DEBUG_HIGHMEM
  323. printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
  324. #endif
  325. for (tmp = start_pfn; tmp < end_pfn; tmp++) {
  326. struct page *page = pfn_to_page(tmp);
  327. ClearPageReserved(page);
  328. init_page_count(page);
  329. __free_page(page);
  330. totalhigh_pages++;
  331. }
  332. }
  333. void __init mem_init(void)
  334. {
  335. int codepages = 0;
  336. int datapages = 0;
  337. int initpages = 0;
  338. int reservedpages = 0;
  339. int i;
  340. if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
  341. prom_printf("BUG: fixmap and pkmap areas overlap\n");
  342. prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
  343. PKMAP_BASE,
  344. (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  345. FIXADDR_START);
  346. prom_printf("Please mail sparclinux@vger.kernel.org.\n");
  347. prom_halt();
  348. }
  349. /* Saves us work later. */
  350. memset((void *)&empty_zero_page, 0, PAGE_SIZE);
  351. i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
  352. i += 1;
  353. sparc_valid_addr_bitmap = (unsigned long *)
  354. __alloc_bootmem(i << 2, SMP_CACHE_BYTES, 0UL);
  355. if (sparc_valid_addr_bitmap == NULL) {
  356. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  357. prom_halt();
  358. }
  359. memset(sparc_valid_addr_bitmap, 0, i << 2);
  360. taint_real_pages();
  361. max_mapnr = last_valid_pfn - pfn_base;
  362. high_memory = __va(max_low_pfn << PAGE_SHIFT);
  363. totalram_pages = free_all_bootmem();
  364. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  365. unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  366. unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  367. num_physpages += sp_banks[i].num_bytes >> PAGE_SHIFT;
  368. if (end_pfn <= highstart_pfn)
  369. continue;
  370. if (start_pfn < highstart_pfn)
  371. start_pfn = highstart_pfn;
  372. map_high_region(start_pfn, end_pfn);
  373. }
  374. totalram_pages += totalhigh_pages;
  375. codepages = (((unsigned long) &_etext) - ((unsigned long)&_start));
  376. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  377. datapages = (((unsigned long) &_edata) - ((unsigned long)&_etext));
  378. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  379. initpages = (((unsigned long) &__init_end) - ((unsigned long) &__init_begin));
  380. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  381. /* Ignore memory holes for the purpose of counting reserved pages */
  382. for (i=0; i < max_low_pfn; i++)
  383. if (test_bit(i >> (20 - PAGE_SHIFT), sparc_valid_addr_bitmap)
  384. && PageReserved(pfn_to_page(i)))
  385. reservedpages++;
  386. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
  387. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  388. num_physpages << (PAGE_SHIFT - 10),
  389. codepages << (PAGE_SHIFT-10),
  390. reservedpages << (PAGE_SHIFT - 10),
  391. datapages << (PAGE_SHIFT-10),
  392. initpages << (PAGE_SHIFT-10),
  393. totalhigh_pages << (PAGE_SHIFT-10));
  394. }
  395. void free_initmem (void)
  396. {
  397. unsigned long addr;
  398. unsigned long freed;
  399. addr = (unsigned long)(&__init_begin);
  400. freed = (unsigned long)(&__init_end) - addr;
  401. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  402. struct page *p;
  403. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  404. p = virt_to_page(addr);
  405. ClearPageReserved(p);
  406. init_page_count(p);
  407. __free_page(p);
  408. totalram_pages++;
  409. num_physpages++;
  410. }
  411. printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n",
  412. freed >> 10);
  413. }
  414. #ifdef CONFIG_BLK_DEV_INITRD
  415. void free_initrd_mem(unsigned long start, unsigned long end)
  416. {
  417. if (start < end)
  418. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
  419. (end - start) >> 10);
  420. for (; start < end; start += PAGE_SIZE) {
  421. struct page *p;
  422. memset((void *)start, POISON_FREE_INITMEM, PAGE_SIZE);
  423. p = virt_to_page(start);
  424. ClearPageReserved(p);
  425. init_page_count(p);
  426. __free_page(p);
  427. totalram_pages++;
  428. num_physpages++;
  429. }
  430. }
  431. #endif
  432. void sparc_flush_page_to_ram(struct page *page)
  433. {
  434. unsigned long vaddr = (unsigned long)page_address(page);
  435. if (vaddr)
  436. __flush_page_to_ram(vaddr);
  437. }
  438. EXPORT_SYMBOL(sparc_flush_page_to_ram);