rom.c 6.7 KB

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