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
105 * call_setup_cpu - call the setup_cpu function for this cpu
106 * r3 = data offset, r24 = cpu number
108 * Setup function is called with:
110 * r4 = ptr to CPU spec (relocated)
112 _GLOBAL(call_setup_cpu)
113 addis r4,r3,cur_cpu_spec@ha
114 addi r4,r4,cur_cpu_spec@l
117 lwz r5,CPU_SPEC_SETUP(r4)
124 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
126 /* This gets called by via-pmu.c to switch the PLL selection
127 * on 750fx CPU. This function should really be moved to some
128 * other place (as most of the cpufreq code in via-pmu
130 _GLOBAL(low_choose_750fx_pll)
136 /* If switching to PLL1, disable HID0:BTIC */
147 /* Calc new HID1 value */
148 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
149 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
150 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
154 /* Store new HID1 image */
158 addis r6,r6,nap_save_hid1@ha
159 stw r4,nap_save_hid1@l(r6)
161 /* If switching to PLL0, enable HID0:BTIC */
176 _GLOBAL(low_choose_7447a_dfs)
182 /* Calc new HID1 value */
184 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
194 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
197 * complement mask on the msr then "or" some values on.
198 * _nmask_and_or_msr(nmask, value_to_or)
200 _GLOBAL(_nmask_and_or_msr)
201 mfmsr r0 /* Get current msr */
202 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
203 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
204 SYNC /* Some chip revs have problems here... */
205 mtmsr r0 /* Update machine state */
212 * Do an IO access in real mode
230 * Do an IO access in real mode
247 #endif /* CONFIG_40x */
253 #if defined(CONFIG_40x)
254 sync /* Flush to memory before changing mapping */
256 isync /* Flush shadow TLB */
257 #elif defined(CONFIG_44x)
261 /* Load high watermark */
262 lis r4,tlb_44x_hwater@ha
263 lwz r5,tlb_44x_hwater@l(r4)
265 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
271 #elif defined(CONFIG_FSL_BOOKE)
272 /* Invalidate all entries in TLB0 */
275 /* Invalidate all entries in TLB1 */
281 #endif /* CONFIG_SMP */
282 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
283 #if defined(CONFIG_SMP)
289 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
290 rlwinm r0,r0,0,28,26 /* clear DR */
294 lis r9,mmu_hash_lock@h
295 ori r9,r9,mmu_hash_lock@l
307 stw r0,0(r9) /* clear mmu_hash_lock */
311 #else /* CONFIG_SMP */
315 #endif /* CONFIG_SMP */
316 #endif /* ! defined(CONFIG_40x) */
320 * Flush MMU TLB for a particular address
323 #if defined(CONFIG_40x)
324 /* We run the search with interrupts disabled because we have to change
325 * the PID and I don't want to preempt when that happens.
336 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
337 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
339 tlbwe r3, r3, TLB_TAG
343 #elif defined(CONFIG_44x)
345 rlwimi r5,r4,0,24,31 /* Set TID */
347 /* We have to run the search with interrupts disabled, even critical
348 * and debug interrupts (in fact the only critical exceptions we have
349 * are debug and machine check). Otherwise an interrupt which causes
350 * a TLB miss can clobber the MMUCR between the mtspr and the tlbsx. */
352 lis r6,(MSR_EE|MSR_CE|MSR_ME|MSR_DE)@ha
353 addi r6,r6,(MSR_EE|MSR_CE|MSR_ME|MSR_DE)@l
361 /* There are only 64 TLB entries, so r3 < 64,
362 * which means bit 22, is clear. Since 22 is
363 * the V bit in the TLB_PAGEID, loading this
364 * value will invalidate the TLB entry.
366 tlbwe r3, r3, PPC44x_TLB_PAGEID
369 #elif defined(CONFIG_FSL_BOOKE)
370 rlwinm r4, r3, 0, 0, 19
371 ori r5, r4, 0x08 /* TLBSEL = 1 */
375 #if defined(CONFIG_SMP)
377 #endif /* CONFIG_SMP */
378 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
379 #if defined(CONFIG_SMP)
385 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
386 rlwinm r0,r0,0,28,26 /* clear DR */
390 lis r9,mmu_hash_lock@h
391 ori r9,r9,mmu_hash_lock@l
403 stw r0,0(r9) /* clear mmu_hash_lock */
407 #else /* CONFIG_SMP */
410 #endif /* CONFIG_SMP */
411 #endif /* ! CONFIG_40x */
415 * Flush instruction cache.
416 * This is a no-op on the 601.
418 _GLOBAL(flush_instruction_cache)
419 #if defined(CONFIG_8xx)
422 mtspr SPRN_IC_CST, r5
423 #elif defined(CONFIG_4xx)
435 #elif CONFIG_FSL_BOOKE
438 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
439 /* msync; isync recommended here */
443 END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE)
445 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
449 rlwinm r3,r3,16,16,31
451 beqlr /* for 601, do nothing */
452 /* 603/604 processor - use invalidate-all bit in HID0 */
456 #endif /* CONFIG_8xx/4xx */
461 * Write any modified data cache blocks out to memory
462 * and invalidate the corresponding instruction cache blocks.
463 * This is a no-op on the 601.
465 * flush_icache_range(unsigned long start, unsigned long stop)
467 _GLOBAL(__flush_icache_range)
469 blr /* for 601, do nothing */
470 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
471 li r5,L1_CACHE_BYTES-1
475 srwi. r4,r4,L1_CACHE_SHIFT
480 addi r3,r3,L1_CACHE_BYTES
482 sync /* wait for dcbst's to get to ram */
485 addi r6,r6,L1_CACHE_BYTES
487 sync /* additional sync needed on g4 */
491 * Write any modified data cache blocks out to memory.
492 * Does not invalidate the corresponding cache lines (especially for
493 * any corresponding instruction cache).
495 * clean_dcache_range(unsigned long start, unsigned long stop)
497 _GLOBAL(clean_dcache_range)
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 */
513 * Write any modified data cache blocks out to memory and invalidate them.
514 * Does not invalidate the corresponding instruction cache blocks.
516 * flush_dcache_range(unsigned long start, unsigned long stop)
518 _GLOBAL(flush_dcache_range)
519 li r5,L1_CACHE_BYTES-1
523 srwi. r4,r4,L1_CACHE_SHIFT
528 addi r3,r3,L1_CACHE_BYTES
530 sync /* wait for dcbst's to get to ram */
534 * Like above, but invalidate the D-cache. This is used by the 8xx
535 * to invalidate the cache so the PPC core doesn't get stale data
536 * from the CPM (no cache snooping here :-).
538 * invalidate_dcache_range(unsigned long start, unsigned long stop)
540 _GLOBAL(invalidate_dcache_range)
541 li r5,L1_CACHE_BYTES-1
545 srwi. r4,r4,L1_CACHE_SHIFT
550 addi r3,r3,L1_CACHE_BYTES
552 sync /* wait for dcbi's to get to ram */
556 * Flush a particular page from the data cache to RAM.
557 * Note: this is necessary because the instruction cache does *not*
558 * snoop from the data cache.
559 * This is a no-op on the 601 which has a unified cache.
561 * void __flush_dcache_icache(void *page)
563 _GLOBAL(__flush_dcache_icache)
566 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
567 rlwinm r3,r3,0,0,19 /* Get page base address */
568 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
571 0: dcbst 0,r3 /* Write line to ram */
572 addi r3,r3,L1_CACHE_BYTES
576 /* We don't flush the icache on 44x. Those have a virtual icache
577 * and we don't have access to the virtual address here (it's
578 * not the page vaddr but where it's mapped in user space). The
579 * flushing of the icache on these is handled elsewhere, when
580 * a change in the address space occurs, before returning to
585 addi r6,r6,L1_CACHE_BYTES
589 #endif /* CONFIG_44x */
593 * Flush a particular page from the data cache to RAM, identified
594 * by its physical address. We turn off the MMU so we can just use
595 * the physical address (this may be a highmem page without a kernel
598 * void __flush_dcache_icache_phys(unsigned long physaddr)
600 _GLOBAL(__flush_dcache_icache_phys)
602 blr /* for 601, do nothing */
603 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
605 rlwinm r0,r10,0,28,26 /* clear DR */
608 rlwinm r3,r3,0,0,19 /* Get page base address */
609 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
612 0: dcbst 0,r3 /* Write line to ram */
613 addi r3,r3,L1_CACHE_BYTES
618 addi r6,r6,L1_CACHE_BYTES
621 mtmsr r10 /* restore DR */
626 * Clear pages using the dcbz instruction, which doesn't cause any
627 * memory traffic (except to write out any cache lines which get
628 * displaced). This only works on cacheable memory.
630 * void clear_pages(void *page, int order) ;
633 li r0,4096/L1_CACHE_BYTES
645 addi r3,r3,L1_CACHE_BYTES
650 * Copy a whole page. We use the dcbz instruction on the destination
651 * to reduce memory traffic (it eliminates the unnecessary reads of
652 * the destination into cache). This requires that the destination
655 #define COPY_16_BYTES \
670 /* don't use prefetch on 8xx */
671 li r0,4096/L1_CACHE_BYTES
677 #else /* not 8xx, we can prefetch */
680 #if MAX_COPY_PREFETCH > 1
681 li r0,MAX_COPY_PREFETCH
685 addi r11,r11,L1_CACHE_BYTES
687 #else /* MAX_COPY_PREFETCH == 1 */
689 li r11,L1_CACHE_BYTES+4
690 #endif /* MAX_COPY_PREFETCH */
691 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
699 #if L1_CACHE_BYTES >= 32
701 #if L1_CACHE_BYTES >= 64
704 #if L1_CACHE_BYTES >= 128
714 crnot 4*cr0+eq,4*cr0+eq
715 li r0,MAX_COPY_PREFETCH
718 #endif /* CONFIG_8xx */
721 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
722 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
724 _GLOBAL(atomic_clear_mask)
731 _GLOBAL(atomic_set_mask)
740 * Extended precision shifts.
742 * Updated to be valid for shift counts from 0 to 63 inclusive.
745 * R3/R4 has 64 bit value
749 * ashrdi3: arithmetic right shift (sign propagation)
750 * lshrdi3: logical right shift
751 * ashldi3: left shift
755 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
756 addi r7,r5,32 # could be xori, or addi with -32
757 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
758 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
759 sraw r7,r3,r7 # t2 = MSW >> (count-32)
760 or r4,r4,r6 # LSW |= t1
761 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
762 sraw r3,r3,r5 # MSW = MSW >> count
763 or r4,r4,r7 # LSW |= t2
768 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
769 addi r7,r5,32 # could be xori, or addi with -32
770 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
771 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
772 or r3,r3,r6 # MSW |= t1
773 slw r4,r4,r5 # LSW = LSW << count
774 or r3,r3,r7 # MSW |= t2
779 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
780 addi r7,r5,32 # could be xori, or addi with -32
781 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
782 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
783 or r4,r4,r6 # LSW |= t1
784 srw r3,r3,r5 # MSW = MSW >> count
785 or r4,r4,r7 # LSW |= t2
795 * Create a kernel thread
796 * kernel_thread(fn, arg, flags)
798 _GLOBAL(kernel_thread)
802 mr r30,r3 /* function */
803 mr r31,r4 /* argument */
804 ori r3,r5,CLONE_VM /* flags */
805 oris r3,r3,CLONE_UNTRACED>>16
806 li r4,0 /* new sp (unused) */
809 cmpwi 0,r3,0 /* parent or child? */
810 bne 1f /* return if parent */
811 li r0,0 /* make top-level stack frame */
813 mtlr r30 /* fn addr in lr */
814 mr r3,r31 /* load arg and call fn */
817 li r0,__NR_exit /* exit if function returns */
826 * This routine is just here to keep GCC happy - sigh...
833 * Must be relocatable PIC code callable as a C function.
835 .globl relocate_new_kernel
838 /* r4 = reboot_code_buffer */
839 /* r5 = start_address */
844 * Set Machine Status Register to a known status,
845 * switch the MMU off and jump to 1: in a single step.
849 ori r8, r8, MSR_RI|MSR_ME
851 addi r8, r4, 1f - relocate_new_kernel
857 /* from this point address translation is turned off */
858 /* and interrupts are disabled */
860 /* set a new stack at the bottom of our page... */
861 /* (not really needed now) */
862 addi r1, r4, KEXEC_CONTROL_CODE_SIZE - 8 /* for LR Save+Back Chain */
866 li r6, 0 /* checksum */
870 0: /* top, read another word for the indirection page */
874 /* is it a destination page? (r8) */
875 rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
878 rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
881 2: /* is it an indirection page? (r3) */
882 rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
885 rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
889 2: /* are we done? */
890 rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
894 2: /* is it a source page? (r9) */
895 rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
898 rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
905 lwzu r0, 4(r9) /* do the copy */
919 /* To be certain of avoiding problems with self-modifying code
920 * execute a serializing instruction here.
925 /* jump to the entry point, usually the setup routine */
931 relocate_new_kernel_end:
933 .globl relocate_new_kernel_size
934 relocate_new_kernel_size:
935 .long relocate_new_kernel_end - relocate_new_kernel