Author: stefanct Date: Fri Jan 4 23:24:58 2013 New Revision: 1642 URL: http://flashrom.org/trac/flashrom/changeset/1642
Log: Unify PCI init and let pcidev clean itself up.
Previously the internal programmer used its own code to initialize pcilib. This patch extracts the common code from the internal programmer and pcidev_init() into pcidev_init_common(). This fixes the non-existent PCI cleanup of the internal programmer and adds an additional safety by checking for an already existing PCI context.
We got a nice shutdown function registration infrastructure, but did not use it very wisely. Instead we added shutdown functions to a myriad of programmers unnecessarily. In this patch we get rid of those that do only call pci_cleanup(pacc) by adding a shutdown function the pcidev.c itself that gets registered by pcidev_init().
Signed-off-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at Acked-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Modified: trunk/atahpt.c trunk/drkaiser.c trunk/gfxnvidia.c trunk/internal.c trunk/nic3com.c trunk/nicintel.c trunk/nicintel_spi.c trunk/nicrealtek.c trunk/ogp_spi.c trunk/pcidev.c trunk/programmer.h trunk/satamv.c trunk/satasii.c
Modified: trunk/atahpt.c ============================================================================== --- trunk/atahpt.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/atahpt.c Fri Jan 4 23:24:58 2013 (r1642) @@ -56,13 +56,6 @@ .chip_writen = fallback_chip_writen, };
-static int atahpt_shutdown(void *data) -{ - /* Flash access is disabled automatically by PCI restore. */ - pci_cleanup(pacc); - return 0; -} - int atahpt_init(void) { uint32_t reg32; @@ -72,9 +65,6 @@
io_base_addr = pcidev_init(PCI_BASE_ADDRESS_4, ata_hpt);
- if (register_shutdown(atahpt_shutdown, NULL)) - return 1; - /* Enable flash access. */ reg32 = pci_read_long(pcidev_dev, REG_FLASH_ACCESS); reg32 |= (1 << 24);
Modified: trunk/drkaiser.c ============================================================================== --- trunk/drkaiser.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/drkaiser.c Fri Jan 4 23:24:58 2013 (r1642) @@ -59,8 +59,6 @@ static int drkaiser_shutdown(void *data) { physunmap(drkaiser_bar, DRKAISER_MEMMAP_SIZE); - /* Flash write is disabled automatically by PCI restore. */ - pci_cleanup(pacc); return 0; }
@@ -71,6 +69,7 @@ if (rget_io_perms()) return 1;
+ /* No need to check for errors, pcidev_init() will not return in case of errors. */ addr = pcidev_init(PCI_BASE_ADDRESS_2, drkaiser_pcidev);
/* Write magic register to enable flash write. */
Modified: trunk/gfxnvidia.c ============================================================================== --- trunk/gfxnvidia.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/gfxnvidia.c Fri Jan 4 23:24:58 2013 (r1642) @@ -80,10 +80,6 @@ static int gfxnvidia_shutdown(void *data) { physunmap(nvidia_bar, GFXNVIDIA_MEMMAP_SIZE); - /* Flash interface access is disabled (and screen enabled) automatically - * by PCI restore. - */ - pci_cleanup(pacc); return 0; }
@@ -94,6 +90,7 @@ if (rget_io_perms()) return 1;
+ /* No need to check for errors, pcidev_init() will not return in case of errors. */ io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, gfx_nvidia);
io_base_addr += 0x300000; @@ -101,7 +98,6 @@
nvidia_bar = physmap("NVIDIA", io_base_addr, GFXNVIDIA_MEMMAP_SIZE);
- /* Must be done before rpci calls. */ if (register_shutdown(gfxnvidia_shutdown, NULL)) return 1;
Modified: trunk/internal.c ============================================================================== --- trunk/internal.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/internal.c Fri Jan 4 23:24:58 2013 (r1642) @@ -245,10 +245,8 @@ internal_buses_supported = BUS_NONSPI;
/* Initialize PCI access for flash enables */ - pacc = pci_alloc(); /* Get the pci_access structure */ - /* Set all options you want -- here we stick with the defaults */ - pci_init(pacc); /* Initialize the PCI library */ - pci_scan_bus(pacc); /* We want to get the list of devices */ + if (pci_init_common() != 0) + return 1;
if (processor_flash_enable()) { msg_perr("Processor detection/init failed.\n"
Modified: trunk/nic3com.c ============================================================================== --- trunk/nic3com.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/nic3com.c Fri Jan 4 23:24:58 2013 (r1642) @@ -81,7 +81,6 @@ OUTL(internal_conf, io_base_addr + INTERNAL_CONFIG); }
- pci_cleanup(pacc); return 0; }
@@ -90,6 +89,7 @@ if (rget_io_perms()) return 1;
+ /* No need to check for errors, pcidev_init() will not return in case of errors. */ io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_3com);
id = pcidev_dev->device_id;
Modified: trunk/nicintel.c ============================================================================== --- trunk/nicintel.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/nicintel.c Fri Jan 4 23:24:58 2013 (r1642) @@ -63,7 +63,6 @@ { physunmap(nicintel_control_bar, NICINTEL_CONTROL_MEMMAP_SIZE); physunmap(nicintel_bar, NICINTEL_MEMMAP_SIZE); - pci_cleanup(pacc); return 0; }
@@ -77,8 +76,7 @@ if (rget_io_perms()) return 1;
- /* No need to check for errors, pcidev_init() will not return in case - * of errors. + /* No need to check for errors, pcidev_init() will not return in case of errors. * FIXME: BAR2 is not available if the device uses the CardBus function. */ addr = pcidev_init(PCI_BASE_ADDRESS_2, nics_intel); @@ -117,7 +115,6 @@ error_out_unmap: physunmap(nicintel_bar, NICINTEL_MEMMAP_SIZE); error_out: - pci_cleanup(pacc); return 1; }
Modified: trunk/nicintel_spi.c ============================================================================== --- trunk/nicintel_spi.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/nicintel_spi.c Fri Jan 4 23:24:58 2013 (r1642) @@ -160,7 +160,6 @@ pci_mmio_writel(tmp, nicintel_spibar + EECD);
physunmap(nicintel_spibar, MEMMAP_SIZE); - pci_cleanup(pacc);
return 0; } @@ -172,6 +171,7 @@ if (rget_io_perms()) return 1;
+ /* No need to check for errors, pcidev_init() will not return in case of errors. */ io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_intel_spi);
nicintel_spibar = physmap("Intel Gigabit NIC w/ SPI flash",
Modified: trunk/nicrealtek.c ============================================================================== --- trunk/nicrealtek.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/nicrealtek.c Fri Jan 4 23:24:58 2013 (r1642) @@ -54,7 +54,6 @@ static int nicrealtek_shutdown(void *data) { /* FIXME: We forgot to disable software access again. */ - pci_cleanup(pacc); return 0; }
@@ -63,8 +62,8 @@ if (rget_io_perms()) return 1;
+ /* No need to check for errors, pcidev_init() will not return in case of errors. */ io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_realtek); - if (register_shutdown(nicrealtek_shutdown, NULL)) return 1;
Modified: trunk/ogp_spi.c ============================================================================== --- trunk/ogp_spi.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/ogp_spi.c Fri Jan 4 23:24:58 2013 (r1642) @@ -100,8 +100,6 @@ static int ogp_spi_shutdown(void *data) { physunmap(ogp_spibar, 4096); - pci_cleanup(pacc); - return 0; }
Modified: trunk/pcidev.c ============================================================================== --- trunk/pcidev.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/pcidev.c Fri Jan 4 23:24:58 2013 (r1642) @@ -154,6 +154,33 @@ return (uintptr_t)addr; }
+static int pcidev_shutdown(void *data) +{ + pcidev_dev = NULL; + if (pacc == NULL) { + msg_perr("%s: Tried to cleanup an invalid PCI context!\n" + "Please report a bug at flashrom@flashrom.org\n", __func__); + return 1; + } + pci_cleanup(pacc); + return 0; +} + +int pci_init_common(void) +{ + if (pacc != NULL) { + msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n" + "Please report a bug at flashrom@flashrom.org\n", __func__); + return 1; + } + pacc = pci_alloc(); /* Get the pci_access structure */ + pci_init(pacc); /* Initialize the PCI library */ + if (register_shutdown(pcidev_shutdown, NULL)) + return 1; + pci_scan_bus(pacc); /* We want to get the list of devices */ + return 0; +} + uintptr_t pcidev_init(int bar, const struct dev_entry *devs) { struct pci_dev *dev; @@ -164,9 +191,8 @@ int i; uintptr_t addr = 0, curaddr = 0;
- pacc = pci_alloc(); /* Get the pci_access structure */ - pci_init(pacc); /* Initialize the PCI library */ - pci_scan_bus(pacc); /* We want to get the list of devices */ + if(pci_init_common() != 0) + return 1; pci_filter_init(pacc, &filter);
/* Filter by bb:dd.f (if supplied by the user). */ @@ -244,6 +270,11 @@ int undo_pci_write(void *p) { struct undo_pci_write_data *data = p; + if (pacc == NULL) { + msg_perr("%s: Tried to undo PCI writes without a valid PCI context!\n" + "Please report a bug at flashrom@flashrom.org\n", __func__); + return 1; + } msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n", data->dev.bus, data->dev.dev, data->dev.func, data->reg); switch (data->type) {
Modified: trunk/programmer.h ============================================================================== --- trunk/programmer.h Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/programmer.h Fri Jan 4 23:24:58 2013 (r1642) @@ -238,6 +238,7 @@ extern uint32_t io_base_addr; extern struct pci_access *pacc; extern struct pci_dev *pcidev_dev; +int pci_init_common(void); uintptr_t pcidev_readbar(struct pci_dev *dev, int bar); uintptr_t pcidev_init(int bar, const struct dev_entry *devs); /* rpci_write_* are reversible writes. The original PCI config space register
Modified: trunk/satamv.c ============================================================================== --- trunk/satamv.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/satamv.c Fri Jan 4 23:24:58 2013 (r1642) @@ -60,7 +60,6 @@ static int satamv_shutdown(void *data) { physunmap(mv_bar, 0x20000); - pci_cleanup(pacc); return 0; }
@@ -96,7 +95,7 @@
mv_bar = physmap("Marvell 88SX7042 registers", addr, 0x20000); if (mv_bar == ERROR_PTR) - goto error_out; + return 1;
if (register_shutdown(satamv_shutdown, NULL)) return 1; @@ -159,10 +158,6 @@ register_par_programmer(&par_programmer_satamv, BUS_PARALLEL);
return 0; - -error_out: - pci_cleanup(pacc); - return 1; }
/* BAR2 (MEM) can map NVRAM and flash. We set it to flash in the init function.
Modified: trunk/satasii.c ============================================================================== --- trunk/satasii.c Thu Jan 3 21:44:30 2013 (r1641) +++ trunk/satasii.c Fri Jan 4 23:24:58 2013 (r1642) @@ -57,7 +57,6 @@ static int satasii_shutdown(void *data) { physunmap(sii_bar, SATASII_MEMMAP_SIZE); - pci_cleanup(pacc); return 0; }