init_64.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  1. /*
  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/initrd.h>
  16. #include <linux/swap.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/poison.h>
  19. #include <linux/fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/cache.h>
  23. #include <linux/sort.h>
  24. #include <linux/percpu.h>
  25. #include <linux/memblock.h>
  26. #include <linux/mmzone.h>
  27. #include <linux/gfp.h>
  28. #include <asm/head.h>
  29. #include <asm/page.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/oplib.h>
  33. #include <asm/iommu.h>
  34. #include <asm/io.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/dma.h>
  39. #include <asm/starfire.h>
  40. #include <asm/tlb.h>
  41. #include <asm/spitfire.h>
  42. #include <asm/sections.h>
  43. #include <asm/tsb.h>
  44. #include <asm/hypervisor.h>
  45. #include <asm/prom.h>
  46. #include <asm/mdesc.h>
  47. #include <asm/cpudata.h>
  48. #include <asm/irq.h>
  49. #include "init_64.h"
  50. unsigned long kern_linear_pte_xor[4] __read_mostly;
  51. /* A bitmap, two bits for every 256MB of physical memory. These two
  52. * bits determine what page size we use for kernel linear
  53. * translations. They form an index into kern_linear_pte_xor[]. The
  54. * value in the indexed slot is XOR'd with the TLB miss virtual
  55. * address to form the resulting TTE. The mapping is:
  56. *
  57. * 0 ==> 4MB
  58. * 1 ==> 256MB
  59. * 2 ==> 2GB
  60. * 3 ==> 16GB
  61. *
  62. * All sun4v chips support 256MB pages. Only SPARC-T4 and later
  63. * support 2GB pages, and hopefully future cpus will support the 16GB
  64. * pages as well. For slots 2 and 3, we encode a 256MB TTE xor there
  65. * if these larger page sizes are not supported by the cpu.
  66. *
  67. * It would be nice to determine this from the machine description
  68. * 'cpu' properties, but we need to have this table setup before the
  69. * MDESC is initialized.
  70. */
  71. unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
  72. #ifndef CONFIG_DEBUG_PAGEALLOC
  73. /* A special kernel TSB for 4MB, 256MB, 2GB and 16GB linear mappings.
  74. * Space is allocated for this right after the trap table in
  75. * arch/sparc64/kernel/head.S
  76. */
  77. extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
  78. #endif
  79. static unsigned long cpu_pgsz_mask;
  80. #define MAX_BANKS 32
  81. static struct linux_prom64_registers pavail[MAX_BANKS] __devinitdata;
  82. static int pavail_ents __devinitdata;
  83. static int cmp_p64(const void *a, const void *b)
  84. {
  85. const struct linux_prom64_registers *x = a, *y = b;
  86. if (x->phys_addr > y->phys_addr)
  87. return 1;
  88. if (x->phys_addr < y->phys_addr)
  89. return -1;
  90. return 0;
  91. }
  92. static void __init read_obp_memory(const char *property,
  93. struct linux_prom64_registers *regs,
  94. int *num_ents)
  95. {
  96. phandle node = prom_finddevice("/memory");
  97. int prop_size = prom_getproplen(node, property);
  98. int ents, ret, i;
  99. ents = prop_size / sizeof(struct linux_prom64_registers);
  100. if (ents > MAX_BANKS) {
  101. prom_printf("The machine has more %s property entries than "
  102. "this kernel can support (%d).\n",
  103. property, MAX_BANKS);
  104. prom_halt();
  105. }
  106. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  107. if (ret == -1) {
  108. prom_printf("Couldn't get %s property from /memory.\n");
  109. prom_halt();
  110. }
  111. /* Sanitize what we got from the firmware, by page aligning
  112. * everything.
  113. */
  114. for (i = 0; i < ents; i++) {
  115. unsigned long base, size;
  116. base = regs[i].phys_addr;
  117. size = regs[i].reg_size;
  118. size &= PAGE_MASK;
  119. if (base & ~PAGE_MASK) {
  120. unsigned long new_base = PAGE_ALIGN(base);
  121. size -= new_base - base;
  122. if ((long) size < 0L)
  123. size = 0UL;
  124. base = new_base;
  125. }
  126. if (size == 0UL) {
  127. /* If it is empty, simply get rid of it.
  128. * This simplifies the logic of the other
  129. * functions that process these arrays.
  130. */
  131. memmove(&regs[i], &regs[i + 1],
  132. (ents - i - 1) * sizeof(regs[0]));
  133. i--;
  134. ents--;
  135. continue;
  136. }
  137. regs[i].phys_addr = base;
  138. regs[i].reg_size = size;
  139. }
  140. *num_ents = ents;
  141. sort(regs, ents, sizeof(struct linux_prom64_registers),
  142. cmp_p64, NULL);
  143. }
  144. unsigned long sparc64_valid_addr_bitmap[VALID_ADDR_BITMAP_BYTES /
  145. sizeof(unsigned long)];
  146. EXPORT_SYMBOL(sparc64_valid_addr_bitmap);
  147. /* Kernel physical address base and size in bytes. */
  148. unsigned long kern_base __read_mostly;
  149. unsigned long kern_size __read_mostly;
  150. /* Initial ramdisk setup */
  151. extern unsigned long sparc_ramdisk_image64;
  152. extern unsigned int sparc_ramdisk_image;
  153. extern unsigned int sparc_ramdisk_size;
  154. struct page *mem_map_zero __read_mostly;
  155. EXPORT_SYMBOL(mem_map_zero);
  156. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  157. unsigned long sparc64_kern_pri_context __read_mostly;
  158. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  159. unsigned long sparc64_kern_sec_context __read_mostly;
  160. int num_kernel_image_mappings;
  161. #ifdef CONFIG_DEBUG_DCFLUSH
  162. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  163. #ifdef CONFIG_SMP
  164. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  165. #endif
  166. #endif
  167. inline void flush_dcache_page_impl(struct page *page)
  168. {
  169. BUG_ON(tlb_type == hypervisor);
  170. #ifdef CONFIG_DEBUG_DCFLUSH
  171. atomic_inc(&dcpage_flushes);
  172. #endif
  173. #ifdef DCACHE_ALIASING_POSSIBLE
  174. __flush_dcache_page(page_address(page),
  175. ((tlb_type == spitfire) &&
  176. page_mapping(page) != NULL));
  177. #else
  178. if (page_mapping(page) != NULL &&
  179. tlb_type == spitfire)
  180. __flush_icache_page(__pa(page_address(page)));
  181. #endif
  182. }
  183. #define PG_dcache_dirty PG_arch_1
  184. #define PG_dcache_cpu_shift 32UL
  185. #define PG_dcache_cpu_mask \
  186. ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
  187. #define dcache_dirty_cpu(page) \
  188. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  189. static inline void set_dcache_dirty(struct page *page, int this_cpu)
  190. {
  191. unsigned long mask = this_cpu;
  192. unsigned long non_cpu_bits;
  193. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  194. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  195. __asm__ __volatile__("1:\n\t"
  196. "ldx [%2], %%g7\n\t"
  197. "and %%g7, %1, %%g1\n\t"
  198. "or %%g1, %0, %%g1\n\t"
  199. "casx [%2], %%g7, %%g1\n\t"
  200. "cmp %%g7, %%g1\n\t"
  201. "bne,pn %%xcc, 1b\n\t"
  202. " nop"
  203. : /* no outputs */
  204. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  205. : "g1", "g7");
  206. }
  207. static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  208. {
  209. unsigned long mask = (1UL << PG_dcache_dirty);
  210. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  211. "1:\n\t"
  212. "ldx [%2], %%g7\n\t"
  213. "srlx %%g7, %4, %%g1\n\t"
  214. "and %%g1, %3, %%g1\n\t"
  215. "cmp %%g1, %0\n\t"
  216. "bne,pn %%icc, 2f\n\t"
  217. " andn %%g7, %1, %%g1\n\t"
  218. "casx [%2], %%g7, %%g1\n\t"
  219. "cmp %%g7, %%g1\n\t"
  220. "bne,pn %%xcc, 1b\n\t"
  221. " nop\n"
  222. "2:"
  223. : /* no outputs */
  224. : "r" (cpu), "r" (mask), "r" (&page->flags),
  225. "i" (PG_dcache_cpu_mask),
  226. "i" (PG_dcache_cpu_shift)
  227. : "g1", "g7");
  228. }
  229. static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
  230. {
  231. unsigned long tsb_addr = (unsigned long) ent;
  232. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  233. tsb_addr = __pa(tsb_addr);
  234. __tsb_insert(tsb_addr, tag, pte);
  235. }
  236. unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
  237. unsigned long _PAGE_SZBITS __read_mostly;
  238. static void flush_dcache(unsigned long pfn)
  239. {
  240. struct page *page;
  241. page = pfn_to_page(pfn);
  242. if (page) {
  243. unsigned long pg_flags;
  244. pg_flags = page->flags;
  245. if (pg_flags & (1UL << PG_dcache_dirty)) {
  246. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  247. PG_dcache_cpu_mask);
  248. int this_cpu = get_cpu();
  249. /* This is just to optimize away some function calls
  250. * in the SMP case.
  251. */
  252. if (cpu == this_cpu)
  253. flush_dcache_page_impl(page);
  254. else
  255. smp_flush_dcache_page_impl(page, cpu);
  256. clear_dcache_dirty_cpu(page, cpu);
  257. put_cpu();
  258. }
  259. }
  260. }
  261. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
  262. {
  263. struct mm_struct *mm;
  264. struct tsb *tsb;
  265. unsigned long tag, flags;
  266. unsigned long tsb_index, tsb_hash_shift;
  267. pte_t pte = *ptep;
  268. if (tlb_type != hypervisor) {
  269. unsigned long pfn = pte_pfn(pte);
  270. if (pfn_valid(pfn))
  271. flush_dcache(pfn);
  272. }
  273. mm = vma->vm_mm;
  274. tsb_index = MM_TSB_BASE;
  275. tsb_hash_shift = PAGE_SHIFT;
  276. spin_lock_irqsave(&mm->context.lock, flags);
  277. #ifdef CONFIG_HUGETLB_PAGE
  278. if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
  279. if ((tlb_type == hypervisor &&
  280. (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
  281. (tlb_type != hypervisor &&
  282. (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
  283. tsb_index = MM_TSB_HUGE;
  284. tsb_hash_shift = HPAGE_SHIFT;
  285. }
  286. }
  287. #endif
  288. tsb = mm->context.tsb_block[tsb_index].tsb;
  289. tsb += ((address >> tsb_hash_shift) &
  290. (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
  291. tag = (address >> 22UL);
  292. tsb_insert(tsb, tag, pte_val(pte));
  293. spin_unlock_irqrestore(&mm->context.lock, flags);
  294. }
  295. void flush_dcache_page(struct page *page)
  296. {
  297. struct address_space *mapping;
  298. int this_cpu;
  299. if (tlb_type == hypervisor)
  300. return;
  301. /* Do not bother with the expensive D-cache flush if it
  302. * is merely the zero page. The 'bigcore' testcase in GDB
  303. * causes this case to run millions of times.
  304. */
  305. if (page == ZERO_PAGE(0))
  306. return;
  307. this_cpu = get_cpu();
  308. mapping = page_mapping(page);
  309. if (mapping && !mapping_mapped(mapping)) {
  310. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  311. if (dirty) {
  312. int dirty_cpu = dcache_dirty_cpu(page);
  313. if (dirty_cpu == this_cpu)
  314. goto out;
  315. smp_flush_dcache_page_impl(page, dirty_cpu);
  316. }
  317. set_dcache_dirty(page, this_cpu);
  318. } else {
  319. /* We could delay the flush for the !page_mapping
  320. * case too. But that case is for exec env/arg
  321. * pages and those are %99 certainly going to get
  322. * faulted into the tlb (and thus flushed) anyways.
  323. */
  324. flush_dcache_page_impl(page);
  325. }
  326. out:
  327. put_cpu();
  328. }
  329. EXPORT_SYMBOL(flush_dcache_page);
  330. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  331. {
  332. /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
  333. if (tlb_type == spitfire) {
  334. unsigned long kaddr;
  335. /* This code only runs on Spitfire cpus so this is
  336. * why we can assume _PAGE_PADDR_4U.
  337. */
  338. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
  339. unsigned long paddr, mask = _PAGE_PADDR_4U;
  340. if (kaddr >= PAGE_OFFSET)
  341. paddr = kaddr & mask;
  342. else {
  343. pgd_t *pgdp = pgd_offset_k(kaddr);
  344. pud_t *pudp = pud_offset(pgdp, kaddr);
  345. pmd_t *pmdp = pmd_offset(pudp, kaddr);
  346. pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
  347. paddr = pte_val(*ptep) & mask;
  348. }
  349. __flush_icache_page(paddr);
  350. }
  351. }
  352. }
  353. EXPORT_SYMBOL(flush_icache_range);
  354. void mmu_info(struct seq_file *m)
  355. {
  356. static const char *pgsz_strings[] = {
  357. "8K", "64K", "512K", "4MB", "32MB",
  358. "256MB", "2GB", "16GB",
  359. };
  360. int i, printed;
  361. if (tlb_type == cheetah)
  362. seq_printf(m, "MMU Type\t: Cheetah\n");
  363. else if (tlb_type == cheetah_plus)
  364. seq_printf(m, "MMU Type\t: Cheetah+\n");
  365. else if (tlb_type == spitfire)
  366. seq_printf(m, "MMU Type\t: Spitfire\n");
  367. else if (tlb_type == hypervisor)
  368. seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
  369. else
  370. seq_printf(m, "MMU Type\t: ???\n");
  371. seq_printf(m, "MMU PGSZs\t: ");
  372. printed = 0;
  373. for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
  374. if (cpu_pgsz_mask & (1UL << i)) {
  375. seq_printf(m, "%s%s",
  376. printed ? "," : "", pgsz_strings[i]);
  377. printed++;
  378. }
  379. }
  380. seq_putc(m, '\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 prom_trans[512] __read_mostly;
  391. unsigned int prom_trans_ents __read_mostly;
  392. unsigned long kern_locked_tte_data;
  393. /* The obp translations are saved based on 8k pagesize, since obp can
  394. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  395. * HI_OBP_ADDRESS range are handled in ktlb.S.
  396. */
  397. static inline int in_obp_range(unsigned long vaddr)
  398. {
  399. return (vaddr >= LOW_OBP_ADDRESS &&
  400. vaddr < HI_OBP_ADDRESS);
  401. }
  402. static int cmp_ptrans(const void *a, const void *b)
  403. {
  404. const struct linux_prom_translation *x = a, *y = b;
  405. if (x->virt > y->virt)
  406. return 1;
  407. if (x->virt < y->virt)
  408. return -1;
  409. return 0;
  410. }
  411. /* Read OBP translations property into 'prom_trans[]'. */
  412. static void __init read_obp_translations(void)
  413. {
  414. int n, node, ents, first, last, i;
  415. node = prom_finddevice("/virtual-memory");
  416. n = prom_getproplen(node, "translations");
  417. if (unlikely(n == 0 || n == -1)) {
  418. prom_printf("prom_mappings: Couldn't get size.\n");
  419. prom_halt();
  420. }
  421. if (unlikely(n > sizeof(prom_trans))) {
  422. prom_printf("prom_mappings: Size %Zd is too big.\n", n);
  423. prom_halt();
  424. }
  425. if ((n = prom_getproperty(node, "translations",
  426. (char *)&prom_trans[0],
  427. sizeof(prom_trans))) == -1) {
  428. prom_printf("prom_mappings: Couldn't get property.\n");
  429. prom_halt();
  430. }
  431. n = n / sizeof(struct linux_prom_translation);
  432. ents = n;
  433. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  434. cmp_ptrans, NULL);
  435. /* Now kick out all the non-OBP entries. */
  436. for (i = 0; i < ents; i++) {
  437. if (in_obp_range(prom_trans[i].virt))
  438. break;
  439. }
  440. first = i;
  441. for (; i < ents; i++) {
  442. if (!in_obp_range(prom_trans[i].virt))
  443. break;
  444. }
  445. last = i;
  446. for (i = 0; i < (last - first); i++) {
  447. struct linux_prom_translation *src = &prom_trans[i + first];
  448. struct linux_prom_translation *dest = &prom_trans[i];
  449. *dest = *src;
  450. }
  451. for (; i < ents; i++) {
  452. struct linux_prom_translation *dest = &prom_trans[i];
  453. dest->virt = dest->size = dest->data = 0x0UL;
  454. }
  455. prom_trans_ents = last - first;
  456. if (tlb_type == spitfire) {
  457. /* Clear diag TTE bits. */
  458. for (i = 0; i < prom_trans_ents; i++)
  459. prom_trans[i].data &= ~0x0003fe0000000000UL;
  460. }
  461. /* Force execute bit on. */
  462. for (i = 0; i < prom_trans_ents; i++)
  463. prom_trans[i].data |= (tlb_type == hypervisor ?
  464. _PAGE_EXEC_4V : _PAGE_EXEC_4U);
  465. }
  466. static void __init hypervisor_tlb_lock(unsigned long vaddr,
  467. unsigned long pte,
  468. unsigned long mmu)
  469. {
  470. unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
  471. if (ret != 0) {
  472. prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
  473. "errors with %lx\n", vaddr, 0, pte, mmu, ret);
  474. prom_halt();
  475. }
  476. }
  477. static unsigned long kern_large_tte(unsigned long paddr);
  478. static void __init remap_kernel(void)
  479. {
  480. unsigned long phys_page, tte_vaddr, tte_data;
  481. int i, tlb_ent = sparc64_highest_locked_tlbent();
  482. tte_vaddr = (unsigned long) KERNBASE;
  483. phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  484. tte_data = kern_large_tte(phys_page);
  485. kern_locked_tte_data = tte_data;
  486. /* Now lock us into the TLBs via Hypervisor or OBP. */
  487. if (tlb_type == hypervisor) {
  488. for (i = 0; i < num_kernel_image_mappings; i++) {
  489. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  490. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  491. tte_vaddr += 0x400000;
  492. tte_data += 0x400000;
  493. }
  494. } else {
  495. for (i = 0; i < num_kernel_image_mappings; i++) {
  496. prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
  497. prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
  498. tte_vaddr += 0x400000;
  499. tte_data += 0x400000;
  500. }
  501. sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
  502. }
  503. if (tlb_type == cheetah_plus) {
  504. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  505. CTX_CHEETAH_PLUS_NUC);
  506. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  507. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  508. }
  509. }
  510. static void __init inherit_prom_mappings(void)
  511. {
  512. /* Now fixup OBP's idea about where we really are mapped. */
  513. printk("Remapping the kernel... ");
  514. remap_kernel();
  515. printk("done.\n");
  516. }
  517. void prom_world(int enter)
  518. {
  519. if (!enter)
  520. set_fs((mm_segment_t) { get_thread_current_ds() });
  521. __asm__ __volatile__("flushw");
  522. }
  523. void __flush_dcache_range(unsigned long start, unsigned long end)
  524. {
  525. unsigned long va;
  526. if (tlb_type == spitfire) {
  527. int n = 0;
  528. for (va = start; va < end; va += 32) {
  529. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  530. if (++n >= 512)
  531. break;
  532. }
  533. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  534. start = __pa(start);
  535. end = __pa(end);
  536. for (va = start; va < end; va += 32)
  537. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  538. "membar #Sync"
  539. : /* no outputs */
  540. : "r" (va),
  541. "i" (ASI_DCACHE_INVALIDATE));
  542. }
  543. }
  544. EXPORT_SYMBOL(__flush_dcache_range);
  545. /* get_new_mmu_context() uses "cache + 1". */
  546. DEFINE_SPINLOCK(ctx_alloc_lock);
  547. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  548. #define MAX_CTX_NR (1UL << CTX_NR_BITS)
  549. #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
  550. DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
  551. /* Caller does TLB context flushing on local CPU if necessary.
  552. * The caller also ensures that CTX_VALID(mm->context) is false.
  553. *
  554. * We must be careful about boundary cases so that we never
  555. * let the user have CTX 0 (nucleus) or we ever use a CTX
  556. * version of zero (and thus NO_CONTEXT would not be caught
  557. * by version mis-match tests in mmu_context.h).
  558. *
  559. * Always invoked with interrupts disabled.
  560. */
  561. void get_new_mmu_context(struct mm_struct *mm)
  562. {
  563. unsigned long ctx, new_ctx;
  564. unsigned long orig_pgsz_bits;
  565. unsigned long flags;
  566. int new_version;
  567. spin_lock_irqsave(&ctx_alloc_lock, flags);
  568. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  569. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  570. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  571. new_version = 0;
  572. if (new_ctx >= (1 << CTX_NR_BITS)) {
  573. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  574. if (new_ctx >= ctx) {
  575. int i;
  576. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  577. CTX_FIRST_VERSION;
  578. if (new_ctx == 1)
  579. new_ctx = CTX_FIRST_VERSION;
  580. /* Don't call memset, for 16 entries that's just
  581. * plain silly...
  582. */
  583. mmu_context_bmap[0] = 3;
  584. mmu_context_bmap[1] = 0;
  585. mmu_context_bmap[2] = 0;
  586. mmu_context_bmap[3] = 0;
  587. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  588. mmu_context_bmap[i + 0] = 0;
  589. mmu_context_bmap[i + 1] = 0;
  590. mmu_context_bmap[i + 2] = 0;
  591. mmu_context_bmap[i + 3] = 0;
  592. }
  593. new_version = 1;
  594. goto out;
  595. }
  596. }
  597. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  598. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  599. out:
  600. tlb_context_cache = new_ctx;
  601. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  602. spin_unlock_irqrestore(&ctx_alloc_lock, flags);
  603. if (unlikely(new_version))
  604. smp_new_mmu_context_version();
  605. }
  606. static int numa_enabled = 1;
  607. static int numa_debug;
  608. static int __init early_numa(char *p)
  609. {
  610. if (!p)
  611. return 0;
  612. if (strstr(p, "off"))
  613. numa_enabled = 0;
  614. if (strstr(p, "debug"))
  615. numa_debug = 1;
  616. return 0;
  617. }
  618. early_param("numa", early_numa);
  619. #define numadbg(f, a...) \
  620. do { if (numa_debug) \
  621. printk(KERN_INFO f, ## a); \
  622. } while (0)
  623. static void __init find_ramdisk(unsigned long phys_base)
  624. {
  625. #ifdef CONFIG_BLK_DEV_INITRD
  626. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  627. unsigned long ramdisk_image;
  628. /* Older versions of the bootloader only supported a
  629. * 32-bit physical address for the ramdisk image
  630. * location, stored at sparc_ramdisk_image. Newer
  631. * SILO versions set sparc_ramdisk_image to zero and
  632. * provide a full 64-bit physical address at
  633. * sparc_ramdisk_image64.
  634. */
  635. ramdisk_image = sparc_ramdisk_image;
  636. if (!ramdisk_image)
  637. ramdisk_image = sparc_ramdisk_image64;
  638. /* Another bootloader quirk. The bootloader normalizes
  639. * the physical address to KERNBASE, so we have to
  640. * factor that back out and add in the lowest valid
  641. * physical page address to get the true physical address.
  642. */
  643. ramdisk_image -= KERNBASE;
  644. ramdisk_image += phys_base;
  645. numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
  646. ramdisk_image, sparc_ramdisk_size);
  647. initrd_start = ramdisk_image;
  648. initrd_end = ramdisk_image + sparc_ramdisk_size;
  649. memblock_reserve(initrd_start, sparc_ramdisk_size);
  650. initrd_start += PAGE_OFFSET;
  651. initrd_end += PAGE_OFFSET;
  652. }
  653. #endif
  654. }
  655. struct node_mem_mask {
  656. unsigned long mask;
  657. unsigned long val;
  658. };
  659. static struct node_mem_mask node_masks[MAX_NUMNODES];
  660. static int num_node_masks;
  661. int numa_cpu_lookup_table[NR_CPUS];
  662. cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
  663. #ifdef CONFIG_NEED_MULTIPLE_NODES
  664. struct mdesc_mblock {
  665. u64 base;
  666. u64 size;
  667. u64 offset; /* RA-to-PA */
  668. };
  669. static struct mdesc_mblock *mblocks;
  670. static int num_mblocks;
  671. static unsigned long ra_to_pa(unsigned long addr)
  672. {
  673. int i;
  674. for (i = 0; i < num_mblocks; i++) {
  675. struct mdesc_mblock *m = &mblocks[i];
  676. if (addr >= m->base &&
  677. addr < (m->base + m->size)) {
  678. addr += m->offset;
  679. break;
  680. }
  681. }
  682. return addr;
  683. }
  684. static int find_node(unsigned long addr)
  685. {
  686. int i;
  687. addr = ra_to_pa(addr);
  688. for (i = 0; i < num_node_masks; i++) {
  689. struct node_mem_mask *p = &node_masks[i];
  690. if ((addr & p->mask) == p->val)
  691. return i;
  692. }
  693. return -1;
  694. }
  695. static u64 memblock_nid_range(u64 start, u64 end, int *nid)
  696. {
  697. *nid = find_node(start);
  698. start += PAGE_SIZE;
  699. while (start < end) {
  700. int n = find_node(start);
  701. if (n != *nid)
  702. break;
  703. start += PAGE_SIZE;
  704. }
  705. if (start > end)
  706. start = end;
  707. return start;
  708. }
  709. #endif
  710. /* This must be invoked after performing all of the necessary
  711. * memblock_set_node() calls for 'nid'. We need to be able to get
  712. * correct data from get_pfn_range_for_nid().
  713. */
  714. static void __init allocate_node_data(int nid)
  715. {
  716. struct pglist_data *p;
  717. unsigned long start_pfn, end_pfn;
  718. #ifdef CONFIG_NEED_MULTIPLE_NODES
  719. unsigned long paddr;
  720. paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
  721. if (!paddr) {
  722. prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
  723. prom_halt();
  724. }
  725. NODE_DATA(nid) = __va(paddr);
  726. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  727. NODE_DATA(nid)->node_id = nid;
  728. #endif
  729. p = NODE_DATA(nid);
  730. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  731. p->node_start_pfn = start_pfn;
  732. p->node_spanned_pages = end_pfn - start_pfn;
  733. }
  734. static void init_node_masks_nonnuma(void)
  735. {
  736. int i;
  737. numadbg("Initializing tables for non-numa.\n");
  738. node_masks[0].mask = node_masks[0].val = 0;
  739. num_node_masks = 1;
  740. for (i = 0; i < NR_CPUS; i++)
  741. numa_cpu_lookup_table[i] = 0;
  742. cpumask_setall(&numa_cpumask_lookup_table[0]);
  743. }
  744. #ifdef CONFIG_NEED_MULTIPLE_NODES
  745. struct pglist_data *node_data[MAX_NUMNODES];
  746. EXPORT_SYMBOL(numa_cpu_lookup_table);
  747. EXPORT_SYMBOL(numa_cpumask_lookup_table);
  748. EXPORT_SYMBOL(node_data);
  749. struct mdesc_mlgroup {
  750. u64 node;
  751. u64 latency;
  752. u64 match;
  753. u64 mask;
  754. };
  755. static struct mdesc_mlgroup *mlgroups;
  756. static int num_mlgroups;
  757. static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
  758. u32 cfg_handle)
  759. {
  760. u64 arc;
  761. mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
  762. u64 target = mdesc_arc_target(md, arc);
  763. const u64 *val;
  764. val = mdesc_get_property(md, target,
  765. "cfg-handle", NULL);
  766. if (val && *val == cfg_handle)
  767. return 0;
  768. }
  769. return -ENODEV;
  770. }
  771. static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
  772. u32 cfg_handle)
  773. {
  774. u64 arc, candidate, best_latency = ~(u64)0;
  775. candidate = MDESC_NODE_NULL;
  776. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
  777. u64 target = mdesc_arc_target(md, arc);
  778. const char *name = mdesc_node_name(md, target);
  779. const u64 *val;
  780. if (strcmp(name, "pio-latency-group"))
  781. continue;
  782. val = mdesc_get_property(md, target, "latency", NULL);
  783. if (!val)
  784. continue;
  785. if (*val < best_latency) {
  786. candidate = target;
  787. best_latency = *val;
  788. }
  789. }
  790. if (candidate == MDESC_NODE_NULL)
  791. return -ENODEV;
  792. return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
  793. }
  794. int of_node_to_nid(struct device_node *dp)
  795. {
  796. const struct linux_prom64_registers *regs;
  797. struct mdesc_handle *md;
  798. u32 cfg_handle;
  799. int count, nid;
  800. u64 grp;
  801. /* This is the right thing to do on currently supported
  802. * SUN4U NUMA platforms as well, as the PCI controller does
  803. * not sit behind any particular memory controller.
  804. */
  805. if (!mlgroups)
  806. return -1;
  807. regs = of_get_property(dp, "reg", NULL);
  808. if (!regs)
  809. return -1;
  810. cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  811. md = mdesc_grab();
  812. count = 0;
  813. nid = -1;
  814. mdesc_for_each_node_by_name(md, grp, "group") {
  815. if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
  816. nid = count;
  817. break;
  818. }
  819. count++;
  820. }
  821. mdesc_release(md);
  822. return nid;
  823. }
  824. static void __init add_node_ranges(void)
  825. {
  826. struct memblock_region *reg;
  827. for_each_memblock(memory, reg) {
  828. unsigned long size = reg->size;
  829. unsigned long start, end;
  830. start = reg->base;
  831. end = start + size;
  832. while (start < end) {
  833. unsigned long this_end;
  834. int nid;
  835. this_end = memblock_nid_range(start, end, &nid);
  836. numadbg("Setting memblock NUMA node nid[%d] "
  837. "start[%lx] end[%lx]\n",
  838. nid, start, this_end);
  839. memblock_set_node(start, this_end - start, nid);
  840. start = this_end;
  841. }
  842. }
  843. }
  844. static int __init grab_mlgroups(struct mdesc_handle *md)
  845. {
  846. unsigned long paddr;
  847. int count = 0;
  848. u64 node;
  849. mdesc_for_each_node_by_name(md, node, "memory-latency-group")
  850. count++;
  851. if (!count)
  852. return -ENOENT;
  853. paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
  854. SMP_CACHE_BYTES);
  855. if (!paddr)
  856. return -ENOMEM;
  857. mlgroups = __va(paddr);
  858. num_mlgroups = count;
  859. count = 0;
  860. mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
  861. struct mdesc_mlgroup *m = &mlgroups[count++];
  862. const u64 *val;
  863. m->node = node;
  864. val = mdesc_get_property(md, node, "latency", NULL);
  865. m->latency = *val;
  866. val = mdesc_get_property(md, node, "address-match", NULL);
  867. m->match = *val;
  868. val = mdesc_get_property(md, node, "address-mask", NULL);
  869. m->mask = *val;
  870. numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
  871. "match[%llx] mask[%llx]\n",
  872. count - 1, m->node, m->latency, m->match, m->mask);
  873. }
  874. return 0;
  875. }
  876. static int __init grab_mblocks(struct mdesc_handle *md)
  877. {
  878. unsigned long paddr;
  879. int count = 0;
  880. u64 node;
  881. mdesc_for_each_node_by_name(md, node, "mblock")
  882. count++;
  883. if (!count)
  884. return -ENOENT;
  885. paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
  886. SMP_CACHE_BYTES);
  887. if (!paddr)
  888. return -ENOMEM;
  889. mblocks = __va(paddr);
  890. num_mblocks = count;
  891. count = 0;
  892. mdesc_for_each_node_by_name(md, node, "mblock") {
  893. struct mdesc_mblock *m = &mblocks[count++];
  894. const u64 *val;
  895. val = mdesc_get_property(md, node, "base", NULL);
  896. m->base = *val;
  897. val = mdesc_get_property(md, node, "size", NULL);
  898. m->size = *val;
  899. val = mdesc_get_property(md, node,
  900. "address-congruence-offset", NULL);
  901. m->offset = *val;
  902. numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
  903. count - 1, m->base, m->size, m->offset);
  904. }
  905. return 0;
  906. }
  907. static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
  908. u64 grp, cpumask_t *mask)
  909. {
  910. u64 arc;
  911. cpumask_clear(mask);
  912. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
  913. u64 target = mdesc_arc_target(md, arc);
  914. const char *name = mdesc_node_name(md, target);
  915. const u64 *id;
  916. if (strcmp(name, "cpu"))
  917. continue;
  918. id = mdesc_get_property(md, target, "id", NULL);
  919. if (*id < nr_cpu_ids)
  920. cpumask_set_cpu(*id, mask);
  921. }
  922. }
  923. static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
  924. {
  925. int i;
  926. for (i = 0; i < num_mlgroups; i++) {
  927. struct mdesc_mlgroup *m = &mlgroups[i];
  928. if (m->node == node)
  929. return m;
  930. }
  931. return NULL;
  932. }
  933. static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
  934. int index)
  935. {
  936. struct mdesc_mlgroup *candidate = NULL;
  937. u64 arc, best_latency = ~(u64)0;
  938. struct node_mem_mask *n;
  939. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
  940. u64 target = mdesc_arc_target(md, arc);
  941. struct mdesc_mlgroup *m = find_mlgroup(target);
  942. if (!m)
  943. continue;
  944. if (m->latency < best_latency) {
  945. candidate = m;
  946. best_latency = m->latency;
  947. }
  948. }
  949. if (!candidate)
  950. return -ENOENT;
  951. if (num_node_masks != index) {
  952. printk(KERN_ERR "Inconsistent NUMA state, "
  953. "index[%d] != num_node_masks[%d]\n",
  954. index, num_node_masks);
  955. return -EINVAL;
  956. }
  957. n = &node_masks[num_node_masks++];
  958. n->mask = candidate->mask;
  959. n->val = candidate->match;
  960. numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%llx])\n",
  961. index, n->mask, n->val, candidate->latency);
  962. return 0;
  963. }
  964. static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
  965. int index)
  966. {
  967. cpumask_t mask;
  968. int cpu;
  969. numa_parse_mdesc_group_cpus(md, grp, &mask);
  970. for_each_cpu(cpu, &mask)
  971. numa_cpu_lookup_table[cpu] = index;
  972. cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
  973. if (numa_debug) {
  974. printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
  975. for_each_cpu(cpu, &mask)
  976. printk("%d ", cpu);
  977. printk("]\n");
  978. }
  979. return numa_attach_mlgroup(md, grp, index);
  980. }
  981. static int __init numa_parse_mdesc(void)
  982. {
  983. struct mdesc_handle *md = mdesc_grab();
  984. int i, err, count;
  985. u64 node;
  986. node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
  987. if (node == MDESC_NODE_NULL) {
  988. mdesc_release(md);
  989. return -ENOENT;
  990. }
  991. err = grab_mblocks(md);
  992. if (err < 0)
  993. goto out;
  994. err = grab_mlgroups(md);
  995. if (err < 0)
  996. goto out;
  997. count = 0;
  998. mdesc_for_each_node_by_name(md, node, "group") {
  999. err = numa_parse_mdesc_group(md, node, count);
  1000. if (err < 0)
  1001. break;
  1002. count++;
  1003. }
  1004. add_node_ranges();
  1005. for (i = 0; i < num_node_masks; i++) {
  1006. allocate_node_data(i);
  1007. node_set_online(i);
  1008. }
  1009. err = 0;
  1010. out:
  1011. mdesc_release(md);
  1012. return err;
  1013. }
  1014. static int __init numa_parse_jbus(void)
  1015. {
  1016. unsigned long cpu, index;
  1017. /* NUMA node id is encoded in bits 36 and higher, and there is
  1018. * a 1-to-1 mapping from CPU ID to NUMA node ID.
  1019. */
  1020. index = 0;
  1021. for_each_present_cpu(cpu) {
  1022. numa_cpu_lookup_table[cpu] = index;
  1023. cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
  1024. node_masks[index].mask = ~((1UL << 36UL) - 1UL);
  1025. node_masks[index].val = cpu << 36UL;
  1026. index++;
  1027. }
  1028. num_node_masks = index;
  1029. add_node_ranges();
  1030. for (index = 0; index < num_node_masks; index++) {
  1031. allocate_node_data(index);
  1032. node_set_online(index);
  1033. }
  1034. return 0;
  1035. }
  1036. static int __init numa_parse_sun4u(void)
  1037. {
  1038. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1039. unsigned long ver;
  1040. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  1041. if ((ver >> 32UL) == __JALAPENO_ID ||
  1042. (ver >> 32UL) == __SERRANO_ID)
  1043. return numa_parse_jbus();
  1044. }
  1045. return -1;
  1046. }
  1047. static int __init bootmem_init_numa(void)
  1048. {
  1049. int err = -1;
  1050. numadbg("bootmem_init_numa()\n");
  1051. if (numa_enabled) {
  1052. if (tlb_type == hypervisor)
  1053. err = numa_parse_mdesc();
  1054. else
  1055. err = numa_parse_sun4u();
  1056. }
  1057. return err;
  1058. }
  1059. #else
  1060. static int bootmem_init_numa(void)
  1061. {
  1062. return -1;
  1063. }
  1064. #endif
  1065. static void __init bootmem_init_nonnuma(void)
  1066. {
  1067. unsigned long top_of_ram = memblock_end_of_DRAM();
  1068. unsigned long total_ram = memblock_phys_mem_size();
  1069. numadbg("bootmem_init_nonnuma()\n");
  1070. printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  1071. top_of_ram, total_ram);
  1072. printk(KERN_INFO "Memory hole size: %ldMB\n",
  1073. (top_of_ram - total_ram) >> 20);
  1074. init_node_masks_nonnuma();
  1075. memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
  1076. allocate_node_data(0);
  1077. node_set_online(0);
  1078. }
  1079. static unsigned long __init bootmem_init(unsigned long phys_base)
  1080. {
  1081. unsigned long end_pfn;
  1082. end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  1083. max_pfn = max_low_pfn = end_pfn;
  1084. min_low_pfn = (phys_base >> PAGE_SHIFT);
  1085. if (bootmem_init_numa() < 0)
  1086. bootmem_init_nonnuma();
  1087. /* Dump memblock with node info. */
  1088. memblock_dump_all();
  1089. /* XXX cpu notifier XXX */
  1090. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  1091. sparse_init();
  1092. return end_pfn;
  1093. }
  1094. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  1095. static int pall_ents __initdata;
  1096. #ifdef CONFIG_DEBUG_PAGEALLOC
  1097. static unsigned long __ref kernel_map_range(unsigned long pstart,
  1098. unsigned long pend, pgprot_t prot)
  1099. {
  1100. unsigned long vstart = PAGE_OFFSET + pstart;
  1101. unsigned long vend = PAGE_OFFSET + pend;
  1102. unsigned long alloc_bytes = 0UL;
  1103. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  1104. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  1105. vstart, vend);
  1106. prom_halt();
  1107. }
  1108. while (vstart < vend) {
  1109. unsigned long this_end, paddr = __pa(vstart);
  1110. pgd_t *pgd = pgd_offset_k(vstart);
  1111. pud_t *pud;
  1112. pmd_t *pmd;
  1113. pte_t *pte;
  1114. pud = pud_offset(pgd, vstart);
  1115. if (pud_none(*pud)) {
  1116. pmd_t *new;
  1117. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1118. alloc_bytes += PAGE_SIZE;
  1119. pud_populate(&init_mm, pud, new);
  1120. }
  1121. pmd = pmd_offset(pud, vstart);
  1122. if (!pmd_present(*pmd)) {
  1123. pte_t *new;
  1124. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1125. alloc_bytes += PAGE_SIZE;
  1126. pmd_populate_kernel(&init_mm, pmd, new);
  1127. }
  1128. pte = pte_offset_kernel(pmd, vstart);
  1129. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  1130. if (this_end > vend)
  1131. this_end = vend;
  1132. while (vstart < this_end) {
  1133. pte_val(*pte) = (paddr | pgprot_val(prot));
  1134. vstart += PAGE_SIZE;
  1135. paddr += PAGE_SIZE;
  1136. pte++;
  1137. }
  1138. }
  1139. return alloc_bytes;
  1140. }
  1141. extern unsigned int kvmap_linear_patch[1];
  1142. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1143. static void __init kpte_set_val(unsigned long index, unsigned long val)
  1144. {
  1145. unsigned long *ptr = kpte_linear_bitmap;
  1146. val <<= ((index % (BITS_PER_LONG / 2)) * 2);
  1147. ptr += (index / (BITS_PER_LONG / 2));
  1148. *ptr |= val;
  1149. }
  1150. static const unsigned long kpte_shift_min = 28; /* 256MB */
  1151. static const unsigned long kpte_shift_max = 34; /* 16GB */
  1152. static const unsigned long kpte_shift_incr = 3;
  1153. static unsigned long kpte_mark_using_shift(unsigned long start, unsigned long end,
  1154. unsigned long shift)
  1155. {
  1156. unsigned long size = (1UL << shift);
  1157. unsigned long mask = (size - 1UL);
  1158. unsigned long remains = end - start;
  1159. unsigned long val;
  1160. if (remains < size || (start & mask))
  1161. return start;
  1162. /* VAL maps:
  1163. *
  1164. * shift 28 --> kern_linear_pte_xor index 1
  1165. * shift 31 --> kern_linear_pte_xor index 2
  1166. * shift 34 --> kern_linear_pte_xor index 3
  1167. */
  1168. val = ((shift - kpte_shift_min) / kpte_shift_incr) + 1;
  1169. remains &= ~mask;
  1170. if (shift != kpte_shift_max)
  1171. remains = size;
  1172. while (remains) {
  1173. unsigned long index = start >> kpte_shift_min;
  1174. kpte_set_val(index, val);
  1175. start += 1UL << kpte_shift_min;
  1176. remains -= 1UL << kpte_shift_min;
  1177. }
  1178. return start;
  1179. }
  1180. static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
  1181. {
  1182. unsigned long smallest_size, smallest_mask;
  1183. unsigned long s;
  1184. smallest_size = (1UL << kpte_shift_min);
  1185. smallest_mask = (smallest_size - 1UL);
  1186. while (start < end) {
  1187. unsigned long orig_start = start;
  1188. for (s = kpte_shift_max; s >= kpte_shift_min; s -= kpte_shift_incr) {
  1189. start = kpte_mark_using_shift(start, end, s);
  1190. if (start != orig_start)
  1191. break;
  1192. }
  1193. if (start == orig_start)
  1194. start = (start + smallest_size) & ~smallest_mask;
  1195. }
  1196. }
  1197. static void __init init_kpte_bitmap(void)
  1198. {
  1199. unsigned long i;
  1200. for (i = 0; i < pall_ents; i++) {
  1201. unsigned long phys_start, phys_end;
  1202. phys_start = pall[i].phys_addr;
  1203. phys_end = phys_start + pall[i].reg_size;
  1204. mark_kpte_bitmap(phys_start, phys_end);
  1205. }
  1206. }
  1207. static void __init kernel_physical_mapping_init(void)
  1208. {
  1209. #ifdef CONFIG_DEBUG_PAGEALLOC
  1210. unsigned long i, mem_alloced = 0UL;
  1211. for (i = 0; i < pall_ents; i++) {
  1212. unsigned long phys_start, phys_end;
  1213. phys_start = pall[i].phys_addr;
  1214. phys_end = phys_start + pall[i].reg_size;
  1215. mem_alloced += kernel_map_range(phys_start, phys_end,
  1216. PAGE_KERNEL);
  1217. }
  1218. printk("Allocated %ld bytes for kernel page tables.\n",
  1219. mem_alloced);
  1220. kvmap_linear_patch[0] = 0x01000000; /* nop */
  1221. flushi(&kvmap_linear_patch[0]);
  1222. __flush_tlb_all();
  1223. #endif
  1224. }
  1225. #ifdef CONFIG_DEBUG_PAGEALLOC
  1226. void kernel_map_pages(struct page *page, int numpages, int enable)
  1227. {
  1228. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  1229. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  1230. kernel_map_range(phys_start, phys_end,
  1231. (enable ? PAGE_KERNEL : __pgprot(0)));
  1232. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  1233. PAGE_OFFSET + phys_end);
  1234. /* we should perform an IPI and flush all tlbs,
  1235. * but that can deadlock->flush only current cpu.
  1236. */
  1237. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  1238. PAGE_OFFSET + phys_end);
  1239. }
  1240. #endif
  1241. unsigned long __init find_ecache_flush_span(unsigned long size)
  1242. {
  1243. int i;
  1244. for (i = 0; i < pavail_ents; i++) {
  1245. if (pavail[i].reg_size >= size)
  1246. return pavail[i].phys_addr;
  1247. }
  1248. return ~0UL;
  1249. }
  1250. static void __init tsb_phys_patch(void)
  1251. {
  1252. struct tsb_ldquad_phys_patch_entry *pquad;
  1253. struct tsb_phys_patch_entry *p;
  1254. pquad = &__tsb_ldquad_phys_patch;
  1255. while (pquad < &__tsb_ldquad_phys_patch_end) {
  1256. unsigned long addr = pquad->addr;
  1257. if (tlb_type == hypervisor)
  1258. *(unsigned int *) addr = pquad->sun4v_insn;
  1259. else
  1260. *(unsigned int *) addr = pquad->sun4u_insn;
  1261. wmb();
  1262. __asm__ __volatile__("flush %0"
  1263. : /* no outputs */
  1264. : "r" (addr));
  1265. pquad++;
  1266. }
  1267. p = &__tsb_phys_patch;
  1268. while (p < &__tsb_phys_patch_end) {
  1269. unsigned long addr = p->addr;
  1270. *(unsigned int *) addr = p->insn;
  1271. wmb();
  1272. __asm__ __volatile__("flush %0"
  1273. : /* no outputs */
  1274. : "r" (addr));
  1275. p++;
  1276. }
  1277. }
  1278. /* Don't mark as init, we give this to the Hypervisor. */
  1279. #ifndef CONFIG_DEBUG_PAGEALLOC
  1280. #define NUM_KTSB_DESCR 2
  1281. #else
  1282. #define NUM_KTSB_DESCR 1
  1283. #endif
  1284. static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
  1285. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  1286. static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
  1287. {
  1288. pa >>= KTSB_PHYS_SHIFT;
  1289. while (start < end) {
  1290. unsigned int *ia = (unsigned int *)(unsigned long)*start;
  1291. ia[0] = (ia[0] & ~0x3fffff) | (pa >> 10);
  1292. __asm__ __volatile__("flush %0" : : "r" (ia));
  1293. ia[1] = (ia[1] & ~0x3ff) | (pa & 0x3ff);
  1294. __asm__ __volatile__("flush %0" : : "r" (ia + 1));
  1295. start++;
  1296. }
  1297. }
  1298. static void ktsb_phys_patch(void)
  1299. {
  1300. extern unsigned int __swapper_tsb_phys_patch;
  1301. extern unsigned int __swapper_tsb_phys_patch_end;
  1302. unsigned long ktsb_pa;
  1303. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1304. patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
  1305. &__swapper_tsb_phys_patch_end, ktsb_pa);
  1306. #ifndef CONFIG_DEBUG_PAGEALLOC
  1307. {
  1308. extern unsigned int __swapper_4m_tsb_phys_patch;
  1309. extern unsigned int __swapper_4m_tsb_phys_patch_end;
  1310. ktsb_pa = (kern_base +
  1311. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1312. patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
  1313. &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
  1314. }
  1315. #endif
  1316. }
  1317. static void __init sun4v_ktsb_init(void)
  1318. {
  1319. unsigned long ktsb_pa;
  1320. /* First KTSB for PAGE_SIZE mappings. */
  1321. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1322. switch (PAGE_SIZE) {
  1323. case 8 * 1024:
  1324. default:
  1325. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  1326. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  1327. break;
  1328. case 64 * 1024:
  1329. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1330. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1331. break;
  1332. case 512 * 1024:
  1333. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1334. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1335. break;
  1336. case 4 * 1024 * 1024:
  1337. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1338. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1339. break;
  1340. }
  1341. ktsb_descr[0].assoc = 1;
  1342. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1343. ktsb_descr[0].ctx_idx = 0;
  1344. ktsb_descr[0].tsb_base = ktsb_pa;
  1345. ktsb_descr[0].resv = 0;
  1346. #ifndef CONFIG_DEBUG_PAGEALLOC
  1347. /* Second KTSB for 4MB/256MB/2GB/16GB mappings. */
  1348. ktsb_pa = (kern_base +
  1349. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1350. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1351. ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
  1352. HV_PGSZ_MASK_256MB);
  1353. if (sun4v_chip_type == SUN4V_CHIP_NIAGARA4)
  1354. ktsb_descr[1].pgsz_mask |= HV_PGSZ_MASK_2GB;
  1355. ktsb_descr[1].assoc = 1;
  1356. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1357. ktsb_descr[1].ctx_idx = 0;
  1358. ktsb_descr[1].tsb_base = ktsb_pa;
  1359. ktsb_descr[1].resv = 0;
  1360. #endif
  1361. }
  1362. void __cpuinit sun4v_ktsb_register(void)
  1363. {
  1364. unsigned long pa, ret;
  1365. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1366. ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
  1367. if (ret != 0) {
  1368. prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
  1369. "errors with %lx\n", pa, ret);
  1370. prom_halt();
  1371. }
  1372. }
  1373. /* paging_init() sets up the page tables */
  1374. static unsigned long last_valid_pfn;
  1375. pgd_t swapper_pg_dir[2048];
  1376. static void sun4u_pgprot_init(void);
  1377. static void sun4v_pgprot_init(void);
  1378. void __init paging_init(void)
  1379. {
  1380. unsigned long end_pfn, shift, phys_base;
  1381. unsigned long real_end, i;
  1382. int node;
  1383. /* These build time checkes make sure that the dcache_dirty_cpu()
  1384. * page->flags usage will work.
  1385. *
  1386. * When a page gets marked as dcache-dirty, we store the
  1387. * cpu number starting at bit 32 in the page->flags. Also,
  1388. * functions like clear_dcache_dirty_cpu use the cpu mask
  1389. * in 13-bit signed-immediate instruction fields.
  1390. */
  1391. /*
  1392. * Page flags must not reach into upper 32 bits that are used
  1393. * for the cpu number
  1394. */
  1395. BUILD_BUG_ON(NR_PAGEFLAGS > 32);
  1396. /*
  1397. * The bit fields placed in the high range must not reach below
  1398. * the 32 bit boundary. Otherwise we cannot place the cpu field
  1399. * at the 32 bit boundary.
  1400. */
  1401. BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
  1402. ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
  1403. BUILD_BUG_ON(NR_CPUS > 4096);
  1404. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  1405. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1406. /* Invalidate both kernel TSBs. */
  1407. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1408. #ifndef CONFIG_DEBUG_PAGEALLOC
  1409. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1410. #endif
  1411. if (tlb_type == hypervisor)
  1412. sun4v_pgprot_init();
  1413. else
  1414. sun4u_pgprot_init();
  1415. if (tlb_type == cheetah_plus ||
  1416. tlb_type == hypervisor) {
  1417. tsb_phys_patch();
  1418. ktsb_phys_patch();
  1419. }
  1420. if (tlb_type == hypervisor) {
  1421. sun4v_patch_tlb_handlers();
  1422. sun4v_ktsb_init();
  1423. }
  1424. /* Find available physical memory...
  1425. *
  1426. * Read it twice in order to work around a bug in openfirmware.
  1427. * The call to grab this table itself can cause openfirmware to
  1428. * allocate memory, which in turn can take away some space from
  1429. * the list of available memory. Reading it twice makes sure
  1430. * we really do get the final value.
  1431. */
  1432. read_obp_translations();
  1433. read_obp_memory("reg", &pall[0], &pall_ents);
  1434. read_obp_memory("available", &pavail[0], &pavail_ents);
  1435. read_obp_memory("available", &pavail[0], &pavail_ents);
  1436. phys_base = 0xffffffffffffffffUL;
  1437. for (i = 0; i < pavail_ents; i++) {
  1438. phys_base = min(phys_base, pavail[i].phys_addr);
  1439. memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
  1440. }
  1441. memblock_reserve(kern_base, kern_size);
  1442. find_ramdisk(phys_base);
  1443. memblock_enforce_memory_limit(cmdline_memory_size);
  1444. memblock_allow_resize();
  1445. memblock_dump_all();
  1446. set_bit(0, mmu_context_bmap);
  1447. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1448. real_end = (unsigned long)_end;
  1449. num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
  1450. printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
  1451. num_kernel_image_mappings);
  1452. /* Set kernel pgd to upper alias so physical page computations
  1453. * work.
  1454. */
  1455. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1456. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  1457. /* Now can init the kernel/bad page tables. */
  1458. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  1459. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  1460. inherit_prom_mappings();
  1461. init_kpte_bitmap();
  1462. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1463. setup_tba();
  1464. __flush_tlb_all();
  1465. if (tlb_type == hypervisor)
  1466. sun4v_ktsb_register();
  1467. prom_build_devicetree();
  1468. of_populate_present_mask();
  1469. #ifndef CONFIG_SMP
  1470. of_fill_in_cpu_data();
  1471. #endif
  1472. if (tlb_type == hypervisor) {
  1473. sun4v_mdesc_init();
  1474. mdesc_populate_present_mask(cpu_all_mask);
  1475. #ifndef CONFIG_SMP
  1476. mdesc_fill_in_cpu_data(cpu_all_mask);
  1477. #endif
  1478. mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
  1479. } else {
  1480. unsigned long impl, ver;
  1481. cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
  1482. HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
  1483. __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
  1484. impl = ((ver >> 32) & 0xffff);
  1485. if (impl == PANTHER_IMPL)
  1486. cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
  1487. HV_PGSZ_MASK_256MB);
  1488. }
  1489. /* Setup bootmem... */
  1490. last_valid_pfn = end_pfn = bootmem_init(phys_base);
  1491. /* Once the OF device tree and MDESC have been setup, we know
  1492. * the list of possible cpus. Therefore we can allocate the
  1493. * IRQ stacks.
  1494. */
  1495. for_each_possible_cpu(i) {
  1496. node = cpu_to_node(i);
  1497. softirq_stack[i] = __alloc_bootmem_node(NODE_DATA(node),
  1498. THREAD_SIZE,
  1499. THREAD_SIZE, 0);
  1500. hardirq_stack[i] = __alloc_bootmem_node(NODE_DATA(node),
  1501. THREAD_SIZE,
  1502. THREAD_SIZE, 0);
  1503. }
  1504. kernel_physical_mapping_init();
  1505. {
  1506. unsigned long max_zone_pfns[MAX_NR_ZONES];
  1507. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  1508. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  1509. free_area_init_nodes(max_zone_pfns);
  1510. }
  1511. printk("Booting Linux...\n");
  1512. }
  1513. int __devinit page_in_phys_avail(unsigned long paddr)
  1514. {
  1515. int i;
  1516. paddr &= PAGE_MASK;
  1517. for (i = 0; i < pavail_ents; i++) {
  1518. unsigned long start, end;
  1519. start = pavail[i].phys_addr;
  1520. end = start + pavail[i].reg_size;
  1521. if (paddr >= start && paddr < end)
  1522. return 1;
  1523. }
  1524. if (paddr >= kern_base && paddr < (kern_base + kern_size))
  1525. return 1;
  1526. #ifdef CONFIG_BLK_DEV_INITRD
  1527. if (paddr >= __pa(initrd_start) &&
  1528. paddr < __pa(PAGE_ALIGN(initrd_end)))
  1529. return 1;
  1530. #endif
  1531. return 0;
  1532. }
  1533. static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
  1534. static int pavail_rescan_ents __initdata;
  1535. /* Certain OBP calls, such as fetching "available" properties, can
  1536. * claim physical memory. So, along with initializing the valid
  1537. * address bitmap, what we do here is refetch the physical available
  1538. * memory list again, and make sure it provides at least as much
  1539. * memory as 'pavail' does.
  1540. */
  1541. static void __init setup_valid_addr_bitmap_from_pavail(unsigned long *bitmap)
  1542. {
  1543. int i;
  1544. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  1545. for (i = 0; i < pavail_ents; i++) {
  1546. unsigned long old_start, old_end;
  1547. old_start = pavail[i].phys_addr;
  1548. old_end = old_start + pavail[i].reg_size;
  1549. while (old_start < old_end) {
  1550. int n;
  1551. for (n = 0; n < pavail_rescan_ents; n++) {
  1552. unsigned long new_start, new_end;
  1553. new_start = pavail_rescan[n].phys_addr;
  1554. new_end = new_start +
  1555. pavail_rescan[n].reg_size;
  1556. if (new_start <= old_start &&
  1557. new_end >= (old_start + PAGE_SIZE)) {
  1558. set_bit(old_start >> 22, bitmap);
  1559. goto do_next_page;
  1560. }
  1561. }
  1562. prom_printf("mem_init: Lost memory in pavail\n");
  1563. prom_printf("mem_init: OLD start[%lx] size[%lx]\n",
  1564. pavail[i].phys_addr,
  1565. pavail[i].reg_size);
  1566. prom_printf("mem_init: NEW start[%lx] size[%lx]\n",
  1567. pavail_rescan[i].phys_addr,
  1568. pavail_rescan[i].reg_size);
  1569. prom_printf("mem_init: Cannot continue, aborting.\n");
  1570. prom_halt();
  1571. do_next_page:
  1572. old_start += PAGE_SIZE;
  1573. }
  1574. }
  1575. }
  1576. static void __init patch_tlb_miss_handler_bitmap(void)
  1577. {
  1578. extern unsigned int valid_addr_bitmap_insn[];
  1579. extern unsigned int valid_addr_bitmap_patch[];
  1580. valid_addr_bitmap_insn[1] = valid_addr_bitmap_patch[1];
  1581. mb();
  1582. valid_addr_bitmap_insn[0] = valid_addr_bitmap_patch[0];
  1583. flushi(&valid_addr_bitmap_insn[0]);
  1584. }
  1585. void __init mem_init(void)
  1586. {
  1587. unsigned long codepages, datapages, initpages;
  1588. unsigned long addr, last;
  1589. addr = PAGE_OFFSET + kern_base;
  1590. last = PAGE_ALIGN(kern_size) + addr;
  1591. while (addr < last) {
  1592. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1593. addr += PAGE_SIZE;
  1594. }
  1595. setup_valid_addr_bitmap_from_pavail(sparc64_valid_addr_bitmap);
  1596. patch_tlb_miss_handler_bitmap();
  1597. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1598. #ifdef CONFIG_NEED_MULTIPLE_NODES
  1599. {
  1600. int i;
  1601. for_each_online_node(i) {
  1602. if (NODE_DATA(i)->node_spanned_pages != 0) {
  1603. totalram_pages +=
  1604. free_all_bootmem_node(NODE_DATA(i));
  1605. }
  1606. }
  1607. totalram_pages += free_low_memory_core_early(MAX_NUMNODES);
  1608. }
  1609. #else
  1610. totalram_pages = free_all_bootmem();
  1611. #endif
  1612. /* We subtract one to account for the mem_map_zero page
  1613. * allocated below.
  1614. */
  1615. totalram_pages -= 1;
  1616. num_physpages = totalram_pages;
  1617. /*
  1618. * Set up the zero page, mark it reserved, so that page count
  1619. * is not manipulated when freeing the page from user ptes.
  1620. */
  1621. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1622. if (mem_map_zero == NULL) {
  1623. prom_printf("paging_init: Cannot alloc zero page.\n");
  1624. prom_halt();
  1625. }
  1626. SetPageReserved(mem_map_zero);
  1627. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1628. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1629. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1630. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1631. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1632. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1633. printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1634. nr_free_pages() << (PAGE_SHIFT-10),
  1635. codepages << (PAGE_SHIFT-10),
  1636. datapages << (PAGE_SHIFT-10),
  1637. initpages << (PAGE_SHIFT-10),
  1638. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1639. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1640. cheetah_ecache_flush_init();
  1641. }
  1642. void free_initmem(void)
  1643. {
  1644. unsigned long addr, initend;
  1645. int do_free = 1;
  1646. /* If the physical memory maps were trimmed by kernel command
  1647. * line options, don't even try freeing this initmem stuff up.
  1648. * The kernel image could have been in the trimmed out region
  1649. * and if so the freeing below will free invalid page structs.
  1650. */
  1651. if (cmdline_memory_size)
  1652. do_free = 0;
  1653. /*
  1654. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1655. */
  1656. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1657. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1658. for (; addr < initend; addr += PAGE_SIZE) {
  1659. unsigned long page;
  1660. struct page *p;
  1661. page = (addr +
  1662. ((unsigned long) __va(kern_base)) -
  1663. ((unsigned long) KERNBASE));
  1664. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1665. if (do_free) {
  1666. p = virt_to_page(page);
  1667. ClearPageReserved(p);
  1668. init_page_count(p);
  1669. __free_page(p);
  1670. num_physpages++;
  1671. totalram_pages++;
  1672. }
  1673. }
  1674. }
  1675. #ifdef CONFIG_BLK_DEV_INITRD
  1676. void free_initrd_mem(unsigned long start, unsigned long end)
  1677. {
  1678. if (start < end)
  1679. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1680. for (; start < end; start += PAGE_SIZE) {
  1681. struct page *p = virt_to_page(start);
  1682. ClearPageReserved(p);
  1683. init_page_count(p);
  1684. __free_page(p);
  1685. num_physpages++;
  1686. totalram_pages++;
  1687. }
  1688. }
  1689. #endif
  1690. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1691. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1692. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1693. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1694. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1695. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1696. pgprot_t PAGE_KERNEL __read_mostly;
  1697. EXPORT_SYMBOL(PAGE_KERNEL);
  1698. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1699. pgprot_t PAGE_COPY __read_mostly;
  1700. pgprot_t PAGE_SHARED __read_mostly;
  1701. EXPORT_SYMBOL(PAGE_SHARED);
  1702. unsigned long pg_iobits __read_mostly;
  1703. unsigned long _PAGE_IE __read_mostly;
  1704. EXPORT_SYMBOL(_PAGE_IE);
  1705. unsigned long _PAGE_E __read_mostly;
  1706. EXPORT_SYMBOL(_PAGE_E);
  1707. unsigned long _PAGE_CACHE __read_mostly;
  1708. EXPORT_SYMBOL(_PAGE_CACHE);
  1709. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  1710. unsigned long vmemmap_table[VMEMMAP_SIZE];
  1711. static long __meminitdata addr_start, addr_end;
  1712. static int __meminitdata node_start;
  1713. int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
  1714. {
  1715. unsigned long vstart = (unsigned long) start;
  1716. unsigned long vend = (unsigned long) (start + nr);
  1717. unsigned long phys_start = (vstart - VMEMMAP_BASE);
  1718. unsigned long phys_end = (vend - VMEMMAP_BASE);
  1719. unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
  1720. unsigned long end = VMEMMAP_ALIGN(phys_end);
  1721. unsigned long pte_base;
  1722. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1723. _PAGE_CP_4U | _PAGE_CV_4U |
  1724. _PAGE_P_4U | _PAGE_W_4U);
  1725. if (tlb_type == hypervisor)
  1726. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1727. _PAGE_CP_4V | _PAGE_CV_4V |
  1728. _PAGE_P_4V | _PAGE_W_4V);
  1729. for (; addr < end; addr += VMEMMAP_CHUNK) {
  1730. unsigned long *vmem_pp =
  1731. vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
  1732. void *block;
  1733. if (!(*vmem_pp & _PAGE_VALID)) {
  1734. block = vmemmap_alloc_block(1UL << 22, node);
  1735. if (!block)
  1736. return -ENOMEM;
  1737. *vmem_pp = pte_base | __pa(block);
  1738. /* check to see if we have contiguous blocks */
  1739. if (addr_end != addr || node_start != node) {
  1740. if (addr_start)
  1741. printk(KERN_DEBUG " [%lx-%lx] on node %d\n",
  1742. addr_start, addr_end-1, node_start);
  1743. addr_start = addr;
  1744. node_start = node;
  1745. }
  1746. addr_end = addr + VMEMMAP_CHUNK;
  1747. }
  1748. }
  1749. return 0;
  1750. }
  1751. void __meminit vmemmap_populate_print_last(void)
  1752. {
  1753. if (addr_start) {
  1754. printk(KERN_DEBUG " [%lx-%lx] on node %d\n",
  1755. addr_start, addr_end-1, node_start);
  1756. addr_start = 0;
  1757. addr_end = 0;
  1758. node_start = 0;
  1759. }
  1760. }
  1761. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  1762. static void prot_init_common(unsigned long page_none,
  1763. unsigned long page_shared,
  1764. unsigned long page_copy,
  1765. unsigned long page_readonly,
  1766. unsigned long page_exec_bit)
  1767. {
  1768. PAGE_COPY = __pgprot(page_copy);
  1769. PAGE_SHARED = __pgprot(page_shared);
  1770. protection_map[0x0] = __pgprot(page_none);
  1771. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1772. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1773. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1774. protection_map[0x4] = __pgprot(page_readonly);
  1775. protection_map[0x5] = __pgprot(page_readonly);
  1776. protection_map[0x6] = __pgprot(page_copy);
  1777. protection_map[0x7] = __pgprot(page_copy);
  1778. protection_map[0x8] = __pgprot(page_none);
  1779. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1780. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1781. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1782. protection_map[0xc] = __pgprot(page_readonly);
  1783. protection_map[0xd] = __pgprot(page_readonly);
  1784. protection_map[0xe] = __pgprot(page_shared);
  1785. protection_map[0xf] = __pgprot(page_shared);
  1786. }
  1787. static void __init sun4u_pgprot_init(void)
  1788. {
  1789. unsigned long page_none, page_shared, page_copy, page_readonly;
  1790. unsigned long page_exec_bit;
  1791. int i;
  1792. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1793. _PAGE_CACHE_4U | _PAGE_P_4U |
  1794. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1795. _PAGE_EXEC_4U);
  1796. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1797. _PAGE_CACHE_4U | _PAGE_P_4U |
  1798. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1799. _PAGE_EXEC_4U | _PAGE_L_4U);
  1800. _PAGE_IE = _PAGE_IE_4U;
  1801. _PAGE_E = _PAGE_E_4U;
  1802. _PAGE_CACHE = _PAGE_CACHE_4U;
  1803. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1804. __ACCESS_BITS_4U | _PAGE_E_4U);
  1805. #ifdef CONFIG_DEBUG_PAGEALLOC
  1806. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
  1807. 0xfffff80000000000UL;
  1808. #else
  1809. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1810. 0xfffff80000000000UL;
  1811. #endif
  1812. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1813. _PAGE_P_4U | _PAGE_W_4U);
  1814. /* XXX Should use 256MB on Panther. XXX */
  1815. for (i = 1; i < 4; i++)
  1816. kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
  1817. _PAGE_SZBITS = _PAGE_SZBITS_4U;
  1818. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1819. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1820. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1821. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1822. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1823. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1824. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1825. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1826. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1827. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1828. page_exec_bit = _PAGE_EXEC_4U;
  1829. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1830. page_exec_bit);
  1831. }
  1832. static void __init sun4v_pgprot_init(void)
  1833. {
  1834. unsigned long page_none, page_shared, page_copy, page_readonly;
  1835. unsigned long page_exec_bit;
  1836. int i;
  1837. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1838. _PAGE_CACHE_4V | _PAGE_P_4V |
  1839. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1840. _PAGE_EXEC_4V);
  1841. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1842. _PAGE_IE = _PAGE_IE_4V;
  1843. _PAGE_E = _PAGE_E_4V;
  1844. _PAGE_CACHE = _PAGE_CACHE_4V;
  1845. #ifdef CONFIG_DEBUG_PAGEALLOC
  1846. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1847. 0xfffff80000000000UL;
  1848. #else
  1849. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  1850. 0xfffff80000000000UL;
  1851. #endif
  1852. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1853. _PAGE_P_4V | _PAGE_W_4V);
  1854. #ifdef CONFIG_DEBUG_PAGEALLOC
  1855. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1856. 0xfffff80000000000UL;
  1857. #else
  1858. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1859. 0xfffff80000000000UL;
  1860. #endif
  1861. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1862. _PAGE_P_4V | _PAGE_W_4V);
  1863. i = 2;
  1864. if (sun4v_chip_type == SUN4V_CHIP_NIAGARA4) {
  1865. #ifdef CONFIG_DEBUG_PAGEALLOC
  1866. kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1867. 0xfffff80000000000UL;
  1868. #else
  1869. kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
  1870. 0xfffff80000000000UL;
  1871. #endif
  1872. kern_linear_pte_xor[2] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1873. _PAGE_P_4V | _PAGE_W_4V);
  1874. i = 3;
  1875. }
  1876. for (; i < 4; i++)
  1877. kern_linear_pte_xor[i] = kern_linear_pte_xor[i - 1];
  1878. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  1879. __ACCESS_BITS_4V | _PAGE_E_4V);
  1880. _PAGE_SZBITS = _PAGE_SZBITS_4V;
  1881. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  1882. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  1883. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  1884. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  1885. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  1886. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1887. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  1888. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1889. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1890. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1891. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1892. page_exec_bit = _PAGE_EXEC_4V;
  1893. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1894. page_exec_bit);
  1895. }
  1896. unsigned long pte_sz_bits(unsigned long sz)
  1897. {
  1898. if (tlb_type == hypervisor) {
  1899. switch (sz) {
  1900. case 8 * 1024:
  1901. default:
  1902. return _PAGE_SZ8K_4V;
  1903. case 64 * 1024:
  1904. return _PAGE_SZ64K_4V;
  1905. case 512 * 1024:
  1906. return _PAGE_SZ512K_4V;
  1907. case 4 * 1024 * 1024:
  1908. return _PAGE_SZ4MB_4V;
  1909. }
  1910. } else {
  1911. switch (sz) {
  1912. case 8 * 1024:
  1913. default:
  1914. return _PAGE_SZ8K_4U;
  1915. case 64 * 1024:
  1916. return _PAGE_SZ64K_4U;
  1917. case 512 * 1024:
  1918. return _PAGE_SZ512K_4U;
  1919. case 4 * 1024 * 1024:
  1920. return _PAGE_SZ4MB_4U;
  1921. }
  1922. }
  1923. }
  1924. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  1925. {
  1926. pte_t pte;
  1927. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  1928. pte_val(pte) |= (((unsigned long)space) << 32);
  1929. pte_val(pte) |= pte_sz_bits(page_size);
  1930. return pte;
  1931. }
  1932. static unsigned long kern_large_tte(unsigned long paddr)
  1933. {
  1934. unsigned long val;
  1935. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1936. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  1937. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  1938. if (tlb_type == hypervisor)
  1939. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1940. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  1941. _PAGE_EXEC_4V | _PAGE_W_4V);
  1942. return val | paddr;
  1943. }
  1944. /* If not locked, zap it. */
  1945. void __flush_tlb_all(void)
  1946. {
  1947. unsigned long pstate;
  1948. int i;
  1949. __asm__ __volatile__("flushw\n\t"
  1950. "rdpr %%pstate, %0\n\t"
  1951. "wrpr %0, %1, %%pstate"
  1952. : "=r" (pstate)
  1953. : "i" (PSTATE_IE));
  1954. if (tlb_type == hypervisor) {
  1955. sun4v_mmu_demap_all();
  1956. } else if (tlb_type == spitfire) {
  1957. for (i = 0; i < 64; i++) {
  1958. /* Spitfire Errata #32 workaround */
  1959. /* NOTE: Always runs on spitfire, so no
  1960. * cheetah+ page size encodings.
  1961. */
  1962. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1963. "flush %%g6"
  1964. : /* No outputs */
  1965. : "r" (0),
  1966. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1967. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  1968. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1969. "membar #Sync"
  1970. : /* no outputs */
  1971. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  1972. spitfire_put_dtlb_data(i, 0x0UL);
  1973. }
  1974. /* Spitfire Errata #32 workaround */
  1975. /* NOTE: Always runs on spitfire, so no
  1976. * cheetah+ page size encodings.
  1977. */
  1978. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1979. "flush %%g6"
  1980. : /* No outputs */
  1981. : "r" (0),
  1982. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1983. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  1984. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1985. "membar #Sync"
  1986. : /* no outputs */
  1987. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  1988. spitfire_put_itlb_data(i, 0x0UL);
  1989. }
  1990. }
  1991. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1992. cheetah_flush_dtlb_all();
  1993. cheetah_flush_itlb_all();
  1994. }
  1995. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  1996. : : "r" (pstate));
  1997. }