xref: /linux/drivers/pci/rom.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
17328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3df62ab5eSBjorn Helgaas  * PCI ROM access routines
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
61da177e4SLinus Torvalds  * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds #include <linux/kernel.h>
9363c75dbSPaul Gortmaker #include <linux/export.h>
101da177e4SLinus Torvalds #include <linux/pci.h>
114e57b681STim Schmielau #include <linux/slab.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include "pci.h"
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /**
161da177e4SLinus Torvalds  * pci_enable_rom - enable ROM decoding for a PCI device
1767be2dd1SMartin Waitz  * @pdev: PCI device to enable
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Enable ROM decoding on @dev.  This involves simply turning on the last
201da177e4SLinus Torvalds  * bit of the PCI ROM BAR.  Note that some cards may share address decoders
211da177e4SLinus Torvalds  * between the ROM and other resources, so enabling it may disable access
221da177e4SLinus Torvalds  * to MMIO registers or other card memory.
231da177e4SLinus Torvalds  */
pci_enable_rom(struct pci_dev * pdev)24e416de5eSAlan Cox int pci_enable_rom(struct pci_dev *pdev)
251da177e4SLinus Torvalds {
264708f9a5SBjorn Helgaas 	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
278085ce08SBenjamin Herrenschmidt 	struct pci_bus_region region;
281da177e4SLinus Torvalds 	u32 rom_addr;
291da177e4SLinus Torvalds 
308085ce08SBenjamin Herrenschmidt 	if (!res->flags)
318085ce08SBenjamin Herrenschmidt 		return -1;
328085ce08SBenjamin Herrenschmidt 
334708f9a5SBjorn Helgaas 	/* Nothing to enable if we're using a shadow copy in RAM */
344708f9a5SBjorn Helgaas 	if (res->flags & IORESOURCE_ROM_SHADOW)
354708f9a5SBjorn Helgaas 		return 0;
364708f9a5SBjorn Helgaas 
370b457ddeSBjorn Helgaas 	/*
380b457ddeSBjorn Helgaas 	 * Ideally pci_update_resource() would update the ROM BAR address,
390b457ddeSBjorn Helgaas 	 * and we would only set the enable bit here.  But apparently some
400b457ddeSBjorn Helgaas 	 * devices have buggy ROM BARs that read as zero when disabled.
410b457ddeSBjorn Helgaas 	 */
42fc279850SYinghai Lu 	pcibios_resource_to_bus(pdev->bus, &region, res);
431da177e4SLinus Torvalds 	pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
448085ce08SBenjamin Herrenschmidt 	rom_addr &= ~PCI_ROM_ADDRESS_MASK;
458085ce08SBenjamin Herrenschmidt 	rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
461da177e4SLinus Torvalds 	pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
478085ce08SBenjamin Herrenschmidt 	return 0;
481da177e4SLinus Torvalds }
49b7fe9434SRyan Desfosses EXPORT_SYMBOL_GPL(pci_enable_rom);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /**
521da177e4SLinus Torvalds  * pci_disable_rom - disable ROM decoding for a PCI device
5367be2dd1SMartin Waitz  * @pdev: PCI device to disable
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * Disable ROM decoding on a PCI device by turning off the last bit in the
561da177e4SLinus Torvalds  * ROM BAR.
571da177e4SLinus Torvalds  */
pci_disable_rom(struct pci_dev * pdev)58e416de5eSAlan Cox void pci_disable_rom(struct pci_dev *pdev)
591da177e4SLinus Torvalds {
604708f9a5SBjorn Helgaas 	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
611da177e4SLinus Torvalds 	u32 rom_addr;
624708f9a5SBjorn Helgaas 
634708f9a5SBjorn Helgaas 	if (res->flags & IORESOURCE_ROM_SHADOW)
644708f9a5SBjorn Helgaas 		return;
654708f9a5SBjorn Helgaas 
661da177e4SLinus Torvalds 	pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
671da177e4SLinus Torvalds 	rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
681da177e4SLinus Torvalds 	pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
691da177e4SLinus Torvalds }
70b7fe9434SRyan Desfosses EXPORT_SYMBOL_GPL(pci_disable_rom);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /**
73d7ad2254SJohn Keller  * pci_get_rom_size - obtain the actual size of the ROM image
744cc59c72SRandy Dunlap  * @pdev: target PCI device
75d7ad2254SJohn Keller  * @rom: kernel virtual pointer to image of ROM
76d7ad2254SJohn Keller  * @size: size of PCI window
77d7ad2254SJohn Keller  *  return: size of actual ROM image
78d7ad2254SJohn Keller  *
79d7ad2254SJohn Keller  * Determine the actual length of the ROM image.
80d7ad2254SJohn Keller  * The PCI window size could be much larger than the
81d7ad2254SJohn Keller  * actual image size.
82d7ad2254SJohn Keller  */
pci_get_rom_size(struct pci_dev * pdev,void __iomem * rom,size_t size)83783e8496SBjorn Helgaas static size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom,
84783e8496SBjorn Helgaas 			       size_t size)
85d7ad2254SJohn Keller {
86d7ad2254SJohn Keller 	void __iomem *image;
87d7ad2254SJohn Keller 	int last_image;
88*fd1ae23bSKrzysztof Wilczyński 	unsigned int length;
89d7ad2254SJohn Keller 
90d7ad2254SJohn Keller 	image = rom;
91d7ad2254SJohn Keller 	do {
92d7ad2254SJohn Keller 		void __iomem *pds;
93d7ad2254SJohn Keller 		/* Standard PCI ROMs start out with these bytes 55 AA */
944066df63SVladis Dronov 		if (readw(image) != 0xAA55) {
95414ae760SBjorn Helgaas 			pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
964066df63SVladis Dronov 				 readw(image));
97d7ad2254SJohn Keller 			break;
9897c44836STimothy S. Nelson 		}
994066df63SVladis Dronov 		/* get the PCI data structure and check its "PCIR" signature */
100d7ad2254SJohn Keller 		pds = image + readw(image + 24);
1014066df63SVladis Dronov 		if (readl(pds) != 0x52494350) {
102414ae760SBjorn Helgaas 			pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
1034066df63SVladis Dronov 				 readl(pds));
104d7ad2254SJohn Keller 			break;
1054066df63SVladis Dronov 		}
106d7ad2254SJohn Keller 		last_image = readb(pds + 21) & 0x80;
10716b036afSMichel Dänzer 		length = readw(pds + 16);
10816b036afSMichel Dänzer 		image += length * 512;
10947b975d2SEdward O'Callaghan 		/* Avoid iterating through memory outside the resource window */
110445ec321SRex Zhu 		if (image >= rom + size)
11147b975d2SEdward O'Callaghan 			break;
112beced88eSRex Zhu 		if (!last_image) {
113beced88eSRex Zhu 			if (readw(image) != 0xAA55) {
114beced88eSRex Zhu 				pci_info(pdev, "No more image in the PCI ROM\n");
115beced88eSRex Zhu 				break;
116beced88eSRex Zhu 			}
117beced88eSRex Zhu 		}
11816b036afSMichel Dänzer 	} while (length && !last_image);
119d7ad2254SJohn Keller 
120d7ad2254SJohn Keller 	/* never return a size larger than the PCI resource window */
121d7ad2254SJohn Keller 	/* there are known ROMs that get the size wrong */
122d7ad2254SJohn Keller 	return min((size_t)(image - rom), size);
123d7ad2254SJohn Keller }
124d7ad2254SJohn Keller 
125d7ad2254SJohn Keller /**
1261da177e4SLinus Torvalds  * pci_map_rom - map a PCI ROM to kernel space
12767be2dd1SMartin Waitz  * @pdev: pointer to pci device struct
1281da177e4SLinus Torvalds  * @size: pointer to receive size of pci window over ROM
129f5dafca5SRandy Dunlap  *
130f5dafca5SRandy Dunlap  * Return: kernel virtual pointer to image of ROM
1311da177e4SLinus Torvalds  *
1321da177e4SLinus Torvalds  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
1331da177e4SLinus Torvalds  * the shadow BIOS copy will be returned instead of the
1341da177e4SLinus Torvalds  * actual ROM.
1351da177e4SLinus Torvalds  */
pci_map_rom(struct pci_dev * pdev,size_t * size)1361da177e4SLinus Torvalds void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
139fffe01f7SMatthew Garrett 	loff_t start;
1401da177e4SLinus Torvalds 	void __iomem *rom;
1411da177e4SLinus Torvalds 
142fffe01f7SMatthew Garrett 	/* assign the ROM an address if it doesn't have one */
143f50dd8c3SBjorn Helgaas 	if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
1448085ce08SBenjamin Herrenschmidt 		return NULL;
145f50dd8c3SBjorn Helgaas 
146fffe01f7SMatthew Garrett 	start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
147fffe01f7SMatthew Garrett 	*size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
148fffe01f7SMatthew Garrett 	if (*size == 0)
149fffe01f7SMatthew Garrett 		return NULL;
150fffe01f7SMatthew Garrett 
151fffe01f7SMatthew Garrett 	/* Enable ROM space decodes */
152fffe01f7SMatthew Garrett 	if (pci_enable_rom(pdev))
153fffe01f7SMatthew Garrett 		return NULL;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	rom = ioremap(start, *size);
156a48a687dSChangbin Du 	if (!rom)
157a48a687dSChangbin Du 		goto err_ioremap;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	/*
1601da177e4SLinus Torvalds 	 * Try to find the true size of the ROM since sometimes the PCI window
1611da177e4SLinus Torvalds 	 * size is much larger than the actual size of the ROM.
1621da177e4SLinus Torvalds 	 * True size is important if the ROM is going to be copied.
1631da177e4SLinus Torvalds 	 */
16497c44836STimothy S. Nelson 	*size = pci_get_rom_size(pdev, rom, *size);
165a405f191SChangbin Du 	if (!*size)
166a405f191SChangbin Du 		goto invalid_rom;
167a405f191SChangbin Du 
1681da177e4SLinus Torvalds 	return rom;
169a48a687dSChangbin Du 
170a405f191SChangbin Du invalid_rom:
171a405f191SChangbin Du 	iounmap(rom);
172a48a687dSChangbin Du err_ioremap:
173a48a687dSChangbin Du 	/* restore enable if ioremap fails */
174a48a687dSChangbin Du 	if (!(res->flags & IORESOURCE_ROM_ENABLE))
175a48a687dSChangbin Du 		pci_disable_rom(pdev);
176a48a687dSChangbin Du 	return NULL;
1771da177e4SLinus Torvalds }
178b7fe9434SRyan Desfosses EXPORT_SYMBOL(pci_map_rom);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /**
1811da177e4SLinus Torvalds  * pci_unmap_rom - unmap the ROM from kernel space
18267be2dd1SMartin Waitz  * @pdev: pointer to pci device struct
1831da177e4SLinus Torvalds  * @rom: virtual address of the previous mapping
1841da177e4SLinus Torvalds  *
1851da177e4SLinus Torvalds  * Remove a mapping of a previously mapped ROM
1861da177e4SLinus Torvalds  */
pci_unmap_rom(struct pci_dev * pdev,void __iomem * rom)1871da177e4SLinus Torvalds void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	iounmap(rom);
1921da177e4SLinus Torvalds 
1934708f9a5SBjorn Helgaas 	/* Disable again before continuing */
1944708f9a5SBjorn Helgaas 	if (!(res->flags & IORESOURCE_ROM_ENABLE))
1951da177e4SLinus Torvalds 		pci_disable_rom(pdev);
1961da177e4SLinus Torvalds }
197b7fe9434SRyan Desfosses EXPORT_SYMBOL(pci_unmap_rom);
198