summaryrefslogtreecommitdiffstats
path: root/libpdbg/adu.c
blob: 4f2e9cb414c6837b36d59ffc95d39dede4249321 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright 2016 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 <stdint.h>
#include <string.h>

#include "operations.h"
#include "bitutils.h"

enum adu_retcode {ADU_DONE, ADU_RETRY, ADU_ERR};

/* ADU SCOM Register Definitions */
#define ALTD_CONTROL_REG	0x0
#define ALTD_CMD_REG		0x1
#define P8_ALTD_STATUS_REG	0x2
#define P8_ALTD_DATA_REG	0x3

/* ALTD_CMD_REG fields */
#define FBC_ALTD_START_OP	PPC_BIT(2)
#define FBC_ALTD_CLEAR_STATUS	PPC_BIT(3)
#define FBC_ALTD_RESET_AD_PCB	PPC_BIT(4)
#define FBC_ALTD_SCOPE		PPC_BITMASK(16, 18)
#define FBC_ALTD_AUTO_INC	PPC_BIT(19)
#define FBC_ALTD_DROP_PRIORITY	PPC_BITMASK(20, 21)
#define FBC_LOCKED		PPC_BIT(11)

#define DROP_PRIORITY_LOW	0ULL
#define DROP_PRIORITY_MEDIUM	1ULL
#define DROP_PRIORITY_HIGH	2ULL

#define SCOPE_NODAL		0
#define SCOPE_GROUP		1
#define SCOPE_SYSTEM		2
#define SCOPE_REMOTE		3

/* ALTD_CONTROL_REG fields */
#define FBC_ALTD_TTYPE		PPC_BITMASK(0, 5)
#define TTYPE_TREAD 		PPC_BIT(6)
#define TTYPE_TWRITE 		0
#define FBC_ALTD_TSIZE		PPC_BITMASK(7, 13)
#define FBC_ALTD_ADDRESS	PPC_BITMASK(14, 63)

#define TTYPE_CI_PARTIAL_WRITE		0b110111
#define TTYPE_CI_PARTIAL_OOO_WRITE	0b110110
#define TTYPE_DMA_PARTIAL_WRITE    	0b100110
#define TTYPE_CI_PARTIAL_READ 	 	0b110100
#define TTYPE_DMA_PARTIAL_READ 		0b110101
#define TTYPE_PBOPERATION 		0b111111

/* P8_ALTD_STATUS_REG fields */
#define FBC_ALTD_ADDR_DONE	PPC_BIT(2)
#define FBC_ALTD_DATA_DONE	PPC_BIT(3)
#define FBC_ALTD_PBINIT_MISSING PPC_BIT(18)

static int adu_lock(struct adu *adu)
{
	uint64_t val;

	CHECK_ERR(pib_read(&adu->target, ALTD_CMD_REG, &val));

	if (val & FBC_LOCKED)
		PR_INFO("ADU already locked! Ignoring.\n");

	val |= FBC_LOCKED;
	CHECK_ERR(pib_write(&adu->target, ALTD_CMD_REG, val));

	return 0;
}

static int adu_unlock(struct adu *adu)
{
	uint64_t val;

	CHECK_ERR(pib_read(&adu->target, ALTD_CMD_REG, &val));

	if (!(val & FBC_LOCKED)) {
		PR_INFO("ADU already unlocked!\n");
		return 0;
	}

	val &= ~FBC_LOCKED;
	CHECK_ERR(pib_write(&adu->target, ALTD_CMD_REG, val));

	return 0;
}

static int adu_reset(struct adu *adu)
{
	uint64_t val;

	CHECK_ERR(pib_read(&adu->target, ALTD_CMD_REG, &val));
	val |= FBC_ALTD_CLEAR_STATUS | FBC_ALTD_RESET_AD_PCB;
	CHECK_ERR(pib_write(&adu->target, ALTD_CMD_REG, val));

	return 0;
}

static uint64_t p8_get_ctrl_reg(uint64_t ctrl_reg, uint64_t addr)
{
	ctrl_reg |= TTYPE_TREAD;
	ctrl_reg = SETFIELD(FBC_ALTD_TTYPE, ctrl_reg, TTYPE_DMA_PARTIAL_READ);
	ctrl_reg = SETFIELD(FBC_ALTD_TSIZE, ctrl_reg, 8);
	ctrl_reg = SETFIELD(FBC_ALTD_ADDRESS, ctrl_reg, addr);
	return ctrl_reg;
}

static uint64_t p8_get_cmd_reg(uint64_t cmd_reg, uint64_t addr)
{
	cmd_reg |= FBC_ALTD_START_OP;
	cmd_reg = SETFIELD(FBC_ALTD_SCOPE, cmd_reg, SCOPE_SYSTEM);
	cmd_reg = SETFIELD(FBC_ALTD_DROP_PRIORITY, cmd_reg, DROP_PRIORITY_MEDIUM);
	return cmd_reg;
}

static enum adu_retcode p8_wait_completion(struct adu *adu)
{
	uint64_t val;

	/* Wait for completion */
	do {
		CHECK_ERR(pib_read(&adu->target, P8_ALTD_STATUS_REG, &val));
	} while (!val);

	if( !(val & FBC_ALTD_ADDR_DONE) ||
	    !(val & FBC_ALTD_DATA_DONE)) {
		/* PBINIT_MISSING is expected occasionally so just retry */
		if (val & FBC_ALTD_PBINIT_MISSING)
			return ADU_RETRY;
		else {
			PR_ERROR("ALTD_STATUS_REG = 0x%016llx\n", val);
			return ADU_ERR;
		}
	}

	return ADU_DONE;
}

static int p8_adu_getmem(struct adu *adu, uint64_t addr)
{
}

/* Return size bytes of memory in *output. *output must point to an
 * array large enough to hold size bytes. */
static int _adu_getmem(struct adu *adu, uint64_t start_addr, uint8_t *output, uint64_t size)
{
	int rc = 0;
	uint64_t addr, ctrl_reg, cmd_reg, val;

	/* We read data in 8-byte aligned chunks */
	for (addr = 8*(start_addr / 8); addr < start_addr + size; addr += 8) {
		uint64_t data;

	retry:
		/* Clear status bits */
		CHECK_ERR(adu_reset(adu));

		/* Set the address */
		ctrl_reg = adu->_get_ctrl_reg(ctrl_reg, addr);
		cmd_reg = adu->_get_cmd_reg(cmd_reg, addr);

		CHECK_ERR(pib_write(&adu->target, ALTD_CONTROL_REG, ctrl_reg));

		/* Start the command */
		CHECK_ERR(pib_write(&adu->target, ALTD_CMD_REG, cmd_reg));

		/* Wait for completion */
		switch (adu->_wait_completion(adu)) {
		case ADU_DONE:
			break;
		case ADU_RETRY:
			goto retry;
			break;
		case ADU_ERR:
			/* Fall through - other return codes also indicate error */
		default:
			PR_ERROR("Unable to read memory\n");
			rc = ADU_ERR;
			break;
		}

		/* Read data */
		CHECK_ERR(pib_read(&adu->target, P8_ALTD_DATA_REG, &data));

		/* ADU returns data in big-endian form in the register */
		data = __builtin_bswap64(data);

		if (addr < start_addr) {
			memcpy(output, ((uint8_t *) &data) + (start_addr - addr), 8 - (start_addr - addr));
			output += 8 - (start_addr - addr);
		} else if (addr + 8 > start_addr + size) {
			memcpy(output, &data, start_addr + size - addr);
		} else {
			memcpy(output, &data, 8);
			output += 8;
		}
	}

	adu_unlock(adu);

	return rc;
}

int p8_adu_putmem(struct adu *adu, uint64_t start_addr, uint8_t *input, uint64_t size)
{
	int rc = 0, tsize;
	uint64_t addr, cmd_reg, ctrl_reg, val, data, end_addr;

	CHECK_ERR(adu_lock(adu));

	ctrl_reg = TTYPE_TWRITE;
	ctrl_reg = SETFIELD(FBC_ALTD_TTYPE, ctrl_reg, TTYPE_DMA_PARTIAL_WRITE);

	CHECK_ERR(pib_read(&adu->target, ALTD_CMD_REG, &cmd_reg));
	cmd_reg |= FBC_ALTD_START_OP;
	cmd_reg = SETFIELD(FBC_ALTD_SCOPE, cmd_reg, SCOPE_SYSTEM);
	cmd_reg = SETFIELD(FBC_ALTD_DROP_PRIORITY, cmd_reg, DROP_PRIORITY_MEDIUM);

	end_addr = start_addr + size;
	for (addr = start_addr; addr < end_addr; addr += tsize, input += tsize) {
		if ((addr % 8) || (addr + 8 > end_addr)) {
			/* If the address is not 64-bit aligned we
			 * copy in a byte at a time until it is. */
			tsize = 1;

			/* Copy the input data in with correct alignment */
			data = ((uint64_t) *input) << 8*(8 - (addr % 8) - 1);
		} else {
			tsize = 8;
			memcpy(&data, input, sizeof(data));
			data = __builtin_bswap64(data);
		}
		ctrl_reg = SETFIELD(FBC_ALTD_TSIZE, ctrl_reg, tsize);

	retry:
		/* Clear status bits */
		CHECK_ERR(adu_reset(adu));

		/* Set the address */
		ctrl_reg = SETFIELD(FBC_ALTD_ADDRESS, ctrl_reg, addr);
		CHECK_ERR(pib_write(&adu->target, ALTD_CONTROL_REG, ctrl_reg));

		/* Write the data */
		CHECK_ERR(pib_write(&adu->target, P8_ALTD_DATA_REG, data));

		/* Start the command */
		CHECK_ERR(pib_write(&adu->target, ALTD_CMD_REG, cmd_reg));

		/* Wait for completion */
		do {
			CHECK_ERR(pib_read(&adu->target, P8_ALTD_STATUS_REG, &val));
		} while (!val);

		if( !(val & FBC_ALTD_ADDR_DONE) ||
		    !(val & FBC_ALTD_DATA_DONE)) {
			/* PBINIT_MISSING is expected occasionally so just retry */
			if (val & FBC_ALTD_PBINIT_MISSING)
				goto retry;
			else {
				PR_ERROR("Unable to write memory. "	\
					 "P8_ALTD_STATUS_REG = 0x%016llx\n", val);
				rc = -1;
				break;
			}
		}
	}

	adu_unlock(adu);

	return rc;
}

struct adu p8_adu = {
	.target = {
		.name =	"POWER8 ADU",
		.compatible = "ibm,power8-adu",
		.class = "adu",
	},
	.getmem = _adu_getmem,
	.putmem = p8_adu_putmem,
	._get_ctrl_reg = p8_get_ctrl_reg,
	._get_cmd_reg = p8_get_cmd_reg,
	._wait_completion = p8_wait_completion,
};
DECLARE_HW_UNIT(p8_adu);



struct adu p9_adu = {
	.target = {
		.name =	"POWER9 ADU",
		.compatible = "ibm,power9-adu",
		.class = "adu",
	},
//	.getmem = p9_adu_getmem,
//	.putmem = p9_adu_putmem,
};
DECLARE_HW_UNIT(p9_adu);
OpenPOWER on IntegriCloud