|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_crc32c_riscv_choose.c |
| 4 | + * Choose between RISC-V Zbc and software CRC-32C implementation. |
| 5 | + * |
| 6 | + * On first call, checks if the CPU supports the RISC-V Zbc (or Zbkc) extension. |
| 7 | + * If it does, use carry-less multiply instructions for CRC-32C computation. |
| 8 | + * Otherwise, fall back to the pure software implementation (slicing-by-8). |
| 9 | + * |
| 10 | + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group |
| 11 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 12 | + * |
| 13 | + * |
| 14 | + * IDENTIFICATION |
| 15 | + * src/port/pg_crc32c_riscv_choose.c |
| 16 | + * |
| 17 | + *------------------------------------------------------------------------- |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef FRONTEND |
| 21 | +#include "postgres.h" |
| 22 | +#else |
| 23 | +#include "postgres_fe.h" |
| 24 | +#endif |
| 25 | + |
| 26 | +#include <sys/syscall.h> |
| 27 | +#include <unistd.h> |
| 28 | + |
| 29 | +#include "port/pg_crc32c.h" |
| 30 | + |
| 31 | +/* |
| 32 | + * RISC-V hardware probing definitions |
| 33 | + */ |
| 34 | +#ifndef __NR_riscv_hwprobe |
| 35 | +#define __NR_riscv_hwprobe 258 |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifndef RISCV_HWPROBE_KEY_IMA_EXT_0 |
| 39 | +#define RISCV_HWPROBE_KEY_IMA_EXT_0 4 |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifndef RISCV_HWPROBE_EXT_ZBC |
| 43 | +#define RISCV_HWPROBE_EXT_ZBC (1ULL << 7) |
| 44 | +#endif |
| 45 | + |
| 46 | +#ifndef RISCV_HWPROBE_EXT_ZBKC |
| 47 | +#define RISCV_HWPROBE_EXT_ZBKC (1ULL << 27) |
| 48 | +#endif |
| 49 | + |
| 50 | +struct riscv_hwprobe |
| 51 | +{ |
| 52 | + int64 key; |
| 53 | + uint64 value; |
| 54 | +}; |
| 55 | + |
| 56 | +/* |
| 57 | + * Check if RISC-V Zbc or Zbkc extension is available |
| 58 | + * |
| 59 | + * Uses the riscv_hwprobe syscall which is available on Linux kernel 6.4+ |
| 60 | + * Falls back to software if the syscall fails or extensions are not available. |
| 61 | + */ |
| 62 | +static bool |
| 63 | +pg_crc32c_riscv_zbc_available(void) |
| 64 | +{ |
| 65 | +#if defined(__linux__) && defined(__riscv) && (__riscv_xlen == 64) |
| 66 | + struct riscv_hwprobe pair = {.key = RISCV_HWPROBE_KEY_IMA_EXT_0}; |
| 67 | + |
| 68 | + /* |
| 69 | + * Make the syscall. If it fails (e.g., old kernel, non-Linux), fall back |
| 70 | + * to software. |
| 71 | + */ |
| 72 | + if (syscall(__NR_riscv_hwprobe, &pair, 1, 0, NULL, 0) != 0) |
| 73 | + return false; |
| 74 | + |
| 75 | + /* |
| 76 | + * Check if either Zbc (general bitmanip carry-less) or Zbkc (crypto |
| 77 | + * carry-less) is available. Both provide clmul/clmulh instructions. |
| 78 | + */ |
| 79 | + return (pair.value & (RISCV_HWPROBE_EXT_ZBC | RISCV_HWPROBE_EXT_ZBKC)) != 0; |
| 80 | +#else |
| 81 | + /* Not on RISC-V Linux, or not 64-bit - use software fallback */ |
| 82 | + return false; |
| 83 | +#endif |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * This gets called on the first call. It replaces the function pointer |
| 88 | + * so that subsequent calls are routed directly to the chosen implementation. |
| 89 | + */ |
| 90 | +static pg_crc32c |
| 91 | +pg_comp_crc32c_choose(pg_crc32c crc, const void *data, size_t len) |
| 92 | +{ |
| 93 | + if (pg_crc32c_riscv_zbc_available()) |
| 94 | + pg_comp_crc32c = pg_comp_crc32c_riscv_zbc; |
| 95 | + else |
| 96 | + pg_comp_crc32c = pg_comp_crc32c_sb8; |
| 97 | + |
| 98 | + return pg_comp_crc32c(crc, data, len); |
| 99 | +} |
| 100 | + |
| 101 | +pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len) = pg_comp_crc32c_choose; |
0 commit comments