init.c 50 KB

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