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