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