bcm2835_fb.c 14 KB

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