rom.c 6.5 KB

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