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