init_64.c 58 KB

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