init.c 49 KB

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