init.c 44 KB

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