Bjorn Helgaas | 7328c8f | 2018-01-26 11:45:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 3 | * PCI ROM access routines |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> |
| 6 | * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include "pci.h" |
| 14 | |
| 15 | /** |
| 16 | * pci_enable_rom - enable ROM decoding for a PCI device |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 17 | * @pdev: PCI device to enable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
| 19 | * Enable ROM decoding on @dev. This involves simply turning on the last |
| 20 | * bit of the PCI ROM BAR. Note that some cards may share address decoders |
| 21 | * between the ROM and other resources, so enabling it may disable access |
| 22 | * to MMIO registers or other card memory. |
| 23 | */ |
Alan Cox | e416de5 | 2008-09-23 17:25:10 +0100 | [diff] [blame] | 24 | int pci_enable_rom(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Bjorn Helgaas | 4708f9a | 2016-03-03 08:53:47 -0600 | [diff] [blame] | 26 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
Benjamin Herrenschmidt | 8085ce08 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 27 | struct pci_bus_region region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | u32 rom_addr; |
| 29 | |
Benjamin Herrenschmidt | 8085ce08 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 30 | if (!res->flags) |
| 31 | return -1; |
| 32 | |
Bjorn Helgaas | 4708f9a | 2016-03-03 08:53:47 -0600 | [diff] [blame] | 33 | /* Nothing to enable if we're using a shadow copy in RAM */ |
| 34 | if (res->flags & IORESOURCE_ROM_SHADOW) |
| 35 | return 0; |
| 36 | |
Bjorn Helgaas | 0b457dde | 2016-11-28 16:17:41 -0600 | [diff] [blame] | 37 | /* |
| 38 | * Ideally pci_update_resource() would update the ROM BAR address, |
| 39 | * and we would only set the enable bit here. But apparently some |
| 40 | * devices have buggy ROM BARs that read as zero when disabled. |
| 41 | */ |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 42 | pcibios_resource_to_bus(pdev->bus, ®ion, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); |
Benjamin Herrenschmidt | 8085ce08 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 44 | rom_addr &= ~PCI_ROM_ADDRESS_MASK; |
| 45 | rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); |
Benjamin Herrenschmidt | 8085ce08 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 47 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
Ryan Desfosses | b7fe943 | 2014-04-25 14:32:25 -0600 | [diff] [blame] | 49 | EXPORT_SYMBOL_GPL(pci_enable_rom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * pci_disable_rom - disable ROM decoding for a PCI device |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 53 | * @pdev: PCI device to disable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * |
| 55 | * Disable ROM decoding on a PCI device by turning off the last bit in the |
| 56 | * ROM BAR. |
| 57 | */ |
Alan Cox | e416de5 | 2008-09-23 17:25:10 +0100 | [diff] [blame] | 58 | void pci_disable_rom(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Bjorn Helgaas | 4708f9a | 2016-03-03 08:53:47 -0600 | [diff] [blame] | 60 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | u32 rom_addr; |
Bjorn Helgaas | 4708f9a | 2016-03-03 08:53:47 -0600 | [diff] [blame] | 62 | |
| 63 | if (res->flags & IORESOURCE_ROM_SHADOW) |
| 64 | return; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); |
| 67 | rom_addr &= ~PCI_ROM_ADDRESS_ENABLE; |
| 68 | pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); |
| 69 | } |
Ryan Desfosses | b7fe943 | 2014-04-25 14:32:25 -0600 | [diff] [blame] | 70 | EXPORT_SYMBOL_GPL(pci_disable_rom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 73 | * pci_get_rom_size - obtain the actual size of the ROM image |
Randy Dunlap | 4cc59c7 | 2009-02-09 09:31:20 -0800 | [diff] [blame] | 74 | * @pdev: target PCI device |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 75 | * @rom: kernel virtual pointer to image of ROM |
| 76 | * @size: size of PCI window |
| 77 | * return: size of actual ROM image |
| 78 | * |
| 79 | * Determine the actual length of the ROM image. |
| 80 | * The PCI window size could be much larger than the |
| 81 | * actual image size. |
| 82 | */ |
Bjorn Helgaas | 783e849 | 2018-06-29 21:17:26 -0500 | [diff] [blame] | 83 | static size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, |
| 84 | size_t size) |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 85 | { |
| 86 | void __iomem *image; |
| 87 | int last_image; |
Michel Dänzer | 16b036a | 2015-01-19 17:53:20 +0900 | [diff] [blame] | 88 | unsigned length; |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 89 | |
| 90 | image = rom; |
| 91 | do { |
| 92 | void __iomem *pds; |
| 93 | /* Standard PCI ROMs start out with these bytes 55 AA */ |
Vladis Dronov | 4066df6 | 2015-11-27 18:20:06 +0100 | [diff] [blame] | 94 | if (readw(image) != 0xAA55) { |
Bjorn Helgaas | 414ae76 | 2018-01-31 10:12:56 -0600 | [diff] [blame] | 95 | pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n", |
| 96 | readw(image)); |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 97 | break; |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 98 | } |
Vladis Dronov | 4066df6 | 2015-11-27 18:20:06 +0100 | [diff] [blame] | 99 | /* get the PCI data structure and check its "PCIR" signature */ |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 100 | pds = image + readw(image + 24); |
Vladis Dronov | 4066df6 | 2015-11-27 18:20:06 +0100 | [diff] [blame] | 101 | if (readl(pds) != 0x52494350) { |
Bjorn Helgaas | 414ae76 | 2018-01-31 10:12:56 -0600 | [diff] [blame] | 102 | pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n", |
| 103 | readl(pds)); |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 104 | break; |
Vladis Dronov | 4066df6 | 2015-11-27 18:20:06 +0100 | [diff] [blame] | 105 | } |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 106 | last_image = readb(pds + 21) & 0x80; |
Michel Dänzer | 16b036a | 2015-01-19 17:53:20 +0900 | [diff] [blame] | 107 | length = readw(pds + 16); |
| 108 | image += length * 512; |
Edward O'Callaghan | 47b975d | 2016-01-08 12:16:04 -0600 | [diff] [blame] | 109 | /* Avoid iterating through memory outside the resource window */ |
Rex Zhu | 445ec32 | 2018-06-05 09:46:45 +0800 | [diff] [blame] | 110 | if (image >= rom + size) |
Edward O'Callaghan | 47b975d | 2016-01-08 12:16:04 -0600 | [diff] [blame] | 111 | break; |
Rex Zhu | beced88 | 2018-06-05 09:46:46 +0800 | [diff] [blame] | 112 | if (!last_image) { |
| 113 | if (readw(image) != 0xAA55) { |
| 114 | pci_info(pdev, "No more image in the PCI ROM\n"); |
| 115 | break; |
| 116 | } |
| 117 | } |
Michel Dänzer | 16b036a | 2015-01-19 17:53:20 +0900 | [diff] [blame] | 118 | } while (length && !last_image); |
John Keller | d7ad225 | 2007-07-09 11:42:24 -0700 | [diff] [blame] | 119 | |
| 120 | /* never return a size larger than the PCI resource window */ |
| 121 | /* there are known ROMs that get the size wrong */ |
| 122 | return min((size_t)(image - rom), size); |
| 123 | } |
| 124 | |
| 125 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * pci_map_rom - map a PCI ROM to kernel space |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 127 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | * @size: pointer to receive size of pci window over ROM |
Randy Dunlap | f5dafca | 2008-10-29 22:35:12 -0700 | [diff] [blame] | 129 | * |
| 130 | * Return: kernel virtual pointer to image of ROM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | * |
| 132 | * Map a PCI ROM into kernel space. If ROM is boot video ROM, |
| 133 | * the shadow BIOS copy will be returned instead of the |
| 134 | * actual ROM. |
| 135 | */ |
| 136 | void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size) |
| 137 | { |
| 138 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
Matthew Garrett | fffe01f | 2013-03-26 17:25:54 -0400 | [diff] [blame] | 139 | loff_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | void __iomem *rom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Bjorn Helgaas | f50dd8c | 2016-03-01 11:13:30 -0600 | [diff] [blame] | 142 | /* assign the ROM an address if it doesn't have one */ |
| 143 | if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE)) |
| 144 | return NULL; |
| 145 | |
| 146 | start = pci_resource_start(pdev, PCI_ROM_RESOURCE); |
| 147 | *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
| 148 | if (*size == 0) |
| 149 | return NULL; |
| 150 | |
| 151 | /* Enable ROM space decodes */ |
| 152 | if (pci_enable_rom(pdev)) |
| 153 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | rom = ioremap(start, *size); |
Changbin Du | a48a687 | 2017-11-07 18:22:26 -0600 | [diff] [blame] | 156 | if (!rom) |
| 157 | goto err_ioremap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * Try to find the true size of the ROM since sometimes the PCI window |
| 161 | * size is much larger than the actual size of the ROM. |
| 162 | * True size is important if the ROM is going to be copied. |
| 163 | */ |
Timothy S. Nelson | 97c4483 | 2009-01-30 06:12:47 +1100 | [diff] [blame] | 164 | *size = pci_get_rom_size(pdev, rom, *size); |
Changbin Du | a405f19 | 2017-11-07 18:26:53 -0600 | [diff] [blame] | 165 | if (!*size) |
| 166 | goto invalid_rom; |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return rom; |
Changbin Du | a48a687 | 2017-11-07 18:22:26 -0600 | [diff] [blame] | 169 | |
Changbin Du | a405f19 | 2017-11-07 18:26:53 -0600 | [diff] [blame] | 170 | invalid_rom: |
| 171 | iounmap(rom); |
Changbin Du | a48a687 | 2017-11-07 18:22:26 -0600 | [diff] [blame] | 172 | err_ioremap: |
| 173 | /* restore enable if ioremap fails */ |
| 174 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
| 175 | pci_disable_rom(pdev); |
| 176 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
Ryan Desfosses | b7fe943 | 2014-04-25 14:32:25 -0600 | [diff] [blame] | 178 | EXPORT_SYMBOL(pci_map_rom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /** |
| 181 | * pci_unmap_rom - unmap the ROM from kernel space |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 182 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * @rom: virtual address of the previous mapping |
| 184 | * |
| 185 | * Remove a mapping of a previously mapped ROM |
| 186 | */ |
| 187 | void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom) |
| 188 | { |
| 189 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
| 190 | |
Matthew Garrett | fffe01f | 2013-03-26 17:25:54 -0400 | [diff] [blame] | 191 | iounmap(rom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Bjorn Helgaas | 4708f9a | 2016-03-03 08:53:47 -0600 | [diff] [blame] | 193 | /* Disable again before continuing */ |
| 194 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | pci_disable_rom(pdev); |
| 196 | } |
Ryan Desfosses | b7fe943 | 2014-04-25 14:32:25 -0600 | [diff] [blame] | 197 | EXPORT_SYMBOL(pci_unmap_rom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /** |
Matthew Garrett | fffe01f | 2013-03-26 17:25:54 -0400 | [diff] [blame] | 200 | * pci_platform_rom - provides a pointer to any ROM image provided by the |
| 201 | * platform |
| 202 | * @pdev: pointer to pci device struct |
| 203 | * @size: pointer to receive size of pci window over ROM |
| 204 | */ |
| 205 | void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size) |
| 206 | { |
| 207 | if (pdev->rom && pdev->romlen) { |
| 208 | *size = pdev->romlen; |
| 209 | return phys_to_virt((phys_addr_t)pdev->rom); |
| 210 | } |
| 211 | |
| 212 | return NULL; |
| 213 | } |
Matthew Garrett | fffe01f | 2013-03-26 17:25:54 -0400 | [diff] [blame] | 214 | EXPORT_SYMBOL(pci_platform_rom); |