blob: 137bf0cee897cf791f33885f2df5aece15464ee7 [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 */
Bjorn Helgaas783e8492018-06-29 21:17:26 -050083static size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom,
84 size_t size)
John Kellerd7ad2252007-07-09 11:42:24 -070085{
86 void __iomem *image;
87 int last_image;
Michel Dänzer16b036a2015-01-19 17:53:20 +090088 unsigned length;
John Kellerd7ad2252007-07-09 11:42:24 -070089
90 image = rom;
91 do {
92 void __iomem *pds;
93 /* Standard PCI ROMs start out with these bytes 55 AA */
Vladis Dronov4066df62015-11-27 18:20:06 +010094 if (readw(image) != 0xAA55) {
Bjorn Helgaas414ae762018-01-31 10:12:56 -060095 pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
96 readw(image));
John Kellerd7ad2252007-07-09 11:42:24 -070097 break;
Timothy S. Nelson97c44832009-01-30 06:12:47 +110098 }
Vladis Dronov4066df62015-11-27 18:20:06 +010099 /* get the PCI data structure and check its "PCIR" signature */
John Kellerd7ad2252007-07-09 11:42:24 -0700100 pds = image + readw(image + 24);
Vladis Dronov4066df62015-11-27 18:20:06 +0100101 if (readl(pds) != 0x52494350) {
Bjorn Helgaas414ae762018-01-31 10:12:56 -0600102 pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
103 readl(pds));
John Kellerd7ad2252007-07-09 11:42:24 -0700104 break;
Vladis Dronov4066df62015-11-27 18:20:06 +0100105 }
John Kellerd7ad2252007-07-09 11:42:24 -0700106 last_image = readb(pds + 21) & 0x80;
Michel Dänzer16b036a2015-01-19 17:53:20 +0900107 length = readw(pds + 16);
108 image += length * 512;
Edward O'Callaghan47b975d2016-01-08 12:16:04 -0600109 /* Avoid iterating through memory outside the resource window */
Rex Zhu445ec322018-06-05 09:46:45 +0800110 if (image >= rom + size)
Edward O'Callaghan47b975d2016-01-08 12:16:04 -0600111 break;
Rex Zhubeced882018-06-05 09:46:46 +0800112 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änzer16b036a2015-01-19 17:53:20 +0900118 } while (length && !last_image);
John Kellerd7ad2252007-07-09 11:42:24 -0700119
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 Torvalds1da177e2005-04-16 15:20:36 -0700126 * pci_map_rom - map a PCI ROM to kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700127 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * @size: pointer to receive size of pci window over ROM
Randy Dunlapf5dafca2008-10-29 22:35:12 -0700129 *
130 * Return: kernel virtual pointer to image of ROM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
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 */
136void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
137{
138 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400139 loff_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 void __iomem *rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Bjorn Helgaasf50dd8c2016-03-01 11:13:30 -0600142 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700154
155 rom = ioremap(start, *size);
Changbin Dua48a6872017-11-07 18:22:26 -0600156 if (!rom)
157 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
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. Nelson97c44832009-01-30 06:12:47 +1100164 *size = pci_get_rom_size(pdev, rom, *size);
Changbin Dua405f192017-11-07 18:26:53 -0600165 if (!*size)
166 goto invalid_rom;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return rom;
Changbin Dua48a6872017-11-07 18:22:26 -0600169
Changbin Dua405f192017-11-07 18:26:53 -0600170invalid_rom:
171 iounmap(rom);
Changbin Dua48a6872017-11-07 18:22:26 -0600172err_ioremap:
173 /* restore enable if ioremap fails */
174 if (!(res->flags & IORESOURCE_ROM_ENABLE))
175 pci_disable_rom(pdev);
176 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600178EXPORT_SYMBOL(pci_map_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/**
181 * pci_unmap_rom - unmap the ROM from kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700182 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @rom: virtual address of the previous mapping
184 *
185 * Remove a mapping of a previously mapped ROM
186 */
187void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
188{
189 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
190
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400191 iounmap(rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -0600193 /* Disable again before continuing */
194 if (!(res->flags & IORESOURCE_ROM_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 pci_disable_rom(pdev);
196}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600197EXPORT_SYMBOL(pci_unmap_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/**
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400200 * 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 */
205void __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 Garrettfffe01f2013-03-26 17:25:54 -0400214EXPORT_SYMBOL(pci_platform_rom);