init_64.c 71 KB

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