rom.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI ROM access routines
  4. *
  5. * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
  6. * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/pci.h>
  11. #include <linux/slab.h>
  12. #include "pci.h"
  13. /**
  14. * pci_enable_rom - enable ROM decoding for a PCI device
  15. * @pdev: PCI device to enable
  16. *
  17. * Enable ROM decoding on @dev. This involves simply turning on the last
  18. * bit of the PCI ROM BAR. Note that some cards may share address decoders
  19. * between the ROM and other resources, so enabling it may disable access
  20. * to MMIO registers or other card memory.
  21. */
  22. int pci_enable_rom(struct pci_dev *pdev)
  23. {
  24. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  25. struct pci_bus_region region;
  26. u32 rom_addr;
  27. if (!res->flags)
  28. return -1;
  29. /* Nothing to enable if we're using a shadow copy in RAM */
  30. if (res->flags & IORESOURCE_ROM_SHADOW)
  31. return 0;
  32. /*
  33. * Ideally pci_update_resource() would update the ROM BAR address,
  34. * and we would only set the enable bit here. But apparently some
  35. * devices have buggy ROM BARs that read as zero when disabled.
  36. */
  37. pcibios_resource_to_bus(pdev->bus, &region, res);
  38. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  39. rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  40. rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
  41. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_GPL(pci_enable_rom);
  45. /**
  46. * pci_disable_rom - disable ROM decoding for a PCI device
  47. * @pdev: PCI device to disable
  48. *
  49. * Disable ROM decoding on a PCI device by turning off the last bit in the
  50. * ROM BAR.
  51. */
  52. void pci_disable_rom(struct pci_dev *pdev)
  53. {
  54. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  55. u32 rom_addr;
  56. if (res->flags & IORESOURCE_ROM_SHADOW)
  57. return;
  58. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  59. rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  60. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  61. }
  62. EXPORT_SYMBOL_GPL(pci_disable_rom);
  63. /**
  64. * pci_get_rom_size - obtain the actual size of the ROM image
  65. * @pdev: target PCI device
  66. * @rom: kernel virtual pointer to image of ROM
  67. * @size: size of PCI window
  68. * return: size of actual ROM image
  69. *
  70. * Determine the actual length of the ROM image.
  71. * The PCI window size could be much larger than the
  72. * actual image size.
  73. */
  74. static size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom,
  75. size_t size)
  76. {
  77. void __iomem *image;
  78. int last_image;
  79. unsigned length;
  80. image = rom;
  81. do {
  82. void __iomem *pds;
  83. /* Standard PCI ROMs start out with these bytes 55 AA */
  84. if (readw(image) != 0xAA55) {
  85. pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
  86. readw(image));
  87. break;
  88. }
  89. /* get the PCI data structure and check its "PCIR" signature */
  90. pds = image + readw(image + 24);
  91. if (readl(pds) != 0x52494350) {
  92. pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
  93. readl(pds));
  94. break;
  95. }
  96. last_image = readb(pds + 21) & 0x80;
  97. length = readw(pds + 16);
  98. image += length * 512;
  99. /* Avoid iterating through memory outside the resource window */
  100. if (image >= rom + size)
  101. break;
  102. if (!last_image) {
  103. if (readw(image) != 0xAA55) {
  104. pci_info(pdev, "No more image in the PCI ROM\n");
  105. break;
  106. }
  107. }
  108. } while (length && !last_image);
  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. * pci_map_rom - map a PCI ROM to kernel space
  115. * @pdev: pointer to pci device struct
  116. * @size: pointer to receive size of pci window over ROM
  117. *
  118. * Return: kernel virtual pointer to image of ROM
  119. *
  120. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  121. * the shadow BIOS copy will be returned instead of the
  122. * actual ROM.
  123. */
  124. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  125. {
  126. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  127. loff_t start;
  128. void __iomem *rom;
  129. /* assign the ROM an address if it doesn't have one */
  130. if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
  131. return NULL;
  132. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  133. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  134. if (*size == 0)
  135. return NULL;
  136. /* Enable ROM space decodes */
  137. if (pci_enable_rom(pdev))
  138. return NULL;
  139. rom = ioremap(start, *size);
  140. if (!rom)
  141. goto err_ioremap;
  142. /*
  143. * Try to find the true size of the ROM since sometimes the PCI window
  144. * size is much larger than the actual size of the ROM.
  145. * True size is important if the ROM is going to be copied.
  146. */
  147. *size = pci_get_rom_size(pdev, rom, *size);
  148. if (!*size)
  149. goto invalid_rom;
  150. return rom;
  151. invalid_rom:
  152. iounmap(rom);
  153. err_ioremap:
  154. /* restore enable if ioremap fails */
  155. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  156. pci_disable_rom(pdev);
  157. return NULL;
  158. }
  159. EXPORT_SYMBOL(pci_map_rom);
  160. /**
  161. * pci_unmap_rom - unmap the ROM from kernel space
  162. * @pdev: pointer to pci device struct
  163. * @rom: virtual address of the previous mapping
  164. *
  165. * Remove a mapping of a previously mapped ROM
  166. */
  167. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  168. {
  169. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  170. iounmap(rom);
  171. /* Disable again before continuing */
  172. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  173. pci_disable_rom(pdev);
  174. }
  175. EXPORT_SYMBOL(pci_unmap_rom);