rom.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  75. {
  76. void __iomem *image;
  77. int last_image;
  78. unsigned length;
  79. image = rom;
  80. do {
  81. void __iomem *pds;
  82. /* Standard PCI ROMs start out with these bytes 55 AA */
  83. if (readw(image) != 0xAA55) {
  84. pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
  85. readw(image));
  86. break;
  87. }
  88. /* get the PCI data structure and check its "PCIR" signature */
  89. pds = image + readw(image + 24);
  90. if (readl(pds) != 0x52494350) {
  91. pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
  92. readl(pds));
  93. break;
  94. }
  95. last_image = readb(pds + 21) & 0x80;
  96. length = readw(pds + 16);
  97. image += length * 512;
  98. /* Avoid iterating through memory outside the resource window */
  99. if (image > rom + size)
  100. break;
  101. } while (length && !last_image);
  102. /* never return a size larger than the PCI resource window */
  103. /* there are known ROMs that get the size wrong */
  104. return min((size_t)(image - rom), size);
  105. }
  106. /**
  107. * pci_map_rom - map a PCI ROM to kernel space
  108. * @pdev: pointer to pci device struct
  109. * @size: pointer to receive size of pci window over ROM
  110. *
  111. * Return: kernel virtual pointer to image of ROM
  112. *
  113. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  114. * the shadow BIOS copy will be returned instead of the
  115. * actual ROM.
  116. */
  117. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  118. {
  119. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  120. loff_t start;
  121. void __iomem *rom;
  122. /* assign the ROM an address if it doesn't have one */
  123. if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
  124. return NULL;
  125. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  126. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  127. if (*size == 0)
  128. return NULL;
  129. /* Enable ROM space decodes */
  130. if (pci_enable_rom(pdev))
  131. return NULL;
  132. rom = ioremap(start, *size);
  133. if (!rom)
  134. goto err_ioremap;
  135. /*
  136. * Try to find the true size of the ROM since sometimes the PCI window
  137. * size is much larger than the actual size of the ROM.
  138. * True size is important if the ROM is going to be copied.
  139. */
  140. *size = pci_get_rom_size(pdev, rom, *size);
  141. if (!*size)
  142. goto invalid_rom;
  143. return rom;
  144. invalid_rom:
  145. iounmap(rom);
  146. err_ioremap:
  147. /* restore enable if ioremap fails */
  148. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  149. pci_disable_rom(pdev);
  150. return NULL;
  151. }
  152. EXPORT_SYMBOL(pci_map_rom);
  153. /**
  154. * pci_unmap_rom - unmap the ROM from kernel space
  155. * @pdev: pointer to pci device struct
  156. * @rom: virtual address of the previous mapping
  157. *
  158. * Remove a mapping of a previously mapped ROM
  159. */
  160. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  161. {
  162. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  163. iounmap(rom);
  164. /* Disable again before continuing */
  165. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  166. pci_disable_rom(pdev);
  167. }
  168. EXPORT_SYMBOL(pci_unmap_rom);
  169. /**
  170. * pci_platform_rom - provides a pointer to any ROM image provided by the
  171. * platform
  172. * @pdev: pointer to pci device struct
  173. * @size: pointer to receive size of pci window over ROM
  174. */
  175. void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
  176. {
  177. if (pdev->rom && pdev->romlen) {
  178. *size = pdev->romlen;
  179. return phys_to_virt((phys_addr_t)pdev->rom);
  180. }
  181. return NULL;
  182. }
  183. EXPORT_SYMBOL(pci_platform_rom);