init_32.c 14 KB

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