system.h 15 KB

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