init_64.c 69 KB

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