init_64.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846
  1. /*
  2. * arch/sparc64/mm/init.c
  3. *
  4. * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/init.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/mm.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/initrd.h>
  16. #include <linux/swap.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/poison.h>
  19. #include <linux/fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/cache.h>
  23. #include <linux/sort.h>
  24. #include <linux/ioport.h>
  25. #include <linux/percpu.h>
  26. #include <linux/memblock.h>
  27. #include <linux/mmzone.h>
  28. #include <linux/gfp.h>
  29. #include <asm/head.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/setup.h>
  50. #include <asm/irq.h>
  51. #include "init_64.h"
  52. unsigned long kern_linear_pte_xor[4] __read_mostly;
  53. /* A bitmap, two bits for every 256MB of physical memory. These two
  54. * bits determine what page size we use for kernel linear
  55. * translations. They form an index into kern_linear_pte_xor[]. The
  56. * value in the indexed slot is XOR'd with the TLB miss virtual
  57. * address to form the resulting TTE. The mapping is:
  58. *
  59. * 0 ==> 4MB
  60. * 1 ==> 256MB
  61. * 2 ==> 2GB
  62. * 3 ==> 16GB
  63. *
  64. * All sun4v chips support 256MB pages. Only SPARC-T4 and later
  65. * support 2GB pages, and hopefully future cpus will support the 16GB
  66. * pages as well. For slots 2 and 3, we encode a 256MB TTE xor there
  67. * if these larger page sizes are not supported by the cpu.
  68. *
  69. * It would be nice to determine this from the machine description
  70. * 'cpu' properties, but we need to have this table setup before the
  71. * MDESC is initialized.
  72. */
  73. #ifndef CONFIG_DEBUG_PAGEALLOC
  74. /* A special kernel TSB for 4MB, 256MB, 2GB and 16GB linear mappings.
  75. * Space is allocated for this right after the trap table in
  76. * arch/sparc64/kernel/head.S
  77. */
  78. extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
  79. #endif
  80. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  81. static unsigned long cpu_pgsz_mask;
  82. #define MAX_BANKS 1024
  83. static struct linux_prom64_registers pavail[MAX_BANKS];
  84. static int pavail_ents;
  85. static int cmp_p64(const void *a, const void *b)
  86. {
  87. const struct linux_prom64_registers *x = a, *y = b;
  88. if (x->phys_addr > y->phys_addr)
  89. return 1;
  90. if (x->phys_addr < y->phys_addr)
  91. return -1;
  92. return 0;
  93. }
  94. static void __init read_obp_memory(const char *property,
  95. struct linux_prom64_registers *regs,
  96. int *num_ents)
  97. {
  98. phandle node = prom_finddevice("/memory");
  99. int prop_size = prom_getproplen(node, property);
  100. int ents, ret, i;
  101. ents = prop_size / sizeof(struct linux_prom64_registers);
  102. if (ents > MAX_BANKS) {
  103. prom_printf("The machine has more %s property entries than "
  104. "this kernel can support (%d).\n",
  105. property, MAX_BANKS);
  106. prom_halt();
  107. }
  108. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  109. if (ret == -1) {
  110. prom_printf("Couldn't get %s property from /memory.\n",
  111. property);
  112. prom_halt();
  113. }
  114. /* Sanitize what we got from the firmware, by page aligning
  115. * everything.
  116. */
  117. for (i = 0; i < ents; i++) {
  118. unsigned long base, size;
  119. base = regs[i].phys_addr;
  120. size = regs[i].reg_size;
  121. size &= PAGE_MASK;
  122. if (base & ~PAGE_MASK) {
  123. unsigned long new_base = PAGE_ALIGN(base);
  124. size -= new_base - base;
  125. if ((long) size < 0L)
  126. size = 0UL;
  127. base = new_base;
  128. }
  129. if (size == 0UL) {
  130. /* If it is empty, simply get rid of it.
  131. * This simplifies the logic of the other
  132. * functions that process these arrays.
  133. */
  134. memmove(&regs[i], &regs[i + 1],
  135. (ents - i - 1) * sizeof(regs[0]));
  136. i--;
  137. ents--;
  138. continue;
  139. }
  140. regs[i].phys_addr = base;
  141. regs[i].reg_size = size;
  142. }
  143. *num_ents = ents;
  144. sort(regs, ents, sizeof(struct linux_prom64_registers),
  145. cmp_p64, NULL);
  146. }
  147. /* Kernel physical address base and size in bytes. */
  148. unsigned long kern_base __read_mostly;
  149. unsigned long kern_size __read_mostly;
  150. /* Initial ramdisk setup */
  151. extern unsigned long sparc_ramdisk_image64;
  152. extern unsigned int sparc_ramdisk_image;
  153. extern unsigned int sparc_ramdisk_size;
  154. struct page *mem_map_zero __read_mostly;
  155. EXPORT_SYMBOL(mem_map_zero);
  156. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  157. unsigned long sparc64_kern_pri_context __read_mostly;
  158. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  159. unsigned long sparc64_kern_sec_context __read_mostly;
  160. int num_kernel_image_mappings;
  161. #ifdef CONFIG_DEBUG_DCFLUSH
  162. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  163. #ifdef CONFIG_SMP
  164. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  165. #endif
  166. #endif
  167. inline void flush_dcache_page_impl(struct page *page)
  168. {
  169. BUG_ON(tlb_type == hypervisor);
  170. #ifdef CONFIG_DEBUG_DCFLUSH
  171. atomic_inc(&dcpage_flushes);
  172. #endif
  173. #ifdef DCACHE_ALIASING_POSSIBLE
  174. __flush_dcache_page(page_address(page),
  175. ((tlb_type == spitfire) &&
  176. page_mapping(page) != NULL));
  177. #else
  178. if (page_mapping(page) != NULL &&
  179. tlb_type == spitfire)
  180. __flush_icache_page(__pa(page_address(page)));
  181. #endif
  182. }
  183. #define PG_dcache_dirty PG_arch_1
  184. #define PG_dcache_cpu_shift 32UL
  185. #define PG_dcache_cpu_mask \
  186. ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
  187. #define dcache_dirty_cpu(page) \
  188. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  189. static inline void set_dcache_dirty(struct page *page, int this_cpu)
  190. {
  191. unsigned long mask = this_cpu;
  192. unsigned long non_cpu_bits;
  193. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  194. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  195. __asm__ __volatile__("1:\n\t"
  196. "ldx [%2], %%g7\n\t"
  197. "and %%g7, %1, %%g1\n\t"
  198. "or %%g1, %0, %%g1\n\t"
  199. "casx [%2], %%g7, %%g1\n\t"
  200. "cmp %%g7, %%g1\n\t"
  201. "bne,pn %%xcc, 1b\n\t"
  202. " nop"
  203. : /* no outputs */
  204. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  205. : "g1", "g7");
  206. }
  207. static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  208. {
  209. unsigned long mask = (1UL << PG_dcache_dirty);
  210. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  211. "1:\n\t"
  212. "ldx [%2], %%g7\n\t"
  213. "srlx %%g7, %4, %%g1\n\t"
  214. "and %%g1, %3, %%g1\n\t"
  215. "cmp %%g1, %0\n\t"
  216. "bne,pn %%icc, 2f\n\t"
  217. " andn %%g7, %1, %%g1\n\t"
  218. "casx [%2], %%g7, %%g1\n\t"
  219. "cmp %%g7, %%g1\n\t"
  220. "bne,pn %%xcc, 1b\n\t"
  221. " nop\n"
  222. "2:"
  223. : /* no outputs */
  224. : "r" (cpu), "r" (mask), "r" (&page->flags),
  225. "i" (PG_dcache_cpu_mask),
  226. "i" (PG_dcache_cpu_shift)
  227. : "g1", "g7");
  228. }
  229. static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
  230. {
  231. unsigned long tsb_addr = (unsigned long) ent;
  232. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  233. tsb_addr = __pa(tsb_addr);
  234. __tsb_insert(tsb_addr, tag, pte);
  235. }
  236. unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
  237. static void flush_dcache(unsigned long pfn)
  238. {
  239. struct page *page;
  240. page = pfn_to_page(pfn);
  241. if (page) {
  242. unsigned long pg_flags;
  243. pg_flags = page->flags;
  244. if (pg_flags & (1UL << PG_dcache_dirty)) {
  245. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  246. PG_dcache_cpu_mask);
  247. int this_cpu = get_cpu();
  248. /* This is just to optimize away some function calls
  249. * in the SMP case.
  250. */
  251. if (cpu == this_cpu)
  252. flush_dcache_page_impl(page);
  253. else
  254. smp_flush_dcache_page_impl(page, cpu);
  255. clear_dcache_dirty_cpu(page, cpu);
  256. put_cpu();
  257. }
  258. }
  259. }
  260. /* mm->context.lock must be held */
  261. static void __update_mmu_tsb_insert(struct mm_struct *mm, unsigned long tsb_index,
  262. unsigned long tsb_hash_shift, unsigned long address,
  263. unsigned long tte)
  264. {
  265. struct tsb *tsb = mm->context.tsb_block[tsb_index].tsb;
  266. unsigned long tag;
  267. if (unlikely(!tsb))
  268. return;
  269. tsb += ((address >> tsb_hash_shift) &
  270. (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
  271. tag = (address >> 22UL);
  272. tsb_insert(tsb, tag, tte);
  273. }
  274. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  275. static inline bool is_hugetlb_pte(pte_t pte)
  276. {
  277. if ((tlb_type == hypervisor &&
  278. (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
  279. (tlb_type != hypervisor &&
  280. (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U))
  281. return true;
  282. return false;
  283. }
  284. #endif
  285. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
  286. {
  287. struct mm_struct *mm;
  288. unsigned long flags;
  289. pte_t pte = *ptep;
  290. if (tlb_type != hypervisor) {
  291. unsigned long pfn = pte_pfn(pte);
  292. if (pfn_valid(pfn))
  293. flush_dcache(pfn);
  294. }
  295. mm = vma->vm_mm;
  296. /* Don't insert a non-valid PTE into the TSB, we'll deadlock. */
  297. if (!pte_accessible(mm, pte))
  298. return;
  299. spin_lock_irqsave(&mm->context.lock, flags);
  300. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  301. if (mm->context.huge_pte_count && is_hugetlb_pte(pte))
  302. __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
  303. address, pte_val(pte));
  304. else
  305. #endif
  306. __update_mmu_tsb_insert(mm, MM_TSB_BASE, PAGE_SHIFT,
  307. address, pte_val(pte));
  308. spin_unlock_irqrestore(&mm->context.lock, flags);
  309. }
  310. void flush_dcache_page(struct page *page)
  311. {
  312. struct address_space *mapping;
  313. int this_cpu;
  314. if (tlb_type == hypervisor)
  315. return;
  316. /* Do not bother with the expensive D-cache flush if it
  317. * is merely the zero page. The 'bigcore' testcase in GDB
  318. * causes this case to run millions of times.
  319. */
  320. if (page == ZERO_PAGE(0))
  321. return;
  322. this_cpu = get_cpu();
  323. mapping = page_mapping(page);
  324. if (mapping && !mapping_mapped(mapping)) {
  325. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  326. if (dirty) {
  327. int dirty_cpu = dcache_dirty_cpu(page);
  328. if (dirty_cpu == this_cpu)
  329. goto out;
  330. smp_flush_dcache_page_impl(page, dirty_cpu);
  331. }
  332. set_dcache_dirty(page, this_cpu);
  333. } else {
  334. /* We could delay the flush for the !page_mapping
  335. * case too. But that case is for exec env/arg
  336. * pages and those are %99 certainly going to get
  337. * faulted into the tlb (and thus flushed) anyways.
  338. */
  339. flush_dcache_page_impl(page);
  340. }
  341. out:
  342. put_cpu();
  343. }
  344. EXPORT_SYMBOL(flush_dcache_page);
  345. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  346. {
  347. /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
  348. if (tlb_type == spitfire) {
  349. unsigned long kaddr;
  350. /* This code only runs on Spitfire cpus so this is
  351. * why we can assume _PAGE_PADDR_4U.
  352. */
  353. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
  354. unsigned long paddr, mask = _PAGE_PADDR_4U;
  355. if (kaddr >= PAGE_OFFSET)
  356. paddr = kaddr & mask;
  357. else {
  358. pgd_t *pgdp = pgd_offset_k(kaddr);
  359. pud_t *pudp = pud_offset(pgdp, kaddr);
  360. pmd_t *pmdp = pmd_offset(pudp, kaddr);
  361. pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
  362. paddr = pte_val(*ptep) & mask;
  363. }
  364. __flush_icache_page(paddr);
  365. }
  366. }
  367. }
  368. EXPORT_SYMBOL(flush_icache_range);
  369. void mmu_info(struct seq_file *m)
  370. {
  371. static const char *pgsz_strings[] = {
  372. "8K", "64K", "512K", "4MB", "32MB",
  373. "256MB", "2GB", "16GB",
  374. };
  375. int i, printed;
  376. if (tlb_type == cheetah)
  377. seq_printf(m, "MMU Type\t: Cheetah\n");
  378. else if (tlb_type == cheetah_plus)
  379. seq_printf(m, "MMU Type\t: Cheetah+\n");
  380. else if (tlb_type == spitfire)
  381. seq_printf(m, "MMU Type\t: Spitfire\n");
  382. else if (tlb_type == hypervisor)
  383. seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
  384. else
  385. seq_printf(m, "MMU Type\t: ???\n");
  386. seq_printf(m, "MMU PGSZs\t: ");
  387. printed = 0;
  388. for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
  389. if (cpu_pgsz_mask & (1UL << i)) {
  390. seq_printf(m, "%s%s",
  391. printed ? "," : "", pgsz_strings[i]);
  392. printed++;
  393. }
  394. }
  395. seq_putc(m, '\n');
  396. #ifdef CONFIG_DEBUG_DCFLUSH
  397. seq_printf(m, "DCPageFlushes\t: %d\n",
  398. atomic_read(&dcpage_flushes));
  399. #ifdef CONFIG_SMP
  400. seq_printf(m, "DCPageFlushesXC\t: %d\n",
  401. atomic_read(&dcpage_flushes_xcall));
  402. #endif /* CONFIG_SMP */
  403. #endif /* CONFIG_DEBUG_DCFLUSH */
  404. }
  405. struct linux_prom_translation prom_trans[512] __read_mostly;
  406. unsigned int prom_trans_ents __read_mostly;
  407. unsigned long kern_locked_tte_data;
  408. /* The obp translations are saved based on 8k pagesize, since obp can
  409. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  410. * HI_OBP_ADDRESS range are handled in ktlb.S.
  411. */
  412. static inline int in_obp_range(unsigned long vaddr)
  413. {
  414. return (vaddr >= LOW_OBP_ADDRESS &&
  415. vaddr < HI_OBP_ADDRESS);
  416. }
  417. static int cmp_ptrans(const void *a, const void *b)
  418. {
  419. const struct linux_prom_translation *x = a, *y = b;
  420. if (x->virt > y->virt)
  421. return 1;
  422. if (x->virt < y->virt)
  423. return -1;
  424. return 0;
  425. }
  426. /* Read OBP translations property into 'prom_trans[]'. */
  427. static void __init read_obp_translations(void)
  428. {
  429. int n, node, ents, first, last, i;
  430. node = prom_finddevice("/virtual-memory");
  431. n = prom_getproplen(node, "translations");
  432. if (unlikely(n == 0 || n == -1)) {
  433. prom_printf("prom_mappings: Couldn't get size.\n");
  434. prom_halt();
  435. }
  436. if (unlikely(n > sizeof(prom_trans))) {
  437. prom_printf("prom_mappings: Size %d is too big.\n", n);
  438. prom_halt();
  439. }
  440. if ((n = prom_getproperty(node, "translations",
  441. (char *)&prom_trans[0],
  442. sizeof(prom_trans))) == -1) {
  443. prom_printf("prom_mappings: Couldn't get property.\n");
  444. prom_halt();
  445. }
  446. n = n / sizeof(struct linux_prom_translation);
  447. ents = n;
  448. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  449. cmp_ptrans, NULL);
  450. /* Now kick out all the non-OBP entries. */
  451. for (i = 0; i < ents; i++) {
  452. if (in_obp_range(prom_trans[i].virt))
  453. break;
  454. }
  455. first = i;
  456. for (; i < ents; i++) {
  457. if (!in_obp_range(prom_trans[i].virt))
  458. break;
  459. }
  460. last = i;
  461. for (i = 0; i < (last - first); i++) {
  462. struct linux_prom_translation *src = &prom_trans[i + first];
  463. struct linux_prom_translation *dest = &prom_trans[i];
  464. *dest = *src;
  465. }
  466. for (; i < ents; i++) {
  467. struct linux_prom_translation *dest = &prom_trans[i];
  468. dest->virt = dest->size = dest->data = 0x0UL;
  469. }
  470. prom_trans_ents = last - first;
  471. if (tlb_type == spitfire) {
  472. /* Clear diag TTE bits. */
  473. for (i = 0; i < prom_trans_ents; i++)
  474. prom_trans[i].data &= ~0x0003fe0000000000UL;
  475. }
  476. /* Force execute bit on. */
  477. for (i = 0; i < prom_trans_ents; i++)
  478. prom_trans[i].data |= (tlb_type == hypervisor ?
  479. _PAGE_EXEC_4V : _PAGE_EXEC_4U);
  480. }
  481. static void __init hypervisor_tlb_lock(unsigned long vaddr,
  482. unsigned long pte,
  483. unsigned long mmu)
  484. {
  485. unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
  486. if (ret != 0) {
  487. prom_printf("hypervisor_tlb_lock[%lx:%x:%lx:%lx]: "
  488. "errors with %lx\n", vaddr, 0, pte, mmu, ret);
  489. prom_halt();
  490. }
  491. }
  492. static unsigned long kern_large_tte(unsigned long paddr);
  493. static void __init remap_kernel(void)
  494. {
  495. unsigned long phys_page, tte_vaddr, tte_data;
  496. int i, tlb_ent = sparc64_highest_locked_tlbent();
  497. tte_vaddr = (unsigned long) KERNBASE;
  498. phys_page = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
  499. tte_data = kern_large_tte(phys_page);
  500. kern_locked_tte_data = tte_data;
  501. /* Now lock us into the TLBs via Hypervisor or OBP. */
  502. if (tlb_type == hypervisor) {
  503. for (i = 0; i < num_kernel_image_mappings; i++) {
  504. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  505. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  506. tte_vaddr += 0x400000;
  507. tte_data += 0x400000;
  508. }
  509. } else {
  510. for (i = 0; i < num_kernel_image_mappings; i++) {
  511. prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
  512. prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
  513. tte_vaddr += 0x400000;
  514. tte_data += 0x400000;
  515. }
  516. sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
  517. }
  518. if (tlb_type == cheetah_plus) {
  519. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  520. CTX_CHEETAH_PLUS_NUC);
  521. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  522. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  523. }
  524. }
  525. static void __init inherit_prom_mappings(void)
  526. {
  527. /* Now fixup OBP's idea about where we really are mapped. */
  528. printk("Remapping the kernel... ");
  529. remap_kernel();
  530. printk("done.\n");
  531. }
  532. void prom_world(int enter)
  533. {
  534. if (!enter)
  535. set_fs(get_fs());
  536. __asm__ __volatile__("flushw");
  537. }
  538. void __flush_dcache_range(unsigned long start, unsigned long end)
  539. {
  540. unsigned long va;
  541. if (tlb_type == spitfire) {
  542. int n = 0;
  543. for (va = start; va < end; va += 32) {
  544. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  545. if (++n >= 512)
  546. break;
  547. }
  548. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  549. start = __pa(start);
  550. end = __pa(end);
  551. for (va = start; va < end; va += 32)
  552. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  553. "membar #Sync"
  554. : /* no outputs */
  555. : "r" (va),
  556. "i" (ASI_DCACHE_INVALIDATE));
  557. }
  558. }
  559. EXPORT_SYMBOL(__flush_dcache_range);
  560. /* get_new_mmu_context() uses "cache + 1". */
  561. DEFINE_SPINLOCK(ctx_alloc_lock);
  562. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  563. #define MAX_CTX_NR (1UL << CTX_NR_BITS)
  564. #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
  565. DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
  566. /* Caller does TLB context flushing on local CPU if necessary.
  567. * The caller also ensures that CTX_VALID(mm->context) is false.
  568. *
  569. * We must be careful about boundary cases so that we never
  570. * let the user have CTX 0 (nucleus) or we ever use a CTX
  571. * version of zero (and thus NO_CONTEXT would not be caught
  572. * by version mis-match tests in mmu_context.h).
  573. *
  574. * Always invoked with interrupts disabled.
  575. */
  576. void get_new_mmu_context(struct mm_struct *mm)
  577. {
  578. unsigned long ctx, new_ctx;
  579. unsigned long orig_pgsz_bits;
  580. int new_version;
  581. spin_lock(&ctx_alloc_lock);
  582. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  583. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  584. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  585. new_version = 0;
  586. if (new_ctx >= (1 << CTX_NR_BITS)) {
  587. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  588. if (new_ctx >= ctx) {
  589. int i;
  590. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  591. CTX_FIRST_VERSION;
  592. if (new_ctx == 1)
  593. new_ctx = CTX_FIRST_VERSION;
  594. /* Don't call memset, for 16 entries that's just
  595. * plain silly...
  596. */
  597. mmu_context_bmap[0] = 3;
  598. mmu_context_bmap[1] = 0;
  599. mmu_context_bmap[2] = 0;
  600. mmu_context_bmap[3] = 0;
  601. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  602. mmu_context_bmap[i + 0] = 0;
  603. mmu_context_bmap[i + 1] = 0;
  604. mmu_context_bmap[i + 2] = 0;
  605. mmu_context_bmap[i + 3] = 0;
  606. }
  607. new_version = 1;
  608. goto out;
  609. }
  610. }
  611. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  612. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  613. out:
  614. tlb_context_cache = new_ctx;
  615. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  616. spin_unlock(&ctx_alloc_lock);
  617. if (unlikely(new_version))
  618. smp_new_mmu_context_version();
  619. }
  620. static int numa_enabled = 1;
  621. static int numa_debug;
  622. static int __init early_numa(char *p)
  623. {
  624. if (!p)
  625. return 0;
  626. if (strstr(p, "off"))
  627. numa_enabled = 0;
  628. if (strstr(p, "debug"))
  629. numa_debug = 1;
  630. return 0;
  631. }
  632. early_param("numa", early_numa);
  633. #define numadbg(f, a...) \
  634. do { if (numa_debug) \
  635. printk(KERN_INFO f, ## a); \
  636. } while (0)
  637. static void __init find_ramdisk(unsigned long phys_base)
  638. {
  639. #ifdef CONFIG_BLK_DEV_INITRD
  640. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  641. unsigned long ramdisk_image;
  642. /* Older versions of the bootloader only supported a
  643. * 32-bit physical address for the ramdisk image
  644. * location, stored at sparc_ramdisk_image. Newer
  645. * SILO versions set sparc_ramdisk_image to zero and
  646. * provide a full 64-bit physical address at
  647. * sparc_ramdisk_image64.
  648. */
  649. ramdisk_image = sparc_ramdisk_image;
  650. if (!ramdisk_image)
  651. ramdisk_image = sparc_ramdisk_image64;
  652. /* Another bootloader quirk. The bootloader normalizes
  653. * the physical address to KERNBASE, so we have to
  654. * factor that back out and add in the lowest valid
  655. * physical page address to get the true physical address.
  656. */
  657. ramdisk_image -= KERNBASE;
  658. ramdisk_image += phys_base;
  659. numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
  660. ramdisk_image, sparc_ramdisk_size);
  661. initrd_start = ramdisk_image;
  662. initrd_end = ramdisk_image + sparc_ramdisk_size;
  663. memblock_reserve(initrd_start, sparc_ramdisk_size);
  664. initrd_start += PAGE_OFFSET;
  665. initrd_end += PAGE_OFFSET;
  666. }
  667. #endif
  668. }
  669. struct node_mem_mask {
  670. unsigned long mask;
  671. unsigned long val;
  672. };
  673. static struct node_mem_mask node_masks[MAX_NUMNODES];
  674. static int num_node_masks;
  675. #ifdef CONFIG_NEED_MULTIPLE_NODES
  676. int numa_cpu_lookup_table[NR_CPUS];
  677. cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
  678. struct mdesc_mblock {
  679. u64 base;
  680. u64 size;
  681. u64 offset; /* RA-to-PA */
  682. };
  683. static struct mdesc_mblock *mblocks;
  684. static int num_mblocks;
  685. static unsigned long ra_to_pa(unsigned long addr)
  686. {
  687. int i;
  688. for (i = 0; i < num_mblocks; i++) {
  689. struct mdesc_mblock *m = &mblocks[i];
  690. if (addr >= m->base &&
  691. addr < (m->base + m->size)) {
  692. addr += m->offset;
  693. break;
  694. }
  695. }
  696. return addr;
  697. }
  698. static int find_node(unsigned long addr)
  699. {
  700. int i;
  701. addr = ra_to_pa(addr);
  702. for (i = 0; i < num_node_masks; i++) {
  703. struct node_mem_mask *p = &node_masks[i];
  704. if ((addr & p->mask) == p->val)
  705. return i;
  706. }
  707. /* The following condition has been observed on LDOM guests.*/
  708. WARN_ONCE(1, "find_node: A physical address doesn't match a NUMA node"
  709. " rule. Some physical memory will be owned by node 0.");
  710. return 0;
  711. }
  712. static u64 memblock_nid_range(u64 start, u64 end, int *nid)
  713. {
  714. *nid = find_node(start);
  715. start += PAGE_SIZE;
  716. while (start < end) {
  717. int n = find_node(start);
  718. if (n != *nid)
  719. break;
  720. start += PAGE_SIZE;
  721. }
  722. if (start > end)
  723. start = end;
  724. return start;
  725. }
  726. #endif
  727. /* This must be invoked after performing all of the necessary
  728. * memblock_set_node() calls for 'nid'. We need to be able to get
  729. * correct data from get_pfn_range_for_nid().
  730. */
  731. static void __init allocate_node_data(int nid)
  732. {
  733. struct pglist_data *p;
  734. unsigned long start_pfn, end_pfn;
  735. #ifdef CONFIG_NEED_MULTIPLE_NODES
  736. unsigned long paddr;
  737. paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
  738. if (!paddr) {
  739. prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
  740. prom_halt();
  741. }
  742. NODE_DATA(nid) = __va(paddr);
  743. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  744. NODE_DATA(nid)->node_id = nid;
  745. #endif
  746. p = NODE_DATA(nid);
  747. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  748. p->node_start_pfn = start_pfn;
  749. p->node_spanned_pages = end_pfn - start_pfn;
  750. }
  751. static void init_node_masks_nonnuma(void)
  752. {
  753. #ifdef CONFIG_NEED_MULTIPLE_NODES
  754. int i;
  755. #endif
  756. numadbg("Initializing tables for non-numa.\n");
  757. node_masks[0].mask = node_masks[0].val = 0;
  758. num_node_masks = 1;
  759. #ifdef CONFIG_NEED_MULTIPLE_NODES
  760. for (i = 0; i < NR_CPUS; i++)
  761. numa_cpu_lookup_table[i] = 0;
  762. cpumask_setall(&numa_cpumask_lookup_table[0]);
  763. #endif
  764. }
  765. #ifdef CONFIG_NEED_MULTIPLE_NODES
  766. struct pglist_data *node_data[MAX_NUMNODES];
  767. EXPORT_SYMBOL(numa_cpu_lookup_table);
  768. EXPORT_SYMBOL(numa_cpumask_lookup_table);
  769. EXPORT_SYMBOL(node_data);
  770. struct mdesc_mlgroup {
  771. u64 node;
  772. u64 latency;
  773. u64 match;
  774. u64 mask;
  775. };
  776. static struct mdesc_mlgroup *mlgroups;
  777. static int num_mlgroups;
  778. static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
  779. u32 cfg_handle)
  780. {
  781. u64 arc;
  782. mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
  783. u64 target = mdesc_arc_target(md, arc);
  784. const u64 *val;
  785. val = mdesc_get_property(md, target,
  786. "cfg-handle", NULL);
  787. if (val && *val == cfg_handle)
  788. return 0;
  789. }
  790. return -ENODEV;
  791. }
  792. static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
  793. u32 cfg_handle)
  794. {
  795. u64 arc, candidate, best_latency = ~(u64)0;
  796. candidate = MDESC_NODE_NULL;
  797. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
  798. u64 target = mdesc_arc_target(md, arc);
  799. const char *name = mdesc_node_name(md, target);
  800. const u64 *val;
  801. if (strcmp(name, "pio-latency-group"))
  802. continue;
  803. val = mdesc_get_property(md, target, "latency", NULL);
  804. if (!val)
  805. continue;
  806. if (*val < best_latency) {
  807. candidate = target;
  808. best_latency = *val;
  809. }
  810. }
  811. if (candidate == MDESC_NODE_NULL)
  812. return -ENODEV;
  813. return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
  814. }
  815. int of_node_to_nid(struct device_node *dp)
  816. {
  817. const struct linux_prom64_registers *regs;
  818. struct mdesc_handle *md;
  819. u32 cfg_handle;
  820. int count, nid;
  821. u64 grp;
  822. /* This is the right thing to do on currently supported
  823. * SUN4U NUMA platforms as well, as the PCI controller does
  824. * not sit behind any particular memory controller.
  825. */
  826. if (!mlgroups)
  827. return -1;
  828. regs = of_get_property(dp, "reg", NULL);
  829. if (!regs)
  830. return -1;
  831. cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  832. md = mdesc_grab();
  833. count = 0;
  834. nid = -1;
  835. mdesc_for_each_node_by_name(md, grp, "group") {
  836. if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
  837. nid = count;
  838. break;
  839. }
  840. count++;
  841. }
  842. mdesc_release(md);
  843. return nid;
  844. }
  845. static void __init add_node_ranges(void)
  846. {
  847. struct memblock_region *reg;
  848. for_each_memblock(memory, reg) {
  849. unsigned long size = reg->size;
  850. unsigned long start, end;
  851. start = reg->base;
  852. end = start + size;
  853. while (start < end) {
  854. unsigned long this_end;
  855. int nid;
  856. this_end = memblock_nid_range(start, end, &nid);
  857. numadbg("Setting memblock NUMA node nid[%d] "
  858. "start[%lx] end[%lx]\n",
  859. nid, start, this_end);
  860. memblock_set_node(start, this_end - start,
  861. &memblock.memory, nid);
  862. start = this_end;
  863. }
  864. }
  865. }
  866. static int __init grab_mlgroups(struct mdesc_handle *md)
  867. {
  868. unsigned long paddr;
  869. int count = 0;
  870. u64 node;
  871. mdesc_for_each_node_by_name(md, node, "memory-latency-group")
  872. count++;
  873. if (!count)
  874. return -ENOENT;
  875. paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
  876. SMP_CACHE_BYTES);
  877. if (!paddr)
  878. return -ENOMEM;
  879. mlgroups = __va(paddr);
  880. num_mlgroups = count;
  881. count = 0;
  882. mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
  883. struct mdesc_mlgroup *m = &mlgroups[count++];
  884. const u64 *val;
  885. m->node = node;
  886. val = mdesc_get_property(md, node, "latency", NULL);
  887. m->latency = *val;
  888. val = mdesc_get_property(md, node, "address-match", NULL);
  889. m->match = *val;
  890. val = mdesc_get_property(md, node, "address-mask", NULL);
  891. m->mask = *val;
  892. numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
  893. "match[%llx] mask[%llx]\n",
  894. count - 1, m->node, m->latency, m->match, m->mask);
  895. }
  896. return 0;
  897. }
  898. static int __init grab_mblocks(struct mdesc_handle *md)
  899. {
  900. unsigned long paddr;
  901. int count = 0;
  902. u64 node;
  903. mdesc_for_each_node_by_name(md, node, "mblock")
  904. count++;
  905. if (!count)
  906. return -ENOENT;
  907. paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
  908. SMP_CACHE_BYTES);
  909. if (!paddr)
  910. return -ENOMEM;
  911. mblocks = __va(paddr);
  912. num_mblocks = count;
  913. count = 0;
  914. mdesc_for_each_node_by_name(md, node, "mblock") {
  915. struct mdesc_mblock *m = &mblocks[count++];
  916. const u64 *val;
  917. val = mdesc_get_property(md, node, "base", NULL);
  918. m->base = *val;
  919. val = mdesc_get_property(md, node, "size", NULL);
  920. m->size = *val;
  921. val = mdesc_get_property(md, node,
  922. "address-congruence-offset", NULL);
  923. /* The address-congruence-offset property is optional.
  924. * Explicity zero it be identifty this.
  925. */
  926. if (val)
  927. m->offset = *val;
  928. else
  929. m->offset = 0UL;
  930. numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
  931. count - 1, m->base, m->size, m->offset);
  932. }
  933. return 0;
  934. }
  935. static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
  936. u64 grp, cpumask_t *mask)
  937. {
  938. u64 arc;
  939. cpumask_clear(mask);
  940. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
  941. u64 target = mdesc_arc_target(md, arc);
  942. const char *name = mdesc_node_name(md, target);
  943. const u64 *id;
  944. if (strcmp(name, "cpu"))
  945. continue;
  946. id = mdesc_get_property(md, target, "id", NULL);
  947. if (*id < nr_cpu_ids)
  948. cpumask_set_cpu(*id, mask);
  949. }
  950. }
  951. static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
  952. {
  953. int i;
  954. for (i = 0; i < num_mlgroups; i++) {
  955. struct mdesc_mlgroup *m = &mlgroups[i];
  956. if (m->node == node)
  957. return m;
  958. }
  959. return NULL;
  960. }
  961. static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
  962. int index)
  963. {
  964. struct mdesc_mlgroup *candidate = NULL;
  965. u64 arc, best_latency = ~(u64)0;
  966. struct node_mem_mask *n;
  967. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
  968. u64 target = mdesc_arc_target(md, arc);
  969. struct mdesc_mlgroup *m = find_mlgroup(target);
  970. if (!m)
  971. continue;
  972. if (m->latency < best_latency) {
  973. candidate = m;
  974. best_latency = m->latency;
  975. }
  976. }
  977. if (!candidate)
  978. return -ENOENT;
  979. if (num_node_masks != index) {
  980. printk(KERN_ERR "Inconsistent NUMA state, "
  981. "index[%d] != num_node_masks[%d]\n",
  982. index, num_node_masks);
  983. return -EINVAL;
  984. }
  985. n = &node_masks[num_node_masks++];
  986. n->mask = candidate->mask;
  987. n->val = candidate->match;
  988. numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%llx])\n",
  989. index, n->mask, n->val, candidate->latency);
  990. return 0;
  991. }
  992. static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
  993. int index)
  994. {
  995. cpumask_t mask;
  996. int cpu;
  997. numa_parse_mdesc_group_cpus(md, grp, &mask);
  998. for_each_cpu(cpu, &mask)
  999. numa_cpu_lookup_table[cpu] = index;
  1000. cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
  1001. if (numa_debug) {
  1002. printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
  1003. for_each_cpu(cpu, &mask)
  1004. printk("%d ", cpu);
  1005. printk("]\n");
  1006. }
  1007. return numa_attach_mlgroup(md, grp, index);
  1008. }
  1009. static int __init numa_parse_mdesc(void)
  1010. {
  1011. struct mdesc_handle *md = mdesc_grab();
  1012. int i, err, count;
  1013. u64 node;
  1014. node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
  1015. if (node == MDESC_NODE_NULL) {
  1016. mdesc_release(md);
  1017. return -ENOENT;
  1018. }
  1019. err = grab_mblocks(md);
  1020. if (err < 0)
  1021. goto out;
  1022. err = grab_mlgroups(md);
  1023. if (err < 0)
  1024. goto out;
  1025. count = 0;
  1026. mdesc_for_each_node_by_name(md, node, "group") {
  1027. err = numa_parse_mdesc_group(md, node, count);
  1028. if (err < 0)
  1029. break;
  1030. count++;
  1031. }
  1032. add_node_ranges();
  1033. for (i = 0; i < num_node_masks; i++) {
  1034. allocate_node_data(i);
  1035. node_set_online(i);
  1036. }
  1037. err = 0;
  1038. out:
  1039. mdesc_release(md);
  1040. return err;
  1041. }
  1042. static int __init numa_parse_jbus(void)
  1043. {
  1044. unsigned long cpu, index;
  1045. /* NUMA node id is encoded in bits 36 and higher, and there is
  1046. * a 1-to-1 mapping from CPU ID to NUMA node ID.
  1047. */
  1048. index = 0;
  1049. for_each_present_cpu(cpu) {
  1050. numa_cpu_lookup_table[cpu] = index;
  1051. cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
  1052. node_masks[index].mask = ~((1UL << 36UL) - 1UL);
  1053. node_masks[index].val = cpu << 36UL;
  1054. index++;
  1055. }
  1056. num_node_masks = index;
  1057. add_node_ranges();
  1058. for (index = 0; index < num_node_masks; index++) {
  1059. allocate_node_data(index);
  1060. node_set_online(index);
  1061. }
  1062. return 0;
  1063. }
  1064. static int __init numa_parse_sun4u(void)
  1065. {
  1066. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1067. unsigned long ver;
  1068. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  1069. if ((ver >> 32UL) == __JALAPENO_ID ||
  1070. (ver >> 32UL) == __SERRANO_ID)
  1071. return numa_parse_jbus();
  1072. }
  1073. return -1;
  1074. }
  1075. static int __init bootmem_init_numa(void)
  1076. {
  1077. int err = -1;
  1078. numadbg("bootmem_init_numa()\n");
  1079. if (numa_enabled) {
  1080. if (tlb_type == hypervisor)
  1081. err = numa_parse_mdesc();
  1082. else
  1083. err = numa_parse_sun4u();
  1084. }
  1085. return err;
  1086. }
  1087. #else
  1088. static int bootmem_init_numa(void)
  1089. {
  1090. return -1;
  1091. }
  1092. #endif
  1093. static void __init bootmem_init_nonnuma(void)
  1094. {
  1095. unsigned long top_of_ram = memblock_end_of_DRAM();
  1096. unsigned long total_ram = memblock_phys_mem_size();
  1097. numadbg("bootmem_init_nonnuma()\n");
  1098. printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  1099. top_of_ram, total_ram);
  1100. printk(KERN_INFO "Memory hole size: %ldMB\n",
  1101. (top_of_ram - total_ram) >> 20);
  1102. init_node_masks_nonnuma();
  1103. memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
  1104. allocate_node_data(0);
  1105. node_set_online(0);
  1106. }
  1107. static unsigned long __init bootmem_init(unsigned long phys_base)
  1108. {
  1109. unsigned long end_pfn;
  1110. end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  1111. max_pfn = max_low_pfn = end_pfn;
  1112. min_low_pfn = (phys_base >> PAGE_SHIFT);
  1113. if (bootmem_init_numa() < 0)
  1114. bootmem_init_nonnuma();
  1115. /* Dump memblock with node info. */
  1116. memblock_dump_all();
  1117. /* XXX cpu notifier XXX */
  1118. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  1119. sparse_init();
  1120. return end_pfn;
  1121. }
  1122. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  1123. static int pall_ents __initdata;
  1124. static unsigned long max_phys_bits = 40;
  1125. bool kern_addr_valid(unsigned long addr)
  1126. {
  1127. pgd_t *pgd;
  1128. pud_t *pud;
  1129. pmd_t *pmd;
  1130. pte_t *pte;
  1131. if ((long)addr < 0L) {
  1132. unsigned long pa = __pa(addr);
  1133. if ((addr >> max_phys_bits) != 0UL)
  1134. return false;
  1135. return pfn_valid(pa >> PAGE_SHIFT);
  1136. }
  1137. if (addr >= (unsigned long) KERNBASE &&
  1138. addr < (unsigned long)&_end)
  1139. return true;
  1140. pgd = pgd_offset_k(addr);
  1141. if (pgd_none(*pgd))
  1142. return 0;
  1143. pud = pud_offset(pgd, addr);
  1144. if (pud_none(*pud))
  1145. return 0;
  1146. if (pud_large(*pud))
  1147. return pfn_valid(pud_pfn(*pud));
  1148. pmd = pmd_offset(pud, addr);
  1149. if (pmd_none(*pmd))
  1150. return 0;
  1151. if (pmd_large(*pmd))
  1152. return pfn_valid(pmd_pfn(*pmd));
  1153. pte = pte_offset_kernel(pmd, addr);
  1154. if (pte_none(*pte))
  1155. return 0;
  1156. return pfn_valid(pte_pfn(*pte));
  1157. }
  1158. EXPORT_SYMBOL(kern_addr_valid);
  1159. static unsigned long __ref kernel_map_hugepud(unsigned long vstart,
  1160. unsigned long vend,
  1161. pud_t *pud)
  1162. {
  1163. const unsigned long mask16gb = (1UL << 34) - 1UL;
  1164. u64 pte_val = vstart;
  1165. /* Each PUD is 8GB */
  1166. if ((vstart & mask16gb) ||
  1167. (vend - vstart <= mask16gb)) {
  1168. pte_val ^= kern_linear_pte_xor[2];
  1169. pud_val(*pud) = pte_val | _PAGE_PUD_HUGE;
  1170. return vstart + PUD_SIZE;
  1171. }
  1172. pte_val ^= kern_linear_pte_xor[3];
  1173. pte_val |= _PAGE_PUD_HUGE;
  1174. vend = vstart + mask16gb + 1UL;
  1175. while (vstart < vend) {
  1176. pud_val(*pud) = pte_val;
  1177. pte_val += PUD_SIZE;
  1178. vstart += PUD_SIZE;
  1179. pud++;
  1180. }
  1181. return vstart;
  1182. }
  1183. static bool kernel_can_map_hugepud(unsigned long vstart, unsigned long vend,
  1184. bool guard)
  1185. {
  1186. if (guard && !(vstart & ~PUD_MASK) && (vend - vstart) >= PUD_SIZE)
  1187. return true;
  1188. return false;
  1189. }
  1190. static unsigned long __ref kernel_map_hugepmd(unsigned long vstart,
  1191. unsigned long vend,
  1192. pmd_t *pmd)
  1193. {
  1194. const unsigned long mask256mb = (1UL << 28) - 1UL;
  1195. const unsigned long mask2gb = (1UL << 31) - 1UL;
  1196. u64 pte_val = vstart;
  1197. /* Each PMD is 8MB */
  1198. if ((vstart & mask256mb) ||
  1199. (vend - vstart <= mask256mb)) {
  1200. pte_val ^= kern_linear_pte_xor[0];
  1201. pmd_val(*pmd) = pte_val | _PAGE_PMD_HUGE;
  1202. return vstart + PMD_SIZE;
  1203. }
  1204. if ((vstart & mask2gb) ||
  1205. (vend - vstart <= mask2gb)) {
  1206. pte_val ^= kern_linear_pte_xor[1];
  1207. pte_val |= _PAGE_PMD_HUGE;
  1208. vend = vstart + mask256mb + 1UL;
  1209. } else {
  1210. pte_val ^= kern_linear_pte_xor[2];
  1211. pte_val |= _PAGE_PMD_HUGE;
  1212. vend = vstart + mask2gb + 1UL;
  1213. }
  1214. while (vstart < vend) {
  1215. pmd_val(*pmd) = pte_val;
  1216. pte_val += PMD_SIZE;
  1217. vstart += PMD_SIZE;
  1218. pmd++;
  1219. }
  1220. return vstart;
  1221. }
  1222. static bool kernel_can_map_hugepmd(unsigned long vstart, unsigned long vend,
  1223. bool guard)
  1224. {
  1225. if (guard && !(vstart & ~PMD_MASK) && (vend - vstart) >= PMD_SIZE)
  1226. return true;
  1227. return false;
  1228. }
  1229. static unsigned long __ref kernel_map_range(unsigned long pstart,
  1230. unsigned long pend, pgprot_t prot,
  1231. bool use_huge)
  1232. {
  1233. unsigned long vstart = PAGE_OFFSET + pstart;
  1234. unsigned long vend = PAGE_OFFSET + pend;
  1235. unsigned long alloc_bytes = 0UL;
  1236. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  1237. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  1238. vstart, vend);
  1239. prom_halt();
  1240. }
  1241. while (vstart < vend) {
  1242. unsigned long this_end, paddr = __pa(vstart);
  1243. pgd_t *pgd = pgd_offset_k(vstart);
  1244. pud_t *pud;
  1245. pmd_t *pmd;
  1246. pte_t *pte;
  1247. if (pgd_none(*pgd)) {
  1248. pud_t *new;
  1249. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1250. alloc_bytes += PAGE_SIZE;
  1251. pgd_populate(&init_mm, pgd, new);
  1252. }
  1253. pud = pud_offset(pgd, vstart);
  1254. if (pud_none(*pud)) {
  1255. pmd_t *new;
  1256. if (kernel_can_map_hugepud(vstart, vend, use_huge)) {
  1257. vstart = kernel_map_hugepud(vstart, vend, pud);
  1258. continue;
  1259. }
  1260. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1261. alloc_bytes += PAGE_SIZE;
  1262. pud_populate(&init_mm, pud, new);
  1263. }
  1264. pmd = pmd_offset(pud, vstart);
  1265. if (pmd_none(*pmd)) {
  1266. pte_t *new;
  1267. if (kernel_can_map_hugepmd(vstart, vend, use_huge)) {
  1268. vstart = kernel_map_hugepmd(vstart, vend, pmd);
  1269. continue;
  1270. }
  1271. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1272. alloc_bytes += PAGE_SIZE;
  1273. pmd_populate_kernel(&init_mm, pmd, new);
  1274. }
  1275. pte = pte_offset_kernel(pmd, vstart);
  1276. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  1277. if (this_end > vend)
  1278. this_end = vend;
  1279. while (vstart < this_end) {
  1280. pte_val(*pte) = (paddr | pgprot_val(prot));
  1281. vstart += PAGE_SIZE;
  1282. paddr += PAGE_SIZE;
  1283. pte++;
  1284. }
  1285. }
  1286. return alloc_bytes;
  1287. }
  1288. static void __init flush_all_kernel_tsbs(void)
  1289. {
  1290. int i;
  1291. for (i = 0; i < KERNEL_TSB_NENTRIES; i++) {
  1292. struct tsb *ent = &swapper_tsb[i];
  1293. ent->tag = (1UL << TSB_TAG_INVALID_BIT);
  1294. }
  1295. #ifndef CONFIG_DEBUG_PAGEALLOC
  1296. for (i = 0; i < KERNEL_TSB4M_NENTRIES; i++) {
  1297. struct tsb *ent = &swapper_4m_tsb[i];
  1298. ent->tag = (1UL << TSB_TAG_INVALID_BIT);
  1299. }
  1300. #endif
  1301. }
  1302. extern unsigned int kvmap_linear_patch[1];
  1303. static void __init kernel_physical_mapping_init(void)
  1304. {
  1305. unsigned long i, mem_alloced = 0UL;
  1306. bool use_huge = true;
  1307. #ifdef CONFIG_DEBUG_PAGEALLOC
  1308. use_huge = false;
  1309. #endif
  1310. for (i = 0; i < pall_ents; i++) {
  1311. unsigned long phys_start, phys_end;
  1312. phys_start = pall[i].phys_addr;
  1313. phys_end = phys_start + pall[i].reg_size;
  1314. mem_alloced += kernel_map_range(phys_start, phys_end,
  1315. PAGE_KERNEL, use_huge);
  1316. }
  1317. printk("Allocated %ld bytes for kernel page tables.\n",
  1318. mem_alloced);
  1319. kvmap_linear_patch[0] = 0x01000000; /* nop */
  1320. flushi(&kvmap_linear_patch[0]);
  1321. flush_all_kernel_tsbs();
  1322. __flush_tlb_all();
  1323. }
  1324. #ifdef CONFIG_DEBUG_PAGEALLOC
  1325. void kernel_map_pages(struct page *page, int numpages, int enable)
  1326. {
  1327. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  1328. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  1329. kernel_map_range(phys_start, phys_end,
  1330. (enable ? PAGE_KERNEL : __pgprot(0)), false);
  1331. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  1332. PAGE_OFFSET + phys_end);
  1333. /* we should perform an IPI and flush all tlbs,
  1334. * but that can deadlock->flush only current cpu.
  1335. */
  1336. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  1337. PAGE_OFFSET + phys_end);
  1338. }
  1339. #endif
  1340. unsigned long __init find_ecache_flush_span(unsigned long size)
  1341. {
  1342. int i;
  1343. for (i = 0; i < pavail_ents; i++) {
  1344. if (pavail[i].reg_size >= size)
  1345. return pavail[i].phys_addr;
  1346. }
  1347. return ~0UL;
  1348. }
  1349. unsigned long PAGE_OFFSET;
  1350. EXPORT_SYMBOL(PAGE_OFFSET);
  1351. unsigned long VMALLOC_END = 0x0000010000000000UL;
  1352. EXPORT_SYMBOL(VMALLOC_END);
  1353. unsigned long sparc64_va_hole_top = 0xfffff80000000000UL;
  1354. unsigned long sparc64_va_hole_bottom = 0x0000080000000000UL;
  1355. static void __init setup_page_offset(void)
  1356. {
  1357. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1358. /* Cheetah/Panther support a full 64-bit virtual
  1359. * address, so we can use all that our page tables
  1360. * support.
  1361. */
  1362. sparc64_va_hole_top = 0xfff0000000000000UL;
  1363. sparc64_va_hole_bottom = 0x0010000000000000UL;
  1364. max_phys_bits = 42;
  1365. } else if (tlb_type == hypervisor) {
  1366. switch (sun4v_chip_type) {
  1367. case SUN4V_CHIP_NIAGARA1:
  1368. case SUN4V_CHIP_NIAGARA2:
  1369. /* T1 and T2 support 48-bit virtual addresses. */
  1370. sparc64_va_hole_top = 0xffff800000000000UL;
  1371. sparc64_va_hole_bottom = 0x0000800000000000UL;
  1372. max_phys_bits = 39;
  1373. break;
  1374. case SUN4V_CHIP_NIAGARA3:
  1375. /* T3 supports 48-bit virtual addresses. */
  1376. sparc64_va_hole_top = 0xffff800000000000UL;
  1377. sparc64_va_hole_bottom = 0x0000800000000000UL;
  1378. max_phys_bits = 43;
  1379. break;
  1380. case SUN4V_CHIP_NIAGARA4:
  1381. case SUN4V_CHIP_NIAGARA5:
  1382. case SUN4V_CHIP_SPARC64X:
  1383. case SUN4V_CHIP_SPARC_M6:
  1384. /* T4 and later support 52-bit virtual addresses. */
  1385. sparc64_va_hole_top = 0xfff8000000000000UL;
  1386. sparc64_va_hole_bottom = 0x0008000000000000UL;
  1387. max_phys_bits = 47;
  1388. break;
  1389. case SUN4V_CHIP_SPARC_M7:
  1390. default:
  1391. /* M7 and later support 52-bit virtual addresses. */
  1392. sparc64_va_hole_top = 0xfff8000000000000UL;
  1393. sparc64_va_hole_bottom = 0x0008000000000000UL;
  1394. max_phys_bits = 49;
  1395. break;
  1396. }
  1397. }
  1398. if (max_phys_bits > MAX_PHYS_ADDRESS_BITS) {
  1399. prom_printf("MAX_PHYS_ADDRESS_BITS is too small, need %lu\n",
  1400. max_phys_bits);
  1401. prom_halt();
  1402. }
  1403. PAGE_OFFSET = sparc64_va_hole_top;
  1404. VMALLOC_END = ((sparc64_va_hole_bottom >> 1) +
  1405. (sparc64_va_hole_bottom >> 2));
  1406. pr_info("MM: PAGE_OFFSET is 0x%016lx (max_phys_bits == %lu)\n",
  1407. PAGE_OFFSET, max_phys_bits);
  1408. pr_info("MM: VMALLOC [0x%016lx --> 0x%016lx]\n",
  1409. VMALLOC_START, VMALLOC_END);
  1410. pr_info("MM: VMEMMAP [0x%016lx --> 0x%016lx]\n",
  1411. VMEMMAP_BASE, VMEMMAP_BASE << 1);
  1412. }
  1413. static void __init tsb_phys_patch(void)
  1414. {
  1415. struct tsb_ldquad_phys_patch_entry *pquad;
  1416. struct tsb_phys_patch_entry *p;
  1417. pquad = &__tsb_ldquad_phys_patch;
  1418. while (pquad < &__tsb_ldquad_phys_patch_end) {
  1419. unsigned long addr = pquad->addr;
  1420. if (tlb_type == hypervisor)
  1421. *(unsigned int *) addr = pquad->sun4v_insn;
  1422. else
  1423. *(unsigned int *) addr = pquad->sun4u_insn;
  1424. wmb();
  1425. __asm__ __volatile__("flush %0"
  1426. : /* no outputs */
  1427. : "r" (addr));
  1428. pquad++;
  1429. }
  1430. p = &__tsb_phys_patch;
  1431. while (p < &__tsb_phys_patch_end) {
  1432. unsigned long addr = p->addr;
  1433. *(unsigned int *) addr = p->insn;
  1434. wmb();
  1435. __asm__ __volatile__("flush %0"
  1436. : /* no outputs */
  1437. : "r" (addr));
  1438. p++;
  1439. }
  1440. }
  1441. /* Don't mark as init, we give this to the Hypervisor. */
  1442. #ifndef CONFIG_DEBUG_PAGEALLOC
  1443. #define NUM_KTSB_DESCR 2
  1444. #else
  1445. #define NUM_KTSB_DESCR 1
  1446. #endif
  1447. static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
  1448. /* The swapper TSBs are loaded with a base sequence of:
  1449. *
  1450. * sethi %uhi(SYMBOL), REG1
  1451. * sethi %hi(SYMBOL), REG2
  1452. * or REG1, %ulo(SYMBOL), REG1
  1453. * or REG2, %lo(SYMBOL), REG2
  1454. * sllx REG1, 32, REG1
  1455. * or REG1, REG2, REG1
  1456. *
  1457. * When we use physical addressing for the TSB accesses, we patch the
  1458. * first four instructions in the above sequence.
  1459. */
  1460. static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
  1461. {
  1462. unsigned long high_bits, low_bits;
  1463. high_bits = (pa >> 32) & 0xffffffff;
  1464. low_bits = (pa >> 0) & 0xffffffff;
  1465. while (start < end) {
  1466. unsigned int *ia = (unsigned int *)(unsigned long)*start;
  1467. ia[0] = (ia[0] & ~0x3fffff) | (high_bits >> 10);
  1468. __asm__ __volatile__("flush %0" : : "r" (ia));
  1469. ia[1] = (ia[1] & ~0x3fffff) | (low_bits >> 10);
  1470. __asm__ __volatile__("flush %0" : : "r" (ia + 1));
  1471. ia[2] = (ia[2] & ~0x1fff) | (high_bits & 0x3ff);
  1472. __asm__ __volatile__("flush %0" : : "r" (ia + 2));
  1473. ia[3] = (ia[3] & ~0x1fff) | (low_bits & 0x3ff);
  1474. __asm__ __volatile__("flush %0" : : "r" (ia + 3));
  1475. start++;
  1476. }
  1477. }
  1478. static void ktsb_phys_patch(void)
  1479. {
  1480. extern unsigned int __swapper_tsb_phys_patch;
  1481. extern unsigned int __swapper_tsb_phys_patch_end;
  1482. unsigned long ktsb_pa;
  1483. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1484. patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
  1485. &__swapper_tsb_phys_patch_end, ktsb_pa);
  1486. #ifndef CONFIG_DEBUG_PAGEALLOC
  1487. {
  1488. extern unsigned int __swapper_4m_tsb_phys_patch;
  1489. extern unsigned int __swapper_4m_tsb_phys_patch_end;
  1490. ktsb_pa = (kern_base +
  1491. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1492. patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
  1493. &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
  1494. }
  1495. #endif
  1496. }
  1497. static void __init sun4v_ktsb_init(void)
  1498. {
  1499. unsigned long ktsb_pa;
  1500. /* First KTSB for PAGE_SIZE mappings. */
  1501. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1502. switch (PAGE_SIZE) {
  1503. case 8 * 1024:
  1504. default:
  1505. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  1506. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  1507. break;
  1508. case 64 * 1024:
  1509. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1510. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1511. break;
  1512. case 512 * 1024:
  1513. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1514. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1515. break;
  1516. case 4 * 1024 * 1024:
  1517. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1518. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1519. break;
  1520. }
  1521. ktsb_descr[0].assoc = 1;
  1522. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1523. ktsb_descr[0].ctx_idx = 0;
  1524. ktsb_descr[0].tsb_base = ktsb_pa;
  1525. ktsb_descr[0].resv = 0;
  1526. #ifndef CONFIG_DEBUG_PAGEALLOC
  1527. /* Second KTSB for 4MB/256MB/2GB/16GB mappings. */
  1528. ktsb_pa = (kern_base +
  1529. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1530. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1531. ktsb_descr[1].pgsz_mask = ((HV_PGSZ_MASK_4MB |
  1532. HV_PGSZ_MASK_256MB |
  1533. HV_PGSZ_MASK_2GB |
  1534. HV_PGSZ_MASK_16GB) &
  1535. cpu_pgsz_mask);
  1536. ktsb_descr[1].assoc = 1;
  1537. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1538. ktsb_descr[1].ctx_idx = 0;
  1539. ktsb_descr[1].tsb_base = ktsb_pa;
  1540. ktsb_descr[1].resv = 0;
  1541. #endif
  1542. }
  1543. void sun4v_ktsb_register(void)
  1544. {
  1545. unsigned long pa, ret;
  1546. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1547. ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
  1548. if (ret != 0) {
  1549. prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
  1550. "errors with %lx\n", pa, ret);
  1551. prom_halt();
  1552. }
  1553. }
  1554. static void __init sun4u_linear_pte_xor_finalize(void)
  1555. {
  1556. #ifndef CONFIG_DEBUG_PAGEALLOC
  1557. /* This is where we would add Panther support for
  1558. * 32MB and 256MB pages.
  1559. */
  1560. #endif
  1561. }
  1562. static void __init sun4v_linear_pte_xor_finalize(void)
  1563. {
  1564. #ifndef CONFIG_DEBUG_PAGEALLOC
  1565. if (cpu_pgsz_mask & HV_PGSZ_MASK_256MB) {
  1566. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1567. PAGE_OFFSET;
  1568. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1569. _PAGE_P_4V | _PAGE_W_4V);
  1570. } else {
  1571. kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
  1572. }
  1573. if (cpu_pgsz_mask & HV_PGSZ_MASK_2GB) {
  1574. kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
  1575. PAGE_OFFSET;
  1576. kern_linear_pte_xor[2] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1577. _PAGE_P_4V | _PAGE_W_4V);
  1578. } else {
  1579. kern_linear_pte_xor[2] = kern_linear_pte_xor[1];
  1580. }
  1581. if (cpu_pgsz_mask & HV_PGSZ_MASK_16GB) {
  1582. kern_linear_pte_xor[3] = (_PAGE_VALID | _PAGE_SZ16GB_4V) ^
  1583. PAGE_OFFSET;
  1584. kern_linear_pte_xor[3] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1585. _PAGE_P_4V | _PAGE_W_4V);
  1586. } else {
  1587. kern_linear_pte_xor[3] = kern_linear_pte_xor[2];
  1588. }
  1589. #endif
  1590. }
  1591. /* paging_init() sets up the page tables */
  1592. static unsigned long last_valid_pfn;
  1593. static void sun4u_pgprot_init(void);
  1594. static void sun4v_pgprot_init(void);
  1595. static phys_addr_t __init available_memory(void)
  1596. {
  1597. phys_addr_t available = 0ULL;
  1598. phys_addr_t pa_start, pa_end;
  1599. u64 i;
  1600. for_each_free_mem_range(i, NUMA_NO_NODE, &pa_start, &pa_end, NULL)
  1601. available = available + (pa_end - pa_start);
  1602. return available;
  1603. }
  1604. /* We need to exclude reserved regions. This exclusion will include
  1605. * vmlinux and initrd. To be more precise the initrd size could be used to
  1606. * compute a new lower limit because it is freed later during initialization.
  1607. */
  1608. static void __init reduce_memory(phys_addr_t limit_ram)
  1609. {
  1610. phys_addr_t avail_ram = available_memory();
  1611. phys_addr_t pa_start, pa_end;
  1612. u64 i;
  1613. if (limit_ram >= avail_ram)
  1614. return;
  1615. for_each_free_mem_range(i, NUMA_NO_NODE, &pa_start, &pa_end, NULL) {
  1616. phys_addr_t region_size = pa_end - pa_start;
  1617. phys_addr_t clip_start = pa_start;
  1618. avail_ram = avail_ram - region_size;
  1619. /* Are we consuming too much? */
  1620. if (avail_ram < limit_ram) {
  1621. phys_addr_t give_back = limit_ram - avail_ram;
  1622. region_size = region_size - give_back;
  1623. clip_start = clip_start + give_back;
  1624. }
  1625. memblock_remove(clip_start, region_size);
  1626. if (avail_ram <= limit_ram)
  1627. break;
  1628. i = 0UL;
  1629. }
  1630. }
  1631. void __init paging_init(void)
  1632. {
  1633. unsigned long end_pfn, shift, phys_base;
  1634. unsigned long real_end, i;
  1635. int node;
  1636. setup_page_offset();
  1637. /* These build time checkes make sure that the dcache_dirty_cpu()
  1638. * page->flags usage will work.
  1639. *
  1640. * When a page gets marked as dcache-dirty, we store the
  1641. * cpu number starting at bit 32 in the page->flags. Also,
  1642. * functions like clear_dcache_dirty_cpu use the cpu mask
  1643. * in 13-bit signed-immediate instruction fields.
  1644. */
  1645. /*
  1646. * Page flags must not reach into upper 32 bits that are used
  1647. * for the cpu number
  1648. */
  1649. BUILD_BUG_ON(NR_PAGEFLAGS > 32);
  1650. /*
  1651. * The bit fields placed in the high range must not reach below
  1652. * the 32 bit boundary. Otherwise we cannot place the cpu field
  1653. * at the 32 bit boundary.
  1654. */
  1655. BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
  1656. ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
  1657. BUILD_BUG_ON(NR_CPUS > 4096);
  1658. kern_base = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
  1659. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1660. /* Invalidate both kernel TSBs. */
  1661. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1662. #ifndef CONFIG_DEBUG_PAGEALLOC
  1663. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1664. #endif
  1665. if (tlb_type == hypervisor)
  1666. sun4v_pgprot_init();
  1667. else
  1668. sun4u_pgprot_init();
  1669. if (tlb_type == cheetah_plus ||
  1670. tlb_type == hypervisor) {
  1671. tsb_phys_patch();
  1672. ktsb_phys_patch();
  1673. }
  1674. if (tlb_type == hypervisor)
  1675. sun4v_patch_tlb_handlers();
  1676. /* Find available physical memory...
  1677. *
  1678. * Read it twice in order to work around a bug in openfirmware.
  1679. * The call to grab this table itself can cause openfirmware to
  1680. * allocate memory, which in turn can take away some space from
  1681. * the list of available memory. Reading it twice makes sure
  1682. * we really do get the final value.
  1683. */
  1684. read_obp_translations();
  1685. read_obp_memory("reg", &pall[0], &pall_ents);
  1686. read_obp_memory("available", &pavail[0], &pavail_ents);
  1687. read_obp_memory("available", &pavail[0], &pavail_ents);
  1688. phys_base = 0xffffffffffffffffUL;
  1689. for (i = 0; i < pavail_ents; i++) {
  1690. phys_base = min(phys_base, pavail[i].phys_addr);
  1691. memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
  1692. }
  1693. memblock_reserve(kern_base, kern_size);
  1694. find_ramdisk(phys_base);
  1695. if (cmdline_memory_size)
  1696. reduce_memory(cmdline_memory_size);
  1697. memblock_allow_resize();
  1698. memblock_dump_all();
  1699. set_bit(0, mmu_context_bmap);
  1700. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1701. real_end = (unsigned long)_end;
  1702. num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << ILOG2_4MB);
  1703. printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
  1704. num_kernel_image_mappings);
  1705. /* Set kernel pgd to upper alias so physical page computations
  1706. * work.
  1707. */
  1708. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1709. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  1710. inherit_prom_mappings();
  1711. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1712. setup_tba();
  1713. __flush_tlb_all();
  1714. prom_build_devicetree();
  1715. of_populate_present_mask();
  1716. #ifndef CONFIG_SMP
  1717. of_fill_in_cpu_data();
  1718. #endif
  1719. if (tlb_type == hypervisor) {
  1720. sun4v_mdesc_init();
  1721. mdesc_populate_present_mask(cpu_all_mask);
  1722. #ifndef CONFIG_SMP
  1723. mdesc_fill_in_cpu_data(cpu_all_mask);
  1724. #endif
  1725. mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
  1726. sun4v_linear_pte_xor_finalize();
  1727. sun4v_ktsb_init();
  1728. sun4v_ktsb_register();
  1729. } else {
  1730. unsigned long impl, ver;
  1731. cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
  1732. HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
  1733. __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
  1734. impl = ((ver >> 32) & 0xffff);
  1735. if (impl == PANTHER_IMPL)
  1736. cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
  1737. HV_PGSZ_MASK_256MB);
  1738. sun4u_linear_pte_xor_finalize();
  1739. }
  1740. /* Flush the TLBs and the 4M TSB so that the updated linear
  1741. * pte XOR settings are realized for all mappings.
  1742. */
  1743. __flush_tlb_all();
  1744. #ifndef CONFIG_DEBUG_PAGEALLOC
  1745. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1746. #endif
  1747. __flush_tlb_all();
  1748. /* Setup bootmem... */
  1749. last_valid_pfn = end_pfn = bootmem_init(phys_base);
  1750. /* Once the OF device tree and MDESC have been setup, we know
  1751. * the list of possible cpus. Therefore we can allocate the
  1752. * IRQ stacks.
  1753. */
  1754. for_each_possible_cpu(i) {
  1755. node = cpu_to_node(i);
  1756. softirq_stack[i] = __alloc_bootmem_node(NODE_DATA(node),
  1757. THREAD_SIZE,
  1758. THREAD_SIZE, 0);
  1759. hardirq_stack[i] = __alloc_bootmem_node(NODE_DATA(node),
  1760. THREAD_SIZE,
  1761. THREAD_SIZE, 0);
  1762. }
  1763. kernel_physical_mapping_init();
  1764. {
  1765. unsigned long max_zone_pfns[MAX_NR_ZONES];
  1766. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  1767. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  1768. free_area_init_nodes(max_zone_pfns);
  1769. }
  1770. printk("Booting Linux...\n");
  1771. }
  1772. int page_in_phys_avail(unsigned long paddr)
  1773. {
  1774. int i;
  1775. paddr &= PAGE_MASK;
  1776. for (i = 0; i < pavail_ents; i++) {
  1777. unsigned long start, end;
  1778. start = pavail[i].phys_addr;
  1779. end = start + pavail[i].reg_size;
  1780. if (paddr >= start && paddr < end)
  1781. return 1;
  1782. }
  1783. if (paddr >= kern_base && paddr < (kern_base + kern_size))
  1784. return 1;
  1785. #ifdef CONFIG_BLK_DEV_INITRD
  1786. if (paddr >= __pa(initrd_start) &&
  1787. paddr < __pa(PAGE_ALIGN(initrd_end)))
  1788. return 1;
  1789. #endif
  1790. return 0;
  1791. }
  1792. static void __init register_page_bootmem_info(void)
  1793. {
  1794. #ifdef CONFIG_NEED_MULTIPLE_NODES
  1795. int i;
  1796. for_each_online_node(i)
  1797. if (NODE_DATA(i)->node_spanned_pages)
  1798. register_page_bootmem_info_node(NODE_DATA(i));
  1799. #endif
  1800. }
  1801. void __init mem_init(void)
  1802. {
  1803. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1804. register_page_bootmem_info();
  1805. free_all_bootmem();
  1806. /*
  1807. * Set up the zero page, mark it reserved, so that page count
  1808. * is not manipulated when freeing the page from user ptes.
  1809. */
  1810. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1811. if (mem_map_zero == NULL) {
  1812. prom_printf("paging_init: Cannot alloc zero page.\n");
  1813. prom_halt();
  1814. }
  1815. mark_page_reserved(mem_map_zero);
  1816. mem_init_print_info(NULL);
  1817. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1818. cheetah_ecache_flush_init();
  1819. }
  1820. void free_initmem(void)
  1821. {
  1822. unsigned long addr, initend;
  1823. int do_free = 1;
  1824. /* If the physical memory maps were trimmed by kernel command
  1825. * line options, don't even try freeing this initmem stuff up.
  1826. * The kernel image could have been in the trimmed out region
  1827. * and if so the freeing below will free invalid page structs.
  1828. */
  1829. if (cmdline_memory_size)
  1830. do_free = 0;
  1831. /*
  1832. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1833. */
  1834. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1835. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1836. for (; addr < initend; addr += PAGE_SIZE) {
  1837. unsigned long page;
  1838. page = (addr +
  1839. ((unsigned long) __va(kern_base)) -
  1840. ((unsigned long) KERNBASE));
  1841. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1842. if (do_free)
  1843. free_reserved_page(virt_to_page(page));
  1844. }
  1845. }
  1846. #ifdef CONFIG_BLK_DEV_INITRD
  1847. void free_initrd_mem(unsigned long start, unsigned long end)
  1848. {
  1849. free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
  1850. "initrd");
  1851. }
  1852. #endif
  1853. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1854. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1855. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1856. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1857. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1858. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1859. pgprot_t PAGE_KERNEL __read_mostly;
  1860. EXPORT_SYMBOL(PAGE_KERNEL);
  1861. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1862. pgprot_t PAGE_COPY __read_mostly;
  1863. pgprot_t PAGE_SHARED __read_mostly;
  1864. EXPORT_SYMBOL(PAGE_SHARED);
  1865. unsigned long pg_iobits __read_mostly;
  1866. unsigned long _PAGE_IE __read_mostly;
  1867. EXPORT_SYMBOL(_PAGE_IE);
  1868. unsigned long _PAGE_E __read_mostly;
  1869. EXPORT_SYMBOL(_PAGE_E);
  1870. unsigned long _PAGE_CACHE __read_mostly;
  1871. EXPORT_SYMBOL(_PAGE_CACHE);
  1872. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  1873. int __meminit vmemmap_populate(unsigned long vstart, unsigned long vend,
  1874. int node)
  1875. {
  1876. unsigned long pte_base;
  1877. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1878. _PAGE_CP_4U | _PAGE_CV_4U |
  1879. _PAGE_P_4U | _PAGE_W_4U);
  1880. if (tlb_type == hypervisor)
  1881. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1882. _PAGE_CP_4V | _PAGE_CV_4V |
  1883. _PAGE_P_4V | _PAGE_W_4V);
  1884. pte_base |= _PAGE_PMD_HUGE;
  1885. vstart = vstart & PMD_MASK;
  1886. vend = ALIGN(vend, PMD_SIZE);
  1887. for (; vstart < vend; vstart += PMD_SIZE) {
  1888. pgd_t *pgd = pgd_offset_k(vstart);
  1889. unsigned long pte;
  1890. pud_t *pud;
  1891. pmd_t *pmd;
  1892. if (pgd_none(*pgd)) {
  1893. pud_t *new = vmemmap_alloc_block(PAGE_SIZE, node);
  1894. if (!new)
  1895. return -ENOMEM;
  1896. pgd_populate(&init_mm, pgd, new);
  1897. }
  1898. pud = pud_offset(pgd, vstart);
  1899. if (pud_none(*pud)) {
  1900. pmd_t *new = vmemmap_alloc_block(PAGE_SIZE, node);
  1901. if (!new)
  1902. return -ENOMEM;
  1903. pud_populate(&init_mm, pud, new);
  1904. }
  1905. pmd = pmd_offset(pud, vstart);
  1906. pte = pmd_val(*pmd);
  1907. if (!(pte & _PAGE_VALID)) {
  1908. void *block = vmemmap_alloc_block(PMD_SIZE, node);
  1909. if (!block)
  1910. return -ENOMEM;
  1911. pmd_val(*pmd) = pte_base | __pa(block);
  1912. }
  1913. }
  1914. return 0;
  1915. }
  1916. void vmemmap_free(unsigned long start, unsigned long end)
  1917. {
  1918. }
  1919. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  1920. static void prot_init_common(unsigned long page_none,
  1921. unsigned long page_shared,
  1922. unsigned long page_copy,
  1923. unsigned long page_readonly,
  1924. unsigned long page_exec_bit)
  1925. {
  1926. PAGE_COPY = __pgprot(page_copy);
  1927. PAGE_SHARED = __pgprot(page_shared);
  1928. protection_map[0x0] = __pgprot(page_none);
  1929. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1930. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1931. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1932. protection_map[0x4] = __pgprot(page_readonly);
  1933. protection_map[0x5] = __pgprot(page_readonly);
  1934. protection_map[0x6] = __pgprot(page_copy);
  1935. protection_map[0x7] = __pgprot(page_copy);
  1936. protection_map[0x8] = __pgprot(page_none);
  1937. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1938. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1939. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1940. protection_map[0xc] = __pgprot(page_readonly);
  1941. protection_map[0xd] = __pgprot(page_readonly);
  1942. protection_map[0xe] = __pgprot(page_shared);
  1943. protection_map[0xf] = __pgprot(page_shared);
  1944. }
  1945. static void __init sun4u_pgprot_init(void)
  1946. {
  1947. unsigned long page_none, page_shared, page_copy, page_readonly;
  1948. unsigned long page_exec_bit;
  1949. int i;
  1950. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1951. _PAGE_CACHE_4U | _PAGE_P_4U |
  1952. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1953. _PAGE_EXEC_4U);
  1954. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1955. _PAGE_CACHE_4U | _PAGE_P_4U |
  1956. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1957. _PAGE_EXEC_4U | _PAGE_L_4U);
  1958. _PAGE_IE = _PAGE_IE_4U;
  1959. _PAGE_E = _PAGE_E_4U;
  1960. _PAGE_CACHE = _PAGE_CACHE_4U;
  1961. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1962. __ACCESS_BITS_4U | _PAGE_E_4U);
  1963. #ifdef CONFIG_DEBUG_PAGEALLOC
  1964. kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
  1965. #else
  1966. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1967. PAGE_OFFSET;
  1968. #endif
  1969. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1970. _PAGE_P_4U | _PAGE_W_4U);
  1971. for (i = 1; i < 4; i++)
  1972. kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
  1973. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1974. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1975. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1976. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1977. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1978. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1979. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1980. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1981. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1982. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1983. page_exec_bit = _PAGE_EXEC_4U;
  1984. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1985. page_exec_bit);
  1986. }
  1987. static void __init sun4v_pgprot_init(void)
  1988. {
  1989. unsigned long page_none, page_shared, page_copy, page_readonly;
  1990. unsigned long page_exec_bit;
  1991. int i;
  1992. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1993. _PAGE_CACHE_4V | _PAGE_P_4V |
  1994. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1995. _PAGE_EXEC_4V);
  1996. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1997. _PAGE_IE = _PAGE_IE_4V;
  1998. _PAGE_E = _PAGE_E_4V;
  1999. _PAGE_CACHE = _PAGE_CACHE_4V;
  2000. #ifdef CONFIG_DEBUG_PAGEALLOC
  2001. kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
  2002. #else
  2003. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  2004. PAGE_OFFSET;
  2005. #endif
  2006. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  2007. _PAGE_P_4V | _PAGE_W_4V);
  2008. for (i = 1; i < 4; i++)
  2009. kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
  2010. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  2011. __ACCESS_BITS_4V | _PAGE_E_4V);
  2012. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  2013. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  2014. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  2015. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  2016. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  2017. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  2018. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  2019. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  2020. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  2021. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  2022. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  2023. page_exec_bit = _PAGE_EXEC_4V;
  2024. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  2025. page_exec_bit);
  2026. }
  2027. unsigned long pte_sz_bits(unsigned long sz)
  2028. {
  2029. if (tlb_type == hypervisor) {
  2030. switch (sz) {
  2031. case 8 * 1024:
  2032. default:
  2033. return _PAGE_SZ8K_4V;
  2034. case 64 * 1024:
  2035. return _PAGE_SZ64K_4V;
  2036. case 512 * 1024:
  2037. return _PAGE_SZ512K_4V;
  2038. case 4 * 1024 * 1024:
  2039. return _PAGE_SZ4MB_4V;
  2040. }
  2041. } else {
  2042. switch (sz) {
  2043. case 8 * 1024:
  2044. default:
  2045. return _PAGE_SZ8K_4U;
  2046. case 64 * 1024:
  2047. return _PAGE_SZ64K_4U;
  2048. case 512 * 1024:
  2049. return _PAGE_SZ512K_4U;
  2050. case 4 * 1024 * 1024:
  2051. return _PAGE_SZ4MB_4U;
  2052. }
  2053. }
  2054. }
  2055. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  2056. {
  2057. pte_t pte;
  2058. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  2059. pte_val(pte) |= (((unsigned long)space) << 32);
  2060. pte_val(pte) |= pte_sz_bits(page_size);
  2061. return pte;
  2062. }
  2063. static unsigned long kern_large_tte(unsigned long paddr)
  2064. {
  2065. unsigned long val;
  2066. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  2067. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  2068. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  2069. if (tlb_type == hypervisor)
  2070. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  2071. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  2072. _PAGE_EXEC_4V | _PAGE_W_4V);
  2073. return val | paddr;
  2074. }
  2075. /* If not locked, zap it. */
  2076. void __flush_tlb_all(void)
  2077. {
  2078. unsigned long pstate;
  2079. int i;
  2080. __asm__ __volatile__("flushw\n\t"
  2081. "rdpr %%pstate, %0\n\t"
  2082. "wrpr %0, %1, %%pstate"
  2083. : "=r" (pstate)
  2084. : "i" (PSTATE_IE));
  2085. if (tlb_type == hypervisor) {
  2086. sun4v_mmu_demap_all();
  2087. } else if (tlb_type == spitfire) {
  2088. for (i = 0; i < 64; i++) {
  2089. /* Spitfire Errata #32 workaround */
  2090. /* NOTE: Always runs on spitfire, so no
  2091. * cheetah+ page size encodings.
  2092. */
  2093. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  2094. "flush %%g6"
  2095. : /* No outputs */
  2096. : "r" (0),
  2097. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  2098. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  2099. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  2100. "membar #Sync"
  2101. : /* no outputs */
  2102. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  2103. spitfire_put_dtlb_data(i, 0x0UL);
  2104. }
  2105. /* Spitfire Errata #32 workaround */
  2106. /* NOTE: Always runs on spitfire, so no
  2107. * cheetah+ page size encodings.
  2108. */
  2109. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  2110. "flush %%g6"
  2111. : /* No outputs */
  2112. : "r" (0),
  2113. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  2114. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  2115. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  2116. "membar #Sync"
  2117. : /* no outputs */
  2118. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  2119. spitfire_put_itlb_data(i, 0x0UL);
  2120. }
  2121. }
  2122. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  2123. cheetah_flush_dtlb_all();
  2124. cheetah_flush_itlb_all();
  2125. }
  2126. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  2127. : : "r" (pstate));
  2128. }
  2129. pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
  2130. unsigned long address)
  2131. {
  2132. struct page *page = alloc_page(GFP_KERNEL | __GFP_NOTRACK |
  2133. __GFP_REPEAT | __GFP_ZERO);
  2134. pte_t *pte = NULL;
  2135. if (page)
  2136. pte = (pte_t *) page_address(page);
  2137. return pte;
  2138. }
  2139. pgtable_t pte_alloc_one(struct mm_struct *mm,
  2140. unsigned long address)
  2141. {
  2142. struct page *page = alloc_page(GFP_KERNEL | __GFP_NOTRACK |
  2143. __GFP_REPEAT | __GFP_ZERO);
  2144. if (!page)
  2145. return NULL;
  2146. if (!pgtable_page_ctor(page)) {
  2147. free_hot_cold_page(page, 0);
  2148. return NULL;
  2149. }
  2150. return (pte_t *) page_address(page);
  2151. }
  2152. void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
  2153. {
  2154. free_page((unsigned long)pte);
  2155. }
  2156. static void __pte_free(pgtable_t pte)
  2157. {
  2158. struct page *page = virt_to_page(pte);
  2159. pgtable_page_dtor(page);
  2160. __free_page(page);
  2161. }
  2162. void pte_free(struct mm_struct *mm, pgtable_t pte)
  2163. {
  2164. __pte_free(pte);
  2165. }
  2166. void pgtable_free(void *table, bool is_page)
  2167. {
  2168. if (is_page)
  2169. __pte_free(table);
  2170. else
  2171. kmem_cache_free(pgtable_cache, table);
  2172. }
  2173. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2174. void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
  2175. pmd_t *pmd)
  2176. {
  2177. unsigned long pte, flags;
  2178. struct mm_struct *mm;
  2179. pmd_t entry = *pmd;
  2180. if (!pmd_large(entry) || !pmd_young(entry))
  2181. return;
  2182. pte = pmd_val(entry);
  2183. /* Don't insert a non-valid PMD into the TSB, we'll deadlock. */
  2184. if (!(pte & _PAGE_VALID))
  2185. return;
  2186. /* We are fabricating 8MB pages using 4MB real hw pages. */
  2187. pte |= (addr & (1UL << REAL_HPAGE_SHIFT));
  2188. mm = vma->vm_mm;
  2189. spin_lock_irqsave(&mm->context.lock, flags);
  2190. if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL)
  2191. __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
  2192. addr, pte);
  2193. spin_unlock_irqrestore(&mm->context.lock, flags);
  2194. }
  2195. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  2196. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  2197. static void context_reload(void *__data)
  2198. {
  2199. struct mm_struct *mm = __data;
  2200. if (mm == current->mm)
  2201. load_secondary_context(mm);
  2202. }
  2203. void hugetlb_setup(struct pt_regs *regs)
  2204. {
  2205. struct mm_struct *mm = current->mm;
  2206. struct tsb_config *tp;
  2207. if (in_atomic() || !mm) {
  2208. const struct exception_table_entry *entry;
  2209. entry = search_exception_tables(regs->tpc);
  2210. if (entry) {
  2211. regs->tpc = entry->fixup;
  2212. regs->tnpc = regs->tpc + 4;
  2213. return;
  2214. }
  2215. pr_alert("Unexpected HugeTLB setup in atomic context.\n");
  2216. die_if_kernel("HugeTSB in atomic", regs);
  2217. }
  2218. tp = &mm->context.tsb_block[MM_TSB_HUGE];
  2219. if (likely(tp->tsb == NULL))
  2220. tsb_grow(mm, MM_TSB_HUGE, 0);
  2221. tsb_context_switch(mm);
  2222. smp_tsb_sync(mm);
  2223. /* On UltraSPARC-III+ and later, configure the second half of
  2224. * the Data-TLB for huge pages.
  2225. */
  2226. if (tlb_type == cheetah_plus) {
  2227. unsigned long ctx;
  2228. spin_lock(&ctx_alloc_lock);
  2229. ctx = mm->context.sparc64_ctx_val;
  2230. ctx &= ~CTX_PGSZ_MASK;
  2231. ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
  2232. ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
  2233. if (ctx != mm->context.sparc64_ctx_val) {
  2234. /* When changing the page size fields, we
  2235. * must perform a context flush so that no
  2236. * stale entries match. This flush must
  2237. * occur with the original context register
  2238. * settings.
  2239. */
  2240. do_flush_tlb_mm(mm);
  2241. /* Reload the context register of all processors
  2242. * also executing in this address space.
  2243. */
  2244. mm->context.sparc64_ctx_val = ctx;
  2245. on_each_cpu(context_reload, mm, 0);
  2246. }
  2247. spin_unlock(&ctx_alloc_lock);
  2248. }
  2249. }
  2250. #endif
  2251. static struct resource code_resource = {
  2252. .name = "Kernel code",
  2253. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  2254. };
  2255. static struct resource data_resource = {
  2256. .name = "Kernel data",
  2257. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  2258. };
  2259. static struct resource bss_resource = {
  2260. .name = "Kernel bss",
  2261. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  2262. };
  2263. static inline resource_size_t compute_kern_paddr(void *addr)
  2264. {
  2265. return (resource_size_t) (addr - KERNBASE + kern_base);
  2266. }
  2267. static void __init kernel_lds_init(void)
  2268. {
  2269. code_resource.start = compute_kern_paddr(_text);
  2270. code_resource.end = compute_kern_paddr(_etext - 1);
  2271. data_resource.start = compute_kern_paddr(_etext);
  2272. data_resource.end = compute_kern_paddr(_edata - 1);
  2273. bss_resource.start = compute_kern_paddr(__bss_start);
  2274. bss_resource.end = compute_kern_paddr(_end - 1);
  2275. }
  2276. static int __init report_memory(void)
  2277. {
  2278. int i;
  2279. struct resource *res;
  2280. kernel_lds_init();
  2281. for (i = 0; i < pavail_ents; i++) {
  2282. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  2283. if (!res) {
  2284. pr_warn("Failed to allocate source.\n");
  2285. break;
  2286. }
  2287. res->name = "System RAM";
  2288. res->start = pavail[i].phys_addr;
  2289. res->end = pavail[i].phys_addr + pavail[i].reg_size - 1;
  2290. res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  2291. if (insert_resource(&iomem_resource, res) < 0) {
  2292. pr_warn("Resource insertion failed.\n");
  2293. break;
  2294. }
  2295. insert_resource(res, &code_resource);
  2296. insert_resource(res, &data_resource);
  2297. insert_resource(res, &bss_resource);
  2298. }
  2299. return 0;
  2300. }
  2301. device_initcall(report_memory);
  2302. #ifdef CONFIG_SMP
  2303. #define do_flush_tlb_kernel_range smp_flush_tlb_kernel_range
  2304. #else
  2305. #define do_flush_tlb_kernel_range __flush_tlb_kernel_range
  2306. #endif
  2307. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  2308. {
  2309. if (start < HI_OBP_ADDRESS && end > LOW_OBP_ADDRESS) {
  2310. if (start < LOW_OBP_ADDRESS) {
  2311. flush_tsb_kernel_range(start, LOW_OBP_ADDRESS);
  2312. do_flush_tlb_kernel_range(start, LOW_OBP_ADDRESS);
  2313. }
  2314. if (end > HI_OBP_ADDRESS) {
  2315. flush_tsb_kernel_range(HI_OBP_ADDRESS, end);
  2316. do_flush_tlb_kernel_range(HI_OBP_ADDRESS, end);
  2317. }
  2318. } else {
  2319. flush_tsb_kernel_range(start, end);
  2320. do_flush_tlb_kernel_range(start, end);
  2321. }
  2322. }