zynqmp-ipi-mailbox.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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/cpuhotplug.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mailbox_controller.h>
  16. #include <linux/mailbox/zynqmp-ipi-message.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/platform_device.h>
  22. /* IPI agent ID any */
  23. #define IPI_ID_ANY 0xFFUL
  24. /* indicate if ZynqMP IPI mailbox driver uses SMC calls or HVC calls */
  25. #define USE_SMC 0
  26. #define USE_HVC 1
  27. /* Default IPI SMC function IDs */
  28. #define SMC_IPI_MAILBOX_OPEN 0x82001000U
  29. #define SMC_IPI_MAILBOX_RELEASE 0x82001001U
  30. #define SMC_IPI_MAILBOX_STATUS_ENQUIRY 0x82001002U
  31. #define SMC_IPI_MAILBOX_NOTIFY 0x82001003U
  32. #define SMC_IPI_MAILBOX_ACK 0x82001004U
  33. #define SMC_IPI_MAILBOX_ENABLE_IRQ 0x82001005U
  34. #define SMC_IPI_MAILBOX_DISABLE_IRQ 0x82001006U
  35. /* IPI SMC Macros */
  36. #define IPI_SMC_ENQUIRY_DIRQ_MASK 0x00000001UL /* Flag to indicate if
  37. * notification interrupt
  38. * to be disabled.
  39. */
  40. #define IPI_SMC_ACK_EIRQ_MASK 0x00000001UL /* Flag to indicate if
  41. * notification interrupt
  42. * to be enabled.
  43. */
  44. /* IPI mailbox status */
  45. #define IPI_MB_STATUS_IDLE 0
  46. #define IPI_MB_STATUS_SEND_PENDING 1
  47. #define IPI_MB_STATUS_RECV_PENDING 2
  48. #define IPI_MB_CHNL_TX 0 /* IPI mailbox TX channel */
  49. #define IPI_MB_CHNL_RX 1 /* IPI mailbox RX channel */
  50. /* IPI Message Buffer Information */
  51. #define RESP_OFFSET 0x20U
  52. #define DEST_OFFSET 0x40U
  53. #define IPI_BUF_SIZE 0x20U
  54. #define DST_BIT_POS 9U
  55. #define SRC_BITMASK GENMASK(11, 8)
  56. /* Macro to represent SGI type for IPI IRQs */
  57. #define IPI_IRQ_TYPE_SGI 2
  58. /*
  59. * Module parameters
  60. */
  61. static int tx_poll_period = 5;
  62. module_param_named(tx_poll_period, tx_poll_period, int, 0644);
  63. MODULE_PARM_DESC(tx_poll_period, "Poll period waiting for ack after send.");
  64. /**
  65. * struct zynqmp_ipi_mchan - Description of a Xilinx ZynqMP IPI mailbox channel
  66. * @is_opened: indicate if the IPI channel is opened
  67. * @req_buf: local to remote request buffer start address
  68. * @resp_buf: local to remote response buffer start address
  69. * @req_buf_size: request buffer size
  70. * @resp_buf_size: response buffer size
  71. * @rx_buf: receive buffer to pass received message to client
  72. * @chan_type: channel type
  73. */
  74. struct zynqmp_ipi_mchan {
  75. int is_opened;
  76. void __iomem *req_buf;
  77. void __iomem *resp_buf;
  78. void *rx_buf;
  79. size_t req_buf_size;
  80. size_t resp_buf_size;
  81. unsigned int chan_type;
  82. };
  83. struct zynqmp_ipi_mbox;
  84. typedef int (*setup_ipi_fn)(struct zynqmp_ipi_mbox *ipi_mbox, struct device_node *node);
  85. /**
  86. * struct zynqmp_ipi_mbox - Description of a ZynqMP IPI mailbox
  87. * platform data.
  88. * @pdata: pointer to the IPI private data
  89. * @dev: device pointer corresponding to the Xilinx ZynqMP
  90. * IPI mailbox
  91. * @remote_id: remote IPI agent ID
  92. * @mbox: mailbox Controller
  93. * @mchans: array for channels, tx channel and rx channel.
  94. * @setup_ipi_fn: Function Pointer to set up IPI Channels
  95. */
  96. struct zynqmp_ipi_mbox {
  97. struct zynqmp_ipi_pdata *pdata;
  98. struct device dev;
  99. u32 remote_id;
  100. struct mbox_controller mbox;
  101. struct zynqmp_ipi_mchan mchans[2];
  102. setup_ipi_fn setup_ipi_fn;
  103. };
  104. /**
  105. * struct zynqmp_ipi_pdata - Description of z ZynqMP IPI agent platform data.
  106. *
  107. * @dev: device pointer corresponding to the Xilinx ZynqMP
  108. * IPI agent
  109. * @irq: IPI agent interrupt ID
  110. * @irq_type: IPI SGI or SPI IRQ type
  111. * @method: IPI SMC or HVC is going to be used
  112. * @local_id: local IPI agent ID
  113. * @virq_sgi: IRQ number mapped to SGI
  114. * @num_mboxes: number of mailboxes of this IPI agent
  115. * @ipi_mboxes: IPI mailboxes of this IPI agent
  116. */
  117. struct zynqmp_ipi_pdata {
  118. struct device *dev;
  119. int irq;
  120. unsigned int irq_type;
  121. unsigned int method;
  122. u32 local_id;
  123. int virq_sgi;
  124. int num_mboxes;
  125. struct zynqmp_ipi_mbox ipi_mboxes[] __counted_by(num_mboxes);
  126. };
  127. static DEFINE_PER_CPU(struct zynqmp_ipi_pdata *, per_cpu_pdata);
  128. static struct device_driver zynqmp_ipi_mbox_driver = {
  129. .owner = THIS_MODULE,
  130. .name = "zynqmp-ipi-mbox",
  131. };
  132. static void zynqmp_ipi_fw_call(struct zynqmp_ipi_mbox *ipi_mbox,
  133. unsigned long a0, unsigned long a3,
  134. struct arm_smccc_res *res)
  135. {
  136. struct zynqmp_ipi_pdata *pdata = ipi_mbox->pdata;
  137. unsigned long a1, a2;
  138. a1 = pdata->local_id;
  139. a2 = ipi_mbox->remote_id;
  140. if (pdata->method == USE_SMC)
  141. arm_smccc_smc(a0, a1, a2, a3, 0, 0, 0, 0, res);
  142. else
  143. arm_smccc_hvc(a0, a1, a2, a3, 0, 0, 0, 0, res);
  144. }
  145. /**
  146. * zynqmp_ipi_interrupt - Interrupt handler for IPI notification
  147. *
  148. * @irq: Interrupt number
  149. * @data: ZynqMP IPI mailbox platform data.
  150. *
  151. * Return: -EINVAL if there is no instance
  152. * IRQ_NONE if the interrupt is not ours.
  153. * IRQ_HANDLED if the rx interrupt was successfully handled.
  154. */
  155. static irqreturn_t zynqmp_ipi_interrupt(int irq, void *data)
  156. {
  157. struct zynqmp_ipi_pdata *pdata = data;
  158. struct mbox_chan *chan;
  159. struct zynqmp_ipi_mbox *ipi_mbox;
  160. struct zynqmp_ipi_mchan *mchan;
  161. struct zynqmp_ipi_message *msg;
  162. u64 arg0, arg3;
  163. struct arm_smccc_res res;
  164. int ret, i, status = IRQ_NONE;
  165. (void)irq;
  166. arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
  167. arg3 = IPI_SMC_ENQUIRY_DIRQ_MASK;
  168. for (i = 0; i < pdata->num_mboxes; i++) {
  169. ipi_mbox = &pdata->ipi_mboxes[i];
  170. mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  171. chan = &ipi_mbox->mbox.chans[IPI_MB_CHNL_RX];
  172. zynqmp_ipi_fw_call(ipi_mbox, arg0, arg3, &res);
  173. ret = (int)(res.a0 & 0xFFFFFFFF);
  174. if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
  175. if (mchan->is_opened) {
  176. msg = mchan->rx_buf;
  177. if (msg) {
  178. msg->len = mchan->req_buf_size;
  179. memcpy_fromio(msg->data, mchan->req_buf,
  180. msg->len);
  181. }
  182. mbox_chan_received_data(chan, (void *)msg);
  183. status = IRQ_HANDLED;
  184. }
  185. }
  186. }
  187. return status;
  188. }
  189. static irqreturn_t zynqmp_sgi_interrupt(int irq, void *data)
  190. {
  191. struct zynqmp_ipi_pdata **pdata_ptr = data;
  192. struct zynqmp_ipi_pdata *pdata = *pdata_ptr;
  193. return zynqmp_ipi_interrupt(irq, pdata);
  194. }
  195. /**
  196. * zynqmp_ipi_peek_data - Peek to see if there are any rx messages.
  197. *
  198. * @chan: Channel Pointer
  199. *
  200. * Return: 'true' if there is pending rx data, 'false' if there is none.
  201. */
  202. static bool zynqmp_ipi_peek_data(struct mbox_chan *chan)
  203. {
  204. struct device *dev = chan->mbox->dev;
  205. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  206. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  207. int ret;
  208. u64 arg0;
  209. struct arm_smccc_res res;
  210. if (WARN_ON(!ipi_mbox)) {
  211. dev_err(dev, "no platform drv data??\n");
  212. return false;
  213. }
  214. arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
  215. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  216. ret = (int)(res.a0 & 0xFFFFFFFF);
  217. if (mchan->chan_type == IPI_MB_CHNL_TX) {
  218. /* TX channel, check if the message has been acked
  219. * by the remote, if yes, response is available.
  220. */
  221. if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
  222. return false;
  223. else
  224. return true;
  225. } else if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
  226. /* RX channel, check if there is message arrived. */
  227. return true;
  228. }
  229. return false;
  230. }
  231. /**
  232. * zynqmp_ipi_last_tx_done - See if the last tx message is sent
  233. *
  234. * @chan: Channel pointer
  235. *
  236. * Return: 'true' is no pending tx data, 'false' if there are any.
  237. */
  238. static bool zynqmp_ipi_last_tx_done(struct mbox_chan *chan)
  239. {
  240. struct device *dev = chan->mbox->dev;
  241. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  242. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  243. int ret;
  244. u64 arg0;
  245. struct arm_smccc_res res;
  246. if (WARN_ON(!ipi_mbox)) {
  247. dev_err(dev, "no platform drv data??\n");
  248. return false;
  249. }
  250. if (mchan->chan_type == IPI_MB_CHNL_TX) {
  251. /* We only need to check if the message been taken
  252. * by the remote in the TX channel
  253. */
  254. arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
  255. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  256. /* Check the SMC call status, a0 of the result */
  257. ret = (int)(res.a0 & 0xFFFFFFFF);
  258. if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
  259. return false;
  260. return true;
  261. }
  262. /* Always true for the response message in RX channel */
  263. return true;
  264. }
  265. /**
  266. * zynqmp_ipi_send_data - Send data
  267. *
  268. * @chan: Channel Pointer
  269. * @data: Message Pointer
  270. *
  271. * Return: 0 if all goes good, else appropriate error messages.
  272. */
  273. static int zynqmp_ipi_send_data(struct mbox_chan *chan, void *data)
  274. {
  275. struct device *dev = chan->mbox->dev;
  276. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  277. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  278. struct zynqmp_ipi_message *msg = data;
  279. u64 arg0;
  280. struct arm_smccc_res res;
  281. if (WARN_ON(!ipi_mbox)) {
  282. dev_err(dev, "no platform drv data??\n");
  283. return -EINVAL;
  284. }
  285. if (mchan->chan_type == IPI_MB_CHNL_TX) {
  286. /* Send request message */
  287. if (msg && msg->len > mchan->req_buf_size && mchan->req_buf) {
  288. dev_err(dev, "channel %d message length %u > max %lu\n",
  289. mchan->chan_type, (unsigned int)msg->len,
  290. mchan->req_buf_size);
  291. return -EINVAL;
  292. }
  293. if (msg && msg->len && mchan->req_buf)
  294. memcpy_toio(mchan->req_buf, msg->data, msg->len);
  295. /* Kick IPI mailbox to send message */
  296. arg0 = SMC_IPI_MAILBOX_NOTIFY;
  297. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  298. } else {
  299. /* Send response message */
  300. if (msg && msg->len > mchan->resp_buf_size && mchan->resp_buf) {
  301. dev_err(dev, "channel %d message length %u > max %lu\n",
  302. mchan->chan_type, (unsigned int)msg->len,
  303. mchan->resp_buf_size);
  304. return -EINVAL;
  305. }
  306. if (msg && msg->len && mchan->resp_buf)
  307. memcpy_toio(mchan->resp_buf, msg->data, msg->len);
  308. arg0 = SMC_IPI_MAILBOX_ACK;
  309. zynqmp_ipi_fw_call(ipi_mbox, arg0, IPI_SMC_ACK_EIRQ_MASK,
  310. &res);
  311. }
  312. return 0;
  313. }
  314. /**
  315. * zynqmp_ipi_startup - Startup the IPI channel
  316. *
  317. * @chan: Channel pointer
  318. *
  319. * Return: 0 if all goes good, else return corresponding error message
  320. */
  321. static int zynqmp_ipi_startup(struct mbox_chan *chan)
  322. {
  323. struct device *dev = chan->mbox->dev;
  324. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  325. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  326. u64 arg0;
  327. struct arm_smccc_res res;
  328. int ret = 0;
  329. unsigned int nchan_type;
  330. if (mchan->is_opened)
  331. return 0;
  332. /* If no channel has been opened, open the IPI mailbox */
  333. nchan_type = (mchan->chan_type + 1) % 2;
  334. if (!ipi_mbox->mchans[nchan_type].is_opened) {
  335. arg0 = SMC_IPI_MAILBOX_OPEN;
  336. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  337. /* Check the SMC call status, a0 of the result */
  338. ret = (int)(res.a0 & 0xFFFFFFFF);
  339. if (ret < 0) {
  340. dev_err(dev, "SMC to open the IPI channel failed.\n");
  341. return ret;
  342. }
  343. ret = 0;
  344. }
  345. /* If it is RX channel, enable the IPI notification interrupt */
  346. if (mchan->chan_type == IPI_MB_CHNL_RX) {
  347. arg0 = SMC_IPI_MAILBOX_ENABLE_IRQ;
  348. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  349. }
  350. mchan->is_opened = 1;
  351. return ret;
  352. }
  353. /**
  354. * zynqmp_ipi_shutdown - Shutdown the IPI channel
  355. *
  356. * @chan: Channel pointer
  357. */
  358. static void zynqmp_ipi_shutdown(struct mbox_chan *chan)
  359. {
  360. struct device *dev = chan->mbox->dev;
  361. struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
  362. struct zynqmp_ipi_mchan *mchan = chan->con_priv;
  363. u64 arg0;
  364. struct arm_smccc_res res;
  365. unsigned int chan_type;
  366. if (!mchan->is_opened)
  367. return;
  368. /* If it is RX channel, disable notification interrupt */
  369. chan_type = mchan->chan_type;
  370. if (chan_type == IPI_MB_CHNL_RX) {
  371. arg0 = SMC_IPI_MAILBOX_DISABLE_IRQ;
  372. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  373. }
  374. /* Release IPI mailbox if no other channel is opened */
  375. chan_type = (chan_type + 1) % 2;
  376. if (!ipi_mbox->mchans[chan_type].is_opened) {
  377. arg0 = SMC_IPI_MAILBOX_RELEASE;
  378. zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
  379. }
  380. mchan->is_opened = 0;
  381. }
  382. /* ZynqMP IPI mailbox operations */
  383. static const struct mbox_chan_ops zynqmp_ipi_chan_ops = {
  384. .startup = zynqmp_ipi_startup,
  385. .shutdown = zynqmp_ipi_shutdown,
  386. .peek_data = zynqmp_ipi_peek_data,
  387. .last_tx_done = zynqmp_ipi_last_tx_done,
  388. .send_data = zynqmp_ipi_send_data,
  389. };
  390. /**
  391. * zynqmp_ipi_of_xlate - Translate of phandle to IPI mailbox channel
  392. *
  393. * @mbox: mailbox controller pointer
  394. * @p: phandle pointer
  395. *
  396. * Return: Mailbox channel, else return error pointer.
  397. */
  398. static struct mbox_chan *zynqmp_ipi_of_xlate(struct mbox_controller *mbox,
  399. const struct of_phandle_args *p)
  400. {
  401. struct mbox_chan *chan;
  402. struct device *dev = mbox->dev;
  403. unsigned int chan_type;
  404. /* Only supports TX and RX channels */
  405. chan_type = p->args[0];
  406. if (chan_type != IPI_MB_CHNL_TX && chan_type != IPI_MB_CHNL_RX) {
  407. dev_err(dev, "req chnl failure: invalid chnl type %u.\n",
  408. chan_type);
  409. return ERR_PTR(-EINVAL);
  410. }
  411. chan = &mbox->chans[chan_type];
  412. return chan;
  413. }
  414. /**
  415. * zynqmp_ipi_mbox_get_buf_res - Get buffer resource from the IPI dev node
  416. *
  417. * @node: IPI mbox device child node
  418. * @name: name of the IPI buffer
  419. * @res: pointer to where the resource information will be stored.
  420. *
  421. * Return: 0 for success, negative value for failure
  422. */
  423. static int zynqmp_ipi_mbox_get_buf_res(struct device_node *node,
  424. const char *name,
  425. struct resource *res)
  426. {
  427. int ret, index;
  428. index = of_property_match_string(node, "reg-names", name);
  429. if (index >= 0) {
  430. ret = of_address_to_resource(node, index, res);
  431. if (ret < 0)
  432. return -EINVAL;
  433. return 0;
  434. }
  435. return -ENODEV;
  436. }
  437. /**
  438. * zynqmp_ipi_mbox_dev_release() - release the existence of a ipi mbox dev
  439. *
  440. * @dev: the ipi mailbox device
  441. *
  442. * This is to avoid the no device release() function kernel warning.
  443. *
  444. */
  445. static void zynqmp_ipi_mbox_dev_release(struct device *dev)
  446. {
  447. (void)dev;
  448. }
  449. /**
  450. * zynqmp_ipi_mbox_probe - probe IPI mailbox resource from device node
  451. *
  452. * @ipi_mbox: pointer to IPI mailbox private data structure
  453. * @node: IPI mailbox device node
  454. *
  455. * Return: 0 for success, negative value for failure
  456. */
  457. static int zynqmp_ipi_mbox_probe(struct zynqmp_ipi_mbox *ipi_mbox,
  458. struct device_node *node)
  459. {
  460. struct mbox_chan *chans;
  461. struct mbox_controller *mbox;
  462. struct device *dev, *mdev;
  463. int ret;
  464. dev = ipi_mbox->pdata->dev;
  465. /* Initialize dev for IPI mailbox */
  466. ipi_mbox->dev.parent = dev;
  467. ipi_mbox->dev.release = NULL;
  468. ipi_mbox->dev.of_node = node;
  469. dev_set_name(&ipi_mbox->dev, "%s", of_node_full_name(node));
  470. dev_set_drvdata(&ipi_mbox->dev, ipi_mbox);
  471. ipi_mbox->dev.release = zynqmp_ipi_mbox_dev_release;
  472. ipi_mbox->dev.driver = &zynqmp_ipi_mbox_driver;
  473. ret = device_register(&ipi_mbox->dev);
  474. if (ret) {
  475. dev_err(dev, "Failed to register ipi mbox dev.\n");
  476. put_device(&ipi_mbox->dev);
  477. return ret;
  478. }
  479. mdev = &ipi_mbox->dev;
  480. /* Get the IPI remote agent ID */
  481. ret = of_property_read_u32(node, "xlnx,ipi-id", &ipi_mbox->remote_id);
  482. if (ret < 0) {
  483. dev_err(dev, "No IPI remote ID is specified.\n");
  484. return ret;
  485. }
  486. ret = ipi_mbox->setup_ipi_fn(ipi_mbox, node);
  487. if (ret) {
  488. dev_err(dev, "Failed to set up IPI Buffers.\n");
  489. return ret;
  490. }
  491. mbox = &ipi_mbox->mbox;
  492. mbox->dev = mdev;
  493. mbox->ops = &zynqmp_ipi_chan_ops;
  494. mbox->num_chans = 2;
  495. mbox->txdone_irq = false;
  496. mbox->txdone_poll = true;
  497. mbox->txpoll_period = tx_poll_period;
  498. mbox->of_xlate = zynqmp_ipi_of_xlate;
  499. chans = devm_kzalloc(mdev, 2 * sizeof(*chans), GFP_KERNEL);
  500. if (!chans)
  501. return -ENOMEM;
  502. mbox->chans = chans;
  503. chans[IPI_MB_CHNL_TX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
  504. chans[IPI_MB_CHNL_RX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  505. ipi_mbox->mchans[IPI_MB_CHNL_TX].chan_type = IPI_MB_CHNL_TX;
  506. ipi_mbox->mchans[IPI_MB_CHNL_RX].chan_type = IPI_MB_CHNL_RX;
  507. ret = devm_mbox_controller_register(mdev, mbox);
  508. if (ret)
  509. dev_err(mdev,
  510. "Failed to register mbox_controller(%d)\n", ret);
  511. else
  512. dev_info(mdev,
  513. "Registered ZynqMP IPI mbox with TX/RX channels.\n");
  514. return ret;
  515. }
  516. /**
  517. * zynqmp_ipi_setup - set up IPI Buffers for classic flow
  518. *
  519. * @ipi_mbox: pointer to IPI mailbox private data structure
  520. * @node: IPI mailbox device node
  521. *
  522. * This will be used to set up IPI Buffers for ZynqMP SOC if user
  523. * wishes to use classic driver usage model on new SOC's with only
  524. * buffered IPIs.
  525. *
  526. * Note that bufferless IPIs and mixed usage of buffered and bufferless
  527. * IPIs are not supported with this flow.
  528. *
  529. * This will be invoked with compatible string "xlnx,zynqmp-ipi-mailbox".
  530. *
  531. * Return: 0 for success, negative value for failure
  532. */
  533. static int zynqmp_ipi_setup(struct zynqmp_ipi_mbox *ipi_mbox,
  534. struct device_node *node)
  535. {
  536. struct zynqmp_ipi_mchan *mchan;
  537. struct device *mdev;
  538. struct resource res;
  539. const char *name;
  540. int ret;
  541. mdev = &ipi_mbox->dev;
  542. mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
  543. name = "local_request_region";
  544. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  545. if (!ret) {
  546. mchan->req_buf_size = resource_size(&res);
  547. mchan->req_buf = devm_ioremap(mdev, res.start,
  548. mchan->req_buf_size);
  549. if (!mchan->req_buf) {
  550. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  551. return -ENOMEM;
  552. }
  553. } else if (ret != -ENODEV) {
  554. dev_err(mdev, "Unmatched resource %s, %d.\n", name, ret);
  555. return ret;
  556. }
  557. name = "remote_response_region";
  558. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  559. if (!ret) {
  560. mchan->resp_buf_size = resource_size(&res);
  561. mchan->resp_buf = devm_ioremap(mdev, res.start,
  562. mchan->resp_buf_size);
  563. if (!mchan->resp_buf) {
  564. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  565. return -ENOMEM;
  566. }
  567. } else if (ret != -ENODEV) {
  568. dev_err(mdev, "Unmatched resource %s.\n", name);
  569. return ret;
  570. }
  571. mchan->rx_buf = devm_kzalloc(mdev,
  572. mchan->resp_buf_size +
  573. sizeof(struct zynqmp_ipi_message),
  574. GFP_KERNEL);
  575. if (!mchan->rx_buf)
  576. return -ENOMEM;
  577. mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  578. name = "remote_request_region";
  579. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  580. if (!ret) {
  581. mchan->req_buf_size = resource_size(&res);
  582. mchan->req_buf = devm_ioremap(mdev, res.start,
  583. mchan->req_buf_size);
  584. if (!mchan->req_buf) {
  585. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  586. return -ENOMEM;
  587. }
  588. } else if (ret != -ENODEV) {
  589. dev_err(mdev, "Unmatched resource %s.\n", name);
  590. return ret;
  591. }
  592. name = "local_response_region";
  593. ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
  594. if (!ret) {
  595. mchan->resp_buf_size = resource_size(&res);
  596. mchan->resp_buf = devm_ioremap(mdev, res.start,
  597. mchan->resp_buf_size);
  598. if (!mchan->resp_buf) {
  599. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  600. return -ENOMEM;
  601. }
  602. } else if (ret != -ENODEV) {
  603. dev_err(mdev, "Unmatched resource %s.\n", name);
  604. return ret;
  605. }
  606. mchan->rx_buf = devm_kzalloc(mdev,
  607. mchan->resp_buf_size +
  608. sizeof(struct zynqmp_ipi_message),
  609. GFP_KERNEL);
  610. if (!mchan->rx_buf)
  611. return -ENOMEM;
  612. return 0;
  613. }
  614. /**
  615. * versal_ipi_setup - Set up IPIs to support mixed usage of
  616. * Buffered and Bufferless IPIs.
  617. *
  618. * @ipi_mbox: pointer to IPI mailbox private data structure
  619. * @node: IPI mailbox device node
  620. *
  621. * Return: 0 for success, negative value for failure
  622. */
  623. static int versal_ipi_setup(struct zynqmp_ipi_mbox *ipi_mbox,
  624. struct device_node *node)
  625. {
  626. struct zynqmp_ipi_mchan *tx_mchan, *rx_mchan;
  627. struct resource host_res, remote_res;
  628. struct device_node *parent_node;
  629. int host_idx, remote_idx;
  630. struct device *mdev;
  631. tx_mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
  632. rx_mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
  633. parent_node = of_get_parent(node);
  634. mdev = &ipi_mbox->dev;
  635. host_idx = zynqmp_ipi_mbox_get_buf_res(parent_node, "msg", &host_res);
  636. remote_idx = zynqmp_ipi_mbox_get_buf_res(node, "msg", &remote_res);
  637. /*
  638. * Only set up buffers if both sides claim to have msg buffers.
  639. * This is because each buffered IPI's corresponding msg buffers
  640. * are reserved for use by other buffered IPI's.
  641. */
  642. if (!host_idx && !remote_idx) {
  643. u32 host_src, host_dst, remote_src, remote_dst;
  644. u32 buff_sz;
  645. buff_sz = resource_size(&host_res);
  646. host_src = host_res.start & SRC_BITMASK;
  647. remote_src = remote_res.start & SRC_BITMASK;
  648. host_dst = (host_src >> DST_BIT_POS) * DEST_OFFSET;
  649. remote_dst = (remote_src >> DST_BIT_POS) * DEST_OFFSET;
  650. /* Validate that IPI IDs is within IPI Message buffer space. */
  651. if (host_dst >= buff_sz || remote_dst >= buff_sz) {
  652. dev_err(mdev,
  653. "Invalid IPI Message buffer values: %x %x\n",
  654. host_dst, remote_dst);
  655. return -EINVAL;
  656. }
  657. tx_mchan->req_buf = devm_ioremap(mdev,
  658. host_res.start | remote_dst,
  659. IPI_BUF_SIZE);
  660. if (!tx_mchan->req_buf) {
  661. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  662. return -ENOMEM;
  663. }
  664. tx_mchan->resp_buf = devm_ioremap(mdev,
  665. (remote_res.start | host_dst) +
  666. RESP_OFFSET, IPI_BUF_SIZE);
  667. if (!tx_mchan->resp_buf) {
  668. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  669. return -ENOMEM;
  670. }
  671. rx_mchan->req_buf = devm_ioremap(mdev,
  672. remote_res.start | host_dst,
  673. IPI_BUF_SIZE);
  674. if (!rx_mchan->req_buf) {
  675. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  676. return -ENOMEM;
  677. }
  678. rx_mchan->resp_buf = devm_ioremap(mdev,
  679. (host_res.start | remote_dst) +
  680. RESP_OFFSET, IPI_BUF_SIZE);
  681. if (!rx_mchan->resp_buf) {
  682. dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
  683. return -ENOMEM;
  684. }
  685. tx_mchan->resp_buf_size = IPI_BUF_SIZE;
  686. tx_mchan->req_buf_size = IPI_BUF_SIZE;
  687. tx_mchan->rx_buf = devm_kzalloc(mdev, IPI_BUF_SIZE +
  688. sizeof(struct zynqmp_ipi_message),
  689. GFP_KERNEL);
  690. if (!tx_mchan->rx_buf)
  691. return -ENOMEM;
  692. rx_mchan->resp_buf_size = IPI_BUF_SIZE;
  693. rx_mchan->req_buf_size = IPI_BUF_SIZE;
  694. rx_mchan->rx_buf = devm_kzalloc(mdev, IPI_BUF_SIZE +
  695. sizeof(struct zynqmp_ipi_message),
  696. GFP_KERNEL);
  697. if (!rx_mchan->rx_buf)
  698. return -ENOMEM;
  699. }
  700. return 0;
  701. }
  702. static int xlnx_mbox_cpuhp_start(unsigned int cpu)
  703. {
  704. struct zynqmp_ipi_pdata *pdata;
  705. pdata = get_cpu_var(per_cpu_pdata);
  706. put_cpu_var(per_cpu_pdata);
  707. enable_percpu_irq(pdata->virq_sgi, IRQ_TYPE_NONE);
  708. return 0;
  709. }
  710. static int xlnx_mbox_cpuhp_down(unsigned int cpu)
  711. {
  712. struct zynqmp_ipi_pdata *pdata;
  713. pdata = get_cpu_var(per_cpu_pdata);
  714. put_cpu_var(per_cpu_pdata);
  715. disable_percpu_irq(pdata->virq_sgi);
  716. return 0;
  717. }
  718. static void xlnx_disable_percpu_irq(void *data)
  719. {
  720. struct zynqmp_ipi_pdata *pdata;
  721. pdata = *this_cpu_ptr(&per_cpu_pdata);
  722. disable_percpu_irq(pdata->virq_sgi);
  723. }
  724. static int xlnx_mbox_init_sgi(struct platform_device *pdev,
  725. int sgi_num,
  726. struct zynqmp_ipi_pdata *pdata)
  727. {
  728. int ret = 0;
  729. int cpu;
  730. /*
  731. * IRQ related structures are used for the following:
  732. * for each SGI interrupt ensure its mapped by GIC IRQ domain
  733. * and that each corresponding linux IRQ for the HW IRQ has
  734. * a handler for when receiving an interrupt from the remote
  735. * processor.
  736. */
  737. struct irq_domain *domain;
  738. struct irq_fwspec sgi_fwspec;
  739. struct device_node *interrupt_parent = NULL;
  740. struct device *dev = &pdev->dev;
  741. /* Find GIC controller to map SGIs. */
  742. interrupt_parent = of_irq_find_parent(dev->of_node);
  743. if (!interrupt_parent) {
  744. dev_err(&pdev->dev, "Failed to find property for Interrupt parent\n");
  745. return -EINVAL;
  746. }
  747. /* Each SGI needs to be associated with GIC's IRQ domain. */
  748. domain = irq_find_host(interrupt_parent);
  749. of_node_put(interrupt_parent);
  750. /* Each mapping needs GIC domain when finding IRQ mapping. */
  751. sgi_fwspec.fwnode = domain->fwnode;
  752. /*
  753. * When irq domain looks at mapping each arg is as follows:
  754. * 3 args for: interrupt type (SGI), interrupt # (set later), type
  755. */
  756. sgi_fwspec.param_count = 1;
  757. /* Set SGI's hwirq */
  758. sgi_fwspec.param[0] = sgi_num;
  759. pdata->virq_sgi = irq_create_fwspec_mapping(&sgi_fwspec);
  760. for_each_possible_cpu(cpu)
  761. per_cpu(per_cpu_pdata, cpu) = pdata;
  762. ret = request_percpu_irq(pdata->virq_sgi, zynqmp_sgi_interrupt, pdev->name,
  763. &per_cpu_pdata);
  764. WARN_ON(ret);
  765. if (ret) {
  766. irq_dispose_mapping(pdata->virq_sgi);
  767. return ret;
  768. }
  769. irq_set_status_flags(pdata->virq_sgi, IRQ_PER_CPU);
  770. /* Setup function for the CPU hot-plug cases */
  771. cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mailbox/sgi:starting",
  772. xlnx_mbox_cpuhp_start, xlnx_mbox_cpuhp_down);
  773. return ret;
  774. }
  775. static void xlnx_mbox_cleanup_sgi(struct zynqmp_ipi_pdata *pdata)
  776. {
  777. cpuhp_remove_state(CPUHP_AP_ONLINE_DYN);
  778. on_each_cpu(xlnx_disable_percpu_irq, NULL, 1);
  779. irq_clear_status_flags(pdata->virq_sgi, IRQ_PER_CPU);
  780. free_percpu_irq(pdata->virq_sgi, &per_cpu_pdata);
  781. irq_dispose_mapping(pdata->virq_sgi);
  782. }
  783. /**
  784. * zynqmp_ipi_free_mboxes - Free IPI mailboxes devices
  785. *
  786. * @pdata: IPI private data
  787. */
  788. static void zynqmp_ipi_free_mboxes(struct zynqmp_ipi_pdata *pdata)
  789. {
  790. struct zynqmp_ipi_mbox *ipi_mbox;
  791. int i;
  792. if (pdata->irq_type == IPI_IRQ_TYPE_SGI)
  793. xlnx_mbox_cleanup_sgi(pdata);
  794. i = pdata->num_mboxes - 1;
  795. for (; i >= 0; i--) {
  796. ipi_mbox = &pdata->ipi_mboxes[i];
  797. if (device_is_registered(&ipi_mbox->dev))
  798. device_unregister(&ipi_mbox->dev);
  799. }
  800. }
  801. static int zynqmp_ipi_probe(struct platform_device *pdev)
  802. {
  803. struct device *dev = &pdev->dev;
  804. struct device_node *nc, *np = pdev->dev.of_node;
  805. struct zynqmp_ipi_pdata *pdata;
  806. struct of_phandle_args out_irq;
  807. struct zynqmp_ipi_mbox *mbox;
  808. int num_mboxes, ret = -EINVAL;
  809. setup_ipi_fn ipi_fn;
  810. num_mboxes = of_get_available_child_count(np);
  811. if (num_mboxes == 0) {
  812. dev_err(dev, "mailbox nodes not available\n");
  813. return -EINVAL;
  814. }
  815. pdata = devm_kzalloc(dev, struct_size(pdata, ipi_mboxes, num_mboxes),
  816. GFP_KERNEL);
  817. if (!pdata)
  818. return -ENOMEM;
  819. pdata->dev = dev;
  820. /* Get the IPI local agents ID */
  821. ret = of_property_read_u32(np, "xlnx,ipi-id", &pdata->local_id);
  822. if (ret < 0) {
  823. dev_err(dev, "No IPI local ID is specified.\n");
  824. return ret;
  825. }
  826. ipi_fn = (setup_ipi_fn)device_get_match_data(&pdev->dev);
  827. if (!ipi_fn) {
  828. dev_err(dev,
  829. "Mbox Compatible String is missing IPI Setup fn.\n");
  830. return -ENODEV;
  831. }
  832. pdata->num_mboxes = num_mboxes;
  833. mbox = pdata->ipi_mboxes;
  834. mbox->setup_ipi_fn = ipi_fn;
  835. for_each_available_child_of_node(np, nc) {
  836. mbox->pdata = pdata;
  837. ret = zynqmp_ipi_mbox_probe(mbox, nc);
  838. if (ret) {
  839. of_node_put(nc);
  840. dev_err(dev, "failed to probe subdev.\n");
  841. ret = -EINVAL;
  842. goto free_mbox_dev;
  843. }
  844. mbox++;
  845. }
  846. ret = of_irq_parse_one(dev_of_node(dev), 0, &out_irq);
  847. if (ret < 0) {
  848. dev_err(dev, "failed to parse interrupts\n");
  849. goto free_mbox_dev;
  850. }
  851. /* Use interrupt type to distinguish SGI and SPI interrupts */
  852. pdata->irq_type = out_irq.args[0];
  853. /*
  854. * If Interrupt number is in SGI range, then request SGI else request
  855. * IPI system IRQ.
  856. */
  857. if (pdata->irq_type == IPI_IRQ_TYPE_SGI) {
  858. pdata->irq = out_irq.args[1];
  859. ret = xlnx_mbox_init_sgi(pdev, pdata->irq, pdata);
  860. if (ret)
  861. goto free_mbox_dev;
  862. } else {
  863. ret = platform_get_irq(pdev, 0);
  864. if (ret < 0)
  865. goto free_mbox_dev;
  866. pdata->irq = ret;
  867. ret = devm_request_irq(dev, pdata->irq, zynqmp_ipi_interrupt,
  868. IRQF_SHARED, dev_name(dev), pdata);
  869. }
  870. if (ret) {
  871. dev_err(dev, "IRQ %d is not requested successfully.\n",
  872. pdata->irq);
  873. goto free_mbox_dev;
  874. }
  875. platform_set_drvdata(pdev, pdata);
  876. return ret;
  877. free_mbox_dev:
  878. zynqmp_ipi_free_mboxes(pdata);
  879. return ret;
  880. }
  881. static void zynqmp_ipi_remove(struct platform_device *pdev)
  882. {
  883. struct zynqmp_ipi_pdata *pdata;
  884. pdata = platform_get_drvdata(pdev);
  885. zynqmp_ipi_free_mboxes(pdata);
  886. }
  887. static const struct of_device_id zynqmp_ipi_of_match[] = {
  888. { .compatible = "xlnx,zynqmp-ipi-mailbox",
  889. .data = &zynqmp_ipi_setup,
  890. },
  891. { .compatible = "xlnx,versal-ipi-mailbox",
  892. .data = &versal_ipi_setup,
  893. },
  894. {},
  895. };
  896. MODULE_DEVICE_TABLE(of, zynqmp_ipi_of_match);
  897. static struct platform_driver zynqmp_ipi_driver = {
  898. .probe = zynqmp_ipi_probe,
  899. .remove_new = zynqmp_ipi_remove,
  900. .driver = {
  901. .name = "zynqmp-ipi",
  902. .of_match_table = of_match_ptr(zynqmp_ipi_of_match),
  903. },
  904. };
  905. static int __init zynqmp_ipi_init(void)
  906. {
  907. return platform_driver_register(&zynqmp_ipi_driver);
  908. }
  909. subsys_initcall(zynqmp_ipi_init);
  910. static void __exit zynqmp_ipi_exit(void)
  911. {
  912. platform_driver_unregister(&zynqmp_ipi_driver);
  913. }
  914. module_exit(zynqmp_ipi_exit);
  915. MODULE_LICENSE("GPL v2");
  916. MODULE_DESCRIPTION("Xilinx ZynqMP IPI Mailbox driver");
  917. MODULE_AUTHOR("Xilinx Inc.");