system.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. #ifndef __ALPHA_SYSTEM_H
  2. #define __ALPHA_SYSTEM_H
  3. #include <asm/pal.h>
  4. #include <asm/page.h>
  5. #include <asm/barrier.h>
  6. /*
  7. * System defines.. Note that this is included both from .c and .S
  8. * files, so it does only defines, not any C code.
  9. */
  10. /*
  11. * We leave one page for the initial stack page, and one page for
  12. * the initial process structure. Also, the console eats 3 MB for
  13. * the initial bootloader (one of which we can reclaim later).
  14. */
  15. #define BOOT_PCB 0x20000000
  16. #define BOOT_ADDR 0x20000000
  17. /* Remove when official MILO sources have ELF support: */
  18. #define BOOT_SIZE (16*1024)
  19. #ifdef CONFIG_ALPHA_LEGACY_START_ADDRESS
  20. #define KERNEL_START_PHYS 0x300000 /* Old bootloaders hardcoded this. */
  21. #else
  22. #define KERNEL_START_PHYS 0x1000000 /* required: Wildfire/Titan/Marvel */
  23. #endif
  24. #define KERNEL_START (PAGE_OFFSET+KERNEL_START_PHYS)
  25. #define SWAPPER_PGD KERNEL_START
  26. #define INIT_STACK (PAGE_OFFSET+KERNEL_START_PHYS+0x02000)
  27. #define EMPTY_PGT (PAGE_OFFSET+KERNEL_START_PHYS+0x04000)
  28. #define EMPTY_PGE (PAGE_OFFSET+KERNEL_START_PHYS+0x08000)
  29. #define ZERO_PGE (PAGE_OFFSET+KERNEL_START_PHYS+0x0A000)
  30. #define START_ADDR (PAGE_OFFSET+KERNEL_START_PHYS+0x10000)
  31. /*
  32. * This is setup by the secondary bootstrap loader. Because
  33. * the zero page is zeroed out as soon as the vm system is
  34. * initialized, we need to copy things out into a more permanent
  35. * place.
  36. */
  37. #define PARAM ZERO_PGE
  38. #define COMMAND_LINE ((char*)(PARAM + 0x0000))
  39. #define INITRD_START (*(unsigned long *) (PARAM+0x100))
  40. #define INITRD_SIZE (*(unsigned long *) (PARAM+0x108))
  41. #ifndef __ASSEMBLY__
  42. #include <linux/kernel.h>
  43. #define AT_VECTOR_SIZE_ARCH 4 /* entries in ARCH_DLINFO */
  44. /*
  45. * This is the logout header that should be common to all platforms
  46. * (assuming they are running OSF/1 PALcode, I guess).
  47. */
  48. struct el_common {
  49. unsigned int size; /* size in bytes of logout area */
  50. unsigned int sbz1 : 30; /* should be zero */
  51. unsigned int err2 : 1; /* second error */
  52. unsigned int retry : 1; /* retry flag */
  53. unsigned int proc_offset; /* processor-specific offset */
  54. unsigned int sys_offset; /* system-specific offset */
  55. unsigned int code; /* machine check code */
  56. unsigned int frame_rev; /* frame revision */
  57. };
  58. /* Machine Check Frame for uncorrectable errors (Large format)
  59. * --- This is used to log uncorrectable errors such as
  60. * double bit ECC errors.
  61. * --- These errors are detected by both processor and systems.
  62. */
  63. struct el_common_EV5_uncorrectable_mcheck {
  64. unsigned long shadow[8]; /* Shadow reg. 8-14, 25 */
  65. unsigned long paltemp[24]; /* PAL TEMP REGS. */
  66. unsigned long exc_addr; /* Address of excepting instruction*/
  67. unsigned long exc_sum; /* Summary of arithmetic traps. */
  68. unsigned long exc_mask; /* Exception mask (from exc_sum). */
  69. unsigned long pal_base; /* Base address for PALcode. */
  70. unsigned long isr; /* Interrupt Status Reg. */
  71. unsigned long icsr; /* CURRENT SETUP OF EV5 IBOX */
  72. unsigned long ic_perr_stat; /* I-CACHE Reg. <11> set Data parity
  73. <12> set TAG parity*/
  74. unsigned long dc_perr_stat; /* D-CACHE error Reg. Bits set to 1:
  75. <2> Data error in bank 0
  76. <3> Data error in bank 1
  77. <4> Tag error in bank 0
  78. <5> Tag error in bank 1 */
  79. unsigned long va; /* Effective VA of fault or miss. */
  80. unsigned long mm_stat; /* Holds the reason for D-stream
  81. fault or D-cache parity errors */
  82. unsigned long sc_addr; /* Address that was being accessed
  83. when EV5 detected Secondary cache
  84. failure. */
  85. unsigned long sc_stat; /* Helps determine if the error was
  86. TAG/Data parity(Secondary Cache)*/
  87. unsigned long bc_tag_addr; /* Contents of EV5 BC_TAG_ADDR */
  88. unsigned long ei_addr; /* Physical address of any transfer
  89. that is logged in EV5 EI_STAT */
  90. unsigned long fill_syndrome; /* For correcting ECC errors. */
  91. unsigned long ei_stat; /* Helps identify reason of any
  92. processor uncorrectable error
  93. at its external interface. */
  94. unsigned long ld_lock; /* Contents of EV5 LD_LOCK register*/
  95. };
  96. struct el_common_EV6_mcheck {
  97. unsigned int FrameSize; /* Bytes, including this field */
  98. unsigned int FrameFlags; /* <31> = Retry, <30> = Second Error */
  99. unsigned int CpuOffset; /* Offset to CPU-specific info */
  100. unsigned int SystemOffset; /* Offset to system-specific info */
  101. unsigned int MCHK_Code;
  102. unsigned int MCHK_Frame_Rev;
  103. unsigned long I_STAT; /* EV6 Internal Processor Registers */
  104. unsigned long DC_STAT; /* (See the 21264 Spec) */
  105. unsigned long C_ADDR;
  106. unsigned long DC1_SYNDROME;
  107. unsigned long DC0_SYNDROME;
  108. unsigned long C_STAT;
  109. unsigned long C_STS;
  110. unsigned long MM_STAT;
  111. unsigned long EXC_ADDR;
  112. unsigned long IER_CM;
  113. unsigned long ISUM;
  114. unsigned long RESERVED0;
  115. unsigned long PAL_BASE;
  116. unsigned long I_CTL;
  117. unsigned long PCTX;
  118. };
  119. extern void halt(void) __attribute__((noreturn));
  120. #define __halt() __asm__ __volatile__ ("call_pal %0 #halt" : : "i" (PAL_halt))
  121. #define switch_to(P,N,L) \
  122. do { \
  123. (L) = alpha_switch_to(virt_to_phys(&task_thread_info(N)->pcb), (P)); \
  124. check_mmu_context(); \
  125. } while (0)
  126. struct task_struct;
  127. extern struct task_struct *alpha_switch_to(unsigned long, struct task_struct*);
  128. #define imb() \
  129. __asm__ __volatile__ ("call_pal %0 #imb" : : "i" (PAL_imb) : "memory")
  130. #define draina() \
  131. __asm__ __volatile__ ("call_pal %0 #draina" : : "i" (PAL_draina) : "memory")
  132. enum implver_enum {
  133. IMPLVER_EV4,
  134. IMPLVER_EV5,
  135. IMPLVER_EV6
  136. };
  137. #ifdef CONFIG_ALPHA_GENERIC
  138. #define implver() \
  139. ({ unsigned long __implver; \
  140. __asm__ ("implver %0" : "=r"(__implver)); \
  141. (enum implver_enum) __implver; })
  142. #else
  143. /* Try to eliminate some dead code. */
  144. #ifdef CONFIG_ALPHA_EV4
  145. #define implver() IMPLVER_EV4
  146. #endif
  147. #ifdef CONFIG_ALPHA_EV5
  148. #define implver() IMPLVER_EV5
  149. #endif
  150. #if defined(CONFIG_ALPHA_EV6)
  151. #define implver() IMPLVER_EV6
  152. #endif
  153. #endif
  154. enum amask_enum {
  155. AMASK_BWX = (1UL << 0),
  156. AMASK_FIX = (1UL << 1),
  157. AMASK_CIX = (1UL << 2),
  158. AMASK_MAX = (1UL << 8),
  159. AMASK_PRECISE_TRAP = (1UL << 9),
  160. };
  161. #define amask(mask) \
  162. ({ unsigned long __amask, __input = (mask); \
  163. __asm__ ("amask %1,%0" : "=r"(__amask) : "rI"(__input)); \
  164. __amask; })
  165. #define __CALL_PAL_R0(NAME, TYPE) \
  166. static inline TYPE NAME(void) \
  167. { \
  168. register TYPE __r0 __asm__("$0"); \
  169. __asm__ __volatile__( \
  170. "call_pal %1 # " #NAME \
  171. :"=r" (__r0) \
  172. :"i" (PAL_ ## NAME) \
  173. :"$1", "$16", "$22", "$23", "$24", "$25"); \
  174. return __r0; \
  175. }
  176. #define __CALL_PAL_W1(NAME, TYPE0) \
  177. static inline void NAME(TYPE0 arg0) \
  178. { \
  179. register TYPE0 __r16 __asm__("$16") = arg0; \
  180. __asm__ __volatile__( \
  181. "call_pal %1 # "#NAME \
  182. : "=r"(__r16) \
  183. : "i"(PAL_ ## NAME), "0"(__r16) \
  184. : "$1", "$22", "$23", "$24", "$25"); \
  185. }
  186. #define __CALL_PAL_W2(NAME, TYPE0, TYPE1) \
  187. static inline void NAME(TYPE0 arg0, TYPE1 arg1) \
  188. { \
  189. register TYPE0 __r16 __asm__("$16") = arg0; \
  190. register TYPE1 __r17 __asm__("$17") = arg1; \
  191. __asm__ __volatile__( \
  192. "call_pal %2 # "#NAME \
  193. : "=r"(__r16), "=r"(__r17) \
  194. : "i"(PAL_ ## NAME), "0"(__r16), "1"(__r17) \
  195. : "$1", "$22", "$23", "$24", "$25"); \
  196. }
  197. #define __CALL_PAL_RW1(NAME, RTYPE, TYPE0) \
  198. static inline RTYPE NAME(TYPE0 arg0) \
  199. { \
  200. register RTYPE __r0 __asm__("$0"); \
  201. register TYPE0 __r16 __asm__("$16") = arg0; \
  202. __asm__ __volatile__( \
  203. "call_pal %2 # "#NAME \
  204. : "=r"(__r16), "=r"(__r0) \
  205. : "i"(PAL_ ## NAME), "0"(__r16) \
  206. : "$1", "$22", "$23", "$24", "$25"); \
  207. return __r0; \
  208. }
  209. #define __CALL_PAL_RW2(NAME, RTYPE, TYPE0, TYPE1) \
  210. static inline RTYPE NAME(TYPE0 arg0, TYPE1 arg1) \
  211. { \
  212. register RTYPE __r0 __asm__("$0"); \
  213. register TYPE0 __r16 __asm__("$16") = arg0; \
  214. register TYPE1 __r17 __asm__("$17") = arg1; \
  215. __asm__ __volatile__( \
  216. "call_pal %3 # "#NAME \
  217. : "=r"(__r16), "=r"(__r17), "=r"(__r0) \
  218. : "i"(PAL_ ## NAME), "0"(__r16), "1"(__r17) \
  219. : "$1", "$22", "$23", "$24", "$25"); \
  220. return __r0; \
  221. }
  222. __CALL_PAL_W1(cflush, unsigned long);
  223. __CALL_PAL_R0(rdmces, unsigned long);
  224. __CALL_PAL_R0(rdps, unsigned long);
  225. __CALL_PAL_R0(rdusp, unsigned long);
  226. __CALL_PAL_RW1(swpipl, unsigned long, unsigned long);
  227. __CALL_PAL_R0(whami, unsigned long);
  228. __CALL_PAL_W2(wrent, void*, unsigned long);
  229. __CALL_PAL_W1(wripir, unsigned long);
  230. __CALL_PAL_W1(wrkgp, unsigned long);
  231. __CALL_PAL_W1(wrmces, unsigned long);
  232. __CALL_PAL_RW2(wrperfmon, unsigned long, unsigned long, unsigned long);
  233. __CALL_PAL_W1(wrusp, unsigned long);
  234. __CALL_PAL_W1(wrvptptr, unsigned long);
  235. #define IPL_MIN 0
  236. #define IPL_SW0 1
  237. #define IPL_SW1 2
  238. #define IPL_DEV0 3
  239. #define IPL_DEV1 4
  240. #define IPL_TIMER 5
  241. #define IPL_PERF 6
  242. #define IPL_POWERFAIL 6
  243. #define IPL_MCHECK 7
  244. #define IPL_MAX 7
  245. #ifdef CONFIG_ALPHA_BROKEN_IRQ_MASK
  246. #undef IPL_MIN
  247. #define IPL_MIN __min_ipl
  248. extern int __min_ipl;
  249. #endif
  250. #define getipl() (rdps() & 7)
  251. #define setipl(ipl) ((void) swpipl(ipl))
  252. #define local_irq_disable() do { setipl(IPL_MAX); barrier(); } while(0)
  253. #define local_irq_enable() do { barrier(); setipl(IPL_MIN); } while(0)
  254. #define local_save_flags(flags) ((flags) = rdps())
  255. #define local_irq_save(flags) do { (flags) = swpipl(IPL_MAX); barrier(); } while(0)
  256. #define local_irq_restore(flags) do { barrier(); setipl(flags); barrier(); } while(0)
  257. #define irqs_disabled() (getipl() == IPL_MAX)
  258. /*
  259. * TB routines..
  260. */
  261. #define __tbi(nr,arg,arg1...) \
  262. ({ \
  263. register unsigned long __r16 __asm__("$16") = (nr); \
  264. register unsigned long __r17 __asm__("$17"); arg; \
  265. __asm__ __volatile__( \
  266. "call_pal %3 #__tbi" \
  267. :"=r" (__r16),"=r" (__r17) \
  268. :"0" (__r16),"i" (PAL_tbi) ,##arg1 \
  269. :"$0", "$1", "$22", "$23", "$24", "$25"); \
  270. })
  271. #define tbi(x,y) __tbi(x,__r17=(y),"1" (__r17))
  272. #define tbisi(x) __tbi(1,__r17=(x),"1" (__r17))
  273. #define tbisd(x) __tbi(2,__r17=(x),"1" (__r17))
  274. #define tbis(x) __tbi(3,__r17=(x),"1" (__r17))
  275. #define tbiap() __tbi(-1, /* no second argument */)
  276. #define tbia() __tbi(-2, /* no second argument */)
  277. /*
  278. * Atomic exchange.
  279. * Since it can be used to implement critical sections
  280. * it must clobber "memory" (also for interrupts in UP).
  281. */
  282. static inline unsigned long
  283. __xchg_u8(volatile char *m, unsigned long val)
  284. {
  285. unsigned long ret, tmp, addr64;
  286. __asm__ __volatile__(
  287. " andnot %4,7,%3\n"
  288. " insbl %1,%4,%1\n"
  289. "1: ldq_l %2,0(%3)\n"
  290. " extbl %2,%4,%0\n"
  291. " mskbl %2,%4,%2\n"
  292. " or %1,%2,%2\n"
  293. " stq_c %2,0(%3)\n"
  294. " beq %2,2f\n"
  295. #ifdef CONFIG_SMP
  296. " mb\n"
  297. #endif
  298. ".subsection 2\n"
  299. "2: br 1b\n"
  300. ".previous"
  301. : "=&r" (ret), "=&r" (val), "=&r" (tmp), "=&r" (addr64)
  302. : "r" ((long)m), "1" (val) : "memory");
  303. return ret;
  304. }
  305. static inline unsigned long
  306. __xchg_u16(volatile short *m, unsigned long val)
  307. {
  308. unsigned long ret, tmp, addr64;
  309. __asm__ __volatile__(
  310. " andnot %4,7,%3\n"
  311. " inswl %1,%4,%1\n"
  312. "1: ldq_l %2,0(%3)\n"
  313. " extwl %2,%4,%0\n"
  314. " mskwl %2,%4,%2\n"
  315. " or %1,%2,%2\n"
  316. " stq_c %2,0(%3)\n"
  317. " beq %2,2f\n"
  318. #ifdef CONFIG_SMP
  319. " mb\n"
  320. #endif
  321. ".subsection 2\n"
  322. "2: br 1b\n"
  323. ".previous"
  324. : "=&r" (ret), "=&r" (val), "=&r" (tmp), "=&r" (addr64)
  325. : "r" ((long)m), "1" (val) : "memory");
  326. return ret;
  327. }
  328. static inline unsigned long
  329. __xchg_u32(volatile int *m, unsigned long val)
  330. {
  331. unsigned long dummy;
  332. __asm__ __volatile__(
  333. "1: ldl_l %0,%4\n"
  334. " bis $31,%3,%1\n"
  335. " stl_c %1,%2\n"
  336. " beq %1,2f\n"
  337. #ifdef CONFIG_SMP
  338. " mb\n"
  339. #endif
  340. ".subsection 2\n"
  341. "2: br 1b\n"
  342. ".previous"
  343. : "=&r" (val), "=&r" (dummy), "=m" (*m)
  344. : "rI" (val), "m" (*m) : "memory");
  345. return val;
  346. }
  347. static inline unsigned long
  348. __xchg_u64(volatile long *m, unsigned long val)
  349. {
  350. unsigned long dummy;
  351. __asm__ __volatile__(
  352. "1: ldq_l %0,%4\n"
  353. " bis $31,%3,%1\n"
  354. " stq_c %1,%2\n"
  355. " beq %1,2f\n"
  356. #ifdef CONFIG_SMP
  357. " mb\n"
  358. #endif
  359. ".subsection 2\n"
  360. "2: br 1b\n"
  361. ".previous"
  362. : "=&r" (val), "=&r" (dummy), "=m" (*m)
  363. : "rI" (val), "m" (*m) : "memory");
  364. return val;
  365. }
  366. /* This function doesn't exist, so you'll get a linker error
  367. if something tries to do an invalid xchg(). */
  368. extern void __xchg_called_with_bad_pointer(void);
  369. #define __xchg(ptr, x, size) \
  370. ({ \
  371. unsigned long __xchg__res; \
  372. volatile void *__xchg__ptr = (ptr); \
  373. switch (size) { \
  374. case 1: __xchg__res = __xchg_u8(__xchg__ptr, x); break; \
  375. case 2: __xchg__res = __xchg_u16(__xchg__ptr, x); break; \
  376. case 4: __xchg__res = __xchg_u32(__xchg__ptr, x); break; \
  377. case 8: __xchg__res = __xchg_u64(__xchg__ptr, x); break; \
  378. default: __xchg_called_with_bad_pointer(); __xchg__res = x; \
  379. } \
  380. __xchg__res; \
  381. })
  382. #define xchg(ptr,x) \
  383. ({ \
  384. __typeof__(*(ptr)) _x_ = (x); \
  385. (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
  386. })
  387. static inline unsigned long
  388. __xchg_u8_local(volatile char *m, unsigned long val)
  389. {
  390. unsigned long ret, tmp, addr64;
  391. __asm__ __volatile__(
  392. " andnot %4,7,%3\n"
  393. " insbl %1,%4,%1\n"
  394. "1: ldq_l %2,0(%3)\n"
  395. " extbl %2,%4,%0\n"
  396. " mskbl %2,%4,%2\n"
  397. " or %1,%2,%2\n"
  398. " stq_c %2,0(%3)\n"
  399. " beq %2,2f\n"
  400. ".subsection 2\n"
  401. "2: br 1b\n"
  402. ".previous"
  403. : "=&r" (ret), "=&r" (val), "=&r" (tmp), "=&r" (addr64)
  404. : "r" ((long)m), "1" (val) : "memory");
  405. return ret;
  406. }
  407. static inline unsigned long
  408. __xchg_u16_local(volatile short *m, unsigned long val)
  409. {
  410. unsigned long ret, tmp, addr64;
  411. __asm__ __volatile__(
  412. " andnot %4,7,%3\n"
  413. " inswl %1,%4,%1\n"
  414. "1: ldq_l %2,0(%3)\n"
  415. " extwl %2,%4,%0\n"
  416. " mskwl %2,%4,%2\n"
  417. " or %1,%2,%2\n"
  418. " stq_c %2,0(%3)\n"
  419. " beq %2,2f\n"
  420. ".subsection 2\n"
  421. "2: br 1b\n"
  422. ".previous"
  423. : "=&r" (ret), "=&r" (val), "=&r" (tmp), "=&r" (addr64)
  424. : "r" ((long)m), "1" (val) : "memory");
  425. return ret;
  426. }
  427. static inline unsigned long
  428. __xchg_u32_local(volatile int *m, unsigned long val)
  429. {
  430. unsigned long dummy;
  431. __asm__ __volatile__(
  432. "1: ldl_l %0,%4\n"
  433. " bis $31,%3,%1\n"
  434. " stl_c %1,%2\n"
  435. " beq %1,2f\n"
  436. ".subsection 2\n"
  437. "2: br 1b\n"
  438. ".previous"
  439. : "=&r" (val), "=&r" (dummy), "=m" (*m)
  440. : "rI" (val), "m" (*m) : "memory");
  441. return val;
  442. }
  443. static inline unsigned long
  444. __xchg_u64_local(volatile long *m, unsigned long val)
  445. {
  446. unsigned long dummy;
  447. __asm__ __volatile__(
  448. "1: ldq_l %0,%4\n"
  449. " bis $31,%3,%1\n"
  450. " stq_c %1,%2\n"
  451. " beq %1,2f\n"
  452. ".subsection 2\n"
  453. "2: br 1b\n"
  454. ".previous"
  455. : "=&r" (val), "=&r" (dummy), "=m" (*m)
  456. : "rI" (val), "m" (*m) : "memory");
  457. return val;
  458. }
  459. #define __xchg_local(ptr, x, size) \
  460. ({ \
  461. unsigned long __xchg__res; \
  462. volatile void *__xchg__ptr = (ptr); \
  463. switch (size) { \
  464. case 1: __xchg__res = __xchg_u8_local(__xchg__ptr, x); break; \
  465. case 2: __xchg__res = __xchg_u16_local(__xchg__ptr, x); break; \
  466. case 4: __xchg__res = __xchg_u32_local(__xchg__ptr, x); break; \
  467. case 8: __xchg__res = __xchg_u64_local(__xchg__ptr, x); break; \
  468. default: __xchg_called_with_bad_pointer(); __xchg__res = x; \
  469. } \
  470. __xchg__res; \
  471. })
  472. #define xchg_local(ptr,x) \
  473. ({ \
  474. __typeof__(*(ptr)) _x_ = (x); \
  475. (__typeof__(*(ptr))) __xchg_local((ptr), (unsigned long)_x_, \
  476. sizeof(*(ptr))); \
  477. })
  478. /*
  479. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  480. * store NEW in MEM. Return the initial value in MEM. Success is
  481. * indicated by comparing RETURN with OLD.
  482. *
  483. * The memory barrier should be placed in SMP only when we actually
  484. * make the change. If we don't change anything (so if the returned
  485. * prev is equal to old) then we aren't acquiring anything new and
  486. * we don't need any memory barrier as far I can tell.
  487. */
  488. #define __HAVE_ARCH_CMPXCHG 1
  489. static inline unsigned long
  490. __cmpxchg_u8(volatile char *m, long old, long new)
  491. {
  492. unsigned long prev, tmp, cmp, addr64;
  493. __asm__ __volatile__(
  494. " andnot %5,7,%4\n"
  495. " insbl %1,%5,%1\n"
  496. "1: ldq_l %2,0(%4)\n"
  497. " extbl %2,%5,%0\n"
  498. " cmpeq %0,%6,%3\n"
  499. " beq %3,2f\n"
  500. " mskbl %2,%5,%2\n"
  501. " or %1,%2,%2\n"
  502. " stq_c %2,0(%4)\n"
  503. " beq %2,3f\n"
  504. #ifdef CONFIG_SMP
  505. " mb\n"
  506. #endif
  507. "2:\n"
  508. ".subsection 2\n"
  509. "3: br 1b\n"
  510. ".previous"
  511. : "=&r" (prev), "=&r" (new), "=&r" (tmp), "=&r" (cmp), "=&r" (addr64)
  512. : "r" ((long)m), "Ir" (old), "1" (new) : "memory");
  513. return prev;
  514. }
  515. static inline unsigned long
  516. __cmpxchg_u16(volatile short *m, long old, long new)
  517. {
  518. unsigned long prev, tmp, cmp, addr64;
  519. __asm__ __volatile__(
  520. " andnot %5,7,%4\n"
  521. " inswl %1,%5,%1\n"
  522. "1: ldq_l %2,0(%4)\n"
  523. " extwl %2,%5,%0\n"
  524. " cmpeq %0,%6,%3\n"
  525. " beq %3,2f\n"
  526. " mskwl %2,%5,%2\n"
  527. " or %1,%2,%2\n"
  528. " stq_c %2,0(%4)\n"
  529. " beq %2,3f\n"
  530. #ifdef CONFIG_SMP
  531. " mb\n"
  532. #endif
  533. "2:\n"
  534. ".subsection 2\n"
  535. "3: br 1b\n"
  536. ".previous"
  537. : "=&r" (prev), "=&r" (new), "=&r" (tmp), "=&r" (cmp), "=&r" (addr64)
  538. : "r" ((long)m), "Ir" (old), "1" (new) : "memory");
  539. return prev;
  540. }
  541. static inline unsigned long
  542. __cmpxchg_u32(volatile int *m, int old, int new)
  543. {
  544. unsigned long prev, cmp;
  545. __asm__ __volatile__(
  546. "1: ldl_l %0,%5\n"
  547. " cmpeq %0,%3,%1\n"
  548. " beq %1,2f\n"
  549. " mov %4,%1\n"
  550. " stl_c %1,%2\n"
  551. " beq %1,3f\n"
  552. #ifdef CONFIG_SMP
  553. " mb\n"
  554. #endif
  555. "2:\n"
  556. ".subsection 2\n"
  557. "3: br 1b\n"
  558. ".previous"
  559. : "=&r"(prev), "=&r"(cmp), "=m"(*m)
  560. : "r"((long) old), "r"(new), "m"(*m) : "memory");
  561. return prev;
  562. }
  563. static inline unsigned long
  564. __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
  565. {
  566. unsigned long prev, cmp;
  567. __asm__ __volatile__(
  568. "1: ldq_l %0,%5\n"
  569. " cmpeq %0,%3,%1\n"
  570. " beq %1,2f\n"
  571. " mov %4,%1\n"
  572. " stq_c %1,%2\n"
  573. " beq %1,3f\n"
  574. #ifdef CONFIG_SMP
  575. " mb\n"
  576. #endif
  577. "2:\n"
  578. ".subsection 2\n"
  579. "3: br 1b\n"
  580. ".previous"
  581. : "=&r"(prev), "=&r"(cmp), "=m"(*m)
  582. : "r"((long) old), "r"(new), "m"(*m) : "memory");
  583. return prev;
  584. }
  585. /* This function doesn't exist, so you'll get a linker error
  586. if something tries to do an invalid cmpxchg(). */
  587. extern void __cmpxchg_called_with_bad_pointer(void);
  588. static __always_inline unsigned long
  589. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  590. {
  591. switch (size) {
  592. case 1:
  593. return __cmpxchg_u8(ptr, old, new);
  594. case 2:
  595. return __cmpxchg_u16(ptr, old, new);
  596. case 4:
  597. return __cmpxchg_u32(ptr, old, new);
  598. case 8:
  599. return __cmpxchg_u64(ptr, old, new);
  600. }
  601. __cmpxchg_called_with_bad_pointer();
  602. return old;
  603. }
  604. #define cmpxchg(ptr,o,n) \
  605. ({ \
  606. __typeof__(*(ptr)) _o_ = (o); \
  607. __typeof__(*(ptr)) _n_ = (n); \
  608. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  609. (unsigned long)_n_, sizeof(*(ptr))); \
  610. })
  611. static inline unsigned long
  612. __cmpxchg_u8_local(volatile char *m, long old, long new)
  613. {
  614. unsigned long prev, tmp, cmp, addr64;
  615. __asm__ __volatile__(
  616. " andnot %5,7,%4\n"
  617. " insbl %1,%5,%1\n"
  618. "1: ldq_l %2,0(%4)\n"
  619. " extbl %2,%5,%0\n"
  620. " cmpeq %0,%6,%3\n"
  621. " beq %3,2f\n"
  622. " mskbl %2,%5,%2\n"
  623. " or %1,%2,%2\n"
  624. " stq_c %2,0(%4)\n"
  625. " beq %2,3f\n"
  626. "2:\n"
  627. ".subsection 2\n"
  628. "3: br 1b\n"
  629. ".previous"
  630. : "=&r" (prev), "=&r" (new), "=&r" (tmp), "=&r" (cmp), "=&r" (addr64)
  631. : "r" ((long)m), "Ir" (old), "1" (new) : "memory");
  632. return prev;
  633. }
  634. static inline unsigned long
  635. __cmpxchg_u16_local(volatile short *m, long old, long new)
  636. {
  637. unsigned long prev, tmp, cmp, addr64;
  638. __asm__ __volatile__(
  639. " andnot %5,7,%4\n"
  640. " inswl %1,%5,%1\n"
  641. "1: ldq_l %2,0(%4)\n"
  642. " extwl %2,%5,%0\n"
  643. " cmpeq %0,%6,%3\n"
  644. " beq %3,2f\n"
  645. " mskwl %2,%5,%2\n"
  646. " or %1,%2,%2\n"
  647. " stq_c %2,0(%4)\n"
  648. " beq %2,3f\n"
  649. "2:\n"
  650. ".subsection 2\n"
  651. "3: br 1b\n"
  652. ".previous"
  653. : "=&r" (prev), "=&r" (new), "=&r" (tmp), "=&r" (cmp), "=&r" (addr64)
  654. : "r" ((long)m), "Ir" (old), "1" (new) : "memory");
  655. return prev;
  656. }
  657. static inline unsigned long
  658. __cmpxchg_u32_local(volatile int *m, int old, int new)
  659. {
  660. unsigned long prev, cmp;
  661. __asm__ __volatile__(
  662. "1: ldl_l %0,%5\n"
  663. " cmpeq %0,%3,%1\n"
  664. " beq %1,2f\n"
  665. " mov %4,%1\n"
  666. " stl_c %1,%2\n"
  667. " beq %1,3f\n"
  668. "2:\n"
  669. ".subsection 2\n"
  670. "3: br 1b\n"
  671. ".previous"
  672. : "=&r"(prev), "=&r"(cmp), "=m"(*m)
  673. : "r"((long) old), "r"(new), "m"(*m) : "memory");
  674. return prev;
  675. }
  676. static inline unsigned long
  677. __cmpxchg_u64_local(volatile long *m, unsigned long old, unsigned long new)
  678. {
  679. unsigned long prev, cmp;
  680. __asm__ __volatile__(
  681. "1: ldq_l %0,%5\n"
  682. " cmpeq %0,%3,%1\n"
  683. " beq %1,2f\n"
  684. " mov %4,%1\n"
  685. " stq_c %1,%2\n"
  686. " beq %1,3f\n"
  687. "2:\n"
  688. ".subsection 2\n"
  689. "3: br 1b\n"
  690. ".previous"
  691. : "=&r"(prev), "=&r"(cmp), "=m"(*m)
  692. : "r"((long) old), "r"(new), "m"(*m) : "memory");
  693. return prev;
  694. }
  695. static __always_inline unsigned long
  696. __cmpxchg_local(volatile void *ptr, unsigned long old, unsigned long new,
  697. int size)
  698. {
  699. switch (size) {
  700. case 1:
  701. return __cmpxchg_u8_local(ptr, old, new);
  702. case 2:
  703. return __cmpxchg_u16_local(ptr, old, new);
  704. case 4:
  705. return __cmpxchg_u32_local(ptr, old, new);
  706. case 8:
  707. return __cmpxchg_u64_local(ptr, old, new);
  708. }
  709. __cmpxchg_called_with_bad_pointer();
  710. return old;
  711. }
  712. #define cmpxchg_local(ptr,o,n) \
  713. ({ \
  714. __typeof__(*(ptr)) _o_ = (o); \
  715. __typeof__(*(ptr)) _n_ = (n); \
  716. (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
  717. (unsigned long)_n_, sizeof(*(ptr))); \
  718. })
  719. #endif /* __ASSEMBLY__ */
  720. #define arch_align_stack(x) (x)
  721. #endif