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)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
15 #include <linux/config.h>
16 #include <linux/sys.h>
17 #include <asm/unistd.h>
18 #include <asm/errno.h>
21 #include <asm/cache.h>
22 #include <asm/cputable.h>
24 #include <asm/ppc_asm.h>
25 #include <asm/thread_info.h>
26 #include <asm/asm-offsets.h>
31 * This returns the high 64 bits of the product of two 64-bit numbers.
43 1: beqlr cr1 /* all done if high part of A is 0 */
58 * Returns (address we're running at) - (address we were linked at)
59 * for use before the text and data are mapped to KERNELBASE.
71 * add_reloc_offset(x) returns x + reloc_offset().
73 _GLOBAL(add_reloc_offset)
84 * sub_reloc_offset(x) returns x - reloc_offset().
86 _GLOBAL(sub_reloc_offset)
98 * reloc_got2 runs through the .got2 section adding an offset
103 lis r7,__got2_start@ha
104 addi r7,r7,__got2_start@l
106 addi r8,r8,__got2_end@l
127 * called with r3 = data offset and r4 = CPU number
130 _GLOBAL(identify_cpu)
131 addis r8,r3,cpu_specs@ha
132 addi r8,r8,cpu_specs@l
135 lwz r5,CPU_SPEC_PVR_MASK(r8)
137 lwz r6,CPU_SPEC_PVR_VALUE(r8)
140 addi r8,r8,CPU_SPEC_ENTRY_SIZE
143 addis r6,r3,cur_cpu_spec@ha
144 addi r6,r6,cur_cpu_spec@l
150 * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
151 * and writes nop's over sections of code that don't apply for this cpu.
152 * r3 = data offset (not changed)
154 _GLOBAL(do_cpu_ftr_fixups)
155 /* Get CPU 0 features */
156 addis r6,r3,cur_cpu_spec@ha
157 addi r6,r6,cur_cpu_spec@l
160 lwz r4,CPU_SPEC_FEATURES(r4)
162 /* Get the fixup table */
163 addis r6,r3,__start___ftr_fixup@ha
164 addi r6,r6,__start___ftr_fixup@l
165 addis r7,r3,__stop___ftr_fixup@ha
166 addi r7,r7,__stop___ftr_fixup@l
172 lwz r8,-16(r6) /* mask */
174 lwz r9,-12(r6) /* value */
177 lwz r8,-8(r6) /* section begin */
178 lwz r9,-4(r6) /* section end */
181 /* write nops over the section of code */
182 /* todo: if large section, add a branch at the start of it */
186 lis r0,0x60000000@h /* nop */
188 andi. r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
190 dcbst 0,r8 /* suboptimal, but simpler */
195 sync /* additional sync needed on g4 */
200 * call_setup_cpu - call the setup_cpu function for this cpu
201 * r3 = data offset, r24 = cpu number
203 * Setup function is called with:
205 * r4 = ptr to CPU spec (relocated)
207 _GLOBAL(call_setup_cpu)
208 addis r4,r3,cur_cpu_spec@ha
209 addi r4,r4,cur_cpu_spec@l
212 lwz r5,CPU_SPEC_SETUP(r4)
219 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
221 /* This gets called by via-pmu.c to switch the PLL selection
222 * on 750fx CPU. This function should really be moved to some
223 * other place (as most of the cpufreq code in via-pmu
225 _GLOBAL(low_choose_750fx_pll)
231 /* If switching to PLL1, disable HID0:BTIC */
242 /* Calc new HID1 value */
243 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
244 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
245 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
249 /* Store new HID1 image */
253 addis r6,r6,nap_save_hid1@ha
254 stw r4,nap_save_hid1@l(r6)
256 /* If switching to PLL0, enable HID0:BTIC */
271 _GLOBAL(low_choose_7447a_dfs)
277 /* Calc new HID1 value */
279 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
289 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
292 * complement mask on the msr then "or" some values on.
293 * _nmask_and_or_msr(nmask, value_to_or)
295 _GLOBAL(_nmask_and_or_msr)
296 mfmsr r0 /* Get current msr */
297 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
298 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
299 SYNC /* Some chip revs have problems here... */
300 mtmsr r0 /* Update machine state */
309 #if defined(CONFIG_40x)
310 sync /* Flush to memory before changing mapping */
312 isync /* Flush shadow TLB */
313 #elif defined(CONFIG_44x)
317 /* Load high watermark */
318 lis r4,tlb_44x_hwater@ha
319 lwz r5,tlb_44x_hwater@l(r4)
321 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
327 #elif defined(CONFIG_FSL_BOOKE)
328 /* Invalidate all entries in TLB0 */
331 /* Invalidate all entries in TLB1 */
334 /* Invalidate all entries in TLB2 */
337 /* Invalidate all entries in TLB3 */
343 #endif /* CONFIG_SMP */
344 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
345 #if defined(CONFIG_SMP)
351 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
352 rlwinm r0,r0,0,28,26 /* clear DR */
356 lis r9,mmu_hash_lock@h
357 ori r9,r9,mmu_hash_lock@l
369 stw r0,0(r9) /* clear mmu_hash_lock */
373 #else /* CONFIG_SMP */
377 #endif /* CONFIG_SMP */
378 #endif /* ! defined(CONFIG_40x) */
382 * Flush MMU TLB for a particular address
385 #if defined(CONFIG_40x)
389 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
390 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
392 tlbwe r3, r3, TLB_TAG
395 #elif defined(CONFIG_44x)
397 mfspr r5,SPRN_PID /* Get PID */
398 rlwimi r4,r5,0,24,31 /* Set TID */
404 /* There are only 64 TLB entries, so r3 < 64,
405 * which means bit 22, is clear. Since 22 is
406 * the V bit in the TLB_PAGEID, loading this
407 * value will invalidate the TLB entry.
409 tlbwe r3, r3, PPC44x_TLB_PAGEID
412 #elif defined(CONFIG_FSL_BOOKE)
413 rlwinm r4, r3, 0, 0, 19
414 ori r5, r4, 0x08 /* TLBSEL = 1 */
415 ori r6, r4, 0x10 /* TLBSEL = 2 */
416 ori r7, r4, 0x18 /* TLBSEL = 3 */
422 #if defined(CONFIG_SMP)
424 #endif /* CONFIG_SMP */
425 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
426 #if defined(CONFIG_SMP)
432 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
433 rlwinm r0,r0,0,28,26 /* clear DR */
437 lis r9,mmu_hash_lock@h
438 ori r9,r9,mmu_hash_lock@l
450 stw r0,0(r9) /* clear mmu_hash_lock */
454 #else /* CONFIG_SMP */
457 #endif /* CONFIG_SMP */
458 #endif /* ! CONFIG_40x */
462 * Flush instruction cache.
463 * This is a no-op on the 601.
465 _GLOBAL(flush_instruction_cache)
466 #if defined(CONFIG_8xx)
469 mtspr SPRN_IC_CST, r5
470 #elif defined(CONFIG_4xx)
482 #elif CONFIG_FSL_BOOKE
485 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
486 /* msync; isync recommended here */
490 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
492 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
496 rlwinm r3,r3,16,16,31
498 beqlr /* for 601, do nothing */
499 /* 603/604 processor - use invalidate-all bit in HID0 */
503 #endif /* CONFIG_8xx/4xx */
508 * Write any modified data cache blocks out to memory
509 * and invalidate the corresponding instruction cache blocks.
510 * This is a no-op on the 601.
512 * flush_icache_range(unsigned long start, unsigned long stop)
514 _GLOBAL(__flush_icache_range)
516 blr /* for 601, do nothing */
517 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
518 li r5,L1_CACHE_BYTES-1
522 srwi. r4,r4,L1_CACHE_SHIFT
527 addi r3,r3,L1_CACHE_BYTES
529 sync /* wait for dcbst's to get to ram */
532 addi r6,r6,L1_CACHE_BYTES
534 sync /* additional sync needed on g4 */
538 * Write any modified data cache blocks out to memory.
539 * Does not invalidate the corresponding cache lines (especially for
540 * any corresponding instruction cache).
542 * clean_dcache_range(unsigned long start, unsigned long stop)
544 _GLOBAL(clean_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 * Write any modified data cache blocks out to memory and invalidate them.
561 * Does not invalidate the corresponding instruction cache blocks.
563 * flush_dcache_range(unsigned long start, unsigned long stop)
565 _GLOBAL(flush_dcache_range)
566 li r5,L1_CACHE_BYTES-1
570 srwi. r4,r4,L1_CACHE_SHIFT
575 addi r3,r3,L1_CACHE_BYTES
577 sync /* wait for dcbst's to get to ram */
581 * Like above, but invalidate the D-cache. This is used by the 8xx
582 * to invalidate the cache so the PPC core doesn't get stale data
583 * from the CPM (no cache snooping here :-).
585 * invalidate_dcache_range(unsigned long start, unsigned long stop)
587 _GLOBAL(invalidate_dcache_range)
588 li r5,L1_CACHE_BYTES-1
592 srwi. r4,r4,L1_CACHE_SHIFT
597 addi r3,r3,L1_CACHE_BYTES
599 sync /* wait for dcbi's to get to ram */
603 * Flush a particular page from the data cache to RAM.
604 * Note: this is necessary because the instruction cache does *not*
605 * snoop from the data cache.
606 * This is a no-op on the 601 which has a unified cache.
608 * void __flush_dcache_icache(void *page)
610 _GLOBAL(__flush_dcache_icache)
612 blr /* for 601, do nothing */
613 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
614 rlwinm r3,r3,0,0,19 /* Get page base address */
615 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
618 0: dcbst 0,r3 /* Write line to ram */
619 addi r3,r3,L1_CACHE_BYTES
624 addi r6,r6,L1_CACHE_BYTES
631 * Flush a particular page from the data cache to RAM, identified
632 * by its physical address. We turn off the MMU so we can just use
633 * the physical address (this may be a highmem page without a kernel
636 * void __flush_dcache_icache_phys(unsigned long physaddr)
638 _GLOBAL(__flush_dcache_icache_phys)
640 blr /* for 601, do nothing */
641 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
643 rlwinm r0,r10,0,28,26 /* clear DR */
646 rlwinm r3,r3,0,0,19 /* Get page base address */
647 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
650 0: dcbst 0,r3 /* Write line to ram */
651 addi r3,r3,L1_CACHE_BYTES
656 addi r6,r6,L1_CACHE_BYTES
659 mtmsr r10 /* restore DR */
664 * Clear pages using the dcbz instruction, which doesn't cause any
665 * memory traffic (except to write out any cache lines which get
666 * displaced). This only works on cacheable memory.
668 * void clear_pages(void *page, int order) ;
671 li r0,4096/L1_CACHE_BYTES
683 addi r3,r3,L1_CACHE_BYTES
688 * Copy a whole page. We use the dcbz instruction on the destination
689 * to reduce memory traffic (it eliminates the unnecessary reads of
690 * the destination into cache). This requires that the destination
693 #define COPY_16_BYTES \
708 /* don't use prefetch on 8xx */
709 li r0,4096/L1_CACHE_BYTES
715 #else /* not 8xx, we can prefetch */
718 #if MAX_COPY_PREFETCH > 1
719 li r0,MAX_COPY_PREFETCH
723 addi r11,r11,L1_CACHE_BYTES
725 #else /* MAX_COPY_PREFETCH == 1 */
727 li r11,L1_CACHE_BYTES+4
728 #endif /* MAX_COPY_PREFETCH */
729 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
737 #if L1_CACHE_BYTES >= 32
739 #if L1_CACHE_BYTES >= 64
742 #if L1_CACHE_BYTES >= 128
752 crnot 4*cr0+eq,4*cr0+eq
753 li r0,MAX_COPY_PREFETCH
756 #endif /* CONFIG_8xx */
759 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
760 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
762 _GLOBAL(atomic_clear_mask)
769 _GLOBAL(atomic_set_mask)
778 * I/O string operations
780 * insb(port, buf, len)
781 * outsb(port, buf, len)
782 * insw(port, buf, len)
783 * outsw(port, buf, len)
784 * insl(port, buf, len)
785 * outsl(port, buf, len)
786 * insw_ns(port, buf, len)
787 * outsw_ns(port, buf, len)
788 * insl_ns(port, buf, len)
789 * outsl_ns(port, buf, len)
791 * The *_ns versions don't do byte-swapping.
859 _GLOBAL(__ide_mm_insw)
871 _GLOBAL(__ide_mm_outsw)
883 _GLOBAL(__ide_mm_insl)
895 _GLOBAL(__ide_mm_outsl)
908 * Extended precision shifts.
910 * Updated to be valid for shift counts from 0 to 63 inclusive.
913 * R3/R4 has 64 bit value
917 * ashrdi3: arithmetic right shift (sign propagation)
918 * lshrdi3: logical right shift
919 * ashldi3: left shift
923 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
924 addi r7,r5,32 # could be xori, or addi with -32
925 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
926 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
927 sraw r7,r3,r7 # t2 = MSW >> (count-32)
928 or r4,r4,r6 # LSW |= t1
929 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
930 sraw r3,r3,r5 # MSW = MSW >> count
931 or r4,r4,r7 # LSW |= t2
936 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
937 addi r7,r5,32 # could be xori, or addi with -32
938 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
939 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
940 or r3,r3,r6 # MSW |= t1
941 slw r4,r4,r5 # LSW = LSW << count
942 or r3,r3,r7 # MSW |= t2
947 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
948 addi r7,r5,32 # could be xori, or addi with -32
949 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
950 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
951 or r4,r4,r6 # LSW |= t1
952 srw r3,r3,r5 # MSW = MSW >> count
953 or r4,r4,r7 # LSW |= t2
963 mr r3,r1 /* Close enough */
967 * Create a kernel thread
968 * kernel_thread(fn, arg, flags)
970 _GLOBAL(kernel_thread)
974 mr r30,r3 /* function */
975 mr r31,r4 /* argument */
976 ori r3,r5,CLONE_VM /* flags */
977 oris r3,r3,CLONE_UNTRACED>>16
978 li r4,0 /* new sp (unused) */
981 cmpwi 0,r3,0 /* parent or child? */
982 bne 1f /* return if parent */
983 li r0,0 /* make top-level stack frame */
985 mtlr r30 /* fn addr in lr */
986 mr r3,r31 /* load arg and call fn */
989 li r0,__NR_exit /* exit if function returns */
1005 * This routine is just here to keep GCC happy - sigh...