2
0

ramfb.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * early boot framebuffer in guest ram
  3. * configured using fw_cfg
  4. *
  5. * Copyright Red Hat, Inc. 2017
  6. *
  7. * Author:
  8. * Gerd Hoffmann <kraxel@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qemu/option.h"
  16. #include "hw/loader.h"
  17. #include "hw/display/ramfb.h"
  18. #include "ui/console.h"
  19. #include "sysemu/reset.h"
  20. struct QEMU_PACKED RAMFBCfg {
  21. uint64_t addr;
  22. uint32_t fourcc;
  23. uint32_t flags;
  24. uint32_t width;
  25. uint32_t height;
  26. uint32_t stride;
  27. };
  28. struct RAMFBState {
  29. DisplaySurface *ds;
  30. uint32_t width, height;
  31. uint32_t starting_width, starting_height;
  32. struct RAMFBCfg cfg;
  33. bool locked;
  34. };
  35. static void ramfb_unmap_display_surface(pixman_image_t *image, void *unused)
  36. {
  37. void *data = pixman_image_get_data(image);
  38. uint32_t size = pixman_image_get_stride(image) *
  39. pixman_image_get_height(image);
  40. cpu_physical_memory_unmap(data, size, 0, 0);
  41. }
  42. static DisplaySurface *ramfb_create_display_surface(int width, int height,
  43. pixman_format_code_t format,
  44. int linesize, uint64_t addr)
  45. {
  46. DisplaySurface *surface;
  47. hwaddr size;
  48. void *data;
  49. if (linesize == 0) {
  50. linesize = width * PIXMAN_FORMAT_BPP(format) / 8;
  51. }
  52. size = (hwaddr)linesize * height;
  53. data = cpu_physical_memory_map(addr, &size, 0);
  54. if (size != (hwaddr)linesize * height) {
  55. cpu_physical_memory_unmap(data, size, 0, 0);
  56. return NULL;
  57. }
  58. surface = qemu_create_displaysurface_from(width, height,
  59. format, linesize, data);
  60. pixman_image_set_destroy_function(surface->image,
  61. ramfb_unmap_display_surface, NULL);
  62. return surface;
  63. }
  64. static void ramfb_fw_cfg_write(void *dev, off_t offset, size_t len)
  65. {
  66. RAMFBState *s = dev;
  67. uint32_t fourcc, format, width, height;
  68. hwaddr stride, addr;
  69. width = be32_to_cpu(s->cfg.width);
  70. height = be32_to_cpu(s->cfg.height);
  71. stride = be32_to_cpu(s->cfg.stride);
  72. fourcc = be32_to_cpu(s->cfg.fourcc);
  73. addr = be64_to_cpu(s->cfg.addr);
  74. format = qemu_drm_format_to_pixman(fourcc);
  75. fprintf(stderr, "%s: %dx%d @ 0x%" PRIx64 "\n", __func__,
  76. width, height, addr);
  77. if (s->locked) {
  78. fprintf(stderr, "%s: resolution locked, change rejected\n", __func__);
  79. return;
  80. }
  81. s->locked = true;
  82. s->width = width;
  83. s->height = height;
  84. s->ds = ramfb_create_display_surface(s->width, s->height,
  85. format, stride, addr);
  86. }
  87. void ramfb_display_update(QemuConsole *con, RAMFBState *s)
  88. {
  89. if (!s->width || !s->height) {
  90. return;
  91. }
  92. if (s->ds) {
  93. dpy_gfx_replace_surface(con, s->ds);
  94. s->ds = NULL;
  95. }
  96. /* simple full screen update */
  97. dpy_gfx_update_full(con);
  98. }
  99. static void ramfb_reset(void *opaque)
  100. {
  101. RAMFBState *s = (RAMFBState *)opaque;
  102. s->locked = false;
  103. memset(&s->cfg, 0, sizeof(s->cfg));
  104. s->cfg.width = s->starting_width;
  105. s->cfg.height = s->starting_height;
  106. }
  107. RAMFBState *ramfb_setup(DeviceState* dev, Error **errp)
  108. {
  109. FWCfgState *fw_cfg = fw_cfg_find();
  110. RAMFBState *s;
  111. if (!fw_cfg || !fw_cfg->dma_enabled) {
  112. error_setg(errp, "ramfb device requires fw_cfg with DMA");
  113. return NULL;
  114. }
  115. s = g_new0(RAMFBState, 1);
  116. const char *s_fb_width = qemu_opt_get(dev->opts, "xres");
  117. const char *s_fb_height = qemu_opt_get(dev->opts, "yres");
  118. if (s_fb_width) {
  119. s->cfg.width = atoi(s_fb_width);
  120. s->starting_width = s->cfg.width;
  121. }
  122. if (s_fb_height) {
  123. s->cfg.height = atoi(s_fb_height);
  124. s->starting_height = s->cfg.height;
  125. }
  126. s->locked = false;
  127. rom_add_vga("vgabios-ramfb.bin");
  128. fw_cfg_add_file_callback(fw_cfg, "etc/ramfb",
  129. NULL, ramfb_fw_cfg_write, s,
  130. &s->cfg, sizeof(s->cfg), false);
  131. qemu_register_reset(ramfb_reset, s);
  132. return s;
  133. }