blob: 06663d391b393782de79260c8ae4addc46983535 [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
Yinghai Lufc279852013-12-09 22:54:40 -080038 pcibios_resource_to_bus(pdev->bus, &region, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100040 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
41 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -060045EXPORT_SYMBOL_GPL(pci_enable_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/**
48 * pci_disable_rom - disable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070049 * @pdev: PCI device to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 *
51 * Disable ROM decoding on a PCI device by turning off the last bit in the
52 * ROM BAR.
53 */
Alan Coxe416de52008-09-23 17:25:10 +010054void pci_disable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060056 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 u32 rom_addr;
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -060058
59 if (res->flags & IORESOURCE_ROM_SHADOW)
60 return;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
63 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
64 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
65}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -060066EXPORT_SYMBOL_GPL(pci_disable_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/**
John Kellerd7ad2252007-07-09 11:42:24 -070069 * pci_get_rom_size - obtain the actual size of the ROM image
Randy Dunlap4cc59c72009-02-09 09:31:20 -080070 * @pdev: target PCI device
John Kellerd7ad2252007-07-09 11:42:24 -070071 * @rom: kernel virtual pointer to image of ROM
72 * @size: size of PCI window
73 * return: size of actual ROM image
74 *
75 * Determine the actual length of the ROM image.
76 * The PCI window size could be much larger than the
77 * actual image size.
78 */
Timothy S. Nelson97c44832009-01-30 06:12:47 +110079size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
John Kellerd7ad2252007-07-09 11:42:24 -070080{
81 void __iomem *image;
82 int last_image;
Michel Dänzer16b036a2015-01-19 17:53:20 +090083 unsigned length;
John Kellerd7ad2252007-07-09 11:42:24 -070084
85 image = rom;
86 do {
87 void __iomem *pds;
88 /* Standard PCI ROMs start out with these bytes 55 AA */
Vladis Dronov4066df62015-11-27 18:20:06 +010089 if (readw(image) != 0xAA55) {
90 dev_err(&pdev->dev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
91 readw(image));
John Kellerd7ad2252007-07-09 11:42:24 -070092 break;
Timothy S. Nelson97c44832009-01-30 06:12:47 +110093 }
Vladis Dronov4066df62015-11-27 18:20:06 +010094 /* get the PCI data structure and check its "PCIR" signature */
John Kellerd7ad2252007-07-09 11:42:24 -070095 pds = image + readw(image + 24);
Vladis Dronov4066df62015-11-27 18:20:06 +010096 if (readl(pds) != 0x52494350) {
97 dev_err(&pdev->dev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
98 readl(pds));
John Kellerd7ad2252007-07-09 11:42:24 -070099 break;
Vladis Dronov4066df62015-11-27 18:20:06 +0100100 }
John Kellerd7ad2252007-07-09 11:42:24 -0700101 last_image = readb(pds + 21) & 0x80;
Michel Dänzer16b036a2015-01-19 17:53:20 +0900102 length = readw(pds + 16);
103 image += length * 512;
Edward O'Callaghan47b975d2016-01-08 12:16:04 -0600104 /* Avoid iterating through memory outside the resource window */
105 if (image > rom + size)
106 break;
Michel Dänzer16b036a2015-01-19 17:53:20 +0900107 } while (length && !last_image);
John Kellerd7ad2252007-07-09 11:42:24 -0700108
109 /* never return a size larger than the PCI resource window */
110 /* there are known ROMs that get the size wrong */
111 return min((size_t)(image - rom), size);
112}
113
114/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * pci_map_rom - map a PCI ROM to kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700116 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * @size: pointer to receive size of pci window over ROM
Randy Dunlapf5dafca2008-10-29 22:35:12 -0700118 *
119 * Return: kernel virtual pointer to image of ROM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
122 * the shadow BIOS copy will be returned instead of the
123 * actual ROM.
124 */
125void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
126{
127 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400128 loff_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 void __iomem *rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Bjorn Helgaasf50dd8c2016-03-01 11:13:30 -0600131 /* assign the ROM an address if it doesn't have one */
132 if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
133 return NULL;
134
135 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
136 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
137 if (*size == 0)
138 return NULL;
139
140 /* Enable ROM space decodes */
141 if (pci_enable_rom(pdev))
142 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 rom = ioremap(start, *size);
145 if (!rom) {
146 /* restore enable if ioremap fails */
Bjorn Helgaasd9c8bea2016-03-02 21:46:50 -0600147 if (!(res->flags & IORESOURCE_ROM_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 pci_disable_rom(pdev);
149 return NULL;
150 }
151
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return rom;
159}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600160EXPORT_SYMBOL(pci_map_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * pci_unmap_rom - unmap the ROM from kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700164 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * @rom: virtual address of the previous mapping
166 *
167 * Remove a mapping of a previously mapped ROM
168 */
169void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
170{
171 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
172
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400173 iounmap(rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bjorn Helgaas4708f9a2016-03-03 08:53:47 -0600175 /* Disable again before continuing */
176 if (!(res->flags & IORESOURCE_ROM_ENABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 pci_disable_rom(pdev);
178}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600179EXPORT_SYMBOL(pci_unmap_rom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400182 * pci_platform_rom - provides a pointer to any ROM image provided by the
183 * platform
184 * @pdev: pointer to pci device struct
185 * @size: pointer to receive size of pci window over ROM
186 */
187void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
188{
189 if (pdev->rom && pdev->romlen) {
190 *size = pdev->romlen;
191 return phys_to_virt((phys_addr_t)pdev->rom);
192 }
193
194 return NULL;
195}
Matthew Garrettfffe01f2013-03-26 17:25:54 -0400196EXPORT_SYMBOL(pci_platform_rom);