2
0

bcm2835_fb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Raspberry Pi emulation (c) 2012 Gregory Estrade
  3. * Refactoring for Pi2 Copyright (c) 2015, Microsoft. Written by Andrew Baumann.
  4. * This code is licensed under the GNU GPLv2 and later.
  5. *
  6. * Heavily based on milkymist-vgafb.c, copyright terms below:
  7. * QEMU model of the Milkymist VGA framebuffer.
  8. *
  9. * Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "hw/display/bcm2835_fb.h"
  28. #include "hw/hw.h"
  29. #include "hw/irq.h"
  30. #include "framebuffer.h"
  31. #include "ui/pixel_ops.h"
  32. #include "hw/misc/bcm2835_mbox_defs.h"
  33. #include "hw/qdev-properties.h"
  34. #include "migration/vmstate.h"
  35. #include "qemu/log.h"
  36. #include "qemu/module.h"
  37. #define DEFAULT_VCRAM_SIZE 0x4000000
  38. #define BCM2835_FB_OFFSET 0x00100000
  39. /* Maximum permitted framebuffer size; experimentally determined on an rpi2 */
  40. #define XRES_MAX 3840
  41. #define YRES_MAX 2560
  42. /* Framebuffer size used if guest requests zero size */
  43. #define XRES_SMALL 592
  44. #define YRES_SMALL 488
  45. static void fb_invalidate_display(void *opaque)
  46. {
  47. BCM2835FBState *s = BCM2835_FB(opaque);
  48. s->invalidate = true;
  49. }
  50. static void draw_line_src16(void *opaque, uint8_t *dst, const uint8_t *src,
  51. int width, int deststep)
  52. {
  53. BCM2835FBState *s = opaque;
  54. uint16_t rgb565;
  55. uint32_t rgb888;
  56. uint8_t r, g, b;
  57. DisplaySurface *surface = qemu_console_surface(s->con);
  58. int bpp = surface_bits_per_pixel(surface);
  59. while (width--) {
  60. switch (s->config.bpp) {
  61. case 8:
  62. /* lookup palette starting at video ram base
  63. * TODO: cache translation, rather than doing this each time!
  64. */
  65. rgb888 = ldl_le_phys(&s->dma_as, s->vcram_base + (*src << 2));
  66. r = (rgb888 >> 0) & 0xff;
  67. g = (rgb888 >> 8) & 0xff;
  68. b = (rgb888 >> 16) & 0xff;
  69. src++;
  70. break;
  71. case 16:
  72. rgb565 = lduw_le_p(src);
  73. r = ((rgb565 >> 11) & 0x1f) << 3;
  74. g = ((rgb565 >> 5) & 0x3f) << 2;
  75. b = ((rgb565 >> 0) & 0x1f) << 3;
  76. src += 2;
  77. break;
  78. case 24:
  79. rgb888 = ldl_le_p(src);
  80. r = (rgb888 >> 0) & 0xff;
  81. g = (rgb888 >> 8) & 0xff;
  82. b = (rgb888 >> 16) & 0xff;
  83. src += 3;
  84. break;
  85. case 32:
  86. rgb888 = ldl_le_p(src);
  87. r = (rgb888 >> 0) & 0xff;
  88. g = (rgb888 >> 8) & 0xff;
  89. b = (rgb888 >> 16) & 0xff;
  90. src += 4;
  91. break;
  92. default:
  93. r = 0;
  94. g = 0;
  95. b = 0;
  96. break;
  97. }
  98. if (s->config.pixo == 0) {
  99. /* swap to BGR pixel format */
  100. uint8_t tmp = r;
  101. r = b;
  102. b = tmp;
  103. }
  104. switch (bpp) {
  105. case 8:
  106. *dst++ = rgb_to_pixel8(r, g, b);
  107. break;
  108. case 15:
  109. *(uint16_t *)dst = rgb_to_pixel15(r, g, b);
  110. dst += 2;
  111. break;
  112. case 16:
  113. *(uint16_t *)dst = rgb_to_pixel16(r, g, b);
  114. dst += 2;
  115. break;
  116. case 24:
  117. rgb888 = rgb_to_pixel24(r, g, b);
  118. *dst++ = rgb888 & 0xff;
  119. *dst++ = (rgb888 >> 8) & 0xff;
  120. *dst++ = (rgb888 >> 16) & 0xff;
  121. break;
  122. case 32:
  123. *(uint32_t *)dst = rgb_to_pixel32(r, g, b);
  124. dst += 4;
  125. break;
  126. default:
  127. return;
  128. }
  129. }
  130. }
  131. static bool fb_use_offsets(BCM2835FBConfig *config)
  132. {
  133. /*
  134. * Return true if we should use the viewport offsets.
  135. * Experimentally, the hardware seems to do this only if the
  136. * viewport size is larger than the physical screen. (It doesn't
  137. * prevent the guest setting this silly viewport setting, though...)
  138. */
  139. return config->xres_virtual > config->xres &&
  140. config->yres_virtual > config->yres;
  141. }
  142. static void fb_update_display(void *opaque)
  143. {
  144. BCM2835FBState *s = opaque;
  145. DisplaySurface *surface = qemu_console_surface(s->con);
  146. int first = 0;
  147. int last = 0;
  148. int src_width = 0;
  149. int dest_width = 0;
  150. uint32_t xoff = 0, yoff = 0;
  151. if (s->lock || !s->config.xres) {
  152. return;
  153. }
  154. src_width = bcm2835_fb_get_pitch(&s->config);
  155. if (fb_use_offsets(&s->config)) {
  156. xoff = s->config.xoffset;
  157. yoff = s->config.yoffset;
  158. }
  159. dest_width = s->config.xres;
  160. switch (surface_bits_per_pixel(surface)) {
  161. case 0:
  162. return;
  163. case 8:
  164. break;
  165. case 15:
  166. dest_width *= 2;
  167. break;
  168. case 16:
  169. dest_width *= 2;
  170. break;
  171. case 24:
  172. dest_width *= 3;
  173. break;
  174. case 32:
  175. dest_width *= 4;
  176. break;
  177. default:
  178. hw_error("bcm2835_fb: bad color depth\n");
  179. break;
  180. }
  181. if (s->invalidate) {
  182. hwaddr base = s->config.base + xoff + (hwaddr)yoff * src_width;
  183. framebuffer_update_memory_section(&s->fbsection, s->dma_mr,
  184. base,
  185. s->config.yres, src_width);
  186. }
  187. framebuffer_update_display(surface, &s->fbsection,
  188. s->config.xres, s->config.yres,
  189. src_width, dest_width, 0, s->invalidate,
  190. draw_line_src16, s, &first, &last);
  191. if (first >= 0) {
  192. dpy_gfx_update(s->con, 0, first, s->config.xres,
  193. last - first + 1);
  194. }
  195. s->invalidate = false;
  196. }
  197. void bcm2835_fb_validate_config(BCM2835FBConfig *config)
  198. {
  199. /*
  200. * Validate the config, and clip any bogus values into range,
  201. * as the hardware does. Note that fb_update_display() relies on
  202. * this happening to prevent it from performing out-of-range
  203. * accesses on redraw.
  204. */
  205. config->xres = MIN(config->xres, XRES_MAX);
  206. config->xres_virtual = MIN(config->xres_virtual, XRES_MAX);
  207. config->yres = MIN(config->yres, YRES_MAX);
  208. config->yres_virtual = MIN(config->yres_virtual, YRES_MAX);
  209. /*
  210. * These are not minima: a 40x40 framebuffer will be accepted.
  211. * They're only used as defaults if the guest asks for zero size.
  212. */
  213. if (config->xres == 0) {
  214. config->xres = XRES_SMALL;
  215. }
  216. if (config->yres == 0) {
  217. config->yres = YRES_SMALL;
  218. }
  219. if (config->xres_virtual == 0) {
  220. config->xres_virtual = config->xres;
  221. }
  222. if (config->yres_virtual == 0) {
  223. config->yres_virtual = config->yres;
  224. }
  225. if (fb_use_offsets(config)) {
  226. /* Clip the offsets so the viewport is within the physical screen */
  227. config->xoffset = MIN(config->xoffset,
  228. config->xres_virtual - config->xres);
  229. config->yoffset = MIN(config->yoffset,
  230. config->yres_virtual - config->yres);
  231. }
  232. }
  233. void bcm2835_fb_reconfigure(BCM2835FBState *s, BCM2835FBConfig *newconfig)
  234. {
  235. s->lock = true;
  236. s->config = *newconfig;
  237. s->invalidate = true;
  238. qemu_console_resize(s->con, s->config.xres, s->config.yres);
  239. s->lock = false;
  240. }
  241. static void bcm2835_fb_mbox_push(BCM2835FBState *s, uint32_t value)
  242. {
  243. uint32_t pitch;
  244. uint32_t size;
  245. BCM2835FBConfig newconf;
  246. value &= ~0xf;
  247. newconf.xres = ldl_le_phys(&s->dma_as, value);
  248. newconf.yres = ldl_le_phys(&s->dma_as, value + 4);
  249. newconf.xres_virtual = ldl_le_phys(&s->dma_as, value + 8);
  250. newconf.yres_virtual = ldl_le_phys(&s->dma_as, value + 12);
  251. newconf.bpp = ldl_le_phys(&s->dma_as, value + 20);
  252. newconf.xoffset = ldl_le_phys(&s->dma_as, value + 24);
  253. newconf.yoffset = ldl_le_phys(&s->dma_as, value + 28);
  254. newconf.base = s->vcram_base | (value & 0xc0000000);
  255. newconf.base += BCM2835_FB_OFFSET;
  256. bcm2835_fb_validate_config(&newconf);
  257. pitch = bcm2835_fb_get_pitch(&newconf);
  258. size = bcm2835_fb_get_size(&newconf);
  259. stl_le_phys(&s->dma_as, value + 16, pitch);
  260. stl_le_phys(&s->dma_as, value + 32, newconf.base);
  261. stl_le_phys(&s->dma_as, value + 36, size);
  262. bcm2835_fb_reconfigure(s, &newconf);
  263. }
  264. static uint64_t bcm2835_fb_read(void *opaque, hwaddr offset, unsigned size)
  265. {
  266. BCM2835FBState *s = opaque;
  267. uint32_t res = 0;
  268. switch (offset) {
  269. case MBOX_AS_DATA:
  270. res = MBOX_CHAN_FB;
  271. s->pending = false;
  272. qemu_set_irq(s->mbox_irq, 0);
  273. break;
  274. case MBOX_AS_PENDING:
  275. res = s->pending;
  276. break;
  277. default:
  278. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
  279. __func__, offset);
  280. return 0;
  281. }
  282. return res;
  283. }
  284. static void bcm2835_fb_write(void *opaque, hwaddr offset, uint64_t value,
  285. unsigned size)
  286. {
  287. BCM2835FBState *s = opaque;
  288. switch (offset) {
  289. case MBOX_AS_DATA:
  290. /* bcm2835_mbox should check our pending status before pushing */
  291. assert(!s->pending);
  292. s->pending = true;
  293. bcm2835_fb_mbox_push(s, value);
  294. qemu_set_irq(s->mbox_irq, 1);
  295. break;
  296. default:
  297. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
  298. __func__, offset);
  299. return;
  300. }
  301. }
  302. static const MemoryRegionOps bcm2835_fb_ops = {
  303. .read = bcm2835_fb_read,
  304. .write = bcm2835_fb_write,
  305. .endianness = DEVICE_NATIVE_ENDIAN,
  306. .valid.min_access_size = 4,
  307. .valid.max_access_size = 4,
  308. };
  309. static const VMStateDescription vmstate_bcm2835_fb = {
  310. .name = TYPE_BCM2835_FB,
  311. .version_id = 1,
  312. .minimum_version_id = 1,
  313. .fields = (VMStateField[]) {
  314. VMSTATE_BOOL(lock, BCM2835FBState),
  315. VMSTATE_BOOL(invalidate, BCM2835FBState),
  316. VMSTATE_BOOL(pending, BCM2835FBState),
  317. VMSTATE_UINT32(config.xres, BCM2835FBState),
  318. VMSTATE_UINT32(config.yres, BCM2835FBState),
  319. VMSTATE_UINT32(config.xres_virtual, BCM2835FBState),
  320. VMSTATE_UINT32(config.yres_virtual, BCM2835FBState),
  321. VMSTATE_UINT32(config.xoffset, BCM2835FBState),
  322. VMSTATE_UINT32(config.yoffset, BCM2835FBState),
  323. VMSTATE_UINT32(config.bpp, BCM2835FBState),
  324. VMSTATE_UINT32(config.base, BCM2835FBState),
  325. VMSTATE_UNUSED(8), /* Was pitch and size */
  326. VMSTATE_UINT32(config.pixo, BCM2835FBState),
  327. VMSTATE_UINT32(config.alpha, BCM2835FBState),
  328. VMSTATE_END_OF_LIST()
  329. }
  330. };
  331. static const GraphicHwOps vgafb_ops = {
  332. .invalidate = fb_invalidate_display,
  333. .gfx_update = fb_update_display,
  334. };
  335. static void bcm2835_fb_init(Object *obj)
  336. {
  337. BCM2835FBState *s = BCM2835_FB(obj);
  338. memory_region_init_io(&s->iomem, obj, &bcm2835_fb_ops, s, TYPE_BCM2835_FB,
  339. 0x10);
  340. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  341. sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
  342. }
  343. static void bcm2835_fb_reset(DeviceState *dev)
  344. {
  345. BCM2835FBState *s = BCM2835_FB(dev);
  346. s->pending = false;
  347. s->config = s->initial_config;
  348. s->invalidate = true;
  349. s->lock = false;
  350. }
  351. static void bcm2835_fb_realize(DeviceState *dev, Error **errp)
  352. {
  353. BCM2835FBState *s = BCM2835_FB(dev);
  354. Error *err = NULL;
  355. Object *obj;
  356. if (s->vcram_base == 0) {
  357. error_setg(errp, "%s: required vcram-base property not set", __func__);
  358. return;
  359. }
  360. obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
  361. if (obj == NULL) {
  362. error_setg(errp, "%s: required dma-mr link not found: %s",
  363. __func__, error_get_pretty(err));
  364. return;
  365. }
  366. /* Fill in the parts of initial_config that are not set by QOM properties */
  367. s->initial_config.xres_virtual = s->initial_config.xres;
  368. s->initial_config.yres_virtual = s->initial_config.yres;
  369. s->initial_config.xoffset = 0;
  370. s->initial_config.yoffset = 0;
  371. s->initial_config.base = s->vcram_base + BCM2835_FB_OFFSET;
  372. s->dma_mr = MEMORY_REGION(obj);
  373. address_space_init(&s->dma_as, s->dma_mr, TYPE_BCM2835_FB "-memory");
  374. bcm2835_fb_reset(dev);
  375. s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
  376. qemu_console_resize(s->con, s->config.xres, s->config.yres);
  377. }
  378. static Property bcm2835_fb_props[] = {
  379. DEFINE_PROP_UINT32("vcram-base", BCM2835FBState, vcram_base, 0),/*required*/
  380. DEFINE_PROP_UINT32("vcram-size", BCM2835FBState, vcram_size,
  381. DEFAULT_VCRAM_SIZE),
  382. DEFINE_PROP_UINT32("xres", BCM2835FBState, initial_config.xres, 640),
  383. DEFINE_PROP_UINT32("yres", BCM2835FBState, initial_config.yres, 480),
  384. DEFINE_PROP_UINT32("bpp", BCM2835FBState, initial_config.bpp, 16),
  385. DEFINE_PROP_UINT32("pixo", BCM2835FBState,
  386. initial_config.pixo, 1), /* 1=RGB, 0=BGR */
  387. DEFINE_PROP_UINT32("alpha", BCM2835FBState,
  388. initial_config.alpha, 2), /* alpha ignored */
  389. DEFINE_PROP_END_OF_LIST()
  390. };
  391. static void bcm2835_fb_class_init(ObjectClass *klass, void *data)
  392. {
  393. DeviceClass *dc = DEVICE_CLASS(klass);
  394. dc->props = bcm2835_fb_props;
  395. dc->realize = bcm2835_fb_realize;
  396. dc->reset = bcm2835_fb_reset;
  397. dc->vmsd = &vmstate_bcm2835_fb;
  398. }
  399. static TypeInfo bcm2835_fb_info = {
  400. .name = TYPE_BCM2835_FB,
  401. .parent = TYPE_SYS_BUS_DEVICE,
  402. .instance_size = sizeof(BCM2835FBState),
  403. .class_init = bcm2835_fb_class_init,
  404. .instance_init = bcm2835_fb_init,
  405. };
  406. static void bcm2835_fb_register_types(void)
  407. {
  408. type_register_static(&bcm2835_fb_info);
  409. }
  410. type_init(bcm2835_fb_register_types)