2 * $Id: setpci.c,v 1.4 1998/06/09 19:22:05 mj Exp $
4 * Linux PCI Utilities -- Manipulate PCI Configuration Registers
6 * Copyright (c) 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
19 #include <asm/byteorder.h>
21 #include <asm/unistd.h>
23 #include <syscall-list.h>
28 static int force; /* Don't complain if no devices match */
29 static int verbose; /* Verbosity level */
30 static int demo_mode; /* Only show */
34 byte bus, devfn, mark;
39 static struct device *first_dev;
43 struct device **dev_vector;
45 unsigned int width; /* Byte width of the access */
46 int num_values; /* Number of values to write; <0=read */
47 unsigned int values[0];
50 static struct op *first_op, **last_op = &first_op;
53 xmalloc(unsigned int howmuch)
55 void *p = malloc(howmuch);
58 fprintf(stderr, "setpci: Unable to allocate %d bytes of memory\n", howmuch);
65 * As libc doesn't support pread/pwrite yet, we have to call them directly
66 * or use lseek/read/write instead.
70 #define SYS_pread __NR_pread
73 pread(unsigned int fd, void *buf, size_t size, loff_t where)
75 return syscall(SYS_pread, fd, buf, size, where);
79 #define SYS_pwrite __NR_pwrite
82 pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
84 return syscall(SYS_pwrite, fd, buf, size, where);
87 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
88 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
94 struct device **last = &first_dev;
98 if (!(f = fopen(PROC_BUS_PCI "/devices", "r")))
100 perror(PROC_BUS_PCI "/devices");
103 while (fgets(line, sizeof(line), f))
105 struct device *d = xmalloc(sizeof(struct device));
106 unsigned int dfn, vend;
108 sscanf(line, "%x %x", &dfn, &vend);
110 d->devfn = dfn & 0xff;
111 d->vendid = vend >> 16U;
112 d->devid = vend & 0xffff;
121 static struct device **
122 select_devices(struct pci_filter *filt)
124 struct device *z, **a, **b;
127 for(z=first_dev; z; z=z->next)
128 if (z->mark = filter_match(filt, z->bus, z->devfn, z->vendid, z->devid))
130 a = b = xmalloc(sizeof(struct device *) * cnt);
131 for(z=first_dev; z; z=z->next)
139 exec_op(struct op *op, struct device *dev)
141 char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
142 char *m = mm[op->width];
149 if (!demo_mode && dev->fd < 0)
152 sprintf(name, PROC_BUS_PCI "/%02x/%02x.%x", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
153 if ((dev->fd = open(name, dev->need_write ? O_RDWR : O_RDONLY)) < 0)
161 printf("%02x:%02x.%x:%02x", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), op->addr);
162 if (op->num_values >= 0)
163 for(i=0; i<op->num_values; i++)
168 printf(m, op->values[i]);
176 i = pwrite(dev->fd, &x8, 1, op->addr);
179 x16 = __cpu_to_le16(op->values[i]);
180 i = pwrite(dev->fd, &x16, 2, op->addr);
183 x32 = __cpu_to_le32(op->values[i]);
184 i = pwrite(dev->fd, &x32, 4, op->addr);
187 if (i != (int) op->width)
189 fprintf(stderr, "Error writing to %02x:%02x.%d: %m\n", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
202 i = pread(dev->fd, &x8, 1, op->addr);
206 i = pread(dev->fd, &x16, 2, op->addr);
207 x = __le16_to_cpu(x16);
210 i = pread(dev->fd, &x32, 4, op->addr);
211 x = __le32_to_cpu(x32);
214 if (i != (int) op->width)
216 fprintf(stderr, "Error reading from %02x:%02x.%d: %m\n", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
228 execute(struct op *op)
230 struct device **vec = NULL;
231 struct device **pdev, *dev;
236 pdev = vec = op->dev_vector;
237 while (dev = *pdev++)
238 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
240 while (op && op->dev_vector == vec)
246 scan_ops(struct op *op)
248 struct device **pdev, *dev;
252 if (op->num_values >= 0)
254 pdev = op->dev_vector;
255 while (dev = *pdev++)
268 static struct reg_name pci_reg_names[] = {
269 { 0x00, 2, "VENDOR_ID", },
270 { 0x02, 2, "DEVICE_ID", },
271 { 0x04, 2, "COMMAND", },
272 { 0x06, 2, "STATUS", },
273 { 0x08, 1, "REVISION", },
274 { 0x09, 1, "CLASS_PROG", },
275 { 0x0a, 2, "CLASS_DEVICE", },
276 { 0x0c, 1, "CACHE_LINE_SIZE", },
277 { 0x0d, 1, "LATENCY_TIMER", },
278 { 0x0e, 1, "HEADER_TYPE", },
279 { 0x0f, 1, "BIST", },
280 { 0x10, 4, "BASE_ADDRESS_0", },
281 { 0x14, 4, "BASE_ADDRESS_1", },
282 { 0x18, 4, "BASE_ADDRESS_2", },
283 { 0x1c, 4, "BASE_ADDRESS_3", },
284 { 0x20, 4, "BASE_ADDRESS_4", },
285 { 0x24, 4, "BASE_ADDRESS_5", },
286 { 0x28, 4, "CARDBUS_CIS", },
287 { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
288 { 0x2e, 2, "SUBSYSTEM_ID", },
289 { 0x30, 4, "ROM_ADDRESS", },
290 { 0x3c, 1, "INTERRUPT_LINE", },
291 { 0x3d, 1, "INTERRUPT_PIN", },
292 { 0x3e, 1, "MIN_GNT", },
293 { 0x3f, 1, "MAX_LAT", },
294 { 0x18, 1, "PRIMARY_BUS", },
295 { 0x19, 1, "SECONDARY_BUS", },
296 { 0x1a, 1, "SUBORDINATE_BUS", },
297 { 0x1b, 1, "SEC_LATENCY_TIMER", },
298 { 0x1c, 1, "IO_BASE", },
299 { 0x1d, 1, "IO_LIMIT", },
300 { 0x1e, 2, "SEC_STATUS", },
301 { 0x20, 2, "MEMORY_BASE", },
302 { 0x22, 2, "MEMORY_LIMIT", },
303 { 0x24, 2, "PREF_MEMORY_BASE", },
304 { 0x26, 2, "PREF_MEMORY_LIMIT", },
305 { 0x28, 4, "PREF_BASE_UPPER32", },
306 { 0x2c, 4, "PREF_LIMIT_UPPER32", },
307 { 0x30, 2, "IO_BASE_UPPER16", },
308 { 0x32, 2, "IO_LIMIT_UPPER16", },
309 { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
310 { 0x3e, 2, "BRIDGE_CONTROL", },
311 { 0x10, 4, "CB_CARDBUS_BASE", },
312 { 0x14, 2, "CB_CAPABILITIES", },
313 { 0x16, 2, "CB_SEC_STATUS", },
314 { 0x18, 1, "CB_BUS_NUMBER", },
315 { 0x19, 1, "CB_CARDBUS_NUMBER", },
316 { 0x1a, 1, "CB_SUBORDINATE_BUS", },
317 { 0x1b, 1, "CB_CARDBUS_LATENCY", },
318 { 0x1c, 4, "CB_MEMORY_BASE_0", },
319 { 0x20, 4, "CB_MEMORY_LIMIT_0", },
320 { 0x24, 4, "CB_MEMORY_BASE_1", },
321 { 0x28, 4, "CB_MEMORY_LIMIT_1", },
322 { 0x2c, 2, "CB_IO_BASE_0", },
323 { 0x2e, 2, "CB_IO_BASE_0_HI", },
324 { 0x30, 2, "CB_IO_LIMIT_0", },
325 { 0x32, 2, "CB_IO_LIMIT_0_HI", },
326 { 0x34, 2, "CB_IO_BASE_1", },
327 { 0x36, 2, "CB_IO_BASE_1_HI", },
328 { 0x38, 2, "CB_IO_LIMIT_1", },
329 { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
330 { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
331 { 0x42, 2, "CB_SUBSYSTEM_ID", },
332 { 0x44, 4, "CB_LEGACY_MODE_BASE", },
336 static void usage(void) __attribute__((noreturn));
342 "Usage: setpci [-fvD] (<device>+ <reg>[=<values>]*)*\n\
343 -f\t\tDon't complain if there's nothing to do\n\
345 -D\t\tList changes, don't commit them\n\
346 <device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
347 \t|\t-d [<vendor>]:[<device>]\n\
348 <reg>:\t\t<number>[.(B|W|L)]\n\
350 <values>:\t<value>[,<value>...]\n\
356 main(int argc, char **argv)
358 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
359 struct pci_filter filter;
360 struct device **selected_devices = NULL;
362 if (argc == 2 && !strcmp(argv[1], "--version"))
364 puts("setpci version " PCIUTILS_VERSION);
369 while (argc && argv[0][0] == '-')
408 unsigned long ll, lim;
412 if (!c[1] || !strchr("sd", c[1]))
415 d = (c[2] == '=') ? c+3 : c+2;
424 if (state != STATE_GOT_FILTER)
426 filter_init(&filter);
427 state = STATE_GOT_FILTER;
432 if (d = filter_parse_slot(&filter, d))
434 fprintf(stderr, "setpci: -s: %s\n", d);
439 if (d = filter_parse_id(&filter, d))
441 fprintf(stderr, "setpci: -d: %s\n", d);
449 else if (state == STATE_INIT)
453 if (state == STATE_GOT_FILTER)
454 selected_devices = select_devices(&filter);
455 if (!selected_devices[0] && !force)
456 fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
457 state = STATE_GOT_OP;
464 for(e=d, n=1; *e; e++)
467 op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int));
472 op = xmalloc(sizeof(struct op));
474 op->dev_vector = selected_devices;
485 op->width = 1; break;
487 op->width = 2; break;
489 op->width = 4; break;
496 ll = strtol(c, &f, 16);
500 for(r = pci_reg_names; r->name; r++)
501 if (!strcasecmp(r->name, c))
506 op->width = r->width;
508 if (ll > 0x100 || ll + op->width*((n < 0) ? 1 : n) > 0x100)
510 fprintf(stderr, "setpci: Register number out of range!\n");
513 if (ll & (op->width - 1))
515 fprintf(stderr, "setpci: Unaligned register address!\n");
524 ll = strtoul(d, &f, 16);
525 lim = (2 << ((op->width << 3) - 1)) - 1;
527 (ll > lim && ll < ~0UL - lim))
539 if (state == STATE_INIT)