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