2 * The PCI Utilities -- Manipulate PCI Configuration Registers
4 * Copyright (c) 1998--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
15 #define PCIUTILS_SETPCI
18 static int force; /* Don't complain if no devices match */
19 static int verbose; /* Verbosity level */
20 static int demo_mode; /* Only show */
22 const char program_name[] = "setpci";
24 static struct pci_access *pacc;
33 struct pci_dev **dev_vector;
35 unsigned int width; /* Byte width of the access */
36 int num_values; /* Number of values to write; <0=read */
37 struct value values[0];
40 static struct op *first_op, **last_op = &first_op;
41 static unsigned int max_values[] = { 0, 0xff, 0xffff, 0, 0xffffffff };
43 static struct pci_dev **
44 select_devices(struct pci_filter *filt)
46 struct pci_dev *z, **a, **b;
49 for(z=pacc->devices; z; z=z->next)
50 if (pci_filter_match(filt, z))
52 a = b = xmalloc(sizeof(struct device *) * cnt);
53 for(z=pacc->devices; z; z=z->next)
54 if (pci_filter_match(filt, z))
61 exec_op(struct op *op, struct pci_dev *dev)
63 char *formats[] = { NULL, "%02x", "%04x", NULL, "%08x" };
64 char *mask_formats[] = { NULL, "%02x->(%02x:%02x)->%02x", "%04x->(%04x:%04x)->%04x", NULL, "%08x->(%08x:%08x)->%08x" };
67 int width = op->width;
70 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
72 if (op->num_values >= 0)
74 for(i=0; i<op->num_values; i++)
76 if ((op->values[i].mask & max_values[width]) == max_values[width])
78 x = op->values[i].value;
82 printf(formats[width], op->values[i].value);
90 y = pci_read_byte(dev, addr);
93 y = pci_read_word(dev, addr);
96 y = pci_read_long(dev, addr);
99 x = (y & ~op->values[i].mask) | op->values[i].value;
103 printf(mask_formats[width], y, op->values[i].value, op->values[i].mask, x);
111 pci_write_byte(dev, addr, x);
114 pci_write_word(dev, addr, x);
117 pci_write_long(dev, addr, x);
133 x = pci_read_byte(dev, addr);
136 x = pci_read_word(dev, addr);
139 x = pci_read_long(dev, addr);
142 printf(formats[width], x);
148 execute(struct op *op)
150 struct pci_dev **vec = NULL;
151 struct pci_dev **pdev, *dev;
156 pdev = vec = op->dev_vector;
157 while (dev = *pdev++)
158 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
160 while (op && op->dev_vector == vec)
166 scan_ops(struct op *op)
170 if (op->num_values >= 0)
182 static const struct reg_name pci_reg_names[] = {
183 { 0x00, 2, "VENDOR_ID", },
184 { 0x02, 2, "DEVICE_ID", },
185 { 0x04, 2, "COMMAND", },
186 { 0x06, 2, "STATUS", },
187 { 0x08, 1, "REVISION", },
188 { 0x09, 1, "CLASS_PROG", },
189 { 0x0a, 2, "CLASS_DEVICE", },
190 { 0x0c, 1, "CACHE_LINE_SIZE", },
191 { 0x0d, 1, "LATENCY_TIMER", },
192 { 0x0e, 1, "HEADER_TYPE", },
193 { 0x0f, 1, "BIST", },
194 { 0x10, 4, "BASE_ADDRESS_0", },
195 { 0x14, 4, "BASE_ADDRESS_1", },
196 { 0x18, 4, "BASE_ADDRESS_2", },
197 { 0x1c, 4, "BASE_ADDRESS_3", },
198 { 0x20, 4, "BASE_ADDRESS_4", },
199 { 0x24, 4, "BASE_ADDRESS_5", },
200 { 0x28, 4, "CARDBUS_CIS", },
201 { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
202 { 0x2e, 2, "SUBSYSTEM_ID", },
203 { 0x30, 4, "ROM_ADDRESS", },
204 { 0x3c, 1, "INTERRUPT_LINE", },
205 { 0x3d, 1, "INTERRUPT_PIN", },
206 { 0x3e, 1, "MIN_GNT", },
207 { 0x3f, 1, "MAX_LAT", },
208 { 0x18, 1, "PRIMARY_BUS", },
209 { 0x19, 1, "SECONDARY_BUS", },
210 { 0x1a, 1, "SUBORDINATE_BUS", },
211 { 0x1b, 1, "SEC_LATENCY_TIMER", },
212 { 0x1c, 1, "IO_BASE", },
213 { 0x1d, 1, "IO_LIMIT", },
214 { 0x1e, 2, "SEC_STATUS", },
215 { 0x20, 2, "MEMORY_BASE", },
216 { 0x22, 2, "MEMORY_LIMIT", },
217 { 0x24, 2, "PREF_MEMORY_BASE", },
218 { 0x26, 2, "PREF_MEMORY_LIMIT", },
219 { 0x28, 4, "PREF_BASE_UPPER32", },
220 { 0x2c, 4, "PREF_LIMIT_UPPER32", },
221 { 0x30, 2, "IO_BASE_UPPER16", },
222 { 0x32, 2, "IO_LIMIT_UPPER16", },
223 { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
224 { 0x3e, 2, "BRIDGE_CONTROL", },
225 { 0x10, 4, "CB_CARDBUS_BASE", },
226 { 0x14, 2, "CB_CAPABILITIES", },
227 { 0x16, 2, "CB_SEC_STATUS", },
228 { 0x18, 1, "CB_BUS_NUMBER", },
229 { 0x19, 1, "CB_CARDBUS_NUMBER", },
230 { 0x1a, 1, "CB_SUBORDINATE_BUS", },
231 { 0x1b, 1, "CB_CARDBUS_LATENCY", },
232 { 0x1c, 4, "CB_MEMORY_BASE_0", },
233 { 0x20, 4, "CB_MEMORY_LIMIT_0", },
234 { 0x24, 4, "CB_MEMORY_BASE_1", },
235 { 0x28, 4, "CB_MEMORY_LIMIT_1", },
236 { 0x2c, 2, "CB_IO_BASE_0", },
237 { 0x2e, 2, "CB_IO_BASE_0_HI", },
238 { 0x30, 2, "CB_IO_LIMIT_0", },
239 { 0x32, 2, "CB_IO_LIMIT_0_HI", },
240 { 0x34, 2, "CB_IO_BASE_1", },
241 { 0x36, 2, "CB_IO_BASE_1_HI", },
242 { 0x38, 2, "CB_IO_LIMIT_1", },
243 { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
244 { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
245 { 0x42, 2, "CB_SUBSYSTEM_ID", },
246 { 0x44, 4, "CB_LEGACY_MODE_BASE", },
250 static void NONRET PCI_PRINTF(1,2)
251 usage(char *msg, ...)
257 fprintf(stderr, "setpci: ");
258 vfprintf(stderr, msg, args);
259 fprintf(stderr, "\n\n");
262 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n"
265 "-f\t\tDon't complain if there's nothing to do\n"
267 "-D\t\tList changes, don't commit them\n"
269 "PCI access options:\n"
272 "Setting commands:\n"
273 "<device>:\t-s [[[<domain>]:][<bus>]:][<slot>][.[<func>]]\n"
274 "\t|\t-d [<vendor>]:[<device>]\n"
275 "<reg>:\t\t<number>[.(B|W|L)]\n"
277 "<values>:\t<value>[,<value>...]\n"
279 " |\t<hex>:<mask>\n");
284 parse_options(int argc, char **argv)
286 char *opts = GENERIC_OPTIONS;
289 if (argc == 2 && !strcmp(argv[1], "--version"))
291 puts("setpci version " PCIUTILS_VERSION);
295 while (i < argc && argv[i][0] == '-')
318 if (e = strchr(opts, *c))
337 if (!parse_generic_option(*e, pacc, arg))
353 static void parse_ops(int argc, char **argv, int i)
355 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
356 struct pci_filter filter;
357 struct pci_dev **selected_devices = NULL;
370 if (!c[1] || !strchr("sd", c[1]))
373 d = (c[2] == '=') ? c+3 : c+2;
382 if (state != STATE_GOT_FILTER)
384 pci_filter_init(pacc, &filter);
385 state = STATE_GOT_FILTER;
390 if (d = pci_filter_parse_slot(&filter, d))
394 if (d = pci_filter_parse_id(&filter, d))
401 else if (state == STATE_INIT)
405 if (state == STATE_GOT_FILTER)
406 selected_devices = select_devices(&filter);
407 if (!selected_devices[0] && !force)
408 fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
409 state = STATE_GOT_OP;
410 /* look for setting of values and count how many */
416 usage("Missing value");
417 for(e=d, n=1; *e; e++)
420 op = xmalloc(sizeof(struct op) + n*sizeof(struct value));
425 op = xmalloc(sizeof(struct op));
427 op->dev_vector = selected_devices;
434 usage("Missing width");
438 op->width = 1; break;
440 op->width = 2; break;
442 op->width = 4; break;
444 usage("Invalid width \"%c\"", *e);
449 ll = strtol(c, &f, 16);
452 const struct reg_name *r;
453 for(r = pci_reg_names; r->name; r++)
454 if (!strcasecmp(r->name, c))
457 usage("Unknown register \"%s\"", c);
458 if (e && op->width != r->width)
459 usage("Explicit width doesn't correspond with the named register \"%s\"", c);
461 op->width = r->width;
463 if (ll > 0x1000 || ll + op->width*((n < 0) ? 1 : n) > 0x1000)
464 die("Register number out of range!");
465 if (ll & (op->width - 1))
466 die("Unaligned register address!");
468 /* read in all the values to be set */
474 ll = strtoul(d, &f, 16);
475 lim = max_values[op->width];
476 if (f && *f && *f != ':')
477 usage("Invalid value \"%s\"", d);
478 if (ll > lim && ll < ~0UL - lim)
479 usage("Value \"%s\" is out of range", d);
480 op->values[j].value = ll;
484 ll = strtoul(d, &f, 16);
486 usage("Invalid mask \"%s\"", d);
487 if (ll > lim && ll < ~0UL - lim)
488 usage("Mask \"%s\" is out of range", d);
489 op->values[j].mask = ll;
490 op->values[j].value &= ll;
493 op->values[j].mask = ~0U;
502 if (state == STATE_INIT)
503 usage("No operation specified");
507 main(int argc, char **argv)
513 i = parse_options(argc, argv);
518 parse_ops(argc, argv, i);