init_64.c 74 KB

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