init_64.c 61 KB

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