bcm2835_property.c 14 KB

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