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/sys.h>
16 #include <asm/unistd.h>
17 #include <asm/errno.h>
18 #include <asm/processor.h>
20 #include <asm/cache.h>
21 #include <asm/cputable.h>
23 #include <asm/ppc_asm.h>
24 #include <asm/thread_info.h>
25 #include <asm/asm-offsets.h>
28 #define ISYNC_8xx isync
43 * Returns (address we're running at) - (address we were linked at)
44 * for use before the text and data are mapped to KERNELBASE.
57 * add_reloc_offset(x) returns x + reloc_offset().
59 _GLOBAL(add_reloc_offset)
71 * sub_reloc_offset(x) returns x - reloc_offset().
73 _GLOBAL(sub_reloc_offset)
85 * reloc_got2 runs through the .got2 section adding an offset
90 lis r7,__got2_start@ha
91 addi r7,r7,__got2_start@l
93 addi r8,r8,__got2_end@l
113 * call_setup_cpu - call the setup_cpu function for this cpu
114 * r3 = data offset, r24 = cpu number
116 * Setup function is called with:
118 * r4 = ptr to CPU spec (relocated)
120 _GLOBAL(call_setup_cpu)
121 addis r4,r3,cur_cpu_spec@ha
122 addi r4,r4,cur_cpu_spec@l
125 lwz r5,CPU_SPEC_SETUP(r4)
133 * complement mask on the msr then "or" some values on.
134 * _nmask_and_or_msr(nmask, value_to_or)
136 _GLOBAL(_nmask_and_or_msr)
137 mfmsr r0 /* Get current msr */
138 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
139 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
140 SYNC /* Some chip revs have problems here... */
141 mtmsr r0 /* Update machine state */
150 #if defined(CONFIG_40x)
151 sync /* Flush to memory before changing mapping */
153 isync /* Flush shadow TLB */
154 #elif defined(CONFIG_44x)
158 /* Load high watermark */
159 lis r4,tlb_44x_hwater@ha
160 lwz r5,tlb_44x_hwater@l(r4)
162 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
168 #else /* !(CONFIG_40x || CONFIG_44x) */
169 #if defined(CONFIG_SMP)
175 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
176 rlwinm r0,r0,0,28,26 /* clear DR */
180 lis r9,mmu_hash_lock@h
181 ori r9,r9,mmu_hash_lock@l
193 stw r0,0(r9) /* clear mmu_hash_lock */
197 #else /* CONFIG_SMP */
201 #endif /* CONFIG_SMP */
202 #endif /* ! defined(CONFIG_40x) */
206 * Flush MMU TLB for a particular address
209 #if defined(CONFIG_40x)
210 /* We run the search with interrupts disabled because we have to change
211 * the PID and I don't want to preempt when that happens.
222 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
223 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
225 tlbwe r3, r3, TLB_TAG
228 #elif defined(CONFIG_44x)
230 rlwimi r5,r4,0,24,31 /* Set TID */
232 /* We have to run the search with interrupts disabled, even critical
233 * and debug interrupts (in fact the only critical exceptions we have
234 * are debug and machine check). Otherwise an interrupt which causes
235 * a TLB miss can clobber the MMUCR between the mtspr and the tlbsx. */
237 lis r6,(MSR_EE|MSR_CE|MSR_ME|MSR_DE)@ha
238 addi r6,r6,(MSR_EE|MSR_CE|MSR_ME|MSR_DE)@l
246 /* There are only 64 TLB entries, so r3 < 64,
247 * which means bit 22, is clear. Since 22 is
248 * the V bit in the TLB_PAGEID, loading this
249 * value will invalidate the TLB entry.
251 tlbwe r3, r3, PPC44x_TLB_PAGEID
254 #else /* !(CONFIG_40x || CONFIG_44x) */
255 #if defined(CONFIG_SMP)
261 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
262 rlwinm r0,r0,0,28,26 /* clear DR */
266 lis r9,mmu_hash_lock@h
267 ori r9,r9,mmu_hash_lock@l
279 stw r0,0(r9) /* clear mmu_hash_lock */
283 #else /* CONFIG_SMP */
286 #endif /* CONFIG_SMP */
287 #endif /* ! CONFIG_40x */
291 * Flush instruction cache.
292 * This is a no-op on the 601.
294 _GLOBAL(flush_instruction_cache)
295 #if defined(CONFIG_8xx)
298 mtspr SPRN_IC_CST, r5
299 #elif defined(CONFIG_4xx)
313 rlwinm r3,r3,16,16,31
315 beqlr /* for 601, do nothing */
316 /* 603/604 processor - use invalidate-all bit in HID0 */
320 #endif /* CONFIG_8xx/4xx */
325 * Write any modified data cache blocks out to memory
326 * and invalidate the corresponding instruction cache blocks.
327 * This is a no-op on the 601.
329 * __flush_icache_range(unsigned long start, unsigned long stop)
331 _GLOBAL(__flush_icache_range)
333 blr /* for 601, do nothing */
334 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
335 li r5,L1_CACHE_BYTES-1
339 srwi. r4,r4,L1_CACHE_SHIFT
344 addi r3,r3,L1_CACHE_BYTES
346 sync /* wait for dcbst's to get to ram */
349 addi r6,r6,L1_CACHE_BYTES
351 sync /* additional sync needed on g4 */
355 * Write any modified data cache blocks out to memory.
356 * Does not invalidate the corresponding cache lines (especially for
357 * any corresponding instruction cache).
359 * clean_dcache_range(unsigned long start, unsigned long stop)
361 _GLOBAL(clean_dcache_range)
362 li r5,L1_CACHE_BYTES-1
366 srwi. r4,r4,L1_CACHE_SHIFT
371 addi r3,r3,L1_CACHE_BYTES
373 sync /* wait for dcbst's to get to ram */
377 * Write any modified data cache blocks out to memory and invalidate them.
378 * Does not invalidate the corresponding instruction cache blocks.
380 * flush_dcache_range(unsigned long start, unsigned long stop)
382 _GLOBAL(flush_dcache_range)
383 li r5,L1_CACHE_BYTES-1
387 srwi. r4,r4,L1_CACHE_SHIFT
392 addi r3,r3,L1_CACHE_BYTES
394 sync /* wait for dcbst's to get to ram */
398 * Like above, but invalidate the D-cache. This is used by the 8xx
399 * to invalidate the cache so the PPC core doesn't get stale data
400 * from the CPM (no cache snooping here :-).
402 * invalidate_dcache_range(unsigned long start, unsigned long stop)
404 _GLOBAL(invalidate_dcache_range)
405 li r5,L1_CACHE_BYTES-1
409 srwi. r4,r4,L1_CACHE_SHIFT
414 addi r3,r3,L1_CACHE_BYTES
416 sync /* wait for dcbi's to get to ram */
419 #ifdef CONFIG_NOT_COHERENT_CACHE
421 * 40x cores have 8K or 16K dcache and 32 byte line size.
422 * 44x has a 32K dcache and 32 byte line size.
423 * 8xx has 1, 2, 4, 8K variants.
424 * For now, cover the worst case of the 44x.
425 * Must be called with external interrupts disabled.
427 #define CACHE_NWAYS 64
428 #define CACHE_NLINES 16
430 _GLOBAL(flush_dcache_all)
431 li r4, (2 * CACHE_NWAYS * CACHE_NLINES)
434 1: lwz r3, 0(r5) /* Load one word from every line */
435 addi r5, r5, L1_CACHE_BYTES
438 #endif /* CONFIG_NOT_COHERENT_CACHE */
441 * Flush a particular page from the data cache to RAM.
442 * Note: this is necessary because the instruction cache does *not*
443 * snoop from the data cache.
444 * This is a no-op on the 601 which has a unified cache.
446 * void __flush_dcache_icache(void *page)
448 _GLOBAL(__flush_dcache_icache)
450 blr /* for 601, do nothing */
451 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
452 rlwinm r3,r3,0,0,19 /* Get page base address */
453 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
456 0: dcbst 0,r3 /* Write line to ram */
457 addi r3,r3,L1_CACHE_BYTES
461 /* We don't flush the icache on 44x. Those have a virtual icache
462 * and we don't have access to the virtual address here (it's
463 * not the page vaddr but where it's mapped in user space). The
464 * flushing of the icache on these is handled elsewhere, when
465 * a change in the address space occurs, before returning to
470 addi r6,r6,L1_CACHE_BYTES
474 #endif /* CONFIG_44x */
478 * Flush a particular page from the data cache to RAM, identified
479 * by its physical address. We turn off the MMU so we can just use
480 * the physical address (this may be a highmem page without a kernel
483 * void __flush_dcache_icache_phys(unsigned long physaddr)
485 _GLOBAL(__flush_dcache_icache_phys)
487 blr /* for 601, do nothing */
488 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
490 rlwinm r0,r10,0,28,26 /* clear DR */
493 rlwinm r3,r3,0,0,19 /* Get page base address */
494 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
497 0: dcbst 0,r3 /* Write line to ram */
498 addi r3,r3,L1_CACHE_BYTES
503 addi r6,r6,L1_CACHE_BYTES
506 mtmsr r10 /* restore DR */
511 * Clear pages using the dcbz instruction, which doesn't cause any
512 * memory traffic (except to write out any cache lines which get
513 * displaced). This only works on cacheable memory.
515 * void clear_pages(void *page, int order) ;
518 li r0,4096/L1_CACHE_BYTES
530 addi r3,r3,L1_CACHE_BYTES
535 * Copy a whole page. We use the dcbz instruction on the destination
536 * to reduce memory traffic (it eliminates the unnecessary reads of
537 * the destination into cache). This requires that the destination
540 #define COPY_16_BYTES \
555 /* don't use prefetch on 8xx */
556 li r0,4096/L1_CACHE_BYTES
562 #else /* not 8xx, we can prefetch */
565 #if MAX_COPY_PREFETCH > 1
566 li r0,MAX_COPY_PREFETCH
570 addi r11,r11,L1_CACHE_BYTES
572 #else /* MAX_COPY_PREFETCH == 1 */
574 li r11,L1_CACHE_BYTES+4
575 #endif /* MAX_COPY_PREFETCH */
576 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
584 #if L1_CACHE_BYTES >= 32
586 #if L1_CACHE_BYTES >= 64
589 #if L1_CACHE_BYTES >= 128
599 crnot 4*cr0+eq,4*cr0+eq
600 li r0,MAX_COPY_PREFETCH
603 #endif /* CONFIG_8xx */
606 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
607 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
609 _GLOBAL(atomic_clear_mask)
616 _GLOBAL(atomic_set_mask)
625 * I/O string operations
627 * insb(port, buf, len)
628 * outsb(port, buf, len)
629 * insw(port, buf, len)
630 * outsw(port, buf, len)
631 * insl(port, buf, len)
632 * outsl(port, buf, len)
633 * insw_ns(port, buf, len)
634 * outsw_ns(port, buf, len)
635 * insl_ns(port, buf, len)
636 * outsl_ns(port, buf, len)
638 * The *_ns versions don't do byte-swapping.
652 .section __ex_table, "a"
673 .section __ex_table, "a"
694 .section __ex_table, "a"
715 .section __ex_table, "a"
736 .section __ex_table, "a"
757 .section __ex_table, "a"
767 * Extended precision shifts.
769 * Updated to be valid for shift counts from 0 to 63 inclusive.
772 * R3/R4 has 64 bit value
776 * ashrdi3: arithmetic right shift (sign propagation)
777 * lshrdi3: logical right shift
778 * ashldi3: left shift
782 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
783 addi r7,r5,32 # could be xori, or addi with -32
784 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
785 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
786 sraw r7,r3,r7 # t2 = MSW >> (count-32)
787 or r4,r4,r6 # LSW |= t1
788 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
789 sraw r3,r3,r5 # MSW = MSW >> count
790 or r4,r4,r7 # LSW |= t2
795 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
796 addi r7,r5,32 # could be xori, or addi with -32
797 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
798 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
799 or r3,r3,r6 # MSW |= t1
800 slw r4,r4,r5 # LSW = LSW << count
801 or r3,r3,r7 # MSW |= t2
806 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
807 addi r7,r5,32 # could be xori, or addi with -32
808 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
809 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
810 or r4,r4,r6 # LSW |= t1
811 srw r3,r3,r5 # MSW = MSW >> count
812 or r4,r4,r7 # LSW |= t2
822 mr r3,r1 /* Close enough */
826 * Create a kernel thread
827 * kernel_thread(fn, arg, flags)
829 _GLOBAL(kernel_thread)
833 mr r30,r3 /* function */
834 mr r31,r4 /* argument */
835 ori r3,r5,CLONE_VM /* flags */
836 oris r3,r3,CLONE_UNTRACED>>16
837 li r4,0 /* new sp (unused) */
840 cmpwi 0,r3,0 /* parent or child? */
841 bne 1f /* return if parent */
842 li r0,0 /* make top-level stack frame */
844 mtlr r30 /* fn addr in lr */
845 mr r3,r31 /* load arg and call fn */
848 li r0,__NR_exit /* exit if function returns */
856 _GLOBAL(kernel_execve)
864 * This routine is just here to keep GCC happy - sigh...