init_64.c 73 KB

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