zynqmp-ipi-mailbox.c 27 KB

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