blob: bb347cde37022f858fab3e25e790330ef43d45cc [file] [log] [blame]
uwea3a82c92009-05-15 17:02:34 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
hailfingerbf923c32011-02-15 22:44:27 +00005 * Copyright (C) 2010, 2011 Carl-Daniel Hailfinger
uwea3a82c92009-05-15 17:02:34 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdlib.h>
23#include <string.h>
uwea3a82c92009-05-15 17:02:34 +000024#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000025#include "programmer.h"
uwea3a82c92009-05-15 17:02:34 +000026
uwea3a82c92009-05-15 17:02:34 +000027struct pci_access *pacc;
uweb3a82ef2009-05-16 21:39:19 +000028struct pci_dev *pcidev_dev = NULL;
uwea3a82c92009-05-15 17:02:34 +000029
hailfingerbf923c32011-02-15 22:44:27 +000030enum pci_bartype {
31 TYPE_MEMBAR,
32 TYPE_IOBAR,
33 TYPE_ROMBAR,
34 TYPE_UNKNOWN
35};
36
Patrick Georgif776a442017-03-28 21:34:33 +020037uintptr_t pcidev_readbar(struct pci_dev *dev, int bar)
uwea3a82c92009-05-15 17:02:34 +000038{
hailfingerbf923c32011-02-15 22:44:27 +000039 uint64_t addr;
40 uint32_t upperaddr;
41 uint8_t headertype;
42 uint16_t supported_cycles;
43 enum pci_bartype bartype = TYPE_UNKNOWN;
uwea3a82c92009-05-15 17:02:34 +000044
Patrick Georgif776a442017-03-28 21:34:33 +020045
46 headertype = pci_read_byte(dev, PCI_HEADER_TYPE) & 0x7f;
47 msg_pspew("PCI header type 0x%02x\n", headertype);
48
49 /* Don't use dev->base_addr[x] (as value for 'bar'), won't work on older libpci. */
50 addr = pci_read_long(dev, bar);
51
52 /* Sanity checks. */
53 switch (headertype) {
54 case PCI_HEADER_TYPE_NORMAL:
55 switch (bar) {
56 case PCI_BASE_ADDRESS_0:
57 case PCI_BASE_ADDRESS_1:
58 case PCI_BASE_ADDRESS_2:
59 case PCI_BASE_ADDRESS_3:
60 case PCI_BASE_ADDRESS_4:
61 case PCI_BASE_ADDRESS_5:
62 if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
63 bartype = TYPE_IOBAR;
64 else
65 bartype = TYPE_MEMBAR;
66 break;
67 case PCI_ROM_ADDRESS:
68 bartype = TYPE_ROMBAR;
69 break;
70 }
71 break;
72 case PCI_HEADER_TYPE_BRIDGE:
73 switch (bar) {
74 case PCI_BASE_ADDRESS_0:
75 case PCI_BASE_ADDRESS_1:
76 if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
77 bartype = TYPE_IOBAR;
78 else
79 bartype = TYPE_MEMBAR;
80 break;
81 case PCI_ROM_ADDRESS1:
82 bartype = TYPE_ROMBAR;
83 break;
84 }
85 break;
86 case PCI_HEADER_TYPE_CARDBUS:
87 break;
88 default:
89 msg_perr("Unknown PCI header type 0x%02x, BAR type cannot be determined reliably.\n",
90 headertype);
91 break;
92 }
93
94 supported_cycles = pci_read_word(dev, PCI_COMMAND);
95
96 msg_pdbg("Requested BAR is of type ");
97 switch (bartype) {
98 case TYPE_MEMBAR:
99 msg_pdbg("MEM");
100 if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
101 msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
102 /* TODO: Abort here? */
103 }
104 msg_pdbg(", %sbit, %sprefetchable\n",
105 ((addr & 0x6) == 0x0) ? "32" : (((addr & 0x6) == 0x4) ? "64" : "reserved"),
106 (addr & 0x8) ? "" : "not ");
107 if ((addr & 0x6) == 0x4) {
108 /* The spec says that a 64-bit register consumes
109 * two subsequent dword locations.
110 */
111 upperaddr = pci_read_long(dev, bar + 4);
112 if (upperaddr != 0x00000000) {
113 /* Fun! A real 64-bit resource. */
114 if (sizeof(uintptr_t) != sizeof(uint64_t)) {
115 msg_perr("BAR unreachable!");
116 /* TODO: Really abort here? If multiple PCI devices match,
117 * we might never tell the user about the other devices.
118 */
119 return 0;
120 }
121 addr |= (uint64_t)upperaddr << 32;
122 }
123 }
124 addr &= PCI_BASE_ADDRESS_MEM_MASK;
125 break;
126 case TYPE_IOBAR:
127 msg_pdbg("I/O\n");
128#if __FLASHROM_HAVE_OUTB__
129 if (!(supported_cycles & PCI_COMMAND_IO)) {
130 msg_perr("I/O BAR access requested, but device has I/O space accesses disabled.\n");
131 /* TODO: Abort here? */
132 }
133#else
134 msg_perr("I/O BAR access requested, but flashrom does not support I/O BAR access on this "
135 "platform (yet).\n");
136#endif
137 addr &= PCI_BASE_ADDRESS_IO_MASK;
138 break;
139 case TYPE_ROMBAR:
140 msg_pdbg("ROM\n");
141 /* Not sure if this check is needed. */
142 if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
143 msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
144 /* TODO: Abort here? */
145 }
146 addr &= PCI_ROM_ADDRESS_MASK;
147 break;
148 case TYPE_UNKNOWN:
149 msg_perr("BAR type unknown, please report a bug at flashrom@flashrom.org\n");
150 }
151
152 return (uintptr_t)addr;
153}
154
155uintptr_t pcidev_validate(struct pci_dev *dev, int bar,
156 const struct dev_entry *devs)
157{
158 int i;
159
uwea3a82c92009-05-15 17:02:34 +0000160 for (i = 0; devs[i].device_name != NULL; i++) {
161 if (dev->device_id != devs[i].device_id)
162 continue;
163
hailfingerbf923c32011-02-15 22:44:27 +0000164 msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n",
165 devs[i].vendor_name, devs[i].device_name,
166 dev->vendor_id, dev->device_id, dev->bus, dev->dev,
167 dev->func);
168
mkarcher6475d3f2010-02-24 00:04:40 +0000169 if (devs[i].status == NT) {
snelsone42c3802010-05-07 20:09:04 +0000170 msg_pinfo("===\nThis PCI device is UNTESTED. Please "
hailfinger5bae2332010-10-08 11:03:02 +0000171 "report the 'flashrom -p xxxx' output \n"
172 "to flashrom@flashrom.org if it works "
173 "for you. Please add the name of your\n"
174 "PCI device to the subject. Thank you for "
175 "your help!\n===\n");
uwea3a82c92009-05-15 17:02:34 +0000176 }
177
Patrick Georgif776a442017-03-28 21:34:33 +0200178 return pcidev_readbar(dev, bar);
uwea3a82c92009-05-15 17:02:34 +0000179 }
180
181 return 0;
182}
183
Patrick Georgifc8e5d92017-03-21 16:49:15 +0100184static int pcidev_shutdown(void *data)
185{
186 if (pacc == NULL) {
187 msg_perr("%s: Tried to cleanup an invalid PCI context!\n"
188 "Please report a bug at flashrom@flashrom.org\n", __func__);
189 return 1;
190 }
191 pci_cleanup(pacc);
192 return 0;
193}
194
195int pci_init_common(void)
196{
197 if (pacc != NULL) {
198 msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n"
199 "Please report a bug at flashrom@flashrom.org\n", __func__);
200 return 1;
201 }
202 pacc = pci_alloc(); /* Get the pci_access structure */
203 pci_init(pacc); /* Initialize the PCI library */
204 if (register_shutdown(pcidev_shutdown, NULL))
205 return 1;
206 pci_scan_bus(pacc); /* We want to get the list of devices */
207 return 0;
208}
209
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200210struct pci_dev *pcidev_init(const struct dev_entry *devs, int bar)
uwea3a82c92009-05-15 17:02:34 +0000211{
212 struct pci_dev *dev;
hailfinger1ff33dc2010-07-03 11:02:10 +0000213 struct pci_filter filter;
hailfinger1ef766d2010-07-06 09:55:48 +0000214 char *pcidev_bdf;
uwea3a82c92009-05-15 17:02:34 +0000215 char *msg = NULL;
216 int found = 0;
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200217 uintptr_t addr = 0;
uwea3a82c92009-05-15 17:02:34 +0000218
Patrick Georgifc8e5d92017-03-21 16:49:15 +0100219 if (pci_init_common() != 0)
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200220 return NULL;
uwea3a82c92009-05-15 17:02:34 +0000221 pci_filter_init(pacc, &filter);
222
hailfinger0d703d42011-03-07 01:08:09 +0000223 /* Filter by bb:dd.f (if supplied by the user). */
hailfingerddeb4ac2010-07-08 10:13:37 +0000224 pcidev_bdf = extract_programmer_param("pci");
uwea3a82c92009-05-15 17:02:34 +0000225 if (pcidev_bdf != NULL) {
226 if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
snelsone42c3802010-05-07 20:09:04 +0000227 msg_perr("Error: %s\n", msg);
uwea3a82c92009-05-15 17:02:34 +0000228 exit(1);
229 }
230 }
hailfinger1ef766d2010-07-06 09:55:48 +0000231 free(pcidev_bdf);
uwea3a82c92009-05-15 17:02:34 +0000232
233 for (dev = pacc->devices; dev; dev = dev->next) {
234 if (pci_filter_match(&filter, dev)) {
hailfinger0d703d42011-03-07 01:08:09 +0000235 /* FIXME: We should count all matching devices, not
236 * just those with a valid BAR.
237 */
uwee2f95ef2009-09-02 23:00:46 +0000238 if ((addr = pcidev_validate(dev, bar, devs)) != 0) {
uweb3a82ef2009-05-16 21:39:19 +0000239 pcidev_dev = dev;
uwea3a82c92009-05-15 17:02:34 +0000240 found++;
uweb3a82ef2009-05-16 21:39:19 +0000241 }
uwea3a82c92009-05-15 17:02:34 +0000242 }
243 }
244
245 /* Only continue if exactly one supported PCI dev has been found. */
246 if (found == 0) {
snelsone42c3802010-05-07 20:09:04 +0000247 msg_perr("Error: No supported PCI device found.\n");
uwea3a82c92009-05-15 17:02:34 +0000248 exit(1);
249 } else if (found > 1) {
snelsone42c3802010-05-07 20:09:04 +0000250 msg_perr("Error: Multiple supported PCI devices found. "
hailfinger6dbba892010-10-06 23:03:21 +0000251 "Use 'flashrom -p xxxx:pci=bb:dd.f' \n"
uwea3a82c92009-05-15 17:02:34 +0000252 "to explicitly select the card with the given BDF "
253 "(PCI bus, device, function).\n");
254 exit(1);
255 }
256
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200257 return pcidev_dev;
uwea3a82c92009-05-15 17:02:34 +0000258}
259
Patrick Georgi8ae16572017-03-09 15:59:25 +0100260void print_supported_pcidevs(const struct dev_entry *devs)
uwea3a82c92009-05-15 17:02:34 +0000261{
262 int i;
263
hailfingerf79d1712010-10-06 23:48:34 +0000264 msg_pinfo("PCI devices:\n");
uwea3a82c92009-05-15 17:02:34 +0000265 for (i = 0; devs[i].vendor_name != NULL; i++) {
hailfinger495bc2e2010-10-07 22:21:45 +0000266 msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
uwe8d342eb2011-07-28 08:13:25 +0000267 devs[i].device_name, devs[i].vendor_id,
268 devs[i].device_id,
269 (devs[i].status == NT) ? " (untested)" : "");
uwea3a82c92009-05-15 17:02:34 +0000270 }
271}
hailfingerf31cbdc2010-11-10 15:25:18 +0000272
273enum pci_write_type {
274 pci_write_type_byte,
275 pci_write_type_word,
276 pci_write_type_long,
277};
278
279struct undo_pci_write_data {
280 struct pci_dev dev;
281 int reg;
282 enum pci_write_type type;
283 union {
284 uint8_t bytedata;
285 uint16_t worddata;
286 uint32_t longdata;
287 };
288};
289
David Hendricks93784b42016-08-09 17:00:38 -0700290int undo_pci_write(void *p)
hailfingerf31cbdc2010-11-10 15:25:18 +0000291{
292 struct undo_pci_write_data *data = p;
293 msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n",
294 data->dev.bus, data->dev.dev, data->dev.func, data->reg);
295 switch (data->type) {
296 case pci_write_type_byte:
297 pci_write_byte(&data->dev, data->reg, data->bytedata);
298 break;
299 case pci_write_type_word:
300 pci_write_word(&data->dev, data->reg, data->worddata);
301 break;
302 case pci_write_type_long:
303 pci_write_long(&data->dev, data->reg, data->longdata);
304 break;
305 }
306 /* p was allocated in register_undo_pci_write. */
307 free(p);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000308 return 0;
hailfingerf31cbdc2010-11-10 15:25:18 +0000309}
310
311#define register_undo_pci_write(a, b, c) \
312{ \
313 struct undo_pci_write_data *undo_pci_write_data; \
314 undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \
stefanctd611e8f2011-07-12 22:35:21 +0000315 if (!undo_pci_write_data) { \
316 msg_gerr("Out of memory!\n"); \
317 exit(1); \
318 } \
hailfingerf31cbdc2010-11-10 15:25:18 +0000319 undo_pci_write_data->dev = *a; \
320 undo_pci_write_data->reg = b; \
321 undo_pci_write_data->type = pci_write_type_##c; \
322 undo_pci_write_data->c##data = pci_read_##c(dev, reg); \
323 register_shutdown(undo_pci_write, undo_pci_write_data); \
324}
325
326#define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte)
327#define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word)
328#define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long)
329
330int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data)
331{
332 register_undo_pci_write_byte(dev, reg);
333 return pci_write_byte(dev, reg, data);
334}
335
336int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data)
337{
338 register_undo_pci_write_word(dev, reg);
339 return pci_write_word(dev, reg, data);
340}
341
342int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data)
343{
344 register_undo_pci_write_long(dev, reg);
345 return pci_write_long(dev, reg, data);
346}