init_64.c 76 KB

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