rom.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. /**
  38. * pci_disable_rom - disable ROM decoding for a PCI device
  39. * @pdev: PCI device to disable
  40. *
  41. * Disable ROM decoding on a PCI device by turning off the last bit in the
  42. * ROM BAR.
  43. */
  44. void pci_disable_rom(struct pci_dev *pdev)
  45. {
  46. u32 rom_addr;
  47. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  48. rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  49. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  50. }
  51. /**
  52. * pci_get_rom_size - obtain the actual size of the ROM image
  53. * @pdev: target PCI device
  54. * @rom: kernel virtual pointer to image of ROM
  55. * @size: size of PCI window
  56. * return: size of actual ROM image
  57. *
  58. * Determine the actual length of the ROM image.
  59. * The PCI window size could be much larger than the
  60. * actual image size.
  61. */
  62. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  63. {
  64. void __iomem *image;
  65. int last_image;
  66. unsigned length;
  67. image = rom;
  68. do {
  69. void __iomem *pds;
  70. /* Standard PCI ROMs start out with these bytes 55 AA */
  71. if (readb(image) != 0x55) {
  72. dev_err(&pdev->dev, "Invalid ROM contents\n");
  73. break;
  74. }
  75. if (readb(image + 1) != 0xAA)
  76. break;
  77. /* get the PCI data structure and check its signature */
  78. pds = image + readw(image + 24);
  79. if (readb(pds) != 'P')
  80. break;
  81. if (readb(pds + 1) != 'C')
  82. break;
  83. if (readb(pds + 2) != 'I')
  84. break;
  85. if (readb(pds + 3) != 'R')
  86. break;
  87. last_image = readb(pds + 21) & 0x80;
  88. length = readw(pds + 16);
  89. image += length * 512;
  90. } while (length && !last_image);
  91. /* never return a size larger than the PCI resource window */
  92. /* there are known ROMs that get the size wrong */
  93. return min((size_t)(image - rom), size);
  94. }
  95. /**
  96. * pci_map_rom - map a PCI ROM to kernel space
  97. * @pdev: pointer to pci device struct
  98. * @size: pointer to receive size of pci window over ROM
  99. *
  100. * Return: kernel virtual pointer to image of ROM
  101. *
  102. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  103. * the shadow BIOS copy will be returned instead of the
  104. * actual ROM.
  105. */
  106. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  107. {
  108. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  109. loff_t start;
  110. void __iomem *rom;
  111. /*
  112. * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
  113. * memory map if the VGA enable bit of the Bridge Control register is
  114. * set for embedded VGA.
  115. */
  116. if (res->flags & IORESOURCE_ROM_SHADOW) {
  117. /* primary video rom always starts here */
  118. start = (loff_t)0xC0000;
  119. *size = 0x20000; /* cover C000:0 through E000:0 */
  120. } else {
  121. if (res->flags &
  122. (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  123. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  124. return (void __iomem *)(unsigned long)
  125. pci_resource_start(pdev, PCI_ROM_RESOURCE);
  126. } else {
  127. /* assign the ROM an address if it doesn't have one */
  128. if (res->parent == NULL &&
  129. pci_assign_resource(pdev,PCI_ROM_RESOURCE))
  130. return NULL;
  131. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  132. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  133. if (*size == 0)
  134. return NULL;
  135. /* Enable ROM space decodes */
  136. if (pci_enable_rom(pdev))
  137. return NULL;
  138. }
  139. }
  140. rom = ioremap(start, *size);
  141. if (!rom) {
  142. /* restore enable if ioremap fails */
  143. if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  144. IORESOURCE_ROM_SHADOW |
  145. IORESOURCE_ROM_COPY)))
  146. pci_disable_rom(pdev);
  147. return NULL;
  148. }
  149. /*
  150. * Try to find the true size of the ROM since sometimes the PCI window
  151. * size is much larger than the actual size of the ROM.
  152. * True size is important if the ROM is going to be copied.
  153. */
  154. *size = pci_get_rom_size(pdev, rom, *size);
  155. return rom;
  156. }
  157. #if 0
  158. /**
  159. * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
  160. * @pdev: pointer to pci device struct
  161. * @size: pointer to receive size of pci window over ROM
  162. *
  163. * Return: kernel virtual pointer to image of ROM
  164. *
  165. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  166. * the shadow BIOS copy will be returned instead of the
  167. * actual ROM.
  168. */
  169. void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
  170. {
  171. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  172. void __iomem *rom;
  173. rom = pci_map_rom(pdev, size);
  174. if (!rom)
  175. return NULL;
  176. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW |
  177. IORESOURCE_ROM_BIOS_COPY))
  178. return rom;
  179. res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
  180. if (!res->start)
  181. return rom;
  182. res->end = res->start + *size;
  183. memcpy_fromio((void*)(unsigned long)res->start, rom, *size);
  184. pci_unmap_rom(pdev, rom);
  185. res->flags |= IORESOURCE_ROM_COPY;
  186. return (void __iomem *)(unsigned long)res->start;
  187. }
  188. #endif /* 0 */
  189. /**
  190. * pci_unmap_rom - unmap the ROM from kernel space
  191. * @pdev: pointer to pci device struct
  192. * @rom: virtual address of the previous mapping
  193. *
  194. * Remove a mapping of a previously mapped ROM
  195. */
  196. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  197. {
  198. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  199. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  200. return;
  201. iounmap(rom);
  202. /* Disable again before continuing, leave enabled if pci=rom */
  203. if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  204. pci_disable_rom(pdev);
  205. }
  206. #if 0
  207. /**
  208. * pci_remove_rom - disable the ROM and remove its sysfs attribute
  209. * @pdev: pointer to pci device struct
  210. *
  211. * Remove the rom file in sysfs and disable ROM decoding.
  212. */
  213. void pci_remove_rom(struct pci_dev *pdev)
  214. {
  215. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  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 |
  220. IORESOURCE_ROM_BIOS_COPY |
  221. IORESOURCE_ROM_COPY)))
  222. pci_disable_rom(pdev);
  223. }
  224. #endif /* 0 */
  225. /**
  226. * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy
  227. * @pdev: pointer to pci device struct
  228. *
  229. * Free the copied ROM if we allocated one.
  230. */
  231. void pci_cleanup_rom(struct pci_dev *pdev)
  232. {
  233. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  234. if (res->flags & IORESOURCE_ROM_COPY) {
  235. kfree((void*)(unsigned long)res->start);
  236. res->flags &= ~IORESOURCE_ROM_COPY;
  237. res->start = 0;
  238. res->end = 0;
  239. }
  240. }
  241. EXPORT_SYMBOL(pci_map_rom);
  242. EXPORT_SYMBOL(pci_unmap_rom);
  243. EXPORT_SYMBOL_GPL(pci_enable_rom);
  244. EXPORT_SYMBOL_GPL(pci_disable_rom);