init_64.c 71 KB

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