rom.c 6.6 KB

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