init_64.c 77 KB

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