pci_rom.c 11 KB

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