pci_rom.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. __weak bool board_should_load_oprom(struct udevice *dev)
  39. {
  40. return true;
  41. }
  42. __weak uint32_t board_map_oprom_vendev(uint32_t vendev)
  43. {
  44. return vendev;
  45. }
  46. static int pci_rom_probe(struct udevice *dev, struct pci_rom_header **hdrp)
  47. {
  48. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  49. struct pci_rom_header *rom_header;
  50. struct pci_rom_data *rom_data;
  51. u16 rom_vendor, rom_device;
  52. u32 rom_class;
  53. u32 vendev;
  54. u32 mapped_vendev;
  55. u32 rom_address;
  56. vendev = pplat->vendor << 16 | pplat->device;
  57. mapped_vendev = board_map_oprom_vendev(vendev);
  58. if (vendev != mapped_vendev)
  59. debug("Device ID mapped to %#08x\n", mapped_vendev);
  60. #ifdef CONFIG_VGA_BIOS_ADDR
  61. rom_address = CONFIG_VGA_BIOS_ADDR;
  62. #else
  63. dm_pci_read_config32(dev, PCI_ROM_ADDRESS, &rom_address);
  64. if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
  65. debug("%s: rom_address=%x\n", __func__, rom_address);
  66. return -ENOENT;
  67. }
  68. /* Enable expansion ROM address decoding. */
  69. dm_pci_write_config32(dev, PCI_ROM_ADDRESS,
  70. rom_address | PCI_ROM_ADDRESS_ENABLE);
  71. #endif
  72. debug("Option ROM address %x\n", rom_address);
  73. rom_header = (struct pci_rom_header *)(unsigned long)rom_address;
  74. debug("PCI expansion ROM, signature %#04x, INIT size %#04x, data ptr %#04x\n",
  75. le16_to_cpu(rom_header->signature),
  76. rom_header->size * 512, le16_to_cpu(rom_header->data));
  77. if (le16_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
  78. printf("Incorrect expansion ROM header signature %04x\n",
  79. le16_to_cpu(rom_header->signature));
  80. #ifndef CONFIG_VGA_BIOS_ADDR
  81. /* Disable expansion ROM address decoding */
  82. dm_pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address);
  83. #endif
  84. return -EINVAL;
  85. }
  86. rom_data = (((void *)rom_header) + le16_to_cpu(rom_header->data));
  87. rom_vendor = le16_to_cpu(rom_data->vendor);
  88. rom_device = le16_to_cpu(rom_data->device);
  89. debug("PCI ROM image, vendor ID %04x, device ID %04x,\n",
  90. rom_vendor, rom_device);
  91. /* If the device id is mapped, a mismatch is expected */
  92. if ((pplat->vendor != rom_vendor || pplat->device != rom_device) &&
  93. (vendev == mapped_vendev)) {
  94. printf("ID mismatch: vendor ID %04x, device ID %04x\n",
  95. rom_vendor, rom_device);
  96. /* Continue anyway */
  97. }
  98. rom_class = (le16_to_cpu(rom_data->class_hi) << 8) | rom_data->class_lo;
  99. debug("PCI ROM image, Class Code %06x, Code Type %02x\n",
  100. rom_class, rom_data->type);
  101. if (pplat->class != rom_class) {
  102. debug("Class Code mismatch ROM %06x, dev %06x\n",
  103. rom_class, pplat->class);
  104. }
  105. *hdrp = rom_header;
  106. return 0;
  107. }
  108. /**
  109. * pci_rom_load() - Load a ROM image and return a pointer to it
  110. *
  111. * @rom_header: Pointer to ROM image
  112. * @ram_headerp: Returns a pointer to the image in RAM
  113. * @allocedp: Returns true if @ram_headerp was allocated and needs
  114. * to be freed
  115. * @return 0 if OK, -ve on error. Note that @allocedp is set up regardless of
  116. * the error state. Even if this function returns an error, it may have
  117. * allocated memory.
  118. */
  119. static int pci_rom_load(struct pci_rom_header *rom_header,
  120. struct pci_rom_header **ram_headerp, bool *allocedp)
  121. {
  122. struct pci_rom_data *rom_data;
  123. unsigned int rom_size;
  124. unsigned int image_size = 0;
  125. void *target;
  126. *allocedp = false;
  127. do {
  128. /* Get next image, until we see an x86 version */
  129. rom_header = (struct pci_rom_header *)((void *)rom_header +
  130. image_size);
  131. rom_data = (struct pci_rom_data *)((void *)rom_header +
  132. le16_to_cpu(rom_header->data));
  133. image_size = le16_to_cpu(rom_data->ilen) * 512;
  134. } while ((rom_data->type != 0) && (rom_data->indicator == 0));
  135. if (rom_data->type != 0)
  136. return -EACCES;
  137. rom_size = rom_header->size * 512;
  138. #ifdef PCI_VGA_RAM_IMAGE_START
  139. target = (void *)PCI_VGA_RAM_IMAGE_START;
  140. #else
  141. target = (void *)malloc(rom_size);
  142. if (!target)
  143. return -ENOMEM;
  144. *allocedp = true;
  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. 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. struct vesa_mode_info *vesa = &mode_info.vesa;
  196. /* Sanity test on VESA parameters */
  197. if (!vesa->x_resolution || !vesa->y_resolution)
  198. return;
  199. screen_info->orig_video_isVGA = VIDEO_TYPE_VLFB;
  200. screen_info->lfb_width = vesa->x_resolution;
  201. screen_info->lfb_height = vesa->y_resolution;
  202. screen_info->lfb_depth = vesa->bits_per_pixel;
  203. screen_info->lfb_linelength = vesa->bytes_per_scanline;
  204. screen_info->lfb_base = vesa->phys_base_ptr;
  205. screen_info->lfb_size =
  206. ALIGN(screen_info->lfb_linelength * screen_info->lfb_height,
  207. 65536);
  208. screen_info->lfb_size >>= 16;
  209. screen_info->red_size = vesa->red_mask_size;
  210. screen_info->red_pos = vesa->red_mask_pos;
  211. screen_info->green_size = vesa->green_mask_size;
  212. screen_info->green_pos = vesa->green_mask_pos;
  213. screen_info->blue_size = vesa->blue_mask_size;
  214. screen_info->blue_pos = vesa->blue_mask_pos;
  215. screen_info->rsvd_size = vesa->reserved_mask_size;
  216. screen_info->rsvd_pos = vesa->reserved_mask_pos;
  217. }
  218. int dm_pci_run_vga_bios(struct udevice *dev, int (*int15_handler)(void),
  219. int exec_method)
  220. {
  221. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  222. struct pci_rom_header *rom = NULL, *ram = NULL;
  223. int vesa_mode = -1;
  224. bool emulate, alloced;
  225. int ret;
  226. /* Only execute VGA ROMs */
  227. if (((pplat->class >> 8) ^ PCI_CLASS_DISPLAY_VGA) & 0xff00) {
  228. debug("%s: Class %#x, should be %#x\n", __func__, pplat->class,
  229. PCI_CLASS_DISPLAY_VGA);
  230. return -ENODEV;
  231. }
  232. if (!board_should_load_oprom(dev))
  233. return -ENXIO;
  234. ret = pci_rom_probe(dev, &rom);
  235. if (ret)
  236. return ret;
  237. ret = pci_rom_load(rom, &ram, &alloced);
  238. if (ret)
  239. goto err;
  240. if (!board_should_run_oprom(dev)) {
  241. ret = -ENXIO;
  242. goto err;
  243. }
  244. #if defined(CONFIG_FRAMEBUFFER_SET_VESA_MODE) && \
  245. defined(CONFIG_FRAMEBUFFER_VESA_MODE)
  246. vesa_mode = CONFIG_FRAMEBUFFER_VESA_MODE;
  247. #endif
  248. debug("Selected vesa mode %#x\n", vesa_mode);
  249. if (exec_method & PCI_ROM_USE_NATIVE) {
  250. #ifdef CONFIG_X86
  251. emulate = false;
  252. #else
  253. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  254. printf("BIOS native execution is only available on x86\n");
  255. ret = -ENOSYS;
  256. goto err;
  257. }
  258. emulate = true;
  259. #endif
  260. } else {
  261. #ifdef CONFIG_BIOSEMU
  262. emulate = true;
  263. #else
  264. if (!(exec_method & PCI_ROM_ALLOW_FALLBACK)) {
  265. printf("BIOS emulation not available - see CONFIG_BIOSEMU\n");
  266. ret = -ENOSYS;
  267. goto err;
  268. }
  269. emulate = false;
  270. #endif
  271. }
  272. if (emulate) {
  273. #ifdef CONFIG_BIOSEMU
  274. BE_VGAInfo *info;
  275. ret = biosemu_setup(dev, &info);
  276. if (ret)
  277. goto err;
  278. biosemu_set_interrupt_handler(0x15, int15_handler);
  279. ret = biosemu_run(dev, (uchar *)ram, 1 << 16, info,
  280. true, vesa_mode, &mode_info);
  281. if (ret)
  282. goto err;
  283. #endif
  284. } else {
  285. #ifdef CONFIG_X86
  286. bios_set_interrupt_handler(0x15, int15_handler);
  287. bios_run_on_x86(dev, (unsigned long)ram, vesa_mode,
  288. &mode_info);
  289. #endif
  290. }
  291. debug("Final vesa mode %#x\n", mode_info.video_mode);
  292. ret = 0;
  293. err:
  294. if (alloced)
  295. free(ram);
  296. return ret;
  297. }