summaryrefslogtreecommitdiffstats
path: root/src/options_arm.c
blob: 0adc0f27e2fbdc480f2e3a5e98b61c85acd3a32a (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "main.h"

#define AMI_BMC "/proc/ractrends/Helper/FwInfo"
#define OPENFSI_BMC "/sys/bus/platform/devices/gpio-fsi/fsi0/"
#define FSI_CFAM_ID "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/cfam_id"

#define CHIP_ID_P8  0xea
#define CHIP_ID_P9  0xd1
#define CHIP_ID_P8P 0xd3

enum backend default_backend(void)
{
	int rc;
	rc = access(AMI_BMC, F_OK);
	if (rc == 0) /* AMI BMC */
		return I2C;

	rc = access(OPENFSI_BMC, F_OK);
	if (rc == 0) /* Kernel interface. OpenBMC */
		return KERNEL;

	return FAKE;
}

void print_backends(FILE *stream)
{
	fprintf(stream, "Valid backends: i2c kernel fsi fake\n");
}

void print_targets(FILE *stream)
{
	fprintf(stream, "kernel: No target is necessary\n");
	fprintf(stream, "i2c: No target is necessary\n");
	fprintf(stream, "fsi: p8 p9w p9b p9r p9t p9z\n");
}

static const char *default_kernel_target(void)
{
	FILE *cfam_id_file;

	/* Try and determine the correct device type */
	cfam_id_file = fopen(FSI_CFAM_ID, "r");
	if (cfam_id_file) {
		uint32_t cfam_id = 0;
		int rc;

		rc = fscanf(cfam_id_file, "0x%" PRIx32, &cfam_id);
		if (rc != 1)
			pdbg_log(PDBG_ERROR, "%s", strerror(errno));
		fclose(cfam_id_file);

		switch((cfam_id >> 4) & 0xff) {
		case CHIP_ID_P9:
			return "p9";
			break;

		case CHIP_ID_P8:
		case CHIP_ID_P8P:
			return "p8";
			break;

		default:
			pdbg_log(PDBG_ERROR, "Unknown chip-id detected\n");
			pdbg_log(PDBG_ERROR, "You will need to specify a host type with -d <host type>\n");
			return NULL;
		}
	} else {
		/* The support for POWER8 included the cfam_id
		 * so if it doesn't exist assume we must be on
		 * P9 */
		pdbg_log(PDBG_WARNING, "Unable to determine host type, defaulting to p9\n");
		return "p9";
	}

	return NULL;
}

static const char *default_fsi_target(void)
{
	FILE *dt_compatible;
	char line[256];
	char *p;

	dt_compatible = fopen("/proc/device-tree/compatible", "r");
	if (!dt_compatible)
		return NULL;

	p = fgets(line, sizeof(line), dt_compatible);
	fclose(dt_compatible);
	if (!p) /* Uh oh*/
		return NULL;

	if (strstr(line, "witherspoon"))
		return "p9w";

	if (strstr(line, "blackbird"))
		return "p9b";

	if (strstr(line, "romulus"))
		return "p9r";

	if (strstr(line, "talos"))
		return "p9t";

	if (strstr(line, "zaius"))
		return "p9z";

	if (strstr(line, "palmetto"))
		return "p8";

	return NULL;
}

const char *default_target(enum backend backend)
{
	switch(backend) {
	case I2C:
		return NULL;
		break;

	case KERNEL:
		return default_kernel_target();
		break;

	case FSI:
		return default_fsi_target();
		break;

	default:
		return NULL;
		break;
	}

	return NULL;
}
OpenPOWER on IntegriCloud