rom.c 5.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
B
Bjorn Helgaas 已提交
3
 * PCI ROM access routines
L
Linus Torvalds 已提交
4 5 6 7 8
 *
 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
 */
#include <linux/kernel.h>
9
#include <linux/export.h>
L
Linus Torvalds 已提交
10
#include <linux/pci.h>
11
#include <linux/slab.h>
L
Linus Torvalds 已提交
12 13 14 15 16

#include "pci.h"

/**
 * pci_enable_rom - enable ROM decoding for a PCI device
17
 * @pdev: PCI device to enable
L
Linus Torvalds 已提交
18 19 20 21 22 23
 *
 * Enable ROM decoding on @dev.  This involves simply turning on the last
 * bit of the PCI ROM BAR.  Note that some cards may share address decoders
 * between the ROM and other resources, so enabling it may disable access
 * to MMIO registers or other card memory.
 */
24
int pci_enable_rom(struct pci_dev *pdev)
L
Linus Torvalds 已提交
25
{
26
	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
27
	struct pci_bus_region region;
L
Linus Torvalds 已提交
28 29
	u32 rom_addr;

30 31 32
	if (!res->flags)
		return -1;

33 34 35 36
	/* Nothing to enable if we're using a shadow copy in RAM */
	if (res->flags & IORESOURCE_ROM_SHADOW)
		return 0;

37 38 39 40 41
	/*
	 * Ideally pci_update_resource() would update the ROM BAR address,
	 * and we would only set the enable bit here.  But apparently some
	 * devices have buggy ROM BARs that read as zero when disabled.
	 */
42
	pcibios_resource_to_bus(pdev->bus, &region, res);
L
Linus Torvalds 已提交
43
	pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
44 45
	rom_addr &= ~PCI_ROM_ADDRESS_MASK;
	rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
L
Linus Torvalds 已提交
46
	pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
47
	return 0;
L
Linus Torvalds 已提交
48
}
49
EXPORT_SYMBOL_GPL(pci_enable_rom);
L
Linus Torvalds 已提交
50 51 52

/**
 * pci_disable_rom - disable ROM decoding for a PCI device
53
 * @pdev: PCI device to disable
L
Linus Torvalds 已提交
54 55 56 57
 *
 * Disable ROM decoding on a PCI device by turning off the last bit in the
 * ROM BAR.
 */
58
void pci_disable_rom(struct pci_dev *pdev)
L
Linus Torvalds 已提交
59
{
60
	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
L
Linus Torvalds 已提交
61
	u32 rom_addr;
62 63 64 65

	if (res->flags & IORESOURCE_ROM_SHADOW)
		return;

L
Linus Torvalds 已提交
66 67 68 69
	pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
	rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
	pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
}
70
EXPORT_SYMBOL_GPL(pci_disable_rom);
L
Linus Torvalds 已提交
71

72 73
/**
 * pci_get_rom_size - obtain the actual size of the ROM image
74
 * @pdev: target PCI device
75 76 77 78 79 80 81 82
 * @rom: kernel virtual pointer to image of ROM
 * @size: size of PCI window
 *  return: size of actual ROM image
 *
 * Determine the actual length of the ROM image.
 * The PCI window size could be much larger than the
 * actual image size.
 */
83
size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
84 85 86
{
	void __iomem *image;
	int last_image;
87
	unsigned length;
88 89 90 91 92

	image = rom;
	do {
		void __iomem *pds;
		/* Standard PCI ROMs start out with these bytes 55 AA */
93
		if (readw(image) != 0xAA55) {
94 95
			pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
				 readw(image));
96
			break;
97
		}
98
		/* get the PCI data structure and check its "PCIR" signature */
99
		pds = image + readw(image + 24);
100
		if (readl(pds) != 0x52494350) {
101 102
			pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
				 readl(pds));
103
			break;
104
		}
105
		last_image = readb(pds + 21) & 0x80;
106 107
		length = readw(pds + 16);
		image += length * 512;
108 109 110
		/* Avoid iterating through memory outside the resource window */
		if (image > rom + size)
			break;
111
	} while (length && !last_image);
112 113 114 115 116 117

	/* never return a size larger than the PCI resource window */
	/* there are known ROMs that get the size wrong */
	return min((size_t)(image - rom), size);
}

L
Linus Torvalds 已提交
118 119
/**
 * pci_map_rom - map a PCI ROM to kernel space
120
 * @pdev: pointer to pci device struct
L
Linus Torvalds 已提交
121
 * @size: pointer to receive size of pci window over ROM
122 123
 *
 * Return: kernel virtual pointer to image of ROM
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131
 *
 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
 * the shadow BIOS copy will be returned instead of the
 * actual ROM.
 */
void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
{
	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
132
	loff_t start;
L
Linus Torvalds 已提交
133 134
	void __iomem *rom;

135 136 137 138 139 140 141 142 143 144 145 146
	/* assign the ROM an address if it doesn't have one */
	if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
		return NULL;

	start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
	*size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
	if (*size == 0)
		return NULL;

	/* Enable ROM space decodes */
	if (pci_enable_rom(pdev))
		return NULL;
147

L
Linus Torvalds 已提交
148
	rom = ioremap(start, *size);
149 150
	if (!rom)
		goto err_ioremap;
L
Linus Torvalds 已提交
151 152 153 154 155 156

	/*
	 * Try to find the true size of the ROM since sometimes the PCI window
	 * size is much larger than the actual size of the ROM.
	 * True size is important if the ROM is going to be copied.
	 */
157
	*size = pci_get_rom_size(pdev, rom, *size);
158 159 160
	if (!*size)
		goto invalid_rom;

L
Linus Torvalds 已提交
161
	return rom;
162

163 164
invalid_rom:
	iounmap(rom);
165 166 167 168 169
err_ioremap:
	/* restore enable if ioremap fails */
	if (!(res->flags & IORESOURCE_ROM_ENABLE))
		pci_disable_rom(pdev);
	return NULL;
L
Linus Torvalds 已提交
170
}
171
EXPORT_SYMBOL(pci_map_rom);
L
Linus Torvalds 已提交
172 173 174

/**
 * pci_unmap_rom - unmap the ROM from kernel space
175
 * @pdev: pointer to pci device struct
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183
 * @rom: virtual address of the previous mapping
 *
 * Remove a mapping of a previously mapped ROM
 */
void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
{
	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];

184
	iounmap(rom);
L
Linus Torvalds 已提交
185

186 187
	/* Disable again before continuing */
	if (!(res->flags & IORESOURCE_ROM_ENABLE))
L
Linus Torvalds 已提交
188 189
		pci_disable_rom(pdev);
}
190
EXPORT_SYMBOL(pci_unmap_rom);
L
Linus Torvalds 已提交
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/**
 * pci_platform_rom - provides a pointer to any ROM image provided by the
 * platform
 * @pdev: pointer to pci device struct
 * @size: pointer to receive size of pci window over ROM
 */
void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
{
	if (pdev->rom && pdev->romlen) {
		*size = pdev->romlen;
		return phys_to_virt((phys_addr_t)pdev->rom);
	}

	return NULL;
}
EXPORT_SYMBOL(pci_platform_rom);
新手
引导
客服 返回
顶部