init_64.c 67 KB

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