blob: 296c102ed1293980cf6e457205c920093bc4ca5e [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;
uwea3a82c92009-05-15 17:02:34 +000028
hailfingerbf923c32011-02-15 22:44:27 +000029enum pci_bartype {
30 TYPE_MEMBAR,
31 TYPE_IOBAR,
32 TYPE_ROMBAR,
33 TYPE_UNKNOWN
34};
35
Patrick Georgif776a442017-03-28 21:34:33 +020036uintptr_t pcidev_readbar(struct pci_dev *dev, int bar)
uwea3a82c92009-05-15 17:02:34 +000037{
hailfingerbf923c32011-02-15 22:44:27 +000038 uint64_t addr;
39 uint32_t upperaddr;
40 uint8_t headertype;
41 uint16_t supported_cycles;
42 enum pci_bartype bartype = TYPE_UNKNOWN;
uwea3a82c92009-05-15 17:02:34 +000043
Patrick Georgif776a442017-03-28 21:34:33 +020044
45 headertype = pci_read_byte(dev, PCI_HEADER_TYPE) & 0x7f;
46 msg_pspew("PCI header type 0x%02x\n", headertype);
47
48 /* Don't use dev->base_addr[x] (as value for 'bar'), won't work on older libpci. */
49 addr = pci_read_long(dev, bar);
50
51 /* Sanity checks. */
52 switch (headertype) {
53 case PCI_HEADER_TYPE_NORMAL:
54 switch (bar) {
55 case PCI_BASE_ADDRESS_0:
56 case PCI_BASE_ADDRESS_1:
57 case PCI_BASE_ADDRESS_2:
58 case PCI_BASE_ADDRESS_3:
59 case PCI_BASE_ADDRESS_4:
60 case PCI_BASE_ADDRESS_5:
61 if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
62 bartype = TYPE_IOBAR;
63 else
64 bartype = TYPE_MEMBAR;
65 break;
66 case PCI_ROM_ADDRESS:
67 bartype = TYPE_ROMBAR;
68 break;
69 }
70 break;
71 case PCI_HEADER_TYPE_BRIDGE:
72 switch (bar) {
73 case PCI_BASE_ADDRESS_0:
74 case PCI_BASE_ADDRESS_1:
75 if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
76 bartype = TYPE_IOBAR;
77 else
78 bartype = TYPE_MEMBAR;
79 break;
80 case PCI_ROM_ADDRESS1:
81 bartype = TYPE_ROMBAR;
82 break;
83 }
84 break;
85 case PCI_HEADER_TYPE_CARDBUS:
86 break;
87 default:
88 msg_perr("Unknown PCI header type 0x%02x, BAR type cannot be determined reliably.\n",
89 headertype);
90 break;
91 }
92
93 supported_cycles = pci_read_word(dev, PCI_COMMAND);
94
95 msg_pdbg("Requested BAR is of type ");
96 switch (bartype) {
97 case TYPE_MEMBAR:
98 msg_pdbg("MEM");
99 if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
100 msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
101 /* TODO: Abort here? */
102 }
103 msg_pdbg(", %sbit, %sprefetchable\n",
104 ((addr & 0x6) == 0x0) ? "32" : (((addr & 0x6) == 0x4) ? "64" : "reserved"),
105 (addr & 0x8) ? "" : "not ");
106 if ((addr & 0x6) == 0x4) {
107 /* The spec says that a 64-bit register consumes
108 * two subsequent dword locations.
109 */
110 upperaddr = pci_read_long(dev, bar + 4);
111 if (upperaddr != 0x00000000) {
112 /* Fun! A real 64-bit resource. */
113 if (sizeof(uintptr_t) != sizeof(uint64_t)) {
114 msg_perr("BAR unreachable!");
115 /* TODO: Really abort here? If multiple PCI devices match,
116 * we might never tell the user about the other devices.
117 */
118 return 0;
119 }
120 addr |= (uint64_t)upperaddr << 32;
121 }
122 }
123 addr &= PCI_BASE_ADDRESS_MEM_MASK;
124 break;
125 case TYPE_IOBAR:
126 msg_pdbg("I/O\n");
127#if __FLASHROM_HAVE_OUTB__
128 if (!(supported_cycles & PCI_COMMAND_IO)) {
129 msg_perr("I/O BAR access requested, but device has I/O space accesses disabled.\n");
130 /* TODO: Abort here? */
131 }
132#else
133 msg_perr("I/O BAR access requested, but flashrom does not support I/O BAR access on this "
134 "platform (yet).\n");
135#endif
136 addr &= PCI_BASE_ADDRESS_IO_MASK;
137 break;
138 case TYPE_ROMBAR:
139 msg_pdbg("ROM\n");
140 /* Not sure if this check is needed. */
141 if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
142 msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
143 /* TODO: Abort here? */
144 }
145 addr &= PCI_ROM_ADDRESS_MASK;
146 break;
147 case TYPE_UNKNOWN:
148 msg_perr("BAR type unknown, please report a bug at flashrom@flashrom.org\n");
149 }
150
151 return (uintptr_t)addr;
152}
153
154uintptr_t pcidev_validate(struct pci_dev *dev, int bar,
155 const struct dev_entry *devs)
156{
157 int i;
158
uwea3a82c92009-05-15 17:02:34 +0000159 for (i = 0; devs[i].device_name != NULL; i++) {
160 if (dev->device_id != devs[i].device_id)
161 continue;
162
hailfingerbf923c32011-02-15 22:44:27 +0000163 msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n",
164 devs[i].vendor_name, devs[i].device_name,
165 dev->vendor_id, dev->device_id, dev->bus, dev->dev,
166 dev->func);
167
mkarcher6475d3f2010-02-24 00:04:40 +0000168 if (devs[i].status == NT) {
snelsone42c3802010-05-07 20:09:04 +0000169 msg_pinfo("===\nThis PCI device is UNTESTED. Please "
hailfinger5bae2332010-10-08 11:03:02 +0000170 "report the 'flashrom -p xxxx' output \n"
171 "to flashrom@flashrom.org if it works "
172 "for you. Please add the name of your\n"
173 "PCI device to the subject. Thank you for "
174 "your help!\n===\n");
uwea3a82c92009-05-15 17:02:34 +0000175 }
176
Patrick Georgif776a442017-03-28 21:34:33 +0200177 return pcidev_readbar(dev, bar);
uwea3a82c92009-05-15 17:02:34 +0000178 }
179
180 return 0;
181}
182
Patrick Georgifc8e5d92017-03-21 16:49:15 +0100183static int pcidev_shutdown(void *data)
184{
185 if (pacc == NULL) {
186 msg_perr("%s: Tried to cleanup an invalid PCI context!\n"
187 "Please report a bug at flashrom@flashrom.org\n", __func__);
188 return 1;
189 }
190 pci_cleanup(pacc);
191 return 0;
192}
193
194int pci_init_common(void)
195{
196 if (pacc != NULL) {
197 msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n"
198 "Please report a bug at flashrom@flashrom.org\n", __func__);
199 return 1;
200 }
201 pacc = pci_alloc(); /* Get the pci_access structure */
202 pci_init(pacc); /* Initialize the PCI library */
203 if (register_shutdown(pcidev_shutdown, NULL))
204 return 1;
205 pci_scan_bus(pacc); /* We want to get the list of devices */
206 return 0;
207}
208
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200209struct pci_dev *pcidev_init(const struct dev_entry *devs, int bar)
uwea3a82c92009-05-15 17:02:34 +0000210{
211 struct pci_dev *dev;
hailfinger1ff33dc2010-07-03 11:02:10 +0000212 struct pci_filter filter;
hailfinger1ef766d2010-07-06 09:55:48 +0000213 char *pcidev_bdf;
uwea3a82c92009-05-15 17:02:34 +0000214 char *msg = NULL;
215 int found = 0;
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200216 uintptr_t addr = 0;
uwea3a82c92009-05-15 17:02:34 +0000217
Patrick Georgifc8e5d92017-03-21 16:49:15 +0100218 if (pci_init_common() != 0)
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200219 return NULL;
uwea3a82c92009-05-15 17:02:34 +0000220 pci_filter_init(pacc, &filter);
221
hailfinger0d703d42011-03-07 01:08:09 +0000222 /* Filter by bb:dd.f (if supplied by the user). */
hailfingerddeb4ac2010-07-08 10:13:37 +0000223 pcidev_bdf = extract_programmer_param("pci");
uwea3a82c92009-05-15 17:02:34 +0000224 if (pcidev_bdf != NULL) {
225 if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
snelsone42c3802010-05-07 20:09:04 +0000226 msg_perr("Error: %s\n", msg);
uwea3a82c92009-05-15 17:02:34 +0000227 exit(1);
228 }
229 }
hailfinger1ef766d2010-07-06 09:55:48 +0000230 free(pcidev_bdf);
uwea3a82c92009-05-15 17:02:34 +0000231
232 for (dev = pacc->devices; dev; dev = dev->next) {
233 if (pci_filter_match(&filter, dev)) {
hailfinger0d703d42011-03-07 01:08:09 +0000234 /* FIXME: We should count all matching devices, not
235 * just those with a valid BAR.
236 */
uwee2f95ef2009-09-02 23:00:46 +0000237 if ((addr = pcidev_validate(dev, bar, devs)) != 0) {
uwea3a82c92009-05-15 17:02:34 +0000238 found++;
uweb3a82ef2009-05-16 21:39:19 +0000239 }
uwea3a82c92009-05-15 17:02:34 +0000240 }
241 }
242
243 /* Only continue if exactly one supported PCI dev has been found. */
244 if (found == 0) {
snelsone42c3802010-05-07 20:09:04 +0000245 msg_perr("Error: No supported PCI device found.\n");
uwea3a82c92009-05-15 17:02:34 +0000246 exit(1);
247 } else if (found > 1) {
snelsone42c3802010-05-07 20:09:04 +0000248 msg_perr("Error: Multiple supported PCI devices found. "
hailfinger6dbba892010-10-06 23:03:21 +0000249 "Use 'flashrom -p xxxx:pci=bb:dd.f' \n"
uwea3a82c92009-05-15 17:02:34 +0000250 "to explicitly select the card with the given BDF "
251 "(PCI bus, device, function).\n");
252 exit(1);
253 }
254
Patrick Georgid490a172017-03-28 23:03:47 +0200255 return dev;
uwea3a82c92009-05-15 17:02:34 +0000256}
257
Patrick Georgi8ae16572017-03-09 15:59:25 +0100258void print_supported_pcidevs(const struct dev_entry *devs)
uwea3a82c92009-05-15 17:02:34 +0000259{
260 int i;
261
hailfingerf79d1712010-10-06 23:48:34 +0000262 msg_pinfo("PCI devices:\n");
uwea3a82c92009-05-15 17:02:34 +0000263 for (i = 0; devs[i].vendor_name != NULL; i++) {
hailfinger495bc2e2010-10-07 22:21:45 +0000264 msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
uwe8d342eb2011-07-28 08:13:25 +0000265 devs[i].device_name, devs[i].vendor_id,
266 devs[i].device_id,
267 (devs[i].status == NT) ? " (untested)" : "");
uwea3a82c92009-05-15 17:02:34 +0000268 }
269}
hailfingerf31cbdc2010-11-10 15:25:18 +0000270
271enum pci_write_type {
272 pci_write_type_byte,
273 pci_write_type_word,
274 pci_write_type_long,
275};
276
277struct undo_pci_write_data {
278 struct pci_dev dev;
279 int reg;
280 enum pci_write_type type;
281 union {
282 uint8_t bytedata;
283 uint16_t worddata;
284 uint32_t longdata;
285 };
286};
287
David Hendricks93784b42016-08-09 17:00:38 -0700288int undo_pci_write(void *p)
hailfingerf31cbdc2010-11-10 15:25:18 +0000289{
290 struct undo_pci_write_data *data = p;
291 msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n",
292 data->dev.bus, data->dev.dev, data->dev.func, data->reg);
293 switch (data->type) {
294 case pci_write_type_byte:
295 pci_write_byte(&data->dev, data->reg, data->bytedata);
296 break;
297 case pci_write_type_word:
298 pci_write_word(&data->dev, data->reg, data->worddata);
299 break;
300 case pci_write_type_long:
301 pci_write_long(&data->dev, data->reg, data->longdata);
302 break;
303 }
304 /* p was allocated in register_undo_pci_write. */
305 free(p);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000306 return 0;
hailfingerf31cbdc2010-11-10 15:25:18 +0000307}
308
309#define register_undo_pci_write(a, b, c) \
310{ \
311 struct undo_pci_write_data *undo_pci_write_data; \
312 undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \
stefanctd611e8f2011-07-12 22:35:21 +0000313 if (!undo_pci_write_data) { \
314 msg_gerr("Out of memory!\n"); \
315 exit(1); \
316 } \
hailfingerf31cbdc2010-11-10 15:25:18 +0000317 undo_pci_write_data->dev = *a; \
318 undo_pci_write_data->reg = b; \
319 undo_pci_write_data->type = pci_write_type_##c; \
320 undo_pci_write_data->c##data = pci_read_##c(dev, reg); \
321 register_shutdown(undo_pci_write, undo_pci_write_data); \
322}
323
324#define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte)
325#define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word)
326#define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long)
327
328int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data)
329{
330 register_undo_pci_write_byte(dev, reg);
331 return pci_write_byte(dev, reg, data);
332}
333
334int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data)
335{
336 register_undo_pci_write_word(dev, reg);
337 return pci_write_word(dev, reg, data);
338}
339
340int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data)
341{
342 register_undo_pci_write_long(dev, reg);
343 return pci_write_long(dev, reg, data);
344}