You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dynamic_mips64.c 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*****************************************************************************
  2. Copyright (c) 2020, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. #include <sys/wait.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/resource.h>
  34. #include "common.h"
  35. extern gotoblas_t gotoblas_LOONGSON3R3;
  36. extern gotoblas_t gotoblas_LOONGSON3R4;
  37. extern void openblas_warning(int verbose, const char * msg);
  38. #define NUM_CORETYPES 2
  39. static char *corename[] = {
  40. "loongson3r3",
  41. "loongson3r4",
  42. "UNKNOWN"
  43. };
  44. char *gotoblas_corename(void) {
  45. if (gotoblas == &gotoblas_LOONGSON3R3) return corename[0];
  46. if (gotoblas == &gotoblas_LOONGSON3R4) return corename[1];
  47. return corename[NUM_CORETYPES];
  48. }
  49. static gotoblas_t *force_coretype(char *coretype) {
  50. int i;
  51. int found = -1;
  52. char message[128];
  53. for ( i=0 ; i < NUM_CORETYPES; i++)
  54. {
  55. if (!strncasecmp(coretype, corename[i], 20))
  56. {
  57. found = i;
  58. break;
  59. }
  60. }
  61. switch (found)
  62. {
  63. case 0: return (&gotoblas_LOONGSON3R3);
  64. case 1: return (&gotoblas_LOONGSON3R4);
  65. }
  66. snprintf(message, 128, "Core not found: %s\n", coretype);
  67. openblas_warning(1, message);
  68. return NULL;
  69. }
  70. #define MMI_MASK 0x00000010
  71. #define MSA_MASK 0x00000020
  72. int fd[2];
  73. int support_cpucfg;
  74. static void handler(int signum)
  75. {
  76. close(fd[1]);
  77. exit(1);
  78. }
  79. /* Brief : Function to check if cpucfg supported on loongson
  80. * Return: 1 supported
  81. * 0 not supported
  82. */
  83. static int cpucfg_test(void) {
  84. pid_t pid;
  85. int status = 0;
  86. support_cpucfg = 0;
  87. pipe(fd);
  88. pid = fork();
  89. if (pid == 0) { /* Subprocess */
  90. struct sigaction act;
  91. close(fd[0]);
  92. /* Set signal action for SIGILL. */
  93. act.sa_handler = handler;
  94. sigaction(SIGILL,&act,NULL);
  95. /* Execute cpucfg in subprocess. */
  96. __asm__ volatile(
  97. ".insn \n\t"
  98. ".word (0xc8080118) \n\t"
  99. :::
  100. );
  101. support_cpucfg = 1;
  102. write(fd[1],&support_cpucfg,sizeof(support_cpucfg));
  103. close(fd[1]);
  104. exit(0);
  105. } else if (pid > 0){ /* Parent process*/
  106. close(fd[1]);
  107. if ((waitpid(pid,&status,0) <= 0) ||
  108. (read(fd[0],&support_cpucfg,sizeof(support_cpucfg)) <= 0))
  109. support_cpucfg = 0;
  110. close(fd[0]);
  111. } else {
  112. support_cpucfg = 0;
  113. }
  114. return support_cpucfg;
  115. }
  116. static gotoblas_t *get_coretype_from_cpucfg(void) {
  117. int flag = 0;
  118. __asm__ volatile(
  119. ".insn \n\t"
  120. "dli $8, 0x01 \n\t"
  121. ".word (0xc9084918) \n\t"
  122. "usw $9, 0x00(%0) \n\t"
  123. :
  124. : "r"(&flag)
  125. : "memory"
  126. );
  127. if (flag & MSA_MASK)
  128. return (&gotoblas_LOONGSON3R4);
  129. if (flag & MMI_MASK)
  130. return (&gotoblas_LOONGSON3R3);
  131. return NULL;
  132. }
  133. static gotoblas_t *get_coretype_from_cpuinfo(void) {
  134. #ifdef linux
  135. FILE *infile;
  136. char buffer[512], *p;
  137. p = (char *)NULL;
  138. //Check model name for Loongson3
  139. infile = fopen("/proc/cpuinfo", "r");
  140. while (fgets(buffer, sizeof(buffer), infile)){
  141. if (!strncmp("model name", buffer, 10)){
  142. p = strchr(buffer, ':') + 2;
  143. break;
  144. }
  145. }
  146. fclose(infile);
  147. if(p != NULL){
  148. if (strstr(p, "Loongson-3A3000") || strstr(p, "Loongson-3B3000"))
  149. return (&gotoblas_LOONGSON3R3);
  150. else if(strstr(p, "Loongson-3A4000") || strstr(p, "Loongson-3B4000"))
  151. return (&gotoblas_LOONGSON3R4);
  152. else
  153. return NULL;
  154. }
  155. #endif
  156. return NULL;
  157. }
  158. static gotoblas_t *get_coretype(void) {
  159. int ret = 0;
  160. ret = cpucfg_test();
  161. if (ret == 1)
  162. return get_coretype_from_cpucfg();
  163. else
  164. return get_coretype_from_cpuinfo();
  165. }
  166. void gotoblas_dynamic_init(void) {
  167. char coremsg[128];
  168. char coren[22];
  169. char *p;
  170. if (gotoblas) return;
  171. p = getenv("OPENBLAS_CORETYPE");
  172. if ( p )
  173. {
  174. gotoblas = force_coretype(p);
  175. }
  176. else
  177. {
  178. gotoblas = get_coretype();
  179. }
  180. if (gotoblas == NULL)
  181. {
  182. snprintf(coremsg, 128, "Falling back to loongson3r3 core\n");
  183. openblas_warning(1, coremsg);
  184. gotoblas = &gotoblas_LOONGSON3R3;
  185. }
  186. if (gotoblas && gotoblas->init) {
  187. strncpy(coren, gotoblas_corename(), 20);
  188. sprintf(coremsg, "Core: %s\n", coren);
  189. openblas_warning(2, coremsg);
  190. gotoblas -> init();
  191. } else {
  192. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  193. exit(1);
  194. }
  195. }
  196. void gotoblas_dynamic_quit(void) {
  197. gotoblas = NULL;
  198. }