init.c 47 KB

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