pci_rom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Google, Inc
  4. *
  5. * From coreboot, originally based on the Linux kernel (drivers/pci/pci.c).
  6. *
  7. * Modifications are:
  8. * Copyright (C) 2003-2004 Linux Networx
  9. * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
  10. * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
  11. * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
  12. * Copyright (C) 2005-2006 Tyan
  13. * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
  14. * Copyright (C) 2005-2009 coresystems GmbH
  15. * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
  16. *
  17. * PCI Bus Services, see include/linux/pci.h for further explanation.
  18. *
  19. * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
  20. * David Mosberger-Tang
  21. *
  22. * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  23. */
  24. #define LOG_CATEGORY UCLASS_PCI
  25. #include <common.h>
  26. #include <bios_emul.h>
  27. #include <bootstage.h>
  28. #include <dm.h>
  29. #include <errno.h>
  30. #include <init.h>
  31. #include <log.h>
  32. #include <malloc.h>
  33. #include <pci.h>
  34. #include <pci_rom.h>
  35. #include <vbe.h>
  36. #include <video.h>
  37. #include <video_fb.h>
  38. #include <acpi/acpi_s3.h>
  39. #include <asm/global_data.h>
  40. #include <linux/screen_info.h>
  41. DECLARE_GLOBAL_DATA_PTR;
  42. __weak bool board_should_run_oprom(struct udevice *dev)
  43. {
  44. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_ACPI_RESUME)
  45. if (gd->arch.prev_sleep_state == ACPI_S3) {
  46. if (IS_ENABLED(CONFIG_S3_VGA_ROM_RUN))
  47. return true;
  48. else
  49. return false;
  50. }
  51. #endif
  52. return true;
  53. }
  54. __weak bool board_should_load_oprom(struct udevice *dev)
  55. {
  56. return true;
  57. }
  58. __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
  59. {
  60. return vendev;
  61. }
  62. static int pci_rom_probe(struct udevice *dev, struct pci_rom_header **hdrp)
  63. {
  64. struct pci_child_plat *pplat = dev_get_parent_plat(dev);
  65. struct pci_rom_header *rom_header;
  66. struct pci_rom_data *rom_data;
  67. u16 rom_vendor, rom_device;
  68. u32 rom_class;
  69. u32 vendev;
  70. u32 mapped_vendev;
  71. u32 rom_address;
  72. vendev = pplat->vendor << 16 | pplat->device;
  73. mapped_vendev = board_map_oprom_vendev(vendev);
  74. if (vendev != mapped_vendev)
  75. debug("Device ID mapped to %#08x\n", mapped_vendev);
  76. #ifdef CONFIG_VGA_BIOS_ADDR
  77. rom_address = CONFIG_VGA_BIOS_ADDR;
  78. #else
  79. dm_pci_read_config32(dev, PCI_ROM_ADDRESS, &rom_address);
  80. if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
  81. debug("%s: rom_address=%x\n", __func__, rom_address);
  82. return -ENOENT;
  83. }
  84. /* Enable expansion ROM address decoding. */
  85. dm_pci_write_config32(dev, PCI_ROM_ADDRESS,
  86. rom_address | PCI_ROM_ADDRESS_ENABLE);
  87. #endif
  88. debug("Option ROM address %x\n", rom_address);
  89. rom_header = (struct pci_rom_header *)(unsigned long)rom_address;
  90. debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n",
  91. le16_to_cpu(rom_header->signature),
  92. rom_header->size * 512, le16_to_cpu(rom_header->data));
  93. if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
  94. printf("Incorrect expansion ROM header signature %04x\n",
  95. le16_to_cpu(rom_header->signature));
  96. #ifndef CONFIG_VGA_BIOS_ADDR
  97. /* Disable expansion ROM address decoding */
  98. dm_pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address);
  99. #endif
  100. return -EINVAL;
  101. }
  102. rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
  103. rom_vendor = le16_to_cpu(rom_data->vendor);
  104. rom_device = le16_to_cpu(rom_data->device);
  105. debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
  106. rom_vendor, rom_device);
  107. /* If the device id is mapped, a mismatch is expected */
  108. if ((pplat->vendor != rom_vendor || pplat->device != rom_device) &&
  109. (vendev == mapped_vendev)) {
  110. printf("ID mismatch: vendor ID %04x, device ID %04x\n",
  111. rom_vendor, rom_device);
  112. /* Continue anyway */
  113. }
  114. rom_class = (le16_to_cpu(rom_data->class_hi) << 8) | rom_data->class_lo;
  115. debug("PCI ROM image, Class Code %06x, Code Type %02x\n",
  116. rom_class, rom_data->type);
  117. if (pplat->class != rom_class) {
  118. debug("Class Code mismatch ROM %06x, dev %06x\n",
  119. rom_class, pplat->class);
  120. }
  121. *hdrp = rom_header;
  122. return 0;
  123. }
  124. /**
  125. * pci_rom_load() - Load a ROM image and return a pointer to it
  126. *
  127. * @rom_header: Pointer to ROM image
  128. * @ram_headerp: Returns a pointer to the image in RAM
  129. * @allocedp: Returns true if @ram_headerp was allocated and needs
  130. * to be freed
  131. * @return 0 if OK, -ve on error. Note that @allocedp is set up regardless of
  132. * the error state. Even if this function returns an error, it may have
  133. * allocated memory.
  134. */
  135. static int pci_rom_load(struct pci_rom_header *rom_header,
  136. struct pci_rom_header **ram_headerp, bool *allocedp)
  137. {
  138. struct pci_rom_data *rom_data;
  139. unsigned int rom_size;
  140. unsigned int image_size = 0;
  141. void *target;
  142. *allocedp = false;
  143. do {
  144. /* Get next image, until we see an x86 version */
  145. rom_header = (struct pci_rom_header *)((void *)rom_header +
  146. image_size);
  147. rom_data = (struct pci_rom_data *)((void *)rom_header +
  148. le16_to_cpu(rom_header->data));
  149. image_size = le16_to_cpu(rom_data->ilen) * 512;
  150. } while ((rom_data->type != 0) && (rom_data->indicator == 0));
  151. if (rom_data->type != 0)
  152. return -EACCES;
  153. rom_size = rom_header->size * 512;
  154. #ifdef PCI_VGA_RAM_IMAGE_START
  155. target = (void *)PCI_VGA_RAM_IMAGE_START;
  156. #else
  157. target = (void *)malloc(rom_size);
  158. if (!target)
  159. return -ENOMEM;
  160. *allocedp = true;
  161. #endif
  162. if (target != rom_header) {
  163. ulong start = get_timer(0);
  164. debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n",
  165. rom_header, target, rom_size);
  166. memcpy(target, rom_header, rom_size);
  167. if (memcmp(target, rom_header, rom_size)) {
  168. printf("VGA ROM copy failed\n");
  169. return -EFAULT;
  170. }
  171. debug("Copy took %lums\n", get_timer(start));
  172. }
  173. *ram_headerp = target;
  174. return 0;
  175. }
  176. struct vbe_mode_info mode_info;
  177. void setup_video(struct screen_info *screen_info)
  178. {
  179. struct vesa_mode_info *vesa = &mode_info.vesa;
  180. /* Sanity test on VESA parameters */
  181. if (!vesa->x_resolution || !vesa->y_resolution)
  182. return;
  183. screen_info->orig_video_isVGA = VIDEO_TYPE_VLFB;
  184. screen_info->lfb_width = vesa->x_resolution;
  185. screen_info->lfb_height = vesa->y_resolution;
  186. screen_info->lfb_depth = vesa->bits_per_pixel;
  187. screen_info->lfb_linelength = vesa->bytes_per_scanline;
  188. screen_info->lfb_base = vesa->phys_base_ptr;
  189. screen_info->lfb_size =
  190. ALIGN(screen_info->lfb_linelength * screen_info->lfb_height,
  191. 65536);
  192. screen_info->lfb_size >>= 16;
  193. screen_info->red_size = vesa->red_mask_size;
  194. screen_info->red_pos = vesa->red_mask_pos;
  195. screen_info->green_size = vesa->green_mask_size;
  196. screen_info->green_pos = vesa->green_mask_pos;
  197. screen_info->blue_size = vesa->blue_mask_size;
  198. screen_info->blue_pos = vesa->blue_mask_pos;
  199. screen_info->rsvd_size = vesa->reserved_mask_size;
  200. screen_info->rsvd_pos = vesa->reserved_mask_pos;
  201. }
  202. int dm_pci_run_vga_bios(struct udevice *dev, int (*int15_handler)(void),
  203. int exec_method)
  204. {
  205. struct pci_child_plat *pplat = dev_get_parent_plat(dev);
  206. struct pci_rom_header *rom = NULL, *ram = NULL;
  207. int vesa_mode = -1;
  208. bool emulate, alloced;
  209. int ret;
  210. /* Only execute VGA ROMs */
  211. if (((pplat->class >> 8) ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
  212. debug("%s: Class %#x, should be %#x\n", __func__, pplat->class,
  213. PCI_CLASS_DISPLAY_VGA);
  214. return -ENODEV;
  215. }
  216. if (!board_should_load_oprom(dev))
  217. return log_msg_ret("Should not load OPROM", -ENXIO);
  218. ret = pci_rom_probe(dev, &rom);
  219. if (ret)
  220. return ret;
  221. ret = pci_rom_load(rom, &ram, &alloced);
  222. if (ret)
  223. goto err;
  224. if (!board_should_run_oprom(dev)) {
  225. ret = -ENXIO;
  226. goto err;
  227. }
  228. #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
  229. defined(CONFIG_FRAMEBUFFER_VESA_MODE)
  230. vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
  231. #endif
  232. debug("Selected vesa mode %#x\n", vesa_mode);
  233. if (exec_method & PCI_ROM_USE_NATIVE) {
  234. #ifdef CONFIG_X86
  235. emulate = false;
  236. #else
  237. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  238. printf("BIOS native execution is only available on x86\n");
  239. ret = -ENOSYS;
  240. goto err;
  241. }
  242. emulate = true;
  243. #endif
  244. } else {
  245. #ifdef CONFIG_BIOSEMU
  246. emulate = true;
  247. #else
  248. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  249. printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
  250. ret = -ENOSYS;
  251. goto err;
  252. }
  253. emulate = false;
  254. #endif
  255. }
  256. if (emulate) {
  257. #ifdef CONFIG_BIOSEMU
  258. BE_VGAInfo *info;
  259. ret = biosemu_setup(dev, &info);
  260. if (ret)
  261. goto err;
  262. biosemu_set_interrupt_handler(0x15, int15_handler);
  263. ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info,
  264. true, vesa_mode, &mode_info);
  265. if (ret)
  266. goto err;
  267. #endif
  268. } else {
  269. #if defined(CONFIG_X86) && (CONFIG_IS_ENABLED(X86_32BIT_INIT) || CONFIG_TPL)
  270. bios_set_interrupt_handler(0x15, int15_handler);
  271. bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
  272. &mode_info);
  273. #endif
  274. }
  275. debug("Final vesa mode %#x\n", mode_info.video_mode);
  276. ret = 0;
  277. err:
  278. if (alloced)
  279. free(ram);
  280. return ret;
  281. }
  282. #ifdef CONFIG_DM_VIDEO
  283. int vbe_setup_video_priv(struct vesa_mode_info *vesa,
  284. struct video_priv *uc_priv,
  285. struct video_uc_plat *plat)
  286. {
  287. if (!vesa->x_resolution)
  288. return log_msg_ret("No x resolution", -ENXIO);
  289. uc_priv->xsize = vesa->x_resolution;
  290. uc_priv->ysize = vesa->y_resolution;
  291. uc_priv->line_length = vesa->bytes_per_scanline;
  292. switch (vesa->bits_per_pixel) {
  293. case 32:
  294. case 24:
  295. uc_priv->bpix = VIDEO_BPP32;
  296. break;
  297. case 16:
  298. uc_priv->bpix = VIDEO_BPP16;
  299. break;
  300. default:
  301. return -EPROTONOSUPPORT;
  302. }
  303. /* Use double buffering if enabled */
  304. if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->base)
  305. plat->copy_base = vesa->phys_base_ptr;
  306. else
  307. plat->base = vesa->phys_base_ptr;
  308. log_debug("base = %lx, copy_base = %lx\n", plat->base, plat->copy_base);
  309. plat->size = vesa->bytes_per_scanline * vesa->y_resolution;
  310. return 0;
  311. }
  312. int vbe_setup_video(struct udevice *dev, int (*int15_handler)(void))
  313. {
  314. struct video_uc_plat *plat = dev_get_uclass_plat(dev);
  315. struct video_priv *uc_priv = dev_get_uclass_priv(dev);
  316. int ret;
  317. /* If we are running from EFI or coreboot, this can't work */
  318. if (!ll_boot_init()) {
  319. printf("Not available (previous bootloader prevents it)\n");
  320. return -EPERM;
  321. }
  322. bootstage_start(BOOTSTAGE_ID_ACCUM_LCD, "vesa display");
  323. ret = dm_pci_run_vga_bios(dev, int15_handler, PCI_ROM_USE_NATIVE |
  324. PCI_ROM_ALLOW_FALLBACK);
  325. bootstage_accum(BOOTSTAGE_ID_ACCUM_LCD);
  326. if (ret) {
  327. debug("failed to run video BIOS: %d\n", ret);
  328. return ret;
  329. }
  330. ret = vbe_setup_video_priv(&mode_info.vesa, uc_priv, plat);
  331. if (ret) {
  332. if (ret == -ENFILE) {
  333. /*
  334. * See video-uclass.c for how to set up reserved memory
  335. * in your video driver
  336. */
  337. log_err("CONFIG_VIDEO_COPY enabled but driver '%s' set up no reserved memory\n",
  338. dev->driver->name);
  339. }
  340. debug("No video mode configured\n");
  341. return ret;
  342. }
  343. printf("Video: %dx%dx%d\n", uc_priv->xsize, uc_priv->ysize,
  344. mode_info.vesa.bits_per_pixel);
  345. return 0;
  346. }
  347. #endif