system.h 21 KB

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