pcidev.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
  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. * 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. #include <stdlib.h>
  22. #include <string.h>
  23. #include "flash.h"
  24. #include "programmer.h"
  25. #include "hwaccess.h"
  26. struct pci_access *pacc;
  27. enum pci_bartype {
  28. TYPE_MEMBAR,
  29. TYPE_IOBAR,
  30. TYPE_ROMBAR,
  31. TYPE_UNKNOWN
  32. };
  33. uintptr_t pcidev_readbar(struct pci_dev *dev, int bar)
  34. {
  35. uint64_t addr;
  36. uint8_t headertype;
  37. uint16_t supported_cycles;
  38. enum pci_bartype bartype = TYPE_UNKNOWN;
  39. #ifndef __PPC64__
  40. uint32_t upperaddr;
  41. #endif
  42. headertype = pci_read_byte(dev, PCI_HEADER_TYPE) & 0x7f;
  43. msg_pspew("PCI header type 0x%02x\n", headertype);
  44. /* Don't use dev->base_addr[x] (as value for 'bar'), won't work on older libpci. */
  45. addr = pci_read_long(dev, bar);
  46. /* Sanity checks. */
  47. switch (headertype) {
  48. case PCI_HEADER_TYPE_NORMAL:
  49. switch (bar) {
  50. case PCI_BASE_ADDRESS_0:
  51. case PCI_BASE_ADDRESS_1:
  52. case PCI_BASE_ADDRESS_2:
  53. case PCI_BASE_ADDRESS_3:
  54. case PCI_BASE_ADDRESS_4:
  55. case PCI_BASE_ADDRESS_5:
  56. if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
  57. bartype = TYPE_IOBAR;
  58. else
  59. bartype = TYPE_MEMBAR;
  60. break;
  61. case PCI_ROM_ADDRESS:
  62. bartype = TYPE_ROMBAR;
  63. break;
  64. }
  65. break;
  66. case PCI_HEADER_TYPE_BRIDGE:
  67. switch (bar) {
  68. case PCI_BASE_ADDRESS_0:
  69. case PCI_BASE_ADDRESS_1:
  70. if ((addr & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
  71. bartype = TYPE_IOBAR;
  72. else
  73. bartype = TYPE_MEMBAR;
  74. break;
  75. case PCI_ROM_ADDRESS1:
  76. bartype = TYPE_ROMBAR;
  77. break;
  78. }
  79. break;
  80. case PCI_HEADER_TYPE_CARDBUS:
  81. break;
  82. default:
  83. msg_perr("Unknown PCI header type 0x%02x, BAR type cannot be determined reliably.\n",
  84. headertype);
  85. break;
  86. }
  87. supported_cycles = pci_read_word(dev, PCI_COMMAND);
  88. msg_pdbg("Requested BAR is of type ");
  89. switch (bartype) {
  90. case TYPE_MEMBAR:
  91. msg_pdbg("MEM");
  92. #ifdef __PPC64__
  93. /* PowerPC is able to translate 32-bit BARs into 64-bit host windows.
  94. * Use the dev->base_addr[x] mechanism to handle mapping.
  95. */
  96. addr = dev->base_addr[(bar - 0x10) / 0x4] & PCI_BASE_ADDRESS_MEM_MASK;
  97. #else
  98. if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
  99. msg_perr("MEM BAR access requested, but device has MEM space accesses disabled.\n");
  100. /* TODO: Abort here? */
  101. }
  102. msg_pdbg(", %sbit, %sprefetchable\n",
  103. ((addr & 0x6) == 0x0) ? "32" : (((addr & 0x6) == 0x4) ? "64" : "reserved"),
  104. (addr & 0x8) ? "" : "not ");
  105. if ((addr & 0x6) == 0x4) {
  106. /* The spec says that a 64-bit register consumes
  107. * two subsequent dword locations.
  108. */
  109. upperaddr = pci_read_long(dev, bar + 4);
  110. if (upperaddr != 0x00000000) {
  111. /* Fun! A real 64-bit resource. */
  112. if (sizeof(uintptr_t) != sizeof(uint64_t)) {
  113. msg_perr("BAR unreachable!");
  114. /* TODO: Really abort here? If multiple PCI devices match,
  115. * we might never tell the user about the other devices.
  116. */
  117. return 0;
  118. }
  119. addr |= (uint64_t)upperaddr << 32;
  120. }
  121. }
  122. addr &= PCI_BASE_ADDRESS_MEM_MASK;
  123. #endif
  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. return (uintptr_t)addr;
  151. }
  152. static int pcidev_shutdown(void *data)
  153. {
  154. if (pacc == NULL) {
  155. msg_perr("%s: Tried to cleanup an invalid PCI context!\n"
  156. "Please report a bug at flashrom@flashrom.org\n", __func__);
  157. return 1;
  158. }
  159. pci_cleanup(pacc);
  160. return 0;
  161. }
  162. int pci_init_common(void)
  163. {
  164. if (pacc != NULL) {
  165. msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n"
  166. "Please report a bug at flashrom@flashrom.org\n", __func__);
  167. return 1;
  168. }
  169. pacc = pci_alloc(); /* Get the pci_access structure */
  170. pci_init(pacc); /* Initialize the PCI library */
  171. if (register_shutdown(pcidev_shutdown, NULL))
  172. return 1;
  173. pci_scan_bus(pacc); /* We want to get the list of devices */
  174. return 0;
  175. }
  176. /* pcidev_init gets an array of allowed PCI device IDs and returns a pointer to struct pci_dev iff exactly one
  177. * match was found. If the "pci=bb:dd.f" programmer parameter was specified, a match is only considered if it
  178. * also matches the specified bus:device.function.
  179. * For convenience, this function also registers its own undo handlers.
  180. */
  181. struct pci_dev *pcidev_init(const struct dev_entry *devs, int bar)
  182. {
  183. struct pci_dev *dev;
  184. struct pci_dev *found_dev = NULL;
  185. struct pci_filter filter;
  186. char *pcidev_bdf;
  187. char *msg = NULL;
  188. int found = 0;
  189. int i;
  190. uintptr_t addr = 0;
  191. if (pci_init_common() != 0)
  192. return NULL;
  193. pci_filter_init(pacc, &filter);
  194. /* Filter by bb:dd.f (if supplied by the user). */
  195. pcidev_bdf = extract_programmer_param("pci");
  196. if (pcidev_bdf != NULL) {
  197. if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
  198. msg_perr("Error: %s\n", msg);
  199. return NULL;
  200. }
  201. }
  202. free(pcidev_bdf);
  203. for (dev = pacc->devices; dev; dev = dev->next) {
  204. if (pci_filter_match(&filter, dev)) {
  205. /* Check against list of supported devices. */
  206. for (i = 0; devs[i].device_name != NULL; i++)
  207. if ((dev->vendor_id == devs[i].vendor_id) &&
  208. (dev->device_id == devs[i].device_id))
  209. break;
  210. /* Not supported, try the next one. */
  211. if (devs[i].device_name == NULL)
  212. continue;
  213. msg_pdbg("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n", devs[i].vendor_name,
  214. devs[i].device_name, dev->vendor_id, dev->device_id, dev->bus, dev->dev,
  215. dev->func);
  216. if (devs[i].status == NT)
  217. msg_pinfo("===\nThis PCI device is UNTESTED. Please report the 'flashrom -p "
  218. "xxxx' output \n"
  219. "to flashrom@flashrom.org if it works for you. Please add the name "
  220. "of your\n"
  221. "PCI device to the subject. Thank you for your help!\n===\n");
  222. /* FIXME: We should count all matching devices, not
  223. * just those with a valid BAR.
  224. */
  225. if ((addr = pcidev_readbar(dev, bar)) != 0) {
  226. found_dev = dev;
  227. found++;
  228. }
  229. }
  230. }
  231. /* Only continue if exactly one supported PCI dev has been found. */
  232. if (found == 0) {
  233. msg_perr("Error: No supported PCI device found.\n");
  234. return NULL;
  235. } else if (found > 1) {
  236. msg_perr("Error: Multiple supported PCI devices found. Use 'flashrom -p xxxx:pci=bb:dd.f' \n"
  237. "to explicitly select the card with the given BDF (PCI bus, device, function).\n");
  238. return NULL;
  239. }
  240. return found_dev;
  241. }
  242. enum pci_write_type {
  243. pci_write_type_byte,
  244. pci_write_type_word,
  245. pci_write_type_long,
  246. };
  247. struct undo_pci_write_data {
  248. struct pci_dev dev;
  249. int reg;
  250. enum pci_write_type type;
  251. union {
  252. uint8_t bytedata;
  253. uint16_t worddata;
  254. uint32_t longdata;
  255. };
  256. };
  257. int undo_pci_write(void *p)
  258. {
  259. struct undo_pci_write_data *data = p;
  260. if (pacc == NULL) {
  261. msg_perr("%s: Tried to undo PCI writes without a valid PCI context!\n"
  262. "Please report a bug at flashrom@flashrom.org\n", __func__);
  263. return 1;
  264. }
  265. msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n",
  266. data->dev.bus, data->dev.dev, data->dev.func, data->reg);
  267. switch (data->type) {
  268. case pci_write_type_byte:
  269. pci_write_byte(&data->dev, data->reg, data->bytedata);
  270. break;
  271. case pci_write_type_word:
  272. pci_write_word(&data->dev, data->reg, data->worddata);
  273. break;
  274. case pci_write_type_long:
  275. pci_write_long(&data->dev, data->reg, data->longdata);
  276. break;
  277. }
  278. /* p was allocated in register_undo_pci_write. */
  279. free(p);
  280. return 0;
  281. }
  282. #define register_undo_pci_write(a, b, c) \
  283. { \
  284. struct undo_pci_write_data *undo_pci_write_data; \
  285. undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \
  286. if (!undo_pci_write_data) { \
  287. msg_gerr("Out of memory!\n"); \
  288. exit(1); \
  289. } \
  290. undo_pci_write_data->dev = *a; \
  291. undo_pci_write_data->reg = b; \
  292. undo_pci_write_data->type = pci_write_type_##c; \
  293. undo_pci_write_data->c##data = pci_read_##c(dev, reg); \
  294. register_shutdown(undo_pci_write, undo_pci_write_data); \
  295. }
  296. #define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte)
  297. #define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word)
  298. #define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long)
  299. int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data)
  300. {
  301. register_undo_pci_write_byte(dev, reg);
  302. return pci_write_byte(dev, reg, data);
  303. }
  304. int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data)
  305. {
  306. register_undo_pci_write_word(dev, reg);
  307. return pci_write_word(dev, reg, data);
  308. }
  309. int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data)
  310. {
  311. register_undo_pci_write_long(dev, reg);
  312. return pci_write_long(dev, reg, data);
  313. }