// SPDX-License-Identifier: GPL-2.0-only /* * Mailbox: Common code for Mailbox controllers and users * * Copyright (C) 2013-2014 Linaro Ltd. * Author: Jassi Brar <[email protected]> */ #include <linux/interrupt.h> #include <linux/spinlock.h> #include <linux/mutex.h> #include <linux/delay.h> #include <linux/slab.h> #include <linux/err.h> #include <linux/module.h> #include <linux/device.h> #include <linux/bitops.h> #include <linux/mailbox_client.h> #include <linux/mailbox_controller.h> #include <linux/of.h> #include "mailbox.h" static LIST_HEAD(mbox_cons); static DEFINE_MUTEX(con_mutex); static int add_to_rbuf(struct mbox_chan *chan, void *mssg) { … } static void msg_submit(struct mbox_chan *chan) { … } static void tx_tick(struct mbox_chan *chan, int r) { … } static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer) { … } /** * mbox_chan_received_data - A way for controller driver to push data * received from remote to the upper layer. * @chan: Pointer to the mailbox channel on which RX happened. * @mssg: Client specific message typecasted as void * * * After startup and before shutdown any data received on the chan * is passed on to the API via atomic mbox_chan_received_data(). * The controller should ACK the RX only after this call returns. */ void mbox_chan_received_data(struct mbox_chan *chan, void *mssg) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_chan_txdone - A way for controller driver to notify the * framework that the last TX has completed. * @chan: Pointer to the mailbox chan on which TX happened. * @r: Status of last TX - OK or ERROR * * The controller that has IRQ for TX ACK calls this atomic API * to tick the TX state machine. It works only if txdone_irq * is set by the controller. */ void mbox_chan_txdone(struct mbox_chan *chan, int r) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_client_txdone - The way for a client to run the TX state machine. * @chan: Mailbox channel assigned to this client. * @r: Success status of last transmission. * * The client/protocol had received some 'ACK' packet and it notifies * the API that the last packet was sent successfully. This only works * if the controller can't sense TX-Done. */ void mbox_client_txdone(struct mbox_chan *chan, int r) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_client_peek_data - A way for client driver to pull data * received from remote by the controller. * @chan: Mailbox channel assigned to this client. * * A poke to controller driver for any received data. * The data is actually passed onto client via the * mbox_chan_received_data() * The call can be made from atomic context, so the controller's * implementation of peek_data() must not sleep. * * Return: True, if controller has, and is going to push after this, * some data. * False, if controller doesn't have any data to be read. */ bool mbox_client_peek_data(struct mbox_chan *chan) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_send_message - For client to submit a message to be * sent to the remote. * @chan: Mailbox channel assigned to this client. * @mssg: Client specific message typecasted. * * For client to submit data to the controller destined for a remote * processor. If the client had set 'tx_block', the call will return * either when the remote receives the data or when 'tx_tout' millisecs * run out. * In non-blocking mode, the requests are buffered by the API and a * non-negative token is returned for each queued request. If the request * is not queued, a negative token is returned. Upon failure or successful * TX, the API calls 'tx_done' from atomic context, from which the client * could submit yet another request. * The pointer to message should be preserved until it is sent * over the chan, i.e, tx_done() is made. * This function could be called from atomic context as it simply * queues the data and returns a token against the request. * * Return: Non-negative integer for successful submission (non-blocking mode) * or transmission over chan (blocking mode). * Negative value denotes failure. */ int mbox_send_message(struct mbox_chan *chan, void *mssg) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_flush - flush a mailbox channel * @chan: mailbox channel to flush * @timeout: time, in milliseconds, to allow the flush operation to succeed * * Mailbox controllers that need to work in atomic context can implement the * ->flush() callback to busy loop until a transmission has been completed. * The implementation must call mbox_chan_txdone() upon success. Clients can * call the mbox_flush() function at any time after mbox_send_message() to * flush the transmission. After the function returns success, the mailbox * transmission is guaranteed to have completed. * * Returns: 0 on success or a negative error code on failure. */ int mbox_flush(struct mbox_chan *chan, unsigned long timeout) { … } EXPORT_SYMBOL_GPL(…); static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl) { … } /** * mbox_bind_client - Request a mailbox channel. * @chan: The mailbox channel to bind the client to. * @cl: Identity of the client requesting the channel. * * The Client specifies its requirements and capabilities while asking for * a mailbox channel. It can't be called from atomic context. * The channel is exclusively allocated and can't be used by another * client before the owner calls mbox_free_channel. * After assignment, any packet received on this channel will be * handed over to the client via the 'rx_callback'. * The framework holds reference to the client, so the mbox_client * structure shouldn't be modified until the mbox_free_channel returns. * * Return: 0 if the channel was assigned to the client successfully. * <0 for request failure. */ int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_request_channel - Request a mailbox channel. * @cl: Identity of the client requesting the channel. * @index: Index of mailbox specifier in 'mboxes' property. * * The Client specifies its requirements and capabilities while asking for * a mailbox channel. It can't be called from atomic context. * The channel is exclusively allocated and can't be used by another * client before the owner calls mbox_free_channel. * After assignment, any packet received on this channel will be * handed over to the client via the 'rx_callback'. * The framework holds reference to the client, so the mbox_client * structure shouldn't be modified until the mbox_free_channel returns. * * Return: Pointer to the channel assigned to the client if successful. * ERR_PTR for request failure. */ struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index) { … } EXPORT_SYMBOL_GPL(…); struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl, const char *name) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_free_channel - The client relinquishes control of a mailbox * channel by this call. * @chan: The mailbox channel to be freed. */ void mbox_free_channel(struct mbox_chan *chan) { … } EXPORT_SYMBOL_GPL(…); static struct mbox_chan * of_mbox_index_xlate(struct mbox_controller *mbox, const struct of_phandle_args *sp) { … } /** * mbox_controller_register - Register the mailbox controller * @mbox: Pointer to the mailbox controller. * * The controller driver registers its communication channels */ int mbox_controller_register(struct mbox_controller *mbox) { … } EXPORT_SYMBOL_GPL(…); /** * mbox_controller_unregister - Unregister the mailbox controller * @mbox: Pointer to the mailbox controller. */ void mbox_controller_unregister(struct mbox_controller *mbox) { … } EXPORT_SYMBOL_GPL(…); static void __devm_mbox_controller_unregister(struct device *dev, void *res) { … } static int devm_mbox_controller_match(struct device *dev, void *res, void *data) { … } /** * devm_mbox_controller_register() - managed mbox_controller_register() * @dev: device owning the mailbox controller being registered * @mbox: mailbox controller being registered * * This function adds a device-managed resource that will make sure that the * mailbox controller, which is registered using mbox_controller_register() * as part of this function, will be unregistered along with the rest of * device-managed resources upon driver probe failure or driver removal. * * Returns 0 on success or a negative error code on failure. */ int devm_mbox_controller_register(struct device *dev, struct mbox_controller *mbox) { … } EXPORT_SYMBOL_GPL(…); /** * devm_mbox_controller_unregister() - managed mbox_controller_unregister() * @dev: device owning the mailbox controller being unregistered * @mbox: mailbox controller being unregistered * * This function unregisters the mailbox controller and removes the device- * managed resource that was set up to automatically unregister the mailbox * controller on driver probe failure or driver removal. It's typically not * necessary to call this function. */ void devm_mbox_controller_unregister(struct device *dev, struct mbox_controller *mbox) { … } EXPORT_SYMBOL_GPL(…);