blob: 191f2601de7958dee72300edabe1d6b8c94c7d87 [file] [log] [blame]
dejanjbf68e982013-08-02 15:39:58 +00001// This file determines MIPS32 features a processor supports.
2//
3// We return:
4// - 0 if the machine matches the asked-for feature.
5// - 1 if the machine does not.
6// - 2 if the asked-for feature isn't recognised (this will be the case for
7// any feature if run on a non-MIPS32 machine).
8// - 3 if there was a usage error (it also prints an error message).
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <assert.h>
13
14#define FEATURE_PRESENT 0
15#define FEATURE_NOT_PRESENT 1
16#define UNRECOGNISED_FEATURE 2
17#define USAGE_ERROR 3
18
19#if defined(VGA_mips32)
20static int mipsCPUInfo(const char *search_string) {
21 const char *file_name = "/proc/cpuinfo";
22 /* Simple detection of MIPS DSP ASE at runtime for Linux.
23 * It is based on /proc/cpuinfo, which reveals hardware configuration
24 * to user-space applications. */
25
26 char cpuinfo_line[256];
27
28 FILE *f = NULL;
29 if ((f = fopen (file_name, "r")) == NULL)
30 return 0;
31
32 while (fgets (cpuinfo_line, sizeof (cpuinfo_line), f) != NULL)
33 {
34 if (strstr (cpuinfo_line, search_string) != NULL)
35 {
36 fclose (f);
37 return 1;
38 }
39 }
40
41 fclose (f);
42
43 /* Did not find string in the /proc/cpuinfo file. */
44 return 0;
45}
46
47static int go(char *feature)
48{
49 int cpuinfo;
50 if ( (strcmp(feature, "mips32-dsp") == 0)) {
51 const char *dsp = "dsp";
52 cpuinfo = mipsCPUInfo(dsp);
53 if (cpuinfo == 1) {
54 return FEATURE_PRESENT;
55 } else{
56 return FEATURE_NOT_PRESENT;
57 }
58 } else if ((strcmp(feature, "mips32-dspr2") == 0)) {
59 const char *dsp2 = "dsp2";
60 cpuinfo = mipsCPUInfo(dsp2);
61 if (cpuinfo == 1) {
62 return FEATURE_PRESENT;
63 } else{
64 return FEATURE_NOT_PRESENT;
65 }
66 } else {
67 return UNRECOGNISED_FEATURE;
68 }
69
70}
71
72#else
73
74static int go(char *feature)
75{
76 /* Feature is not recognised. (non-MIPS32 machine!) */
77 return UNRECOGNISED_FEATURE;
78}
79
80#endif
81
82
83//---------------------------------------------------------------------------
84// main
85//---------------------------------------------------------------------------
86int main(int argc, char **argv)
87{
88 if (argc != 2) {
89 fprintf( stderr, "usage: mips32_features <feature>\n" );
90 exit(USAGE_ERROR);
91 }
92 return go(argv[1]);
93}