bcm2835_property.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Raspberry Pi emulation (c) 2012 Gregory Estrade
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  5. * See the COPYING file in the top-level directory.
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qapi/error.h"
  9. #include "hw/misc/bcm2835_property.h"
  10. #include "hw/qdev-properties.h"
  11. #include "migration/vmstate.h"
  12. #include "hw/irq.h"
  13. #include "hw/misc/bcm2835_mbox_defs.h"
  14. #include "sysemu/dma.h"
  15. #include "qemu/log.h"
  16. #include "qemu/module.h"
  17. #include "trace.h"
  18. /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
  19. static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
  20. {
  21. uint32_t tag;
  22. uint32_t bufsize;
  23. uint32_t tot_len;
  24. size_t resplen;
  25. uint32_t tmp;
  26. int n;
  27. uint32_t offset, length, color;
  28. /*
  29. * Copy the current state of the framebuffer config; we will update
  30. * this copy as we process tags and then ask the framebuffer to use
  31. * it at the end.
  32. */
  33. BCM2835FBConfig fbconfig = s->fbdev->config;
  34. bool fbconfig_updated = false;
  35. value &= ~0xf;
  36. s->addr = value;
  37. tot_len = ldl_le_phys(&s->dma_as, value);
  38. /* @(addr + 4) : Buffer response code */
  39. value = s->addr + 8;
  40. while (value + 8 <= s->addr + tot_len) {
  41. tag = ldl_le_phys(&s->dma_as, value);
  42. bufsize = ldl_le_phys(&s->dma_as, value + 4);
  43. /* @(value + 8) : Request/response indicator */
  44. resplen = 0;
  45. switch (tag) {
  46. case 0x00000000: /* End tag */
  47. break;
  48. case 0x00000001: /* Get firmware revision */
  49. stl_le_phys(&s->dma_as, value + 12, 346337);
  50. resplen = 4;
  51. break;
  52. case 0x00010001: /* Get board model */
  53. qemu_log_mask(LOG_UNIMP,
  54. "bcm2835_property: 0x%08x get board model NYI\n",
  55. tag);
  56. resplen = 4;
  57. break;
  58. case 0x00010002: /* Get board revision */
  59. stl_le_phys(&s->dma_as, value + 12, s->board_rev);
  60. resplen = 4;
  61. break;
  62. case 0x00010003: /* Get board MAC address */
  63. resplen = sizeof(s->macaddr.a);
  64. dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen);
  65. break;
  66. case 0x00010004: /* Get board serial */
  67. qemu_log_mask(LOG_UNIMP,
  68. "bcm2835_property: 0x%08x get board serial NYI\n",
  69. tag);
  70. resplen = 8;
  71. break;
  72. case 0x00010005: /* Get ARM memory */
  73. /* base */
  74. stl_le_phys(&s->dma_as, value + 12, 0);
  75. /* size */
  76. stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
  77. resplen = 8;
  78. break;
  79. case 0x00010006: /* Get VC memory */
  80. /* base */
  81. stl_le_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
  82. /* size */
  83. stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
  84. resplen = 8;
  85. break;
  86. case 0x00028001: /* Set power state */
  87. /* Assume that whatever device they asked for exists,
  88. * and we'll just claim we set it to the desired state
  89. */
  90. tmp = ldl_le_phys(&s->dma_as, value + 16);
  91. stl_le_phys(&s->dma_as, value + 16, (tmp & 1));
  92. resplen = 8;
  93. break;
  94. /* Clocks */
  95. case 0x00030001: /* Get clock state */
  96. stl_le_phys(&s->dma_as, value + 16, 0x1);
  97. resplen = 8;
  98. break;
  99. case 0x00038001: /* Set clock state */
  100. qemu_log_mask(LOG_UNIMP,
  101. "bcm2835_property: 0x%08x set clock state NYI\n",
  102. tag);
  103. resplen = 8;
  104. break;
  105. case 0x00030002: /* Get clock rate */
  106. case 0x00030004: /* Get max clock rate */
  107. case 0x00030007: /* Get min clock rate */
  108. switch (ldl_le_phys(&s->dma_as, value + 12)) {
  109. case 1: /* EMMC */
  110. stl_le_phys(&s->dma_as, value + 16, 50000000);
  111. break;
  112. case 2: /* UART */
  113. stl_le_phys(&s->dma_as, value + 16, 3000000);
  114. break;
  115. default:
  116. stl_le_phys(&s->dma_as, value + 16, 700000000);
  117. break;
  118. }
  119. resplen = 8;
  120. break;
  121. case 0x00038002: /* Set clock rate */
  122. case 0x00038004: /* Set max clock rate */
  123. case 0x00038007: /* Set min clock rate */
  124. qemu_log_mask(LOG_UNIMP,
  125. "bcm2835_property: 0x%08x set clock rate NYI\n",
  126. tag);
  127. resplen = 8;
  128. break;
  129. /* Temperature */
  130. case 0x00030006: /* Get temperature */
  131. stl_le_phys(&s->dma_as, value + 16, 25000);
  132. resplen = 8;
  133. break;
  134. case 0x0003000A: /* Get max temperature */
  135. stl_le_phys(&s->dma_as, value + 16, 99000);
  136. resplen = 8;
  137. break;
  138. /* Frame buffer */
  139. case 0x00040001: /* Allocate buffer */
  140. stl_le_phys(&s->dma_as, value + 12, fbconfig.base);
  141. stl_le_phys(&s->dma_as, value + 16,
  142. bcm2835_fb_get_size(&fbconfig));
  143. resplen = 8;
  144. break;
  145. case 0x00048001: /* Release buffer */
  146. resplen = 0;
  147. break;
  148. case 0x00040002: /* Blank screen */
  149. resplen = 4;
  150. break;
  151. case 0x00044003: /* Test physical display width/height */
  152. case 0x00044004: /* Test virtual display width/height */
  153. resplen = 8;
  154. break;
  155. case 0x00048003: /* Set physical display width/height */
  156. fbconfig.xres = ldl_le_phys(&s->dma_as, value + 12);
  157. fbconfig.yres = ldl_le_phys(&s->dma_as, value + 16);
  158. bcm2835_fb_validate_config(&fbconfig);
  159. fbconfig_updated = true;
  160. /* fall through */
  161. case 0x00040003: /* Get physical display width/height */
  162. stl_le_phys(&s->dma_as, value + 12, fbconfig.xres);
  163. stl_le_phys(&s->dma_as, value + 16, fbconfig.yres);
  164. resplen = 8;
  165. break;
  166. case 0x00048004: /* Set virtual display width/height */
  167. fbconfig.xres_virtual = ldl_le_phys(&s->dma_as, value + 12);
  168. fbconfig.yres_virtual = ldl_le_phys(&s->dma_as, value + 16);
  169. bcm2835_fb_validate_config(&fbconfig);
  170. fbconfig_updated = true;
  171. /* fall through */
  172. case 0x00040004: /* Get virtual display width/height */
  173. stl_le_phys(&s->dma_as, value + 12, fbconfig.xres_virtual);
  174. stl_le_phys(&s->dma_as, value + 16, fbconfig.yres_virtual);
  175. resplen = 8;
  176. break;
  177. case 0x00044005: /* Test depth */
  178. resplen = 4;
  179. break;
  180. case 0x00048005: /* Set depth */
  181. fbconfig.bpp = ldl_le_phys(&s->dma_as, value + 12);
  182. bcm2835_fb_validate_config(&fbconfig);
  183. fbconfig_updated = true;
  184. /* fall through */
  185. case 0x00040005: /* Get depth */
  186. stl_le_phys(&s->dma_as, value + 12, fbconfig.bpp);
  187. resplen = 4;
  188. break;
  189. case 0x00044006: /* Test pixel order */
  190. resplen = 4;
  191. break;
  192. case 0x00048006: /* Set pixel order */
  193. fbconfig.pixo = ldl_le_phys(&s->dma_as, value + 12);
  194. bcm2835_fb_validate_config(&fbconfig);
  195. fbconfig_updated = true;
  196. /* fall through */
  197. case 0x00040006: /* Get pixel order */
  198. stl_le_phys(&s->dma_as, value + 12, fbconfig.pixo);
  199. resplen = 4;
  200. break;
  201. case 0x00044007: /* Test pixel alpha */
  202. resplen = 4;
  203. break;
  204. case 0x00048007: /* Set alpha */
  205. fbconfig.alpha = ldl_le_phys(&s->dma_as, value + 12);
  206. bcm2835_fb_validate_config(&fbconfig);
  207. fbconfig_updated = true;
  208. /* fall through */
  209. case 0x00040007: /* Get alpha */
  210. stl_le_phys(&s->dma_as, value + 12, fbconfig.alpha);
  211. resplen = 4;
  212. break;
  213. case 0x00040008: /* Get pitch */
  214. stl_le_phys(&s->dma_as, value + 12,
  215. bcm2835_fb_get_pitch(&fbconfig));
  216. resplen = 4;
  217. break;
  218. case 0x00044009: /* Test virtual offset */
  219. resplen = 8;
  220. break;
  221. case 0x00048009: /* Set virtual offset */
  222. fbconfig.xoffset = ldl_le_phys(&s->dma_as, value + 12);
  223. fbconfig.yoffset = ldl_le_phys(&s->dma_as, value + 16);
  224. bcm2835_fb_validate_config(&fbconfig);
  225. fbconfig_updated = true;
  226. /* fall through */
  227. case 0x00040009: /* Get virtual offset */
  228. stl_le_phys(&s->dma_as, value + 12, fbconfig.xoffset);
  229. stl_le_phys(&s->dma_as, value + 16, fbconfig.yoffset);
  230. resplen = 8;
  231. break;
  232. case 0x0004000a: /* Get/Test/Set overscan */
  233. case 0x0004400a:
  234. case 0x0004800a:
  235. stl_le_phys(&s->dma_as, value + 12, 0);
  236. stl_le_phys(&s->dma_as, value + 16, 0);
  237. stl_le_phys(&s->dma_as, value + 20, 0);
  238. stl_le_phys(&s->dma_as, value + 24, 0);
  239. resplen = 16;
  240. break;
  241. case 0x0004800b: /* Set palette */
  242. offset = ldl_le_phys(&s->dma_as, value + 12);
  243. length = ldl_le_phys(&s->dma_as, value + 16);
  244. n = 0;
  245. while (n < length - offset) {
  246. color = ldl_le_phys(&s->dma_as, value + 20 + (n << 2));
  247. stl_le_phys(&s->dma_as,
  248. s->fbdev->vcram_base + ((offset + n) << 2), color);
  249. n++;
  250. }
  251. stl_le_phys(&s->dma_as, value + 12, 0);
  252. resplen = 4;
  253. break;
  254. case 0x00060001: /* Get DMA channels */
  255. /* channels 2-5 */
  256. stl_le_phys(&s->dma_as, value + 12, 0x003C);
  257. resplen = 4;
  258. break;
  259. case 0x00050001: /* Get command line */
  260. resplen = 0;
  261. break;
  262. default:
  263. qemu_log_mask(LOG_UNIMP,
  264. "bcm2835_property: unhandled tag 0x%08x\n", tag);
  265. break;
  266. }
  267. trace_bcm2835_mbox_property(tag, bufsize, resplen);
  268. if (tag == 0) {
  269. break;
  270. }
  271. stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
  272. value += bufsize + 12;
  273. }
  274. /* Reconfigure framebuffer if required */
  275. if (fbconfig_updated) {
  276. bcm2835_fb_reconfigure(s->fbdev, &fbconfig);
  277. }
  278. /* Buffer response code */
  279. stl_le_phys(&s->dma_as, s->addr + 4, (1 << 31));
  280. }
  281. static uint64_t bcm2835_property_read(void *opaque, hwaddr offset,
  282. unsigned size)
  283. {
  284. BCM2835PropertyState *s = opaque;
  285. uint32_t res = 0;
  286. switch (offset) {
  287. case MBOX_AS_DATA:
  288. res = MBOX_CHAN_PROPERTY | s->addr;
  289. s->pending = false;
  290. qemu_set_irq(s->mbox_irq, 0);
  291. break;
  292. case MBOX_AS_PENDING:
  293. res = s->pending;
  294. break;
  295. default:
  296. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
  297. __func__, offset);
  298. return 0;
  299. }
  300. return res;
  301. }
  302. static void bcm2835_property_write(void *opaque, hwaddr offset,
  303. uint64_t value, unsigned size)
  304. {
  305. BCM2835PropertyState *s = opaque;
  306. switch (offset) {
  307. case MBOX_AS_DATA:
  308. /* bcm2835_mbox should check our pending status before pushing */
  309. assert(!s->pending);
  310. s->pending = true;
  311. bcm2835_property_mbox_push(s, value);
  312. qemu_set_irq(s->mbox_irq, 1);
  313. break;
  314. default:
  315. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
  316. __func__, offset);
  317. return;
  318. }
  319. }
  320. static const MemoryRegionOps bcm2835_property_ops = {
  321. .read = bcm2835_property_read,
  322. .write = bcm2835_property_write,
  323. .endianness = DEVICE_NATIVE_ENDIAN,
  324. .valid.min_access_size = 4,
  325. .valid.max_access_size = 4,
  326. };
  327. static const VMStateDescription vmstate_bcm2835_property = {
  328. .name = TYPE_BCM2835_PROPERTY,
  329. .version_id = 1,
  330. .minimum_version_id = 1,
  331. .fields = (VMStateField[]) {
  332. VMSTATE_MACADDR(macaddr, BCM2835PropertyState),
  333. VMSTATE_UINT32(addr, BCM2835PropertyState),
  334. VMSTATE_BOOL(pending, BCM2835PropertyState),
  335. VMSTATE_END_OF_LIST()
  336. }
  337. };
  338. static void bcm2835_property_init(Object *obj)
  339. {
  340. BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
  341. memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
  342. TYPE_BCM2835_PROPERTY, 0x10);
  343. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  344. sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
  345. }
  346. static void bcm2835_property_reset(DeviceState *dev)
  347. {
  348. BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
  349. s->pending = false;
  350. }
  351. static void bcm2835_property_realize(DeviceState *dev, Error **errp)
  352. {
  353. BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
  354. Object *obj;
  355. obj = object_property_get_link(OBJECT(dev), "fb", &error_abort);
  356. s->fbdev = BCM2835_FB(obj);
  357. obj = object_property_get_link(OBJECT(dev), "dma-mr", &error_abort);
  358. s->dma_mr = MEMORY_REGION(obj);
  359. address_space_init(&s->dma_as, s->dma_mr, TYPE_BCM2835_PROPERTY "-memory");
  360. /* TODO: connect to MAC address of USB NIC device, once we emulate it */
  361. qemu_macaddr_default_if_unset(&s->macaddr);
  362. bcm2835_property_reset(dev);
  363. }
  364. static Property bcm2835_property_props[] = {
  365. DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0),
  366. DEFINE_PROP_END_OF_LIST()
  367. };
  368. static void bcm2835_property_class_init(ObjectClass *klass, void *data)
  369. {
  370. DeviceClass *dc = DEVICE_CLASS(klass);
  371. device_class_set_props(dc, bcm2835_property_props);
  372. dc->realize = bcm2835_property_realize;
  373. dc->vmsd = &vmstate_bcm2835_property;
  374. }
  375. static TypeInfo bcm2835_property_info = {
  376. .name = TYPE_BCM2835_PROPERTY,
  377. .parent = TYPE_SYS_BUS_DEVICE,
  378. .instance_size = sizeof(BCM2835PropertyState),
  379. .class_init = bcm2835_property_class_init,
  380. .instance_init = bcm2835_property_init,
  381. };
  382. static void bcm2835_property_register_types(void)
  383. {
  384. type_register_static(&bcm2835_property_info);
  385. }
  386. type_init(bcm2835_property_register_types)