init_64.c 57 KB

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