ti-msgmgr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments' Message Manager Driver
  4. *
  5. * Copyright (C) 2015-2022 Texas Instruments Incorporated - https://www.ti.com/
  6. * Nishanth Menon
  7. */
  8. #define pr_fmt(fmt) "%s: " fmt, __func__
  9. #include <linux/device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mailbox_controller.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/soc/ti/ti-msgmgr.h>
  21. #define Q_DATA_OFFSET(proxy, queue, reg) \
  22. ((0x10000 * (proxy)) + (0x80 * (queue)) + ((reg) * 4))
  23. #define Q_STATE_OFFSET(queue) ((queue) * 0x4)
  24. #define Q_STATE_ENTRY_COUNT_MASK (0xFFF000)
  25. #define SPROXY_THREAD_OFFSET(tid) (0x1000 * (tid))
  26. #define SPROXY_THREAD_DATA_OFFSET(tid, reg) \
  27. (SPROXY_THREAD_OFFSET(tid) + ((reg) * 0x4) + 0x4)
  28. #define SPROXY_THREAD_STATUS_OFFSET(tid) (SPROXY_THREAD_OFFSET(tid))
  29. #define SPROXY_THREAD_STATUS_COUNT_MASK (0xFF)
  30. #define SPROXY_THREAD_CTRL_OFFSET(tid) (0x1000 + SPROXY_THREAD_OFFSET(tid))
  31. #define SPROXY_THREAD_CTRL_DIR_MASK (0x1 << 31)
  32. /**
  33. * struct ti_msgmgr_valid_queue_desc - SoC valid queues meant for this processor
  34. * @queue_id: Queue Number for this path
  35. * @proxy_id: Proxy ID representing the processor in SoC
  36. * @is_tx: Is this a receive path?
  37. */
  38. struct ti_msgmgr_valid_queue_desc {
  39. u8 queue_id;
  40. u8 proxy_id;
  41. bool is_tx;
  42. };
  43. /**
  44. * struct ti_msgmgr_desc - Description of message manager integration
  45. * @queue_count: Number of Queues
  46. * @max_message_size: Message size in bytes
  47. * @max_messages: Number of messages
  48. * @data_first_reg: First data register for proxy data region
  49. * @data_last_reg: Last data register for proxy data region
  50. * @status_cnt_mask: Mask for getting the status value
  51. * @status_err_mask: Mask for getting the error value, if applicable
  52. * @tx_polled: Do I need to use polled mechanism for tx
  53. * @tx_poll_timeout_ms: Timeout in ms if polled
  54. * @valid_queues: List of Valid queues that the processor can access
  55. * @data_region_name: Name of the proxy data region
  56. * @status_region_name: Name of the proxy status region
  57. * @ctrl_region_name: Name of the proxy control region
  58. * @num_valid_queues: Number of valid queues
  59. * @is_sproxy: Is this an Secure Proxy instance?
  60. *
  61. * This structure is used in of match data to describe how integration
  62. * for a specific compatible SoC is done.
  63. */
  64. struct ti_msgmgr_desc {
  65. u8 queue_count;
  66. u8 max_message_size;
  67. u8 max_messages;
  68. u8 data_first_reg;
  69. u8 data_last_reg;
  70. u32 status_cnt_mask;
  71. u32 status_err_mask;
  72. bool tx_polled;
  73. int tx_poll_timeout_ms;
  74. const struct ti_msgmgr_valid_queue_desc *valid_queues;
  75. const char *data_region_name;
  76. const char *status_region_name;
  77. const char *ctrl_region_name;
  78. int num_valid_queues;
  79. bool is_sproxy;
  80. };
  81. /**
  82. * struct ti_queue_inst - Description of a queue instance
  83. * @name: Queue Name
  84. * @queue_id: Queue Identifier as mapped on SoC
  85. * @proxy_id: Proxy Identifier as mapped on SoC
  86. * @irq: IRQ for Rx Queue
  87. * @is_tx: 'true' if transmit queue, else, 'false'
  88. * @queue_buff_start: First register of Data Buffer
  89. * @queue_buff_end: Last (or confirmation) register of Data buffer
  90. * @queue_state: Queue status register
  91. * @queue_ctrl: Queue Control register
  92. * @chan: Mailbox channel
  93. * @rx_buff: Receive buffer pointer allocated at probe, max_message_size
  94. * @polled_rx_mode: Use polling for rx instead of interrupts
  95. */
  96. struct ti_queue_inst {
  97. char name[30];
  98. u8 queue_id;
  99. u8 proxy_id;
  100. int irq;
  101. bool is_tx;
  102. void __iomem *queue_buff_start;
  103. void __iomem *queue_buff_end;
  104. void __iomem *queue_state;
  105. void __iomem *queue_ctrl;
  106. struct mbox_chan *chan;
  107. u32 *rx_buff;
  108. bool polled_rx_mode;
  109. };
  110. /**
  111. * struct ti_msgmgr_inst - Description of a Message Manager Instance
  112. * @dev: device pointer corresponding to the Message Manager instance
  113. * @desc: Description of the SoC integration
  114. * @queue_proxy_region: Queue proxy region where queue buffers are located
  115. * @queue_state_debug_region: Queue status register regions
  116. * @queue_ctrl_region: Queue Control register regions
  117. * @num_valid_queues: Number of valid queues defined for the processor
  118. * Note: other queues are probably reserved for other processors
  119. * in the SoC.
  120. * @qinsts: Array of valid Queue Instances for the Processor
  121. * @mbox: Mailbox Controller
  122. * @chans: Array for channels corresponding to the Queue Instances.
  123. */
  124. struct ti_msgmgr_inst {
  125. struct device *dev;
  126. const struct ti_msgmgr_desc *desc;
  127. void __iomem *queue_proxy_region;
  128. void __iomem *queue_state_debug_region;
  129. void __iomem *queue_ctrl_region;
  130. u8 num_valid_queues;
  131. struct ti_queue_inst *qinsts;
  132. struct mbox_controller mbox;
  133. struct mbox_chan *chans;
  134. };
  135. /**
  136. * ti_msgmgr_queue_get_num_messages() - Get the number of pending messages
  137. * @d: Description of message manager
  138. * @qinst: Queue instance for which we check the number of pending messages
  139. *
  140. * Return: number of messages pending in the queue (0 == no pending messages)
  141. */
  142. static inline int
  143. ti_msgmgr_queue_get_num_messages(const struct ti_msgmgr_desc *d,
  144. struct ti_queue_inst *qinst)
  145. {
  146. u32 val;
  147. u32 status_cnt_mask = d->status_cnt_mask;
  148. /*
  149. * We cannot use relaxed operation here - update may happen
  150. * real-time.
  151. */
  152. val = readl(qinst->queue_state) & status_cnt_mask;
  153. val >>= __ffs(status_cnt_mask);
  154. return val;
  155. }
  156. /**
  157. * ti_msgmgr_queue_is_error() - Check to see if there is queue error
  158. * @d: Description of message manager
  159. * @qinst: Queue instance for which we check the number of pending messages
  160. *
  161. * Return: true if error, else false
  162. */
  163. static inline bool ti_msgmgr_queue_is_error(const struct ti_msgmgr_desc *d,
  164. struct ti_queue_inst *qinst)
  165. {
  166. u32 val;
  167. /* Msgmgr has no error detection */
  168. if (!d->is_sproxy)
  169. return false;
  170. /*
  171. * We cannot use relaxed operation here - update may happen
  172. * real-time.
  173. */
  174. val = readl(qinst->queue_state) & d->status_err_mask;
  175. return val ? true : false;
  176. }
  177. static int ti_msgmgr_queue_rx_data(struct mbox_chan *chan, struct ti_queue_inst *qinst,
  178. const struct ti_msgmgr_desc *desc)
  179. {
  180. int num_words;
  181. struct ti_msgmgr_message message;
  182. void __iomem *data_reg;
  183. u32 *word_data;
  184. /*
  185. * I have no idea about the protocol being used to communicate with the
  186. * remote producer - 0 could be valid data, so I wont make a judgement
  187. * of how many bytes I should be reading. Let the client figure this
  188. * out.. I just read the full message and pass it on..
  189. */
  190. message.len = desc->max_message_size;
  191. message.buf = (u8 *)qinst->rx_buff;
  192. /*
  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.
  202. *
  203. * Also note that the final register read automatically marks the
  204. * queue message as read.
  205. */
  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);
  210. /*
  211. * Last register read automatically clears the IRQ if only 1 message
  212. * is pending - so send the data up the stack..
  213. * NOTE: Client is expected to be as optimal as possible, since
  214. * we invoke the handler in IRQ context.
  215. */
  216. mbox_chan_received_data(chan, (void *)&message);
  217. return 0;
  218. }
  219. static int ti_msgmgr_queue_rx_poll_timeout(struct mbox_chan *chan, int timeout_us)
  220. {
  221. struct device *dev = chan->mbox->dev;
  222. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  223. struct ti_queue_inst *qinst = chan->con_priv;
  224. const struct ti_msgmgr_desc *desc = inst->desc;
  225. int msg_count;
  226. int ret;
  227. ret = readl_poll_timeout_atomic(qinst->queue_state, msg_count,
  228. (msg_count & desc->status_cnt_mask),
  229. 10, timeout_us);
  230. if (ret != 0)
  231. return ret;
  232. ti_msgmgr_queue_rx_data(chan, qinst, desc);
  233. return 0;
  234. }
  235. /**
  236. * ti_msgmgr_queue_rx_interrupt() - Interrupt handler for receive Queue
  237. * @irq: Interrupt number
  238. * @p: Channel Pointer
  239. *
  240. * Return: -EINVAL if there is no instance
  241. * IRQ_NONE if the interrupt is not ours.
  242. * IRQ_HANDLED if the rx interrupt was successfully handled.
  243. */
  244. static irqreturn_t ti_msgmgr_queue_rx_interrupt(int irq, void *p)
  245. {
  246. struct mbox_chan *chan = p;
  247. struct device *dev = chan->mbox->dev;
  248. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  249. struct ti_queue_inst *qinst = chan->con_priv;
  250. const struct ti_msgmgr_desc *desc;
  251. int msg_count;
  252. if (WARN_ON(!inst)) {
  253. dev_err(dev, "no platform drv data??\n");
  254. return -EINVAL;
  255. }
  256. /* Do I have an invalid interrupt source? */
  257. if (qinst->is_tx) {
  258. dev_err(dev, "Cannot handle rx interrupt on tx channel %s\n",
  259. qinst->name);
  260. return IRQ_NONE;
  261. }
  262. desc = inst->desc;
  263. if (ti_msgmgr_queue_is_error(desc, qinst)) {
  264. dev_err(dev, "Error on Rx channel %s\n", qinst->name);
  265. return IRQ_NONE;
  266. }
  267. /* Do I actually have messages to read? */
  268. msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
  269. if (!msg_count) {
  270. /* Shared IRQ? */
  271. dev_dbg(dev, "Spurious event - 0 pending data!\n");
  272. return IRQ_NONE;
  273. }
  274. ti_msgmgr_queue_rx_data(chan, qinst, desc);
  275. return IRQ_HANDLED;
  276. }
  277. /**
  278. * ti_msgmgr_queue_peek_data() - Peek to see if there are any rx messages.
  279. * @chan: Channel Pointer
  280. *
  281. * Return: 'true' if there is pending rx data, 'false' if there is none.
  282. */
  283. static bool ti_msgmgr_queue_peek_data(struct mbox_chan *chan)
  284. {
  285. struct ti_queue_inst *qinst = chan->con_priv;
  286. struct device *dev = chan->mbox->dev;
  287. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  288. const struct ti_msgmgr_desc *desc = inst->desc;
  289. int msg_count;
  290. if (qinst->is_tx)
  291. return false;
  292. if (ti_msgmgr_queue_is_error(desc, qinst)) {
  293. dev_err(dev, "Error on channel %s\n", qinst->name);
  294. return false;
  295. }
  296. msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
  297. return msg_count ? true : false;
  298. }
  299. /**
  300. * ti_msgmgr_last_tx_done() - See if all the tx messages are sent
  301. * @chan: Channel pointer
  302. *
  303. * Return: 'true' is no pending tx data, 'false' if there are any.
  304. */
  305. static bool ti_msgmgr_last_tx_done(struct mbox_chan *chan)
  306. {
  307. struct ti_queue_inst *qinst = chan->con_priv;
  308. struct device *dev = chan->mbox->dev;
  309. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  310. const struct ti_msgmgr_desc *desc = inst->desc;
  311. int msg_count;
  312. if (!qinst->is_tx)
  313. return false;
  314. if (ti_msgmgr_queue_is_error(desc, qinst)) {
  315. dev_err(dev, "Error on channel %s\n", qinst->name);
  316. return false;
  317. }
  318. msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
  319. if (desc->is_sproxy) {
  320. /* In secure proxy, msg_count indicates how many we can send */
  321. return msg_count ? true : false;
  322. }
  323. /* if we have any messages pending.. */
  324. return msg_count ? false : true;
  325. }
  326. static bool ti_msgmgr_chan_has_polled_queue_rx(struct mbox_chan *chan)
  327. {
  328. struct ti_queue_inst *qinst;
  329. if (!chan)
  330. return false;
  331. qinst = chan->con_priv;
  332. return qinst->polled_rx_mode;
  333. }
  334. /**
  335. * ti_msgmgr_send_data() - Send data
  336. * @chan: Channel Pointer
  337. * @data: ti_msgmgr_message * Message Pointer
  338. *
  339. * Return: 0 if all goes good, else appropriate error messages.
  340. */
  341. static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
  342. {
  343. struct device *dev = chan->mbox->dev;
  344. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  345. const struct ti_msgmgr_desc *desc;
  346. struct ti_queue_inst *qinst = chan->con_priv;
  347. int num_words, trail_bytes;
  348. struct ti_msgmgr_message *message = data;
  349. void __iomem *data_reg;
  350. u32 *word_data;
  351. int ret = 0;
  352. if (WARN_ON(!inst)) {
  353. dev_err(dev, "no platform drv data??\n");
  354. return -EINVAL;
  355. }
  356. desc = inst->desc;
  357. if (ti_msgmgr_queue_is_error(desc, qinst)) {
  358. dev_err(dev, "Error on channel %s\n", qinst->name);
  359. return false;
  360. }
  361. if (desc->max_message_size < message->len) {
  362. dev_err(dev, "Queue %s message length %zu > max %d\n",
  363. qinst->name, message->len, desc->max_message_size);
  364. return -EINVAL;
  365. }
  366. /* NOTE: Constraints similar to rx path exists here as well */
  367. for (data_reg = qinst->queue_buff_start,
  368. num_words = message->len / sizeof(u32),
  369. word_data = (u32 *)message->buf;
  370. num_words; num_words--, data_reg += sizeof(u32), word_data++)
  371. writel(*word_data, data_reg);
  372. trail_bytes = message->len % sizeof(u32);
  373. if (trail_bytes) {
  374. u32 data_trail = *word_data;
  375. /* Ensure all unused data is 0 */
  376. data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
  377. writel(data_trail, data_reg);
  378. data_reg += sizeof(u32);
  379. }
  380. /*
  381. * 'data_reg' indicates next register to write. If we did not already
  382. * write on tx complete reg(last reg), we must do so for transmit
  383. * In addition, we also need to make sure all intermediate data
  384. * registers(if any required), are reset to 0 for TISCI backward
  385. * compatibility to be maintained.
  386. */
  387. while (data_reg <= qinst->queue_buff_end) {
  388. writel(0, data_reg);
  389. data_reg += sizeof(u32);
  390. }
  391. /* If we are in polled mode, wait for a response before proceeding */
  392. if (ti_msgmgr_chan_has_polled_queue_rx(message->chan_rx))
  393. ret = ti_msgmgr_queue_rx_poll_timeout(message->chan_rx,
  394. message->timeout_rx_ms * 1000);
  395. return ret;
  396. }
  397. /**
  398. * ti_msgmgr_queue_rx_irq_req() - RX IRQ request
  399. * @dev: device pointer
  400. * @d: descriptor for ti_msgmgr
  401. * @qinst: Queue instance
  402. * @chan: Channel pointer
  403. */
  404. static int ti_msgmgr_queue_rx_irq_req(struct device *dev,
  405. const struct ti_msgmgr_desc *d,
  406. struct ti_queue_inst *qinst,
  407. struct mbox_chan *chan)
  408. {
  409. int ret = 0;
  410. char of_rx_irq_name[7];
  411. struct device_node *np;
  412. snprintf(of_rx_irq_name, sizeof(of_rx_irq_name),
  413. "rx_%03d", d->is_sproxy ? qinst->proxy_id : qinst->queue_id);
  414. /* Get the IRQ if not found */
  415. if (qinst->irq < 0) {
  416. np = of_node_get(dev->of_node);
  417. if (!np)
  418. return -ENODATA;
  419. qinst->irq = of_irq_get_byname(np, of_rx_irq_name);
  420. of_node_put(np);
  421. if (qinst->irq < 0) {
  422. dev_err(dev,
  423. "QID %d PID %d:No IRQ[%s]: %d\n",
  424. qinst->queue_id, qinst->proxy_id,
  425. of_rx_irq_name, qinst->irq);
  426. return qinst->irq;
  427. }
  428. }
  429. /* With the expectation that the IRQ might be shared in SoC */
  430. ret = request_irq(qinst->irq, ti_msgmgr_queue_rx_interrupt,
  431. IRQF_SHARED, qinst->name, chan);
  432. if (ret) {
  433. dev_err(dev, "Unable to get IRQ %d on %s(res=%d)\n",
  434. qinst->irq, qinst->name, ret);
  435. }
  436. return ret;
  437. }
  438. /**
  439. * ti_msgmgr_queue_startup() - Startup queue
  440. * @chan: Channel pointer
  441. *
  442. * Return: 0 if all goes good, else return corresponding error message
  443. */
  444. static int ti_msgmgr_queue_startup(struct mbox_chan *chan)
  445. {
  446. struct device *dev = chan->mbox->dev;
  447. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  448. struct ti_queue_inst *qinst = chan->con_priv;
  449. const struct ti_msgmgr_desc *d = inst->desc;
  450. int ret;
  451. int msg_count;
  452. /*
  453. * If sproxy is starting and can send messages, we are a Tx thread,
  454. * else Rx
  455. */
  456. if (d->is_sproxy) {
  457. qinst->is_tx = (readl(qinst->queue_ctrl) &
  458. SPROXY_THREAD_CTRL_DIR_MASK) ? false : true;
  459. msg_count = ti_msgmgr_queue_get_num_messages(d, qinst);
  460. if (!msg_count && qinst->is_tx) {
  461. dev_err(dev, "%s: Cannot transmit with 0 credits!\n",
  462. qinst->name);
  463. return -EINVAL;
  464. }
  465. }
  466. if (!qinst->is_tx) {
  467. /* Allocate usage buffer for rx */
  468. qinst->rx_buff = kzalloc(d->max_message_size, GFP_KERNEL);
  469. if (!qinst->rx_buff)
  470. return -ENOMEM;
  471. /* Request IRQ */
  472. ret = ti_msgmgr_queue_rx_irq_req(dev, d, qinst, chan);
  473. if (ret) {
  474. kfree(qinst->rx_buff);
  475. return ret;
  476. }
  477. }
  478. return 0;
  479. }
  480. /**
  481. * ti_msgmgr_queue_shutdown() - Shutdown the queue
  482. * @chan: Channel pointer
  483. */
  484. static void ti_msgmgr_queue_shutdown(struct mbox_chan *chan)
  485. {
  486. struct ti_queue_inst *qinst = chan->con_priv;
  487. if (!qinst->is_tx) {
  488. free_irq(qinst->irq, chan);
  489. kfree(qinst->rx_buff);
  490. }
  491. }
  492. /**
  493. * ti_msgmgr_of_xlate() - Translation of phandle to queue
  494. * @mbox: Mailbox controller
  495. * @p: phandle pointer
  496. *
  497. * Return: Mailbox channel corresponding to the queue, else return error
  498. * pointer.
  499. */
  500. static struct mbox_chan *ti_msgmgr_of_xlate(struct mbox_controller *mbox,
  501. const struct of_phandle_args *p)
  502. {
  503. struct ti_msgmgr_inst *inst;
  504. int req_qid, req_pid;
  505. struct ti_queue_inst *qinst;
  506. const struct ti_msgmgr_desc *d;
  507. int i, ncells;
  508. inst = container_of(mbox, struct ti_msgmgr_inst, mbox);
  509. if (WARN_ON(!inst))
  510. return ERR_PTR(-EINVAL);
  511. d = inst->desc;
  512. if (d->is_sproxy)
  513. ncells = 1;
  514. else
  515. ncells = 2;
  516. if (p->args_count != ncells) {
  517. dev_err(inst->dev, "Invalid arguments in dt[%d]. Must be %d\n",
  518. p->args_count, ncells);
  519. return ERR_PTR(-EINVAL);
  520. }
  521. if (ncells == 1) {
  522. req_qid = 0;
  523. req_pid = p->args[0];
  524. } else {
  525. req_qid = p->args[0];
  526. req_pid = p->args[1];
  527. }
  528. if (d->is_sproxy) {
  529. if (req_pid >= d->num_valid_queues)
  530. goto err;
  531. qinst = &inst->qinsts[req_pid];
  532. return qinst->chan;
  533. }
  534. for (qinst = inst->qinsts, i = 0; i < inst->num_valid_queues;
  535. i++, qinst++) {
  536. if (req_qid == qinst->queue_id && req_pid == qinst->proxy_id)
  537. return qinst->chan;
  538. }
  539. err:
  540. dev_err(inst->dev, "Queue ID %d, Proxy ID %d is wrong on %pOFn\n",
  541. req_qid, req_pid, p->np);
  542. return ERR_PTR(-ENOENT);
  543. }
  544. /**
  545. * ti_msgmgr_queue_setup() - Setup data structures for each queue instance
  546. * @idx: index of the queue
  547. * @dev: pointer to the message manager device
  548. * @np: pointer to the of node
  549. * @inst: Queue instance pointer
  550. * @d: Message Manager instance description data
  551. * @qd: Queue description data
  552. * @qinst: Queue instance pointer
  553. * @chan: pointer to mailbox channel
  554. *
  555. * Return: 0 if all went well, else return corresponding error
  556. */
  557. static int ti_msgmgr_queue_setup(int idx, struct device *dev,
  558. struct device_node *np,
  559. struct ti_msgmgr_inst *inst,
  560. const struct ti_msgmgr_desc *d,
  561. const struct ti_msgmgr_valid_queue_desc *qd,
  562. struct ti_queue_inst *qinst,
  563. struct mbox_chan *chan)
  564. {
  565. char *dir;
  566. qinst->proxy_id = qd->proxy_id;
  567. qinst->queue_id = qd->queue_id;
  568. if (qinst->queue_id > d->queue_count) {
  569. dev_err(dev, "Queue Data [idx=%d] queuid %d > %d\n",
  570. idx, qinst->queue_id, d->queue_count);
  571. return -ERANGE;
  572. }
  573. if (d->is_sproxy) {
  574. qinst->queue_buff_start = inst->queue_proxy_region +
  575. SPROXY_THREAD_DATA_OFFSET(qinst->proxy_id,
  576. d->data_first_reg);
  577. qinst->queue_buff_end = inst->queue_proxy_region +
  578. SPROXY_THREAD_DATA_OFFSET(qinst->proxy_id,
  579. d->data_last_reg);
  580. qinst->queue_state = inst->queue_state_debug_region +
  581. SPROXY_THREAD_STATUS_OFFSET(qinst->proxy_id);
  582. qinst->queue_ctrl = inst->queue_ctrl_region +
  583. SPROXY_THREAD_CTRL_OFFSET(qinst->proxy_id);
  584. /* XXX: DONOT read registers here!.. Some may be unusable */
  585. dir = "thr";
  586. snprintf(qinst->name, sizeof(qinst->name), "%s %s_%03d",
  587. dev_name(dev), dir, qinst->proxy_id);
  588. } else {
  589. qinst->queue_buff_start = inst->queue_proxy_region +
  590. Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id,
  591. d->data_first_reg);
  592. qinst->queue_buff_end = inst->queue_proxy_region +
  593. Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id,
  594. d->data_last_reg);
  595. qinst->queue_state =
  596. inst->queue_state_debug_region +
  597. Q_STATE_OFFSET(qinst->queue_id);
  598. qinst->is_tx = qd->is_tx;
  599. dir = qinst->is_tx ? "tx" : "rx";
  600. snprintf(qinst->name, sizeof(qinst->name), "%s %s_%03d_%03d",
  601. dev_name(dev), dir, qinst->queue_id, qinst->proxy_id);
  602. }
  603. qinst->chan = chan;
  604. /* Setup an error value for IRQ - Lazy allocation */
  605. qinst->irq = -EINVAL;
  606. chan->con_priv = qinst;
  607. dev_dbg(dev, "[%d] qidx=%d pidx=%d irq=%d q_s=%p q_e = %p\n",
  608. idx, qinst->queue_id, qinst->proxy_id, qinst->irq,
  609. qinst->queue_buff_start, qinst->queue_buff_end);
  610. return 0;
  611. }
  612. static int ti_msgmgr_queue_rx_set_polled_mode(struct ti_queue_inst *qinst, bool enable)
  613. {
  614. if (enable) {
  615. disable_irq(qinst->irq);
  616. qinst->polled_rx_mode = true;
  617. } else {
  618. enable_irq(qinst->irq);
  619. qinst->polled_rx_mode = false;
  620. }
  621. return 0;
  622. }
  623. static int ti_msgmgr_suspend(struct device *dev)
  624. {
  625. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  626. struct ti_queue_inst *qinst;
  627. int i;
  628. /*
  629. * We must switch operation to polled mode now as drivers and the genpd
  630. * layer may make late TI SCI calls to change clock and device states
  631. * from the noirq phase of suspend.
  632. */
  633. for (qinst = inst->qinsts, i = 0; i < inst->num_valid_queues; qinst++, i++) {
  634. if (!qinst->is_tx)
  635. ti_msgmgr_queue_rx_set_polled_mode(qinst, true);
  636. }
  637. return 0;
  638. }
  639. static int ti_msgmgr_resume(struct device *dev)
  640. {
  641. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  642. struct ti_queue_inst *qinst;
  643. int i;
  644. for (qinst = inst->qinsts, i = 0; i < inst->num_valid_queues; qinst++, i++) {
  645. if (!qinst->is_tx)
  646. ti_msgmgr_queue_rx_set_polled_mode(qinst, false);
  647. }
  648. return 0;
  649. }
  650. static DEFINE_SIMPLE_DEV_PM_OPS(ti_msgmgr_pm_ops, ti_msgmgr_suspend, ti_msgmgr_resume);
  651. /* Queue operations */
  652. static const struct mbox_chan_ops ti_msgmgr_chan_ops = {
  653. .startup = ti_msgmgr_queue_startup,
  654. .shutdown = ti_msgmgr_queue_shutdown,
  655. .peek_data = ti_msgmgr_queue_peek_data,
  656. .last_tx_done = ti_msgmgr_last_tx_done,
  657. .send_data = ti_msgmgr_send_data,
  658. };
  659. /* Keystone K2G SoC integration details */
  660. static const struct ti_msgmgr_valid_queue_desc k2g_valid_queues[] = {
  661. {.queue_id = 0, .proxy_id = 0, .is_tx = true,},
  662. {.queue_id = 1, .proxy_id = 0, .is_tx = true,},
  663. {.queue_id = 2, .proxy_id = 0, .is_tx = true,},
  664. {.queue_id = 3, .proxy_id = 0, .is_tx = true,},
  665. {.queue_id = 5, .proxy_id = 2, .is_tx = false,},
  666. {.queue_id = 56, .proxy_id = 1, .is_tx = true,},
  667. {.queue_id = 57, .proxy_id = 2, .is_tx = false,},
  668. {.queue_id = 58, .proxy_id = 3, .is_tx = true,},
  669. {.queue_id = 59, .proxy_id = 4, .is_tx = true,},
  670. {.queue_id = 60, .proxy_id = 5, .is_tx = true,},
  671. {.queue_id = 61, .proxy_id = 6, .is_tx = true,},
  672. };
  673. static const struct ti_msgmgr_desc k2g_desc = {
  674. .queue_count = 64,
  675. .max_message_size = 64,
  676. .max_messages = 128,
  677. .data_region_name = "queue_proxy_region",
  678. .status_region_name = "queue_state_debug_region",
  679. .data_first_reg = 16,
  680. .data_last_reg = 31,
  681. .status_cnt_mask = Q_STATE_ENTRY_COUNT_MASK,
  682. .tx_polled = false,
  683. .valid_queues = k2g_valid_queues,
  684. .num_valid_queues = ARRAY_SIZE(k2g_valid_queues),
  685. .is_sproxy = false,
  686. };
  687. static const struct ti_msgmgr_desc am654_desc = {
  688. .queue_count = 190,
  689. .num_valid_queues = 190,
  690. .max_message_size = 60,
  691. .data_region_name = "target_data",
  692. .status_region_name = "rt",
  693. .ctrl_region_name = "scfg",
  694. .data_first_reg = 0,
  695. .data_last_reg = 14,
  696. .status_cnt_mask = SPROXY_THREAD_STATUS_COUNT_MASK,
  697. .tx_polled = false,
  698. .is_sproxy = true,
  699. };
  700. static const struct of_device_id ti_msgmgr_of_match[] = {
  701. {.compatible = "ti,k2g-message-manager", .data = &k2g_desc},
  702. {.compatible = "ti,am654-secure-proxy", .data = &am654_desc},
  703. { /* Sentinel */ }
  704. };
  705. MODULE_DEVICE_TABLE(of, ti_msgmgr_of_match);
  706. static int ti_msgmgr_probe(struct platform_device *pdev)
  707. {
  708. struct device *dev = &pdev->dev;
  709. const struct of_device_id *of_id;
  710. struct device_node *np;
  711. struct resource *res;
  712. const struct ti_msgmgr_desc *desc;
  713. struct ti_msgmgr_inst *inst;
  714. struct ti_queue_inst *qinst;
  715. struct mbox_controller *mbox;
  716. struct mbox_chan *chans;
  717. int queue_count;
  718. int i;
  719. int ret = -EINVAL;
  720. const struct ti_msgmgr_valid_queue_desc *queue_desc;
  721. if (!dev->of_node) {
  722. dev_err(dev, "no OF information\n");
  723. return -EINVAL;
  724. }
  725. np = dev->of_node;
  726. of_id = of_match_device(ti_msgmgr_of_match, dev);
  727. if (!of_id) {
  728. dev_err(dev, "OF data missing\n");
  729. return -EINVAL;
  730. }
  731. desc = of_id->data;
  732. inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
  733. if (!inst)
  734. return -ENOMEM;
  735. inst->dev = dev;
  736. inst->desc = desc;
  737. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  738. desc->data_region_name);
  739. inst->queue_proxy_region = devm_ioremap_resource(dev, res);
  740. if (IS_ERR(inst->queue_proxy_region))
  741. return PTR_ERR(inst->queue_proxy_region);
  742. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  743. desc->status_region_name);
  744. inst->queue_state_debug_region = devm_ioremap_resource(dev, res);
  745. if (IS_ERR(inst->queue_state_debug_region))
  746. return PTR_ERR(inst->queue_state_debug_region);
  747. if (desc->is_sproxy) {
  748. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  749. desc->ctrl_region_name);
  750. inst->queue_ctrl_region = devm_ioremap_resource(dev, res);
  751. if (IS_ERR(inst->queue_ctrl_region))
  752. return PTR_ERR(inst->queue_ctrl_region);
  753. }
  754. dev_dbg(dev, "proxy region=%p, queue_state=%p\n",
  755. inst->queue_proxy_region, inst->queue_state_debug_region);
  756. queue_count = desc->num_valid_queues;
  757. if (!queue_count || queue_count > desc->queue_count) {
  758. dev_crit(dev, "Invalid Number of queues %d. Max %d\n",
  759. queue_count, desc->queue_count);
  760. return -ERANGE;
  761. }
  762. inst->num_valid_queues = queue_count;
  763. qinst = devm_kcalloc(dev, queue_count, sizeof(*qinst), GFP_KERNEL);
  764. if (!qinst)
  765. return -ENOMEM;
  766. inst->qinsts = qinst;
  767. chans = devm_kcalloc(dev, queue_count, sizeof(*chans), GFP_KERNEL);
  768. if (!chans)
  769. return -ENOMEM;
  770. inst->chans = chans;
  771. if (desc->is_sproxy) {
  772. struct ti_msgmgr_valid_queue_desc sproxy_desc;
  773. /* All proxies may be valid in Secure Proxy instance */
  774. for (i = 0; i < queue_count; i++, qinst++, chans++) {
  775. sproxy_desc.queue_id = 0;
  776. sproxy_desc.proxy_id = i;
  777. ret = ti_msgmgr_queue_setup(i, dev, np, inst,
  778. desc, &sproxy_desc, qinst,
  779. chans);
  780. if (ret)
  781. return ret;
  782. }
  783. } else {
  784. /* Only Some proxies are valid in Message Manager */
  785. for (i = 0, queue_desc = desc->valid_queues;
  786. i < queue_count; i++, qinst++, chans++, queue_desc++) {
  787. ret = ti_msgmgr_queue_setup(i, dev, np, inst,
  788. desc, queue_desc, qinst,
  789. chans);
  790. if (ret)
  791. return ret;
  792. }
  793. }
  794. mbox = &inst->mbox;
  795. mbox->dev = dev;
  796. mbox->ops = &ti_msgmgr_chan_ops;
  797. mbox->chans = inst->chans;
  798. mbox->num_chans = inst->num_valid_queues;
  799. mbox->txdone_irq = false;
  800. mbox->txdone_poll = desc->tx_polled;
  801. if (desc->tx_polled)
  802. mbox->txpoll_period = desc->tx_poll_timeout_ms;
  803. mbox->of_xlate = ti_msgmgr_of_xlate;
  804. platform_set_drvdata(pdev, inst);
  805. ret = devm_mbox_controller_register(dev, mbox);
  806. if (ret)
  807. dev_err(dev, "Failed to register mbox_controller(%d)\n", ret);
  808. return ret;
  809. }
  810. static struct platform_driver ti_msgmgr_driver = {
  811. .probe = ti_msgmgr_probe,
  812. .driver = {
  813. .name = "ti-msgmgr",
  814. .of_match_table = of_match_ptr(ti_msgmgr_of_match),
  815. .pm = &ti_msgmgr_pm_ops,
  816. },
  817. };
  818. module_platform_driver(ti_msgmgr_driver);
  819. MODULE_LICENSE("GPL v2");
  820. MODULE_DESCRIPTION("TI message manager driver");
  821. MODULE_AUTHOR("Nishanth Menon");
  822. MODULE_ALIAS("platform:ti-msgmgr");