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. |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 20 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 21 | #include "programmer.h" |
Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 22 | #include "platform/pci.h" |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 23 | |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 24 | struct pci_access *pacc; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 25 | |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 26 | enum pci_bartype { |
| 27 | TYPE_MEMBAR, |
| 28 | TYPE_IOBAR, |
| 29 | TYPE_ROMBAR, |
| 30 | TYPE_UNKNOWN |
| 31 | }; |
| 32 | |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 33 | uintptr_t pcidev_readbar(struct pci_dev *dev, int bar) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 34 | { |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 35 | uint64_t addr; |
| 36 | uint32_t upperaddr; |
| 37 | uint8_t headertype; |
| 38 | uint16_t supported_cycles; |
| 39 | enum pci_bartype bartype = TYPE_UNKNOWN; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 40 | |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 41 | |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 42 | headertype = pci_read_byte(dev, PCI_HEADER_TYPE) & 0x7f; |
| 43 | msg_pspew("PCI header type 0x%02x\n", headertype); |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 44 | |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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); |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 47 | |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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; |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 62 | break; |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 63 | case PCI_ROM_ADDRESS: |
| 64 | bartype = TYPE_ROMBAR; |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 65 | break; |
| 66 | } |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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; |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 76 | break; |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 77 | case PCI_ROM_ADDRESS1: |
| 78 | bartype = TYPE_ROMBAR; |
Carl-Daniel Hailfinger | 8a19ef1 | 2011-02-15 22:44:27 +0000 | [diff] [blame] | 79 | break; |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 80 | } |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 90 | supported_cycles = pci_read_word(dev, PCI_COMMAND); |
| 91 | |
Niklas Söderlund | 89edf36 | 2013-08-23 23:29:23 +0000 | [diff] [blame] | 92 | msg_pdbg("Requested BAR is of type "); |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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"); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 124 | #if __FLASHPROG_HAVE_OUTB__ |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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 |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 130 | msg_perr("I/O BAR access requested, but flashprog does not support I/O BAR access on this " |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 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: |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 145 | msg_perr("BAR type unknown, please report a bug at flashprog@flashprog.org\n"); |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return (uintptr_t)addr; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Edward O'Callaghan | 15004ba | 2021-11-13 13:14:06 +1100 | [diff] [blame] | 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 | |
Edward O'Callaghan | 6c73e27 | 2021-11-13 17:56:20 +1100 | [diff] [blame] | 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 | |
Edward O'Callaghan | 19ce50d | 2021-11-13 17:59:18 +1100 | [diff] [blame] | 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 | |
Edward O'Callaghan | 48a9466 | 2022-02-26 11:36:17 +1100 | [diff] [blame] | 193 | struct pci_dev *pcidev_find_vendorclass(uint16_t vendor, uint16_t devclass) |
| 194 | { |
| 195 | struct pci_dev *temp = NULL; |
| 196 | struct pci_filter filter; |
| 197 | uint16_t tmp2; |
| 198 | |
| 199 | pci_filter_init(NULL, &filter); |
| 200 | filter.vendor = vendor; |
| 201 | |
| 202 | while ((temp = pcidev_scandev(&filter, temp))) { |
| 203 | /* Read PCI class */ |
| 204 | tmp2 = pci_read_word(temp, PCI_CLASS_DEVICE); |
| 205 | if (tmp2 == devclass) |
| 206 | return temp; |
| 207 | } |
| 208 | |
| 209 | return NULL; |
| 210 | } |
| 211 | |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 212 | static int pcidev_shutdown(void *data) |
| 213 | { |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 214 | if (pacc == NULL) { |
| 215 | msg_perr("%s: Tried to cleanup an invalid PCI context!\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 216 | "Please report a bug at flashprog@flashprog.org\n", |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 217 | __func__); |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 218 | return 1; |
| 219 | } |
| 220 | pci_cleanup(pacc); |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 221 | pacc = NULL; |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | int pci_init_common(void) |
| 226 | { |
| 227 | if (pacc != NULL) { |
| 228 | msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 229 | "Please report a bug at flashprog@flashprog.org\n", __func__); |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 230 | return 1; |
| 231 | } |
| 232 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 233 | pci_init(pacc); /* Initialize the PCI library */ |
| 234 | if (register_shutdown(pcidev_shutdown, NULL)) |
| 235 | return 1; |
| 236 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 237 | return 0; |
| 238 | } |
| 239 | |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 240 | /* pcidev_init gets an array of allowed PCI device IDs and returns a pointer to struct pci_dev iff exactly one |
| 241 | * match was found. If the "pci=bb:dd.f" programmer parameter was specified, a match is only considered if it |
| 242 | * also matches the specified bus:device.function. |
| 243 | * For convenience, this function also registers its own undo handlers. |
| 244 | */ |
| 245 | struct pci_dev *pcidev_init(const struct dev_entry *devs, int bar) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 246 | { |
| 247 | struct pci_dev *dev; |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 248 | struct pci_dev *found_dev = NULL; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 249 | struct pci_filter filter; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 250 | char *pcidev_bdf; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 251 | char *msg = NULL; |
| 252 | int found = 0; |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 253 | int i; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 254 | |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 255 | if (pci_init_common() != 0) |
| 256 | return NULL; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 257 | pci_filter_init(pacc, &filter); |
| 258 | |
Carl-Daniel Hailfinger | 40446ee | 2011-03-07 01:08:09 +0000 | [diff] [blame] | 259 | /* Filter by bb:dd.f (if supplied by the user). */ |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 260 | pcidev_bdf = extract_programmer_param("pci"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 261 | if (pcidev_bdf != NULL) { |
| 262 | if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 263 | msg_perr("Error: %s\n", msg); |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 264 | return NULL; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 267 | free(pcidev_bdf); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 268 | |
| 269 | for (dev = pacc->devices; dev; dev = dev->next) { |
| 270 | if (pci_filter_match(&filter, dev)) { |
Daniel Verkamp | 298ac33 | 2020-10-12 12:55:56 -0700 | [diff] [blame] | 271 | pci_fill_info(dev, PCI_FILL_IDENT); |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 272 | /* Check against list of supported devices. */ |
| 273 | for (i = 0; devs[i].device_name != NULL; i++) |
| 274 | if ((dev->vendor_id == devs[i].vendor_id) && |
| 275 | (dev->device_id == devs[i].device_id)) |
| 276 | break; |
| 277 | /* Not supported, try the next one. */ |
| 278 | if (devs[i].device_name == NULL) |
| 279 | continue; |
| 280 | |
| 281 | msg_pdbg("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n", devs[i].vendor_name, |
| 282 | devs[i].device_name, dev->vendor_id, dev->device_id, dev->bus, dev->dev, |
| 283 | dev->func); |
| 284 | if (devs[i].status == NT) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 285 | msg_pinfo("===\nThis PCI device is UNTESTED. Please report the 'flashprog -p " |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 286 | "xxxx' output\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 287 | "to flashprog@flashprog.org if it works for you. Please add " |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 288 | "the name of your\n" |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 289 | "PCI device to the subject. Thank you for your help!\n===\n"); |
| 290 | |
Carl-Daniel Hailfinger | 40446ee | 2011-03-07 01:08:09 +0000 | [diff] [blame] | 291 | /* FIXME: We should count all matching devices, not |
| 292 | * just those with a valid BAR. |
| 293 | */ |
Elyes HAOUAS | 34e7456 | 2020-04-21 21:20:44 +0200 | [diff] [blame] | 294 | if (pcidev_readbar(dev, bar) != 0) { |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 295 | found_dev = dev; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 296 | found++; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 297 | } |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | /* Only continue if exactly one supported PCI dev has been found. */ |
| 302 | if (found == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 303 | msg_perr("Error: No supported PCI device found.\n"); |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 304 | return NULL; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 305 | } else if (found > 1) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 306 | msg_perr("Error: Multiple supported PCI devices found. Use 'flashprog -p xxxx:pci=bb:dd.f'\n" |
Carl-Daniel Hailfinger | 3834c2d | 2012-07-16 21:32:19 +0000 | [diff] [blame] | 307 | "to explicitly select the card with the given BDF (PCI bus, device, function).\n"); |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 308 | return NULL; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Nico Huber | 67710af | 2020-01-18 18:23:22 +0000 | [diff] [blame] | 311 | return found_dev; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 314 | enum pci_write_type { |
| 315 | pci_write_type_byte, |
| 316 | pci_write_type_word, |
| 317 | pci_write_type_long, |
| 318 | }; |
| 319 | |
| 320 | struct undo_pci_write_data { |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 321 | struct pci_dev *dev; |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 322 | int reg; |
| 323 | enum pci_write_type type; |
| 324 | union { |
| 325 | uint8_t bytedata; |
| 326 | uint16_t worddata; |
| 327 | uint32_t longdata; |
| 328 | }; |
| 329 | }; |
| 330 | |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 331 | static int undo_pci_write(void *p) |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 332 | { |
| 333 | struct undo_pci_write_data *data = p; |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 334 | if (pacc == NULL || data->dev == NULL) { |
| 335 | msg_perr("%s: Tried to undo PCI writes without a valid PCI %s!\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 336 | "Please report a bug at flashprog@flashprog.org\n", |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 337 | __func__, data->dev == NULL ? "device" : "context"); |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 338 | return 1; |
| 339 | } |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 340 | msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n", |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 341 | data->dev->bus, data->dev->dev, data->dev->func, data->reg); |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 342 | switch (data->type) { |
| 343 | case pci_write_type_byte: |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 344 | pci_write_byte(data->dev, data->reg, data->bytedata); |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 345 | break; |
| 346 | case pci_write_type_word: |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 347 | pci_write_word(data->dev, data->reg, data->worddata); |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 348 | break; |
| 349 | case pci_write_type_long: |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 350 | pci_write_long(data->dev, data->reg, data->longdata); |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | /* p was allocated in register_undo_pci_write. */ |
| 354 | free(p); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 355 | return 0; |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Elyes HAOUAS | ac01baa | 2018-05-28 16:52:21 +0200 | [diff] [blame] | 358 | #define register_undo_pci_write(a, b, c) \ |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 359 | { \ |
| 360 | struct undo_pci_write_data *undo_pci_write_data; \ |
Angel Pons | 690a944 | 2021-06-07 12:33:53 +0200 | [diff] [blame] | 361 | undo_pci_write_data = malloc(sizeof(*undo_pci_write_data)); \ |
Stefan Tauner | 269de35 | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 362 | if (!undo_pci_write_data) { \ |
| 363 | msg_gerr("Out of memory!\n"); \ |
| 364 | exit(1); \ |
| 365 | } \ |
Youness Alaoui | a54ceb1 | 2017-07-26 18:03:36 -0400 | [diff] [blame] | 366 | if (pacc) \ |
| 367 | undo_pci_write_data->dev = pci_get_dev(pacc, \ |
| 368 | a->domain, a->bus, a->dev, a->func); \ |
| 369 | else \ |
| 370 | undo_pci_write_data->dev = NULL; \ |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 371 | undo_pci_write_data->reg = b; \ |
| 372 | undo_pci_write_data->type = pci_write_type_##c; \ |
| 373 | undo_pci_write_data->c##data = pci_read_##c(dev, reg); \ |
| 374 | register_shutdown(undo_pci_write, undo_pci_write_data); \ |
| 375 | } |
| 376 | |
| 377 | #define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte) |
| 378 | #define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word) |
| 379 | #define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long) |
| 380 | |
| 381 | int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data) |
| 382 | { |
| 383 | register_undo_pci_write_byte(dev, reg); |
| 384 | return pci_write_byte(dev, reg, data); |
| 385 | } |
Elyes HAOUAS | 124ef38 | 2018-03-27 12:15:09 +0200 | [diff] [blame] | 386 | |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 387 | int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data) |
| 388 | { |
| 389 | register_undo_pci_write_word(dev, reg); |
| 390 | return pci_write_word(dev, reg, data); |
| 391 | } |
Elyes HAOUAS | 124ef38 | 2018-03-27 12:15:09 +0200 | [diff] [blame] | 392 | |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 393 | int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data) |
| 394 | { |
| 395 | register_undo_pci_write_long(dev, reg); |
| 396 | return pci_write_long(dev, reg, data); |
| 397 | } |