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