init.c 57 KB

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