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 _GLOBAL(call_do_softirq)
39 stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
47 _GLOBAL(call_handle_irq)
51 stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r5)
60 * This returns the high 64 bits of the product of two 64-bit numbers.
72 1: beqlr cr1 /* all done if high part of A is 0 */
87 * sub_reloc_offset(x) returns x - reloc_offset().
89 _GLOBAL(sub_reloc_offset)
101 * reloc_got2 runs through the .got2 section adding an offset
106 lis r7,__got2_start@ha
107 addi r7,r7,__got2_start@l
109 addi r8,r8,__got2_end@l
129 * call_setup_cpu - call the setup_cpu function for this cpu
130 * r3 = data offset, r24 = cpu number
132 * Setup function is called with:
134 * r4 = ptr to CPU spec (relocated)
136 _GLOBAL(call_setup_cpu)
137 addis r4,r3,cur_cpu_spec@ha
138 addi r4,r4,cur_cpu_spec@l
141 lwz r5,CPU_SPEC_SETUP(r4)
148 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
150 /* This gets called by via-pmu.c to switch the PLL selection
151 * on 750fx CPU. This function should really be moved to some
152 * other place (as most of the cpufreq code in via-pmu
154 _GLOBAL(low_choose_750fx_pll)
160 /* If switching to PLL1, disable HID0:BTIC */
171 /* Calc new HID1 value */
172 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
173 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
174 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
178 /* Store new HID1 image */
179 rlwinm r6,r1,0,0,(31-THREAD_SHIFT)
182 addis r6,r6,nap_save_hid1@ha
183 stw r4,nap_save_hid1@l(r6)
185 /* If switching to PLL0, enable HID0:BTIC */
200 _GLOBAL(low_choose_7447a_dfs)
206 /* Calc new HID1 value */
208 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
218 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
221 * complement mask on the msr then "or" some values on.
222 * _nmask_and_or_msr(nmask, value_to_or)
224 _GLOBAL(_nmask_and_or_msr)
225 mfmsr r0 /* Get current msr */
226 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
227 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
228 SYNC /* Some chip revs have problems here... */
229 mtmsr r0 /* Update machine state */
236 * Do an IO access in real mode
254 * Do an IO access in real mode
271 #endif /* CONFIG_40x */
275 * Flush instruction cache.
276 * This is a no-op on the 601.
278 _GLOBAL(flush_instruction_cache)
279 #if defined(CONFIG_8xx)
282 mtspr SPRN_IC_CST, r5
283 #elif defined(CONFIG_4xx)
295 #elif CONFIG_FSL_BOOKE
298 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
299 /* msync; isync recommended here */
303 END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE)
305 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
309 rlwinm r3,r3,16,16,31
311 beqlr /* for 601, do nothing */
312 /* 603/604 processor - use invalidate-all bit in HID0 */
316 #endif /* CONFIG_8xx/4xx */
321 * Write any modified data cache blocks out to memory
322 * and invalidate the corresponding instruction cache blocks.
323 * This is a no-op on the 601.
325 * flush_icache_range(unsigned long start, unsigned long stop)
327 _KPROBE(__flush_icache_range)
329 blr /* for 601, do nothing */
330 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
331 li r5,L1_CACHE_BYTES-1
335 srwi. r4,r4,L1_CACHE_SHIFT
340 addi r3,r3,L1_CACHE_BYTES
342 sync /* wait for dcbst's to get to ram */
346 addi r6,r6,L1_CACHE_BYTES
349 /* Flash invalidate on 44x because we are passed kmapped addresses and
350 this doesn't work for userspace pages due to the virtually tagged
354 sync /* additional sync needed on g4 */
358 * Write any modified data cache blocks out to memory.
359 * Does not invalidate the corresponding cache lines (especially for
360 * any corresponding instruction cache).
362 * clean_dcache_range(unsigned long start, unsigned long stop)
364 _GLOBAL(clean_dcache_range)
365 li r5,L1_CACHE_BYTES-1
369 srwi. r4,r4,L1_CACHE_SHIFT
374 addi r3,r3,L1_CACHE_BYTES
376 sync /* wait for dcbst's to get to ram */
380 * Write any modified data cache blocks out to memory and invalidate them.
381 * Does not invalidate the corresponding instruction cache blocks.
383 * flush_dcache_range(unsigned long start, unsigned long stop)
385 _GLOBAL(flush_dcache_range)
386 li r5,L1_CACHE_BYTES-1
390 srwi. r4,r4,L1_CACHE_SHIFT
395 addi r3,r3,L1_CACHE_BYTES
397 sync /* wait for dcbst's to get to ram */
401 * Like above, but invalidate the D-cache. This is used by the 8xx
402 * to invalidate the cache so the PPC core doesn't get stale data
403 * from the CPM (no cache snooping here :-).
405 * invalidate_dcache_range(unsigned long start, unsigned long stop)
407 _GLOBAL(invalidate_dcache_range)
408 li r5,L1_CACHE_BYTES-1
412 srwi. r4,r4,L1_CACHE_SHIFT
417 addi r3,r3,L1_CACHE_BYTES
419 sync /* wait for dcbi's to get to ram */
423 * Flush a particular page from the data cache to RAM.
424 * Note: this is necessary because the instruction cache does *not*
425 * snoop from the data cache.
426 * This is a no-op on the 601 which has a unified cache.
428 * void __flush_dcache_icache(void *page)
430 _GLOBAL(__flush_dcache_icache)
433 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
434 rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
435 li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
438 0: dcbst 0,r3 /* Write line to ram */
439 addi r3,r3,L1_CACHE_BYTES
443 /* We don't flush the icache on 44x. Those have a virtual icache
444 * and we don't have access to the virtual address here (it's
445 * not the page vaddr but where it's mapped in user space). The
446 * flushing of the icache on these is handled elsewhere, when
447 * a change in the address space occurs, before returning to
450 BEGIN_MMU_FTR_SECTION
452 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x)
453 #endif /* CONFIG_44x */
456 addi r6,r6,L1_CACHE_BYTES
464 * Flush a particular page from the data cache to RAM, identified
465 * by its physical address. We turn off the MMU so we can just use
466 * the physical address (this may be a highmem page without a kernel
469 * void __flush_dcache_icache_phys(unsigned long physaddr)
471 _GLOBAL(__flush_dcache_icache_phys)
473 blr /* for 601, do nothing */
474 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
476 rlwinm r0,r10,0,28,26 /* clear DR */
479 rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
480 li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
483 0: dcbst 0,r3 /* Write line to ram */
484 addi r3,r3,L1_CACHE_BYTES
489 addi r6,r6,L1_CACHE_BYTES
492 mtmsr r10 /* restore DR */
495 #endif /* CONFIG_BOOKE */
498 * Clear pages using the dcbz instruction, which doesn't cause any
499 * memory traffic (except to write out any cache lines which get
500 * displaced). This only works on cacheable memory.
502 * void clear_pages(void *page, int order) ;
505 li r0,PAGE_SIZE/L1_CACHE_BYTES
509 addi r3,r3,L1_CACHE_BYTES
514 * Copy a whole page. We use the dcbz instruction on the destination
515 * to reduce memory traffic (it eliminates the unnecessary reads of
516 * the destination into cache). This requires that the destination
519 #define COPY_16_BYTES \
535 #if MAX_COPY_PREFETCH > 1
536 li r0,MAX_COPY_PREFETCH
540 addi r11,r11,L1_CACHE_BYTES
542 #else /* MAX_COPY_PREFETCH == 1 */
544 li r11,L1_CACHE_BYTES+4
545 #endif /* MAX_COPY_PREFETCH */
546 li r0,PAGE_SIZE/L1_CACHE_BYTES - MAX_COPY_PREFETCH
554 #if L1_CACHE_BYTES >= 32
556 #if L1_CACHE_BYTES >= 64
559 #if L1_CACHE_BYTES >= 128
569 crnot 4*cr0+eq,4*cr0+eq
570 li r0,MAX_COPY_PREFETCH
575 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
576 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
578 _GLOBAL(atomic_clear_mask)
585 _GLOBAL(atomic_set_mask)
594 * Extended precision shifts.
596 * Updated to be valid for shift counts from 0 to 63 inclusive.
599 * R3/R4 has 64 bit value
603 * ashrdi3: arithmetic right shift (sign propagation)
604 * lshrdi3: logical right shift
605 * ashldi3: left shift
609 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
610 addi r7,r5,32 # could be xori, or addi with -32
611 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
612 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
613 sraw r7,r3,r7 # t2 = MSW >> (count-32)
614 or r4,r4,r6 # LSW |= t1
615 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
616 sraw r3,r3,r5 # MSW = MSW >> count
617 or r4,r4,r7 # LSW |= t2
622 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
623 addi r7,r5,32 # could be xori, or addi with -32
624 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
625 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
626 or r3,r3,r6 # MSW |= t1
627 slw r4,r4,r5 # LSW = LSW << count
628 or r3,r3,r7 # MSW |= t2
633 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
634 addi r7,r5,32 # could be xori, or addi with -32
635 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
636 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
637 or r4,r4,r6 # LSW |= t1
638 srw r3,r3,r5 # MSW = MSW >> count
639 or r4,r4,r7 # LSW |= t2
643 * 64-bit comparison: __ucmpdi2(u64 a, u64 b)
644 * Returns 0 if a < b, 1 if a == b, 2 if a > b.
664 * Create a kernel thread
665 * kernel_thread(fn, arg, flags)
667 _GLOBAL(kernel_thread)
671 mr r30,r3 /* function */
672 mr r31,r4 /* argument */
673 ori r3,r5,CLONE_VM /* flags */
674 oris r3,r3,CLONE_UNTRACED>>16
675 li r4,0 /* new sp (unused) */
678 bns+ 1f /* did system call indicate error? */
679 neg r3,r3 /* if so, make return code negative */
680 1: cmpwi 0,r3,0 /* parent or child? */
681 bne 2f /* return if parent */
682 li r0,0 /* make top-level stack frame */
684 mtlr r30 /* fn addr in lr */
685 mr r3,r31 /* load arg and call fn */
688 li r0,__NR_exit /* exit if function returns */
697 * This routine is just here to keep GCC happy - sigh...
704 * Must be relocatable PIC code callable as a C function.
706 .globl relocate_new_kernel
709 /* r4 = reboot_code_buffer */
710 /* r5 = start_address */
712 #ifdef CONFIG_FSL_BOOKE
718 #define ENTRY_MAPPING_KEXEC_SETUP
719 #include "fsl_booke_entry_mapping.S"
720 #undef ENTRY_MAPPING_KEXEC_SETUP
731 * Set Machine Status Register to a known status,
732 * switch the MMU off and jump to 1: in a single step.
736 ori r8, r8, MSR_RI|MSR_ME
738 addi r8, r4, 1f - relocate_new_kernel
745 /* from this point address translation is turned off */
746 /* and interrupts are disabled */
748 /* set a new stack at the bottom of our page... */
749 /* (not really needed now) */
750 addi r1, r4, KEXEC_CONTROL_PAGE_SIZE - 8 /* for LR Save+Back Chain */
754 li r6, 0 /* checksum */
758 0: /* top, read another word for the indirection page */
762 /* is it a destination page? (r8) */
763 rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
766 rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
769 2: /* is it an indirection page? (r3) */
770 rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
773 rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
777 2: /* are we done? */
778 rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
782 2: /* is it a source page? (r9) */
783 rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
786 rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
793 lwzu r0, 4(r9) /* do the copy */
807 /* To be certain of avoiding problems with self-modifying code
808 * execute a serializing instruction here.
813 mfspr r3, SPRN_PIR /* current core we are running on */
814 mr r4, r5 /* load physical address of chunk called */
816 /* jump to the entry point, usually the setup routine */
822 relocate_new_kernel_end:
824 .globl relocate_new_kernel_size
825 relocate_new_kernel_size:
826 .long relocate_new_kernel_end - relocate_new_kernel