mailbox.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. The Common Mailbox Framework
  2. Jassi Brar <jaswinder.singh@linaro.org>
  3. This document aims to help developers write client and controller
  4. drivers for the API. But before we start, let us note that the
  5. client (especially) and controller drivers are likely going to be
  6. very platform specific because the remote firmware is likely to be
  7. proprietary and implement non-standard protocol. So even if two
  8. platforms employ, say, PL320 controller, the client drivers can't
  9. be shared across them. Even the PL320 driver might need to accommodate
  10. some platform specific quirks. So the API is meant mainly to avoid
  11. similar copies of code written for each platform. Having said that,
  12. nothing prevents the remote f/w to also be Linux based and use the
  13. same api there. However none of that helps us locally because we only
  14. ever deal at client's protocol level.
  15. Some of the choices made during implementation are the result of this
  16. peculiarity of this "common" framework.
  17. Part 1 - Controller Driver (See include/linux/mailbox_controller.h)
  18. Allocate mbox_controller and the array of mbox_chan.
  19. Populate mbox_chan_ops, except peek_data() all are mandatory.
  20. The controller driver might know a message has been consumed
  21. by the remote by getting an IRQ or polling some hardware flag
  22. or it can never know (the client knows by way of the protocol).
  23. The method in order of preference is IRQ -> Poll -> None, which
  24. the controller driver should set via 'txdone_irq' or 'txdone_poll'
  25. or neither.
  26. Part 2 - Client Driver (See include/linux/mailbox_client.h)
  27. The client might want to operate in blocking mode (synchronously
  28. send a message through before returning) or non-blocking/async mode (submit
  29. a message and a callback function to the API and return immediately).
  30. struct demo_client {
  31. struct mbox_client cl;
  32. struct mbox_chan *mbox;
  33. struct completion c;
  34. bool async;
  35. /* ... */
  36. };
  37. /*
  38. * This is the handler for data received from remote. The behaviour is purely
  39. * dependent upon the protocol. This is just an example.
  40. */
  41. static void message_from_remote(struct mbox_client *cl, void *mssg)
  42. {
  43. struct demo_client *dc = container_of(cl, struct demo_client, cl);
  44. if (dc->async) {
  45. if (is_an_ack(mssg)) {
  46. /* An ACK to our last sample sent */
  47. return; /* Or do something else here */
  48. } else { /* A new message from remote */
  49. queue_req(mssg);
  50. }
  51. } else {
  52. /* Remote f/w sends only ACK packets on this channel */
  53. return;
  54. }
  55. }
  56. static void sample_sent(struct mbox_client *cl, void *mssg, int r)
  57. {
  58. struct demo_client *dc = container_of(cl, struct demo_client, cl);
  59. complete(&dc->c);
  60. }
  61. static void client_demo(struct platform_device *pdev)
  62. {
  63. struct demo_client *dc_sync, *dc_async;
  64. /* The controller already knows async_pkt and sync_pkt */
  65. struct async_pkt ap;
  66. struct sync_pkt sp;
  67. dc_sync = kzalloc(sizeof(*dc_sync), GFP_KERNEL);
  68. dc_async = kzalloc(sizeof(*dc_async), GFP_KERNEL);
  69. /* Populate non-blocking mode client */
  70. dc_async->cl.dev = &pdev->dev;
  71. dc_async->cl.rx_callback = message_from_remote;
  72. dc_async->cl.tx_done = sample_sent;
  73. dc_async->cl.tx_block = false;
  74. dc_async->cl.tx_tout = 0; /* doesn't matter here */
  75. dc_async->cl.knows_txdone = false; /* depending upon protocol */
  76. dc_async->async = true;
  77. init_completion(&dc_async->c);
  78. /* Populate blocking mode client */
  79. dc_sync->cl.dev = &pdev->dev;
  80. dc_sync->cl.rx_callback = message_from_remote;
  81. dc_sync->cl.tx_done = NULL; /* operate in blocking mode */
  82. dc_sync->cl.tx_block = true;
  83. dc_sync->cl.tx_tout = 500; /* by half a second */
  84. dc_sync->cl.knows_txdone = false; /* depending upon protocol */
  85. dc_sync->async = false;
  86. /* ASync mailbox is listed second in 'mboxes' property */
  87. dc_async->mbox = mbox_request_channel(&dc_async->cl, 1);
  88. /* Populate data packet */
  89. /* ap.xxx = 123; etc */
  90. /* Send async message to remote */
  91. mbox_send_message(dc_async->mbox, &ap);
  92. /* Sync mailbox is listed first in 'mboxes' property */
  93. dc_sync->mbox = mbox_request_channel(&dc_sync->cl, 0);
  94. /* Populate data packet */
  95. /* sp.abc = 123; etc */
  96. /* Send message to remote in blocking mode */
  97. mbox_send_message(dc_sync->mbox, &sp);
  98. /* At this point 'sp' has been sent */
  99. /* Now wait for async chan to be done */
  100. wait_for_completion(&dc_async->c);
  101. }