init.c 58 KB

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