Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include "pci.h" |
| 14 | |
| 15 | /** |
| 16 | * pci_enable_rom - enable ROM decoding for a PCI device |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 17 | * @pdev: PCI device to enable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
| 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 | */ |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 24 | static int pci_enable_rom(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 26 | struct resource *res = pdev->resource + PCI_ROM_RESOURCE; |
| 27 | struct pci_bus_region region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | u32 rom_addr; |
| 29 | |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 30 | if (!res->flags) |
| 31 | return -1; |
| 32 | |
| 33 | pcibios_resource_to_bus(pdev, ®ion, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 35 | rom_addr &= ~PCI_ROM_ADDRESS_MASK; |
| 36 | rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 38 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * pci_disable_rom - disable ROM decoding for a PCI device |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 43 | * @pdev: PCI device to disable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * |
| 45 | * Disable ROM decoding on a PCI device by turning off the last bit in the |
| 46 | * ROM BAR. |
| 47 | */ |
| 48 | static void pci_disable_rom(struct pci_dev *pdev) |
| 49 | { |
| 50 | u32 rom_addr; |
| 51 | pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); |
| 52 | rom_addr &= ~PCI_ROM_ADDRESS_ENABLE; |
| 53 | pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * pci_map_rom - map a PCI ROM to kernel space |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 58 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * @size: pointer to receive size of pci window over ROM |
| 60 | * @return: kernel virtual pointer to image of ROM |
| 61 | * |
| 62 | * Map a PCI ROM into kernel space. If ROM is boot video ROM, |
| 63 | * the shadow BIOS copy will be returned instead of the |
| 64 | * actual ROM. |
| 65 | */ |
| 66 | void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size) |
| 67 | { |
| 68 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
| 69 | loff_t start; |
| 70 | void __iomem *rom; |
| 71 | void __iomem *image; |
| 72 | int last_image; |
| 73 | |
eiichiro.oiwa.nm@hitachi.com | b5e4efe | 2006-09-28 13:55:47 +0900 | [diff] [blame] | 74 | /* |
Eiichiro Oiwa | 6b5c76b | 2006-10-23 15:14:07 +0900 | [diff] [blame] | 75 | * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy |
| 76 | * memory map if the VGA enable bit of the Bridge Control register is |
| 77 | * set for embedded VGA. |
eiichiro.oiwa.nm@hitachi.com | b5e4efe | 2006-09-28 13:55:47 +0900 | [diff] [blame] | 78 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (res->flags & IORESOURCE_ROM_SHADOW) { |
| 80 | /* primary video rom always starts here */ |
| 81 | start = (loff_t)0xC0000; |
| 82 | *size = 0x20000; /* cover C000:0 through E000:0 */ |
| 83 | } else { |
John Keller | a2302c6 | 2006-10-04 16:49:52 -0500 | [diff] [blame^] | 84 | if (res->flags & |
| 85 | (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 87 | return (void __iomem *)(unsigned long) |
| 88 | pci_resource_start(pdev, PCI_ROM_RESOURCE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } else { |
| 90 | /* assign the ROM an address if it doesn't have one */ |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 91 | if (res->parent == NULL && |
| 92 | pci_assign_resource(pdev,PCI_ROM_RESOURCE)) |
| 93 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | start = pci_resource_start(pdev, PCI_ROM_RESOURCE); |
| 95 | *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
| 96 | if (*size == 0) |
| 97 | return NULL; |
| 98 | |
| 99 | /* Enable ROM space decodes */ |
Benjamin Herrenschmidt | 8085ce0 | 2005-08-31 14:16:53 +1000 | [diff] [blame] | 100 | if (pci_enable_rom(pdev)) |
| 101 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | rom = ioremap(start, *size); |
| 106 | if (!rom) { |
| 107 | /* restore enable if ioremap fails */ |
| 108 | if (!(res->flags & (IORESOURCE_ROM_ENABLE | |
| 109 | IORESOURCE_ROM_SHADOW | |
| 110 | IORESOURCE_ROM_COPY))) |
| 111 | pci_disable_rom(pdev); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Try to find the true size of the ROM since sometimes the PCI window |
| 117 | * size is much larger than the actual size of the ROM. |
| 118 | * True size is important if the ROM is going to be copied. |
| 119 | */ |
| 120 | image = rom; |
| 121 | do { |
| 122 | void __iomem *pds; |
| 123 | /* Standard PCI ROMs start out with these bytes 55 AA */ |
| 124 | if (readb(image) != 0x55) |
| 125 | break; |
| 126 | if (readb(image + 1) != 0xAA) |
| 127 | break; |
| 128 | /* get the PCI data structure and check its signature */ |
| 129 | pds = image + readw(image + 24); |
| 130 | if (readb(pds) != 'P') |
| 131 | break; |
| 132 | if (readb(pds + 1) != 'C') |
| 133 | break; |
| 134 | if (readb(pds + 2) != 'I') |
| 135 | break; |
| 136 | if (readb(pds + 3) != 'R') |
| 137 | break; |
| 138 | last_image = readb(pds + 21) & 0x80; |
| 139 | /* this length is reliable */ |
| 140 | image += readw(pds + 16) * 512; |
| 141 | } while (!last_image); |
| 142 | |
Jon Smirl | 761a3ac | 2005-07-29 12:16:17 -0700 | [diff] [blame] | 143 | /* never return a size larger than the PCI resource window */ |
| 144 | /* there are known ROMs that get the size wrong */ |
| 145 | *size = min((size_t)(image - rom), *size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | return rom; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 152 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * @size: pointer to receive size of pci window over ROM |
| 154 | * @return: kernel virtual pointer to image of ROM |
| 155 | * |
| 156 | * Map a PCI ROM into kernel space. If ROM is boot video ROM, |
| 157 | * the shadow BIOS copy will be returned instead of the |
| 158 | * actual ROM. |
| 159 | */ |
| 160 | void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size) |
| 161 | { |
| 162 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
| 163 | void __iomem *rom; |
| 164 | |
| 165 | rom = pci_map_rom(pdev, size); |
| 166 | if (!rom) |
| 167 | return NULL; |
| 168 | |
John Keller | a2302c6 | 2006-10-04 16:49:52 -0500 | [diff] [blame^] | 169 | if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW | |
| 170 | IORESOURCE_ROM_BIOS_COPY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return rom; |
| 172 | |
| 173 | res->start = (unsigned long)kmalloc(*size, GFP_KERNEL); |
| 174 | if (!res->start) |
| 175 | return rom; |
| 176 | |
| 177 | res->end = res->start + *size; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 178 | memcpy_fromio((void*)(unsigned long)res->start, rom, *size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | pci_unmap_rom(pdev, rom); |
| 180 | res->flags |= IORESOURCE_ROM_COPY; |
| 181 | |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 182 | return (void __iomem *)(unsigned long)res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * pci_unmap_rom - unmap the ROM from kernel space |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 187 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * @rom: virtual address of the previous mapping |
| 189 | * |
| 190 | * Remove a mapping of a previously mapped ROM |
| 191 | */ |
| 192 | void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom) |
| 193 | { |
| 194 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
| 195 | |
John Keller | a2302c6 | 2006-10-04 16:49:52 -0500 | [diff] [blame^] | 196 | if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return; |
| 198 | |
| 199 | iounmap(rom); |
| 200 | |
| 201 | /* Disable again before continuing, leave enabled if pci=rom */ |
| 202 | if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW))) |
| 203 | pci_disable_rom(pdev); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * pci_remove_rom - disable the ROM and remove its sysfs attribute |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 208 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * |
| 210 | * Remove the rom file in sysfs and disable ROM decoding. |
| 211 | */ |
| 212 | void pci_remove_rom(struct pci_dev *pdev) |
| 213 | { |
| 214 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
| 215 | |
| 216 | if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) |
| 217 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
| 218 | if (!(res->flags & (IORESOURCE_ROM_ENABLE | |
| 219 | IORESOURCE_ROM_SHADOW | |
John Keller | a2302c6 | 2006-10-04 16:49:52 -0500 | [diff] [blame^] | 220 | IORESOURCE_ROM_BIOS_COPY | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | IORESOURCE_ROM_COPY))) |
| 222 | pci_disable_rom(pdev); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * pci_cleanup_rom - internal routine for freeing the ROM copy created |
| 227 | * by pci_map_rom_copy called from remove.c |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 228 | * @pdev: pointer to pci device struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | * |
| 230 | * Free the copied ROM if we allocated one. |
| 231 | */ |
| 232 | void pci_cleanup_rom(struct pci_dev *pdev) |
| 233 | { |
| 234 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
| 235 | if (res->flags & IORESOURCE_ROM_COPY) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 236 | kfree((void*)(unsigned long)res->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | res->flags &= ~IORESOURCE_ROM_COPY; |
| 238 | res->start = 0; |
| 239 | res->end = 0; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | EXPORT_SYMBOL(pci_map_rom); |
| 244 | EXPORT_SYMBOL(pci_map_rom_copy); |
| 245 | EXPORT_SYMBOL(pci_unmap_rom); |
| 246 | EXPORT_SYMBOL(pci_remove_rom); |