init.c 50 KB

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