init_64.c 71 KB

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