rom.c 6.6 KB

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