rom.c 7.2 KB

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