init.c 52 KB

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