init.c 48 KB

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