init.c 51 KB

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