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 #ifdef CONFIG_IRQSTACKS
37 _GLOBAL(call_do_softirq)
40 stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
48 _GLOBAL(call_handle_irq)
52 stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r5)
59 #endif /* CONFIG_IRQSTACKS */
62 * This returns the high 64 bits of the product of two 64-bit numbers.
74 1: beqlr cr1 /* all done if high part of A is 0 */
89 * sub_reloc_offset(x) returns x - reloc_offset().
91 _GLOBAL(sub_reloc_offset)
103 * reloc_got2 runs through the .got2 section adding an offset
108 lis r7,__got2_start@ha
109 addi r7,r7,__got2_start@l
111 addi r8,r8,__got2_end@l
131 * call_setup_cpu - call the setup_cpu function for this cpu
132 * r3 = data offset, r24 = cpu number
134 * Setup function is called with:
136 * r4 = ptr to CPU spec (relocated)
138 _GLOBAL(call_setup_cpu)
139 addis r4,r3,cur_cpu_spec@ha
140 addi r4,r4,cur_cpu_spec@l
143 lwz r5,CPU_SPEC_SETUP(r4)
150 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
152 /* This gets called by via-pmu.c to switch the PLL selection
153 * on 750fx CPU. This function should really be moved to some
154 * other place (as most of the cpufreq code in via-pmu
156 _GLOBAL(low_choose_750fx_pll)
162 /* If switching to PLL1, disable HID0:BTIC */
173 /* Calc new HID1 value */
174 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
175 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
176 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
180 /* Store new HID1 image */
181 rlwinm r6,r1,0,0,(31-THREAD_SHIFT)
184 addis r6,r6,nap_save_hid1@ha
185 stw r4,nap_save_hid1@l(r6)
187 /* If switching to PLL0, enable HID0:BTIC */
202 _GLOBAL(low_choose_7447a_dfs)
208 /* Calc new HID1 value */
210 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
220 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
223 * complement mask on the msr then "or" some values on.
224 * _nmask_and_or_msr(nmask, value_to_or)
226 _GLOBAL(_nmask_and_or_msr)
227 mfmsr r0 /* Get current msr */
228 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
229 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
230 SYNC /* Some chip revs have problems here... */
231 mtmsr r0 /* Update machine state */
238 * Do an IO access in real mode
256 * Do an IO access in real mode
273 #endif /* CONFIG_40x */
277 * Flush instruction cache.
278 * This is a no-op on the 601.
280 _GLOBAL(flush_instruction_cache)
281 #if defined(CONFIG_8xx)
284 mtspr SPRN_IC_CST, r5
285 #elif defined(CONFIG_4xx)
297 #elif CONFIG_FSL_BOOKE
300 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
301 /* msync; isync recommended here */
305 END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE)
307 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
311 rlwinm r3,r3,16,16,31
313 beqlr /* for 601, do nothing */
314 /* 603/604 processor - use invalidate-all bit in HID0 */
318 #endif /* CONFIG_8xx/4xx */
323 * Write any modified data cache blocks out to memory
324 * and invalidate the corresponding instruction cache blocks.
325 * This is a no-op on the 601.
327 * flush_icache_range(unsigned long start, unsigned long stop)
329 _KPROBE(__flush_icache_range)
331 blr /* for 601, do nothing */
332 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
333 li r5,L1_CACHE_BYTES-1
337 srwi. r4,r4,L1_CACHE_SHIFT
342 addi r3,r3,L1_CACHE_BYTES
344 sync /* wait for dcbst's to get to ram */
348 addi r6,r6,L1_CACHE_BYTES
351 /* Flash invalidate on 44x because we are passed kmapped addresses and
352 this doesn't work for userspace pages due to the virtually tagged
356 sync /* additional sync needed on g4 */
360 * Write any modified data cache blocks out to memory.
361 * Does not invalidate the corresponding cache lines (especially for
362 * any corresponding instruction cache).
364 * clean_dcache_range(unsigned long start, unsigned long stop)
366 _GLOBAL(clean_dcache_range)
367 li r5,L1_CACHE_BYTES-1
371 srwi. r4,r4,L1_CACHE_SHIFT
376 addi r3,r3,L1_CACHE_BYTES
378 sync /* wait for dcbst's to get to ram */
382 * Write any modified data cache blocks out to memory and invalidate them.
383 * Does not invalidate the corresponding instruction cache blocks.
385 * flush_dcache_range(unsigned long start, unsigned long stop)
387 _GLOBAL(flush_dcache_range)
388 li r5,L1_CACHE_BYTES-1
392 srwi. r4,r4,L1_CACHE_SHIFT
397 addi r3,r3,L1_CACHE_BYTES
399 sync /* wait for dcbst's to get to ram */
403 * Like above, but invalidate the D-cache. This is used by the 8xx
404 * to invalidate the cache so the PPC core doesn't get stale data
405 * from the CPM (no cache snooping here :-).
407 * invalidate_dcache_range(unsigned long start, unsigned long stop)
409 _GLOBAL(invalidate_dcache_range)
410 li r5,L1_CACHE_BYTES-1
414 srwi. r4,r4,L1_CACHE_SHIFT
419 addi r3,r3,L1_CACHE_BYTES
421 sync /* wait for dcbi's to get to ram */
425 * Flush a particular page from the data cache to RAM.
426 * Note: this is necessary because the instruction cache does *not*
427 * snoop from the data cache.
428 * This is a no-op on the 601 which has a unified cache.
430 * void __flush_dcache_icache(void *page)
432 _GLOBAL(__flush_dcache_icache)
435 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
436 rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
437 li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
440 0: dcbst 0,r3 /* Write line to ram */
441 addi r3,r3,L1_CACHE_BYTES
445 /* We don't flush the icache on 44x. Those have a virtual icache
446 * and we don't have access to the virtual address here (it's
447 * not the page vaddr but where it's mapped in user space). The
448 * flushing of the icache on these is handled elsewhere, when
449 * a change in the address space occurs, before returning to
454 addi r6,r6,L1_CACHE_BYTES
458 #endif /* CONFIG_44x */
462 * Flush a particular page from the data cache to RAM, identified
463 * by its physical address. We turn off the MMU so we can just use
464 * the physical address (this may be a highmem page without a kernel
467 * void __flush_dcache_icache_phys(unsigned long physaddr)
469 _GLOBAL(__flush_dcache_icache_phys)
471 blr /* for 601, do nothing */
472 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
474 rlwinm r0,r10,0,28,26 /* clear DR */
477 rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
478 li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
481 0: dcbst 0,r3 /* Write line to ram */
482 addi r3,r3,L1_CACHE_BYTES
487 addi r6,r6,L1_CACHE_BYTES
490 mtmsr r10 /* restore DR */
495 * Clear pages using the dcbz instruction, which doesn't cause any
496 * memory traffic (except to write out any cache lines which get
497 * displaced). This only works on cacheable memory.
499 * void clear_pages(void *page, int order) ;
502 li r0,PAGE_SIZE/L1_CACHE_BYTES
514 addi r3,r3,L1_CACHE_BYTES
519 * Copy a whole page. We use the dcbz instruction on the destination
520 * to reduce memory traffic (it eliminates the unnecessary reads of
521 * the destination into cache). This requires that the destination
524 #define COPY_16_BYTES \
539 /* don't use prefetch on 8xx */
540 li r0,4096/L1_CACHE_BYTES
546 #else /* not 8xx, we can prefetch */
549 #if MAX_COPY_PREFETCH > 1
550 li r0,MAX_COPY_PREFETCH
554 addi r11,r11,L1_CACHE_BYTES
556 #else /* MAX_COPY_PREFETCH == 1 */
558 li r11,L1_CACHE_BYTES+4
559 #endif /* MAX_COPY_PREFETCH */
560 li r0,PAGE_SIZE/L1_CACHE_BYTES - MAX_COPY_PREFETCH
568 #if L1_CACHE_BYTES >= 32
570 #if L1_CACHE_BYTES >= 64
573 #if L1_CACHE_BYTES >= 128
583 crnot 4*cr0+eq,4*cr0+eq
584 li r0,MAX_COPY_PREFETCH
587 #endif /* CONFIG_8xx */
590 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
591 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
593 _GLOBAL(atomic_clear_mask)
600 _GLOBAL(atomic_set_mask)
609 * Extended precision shifts.
611 * Updated to be valid for shift counts from 0 to 63 inclusive.
614 * R3/R4 has 64 bit value
618 * ashrdi3: arithmetic right shift (sign propagation)
619 * lshrdi3: logical right shift
620 * ashldi3: left shift
624 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
625 addi r7,r5,32 # could be xori, or addi with -32
626 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
627 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
628 sraw r7,r3,r7 # t2 = MSW >> (count-32)
629 or r4,r4,r6 # LSW |= t1
630 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
631 sraw r3,r3,r5 # MSW = MSW >> count
632 or r4,r4,r7 # LSW |= t2
637 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
638 addi r7,r5,32 # could be xori, or addi with -32
639 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
640 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
641 or r3,r3,r6 # MSW |= t1
642 slw r4,r4,r5 # LSW = LSW << count
643 or r3,r3,r7 # MSW |= t2
648 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
649 addi r7,r5,32 # could be xori, or addi with -32
650 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
651 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
652 or r4,r4,r6 # LSW |= t1
653 srw r3,r3,r5 # MSW = MSW >> count
654 or r4,r4,r7 # LSW |= t2
658 * 64-bit comparison: __ucmpdi2(u64 a, u64 b)
659 * Returns 0 if a < b, 1 if a == b, 2 if a > b.
679 * Create a kernel thread
680 * kernel_thread(fn, arg, flags)
682 _GLOBAL(kernel_thread)
686 mr r30,r3 /* function */
687 mr r31,r4 /* argument */
688 ori r3,r5,CLONE_VM /* flags */
689 oris r3,r3,CLONE_UNTRACED>>16
690 li r4,0 /* new sp (unused) */
693 bns+ 1f /* did system call indicate error? */
694 neg r3,r3 /* if so, make return code negative */
695 1: cmpwi 0,r3,0 /* parent or child? */
696 bne 2f /* return if parent */
697 li r0,0 /* make top-level stack frame */
699 mtlr r30 /* fn addr in lr */
700 mr r3,r31 /* load arg and call fn */
703 li r0,__NR_exit /* exit if function returns */
712 * This routine is just here to keep GCC happy - sigh...
719 * Must be relocatable PIC code callable as a C function.
721 .globl relocate_new_kernel
724 /* r4 = reboot_code_buffer */
725 /* r5 = start_address */
730 * Set Machine Status Register to a known status,
731 * switch the MMU off and jump to 1: in a single step.
735 ori r8, r8, MSR_RI|MSR_ME
737 addi r8, r4, 1f - relocate_new_kernel
743 /* from this point address translation is turned off */
744 /* and interrupts are disabled */
746 /* set a new stack at the bottom of our page... */
747 /* (not really needed now) */
748 addi r1, r4, KEXEC_CONTROL_PAGE_SIZE - 8 /* for LR Save+Back Chain */
752 li r6, 0 /* checksum */
756 0: /* top, read another word for the indirection page */
760 /* is it a destination page? (r8) */
761 rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
764 rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
767 2: /* is it an indirection page? (r3) */
768 rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
771 rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
775 2: /* are we done? */
776 rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
780 2: /* is it a source page? (r9) */
781 rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
784 rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
791 lwzu r0, 4(r9) /* do the copy */
805 /* To be certain of avoiding problems with self-modifying code
806 * execute a serializing instruction here.
811 /* jump to the entry point, usually the setup routine */
817 relocate_new_kernel_end:
819 .globl relocate_new_kernel_size
820 relocate_new_kernel_size:
821 .long relocate_new_kernel_end - relocate_new_kernel