rom.c 7.2 KB

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