init_64.c 77 KB

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