init_64.c 73 KB

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