init.c 35 KB

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