init.c 45 KB

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