init.c 50 KB

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