Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*	$NetBSD: pcictl.c,v 1.22 2016/09/24 23:12:54 mrg Exp $	*/

/*
 * Copyright 2001 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * pcictl(8) -- a program to manipulate the PCI bus
 */

#include <sys/param.h>
#include <sys/ioctl.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <pci.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pciio.h>

struct command {
	const char *cmd_name;
	const char *arg_names;
	void (*cmd_func)(int, char *[]);
	int open_flags;
};

__dead static void	usage(void);

static int	pcifd;

static struct pciio_businfo pci_businfo;

static const	char *dvname;
static char	dvname_store[MAXPATHLEN];
static const	char *cmdname;
static int	print_numbers = 0;
static int	print_names = 0;

static void	cmd_list(int, char *[]);
static void	cmd_dump(int, char *[]);
static void	cmd_read(int, char *[]);
static void	cmd_write(int, char *[]);

static const struct command commands[] = {
	{ "list",
	  "[-Nn] [-b bus] [-d device] [-f function]",
	  cmd_list,
	  O_RDONLY },

	{ "dump",
	  "[-b bus] -d device [-f function]",
	  cmd_dump,
	  O_RDONLY },

	{ "read",
	  "[-b bus] -d device [-f function] reg",
	  cmd_read,
	  O_RDONLY },

	{ "write",
	  "[-b bus] -d device [-f function] reg value",
	  cmd_write,
	  O_WRONLY },

	{ 0, 0, 0, 0 },
};

static int	parse_bdf(const char *);
static u_int	parse_reg(const char *);

static void	scan_pci(int, int, int, void (*)(u_int, u_int, u_int));

static void	scan_pci_list(u_int, u_int, u_int);
static void	scan_pci_dump(u_int, u_int, u_int);

int
main(int argc, char *argv[])
{
	int i;

	/* Must have at least: device command */
	if (argc < 3)
		usage();

	/* Skip program name, get and skip device name, get command. */
	dvname = argv[1];
	cmdname = argv[2];
	argv += 2;
	argc -= 2;

	/* Look up and call the command. */
	for (i = 0; commands[i].cmd_name != NULL; i++)
		if (strcmp(cmdname, commands[i].cmd_name) == 0)
			break;
	if (commands[i].cmd_name == NULL)
		errx(EXIT_FAILURE, "unknown command: %s", cmdname);

	/* Open the device. */
	if ((strchr(dvname, '/') == NULL) &&
	    (snprintf(dvname_store, sizeof(dvname_store), _PATH_DEV "%s",
	     dvname) < (int)sizeof(dvname_store)))
		dvname = dvname_store;
	pcifd = open(dvname, commands[i].open_flags);
	if (pcifd < 0)
		err(EXIT_FAILURE, "%s", dvname);

	/* Make sure the device is a PCI bus. */
	if (ioctl(pcifd, PCI_IOC_BUSINFO, &pci_businfo) != 0)
		errx(EXIT_FAILURE, "%s: not a PCI bus device", dvname);

	(*commands[i].cmd_func)(argc, argv);
	exit(EXIT_SUCCESS);
}

static void
usage(void)
{
	int i;

	fprintf(stderr, "usage: %s device command [arg [...]]\n",
	    getprogname());

	fprintf(stderr, "   Available commands:\n");
	for (i = 0; commands[i].cmd_name != NULL; i++)
		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
		    commands[i].arg_names);

	exit(EXIT_FAILURE);
}

static void
cmd_list(int argc, char *argv[])
{
	int bus, dev, func;
	int ch;

	bus = -1;
	dev = func = -1;

	while ((ch = getopt(argc, argv, "b:d:f:Nn")) != -1) {
		switch (ch) {
		case 'b':
			bus = parse_bdf(optarg);
			break;
		case 'd':
			dev = parse_bdf(optarg);
			break;
		case 'f':
			func = parse_bdf(optarg);
			break;
		case 'n':
			print_numbers = 1;
			break;
		case 'N':
			print_names = 1;
			break;
		default:
			usage();
		}
	}
	argv += optind;
	argc -= optind;

	if (argc != 0)
		usage();

	scan_pci(bus, dev, func, scan_pci_list);
}

static void
cmd_dump(int argc, char *argv[])
{
	int bus, dev, func;
	int ch;

	bus = pci_businfo.busno;
	func = 0;
	dev = -1;

	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
		switch (ch) {
		case 'b':
			bus = parse_bdf(optarg);
			break;
		case 'd':
			dev = parse_bdf(optarg);
			break;
		case 'f':
			func = parse_bdf(optarg);
			break;
		default:
			usage();
		}
	}
	argv += optind;
	argc -= optind;

	if (argc != 0)
		usage();

	if (bus == -1)
		errx(EXIT_FAILURE, "dump: wildcard bus number not permitted");
	if (dev == -1)
		errx(EXIT_FAILURE, "dump: must specify a device number");
	if (func == -1)
		errx(EXIT_FAILURE, "dump: wildcard function number not permitted");

	scan_pci(bus, dev, func, scan_pci_dump);
}

static void
cmd_read(int argc, char *argv[])
{
	int bus, dev, func;
	u_int reg;
	pcireg_t value;
	int ch;

	bus = pci_businfo.busno;
	func = 0;
	dev = -1;

	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
		switch (ch) {
		case 'b':
			bus = parse_bdf(optarg);
			break;
		case 'd':
			dev = parse_bdf(optarg);
			break;
		case 'f':
			func = parse_bdf(optarg);
			break;
		default:
			usage();
		}
	}
	argv += optind;
	argc -= optind;

	if (argc != 1)
		usage();
	reg = parse_reg(argv[0]);
	if (pcibus_conf_read(pcifd, bus, dev, func, reg, &value) == -1)
		err(EXIT_FAILURE, "pcibus_conf_read"
		    "(bus %d dev %d func %d reg %u)", bus, dev, func, reg);
	if (printf("%08x\n", value) < 0)
		err(EXIT_FAILURE, "printf");
}

static void
cmd_write(int argc, char *argv[])
{
	int bus, dev, func;
	u_int reg;
	pcireg_t value;
	int ch;

	bus = pci_businfo.busno;
	func = 0;
	dev = -1;

	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
		switch (ch) {
		case 'b':
			bus = parse_bdf(optarg);
			break;
		case 'd':
			dev = parse_bdf(optarg);
			break;
		case 'f':
			func = parse_bdf(optarg);
			break;
		default:
			usage();
		}
	}
	argv += optind;
	argc -= optind;

	if (argc != 2)
		usage();
	reg = parse_reg(argv[0]);
	__CTASSERT(sizeof(value) == sizeof(u_int));
	value = parse_reg(argv[1]);
	if (pcibus_conf_write(pcifd, bus, dev, func, reg, value) == -1)
		err(EXIT_FAILURE, "pcibus_conf_write"
		    "(bus %d dev %d func %d reg %u value 0x%x)",
		    bus, dev, func, reg, value);
}

static int
parse_bdf(const char *str)
{
	long value;
	char *end;

	if (strcmp(str, "all") == 0 ||
	    strcmp(str, "any") == 0)
		return (-1);

	errno = 0;
	value = strtol(str, &end, 0);
	if ((str[0] == '\0') || (*end != '\0'))
		errx(EXIT_FAILURE, "\"%s\" is not a number", str);
	if ((errno == ERANGE) && ((value == LONG_MIN) || (value == LONG_MAX)))
		errx(EXIT_FAILURE, "out of range: %s", str);
	if ((value < INT_MIN) || (INT_MAX < value))
		errx(EXIT_FAILURE, "out of range: %lu", value);

	return value;
}

static u_int
parse_reg(const char *str)
{
	unsigned long value;
	char *end;

	errno = 0;
	value = strtoul(str, &end, 0);
	if (*end != '\0')
		errx(EXIT_FAILURE, "\"%s\" is not a number", str);
	if ((errno == ERANGE) && (value == ULONG_MAX))
		errx(EXIT_FAILURE, "out of range: %s", str);
	if (UINT_MAX < value)
		errx(EXIT_FAILURE, "out of range: %lu", value);

	return value;
}

static void
scan_pci(int busarg, int devarg, int funcarg, void (*cb)(u_int, u_int, u_int))
{
	u_int busmin, busmax;
	u_int devmin, devmax;
	u_int funcmin, funcmax;
	u_int bus, dev, func;
	pcireg_t id, bhlcr;

	if (busarg == -1) {
		busmin = 0;
		busmax = 255;
	} else
		busmin = busmax = busarg;

	if (devarg == -1) {
		devmin = 0;
		if (pci_businfo.maxdevs <= 0)
			devmax = 0;
		else
			devmax = pci_businfo.maxdevs - 1;
	} else
		devmin = devmax = devarg;

	for (bus = busmin; bus <= busmax; bus++) {
		for (dev = devmin; dev <= devmax; dev++) {
			if (pcibus_conf_read(pcifd, bus, dev, 0,
			    PCI_BHLC_REG, &bhlcr) != 0)
				continue;
			if (funcarg == -1) {
				funcmin = 0;
				if (PCI_HDRTYPE_MULTIFN(bhlcr))
					funcmax = 7;
				else
					funcmax = 0;
			} else
				funcmin = funcmax = funcarg;
			for (func = funcmin; func <= funcmax; func++) {
				if (pcibus_conf_read(pcifd, bus, dev,
				    func, PCI_ID_REG, &id) != 0)
					continue;

				/* Invalid vendor ID value? */
				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
					continue;
				/*
				 * XXX Not invalid, but we've done this
				 * ~forever.
				 */
				if (PCI_VENDOR(id) == 0)
					continue;

				(*cb)(bus, dev, func);
			}
		}
	}
}

static void
scan_pci_list(u_int bus, u_int dev, u_int func)
{
	pcireg_t id, class;
	char devinfo[256];

	if (pcibus_conf_read(pcifd, bus, dev, func, PCI_ID_REG, &id) != 0)
		return;
	if (pcibus_conf_read(pcifd, bus, dev, func, PCI_CLASS_REG, &class) != 0)
		return;

	printf("%03u:%02u:%01u: ", bus, dev, func);
	if (print_numbers) {
		printf("0x%08x (0x%08x)", id, class);
	} else {
		pci_devinfo(id, class, 1, devinfo, sizeof(devinfo));
		printf("%s", devinfo);
	}
	if (print_names) {
		char drvname[16];
		if (pci_drvnameonbus(pcifd, bus, dev, func, drvname,
				     sizeof drvname) == 0)
			printf(" [%s]", drvname);
	}
	printf("\n");
}

static void
scan_pci_dump(u_int bus, u_int dev, u_int func)
{

	pci_conf_print(pcifd, bus, dev, func);
}