blob: b6edb187d160c6dda3fb4bbccb5b0b85877ec4f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/rom.c
3 *
4 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
5 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
6 *
7 * PCI ROM access routines
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/pci.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "pci.h"
15
16/**
17 * pci_enable_rom - enable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070018 * @pdev: PCI device to enable
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * Enable ROM decoding on @dev. This involves simply turning on the last
21 * bit of the PCI ROM BAR. Note that some cards may share address decoders
22 * between the ROM and other resources, so enabling it may disable access
23 * to MMIO registers or other card memory.
24 */
Alan Coxe416de52008-09-23 17:25:10 +010025int pci_enable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060027 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100028 struct pci_bus_region region;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 u32 rom_addr;
30
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100031 if (!res->flags)
32 return -1;
33
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060034 /* Nothing to enable if we're using a shadow copy in RAM */
35 if (res->flags & IORESOURCE_ROM_SHADOW)
36 return 0;
37
Bjorn Helgaased09d212017-03-17 00:48:23 +000038 /*
39 * Ideally pci_update_resource() would update the ROM BAR address,
40 * and we would only set the enable bit here. But apparently some
41 * devices have buggy ROM BARs that read as zero when disabled.
42 */
Yinghai Lufc279852013-12-09 22:54:40 -080043 pcibios_resource_to_bus(pdev->bus, &region, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100045 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
46 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -060050EXPORT_SYMBOL_GPL(pci_enable_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/**
53 * pci_disable_rom - disable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070054 * @pdev: PCI device to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
56 * Disable ROM decoding on a PCI device by turning off the last bit in the
57 * ROM BAR.
58 */
Alan Coxe416de52008-09-23 17:25:10 +010059void pci_disable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060061 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 u32 rom_addr;
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060063
64 if (res->flags & IORESOURCE_ROM_SHADOW)
65 return;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
68 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
69 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
70}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -060071EXPORT_SYMBOL_GPL(pci_disable_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/**
John Kellerd7ad2252007-07-09 11:42:24 -070074 * pci_get_rom_size - obtain the actual size of the ROM image
Randy Dunlap4cc59c72009-02-09 09:31:20 -080075 * @pdev: target PCI device
John Kellerd7ad2252007-07-09 11:42:24 -070076 * @rom: kernel virtual pointer to image of ROM
77 * @size: size of PCI window
78 * return: size of actual ROM image
79 *
80 * Determine the actual length of the ROM image.
81 * The PCI window size could be much larger than the
82 * actual image size.
83 */
Timothy S. Nelson97c44832009-01-30 06:12:47 +110084size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, 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) {
95 dev_err(&pdev->dev, "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) {
102 dev_err(&pdev->dev, "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 */
110 if (image > rom + size)
111 break;
Michel Dänzer16b036a2015-01-19 17:53:20 +0900112 } while (length && !last_image);
John Kellerd7ad2252007-07-09 11:42:24 -0700113
114 /* never return a size larger than the PCI resource window */
115 /* there are known ROMs that get the size wrong */
116 return min((size_t)(image - rom), size);
117}
118
119/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * pci_map_rom - map a PCI ROM to kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700121 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * @size: pointer to receive size of pci window over ROM
Randy Dunlapf5dafca2008-10-29 22:35:12 -0700123 *
124 * Return: kernel virtual pointer to image of ROM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 *
126 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
127 * the shadow BIOS copy will be returned instead of the
128 * actual ROM.
129 */
130void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
131{
132 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400133 loff_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 void __iomem *rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Bjorn Helgaasf50dd8c2016-03-01 11:13:30 -0600136 /* assign the ROM an address if it doesn't have one */
137 if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
138 return NULL;
139
140 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
141 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
142 if (*size == 0)
143 return NULL;
144
145 /* Enable ROM space decodes */
146 if (pci_enable_rom(pdev))
147 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 rom = ioremap(start, *size);
150 if (!rom) {
151 /* restore enable if ioremap fails */
Bjorn Helgaasd9c8bea2016-03-02 21:46:50 -0600152 if (!(res->flags & IORESOURCE_ROM_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 pci_disable_rom(pdev);
154 return NULL;
155 }
156
157 /*
158 * Try to find the true size of the ROM since sometimes the PCI window
159 * size is much larger than the actual size of the ROM.
160 * True size is important if the ROM is going to be copied.
161 */
Timothy S. Nelson97c44832009-01-30 06:12:47 +1100162 *size = pci_get_rom_size(pdev, rom, *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return rom;
164}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600165EXPORT_SYMBOL(pci_map_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/**
168 * pci_unmap_rom - unmap the ROM from kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700169 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * @rom: virtual address of the previous mapping
171 *
172 * Remove a mapping of a previously mapped ROM
173 */
174void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
175{
176 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
177
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400178 iounmap(rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -0600180 /* Disable again before continuing */
181 if (!(res->flags & IORESOURCE_ROM_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 pci_disable_rom(pdev);
183}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600184EXPORT_SYMBOL(pci_unmap_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/**
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400187 * pci_platform_rom - provides a pointer to any ROM image provided by the
188 * platform
189 * @pdev: pointer to pci device struct
190 * @size: pointer to receive size of pci window over ROM
191 */
192void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
193{
194 if (pdev->rom && pdev->romlen) {
195 *size = pdev->romlen;
196 return phys_to_virt((phys_addr_t)pdev->rom);
197 }
198
199 return NULL;
200}
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400201EXPORT_SYMBOL(pci_platform_rom);