cpu_linux_riscv64.odin 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #+build riscv64
  2. #+build linux
  3. package sysinfo
  4. import "base:intrinsics"
  5. import "core:sys/linux"
  6. @(init, private)
  7. init_cpu_features :: proc() {
  8. _features: CPU_Features
  9. defer cpu_features = _features
  10. HWCAP_Bits :: enum u64 {
  11. I = 'I' - 'A',
  12. M = 'M' - 'A',
  13. A = 'A' - 'A',
  14. F = 'F' - 'A',
  15. D = 'D' - 'A',
  16. C = 'C' - 'A',
  17. V = 'V' - 'A',
  18. }
  19. HWCAP :: bit_set[HWCAP_Bits; u64]
  20. // Read HWCAP for base extensions, we can get this info through hwprobe too but that is Linux 6.4+ only.
  21. {
  22. fd, err := linux.open("/proc/self/auxv", {})
  23. if err != .NONE { return }
  24. defer linux.close(fd)
  25. // This is probably enough right?
  26. buf: [4096]byte
  27. n, rerr := linux.read(fd, buf[:])
  28. if rerr != .NONE || n == 0 { return }
  29. ulong :: u64
  30. AT_HWCAP :: 16
  31. auxv := buf[:n]
  32. for len(auxv) >= size_of(ulong)*2 {
  33. key := intrinsics.unaligned_load((^ulong)(&auxv[0]))
  34. val := intrinsics.unaligned_load((^ulong)(&auxv[size_of(ulong)]))
  35. auxv = auxv[2*size_of(ulong):]
  36. if key != AT_HWCAP {
  37. continue
  38. }
  39. cap := transmute(HWCAP)(val)
  40. if .I in cap {
  41. _features += { .I }
  42. }
  43. if .M in cap {
  44. _features += { .M }
  45. }
  46. if .A in cap {
  47. _features += { .A }
  48. }
  49. if .F in cap {
  50. _features += { .F }
  51. }
  52. if .D in cap {
  53. _features += { .D }
  54. }
  55. if .C in cap {
  56. _features += { .C }
  57. }
  58. if .V in cap {
  59. _features += { .V }
  60. }
  61. break
  62. }
  63. }
  64. // hwprobe for other features.
  65. {
  66. pairs := []linux.RISCV_HWProbe{
  67. { key = .IMA_EXT_0 },
  68. { key = .CPUPERF_0 },
  69. { key = .MISALIGNED_SCALAR_PERF },
  70. }
  71. err := linux.riscv_hwprobe(raw_data(pairs), len(pairs), 0, nil, {})
  72. if err != nil {
  73. assert(err == .ENOSYS, "unexpected error from riscv_hwprobe()")
  74. return
  75. }
  76. assert(pairs[0].key == .IMA_EXT_0)
  77. exts := pairs[0].value.ima_ext_0
  78. exts -= { .FD, .C, .V }
  79. _features += transmute(CPU_Features)exts
  80. if pairs[2].key == .MISALIGNED_SCALAR_PERF {
  81. if pairs[2].value.misaligned_scalar_perf == .FAST {
  82. _features += { .Misaligned_Supported, .Misaligned_Fast }
  83. } else if pairs[2].value.misaligned_scalar_perf != .UNSUPPORTED {
  84. _features += { .Misaligned_Supported }
  85. }
  86. } else {
  87. assert(pairs[1].key == .CPUPERF_0)
  88. if .FAST in pairs[1].value.cpu_perf_0 {
  89. _features += { .Misaligned_Supported, .Misaligned_Fast }
  90. } else if .UNSUPPORTED not_in pairs[1].value.cpu_perf_0 {
  91. _features += { .Misaligned_Supported }
  92. }
  93. }
  94. }
  95. }
  96. @(init, private)
  97. init_cpu_name :: proc() {
  98. cpu_name = "RISCV64"
  99. }