init.c 58 KB

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