init.c 49 KB

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