vppinfra: Improve code portability
[vpp.git] / src / vppinfra / cache.h
index 7a54d34..4229a06 100644 (file)
 
 #include <vppinfra/error_bootstrap.h>
 
-/*
- * Allow CFLAGS to override the configured / deduced cache line size
- */
-#ifndef CLIB_LOG2_CACHE_LINE_BYTES
-
 /* Default cache line size of 64 bytes. */
 #ifndef CLIB_LOG2_CACHE_LINE_BYTES
 #define CLIB_LOG2_CACHE_LINE_BYTES 6
 #endif
 
-#endif /* CLIB_LOG2_CACHE_LINE_BYTES defined */
+/* How much data prefetch instruction prefetches */
+#ifndef CLIB_LOG2_CACHE_PREFETCH_BYTES
+#define CLIB_LOG2_CACHE_PREFETCH_BYTES CLIB_LOG2_CACHE_LINE_BYTES
+#endif
 
-#if (CLIB_LOG2_CACHE_LINE_BYTES >= 9)
-#error Cache line size 512 bytes or greater
+/* Default cache line fill buffers. */
+#ifndef CLIB_N_PREFETCHES
+#define CLIB_N_PREFETCHES 16
 #endif
 
-#define CLIB_CACHE_LINE_BYTES (1 << CLIB_LOG2_CACHE_LINE_BYTES)
-#define CLIB_CACHE_LINE_ALIGN_MARK(mark) u8 mark[0] __attribute__((aligned(CLIB_CACHE_LINE_BYTES)))
+#define CLIB_CACHE_LINE_BYTES    (1 << CLIB_LOG2_CACHE_LINE_BYTES)
+#define CLIB_CACHE_PREFETCH_BYTES (1 << CLIB_LOG2_CACHE_PREFETCH_BYTES)
+#define CLIB_CACHE_LINE_ALIGN_MARK(mark)                                      \
+  u8 mark[0] __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)))
+#define CLIB_CACHE_LINE_ROUND(x)                                              \
+  ((x + CLIB_CACHE_LINE_BYTES - 1) & ~(CLIB_CACHE_LINE_BYTES - 1))
 
 /* Read/write arguments to __builtin_prefetch. */
 #define CLIB_PREFETCH_READ 0
 #define CLIB_PREFETCH_WRITE 1
 #define CLIB_PREFETCH_STORE 1  /* alias for write */
 
-#define _CLIB_PREFETCH(n,size,type)                            \
-  if ((size) > (n)*CLIB_CACHE_LINE_BYTES)                      \
-    __builtin_prefetch (_addr + (n)*CLIB_CACHE_LINE_BYTES,     \
-                       CLIB_PREFETCH_##type,                   \
-                       /* locality */ 3);
-
-#define CLIB_PREFETCH(addr,size,type)          \
-do {                                           \
-  void * _addr = (addr);                       \
-                                               \
-  ASSERT ((size) <= 4*CLIB_CACHE_LINE_BYTES);  \
-  _CLIB_PREFETCH (0, size, type);              \
-  _CLIB_PREFETCH (1, size, type);              \
-  _CLIB_PREFETCH (2, size, type);              \
-  _CLIB_PREFETCH (3, size, type);              \
-} while (0)
+#define _CLIB_PREFETCH(n, size, type)                                         \
+  if ((size) > (n) *CLIB_CACHE_PREFETCH_BYTES)                                \
+    __builtin_prefetch (_addr + (n) *CLIB_CACHE_PREFETCH_BYTES,               \
+                       CLIB_PREFETCH_##type, /* locality */ 3);
+
+#define CLIB_PREFETCH(addr, size, type)                                       \
+  do                                                                          \
+    {                                                                         \
+      void *_addr = (addr);                                                   \
+                                                                              \
+      ASSERT ((size) <= 4 * CLIB_CACHE_PREFETCH_BYTES);                       \
+      _CLIB_PREFETCH (0, size, type);                                         \
+      _CLIB_PREFETCH (1, size, type);                                         \
+      _CLIB_PREFETCH (2, size, type);                                         \
+      _CLIB_PREFETCH (3, size, type);                                         \
+    }                                                                         \
+  while (0)
 
 #undef _
 
+static_always_inline void
+clib_prefetch_load (void *p)
+{
+  __builtin_prefetch (p, /* rw */ 0, /* locality */ 3);
+}
+
+static_always_inline void
+clib_prefetch_store (void *p)
+{
+  __builtin_prefetch (p, /* rw */ 1, /* locality */ 3);
+}
+
 #endif /* included_clib_cache_h */