system.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  3. */
  4. #ifndef _ASM_POWERPC_SYSTEM_H
  5. #define _ASM_POWERPC_SYSTEM_H
  6. #include <linux/kernel.h>
  7. #include <asm/hw_irq.h>
  8. #include <asm/atomic.h>
  9. /*
  10. * Memory barrier.
  11. * The sync instruction guarantees that all memory accesses initiated
  12. * by this processor have been performed (with respect to all other
  13. * mechanisms that access memory). The eieio instruction is a barrier
  14. * providing an ordering (separately) for (a) cacheable stores and (b)
  15. * loads and stores to non-cacheable memory (e.g. I/O devices).
  16. *
  17. * mb() prevents loads and stores being reordered across this point.
  18. * rmb() prevents loads being reordered across this point.
  19. * wmb() prevents stores being reordered across this point.
  20. * read_barrier_depends() prevents data-dependent loads being reordered
  21. * across this point (nop on PPC).
  22. *
  23. * We have to use the sync instructions for mb(), since lwsync doesn't
  24. * order loads with respect to previous stores. Lwsync is fine for
  25. * rmb(), though. Note that lwsync is interpreted as sync by
  26. * 32-bit and older 64-bit CPUs.
  27. *
  28. * For wmb(), we use sync since wmb is used in drivers to order
  29. * stores to system memory with respect to writes to the device.
  30. * However, smp_wmb() can be a lighter-weight eieio barrier on
  31. * SMP since it is only used to order updates to system memory.
  32. */
  33. #define mb() __asm__ __volatile__ ("sync" : : : "memory")
  34. #define rmb() __asm__ __volatile__ ("lwsync" : : : "memory")
  35. #define wmb() __asm__ __volatile__ ("sync" : : : "memory")
  36. #define read_barrier_depends() do { } while(0)
  37. #define set_mb(var, value) do { var = value; mb(); } while (0)
  38. #ifdef __KERNEL__
  39. #ifdef CONFIG_SMP
  40. #define smp_mb() mb()
  41. #define smp_rmb() rmb()
  42. #define smp_wmb() __asm__ __volatile__ ("eieio" : : : "memory")
  43. #define smp_read_barrier_depends() read_barrier_depends()
  44. #else
  45. #define smp_mb() barrier()
  46. #define smp_rmb() barrier()
  47. #define smp_wmb() barrier()
  48. #define smp_read_barrier_depends() do { } while(0)
  49. #endif /* CONFIG_SMP */
  50. /*
  51. * This is a barrier which prevents following instructions from being
  52. * started until the value of the argument x is known. For example, if
  53. * x is a variable loaded from memory, this prevents following
  54. * instructions from being executed until the load has been performed.
  55. */
  56. #define data_barrier(x) \
  57. asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
  58. struct task_struct;
  59. struct pt_regs;
  60. #ifdef CONFIG_DEBUGGER
  61. extern int (*__debugger)(struct pt_regs *regs);
  62. extern int (*__debugger_ipi)(struct pt_regs *regs);
  63. extern int (*__debugger_bpt)(struct pt_regs *regs);
  64. extern int (*__debugger_sstep)(struct pt_regs *regs);
  65. extern int (*__debugger_iabr_match)(struct pt_regs *regs);
  66. extern int (*__debugger_dabr_match)(struct pt_regs *regs);
  67. extern int (*__debugger_fault_handler)(struct pt_regs *regs);
  68. #define DEBUGGER_BOILERPLATE(__NAME) \
  69. static inline int __NAME(struct pt_regs *regs) \
  70. { \
  71. if (unlikely(__ ## __NAME)) \
  72. return __ ## __NAME(regs); \
  73. return 0; \
  74. }
  75. DEBUGGER_BOILERPLATE(debugger)
  76. DEBUGGER_BOILERPLATE(debugger_ipi)
  77. DEBUGGER_BOILERPLATE(debugger_bpt)
  78. DEBUGGER_BOILERPLATE(debugger_sstep)
  79. DEBUGGER_BOILERPLATE(debugger_iabr_match)
  80. DEBUGGER_BOILERPLATE(debugger_dabr_match)
  81. DEBUGGER_BOILERPLATE(debugger_fault_handler)
  82. #ifdef CONFIG_XMON
  83. extern void xmon_init(int enable);
  84. #endif
  85. #else
  86. static inline int debugger(struct pt_regs *regs) { return 0; }
  87. static inline int debugger_ipi(struct pt_regs *regs) { return 0; }
  88. static inline int debugger_bpt(struct pt_regs *regs) { return 0; }
  89. static inline int debugger_sstep(struct pt_regs *regs) { return 0; }
  90. static inline int debugger_iabr_match(struct pt_regs *regs) { return 0; }
  91. static inline int debugger_dabr_match(struct pt_regs *regs) { return 0; }
  92. static inline int debugger_fault_handler(struct pt_regs *regs) { return 0; }
  93. #endif
  94. extern int set_dabr(unsigned long dabr);
  95. extern void print_backtrace(unsigned long *);
  96. extern void show_regs(struct pt_regs * regs);
  97. extern void flush_instruction_cache(void);
  98. extern void hard_reset_now(void);
  99. extern void poweroff_now(void);
  100. #ifdef CONFIG_6xx
  101. extern long _get_L2CR(void);
  102. extern long _get_L3CR(void);
  103. extern void _set_L2CR(unsigned long);
  104. extern void _set_L3CR(unsigned long);
  105. #else
  106. #define _get_L2CR() 0L
  107. #define _get_L3CR() 0L
  108. #define _set_L2CR(val) do { } while(0)
  109. #define _set_L3CR(val) do { } while(0)
  110. #endif
  111. extern void via_cuda_init(void);
  112. extern void read_rtc_time(void);
  113. extern void pmac_find_display(void);
  114. extern void giveup_fpu(struct task_struct *);
  115. extern void disable_kernel_fp(void);
  116. extern void enable_kernel_fp(void);
  117. extern void flush_fp_to_thread(struct task_struct *);
  118. extern void enable_kernel_altivec(void);
  119. extern void giveup_altivec(struct task_struct *);
  120. extern void load_up_altivec(struct task_struct *);
  121. extern int emulate_altivec(struct pt_regs *);
  122. extern void giveup_spe(struct task_struct *);
  123. extern void load_up_spe(struct task_struct *);
  124. extern int fix_alignment(struct pt_regs *);
  125. extern void cvt_fd(float *from, double *to, struct thread_struct *thread);
  126. extern void cvt_df(double *from, float *to, struct thread_struct *thread);
  127. #ifndef CONFIG_SMP
  128. extern void discard_lazy_cpu_state(void);
  129. #else
  130. static inline void discard_lazy_cpu_state(void)
  131. {
  132. }
  133. #endif
  134. #ifdef CONFIG_ALTIVEC
  135. extern void flush_altivec_to_thread(struct task_struct *);
  136. #else
  137. static inline void flush_altivec_to_thread(struct task_struct *t)
  138. {
  139. }
  140. #endif
  141. #ifdef CONFIG_SPE
  142. extern void flush_spe_to_thread(struct task_struct *);
  143. #else
  144. static inline void flush_spe_to_thread(struct task_struct *t)
  145. {
  146. }
  147. #endif
  148. extern int call_rtas(const char *, int, int, unsigned long *, ...);
  149. extern void cacheable_memzero(void *p, unsigned int nb);
  150. extern void *cacheable_memcpy(void *, const void *, unsigned int);
  151. extern int do_page_fault(struct pt_regs *, unsigned long, unsigned long);
  152. extern void bad_page_fault(struct pt_regs *, unsigned long, int);
  153. extern int die(const char *, struct pt_regs *, long);
  154. extern void _exception(int, struct pt_regs *, int, unsigned long);
  155. #ifdef CONFIG_BOOKE_WDT
  156. extern u32 booke_wdt_enabled;
  157. extern u32 booke_wdt_period;
  158. #endif /* CONFIG_BOOKE_WDT */
  159. struct device_node;
  160. extern void note_scsi_host(struct device_node *, void *);
  161. extern struct task_struct *__switch_to(struct task_struct *,
  162. struct task_struct *);
  163. #define switch_to(prev, next, last) ((last) = __switch_to((prev), (next)))
  164. struct thread_struct;
  165. extern struct task_struct *_switch(struct thread_struct *prev,
  166. struct thread_struct *next);
  167. /*
  168. * On SMP systems, when the scheduler does migration-cost autodetection,
  169. * it needs a way to flush as much of the CPU's caches as possible.
  170. *
  171. * TODO: fill this in!
  172. */
  173. static inline void sched_cacheflush(void)
  174. {
  175. }
  176. extern unsigned int rtas_data;
  177. extern int mem_init_done; /* set on boot once kmalloc can be called */
  178. extern unsigned long memory_limit;
  179. extern unsigned long klimit;
  180. extern int powersave_nap; /* set if nap mode can be used in idle loop */
  181. /*
  182. * Atomic exchange
  183. *
  184. * Changes the memory location '*ptr' to be val and returns
  185. * the previous value stored there.
  186. */
  187. static __inline__ unsigned long
  188. __xchg_u32(volatile void *p, unsigned long val)
  189. {
  190. unsigned long prev;
  191. __asm__ __volatile__(
  192. LWSYNC_ON_SMP
  193. "1: lwarx %0,0,%2 \n"
  194. PPC405_ERR77(0,%2)
  195. " stwcx. %3,0,%2 \n\
  196. bne- 1b"
  197. ISYNC_ON_SMP
  198. : "=&r" (prev), "+m" (*(volatile unsigned int *)p)
  199. : "r" (p), "r" (val)
  200. : "cc", "memory");
  201. return prev;
  202. }
  203. #ifdef CONFIG_PPC64
  204. static __inline__ unsigned long
  205. __xchg_u64(volatile void *p, unsigned long val)
  206. {
  207. unsigned long prev;
  208. __asm__ __volatile__(
  209. LWSYNC_ON_SMP
  210. "1: ldarx %0,0,%2 \n"
  211. PPC405_ERR77(0,%2)
  212. " stdcx. %3,0,%2 \n\
  213. bne- 1b"
  214. ISYNC_ON_SMP
  215. : "=&r" (prev), "+m" (*(volatile unsigned long *)p)
  216. : "r" (p), "r" (val)
  217. : "cc", "memory");
  218. return prev;
  219. }
  220. #endif
  221. /*
  222. * This function doesn't exist, so you'll get a linker error
  223. * if something tries to do an invalid xchg().
  224. */
  225. extern void __xchg_called_with_bad_pointer(void);
  226. static __inline__ unsigned long
  227. __xchg(volatile void *ptr, unsigned long x, unsigned int size)
  228. {
  229. switch (size) {
  230. case 4:
  231. return __xchg_u32(ptr, x);
  232. #ifdef CONFIG_PPC64
  233. case 8:
  234. return __xchg_u64(ptr, x);
  235. #endif
  236. }
  237. __xchg_called_with_bad_pointer();
  238. return x;
  239. }
  240. #define xchg(ptr,x) \
  241. ({ \
  242. __typeof__(*(ptr)) _x_ = (x); \
  243. (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
  244. })
  245. #define tas(ptr) (xchg((ptr),1))
  246. /*
  247. * Compare and exchange - if *p == old, set it to new,
  248. * and return the old value of *p.
  249. */
  250. #define __HAVE_ARCH_CMPXCHG 1
  251. static __inline__ unsigned long
  252. __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)
  253. {
  254. unsigned int prev;
  255. __asm__ __volatile__ (
  256. LWSYNC_ON_SMP
  257. "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
  258. cmpw 0,%0,%3\n\
  259. bne- 2f\n"
  260. PPC405_ERR77(0,%2)
  261. " stwcx. %4,0,%2\n\
  262. bne- 1b"
  263. ISYNC_ON_SMP
  264. "\n\
  265. 2:"
  266. : "=&r" (prev), "+m" (*p)
  267. : "r" (p), "r" (old), "r" (new)
  268. : "cc", "memory");
  269. return prev;
  270. }
  271. #ifdef CONFIG_PPC64
  272. static __inline__ unsigned long
  273. __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)
  274. {
  275. unsigned long prev;
  276. __asm__ __volatile__ (
  277. LWSYNC_ON_SMP
  278. "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
  279. cmpd 0,%0,%3\n\
  280. bne- 2f\n\
  281. stdcx. %4,0,%2\n\
  282. bne- 1b"
  283. ISYNC_ON_SMP
  284. "\n\
  285. 2:"
  286. : "=&r" (prev), "+m" (*p)
  287. : "r" (p), "r" (old), "r" (new)
  288. : "cc", "memory");
  289. return prev;
  290. }
  291. #endif
  292. /* This function doesn't exist, so you'll get a linker error
  293. if something tries to do an invalid cmpxchg(). */
  294. extern void __cmpxchg_called_with_bad_pointer(void);
  295. static __inline__ unsigned long
  296. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new,
  297. unsigned int size)
  298. {
  299. switch (size) {
  300. case 4:
  301. return __cmpxchg_u32(ptr, old, new);
  302. #ifdef CONFIG_PPC64
  303. case 8:
  304. return __cmpxchg_u64(ptr, old, new);
  305. #endif
  306. }
  307. __cmpxchg_called_with_bad_pointer();
  308. return old;
  309. }
  310. #define cmpxchg(ptr,o,n) \
  311. ({ \
  312. __typeof__(*(ptr)) _o_ = (o); \
  313. __typeof__(*(ptr)) _n_ = (n); \
  314. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  315. (unsigned long)_n_, sizeof(*(ptr))); \
  316. })
  317. #ifdef CONFIG_PPC64
  318. /*
  319. * We handle most unaligned accesses in hardware. On the other hand
  320. * unaligned DMA can be very expensive on some ppc64 IO chips (it does
  321. * powers of 2 writes until it reaches sufficient alignment).
  322. *
  323. * Based on this we disable the IP header alignment in network drivers.
  324. * We also modify NET_SKB_PAD to be a cacheline in size, thus maintaining
  325. * cacheline alignment of buffers.
  326. */
  327. #define NET_IP_ALIGN 0
  328. #define NET_SKB_PAD L1_CACHE_BYTES
  329. #endif
  330. #define arch_align_stack(x) (x)
  331. /* Used in very early kernel initialization. */
  332. extern unsigned long reloc_offset(void);
  333. extern unsigned long add_reloc_offset(unsigned long);
  334. extern void reloc_got2(unsigned long);
  335. #define PTRRELOC(x) ((typeof(x)) add_reloc_offset((unsigned long)(x)))
  336. static inline void create_instruction(unsigned long addr, unsigned int instr)
  337. {
  338. unsigned int *p;
  339. p = (unsigned int *)addr;
  340. *p = instr;
  341. asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (p));
  342. }
  343. /* Flags for create_branch:
  344. * "b" == create_branch(addr, target, 0);
  345. * "ba" == create_branch(addr, target, BRANCH_ABSOLUTE);
  346. * "bl" == create_branch(addr, target, BRANCH_SET_LINK);
  347. * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK);
  348. */
  349. #define BRANCH_SET_LINK 0x1
  350. #define BRANCH_ABSOLUTE 0x2
  351. static inline void create_branch(unsigned long addr,
  352. unsigned long target, int flags)
  353. {
  354. unsigned int instruction;
  355. if (! (flags & BRANCH_ABSOLUTE))
  356. target = target - addr;
  357. /* Mask out the flags and target, so they don't step on each other. */
  358. instruction = 0x48000000 | (flags & 0x3) | (target & 0x03FFFFFC);
  359. create_instruction(addr, instruction);
  360. }
  361. static inline void create_function_call(unsigned long addr, void * func)
  362. {
  363. unsigned long func_addr;
  364. #ifdef CONFIG_PPC64
  365. /*
  366. * On PPC64 the function pointer actually points to the function's
  367. * descriptor. The first entry in the descriptor is the address
  368. * of the function text.
  369. */
  370. func_addr = *(unsigned long *)func;
  371. #else
  372. func_addr = (unsigned long)func;
  373. #endif
  374. create_branch(addr, func_addr, BRANCH_SET_LINK);
  375. }
  376. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  377. extern void account_system_vtime(struct task_struct *);
  378. #endif
  379. #endif /* __KERNEL__ */
  380. #endif /* _ASM_POWERPC_SYSTEM_H */