2 * This file contains miscellaneous low-level functions.
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
9 * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
10 * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
19 #include <linux/config.h>
20 #include <linux/sys.h>
21 #include <asm/unistd.h>
22 #include <asm/errno.h>
25 #include <asm/cache.h>
26 #include <asm/cputable.h>
28 #include <asm/ppc_asm.h>
29 #include <asm/thread_info.h>
30 #include <asm/asm-offsets.h>
31 #include <asm/processor.h>
32 #include <asm/kexec.h>
37 * This returns the high 64 bits of the product of two 64-bit numbers.
49 1: beqlr cr1 /* all done if high part of A is 0 */
64 * sub_reloc_offset(x) returns x - reloc_offset().
66 _GLOBAL(sub_reloc_offset)
78 * reloc_got2 runs through the .got2 section adding an offset
83 lis r7,__got2_start@ha
84 addi r7,r7,__got2_start@l
86 addi r8,r8,__got2_end@l
107 * called with r3 = data offset and r4 = CPU number
110 _GLOBAL(identify_cpu)
111 addis r8,r3,cpu_specs@ha
112 addi r8,r8,cpu_specs@l
115 lwz r5,CPU_SPEC_PVR_MASK(r8)
117 lwz r6,CPU_SPEC_PVR_VALUE(r8)
120 addi r8,r8,CPU_SPEC_ENTRY_SIZE
123 addis r6,r3,cur_cpu_spec@ha
124 addi r6,r6,cur_cpu_spec@l
130 * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
131 * and writes nop's over sections of code that don't apply for this cpu.
132 * r3 = data offset (not changed)
134 _GLOBAL(do_cpu_ftr_fixups)
135 /* Get CPU 0 features */
136 addis r6,r3,cur_cpu_spec@ha
137 addi r6,r6,cur_cpu_spec@l
140 lwz r4,CPU_SPEC_FEATURES(r4)
142 /* Get the fixup table */
143 addis r6,r3,__start___ftr_fixup@ha
144 addi r6,r6,__start___ftr_fixup@l
145 addis r7,r3,__stop___ftr_fixup@ha
146 addi r7,r7,__stop___ftr_fixup@l
152 lwz r8,-16(r6) /* mask */
154 lwz r9,-12(r6) /* value */
157 lwz r8,-8(r6) /* section begin */
158 lwz r9,-4(r6) /* section end */
161 /* write nops over the section of code */
162 /* todo: if large section, add a branch at the start of it */
166 lis r0,0x60000000@h /* nop */
168 andi. r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
170 dcbst 0,r8 /* suboptimal, but simpler */
175 sync /* additional sync needed on g4 */
180 * call_setup_cpu - call the setup_cpu function for this cpu
181 * r3 = data offset, r24 = cpu number
183 * Setup function is called with:
185 * r4 = ptr to CPU spec (relocated)
187 _GLOBAL(call_setup_cpu)
188 addis r4,r3,cur_cpu_spec@ha
189 addi r4,r4,cur_cpu_spec@l
192 lwz r5,CPU_SPEC_SETUP(r4)
199 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
201 /* This gets called by via-pmu.c to switch the PLL selection
202 * on 750fx CPU. This function should really be moved to some
203 * other place (as most of the cpufreq code in via-pmu
205 _GLOBAL(low_choose_750fx_pll)
211 /* If switching to PLL1, disable HID0:BTIC */
222 /* Calc new HID1 value */
223 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
224 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
225 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
229 /* Store new HID1 image */
233 addis r6,r6,nap_save_hid1@ha
234 stw r4,nap_save_hid1@l(r6)
236 /* If switching to PLL0, enable HID0:BTIC */
251 _GLOBAL(low_choose_7447a_dfs)
257 /* Calc new HID1 value */
259 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
269 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
272 * complement mask on the msr then "or" some values on.
273 * _nmask_and_or_msr(nmask, value_to_or)
275 _GLOBAL(_nmask_and_or_msr)
276 mfmsr r0 /* Get current msr */
277 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
278 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
279 SYNC /* Some chip revs have problems here... */
280 mtmsr r0 /* Update machine state */
289 #if defined(CONFIG_40x)
290 sync /* Flush to memory before changing mapping */
292 isync /* Flush shadow TLB */
293 #elif defined(CONFIG_44x)
297 /* Load high watermark */
298 lis r4,tlb_44x_hwater@ha
299 lwz r5,tlb_44x_hwater@l(r4)
301 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
307 #elif defined(CONFIG_FSL_BOOKE)
308 /* Invalidate all entries in TLB0 */
311 /* Invalidate all entries in TLB1 */
314 /* Invalidate all entries in TLB2 */
317 /* Invalidate all entries in TLB3 */
323 #endif /* CONFIG_SMP */
324 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
325 #if defined(CONFIG_SMP)
331 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
332 rlwinm r0,r0,0,28,26 /* clear DR */
336 lis r9,mmu_hash_lock@h
337 ori r9,r9,mmu_hash_lock@l
349 stw r0,0(r9) /* clear mmu_hash_lock */
353 #else /* CONFIG_SMP */
357 #endif /* CONFIG_SMP */
358 #endif /* ! defined(CONFIG_40x) */
362 * Flush MMU TLB for a particular address
365 #if defined(CONFIG_40x)
369 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
370 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
372 tlbwe r3, r3, TLB_TAG
375 #elif defined(CONFIG_44x)
377 mfspr r5,SPRN_PID /* Get PID */
378 rlwimi r4,r5,0,24,31 /* Set TID */
384 /* There are only 64 TLB entries, so r3 < 64,
385 * which means bit 22, is clear. Since 22 is
386 * the V bit in the TLB_PAGEID, loading this
387 * value will invalidate the TLB entry.
389 tlbwe r3, r3, PPC44x_TLB_PAGEID
392 #elif defined(CONFIG_FSL_BOOKE)
393 rlwinm r4, r3, 0, 0, 19
394 ori r5, r4, 0x08 /* TLBSEL = 1 */
395 ori r6, r4, 0x10 /* TLBSEL = 2 */
396 ori r7, r4, 0x18 /* TLBSEL = 3 */
402 #if defined(CONFIG_SMP)
404 #endif /* CONFIG_SMP */
405 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
406 #if defined(CONFIG_SMP)
412 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
413 rlwinm r0,r0,0,28,26 /* clear DR */
417 lis r9,mmu_hash_lock@h
418 ori r9,r9,mmu_hash_lock@l
430 stw r0,0(r9) /* clear mmu_hash_lock */
434 #else /* CONFIG_SMP */
437 #endif /* CONFIG_SMP */
438 #endif /* ! CONFIG_40x */
442 * Flush instruction cache.
443 * This is a no-op on the 601.
445 _GLOBAL(flush_instruction_cache)
446 #if defined(CONFIG_8xx)
449 mtspr SPRN_IC_CST, r5
450 #elif defined(CONFIG_4xx)
462 #elif CONFIG_FSL_BOOKE
465 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
466 /* msync; isync recommended here */
470 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
472 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
476 rlwinm r3,r3,16,16,31
478 beqlr /* for 601, do nothing */
479 /* 603/604 processor - use invalidate-all bit in HID0 */
483 #endif /* CONFIG_8xx/4xx */
488 * Write any modified data cache blocks out to memory
489 * and invalidate the corresponding instruction cache blocks.
490 * This is a no-op on the 601.
492 * flush_icache_range(unsigned long start, unsigned long stop)
494 _GLOBAL(__flush_icache_range)
496 blr /* for 601, do nothing */
497 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
498 li r5,L1_CACHE_BYTES-1
502 srwi. r4,r4,L1_CACHE_SHIFT
507 addi r3,r3,L1_CACHE_BYTES
509 sync /* wait for dcbst's to get to ram */
512 addi r6,r6,L1_CACHE_BYTES
514 sync /* additional sync needed on g4 */
518 * Write any modified data cache blocks out to memory.
519 * Does not invalidate the corresponding cache lines (especially for
520 * any corresponding instruction cache).
522 * clean_dcache_range(unsigned long start, unsigned long stop)
524 _GLOBAL(clean_dcache_range)
525 li r5,L1_CACHE_BYTES-1
529 srwi. r4,r4,L1_CACHE_SHIFT
534 addi r3,r3,L1_CACHE_BYTES
536 sync /* wait for dcbst's to get to ram */
540 * Write any modified data cache blocks out to memory and invalidate them.
541 * Does not invalidate the corresponding instruction cache blocks.
543 * flush_dcache_range(unsigned long start, unsigned long stop)
545 _GLOBAL(flush_dcache_range)
546 li r5,L1_CACHE_BYTES-1
550 srwi. r4,r4,L1_CACHE_SHIFT
555 addi r3,r3,L1_CACHE_BYTES
557 sync /* wait for dcbst's to get to ram */
561 * Like above, but invalidate the D-cache. This is used by the 8xx
562 * to invalidate the cache so the PPC core doesn't get stale data
563 * from the CPM (no cache snooping here :-).
565 * invalidate_dcache_range(unsigned long start, unsigned long stop)
567 _GLOBAL(invalidate_dcache_range)
568 li r5,L1_CACHE_BYTES-1
572 srwi. r4,r4,L1_CACHE_SHIFT
577 addi r3,r3,L1_CACHE_BYTES
579 sync /* wait for dcbi's to get to ram */
583 * Flush a particular page from the data cache to RAM.
584 * Note: this is necessary because the instruction cache does *not*
585 * snoop from the data cache.
586 * This is a no-op on the 601 which has a unified cache.
588 * void __flush_dcache_icache(void *page)
590 _GLOBAL(__flush_dcache_icache)
592 blr /* for 601, do nothing */
593 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
594 rlwinm r3,r3,0,0,19 /* Get page base address */
595 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
598 0: dcbst 0,r3 /* Write line to ram */
599 addi r3,r3,L1_CACHE_BYTES
604 addi r6,r6,L1_CACHE_BYTES
611 * Flush a particular page from the data cache to RAM, identified
612 * by its physical address. We turn off the MMU so we can just use
613 * the physical address (this may be a highmem page without a kernel
616 * void __flush_dcache_icache_phys(unsigned long physaddr)
618 _GLOBAL(__flush_dcache_icache_phys)
620 blr /* for 601, do nothing */
621 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
623 rlwinm r0,r10,0,28,26 /* clear DR */
626 rlwinm r3,r3,0,0,19 /* Get page base address */
627 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
630 0: dcbst 0,r3 /* Write line to ram */
631 addi r3,r3,L1_CACHE_BYTES
636 addi r6,r6,L1_CACHE_BYTES
639 mtmsr r10 /* restore DR */
644 * Clear pages using the dcbz instruction, which doesn't cause any
645 * memory traffic (except to write out any cache lines which get
646 * displaced). This only works on cacheable memory.
648 * void clear_pages(void *page, int order) ;
651 li r0,4096/L1_CACHE_BYTES
663 addi r3,r3,L1_CACHE_BYTES
668 * Copy a whole page. We use the dcbz instruction on the destination
669 * to reduce memory traffic (it eliminates the unnecessary reads of
670 * the destination into cache). This requires that the destination
673 #define COPY_16_BYTES \
688 /* don't use prefetch on 8xx */
689 li r0,4096/L1_CACHE_BYTES
695 #else /* not 8xx, we can prefetch */
698 #if MAX_COPY_PREFETCH > 1
699 li r0,MAX_COPY_PREFETCH
703 addi r11,r11,L1_CACHE_BYTES
705 #else /* MAX_COPY_PREFETCH == 1 */
707 li r11,L1_CACHE_BYTES+4
708 #endif /* MAX_COPY_PREFETCH */
709 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
717 #if L1_CACHE_BYTES >= 32
719 #if L1_CACHE_BYTES >= 64
722 #if L1_CACHE_BYTES >= 128
732 crnot 4*cr0+eq,4*cr0+eq
733 li r0,MAX_COPY_PREFETCH
736 #endif /* CONFIG_8xx */
739 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
740 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
742 _GLOBAL(atomic_clear_mask)
749 _GLOBAL(atomic_set_mask)
758 * Extended precision shifts.
760 * Updated to be valid for shift counts from 0 to 63 inclusive.
763 * R3/R4 has 64 bit value
767 * ashrdi3: arithmetic right shift (sign propagation)
768 * lshrdi3: logical right shift
769 * ashldi3: left shift
773 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
774 addi r7,r5,32 # could be xori, or addi with -32
775 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
776 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
777 sraw r7,r3,r7 # t2 = MSW >> (count-32)
778 or r4,r4,r6 # LSW |= t1
779 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
780 sraw r3,r3,r5 # MSW = MSW >> count
781 or r4,r4,r7 # LSW |= t2
786 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
787 addi r7,r5,32 # could be xori, or addi with -32
788 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
789 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
790 or r3,r3,r6 # MSW |= t1
791 slw r4,r4,r5 # LSW = LSW << count
792 or r3,r3,r7 # MSW |= t2
797 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
798 addi r7,r5,32 # could be xori, or addi with -32
799 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
800 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
801 or r4,r4,r6 # LSW |= t1
802 srw r3,r3,r5 # MSW = MSW >> count
803 or r4,r4,r7 # LSW |= t2
813 mr r3,r1 /* Close enough */
817 * Create a kernel thread
818 * kernel_thread(fn, arg, flags)
820 _GLOBAL(kernel_thread)
824 mr r30,r3 /* function */
825 mr r31,r4 /* argument */
826 ori r3,r5,CLONE_VM /* flags */
827 oris r3,r3,CLONE_UNTRACED>>16
828 li r4,0 /* new sp (unused) */
831 cmpwi 0,r3,0 /* parent or child? */
832 bne 1f /* return if parent */
833 li r0,0 /* make top-level stack frame */
835 mtlr r30 /* fn addr in lr */
836 mr r3,r31 /* load arg and call fn */
839 li r0,__NR_exit /* exit if function returns */
855 * This routine is just here to keep GCC happy - sigh...
862 * Must be relocatable PIC code callable as a C function.
864 .globl relocate_new_kernel
867 /* r4 = reboot_code_buffer */
868 /* r5 = start_address */
873 * Set Machine Status Register to a known status,
874 * switch the MMU off and jump to 1: in a single step.
878 ori r8, r8, MSR_RI|MSR_ME
880 addi r8, r4, 1f - relocate_new_kernel
886 /* from this point address translation is turned off */
887 /* and interrupts are disabled */
889 /* set a new stack at the bottom of our page... */
890 /* (not really needed now) */
891 addi r1, r4, KEXEC_CONTROL_CODE_SIZE - 8 /* for LR Save+Back Chain */
895 li r6, 0 /* checksum */
899 0: /* top, read another word for the indirection page */
903 /* is it a destination page? (r8) */
904 rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
907 rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
910 2: /* is it an indirection page? (r3) */
911 rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
914 rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
918 2: /* are we done? */
919 rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
923 2: /* is it a source page? (r9) */
924 rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
927 rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
934 lwzu r0, 4(r9) /* do the copy */
948 /* To be certain of avoiding problems with self-modifying code
949 * execute a serializing instruction here.
954 /* jump to the entry point, usually the setup routine */
960 relocate_new_kernel_end:
962 .globl relocate_new_kernel_size
963 relocate_new_kernel_size:
964 .long relocate_new_kernel_end - relocate_new_kernel