rom.c 6.2 KB

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