init.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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/module.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/poison.h>
  20. #include <linux/fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/cache.h>
  24. #include <linux/sort.h>
  25. #include <linux/percpu.h>
  26. #include <asm/head.h>
  27. #include <asm/system.h>
  28. #include <asm/page.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/oplib.h>
  32. #include <asm/iommu.h>
  33. #include <asm/io.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/dma.h>
  38. #include <asm/starfire.h>
  39. #include <asm/tlb.h>
  40. #include <asm/spitfire.h>
  41. #include <asm/sections.h>
  42. #include <asm/tsb.h>
  43. #include <asm/hypervisor.h>
  44. #include <asm/prom.h>
  45. #include <asm/sstate.h>
  46. #include <asm/mdesc.h>
  47. #include <asm/cpudata.h>
  48. #define MAX_PHYS_ADDRESS (1UL << 42UL)
  49. #define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
  50. #define KPTE_BITMAP_BYTES \
  51. ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
  52. unsigned long kern_linear_pte_xor[2] __read_mostly;
  53. /* A bitmap, one bit for every 256MB of physical memory. If the bit
  54. * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
  55. * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
  56. */
  57. unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
  58. #ifndef CONFIG_DEBUG_PAGEALLOC
  59. /* A special kernel TSB for 4MB and 256MB linear mappings.
  60. * Space is allocated for this right after the trap table
  61. * in arch/sparc64/kernel/head.S
  62. */
  63. extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
  64. #endif
  65. #define MAX_BANKS 32
  66. static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
  67. static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
  68. static int pavail_ents __initdata;
  69. static int pavail_rescan_ents __initdata;
  70. static int cmp_p64(const void *a, const void *b)
  71. {
  72. const struct linux_prom64_registers *x = a, *y = b;
  73. if (x->phys_addr > y->phys_addr)
  74. return 1;
  75. if (x->phys_addr < y->phys_addr)
  76. return -1;
  77. return 0;
  78. }
  79. static void __init read_obp_memory(const char *property,
  80. struct linux_prom64_registers *regs,
  81. int *num_ents)
  82. {
  83. int node = prom_finddevice("/memory");
  84. int prop_size = prom_getproplen(node, property);
  85. int ents, ret, i;
  86. ents = prop_size / sizeof(struct linux_prom64_registers);
  87. if (ents > MAX_BANKS) {
  88. prom_printf("The machine has more %s property entries than "
  89. "this kernel can support (%d).\n",
  90. property, MAX_BANKS);
  91. prom_halt();
  92. }
  93. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  94. if (ret == -1) {
  95. prom_printf("Couldn't get %s property from /memory.\n");
  96. prom_halt();
  97. }
  98. /* Sanitize what we got from the firmware, by page aligning
  99. * everything.
  100. */
  101. for (i = 0; i < ents; i++) {
  102. unsigned long base, size;
  103. base = regs[i].phys_addr;
  104. size = regs[i].reg_size;
  105. size &= PAGE_MASK;
  106. if (base & ~PAGE_MASK) {
  107. unsigned long new_base = PAGE_ALIGN(base);
  108. size -= new_base - base;
  109. if ((long) size < 0L)
  110. size = 0UL;
  111. base = new_base;
  112. }
  113. if (size == 0UL) {
  114. /* If it is empty, simply get rid of it.
  115. * This simplifies the logic of the other
  116. * functions that process these arrays.
  117. */
  118. memmove(&regs[i], &regs[i + 1],
  119. (ents - i - 1) * sizeof(regs[0]));
  120. i--;
  121. ents--;
  122. continue;
  123. }
  124. regs[i].phys_addr = base;
  125. regs[i].reg_size = size;
  126. }
  127. *num_ents = ents;
  128. sort(regs, ents, sizeof(struct linux_prom64_registers),
  129. cmp_p64, NULL);
  130. }
  131. unsigned long *sparc64_valid_addr_bitmap __read_mostly;
  132. /* Kernel physical address base and size in bytes. */
  133. unsigned long kern_base __read_mostly;
  134. unsigned long kern_size __read_mostly;
  135. /* Initial ramdisk setup */
  136. extern unsigned long sparc_ramdisk_image64;
  137. extern unsigned int sparc_ramdisk_image;
  138. extern unsigned int sparc_ramdisk_size;
  139. struct page *mem_map_zero __read_mostly;
  140. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  141. unsigned long sparc64_kern_pri_context __read_mostly;
  142. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  143. unsigned long sparc64_kern_sec_context __read_mostly;
  144. int num_kernel_image_mappings;
  145. #ifdef CONFIG_DEBUG_DCFLUSH
  146. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  147. #ifdef CONFIG_SMP
  148. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  149. #endif
  150. #endif
  151. inline void flush_dcache_page_impl(struct page *page)
  152. {
  153. BUG_ON(tlb_type == hypervisor);
  154. #ifdef CONFIG_DEBUG_DCFLUSH
  155. atomic_inc(&dcpage_flushes);
  156. #endif
  157. #ifdef DCACHE_ALIASING_POSSIBLE
  158. __flush_dcache_page(page_address(page),
  159. ((tlb_type == spitfire) &&
  160. page_mapping(page) != NULL));
  161. #else
  162. if (page_mapping(page) != NULL &&
  163. tlb_type == spitfire)
  164. __flush_icache_page(__pa(page_address(page)));
  165. #endif
  166. }
  167. #define PG_dcache_dirty PG_arch_1
  168. #define PG_dcache_cpu_shift 32UL
  169. #define PG_dcache_cpu_mask \
  170. ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
  171. #define dcache_dirty_cpu(page) \
  172. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  173. static inline void set_dcache_dirty(struct page *page, int this_cpu)
  174. {
  175. unsigned long mask = this_cpu;
  176. unsigned long non_cpu_bits;
  177. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  178. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  179. __asm__ __volatile__("1:\n\t"
  180. "ldx [%2], %%g7\n\t"
  181. "and %%g7, %1, %%g1\n\t"
  182. "or %%g1, %0, %%g1\n\t"
  183. "casx [%2], %%g7, %%g1\n\t"
  184. "cmp %%g7, %%g1\n\t"
  185. "membar #StoreLoad | #StoreStore\n\t"
  186. "bne,pn %%xcc, 1b\n\t"
  187. " nop"
  188. : /* no outputs */
  189. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  190. : "g1", "g7");
  191. }
  192. static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  193. {
  194. unsigned long mask = (1UL << PG_dcache_dirty);
  195. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  196. "1:\n\t"
  197. "ldx [%2], %%g7\n\t"
  198. "srlx %%g7, %4, %%g1\n\t"
  199. "and %%g1, %3, %%g1\n\t"
  200. "cmp %%g1, %0\n\t"
  201. "bne,pn %%icc, 2f\n\t"
  202. " andn %%g7, %1, %%g1\n\t"
  203. "casx [%2], %%g7, %%g1\n\t"
  204. "cmp %%g7, %%g1\n\t"
  205. "membar #StoreLoad | #StoreStore\n\t"
  206. "bne,pn %%xcc, 1b\n\t"
  207. " nop\n"
  208. "2:"
  209. : /* no outputs */
  210. : "r" (cpu), "r" (mask), "r" (&page->flags),
  211. "i" (PG_dcache_cpu_mask),
  212. "i" (PG_dcache_cpu_shift)
  213. : "g1", "g7");
  214. }
  215. static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
  216. {
  217. unsigned long tsb_addr = (unsigned long) ent;
  218. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  219. tsb_addr = __pa(tsb_addr);
  220. __tsb_insert(tsb_addr, tag, pte);
  221. }
  222. unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
  223. unsigned long _PAGE_SZBITS __read_mostly;
  224. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  225. {
  226. struct mm_struct *mm;
  227. struct tsb *tsb;
  228. unsigned long tag, flags;
  229. unsigned long tsb_index, tsb_hash_shift;
  230. if (tlb_type != hypervisor) {
  231. unsigned long pfn = pte_pfn(pte);
  232. unsigned long pg_flags;
  233. struct page *page;
  234. if (pfn_valid(pfn) &&
  235. (page = pfn_to_page(pfn), page_mapping(page)) &&
  236. ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
  237. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  238. PG_dcache_cpu_mask);
  239. int this_cpu = get_cpu();
  240. /* This is just to optimize away some function calls
  241. * in the SMP case.
  242. */
  243. if (cpu == this_cpu)
  244. flush_dcache_page_impl(page);
  245. else
  246. smp_flush_dcache_page_impl(page, cpu);
  247. clear_dcache_dirty_cpu(page, cpu);
  248. put_cpu();
  249. }
  250. }
  251. mm = vma->vm_mm;
  252. tsb_index = MM_TSB_BASE;
  253. tsb_hash_shift = PAGE_SHIFT;
  254. spin_lock_irqsave(&mm->context.lock, flags);
  255. #ifdef CONFIG_HUGETLB_PAGE
  256. if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
  257. if ((tlb_type == hypervisor &&
  258. (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
  259. (tlb_type != hypervisor &&
  260. (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
  261. tsb_index = MM_TSB_HUGE;
  262. tsb_hash_shift = HPAGE_SHIFT;
  263. }
  264. }
  265. #endif
  266. tsb = mm->context.tsb_block[tsb_index].tsb;
  267. tsb += ((address >> tsb_hash_shift) &
  268. (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
  269. tag = (address >> 22UL);
  270. tsb_insert(tsb, tag, pte_val(pte));
  271. spin_unlock_irqrestore(&mm->context.lock, flags);
  272. }
  273. void flush_dcache_page(struct page *page)
  274. {
  275. struct address_space *mapping;
  276. int this_cpu;
  277. if (tlb_type == hypervisor)
  278. return;
  279. /* Do not bother with the expensive D-cache flush if it
  280. * is merely the zero page. The 'bigcore' testcase in GDB
  281. * causes this case to run millions of times.
  282. */
  283. if (page == ZERO_PAGE(0))
  284. return;
  285. this_cpu = get_cpu();
  286. mapping = page_mapping(page);
  287. if (mapping && !mapping_mapped(mapping)) {
  288. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  289. if (dirty) {
  290. int dirty_cpu = dcache_dirty_cpu(page);
  291. if (dirty_cpu == this_cpu)
  292. goto out;
  293. smp_flush_dcache_page_impl(page, dirty_cpu);
  294. }
  295. set_dcache_dirty(page, this_cpu);
  296. } else {
  297. /* We could delay the flush for the !page_mapping
  298. * case too. But that case is for exec env/arg
  299. * pages and those are %99 certainly going to get
  300. * faulted into the tlb (and thus flushed) anyways.
  301. */
  302. flush_dcache_page_impl(page);
  303. }
  304. out:
  305. put_cpu();
  306. }
  307. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  308. {
  309. /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
  310. if (tlb_type == spitfire) {
  311. unsigned long kaddr;
  312. /* This code only runs on Spitfire cpus so this is
  313. * why we can assume _PAGE_PADDR_4U.
  314. */
  315. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
  316. unsigned long paddr, mask = _PAGE_PADDR_4U;
  317. if (kaddr >= PAGE_OFFSET)
  318. paddr = kaddr & mask;
  319. else {
  320. pgd_t *pgdp = pgd_offset_k(kaddr);
  321. pud_t *pudp = pud_offset(pgdp, kaddr);
  322. pmd_t *pmdp = pmd_offset(pudp, kaddr);
  323. pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
  324. paddr = pte_val(*ptep) & mask;
  325. }
  326. __flush_icache_page(paddr);
  327. }
  328. }
  329. }
  330. void show_mem(void)
  331. {
  332. unsigned long total = 0, reserved = 0;
  333. unsigned long shared = 0, cached = 0;
  334. pg_data_t *pgdat;
  335. printk(KERN_INFO "Mem-info:\n");
  336. show_free_areas();
  337. printk(KERN_INFO "Free swap: %6ldkB\n",
  338. nr_swap_pages << (PAGE_SHIFT-10));
  339. for_each_online_pgdat(pgdat) {
  340. unsigned long i, flags;
  341. pgdat_resize_lock(pgdat, &flags);
  342. for (i = 0; i < pgdat->node_spanned_pages; i++) {
  343. struct page *page = pgdat_page_nr(pgdat, i);
  344. total++;
  345. if (PageReserved(page))
  346. reserved++;
  347. else if (PageSwapCache(page))
  348. cached++;
  349. else if (page_count(page))
  350. shared += page_count(page) - 1;
  351. }
  352. pgdat_resize_unlock(pgdat, &flags);
  353. }
  354. printk(KERN_INFO "%lu pages of RAM\n", total);
  355. printk(KERN_INFO "%lu reserved pages\n", reserved);
  356. printk(KERN_INFO "%lu pages shared\n", shared);
  357. printk(KERN_INFO "%lu pages swap cached\n", cached);
  358. printk(KERN_INFO "%lu pages dirty\n",
  359. global_page_state(NR_FILE_DIRTY));
  360. printk(KERN_INFO "%lu pages writeback\n",
  361. global_page_state(NR_WRITEBACK));
  362. printk(KERN_INFO "%lu pages mapped\n",
  363. global_page_state(NR_FILE_MAPPED));
  364. printk(KERN_INFO "%lu pages slab\n",
  365. global_page_state(NR_SLAB_RECLAIMABLE) +
  366. global_page_state(NR_SLAB_UNRECLAIMABLE));
  367. printk(KERN_INFO "%lu pages pagetables\n",
  368. global_page_state(NR_PAGETABLE));
  369. }
  370. void mmu_info(struct seq_file *m)
  371. {
  372. if (tlb_type == cheetah)
  373. seq_printf(m, "MMU Type\t: Cheetah\n");
  374. else if (tlb_type == cheetah_plus)
  375. seq_printf(m, "MMU Type\t: Cheetah+\n");
  376. else if (tlb_type == spitfire)
  377. seq_printf(m, "MMU Type\t: Spitfire\n");
  378. else if (tlb_type == hypervisor)
  379. seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
  380. else
  381. seq_printf(m, "MMU Type\t: ???\n");
  382. #ifdef CONFIG_DEBUG_DCFLUSH
  383. seq_printf(m, "DCPageFlushes\t: %d\n",
  384. atomic_read(&dcpage_flushes));
  385. #ifdef CONFIG_SMP
  386. seq_printf(m, "DCPageFlushesXC\t: %d\n",
  387. atomic_read(&dcpage_flushes_xcall));
  388. #endif /* CONFIG_SMP */
  389. #endif /* CONFIG_DEBUG_DCFLUSH */
  390. }
  391. struct linux_prom_translation {
  392. unsigned long virt;
  393. unsigned long size;
  394. unsigned long data;
  395. };
  396. /* Exported for kernel TLB miss handling in ktlb.S */
  397. struct linux_prom_translation prom_trans[512] __read_mostly;
  398. unsigned int prom_trans_ents __read_mostly;
  399. /* Exported for SMP bootup purposes. */
  400. unsigned long kern_locked_tte_data;
  401. /* The obp translations are saved based on 8k pagesize, since obp can
  402. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  403. * HI_OBP_ADDRESS range are handled in ktlb.S.
  404. */
  405. static inline int in_obp_range(unsigned long vaddr)
  406. {
  407. return (vaddr >= LOW_OBP_ADDRESS &&
  408. vaddr < HI_OBP_ADDRESS);
  409. }
  410. static int cmp_ptrans(const void *a, const void *b)
  411. {
  412. const struct linux_prom_translation *x = a, *y = b;
  413. if (x->virt > y->virt)
  414. return 1;
  415. if (x->virt < y->virt)
  416. return -1;
  417. return 0;
  418. }
  419. /* Read OBP translations property into 'prom_trans[]'. */
  420. static void __init read_obp_translations(void)
  421. {
  422. int n, node, ents, first, last, i;
  423. node = prom_finddevice("/virtual-memory");
  424. n = prom_getproplen(node, "translations");
  425. if (unlikely(n == 0 || n == -1)) {
  426. prom_printf("prom_mappings: Couldn't get size.\n");
  427. prom_halt();
  428. }
  429. if (unlikely(n > sizeof(prom_trans))) {
  430. prom_printf("prom_mappings: Size %Zd is too big.\n", n);
  431. prom_halt();
  432. }
  433. if ((n = prom_getproperty(node, "translations",
  434. (char *)&prom_trans[0],
  435. sizeof(prom_trans))) == -1) {
  436. prom_printf("prom_mappings: Couldn't get property.\n");
  437. prom_halt();
  438. }
  439. n = n / sizeof(struct linux_prom_translation);
  440. ents = n;
  441. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  442. cmp_ptrans, NULL);
  443. /* Now kick out all the non-OBP entries. */
  444. for (i = 0; i < ents; i++) {
  445. if (in_obp_range(prom_trans[i].virt))
  446. break;
  447. }
  448. first = i;
  449. for (; i < ents; i++) {
  450. if (!in_obp_range(prom_trans[i].virt))
  451. break;
  452. }
  453. last = i;
  454. for (i = 0; i < (last - first); i++) {
  455. struct linux_prom_translation *src = &prom_trans[i + first];
  456. struct linux_prom_translation *dest = &prom_trans[i];
  457. *dest = *src;
  458. }
  459. for (; i < ents; i++) {
  460. struct linux_prom_translation *dest = &prom_trans[i];
  461. dest->virt = dest->size = dest->data = 0x0UL;
  462. }
  463. prom_trans_ents = last - first;
  464. if (tlb_type == spitfire) {
  465. /* Clear diag TTE bits. */
  466. for (i = 0; i < prom_trans_ents; i++)
  467. prom_trans[i].data &= ~0x0003fe0000000000UL;
  468. }
  469. }
  470. static void __init hypervisor_tlb_lock(unsigned long vaddr,
  471. unsigned long pte,
  472. unsigned long mmu)
  473. {
  474. unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
  475. if (ret != 0) {
  476. prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
  477. "errors with %lx\n", vaddr, 0, pte, mmu, ret);
  478. prom_halt();
  479. }
  480. }
  481. static unsigned long kern_large_tte(unsigned long paddr);
  482. static void __init remap_kernel(void)
  483. {
  484. unsigned long phys_page, tte_vaddr, tte_data;
  485. int i, tlb_ent = sparc64_highest_locked_tlbent();
  486. tte_vaddr = (unsigned long) KERNBASE;
  487. phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  488. tte_data = kern_large_tte(phys_page);
  489. kern_locked_tte_data = tte_data;
  490. /* Now lock us into the TLBs via Hypervisor or OBP. */
  491. if (tlb_type == hypervisor) {
  492. for (i = 0; i < num_kernel_image_mappings; i++) {
  493. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  494. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  495. tte_vaddr += 0x400000;
  496. tte_data += 0x400000;
  497. }
  498. } else {
  499. for (i = 0; i < num_kernel_image_mappings; i++) {
  500. prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
  501. prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
  502. tte_vaddr += 0x400000;
  503. tte_data += 0x400000;
  504. }
  505. sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
  506. }
  507. if (tlb_type == cheetah_plus) {
  508. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  509. CTX_CHEETAH_PLUS_NUC);
  510. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  511. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  512. }
  513. }
  514. static void __init inherit_prom_mappings(void)
  515. {
  516. read_obp_translations();
  517. /* Now fixup OBP's idea about where we really are mapped. */
  518. printk("Remapping the kernel... ");
  519. remap_kernel();
  520. printk("done.\n");
  521. }
  522. void prom_world(int enter)
  523. {
  524. if (!enter)
  525. set_fs((mm_segment_t) { get_thread_current_ds() });
  526. __asm__ __volatile__("flushw");
  527. }
  528. void __flush_dcache_range(unsigned long start, unsigned long end)
  529. {
  530. unsigned long va;
  531. if (tlb_type == spitfire) {
  532. int n = 0;
  533. for (va = start; va < end; va += 32) {
  534. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  535. if (++n >= 512)
  536. break;
  537. }
  538. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  539. start = __pa(start);
  540. end = __pa(end);
  541. for (va = start; va < end; va += 32)
  542. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  543. "membar #Sync"
  544. : /* no outputs */
  545. : "r" (va),
  546. "i" (ASI_DCACHE_INVALIDATE));
  547. }
  548. }
  549. /* get_new_mmu_context() uses "cache + 1". */
  550. DEFINE_SPINLOCK(ctx_alloc_lock);
  551. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  552. #define MAX_CTX_NR (1UL << CTX_NR_BITS)
  553. #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
  554. DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
  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. * Always invoked with interrupts disabled.
  564. */
  565. void get_new_mmu_context(struct mm_struct *mm)
  566. {
  567. unsigned long ctx, new_ctx;
  568. unsigned long orig_pgsz_bits;
  569. unsigned long flags;
  570. int new_version;
  571. spin_lock_irqsave(&ctx_alloc_lock, flags);
  572. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  573. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  574. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  575. new_version = 0;
  576. if (new_ctx >= (1 << CTX_NR_BITS)) {
  577. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  578. if (new_ctx >= ctx) {
  579. int i;
  580. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  581. CTX_FIRST_VERSION;
  582. if (new_ctx == 1)
  583. new_ctx = CTX_FIRST_VERSION;
  584. /* Don't call memset, for 16 entries that's just
  585. * plain silly...
  586. */
  587. mmu_context_bmap[0] = 3;
  588. mmu_context_bmap[1] = 0;
  589. mmu_context_bmap[2] = 0;
  590. mmu_context_bmap[3] = 0;
  591. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  592. mmu_context_bmap[i + 0] = 0;
  593. mmu_context_bmap[i + 1] = 0;
  594. mmu_context_bmap[i + 2] = 0;
  595. mmu_context_bmap[i + 3] = 0;
  596. }
  597. new_version = 1;
  598. goto out;
  599. }
  600. }
  601. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  602. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  603. out:
  604. tlb_context_cache = new_ctx;
  605. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  606. spin_unlock_irqrestore(&ctx_alloc_lock, flags);
  607. if (unlikely(new_version))
  608. smp_new_mmu_context_version();
  609. }
  610. /* Find a free area for the bootmem map, avoiding the kernel image
  611. * and the initial ramdisk.
  612. */
  613. static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
  614. unsigned long end_pfn)
  615. {
  616. unsigned long avoid_start, avoid_end, bootmap_size;
  617. int i;
  618. bootmap_size = bootmem_bootmap_pages(end_pfn - start_pfn);
  619. bootmap_size <<= PAGE_SHIFT;
  620. avoid_start = avoid_end = 0;
  621. #ifdef CONFIG_BLK_DEV_INITRD
  622. avoid_start = initrd_start;
  623. avoid_end = PAGE_ALIGN(initrd_end);
  624. #endif
  625. for (i = 0; i < pavail_ents; i++) {
  626. unsigned long start, end;
  627. start = pavail[i].phys_addr;
  628. end = start + pavail[i].reg_size;
  629. while (start < end) {
  630. if (start >= kern_base &&
  631. start < PAGE_ALIGN(kern_base + kern_size)) {
  632. start = PAGE_ALIGN(kern_base + kern_size);
  633. continue;
  634. }
  635. if (start >= avoid_start && start < avoid_end) {
  636. start = avoid_end;
  637. continue;
  638. }
  639. if ((end - start) < bootmap_size)
  640. break;
  641. if (start < kern_base &&
  642. (start + bootmap_size) > kern_base) {
  643. start = PAGE_ALIGN(kern_base + kern_size);
  644. continue;
  645. }
  646. if (start < avoid_start &&
  647. (start + bootmap_size) > avoid_start) {
  648. start = avoid_end;
  649. continue;
  650. }
  651. /* OK, it doesn't overlap anything, use it. */
  652. return start >> PAGE_SHIFT;
  653. }
  654. }
  655. prom_printf("Cannot find free area for bootmap, aborting.\n");
  656. prom_halt();
  657. }
  658. static void __init trim_pavail(unsigned long *cur_size_p,
  659. unsigned long *end_of_phys_p)
  660. {
  661. unsigned long to_trim = *cur_size_p - cmdline_memory_size;
  662. unsigned long avoid_start, avoid_end;
  663. int i;
  664. to_trim = PAGE_ALIGN(to_trim);
  665. avoid_start = avoid_end = 0;
  666. #ifdef CONFIG_BLK_DEV_INITRD
  667. avoid_start = initrd_start;
  668. avoid_end = PAGE_ALIGN(initrd_end);
  669. #endif
  670. /* Trim some pavail[] entries in order to satisfy the
  671. * requested "mem=xxx" kernel command line specification.
  672. *
  673. * We must not trim off the kernel image area nor the
  674. * initial ramdisk range (if any). Also, we must not trim
  675. * any pavail[] entry down to zero in order to preserve
  676. * the invariant that all pavail[] entries have a non-zero
  677. * size which is assumed by all of the code in here.
  678. */
  679. for (i = 0; i < pavail_ents; i++) {
  680. unsigned long start, end, kern_end;
  681. unsigned long trim_low, trim_high, n;
  682. kern_end = PAGE_ALIGN(kern_base + kern_size);
  683. trim_low = start = pavail[i].phys_addr;
  684. trim_high = end = start + pavail[i].reg_size;
  685. if (kern_base >= start &&
  686. kern_base < end) {
  687. trim_low = kern_base;
  688. if (kern_end >= end)
  689. continue;
  690. }
  691. if (kern_end >= start &&
  692. kern_end < end) {
  693. trim_high = kern_end;
  694. }
  695. if (avoid_start &&
  696. avoid_start >= start &&
  697. avoid_start < end) {
  698. if (trim_low > avoid_start)
  699. trim_low = avoid_start;
  700. if (avoid_end >= end)
  701. continue;
  702. }
  703. if (avoid_end &&
  704. avoid_end >= start &&
  705. avoid_end < end) {
  706. if (trim_high < avoid_end)
  707. trim_high = avoid_end;
  708. }
  709. if (trim_high <= trim_low)
  710. continue;
  711. if (trim_low == start && trim_high == end) {
  712. /* Whole chunk is available for trimming.
  713. * Trim all except one page, in order to keep
  714. * entry non-empty.
  715. */
  716. n = (end - start) - PAGE_SIZE;
  717. if (n > to_trim)
  718. n = to_trim;
  719. if (n) {
  720. pavail[i].phys_addr += n;
  721. pavail[i].reg_size -= n;
  722. to_trim -= n;
  723. }
  724. } else {
  725. n = (trim_low - start);
  726. if (n > to_trim)
  727. n = to_trim;
  728. if (n) {
  729. pavail[i].phys_addr += n;
  730. pavail[i].reg_size -= n;
  731. to_trim -= n;
  732. }
  733. if (to_trim) {
  734. n = end - trim_high;
  735. if (n > to_trim)
  736. n = to_trim;
  737. if (n) {
  738. pavail[i].reg_size -= n;
  739. to_trim -= n;
  740. }
  741. }
  742. }
  743. if (!to_trim)
  744. break;
  745. }
  746. /* Recalculate. */
  747. *cur_size_p = 0UL;
  748. for (i = 0; i < pavail_ents; i++) {
  749. *end_of_phys_p = pavail[i].phys_addr +
  750. pavail[i].reg_size;
  751. *cur_size_p += pavail[i].reg_size;
  752. }
  753. }
  754. /* About pages_avail, this is the value we will use to calculate
  755. * the zholes_size[] argument given to free_area_init_node(). The
  756. * page allocator uses this to calculate nr_kernel_pages,
  757. * nr_all_pages and zone->present_pages. On NUMA it is used
  758. * to calculate zone->min_unmapped_pages and zone->min_slab_pages.
  759. *
  760. * So this number should really be set to what the page allocator
  761. * actually ends up with. This means:
  762. * 1) It should include bootmem map pages, we'll release those.
  763. * 2) It should not include the kernel image, except for the
  764. * __init sections which we will also release.
  765. * 3) It should include the initrd image, since we'll release
  766. * that too.
  767. */
  768. static unsigned long __init bootmem_init(unsigned long *pages_avail,
  769. unsigned long phys_base)
  770. {
  771. unsigned long bootmap_size, end_pfn;
  772. unsigned long end_of_phys_memory = 0UL;
  773. unsigned long bootmap_pfn, bytes_avail, size;
  774. int i;
  775. bytes_avail = 0UL;
  776. for (i = 0; i < pavail_ents; i++) {
  777. end_of_phys_memory = pavail[i].phys_addr +
  778. pavail[i].reg_size;
  779. bytes_avail += pavail[i].reg_size;
  780. }
  781. /* Determine the location of the initial ramdisk before trying
  782. * to honor the "mem=xxx" command line argument. We must know
  783. * where the kernel image and the ramdisk image are so that we
  784. * do not trim those two areas from the physical memory map.
  785. */
  786. #ifdef CONFIG_BLK_DEV_INITRD
  787. /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
  788. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  789. unsigned long ramdisk_image = sparc_ramdisk_image ?
  790. sparc_ramdisk_image : sparc_ramdisk_image64;
  791. ramdisk_image -= KERNBASE;
  792. initrd_start = ramdisk_image + phys_base;
  793. initrd_end = initrd_start + sparc_ramdisk_size;
  794. if (initrd_end > end_of_phys_memory) {
  795. printk(KERN_CRIT "initrd extends beyond end of memory "
  796. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  797. initrd_end, end_of_phys_memory);
  798. initrd_start = 0;
  799. initrd_end = 0;
  800. }
  801. }
  802. #endif
  803. if (cmdline_memory_size &&
  804. bytes_avail > cmdline_memory_size)
  805. trim_pavail(&bytes_avail,
  806. &end_of_phys_memory);
  807. *pages_avail = bytes_avail >> PAGE_SHIFT;
  808. end_pfn = end_of_phys_memory >> PAGE_SHIFT;
  809. /* Initialize the boot-time allocator. */
  810. max_pfn = max_low_pfn = end_pfn;
  811. min_low_pfn = (phys_base >> PAGE_SHIFT);
  812. bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
  813. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
  814. min_low_pfn, end_pfn);
  815. /* Now register the available physical memory with the
  816. * allocator.
  817. */
  818. for (i = 0; i < pavail_ents; i++)
  819. free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
  820. #ifdef CONFIG_BLK_DEV_INITRD
  821. if (initrd_start) {
  822. size = initrd_end - initrd_start;
  823. /* Reserve the initrd image area. */
  824. reserve_bootmem(initrd_start, size, BOOTMEM_DEFAULT);
  825. initrd_start += PAGE_OFFSET;
  826. initrd_end += PAGE_OFFSET;
  827. }
  828. #endif
  829. /* Reserve the kernel text/data/bss. */
  830. reserve_bootmem(kern_base, kern_size, BOOTMEM_DEFAULT);
  831. *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
  832. /* Add back in the initmem pages. */
  833. size = ((unsigned long)(__init_end) & PAGE_MASK) -
  834. PAGE_ALIGN((unsigned long)__init_begin);
  835. *pages_avail += size >> PAGE_SHIFT;
  836. /* Reserve the bootmem map. We do not account for it
  837. * in pages_avail because we will release that memory
  838. * in free_all_bootmem.
  839. */
  840. size = bootmap_size;
  841. reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size, BOOTMEM_DEFAULT);
  842. for (i = 0; i < pavail_ents; i++) {
  843. unsigned long start_pfn, end_pfn;
  844. start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
  845. end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
  846. memory_present(0, start_pfn, end_pfn);
  847. }
  848. sparse_init();
  849. return end_pfn;
  850. }
  851. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  852. static int pall_ents __initdata;
  853. #ifdef CONFIG_DEBUG_PAGEALLOC
  854. static unsigned long __ref kernel_map_range(unsigned long pstart,
  855. unsigned long pend, pgprot_t prot)
  856. {
  857. unsigned long vstart = PAGE_OFFSET + pstart;
  858. unsigned long vend = PAGE_OFFSET + pend;
  859. unsigned long alloc_bytes = 0UL;
  860. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  861. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  862. vstart, vend);
  863. prom_halt();
  864. }
  865. while (vstart < vend) {
  866. unsigned long this_end, paddr = __pa(vstart);
  867. pgd_t *pgd = pgd_offset_k(vstart);
  868. pud_t *pud;
  869. pmd_t *pmd;
  870. pte_t *pte;
  871. pud = pud_offset(pgd, vstart);
  872. if (pud_none(*pud)) {
  873. pmd_t *new;
  874. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  875. alloc_bytes += PAGE_SIZE;
  876. pud_populate(&init_mm, pud, new);
  877. }
  878. pmd = pmd_offset(pud, vstart);
  879. if (!pmd_present(*pmd)) {
  880. pte_t *new;
  881. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  882. alloc_bytes += PAGE_SIZE;
  883. pmd_populate_kernel(&init_mm, pmd, new);
  884. }
  885. pte = pte_offset_kernel(pmd, vstart);
  886. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  887. if (this_end > vend)
  888. this_end = vend;
  889. while (vstart < this_end) {
  890. pte_val(*pte) = (paddr | pgprot_val(prot));
  891. vstart += PAGE_SIZE;
  892. paddr += PAGE_SIZE;
  893. pte++;
  894. }
  895. }
  896. return alloc_bytes;
  897. }
  898. extern unsigned int kvmap_linear_patch[1];
  899. #endif /* CONFIG_DEBUG_PAGEALLOC */
  900. static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
  901. {
  902. const unsigned long shift_256MB = 28;
  903. const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
  904. const unsigned long size_256MB = (1UL << shift_256MB);
  905. while (start < end) {
  906. long remains;
  907. remains = end - start;
  908. if (remains < size_256MB)
  909. break;
  910. if (start & mask_256MB) {
  911. start = (start + size_256MB) & ~mask_256MB;
  912. continue;
  913. }
  914. while (remains >= size_256MB) {
  915. unsigned long index = start >> shift_256MB;
  916. __set_bit(index, kpte_linear_bitmap);
  917. start += size_256MB;
  918. remains -= size_256MB;
  919. }
  920. }
  921. }
  922. static void __init init_kpte_bitmap(void)
  923. {
  924. unsigned long i;
  925. for (i = 0; i < pall_ents; i++) {
  926. unsigned long phys_start, phys_end;
  927. phys_start = pall[i].phys_addr;
  928. phys_end = phys_start + pall[i].reg_size;
  929. mark_kpte_bitmap(phys_start, phys_end);
  930. }
  931. }
  932. static void __init kernel_physical_mapping_init(void)
  933. {
  934. #ifdef CONFIG_DEBUG_PAGEALLOC
  935. unsigned long i, mem_alloced = 0UL;
  936. for (i = 0; i < pall_ents; i++) {
  937. unsigned long phys_start, phys_end;
  938. phys_start = pall[i].phys_addr;
  939. phys_end = phys_start + pall[i].reg_size;
  940. mem_alloced += kernel_map_range(phys_start, phys_end,
  941. PAGE_KERNEL);
  942. }
  943. printk("Allocated %ld bytes for kernel page tables.\n",
  944. mem_alloced);
  945. kvmap_linear_patch[0] = 0x01000000; /* nop */
  946. flushi(&kvmap_linear_patch[0]);
  947. __flush_tlb_all();
  948. #endif
  949. }
  950. #ifdef CONFIG_DEBUG_PAGEALLOC
  951. void kernel_map_pages(struct page *page, int numpages, int enable)
  952. {
  953. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  954. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  955. kernel_map_range(phys_start, phys_end,
  956. (enable ? PAGE_KERNEL : __pgprot(0)));
  957. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  958. PAGE_OFFSET + phys_end);
  959. /* we should perform an IPI and flush all tlbs,
  960. * but that can deadlock->flush only current cpu.
  961. */
  962. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  963. PAGE_OFFSET + phys_end);
  964. }
  965. #endif
  966. unsigned long __init find_ecache_flush_span(unsigned long size)
  967. {
  968. int i;
  969. for (i = 0; i < pavail_ents; i++) {
  970. if (pavail[i].reg_size >= size)
  971. return pavail[i].phys_addr;
  972. }
  973. return ~0UL;
  974. }
  975. static void __init tsb_phys_patch(void)
  976. {
  977. struct tsb_ldquad_phys_patch_entry *pquad;
  978. struct tsb_phys_patch_entry *p;
  979. pquad = &__tsb_ldquad_phys_patch;
  980. while (pquad < &__tsb_ldquad_phys_patch_end) {
  981. unsigned long addr = pquad->addr;
  982. if (tlb_type == hypervisor)
  983. *(unsigned int *) addr = pquad->sun4v_insn;
  984. else
  985. *(unsigned int *) addr = pquad->sun4u_insn;
  986. wmb();
  987. __asm__ __volatile__("flush %0"
  988. : /* no outputs */
  989. : "r" (addr));
  990. pquad++;
  991. }
  992. p = &__tsb_phys_patch;
  993. while (p < &__tsb_phys_patch_end) {
  994. unsigned long addr = p->addr;
  995. *(unsigned int *) addr = p->insn;
  996. wmb();
  997. __asm__ __volatile__("flush %0"
  998. : /* no outputs */
  999. : "r" (addr));
  1000. p++;
  1001. }
  1002. }
  1003. /* Don't mark as init, we give this to the Hypervisor. */
  1004. #ifndef CONFIG_DEBUG_PAGEALLOC
  1005. #define NUM_KTSB_DESCR 2
  1006. #else
  1007. #define NUM_KTSB_DESCR 1
  1008. #endif
  1009. static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
  1010. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  1011. static void __init sun4v_ktsb_init(void)
  1012. {
  1013. unsigned long ktsb_pa;
  1014. /* First KTSB for PAGE_SIZE mappings. */
  1015. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1016. switch (PAGE_SIZE) {
  1017. case 8 * 1024:
  1018. default:
  1019. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  1020. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  1021. break;
  1022. case 64 * 1024:
  1023. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1024. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1025. break;
  1026. case 512 * 1024:
  1027. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1028. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1029. break;
  1030. case 4 * 1024 * 1024:
  1031. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1032. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1033. break;
  1034. };
  1035. ktsb_descr[0].assoc = 1;
  1036. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1037. ktsb_descr[0].ctx_idx = 0;
  1038. ktsb_descr[0].tsb_base = ktsb_pa;
  1039. ktsb_descr[0].resv = 0;
  1040. #ifndef CONFIG_DEBUG_PAGEALLOC
  1041. /* Second KTSB for 4MB/256MB mappings. */
  1042. ktsb_pa = (kern_base +
  1043. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1044. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1045. ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
  1046. HV_PGSZ_MASK_256MB);
  1047. ktsb_descr[1].assoc = 1;
  1048. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1049. ktsb_descr[1].ctx_idx = 0;
  1050. ktsb_descr[1].tsb_base = ktsb_pa;
  1051. ktsb_descr[1].resv = 0;
  1052. #endif
  1053. }
  1054. void __cpuinit sun4v_ktsb_register(void)
  1055. {
  1056. unsigned long pa, ret;
  1057. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1058. ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
  1059. if (ret != 0) {
  1060. prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
  1061. "errors with %lx\n", pa, ret);
  1062. prom_halt();
  1063. }
  1064. }
  1065. /* paging_init() sets up the page tables */
  1066. extern void cheetah_ecache_flush_init(void);
  1067. extern void sun4v_patch_tlb_handlers(void);
  1068. extern void cpu_probe(void);
  1069. extern void central_probe(void);
  1070. static unsigned long last_valid_pfn;
  1071. pgd_t swapper_pg_dir[2048];
  1072. static void sun4u_pgprot_init(void);
  1073. static void sun4v_pgprot_init(void);
  1074. /* Dummy function */
  1075. void __init setup_per_cpu_areas(void)
  1076. {
  1077. }
  1078. void __init paging_init(void)
  1079. {
  1080. unsigned long end_pfn, pages_avail, shift, phys_base;
  1081. unsigned long real_end, i;
  1082. /* These build time checkes make sure that the dcache_dirty_cpu()
  1083. * page->flags usage will work.
  1084. *
  1085. * When a page gets marked as dcache-dirty, we store the
  1086. * cpu number starting at bit 32 in the page->flags. Also,
  1087. * functions like clear_dcache_dirty_cpu use the cpu mask
  1088. * in 13-bit signed-immediate instruction fields.
  1089. */
  1090. BUILD_BUG_ON(FLAGS_RESERVED != 32);
  1091. BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
  1092. ilog2(roundup_pow_of_two(NR_CPUS)) > FLAGS_RESERVED);
  1093. BUILD_BUG_ON(NR_CPUS > 4096);
  1094. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  1095. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1096. sstate_booting();
  1097. /* Invalidate both kernel TSBs. */
  1098. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1099. #ifndef CONFIG_DEBUG_PAGEALLOC
  1100. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1101. #endif
  1102. if (tlb_type == hypervisor)
  1103. sun4v_pgprot_init();
  1104. else
  1105. sun4u_pgprot_init();
  1106. if (tlb_type == cheetah_plus ||
  1107. tlb_type == hypervisor)
  1108. tsb_phys_patch();
  1109. if (tlb_type == hypervisor) {
  1110. sun4v_patch_tlb_handlers();
  1111. sun4v_ktsb_init();
  1112. }
  1113. /* Find available physical memory... */
  1114. read_obp_memory("available", &pavail[0], &pavail_ents);
  1115. phys_base = 0xffffffffffffffffUL;
  1116. for (i = 0; i < pavail_ents; i++)
  1117. phys_base = min(phys_base, pavail[i].phys_addr);
  1118. set_bit(0, mmu_context_bmap);
  1119. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1120. real_end = (unsigned long)_end;
  1121. num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
  1122. printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
  1123. num_kernel_image_mappings);
  1124. /* Set kernel pgd to upper alias so physical page computations
  1125. * work.
  1126. */
  1127. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1128. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  1129. /* Now can init the kernel/bad page tables. */
  1130. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  1131. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  1132. inherit_prom_mappings();
  1133. read_obp_memory("reg", &pall[0], &pall_ents);
  1134. init_kpte_bitmap();
  1135. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1136. setup_tba();
  1137. __flush_tlb_all();
  1138. if (tlb_type == hypervisor)
  1139. sun4v_ktsb_register();
  1140. /* Setup bootmem... */
  1141. pages_avail = 0;
  1142. last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
  1143. max_mapnr = last_valid_pfn;
  1144. kernel_physical_mapping_init();
  1145. real_setup_per_cpu_areas();
  1146. prom_build_devicetree();
  1147. if (tlb_type == hypervisor)
  1148. sun4v_mdesc_init();
  1149. {
  1150. unsigned long zones_size[MAX_NR_ZONES];
  1151. unsigned long zholes_size[MAX_NR_ZONES];
  1152. int znum;
  1153. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  1154. zones_size[znum] = zholes_size[znum] = 0;
  1155. zones_size[ZONE_NORMAL] = end_pfn;
  1156. zholes_size[ZONE_NORMAL] = end_pfn - pages_avail;
  1157. free_area_init_node(0, &contig_page_data, zones_size,
  1158. __pa(PAGE_OFFSET) >> PAGE_SHIFT,
  1159. zholes_size);
  1160. }
  1161. printk("Booting Linux...\n");
  1162. central_probe();
  1163. cpu_probe();
  1164. }
  1165. static void __init taint_real_pages(void)
  1166. {
  1167. int i;
  1168. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  1169. /* Find changes discovered in the physmem available rescan and
  1170. * reserve the lost portions in the bootmem maps.
  1171. */
  1172. for (i = 0; i < pavail_ents; i++) {
  1173. unsigned long old_start, old_end;
  1174. old_start = pavail[i].phys_addr;
  1175. old_end = old_start +
  1176. pavail[i].reg_size;
  1177. while (old_start < old_end) {
  1178. int n;
  1179. for (n = 0; n < pavail_rescan_ents; n++) {
  1180. unsigned long new_start, new_end;
  1181. new_start = pavail_rescan[n].phys_addr;
  1182. new_end = new_start +
  1183. pavail_rescan[n].reg_size;
  1184. if (new_start <= old_start &&
  1185. new_end >= (old_start + PAGE_SIZE)) {
  1186. set_bit(old_start >> 22,
  1187. sparc64_valid_addr_bitmap);
  1188. goto do_next_page;
  1189. }
  1190. }
  1191. reserve_bootmem(old_start, PAGE_SIZE, BOOTMEM_DEFAULT);
  1192. do_next_page:
  1193. old_start += PAGE_SIZE;
  1194. }
  1195. }
  1196. }
  1197. int __init page_in_phys_avail(unsigned long paddr)
  1198. {
  1199. int i;
  1200. paddr &= PAGE_MASK;
  1201. for (i = 0; i < pavail_rescan_ents; i++) {
  1202. unsigned long start, end;
  1203. start = pavail_rescan[i].phys_addr;
  1204. end = start + pavail_rescan[i].reg_size;
  1205. if (paddr >= start && paddr < end)
  1206. return 1;
  1207. }
  1208. if (paddr >= kern_base && paddr < (kern_base + kern_size))
  1209. return 1;
  1210. #ifdef CONFIG_BLK_DEV_INITRD
  1211. if (paddr >= __pa(initrd_start) &&
  1212. paddr < __pa(PAGE_ALIGN(initrd_end)))
  1213. return 1;
  1214. #endif
  1215. return 0;
  1216. }
  1217. void __init mem_init(void)
  1218. {
  1219. unsigned long codepages, datapages, initpages;
  1220. unsigned long addr, last;
  1221. int i;
  1222. i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
  1223. i += 1;
  1224. sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
  1225. if (sparc64_valid_addr_bitmap == NULL) {
  1226. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  1227. prom_halt();
  1228. }
  1229. memset(sparc64_valid_addr_bitmap, 0, i << 3);
  1230. addr = PAGE_OFFSET + kern_base;
  1231. last = PAGE_ALIGN(kern_size) + addr;
  1232. while (addr < last) {
  1233. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1234. addr += PAGE_SIZE;
  1235. }
  1236. taint_real_pages();
  1237. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1238. /* We subtract one to account for the mem_map_zero page
  1239. * allocated below.
  1240. */
  1241. totalram_pages = num_physpages = free_all_bootmem() - 1;
  1242. /*
  1243. * Set up the zero page, mark it reserved, so that page count
  1244. * is not manipulated when freeing the page from user ptes.
  1245. */
  1246. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1247. if (mem_map_zero == NULL) {
  1248. prom_printf("paging_init: Cannot alloc zero page.\n");
  1249. prom_halt();
  1250. }
  1251. SetPageReserved(mem_map_zero);
  1252. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1253. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1254. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1255. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1256. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1257. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1258. printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1259. nr_free_pages() << (PAGE_SHIFT-10),
  1260. codepages << (PAGE_SHIFT-10),
  1261. datapages << (PAGE_SHIFT-10),
  1262. initpages << (PAGE_SHIFT-10),
  1263. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1264. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1265. cheetah_ecache_flush_init();
  1266. }
  1267. void free_initmem(void)
  1268. {
  1269. unsigned long addr, initend;
  1270. /*
  1271. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1272. */
  1273. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1274. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1275. for (; addr < initend; addr += PAGE_SIZE) {
  1276. unsigned long page;
  1277. struct page *p;
  1278. page = (addr +
  1279. ((unsigned long) __va(kern_base)) -
  1280. ((unsigned long) KERNBASE));
  1281. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1282. p = virt_to_page(page);
  1283. ClearPageReserved(p);
  1284. init_page_count(p);
  1285. __free_page(p);
  1286. num_physpages++;
  1287. totalram_pages++;
  1288. }
  1289. }
  1290. #ifdef CONFIG_BLK_DEV_INITRD
  1291. void free_initrd_mem(unsigned long start, unsigned long end)
  1292. {
  1293. if (start < end)
  1294. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1295. for (; start < end; start += PAGE_SIZE) {
  1296. struct page *p = virt_to_page(start);
  1297. ClearPageReserved(p);
  1298. init_page_count(p);
  1299. __free_page(p);
  1300. num_physpages++;
  1301. totalram_pages++;
  1302. }
  1303. }
  1304. #endif
  1305. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1306. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1307. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1308. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1309. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1310. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1311. pgprot_t PAGE_KERNEL __read_mostly;
  1312. EXPORT_SYMBOL(PAGE_KERNEL);
  1313. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1314. pgprot_t PAGE_COPY __read_mostly;
  1315. pgprot_t PAGE_SHARED __read_mostly;
  1316. EXPORT_SYMBOL(PAGE_SHARED);
  1317. pgprot_t PAGE_EXEC __read_mostly;
  1318. unsigned long pg_iobits __read_mostly;
  1319. unsigned long _PAGE_IE __read_mostly;
  1320. EXPORT_SYMBOL(_PAGE_IE);
  1321. unsigned long _PAGE_E __read_mostly;
  1322. EXPORT_SYMBOL(_PAGE_E);
  1323. unsigned long _PAGE_CACHE __read_mostly;
  1324. EXPORT_SYMBOL(_PAGE_CACHE);
  1325. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  1326. #define VMEMMAP_CHUNK_SHIFT 22
  1327. #define VMEMMAP_CHUNK (1UL << VMEMMAP_CHUNK_SHIFT)
  1328. #define VMEMMAP_CHUNK_MASK ~(VMEMMAP_CHUNK - 1UL)
  1329. #define VMEMMAP_ALIGN(x) (((x)+VMEMMAP_CHUNK-1UL)&VMEMMAP_CHUNK_MASK)
  1330. #define VMEMMAP_SIZE ((((1UL << MAX_PHYSADDR_BITS) >> PAGE_SHIFT) * \
  1331. sizeof(struct page *)) >> VMEMMAP_CHUNK_SHIFT)
  1332. unsigned long vmemmap_table[VMEMMAP_SIZE];
  1333. int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
  1334. {
  1335. unsigned long vstart = (unsigned long) start;
  1336. unsigned long vend = (unsigned long) (start + nr);
  1337. unsigned long phys_start = (vstart - VMEMMAP_BASE);
  1338. unsigned long phys_end = (vend - VMEMMAP_BASE);
  1339. unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
  1340. unsigned long end = VMEMMAP_ALIGN(phys_end);
  1341. unsigned long pte_base;
  1342. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1343. _PAGE_CP_4U | _PAGE_CV_4U |
  1344. _PAGE_P_4U | _PAGE_W_4U);
  1345. if (tlb_type == hypervisor)
  1346. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1347. _PAGE_CP_4V | _PAGE_CV_4V |
  1348. _PAGE_P_4V | _PAGE_W_4V);
  1349. for (; addr < end; addr += VMEMMAP_CHUNK) {
  1350. unsigned long *vmem_pp =
  1351. vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
  1352. void *block;
  1353. if (!(*vmem_pp & _PAGE_VALID)) {
  1354. block = vmemmap_alloc_block(1UL << 22, node);
  1355. if (!block)
  1356. return -ENOMEM;
  1357. *vmem_pp = pte_base | __pa(block);
  1358. printk(KERN_INFO "[%p-%p] page_structs=%lu "
  1359. "node=%d entry=%lu/%lu\n", start, block, nr,
  1360. node,
  1361. addr >> VMEMMAP_CHUNK_SHIFT,
  1362. VMEMMAP_SIZE >> VMEMMAP_CHUNK_SHIFT);
  1363. }
  1364. }
  1365. return 0;
  1366. }
  1367. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  1368. static void prot_init_common(unsigned long page_none,
  1369. unsigned long page_shared,
  1370. unsigned long page_copy,
  1371. unsigned long page_readonly,
  1372. unsigned long page_exec_bit)
  1373. {
  1374. PAGE_COPY = __pgprot(page_copy);
  1375. PAGE_SHARED = __pgprot(page_shared);
  1376. protection_map[0x0] = __pgprot(page_none);
  1377. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1378. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1379. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1380. protection_map[0x4] = __pgprot(page_readonly);
  1381. protection_map[0x5] = __pgprot(page_readonly);
  1382. protection_map[0x6] = __pgprot(page_copy);
  1383. protection_map[0x7] = __pgprot(page_copy);
  1384. protection_map[0x8] = __pgprot(page_none);
  1385. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1386. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1387. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1388. protection_map[0xc] = __pgprot(page_readonly);
  1389. protection_map[0xd] = __pgprot(page_readonly);
  1390. protection_map[0xe] = __pgprot(page_shared);
  1391. protection_map[0xf] = __pgprot(page_shared);
  1392. }
  1393. static void __init sun4u_pgprot_init(void)
  1394. {
  1395. unsigned long page_none, page_shared, page_copy, page_readonly;
  1396. unsigned long page_exec_bit;
  1397. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1398. _PAGE_CACHE_4U | _PAGE_P_4U |
  1399. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1400. _PAGE_EXEC_4U);
  1401. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1402. _PAGE_CACHE_4U | _PAGE_P_4U |
  1403. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1404. _PAGE_EXEC_4U | _PAGE_L_4U);
  1405. PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
  1406. _PAGE_IE = _PAGE_IE_4U;
  1407. _PAGE_E = _PAGE_E_4U;
  1408. _PAGE_CACHE = _PAGE_CACHE_4U;
  1409. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1410. __ACCESS_BITS_4U | _PAGE_E_4U);
  1411. #ifdef CONFIG_DEBUG_PAGEALLOC
  1412. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
  1413. 0xfffff80000000000;
  1414. #else
  1415. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1416. 0xfffff80000000000;
  1417. #endif
  1418. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1419. _PAGE_P_4U | _PAGE_W_4U);
  1420. /* XXX Should use 256MB on Panther. XXX */
  1421. kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
  1422. _PAGE_SZBITS = _PAGE_SZBITS_4U;
  1423. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1424. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1425. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1426. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1427. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1428. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1429. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1430. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1431. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1432. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1433. page_exec_bit = _PAGE_EXEC_4U;
  1434. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1435. page_exec_bit);
  1436. }
  1437. static void __init sun4v_pgprot_init(void)
  1438. {
  1439. unsigned long page_none, page_shared, page_copy, page_readonly;
  1440. unsigned long page_exec_bit;
  1441. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1442. _PAGE_CACHE_4V | _PAGE_P_4V |
  1443. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1444. _PAGE_EXEC_4V);
  1445. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1446. PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
  1447. _PAGE_IE = _PAGE_IE_4V;
  1448. _PAGE_E = _PAGE_E_4V;
  1449. _PAGE_CACHE = _PAGE_CACHE_4V;
  1450. #ifdef CONFIG_DEBUG_PAGEALLOC
  1451. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1452. 0xfffff80000000000;
  1453. #else
  1454. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  1455. 0xfffff80000000000;
  1456. #endif
  1457. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1458. _PAGE_P_4V | _PAGE_W_4V);
  1459. #ifdef CONFIG_DEBUG_PAGEALLOC
  1460. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1461. 0xfffff80000000000;
  1462. #else
  1463. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1464. 0xfffff80000000000;
  1465. #endif
  1466. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1467. _PAGE_P_4V | _PAGE_W_4V);
  1468. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  1469. __ACCESS_BITS_4V | _PAGE_E_4V);
  1470. _PAGE_SZBITS = _PAGE_SZBITS_4V;
  1471. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  1472. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  1473. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  1474. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  1475. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  1476. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1477. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  1478. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1479. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1480. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1481. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1482. page_exec_bit = _PAGE_EXEC_4V;
  1483. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1484. page_exec_bit);
  1485. }
  1486. unsigned long pte_sz_bits(unsigned long sz)
  1487. {
  1488. if (tlb_type == hypervisor) {
  1489. switch (sz) {
  1490. case 8 * 1024:
  1491. default:
  1492. return _PAGE_SZ8K_4V;
  1493. case 64 * 1024:
  1494. return _PAGE_SZ64K_4V;
  1495. case 512 * 1024:
  1496. return _PAGE_SZ512K_4V;
  1497. case 4 * 1024 * 1024:
  1498. return _PAGE_SZ4MB_4V;
  1499. };
  1500. } else {
  1501. switch (sz) {
  1502. case 8 * 1024:
  1503. default:
  1504. return _PAGE_SZ8K_4U;
  1505. case 64 * 1024:
  1506. return _PAGE_SZ64K_4U;
  1507. case 512 * 1024:
  1508. return _PAGE_SZ512K_4U;
  1509. case 4 * 1024 * 1024:
  1510. return _PAGE_SZ4MB_4U;
  1511. };
  1512. }
  1513. }
  1514. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  1515. {
  1516. pte_t pte;
  1517. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  1518. pte_val(pte) |= (((unsigned long)space) << 32);
  1519. pte_val(pte) |= pte_sz_bits(page_size);
  1520. return pte;
  1521. }
  1522. static unsigned long kern_large_tte(unsigned long paddr)
  1523. {
  1524. unsigned long val;
  1525. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1526. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  1527. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  1528. if (tlb_type == hypervisor)
  1529. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1530. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  1531. _PAGE_EXEC_4V | _PAGE_W_4V);
  1532. return val | paddr;
  1533. }
  1534. /* If not locked, zap it. */
  1535. void __flush_tlb_all(void)
  1536. {
  1537. unsigned long pstate;
  1538. int i;
  1539. __asm__ __volatile__("flushw\n\t"
  1540. "rdpr %%pstate, %0\n\t"
  1541. "wrpr %0, %1, %%pstate"
  1542. : "=r" (pstate)
  1543. : "i" (PSTATE_IE));
  1544. if (tlb_type == hypervisor) {
  1545. sun4v_mmu_demap_all();
  1546. } else if (tlb_type == spitfire) {
  1547. for (i = 0; i < 64; i++) {
  1548. /* Spitfire Errata #32 workaround */
  1549. /* NOTE: Always runs on spitfire, so no
  1550. * cheetah+ page size encodings.
  1551. */
  1552. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1553. "flush %%g6"
  1554. : /* No outputs */
  1555. : "r" (0),
  1556. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1557. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  1558. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1559. "membar #Sync"
  1560. : /* no outputs */
  1561. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  1562. spitfire_put_dtlb_data(i, 0x0UL);
  1563. }
  1564. /* Spitfire Errata #32 workaround */
  1565. /* NOTE: Always runs on spitfire, so no
  1566. * cheetah+ page size encodings.
  1567. */
  1568. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1569. "flush %%g6"
  1570. : /* No outputs */
  1571. : "r" (0),
  1572. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1573. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  1574. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1575. "membar #Sync"
  1576. : /* no outputs */
  1577. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  1578. spitfire_put_itlb_data(i, 0x0UL);
  1579. }
  1580. }
  1581. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1582. cheetah_flush_dtlb_all();
  1583. cheetah_flush_itlb_all();
  1584. }
  1585. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  1586. : : "r" (pstate));
  1587. }
  1588. #ifdef CONFIG_MEMORY_HOTPLUG
  1589. void online_page(struct page *page)
  1590. {
  1591. ClearPageReserved(page);
  1592. init_page_count(page);
  1593. __free_page(page);
  1594. totalram_pages++;
  1595. num_physpages++;
  1596. }
  1597. #endif /* CONFIG_MEMORY_HOTPLUG */