File: mips_features.c

package info (click to toggle)
valgrind 1:3.10.0-4~bpo7+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 97,940 kB
  • sloc: ansic: 589,429; xml: 21,096; exp: 8,751; cpp: 7,366; asm: 6,526; perl: 5,656; sh: 5,334; makefile: 4,946; haskell: 195
file content (115 lines) | stat: -rw-r--r-- 2,964 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// This file determines MIPS features a processor supports.
//
// We return:
// - 0 if the machine matches the asked-for feature.
// - 1 if the machine does not.
// - 2 if the asked-for feature isn't recognised (this will be the case for
//     any feature if run on a non-MIPS machine).
// - 3 if there was a usage error (it also prints an error message).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define FEATURE_PRESENT       0
#define FEATURE_NOT_PRESENT   1
#define UNRECOGNISED_FEATURE  2
#define USAGE_ERROR           3

#if defined(VGA_mips32) || defined(VGA_mips64)
static int mipsCPUInfo(const char *search_string) {
   const char *file_name = "/proc/cpuinfo";
   /* Simple detection of MIPS DSP ASE at runtime for Linux.
   * It is based on /proc/cpuinfo, which reveals hardware configuration
   * to user-space applications. */

   char cpuinfo_line[256];

   FILE *f = NULL;
   if ((f = fopen (file_name, "r")) == NULL)
     return 0;

   while (fgets (cpuinfo_line, sizeof (cpuinfo_line), f) != NULL)
   {
     if (strstr (cpuinfo_line, search_string) != NULL)
     {
         fclose (f);
         return 1;
     }
   }

   fclose (f);

   /* Did not find string in the /proc/cpuinfo file. */
   return 0;
}

static int go(char *feature)
{
   int cpuinfo;
   if (strcmp(feature, "mips32-dsp") == 0) {
      const char *dsp = "dsp";
      cpuinfo = mipsCPUInfo(dsp);
      if (cpuinfo == 1) {
         return FEATURE_PRESENT;
      } else{
         return FEATURE_NOT_PRESENT;
      }
   } else if (strcmp(feature, "mips32-dspr2") == 0) {
      const char *dsp2 = "dsp2";
      cpuinfo = mipsCPUInfo(dsp2);
      if (cpuinfo == 1) {
         return FEATURE_PRESENT;
      } else{
         return FEATURE_NOT_PRESENT;
      }
   } else if (strcmp(feature, "cavium-octeon") == 0) {
      const char *cavium = "Cavium Octeon";
      cpuinfo = mipsCPUInfo(cavium);
      if (cpuinfo == 1) {
         return FEATURE_PRESENT;
      } else{
         return FEATURE_NOT_PRESENT;
      }
   } else if (strcmp(feature, "cavium-octeon2") == 0) {
      const char *cavium2 = "Cavium Octeon II";
      cpuinfo = mipsCPUInfo(cavium2);
      if (cpuinfo == 1) {
         return FEATURE_PRESENT;
      } else{
         return FEATURE_NOT_PRESENT;
      }
   } else if (strcmp(feature, "mips-be") == 0) {
#if defined (_MIPSEB)
     return FEATURE_PRESENT;
#else
     return FEATURE_NOT_PRESENT;
#endif
   } else {
      return UNRECOGNISED_FEATURE;
   }

}

#else

static int go(char *feature)
{
   /* Feature is not recognised. (non-MIPS machine!) */
   return UNRECOGNISED_FEATURE;
}

#endif


//---------------------------------------------------------------------------
// main
//---------------------------------------------------------------------------
int main(int argc, char **argv)
{
   if (argc != 2) {
      fprintf( stderr, "usage: mips_features <feature>\n" );
      exit(USAGE_ERROR);
   }
   return go(argv[1]);
}