rom.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /**
  152. * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
  153. * @pdev: pointer to pci device struct
  154. * @size: pointer to receive size of pci window over ROM
  155. * @return: kernel virtual pointer to image of ROM
  156. *
  157. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  158. * the shadow BIOS copy will be returned instead of the
  159. * actual ROM.
  160. */
  161. void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
  162. {
  163. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  164. void __iomem *rom;
  165. rom = pci_map_rom(pdev, size);
  166. if (!rom)
  167. return NULL;
  168. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW |
  169. IORESOURCE_ROM_BIOS_COPY))
  170. return rom;
  171. res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
  172. if (!res->start)
  173. return rom;
  174. res->end = res->start + *size;
  175. memcpy_fromio((void*)(unsigned long)res->start, rom, *size);
  176. pci_unmap_rom(pdev, rom);
  177. res->flags |= IORESOURCE_ROM_COPY;
  178. return (void __iomem *)(unsigned long)res->start;
  179. }
  180. /**
  181. * pci_unmap_rom - unmap the ROM from kernel space
  182. * @pdev: pointer to pci device struct
  183. * @rom: virtual address of the previous mapping
  184. *
  185. * Remove a mapping of a previously mapped ROM
  186. */
  187. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  188. {
  189. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  190. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  191. return;
  192. iounmap(rom);
  193. /* Disable again before continuing, leave enabled if pci=rom */
  194. if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  195. pci_disable_rom(pdev);
  196. }
  197. /**
  198. * pci_remove_rom - disable the ROM and remove its sysfs attribute
  199. * @pdev: pointer to pci device struct
  200. *
  201. * Remove the rom file in sysfs and disable ROM decoding.
  202. */
  203. void pci_remove_rom(struct pci_dev *pdev)
  204. {
  205. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  206. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  207. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  208. if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  209. IORESOURCE_ROM_SHADOW |
  210. IORESOURCE_ROM_BIOS_COPY |
  211. IORESOURCE_ROM_COPY)))
  212. pci_disable_rom(pdev);
  213. }
  214. /**
  215. * pci_cleanup_rom - internal routine for freeing the ROM copy created
  216. * by pci_map_rom_copy called from remove.c
  217. * @pdev: pointer to pci device struct
  218. *
  219. * Free the copied ROM if we allocated one.
  220. */
  221. void pci_cleanup_rom(struct pci_dev *pdev)
  222. {
  223. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  224. if (res->flags & IORESOURCE_ROM_COPY) {
  225. kfree((void*)(unsigned long)res->start);
  226. res->flags &= ~IORESOURCE_ROM_COPY;
  227. res->start = 0;
  228. res->end = 0;
  229. }
  230. }
  231. EXPORT_SYMBOL(pci_map_rom);
  232. EXPORT_SYMBOL(pci_map_rom_copy);
  233. EXPORT_SYMBOL(pci_unmap_rom);
  234. EXPORT_SYMBOL(pci_remove_rom);