summaryrefslogtreecommitdiff
path: root/tools/intel_l3_parity.c
blob: 484dd0281b172a84ad976d609f64ba352ebcc260 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * Copyright © 2012 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Ben Widawsky <ben@bwidawsk.net>
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "intel_chipset.h"
#include "intel_io.h"
#include "igt_sysfs.h"
#include "drmtest.h"
#include "config.h"
#include <libudev.h>
#include <syslog.h>
#include "intel_l3_parity.h"

static unsigned int devid;

/* L3 size is always a function of banks. The number of banks cannot be
 * determined by number of slices however */
static inline int num_banks(void) {
	switch (intel_get_device_info(devid)->gt) {
	case 2: return 8;
	case 1: return 4;
	default: return 2;
	}
}
#define NUM_SUBBANKS 8
#define BYTES_PER_BANK (128 << 10)
/* Each row addresses [up to] 4b. This multiplied by the number of subbanks
 * will give the L3 size per bank.
 * TODO: Row size is fixed on IVB, and variable on HSW.*/
#define MAX_ROW (1<<12)
#define MAX_BANKS_PER_SLICE 4
#define NUM_REGS (MAX_BANKS_PER_SLICE * NUM_SUBBANKS)
#define MAX_SLICES (intel_get_device_info(devid)->gt > 1 ? 2 : 1)
#define REAL_MAX_SLICES 2
/* TODO support SLM config */
#define L3_SIZE ((MAX_ROW * 4) * NUM_SUBBANKS *  num_banks())

struct __attribute__ ((__packed__)) l3_log_register {
	uint32_t row0_enable	: 1;
	uint32_t rsvd2		: 4;
	uint32_t row0		: 11;
	uint32_t row1_enable	: 1;
	uint32_t rsvd1		: 4;
	uint32_t row1		: 11;
} l3logs[REAL_MAX_SLICES][MAX_BANKS_PER_SLICE][NUM_SUBBANKS];

static int which_slice = -1;
#define for_each_slice(__i) \
	for ((__i) = (which_slice == -1) ? 0 : which_slice; \
			(__i) < ((which_slice == -1) ? MAX_SLICES : (which_slice + 1)); \
			(__i)++)

static void decode_dft(uint32_t dft)
{
	if (IS_IVYBRIDGE(devid) || !(dft & 1)) {
		printf("Error injection disabled\n");
		return;
	}
	printf("Error injection enabled\n");
	printf("  Hang = %s\n", (dft >> 28) & 0x1 ? "yes" : "no");
	printf("  Row = %d\n", (dft >> 7) & 0x7ff);
	printf("  Bank = %d\n", (dft >> 2) & 0x3);
	printf("  Subbank = %d\n", (dft >> 4) & 0x7);
	printf("  Slice = %d\n", (dft >> 1) & 0x1);
}

static void dumpit(int slice)
{
	int i, j;

	for (i = 0; i < MAX_BANKS_PER_SLICE; i++) {
		for (j = 0; j < NUM_SUBBANKS; j++) {
			struct l3_log_register *reg = &l3logs[slice][i][j];

			if (reg->row0_enable)
				printf("Slice %d, Row %d, Bank %d, Subbank %d is disabled\n",
				       slice, reg->row0, i, j);
			if (reg->row1_enable)
				printf("Slice %d, Row %d, Bank %d, Subbank %d is disabled\n",
				       slice, reg->row1, i, j);
		}
	}
}

static int disable_rbs(int row, int bank, int sbank, int slice)
{
	struct l3_log_register *reg = &l3logs[slice][bank][sbank];

	// can't map more than 2 rows
	if (reg->row0_enable && reg->row1_enable)
		return -1;

	// can't remap the same row twice
	if ((reg->row0_enable && reg->row0 == row) ||
	    (reg->row1_enable && reg->row1 == row)) {
		return -1;
	}

	if (reg->row0_enable) {
		reg->row1 = row;
		reg->row1_enable = 1;
	} else {
		reg->row0 = row;
		reg->row0_enable = 1;
	}

	return 0;
}

static void enables_rbs(int row, int bank, int sbank, int slice)
{
	struct l3_log_register *reg = &l3logs[slice][bank][sbank];

	if (!reg->row0_enable && !reg->row1_enable)
		return;

	if (reg->row1_enable && reg->row1 == row)
		reg->row1_enable = 0;
	else if (reg->row0_enable && reg->row0 == row)
		reg->row0_enable = 0;
}

static void usage(const char *name)
{
	printf("usage: %s [OPTIONS] [ACTION]\n"
		"Operate on the i915 L3 GPU cache (should be run as root)\n\n"
		" OPTIONS:\n"
		"  -r, --row=[row]			The row to act upon (default 0)\n"
		"  -b, --bank=[bank]			The bank to act upon (default 0)\n"
		"  -s, --subbank=[subbank]		The subbank to act upon (default 0)\n"
		"  -w, --slice=[slice]			Which slice to act on (default: -1 [all])\n"
		"    , --daemon				Run the listener (-L) as a daemon\n"
		" ACTIONS (only 1 may be specified at a time):\n"
		"  -h, --help				Display this help\n"
		"  -H, --hw-info				Display the current L3 properties\n"
		"  -l, --list				List the current L3 logs\n"
		"  -a, --clear-all			Clear all disabled rows\n"
		"  -e, --enable				Enable row, bank, subbank (undo -d)\n"
		"  -d, --disable=<row,bank,subbank>	Disable row, bank, subbank (inline arguments are deprecated. Please use -r, -b, -s instead\n"
		"  -i, --inject				[HSW only] Cause hardware to inject a row errors\n"
		"  -u, --uninject			[HSW only] Turn off hardware error injectection (undo -i)\n"
		"  -L, --listen				Listen for uevent errors\n",
		name);
}

int main(int argc, char *argv[])
{
	struct intel_mmio_data mmio_data;
	const char *path[REAL_MAX_SLICES] = {"l3_parity", "l3_parity_slice_1"};
	int row = 0, bank = 0, sbank = 0;
	int fd[REAL_MAX_SLICES] = {0}, ret, i;
	int exitcode = EXIT_FAILURE;
	int action = '0';
	int daemonize = 0;
	int device, dir;
	uint32_t dft;

	device = drm_open_driver(DRIVER_INTEL);
	devid = intel_get_drm_devid(device);
	if (intel_gen(devid) < 7 || IS_VALLEYVIEW(devid))
		exit(77);

	assert(intel_register_access_init(&mmio_data, intel_get_pci_device(), 0, device) == 0);

	dir = igt_sysfs_open(device);

	for_each_slice(i) {
		fd[i] = openat(dir, path[i], O_RDWR);
		if (fd[i] < 0) {
			if (i == 0) /* at least one slice must be supported */
				goto skip;
			continue;
		}

		if (read(fd[i], l3logs[i], NUM_REGS * sizeof(uint32_t)) < 0) {
			perror(path[i]);
			goto skip;
		}
		assert(lseek(fd[i], 0, SEEK_SET) == 0);
	}

	close(dir);

	/* NB: It is potentially unsafe to read this register if the kernel is
	 * actively using this register range, or we're running multiple
	 * instances of this tool. Since neither of those cases should occur
	 * (and the tool should be root only) we can safely ignore this for
	 * now. Just be aware of this if for some reason a hang is reported
	 * when using this tool.
	 */
	dft = intel_register_read(&mmio_data, 0xb038);

	while (1) {
		int c, option_index = 0;
		struct option long_options[] = {
			{ "help", no_argument, 0, 'h' },
			{ "list", no_argument, 0, 'l' },
			{ "clear-all", no_argument, 0, 'a' },
			{ "enable", no_argument, 0, 'e' },
			{ "disable", optional_argument, 0, 'd' },
			{ "inject", no_argument, 0, 'i' },
			{ "uninject", no_argument, 0, 'u' },
			{ "hw-info", no_argument, 0, 'H' },
			{ "listen", no_argument, 0, 'L' },
			{ "row", required_argument, 0, 'r' },
			{ "bank", required_argument, 0, 'b' },
			{ "subbank", required_argument, 0, 's' },
			{ "slice", required_argument, 0, 'w' },
			{ "daemon", no_argument, &daemonize, 1 },
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "hHr:b:s:w:aled::iuL", long_options,
				&option_index);
		if (c == -1)
			break;

		if (c == 0)
			continue;

		switch (c) {
			case '?':
			case 'h':
				usage(argv[0]);
				goto success;
			case 'H':
				printf("Number of slices: %d\n", MAX_SLICES);
				printf("Number of banks: %d\n", num_banks());
				printf("Subbanks per bank: %d\n", NUM_SUBBANKS);
				printf("Max L3 size: %dK\n", L3_SIZE >> 10);
				printf("Has error injection: %s\n", IS_HASWELL(devid) ? "yes" : "no");
				goto success;
			case 'r':
				row = atoi(optarg);
				if (row >= MAX_ROW)
					goto failure;
				break;
			case 'b':
				bank = atoi(optarg);
				if (bank >= num_banks() || bank >= MAX_BANKS_PER_SLICE)
					goto failure;
				break;
			case 's':
				sbank = atoi(optarg);
				if (sbank >= NUM_SUBBANKS)
					goto failure;
				break;
			case 'w':
				which_slice = atoi(optarg);
				if (which_slice >= MAX_SLICES)
					goto failure;
				break;
			case 'i':
			case 'u':
				if (!IS_HASWELL(devid)) {
					fprintf(stderr, "Error injection supported on HSW+ only\n");
					goto failure;
				}
			case 'd':
				if (optarg) {
					ret = sscanf(optarg, "%d,%d,%d", &row, &bank, &sbank);
					if (ret != 3)
						goto failure;
				}
			case 'a':
			case 'l':
			case 'e':
			case 'L':
				if (action != '0') {
					fprintf(stderr, "Only one action may be specified\n");
					goto failure;
				}
				action = c;
				break;
			default:
				goto failure;
		}
	}

	if (action == 'i') {
		if (((dft >> 1) & 1) != which_slice) {
			fprintf(stderr, "DFT register already has slice %d enabled, and we don't support multiple slices. Try modifying -w; but sometimes the register sticks in the wrong way\n", (dft >> 1) & 1);
			goto failure;
		}

		if (which_slice == -1) {
			fprintf(stderr, "Cannot inject errors to multiple slices (modify -w)\n");
			goto failure;
		}
		if (dft & 1 && ((dft >> 1) && 1) == which_slice)
			printf("warning: overwriting existing injections. This is very dangerous.\n");
	}

	/* Daemon doesn't work like the other commands */
	if (action == 'L') {
		struct l3_parity par;
		struct l3_location loc;
		if (daemonize) {
			assert(daemon(0, 0) == 0);
			openlog(argv[0], LOG_CONS | LOG_PID, LOG_USER);
		}
		memset(&par, 0, sizeof(par));
		assert(l3_uevent_setup(&par) == 0);
		assert(l3_listen(&par, daemonize == 1, &loc) == 0);
		goto success;
	}

	if (action == 'l')
		decode_dft(dft);

	/* Per slice operations */
	for_each_slice(i) {
		if (fd[i] < 0)
			continue;

		switch (action) {
			case 'l':
				dumpit(i);
				break;
			case 'a':
				memset(l3logs[i], 0, NUM_REGS * sizeof(struct l3_log_register));
				break;
			case 'e':
				enables_rbs(row, bank, sbank, i);
				break;
			case 'd':
				assert(disable_rbs(row, bank, sbank, i) == 0);
				break;
			case 'i':
				if (bank == 3) {
					fprintf(stderr, "The hardware does not support error inject on bank 3.\n");
					goto failure;
				}
				dft |= row << 7;
				dft |= sbank << 4;
				dft |= bank << 2;
				assert(i < 2);
				dft |= i << 1; /* slice */
				dft |= 1 << 0; /* enable */
				intel_register_write(&mmio_data, 0xb038, dft);
				break;
			case 'u':
				intel_register_write(&mmio_data ,0xb038, dft & ~(1<<0));
				break;
			case 'L':
				break;
			default:
				goto failure;
		}
	}

	if (action == 'l')
		goto success;

	for_each_slice(i) {
		if (fd[i] < 0)
			continue;

		ret = write(fd[i], l3logs[i], NUM_REGS * sizeof(uint32_t));
		if (ret == -1) {
			perror("Writing sysfs");
			goto failure;
		}
		close(fd[i]);
	}


success:
	exitcode = EXIT_SUCCESS;
failure:
	intel_register_access_fini(&mmio_data);
	return exitcode;

skip:
	exitcode = 77;
	goto failure;
}