init.c 49 KB

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