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/sys.h>
20 #include <asm/unistd.h>
21 #include <asm/errno.h>
24 #include <asm/cache.h>
25 #include <asm/cputable.h>
27 #include <asm/ppc_asm.h>
28 #include <asm/thread_info.h>
29 #include <asm/asm-offsets.h>
30 #include <asm/processor.h>
31 #include <asm/kexec.h>
36 * This returns the high 64 bits of the product of two 64-bit numbers.
48 1: beqlr cr1 /* all done if high part of A is 0 */
63 * sub_reloc_offset(x) returns x - reloc_offset().
65 _GLOBAL(sub_reloc_offset)
77 * reloc_got2 runs through the .got2 section adding an offset
82 lis r7,__got2_start@ha
83 addi r7,r7,__got2_start@l
85 addi r8,r8,__got2_end@l
106 * called with r3 = data offset and r4 = CPU number
109 _GLOBAL(identify_cpu)
110 addis r8,r3,cpu_specs@ha
111 addi r8,r8,cpu_specs@l
114 lwz r5,CPU_SPEC_PVR_MASK(r8)
116 lwz r6,CPU_SPEC_PVR_VALUE(r8)
119 addi r8,r8,CPU_SPEC_ENTRY_SIZE
122 addis r6,r3,cur_cpu_spec@ha
123 addi r6,r6,cur_cpu_spec@l
129 * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
130 * and writes nop's over sections of code that don't apply for this cpu.
131 * r3 = data offset (not changed)
133 _GLOBAL(do_cpu_ftr_fixups)
134 /* Get CPU 0 features */
135 addis r6,r3,cur_cpu_spec@ha
136 addi r6,r6,cur_cpu_spec@l
139 lwz r4,CPU_SPEC_FEATURES(r4)
141 /* Get the fixup table */
142 addis r6,r3,__start___ftr_fixup@ha
143 addi r6,r6,__start___ftr_fixup@l
144 addis r7,r3,__stop___ftr_fixup@ha
145 addi r7,r7,__stop___ftr_fixup@l
151 lwz r8,-16(r6) /* mask */
153 lwz r9,-12(r6) /* value */
156 lwz r8,-8(r6) /* section begin */
157 lwz r9,-4(r6) /* section end */
160 /* write nops over the section of code */
161 /* todo: if large section, add a branch at the start of it */
165 lis r0,0x60000000@h /* nop */
167 andi. r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
169 dcbst 0,r8 /* suboptimal, but simpler */
174 sync /* additional sync needed on g4 */
179 * call_setup_cpu - call the setup_cpu function for this cpu
180 * r3 = data offset, r24 = cpu number
182 * Setup function is called with:
184 * r4 = ptr to CPU spec (relocated)
186 _GLOBAL(call_setup_cpu)
187 addis r4,r3,cur_cpu_spec@ha
188 addi r4,r4,cur_cpu_spec@l
191 lwz r5,CPU_SPEC_SETUP(r4)
198 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
200 /* This gets called by via-pmu.c to switch the PLL selection
201 * on 750fx CPU. This function should really be moved to some
202 * other place (as most of the cpufreq code in via-pmu
204 _GLOBAL(low_choose_750fx_pll)
210 /* If switching to PLL1, disable HID0:BTIC */
221 /* Calc new HID1 value */
222 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
223 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
224 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
228 /* Store new HID1 image */
232 addis r6,r6,nap_save_hid1@ha
233 stw r4,nap_save_hid1@l(r6)
235 /* If switching to PLL0, enable HID0:BTIC */
250 _GLOBAL(low_choose_7447a_dfs)
256 /* Calc new HID1 value */
258 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
268 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
271 * complement mask on the msr then "or" some values on.
272 * _nmask_and_or_msr(nmask, value_to_or)
274 _GLOBAL(_nmask_and_or_msr)
275 mfmsr r0 /* Get current msr */
276 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
277 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
278 SYNC /* Some chip revs have problems here... */
279 mtmsr r0 /* Update machine state */
288 #if defined(CONFIG_40x)
289 sync /* Flush to memory before changing mapping */
291 isync /* Flush shadow TLB */
292 #elif defined(CONFIG_44x)
296 /* Load high watermark */
297 lis r4,tlb_44x_hwater@ha
298 lwz r5,tlb_44x_hwater@l(r4)
300 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
306 #elif defined(CONFIG_FSL_BOOKE)
307 /* Invalidate all entries in TLB0 */
310 /* Invalidate all entries in TLB1 */
313 /* Invalidate all entries in TLB2 */
316 /* Invalidate all entries in TLB3 */
322 #endif /* CONFIG_SMP */
323 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
324 #if defined(CONFIG_SMP)
330 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
331 rlwinm r0,r0,0,28,26 /* clear DR */
335 lis r9,mmu_hash_lock@h
336 ori r9,r9,mmu_hash_lock@l
348 stw r0,0(r9) /* clear mmu_hash_lock */
352 #else /* CONFIG_SMP */
356 #endif /* CONFIG_SMP */
357 #endif /* ! defined(CONFIG_40x) */
361 * Flush MMU TLB for a particular address
364 #if defined(CONFIG_40x)
368 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
369 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
371 tlbwe r3, r3, TLB_TAG
374 #elif defined(CONFIG_44x)
376 mfspr r5,SPRN_PID /* Get PID */
377 rlwimi r4,r5,0,24,31 /* Set TID */
383 /* There are only 64 TLB entries, so r3 < 64,
384 * which means bit 22, is clear. Since 22 is
385 * the V bit in the TLB_PAGEID, loading this
386 * value will invalidate the TLB entry.
388 tlbwe r3, r3, PPC44x_TLB_PAGEID
391 #elif defined(CONFIG_FSL_BOOKE)
392 rlwinm r4, r3, 0, 0, 19
393 ori r5, r4, 0x08 /* TLBSEL = 1 */
394 ori r6, r4, 0x10 /* TLBSEL = 2 */
395 ori r7, r4, 0x18 /* TLBSEL = 3 */
401 #if defined(CONFIG_SMP)
403 #endif /* CONFIG_SMP */
404 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
405 #if defined(CONFIG_SMP)
411 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
412 rlwinm r0,r0,0,28,26 /* clear DR */
416 lis r9,mmu_hash_lock@h
417 ori r9,r9,mmu_hash_lock@l
429 stw r0,0(r9) /* clear mmu_hash_lock */
433 #else /* CONFIG_SMP */
436 #endif /* CONFIG_SMP */
437 #endif /* ! CONFIG_40x */
441 * Flush instruction cache.
442 * This is a no-op on the 601.
444 _GLOBAL(flush_instruction_cache)
445 #if defined(CONFIG_8xx)
448 mtspr SPRN_IC_CST, r5
449 #elif defined(CONFIG_4xx)
461 #elif CONFIG_FSL_BOOKE
464 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
465 /* msync; isync recommended here */
469 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
471 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
475 rlwinm r3,r3,16,16,31
477 beqlr /* for 601, do nothing */
478 /* 603/604 processor - use invalidate-all bit in HID0 */
482 #endif /* CONFIG_8xx/4xx */
487 * Write any modified data cache blocks out to memory
488 * and invalidate the corresponding instruction cache blocks.
489 * This is a no-op on the 601.
491 * flush_icache_range(unsigned long start, unsigned long stop)
493 _GLOBAL(__flush_icache_range)
495 blr /* for 601, do nothing */
496 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
497 li r5,L1_CACHE_BYTES-1
501 srwi. r4,r4,L1_CACHE_SHIFT
506 addi r3,r3,L1_CACHE_BYTES
508 sync /* wait for dcbst's to get to ram */
511 addi r6,r6,L1_CACHE_BYTES
513 sync /* additional sync needed on g4 */
517 * Write any modified data cache blocks out to memory.
518 * Does not invalidate the corresponding cache lines (especially for
519 * any corresponding instruction cache).
521 * clean_dcache_range(unsigned long start, unsigned long stop)
523 _GLOBAL(clean_dcache_range)
524 li r5,L1_CACHE_BYTES-1
528 srwi. r4,r4,L1_CACHE_SHIFT
533 addi r3,r3,L1_CACHE_BYTES
535 sync /* wait for dcbst's to get to ram */
539 * Write any modified data cache blocks out to memory and invalidate them.
540 * Does not invalidate the corresponding instruction cache blocks.
542 * flush_dcache_range(unsigned long start, unsigned long stop)
544 _GLOBAL(flush_dcache_range)
545 li r5,L1_CACHE_BYTES-1
549 srwi. r4,r4,L1_CACHE_SHIFT
554 addi r3,r3,L1_CACHE_BYTES
556 sync /* wait for dcbst's to get to ram */
560 * Like above, but invalidate the D-cache. This is used by the 8xx
561 * to invalidate the cache so the PPC core doesn't get stale data
562 * from the CPM (no cache snooping here :-).
564 * invalidate_dcache_range(unsigned long start, unsigned long stop)
566 _GLOBAL(invalidate_dcache_range)
567 li r5,L1_CACHE_BYTES-1
571 srwi. r4,r4,L1_CACHE_SHIFT
576 addi r3,r3,L1_CACHE_BYTES
578 sync /* wait for dcbi's to get to ram */
582 * Flush a particular page from the data cache to RAM.
583 * Note: this is necessary because the instruction cache does *not*
584 * snoop from the data cache.
585 * This is a no-op on the 601 which has a unified cache.
587 * void __flush_dcache_icache(void *page)
589 _GLOBAL(__flush_dcache_icache)
591 blr /* for 601, do nothing */
592 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
593 rlwinm r3,r3,0,0,19 /* Get page base address */
594 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
597 0: dcbst 0,r3 /* Write line to ram */
598 addi r3,r3,L1_CACHE_BYTES
603 addi r6,r6,L1_CACHE_BYTES
610 * Flush a particular page from the data cache to RAM, identified
611 * by its physical address. We turn off the MMU so we can just use
612 * the physical address (this may be a highmem page without a kernel
615 * void __flush_dcache_icache_phys(unsigned long physaddr)
617 _GLOBAL(__flush_dcache_icache_phys)
619 blr /* for 601, do nothing */
620 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
622 rlwinm r0,r10,0,28,26 /* clear DR */
625 rlwinm r3,r3,0,0,19 /* Get page base address */
626 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
629 0: dcbst 0,r3 /* Write line to ram */
630 addi r3,r3,L1_CACHE_BYTES
635 addi r6,r6,L1_CACHE_BYTES
638 mtmsr r10 /* restore DR */
643 * Clear pages using the dcbz instruction, which doesn't cause any
644 * memory traffic (except to write out any cache lines which get
645 * displaced). This only works on cacheable memory.
647 * void clear_pages(void *page, int order) ;
650 li r0,4096/L1_CACHE_BYTES
662 addi r3,r3,L1_CACHE_BYTES
667 * Copy a whole page. We use the dcbz instruction on the destination
668 * to reduce memory traffic (it eliminates the unnecessary reads of
669 * the destination into cache). This requires that the destination
672 #define COPY_16_BYTES \
687 /* don't use prefetch on 8xx */
688 li r0,4096/L1_CACHE_BYTES
694 #else /* not 8xx, we can prefetch */
697 #if MAX_COPY_PREFETCH > 1
698 li r0,MAX_COPY_PREFETCH
702 addi r11,r11,L1_CACHE_BYTES
704 #else /* MAX_COPY_PREFETCH == 1 */
706 li r11,L1_CACHE_BYTES+4
707 #endif /* MAX_COPY_PREFETCH */
708 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
716 #if L1_CACHE_BYTES >= 32
718 #if L1_CACHE_BYTES >= 64
721 #if L1_CACHE_BYTES >= 128
731 crnot 4*cr0+eq,4*cr0+eq
732 li r0,MAX_COPY_PREFETCH
735 #endif /* CONFIG_8xx */
738 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
739 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
741 _GLOBAL(atomic_clear_mask)
748 _GLOBAL(atomic_set_mask)
757 * Extended precision shifts.
759 * Updated to be valid for shift counts from 0 to 63 inclusive.
762 * R3/R4 has 64 bit value
766 * ashrdi3: arithmetic right shift (sign propagation)
767 * lshrdi3: logical right shift
768 * ashldi3: left shift
772 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
773 addi r7,r5,32 # could be xori, or addi with -32
774 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
775 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
776 sraw r7,r3,r7 # t2 = MSW >> (count-32)
777 or r4,r4,r6 # LSW |= t1
778 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
779 sraw r3,r3,r5 # MSW = MSW >> count
780 or r4,r4,r7 # LSW |= t2
785 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
786 addi r7,r5,32 # could be xori, or addi with -32
787 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
788 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
789 or r3,r3,r6 # MSW |= t1
790 slw r4,r4,r5 # LSW = LSW << count
791 or r3,r3,r7 # MSW |= t2
796 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
797 addi r7,r5,32 # could be xori, or addi with -32
798 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
799 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
800 or r4,r4,r6 # LSW |= t1
801 srw r3,r3,r5 # MSW = MSW >> count
802 or r4,r4,r7 # LSW |= t2
812 mr r3,r1 /* Close enough */
816 * Create a kernel thread
817 * kernel_thread(fn, arg, flags)
819 _GLOBAL(kernel_thread)
823 mr r30,r3 /* function */
824 mr r31,r4 /* argument */
825 ori r3,r5,CLONE_VM /* flags */
826 oris r3,r3,CLONE_UNTRACED>>16
827 li r4,0 /* new sp (unused) */
830 cmpwi 0,r3,0 /* parent or child? */
831 bne 1f /* return if parent */
832 li r0,0 /* make top-level stack frame */
834 mtlr r30 /* fn addr in lr */
835 mr r3,r31 /* load arg and call fn */
838 li r0,__NR_exit /* exit if function returns */
854 * This routine is just here to keep GCC happy - sigh...
861 * Must be relocatable PIC code callable as a C function.
863 .globl relocate_new_kernel
866 /* r4 = reboot_code_buffer */
867 /* r5 = start_address */
872 * Set Machine Status Register to a known status,
873 * switch the MMU off and jump to 1: in a single step.
877 ori r8, r8, MSR_RI|MSR_ME
879 addi r8, r4, 1f - relocate_new_kernel
885 /* from this point address translation is turned off */
886 /* and interrupts are disabled */
888 /* set a new stack at the bottom of our page... */
889 /* (not really needed now) */
890 addi r1, r4, KEXEC_CONTROL_CODE_SIZE - 8 /* for LR Save+Back Chain */
894 li r6, 0 /* checksum */
898 0: /* top, read another word for the indirection page */
902 /* is it a destination page? (r8) */
903 rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
906 rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
909 2: /* is it an indirection page? (r3) */
910 rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
913 rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
917 2: /* are we done? */
918 rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
922 2: /* is it a source page? (r9) */
923 rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
926 rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
933 lwzu r0, 4(r9) /* do the copy */
947 /* To be certain of avoiding problems with self-modifying code
948 * execute a serializing instruction here.
953 /* jump to the entry point, usually the setup routine */
959 relocate_new_kernel_end:
961 .globl relocate_new_kernel_size
962 relocate_new_kernel_size:
963 .long relocate_new_kernel_end - relocate_new_kernel