linux.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* contrib/mips-mmi/linux.c
  2. *
  3. * Copyright (c) 2024 Cosmin Truta
  4. * Written by guxiwei, 2023
  5. *
  6. * This code is released under the libpng license.
  7. * For conditions of distribution and use, see the disclaimer
  8. * and license in png.h
  9. */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/auxv.h>
  14. /*
  15. * parse_r var, r - Helper assembler macro for parsing register names.
  16. *
  17. * This converts the register name in $n form provided in \r to the
  18. * corresponding register number, which is assigned to the variable \var. It is
  19. * needed to allow explicit encoding of instructions in inline assembly where
  20. * registers are chosen by the compiler in $n form, allowing us to avoid using
  21. * fixed register numbers.
  22. *
  23. * It also allows newer instructions (not implemented by the assembler) to be
  24. * transparently implemented using assembler macros, instead of needing separate
  25. * cases depending on toolchain support.
  26. *
  27. * Simple usage example:
  28. * __asm__ __volatile__("parse_r __rt, %0\n\t"
  29. * ".insn\n\t"
  30. * "# di %0\n\t"
  31. * ".word (0x41606000 | (__rt << 16))"
  32. * : "=r" (status);
  33. */
  34. /* Match an individual register number and assign to \var */
  35. #define _IFC_REG(n) \
  36. ".ifc \\r, $" #n "\n\t" \
  37. "\\var = " #n "\n\t" \
  38. ".endif\n\t"
  39. __asm__(".macro parse_r var r\n\t"
  40. "\\var = -1\n\t"
  41. _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3)
  42. _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7)
  43. _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11)
  44. _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15)
  45. _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19)
  46. _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23)
  47. _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27)
  48. _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31)
  49. ".iflt \\var\n\t"
  50. ".error \"Unable to parse register name \\r\"\n\t"
  51. ".endif\n\t"
  52. ".endm");
  53. #define HWCAP_LOONGSON_CPUCFG (1 << 14)
  54. static int cpucfg_available(void)
  55. {
  56. return getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG;
  57. }
  58. static int strstart(const char *str, const char *pfx, const char **ptr)
  59. {
  60. while (*pfx && *pfx == *str) {
  61. pfx++;
  62. str++;
  63. }
  64. if (!*pfx && ptr)
  65. *ptr = str;
  66. return !*pfx;
  67. }
  68. /* Most toolchains have no CPUCFG support yet */
  69. static uint32_t read_cpucfg(uint32_t reg)
  70. {
  71. uint32_t __res;
  72. __asm__ __volatile__(
  73. "parse_r __res,%0\n\t"
  74. "parse_r reg,%1\n\t"
  75. ".insn \n\t"
  76. ".word (0xc8080118 | (reg << 21) | (__res << 11))\n\t"
  77. :"=r"(__res)
  78. :"r"(reg)
  79. :
  80. );
  81. return __res;
  82. }
  83. #define LOONGSON_CFG1 0x1
  84. #define LOONGSON_CFG1_MMI (1 << 4)
  85. static int cpu_flags_cpucfg(void)
  86. {
  87. int flags = 0;
  88. uint32_t cfg1 = read_cpucfg(LOONGSON_CFG1);
  89. if (cfg1 & LOONGSON_CFG1_MMI)
  90. flags = 1;
  91. return flags;
  92. }
  93. static int cpu_flags_cpuinfo(void)
  94. {
  95. FILE *f = fopen("/proc/cpuinfo", "r");
  96. char buf[200];
  97. int flags = 0;
  98. if (!f)
  99. return flags;
  100. while (fgets(buf, sizeof(buf), f)) {
  101. /* Legacy kernel may not export MMI in ASEs implemented */
  102. if (strstart(buf, "cpu model", NULL)) {
  103. if (strstr(buf, "Loongson-3 "))
  104. flags = 1;
  105. break;
  106. }
  107. if (strstart(buf, "ASEs implemented", NULL)) {
  108. if (strstr(buf, " loongson-mmi"))
  109. flags = 1;
  110. break;
  111. }
  112. }
  113. fclose(f);
  114. return flags;
  115. }
  116. static int png_have_mmi()
  117. {
  118. if (cpucfg_available())
  119. return cpu_flags_cpucfg();
  120. else
  121. return cpu_flags_cpuinfo();
  122. return 0;
  123. }