bcm2835_property.c 14 KB

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