1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * xchg/cmpxchg operations for the Hexagon architecture 4 * 5 * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved. 6 */ 7 8 #ifndef _ASM_CMPXCHG_H 9 #define _ASM_CMPXCHG_H 10 11 #include <asm-generic/cmpxchg-local.h> 12 13 /* 14 * __arch_xchg - atomically exchange a register and a memory location 15 * @x: value to swap 16 * @ptr: pointer to memory 17 * @size: size of the value 18 * 19 * Only 4 bytes supported currently. 20 * 21 * Note: there was an errata for V2 about .new's and memw_locked. 22 * 23 */ 24 static inline unsigned long 25 __arch_xchg(unsigned long x, volatile void *ptr, int size) 26 { 27 unsigned long retval; 28 29 /* Can't seem to use printk or panic here, so just stop */ 30 if (size != 4) do { asm volatile("brkpt;\n"); } while (1); 31 32 __asm__ __volatile__ ( 33 "1: %0 = memw_locked(%1);\n" /* load into retval */ 34 " memw_locked(%1,P0) = %2;\n" /* store into memory */ 35 " if (!P0) jump 1b;\n" 36 : "=&r" (retval) 37 : "r" (ptr), "r" (x) 38 : "memory", "p0" 39 ); 40 return retval; 41 } 42 43 /* 44 * Atomically swap the contents of a register with memory. Should be atomic 45 * between multiple CPU's and within interrupts on the same CPU. 46 */ 47 #define arch_xchg(ptr, v) ((__typeof__(*(ptr)))__arch_xchg((unsigned long)(v), (ptr), \ 48 sizeof(*(ptr)))) 49 50 /* 51 * see rt-mutex-design.txt; cmpxchg supposedly checks if *ptr == A and swaps. 52 * looks just like atomic_cmpxchg on our arch currently with a bunch of 53 * variable casting. 54 */ 55 56 #define arch_cmpxchg(ptr, old, new) \ 57 ({ \ 58 __typeof__(ptr) __ptr = (ptr); \ 59 __typeof__(*(ptr)) __old = (old); \ 60 __typeof__(*(ptr)) __new = (new); \ 61 __typeof__(*(ptr)) __oldval = (__typeof__(*(ptr))) 0; \ 62 \ 63 asm volatile( \ 64 "1: %0 = memw_locked(%1);\n" \ 65 " { P0 = cmp.eq(%0,%2);\n" \ 66 " if (!P0.new) jump:nt 2f; }\n" \ 67 " memw_locked(%1,p0) = %3;\n" \ 68 " if (!P0) jump 1b;\n" \ 69 "2:\n" \ 70 : "=&r" (__oldval) \ 71 : "r" (__ptr), "r" (__old), "r" (__new) \ 72 : "memory", "p0" \ 73 ); \ 74 __oldval; \ 75 }) 76 77 #define arch_cmpxchg_local(ptr, o, n) \ 78 ((__typeof__(*(ptr)))__generic_cmpxchg_local((ptr), \ 79 (unsigned long)(o), (unsigned long)(n), sizeof(*(ptr)))) 80 81 #endif /* _ASM_CMPXCHG_H */ 82