init.c 14 KB

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