rom.c 6.4 KB

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