rom.c 7.1 KB

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