init.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /* $Id: init.c,v 1.209 2002/02/09 19:49:31 davem Exp $
  2. * arch/sparc64/mm/init.c
  3. *
  4. * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. #include <linux/config.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/init.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/mm.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/slab.h>
  16. #include <linux/initrd.h>
  17. #include <linux/swap.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/cache.h>
  23. #include <linux/sort.h>
  24. #include <asm/head.h>
  25. #include <asm/system.h>
  26. #include <asm/page.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/oplib.h>
  30. #include <asm/iommu.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/dma.h>
  36. #include <asm/starfire.h>
  37. #include <asm/tlb.h>
  38. #include <asm/spitfire.h>
  39. #include <asm/sections.h>
  40. extern void device_scan(void);
  41. #define MAX_BANKS 32
  42. static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
  43. static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
  44. static int pavail_ents __initdata;
  45. static int pavail_rescan_ents __initdata;
  46. static int cmp_p64(const void *a, const void *b)
  47. {
  48. const struct linux_prom64_registers *x = a, *y = b;
  49. if (x->phys_addr > y->phys_addr)
  50. return 1;
  51. if (x->phys_addr < y->phys_addr)
  52. return -1;
  53. return 0;
  54. }
  55. static void __init read_obp_memory(const char *property,
  56. struct linux_prom64_registers *regs,
  57. int *num_ents)
  58. {
  59. int node = prom_finddevice("/memory");
  60. int prop_size = prom_getproplen(node, property);
  61. int ents, ret, i;
  62. ents = prop_size / sizeof(struct linux_prom64_registers);
  63. if (ents > MAX_BANKS) {
  64. prom_printf("The machine has more %s property entries than "
  65. "this kernel can support (%d).\n",
  66. property, MAX_BANKS);
  67. prom_halt();
  68. }
  69. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  70. if (ret == -1) {
  71. prom_printf("Couldn't get %s property from /memory.\n");
  72. prom_halt();
  73. }
  74. *num_ents = ents;
  75. /* Sanitize what we got from the firmware, by page aligning
  76. * everything.
  77. */
  78. for (i = 0; i < ents; i++) {
  79. unsigned long base, size;
  80. base = regs[i].phys_addr;
  81. size = regs[i].reg_size;
  82. size &= PAGE_MASK;
  83. if (base & ~PAGE_MASK) {
  84. unsigned long new_base = PAGE_ALIGN(base);
  85. size -= new_base - base;
  86. if ((long) size < 0L)
  87. size = 0UL;
  88. base = new_base;
  89. }
  90. regs[i].phys_addr = base;
  91. regs[i].reg_size = size;
  92. }
  93. sort(regs, ents, sizeof(struct linux_prom64_registers),
  94. cmp_p64, NULL);
  95. }
  96. unsigned long *sparc64_valid_addr_bitmap __read_mostly;
  97. /* Ugly, but necessary... -DaveM */
  98. unsigned long phys_base __read_mostly;
  99. unsigned long kern_base __read_mostly;
  100. unsigned long kern_size __read_mostly;
  101. unsigned long pfn_base __read_mostly;
  102. /* get_new_mmu_context() uses "cache + 1". */
  103. DEFINE_SPINLOCK(ctx_alloc_lock);
  104. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  105. #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
  106. unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
  107. /* References to special section boundaries */
  108. extern char _start[], _end[];
  109. /* Initial ramdisk setup */
  110. extern unsigned long sparc_ramdisk_image64;
  111. extern unsigned int sparc_ramdisk_image;
  112. extern unsigned int sparc_ramdisk_size;
  113. struct page *mem_map_zero __read_mostly;
  114. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  115. unsigned long sparc64_kern_pri_context __read_mostly;
  116. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  117. unsigned long sparc64_kern_sec_context __read_mostly;
  118. int bigkernel = 0;
  119. kmem_cache_t *pgtable_cache __read_mostly;
  120. static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
  121. {
  122. clear_page(addr);
  123. }
  124. void pgtable_cache_init(void)
  125. {
  126. pgtable_cache = kmem_cache_create("pgtable_cache",
  127. PAGE_SIZE, PAGE_SIZE,
  128. SLAB_HWCACHE_ALIGN |
  129. SLAB_MUST_HWCACHE_ALIGN,
  130. zero_ctor,
  131. NULL);
  132. if (!pgtable_cache) {
  133. prom_printf("pgtable_cache_init(): Could not create!\n");
  134. prom_halt();
  135. }
  136. }
  137. #ifdef CONFIG_DEBUG_DCFLUSH
  138. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  139. #ifdef CONFIG_SMP
  140. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  141. #endif
  142. #endif
  143. __inline__ void flush_dcache_page_impl(struct page *page)
  144. {
  145. #ifdef CONFIG_DEBUG_DCFLUSH
  146. atomic_inc(&dcpage_flushes);
  147. #endif
  148. #ifdef DCACHE_ALIASING_POSSIBLE
  149. __flush_dcache_page(page_address(page),
  150. ((tlb_type == spitfire) &&
  151. page_mapping(page) != NULL));
  152. #else
  153. if (page_mapping(page) != NULL &&
  154. tlb_type == spitfire)
  155. __flush_icache_page(__pa(page_address(page)));
  156. #endif
  157. }
  158. #define PG_dcache_dirty PG_arch_1
  159. #define PG_dcache_cpu_shift 24
  160. #define PG_dcache_cpu_mask (256 - 1)
  161. #if NR_CPUS > 256
  162. #error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
  163. #endif
  164. #define dcache_dirty_cpu(page) \
  165. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  166. static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
  167. {
  168. unsigned long mask = this_cpu;
  169. unsigned long non_cpu_bits;
  170. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  171. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  172. __asm__ __volatile__("1:\n\t"
  173. "ldx [%2], %%g7\n\t"
  174. "and %%g7, %1, %%g1\n\t"
  175. "or %%g1, %0, %%g1\n\t"
  176. "casx [%2], %%g7, %%g1\n\t"
  177. "cmp %%g7, %%g1\n\t"
  178. "membar #StoreLoad | #StoreStore\n\t"
  179. "bne,pn %%xcc, 1b\n\t"
  180. " nop"
  181. : /* no outputs */
  182. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  183. : "g1", "g7");
  184. }
  185. static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  186. {
  187. unsigned long mask = (1UL << PG_dcache_dirty);
  188. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  189. "1:\n\t"
  190. "ldx [%2], %%g7\n\t"
  191. "srlx %%g7, %4, %%g1\n\t"
  192. "and %%g1, %3, %%g1\n\t"
  193. "cmp %%g1, %0\n\t"
  194. "bne,pn %%icc, 2f\n\t"
  195. " andn %%g7, %1, %%g1\n\t"
  196. "casx [%2], %%g7, %%g1\n\t"
  197. "cmp %%g7, %%g1\n\t"
  198. "membar #StoreLoad | #StoreStore\n\t"
  199. "bne,pn %%xcc, 1b\n\t"
  200. " nop\n"
  201. "2:"
  202. : /* no outputs */
  203. : "r" (cpu), "r" (mask), "r" (&page->flags),
  204. "i" (PG_dcache_cpu_mask),
  205. "i" (PG_dcache_cpu_shift)
  206. : "g1", "g7");
  207. }
  208. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  209. {
  210. struct mm_struct *mm;
  211. struct page *page;
  212. unsigned long pfn;
  213. unsigned long pg_flags;
  214. unsigned long mm_rss;
  215. pfn = pte_pfn(pte);
  216. if (pfn_valid(pfn) &&
  217. (page = pfn_to_page(pfn), page_mapping(page)) &&
  218. ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
  219. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  220. PG_dcache_cpu_mask);
  221. int this_cpu = get_cpu();
  222. /* This is just to optimize away some function calls
  223. * in the SMP case.
  224. */
  225. if (cpu == this_cpu)
  226. flush_dcache_page_impl(page);
  227. else
  228. smp_flush_dcache_page_impl(page, cpu);
  229. clear_dcache_dirty_cpu(page, cpu);
  230. put_cpu();
  231. }
  232. mm = vma->vm_mm;
  233. mm_rss = get_mm_rss(mm);
  234. if (mm_rss >= mm->context.tsb_rss_limit)
  235. tsb_grow(mm, mm_rss, GFP_ATOMIC);
  236. if ((pte_val(pte) & _PAGE_ALL_SZ_BITS) == _PAGE_SZBITS) {
  237. struct tsb *tsb;
  238. unsigned long tag;
  239. tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
  240. (mm->context.tsb_nentries - 1UL)];
  241. tag = (address >> 22UL) | CTX_HWBITS(mm->context) << 48UL;
  242. tsb_insert(tsb, tag, pte_val(pte));
  243. }
  244. }
  245. void flush_dcache_page(struct page *page)
  246. {
  247. struct address_space *mapping;
  248. int this_cpu;
  249. /* Do not bother with the expensive D-cache flush if it
  250. * is merely the zero page. The 'bigcore' testcase in GDB
  251. * causes this case to run millions of times.
  252. */
  253. if (page == ZERO_PAGE(0))
  254. return;
  255. this_cpu = get_cpu();
  256. mapping = page_mapping(page);
  257. if (mapping && !mapping_mapped(mapping)) {
  258. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  259. if (dirty) {
  260. int dirty_cpu = dcache_dirty_cpu(page);
  261. if (dirty_cpu == this_cpu)
  262. goto out;
  263. smp_flush_dcache_page_impl(page, dirty_cpu);
  264. }
  265. set_dcache_dirty(page, this_cpu);
  266. } else {
  267. /* We could delay the flush for the !page_mapping
  268. * case too. But that case is for exec env/arg
  269. * pages and those are %99 certainly going to get
  270. * faulted into the tlb (and thus flushed) anyways.
  271. */
  272. flush_dcache_page_impl(page);
  273. }
  274. out:
  275. put_cpu();
  276. }
  277. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  278. {
  279. /* Cheetah has coherent I-cache. */
  280. if (tlb_type == spitfire) {
  281. unsigned long kaddr;
  282. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
  283. __flush_icache_page(__get_phys(kaddr));
  284. }
  285. }
  286. unsigned long page_to_pfn(struct page *page)
  287. {
  288. return (unsigned long) ((page - mem_map) + pfn_base);
  289. }
  290. struct page *pfn_to_page(unsigned long pfn)
  291. {
  292. return (mem_map + (pfn - pfn_base));
  293. }
  294. void show_mem(void)
  295. {
  296. printk("Mem-info:\n");
  297. show_free_areas();
  298. printk("Free swap: %6ldkB\n",
  299. nr_swap_pages << (PAGE_SHIFT-10));
  300. printk("%ld pages of RAM\n", num_physpages);
  301. printk("%d free pages\n", nr_free_pages());
  302. }
  303. void mmu_info(struct seq_file *m)
  304. {
  305. if (tlb_type == cheetah)
  306. seq_printf(m, "MMU Type\t: Cheetah\n");
  307. else if (tlb_type == cheetah_plus)
  308. seq_printf(m, "MMU Type\t: Cheetah+\n");
  309. else if (tlb_type == spitfire)
  310. seq_printf(m, "MMU Type\t: Spitfire\n");
  311. else
  312. seq_printf(m, "MMU Type\t: ???\n");
  313. #ifdef CONFIG_DEBUG_DCFLUSH
  314. seq_printf(m, "DCPageFlushes\t: %d\n",
  315. atomic_read(&dcpage_flushes));
  316. #ifdef CONFIG_SMP
  317. seq_printf(m, "DCPageFlushesXC\t: %d\n",
  318. atomic_read(&dcpage_flushes_xcall));
  319. #endif /* CONFIG_SMP */
  320. #endif /* CONFIG_DEBUG_DCFLUSH */
  321. }
  322. struct linux_prom_translation {
  323. unsigned long virt;
  324. unsigned long size;
  325. unsigned long data;
  326. };
  327. /* Exported for kernel TLB miss handling in ktlb.S */
  328. struct linux_prom_translation prom_trans[512] __read_mostly;
  329. unsigned int prom_trans_ents __read_mostly;
  330. unsigned int swapper_pgd_zero __read_mostly;
  331. extern unsigned long prom_boot_page;
  332. extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
  333. extern int prom_get_mmu_ihandle(void);
  334. extern void register_prom_callbacks(void);
  335. /* Exported for SMP bootup purposes. */
  336. unsigned long kern_locked_tte_data;
  337. /*
  338. * Translate PROM's mapping we capture at boot time into physical address.
  339. * The second parameter is only set from prom_callback() invocations.
  340. */
  341. unsigned long prom_virt_to_phys(unsigned long promva, int *error)
  342. {
  343. int i;
  344. for (i = 0; i < prom_trans_ents; i++) {
  345. struct linux_prom_translation *p = &prom_trans[i];
  346. if (promva >= p->virt &&
  347. promva < (p->virt + p->size)) {
  348. unsigned long base = p->data & _PAGE_PADDR;
  349. if (error)
  350. *error = 0;
  351. return base + (promva & (8192 - 1));
  352. }
  353. }
  354. if (error)
  355. *error = 1;
  356. return 0UL;
  357. }
  358. /* The obp translations are saved based on 8k pagesize, since obp can
  359. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  360. * HI_OBP_ADDRESS range are handled in ktlb.S.
  361. */
  362. static inline int in_obp_range(unsigned long vaddr)
  363. {
  364. return (vaddr >= LOW_OBP_ADDRESS &&
  365. vaddr < HI_OBP_ADDRESS);
  366. }
  367. static int cmp_ptrans(const void *a, const void *b)
  368. {
  369. const struct linux_prom_translation *x = a, *y = b;
  370. if (x->virt > y->virt)
  371. return 1;
  372. if (x->virt < y->virt)
  373. return -1;
  374. return 0;
  375. }
  376. /* Read OBP translations property into 'prom_trans[]'. */
  377. static void __init read_obp_translations(void)
  378. {
  379. int n, node, ents, first, last, i;
  380. node = prom_finddevice("/virtual-memory");
  381. n = prom_getproplen(node, "translations");
  382. if (unlikely(n == 0 || n == -1)) {
  383. prom_printf("prom_mappings: Couldn't get size.\n");
  384. prom_halt();
  385. }
  386. if (unlikely(n > sizeof(prom_trans))) {
  387. prom_printf("prom_mappings: Size %Zd is too big.\n", n);
  388. prom_halt();
  389. }
  390. if ((n = prom_getproperty(node, "translations",
  391. (char *)&prom_trans[0],
  392. sizeof(prom_trans))) == -1) {
  393. prom_printf("prom_mappings: Couldn't get property.\n");
  394. prom_halt();
  395. }
  396. n = n / sizeof(struct linux_prom_translation);
  397. ents = n;
  398. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  399. cmp_ptrans, NULL);
  400. /* Now kick out all the non-OBP entries. */
  401. for (i = 0; i < ents; i++) {
  402. if (in_obp_range(prom_trans[i].virt))
  403. break;
  404. }
  405. first = i;
  406. for (; i < ents; i++) {
  407. if (!in_obp_range(prom_trans[i].virt))
  408. break;
  409. }
  410. last = i;
  411. for (i = 0; i < (last - first); i++) {
  412. struct linux_prom_translation *src = &prom_trans[i + first];
  413. struct linux_prom_translation *dest = &prom_trans[i];
  414. *dest = *src;
  415. }
  416. for (; i < ents; i++) {
  417. struct linux_prom_translation *dest = &prom_trans[i];
  418. dest->virt = dest->size = dest->data = 0x0UL;
  419. }
  420. prom_trans_ents = last - first;
  421. if (tlb_type == spitfire) {
  422. /* Clear diag TTE bits. */
  423. for (i = 0; i < prom_trans_ents; i++)
  424. prom_trans[i].data &= ~0x0003fe0000000000UL;
  425. }
  426. }
  427. static void __init remap_kernel(void)
  428. {
  429. unsigned long phys_page, tte_vaddr, tte_data;
  430. int tlb_ent = sparc64_highest_locked_tlbent();
  431. tte_vaddr = (unsigned long) KERNBASE;
  432. phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  433. tte_data = (phys_page | (_PAGE_VALID | _PAGE_SZ4MB |
  434. _PAGE_CP | _PAGE_CV | _PAGE_P |
  435. _PAGE_L | _PAGE_W));
  436. kern_locked_tte_data = tte_data;
  437. /* Now lock us into the TLBs via OBP. */
  438. prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
  439. prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
  440. if (bigkernel) {
  441. tlb_ent -= 1;
  442. prom_dtlb_load(tlb_ent,
  443. tte_data + 0x400000,
  444. tte_vaddr + 0x400000);
  445. prom_itlb_load(tlb_ent,
  446. tte_data + 0x400000,
  447. tte_vaddr + 0x400000);
  448. }
  449. sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
  450. if (tlb_type == cheetah_plus) {
  451. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  452. CTX_CHEETAH_PLUS_NUC);
  453. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  454. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  455. }
  456. }
  457. static void __init inherit_prom_mappings(void)
  458. {
  459. read_obp_translations();
  460. /* Now fixup OBP's idea about where we really are mapped. */
  461. prom_printf("Remapping the kernel... ");
  462. remap_kernel();
  463. prom_printf("done.\n");
  464. prom_printf("Registering callbacks... ");
  465. register_prom_callbacks();
  466. prom_printf("done.\n");
  467. }
  468. void prom_world(int enter)
  469. {
  470. if (!enter)
  471. set_fs((mm_segment_t) { get_thread_current_ds() });
  472. __asm__ __volatile__("flushw");
  473. }
  474. #ifdef DCACHE_ALIASING_POSSIBLE
  475. void __flush_dcache_range(unsigned long start, unsigned long end)
  476. {
  477. unsigned long va;
  478. if (tlb_type == spitfire) {
  479. int n = 0;
  480. for (va = start; va < end; va += 32) {
  481. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  482. if (++n >= 512)
  483. break;
  484. }
  485. } else {
  486. start = __pa(start);
  487. end = __pa(end);
  488. for (va = start; va < end; va += 32)
  489. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  490. "membar #Sync"
  491. : /* no outputs */
  492. : "r" (va),
  493. "i" (ASI_DCACHE_INVALIDATE));
  494. }
  495. }
  496. #endif /* DCACHE_ALIASING_POSSIBLE */
  497. /* If not locked, zap it. */
  498. void __flush_tlb_all(void)
  499. {
  500. unsigned long pstate;
  501. int i;
  502. __asm__ __volatile__("flushw\n\t"
  503. "rdpr %%pstate, %0\n\t"
  504. "wrpr %0, %1, %%pstate"
  505. : "=r" (pstate)
  506. : "i" (PSTATE_IE));
  507. if (tlb_type == spitfire) {
  508. for (i = 0; i < 64; i++) {
  509. /* Spitfire Errata #32 workaround */
  510. /* NOTE: Always runs on spitfire, so no
  511. * cheetah+ page size encodings.
  512. */
  513. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  514. "flush %%g6"
  515. : /* No outputs */
  516. : "r" (0),
  517. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  518. if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
  519. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  520. "membar #Sync"
  521. : /* no outputs */
  522. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  523. spitfire_put_dtlb_data(i, 0x0UL);
  524. }
  525. /* Spitfire Errata #32 workaround */
  526. /* NOTE: Always runs on spitfire, so no
  527. * cheetah+ page size encodings.
  528. */
  529. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  530. "flush %%g6"
  531. : /* No outputs */
  532. : "r" (0),
  533. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  534. if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
  535. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  536. "membar #Sync"
  537. : /* no outputs */
  538. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  539. spitfire_put_itlb_data(i, 0x0UL);
  540. }
  541. }
  542. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  543. cheetah_flush_dtlb_all();
  544. cheetah_flush_itlb_all();
  545. }
  546. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  547. : : "r" (pstate));
  548. }
  549. /* Caller does TLB context flushing on local CPU if necessary.
  550. * The caller also ensures that CTX_VALID(mm->context) is false.
  551. *
  552. * We must be careful about boundary cases so that we never
  553. * let the user have CTX 0 (nucleus) or we ever use a CTX
  554. * version of zero (and thus NO_CONTEXT would not be caught
  555. * by version mis-match tests in mmu_context.h).
  556. */
  557. void get_new_mmu_context(struct mm_struct *mm)
  558. {
  559. unsigned long ctx, new_ctx;
  560. unsigned long orig_pgsz_bits;
  561. spin_lock(&ctx_alloc_lock);
  562. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  563. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  564. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  565. if (new_ctx >= (1 << CTX_NR_BITS)) {
  566. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  567. if (new_ctx >= ctx) {
  568. int i;
  569. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  570. CTX_FIRST_VERSION;
  571. if (new_ctx == 1)
  572. new_ctx = CTX_FIRST_VERSION;
  573. /* Don't call memset, for 16 entries that's just
  574. * plain silly...
  575. */
  576. mmu_context_bmap[0] = 3;
  577. mmu_context_bmap[1] = 0;
  578. mmu_context_bmap[2] = 0;
  579. mmu_context_bmap[3] = 0;
  580. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  581. mmu_context_bmap[i + 0] = 0;
  582. mmu_context_bmap[i + 1] = 0;
  583. mmu_context_bmap[i + 2] = 0;
  584. mmu_context_bmap[i + 3] = 0;
  585. }
  586. goto out;
  587. }
  588. }
  589. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  590. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  591. out:
  592. tlb_context_cache = new_ctx;
  593. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  594. spin_unlock(&ctx_alloc_lock);
  595. }
  596. void sparc_ultra_dump_itlb(void)
  597. {
  598. int slot;
  599. if (tlb_type == spitfire) {
  600. printk ("Contents of itlb: ");
  601. for (slot = 0; slot < 14; slot++) printk (" ");
  602. printk ("%2x:%016lx,%016lx\n",
  603. 0,
  604. spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
  605. for (slot = 1; slot < 64; slot+=3) {
  606. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  607. slot,
  608. spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
  609. slot+1,
  610. spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
  611. slot+2,
  612. spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
  613. }
  614. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  615. printk ("Contents of itlb0:\n");
  616. for (slot = 0; slot < 16; slot+=2) {
  617. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  618. slot,
  619. cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
  620. slot+1,
  621. cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
  622. }
  623. printk ("Contents of itlb2:\n");
  624. for (slot = 0; slot < 128; slot+=2) {
  625. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  626. slot,
  627. cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
  628. slot+1,
  629. cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
  630. }
  631. }
  632. }
  633. void sparc_ultra_dump_dtlb(void)
  634. {
  635. int slot;
  636. if (tlb_type == spitfire) {
  637. printk ("Contents of dtlb: ");
  638. for (slot = 0; slot < 14; slot++) printk (" ");
  639. printk ("%2x:%016lx,%016lx\n", 0,
  640. spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
  641. for (slot = 1; slot < 64; slot+=3) {
  642. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  643. slot,
  644. spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
  645. slot+1,
  646. spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
  647. slot+2,
  648. spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
  649. }
  650. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  651. printk ("Contents of dtlb0:\n");
  652. for (slot = 0; slot < 16; slot+=2) {
  653. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  654. slot,
  655. cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
  656. slot+1,
  657. cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
  658. }
  659. printk ("Contents of dtlb2:\n");
  660. for (slot = 0; slot < 512; slot+=2) {
  661. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  662. slot,
  663. cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
  664. slot+1,
  665. cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
  666. }
  667. if (tlb_type == cheetah_plus) {
  668. printk ("Contents of dtlb3:\n");
  669. for (slot = 0; slot < 512; slot+=2) {
  670. printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
  671. slot,
  672. cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
  673. slot+1,
  674. cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
  675. }
  676. }
  677. }
  678. }
  679. static inline void spitfire_errata32(void)
  680. {
  681. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  682. "flush %%g6"
  683. : /* No outputs */
  684. : "r" (0),
  685. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  686. }
  687. extern unsigned long cmdline_memory_size;
  688. unsigned long __init bootmem_init(unsigned long *pages_avail)
  689. {
  690. unsigned long bootmap_size, start_pfn, end_pfn;
  691. unsigned long end_of_phys_memory = 0UL;
  692. unsigned long bootmap_pfn, bytes_avail, size;
  693. int i;
  694. #ifdef CONFIG_DEBUG_BOOTMEM
  695. prom_printf("bootmem_init: Scan pavail, ");
  696. #endif
  697. bytes_avail = 0UL;
  698. for (i = 0; i < pavail_ents; i++) {
  699. end_of_phys_memory = pavail[i].phys_addr +
  700. pavail[i].reg_size;
  701. bytes_avail += pavail[i].reg_size;
  702. if (cmdline_memory_size) {
  703. if (bytes_avail > cmdline_memory_size) {
  704. unsigned long slack = bytes_avail - cmdline_memory_size;
  705. bytes_avail -= slack;
  706. end_of_phys_memory -= slack;
  707. pavail[i].reg_size -= slack;
  708. if ((long)pavail[i].reg_size <= 0L) {
  709. pavail[i].phys_addr = 0xdeadbeefUL;
  710. pavail[i].reg_size = 0UL;
  711. pavail_ents = i;
  712. } else {
  713. pavail[i+1].reg_size = 0Ul;
  714. pavail[i+1].phys_addr = 0xdeadbeefUL;
  715. pavail_ents = i + 1;
  716. }
  717. break;
  718. }
  719. }
  720. }
  721. *pages_avail = bytes_avail >> PAGE_SHIFT;
  722. /* Start with page aligned address of last symbol in kernel
  723. * image. The kernel is hard mapped below PAGE_OFFSET in a
  724. * 4MB locked TLB translation.
  725. */
  726. start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
  727. bootmap_pfn = start_pfn;
  728. end_pfn = end_of_phys_memory >> PAGE_SHIFT;
  729. #ifdef CONFIG_BLK_DEV_INITRD
  730. /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
  731. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  732. unsigned long ramdisk_image = sparc_ramdisk_image ?
  733. sparc_ramdisk_image : sparc_ramdisk_image64;
  734. if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
  735. ramdisk_image -= KERNBASE;
  736. initrd_start = ramdisk_image + phys_base;
  737. initrd_end = initrd_start + sparc_ramdisk_size;
  738. if (initrd_end > end_of_phys_memory) {
  739. printk(KERN_CRIT "initrd extends beyond end of memory "
  740. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  741. initrd_end, end_of_phys_memory);
  742. initrd_start = 0;
  743. }
  744. if (initrd_start) {
  745. if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
  746. initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
  747. bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
  748. }
  749. }
  750. #endif
  751. /* Initialize the boot-time allocator. */
  752. max_pfn = max_low_pfn = end_pfn;
  753. min_low_pfn = pfn_base;
  754. #ifdef CONFIG_DEBUG_BOOTMEM
  755. prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
  756. min_low_pfn, bootmap_pfn, max_low_pfn);
  757. #endif
  758. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
  759. /* Now register the available physical memory with the
  760. * allocator.
  761. */
  762. for (i = 0; i < pavail_ents; i++) {
  763. #ifdef CONFIG_DEBUG_BOOTMEM
  764. prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
  765. i, pavail[i].phys_addr, pavail[i].reg_size);
  766. #endif
  767. free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
  768. }
  769. #ifdef CONFIG_BLK_DEV_INITRD
  770. if (initrd_start) {
  771. size = initrd_end - initrd_start;
  772. /* Resert the initrd image area. */
  773. #ifdef CONFIG_DEBUG_BOOTMEM
  774. prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
  775. initrd_start, initrd_end);
  776. #endif
  777. reserve_bootmem(initrd_start, size);
  778. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  779. initrd_start += PAGE_OFFSET;
  780. initrd_end += PAGE_OFFSET;
  781. }
  782. #endif
  783. /* Reserve the kernel text/data/bss. */
  784. #ifdef CONFIG_DEBUG_BOOTMEM
  785. prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
  786. #endif
  787. reserve_bootmem(kern_base, kern_size);
  788. *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
  789. /* Reserve the bootmem map. We do not account for it
  790. * in pages_avail because we will release that memory
  791. * in free_all_bootmem.
  792. */
  793. size = bootmap_size;
  794. #ifdef CONFIG_DEBUG_BOOTMEM
  795. prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
  796. (bootmap_pfn << PAGE_SHIFT), size);
  797. #endif
  798. reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
  799. *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
  800. return end_pfn;
  801. }
  802. #ifdef CONFIG_DEBUG_PAGEALLOC
  803. static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
  804. {
  805. unsigned long vstart = PAGE_OFFSET + pstart;
  806. unsigned long vend = PAGE_OFFSET + pend;
  807. unsigned long alloc_bytes = 0UL;
  808. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  809. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  810. vstart, vend);
  811. prom_halt();
  812. }
  813. while (vstart < vend) {
  814. unsigned long this_end, paddr = __pa(vstart);
  815. pgd_t *pgd = pgd_offset_k(vstart);
  816. pud_t *pud;
  817. pmd_t *pmd;
  818. pte_t *pte;
  819. pud = pud_offset(pgd, vstart);
  820. if (pud_none(*pud)) {
  821. pmd_t *new;
  822. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  823. alloc_bytes += PAGE_SIZE;
  824. pud_populate(&init_mm, pud, new);
  825. }
  826. pmd = pmd_offset(pud, vstart);
  827. if (!pmd_present(*pmd)) {
  828. pte_t *new;
  829. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  830. alloc_bytes += PAGE_SIZE;
  831. pmd_populate_kernel(&init_mm, pmd, new);
  832. }
  833. pte = pte_offset_kernel(pmd, vstart);
  834. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  835. if (this_end > vend)
  836. this_end = vend;
  837. while (vstart < this_end) {
  838. pte_val(*pte) = (paddr | pgprot_val(prot));
  839. vstart += PAGE_SIZE;
  840. paddr += PAGE_SIZE;
  841. pte++;
  842. }
  843. }
  844. return alloc_bytes;
  845. }
  846. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  847. static int pall_ents __initdata;
  848. extern unsigned int kvmap_linear_patch[1];
  849. static void __init kernel_physical_mapping_init(void)
  850. {
  851. unsigned long i, mem_alloced = 0UL;
  852. read_obp_memory("reg", &pall[0], &pall_ents);
  853. for (i = 0; i < pall_ents; i++) {
  854. unsigned long phys_start, phys_end;
  855. phys_start = pall[i].phys_addr;
  856. phys_end = phys_start + pall[i].reg_size;
  857. mem_alloced += kernel_map_range(phys_start, phys_end,
  858. PAGE_KERNEL);
  859. }
  860. printk("Allocated %ld bytes for kernel page tables.\n",
  861. mem_alloced);
  862. kvmap_linear_patch[0] = 0x01000000; /* nop */
  863. flushi(&kvmap_linear_patch[0]);
  864. __flush_tlb_all();
  865. }
  866. void kernel_map_pages(struct page *page, int numpages, int enable)
  867. {
  868. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  869. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  870. kernel_map_range(phys_start, phys_end,
  871. (enable ? PAGE_KERNEL : __pgprot(0)));
  872. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  873. PAGE_OFFSET + phys_end);
  874. /* we should perform an IPI and flush all tlbs,
  875. * but that can deadlock->flush only current cpu.
  876. */
  877. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  878. PAGE_OFFSET + phys_end);
  879. }
  880. #endif
  881. unsigned long __init find_ecache_flush_span(unsigned long size)
  882. {
  883. int i;
  884. for (i = 0; i < pavail_ents; i++) {
  885. if (pavail[i].reg_size >= size)
  886. return pavail[i].phys_addr;
  887. }
  888. return ~0UL;
  889. }
  890. /* paging_init() sets up the page tables */
  891. extern void cheetah_ecache_flush_init(void);
  892. static unsigned long last_valid_pfn;
  893. pgd_t swapper_pg_dir[2048];
  894. void __init paging_init(void)
  895. {
  896. unsigned long end_pfn, pages_avail, shift;
  897. unsigned long real_end, i;
  898. /* Find available physical memory... */
  899. read_obp_memory("available", &pavail[0], &pavail_ents);
  900. phys_base = 0xffffffffffffffffUL;
  901. for (i = 0; i < pavail_ents; i++)
  902. phys_base = min(phys_base, pavail[i].phys_addr);
  903. pfn_base = phys_base >> PAGE_SHIFT;
  904. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  905. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  906. set_bit(0, mmu_context_bmap);
  907. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  908. real_end = (unsigned long)_end;
  909. if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
  910. bigkernel = 1;
  911. if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
  912. prom_printf("paging_init: Kernel > 8MB, too large.\n");
  913. prom_halt();
  914. }
  915. /* Set kernel pgd to upper alias so physical page computations
  916. * work.
  917. */
  918. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  919. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  920. /* Now can init the kernel/bad page tables. */
  921. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  922. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  923. swapper_pgd_zero = pgd_val(swapper_pg_dir[0]);
  924. inherit_prom_mappings();
  925. /* Ok, we can use our TLB miss and window trap handlers safely.
  926. * We need to do a quick peek here to see if we are on StarFire
  927. * or not, so setup_tba can setup the IRQ globals correctly (it
  928. * needs to get the hard smp processor id correctly).
  929. */
  930. {
  931. extern void setup_tba(int);
  932. setup_tba(this_is_starfire);
  933. }
  934. __flush_tlb_all();
  935. /* Setup bootmem... */
  936. pages_avail = 0;
  937. last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
  938. #ifdef CONFIG_DEBUG_PAGEALLOC
  939. kernel_physical_mapping_init();
  940. #endif
  941. {
  942. unsigned long zones_size[MAX_NR_ZONES];
  943. unsigned long zholes_size[MAX_NR_ZONES];
  944. unsigned long npages;
  945. int znum;
  946. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  947. zones_size[znum] = zholes_size[znum] = 0;
  948. npages = end_pfn - pfn_base;
  949. zones_size[ZONE_DMA] = npages;
  950. zholes_size[ZONE_DMA] = npages - pages_avail;
  951. free_area_init_node(0, &contig_page_data, zones_size,
  952. phys_base >> PAGE_SHIFT, zholes_size);
  953. }
  954. device_scan();
  955. }
  956. static void __init taint_real_pages(void)
  957. {
  958. int i;
  959. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  960. /* Find changes discovered in the physmem available rescan and
  961. * reserve the lost portions in the bootmem maps.
  962. */
  963. for (i = 0; i < pavail_ents; i++) {
  964. unsigned long old_start, old_end;
  965. old_start = pavail[i].phys_addr;
  966. old_end = old_start +
  967. pavail[i].reg_size;
  968. while (old_start < old_end) {
  969. int n;
  970. for (n = 0; pavail_rescan_ents; n++) {
  971. unsigned long new_start, new_end;
  972. new_start = pavail_rescan[n].phys_addr;
  973. new_end = new_start +
  974. pavail_rescan[n].reg_size;
  975. if (new_start <= old_start &&
  976. new_end >= (old_start + PAGE_SIZE)) {
  977. set_bit(old_start >> 22,
  978. sparc64_valid_addr_bitmap);
  979. goto do_next_page;
  980. }
  981. }
  982. reserve_bootmem(old_start, PAGE_SIZE);
  983. do_next_page:
  984. old_start += PAGE_SIZE;
  985. }
  986. }
  987. }
  988. void __init mem_init(void)
  989. {
  990. unsigned long codepages, datapages, initpages;
  991. unsigned long addr, last;
  992. int i;
  993. i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
  994. i += 1;
  995. sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
  996. if (sparc64_valid_addr_bitmap == NULL) {
  997. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  998. prom_halt();
  999. }
  1000. memset(sparc64_valid_addr_bitmap, 0, i << 3);
  1001. addr = PAGE_OFFSET + kern_base;
  1002. last = PAGE_ALIGN(kern_size) + addr;
  1003. while (addr < last) {
  1004. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1005. addr += PAGE_SIZE;
  1006. }
  1007. taint_real_pages();
  1008. max_mapnr = last_valid_pfn - pfn_base;
  1009. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1010. #ifdef CONFIG_DEBUG_BOOTMEM
  1011. prom_printf("mem_init: Calling free_all_bootmem().\n");
  1012. #endif
  1013. totalram_pages = num_physpages = free_all_bootmem() - 1;
  1014. /*
  1015. * Set up the zero page, mark it reserved, so that page count
  1016. * is not manipulated when freeing the page from user ptes.
  1017. */
  1018. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1019. if (mem_map_zero == NULL) {
  1020. prom_printf("paging_init: Cannot alloc zero page.\n");
  1021. prom_halt();
  1022. }
  1023. SetPageReserved(mem_map_zero);
  1024. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1025. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1026. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1027. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1028. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1029. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1030. printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1031. nr_free_pages() << (PAGE_SHIFT-10),
  1032. codepages << (PAGE_SHIFT-10),
  1033. datapages << (PAGE_SHIFT-10),
  1034. initpages << (PAGE_SHIFT-10),
  1035. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1036. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1037. cheetah_ecache_flush_init();
  1038. }
  1039. void free_initmem(void)
  1040. {
  1041. unsigned long addr, initend;
  1042. /*
  1043. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1044. */
  1045. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1046. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1047. for (; addr < initend; addr += PAGE_SIZE) {
  1048. unsigned long page;
  1049. struct page *p;
  1050. page = (addr +
  1051. ((unsigned long) __va(kern_base)) -
  1052. ((unsigned long) KERNBASE));
  1053. memset((void *)addr, 0xcc, PAGE_SIZE);
  1054. p = virt_to_page(page);
  1055. ClearPageReserved(p);
  1056. set_page_count(p, 1);
  1057. __free_page(p);
  1058. num_physpages++;
  1059. totalram_pages++;
  1060. }
  1061. }
  1062. #ifdef CONFIG_BLK_DEV_INITRD
  1063. void free_initrd_mem(unsigned long start, unsigned long end)
  1064. {
  1065. if (start < end)
  1066. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1067. for (; start < end; start += PAGE_SIZE) {
  1068. struct page *p = virt_to_page(start);
  1069. ClearPageReserved(p);
  1070. set_page_count(p, 1);
  1071. __free_page(p);
  1072. num_physpages++;
  1073. totalram_pages++;
  1074. }
  1075. }
  1076. #endif