1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments' Message Manager Driver
5 * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
9 #define pr_fmt(fmt) "%s: " fmt, __func__
11 #include <linux/device.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mailbox_controller.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
19 #include <linux/of_irq.h>
20 #include <linux/platform_device.h>
21 #include <linux/soc/ti/ti-msgmgr.h>
23 #define Q_DATA_OFFSET(proxy, queue, reg) \
24 ((0x10000 * (proxy)) + (0x80 * (queue)) + ((reg) * 4))
25 #define Q_STATE_OFFSET(queue) ((queue) * 0x4)
26 #define Q_STATE_ENTRY_COUNT_MASK (0xFFF000)
29 * struct ti_msgmgr_valid_queue_desc - SoC valid queues meant for this processor
30 * @queue_id: Queue Number for this path
31 * @proxy_id: Proxy ID representing the processor in SoC
32 * @is_tx: Is this a receive path?
34 struct ti_msgmgr_valid_queue_desc
{
41 * struct ti_msgmgr_desc - Description of message manager integration
42 * @queue_count: Number of Queues
43 * @max_message_size: Message size in bytes
44 * @max_messages: Number of messages
45 * @q_slices: Number of queue engines
46 * @q_proxies: Number of queue proxies per page
47 * @data_first_reg: First data register for proxy data region
48 * @data_last_reg: Last data register for proxy data region
49 * @tx_polled: Do I need to use polled mechanism for tx
50 * @tx_poll_timeout_ms: Timeout in ms if polled
51 * @valid_queues: List of Valid queues that the processor can access
52 * @num_valid_queues: Number of valid queues
54 * This structure is used in of match data to describe how integration
55 * for a specific compatible SoC is done.
57 struct ti_msgmgr_desc
{
66 int tx_poll_timeout_ms
;
67 const struct ti_msgmgr_valid_queue_desc
*valid_queues
;
72 * struct ti_queue_inst - Description of a queue instance
74 * @queue_id: Queue Identifier as mapped on SoC
75 * @proxy_id: Proxy Identifier as mapped on SoC
76 * @irq: IRQ for Rx Queue
77 * @is_tx: 'true' if transmit queue, else, 'false'
78 * @queue_buff_start: First register of Data Buffer
79 * @queue_buff_end: Last (or confirmation) register of Data buffer
80 * @queue_state: Queue status register
81 * @chan: Mailbox channel
82 * @rx_buff: Receive buffer pointer allocated at probe, max_message_size
84 struct ti_queue_inst
{
90 void __iomem
*queue_buff_start
;
91 void __iomem
*queue_buff_end
;
92 void __iomem
*queue_state
;
93 struct mbox_chan
*chan
;
98 * struct ti_msgmgr_inst - Description of a Message Manager Instance
99 * @dev: device pointer corresponding to the Message Manager instance
100 * @desc: Description of the SoC integration
101 * @queue_proxy_region: Queue proxy region where queue buffers are located
102 * @queue_state_debug_region: Queue status register regions
103 * @num_valid_queues: Number of valid queues defined for the processor
104 * Note: other queues are probably reserved for other processors
106 * @qinsts: Array of valid Queue Instances for the Processor
107 * @mbox: Mailbox Controller
108 * @chans: Array for channels corresponding to the Queue Instances.
110 struct ti_msgmgr_inst
{
112 const struct ti_msgmgr_desc
*desc
;
113 void __iomem
*queue_proxy_region
;
114 void __iomem
*queue_state_debug_region
;
116 struct ti_queue_inst
*qinsts
;
117 struct mbox_controller mbox
;
118 struct mbox_chan
*chans
;
122 * ti_msgmgr_queue_get_num_messages() - Get the number of pending messages
123 * @qinst: Queue instance for which we check the number of pending messages
125 * Return: number of messages pending in the queue (0 == no pending messages)
127 static inline int ti_msgmgr_queue_get_num_messages(struct ti_queue_inst
*qinst
)
132 * We cannot use relaxed operation here - update may happen
135 val
= readl(qinst
->queue_state
) & Q_STATE_ENTRY_COUNT_MASK
;
136 val
>>= __ffs(Q_STATE_ENTRY_COUNT_MASK
);
142 * ti_msgmgr_queue_rx_interrupt() - Interrupt handler for receive Queue
143 * @irq: Interrupt number
144 * @p: Channel Pointer
146 * Return: -EINVAL if there is no instance
147 * IRQ_NONE if the interrupt is not ours.
148 * IRQ_HANDLED if the rx interrupt was successfully handled.
150 static irqreturn_t
ti_msgmgr_queue_rx_interrupt(int irq
, void *p
)
152 struct mbox_chan
*chan
= p
;
153 struct device
*dev
= chan
->mbox
->dev
;
154 struct ti_msgmgr_inst
*inst
= dev_get_drvdata(dev
);
155 struct ti_queue_inst
*qinst
= chan
->con_priv
;
156 const struct ti_msgmgr_desc
*desc
;
157 int msg_count
, num_words
;
158 struct ti_msgmgr_message message
;
159 void __iomem
*data_reg
;
162 if (WARN_ON(!inst
)) {
163 dev_err(dev
, "no platform drv data??\n");
167 /* Do I have an invalid interrupt source? */
169 dev_err(dev
, "Cannot handle rx interrupt on tx channel %s\n",
174 /* Do I actually have messages to read? */
175 msg_count
= ti_msgmgr_queue_get_num_messages(qinst
);
178 dev_dbg(dev
, "Spurious event - 0 pending data!\n");
183 * I have no idea about the protocol being used to communicate with the
184 * remote producer - 0 could be valid data, so I wont make a judgement
185 * of how many bytes I should be reading. Let the client figure this
186 * out.. I just read the full message and pass it on..
189 message
.len
= desc
->max_message_size
;
190 message
.buf
= (u8
*)qinst
->rx_buff
;
193 * NOTE about register access involved here:
194 * the hardware block is implemented with 32bit access operations and no
195 * support for data splitting. We don't want the hardware to misbehave
196 * with sub 32bit access - For example: if the last register read is
197 * split into byte wise access, it can result in the queue getting
198 * stuck or indeterminate behavior. An out of order read operation may
199 * result in weird data results as well.
200 * Hence, we do not use memcpy_fromio or __ioread32_copy here, instead
201 * we depend on readl for the purpose.
203 * Also note that the final register read automatically marks the
204 * queue message as read.
206 for (data_reg
= qinst
->queue_buff_start
, word_data
= qinst
->rx_buff
,
207 num_words
= (desc
->max_message_size
/ sizeof(u32
));
208 num_words
; num_words
--, data_reg
+= sizeof(u32
), word_data
++)
209 *word_data
= readl(data_reg
);
212 * Last register read automatically clears the IRQ if only 1 message
213 * is pending - so send the data up the stack..
214 * NOTE: Client is expected to be as optimal as possible, since
215 * we invoke the handler in IRQ context.
217 mbox_chan_received_data(chan
, (void *)&message
);
223 * ti_msgmgr_queue_peek_data() - Peek to see if there are any rx messages.
224 * @chan: Channel Pointer
226 * Return: 'true' if there is pending rx data, 'false' if there is none.
228 static bool ti_msgmgr_queue_peek_data(struct mbox_chan
*chan
)
230 struct ti_queue_inst
*qinst
= chan
->con_priv
;
236 msg_count
= ti_msgmgr_queue_get_num_messages(qinst
);
238 return msg_count
? true : false;
242 * ti_msgmgr_last_tx_done() - See if all the tx messages are sent
243 * @chan: Channel pointer
245 * Return: 'true' is no pending tx data, 'false' if there are any.
247 static bool ti_msgmgr_last_tx_done(struct mbox_chan
*chan
)
249 struct ti_queue_inst
*qinst
= chan
->con_priv
;
255 msg_count
= ti_msgmgr_queue_get_num_messages(qinst
);
257 /* if we have any messages pending.. */
258 return msg_count
? false : true;
262 * ti_msgmgr_send_data() - Send data
263 * @chan: Channel Pointer
264 * @data: ti_msgmgr_message * Message Pointer
266 * Return: 0 if all goes good, else appropriate error messages.
268 static int ti_msgmgr_send_data(struct mbox_chan
*chan
, void *data
)
270 struct device
*dev
= chan
->mbox
->dev
;
271 struct ti_msgmgr_inst
*inst
= dev_get_drvdata(dev
);
272 const struct ti_msgmgr_desc
*desc
;
273 struct ti_queue_inst
*qinst
= chan
->con_priv
;
274 int num_words
, trail_bytes
;
275 struct ti_msgmgr_message
*message
= data
;
276 void __iomem
*data_reg
;
279 if (WARN_ON(!inst
)) {
280 dev_err(dev
, "no platform drv data??\n");
285 if (desc
->max_message_size
< message
->len
) {
286 dev_err(dev
, "Queue %s message length %zu > max %d\n",
287 qinst
->name
, message
->len
, desc
->max_message_size
);
291 /* NOTE: Constraints similar to rx path exists here as well */
292 for (data_reg
= qinst
->queue_buff_start
,
293 num_words
= message
->len
/ sizeof(u32
),
294 word_data
= (u32
*)message
->buf
;
295 num_words
; num_words
--, data_reg
+= sizeof(u32
), word_data
++)
296 writel(*word_data
, data_reg
);
298 trail_bytes
= message
->len
% sizeof(u32
);
300 u32 data_trail
= *word_data
;
302 /* Ensure all unused data is 0 */
303 data_trail
&= 0xFFFFFFFF >> (8 * (sizeof(u32
) - trail_bytes
));
304 writel(data_trail
, data_reg
);
308 * 'data_reg' indicates next register to write. If we did not already
309 * write on tx complete reg(last reg), we must do so for transmit
311 if (data_reg
<= qinst
->queue_buff_end
)
312 writel(0, qinst
->queue_buff_end
);
318 * ti_msgmgr_queue_startup() - Startup queue
319 * @chan: Channel pointer
321 * Return: 0 if all goes good, else return corresponding error message
323 static int ti_msgmgr_queue_startup(struct mbox_chan
*chan
)
325 struct ti_queue_inst
*qinst
= chan
->con_priv
;
326 struct device
*dev
= chan
->mbox
->dev
;
331 * With the expectation that the IRQ might be shared in SoC
333 ret
= request_irq(qinst
->irq
, ti_msgmgr_queue_rx_interrupt
,
334 IRQF_SHARED
, qinst
->name
, chan
);
336 dev_err(dev
, "Unable to get IRQ %d on %s(res=%d)\n",
337 qinst
->irq
, qinst
->name
, ret
);
346 * ti_msgmgr_queue_shutdown() - Shutdown the queue
347 * @chan: Channel pointer
349 static void ti_msgmgr_queue_shutdown(struct mbox_chan
*chan
)
351 struct ti_queue_inst
*qinst
= chan
->con_priv
;
354 free_irq(qinst
->irq
, chan
);
358 * ti_msgmgr_of_xlate() - Translation of phandle to queue
359 * @mbox: Mailbox controller
360 * @p: phandle pointer
362 * Return: Mailbox channel corresponding to the queue, else return error
365 static struct mbox_chan
*ti_msgmgr_of_xlate(struct mbox_controller
*mbox
,
366 const struct of_phandle_args
*p
)
368 struct ti_msgmgr_inst
*inst
;
369 int req_qid
, req_pid
;
370 struct ti_queue_inst
*qinst
;
373 inst
= container_of(mbox
, struct ti_msgmgr_inst
, mbox
);
375 return ERR_PTR(-EINVAL
);
377 /* #mbox-cells is 2 */
378 if (p
->args_count
!= 2) {
379 dev_err(inst
->dev
, "Invalid arguments in dt[%d] instead of 2\n",
381 return ERR_PTR(-EINVAL
);
383 req_qid
= p
->args
[0];
384 req_pid
= p
->args
[1];
386 for (qinst
= inst
->qinsts
, i
= 0; i
< inst
->num_valid_queues
;
388 if (req_qid
== qinst
->queue_id
&& req_pid
== qinst
->proxy_id
)
392 dev_err(inst
->dev
, "Queue ID %d, Proxy ID %d is wrong on %s\n",
393 req_qid
, req_pid
, p
->np
->name
);
394 return ERR_PTR(-ENOENT
);
398 * ti_msgmgr_queue_setup() - Setup data structures for each queue instance
399 * @idx: index of the queue
400 * @dev: pointer to the message manager device
401 * @np: pointer to the of node
402 * @inst: Queue instance pointer
403 * @d: Message Manager instance description data
404 * @qd: Queue description data
405 * @qinst: Queue instance pointer
406 * @chan: pointer to mailbox channel
408 * Return: 0 if all went well, else return corresponding error
410 static int ti_msgmgr_queue_setup(int idx
, struct device
*dev
,
411 struct device_node
*np
,
412 struct ti_msgmgr_inst
*inst
,
413 const struct ti_msgmgr_desc
*d
,
414 const struct ti_msgmgr_valid_queue_desc
*qd
,
415 struct ti_queue_inst
*qinst
,
416 struct mbox_chan
*chan
)
418 qinst
->proxy_id
= qd
->proxy_id
;
419 qinst
->queue_id
= qd
->queue_id
;
421 if (qinst
->queue_id
> d
->queue_count
) {
422 dev_err(dev
, "Queue Data [idx=%d] queuid %d > %d\n",
423 idx
, qinst
->queue_id
, d
->queue_count
);
427 qinst
->is_tx
= qd
->is_tx
;
428 snprintf(qinst
->name
, sizeof(qinst
->name
), "%s %s_%03d_%03d",
429 dev_name(dev
), qinst
->is_tx
? "tx" : "rx", qinst
->queue_id
,
433 char of_rx_irq_name
[7];
435 snprintf(of_rx_irq_name
, sizeof(of_rx_irq_name
),
436 "rx_%03d", qinst
->queue_id
);
438 qinst
->irq
= of_irq_get_byname(np
, of_rx_irq_name
);
439 if (qinst
->irq
< 0) {
441 "[%d]QID %d PID %d:No IRQ[%s]: %d\n",
442 idx
, qinst
->queue_id
, qinst
->proxy_id
,
443 of_rx_irq_name
, qinst
->irq
);
446 /* Allocate usage buffer for rx */
447 qinst
->rx_buff
= devm_kzalloc(dev
,
448 d
->max_message_size
, GFP_KERNEL
);
453 qinst
->queue_buff_start
= inst
->queue_proxy_region
+
454 Q_DATA_OFFSET(qinst
->proxy_id
, qinst
->queue_id
, d
->data_first_reg
);
455 qinst
->queue_buff_end
= inst
->queue_proxy_region
+
456 Q_DATA_OFFSET(qinst
->proxy_id
, qinst
->queue_id
, d
->data_last_reg
);
457 qinst
->queue_state
= inst
->queue_state_debug_region
+
458 Q_STATE_OFFSET(qinst
->queue_id
);
461 chan
->con_priv
= qinst
;
463 dev_dbg(dev
, "[%d] qidx=%d pidx=%d irq=%d q_s=%p q_e = %p\n",
464 idx
, qinst
->queue_id
, qinst
->proxy_id
, qinst
->irq
,
465 qinst
->queue_buff_start
, qinst
->queue_buff_end
);
469 /* Queue operations */
470 static const struct mbox_chan_ops ti_msgmgr_chan_ops
= {
471 .startup
= ti_msgmgr_queue_startup
,
472 .shutdown
= ti_msgmgr_queue_shutdown
,
473 .peek_data
= ti_msgmgr_queue_peek_data
,
474 .last_tx_done
= ti_msgmgr_last_tx_done
,
475 .send_data
= ti_msgmgr_send_data
,
478 /* Keystone K2G SoC integration details */
479 static const struct ti_msgmgr_valid_queue_desc k2g_valid_queues
[] = {
480 {.queue_id
= 0, .proxy_id
= 0, .is_tx
= true,},
481 {.queue_id
= 1, .proxy_id
= 0, .is_tx
= true,},
482 {.queue_id
= 2, .proxy_id
= 0, .is_tx
= true,},
483 {.queue_id
= 3, .proxy_id
= 0, .is_tx
= true,},
484 {.queue_id
= 5, .proxy_id
= 2, .is_tx
= false,},
485 {.queue_id
= 56, .proxy_id
= 1, .is_tx
= true,},
486 {.queue_id
= 57, .proxy_id
= 2, .is_tx
= false,},
487 {.queue_id
= 58, .proxy_id
= 3, .is_tx
= true,},
488 {.queue_id
= 59, .proxy_id
= 4, .is_tx
= true,},
489 {.queue_id
= 60, .proxy_id
= 5, .is_tx
= true,},
490 {.queue_id
= 61, .proxy_id
= 6, .is_tx
= true,},
493 static const struct ti_msgmgr_desc k2g_desc
= {
495 .max_message_size
= 64,
499 .data_first_reg
= 16,
502 .valid_queues
= k2g_valid_queues
,
503 .num_valid_queues
= ARRAY_SIZE(k2g_valid_queues
),
506 static const struct of_device_id ti_msgmgr_of_match
[] = {
507 {.compatible
= "ti,k2g-message-manager", .data
= &k2g_desc
},
510 MODULE_DEVICE_TABLE(of
, ti_msgmgr_of_match
);
512 static int ti_msgmgr_probe(struct platform_device
*pdev
)
514 struct device
*dev
= &pdev
->dev
;
515 const struct of_device_id
*of_id
;
516 struct device_node
*np
;
517 struct resource
*res
;
518 const struct ti_msgmgr_desc
*desc
;
519 struct ti_msgmgr_inst
*inst
;
520 struct ti_queue_inst
*qinst
;
521 struct mbox_controller
*mbox
;
522 struct mbox_chan
*chans
;
526 const struct ti_msgmgr_valid_queue_desc
*queue_desc
;
529 dev_err(dev
, "no OF information\n");
534 of_id
= of_match_device(ti_msgmgr_of_match
, dev
);
536 dev_err(dev
, "OF data missing\n");
541 inst
= devm_kzalloc(dev
, sizeof(*inst
), GFP_KERNEL
);
548 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
549 "queue_proxy_region");
550 inst
->queue_proxy_region
= devm_ioremap_resource(dev
, res
);
551 if (IS_ERR(inst
->queue_proxy_region
))
552 return PTR_ERR(inst
->queue_proxy_region
);
554 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
555 "queue_state_debug_region");
556 inst
->queue_state_debug_region
= devm_ioremap_resource(dev
, res
);
557 if (IS_ERR(inst
->queue_state_debug_region
))
558 return PTR_ERR(inst
->queue_state_debug_region
);
560 dev_dbg(dev
, "proxy region=%p, queue_state=%p\n",
561 inst
->queue_proxy_region
, inst
->queue_state_debug_region
);
563 queue_count
= desc
->num_valid_queues
;
564 if (!queue_count
|| queue_count
> desc
->queue_count
) {
565 dev_crit(dev
, "Invalid Number of queues %d. Max %d\n",
566 queue_count
, desc
->queue_count
);
569 inst
->num_valid_queues
= queue_count
;
571 qinst
= devm_kcalloc(dev
, queue_count
, sizeof(*qinst
), GFP_KERNEL
);
574 inst
->qinsts
= qinst
;
576 chans
= devm_kcalloc(dev
, queue_count
, sizeof(*chans
), GFP_KERNEL
);
581 for (i
= 0, queue_desc
= desc
->valid_queues
;
582 i
< queue_count
; i
++, qinst
++, chans
++, queue_desc
++) {
583 ret
= ti_msgmgr_queue_setup(i
, dev
, np
, inst
,
584 desc
, queue_desc
, qinst
, chans
);
591 mbox
->ops
= &ti_msgmgr_chan_ops
;
592 mbox
->chans
= inst
->chans
;
593 mbox
->num_chans
= inst
->num_valid_queues
;
594 mbox
->txdone_irq
= false;
595 mbox
->txdone_poll
= desc
->tx_polled
;
597 mbox
->txpoll_period
= desc
->tx_poll_timeout_ms
;
598 mbox
->of_xlate
= ti_msgmgr_of_xlate
;
600 platform_set_drvdata(pdev
, inst
);
601 ret
= mbox_controller_register(mbox
);
603 dev_err(dev
, "Failed to register mbox_controller(%d)\n", ret
);
608 static int ti_msgmgr_remove(struct platform_device
*pdev
)
610 struct ti_msgmgr_inst
*inst
;
612 inst
= platform_get_drvdata(pdev
);
613 mbox_controller_unregister(&inst
->mbox
);
618 static struct platform_driver ti_msgmgr_driver
= {
619 .probe
= ti_msgmgr_probe
,
620 .remove
= ti_msgmgr_remove
,
623 .of_match_table
= of_match_ptr(ti_msgmgr_of_match
),
626 module_platform_driver(ti_msgmgr_driver
);
628 MODULE_LICENSE("GPL v2");
629 MODULE_DESCRIPTION("TI message manager driver");
630 MODULE_AUTHOR("Nishanth Menon");
631 MODULE_ALIAS("platform:ti-msgmgr");