init.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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 <asm/head.h>
  27. #include <asm/system.h>
  28. #include <asm/page.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/oplib.h>
  32. #include <asm/iommu.h>
  33. #include <asm/io.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/dma.h>
  38. #include <asm/starfire.h>
  39. #include <asm/tlb.h>
  40. #include <asm/spitfire.h>
  41. #include <asm/sections.h>
  42. #include <asm/tsb.h>
  43. #include <asm/hypervisor.h>
  44. #include <asm/prom.h>
  45. #include <asm/sstate.h>
  46. #include <asm/mdesc.h>
  47. #define MAX_PHYS_ADDRESS (1UL << 42UL)
  48. #define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
  49. #define KPTE_BITMAP_BYTES \
  50. ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
  51. unsigned long kern_linear_pte_xor[2] __read_mostly;
  52. /* A bitmap, one bit for every 256MB of physical memory. If the bit
  53. * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
  54. * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
  55. */
  56. unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
  57. #ifndef CONFIG_DEBUG_PAGEALLOC
  58. /* A special kernel TSB for 4MB and 256MB linear mappings.
  59. * Space is allocated for this right after the trap table
  60. * in arch/sparc64/kernel/head.S
  61. */
  62. extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
  63. #endif
  64. #define MAX_BANKS 32
  65. static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
  66. static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
  67. static int pavail_ents __initdata;
  68. static int pavail_rescan_ents __initdata;
  69. static int cmp_p64(const void *a, const void *b)
  70. {
  71. const struct linux_prom64_registers *x = a, *y = b;
  72. if (x->phys_addr > y->phys_addr)
  73. return 1;
  74. if (x->phys_addr < y->phys_addr)
  75. return -1;
  76. return 0;
  77. }
  78. static void __init read_obp_memory(const char *property,
  79. struct linux_prom64_registers *regs,
  80. int *num_ents)
  81. {
  82. int node = prom_finddevice("/memory");
  83. int prop_size = prom_getproplen(node, property);
  84. int ents, ret, i;
  85. ents = prop_size / sizeof(struct linux_prom64_registers);
  86. if (ents > MAX_BANKS) {
  87. prom_printf("The machine has more %s property entries than "
  88. "this kernel can support (%d).\n",
  89. property, MAX_BANKS);
  90. prom_halt();
  91. }
  92. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  93. if (ret == -1) {
  94. prom_printf("Couldn't get %s property from /memory.\n");
  95. prom_halt();
  96. }
  97. /* Sanitize what we got from the firmware, by page aligning
  98. * everything.
  99. */
  100. for (i = 0; i < ents; i++) {
  101. unsigned long base, size;
  102. base = regs[i].phys_addr;
  103. size = regs[i].reg_size;
  104. size &= PAGE_MASK;
  105. if (base & ~PAGE_MASK) {
  106. unsigned long new_base = PAGE_ALIGN(base);
  107. size -= new_base - base;
  108. if ((long) size < 0L)
  109. size = 0UL;
  110. base = new_base;
  111. }
  112. if (size == 0UL) {
  113. /* If it is empty, simply get rid of it.
  114. * This simplifies the logic of the other
  115. * functions that process these arrays.
  116. */
  117. memmove(&regs[i], &regs[i + 1],
  118. (ents - i - 1) * sizeof(regs[0]));
  119. i--;
  120. ents--;
  121. continue;
  122. }
  123. regs[i].phys_addr = base;
  124. regs[i].reg_size = size;
  125. }
  126. *num_ents = ents;
  127. sort(regs, ents, sizeof(struct linux_prom64_registers),
  128. cmp_p64, NULL);
  129. }
  130. unsigned long *sparc64_valid_addr_bitmap __read_mostly;
  131. /* Kernel physical address base and size in bytes. */
  132. unsigned long kern_base __read_mostly;
  133. unsigned long kern_size __read_mostly;
  134. /* Initial ramdisk setup */
  135. extern unsigned long sparc_ramdisk_image64;
  136. extern unsigned int sparc_ramdisk_image;
  137. extern unsigned int sparc_ramdisk_size;
  138. struct page *mem_map_zero __read_mostly;
  139. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  140. unsigned long sparc64_kern_pri_context __read_mostly;
  141. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  142. unsigned long sparc64_kern_sec_context __read_mostly;
  143. int bigkernel = 0;
  144. #ifdef CONFIG_DEBUG_DCFLUSH
  145. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  146. #ifdef CONFIG_SMP
  147. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  148. #endif
  149. #endif
  150. inline void flush_dcache_page_impl(struct page *page)
  151. {
  152. BUG_ON(tlb_type == hypervisor);
  153. #ifdef CONFIG_DEBUG_DCFLUSH
  154. atomic_inc(&dcpage_flushes);
  155. #endif
  156. #ifdef DCACHE_ALIASING_POSSIBLE
  157. __flush_dcache_page(page_address(page),
  158. ((tlb_type == spitfire) &&
  159. page_mapping(page) != NULL));
  160. #else
  161. if (page_mapping(page) != NULL &&
  162. tlb_type == spitfire)
  163. __flush_icache_page(__pa(page_address(page)));
  164. #endif
  165. }
  166. #define PG_dcache_dirty PG_arch_1
  167. #define PG_dcache_cpu_shift 32UL
  168. #define PG_dcache_cpu_mask \
  169. ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
  170. #define dcache_dirty_cpu(page) \
  171. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  172. static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
  173. {
  174. unsigned long mask = this_cpu;
  175. unsigned long non_cpu_bits;
  176. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  177. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  178. __asm__ __volatile__("1:\n\t"
  179. "ldx [%2], %%g7\n\t"
  180. "and %%g7, %1, %%g1\n\t"
  181. "or %%g1, %0, %%g1\n\t"
  182. "casx [%2], %%g7, %%g1\n\t"
  183. "cmp %%g7, %%g1\n\t"
  184. "membar #StoreLoad | #StoreStore\n\t"
  185. "bne,pn %%xcc, 1b\n\t"
  186. " nop"
  187. : /* no outputs */
  188. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  189. : "g1", "g7");
  190. }
  191. static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  192. {
  193. unsigned long mask = (1UL << PG_dcache_dirty);
  194. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  195. "1:\n\t"
  196. "ldx [%2], %%g7\n\t"
  197. "srlx %%g7, %4, %%g1\n\t"
  198. "and %%g1, %3, %%g1\n\t"
  199. "cmp %%g1, %0\n\t"
  200. "bne,pn %%icc, 2f\n\t"
  201. " andn %%g7, %1, %%g1\n\t"
  202. "casx [%2], %%g7, %%g1\n\t"
  203. "cmp %%g7, %%g1\n\t"
  204. "membar #StoreLoad | #StoreStore\n\t"
  205. "bne,pn %%xcc, 1b\n\t"
  206. " nop\n"
  207. "2:"
  208. : /* no outputs */
  209. : "r" (cpu), "r" (mask), "r" (&page->flags),
  210. "i" (PG_dcache_cpu_mask),
  211. "i" (PG_dcache_cpu_shift)
  212. : "g1", "g7");
  213. }
  214. static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
  215. {
  216. unsigned long tsb_addr = (unsigned long) ent;
  217. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  218. tsb_addr = __pa(tsb_addr);
  219. __tsb_insert(tsb_addr, tag, pte);
  220. }
  221. unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
  222. unsigned long _PAGE_SZBITS __read_mostly;
  223. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  224. {
  225. struct mm_struct *mm;
  226. struct tsb *tsb;
  227. unsigned long tag, flags;
  228. unsigned long tsb_index, tsb_hash_shift;
  229. if (tlb_type != hypervisor) {
  230. unsigned long pfn = pte_pfn(pte);
  231. unsigned long pg_flags;
  232. struct page *page;
  233. if (pfn_valid(pfn) &&
  234. (page = pfn_to_page(pfn), page_mapping(page)) &&
  235. ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
  236. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  237. PG_dcache_cpu_mask);
  238. int this_cpu = get_cpu();
  239. /* This is just to optimize away some function calls
  240. * in the SMP case.
  241. */
  242. if (cpu == this_cpu)
  243. flush_dcache_page_impl(page);
  244. else
  245. smp_flush_dcache_page_impl(page, cpu);
  246. clear_dcache_dirty_cpu(page, cpu);
  247. put_cpu();
  248. }
  249. }
  250. mm = vma->vm_mm;
  251. tsb_index = MM_TSB_BASE;
  252. tsb_hash_shift = PAGE_SHIFT;
  253. spin_lock_irqsave(&mm->context.lock, flags);
  254. #ifdef CONFIG_HUGETLB_PAGE
  255. if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
  256. if ((tlb_type == hypervisor &&
  257. (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
  258. (tlb_type != hypervisor &&
  259. (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
  260. tsb_index = MM_TSB_HUGE;
  261. tsb_hash_shift = HPAGE_SHIFT;
  262. }
  263. }
  264. #endif
  265. tsb = mm->context.tsb_block[tsb_index].tsb;
  266. tsb += ((address >> tsb_hash_shift) &
  267. (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
  268. tag = (address >> 22UL);
  269. tsb_insert(tsb, tag, pte_val(pte));
  270. spin_unlock_irqrestore(&mm->context.lock, flags);
  271. }
  272. void flush_dcache_page(struct page *page)
  273. {
  274. struct address_space *mapping;
  275. int this_cpu;
  276. if (tlb_type == hypervisor)
  277. return;
  278. /* Do not bother with the expensive D-cache flush if it
  279. * is merely the zero page. The 'bigcore' testcase in GDB
  280. * causes this case to run millions of times.
  281. */
  282. if (page == ZERO_PAGE(0))
  283. return;
  284. this_cpu = get_cpu();
  285. mapping = page_mapping(page);
  286. if (mapping && !mapping_mapped(mapping)) {
  287. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  288. if (dirty) {
  289. int dirty_cpu = dcache_dirty_cpu(page);
  290. if (dirty_cpu == this_cpu)
  291. goto out;
  292. smp_flush_dcache_page_impl(page, dirty_cpu);
  293. }
  294. set_dcache_dirty(page, this_cpu);
  295. } else {
  296. /* We could delay the flush for the !page_mapping
  297. * case too. But that case is for exec env/arg
  298. * pages and those are %99 certainly going to get
  299. * faulted into the tlb (and thus flushed) anyways.
  300. */
  301. flush_dcache_page_impl(page);
  302. }
  303. out:
  304. put_cpu();
  305. }
  306. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  307. {
  308. /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
  309. if (tlb_type == spitfire) {
  310. unsigned long kaddr;
  311. /* This code only runs on Spitfire cpus so this is
  312. * why we can assume _PAGE_PADDR_4U.
  313. */
  314. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
  315. unsigned long paddr, mask = _PAGE_PADDR_4U;
  316. if (kaddr >= PAGE_OFFSET)
  317. paddr = kaddr & mask;
  318. else {
  319. pgd_t *pgdp = pgd_offset_k(kaddr);
  320. pud_t *pudp = pud_offset(pgdp, kaddr);
  321. pmd_t *pmdp = pmd_offset(pudp, kaddr);
  322. pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
  323. paddr = pte_val(*ptep) & mask;
  324. }
  325. __flush_icache_page(paddr);
  326. }
  327. }
  328. }
  329. void show_mem(void)
  330. {
  331. unsigned long total = 0, reserved = 0;
  332. unsigned long shared = 0, cached = 0;
  333. pg_data_t *pgdat;
  334. printk(KERN_INFO "Mem-info:\n");
  335. show_free_areas();
  336. printk(KERN_INFO "Free swap: %6ldkB\n",
  337. nr_swap_pages << (PAGE_SHIFT-10));
  338. for_each_online_pgdat(pgdat) {
  339. unsigned long i, flags;
  340. pgdat_resize_lock(pgdat, &flags);
  341. for (i = 0; i < pgdat->node_spanned_pages; i++) {
  342. struct page *page = pgdat_page_nr(pgdat, i);
  343. total++;
  344. if (PageReserved(page))
  345. reserved++;
  346. else if (PageSwapCache(page))
  347. cached++;
  348. else if (page_count(page))
  349. shared += page_count(page) - 1;
  350. }
  351. pgdat_resize_unlock(pgdat, &flags);
  352. }
  353. printk(KERN_INFO "%lu pages of RAM\n", total);
  354. printk(KERN_INFO "%lu reserved pages\n", reserved);
  355. printk(KERN_INFO "%lu pages shared\n", shared);
  356. printk(KERN_INFO "%lu pages swap cached\n", cached);
  357. printk(KERN_INFO "%lu pages dirty\n",
  358. global_page_state(NR_FILE_DIRTY));
  359. printk(KERN_INFO "%lu pages writeback\n",
  360. global_page_state(NR_WRITEBACK));
  361. printk(KERN_INFO "%lu pages mapped\n",
  362. global_page_state(NR_FILE_MAPPED));
  363. printk(KERN_INFO "%lu pages slab\n",
  364. global_page_state(NR_SLAB_RECLAIMABLE) +
  365. global_page_state(NR_SLAB_UNRECLAIMABLE));
  366. printk(KERN_INFO "%lu pages pagetables\n",
  367. global_page_state(NR_PAGETABLE));
  368. }
  369. void mmu_info(struct seq_file *m)
  370. {
  371. if (tlb_type == cheetah)
  372. seq_printf(m, "MMU Type\t: Cheetah\n");
  373. else if (tlb_type == cheetah_plus)
  374. seq_printf(m, "MMU Type\t: Cheetah+\n");
  375. else if (tlb_type == spitfire)
  376. seq_printf(m, "MMU Type\t: Spitfire\n");
  377. else if (tlb_type == hypervisor)
  378. seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
  379. else
  380. seq_printf(m, "MMU Type\t: ???\n");
  381. #ifdef CONFIG_DEBUG_DCFLUSH
  382. seq_printf(m, "DCPageFlushes\t: %d\n",
  383. atomic_read(&dcpage_flushes));
  384. #ifdef CONFIG_SMP
  385. seq_printf(m, "DCPageFlushesXC\t: %d\n",
  386. atomic_read(&dcpage_flushes_xcall));
  387. #endif /* CONFIG_SMP */
  388. #endif /* CONFIG_DEBUG_DCFLUSH */
  389. }
  390. struct linux_prom_translation {
  391. unsigned long virt;
  392. unsigned long size;
  393. unsigned long data;
  394. };
  395. /* Exported for kernel TLB miss handling in ktlb.S */
  396. struct linux_prom_translation prom_trans[512] __read_mostly;
  397. unsigned int prom_trans_ents __read_mostly;
  398. /* Exported for SMP bootup purposes. */
  399. unsigned long kern_locked_tte_data;
  400. /* The obp translations are saved based on 8k pagesize, since obp can
  401. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  402. * HI_OBP_ADDRESS range are handled in ktlb.S.
  403. */
  404. static inline int in_obp_range(unsigned long vaddr)
  405. {
  406. return (vaddr >= LOW_OBP_ADDRESS &&
  407. vaddr < HI_OBP_ADDRESS);
  408. }
  409. static int cmp_ptrans(const void *a, const void *b)
  410. {
  411. const struct linux_prom_translation *x = a, *y = b;
  412. if (x->virt > y->virt)
  413. return 1;
  414. if (x->virt < y->virt)
  415. return -1;
  416. return 0;
  417. }
  418. /* Read OBP translations property into 'prom_trans[]'. */
  419. static void __init read_obp_translations(void)
  420. {
  421. int n, node, ents, first, last, i;
  422. node = prom_finddevice("/virtual-memory");
  423. n = prom_getproplen(node, "translations");
  424. if (unlikely(n == 0 || n == -1)) {
  425. prom_printf("prom_mappings: Couldn't get size.\n");
  426. prom_halt();
  427. }
  428. if (unlikely(n > sizeof(prom_trans))) {
  429. prom_printf("prom_mappings: Size %Zd is too big.\n", n);
  430. prom_halt();
  431. }
  432. if ((n = prom_getproperty(node, "translations",
  433. (char *)&prom_trans[0],
  434. sizeof(prom_trans))) == -1) {
  435. prom_printf("prom_mappings: Couldn't get property.\n");
  436. prom_halt();
  437. }
  438. n = n / sizeof(struct linux_prom_translation);
  439. ents = n;
  440. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  441. cmp_ptrans, NULL);
  442. /* Now kick out all the non-OBP entries. */
  443. for (i = 0; i < ents; i++) {
  444. if (in_obp_range(prom_trans[i].virt))
  445. break;
  446. }
  447. first = i;
  448. for (; i < ents; i++) {
  449. if (!in_obp_range(prom_trans[i].virt))
  450. break;
  451. }
  452. last = i;
  453. for (i = 0; i < (last - first); i++) {
  454. struct linux_prom_translation *src = &prom_trans[i + first];
  455. struct linux_prom_translation *dest = &prom_trans[i];
  456. *dest = *src;
  457. }
  458. for (; i < ents; i++) {
  459. struct linux_prom_translation *dest = &prom_trans[i];
  460. dest->virt = dest->size = dest->data = 0x0UL;
  461. }
  462. prom_trans_ents = last - first;
  463. if (tlb_type == spitfire) {
  464. /* Clear diag TTE bits. */
  465. for (i = 0; i < prom_trans_ents; i++)
  466. prom_trans[i].data &= ~0x0003fe0000000000UL;
  467. }
  468. }
  469. static void __init hypervisor_tlb_lock(unsigned long vaddr,
  470. unsigned long pte,
  471. unsigned long mmu)
  472. {
  473. register unsigned long func asm("%o5");
  474. register unsigned long arg0 asm("%o0");
  475. register unsigned long arg1 asm("%o1");
  476. register unsigned long arg2 asm("%o2");
  477. register unsigned long arg3 asm("%o3");
  478. func = HV_FAST_MMU_MAP_PERM_ADDR;
  479. arg0 = vaddr;
  480. arg1 = 0;
  481. arg2 = pte;
  482. arg3 = mmu;
  483. __asm__ __volatile__("ta 0x80"
  484. : "=&r" (func), "=&r" (arg0),
  485. "=&r" (arg1), "=&r" (arg2),
  486. "=&r" (arg3)
  487. : "0" (func), "1" (arg0), "2" (arg1),
  488. "3" (arg2), "4" (arg3));
  489. if (arg0 != 0) {
  490. prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
  491. "errors with %lx\n", vaddr, 0, pte, mmu, arg0);
  492. prom_halt();
  493. }
  494. }
  495. static unsigned long kern_large_tte(unsigned long paddr);
  496. static void __init remap_kernel(void)
  497. {
  498. unsigned long phys_page, tte_vaddr, tte_data;
  499. int tlb_ent = sparc64_highest_locked_tlbent();
  500. tte_vaddr = (unsigned long) KERNBASE;
  501. phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  502. tte_data = kern_large_tte(phys_page);
  503. kern_locked_tte_data = tte_data;
  504. /* Now lock us into the TLBs via Hypervisor or OBP. */
  505. if (tlb_type == hypervisor) {
  506. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  507. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  508. if (bigkernel) {
  509. tte_vaddr += 0x400000;
  510. tte_data += 0x400000;
  511. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  512. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  513. }
  514. } else {
  515. prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
  516. prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
  517. if (bigkernel) {
  518. tlb_ent -= 1;
  519. prom_dtlb_load(tlb_ent,
  520. tte_data + 0x400000,
  521. tte_vaddr + 0x400000);
  522. prom_itlb_load(tlb_ent,
  523. tte_data + 0x400000,
  524. tte_vaddr + 0x400000);
  525. }
  526. sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
  527. }
  528. if (tlb_type == cheetah_plus) {
  529. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  530. CTX_CHEETAH_PLUS_NUC);
  531. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  532. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  533. }
  534. }
  535. static void __init inherit_prom_mappings(void)
  536. {
  537. read_obp_translations();
  538. /* Now fixup OBP's idea about where we really are mapped. */
  539. prom_printf("Remapping the kernel... ");
  540. remap_kernel();
  541. prom_printf("done.\n");
  542. }
  543. void prom_world(int enter)
  544. {
  545. if (!enter)
  546. set_fs((mm_segment_t) { get_thread_current_ds() });
  547. __asm__ __volatile__("flushw");
  548. }
  549. #ifdef DCACHE_ALIASING_POSSIBLE
  550. void __flush_dcache_range(unsigned long start, unsigned long end)
  551. {
  552. unsigned long va;
  553. if (tlb_type == spitfire) {
  554. int n = 0;
  555. for (va = start; va < end; va += 32) {
  556. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  557. if (++n >= 512)
  558. break;
  559. }
  560. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  561. start = __pa(start);
  562. end = __pa(end);
  563. for (va = start; va < end; va += 32)
  564. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  565. "membar #Sync"
  566. : /* no outputs */
  567. : "r" (va),
  568. "i" (ASI_DCACHE_INVALIDATE));
  569. }
  570. }
  571. #endif /* DCACHE_ALIASING_POSSIBLE */
  572. /* get_new_mmu_context() uses "cache + 1". */
  573. DEFINE_SPINLOCK(ctx_alloc_lock);
  574. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  575. #define MAX_CTX_NR (1UL << CTX_NR_BITS)
  576. #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
  577. DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
  578. /* Caller does TLB context flushing on local CPU if necessary.
  579. * The caller also ensures that CTX_VALID(mm->context) is false.
  580. *
  581. * We must be careful about boundary cases so that we never
  582. * let the user have CTX 0 (nucleus) or we ever use a CTX
  583. * version of zero (and thus NO_CONTEXT would not be caught
  584. * by version mis-match tests in mmu_context.h).
  585. *
  586. * Always invoked with interrupts disabled.
  587. */
  588. void get_new_mmu_context(struct mm_struct *mm)
  589. {
  590. unsigned long ctx, new_ctx;
  591. unsigned long orig_pgsz_bits;
  592. unsigned long flags;
  593. int new_version;
  594. spin_lock_irqsave(&ctx_alloc_lock, flags);
  595. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  596. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  597. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  598. new_version = 0;
  599. if (new_ctx >= (1 << CTX_NR_BITS)) {
  600. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  601. if (new_ctx >= ctx) {
  602. int i;
  603. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  604. CTX_FIRST_VERSION;
  605. if (new_ctx == 1)
  606. new_ctx = CTX_FIRST_VERSION;
  607. /* Don't call memset, for 16 entries that's just
  608. * plain silly...
  609. */
  610. mmu_context_bmap[0] = 3;
  611. mmu_context_bmap[1] = 0;
  612. mmu_context_bmap[2] = 0;
  613. mmu_context_bmap[3] = 0;
  614. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  615. mmu_context_bmap[i + 0] = 0;
  616. mmu_context_bmap[i + 1] = 0;
  617. mmu_context_bmap[i + 2] = 0;
  618. mmu_context_bmap[i + 3] = 0;
  619. }
  620. new_version = 1;
  621. goto out;
  622. }
  623. }
  624. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  625. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  626. out:
  627. tlb_context_cache = new_ctx;
  628. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  629. spin_unlock_irqrestore(&ctx_alloc_lock, flags);
  630. if (unlikely(new_version))
  631. smp_new_mmu_context_version();
  632. }
  633. /* Find a free area for the bootmem map, avoiding the kernel image
  634. * and the initial ramdisk.
  635. */
  636. static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
  637. unsigned long end_pfn)
  638. {
  639. unsigned long avoid_start, avoid_end, bootmap_size;
  640. int i;
  641. bootmap_size = bootmem_bootmap_pages(end_pfn - start_pfn);
  642. bootmap_size <<= PAGE_SHIFT;
  643. avoid_start = avoid_end = 0;
  644. #ifdef CONFIG_BLK_DEV_INITRD
  645. avoid_start = initrd_start;
  646. avoid_end = PAGE_ALIGN(initrd_end);
  647. #endif
  648. #ifdef CONFIG_DEBUG_BOOTMEM
  649. prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
  650. kern_base, PAGE_ALIGN(kern_base + kern_size),
  651. avoid_start, avoid_end);
  652. #endif
  653. for (i = 0; i < pavail_ents; i++) {
  654. unsigned long start, end;
  655. start = pavail[i].phys_addr;
  656. end = start + pavail[i].reg_size;
  657. while (start < end) {
  658. if (start >= kern_base &&
  659. start < PAGE_ALIGN(kern_base + kern_size)) {
  660. start = PAGE_ALIGN(kern_base + kern_size);
  661. continue;
  662. }
  663. if (start >= avoid_start && start < avoid_end) {
  664. start = avoid_end;
  665. continue;
  666. }
  667. if ((end - start) < bootmap_size)
  668. break;
  669. if (start < kern_base &&
  670. (start + bootmap_size) > kern_base) {
  671. start = PAGE_ALIGN(kern_base + kern_size);
  672. continue;
  673. }
  674. if (start < avoid_start &&
  675. (start + bootmap_size) > avoid_start) {
  676. start = avoid_end;
  677. continue;
  678. }
  679. /* OK, it doesn't overlap anything, use it. */
  680. #ifdef CONFIG_DEBUG_BOOTMEM
  681. prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
  682. start >> PAGE_SHIFT, start);
  683. #endif
  684. return start >> PAGE_SHIFT;
  685. }
  686. }
  687. prom_printf("Cannot find free area for bootmap, aborting.\n");
  688. prom_halt();
  689. }
  690. static void __init trim_pavail(unsigned long *cur_size_p,
  691. unsigned long *end_of_phys_p)
  692. {
  693. unsigned long to_trim = *cur_size_p - cmdline_memory_size;
  694. unsigned long avoid_start, avoid_end;
  695. int i;
  696. to_trim = PAGE_ALIGN(to_trim);
  697. avoid_start = avoid_end = 0;
  698. #ifdef CONFIG_BLK_DEV_INITRD
  699. avoid_start = initrd_start;
  700. avoid_end = PAGE_ALIGN(initrd_end);
  701. #endif
  702. /* Trim some pavail[] entries in order to satisfy the
  703. * requested "mem=xxx" kernel command line specification.
  704. *
  705. * We must not trim off the kernel image area nor the
  706. * initial ramdisk range (if any). Also, we must not trim
  707. * any pavail[] entry down to zero in order to preserve
  708. * the invariant that all pavail[] entries have a non-zero
  709. * size which is assumed by all of the code in here.
  710. */
  711. for (i = 0; i < pavail_ents; i++) {
  712. unsigned long start, end, kern_end;
  713. unsigned long trim_low, trim_high, n;
  714. kern_end = PAGE_ALIGN(kern_base + kern_size);
  715. trim_low = start = pavail[i].phys_addr;
  716. trim_high = end = start + pavail[i].reg_size;
  717. if (kern_base >= start &&
  718. kern_base < end) {
  719. trim_low = kern_base;
  720. if (kern_end >= end)
  721. continue;
  722. }
  723. if (kern_end >= start &&
  724. kern_end < end) {
  725. trim_high = kern_end;
  726. }
  727. if (avoid_start &&
  728. avoid_start >= start &&
  729. avoid_start < end) {
  730. if (trim_low > avoid_start)
  731. trim_low = avoid_start;
  732. if (avoid_end >= end)
  733. continue;
  734. }
  735. if (avoid_end &&
  736. avoid_end >= start &&
  737. avoid_end < end) {
  738. if (trim_high < avoid_end)
  739. trim_high = avoid_end;
  740. }
  741. if (trim_high <= trim_low)
  742. continue;
  743. if (trim_low == start && trim_high == end) {
  744. /* Whole chunk is available for trimming.
  745. * Trim all except one page, in order to keep
  746. * entry non-empty.
  747. */
  748. n = (end - start) - PAGE_SIZE;
  749. if (n > to_trim)
  750. n = to_trim;
  751. if (n) {
  752. pavail[i].phys_addr += n;
  753. pavail[i].reg_size -= n;
  754. to_trim -= n;
  755. }
  756. } else {
  757. n = (trim_low - start);
  758. if (n > to_trim)
  759. n = to_trim;
  760. if (n) {
  761. pavail[i].phys_addr += n;
  762. pavail[i].reg_size -= n;
  763. to_trim -= n;
  764. }
  765. if (to_trim) {
  766. n = end - trim_high;
  767. if (n > to_trim)
  768. n = to_trim;
  769. if (n) {
  770. pavail[i].reg_size -= n;
  771. to_trim -= n;
  772. }
  773. }
  774. }
  775. if (!to_trim)
  776. break;
  777. }
  778. /* Recalculate. */
  779. *cur_size_p = 0UL;
  780. for (i = 0; i < pavail_ents; i++) {
  781. *end_of_phys_p = pavail[i].phys_addr +
  782. pavail[i].reg_size;
  783. *cur_size_p += pavail[i].reg_size;
  784. }
  785. }
  786. /* About pages_avail, this is the value we will use to calculate
  787. * the zholes_size[] argument given to free_area_init_node(). The
  788. * page allocator uses this to calculate nr_kernel_pages,
  789. * nr_all_pages and zone->present_pages. On NUMA it is used
  790. * to calculate zone->min_unmapped_pages and zone->min_slab_pages.
  791. *
  792. * So this number should really be set to what the page allocator
  793. * actually ends up with. This means:
  794. * 1) It should include bootmem map pages, we'll release those.
  795. * 2) It should not include the kernel image, except for the
  796. * __init sections which we will also release.
  797. * 3) It should include the initrd image, since we'll release
  798. * that too.
  799. */
  800. static unsigned long __init bootmem_init(unsigned long *pages_avail,
  801. unsigned long phys_base)
  802. {
  803. unsigned long bootmap_size, end_pfn;
  804. unsigned long end_of_phys_memory = 0UL;
  805. unsigned long bootmap_pfn, bytes_avail, size;
  806. int i;
  807. #ifdef CONFIG_DEBUG_BOOTMEM
  808. prom_printf("bootmem_init: Scan pavail, ");
  809. #endif
  810. bytes_avail = 0UL;
  811. for (i = 0; i < pavail_ents; i++) {
  812. end_of_phys_memory = pavail[i].phys_addr +
  813. pavail[i].reg_size;
  814. bytes_avail += pavail[i].reg_size;
  815. }
  816. /* Determine the location of the initial ramdisk before trying
  817. * to honor the "mem=xxx" command line argument. We must know
  818. * where the kernel image and the ramdisk image are so that we
  819. * do not trim those two areas from the physical memory map.
  820. */
  821. #ifdef CONFIG_BLK_DEV_INITRD
  822. /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
  823. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  824. unsigned long ramdisk_image = sparc_ramdisk_image ?
  825. sparc_ramdisk_image : sparc_ramdisk_image64;
  826. ramdisk_image -= KERNBASE;
  827. initrd_start = ramdisk_image + phys_base;
  828. initrd_end = initrd_start + sparc_ramdisk_size;
  829. if (initrd_end > end_of_phys_memory) {
  830. printk(KERN_CRIT "initrd extends beyond end of memory "
  831. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  832. initrd_end, end_of_phys_memory);
  833. initrd_start = 0;
  834. initrd_end = 0;
  835. }
  836. }
  837. #endif
  838. if (cmdline_memory_size &&
  839. bytes_avail > cmdline_memory_size)
  840. trim_pavail(&bytes_avail,
  841. &end_of_phys_memory);
  842. *pages_avail = bytes_avail >> PAGE_SHIFT;
  843. end_pfn = end_of_phys_memory >> PAGE_SHIFT;
  844. /* Initialize the boot-time allocator. */
  845. max_pfn = max_low_pfn = end_pfn;
  846. min_low_pfn = (phys_base >> PAGE_SHIFT);
  847. bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
  848. #ifdef CONFIG_DEBUG_BOOTMEM
  849. prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
  850. min_low_pfn, bootmap_pfn, max_low_pfn);
  851. #endif
  852. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
  853. min_low_pfn, end_pfn);
  854. /* Now register the available physical memory with the
  855. * allocator.
  856. */
  857. for (i = 0; i < pavail_ents; i++) {
  858. #ifdef CONFIG_DEBUG_BOOTMEM
  859. prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
  860. i, pavail[i].phys_addr, pavail[i].reg_size);
  861. #endif
  862. free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
  863. }
  864. #ifdef CONFIG_BLK_DEV_INITRD
  865. if (initrd_start) {
  866. size = initrd_end - initrd_start;
  867. /* Reserve the initrd image area. */
  868. #ifdef CONFIG_DEBUG_BOOTMEM
  869. prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
  870. initrd_start, initrd_end);
  871. #endif
  872. reserve_bootmem(initrd_start, size);
  873. initrd_start += PAGE_OFFSET;
  874. initrd_end += PAGE_OFFSET;
  875. }
  876. #endif
  877. /* Reserve the kernel text/data/bss. */
  878. #ifdef CONFIG_DEBUG_BOOTMEM
  879. prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
  880. #endif
  881. reserve_bootmem(kern_base, kern_size);
  882. *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
  883. /* Add back in the initmem pages. */
  884. size = ((unsigned long)(__init_end) & PAGE_MASK) -
  885. PAGE_ALIGN((unsigned long)__init_begin);
  886. *pages_avail += size >> PAGE_SHIFT;
  887. /* Reserve the bootmem map. We do not account for it
  888. * in pages_avail because we will release that memory
  889. * in free_all_bootmem.
  890. */
  891. size = bootmap_size;
  892. #ifdef CONFIG_DEBUG_BOOTMEM
  893. prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
  894. (bootmap_pfn << PAGE_SHIFT), size);
  895. #endif
  896. reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
  897. for (i = 0; i < pavail_ents; i++) {
  898. unsigned long start_pfn, end_pfn;
  899. start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
  900. end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
  901. #ifdef CONFIG_DEBUG_BOOTMEM
  902. prom_printf("memory_present(0, %lx, %lx)\n",
  903. start_pfn, end_pfn);
  904. #endif
  905. memory_present(0, start_pfn, end_pfn);
  906. }
  907. sparse_init();
  908. return end_pfn;
  909. }
  910. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  911. static int pall_ents __initdata;
  912. #ifdef CONFIG_DEBUG_PAGEALLOC
  913. static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
  914. {
  915. unsigned long vstart = PAGE_OFFSET + pstart;
  916. unsigned long vend = PAGE_OFFSET + pend;
  917. unsigned long alloc_bytes = 0UL;
  918. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  919. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  920. vstart, vend);
  921. prom_halt();
  922. }
  923. while (vstart < vend) {
  924. unsigned long this_end, paddr = __pa(vstart);
  925. pgd_t *pgd = pgd_offset_k(vstart);
  926. pud_t *pud;
  927. pmd_t *pmd;
  928. pte_t *pte;
  929. pud = pud_offset(pgd, vstart);
  930. if (pud_none(*pud)) {
  931. pmd_t *new;
  932. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  933. alloc_bytes += PAGE_SIZE;
  934. pud_populate(&init_mm, pud, new);
  935. }
  936. pmd = pmd_offset(pud, vstart);
  937. if (!pmd_present(*pmd)) {
  938. pte_t *new;
  939. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  940. alloc_bytes += PAGE_SIZE;
  941. pmd_populate_kernel(&init_mm, pmd, new);
  942. }
  943. pte = pte_offset_kernel(pmd, vstart);
  944. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  945. if (this_end > vend)
  946. this_end = vend;
  947. while (vstart < this_end) {
  948. pte_val(*pte) = (paddr | pgprot_val(prot));
  949. vstart += PAGE_SIZE;
  950. paddr += PAGE_SIZE;
  951. pte++;
  952. }
  953. }
  954. return alloc_bytes;
  955. }
  956. extern unsigned int kvmap_linear_patch[1];
  957. #endif /* CONFIG_DEBUG_PAGEALLOC */
  958. static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
  959. {
  960. const unsigned long shift_256MB = 28;
  961. const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
  962. const unsigned long size_256MB = (1UL << shift_256MB);
  963. while (start < end) {
  964. long remains;
  965. remains = end - start;
  966. if (remains < size_256MB)
  967. break;
  968. if (start & mask_256MB) {
  969. start = (start + size_256MB) & ~mask_256MB;
  970. continue;
  971. }
  972. while (remains >= size_256MB) {
  973. unsigned long index = start >> shift_256MB;
  974. __set_bit(index, kpte_linear_bitmap);
  975. start += size_256MB;
  976. remains -= size_256MB;
  977. }
  978. }
  979. }
  980. static void __init kernel_physical_mapping_init(void)
  981. {
  982. unsigned long i;
  983. #ifdef CONFIG_DEBUG_PAGEALLOC
  984. unsigned long mem_alloced = 0UL;
  985. #endif
  986. read_obp_memory("reg", &pall[0], &pall_ents);
  987. for (i = 0; i < pall_ents; i++) {
  988. unsigned long phys_start, phys_end;
  989. phys_start = pall[i].phys_addr;
  990. phys_end = phys_start + pall[i].reg_size;
  991. mark_kpte_bitmap(phys_start, phys_end);
  992. #ifdef CONFIG_DEBUG_PAGEALLOC
  993. mem_alloced += kernel_map_range(phys_start, phys_end,
  994. PAGE_KERNEL);
  995. #endif
  996. }
  997. #ifdef CONFIG_DEBUG_PAGEALLOC
  998. printk("Allocated %ld bytes for kernel page tables.\n",
  999. mem_alloced);
  1000. kvmap_linear_patch[0] = 0x01000000; /* nop */
  1001. flushi(&kvmap_linear_patch[0]);
  1002. __flush_tlb_all();
  1003. #endif
  1004. }
  1005. #ifdef CONFIG_DEBUG_PAGEALLOC
  1006. void kernel_map_pages(struct page *page, int numpages, int enable)
  1007. {
  1008. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  1009. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  1010. kernel_map_range(phys_start, phys_end,
  1011. (enable ? PAGE_KERNEL : __pgprot(0)));
  1012. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  1013. PAGE_OFFSET + phys_end);
  1014. /* we should perform an IPI and flush all tlbs,
  1015. * but that can deadlock->flush only current cpu.
  1016. */
  1017. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  1018. PAGE_OFFSET + phys_end);
  1019. }
  1020. #endif
  1021. unsigned long __init find_ecache_flush_span(unsigned long size)
  1022. {
  1023. int i;
  1024. for (i = 0; i < pavail_ents; i++) {
  1025. if (pavail[i].reg_size >= size)
  1026. return pavail[i].phys_addr;
  1027. }
  1028. return ~0UL;
  1029. }
  1030. static void __init tsb_phys_patch(void)
  1031. {
  1032. struct tsb_ldquad_phys_patch_entry *pquad;
  1033. struct tsb_phys_patch_entry *p;
  1034. pquad = &__tsb_ldquad_phys_patch;
  1035. while (pquad < &__tsb_ldquad_phys_patch_end) {
  1036. unsigned long addr = pquad->addr;
  1037. if (tlb_type == hypervisor)
  1038. *(unsigned int *) addr = pquad->sun4v_insn;
  1039. else
  1040. *(unsigned int *) addr = pquad->sun4u_insn;
  1041. wmb();
  1042. __asm__ __volatile__("flush %0"
  1043. : /* no outputs */
  1044. : "r" (addr));
  1045. pquad++;
  1046. }
  1047. p = &__tsb_phys_patch;
  1048. while (p < &__tsb_phys_patch_end) {
  1049. unsigned long addr = p->addr;
  1050. *(unsigned int *) addr = p->insn;
  1051. wmb();
  1052. __asm__ __volatile__("flush %0"
  1053. : /* no outputs */
  1054. : "r" (addr));
  1055. p++;
  1056. }
  1057. }
  1058. /* Don't mark as init, we give this to the Hypervisor. */
  1059. #ifndef CONFIG_DEBUG_PAGEALLOC
  1060. #define NUM_KTSB_DESCR 2
  1061. #else
  1062. #define NUM_KTSB_DESCR 1
  1063. #endif
  1064. static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
  1065. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  1066. static void __init sun4v_ktsb_init(void)
  1067. {
  1068. unsigned long ktsb_pa;
  1069. /* First KTSB for PAGE_SIZE mappings. */
  1070. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1071. switch (PAGE_SIZE) {
  1072. case 8 * 1024:
  1073. default:
  1074. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  1075. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  1076. break;
  1077. case 64 * 1024:
  1078. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1079. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1080. break;
  1081. case 512 * 1024:
  1082. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1083. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1084. break;
  1085. case 4 * 1024 * 1024:
  1086. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1087. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1088. break;
  1089. };
  1090. ktsb_descr[0].assoc = 1;
  1091. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1092. ktsb_descr[0].ctx_idx = 0;
  1093. ktsb_descr[0].tsb_base = ktsb_pa;
  1094. ktsb_descr[0].resv = 0;
  1095. #ifndef CONFIG_DEBUG_PAGEALLOC
  1096. /* Second KTSB for 4MB/256MB mappings. */
  1097. ktsb_pa = (kern_base +
  1098. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1099. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1100. ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
  1101. HV_PGSZ_MASK_256MB);
  1102. ktsb_descr[1].assoc = 1;
  1103. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1104. ktsb_descr[1].ctx_idx = 0;
  1105. ktsb_descr[1].tsb_base = ktsb_pa;
  1106. ktsb_descr[1].resv = 0;
  1107. #endif
  1108. }
  1109. void __cpuinit sun4v_ktsb_register(void)
  1110. {
  1111. register unsigned long func asm("%o5");
  1112. register unsigned long arg0 asm("%o0");
  1113. register unsigned long arg1 asm("%o1");
  1114. unsigned long pa;
  1115. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1116. func = HV_FAST_MMU_TSB_CTX0;
  1117. arg0 = NUM_KTSB_DESCR;
  1118. arg1 = pa;
  1119. __asm__ __volatile__("ta %6"
  1120. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  1121. : "0" (func), "1" (arg0), "2" (arg1),
  1122. "i" (HV_FAST_TRAP));
  1123. }
  1124. /* paging_init() sets up the page tables */
  1125. extern void cheetah_ecache_flush_init(void);
  1126. extern void sun4v_patch_tlb_handlers(void);
  1127. extern void cpu_probe(void);
  1128. extern void central_probe(void);
  1129. static unsigned long last_valid_pfn;
  1130. pgd_t swapper_pg_dir[2048];
  1131. static void sun4u_pgprot_init(void);
  1132. static void sun4v_pgprot_init(void);
  1133. void __init paging_init(void)
  1134. {
  1135. unsigned long end_pfn, pages_avail, shift, phys_base;
  1136. unsigned long real_end, i;
  1137. /* These build time checkes make sure that the dcache_dirty_cpu()
  1138. * page->flags usage will work.
  1139. *
  1140. * When a page gets marked as dcache-dirty, we store the
  1141. * cpu number starting at bit 32 in the page->flags. Also,
  1142. * functions like clear_dcache_dirty_cpu use the cpu mask
  1143. * in 13-bit signed-immediate instruction fields.
  1144. */
  1145. BUILD_BUG_ON(FLAGS_RESERVED != 32);
  1146. BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
  1147. ilog2(roundup_pow_of_two(NR_CPUS)) > FLAGS_RESERVED);
  1148. BUILD_BUG_ON(NR_CPUS > 4096);
  1149. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  1150. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1151. sstate_booting();
  1152. /* Invalidate both kernel TSBs. */
  1153. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1154. #ifndef CONFIG_DEBUG_PAGEALLOC
  1155. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1156. #endif
  1157. if (tlb_type == hypervisor)
  1158. sun4v_pgprot_init();
  1159. else
  1160. sun4u_pgprot_init();
  1161. if (tlb_type == cheetah_plus ||
  1162. tlb_type == hypervisor)
  1163. tsb_phys_patch();
  1164. if (tlb_type == hypervisor) {
  1165. sun4v_patch_tlb_handlers();
  1166. sun4v_ktsb_init();
  1167. }
  1168. /* Find available physical memory... */
  1169. read_obp_memory("available", &pavail[0], &pavail_ents);
  1170. phys_base = 0xffffffffffffffffUL;
  1171. for (i = 0; i < pavail_ents; i++)
  1172. phys_base = min(phys_base, pavail[i].phys_addr);
  1173. set_bit(0, mmu_context_bmap);
  1174. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1175. real_end = (unsigned long)_end;
  1176. if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
  1177. bigkernel = 1;
  1178. if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
  1179. prom_printf("paging_init: Kernel > 8MB, too large.\n");
  1180. prom_halt();
  1181. }
  1182. /* Set kernel pgd to upper alias so physical page computations
  1183. * work.
  1184. */
  1185. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1186. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  1187. /* Now can init the kernel/bad page tables. */
  1188. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  1189. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  1190. inherit_prom_mappings();
  1191. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1192. setup_tba();
  1193. __flush_tlb_all();
  1194. if (tlb_type == hypervisor)
  1195. sun4v_ktsb_register();
  1196. /* Setup bootmem... */
  1197. pages_avail = 0;
  1198. last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
  1199. max_mapnr = last_valid_pfn;
  1200. kernel_physical_mapping_init();
  1201. real_setup_per_cpu_areas();
  1202. prom_build_devicetree();
  1203. if (tlb_type == hypervisor)
  1204. sun4v_mdesc_init();
  1205. {
  1206. unsigned long zones_size[MAX_NR_ZONES];
  1207. unsigned long zholes_size[MAX_NR_ZONES];
  1208. int znum;
  1209. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  1210. zones_size[znum] = zholes_size[znum] = 0;
  1211. zones_size[ZONE_NORMAL] = end_pfn;
  1212. zholes_size[ZONE_NORMAL] = end_pfn - pages_avail;
  1213. free_area_init_node(0, &contig_page_data, zones_size,
  1214. __pa(PAGE_OFFSET) >> PAGE_SHIFT,
  1215. zholes_size);
  1216. }
  1217. prom_printf("Booting Linux...\n");
  1218. central_probe();
  1219. cpu_probe();
  1220. }
  1221. static void __init taint_real_pages(void)
  1222. {
  1223. int i;
  1224. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  1225. /* Find changes discovered in the physmem available rescan and
  1226. * reserve the lost portions in the bootmem maps.
  1227. */
  1228. for (i = 0; i < pavail_ents; i++) {
  1229. unsigned long old_start, old_end;
  1230. old_start = pavail[i].phys_addr;
  1231. old_end = old_start +
  1232. pavail[i].reg_size;
  1233. while (old_start < old_end) {
  1234. int n;
  1235. for (n = 0; n < pavail_rescan_ents; n++) {
  1236. unsigned long new_start, new_end;
  1237. new_start = pavail_rescan[n].phys_addr;
  1238. new_end = new_start +
  1239. pavail_rescan[n].reg_size;
  1240. if (new_start <= old_start &&
  1241. new_end >= (old_start + PAGE_SIZE)) {
  1242. set_bit(old_start >> 22,
  1243. sparc64_valid_addr_bitmap);
  1244. goto do_next_page;
  1245. }
  1246. }
  1247. reserve_bootmem(old_start, PAGE_SIZE);
  1248. do_next_page:
  1249. old_start += PAGE_SIZE;
  1250. }
  1251. }
  1252. }
  1253. int __init page_in_phys_avail(unsigned long paddr)
  1254. {
  1255. int i;
  1256. paddr &= PAGE_MASK;
  1257. for (i = 0; i < pavail_rescan_ents; i++) {
  1258. unsigned long start, end;
  1259. start = pavail_rescan[i].phys_addr;
  1260. end = start + pavail_rescan[i].reg_size;
  1261. if (paddr >= start && paddr < end)
  1262. return 1;
  1263. }
  1264. if (paddr >= kern_base && paddr < (kern_base + kern_size))
  1265. return 1;
  1266. #ifdef CONFIG_BLK_DEV_INITRD
  1267. if (paddr >= __pa(initrd_start) &&
  1268. paddr < __pa(PAGE_ALIGN(initrd_end)))
  1269. return 1;
  1270. #endif
  1271. return 0;
  1272. }
  1273. void __init mem_init(void)
  1274. {
  1275. unsigned long codepages, datapages, initpages;
  1276. unsigned long addr, last;
  1277. int i;
  1278. i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
  1279. i += 1;
  1280. sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
  1281. if (sparc64_valid_addr_bitmap == NULL) {
  1282. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  1283. prom_halt();
  1284. }
  1285. memset(sparc64_valid_addr_bitmap, 0, i << 3);
  1286. addr = PAGE_OFFSET + kern_base;
  1287. last = PAGE_ALIGN(kern_size) + addr;
  1288. while (addr < last) {
  1289. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1290. addr += PAGE_SIZE;
  1291. }
  1292. taint_real_pages();
  1293. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1294. #ifdef CONFIG_DEBUG_BOOTMEM
  1295. prom_printf("mem_init: Calling free_all_bootmem().\n");
  1296. #endif
  1297. /* We subtract one to account for the mem_map_zero page
  1298. * allocated below.
  1299. */
  1300. totalram_pages = num_physpages = free_all_bootmem() - 1;
  1301. /*
  1302. * Set up the zero page, mark it reserved, so that page count
  1303. * is not manipulated when freeing the page from user ptes.
  1304. */
  1305. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1306. if (mem_map_zero == NULL) {
  1307. prom_printf("paging_init: Cannot alloc zero page.\n");
  1308. prom_halt();
  1309. }
  1310. SetPageReserved(mem_map_zero);
  1311. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1312. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1313. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1314. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1315. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1316. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1317. printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1318. nr_free_pages() << (PAGE_SHIFT-10),
  1319. codepages << (PAGE_SHIFT-10),
  1320. datapages << (PAGE_SHIFT-10),
  1321. initpages << (PAGE_SHIFT-10),
  1322. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1323. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1324. cheetah_ecache_flush_init();
  1325. }
  1326. void free_initmem(void)
  1327. {
  1328. unsigned long addr, initend;
  1329. /*
  1330. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1331. */
  1332. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1333. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1334. for (; addr < initend; addr += PAGE_SIZE) {
  1335. unsigned long page;
  1336. struct page *p;
  1337. page = (addr +
  1338. ((unsigned long) __va(kern_base)) -
  1339. ((unsigned long) KERNBASE));
  1340. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1341. p = virt_to_page(page);
  1342. ClearPageReserved(p);
  1343. init_page_count(p);
  1344. __free_page(p);
  1345. num_physpages++;
  1346. totalram_pages++;
  1347. }
  1348. }
  1349. #ifdef CONFIG_BLK_DEV_INITRD
  1350. void free_initrd_mem(unsigned long start, unsigned long end)
  1351. {
  1352. if (start < end)
  1353. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1354. for (; start < end; start += PAGE_SIZE) {
  1355. struct page *p = virt_to_page(start);
  1356. ClearPageReserved(p);
  1357. init_page_count(p);
  1358. __free_page(p);
  1359. num_physpages++;
  1360. totalram_pages++;
  1361. }
  1362. }
  1363. #endif
  1364. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1365. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1366. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1367. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1368. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1369. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1370. pgprot_t PAGE_KERNEL __read_mostly;
  1371. EXPORT_SYMBOL(PAGE_KERNEL);
  1372. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1373. pgprot_t PAGE_COPY __read_mostly;
  1374. pgprot_t PAGE_SHARED __read_mostly;
  1375. EXPORT_SYMBOL(PAGE_SHARED);
  1376. pgprot_t PAGE_EXEC __read_mostly;
  1377. unsigned long pg_iobits __read_mostly;
  1378. unsigned long _PAGE_IE __read_mostly;
  1379. EXPORT_SYMBOL(_PAGE_IE);
  1380. unsigned long _PAGE_E __read_mostly;
  1381. EXPORT_SYMBOL(_PAGE_E);
  1382. unsigned long _PAGE_CACHE __read_mostly;
  1383. EXPORT_SYMBOL(_PAGE_CACHE);
  1384. static void prot_init_common(unsigned long page_none,
  1385. unsigned long page_shared,
  1386. unsigned long page_copy,
  1387. unsigned long page_readonly,
  1388. unsigned long page_exec_bit)
  1389. {
  1390. PAGE_COPY = __pgprot(page_copy);
  1391. PAGE_SHARED = __pgprot(page_shared);
  1392. protection_map[0x0] = __pgprot(page_none);
  1393. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1394. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1395. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1396. protection_map[0x4] = __pgprot(page_readonly);
  1397. protection_map[0x5] = __pgprot(page_readonly);
  1398. protection_map[0x6] = __pgprot(page_copy);
  1399. protection_map[0x7] = __pgprot(page_copy);
  1400. protection_map[0x8] = __pgprot(page_none);
  1401. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1402. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1403. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1404. protection_map[0xc] = __pgprot(page_readonly);
  1405. protection_map[0xd] = __pgprot(page_readonly);
  1406. protection_map[0xe] = __pgprot(page_shared);
  1407. protection_map[0xf] = __pgprot(page_shared);
  1408. }
  1409. static void __init sun4u_pgprot_init(void)
  1410. {
  1411. unsigned long page_none, page_shared, page_copy, page_readonly;
  1412. unsigned long page_exec_bit;
  1413. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1414. _PAGE_CACHE_4U | _PAGE_P_4U |
  1415. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1416. _PAGE_EXEC_4U);
  1417. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1418. _PAGE_CACHE_4U | _PAGE_P_4U |
  1419. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1420. _PAGE_EXEC_4U | _PAGE_L_4U);
  1421. PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
  1422. _PAGE_IE = _PAGE_IE_4U;
  1423. _PAGE_E = _PAGE_E_4U;
  1424. _PAGE_CACHE = _PAGE_CACHE_4U;
  1425. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1426. __ACCESS_BITS_4U | _PAGE_E_4U);
  1427. #ifdef CONFIG_DEBUG_PAGEALLOC
  1428. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
  1429. 0xfffff80000000000;
  1430. #else
  1431. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1432. 0xfffff80000000000;
  1433. #endif
  1434. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1435. _PAGE_P_4U | _PAGE_W_4U);
  1436. /* XXX Should use 256MB on Panther. XXX */
  1437. kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
  1438. _PAGE_SZBITS = _PAGE_SZBITS_4U;
  1439. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1440. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1441. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1442. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1443. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1444. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1445. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1446. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1447. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1448. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1449. page_exec_bit = _PAGE_EXEC_4U;
  1450. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1451. page_exec_bit);
  1452. }
  1453. static void __init sun4v_pgprot_init(void)
  1454. {
  1455. unsigned long page_none, page_shared, page_copy, page_readonly;
  1456. unsigned long page_exec_bit;
  1457. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1458. _PAGE_CACHE_4V | _PAGE_P_4V |
  1459. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1460. _PAGE_EXEC_4V);
  1461. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1462. PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
  1463. _PAGE_IE = _PAGE_IE_4V;
  1464. _PAGE_E = _PAGE_E_4V;
  1465. _PAGE_CACHE = _PAGE_CACHE_4V;
  1466. #ifdef CONFIG_DEBUG_PAGEALLOC
  1467. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1468. 0xfffff80000000000;
  1469. #else
  1470. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  1471. 0xfffff80000000000;
  1472. #endif
  1473. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1474. _PAGE_P_4V | _PAGE_W_4V);
  1475. #ifdef CONFIG_DEBUG_PAGEALLOC
  1476. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1477. 0xfffff80000000000;
  1478. #else
  1479. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1480. 0xfffff80000000000;
  1481. #endif
  1482. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1483. _PAGE_P_4V | _PAGE_W_4V);
  1484. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  1485. __ACCESS_BITS_4V | _PAGE_E_4V);
  1486. _PAGE_SZBITS = _PAGE_SZBITS_4V;
  1487. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  1488. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  1489. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  1490. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  1491. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  1492. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1493. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  1494. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1495. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1496. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1497. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1498. page_exec_bit = _PAGE_EXEC_4V;
  1499. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1500. page_exec_bit);
  1501. }
  1502. unsigned long pte_sz_bits(unsigned long sz)
  1503. {
  1504. if (tlb_type == hypervisor) {
  1505. switch (sz) {
  1506. case 8 * 1024:
  1507. default:
  1508. return _PAGE_SZ8K_4V;
  1509. case 64 * 1024:
  1510. return _PAGE_SZ64K_4V;
  1511. case 512 * 1024:
  1512. return _PAGE_SZ512K_4V;
  1513. case 4 * 1024 * 1024:
  1514. return _PAGE_SZ4MB_4V;
  1515. };
  1516. } else {
  1517. switch (sz) {
  1518. case 8 * 1024:
  1519. default:
  1520. return _PAGE_SZ8K_4U;
  1521. case 64 * 1024:
  1522. return _PAGE_SZ64K_4U;
  1523. case 512 * 1024:
  1524. return _PAGE_SZ512K_4U;
  1525. case 4 * 1024 * 1024:
  1526. return _PAGE_SZ4MB_4U;
  1527. };
  1528. }
  1529. }
  1530. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  1531. {
  1532. pte_t pte;
  1533. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  1534. pte_val(pte) |= (((unsigned long)space) << 32);
  1535. pte_val(pte) |= pte_sz_bits(page_size);
  1536. return pte;
  1537. }
  1538. static unsigned long kern_large_tte(unsigned long paddr)
  1539. {
  1540. unsigned long val;
  1541. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1542. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  1543. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  1544. if (tlb_type == hypervisor)
  1545. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1546. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  1547. _PAGE_EXEC_4V | _PAGE_W_4V);
  1548. return val | paddr;
  1549. }
  1550. /* If not locked, zap it. */
  1551. void __flush_tlb_all(void)
  1552. {
  1553. unsigned long pstate;
  1554. int i;
  1555. __asm__ __volatile__("flushw\n\t"
  1556. "rdpr %%pstate, %0\n\t"
  1557. "wrpr %0, %1, %%pstate"
  1558. : "=r" (pstate)
  1559. : "i" (PSTATE_IE));
  1560. if (tlb_type == spitfire) {
  1561. for (i = 0; i < 64; i++) {
  1562. /* Spitfire Errata #32 workaround */
  1563. /* NOTE: Always runs on spitfire, so no
  1564. * cheetah+ page size encodings.
  1565. */
  1566. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1567. "flush %%g6"
  1568. : /* No outputs */
  1569. : "r" (0),
  1570. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1571. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  1572. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1573. "membar #Sync"
  1574. : /* no outputs */
  1575. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  1576. spitfire_put_dtlb_data(i, 0x0UL);
  1577. }
  1578. /* Spitfire Errata #32 workaround */
  1579. /* NOTE: Always runs on spitfire, so no
  1580. * cheetah+ page size encodings.
  1581. */
  1582. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1583. "flush %%g6"
  1584. : /* No outputs */
  1585. : "r" (0),
  1586. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1587. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  1588. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1589. "membar #Sync"
  1590. : /* no outputs */
  1591. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  1592. spitfire_put_itlb_data(i, 0x0UL);
  1593. }
  1594. }
  1595. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1596. cheetah_flush_dtlb_all();
  1597. cheetah_flush_itlb_all();
  1598. }
  1599. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  1600. : : "r" (pstate));
  1601. }
  1602. #ifdef CONFIG_MEMORY_HOTPLUG
  1603. void online_page(struct page *page)
  1604. {
  1605. ClearPageReserved(page);
  1606. init_page_count(page);
  1607. __free_page(page);
  1608. totalram_pages++;
  1609. num_physpages++;
  1610. }
  1611. int remove_memory(u64 start, u64 size)
  1612. {
  1613. return -EINVAL;
  1614. }
  1615. #endif /* CONFIG_MEMORY_HOTPLUG */