mailbox_controller.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. */
  6. #ifndef __MAILBOX_CONTROLLER_H
  7. #define __MAILBOX_CONTROLLER_H
  8. #include <linux/of.h>
  9. #include <linux/types.h>
  10. #include <linux/timer.h>
  11. #include <linux/device.h>
  12. #include <linux/completion.h>
  13. struct mbox_chan;
  14. /**
  15. * struct mbox_chan_ops - methods to control mailbox channels
  16. * @send_data: The API asks the MBOX controller driver, in atomic
  17. * context try to transmit a message on the bus. Returns 0 if
  18. * data is accepted for transmission, -EBUSY while rejecting
  19. * if the remote hasn't yet read the last data sent. Actual
  20. * transmission of data is reported by the controller via
  21. * mbox_chan_txdone (if it has some TX ACK irq). It must not
  22. * sleep.
  23. * @startup: Called when a client requests the chan. The controller
  24. * could ask clients for additional parameters of communication
  25. * to be provided via client's chan_data. This call may
  26. * block. After this call the Controller must forward any
  27. * data received on the chan by calling mbox_chan_received_data.
  28. * The controller may do stuff that need to sleep.
  29. * @shutdown: Called when a client relinquishes control of a chan.
  30. * This call may block too. The controller must not forward
  31. * any received data anymore.
  32. * The controller may do stuff that need to sleep.
  33. * @last_tx_done: If the controller sets 'txdone_poll', the API calls
  34. * this to poll status of last TX. The controller must
  35. * give priority to IRQ method over polling and never
  36. * set both txdone_poll and txdone_irq. Only in polling
  37. * mode 'send_data' is expected to return -EBUSY.
  38. * The controller may do stuff that need to sleep/block.
  39. * Used only if txdone_poll:=true && txdone_irq:=false
  40. * @peek_data: Atomic check for any received data. Return true if controller
  41. * has some data to push to the client. False otherwise.
  42. */
  43. struct mbox_chan_ops {
  44. int (*send_data)(struct mbox_chan *chan, void *data);
  45. int (*startup)(struct mbox_chan *chan);
  46. void (*shutdown)(struct mbox_chan *chan);
  47. bool (*last_tx_done)(struct mbox_chan *chan);
  48. bool (*peek_data)(struct mbox_chan *chan);
  49. };
  50. /**
  51. * struct mbox_controller - Controller of a class of communication channels
  52. * @dev: Device backing this controller
  53. * @ops: Operators that work on each communication chan
  54. * @chans: Array of channels
  55. * @num_chans: Number of channels in the 'chans' array.
  56. * @txdone_irq: Indicates if the controller can report to API when
  57. * the last transmitted data was read by the remote.
  58. * Eg, if it has some TX ACK irq.
  59. * @txdone_poll: If the controller can read but not report the TX
  60. * done. Ex, some register shows the TX status but
  61. * no interrupt rises. Ignored if 'txdone_irq' is set.
  62. * @txpoll_period: If 'txdone_poll' is in effect, the API polls for
  63. * last TX's status after these many millisecs
  64. * @of_xlate: Controller driver specific mapping of channel via DT
  65. * @poll: API private. Used to poll for TXDONE on all channels.
  66. * @node: API private. To hook into list of controllers.
  67. */
  68. struct mbox_controller {
  69. struct device *dev;
  70. const struct mbox_chan_ops *ops;
  71. struct mbox_chan *chans;
  72. int num_chans;
  73. bool txdone_irq;
  74. bool txdone_poll;
  75. unsigned txpoll_period;
  76. struct mbox_chan *(*of_xlate)(struct mbox_controller *mbox,
  77. const struct of_phandle_args *sp);
  78. /* Internal to API */
  79. struct timer_list poll;
  80. struct list_head node;
  81. };
  82. /*
  83. * The length of circular buffer for queuing messages from a client.
  84. * 'msg_count' tracks the number of buffered messages while 'msg_free'
  85. * is the index where the next message would be buffered.
  86. * We shouldn't need it too big because every transfer is interrupt
  87. * triggered and if we have lots of data to transfer, the interrupt
  88. * latencies are going to be the bottleneck, not the buffer length.
  89. * Besides, mbox_send_message could be called from atomic context and
  90. * the client could also queue another message from the notifier 'tx_done'
  91. * of the last transfer done.
  92. * REVISIT: If too many platforms see the "Try increasing MBOX_TX_QUEUE_LEN"
  93. * print, it needs to be taken from config option or somesuch.
  94. */
  95. #define MBOX_TX_QUEUE_LEN 20
  96. /**
  97. * struct mbox_chan - s/w representation of a communication chan
  98. * @mbox: Pointer to the parent/provider of this channel
  99. * @txdone_method: Way to detect TXDone chosen by the API
  100. * @cl: Pointer to the current owner of this channel
  101. * @tx_complete: Transmission completion
  102. * @active_req: Currently active request hook
  103. * @msg_count: No. of mssg currently queued
  104. * @msg_free: Index of next available mssg slot
  105. * @msg_data: Hook for data packet
  106. * @lock: Serialise access to the channel
  107. * @con_priv: Hook for controller driver to attach private data
  108. */
  109. struct mbox_chan {
  110. struct mbox_controller *mbox;
  111. unsigned txdone_method;
  112. struct mbox_client *cl;
  113. struct completion tx_complete;
  114. void *active_req;
  115. unsigned msg_count, msg_free;
  116. void *msg_data[MBOX_TX_QUEUE_LEN];
  117. spinlock_t lock; /* Serialise access to the channel */
  118. void *con_priv;
  119. };
  120. int mbox_controller_register(struct mbox_controller *mbox); /* can sleep */
  121. void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */
  122. void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */
  123. void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */
  124. #endif /* __MAILBOX_CONTROLLER_H */