pci_rom.c 9.1 KB

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