pcidev.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. uint32_t io_base_addr;
  26. struct pci_access *pacc;
  27. struct pci_dev *pcidev_dev = NULL;
  28. enum pci_bartype {
  29. TYPE_MEMBAR,
  30. TYPE_IOBAR,
  31. TYPE_ROMBAR,
  32. TYPE_UNKNOWN
  33. };
  34. uintptr_t pcidev_validate(struct pci_dev *dev, int bar,
  35. const struct pcidev_status *devs)
  36. {
  37. int i;
  38. uint64_t addr;
  39. uint32_t upperaddr;
  40. uint8_t headertype;
  41. uint16_t supported_cycles;
  42. enum pci_bartype bartype = TYPE_UNKNOWN;
  43. for (i = 0; devs[i].device_name != NULL; i++) {
  44. if (dev->device_id != devs[i].device_id)
  45. continue;
  46. msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n",
  47. devs[i].vendor_name, devs[i].device_name,
  48. dev->vendor_id, dev->device_id, dev->bus, dev->dev,
  49. dev->func);
  50. headertype = pci_read_byte(dev, PCI_HEADER_TYPE) & 0x7f;
  51. msg_pspew("PCI header type 0x%02x\n", headertype);
  52. /*
  53. * Don't use dev->base_addr[x] (as value for 'bar'), won't
  54. * work on older libpci.
  55. */
  56. addr = pci_read_long(dev, bar);
  57. /* Sanity checks. */
  58. switch (headertype) {
  59. case PCI_HEADER_TYPE_NORMAL:
  60. switch (bar) {
  61. case PCI_BASE_ADDRESS_0:
  62. case PCI_BASE_ADDRESS_1:
  63. case PCI_BASE_ADDRESS_2:
  64. case PCI_BASE_ADDRESS_3:
  65. case PCI_BASE_ADDRESS_4:
  66. case PCI_BASE_ADDRESS_5:
  67. if ((addr & PCI_BASE_ADDRESS_SPACE) ==
  68. PCI_BASE_ADDRESS_SPACE_IO)
  69. bartype = TYPE_IOBAR;
  70. else
  71. bartype = TYPE_MEMBAR;
  72. break;
  73. case PCI_ROM_ADDRESS:
  74. bartype = TYPE_ROMBAR;
  75. break;
  76. }
  77. break;
  78. case PCI_HEADER_TYPE_BRIDGE:
  79. switch (bar) {
  80. case PCI_BASE_ADDRESS_0:
  81. case PCI_BASE_ADDRESS_1:
  82. if ((addr & PCI_BASE_ADDRESS_SPACE) ==
  83. PCI_BASE_ADDRESS_SPACE_IO)
  84. bartype = TYPE_IOBAR;
  85. else
  86. bartype = TYPE_MEMBAR;
  87. break;
  88. case PCI_ROM_ADDRESS1:
  89. bartype = TYPE_ROMBAR;
  90. break;
  91. }
  92. break;
  93. case PCI_HEADER_TYPE_CARDBUS:
  94. break;
  95. default:
  96. msg_perr("Unknown PCI header type 0x%02x, BAR type "
  97. "cannot be determined reliably.\n", headertype);
  98. break;
  99. }
  100. supported_cycles = pci_read_word(dev, PCI_COMMAND);
  101. msg_pdbg("Requested BAR is ");
  102. switch (bartype) {
  103. case TYPE_MEMBAR:
  104. msg_pdbg("MEM");
  105. if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
  106. msg_perr("MEM BAR access requested, but device "
  107. "has MEM space accesses disabled.\n");
  108. /* TODO: Abort here? */
  109. }
  110. msg_pdbg(", %sbit, %sprefetchable\n",
  111. ((addr & 0x6) == 0x0) ? "32" :
  112. (((addr & 0x6) == 0x4) ? "64" : "reserved"),
  113. (addr & 0x8) ? "" : "not ");
  114. if ((addr & 0x6) == 0x4) {
  115. /* The spec says that a 64-bit register consumes
  116. * two subsequent dword locations.
  117. */
  118. upperaddr = pci_read_long(dev, bar + 4);
  119. if (upperaddr != 0x00000000) {
  120. /* Fun! A real 64-bit resource. */
  121. if (sizeof(uintptr_t) != sizeof(uint64_t)) {
  122. msg_perr("BAR unreachable!");
  123. /* TODO: Really abort here? If
  124. * multiple PCI devices match,
  125. * we might never tell the user
  126. * about the other devices.
  127. */
  128. return 0;
  129. }
  130. addr |= (uint64_t)upperaddr << 32;
  131. }
  132. }
  133. addr &= PCI_BASE_ADDRESS_MEM_MASK;
  134. break;
  135. case TYPE_IOBAR:
  136. msg_pdbg("I/O\n");
  137. #if __FLASHROM_HAVE_OUTB__
  138. if (!(supported_cycles & PCI_COMMAND_IO)) {
  139. msg_perr("I/O BAR access requested, but device "
  140. "has I/O space accesses disabled.\n");
  141. /* TODO: Abort here? */
  142. }
  143. #else
  144. msg_perr("I/O BAR access requested, but flashrom does "
  145. "not support I/O BAR access on this platform "
  146. "(yet).\n");
  147. #endif
  148. addr &= PCI_BASE_ADDRESS_IO_MASK;
  149. break;
  150. case TYPE_ROMBAR:
  151. msg_pdbg("ROM\n");
  152. /* Not sure if this check is needed. */
  153. if (!(supported_cycles & PCI_COMMAND_MEMORY)) {
  154. msg_perr("MEM BAR access requested, but device "
  155. "has MEM space accesses disabled.\n");
  156. /* TODO: Abort here? */
  157. }
  158. addr &= PCI_ROM_ADDRESS_MASK;
  159. break;
  160. case TYPE_UNKNOWN:
  161. msg_perr("BAR type unknown, please report a bug at "
  162. "flashrom@flashrom.org\n");
  163. }
  164. if (devs[i].status == NT) {
  165. msg_pinfo("===\nThis PCI device is UNTESTED. Please "
  166. "report the 'flashrom -p xxxx' output \n"
  167. "to flashrom@flashrom.org if it works "
  168. "for you. Please add the name of your\n"
  169. "PCI device to the subject. Thank you for "
  170. "your help!\n===\n");
  171. }
  172. return (uintptr_t)addr;
  173. }
  174. return 0;
  175. }
  176. uintptr_t pcidev_init(int bar, const struct pcidev_status *devs)
  177. {
  178. struct pci_dev *dev;
  179. struct pci_filter filter;
  180. char *pcidev_bdf;
  181. char *msg = NULL;
  182. int found = 0;
  183. uintptr_t addr = 0, curaddr = 0;
  184. pacc = pci_alloc(); /* Get the pci_access structure */
  185. pci_init(pacc); /* Initialize the PCI library */
  186. pci_scan_bus(pacc); /* We want to get the list of devices */
  187. pci_filter_init(pacc, &filter);
  188. /* Filter by bb:dd.f (if supplied by the user). */
  189. pcidev_bdf = extract_programmer_param("pci");
  190. if (pcidev_bdf != NULL) {
  191. if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
  192. msg_perr("Error: %s\n", msg);
  193. exit(1);
  194. }
  195. }
  196. free(pcidev_bdf);
  197. for (dev = pacc->devices; dev; dev = dev->next) {
  198. if (pci_filter_match(&filter, dev)) {
  199. /* FIXME: We should count all matching devices, not
  200. * just those with a valid BAR.
  201. */
  202. if ((addr = pcidev_validate(dev, bar, devs)) != 0) {
  203. curaddr = addr;
  204. pcidev_dev = dev;
  205. found++;
  206. }
  207. }
  208. }
  209. /* Only continue if exactly one supported PCI dev has been found. */
  210. if (found == 0) {
  211. msg_perr("Error: No supported PCI device found.\n");
  212. exit(1);
  213. } else if (found > 1) {
  214. msg_perr("Error: Multiple supported PCI devices found. "
  215. "Use 'flashrom -p xxxx:pci=bb:dd.f' \n"
  216. "to explicitly select the card with the given BDF "
  217. "(PCI bus, device, function).\n");
  218. exit(1);
  219. }
  220. return curaddr;
  221. }
  222. void print_supported_pcidevs(const struct pcidev_status *devs)
  223. {
  224. int i;
  225. msg_pinfo("PCI devices:\n");
  226. for (i = 0; devs[i].vendor_name != NULL; i++) {
  227. msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
  228. devs[i].device_name, devs[i].vendor_id,
  229. devs[i].device_id,
  230. (devs[i].status == NT) ? " (untested)" : "");
  231. }
  232. }
  233. enum pci_write_type {
  234. pci_write_type_byte,
  235. pci_write_type_word,
  236. pci_write_type_long,
  237. };
  238. struct undo_pci_write_data {
  239. struct pci_dev dev;
  240. int reg;
  241. enum pci_write_type type;
  242. union {
  243. uint8_t bytedata;
  244. uint16_t worddata;
  245. uint32_t longdata;
  246. };
  247. };
  248. int undo_pci_write(void *p)
  249. {
  250. struct undo_pci_write_data *data = p;
  251. msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n",
  252. data->dev.bus, data->dev.dev, data->dev.func, data->reg);
  253. switch (data->type) {
  254. case pci_write_type_byte:
  255. pci_write_byte(&data->dev, data->reg, data->bytedata);
  256. break;
  257. case pci_write_type_word:
  258. pci_write_word(&data->dev, data->reg, data->worddata);
  259. break;
  260. case pci_write_type_long:
  261. pci_write_long(&data->dev, data->reg, data->longdata);
  262. break;
  263. }
  264. /* p was allocated in register_undo_pci_write. */
  265. free(p);
  266. return 0;
  267. }
  268. #define register_undo_pci_write(a, b, c) \
  269. { \
  270. struct undo_pci_write_data *undo_pci_write_data; \
  271. undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \
  272. if (!undo_pci_write_data) { \
  273. msg_gerr("Out of memory!\n"); \
  274. exit(1); \
  275. } \
  276. undo_pci_write_data->dev = *a; \
  277. undo_pci_write_data->reg = b; \
  278. undo_pci_write_data->type = pci_write_type_##c; \
  279. undo_pci_write_data->c##data = pci_read_##c(dev, reg); \
  280. register_shutdown(undo_pci_write, undo_pci_write_data); \
  281. }
  282. #define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte)
  283. #define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word)
  284. #define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long)
  285. int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data)
  286. {
  287. register_undo_pci_write_byte(dev, reg);
  288. return pci_write_byte(dev, reg, data);
  289. }
  290. int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data)
  291. {
  292. register_undo_pci_write_word(dev, reg);
  293. return pci_write_word(dev, reg, data);
  294. }
  295. int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data)
  296. {
  297. register_undo_pci_write_long(dev, reg);
  298. return pci_write_long(dev, reg, data);
  299. }