pci_rom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <linux/screen_info.h>
  40. DECLARE_GLOBAL_DATA_PTR;
  41. __weak bool board_should_run_oprom(struct udevice *dev)
  42. {
  43. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_ACPI_RESUME)
  44. if (gd->arch.prev_sleep_state == ACPI_S3) {
  45. if (IS_ENABLED(CONFIG_S3_VGA_ROM_RUN))
  46. return true;
  47. else
  48. return false;
  49. }
  50. #endif
  51. return true;
  52. }
  53. __weak bool board_should_load_oprom(struct udevice *dev)
  54. {
  55. return true;
  56. }
  57. __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
  58. {
  59. return vendev;
  60. }
  61. static int pci_rom_probe(struct udevice *dev, struct pci_rom_header **hdrp)
  62. {
  63. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  64. struct pci_rom_header *rom_header;
  65. struct pci_rom_data *rom_data;
  66. u16 rom_vendor, rom_device;
  67. u32 rom_class;
  68. u32 vendev;
  69. u32 mapped_vendev;
  70. u32 rom_address;
  71. vendev = pplat->vendor << 16 | pplat->device;
  72. mapped_vendev = board_map_oprom_vendev(vendev);
  73. if (vendev != mapped_vendev)
  74. debug("Device ID mapped to %#08x\n", mapped_vendev);
  75. #ifdef CONFIG_VGA_BIOS_ADDR
  76. rom_address = CONFIG_VGA_BIOS_ADDR;
  77. #else
  78. dm_pci_read_config32(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. dm_pci_write_config32(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. #ifndef CONFIG_VGA_BIOS_ADDR
  96. /* Disable expansion ROM address decoding */
  97. dm_pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address);
  98. #endif
  99. return -EINVAL;
  100. }
  101. rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
  102. rom_vendor = le16_to_cpu(rom_data->vendor);
  103. rom_device = le16_to_cpu(rom_data->device);
  104. debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
  105. rom_vendor, rom_device);
  106. /* If the device id is mapped, a mismatch is expected */
  107. if ((pplat->vendor != rom_vendor || pplat->device != rom_device) &&
  108. (vendev == mapped_vendev)) {
  109. printf("ID mismatch: vendor ID %04x, device ID %04x\n",
  110. rom_vendor, rom_device);
  111. /* Continue anyway */
  112. }
  113. rom_class = (le16_to_cpu(rom_data->class_hi) << 8) | rom_data->class_lo;
  114. debug("PCI ROM image, Class Code %06x, Code Type %02x\n",
  115. rom_class, rom_data->type);
  116. if (pplat->class != rom_class) {
  117. debug("Class Code mismatch ROM %06x, dev %06x\n",
  118. rom_class, pplat->class);
  119. }
  120. *hdrp = rom_header;
  121. return 0;
  122. }
  123. /**
  124. * pci_rom_load() - Load a ROM image and return a pointer to it
  125. *
  126. * @rom_header: Pointer to ROM image
  127. * @ram_headerp: Returns a pointer to the image in RAM
  128. * @allocedp: Returns true if @ram_headerp was allocated and needs
  129. * to be freed
  130. * @return 0 if OK, -ve on error. Note that @allocedp is set up regardless of
  131. * the error state. Even if this function returns an error, it may have
  132. * allocated memory.
  133. */
  134. static int pci_rom_load(struct pci_rom_header *rom_header,
  135. struct pci_rom_header **ram_headerp, bool *allocedp)
  136. {
  137. struct pci_rom_data *rom_data;
  138. unsigned int rom_size;
  139. unsigned int image_size = 0;
  140. void *target;
  141. *allocedp = false;
  142. do {
  143. /* Get next image, until we see an x86 version */
  144. rom_header = (struct pci_rom_header *)((void *)rom_header +
  145. image_size);
  146. rom_data = (struct pci_rom_data *)((void *)rom_header +
  147. le16_to_cpu(rom_header->data));
  148. image_size = le16_to_cpu(rom_data->ilen) * 512;
  149. } while ((rom_data->type != 0) && (rom_data->indicator == 0));
  150. if (rom_data->type != 0)
  151. return -EACCES;
  152. rom_size = rom_header->size * 512;
  153. #ifdef PCI_VGA_RAM_IMAGE_START
  154. target = (void *)PCI_VGA_RAM_IMAGE_START;
  155. #else
  156. target = (void *)malloc(rom_size);
  157. if (!target)
  158. return -ENOMEM;
  159. *allocedp = true;
  160. #endif
  161. if (target != rom_header) {
  162. ulong start = get_timer(0);
  163. debug("Copying VGA ROM Image from %p to %p, 0x%x bytes\n",
  164. rom_header, target, rom_size);
  165. memcpy(target, rom_header, rom_size);
  166. if (memcmp(target, rom_header, rom_size)) {
  167. printf("VGA ROM copy failed\n");
  168. return -EFAULT;
  169. }
  170. debug("Copy took %lums\n", get_timer(start));
  171. }
  172. *ram_headerp = target;
  173. return 0;
  174. }
  175. struct vbe_mode_info mode_info;
  176. void setup_video(struct screen_info *screen_info)
  177. {
  178. struct vesa_mode_info *vesa = &mode_info.vesa;
  179. /* Sanity test on VESA parameters */
  180. if (!vesa->x_resolution || !vesa->y_resolution)
  181. return;
  182. screen_info->orig_video_isVGA = VIDEO_TYPE_VLFB;
  183. screen_info->lfb_width = vesa->x_resolution;
  184. screen_info->lfb_height = vesa->y_resolution;
  185. screen_info->lfb_depth = vesa->bits_per_pixel;
  186. screen_info->lfb_linelength = vesa->bytes_per_scanline;
  187. screen_info->lfb_base = vesa->phys_base_ptr;
  188. screen_info->lfb_size =
  189. ALIGN(screen_info->lfb_linelength * screen_info->lfb_height,
  190. 65536);
  191. screen_info->lfb_size >>= 16;
  192. screen_info->red_size = vesa->red_mask_size;
  193. screen_info->red_pos = vesa->red_mask_pos;
  194. screen_info->green_size = vesa->green_mask_size;
  195. screen_info->green_pos = vesa->green_mask_pos;
  196. screen_info->blue_size = vesa->blue_mask_size;
  197. screen_info->blue_pos = vesa->blue_mask_pos;
  198. screen_info->rsvd_size = vesa->reserved_mask_size;
  199. screen_info->rsvd_pos = vesa->reserved_mask_pos;
  200. }
  201. int dm_pci_run_vga_bios(struct udevice *dev, int (*int15_handler)(void),
  202. int exec_method)
  203. {
  204. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  205. struct pci_rom_header *rom = NULL, *ram = NULL;
  206. int vesa_mode = -1;
  207. bool emulate, alloced;
  208. int ret;
  209. /* Only execute VGA ROMs */
  210. if (((pplat->class >> 8) ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
  211. debug("%s: Class %#x, should be %#x\n", __func__, pplat->class,
  212. PCI_CLASS_DISPLAY_VGA);
  213. return -ENODEV;
  214. }
  215. if (!board_should_load_oprom(dev))
  216. return log_msg_ret("Should not load OPROM", -ENXIO);
  217. ret = pci_rom_probe(dev, &rom);
  218. if (ret)
  219. return ret;
  220. ret = pci_rom_load(rom, &ram, &alloced);
  221. if (ret)
  222. goto err;
  223. if (!board_should_run_oprom(dev)) {
  224. ret = -ENXIO;
  225. goto err;
  226. }
  227. #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
  228. defined(CONFIG_FRAMEBUFFER_VESA_MODE)
  229. vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
  230. #endif
  231. debug("Selected vesa mode %#x\n", vesa_mode);
  232. if (exec_method & PCI_ROM_USE_NATIVE) {
  233. #ifdef CONFIG_X86
  234. emulate = false;
  235. #else
  236. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  237. printf("BIOS native execution is only available on x86\n");
  238. ret = -ENOSYS;
  239. goto err;
  240. }
  241. emulate = true;
  242. #endif
  243. } else {
  244. #ifdef CONFIG_BIOSEMU
  245. emulate = true;
  246. #else
  247. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  248. printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
  249. ret = -ENOSYS;
  250. goto err;
  251. }
  252. emulate = false;
  253. #endif
  254. }
  255. if (emulate) {
  256. #ifdef CONFIG_BIOSEMU
  257. BE_VGAInfo *info;
  258. ret = biosemu_setup(dev, &info);
  259. if (ret)
  260. goto err;
  261. biosemu_set_interrupt_handler(0x15, int15_handler);
  262. ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info,
  263. true, vesa_mode, &mode_info);
  264. if (ret)
  265. goto err;
  266. #endif
  267. } else {
  268. #if defined(CONFIG_X86) && (CONFIG_IS_ENABLED(X86_32BIT_INIT) || CONFIG_TPL)
  269. bios_set_interrupt_handler(0x15, int15_handler);
  270. bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
  271. &mode_info);
  272. #endif
  273. }
  274. debug("Final vesa mode %#x\n", mode_info.video_mode);
  275. ret = 0;
  276. err:
  277. if (alloced)
  278. free(ram);
  279. return ret;
  280. }
  281. #ifdef CONFIG_DM_VIDEO
  282. int vbe_setup_video_priv(struct vesa_mode_info *vesa,
  283. struct video_priv *uc_priv,
  284. struct video_uc_platdata *plat)
  285. {
  286. if (!vesa->x_resolution)
  287. return log_msg_ret("No x resolution", -ENXIO);
  288. uc_priv->xsize = vesa->x_resolution;
  289. uc_priv->ysize = vesa->y_resolution;
  290. uc_priv->line_length = vesa->bytes_per_scanline;
  291. switch (vesa->bits_per_pixel) {
  292. case 32:
  293. case 24:
  294. uc_priv->bpix = VIDEO_BPP32;
  295. break;
  296. case 16:
  297. uc_priv->bpix = VIDEO_BPP16;
  298. break;
  299. default:
  300. return -EPROTONOSUPPORT;
  301. }
  302. /* Use double buffering if enabled */
  303. if (IS_ENABLED(CONFIG_VIDEO_COPY)) {
  304. if (!plat->base)
  305. return log_msg_ret("copy", -ENFILE);
  306. plat->copy_base = vesa->phys_base_ptr;
  307. } else {
  308. plat->base = vesa->phys_base_ptr;
  309. }
  310. log_debug("base = %lx, copy_base = %lx\n", plat->base, plat->copy_base);
  311. plat->size = vesa->bytes_per_scanline * vesa->y_resolution;
  312. return 0;
  313. }
  314. int vbe_setup_video(struct udevice *dev, int (*int15_handler)(void))
  315. {
  316. struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
  317. struct video_priv *uc_priv = dev_get_uclass_priv(dev);
  318. int ret;
  319. /* If we are running from EFI or coreboot, this can't work */
  320. if (!ll_boot_init()) {
  321. printf("Not available (previous bootloader prevents it)\n");
  322. return -EPERM;
  323. }
  324. bootstage_start(BOOTSTAGE_ID_ACCUM_LCD, "vesa display");
  325. ret = dm_pci_run_vga_bios(dev, int15_handler, PCI_ROM_USE_NATIVE |
  326. PCI_ROM_ALLOW_FALLBACK);
  327. bootstage_accum(BOOTSTAGE_ID_ACCUM_LCD);
  328. if (ret) {
  329. debug("failed to run video BIOS: %d\n", ret);
  330. return ret;
  331. }
  332. ret = vbe_setup_video_priv(&mode_info.vesa, uc_priv, plat);
  333. if (ret) {
  334. if (ret == -ENFILE) {
  335. /*
  336. * See video-uclass.c for how to set up reserved memory
  337. * in your video driver
  338. */
  339. log_err("CONFIG_VIDEO_COPY enabled but driver '%s' set up no reserved memory\n",
  340. dev->driver->name);
  341. }
  342. debug("No video mode configured\n");
  343. return ret;
  344. }
  345. printf("Video: %dx%dx%d\n", uc_priv->xsize, uc_priv->ysize,
  346. mode_info.vesa.bits_per_pixel);
  347. return 0;
  348. }
  349. #endif