zynqmp-ipi-mailbox.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx Inter Processor Interrupt(IPI) Mailbox Driver
  4. *
  5. * Copyright (C) 2018 Xilinx, Inc.
  6. */
  7. #include <linux/arm-smccc.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mailbox_controller.h>
  14. #include <linux/mailbox/zynqmp-ipi-message.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/platform_device.h>
  21. /* IPI agent ID any */
  22. #define IPI_ID_ANY 0xFFUL
  23. /* indicate if ZynqMP IPI mailbox driver uses SMC calls or HVC calls */
  24. #define USE_SMC 0
  25. #define USE_HVC 1
  26. /* Default IPI SMC function IDs */
  27. #define SMC_IPI_MAILBOX_OPEN 0x82001000U
  28. #define SMC_IPI_MAILBOX_RELEASE 0x82001001U
  29. #define SMC_IPI_MAILBOX_STATUS_ENQUIRY 0x82001002U
  30. #define SMC_IPI_MAILBOX_NOTIFY 0x82001003U
  31. #define SMC_IPI_MAILBOX_ACK 0x82001004U
  32. #define SMC_IPI_MAILBOX_ENABLE_IRQ 0x82001005U
  33. #define SMC_IPI_MAILBOX_DISABLE_IRQ 0x82001006U
  34. /* IPI SMC Macros */
  35. #define IPI_SMC_ENQUIRY_DIRQ_MASK 0x00000001UL /* Flag to indicate if
  36. * notification interrupt
  37. * to be disabled.
  38. */
  39. #define IPI_SMC_ACK_EIRQ_MASK 0x00000001UL /* Flag to indicate if
  40. * notification interrupt
  41. * to be enabled.
  42. */
  43. /* IPI mailbox status */
  44. #define IPI_MB_STATUS_IDLE 0
  45. #define IPI_MB_STATUS_SEND_PENDING 1
  46. #define IPI_MB_STATUS_RECV_PENDING 2
  47. #define IPI_MB_CHNL_TX 0 /* IPI mailbox TX channel */
  48. #define IPI_MB_CHNL_RX 1 /* IPI mailbox RX channel */
  49. /**
  50. * struct zynqmp_ipi_mchan - Description of a Xilinx ZynqMP IPI mailbox channel
  51. * @is_opened: indicate if the IPI channel is opened
  52. * @req_buf: local to remote request buffer start address
  53. * @resp_buf: local to remote response buffer start address
  54. * @req_buf_size: request buffer size
  55. * @resp_buf_size: response buffer size
  56. * @rx_buf: receive buffer to pass received message to client
  57. * @chan_type: channel type
  58. */
  59. struct zynqmp_ipi_mchan {
  60. int is_opened;
  61. void __iomem *req_buf;
  62. void __iomem *resp_buf;
  63. void *rx_buf;
  64. size_t req_buf_size;
  65. size_t resp_buf_size;
  66. unsigned int chan_type;
  67. };
  68. /**
  69. * struct zynqmp_ipi_mbox - Description of a ZynqMP IPI mailbox
  70. * platform data.
  71. * @pdata: pointer to the IPI private data
  72. * @dev: device pointer corresponding to the Xilinx ZynqMP
  73. * IPI mailbox
  74. * @remote_id: remote IPI agent ID
  75. * @mbox: mailbox Controller
  76. * @mchans: array for channels, tx channel and rx channel.
  77. * @irq: IPI agent interrupt ID
  78. */
  79. struct zynqmp_ipi_mbox {
  80. struct zynqmp_ipi_pdata *pdata;
  81. struct device dev;
  82. u32 remote_id;
  83. struct mbox_controller mbox;
  84. struct zynqmp_ipi_mchan mchans[2];
  85. };
  86. /**
  87. * struct zynqmp_ipi_pdata - Description of z ZynqMP IPI agent platform data.
  88. *
  89. * @dev: device pointer corresponding to the Xilinx ZynqMP
  90. * IPI agent
  91. * @irq: IPI agent interrupt ID
  92. * @method: IPI SMC or HVC is going to be used
  93. * @local_id: local IPI agent ID
  94. * @num_mboxes: number of mailboxes of this IPI agent
  95. * @ipi_mboxes: IPI mailboxes of this IPI agent
  96. */
  97. struct zynqmp_ipi_pdata {
  98. struct device *dev;
  99. int irq;
  100. unsigned int method;
  101. u32 local_id;
  102. int num_mboxes;
  103. struct zynqmp_ipi_mbox *ipi_mboxes;
  104. };
  105. static struct device_driver zynqmp_ipi_mbox_driver = {
  106. .owner = THIS_MODULE,
  107. .name = "zynqmp-ipi-mbox",
  108. };
  109. static void zynqmp_ipi_fw_call(struct zynqmp_ipi_mbox *ipi_mbox,
  110. unsigned long a0, unsigned long a3,
  111. struct arm_smccc_res *res)
  112. {
  113. struct zynqmp_ipi_pdata *pdata = ipi_mbox->pdata;
  114. unsigned long a1, a2;
  115. a1 = pdata->local_id;
  116. a2 = ipi_mbox->remote_id;
  117. if (pdata->method == USE_SMC)
  118. arm_smccc_smc(a0, a1, a2, a3, 0, 0, 0, 0, res);
  119. else
  120. arm_smccc_hvc(a0, a1, a2, a3, 0, 0, 0, 0, res);
  121. }
  122. /**
  123. * zynqmp_ipi_interrupt - Interrupt handler for IPI notification
  124. *
  125. * @irq: Interrupt number
  126. * @data: ZynqMP IPI mailbox platform data.
  127. *
  128. * Return: -EINVAL if there is no instance
  129. * IRQ_NONE if the interrupt is not ours.
  130. * IRQ_HANDLED if the rx interrupt was successfully handled.
  131. */
  132. static irqreturn_t zynqmp_ipi_interrupt(int irq, void *data)
  133. {
  134. struct zynqmp_ipi_pdata *pdata = data;
  135. struct mbox_chan *chan;
  136. struct zynqmp_ipi_mbox *ipi_mbox;
  137. struct zynqmp_ipi_mchan *mchan;
  138. struct zynqmp_ipi_message *msg;
  139. u64 arg0, arg3;
  140. struct arm_smccc_res res;
  141. int ret, i;
  142. (void)irq;
  143. arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
  144. arg3 = IPI_SMC_ENQUIRY_DIRQ_MASK;
  145. for (i = 0; i < pdata->num_mboxes; i++) {
  146. ipi_mbox = &pdata->ipi_mboxes[i];
  147. mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  148. chan = &ipi_mbox->mbox.chans[IPI_MB_CHNL_RX];
  149. zynqmp_ipi_fw_call(ipi_mbox, arg0, arg3, &res);
  150. ret = (int)(res.a0 & 0xFFFFFFFF);
  151. if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
  152. if (mchan->is_opened) {
  153. msg = mchan->rx_buf;
  154. msg->len = mchan->req_buf_size;
  155. memcpy_fromio(msg->data, mchan->req_buf,
  156. msg->len);
  157. mbox_chan_received_data(chan, (void *)msg);
  158. return IRQ_HANDLED;
  159. }
  160. }
  161. }
  162. return IRQ_NONE;
  163. }
  164. /**
  165. * zynqmp_ipi_peek_data - Peek to see if there are any rx messages.
  166. *
  167. * @chan: Channel Pointer
  168. *
  169. * Return: 'true' if there is pending rx data, 'false' if there is none.
  170. */
  171. static bool zynqmp_ipi_peek_data(struct mbox_chan *chan)
  172. {
  173. struct device *dev = chan->mbox->dev;
  174. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  175. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  176. int ret;
  177. u64 arg0;
  178. struct arm_smccc_res res;
  179. if (WARN_ON(!ipi_mbox)) {
  180. dev_err(dev, "no platform drv data??\n");
  181. return false;
  182. }
  183. arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
  184. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  185. ret = (int)(res.a0 & 0xFFFFFFFF);
  186. if (mchan->chan_type == IPI_MB_CHNL_TX) {
  187. /* TX channel, check if the message has been acked
  188. * by the remote, if yes, response is available.
  189. */
  190. if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
  191. return false;
  192. else
  193. return true;
  194. } else if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
  195. /* RX channel, check if there is message arrived. */
  196. return true;
  197. }
  198. return false;
  199. }
  200. /**
  201. * zynqmp_ipi_last_tx_done - See if the last tx message is sent
  202. *
  203. * @chan: Channel pointer
  204. *
  205. * Return: 'true' is no pending tx data, 'false' if there are any.
  206. */
  207. static bool zynqmp_ipi_last_tx_done(struct mbox_chan *chan)
  208. {
  209. struct device *dev = chan->mbox->dev;
  210. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  211. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  212. int ret;
  213. u64 arg0;
  214. struct arm_smccc_res res;
  215. if (WARN_ON(!ipi_mbox)) {
  216. dev_err(dev, "no platform drv data??\n");
  217. return false;
  218. }
  219. if (mchan->chan_type == IPI_MB_CHNL_TX) {
  220. /* We only need to check if the message been taken
  221. * by the remote in the TX channel
  222. */
  223. arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
  224. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  225. /* Check the SMC call status, a0 of the result */
  226. ret = (int)(res.a0 & 0xFFFFFFFF);
  227. if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
  228. return false;
  229. return true;
  230. }
  231. /* Always true for the response message in RX channel */
  232. return true;
  233. }
  234. /**
  235. * zynqmp_ipi_send_data - Send data
  236. *
  237. * @chan: Channel Pointer
  238. * @data: Message Pointer
  239. *
  240. * Return: 0 if all goes good, else appropriate error messages.
  241. */
  242. static int zynqmp_ipi_send_data(struct mbox_chan *chan, void *data)
  243. {
  244. struct device *dev = chan->mbox->dev;
  245. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  246. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  247. struct zynqmp_ipi_message *msg = data;
  248. u64 arg0;
  249. struct arm_smccc_res res;
  250. if (WARN_ON(!ipi_mbox)) {
  251. dev_err(dev, "no platform drv data??\n");
  252. return -EINVAL;
  253. }
  254. if (mchan->chan_type == IPI_MB_CHNL_TX) {
  255. /* Send request message */
  256. if (msg && msg->len > mchan->req_buf_size) {
  257. dev_err(dev, "channel %d message length %u > max %lu\n",
  258. mchan->chan_type, (unsigned int)msg->len,
  259. mchan->req_buf_size);
  260. return -EINVAL;
  261. }
  262. if (msg && msg->len)
  263. memcpy_toio(mchan->req_buf, msg->data, msg->len);
  264. /* Kick IPI mailbox to send message */
  265. arg0 = SMC_IPI_MAILBOX_NOTIFY;
  266. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  267. } else {
  268. /* Send response message */
  269. if (msg && msg->len > mchan->resp_buf_size) {
  270. dev_err(dev, "channel %d message length %u > max %lu\n",
  271. mchan->chan_type, (unsigned int)msg->len,
  272. mchan->resp_buf_size);
  273. return -EINVAL;
  274. }
  275. if (msg && msg->len)
  276. memcpy_toio(mchan->resp_buf, msg->data, msg->len);
  277. arg0 = SMC_IPI_MAILBOX_ACK;
  278. zynqmp_ipi_fw_call(ipi_mbox, arg0, IPI_SMC_ACK_EIRQ_MASK,
  279. &res);
  280. }
  281. return 0;
  282. }
  283. /**
  284. * zynqmp_ipi_startup - Startup the IPI channel
  285. *
  286. * @chan: Channel pointer
  287. *
  288. * Return: 0 if all goes good, else return corresponding error message
  289. */
  290. static int zynqmp_ipi_startup(struct mbox_chan *chan)
  291. {
  292. struct device *dev = chan->mbox->dev;
  293. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  294. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  295. u64 arg0;
  296. struct arm_smccc_res res;
  297. int ret = 0;
  298. unsigned int nchan_type;
  299. if (mchan->is_opened)
  300. return 0;
  301. /* If no channel has been opened, open the IPI mailbox */
  302. nchan_type = (mchan->chan_type + 1) % 2;
  303. if (!ipi_mbox->mchans[nchan_type].is_opened) {
  304. arg0 = SMC_IPI_MAILBOX_OPEN;
  305. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  306. /* Check the SMC call status, a0 of the result */
  307. ret = (int)(res.a0 & 0xFFFFFFFF);
  308. if (ret < 0) {
  309. dev_err(dev, "SMC to open the IPI channel failed.\n");
  310. return ret;
  311. }
  312. ret = 0;
  313. }
  314. /* If it is RX channel, enable the IPI notification interrupt */
  315. if (mchan->chan_type == IPI_MB_CHNL_RX) {
  316. arg0 = SMC_IPI_MAILBOX_ENABLE_IRQ;
  317. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  318. }
  319. mchan->is_opened = 1;
  320. return ret;
  321. }
  322. /**
  323. * zynqmp_ipi_shutdown - Shutdown the IPI channel
  324. *
  325. * @chan: Channel pointer
  326. */
  327. static void zynqmp_ipi_shutdown(struct mbox_chan *chan)
  328. {
  329. struct device *dev = chan->mbox->dev;
  330. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  331. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  332. u64 arg0;
  333. struct arm_smccc_res res;
  334. unsigned int chan_type;
  335. if (!mchan->is_opened)
  336. return;
  337. /* If it is RX channel, disable notification interrupt */
  338. chan_type = mchan->chan_type;
  339. if (chan_type == IPI_MB_CHNL_RX) {
  340. arg0 = SMC_IPI_MAILBOX_DISABLE_IRQ;
  341. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  342. }
  343. /* Release IPI mailbox if no other channel is opened */
  344. chan_type = (chan_type + 1) % 2;
  345. if (!ipi_mbox->mchans[chan_type].is_opened) {
  346. arg0 = SMC_IPI_MAILBOX_RELEASE;
  347. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  348. }
  349. mchan->is_opened = 0;
  350. }
  351. /* ZynqMP IPI mailbox operations */
  352. static const struct mbox_chan_ops zynqmp_ipi_chan_ops = {
  353. .startup = zynqmp_ipi_startup,
  354. .shutdown = zynqmp_ipi_shutdown,
  355. .peek_data = zynqmp_ipi_peek_data,
  356. .last_tx_done = zynqmp_ipi_last_tx_done,
  357. .send_data = zynqmp_ipi_send_data,
  358. };
  359. /**
  360. * zynqmp_ipi_of_xlate - Translate of phandle to IPI mailbox channel
  361. *
  362. * @mbox: mailbox controller pointer
  363. * @p: phandle pointer
  364. *
  365. * Return: Mailbox channel, else return error pointer.
  366. */
  367. static struct mbox_chan *zynqmp_ipi_of_xlate(struct mbox_controller *mbox,
  368. const struct of_phandle_args *p)
  369. {
  370. struct mbox_chan *chan;
  371. struct device *dev = mbox->dev;
  372. unsigned int chan_type;
  373. /* Only supports TX and RX channels */
  374. chan_type = p->args[0];
  375. if (chan_type != IPI_MB_CHNL_TX && chan_type != IPI_MB_CHNL_RX) {
  376. dev_err(dev, "req chnl failure: invalid chnl type %u.\n",
  377. chan_type);
  378. return ERR_PTR(-EINVAL);
  379. }
  380. chan = &mbox->chans[chan_type];
  381. return chan;
  382. }
  383. static const struct of_device_id zynqmp_ipi_of_match[] = {
  384. { .compatible = "xlnx,zynqmp-ipi-mailbox" },
  385. {},
  386. };
  387. MODULE_DEVICE_TABLE(of, zynqmp_ipi_of_match);
  388. /**
  389. * zynqmp_ipi_mbox_get_buf_res - Get buffer resource from the IPI dev node
  390. *
  391. * @node: IPI mbox device child node
  392. * @name: name of the IPI buffer
  393. * @res: pointer to where the resource information will be stored.
  394. *
  395. * Return: 0 for success, negative value for failure
  396. */
  397. static int zynqmp_ipi_mbox_get_buf_res(struct device_node *node,
  398. const char *name,
  399. struct resource *res)
  400. {
  401. int ret, index;
  402. index = of_property_match_string(node, "reg-names", name);
  403. if (index >= 0) {
  404. ret = of_address_to_resource(node, index, res);
  405. if (ret < 0)
  406. return -EINVAL;
  407. return 0;
  408. }
  409. return -ENODEV;
  410. }
  411. /**
  412. * zynqmp_ipi_mbox_dev_release() - release the existence of a ipi mbox dev
  413. *
  414. * @dev: the ipi mailbox device
  415. *
  416. * This is to avoid the no device release() function kernel warning.
  417. *
  418. */
  419. static void zynqmp_ipi_mbox_dev_release(struct device *dev)
  420. {
  421. (void)dev;
  422. }
  423. /**
  424. * zynqmp_ipi_mbox_probe - probe IPI mailbox resource from device node
  425. *
  426. * @ipi_mbox: pointer to IPI mailbox private data structure
  427. * @node: IPI mailbox device node
  428. *
  429. * Return: 0 for success, negative value for failure
  430. */
  431. static int zynqmp_ipi_mbox_probe(struct zynqmp_ipi_mbox *ipi_mbox,
  432. struct device_node *node)
  433. {
  434. struct zynqmp_ipi_mchan *mchan;
  435. struct mbox_chan *chans;
  436. struct mbox_controller *mbox;
  437. struct resource res;
  438. struct device *dev, *mdev;
  439. const char *name;
  440. int ret;
  441. dev = ipi_mbox->pdata->dev;
  442. /* Initialize dev for IPI mailbox */
  443. ipi_mbox->dev.parent = dev;
  444. ipi_mbox->dev.release = NULL;
  445. ipi_mbox->dev.of_node = node;
  446. dev_set_name(&ipi_mbox->dev, "%s", of_node_full_name(node));
  447. dev_set_drvdata(&ipi_mbox->dev, ipi_mbox);
  448. ipi_mbox->dev.release = zynqmp_ipi_mbox_dev_release;
  449. ipi_mbox->dev.driver = &zynqmp_ipi_mbox_driver;
  450. ret = device_register(&ipi_mbox->dev);
  451. if (ret) {
  452. dev_err(dev, "Failed to register ipi mbox dev.\n");
  453. return ret;
  454. }
  455. mdev = &ipi_mbox->dev;
  456. mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
  457. name = "local_request_region";
  458. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  459. if (!ret) {
  460. mchan->req_buf_size = resource_size(&res);
  461. mchan->req_buf = devm_ioremap(mdev, res.start,
  462. mchan->req_buf_size);
  463. if (!mchan->req_buf) {
  464. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  465. return -ENOMEM;
  466. }
  467. } else if (ret != -ENODEV) {
  468. dev_err(mdev, "Unmatched resource %s, %d.\n", name, ret);
  469. return ret;
  470. }
  471. name = "remote_response_region";
  472. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  473. if (!ret) {
  474. mchan->resp_buf_size = resource_size(&res);
  475. mchan->resp_buf = devm_ioremap(mdev, res.start,
  476. mchan->resp_buf_size);
  477. if (!mchan->resp_buf) {
  478. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  479. return -ENOMEM;
  480. }
  481. } else if (ret != -ENODEV) {
  482. dev_err(mdev, "Unmatched resource %s.\n", name);
  483. return ret;
  484. }
  485. mchan->rx_buf = devm_kzalloc(mdev,
  486. mchan->resp_buf_size +
  487. sizeof(struct zynqmp_ipi_message),
  488. GFP_KERNEL);
  489. if (!mchan->rx_buf)
  490. return -ENOMEM;
  491. mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  492. name = "remote_request_region";
  493. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  494. if (!ret) {
  495. mchan->req_buf_size = resource_size(&res);
  496. mchan->req_buf = devm_ioremap(mdev, res.start,
  497. mchan->req_buf_size);
  498. if (!mchan->req_buf) {
  499. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  500. return -ENOMEM;
  501. }
  502. } else if (ret != -ENODEV) {
  503. dev_err(mdev, "Unmatched resource %s.\n", name);
  504. return ret;
  505. }
  506. name = "local_response_region";
  507. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  508. if (!ret) {
  509. mchan->resp_buf_size = resource_size(&res);
  510. mchan->resp_buf = devm_ioremap(mdev, res.start,
  511. mchan->resp_buf_size);
  512. if (!mchan->resp_buf) {
  513. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  514. return -ENOMEM;
  515. }
  516. } else if (ret != -ENODEV) {
  517. dev_err(mdev, "Unmatched resource %s.\n", name);
  518. return ret;
  519. }
  520. mchan->rx_buf = devm_kzalloc(mdev,
  521. mchan->resp_buf_size +
  522. sizeof(struct zynqmp_ipi_message),
  523. GFP_KERNEL);
  524. if (!mchan->rx_buf)
  525. return -ENOMEM;
  526. /* Get the IPI remote agent ID */
  527. ret = of_property_read_u32(node, "xlnx,ipi-id", &ipi_mbox->remote_id);
  528. if (ret < 0) {
  529. dev_err(dev, "No IPI remote ID is specified.\n");
  530. return ret;
  531. }
  532. mbox = &ipi_mbox->mbox;
  533. mbox->dev = mdev;
  534. mbox->ops = &zynqmp_ipi_chan_ops;
  535. mbox->num_chans = 2;
  536. mbox->txdone_irq = false;
  537. mbox->txdone_poll = true;
  538. mbox->txpoll_period = 5;
  539. mbox->of_xlate = zynqmp_ipi_of_xlate;
  540. chans = devm_kzalloc(mdev, 2 * sizeof(*chans), GFP_KERNEL);
  541. if (!chans)
  542. return -ENOMEM;
  543. mbox->chans = chans;
  544. chans[IPI_MB_CHNL_TX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
  545. chans[IPI_MB_CHNL_RX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  546. ipi_mbox->mchans[IPI_MB_CHNL_TX].chan_type = IPI_MB_CHNL_TX;
  547. ipi_mbox->mchans[IPI_MB_CHNL_RX].chan_type = IPI_MB_CHNL_RX;
  548. ret = devm_mbox_controller_register(mdev, mbox);
  549. if (ret)
  550. dev_err(mdev,
  551. "Failed to register mbox_controller(%d)\n", ret);
  552. else
  553. dev_info(mdev,
  554. "Registered ZynqMP IPI mbox with TX/RX channels.\n");
  555. return ret;
  556. }
  557. /**
  558. * zynqmp_ipi_free_mboxes - Free IPI mailboxes devices
  559. *
  560. * @pdata: IPI private data
  561. */
  562. static void zynqmp_ipi_free_mboxes(struct zynqmp_ipi_pdata *pdata)
  563. {
  564. struct zynqmp_ipi_mbox *ipi_mbox;
  565. int i;
  566. i = pdata->num_mboxes;
  567. for (; i >= 0; i--) {
  568. ipi_mbox = &pdata->ipi_mboxes[i];
  569. if (ipi_mbox->dev.parent) {
  570. mbox_controller_unregister(&ipi_mbox->mbox);
  571. device_unregister(&ipi_mbox->dev);
  572. }
  573. }
  574. }
  575. static int zynqmp_ipi_probe(struct platform_device *pdev)
  576. {
  577. struct device *dev = &pdev->dev;
  578. struct device_node *nc, *np = pdev->dev.of_node;
  579. struct zynqmp_ipi_pdata *pdata;
  580. struct zynqmp_ipi_mbox *mbox;
  581. int num_mboxes, ret = -EINVAL;
  582. num_mboxes = of_get_child_count(np);
  583. pdata = devm_kzalloc(dev, sizeof(*pdata) + (num_mboxes * sizeof(*mbox)),
  584. GFP_KERNEL);
  585. if (!pdata)
  586. return -ENOMEM;
  587. pdata->dev = dev;
  588. /* Get the IPI local agents ID */
  589. ret = of_property_read_u32(np, "xlnx,ipi-id", &pdata->local_id);
  590. if (ret < 0) {
  591. dev_err(dev, "No IPI local ID is specified.\n");
  592. return ret;
  593. }
  594. pdata->num_mboxes = num_mboxes;
  595. pdata->ipi_mboxes = (struct zynqmp_ipi_mbox *)
  596. ((char *)pdata + sizeof(*pdata));
  597. mbox = pdata->ipi_mboxes;
  598. for_each_available_child_of_node(np, nc) {
  599. mbox->pdata = pdata;
  600. ret = zynqmp_ipi_mbox_probe(mbox, nc);
  601. if (ret) {
  602. dev_err(dev, "failed to probe subdev.\n");
  603. ret = -EINVAL;
  604. goto free_mbox_dev;
  605. }
  606. mbox++;
  607. }
  608. /* IPI IRQ */
  609. ret = platform_get_irq(pdev, 0);
  610. if (ret < 0)
  611. goto free_mbox_dev;
  612. pdata->irq = ret;
  613. ret = devm_request_irq(dev, pdata->irq, zynqmp_ipi_interrupt,
  614. IRQF_SHARED, dev_name(dev), pdata);
  615. if (ret) {
  616. dev_err(dev, "IRQ %d is not requested successfully.\n",
  617. pdata->irq);
  618. goto free_mbox_dev;
  619. }
  620. platform_set_drvdata(pdev, pdata);
  621. return ret;
  622. free_mbox_dev:
  623. zynqmp_ipi_free_mboxes(pdata);
  624. return ret;
  625. }
  626. static int zynqmp_ipi_remove(struct platform_device *pdev)
  627. {
  628. struct zynqmp_ipi_pdata *pdata;
  629. pdata = platform_get_drvdata(pdev);
  630. zynqmp_ipi_free_mboxes(pdata);
  631. return 0;
  632. }
  633. static struct platform_driver zynqmp_ipi_driver = {
  634. .probe = zynqmp_ipi_probe,
  635. .remove = zynqmp_ipi_remove,
  636. .driver = {
  637. .name = "zynqmp-ipi",
  638. .of_match_table = of_match_ptr(zynqmp_ipi_of_match),
  639. },
  640. };
  641. static int __init zynqmp_ipi_init(void)
  642. {
  643. return platform_driver_register(&zynqmp_ipi_driver);
  644. }
  645. subsys_initcall(zynqmp_ipi_init);
  646. static void __exit zynqmp_ipi_exit(void)
  647. {
  648. platform_driver_unregister(&zynqmp_ipi_driver);
  649. }
  650. module_exit(zynqmp_ipi_exit);
  651. MODULE_LICENSE("GPL v2");
  652. MODULE_DESCRIPTION("Xilinx ZynqMP IPI Mailbox driver");
  653. MODULE_AUTHOR("Xilinx Inc.");