init.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  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. /* Find a free area for the bootmem map, avoiding the kernel image
  651. * and the initial ramdisk.
  652. */
  653. static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
  654. unsigned long end_pfn)
  655. {
  656. unsigned long avoid_start, avoid_end, bootmap_size;
  657. int i;
  658. bootmap_size = bootmem_bootmap_pages(end_pfn - start_pfn);
  659. bootmap_size <<= PAGE_SHIFT;
  660. avoid_start = avoid_end = 0;
  661. #ifdef CONFIG_BLK_DEV_INITRD
  662. avoid_start = initrd_start;
  663. avoid_end = PAGE_ALIGN(initrd_end);
  664. #endif
  665. #ifdef CONFIG_DEBUG_BOOTMEM
  666. prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
  667. kern_base, PAGE_ALIGN(kern_base + kern_size),
  668. avoid_start, avoid_end);
  669. #endif
  670. for (i = 0; i < pavail_ents; i++) {
  671. unsigned long start, end;
  672. start = pavail[i].phys_addr;
  673. end = start + pavail[i].reg_size;
  674. while (start < end) {
  675. if (start >= kern_base &&
  676. start < PAGE_ALIGN(kern_base + kern_size)) {
  677. start = PAGE_ALIGN(kern_base + kern_size);
  678. continue;
  679. }
  680. if (start >= avoid_start && start < avoid_end) {
  681. start = avoid_end;
  682. continue;
  683. }
  684. if ((end - start) < bootmap_size)
  685. break;
  686. if (start < kern_base &&
  687. (start + bootmap_size) > kern_base) {
  688. start = PAGE_ALIGN(kern_base + kern_size);
  689. continue;
  690. }
  691. if (start < avoid_start &&
  692. (start + bootmap_size) > avoid_start) {
  693. start = avoid_end;
  694. continue;
  695. }
  696. /* OK, it doesn't overlap anything, use it. */
  697. #ifdef CONFIG_DEBUG_BOOTMEM
  698. prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
  699. start >> PAGE_SHIFT, start);
  700. #endif
  701. return start >> PAGE_SHIFT;
  702. }
  703. }
  704. prom_printf("Cannot find free area for bootmap, aborting.\n");
  705. prom_halt();
  706. }
  707. static void __init trim_pavail(unsigned long *cur_size_p,
  708. unsigned long *end_of_phys_p)
  709. {
  710. unsigned long to_trim = *cur_size_p - cmdline_memory_size;
  711. unsigned long avoid_start, avoid_end;
  712. int i;
  713. to_trim = PAGE_ALIGN(to_trim);
  714. avoid_start = avoid_end = 0;
  715. #ifdef CONFIG_BLK_DEV_INITRD
  716. avoid_start = initrd_start;
  717. avoid_end = PAGE_ALIGN(initrd_end);
  718. #endif
  719. /* Trim some pavail[] entries in order to satisfy the
  720. * requested "mem=xxx" kernel command line specification.
  721. *
  722. * We must not trim off the kernel image area nor the
  723. * initial ramdisk range (if any). Also, we must not trim
  724. * any pavail[] entry down to zero in order to preserve
  725. * the invariant that all pavail[] entries have a non-zero
  726. * size which is assumed by all of the code in here.
  727. */
  728. for (i = 0; i < pavail_ents; i++) {
  729. unsigned long start, end, kern_end;
  730. unsigned long trim_low, trim_high, n;
  731. kern_end = PAGE_ALIGN(kern_base + kern_size);
  732. trim_low = start = pavail[i].phys_addr;
  733. trim_high = end = start + pavail[i].reg_size;
  734. if (kern_base >= start &&
  735. kern_base < end) {
  736. trim_low = kern_base;
  737. if (kern_end >= end)
  738. continue;
  739. }
  740. if (kern_end >= start &&
  741. kern_end < end) {
  742. trim_high = kern_end;
  743. }
  744. if (avoid_start &&
  745. avoid_start >= start &&
  746. avoid_start < end) {
  747. if (trim_low > avoid_start)
  748. trim_low = avoid_start;
  749. if (avoid_end >= end)
  750. continue;
  751. }
  752. if (avoid_end &&
  753. avoid_end >= start &&
  754. avoid_end < end) {
  755. if (trim_high < avoid_end)
  756. trim_high = avoid_end;
  757. }
  758. if (trim_high <= trim_low)
  759. continue;
  760. if (trim_low == start && trim_high == end) {
  761. /* Whole chunk is available for trimming.
  762. * Trim all except one page, in order to keep
  763. * entry non-empty.
  764. */
  765. n = (end - start) - PAGE_SIZE;
  766. if (n > to_trim)
  767. n = to_trim;
  768. if (n) {
  769. pavail[i].phys_addr += n;
  770. pavail[i].reg_size -= n;
  771. to_trim -= n;
  772. }
  773. } else {
  774. n = (trim_low - start);
  775. if (n > to_trim)
  776. n = to_trim;
  777. if (n) {
  778. pavail[i].phys_addr += n;
  779. pavail[i].reg_size -= n;
  780. to_trim -= n;
  781. }
  782. if (to_trim) {
  783. n = end - trim_high;
  784. if (n > to_trim)
  785. n = to_trim;
  786. if (n) {
  787. pavail[i].reg_size -= n;
  788. to_trim -= n;
  789. }
  790. }
  791. }
  792. if (!to_trim)
  793. break;
  794. }
  795. /* Recalculate. */
  796. *cur_size_p = 0UL;
  797. for (i = 0; i < pavail_ents; i++) {
  798. *end_of_phys_p = pavail[i].phys_addr +
  799. pavail[i].reg_size;
  800. *cur_size_p += pavail[i].reg_size;
  801. }
  802. }
  803. /* About pages_avail, this is the value we will use to calculate
  804. * the zholes_size[] argument given to free_area_init_node(). The
  805. * page allocator uses this to calculate nr_kernel_pages,
  806. * nr_all_pages and zone->present_pages. On NUMA it is used
  807. * to calculate zone->min_unmapped_pages and zone->min_slab_pages.
  808. *
  809. * So this number should really be set to what the page allocator
  810. * actually ends up with. This means:
  811. * 1) It should include bootmem map pages, we'll release those.
  812. * 2) It should not include the kernel image, except for the
  813. * __init sections which we will also release.
  814. * 3) It should include the initrd image, since we'll release
  815. * that too.
  816. */
  817. static unsigned long __init bootmem_init(unsigned long *pages_avail,
  818. unsigned long phys_base)
  819. {
  820. unsigned long bootmap_size, end_pfn;
  821. unsigned long end_of_phys_memory = 0UL;
  822. unsigned long bootmap_pfn, bytes_avail, size;
  823. int i;
  824. #ifdef CONFIG_DEBUG_BOOTMEM
  825. prom_printf("bootmem_init: Scan pavail, ");
  826. #endif
  827. bytes_avail = 0UL;
  828. for (i = 0; i < pavail_ents; i++) {
  829. end_of_phys_memory = pavail[i].phys_addr +
  830. pavail[i].reg_size;
  831. bytes_avail += pavail[i].reg_size;
  832. }
  833. /* Determine the location of the initial ramdisk before trying
  834. * to honor the "mem=xxx" command line argument. We must know
  835. * where the kernel image and the ramdisk image are so that we
  836. * do not trim those two areas from the physical memory map.
  837. */
  838. #ifdef CONFIG_BLK_DEV_INITRD
  839. /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
  840. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  841. unsigned long ramdisk_image = sparc_ramdisk_image ?
  842. sparc_ramdisk_image : sparc_ramdisk_image64;
  843. ramdisk_image -= KERNBASE;
  844. initrd_start = ramdisk_image + phys_base;
  845. initrd_end = initrd_start + sparc_ramdisk_size;
  846. if (initrd_end > end_of_phys_memory) {
  847. printk(KERN_CRIT "initrd extends beyond end of memory "
  848. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  849. initrd_end, end_of_phys_memory);
  850. initrd_start = 0;
  851. initrd_end = 0;
  852. }
  853. }
  854. #endif
  855. if (cmdline_memory_size &&
  856. bytes_avail > cmdline_memory_size)
  857. trim_pavail(&bytes_avail,
  858. &end_of_phys_memory);
  859. *pages_avail = bytes_avail >> PAGE_SHIFT;
  860. end_pfn = end_of_phys_memory >> PAGE_SHIFT;
  861. /* Initialize the boot-time allocator. */
  862. max_pfn = max_low_pfn = end_pfn;
  863. min_low_pfn = (phys_base >> PAGE_SHIFT);
  864. bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
  865. #ifdef CONFIG_DEBUG_BOOTMEM
  866. prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
  867. min_low_pfn, bootmap_pfn, max_low_pfn);
  868. #endif
  869. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
  870. min_low_pfn, end_pfn);
  871. /* Now register the available physical memory with the
  872. * allocator.
  873. */
  874. for (i = 0; i < pavail_ents; i++) {
  875. #ifdef CONFIG_DEBUG_BOOTMEM
  876. prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
  877. i, pavail[i].phys_addr, pavail[i].reg_size);
  878. #endif
  879. free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
  880. }
  881. #ifdef CONFIG_BLK_DEV_INITRD
  882. if (initrd_start) {
  883. size = initrd_end - initrd_start;
  884. /* Resert the initrd image area. */
  885. #ifdef CONFIG_DEBUG_BOOTMEM
  886. prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
  887. initrd_start, initrd_end);
  888. #endif
  889. reserve_bootmem(initrd_start, size);
  890. initrd_start += PAGE_OFFSET;
  891. initrd_end += PAGE_OFFSET;
  892. }
  893. #endif
  894. /* Reserve the kernel text/data/bss. */
  895. #ifdef CONFIG_DEBUG_BOOTMEM
  896. prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
  897. #endif
  898. reserve_bootmem(kern_base, kern_size);
  899. *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
  900. /* Add back in the initmem pages. */
  901. size = ((unsigned long)(__init_end) & PAGE_MASK) -
  902. PAGE_ALIGN((unsigned long)__init_begin);
  903. *pages_avail += size >> PAGE_SHIFT;
  904. /* Reserve the bootmem map. We do not account for it
  905. * in pages_avail because we will release that memory
  906. * in free_all_bootmem.
  907. */
  908. size = bootmap_size;
  909. #ifdef CONFIG_DEBUG_BOOTMEM
  910. prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
  911. (bootmap_pfn << PAGE_SHIFT), size);
  912. #endif
  913. reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
  914. for (i = 0; i < pavail_ents; i++) {
  915. unsigned long start_pfn, end_pfn;
  916. start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
  917. end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
  918. #ifdef CONFIG_DEBUG_BOOTMEM
  919. prom_printf("memory_present(0, %lx, %lx)\n",
  920. start_pfn, end_pfn);
  921. #endif
  922. memory_present(0, start_pfn, end_pfn);
  923. }
  924. sparse_init();
  925. return end_pfn;
  926. }
  927. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  928. static int pall_ents __initdata;
  929. #ifdef CONFIG_DEBUG_PAGEALLOC
  930. static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
  931. {
  932. unsigned long vstart = PAGE_OFFSET + pstart;
  933. unsigned long vend = PAGE_OFFSET + pend;
  934. unsigned long alloc_bytes = 0UL;
  935. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  936. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  937. vstart, vend);
  938. prom_halt();
  939. }
  940. while (vstart < vend) {
  941. unsigned long this_end, paddr = __pa(vstart);
  942. pgd_t *pgd = pgd_offset_k(vstart);
  943. pud_t *pud;
  944. pmd_t *pmd;
  945. pte_t *pte;
  946. pud = pud_offset(pgd, vstart);
  947. if (pud_none(*pud)) {
  948. pmd_t *new;
  949. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  950. alloc_bytes += PAGE_SIZE;
  951. pud_populate(&init_mm, pud, new);
  952. }
  953. pmd = pmd_offset(pud, vstart);
  954. if (!pmd_present(*pmd)) {
  955. pte_t *new;
  956. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  957. alloc_bytes += PAGE_SIZE;
  958. pmd_populate_kernel(&init_mm, pmd, new);
  959. }
  960. pte = pte_offset_kernel(pmd, vstart);
  961. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  962. if (this_end > vend)
  963. this_end = vend;
  964. while (vstart < this_end) {
  965. pte_val(*pte) = (paddr | pgprot_val(prot));
  966. vstart += PAGE_SIZE;
  967. paddr += PAGE_SIZE;
  968. pte++;
  969. }
  970. }
  971. return alloc_bytes;
  972. }
  973. extern unsigned int kvmap_linear_patch[1];
  974. #endif /* CONFIG_DEBUG_PAGEALLOC */
  975. static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
  976. {
  977. const unsigned long shift_256MB = 28;
  978. const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
  979. const unsigned long size_256MB = (1UL << shift_256MB);
  980. while (start < end) {
  981. long remains;
  982. remains = end - start;
  983. if (remains < size_256MB)
  984. break;
  985. if (start & mask_256MB) {
  986. start = (start + size_256MB) & ~mask_256MB;
  987. continue;
  988. }
  989. while (remains >= size_256MB) {
  990. unsigned long index = start >> shift_256MB;
  991. __set_bit(index, kpte_linear_bitmap);
  992. start += size_256MB;
  993. remains -= size_256MB;
  994. }
  995. }
  996. }
  997. static void __init kernel_physical_mapping_init(void)
  998. {
  999. unsigned long i;
  1000. #ifdef CONFIG_DEBUG_PAGEALLOC
  1001. unsigned long mem_alloced = 0UL;
  1002. #endif
  1003. read_obp_memory("reg", &pall[0], &pall_ents);
  1004. for (i = 0; i < pall_ents; i++) {
  1005. unsigned long phys_start, phys_end;
  1006. phys_start = pall[i].phys_addr;
  1007. phys_end = phys_start + pall[i].reg_size;
  1008. mark_kpte_bitmap(phys_start, phys_end);
  1009. #ifdef CONFIG_DEBUG_PAGEALLOC
  1010. mem_alloced += kernel_map_range(phys_start, phys_end,
  1011. PAGE_KERNEL);
  1012. #endif
  1013. }
  1014. #ifdef CONFIG_DEBUG_PAGEALLOC
  1015. printk("Allocated %ld bytes for kernel page tables.\n",
  1016. mem_alloced);
  1017. kvmap_linear_patch[0] = 0x01000000; /* nop */
  1018. flushi(&kvmap_linear_patch[0]);
  1019. __flush_tlb_all();
  1020. #endif
  1021. }
  1022. #ifdef CONFIG_DEBUG_PAGEALLOC
  1023. void kernel_map_pages(struct page *page, int numpages, int enable)
  1024. {
  1025. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  1026. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  1027. kernel_map_range(phys_start, phys_end,
  1028. (enable ? PAGE_KERNEL : __pgprot(0)));
  1029. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  1030. PAGE_OFFSET + phys_end);
  1031. /* we should perform an IPI and flush all tlbs,
  1032. * but that can deadlock->flush only current cpu.
  1033. */
  1034. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  1035. PAGE_OFFSET + phys_end);
  1036. }
  1037. #endif
  1038. unsigned long __init find_ecache_flush_span(unsigned long size)
  1039. {
  1040. int i;
  1041. for (i = 0; i < pavail_ents; i++) {
  1042. if (pavail[i].reg_size >= size)
  1043. return pavail[i].phys_addr;
  1044. }
  1045. return ~0UL;
  1046. }
  1047. static void __init tsb_phys_patch(void)
  1048. {
  1049. struct tsb_ldquad_phys_patch_entry *pquad;
  1050. struct tsb_phys_patch_entry *p;
  1051. pquad = &__tsb_ldquad_phys_patch;
  1052. while (pquad < &__tsb_ldquad_phys_patch_end) {
  1053. unsigned long addr = pquad->addr;
  1054. if (tlb_type == hypervisor)
  1055. *(unsigned int *) addr = pquad->sun4v_insn;
  1056. else
  1057. *(unsigned int *) addr = pquad->sun4u_insn;
  1058. wmb();
  1059. __asm__ __volatile__("flush %0"
  1060. : /* no outputs */
  1061. : "r" (addr));
  1062. pquad++;
  1063. }
  1064. p = &__tsb_phys_patch;
  1065. while (p < &__tsb_phys_patch_end) {
  1066. unsigned long addr = p->addr;
  1067. *(unsigned int *) addr = p->insn;
  1068. wmb();
  1069. __asm__ __volatile__("flush %0"
  1070. : /* no outputs */
  1071. : "r" (addr));
  1072. p++;
  1073. }
  1074. }
  1075. /* Don't mark as init, we give this to the Hypervisor. */
  1076. #ifndef CONFIG_DEBUG_PAGEALLOC
  1077. #define NUM_KTSB_DESCR 2
  1078. #else
  1079. #define NUM_KTSB_DESCR 1
  1080. #endif
  1081. static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
  1082. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  1083. static void __init sun4v_ktsb_init(void)
  1084. {
  1085. unsigned long ktsb_pa;
  1086. /* First KTSB for PAGE_SIZE mappings. */
  1087. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1088. switch (PAGE_SIZE) {
  1089. case 8 * 1024:
  1090. default:
  1091. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  1092. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  1093. break;
  1094. case 64 * 1024:
  1095. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1096. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1097. break;
  1098. case 512 * 1024:
  1099. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1100. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1101. break;
  1102. case 4 * 1024 * 1024:
  1103. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1104. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1105. break;
  1106. };
  1107. ktsb_descr[0].assoc = 1;
  1108. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1109. ktsb_descr[0].ctx_idx = 0;
  1110. ktsb_descr[0].tsb_base = ktsb_pa;
  1111. ktsb_descr[0].resv = 0;
  1112. #ifndef CONFIG_DEBUG_PAGEALLOC
  1113. /* Second KTSB for 4MB/256MB mappings. */
  1114. ktsb_pa = (kern_base +
  1115. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1116. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1117. ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
  1118. HV_PGSZ_MASK_256MB);
  1119. ktsb_descr[1].assoc = 1;
  1120. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1121. ktsb_descr[1].ctx_idx = 0;
  1122. ktsb_descr[1].tsb_base = ktsb_pa;
  1123. ktsb_descr[1].resv = 0;
  1124. #endif
  1125. }
  1126. void __cpuinit sun4v_ktsb_register(void)
  1127. {
  1128. register unsigned long func asm("%o5");
  1129. register unsigned long arg0 asm("%o0");
  1130. register unsigned long arg1 asm("%o1");
  1131. unsigned long pa;
  1132. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1133. func = HV_FAST_MMU_TSB_CTX0;
  1134. arg0 = NUM_KTSB_DESCR;
  1135. arg1 = pa;
  1136. __asm__ __volatile__("ta %6"
  1137. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  1138. : "0" (func), "1" (arg0), "2" (arg1),
  1139. "i" (HV_FAST_TRAP));
  1140. }
  1141. /* paging_init() sets up the page tables */
  1142. extern void cheetah_ecache_flush_init(void);
  1143. extern void sun4v_patch_tlb_handlers(void);
  1144. static unsigned long last_valid_pfn;
  1145. pgd_t swapper_pg_dir[2048];
  1146. static void sun4u_pgprot_init(void);
  1147. static void sun4v_pgprot_init(void);
  1148. void __init paging_init(void)
  1149. {
  1150. unsigned long end_pfn, pages_avail, shift, phys_base;
  1151. unsigned long real_end, i;
  1152. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  1153. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1154. /* Invalidate both kernel TSBs. */
  1155. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1156. #ifndef CONFIG_DEBUG_PAGEALLOC
  1157. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1158. #endif
  1159. if (tlb_type == hypervisor)
  1160. sun4v_pgprot_init();
  1161. else
  1162. sun4u_pgprot_init();
  1163. if (tlb_type == cheetah_plus ||
  1164. tlb_type == hypervisor)
  1165. tsb_phys_patch();
  1166. if (tlb_type == hypervisor) {
  1167. sun4v_patch_tlb_handlers();
  1168. sun4v_ktsb_init();
  1169. }
  1170. /* Find available physical memory... */
  1171. read_obp_memory("available", &pavail[0], &pavail_ents);
  1172. phys_base = 0xffffffffffffffffUL;
  1173. for (i = 0; i < pavail_ents; i++)
  1174. phys_base = min(phys_base, pavail[i].phys_addr);
  1175. set_bit(0, mmu_context_bmap);
  1176. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1177. real_end = (unsigned long)_end;
  1178. if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
  1179. bigkernel = 1;
  1180. if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
  1181. prom_printf("paging_init: Kernel > 8MB, too large.\n");
  1182. prom_halt();
  1183. }
  1184. /* Set kernel pgd to upper alias so physical page computations
  1185. * work.
  1186. */
  1187. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1188. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  1189. /* Now can init the kernel/bad page tables. */
  1190. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  1191. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  1192. inherit_prom_mappings();
  1193. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1194. setup_tba();
  1195. __flush_tlb_all();
  1196. if (tlb_type == hypervisor)
  1197. sun4v_ktsb_register();
  1198. /* Setup bootmem... */
  1199. pages_avail = 0;
  1200. last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
  1201. max_mapnr = last_valid_pfn;
  1202. kernel_physical_mapping_init();
  1203. prom_build_devicetree();
  1204. {
  1205. unsigned long zones_size[MAX_NR_ZONES];
  1206. unsigned long zholes_size[MAX_NR_ZONES];
  1207. int znum;
  1208. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  1209. zones_size[znum] = zholes_size[znum] = 0;
  1210. zones_size[ZONE_NORMAL] = end_pfn;
  1211. zholes_size[ZONE_NORMAL] = end_pfn - pages_avail;
  1212. free_area_init_node(0, &contig_page_data, zones_size,
  1213. __pa(PAGE_OFFSET) >> PAGE_SHIFT,
  1214. zholes_size);
  1215. }
  1216. device_scan();
  1217. }
  1218. static void __init taint_real_pages(void)
  1219. {
  1220. int i;
  1221. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  1222. /* Find changes discovered in the physmem available rescan and
  1223. * reserve the lost portions in the bootmem maps.
  1224. */
  1225. for (i = 0; i < pavail_ents; i++) {
  1226. unsigned long old_start, old_end;
  1227. old_start = pavail[i].phys_addr;
  1228. old_end = old_start +
  1229. pavail[i].reg_size;
  1230. while (old_start < old_end) {
  1231. int n;
  1232. for (n = 0; n < pavail_rescan_ents; n++) {
  1233. unsigned long new_start, new_end;
  1234. new_start = pavail_rescan[n].phys_addr;
  1235. new_end = new_start +
  1236. pavail_rescan[n].reg_size;
  1237. if (new_start <= old_start &&
  1238. new_end >= (old_start + PAGE_SIZE)) {
  1239. set_bit(old_start >> 22,
  1240. sparc64_valid_addr_bitmap);
  1241. goto do_next_page;
  1242. }
  1243. }
  1244. reserve_bootmem(old_start, PAGE_SIZE);
  1245. do_next_page:
  1246. old_start += PAGE_SIZE;
  1247. }
  1248. }
  1249. }
  1250. int __init page_in_phys_avail(unsigned long paddr)
  1251. {
  1252. int i;
  1253. paddr &= PAGE_MASK;
  1254. for (i = 0; i < pavail_rescan_ents; i++) {
  1255. unsigned long start, end;
  1256. start = pavail_rescan[i].phys_addr;
  1257. end = start + pavail_rescan[i].reg_size;
  1258. if (paddr >= start && paddr < end)
  1259. return 1;
  1260. }
  1261. if (paddr >= kern_base && paddr < (kern_base + kern_size))
  1262. return 1;
  1263. #ifdef CONFIG_BLK_DEV_INITRD
  1264. if (paddr >= __pa(initrd_start) &&
  1265. paddr < __pa(PAGE_ALIGN(initrd_end)))
  1266. return 1;
  1267. #endif
  1268. return 0;
  1269. }
  1270. void __init mem_init(void)
  1271. {
  1272. unsigned long codepages, datapages, initpages;
  1273. unsigned long addr, last;
  1274. int i;
  1275. i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
  1276. i += 1;
  1277. sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
  1278. if (sparc64_valid_addr_bitmap == NULL) {
  1279. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  1280. prom_halt();
  1281. }
  1282. memset(sparc64_valid_addr_bitmap, 0, i << 3);
  1283. addr = PAGE_OFFSET + kern_base;
  1284. last = PAGE_ALIGN(kern_size) + addr;
  1285. while (addr < last) {
  1286. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1287. addr += PAGE_SIZE;
  1288. }
  1289. taint_real_pages();
  1290. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1291. #ifdef CONFIG_DEBUG_BOOTMEM
  1292. prom_printf("mem_init: Calling free_all_bootmem().\n");
  1293. #endif
  1294. /* We subtract one to account for the mem_map_zero page
  1295. * allocated below.
  1296. */
  1297. totalram_pages = num_physpages = free_all_bootmem() - 1;
  1298. /*
  1299. * Set up the zero page, mark it reserved, so that page count
  1300. * is not manipulated when freeing the page from user ptes.
  1301. */
  1302. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1303. if (mem_map_zero == NULL) {
  1304. prom_printf("paging_init: Cannot alloc zero page.\n");
  1305. prom_halt();
  1306. }
  1307. SetPageReserved(mem_map_zero);
  1308. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1309. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1310. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1311. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1312. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1313. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1314. printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1315. nr_free_pages() << (PAGE_SHIFT-10),
  1316. codepages << (PAGE_SHIFT-10),
  1317. datapages << (PAGE_SHIFT-10),
  1318. initpages << (PAGE_SHIFT-10),
  1319. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1320. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1321. cheetah_ecache_flush_init();
  1322. }
  1323. void free_initmem(void)
  1324. {
  1325. unsigned long addr, initend;
  1326. /*
  1327. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1328. */
  1329. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1330. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1331. for (; addr < initend; addr += PAGE_SIZE) {
  1332. unsigned long page;
  1333. struct page *p;
  1334. page = (addr +
  1335. ((unsigned long) __va(kern_base)) -
  1336. ((unsigned long) KERNBASE));
  1337. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1338. p = virt_to_page(page);
  1339. ClearPageReserved(p);
  1340. init_page_count(p);
  1341. __free_page(p);
  1342. num_physpages++;
  1343. totalram_pages++;
  1344. }
  1345. }
  1346. #ifdef CONFIG_BLK_DEV_INITRD
  1347. void free_initrd_mem(unsigned long start, unsigned long end)
  1348. {
  1349. if (start < end)
  1350. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1351. for (; start < end; start += PAGE_SIZE) {
  1352. struct page *p = virt_to_page(start);
  1353. ClearPageReserved(p);
  1354. init_page_count(p);
  1355. __free_page(p);
  1356. num_physpages++;
  1357. totalram_pages++;
  1358. }
  1359. }
  1360. #endif
  1361. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1362. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1363. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1364. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1365. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1366. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1367. pgprot_t PAGE_KERNEL __read_mostly;
  1368. EXPORT_SYMBOL(PAGE_KERNEL);
  1369. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1370. pgprot_t PAGE_COPY __read_mostly;
  1371. pgprot_t PAGE_SHARED __read_mostly;
  1372. EXPORT_SYMBOL(PAGE_SHARED);
  1373. pgprot_t PAGE_EXEC __read_mostly;
  1374. unsigned long pg_iobits __read_mostly;
  1375. unsigned long _PAGE_IE __read_mostly;
  1376. EXPORT_SYMBOL(_PAGE_IE);
  1377. unsigned long _PAGE_E __read_mostly;
  1378. EXPORT_SYMBOL(_PAGE_E);
  1379. unsigned long _PAGE_CACHE __read_mostly;
  1380. EXPORT_SYMBOL(_PAGE_CACHE);
  1381. static void prot_init_common(unsigned long page_none,
  1382. unsigned long page_shared,
  1383. unsigned long page_copy,
  1384. unsigned long page_readonly,
  1385. unsigned long page_exec_bit)
  1386. {
  1387. PAGE_COPY = __pgprot(page_copy);
  1388. PAGE_SHARED = __pgprot(page_shared);
  1389. protection_map[0x0] = __pgprot(page_none);
  1390. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1391. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1392. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1393. protection_map[0x4] = __pgprot(page_readonly);
  1394. protection_map[0x5] = __pgprot(page_readonly);
  1395. protection_map[0x6] = __pgprot(page_copy);
  1396. protection_map[0x7] = __pgprot(page_copy);
  1397. protection_map[0x8] = __pgprot(page_none);
  1398. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1399. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1400. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1401. protection_map[0xc] = __pgprot(page_readonly);
  1402. protection_map[0xd] = __pgprot(page_readonly);
  1403. protection_map[0xe] = __pgprot(page_shared);
  1404. protection_map[0xf] = __pgprot(page_shared);
  1405. }
  1406. static void __init sun4u_pgprot_init(void)
  1407. {
  1408. unsigned long page_none, page_shared, page_copy, page_readonly;
  1409. unsigned long page_exec_bit;
  1410. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1411. _PAGE_CACHE_4U | _PAGE_P_4U |
  1412. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1413. _PAGE_EXEC_4U);
  1414. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1415. _PAGE_CACHE_4U | _PAGE_P_4U |
  1416. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1417. _PAGE_EXEC_4U | _PAGE_L_4U);
  1418. PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
  1419. _PAGE_IE = _PAGE_IE_4U;
  1420. _PAGE_E = _PAGE_E_4U;
  1421. _PAGE_CACHE = _PAGE_CACHE_4U;
  1422. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1423. __ACCESS_BITS_4U | _PAGE_E_4U);
  1424. #ifdef CONFIG_DEBUG_PAGEALLOC
  1425. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
  1426. 0xfffff80000000000;
  1427. #else
  1428. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1429. 0xfffff80000000000;
  1430. #endif
  1431. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1432. _PAGE_P_4U | _PAGE_W_4U);
  1433. /* XXX Should use 256MB on Panther. XXX */
  1434. kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
  1435. _PAGE_SZBITS = _PAGE_SZBITS_4U;
  1436. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1437. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1438. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1439. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1440. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1441. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1442. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1443. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1444. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1445. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1446. page_exec_bit = _PAGE_EXEC_4U;
  1447. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1448. page_exec_bit);
  1449. }
  1450. static void __init sun4v_pgprot_init(void)
  1451. {
  1452. unsigned long page_none, page_shared, page_copy, page_readonly;
  1453. unsigned long page_exec_bit;
  1454. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1455. _PAGE_CACHE_4V | _PAGE_P_4V |
  1456. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1457. _PAGE_EXEC_4V);
  1458. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1459. PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
  1460. _PAGE_IE = _PAGE_IE_4V;
  1461. _PAGE_E = _PAGE_E_4V;
  1462. _PAGE_CACHE = _PAGE_CACHE_4V;
  1463. #ifdef CONFIG_DEBUG_PAGEALLOC
  1464. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1465. 0xfffff80000000000;
  1466. #else
  1467. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  1468. 0xfffff80000000000;
  1469. #endif
  1470. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1471. _PAGE_P_4V | _PAGE_W_4V);
  1472. #ifdef CONFIG_DEBUG_PAGEALLOC
  1473. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1474. 0xfffff80000000000;
  1475. #else
  1476. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1477. 0xfffff80000000000;
  1478. #endif
  1479. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1480. _PAGE_P_4V | _PAGE_W_4V);
  1481. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  1482. __ACCESS_BITS_4V | _PAGE_E_4V);
  1483. _PAGE_SZBITS = _PAGE_SZBITS_4V;
  1484. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  1485. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  1486. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  1487. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  1488. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  1489. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1490. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  1491. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1492. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1493. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1494. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1495. page_exec_bit = _PAGE_EXEC_4V;
  1496. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1497. page_exec_bit);
  1498. }
  1499. unsigned long pte_sz_bits(unsigned long sz)
  1500. {
  1501. if (tlb_type == hypervisor) {
  1502. switch (sz) {
  1503. case 8 * 1024:
  1504. default:
  1505. return _PAGE_SZ8K_4V;
  1506. case 64 * 1024:
  1507. return _PAGE_SZ64K_4V;
  1508. case 512 * 1024:
  1509. return _PAGE_SZ512K_4V;
  1510. case 4 * 1024 * 1024:
  1511. return _PAGE_SZ4MB_4V;
  1512. };
  1513. } else {
  1514. switch (sz) {
  1515. case 8 * 1024:
  1516. default:
  1517. return _PAGE_SZ8K_4U;
  1518. case 64 * 1024:
  1519. return _PAGE_SZ64K_4U;
  1520. case 512 * 1024:
  1521. return _PAGE_SZ512K_4U;
  1522. case 4 * 1024 * 1024:
  1523. return _PAGE_SZ4MB_4U;
  1524. };
  1525. }
  1526. }
  1527. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  1528. {
  1529. pte_t pte;
  1530. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  1531. pte_val(pte) |= (((unsigned long)space) << 32);
  1532. pte_val(pte) |= pte_sz_bits(page_size);
  1533. return pte;
  1534. }
  1535. static unsigned long kern_large_tte(unsigned long paddr)
  1536. {
  1537. unsigned long val;
  1538. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1539. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  1540. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  1541. if (tlb_type == hypervisor)
  1542. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1543. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  1544. _PAGE_EXEC_4V | _PAGE_W_4V);
  1545. return val | paddr;
  1546. }
  1547. /* If not locked, zap it. */
  1548. void __flush_tlb_all(void)
  1549. {
  1550. unsigned long pstate;
  1551. int i;
  1552. __asm__ __volatile__("flushw\n\t"
  1553. "rdpr %%pstate, %0\n\t"
  1554. "wrpr %0, %1, %%pstate"
  1555. : "=r" (pstate)
  1556. : "i" (PSTATE_IE));
  1557. if (tlb_type == spitfire) {
  1558. for (i = 0; i < 64; i++) {
  1559. /* Spitfire Errata #32 workaround */
  1560. /* NOTE: Always runs on spitfire, so no
  1561. * cheetah+ page size encodings.
  1562. */
  1563. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1564. "flush %%g6"
  1565. : /* No outputs */
  1566. : "r" (0),
  1567. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1568. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  1569. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1570. "membar #Sync"
  1571. : /* no outputs */
  1572. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  1573. spitfire_put_dtlb_data(i, 0x0UL);
  1574. }
  1575. /* Spitfire Errata #32 workaround */
  1576. /* NOTE: Always runs on spitfire, so no
  1577. * cheetah+ page size encodings.
  1578. */
  1579. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1580. "flush %%g6"
  1581. : /* No outputs */
  1582. : "r" (0),
  1583. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1584. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  1585. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1586. "membar #Sync"
  1587. : /* no outputs */
  1588. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  1589. spitfire_put_itlb_data(i, 0x0UL);
  1590. }
  1591. }
  1592. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1593. cheetah_flush_dtlb_all();
  1594. cheetah_flush_itlb_all();
  1595. }
  1596. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  1597. : : "r" (pstate));
  1598. }
  1599. #ifdef CONFIG_MEMORY_HOTPLUG
  1600. void online_page(struct page *page)
  1601. {
  1602. ClearPageReserved(page);
  1603. init_page_count(page);
  1604. __free_page(page);
  1605. totalram_pages++;
  1606. num_physpages++;
  1607. }
  1608. int remove_memory(u64 start, u64 size)
  1609. {
  1610. return -EINVAL;
  1611. }
  1612. #endif /* CONFIG_MEMORY_HOTPLUG */