init.c 48 KB

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