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