init_64.c 58 KB

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