blob: 1aef820bdd44d3cb81157c1b87f6a8186e30b2f1 [file] [log] [blame]
petarj0432cfe2013-09-15 22:49:01 +00001// This file determines MIPS features a processor supports.
dejanjbf68e982013-08-02 15:39:58 +00002//
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
petarj0432cfe2013-09-15 22:49:01 +00007// any feature if run on a non-MIPS machine).
dejanjbf68e982013-08-02 15:39:58 +00008// - 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
petarj0432cfe2013-09-15 22:49:01 +000019#if defined(VGA_mips32) || defined(VGA_mips64)
dejanjbf68e982013-08-02 15:39:58 +000020static 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;
petarj55e7f192014-01-29 15:58:09 +000050 if (strcmp(feature, "mips32-dsp") == 0) {
dejanjbf68e982013-08-02 15:39:58 +000051 const char *dsp = "dsp";
52 cpuinfo = mipsCPUInfo(dsp);
53 if (cpuinfo == 1) {
54 return FEATURE_PRESENT;
55 } else{
56 return FEATURE_NOT_PRESENT;
57 }
petarj55e7f192014-01-29 15:58:09 +000058 } else if (strcmp(feature, "mips32-dspr2") == 0) {
dejanjbf68e982013-08-02 15:39:58 +000059 const char *dsp2 = "dsp2";
60 cpuinfo = mipsCPUInfo(dsp2);
61 if (cpuinfo == 1) {
62 return FEATURE_PRESENT;
63 } else{
64 return FEATURE_NOT_PRESENT;
65 }
petarj55e7f192014-01-29 15:58:09 +000066 } else if (strcmp(feature, "cavium-octeon") == 0) {
petarj0432cfe2013-09-15 22:49:01 +000067 const char *cavium = "Cavium Octeon";
68 cpuinfo = mipsCPUInfo(cavium);
69 if (cpuinfo == 1) {
70 return FEATURE_PRESENT;
71 } else{
72 return FEATURE_NOT_PRESENT;
73 }
petarj55e7f192014-01-29 15:58:09 +000074 } else if (strcmp(feature, "cavium-octeon2") == 0) {
75 const char *cavium2 = "Cavium Octeon II";
76 cpuinfo = mipsCPUInfo(cavium2);
77 if (cpuinfo == 1) {
78 return FEATURE_PRESENT;
79 } else{
80 return FEATURE_NOT_PRESENT;
81 }
dejanjb3eae3e2014-03-19 15:44:19 +000082 } else if (strcmp(feature, "mips-be") == 0) {
83#if defined (_MIPSEB)
84 return FEATURE_PRESENT;
85#else
86 return FEATURE_NOT_PRESENT;
87#endif
dejanjbf68e982013-08-02 15:39:58 +000088 } else {
89 return UNRECOGNISED_FEATURE;
90 }
91
92}
93
94#else
95
96static int go(char *feature)
97{
petarj0432cfe2013-09-15 22:49:01 +000098 /* Feature is not recognised. (non-MIPS machine!) */
dejanjbf68e982013-08-02 15:39:58 +000099 return UNRECOGNISED_FEATURE;
100}
101
102#endif
103
104
105//---------------------------------------------------------------------------
106// main
107//---------------------------------------------------------------------------
108int main(int argc, char **argv)
109{
110 if (argc != 2) {
petarj0432cfe2013-09-15 22:49:01 +0000111 fprintf( stderr, "usage: mips_features <feature>\n" );
dejanjbf68e982013-08-02 15:39:58 +0000112 exit(USAGE_ERROR);
113 }
114 return go(argv[1]);
115}