rom.c 7.1 KB

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