xref: /aosp_15_r20/external/flashrom/pcidev.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2009 Uwe Hermann <[email protected]>
5  * Copyright (C) 2010, 2011 Carl-Daniel Hailfinger
6  *
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 
18 #include <stdlib.h>
19 #include <string.h>
20 #include "flash.h"
21 #include "programmer.h"
22 #include "platform/pci.h"
23 
24 struct pci_access *pacc;
25 
26 enum pci_bartype {
27 	TYPE_MEMBAR,
28 	TYPE_IOBAR,
29 	TYPE_ROMBAR,
30 	TYPE_UNKNOWN
31 };
32 
pcidev_readbar(struct pci_dev * dev,int bar)33 uintptr_t pcidev_readbar(struct pci_dev *dev, int bar)
34 {
35 	uint64_t addr;
36 	uint32_t upperaddr;
37 	uint8_t headertype;
38 	uint16_t supported_cycles;
39 	enum pci_bartype bartype = TYPE_UNKNOWN;
40 
41 
42 	headertype = pci_read_byte(dev, PCI_HEADER_TYPE) & 0x7f;
43 	msg_pspew("PCI header type 0x%02x\n", headertype);
44 
45 	/* Don't use dev->base_addr[x] (as value for 'bar'), won't work on older libpci. */
46 	addr = pci_read_long(dev, bar);
47 
48 	/* Sanity checks. */
49 	switch (headertype) {
50 	case PCI_HEADER_TYPE_NORMAL:
51 		switch (bar) {
52 		case PCI_BASE_ADDRESS_0:
53 		case PCI_BASE_ADDRESS_1:
54 		case PCI_BASE_ADDRESS_2:
55 		case PCI_BASE_ADDRESS_3:
56 		case PCI_BASE_ADDRESS_4:
57 		case PCI_BASE_ADDRESS_5:
58 			if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
59 				bartype = TYPE_IOBAR;
60 			else
61 				bartype = TYPE_MEMBAR;
62 			break;
63 		case PCI_ROM_ADDRESS:
64 			bartype = TYPE_ROMBAR;
65 			break;
66 		}
67 		break;
68 	case PCI_HEADER_TYPE_BRIDGE:
69 		switch (bar) {
70 		case PCI_BASE_ADDRESS_0:
71 		case PCI_BASE_ADDRESS_1:
72 			if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
73 				bartype = TYPE_IOBAR;
74 			else
75 				bartype = TYPE_MEMBAR;
76 			break;
77 		case PCI_ROM_ADDRESS1:
78 			bartype = TYPE_ROMBAR;
79 			break;
80 		}
81 		break;
82 	case PCI_HEADER_TYPE_CARDBUS:
83 		break;
84 	default:
85 		msg_perr("Unknown PCI header type 0x%02x, BAR type cannot be determined reliably.\n",
86 			 headertype);
87 		break;
88 	}
89 
90 	supported_cycles = pci_read_word(dev, PCI_COMMAND);
91 
92 	msg_pdbg("Requested BAR is of type ");
93 	switch (bartype) {
94 	case TYPE_MEMBAR:
95 		msg_pdbg("MEM");
96 		if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
97 			msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
98 			/* TODO: Abort here? */
99 		}
100 		msg_pdbg(", %sbit, %sprefetchable\n",
101 			 ((addr & 0x6) == 0x0) ? "32" : (((addr & 0x6) == 0x4) ? "64" : "reserved"),
102 			 (addr & 0x8) ? "" : "not ");
103 		if ((addr & 0x6) == 0x4) {
104 			/* The spec says that a 64-bit register consumes
105 			 * two subsequent dword locations.
106 			 */
107 			upperaddr = pci_read_long(dev, bar + 4);
108 			if (upperaddr != 0x00000000) {
109 				/* Fun! A real 64-bit resource. */
110 				if (sizeof(uintptr_t) != sizeof(uint64_t)) {
111 					msg_perr("BAR unreachable!");
112 					/* TODO: Really abort here? If multiple PCI devices match,
113 					 * we might never tell the user about the other devices.
114 					 */
115 					return 0;
116 				}
117 				addr |= (uint64_t)upperaddr << 32;
118 			}
119 		}
120 		addr &= PCI_BASE_ADDRESS_MEM_MASK;
121 		break;
122 	case TYPE_IOBAR:
123 		msg_pdbg("I/O\n");
124 #if __FLASHROM_HAVE_OUTB__
125 		if (!(supported_cycles & PCI_COMMAND_IO)) {
126 			msg_perr("I/O BAR access requested, but device has I/O space accesses disabled.\n");
127 			/* TODO: Abort here? */
128 		}
129 #else
130 		msg_perr("I/O BAR access requested, but flashrom does not support I/O BAR access on this "
131 			 "platform (yet).\n");
132 #endif
133 		addr &= PCI_BASE_ADDRESS_IO_MASK;
134 		break;
135 	case TYPE_ROMBAR:
136 		msg_pdbg("ROM\n");
137 		/* Not sure if this check is needed. */
138 		if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
139 			msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
140 			/* TODO: Abort here? */
141 		}
142 		addr &= PCI_ROM_ADDRESS_MASK;
143 		break;
144 	case TYPE_UNKNOWN:
145 		msg_perr("BAR type unknown, please report a bug at [email protected]\n");
146 	}
147 
148 	return (uintptr_t)addr;
149 }
150 
pcidev_scandev(struct pci_filter * filter,struct pci_dev * start)151 struct pci_dev *pcidev_scandev(struct pci_filter *filter, struct pci_dev *start)
152 {
153 	struct pci_dev *temp;
154 	for (temp = start ? start->next : pacc->devices; temp; temp = temp->next) {
155 		if (pci_filter_match(filter, temp)) {
156 			pci_fill_info(temp, PCI_FILL_IDENT);
157 			return temp;
158 		}
159 	}
160 	return NULL;
161 }
162 
pcidev_card_find(uint16_t vendor,uint16_t device,uint16_t card_vendor,uint16_t card_device)163 struct pci_dev *pcidev_card_find(uint16_t vendor, uint16_t device,
164 				 uint16_t card_vendor, uint16_t card_device)
165 {
166 	struct pci_dev *temp = NULL;
167 	struct pci_filter filter;
168 
169 	pci_filter_init(NULL, &filter);
170 	filter.vendor = vendor;
171 	filter.device = device;
172 
173 	while ((temp = pcidev_scandev(&filter, temp))) {
174 		if ((card_vendor == pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID))
175 		    && (card_device == pci_read_word(temp, PCI_SUBSYSTEM_ID)))
176 			return temp;
177 	}
178 
179 	return NULL;
180 }
181 
pcidev_find(uint16_t vendor,uint16_t device)182 struct pci_dev *pcidev_find(uint16_t vendor, uint16_t device)
183 {
184 	struct pci_filter filter;
185 
186 	pci_filter_init(NULL, &filter);
187 	filter.vendor = vendor;
188 	filter.device = device;
189 
190 	return pcidev_scandev(&filter, NULL);
191 }
192 
pcidev_getdevfn(struct pci_dev * dev,const int func)193 struct pci_dev *pcidev_getdevfn(struct pci_dev *dev, const int func)
194 {
195 	struct pci_dev *const new = pci_get_dev(pacc, dev->domain, dev->bus, dev->dev, func);
196 	if (new)
197 		pci_fill_info(new, PCI_FILL_IDENT);
198 	return new;
199 }
200 
pcidev_find_vendorclass(uint16_t vendor,uint16_t devclass)201 struct pci_dev *pcidev_find_vendorclass(uint16_t vendor, uint16_t devclass)
202 {
203 	struct pci_dev *temp = NULL;
204 	struct pci_filter filter;
205 	uint16_t tmp2;
206 
207 	pci_filter_init(NULL, &filter);
208 	filter.vendor = vendor;
209 
210 	while ((temp = pcidev_scandev(&filter, temp))) {
211 		/* Read PCI class */
212 		tmp2 = pci_read_word(temp, PCI_CLASS_DEVICE);
213 		if (tmp2 == devclass)
214 			return temp;
215 	}
216 
217 	return NULL;
218 }
219 
pcidev_shutdown(void * data)220 static int pcidev_shutdown(void *data)
221 {
222 	if (pacc == NULL) {
223 		msg_perr("%s: Tried to cleanup an invalid PCI context!\n"
224 			 "Please report a bug at [email protected]\n", __func__);
225 		return 1;
226 	}
227 	pci_cleanup(pacc);
228 	pacc = NULL;
229 	return 0;
230 }
231 
pci_init_common(void)232 int pci_init_common(void)
233 {
234 	if (pacc != NULL) {
235 		msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n"
236 			 "Please report a bug at [email protected]\n", __func__);
237 		return 1;
238 	}
239 	pacc = pci_alloc();     /* Get the pci_access structure */
240 	pci_init(pacc);         /* Initialize the PCI library */
241 	if (register_shutdown(pcidev_shutdown, NULL))
242 		return 1;
243 	pci_scan_bus(pacc);     /* We want to get the list of devices */
244 	return 0;
245 }
246 
247 /* pcidev_init gets an array of allowed PCI device IDs and returns a pointer to struct pci_dev iff exactly one
248  * match was found. If the "pci=bb:dd.f" programmer parameter was specified, a match is only considered if it
249  * also matches the specified bus:device.function.
250  * For convenience, this function also registers its own undo handlers.
251  */
pcidev_init(const struct programmer_cfg * cfg,const struct dev_entry * devs,int bar)252 struct pci_dev *pcidev_init(const struct programmer_cfg *cfg, const struct dev_entry *devs, int bar)
253 {
254 	struct pci_dev *dev;
255 	struct pci_dev *found_dev = NULL;
256 	struct pci_filter filter;
257 	char *pcidev_bdf;
258 	char *msg = NULL;
259 	int found = 0;
260 	int i;
261 
262 	if (pci_init_common() != 0)
263 		return NULL;
264 	pci_filter_init(pacc, &filter);
265 
266 	/* Filter by bb:dd.f (if supplied by the user). */
267 	pcidev_bdf = extract_programmer_param_str(cfg, "pci");
268 	if (pcidev_bdf != NULL) {
269 		if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
270 			msg_perr("Error: %s\n", msg);
271 			return NULL;
272 		}
273 	}
274 	free(pcidev_bdf);
275 
276 	for (dev = pacc->devices; dev; dev = dev->next) {
277 		if (pci_filter_match(&filter, dev)) {
278 			pci_fill_info(dev, PCI_FILL_IDENT);
279 			/* Check against list of supported devices. */
280 			for (i = 0; devs[i].device_name != NULL; i++)
281 				if ((dev->vendor_id == devs[i].vendor_id) &&
282 				    (dev->device_id == devs[i].device_id))
283 					break;
284 			/* Not supported, try the next one. */
285 			if (devs[i].device_name == NULL)
286 				continue;
287 
288 			msg_pdbg("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n", devs[i].vendor_name,
289 				 devs[i].device_name, dev->vendor_id, dev->device_id, dev->bus, dev->dev,
290 				 dev->func);
291 			if (devs[i].status == NT)
292 				msg_pinfo("===\nThis PCI device is UNTESTED. Please report the 'flashrom -p "
293 					  "xxxx' output\n"
294 					  "to [email protected] if it works for you. Please add the name "
295 					  "of your\n"
296 					  "PCI device to the subject. Thank you for your help!\n===\n");
297 
298 			/* FIXME: We should count all matching devices, not
299 			 * just those with a valid BAR.
300 			 */
301 			if (pcidev_readbar(dev, bar) != 0) {
302 				found_dev = dev;
303 				found++;
304 			}
305 		}
306 	}
307 
308 	/* Only continue if exactly one supported PCI dev has been found. */
309 	if (found == 0) {
310 		msg_perr("Error: No supported PCI device found.\n");
311 		return NULL;
312 	} else if (found > 1) {
313 		msg_perr("Error: Multiple supported PCI devices found. Use 'flashrom -p xxxx:pci=bb:dd.f'\n"
314 			 "to explicitly select the card with the given BDF (PCI bus, device, function).\n");
315 		return NULL;
316 	}
317 
318 	return found_dev;
319 }
320 
321 enum pci_write_type {
322 	pci_write_type_byte,
323 	pci_write_type_word,
324 	pci_write_type_long,
325 };
326 
327 struct undo_pci_write_data {
328 	struct pci_dev *dev;
329 	int reg;
330 	enum pci_write_type type;
331 	union {
332 		uint8_t bytedata;
333 		uint16_t worddata;
334 		uint32_t longdata;
335 	};
336 };
337 
undo_pci_write(void * p)338 static int undo_pci_write(void *p)
339 {
340 	struct undo_pci_write_data *data = p;
341 	if (pacc == NULL || data->dev == NULL) {
342 		msg_perr("%s: Tried to undo PCI writes without a valid PCI %s!\n"
343 			"Please report a bug at [email protected]\n",
344 			__func__, data->dev == NULL ? "device" : "context");
345 		return 1;
346 	}
347 	msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n",
348 		 data->dev->bus, data->dev->dev, data->dev->func, data->reg);
349 	switch (data->type) {
350 	case pci_write_type_byte:
351 		pci_write_byte(data->dev, data->reg, data->bytedata);
352 		break;
353 	case pci_write_type_word:
354 		pci_write_word(data->dev, data->reg, data->worddata);
355 		break;
356 	case pci_write_type_long:
357 		pci_write_long(data->dev, data->reg, data->longdata);
358 		break;
359 	}
360 	/* p was allocated in register_undo_pci_write. */
361 	free(p);
362 	return 0;
363 }
364 
365 #define register_undo_pci_write(a, b, c)				\
366 {									\
367 	struct undo_pci_write_data *undo_pci_write_data;		\
368 	undo_pci_write_data = malloc(sizeof(*undo_pci_write_data));	\
369 	if (!undo_pci_write_data) {					\
370 		msg_gerr("Out of memory!\n");				\
371 		exit(1);						\
372 	}								\
373 	if (pacc)							\
374 		undo_pci_write_data->dev = pci_get_dev(pacc,		\
375 				a->domain, a->bus, a->dev, a->func);	\
376 	else								\
377 		undo_pci_write_data->dev =  NULL;			\
378 	undo_pci_write_data->reg = b;					\
379 	undo_pci_write_data->type = pci_write_type_##c;			\
380 	undo_pci_write_data->c##data = pci_read_##c(dev, reg);		\
381 	register_shutdown(undo_pci_write, undo_pci_write_data);		\
382 }
383 
384 #define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte)
385 #define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word)
386 #define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long)
387 
rpci_write_byte(struct pci_dev * dev,int reg,uint8_t data)388 int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data)
389 {
390 	register_undo_pci_write_byte(dev, reg);
391 	return pci_write_byte(dev, reg, data);
392 }
393 
rpci_write_word(struct pci_dev * dev,int reg,uint16_t data)394 int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data)
395 {
396 	register_undo_pci_write_word(dev, reg);
397 	return pci_write_word(dev, reg, data);
398 }
399 
rpci_write_long(struct pci_dev * dev,int reg,uint32_t data)400 int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data)
401 {
402 	register_undo_pci_write_long(dev, reg);
403 	return pci_write_long(dev, reg, data);
404 }
405