probe_roms.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/sched.h>
  3. #include <linux/mm.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/mmzone.h>
  6. #include <linux/ioport.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/console.h>
  9. #include <linux/init.h>
  10. #include <linux/edd.h>
  11. #include <linux/dmi.h>
  12. #include <linux/pfn.h>
  13. #include <linux/pci.h>
  14. #include <linux/export.h>
  15. #include <asm/probe_roms.h>
  16. #include <asm/pci-direct.h>
  17. #include <asm/e820/api.h>
  18. #include <asm/mmzone.h>
  19. #include <asm/setup.h>
  20. #include <asm/sections.h>
  21. #include <asm/io.h>
  22. #include <asm/setup_arch.h>
  23. #include <asm/sev.h>
  24. static struct resource system_rom_resource = {
  25. .name = "System ROM",
  26. .start = 0xf0000,
  27. .end = 0xfffff,
  28. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  29. };
  30. static struct resource extension_rom_resource = {
  31. .name = "Extension ROM",
  32. .start = 0xe0000,
  33. .end = 0xeffff,
  34. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  35. };
  36. static struct resource adapter_rom_resources[] = { {
  37. .name = "Adapter ROM",
  38. .start = 0xc8000,
  39. .end = 0,
  40. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  41. }, {
  42. .name = "Adapter ROM",
  43. .start = 0,
  44. .end = 0,
  45. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  46. }, {
  47. .name = "Adapter ROM",
  48. .start = 0,
  49. .end = 0,
  50. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  51. }, {
  52. .name = "Adapter ROM",
  53. .start = 0,
  54. .end = 0,
  55. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  56. }, {
  57. .name = "Adapter ROM",
  58. .start = 0,
  59. .end = 0,
  60. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  61. }, {
  62. .name = "Adapter ROM",
  63. .start = 0,
  64. .end = 0,
  65. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  66. } };
  67. static struct resource video_rom_resource = {
  68. .name = "Video ROM",
  69. .start = 0xc0000,
  70. .end = 0xc7fff,
  71. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  72. };
  73. /* does this oprom support the given pci device, or any of the devices
  74. * that the driver supports?
  75. */
  76. static bool match_id(struct pci_dev *pdev, unsigned short vendor, unsigned short device)
  77. {
  78. struct pci_driver *drv = to_pci_driver(pdev->dev.driver);
  79. const struct pci_device_id *id;
  80. if (pdev->vendor == vendor && pdev->device == device)
  81. return true;
  82. for (id = drv ? drv->id_table : NULL; id && id->vendor; id++)
  83. if (id->vendor == vendor && id->device == device)
  84. break;
  85. return id && id->vendor;
  86. }
  87. static bool probe_list(struct pci_dev *pdev, unsigned short vendor,
  88. const void *rom_list)
  89. {
  90. unsigned short device;
  91. do {
  92. if (get_kernel_nofault(device, rom_list) != 0)
  93. device = 0;
  94. if (device && match_id(pdev, vendor, device))
  95. break;
  96. rom_list += 2;
  97. } while (device);
  98. return !!device;
  99. }
  100. static struct resource *find_oprom(struct pci_dev *pdev)
  101. {
  102. struct resource *oprom = NULL;
  103. int i;
  104. for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
  105. struct resource *res = &adapter_rom_resources[i];
  106. unsigned short offset, vendor, device, list, rev;
  107. const void *rom;
  108. if (res->end == 0)
  109. break;
  110. rom = isa_bus_to_virt(res->start);
  111. if (get_kernel_nofault(offset, rom + 0x18) != 0)
  112. continue;
  113. if (get_kernel_nofault(vendor, rom + offset + 0x4) != 0)
  114. continue;
  115. if (get_kernel_nofault(device, rom + offset + 0x6) != 0)
  116. continue;
  117. if (match_id(pdev, vendor, device)) {
  118. oprom = res;
  119. break;
  120. }
  121. if (get_kernel_nofault(list, rom + offset + 0x8) == 0 &&
  122. get_kernel_nofault(rev, rom + offset + 0xc) == 0 &&
  123. rev >= 3 && list &&
  124. probe_list(pdev, vendor, rom + offset + list)) {
  125. oprom = res;
  126. break;
  127. }
  128. }
  129. return oprom;
  130. }
  131. void __iomem *pci_map_biosrom(struct pci_dev *pdev)
  132. {
  133. struct resource *oprom = find_oprom(pdev);
  134. if (!oprom)
  135. return NULL;
  136. return ioremap(oprom->start, resource_size(oprom));
  137. }
  138. EXPORT_SYMBOL(pci_map_biosrom);
  139. void pci_unmap_biosrom(void __iomem *image)
  140. {
  141. iounmap(image);
  142. }
  143. EXPORT_SYMBOL(pci_unmap_biosrom);
  144. size_t pci_biosrom_size(struct pci_dev *pdev)
  145. {
  146. struct resource *oprom = find_oprom(pdev);
  147. return oprom ? resource_size(oprom) : 0;
  148. }
  149. EXPORT_SYMBOL(pci_biosrom_size);
  150. #define ROMSIGNATURE 0xaa55
  151. static int __init romsignature(const unsigned char *rom)
  152. {
  153. const unsigned short * const ptr = (const unsigned short *)rom;
  154. unsigned short sig;
  155. return get_kernel_nofault(sig, ptr) == 0 && sig == ROMSIGNATURE;
  156. }
  157. static int __init romchecksum(const unsigned char *rom, unsigned long length)
  158. {
  159. unsigned char sum, c;
  160. for (sum = 0; length && get_kernel_nofault(c, rom++) == 0; length--)
  161. sum += c;
  162. return !length && !sum;
  163. }
  164. void __init probe_roms(void)
  165. {
  166. unsigned long start, length, upper;
  167. const unsigned char *rom;
  168. unsigned char c;
  169. int i;
  170. /*
  171. * The ROM memory range is not part of the e820 table and is therefore not
  172. * pre-validated by BIOS. The kernel page table maps the ROM region as encrypted
  173. * memory, and SNP requires encrypted memory to be validated before access.
  174. * Do that here.
  175. */
  176. snp_prep_memory(video_rom_resource.start,
  177. ((system_rom_resource.end + 1) - video_rom_resource.start),
  178. SNP_PAGE_STATE_PRIVATE);
  179. /* video rom */
  180. upper = adapter_rom_resources[0].start;
  181. for (start = video_rom_resource.start; start < upper; start += 2048) {
  182. rom = isa_bus_to_virt(start);
  183. if (!romsignature(rom))
  184. continue;
  185. video_rom_resource.start = start;
  186. if (get_kernel_nofault(c, rom + 2) != 0)
  187. continue;
  188. /* 0 < length <= 0x7f * 512, historically */
  189. length = c * 512;
  190. /* if checksum okay, trust length byte */
  191. if (length && romchecksum(rom, length))
  192. video_rom_resource.end = start + length - 1;
  193. request_resource(&iomem_resource, &video_rom_resource);
  194. break;
  195. }
  196. start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
  197. if (start < upper)
  198. start = upper;
  199. /* system rom */
  200. request_resource(&iomem_resource, &system_rom_resource);
  201. upper = system_rom_resource.start;
  202. /* check for extension rom (ignore length byte!) */
  203. rom = isa_bus_to_virt(extension_rom_resource.start);
  204. if (romsignature(rom)) {
  205. length = resource_size(&extension_rom_resource);
  206. if (romchecksum(rom, length)) {
  207. request_resource(&iomem_resource, &extension_rom_resource);
  208. upper = extension_rom_resource.start;
  209. }
  210. }
  211. /* check for adapter roms on 2k boundaries */
  212. for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
  213. rom = isa_bus_to_virt(start);
  214. if (!romsignature(rom))
  215. continue;
  216. if (get_kernel_nofault(c, rom + 2) != 0)
  217. continue;
  218. /* 0 < length <= 0x7f * 512, historically */
  219. length = c * 512;
  220. /* but accept any length that fits if checksum okay */
  221. if (!length || start + length > upper || !romchecksum(rom, length))
  222. continue;
  223. adapter_rom_resources[i].start = start;
  224. adapter_rom_resources[i].end = start + length - 1;
  225. request_resource(&iomem_resource, &adapter_rom_resources[i]);
  226. start = adapter_rom_resources[i++].end & ~2047UL;
  227. }
  228. }