blob: 946795fc007187ee21643a324c1e88aab0f23e31 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * PCI ROM access routines
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
6 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -04009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pci.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "pci.h"
14
15/**
16 * pci_enable_rom - enable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070017 * @pdev: PCI device to enable
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
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 Coxe416de52008-09-23 17:25:10 +010024int pci_enable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060026 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100027 struct pci_bus_region region;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 u32 rom_addr;
29
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100030 if (!res->flags)
31 return -1;
32
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060033 /* Nothing to enable if we're using a shadow copy in RAM */
34 if (res->flags & IORESOURCE_ROM_SHADOW)
35 return 0;
36
Bjorn Helgaas0b457dde2016-11-28 16:17:41 -060037 /*
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 Lufc279852013-12-09 22:54:40 -080042 pcibios_resource_to_bus(pdev->bus, &region, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100044 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
45 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -060049EXPORT_SYMBOL_GPL(pci_enable_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/**
52 * pci_disable_rom - disable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070053 * @pdev: PCI device to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 *
55 * Disable ROM decoding on a PCI device by turning off the last bit in the
56 * ROM BAR.
57 */
Alan Coxe416de52008-09-23 17:25:10 +010058void pci_disable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060060 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 u32 rom_addr;
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060062
63 if (res->flags & IORESOURCE_ROM_SHADOW)
64 return;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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 Desfossesb7fe9432014-04-25 14:32:25 -060070EXPORT_SYMBOL_GPL(pci_disable_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/**
John Kellerd7ad2252007-07-09 11:42:24 -070073 * pci_get_rom_size - obtain the actual size of the ROM image
Randy Dunlap4cc59c72009-02-09 09:31:20 -080074 * @pdev: target PCI device
John Kellerd7ad2252007-07-09 11:42:24 -070075 * @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 */
Timothy S. Nelson97c44832009-01-30 06:12:47 +110083size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
John Kellerd7ad2252007-07-09 11:42:24 -070084{
85 void __iomem *image;
86 int last_image;
Michel Dänzer16b036a2015-01-19 17:53:20 +090087 unsigned length;
John Kellerd7ad2252007-07-09 11:42:24 -070088
89 image = rom;
90 do {
91 void __iomem *pds;
92 /* Standard PCI ROMs start out with these bytes 55 AA */
Vladis Dronov4066df62015-11-27 18:20:06 +010093 if (readw(image) != 0xAA55) {
Bjorn Helgaas414ae762018-01-31 10:12:56 -060094 pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
95 readw(image));
John Kellerd7ad2252007-07-09 11:42:24 -070096 break;
Timothy S. Nelson97c44832009-01-30 06:12:47 +110097 }
Vladis Dronov4066df62015-11-27 18:20:06 +010098 /* get the PCI data structure and check its "PCIR" signature */
John Kellerd7ad2252007-07-09 11:42:24 -070099 pds = image + readw(image + 24);
Vladis Dronov4066df62015-11-27 18:20:06 +0100100 if (readl(pds) != 0x52494350) {
Bjorn Helgaas414ae762018-01-31 10:12:56 -0600101 pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
102 readl(pds));
John Kellerd7ad2252007-07-09 11:42:24 -0700103 break;
Vladis Dronov4066df62015-11-27 18:20:06 +0100104 }
John Kellerd7ad2252007-07-09 11:42:24 -0700105 last_image = readb(pds + 21) & 0x80;
Michel Dänzer16b036a2015-01-19 17:53:20 +0900106 length = readw(pds + 16);
107 image += length * 512;
Edward O'Callaghan47b975d2016-01-08 12:16:04 -0600108 /* Avoid iterating through memory outside the resource window */
Rex Zhu445ec322018-06-05 09:46:45 +0800109 if (image >= rom + size)
Edward O'Callaghan47b975d2016-01-08 12:16:04 -0600110 break;
Michel Dänzer16b036a2015-01-19 17:53:20 +0900111 } while (length && !last_image);
John Kellerd7ad2252007-07-09 11:42:24 -0700112
113 /* never return a size larger than the PCI resource window */
114 /* there are known ROMs that get the size wrong */
115 return min((size_t)(image - rom), size);
116}
117
118/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * pci_map_rom - map a PCI ROM to kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700120 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * @size: pointer to receive size of pci window over ROM
Randy Dunlapf5dafca2008-10-29 22:35:12 -0700122 *
123 * Return: kernel virtual pointer to image of ROM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
125 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
126 * the shadow BIOS copy will be returned instead of the
127 * actual ROM.
128 */
129void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
130{
131 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400132 loff_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 void __iomem *rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Bjorn Helgaasf50dd8c2016-03-01 11:13:30 -0600135 /* assign the ROM an address if it doesn't have one */
136 if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
137 return NULL;
138
139 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
140 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
141 if (*size == 0)
142 return NULL;
143
144 /* Enable ROM space decodes */
145 if (pci_enable_rom(pdev))
146 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 rom = ioremap(start, *size);
Changbin Dua48a6872017-11-07 18:22:26 -0600149 if (!rom)
150 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
153 * Try to find the true size of the ROM since sometimes the PCI window
154 * size is much larger than the actual size of the ROM.
155 * True size is important if the ROM is going to be copied.
156 */
Timothy S. Nelson97c44832009-01-30 06:12:47 +1100157 *size = pci_get_rom_size(pdev, rom, *size);
Changbin Dua405f192017-11-07 18:26:53 -0600158 if (!*size)
159 goto invalid_rom;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return rom;
Changbin Dua48a6872017-11-07 18:22:26 -0600162
Changbin Dua405f192017-11-07 18:26:53 -0600163invalid_rom:
164 iounmap(rom);
Changbin Dua48a6872017-11-07 18:22:26 -0600165err_ioremap:
166 /* restore enable if ioremap fails */
167 if (!(res->flags & IORESOURCE_ROM_ENABLE))
168 pci_disable_rom(pdev);
169 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600171EXPORT_SYMBOL(pci_map_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/**
174 * pci_unmap_rom - unmap the ROM from kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700175 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * @rom: virtual address of the previous mapping
177 *
178 * Remove a mapping of a previously mapped ROM
179 */
180void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
181{
182 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
183
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400184 iounmap(rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -0600186 /* Disable again before continuing */
187 if (!(res->flags & IORESOURCE_ROM_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 pci_disable_rom(pdev);
189}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600190EXPORT_SYMBOL(pci_unmap_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/**
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400193 * pci_platform_rom - provides a pointer to any ROM image provided by the
194 * platform
195 * @pdev: pointer to pci device struct
196 * @size: pointer to receive size of pci window over ROM
197 */
198void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
199{
200 if (pdev->rom && pdev->romlen) {
201 *size = pdev->romlen;
202 return phys_to_virt((phys_addr_t)pdev->rom);
203 }
204
205 return NULL;
206}
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400207EXPORT_SYMBOL(pci_platform_rom);