199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2015 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef _RTE_PREFETCH_H_ 699a2dd95SBruce Richardson #define _RTE_PREFETCH_H_ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson #include <rte_compat.h> 999a2dd95SBruce Richardson 10*719834a6SMattias Rönnblom #ifdef __cplusplus 11*719834a6SMattias Rönnblom extern "C" { 12*719834a6SMattias Rönnblom #endif 13*719834a6SMattias Rönnblom 1499a2dd95SBruce Richardson /** 1599a2dd95SBruce Richardson * @file 1699a2dd95SBruce Richardson * 1799a2dd95SBruce Richardson * Prefetch operations. 1899a2dd95SBruce Richardson * 1999a2dd95SBruce Richardson * This file defines an API for prefetch macros / inline-functions, 2099a2dd95SBruce Richardson * which are architecture-dependent. Prefetching occurs when a 2199a2dd95SBruce Richardson * processor requests an instruction or data from memory to cache 2299a2dd95SBruce Richardson * before it is actually needed, potentially speeding up the execution of the 2399a2dd95SBruce Richardson * program. 2499a2dd95SBruce Richardson */ 2599a2dd95SBruce Richardson 2699a2dd95SBruce Richardson /** 2799a2dd95SBruce Richardson * Prefetch a cache line into all cache levels. 2899a2dd95SBruce Richardson * @param p 2999a2dd95SBruce Richardson * Address to prefetch 3099a2dd95SBruce Richardson */ 3199a2dd95SBruce Richardson static inline void rte_prefetch0(const volatile void *p); 3299a2dd95SBruce Richardson 3399a2dd95SBruce Richardson /** 3499a2dd95SBruce Richardson * Prefetch a cache line into all cache levels except the 0th cache level. 3599a2dd95SBruce Richardson * @param p 3699a2dd95SBruce Richardson * Address to prefetch 3799a2dd95SBruce Richardson */ 3899a2dd95SBruce Richardson static inline void rte_prefetch1(const volatile void *p); 3999a2dd95SBruce Richardson 4099a2dd95SBruce Richardson /** 4199a2dd95SBruce Richardson * Prefetch a cache line into all cache levels except the 0th and 1th cache 4299a2dd95SBruce Richardson * levels. 4399a2dd95SBruce Richardson * @param p 4499a2dd95SBruce Richardson * Address to prefetch 4599a2dd95SBruce Richardson */ 4699a2dd95SBruce Richardson static inline void rte_prefetch2(const volatile void *p); 4799a2dd95SBruce Richardson 4899a2dd95SBruce Richardson /** 4999a2dd95SBruce Richardson * Prefetch a cache line into all cache levels (non-temporal/transient version) 5099a2dd95SBruce Richardson * 5199a2dd95SBruce Richardson * The non-temporal prefetch is intended as a prefetch hint that processor will 5299a2dd95SBruce Richardson * use the prefetched data only once or short period, unlike the 5399a2dd95SBruce Richardson * rte_prefetch0() function which imply that prefetched data to use repeatedly. 5499a2dd95SBruce Richardson * 5599a2dd95SBruce Richardson * @param p 5699a2dd95SBruce Richardson * Address to prefetch 5799a2dd95SBruce Richardson */ 5899a2dd95SBruce Richardson static inline void rte_prefetch_non_temporal(const volatile void *p); 5999a2dd95SBruce Richardson 6099a2dd95SBruce Richardson /** 6199a2dd95SBruce Richardson * @warning 6299a2dd95SBruce Richardson * @b EXPERIMENTAL: this API may change, or be removed, without prior notice 6399a2dd95SBruce Richardson * 6499a2dd95SBruce Richardson * Prefetch a cache line into all cache levels, with intention to write. This 6599a2dd95SBruce Richardson * prefetch variant hints to the CPU that the program is expecting to write to 6699a2dd95SBruce Richardson * the cache line being prefetched. 6799a2dd95SBruce Richardson * 6899a2dd95SBruce Richardson * @param p Address to prefetch 6999a2dd95SBruce Richardson */ 7099a2dd95SBruce Richardson __rte_experimental 7199a2dd95SBruce Richardson static inline void 7299a2dd95SBruce Richardson rte_prefetch0_write(const void *p) 7399a2dd95SBruce Richardson { 7499a2dd95SBruce Richardson /* 1 indicates intention to write, 3 sets target cache level to L1. See 7599a2dd95SBruce Richardson * GCC docs where these integer constants are described in more detail: 7699a2dd95SBruce Richardson * https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html 7799a2dd95SBruce Richardson */ 7888f29340STyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC 7988f29340STyler Retzlaff rte_prefetch0(p); 8088f29340STyler Retzlaff #else 8199a2dd95SBruce Richardson __builtin_prefetch(p, 1, 3); 8288f29340STyler Retzlaff #endif 8399a2dd95SBruce Richardson } 8499a2dd95SBruce Richardson 8599a2dd95SBruce Richardson /** 8699a2dd95SBruce Richardson * @warning 8799a2dd95SBruce Richardson * @b EXPERIMENTAL: this API may change, or be removed, without prior notice 8899a2dd95SBruce Richardson * 8999a2dd95SBruce Richardson * Prefetch a cache line into all cache levels, except the 0th, with intention 9099a2dd95SBruce Richardson * to write. This prefetch variant hints to the CPU that the program is 9199a2dd95SBruce Richardson * expecting to write to the cache line being prefetched. 9299a2dd95SBruce Richardson * 9399a2dd95SBruce Richardson * @param p Address to prefetch 9499a2dd95SBruce Richardson */ 9599a2dd95SBruce Richardson __rte_experimental 9699a2dd95SBruce Richardson static inline void 9799a2dd95SBruce Richardson rte_prefetch1_write(const void *p) 9899a2dd95SBruce Richardson { 9999a2dd95SBruce Richardson /* 1 indicates intention to write, 2 sets target cache level to L2. See 10099a2dd95SBruce Richardson * GCC docs where these integer constants are described in more detail: 10199a2dd95SBruce Richardson * https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html 10299a2dd95SBruce Richardson */ 10388f29340STyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC 10488f29340STyler Retzlaff rte_prefetch1(p); 10588f29340STyler Retzlaff #else 10699a2dd95SBruce Richardson __builtin_prefetch(p, 1, 2); 10788f29340STyler Retzlaff #endif 10899a2dd95SBruce Richardson } 10999a2dd95SBruce Richardson 11099a2dd95SBruce Richardson /** 11199a2dd95SBruce Richardson * @warning 11299a2dd95SBruce Richardson * @b EXPERIMENTAL: this API may change, or be removed, without prior notice 11399a2dd95SBruce Richardson * 11499a2dd95SBruce Richardson * Prefetch a cache line into all cache levels, except the 0th and 1st, with 11599a2dd95SBruce Richardson * intention to write. This prefetch variant hints to the CPU that the program 11699a2dd95SBruce Richardson * is expecting to write to the cache line being prefetched. 11799a2dd95SBruce Richardson * 11899a2dd95SBruce Richardson * @param p Address to prefetch 11999a2dd95SBruce Richardson */ 12099a2dd95SBruce Richardson __rte_experimental 12199a2dd95SBruce Richardson static inline void 12299a2dd95SBruce Richardson rte_prefetch2_write(const void *p) 12399a2dd95SBruce Richardson { 12499a2dd95SBruce Richardson /* 1 indicates intention to write, 1 sets target cache level to L3. See 12599a2dd95SBruce Richardson * GCC docs where these integer constants are described in more detail: 12699a2dd95SBruce Richardson * https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html 12799a2dd95SBruce Richardson */ 12888f29340STyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC 12988f29340STyler Retzlaff rte_prefetch2(p); 13088f29340STyler Retzlaff #else 13199a2dd95SBruce Richardson __builtin_prefetch(p, 1, 1); 13288f29340STyler Retzlaff #endif 13399a2dd95SBruce Richardson } 13499a2dd95SBruce Richardson 13599a2dd95SBruce Richardson /** 13699a2dd95SBruce Richardson * @warning 13799a2dd95SBruce Richardson * @b EXPERIMENTAL: this API may change, or be removed, without prior notice 13899a2dd95SBruce Richardson * 13999a2dd95SBruce Richardson * Demote a cache line to a more distant level of cache from the processor. 14099a2dd95SBruce Richardson * CLDEMOTE hints to hardware to move (demote) a cache line from the closest to 14199a2dd95SBruce Richardson * the processor to a level more distant from the processor. It is a hint and 14299a2dd95SBruce Richardson * not guaranteed. rte_cldemote is intended to move the cache line to the more 14399a2dd95SBruce Richardson * remote cache, where it expects sharing to be efficient and to indicate that 14499a2dd95SBruce Richardson * a line may be accessed by a different core in the future. 14599a2dd95SBruce Richardson * 14699a2dd95SBruce Richardson * @param p 14799a2dd95SBruce Richardson * Address to demote 14899a2dd95SBruce Richardson */ 14999a2dd95SBruce Richardson __rte_experimental 15099a2dd95SBruce Richardson static inline void 15199a2dd95SBruce Richardson rte_cldemote(const volatile void *p); 15299a2dd95SBruce Richardson 153*719834a6SMattias Rönnblom #ifdef __cplusplus 154*719834a6SMattias Rönnblom } 155*719834a6SMattias Rönnblom #endif 156*719834a6SMattias Rönnblom 15799a2dd95SBruce Richardson #endif /* _RTE_PREFETCH_H_ */ 158