init_64.c 57 KB

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