18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _SPARC64_BACKOFF_H 38c2ecf20Sopenharmony_ci#define _SPARC64_BACKOFF_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* The macros in this file implement an exponential backoff facility 68c2ecf20Sopenharmony_ci * for atomic operations. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * When multiple threads compete on an atomic operation, it is 98c2ecf20Sopenharmony_ci * possible for one thread to be continually denied a successful 108c2ecf20Sopenharmony_ci * completion of the compare-and-swap instruction. Heavily 118c2ecf20Sopenharmony_ci * threaded cpu implementations like Niagara can compound this 128c2ecf20Sopenharmony_ci * problem even further. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * When an atomic operation fails and needs to be retried, we spin a 158c2ecf20Sopenharmony_ci * certain number of times. At each subsequent failure of the same 168c2ecf20Sopenharmony_ci * operation we double the spin count, realizing an exponential 178c2ecf20Sopenharmony_ci * backoff. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * When we spin, we try to use an operation that will cause the 208c2ecf20Sopenharmony_ci * current cpu strand to block, and therefore make the core fully 218c2ecf20Sopenharmony_ci * available to any other other runnable strands. There are two 228c2ecf20Sopenharmony_ci * options, based upon cpu capabilities. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * On all cpus prior to SPARC-T4 we do three dummy reads of the 258c2ecf20Sopenharmony_ci * condition code register. Each read blocks the strand for something 268c2ecf20Sopenharmony_ci * between 40 and 50 cpu cycles. 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * For SPARC-T4 and later we have a special "pause" instruction 298c2ecf20Sopenharmony_ci * available. This is implemented using writes to register %asr27. 308c2ecf20Sopenharmony_ci * The cpu will block the number of cycles written into the register, 318c2ecf20Sopenharmony_ci * unless a disrupting trap happens first. SPARC-T4 specifically 328c2ecf20Sopenharmony_ci * implements pause with a granularity of 8 cycles. Each strand has 338c2ecf20Sopenharmony_ci * an internal pause counter which decrements every 8 cycles. So the 348c2ecf20Sopenharmony_ci * chip shifts the %asr27 value down by 3 bits, and writes the result 358c2ecf20Sopenharmony_ci * into the pause counter. If a value smaller than 8 is written, the 368c2ecf20Sopenharmony_ci * chip blocks for 1 cycle. 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * To achieve the same amount of backoff as the three %ccr reads give 398c2ecf20Sopenharmony_ci * on earlier chips, we shift the backoff value up by 7 bits. (Three 408c2ecf20Sopenharmony_ci * %ccr reads block for about 128 cycles, 1 << 7 == 128) We write the 418c2ecf20Sopenharmony_ci * whole amount we want to block into the pause register, rather than 428c2ecf20Sopenharmony_ci * loop writing 128 each time. 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define BACKOFF_LIMIT (4 * 1024) 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#define BACKOFF_SETUP(reg) \ 508c2ecf20Sopenharmony_ci mov 1, reg 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define BACKOFF_LABEL(spin_label, continue_label) \ 538c2ecf20Sopenharmony_ci spin_label 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define BACKOFF_SPIN(reg, tmp, label) \ 568c2ecf20Sopenharmony_ci mov reg, tmp; \ 578c2ecf20Sopenharmony_ci88: rd %ccr, %g0; \ 588c2ecf20Sopenharmony_ci rd %ccr, %g0; \ 598c2ecf20Sopenharmony_ci rd %ccr, %g0; \ 608c2ecf20Sopenharmony_ci .section .pause_3insn_patch,"ax";\ 618c2ecf20Sopenharmony_ci .word 88b; \ 628c2ecf20Sopenharmony_ci sllx tmp, 7, tmp; \ 638c2ecf20Sopenharmony_ci wr tmp, 0, %asr27; \ 648c2ecf20Sopenharmony_ci clr tmp; \ 658c2ecf20Sopenharmony_ci .previous; \ 668c2ecf20Sopenharmony_ci brnz,pt tmp, 88b; \ 678c2ecf20Sopenharmony_ci sub tmp, 1, tmp; \ 688c2ecf20Sopenharmony_ci set BACKOFF_LIMIT, tmp; \ 698c2ecf20Sopenharmony_ci cmp reg, tmp; \ 708c2ecf20Sopenharmony_ci bg,pn %xcc, label; \ 718c2ecf20Sopenharmony_ci nop; \ 728c2ecf20Sopenharmony_ci ba,pt %xcc, label; \ 738c2ecf20Sopenharmony_ci sllx reg, 1, reg; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#else 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#define BACKOFF_SETUP(reg) 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci#define BACKOFF_LABEL(spin_label, continue_label) \ 808c2ecf20Sopenharmony_ci continue_label 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci#define BACKOFF_SPIN(reg, tmp, label) 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci#endif 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci#endif /* _SPARC64_BACKOFF_H */ 87