rom.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. */
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include "pci.h"
  14. /**
  15. * pci_enable_rom - enable ROM decoding for a PCI device
  16. * @pdev: PCI device to enable
  17. *
  18. * Enable ROM decoding on @dev. This involves simply turning on the last
  19. * bit of the PCI ROM BAR. Note that some cards may share address decoders
  20. * between the ROM and other resources, so enabling it may disable access
  21. * to MMIO registers or other card memory.
  22. */
  23. int pci_enable_rom(struct pci_dev *pdev)
  24. {
  25. struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
  26. struct pci_bus_region region;
  27. u32 rom_addr;
  28. if (!res->flags)
  29. return -1;
  30. pcibios_resource_to_bus(pdev->bus, &region, res);
  31. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  32. rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  33. rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
  34. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  35. return 0;
  36. }
  37. EXPORT_SYMBOL_GPL(pci_enable_rom);
  38. /**
  39. * pci_disable_rom - disable ROM decoding for a PCI device
  40. * @pdev: PCI device to disable
  41. *
  42. * Disable ROM decoding on a PCI device by turning off the last bit in the
  43. * ROM BAR.
  44. */
  45. void pci_disable_rom(struct pci_dev *pdev)
  46. {
  47. u32 rom_addr;
  48. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  49. rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  50. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  51. }
  52. EXPORT_SYMBOL_GPL(pci_disable_rom);
  53. /**
  54. * pci_get_rom_size - obtain the actual size of the ROM image
  55. * @pdev: target PCI device
  56. * @rom: kernel virtual pointer to image of ROM
  57. * @size: size of PCI window
  58. * return: size of actual ROM image
  59. *
  60. * Determine the actual length of the ROM image.
  61. * The PCI window size could be much larger than the
  62. * actual image size.
  63. */
  64. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  65. {
  66. void __iomem *image;
  67. int last_image;
  68. unsigned length;
  69. image = rom;
  70. do {
  71. void __iomem *pds;
  72. /* Standard PCI ROMs start out with these bytes 55 AA */
  73. if (readb(image) != 0x55) {
  74. dev_err(&pdev->dev, "Invalid ROM contents\n");
  75. break;
  76. }
  77. if (readb(image + 1) != 0xAA)
  78. break;
  79. /* get the PCI data structure and check its signature */
  80. pds = image + readw(image + 24);
  81. if (readb(pds) != 'P')
  82. break;
  83. if (readb(pds + 1) != 'C')
  84. break;
  85. if (readb(pds + 2) != 'I')
  86. break;
  87. if (readb(pds + 3) != 'R')
  88. break;
  89. last_image = readb(pds + 21) & 0x80;
  90. length = readw(pds + 16);
  91. image += length * 512;
  92. } while (length && !last_image);
  93. /* never return a size larger than the PCI resource window */
  94. /* there are known ROMs that get the size wrong */
  95. return min((size_t)(image - rom), size);
  96. }
  97. /**
  98. * pci_map_rom - map a PCI ROM to kernel space
  99. * @pdev: pointer to pci device struct
  100. * @size: pointer to receive size of pci window over ROM
  101. *
  102. * Return: kernel virtual pointer to image of ROM
  103. *
  104. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  105. * the shadow BIOS copy will be returned instead of the
  106. * actual ROM.
  107. */
  108. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  109. {
  110. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  111. loff_t start;
  112. void __iomem *rom;
  113. /*
  114. * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
  115. * memory map if the VGA enable bit of the Bridge Control register is
  116. * set for embedded VGA.
  117. */
  118. if (res->flags & IORESOURCE_ROM_SHADOW) {
  119. /* primary video rom always starts here */
  120. start = (loff_t)0xC0000;
  121. *size = 0x20000; /* cover C000:0 through E000:0 */
  122. } else {
  123. if (res->flags &
  124. (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  125. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  126. return (void __iomem *)(unsigned long)
  127. pci_resource_start(pdev, PCI_ROM_RESOURCE);
  128. } else {
  129. /* assign the ROM an address if it doesn't have one */
  130. if (res->parent == NULL &&
  131. pci_assign_resource(pdev, PCI_ROM_RESOURCE))
  132. return NULL;
  133. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  134. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  135. if (*size == 0)
  136. return NULL;
  137. /* Enable ROM space decodes */
  138. if (pci_enable_rom(pdev))
  139. return NULL;
  140. }
  141. }
  142. rom = ioremap(start, *size);
  143. if (!rom) {
  144. /* restore enable if ioremap fails */
  145. if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  146. IORESOURCE_ROM_SHADOW |
  147. IORESOURCE_ROM_COPY)))
  148. pci_disable_rom(pdev);
  149. return NULL;
  150. }
  151. /*
  152. * Try to find the true size of the ROM since sometimes the PCI window
  153. * size is much larger than the actual size of the ROM.
  154. * True size is important if the ROM is going to be copied.
  155. */
  156. *size = pci_get_rom_size(pdev, rom, *size);
  157. return rom;
  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. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  171. return;
  172. iounmap(rom);
  173. /* Disable again before continuing, leave enabled if pci=rom */
  174. if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  175. pci_disable_rom(pdev);
  176. }
  177. EXPORT_SYMBOL(pci_unmap_rom);
  178. /**
  179. * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy
  180. * @pdev: pointer to pci device struct
  181. *
  182. * Free the copied ROM if we allocated one.
  183. */
  184. void pci_cleanup_rom(struct pci_dev *pdev)
  185. {
  186. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  187. if (res->flags & IORESOURCE_ROM_COPY) {
  188. kfree((void *)(unsigned long)res->start);
  189. res->flags |= IORESOURCE_UNSET;
  190. res->flags &= ~IORESOURCE_ROM_COPY;
  191. res->start = 0;
  192. res->end = 0;
  193. }
  194. }
  195. /**
  196. * pci_platform_rom - provides a pointer to any ROM image provided by the
  197. * platform
  198. * @pdev: pointer to pci device struct
  199. * @size: pointer to receive size of pci window over ROM
  200. */
  201. void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
  202. {
  203. if (pdev->rom && pdev->romlen) {
  204. *size = pdev->romlen;
  205. return phys_to_virt((phys_addr_t)pdev->rom);
  206. }
  207. return NULL;
  208. }
  209. EXPORT_SYMBOL(pci_platform_rom);