pci_rom.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (C) 2014 Google, Inc
  3. *
  4. * From coreboot, originally based on the Linux kernel (drivers/pci/pci.c).
  5. *
  6. * Modifications are:
  7. * Copyright (C) 2003-2004 Linux Networx
  8. * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
  9. * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
  10. * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
  11. * Copyright (C) 2005-2006 Tyan
  12. * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
  13. * Copyright (C) 2005-2009 coresystems GmbH
  14. * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
  15. *
  16. * PCI Bus Services, see include/linux/pci.h for further explanation.
  17. *
  18. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  19. * David Mosberger-Tang
  20. *
  21. * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  22. * SPDX-License-Identifier: GPL-2.0
  23. */
  24. #include <common.h>
  25. #include <bios_emul.h>
  26. #include <errno.h>
  27. #include <malloc.h>
  28. #include <pci.h>
  29. #include <pci_rom.h>
  30. #include <vbe.h>
  31. #include <video_fb.h>
  32. #ifdef CONFIG_HAVE_ACPI_RESUME
  33. #include <asm/acpi.h>
  34. #endif
  35. __weak bool board_should_run_oprom(pci_dev_t dev)
  36. {
  37. return true;
  38. }
  39. static bool should_load_oprom(pci_dev_t dev)
  40. {
  41. #ifdef CONFIG_HAVE_ACPI_RESUME
  42. if (acpi_get_slp_type() == 3)
  43. return false;
  44. #endif
  45. if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
  46. return 1;
  47. if (board_should_run_oprom(dev))
  48. return 1;
  49. return 0;
  50. }
  51. __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
  52. {
  53. return vendev;
  54. }
  55. static int pci_rom_probe(pci_dev_t dev, uint class,
  56. struct pci_rom_header **hdrp)
  57. {
  58. struct pci_rom_header *rom_header;
  59. struct pci_rom_data *rom_data;
  60. u16 vendor, device;
  61. u16 rom_vendor, rom_device;
  62. u32 vendev;
  63. u32 mapped_vendev;
  64. u32 rom_address;
  65. pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
  66. pci_read_config_word(dev, PCI_DEVICE_ID, &device);
  67. vendev = vendor << 16 | device;
  68. mapped_vendev = board_map_oprom_vendev(vendev);
  69. if (vendev != mapped_vendev)
  70. debug("Device ID mapped to %#08x\n", mapped_vendev);
  71. #ifdef CONFIG_X86_OPTION_ROM_ADDR
  72. rom_address = CONFIG_X86_OPTION_ROM_ADDR;
  73. #else
  74. if (pciauto_setup_rom(pci_bus_to_hose(PCI_BUS(dev)), dev)) {
  75. debug("Cannot find option ROM\n");
  76. return -ENOENT;
  77. }
  78. pci_read_config_dword(dev, PCI_ROM_ADDRESS, &rom_address);
  79. if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
  80. debug("%s: rom_address=%x\n", __func__, rom_address);
  81. return -ENOENT;
  82. }
  83. /* Enable expansion ROM address decoding. */
  84. pci_write_config_dword(dev, PCI_ROM_ADDRESS,
  85. rom_address | PCI_ROM_ADDRESS_ENABLE);
  86. #endif
  87. debug("Option ROM address %x\n", rom_address);
  88. rom_header = (struct pci_rom_header *)(unsigned long)rom_address;
  89. debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n",
  90. le16_to_cpu(rom_header->signature),
  91. rom_header->size * 512, le16_to_cpu(rom_header->data));
  92. if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
  93. printf("Incorrect expansion ROM header signature %04x\n",
  94. le16_to_cpu(rom_header->signature));
  95. return -EINVAL;
  96. }
  97. rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
  98. rom_vendor = le16_to_cpu(rom_data->vendor);
  99. rom_device = le16_to_cpu(rom_data->device);
  100. debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
  101. rom_vendor, rom_device);
  102. /* If the device id is mapped, a mismatch is expected */
  103. if ((vendor != rom_vendor || device != rom_device) &&
  104. (vendev == mapped_vendev)) {
  105. printf("ID mismatch: vendor ID %04x, device ID %04x\n",
  106. rom_vendor, rom_device);
  107. /* Continue anyway */
  108. }
  109. debug("PCI ROM image, Class Code %04x%02x, Code Type %02x\n",
  110. rom_data->class_hi, rom_data->class_lo, rom_data->type);
  111. if (class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
  112. debug("Class Code mismatch ROM %08x, dev %08x\n",
  113. (rom_data->class_hi << 8) | rom_data->class_lo,
  114. class);
  115. }
  116. *hdrp = rom_header;
  117. return 0;
  118. }
  119. int pci_rom_load(uint16_t class, struct pci_rom_header *rom_header,
  120. struct pci_rom_header **ram_headerp)
  121. {
  122. struct pci_rom_data *rom_data;
  123. unsigned int rom_size;
  124. unsigned int image_size = 0;
  125. void *target;
  126. do {
  127. /* Get next image, until we see an x86 version */
  128. rom_header = (struct pci_rom_header *)((void *)rom_header +
  129. image_size);
  130. rom_data = (struct pci_rom_data *)((void *)rom_header +
  131. le16_to_cpu(rom_header->data));
  132. image_size = le16_to_cpu(rom_data->ilen) * 512;
  133. } while ((rom_data->type != 0) && (rom_data->indicator == 0));
  134. if (rom_data->type != 0)
  135. return -EACCES;
  136. rom_size = rom_header->size * 512;
  137. #ifdef PCI_VGA_RAM_IMAGE_START
  138. target = (void *)PCI_VGA_RAM_IMAGE_START;
  139. #else
  140. target = (void *)malloc(rom_size);
  141. if (!target)
  142. return -ENOMEM;
  143. #endif
  144. if (target != rom_header) {
  145. ulong start = get_timer(0);
  146. debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n",
  147. rom_header, target, rom_size);
  148. memcpy(target, rom_header, rom_size);
  149. if (memcmp(target, rom_header, rom_size)) {
  150. printf("VGA ROM copy failed\n");
  151. return -EFAULT;
  152. }
  153. debug("Copy took %lums\n", get_timer(start));
  154. }
  155. *ram_headerp = target;
  156. return 0;
  157. }
  158. static struct vbe_mode_info mode_info;
  159. int vbe_get_video_info(struct graphic_device *gdev)
  160. {
  161. #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
  162. struct vesa_mode_info *vesa = &mode_info.vesa;
  163. gdev->winSizeX = vesa->x_resolution;
  164. gdev->winSizeY = vesa->y_resolution;
  165. gdev->plnSizeX = vesa->x_resolution;
  166. gdev->plnSizeY = vesa->y_resolution;
  167. gdev->gdfBytesPP = vesa->bits_per_pixel / 8;
  168. switch (vesa->bits_per_pixel) {
  169. case 24:
  170. gdev->gdfIndex = GDF_32BIT_X888RGB;
  171. break;
  172. case 16:
  173. gdev->gdfIndex = GDF_16BIT_565RGB;
  174. break;
  175. default:
  176. gdev->gdfIndex = GDF__8BIT_INDEX;
  177. break;
  178. }
  179. gdev->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS;
  180. gdev->pciBase = vesa->phys_base_ptr;
  181. gdev->frameAdrs = vesa->phys_base_ptr;
  182. gdev->memSize = vesa->bytes_per_scanline * vesa->y_resolution;
  183. gdev->vprBase = vesa->phys_base_ptr;
  184. gdev->cprBase = vesa->phys_base_ptr;
  185. return gdev->winSizeX ? 0 : -ENOSYS;
  186. #else
  187. return -ENOSYS;
  188. #endif
  189. }
  190. int pci_run_vga_bios(pci_dev_t dev, int (*int15_handler)(void), int exec_method)
  191. {
  192. struct pci_rom_header *rom, *ram;
  193. int vesa_mode = -1;
  194. uint16_t class;
  195. bool emulate;
  196. int ret;
  197. /* Only execute VGA ROMs */
  198. pci_read_config_word(dev, PCI_CLASS_DEVICE, &class);
  199. if ((class ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
  200. debug("%s: Class %#x, should be %#x\n", __func__, class,
  201. PCI_CLASS_DISPLAY_VGA);
  202. return -ENODEV;
  203. }
  204. if (!should_load_oprom(dev))
  205. return -ENXIO;
  206. ret = pci_rom_probe(dev, class, &rom);
  207. if (ret)
  208. return ret;
  209. ret = pci_rom_load(class, rom, &ram);
  210. if (ret)
  211. return ret;
  212. if (!board_should_run_oprom(dev))
  213. return -ENXIO;
  214. #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
  215. defined(CONFIG_FRAMEBUFFER_VESA_MODE)
  216. vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
  217. #endif
  218. debug("Selected vesa mode %#x\n", vesa_mode);
  219. if (exec_method & PCI_ROM_USE_NATIVE) {
  220. #ifdef CONFIG_X86
  221. emulate = false;
  222. #else
  223. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  224. printf("BIOS native execution is only available on x86\n");
  225. return -ENOSYS;
  226. }
  227. emulate = true;
  228. #endif
  229. } else {
  230. #ifdef CONFIG_BIOSEMU
  231. emulate = true;
  232. #else
  233. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  234. printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
  235. return -ENOSYS;
  236. }
  237. emulate = false;
  238. #endif
  239. }
  240. if (emulate) {
  241. #ifdef CONFIG_BIOSEMU
  242. BE_VGAInfo *info;
  243. ret = biosemu_setup(dev, &info);
  244. if (ret)
  245. return ret;
  246. biosemu_set_interrupt_handler(0x15, int15_handler);
  247. ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info, true,
  248. vesa_mode, &mode_info);
  249. if (ret)
  250. return ret;
  251. #endif
  252. } else {
  253. #ifdef CONFIG_X86
  254. bios_set_interrupt_handler(0x15, int15_handler);
  255. bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
  256. &mode_info);
  257. #endif
  258. }
  259. debug("Final vesa mode %#x\n", mode_info.video_mode);
  260. return 0;
  261. }