pci_rom.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include <linux/screen_info.h>
  33. #ifdef CONFIG_HAVE_ACPI_RESUME
  34. #include <asm/acpi.h>
  35. #endif
  36. __weak bool board_should_run_oprom(pci_dev_t dev)
  37. {
  38. return true;
  39. }
  40. static bool should_load_oprom(pci_dev_t dev)
  41. {
  42. #ifdef CONFIG_HAVE_ACPI_RESUME
  43. if (acpi_get_slp_type() == 3)
  44. return false;
  45. #endif
  46. if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
  47. return 1;
  48. if (board_should_run_oprom(dev))
  49. return 1;
  50. return 0;
  51. }
  52. __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
  53. {
  54. return vendev;
  55. }
  56. static int pci_rom_probe(pci_dev_t dev, uint class,
  57. struct pci_rom_header **hdrp)
  58. {
  59. struct pci_rom_header *rom_header;
  60. struct pci_rom_data *rom_data;
  61. u16 vendor, device;
  62. u16 rom_vendor, rom_device;
  63. u32 rom_class;
  64. u32 vendev;
  65. u32 mapped_vendev;
  66. u32 rom_address;
  67. pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
  68. pci_read_config_word(dev, PCI_DEVICE_ID, &device);
  69. vendev = vendor << 16 | device;
  70. mapped_vendev = board_map_oprom_vendev(vendev);
  71. if (vendev != mapped_vendev)
  72. debug("Device ID mapped to %#08x\n", mapped_vendev);
  73. #ifdef CONFIG_VGA_BIOS_ADDR
  74. rom_address = CONFIG_VGA_BIOS_ADDR;
  75. #else
  76. pci_read_config_dword(dev, PCI_ROM_ADDRESS, &rom_address);
  77. if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
  78. debug("%s: rom_address=%x\n", __func__, rom_address);
  79. return -ENOENT;
  80. }
  81. /* Enable expansion ROM address decoding. */
  82. pci_write_config_dword(dev, PCI_ROM_ADDRESS,
  83. rom_address | PCI_ROM_ADDRESS_ENABLE);
  84. #endif
  85. debug("Option ROM address %x\n", rom_address);
  86. rom_header = (struct pci_rom_header *)(unsigned long)rom_address;
  87. debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n",
  88. le16_to_cpu(rom_header->signature),
  89. rom_header->size * 512, le16_to_cpu(rom_header->data));
  90. if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
  91. printf("Incorrect expansion ROM header signature %04x\n",
  92. le16_to_cpu(rom_header->signature));
  93. #ifndef CONFIG_VGA_BIOS_ADDR
  94. /* Disable expansion ROM address decoding */
  95. pci_write_config_dword(dev, PCI_ROM_ADDRESS, rom_address);
  96. #endif
  97. return -EINVAL;
  98. }
  99. rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
  100. rom_vendor = le16_to_cpu(rom_data->vendor);
  101. rom_device = le16_to_cpu(rom_data->device);
  102. debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
  103. rom_vendor, rom_device);
  104. /* If the device id is mapped, a mismatch is expected */
  105. if ((vendor != rom_vendor || device != rom_device) &&
  106. (vendev == mapped_vendev)) {
  107. printf("ID mismatch: vendor ID %04x, device ID %04x\n",
  108. rom_vendor, rom_device);
  109. /* Continue anyway */
  110. }
  111. rom_class = (le16_to_cpu(rom_data->class_hi) << 8) | rom_data->class_lo;
  112. debug("PCI ROM image, Class Code %06x, Code Type %02x\n",
  113. rom_class, rom_data->type);
  114. if (class != rom_class) {
  115. debug("Class Code mismatch ROM %06x, dev %06x\n",
  116. rom_class, class);
  117. }
  118. *hdrp = rom_header;
  119. return 0;
  120. }
  121. int pci_rom_load(struct pci_rom_header *rom_header,
  122. struct pci_rom_header **ram_headerp)
  123. {
  124. struct pci_rom_data *rom_data;
  125. unsigned int rom_size;
  126. unsigned int image_size = 0;
  127. void *target;
  128. do {
  129. /* Get next image, until we see an x86 version */
  130. rom_header = (struct pci_rom_header *)((void *)rom_header +
  131. image_size);
  132. rom_data = (struct pci_rom_data *)((void *)rom_header +
  133. le16_to_cpu(rom_header->data));
  134. image_size = le16_to_cpu(rom_data->ilen) * 512;
  135. } while ((rom_data->type != 0) && (rom_data->indicator == 0));
  136. if (rom_data->type != 0)
  137. return -EACCES;
  138. rom_size = rom_header->size * 512;
  139. #ifdef PCI_VGA_RAM_IMAGE_START
  140. target = (void *)PCI_VGA_RAM_IMAGE_START;
  141. #else
  142. target = (void *)malloc(rom_size);
  143. if (!target)
  144. return -ENOMEM;
  145. #endif
  146. if (target != rom_header) {
  147. ulong start = get_timer(0);
  148. debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n",
  149. rom_header, target, rom_size);
  150. memcpy(target, rom_header, rom_size);
  151. if (memcmp(target, rom_header, rom_size)) {
  152. printf("VGA ROM copy failed\n");
  153. return -EFAULT;
  154. }
  155. debug("Copy took %lums\n", get_timer(start));
  156. }
  157. *ram_headerp = target;
  158. return 0;
  159. }
  160. static struct vbe_mode_info mode_info;
  161. int vbe_get_video_info(struct graphic_device *gdev)
  162. {
  163. #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
  164. struct vesa_mode_info *vesa = &mode_info.vesa;
  165. gdev->winSizeX = vesa->x_resolution;
  166. gdev->winSizeY = vesa->y_resolution;
  167. gdev->plnSizeX = vesa->x_resolution;
  168. gdev->plnSizeY = vesa->y_resolution;
  169. gdev->gdfBytesPP = vesa->bits_per_pixel / 8;
  170. switch (vesa->bits_per_pixel) {
  171. case 32:
  172. case 24:
  173. gdev->gdfIndex = GDF_32BIT_X888RGB;
  174. break;
  175. case 16:
  176. gdev->gdfIndex = GDF_16BIT_565RGB;
  177. break;
  178. default:
  179. gdev->gdfIndex = GDF__8BIT_INDEX;
  180. break;
  181. }
  182. gdev->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS;
  183. gdev->pciBase = vesa->phys_base_ptr;
  184. gdev->frameAdrs = vesa->phys_base_ptr;
  185. gdev->memSize = vesa->bytes_per_scanline * vesa->y_resolution;
  186. gdev->vprBase = vesa->phys_base_ptr;
  187. gdev->cprBase = vesa->phys_base_ptr;
  188. return gdev->winSizeX ? 0 : -ENOSYS;
  189. #else
  190. return -ENOSYS;
  191. #endif
  192. }
  193. void setup_video(struct screen_info *screen_info)
  194. {
  195. #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
  196. struct vesa_mode_info *vesa = &mode_info.vesa;
  197. screen_info->orig_video_isVGA = VIDEO_TYPE_VLFB;
  198. screen_info->lfb_width = vesa->x_resolution;
  199. screen_info->lfb_height = vesa->y_resolution;
  200. screen_info->lfb_depth = vesa->bits_per_pixel;
  201. screen_info->lfb_linelength = vesa->bytes_per_scanline;
  202. screen_info->lfb_base = vesa->phys_base_ptr;
  203. screen_info->lfb_size =
  204. ALIGN(screen_info->lfb_linelength * screen_info->lfb_height,
  205. 65536);
  206. screen_info->lfb_size >>= 16;
  207. screen_info->red_size = vesa->red_mask_size;
  208. screen_info->red_pos = vesa->red_mask_pos;
  209. screen_info->green_size = vesa->green_mask_size;
  210. screen_info->green_pos = vesa->green_mask_pos;
  211. screen_info->blue_size = vesa->blue_mask_size;
  212. screen_info->blue_pos = vesa->blue_mask_pos;
  213. screen_info->rsvd_size = vesa->reserved_mask_size;
  214. screen_info->rsvd_pos = vesa->reserved_mask_pos;
  215. #endif
  216. }
  217. int pci_run_vga_bios(pci_dev_t dev, int (*int15_handler)(void), int exec_method)
  218. {
  219. struct pci_rom_header *rom, *ram;
  220. int vesa_mode = -1;
  221. uint class;
  222. bool emulate;
  223. int ret;
  224. /* Only execute VGA ROMs */
  225. pci_read_config_dword(dev, PCI_REVISION_ID, &class);
  226. if (((class >> 16) ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
  227. debug("%s: Class %#x, should be %#x\n", __func__, class,
  228. PCI_CLASS_DISPLAY_VGA);
  229. return -ENODEV;
  230. }
  231. class >>= 8;
  232. if (!should_load_oprom(dev))
  233. return -ENXIO;
  234. ret = pci_rom_probe(dev, class, &rom);
  235. if (ret)
  236. return ret;
  237. ret = pci_rom_load(rom, &ram);
  238. if (ret)
  239. return ret;
  240. if (!board_should_run_oprom(dev))
  241. return -ENXIO;
  242. #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
  243. defined(CONFIG_FRAMEBUFFER_VESA_MODE)
  244. vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
  245. #endif
  246. debug("Selected vesa mode %#x\n", vesa_mode);
  247. if (exec_method & PCI_ROM_USE_NATIVE) {
  248. #ifdef CONFIG_X86
  249. emulate = false;
  250. #else
  251. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  252. printf("BIOS native execution is only available on x86\n");
  253. return -ENOSYS;
  254. }
  255. emulate = true;
  256. #endif
  257. } else {
  258. #ifdef CONFIG_BIOSEMU
  259. emulate = true;
  260. #else
  261. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  262. printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
  263. return -ENOSYS;
  264. }
  265. emulate = false;
  266. #endif
  267. }
  268. if (emulate) {
  269. #ifdef CONFIG_BIOSEMU
  270. BE_VGAInfo *info;
  271. ret = biosemu_setup(dev, &info);
  272. if (ret)
  273. return ret;
  274. biosemu_set_interrupt_handler(0x15, int15_handler);
  275. ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info, true,
  276. vesa_mode, &mode_info);
  277. if (ret)
  278. return ret;
  279. #endif
  280. } else {
  281. #ifdef CONFIG_X86
  282. bios_set_interrupt_handler(0x15, int15_handler);
  283. bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
  284. &mode_info);
  285. #endif
  286. }
  287. debug("Final vesa mode %#x\n", mode_info.video_mode);
  288. return 0;
  289. }