/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _SPARC64_BACKOFF_H2#define _SPARC64_BACKOFF_H34/* The macros in this file implement an exponential backoff facility5* for atomic operations.6*7* When multiple threads compete on an atomic operation, it is8* possible for one thread to be continually denied a successful9* completion of the compare-and-swap instruction. Heavily10* threaded cpu implementations like Niagara can compound this11* problem even further.12*13* When an atomic operation fails and needs to be retried, we spin a14* certain number of times. At each subsequent failure of the same15* operation we double the spin count, realizing an exponential16* backoff.17*18* When we spin, we try to use an operation that will cause the19* current cpu strand to block, and therefore make the core fully20* available to any other runnable strands. There are two21* options, based upon cpu capabilities.22*23* On all cpus prior to SPARC-T4 we do three dummy reads of the24* condition code register. Each read blocks the strand for something25* between 40 and 50 cpu cycles.26*27* For SPARC-T4 and later we have a special "pause" instruction28* available. This is implemented using writes to register %asr27.29* The cpu will block the number of cycles written into the register,30* unless a disrupting trap happens first. SPARC-T4 specifically31* implements pause with a granularity of 8 cycles. Each strand has32* an internal pause counter which decrements every 8 cycles. So the33* chip shifts the %asr27 value down by 3 bits, and writes the result34* into the pause counter. If a value smaller than 8 is written, the35* chip blocks for 1 cycle.36*37* To achieve the same amount of backoff as the three %ccr reads give38* on earlier chips, we shift the backoff value up by 7 bits. (Three39* %ccr reads block for about 128 cycles, 1 << 7 == 128) We write the40* whole amount we want to block into the pause register, rather than41* loop writing 128 each time.42*/4344#define BACKOFF_LIMIT (4 * 1024)4546#ifdef CONFIG_SMP4748#define BACKOFF_SETUP(reg) \49mov 1, reg5051#define BACKOFF_LABEL(spin_label, continue_label) \52spin_label5354#define BACKOFF_SPIN(reg, tmp, label) \55mov reg, tmp; \5688: rd %ccr, %g0; \57rd %ccr, %g0; \58rd %ccr, %g0; \59.section .pause_3insn_patch,"ax";\60.word 88b; \61sllx tmp, 7, tmp; \62wr tmp, 0, %asr27; \63clr tmp; \64.previous; \65brnz,pt tmp, 88b; \66sub tmp, 1, tmp; \67set BACKOFF_LIMIT, tmp; \68cmp reg, tmp; \69bg,pn %xcc, label; \70nop; \71ba,pt %xcc, label; \72sllx reg, 1, reg;7374#else7576#define BACKOFF_SETUP(reg)7778#define BACKOFF_LABEL(spin_label, continue_label) \79continue_label8081#define BACKOFF_SPIN(reg, tmp, label)8283#endif8485#endif /* _SPARC64_BACKOFF_H */868788