init.c 49 KB

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