smp2p.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, Sony Mobile Communications AB.
  4. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/list.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/mailbox_client.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/soc/qcom/smem.h>
  18. #include <linux/soc/qcom/smem_state.h>
  19. #include <linux/spinlock.h>
  20. /*
  21. * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
  22. * of a single 32-bit value between two processors. Each value has a single
  23. * writer (the local side) and a single reader (the remote side). Values are
  24. * uniquely identified in the system by the directed edge (local processor ID
  25. * to remote processor ID) and a string identifier.
  26. *
  27. * Each processor is responsible for creating the outgoing SMEM items and each
  28. * item is writable by the local processor and readable by the remote
  29. * processor. By using two separate SMEM items that are single-reader and
  30. * single-writer, SMP2P does not require any remote locking mechanisms.
  31. *
  32. * The driver uses the Linux GPIO and interrupt framework to expose a virtual
  33. * GPIO for each outbound entry and a virtual interrupt controller for each
  34. * inbound entry.
  35. */
  36. #define SMP2P_MAX_ENTRY 16
  37. #define SMP2P_MAX_ENTRY_NAME 16
  38. #define SMP2P_FEATURE_SSR_ACK 0x1
  39. #define SMP2P_MAGIC 0x504d5324
  40. /**
  41. * struct smp2p_smem_item - in memory communication structure
  42. * @magic: magic number
  43. * @version: version - must be 1
  44. * @features: features flag - currently unused
  45. * @local_pid: processor id of sending end
  46. * @remote_pid: processor id of receiving end
  47. * @total_entries: number of entries - always SMP2P_MAX_ENTRY
  48. * @valid_entries: number of allocated entries
  49. * @flags:
  50. * @entries: individual communication entries
  51. * @name: name of the entry
  52. * @value: content of the entry
  53. */
  54. struct smp2p_smem_item {
  55. u32 magic;
  56. u8 version;
  57. unsigned features:24;
  58. u16 local_pid;
  59. u16 remote_pid;
  60. u16 total_entries;
  61. u16 valid_entries;
  62. u32 flags;
  63. struct {
  64. u8 name[SMP2P_MAX_ENTRY_NAME];
  65. u32 value;
  66. } entries[SMP2P_MAX_ENTRY];
  67. } __packed;
  68. /**
  69. * struct smp2p_entry - driver context matching one entry
  70. * @node: list entry to keep track of allocated entries
  71. * @smp2p: reference to the device driver context
  72. * @name: name of the entry, to match against smp2p_smem_item
  73. * @value: pointer to smp2p_smem_item entry value
  74. * @last_value: last handled value
  75. * @domain: irq_domain for inbound entries
  76. * @irq_enabled:bitmap to track enabled irq bits
  77. * @irq_rising: bitmap to mark irq bits for rising detection
  78. * @irq_falling:bitmap to mark irq bits for falling detection
  79. * @state: smem state handle
  80. * @lock: spinlock to protect read-modify-write of the value
  81. */
  82. struct smp2p_entry {
  83. struct list_head node;
  84. struct qcom_smp2p *smp2p;
  85. const char *name;
  86. u32 *value;
  87. u32 last_value;
  88. struct irq_domain *domain;
  89. DECLARE_BITMAP(irq_enabled, 32);
  90. DECLARE_BITMAP(irq_rising, 32);
  91. DECLARE_BITMAP(irq_falling, 32);
  92. struct qcom_smem_state *state;
  93. spinlock_t lock;
  94. };
  95. #define SMP2P_INBOUND 0
  96. #define SMP2P_OUTBOUND 1
  97. /**
  98. * struct qcom_smp2p - device driver context
  99. * @dev: device driver handle
  100. * @in: pointer to the inbound smem item
  101. * @smem_items: ids of the two smem items
  102. * @valid_entries: already scanned inbound entries
  103. * @local_pid: processor id of the inbound edge
  104. * @remote_pid: processor id of the outbound edge
  105. * @ipc_regmap: regmap for the outbound ipc
  106. * @ipc_offset: offset within the regmap
  107. * @ipc_bit: bit in regmap@offset to kick to signal remote processor
  108. * @mbox_client: mailbox client handle
  109. * @mbox_chan: apcs ipc mailbox channel handle
  110. * @inbound: list of inbound entries
  111. * @outbound: list of outbound entries
  112. */
  113. struct qcom_smp2p {
  114. struct device *dev;
  115. struct smp2p_smem_item *in;
  116. struct smp2p_smem_item *out;
  117. unsigned smem_items[SMP2P_OUTBOUND + 1];
  118. unsigned valid_entries;
  119. unsigned local_pid;
  120. unsigned remote_pid;
  121. struct regmap *ipc_regmap;
  122. int ipc_offset;
  123. int ipc_bit;
  124. struct mbox_client mbox_client;
  125. struct mbox_chan *mbox_chan;
  126. struct list_head inbound;
  127. struct list_head outbound;
  128. };
  129. static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
  130. {
  131. /* Make sure any updated data is written before the kick */
  132. wmb();
  133. if (smp2p->mbox_chan) {
  134. mbox_send_message(smp2p->mbox_chan, NULL);
  135. mbox_client_txdone(smp2p->mbox_chan, 0);
  136. } else {
  137. regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
  138. }
  139. }
  140. /**
  141. * qcom_smp2p_intr() - interrupt handler for incoming notifications
  142. * @irq: unused
  143. * @data: smp2p driver context
  144. *
  145. * Handle notifications from the remote side to handle newly allocated entries
  146. * or any changes to the state bits of existing entries.
  147. */
  148. static irqreturn_t qcom_smp2p_intr(int irq, void *data)
  149. {
  150. struct smp2p_smem_item *in;
  151. struct smp2p_entry *entry;
  152. struct qcom_smp2p *smp2p = data;
  153. unsigned smem_id = smp2p->smem_items[SMP2P_INBOUND];
  154. unsigned pid = smp2p->remote_pid;
  155. size_t size;
  156. int irq_pin;
  157. u32 status;
  158. char buf[SMP2P_MAX_ENTRY_NAME];
  159. u32 val;
  160. int i;
  161. in = smp2p->in;
  162. /* Acquire smem item, if not already found */
  163. if (!in) {
  164. in = qcom_smem_get(pid, smem_id, &size);
  165. if (IS_ERR(in)) {
  166. dev_err(smp2p->dev,
  167. "Unable to acquire remote smp2p item\n");
  168. return IRQ_HANDLED;
  169. }
  170. smp2p->in = in;
  171. }
  172. /* Match newly created entries */
  173. for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
  174. list_for_each_entry(entry, &smp2p->inbound, node) {
  175. memcpy(buf, in->entries[i].name, sizeof(buf));
  176. if (!strcmp(buf, entry->name)) {
  177. entry->value = &in->entries[i].value;
  178. break;
  179. }
  180. }
  181. }
  182. smp2p->valid_entries = i;
  183. /* Fire interrupts based on any value changes */
  184. list_for_each_entry(entry, &smp2p->inbound, node) {
  185. /* Ignore entries not yet allocated by the remote side */
  186. if (!entry->value)
  187. continue;
  188. val = readl(entry->value);
  189. status = val ^ entry->last_value;
  190. entry->last_value = val;
  191. /* No changes of this entry? */
  192. if (!status)
  193. continue;
  194. for_each_set_bit(i, entry->irq_enabled, 32) {
  195. if (!(status & BIT(i)))
  196. continue;
  197. if ((val & BIT(i) && test_bit(i, entry->irq_rising)) ||
  198. (!(val & BIT(i)) && test_bit(i, entry->irq_falling))) {
  199. irq_pin = irq_find_mapping(entry->domain, i);
  200. handle_nested_irq(irq_pin);
  201. }
  202. }
  203. }
  204. return IRQ_HANDLED;
  205. }
  206. static void smp2p_mask_irq(struct irq_data *irqd)
  207. {
  208. struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
  209. irq_hw_number_t irq = irqd_to_hwirq(irqd);
  210. clear_bit(irq, entry->irq_enabled);
  211. }
  212. static void smp2p_unmask_irq(struct irq_data *irqd)
  213. {
  214. struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
  215. irq_hw_number_t irq = irqd_to_hwirq(irqd);
  216. set_bit(irq, entry->irq_enabled);
  217. }
  218. static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
  219. {
  220. struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
  221. irq_hw_number_t irq = irqd_to_hwirq(irqd);
  222. if (!(type & IRQ_TYPE_EDGE_BOTH))
  223. return -EINVAL;
  224. if (type & IRQ_TYPE_EDGE_RISING)
  225. set_bit(irq, entry->irq_rising);
  226. else
  227. clear_bit(irq, entry->irq_rising);
  228. if (type & IRQ_TYPE_EDGE_FALLING)
  229. set_bit(irq, entry->irq_falling);
  230. else
  231. clear_bit(irq, entry->irq_falling);
  232. return 0;
  233. }
  234. static struct irq_chip smp2p_irq_chip = {
  235. .name = "smp2p",
  236. .irq_mask = smp2p_mask_irq,
  237. .irq_unmask = smp2p_unmask_irq,
  238. .irq_set_type = smp2p_set_irq_type,
  239. };
  240. static int smp2p_irq_map(struct irq_domain *d,
  241. unsigned int irq,
  242. irq_hw_number_t hw)
  243. {
  244. struct smp2p_entry *entry = d->host_data;
  245. irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
  246. irq_set_chip_data(irq, entry);
  247. irq_set_nested_thread(irq, 1);
  248. irq_set_noprobe(irq);
  249. return 0;
  250. }
  251. static const struct irq_domain_ops smp2p_irq_ops = {
  252. .map = smp2p_irq_map,
  253. .xlate = irq_domain_xlate_twocell,
  254. };
  255. static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
  256. struct smp2p_entry *entry,
  257. struct device_node *node)
  258. {
  259. entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
  260. if (!entry->domain) {
  261. dev_err(smp2p->dev, "failed to add irq_domain\n");
  262. return -ENOMEM;
  263. }
  264. return 0;
  265. }
  266. static int smp2p_update_bits(void *data, u32 mask, u32 value)
  267. {
  268. struct smp2p_entry *entry = data;
  269. unsigned long flags;
  270. u32 orig;
  271. u32 val;
  272. spin_lock_irqsave(&entry->lock, flags);
  273. val = orig = readl(entry->value);
  274. val &= ~mask;
  275. val |= value;
  276. writel(val, entry->value);
  277. spin_unlock_irqrestore(&entry->lock, flags);
  278. if (val != orig)
  279. qcom_smp2p_kick(entry->smp2p);
  280. return 0;
  281. }
  282. static const struct qcom_smem_state_ops smp2p_state_ops = {
  283. .update_bits = smp2p_update_bits,
  284. };
  285. static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
  286. struct smp2p_entry *entry,
  287. struct device_node *node)
  288. {
  289. struct smp2p_smem_item *out = smp2p->out;
  290. char buf[SMP2P_MAX_ENTRY_NAME] = {};
  291. /* Allocate an entry from the smem item */
  292. strlcpy(buf, entry->name, SMP2P_MAX_ENTRY_NAME);
  293. memcpy(out->entries[out->valid_entries].name, buf, SMP2P_MAX_ENTRY_NAME);
  294. /* Make the logical entry reference the physical value */
  295. entry->value = &out->entries[out->valid_entries].value;
  296. out->valid_entries++;
  297. entry->state = qcom_smem_state_register(node, &smp2p_state_ops, entry);
  298. if (IS_ERR(entry->state)) {
  299. dev_err(smp2p->dev, "failed to register qcom_smem_state\n");
  300. return PTR_ERR(entry->state);
  301. }
  302. return 0;
  303. }
  304. static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
  305. {
  306. struct smp2p_smem_item *out;
  307. unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
  308. unsigned pid = smp2p->remote_pid;
  309. int ret;
  310. ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
  311. if (ret < 0 && ret != -EEXIST) {
  312. if (ret != -EPROBE_DEFER)
  313. dev_err(smp2p->dev,
  314. "unable to allocate local smp2p item\n");
  315. return ret;
  316. }
  317. out = qcom_smem_get(pid, smem_id, NULL);
  318. if (IS_ERR(out)) {
  319. dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
  320. return PTR_ERR(out);
  321. }
  322. memset(out, 0, sizeof(*out));
  323. out->magic = SMP2P_MAGIC;
  324. out->local_pid = smp2p->local_pid;
  325. out->remote_pid = smp2p->remote_pid;
  326. out->total_entries = SMP2P_MAX_ENTRY;
  327. out->valid_entries = 0;
  328. /*
  329. * Make sure the rest of the header is written before we validate the
  330. * item by writing a valid version number.
  331. */
  332. wmb();
  333. out->version = 1;
  334. qcom_smp2p_kick(smp2p);
  335. smp2p->out = out;
  336. return 0;
  337. }
  338. static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
  339. {
  340. struct device_node *syscon;
  341. struct device *dev = smp2p->dev;
  342. const char *key;
  343. int ret;
  344. syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
  345. if (!syscon) {
  346. dev_err(dev, "no qcom,ipc node\n");
  347. return -ENODEV;
  348. }
  349. smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
  350. if (IS_ERR(smp2p->ipc_regmap))
  351. return PTR_ERR(smp2p->ipc_regmap);
  352. key = "qcom,ipc";
  353. ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
  354. if (ret < 0) {
  355. dev_err(dev, "no offset in %s\n", key);
  356. return -EINVAL;
  357. }
  358. ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
  359. if (ret < 0) {
  360. dev_err(dev, "no bit in %s\n", key);
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static int qcom_smp2p_probe(struct platform_device *pdev)
  366. {
  367. struct smp2p_entry *entry;
  368. struct device_node *node;
  369. struct qcom_smp2p *smp2p;
  370. const char *key;
  371. int irq;
  372. int ret;
  373. smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
  374. if (!smp2p)
  375. return -ENOMEM;
  376. smp2p->dev = &pdev->dev;
  377. INIT_LIST_HEAD(&smp2p->inbound);
  378. INIT_LIST_HEAD(&smp2p->outbound);
  379. platform_set_drvdata(pdev, smp2p);
  380. key = "qcom,smem";
  381. ret = of_property_read_u32_array(pdev->dev.of_node, key,
  382. smp2p->smem_items, 2);
  383. if (ret)
  384. return ret;
  385. key = "qcom,local-pid";
  386. ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
  387. if (ret)
  388. goto report_read_failure;
  389. key = "qcom,remote-pid";
  390. ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
  391. if (ret)
  392. goto report_read_failure;
  393. irq = platform_get_irq(pdev, 0);
  394. if (irq < 0) {
  395. dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
  396. return irq;
  397. }
  398. smp2p->mbox_client.dev = &pdev->dev;
  399. smp2p->mbox_client.knows_txdone = true;
  400. smp2p->mbox_chan = mbox_request_channel(&smp2p->mbox_client, 0);
  401. if (IS_ERR(smp2p->mbox_chan)) {
  402. if (PTR_ERR(smp2p->mbox_chan) != -ENODEV)
  403. return PTR_ERR(smp2p->mbox_chan);
  404. smp2p->mbox_chan = NULL;
  405. ret = smp2p_parse_ipc(smp2p);
  406. if (ret)
  407. return ret;
  408. }
  409. ret = qcom_smp2p_alloc_outbound_item(smp2p);
  410. if (ret < 0)
  411. goto release_mbox;
  412. for_each_available_child_of_node(pdev->dev.of_node, node) {
  413. entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
  414. if (!entry) {
  415. ret = -ENOMEM;
  416. goto unwind_interfaces;
  417. }
  418. entry->smp2p = smp2p;
  419. spin_lock_init(&entry->lock);
  420. ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
  421. if (ret < 0)
  422. goto unwind_interfaces;
  423. if (of_property_read_bool(node, "interrupt-controller")) {
  424. ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
  425. if (ret < 0)
  426. goto unwind_interfaces;
  427. list_add(&entry->node, &smp2p->inbound);
  428. } else {
  429. ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
  430. if (ret < 0)
  431. goto unwind_interfaces;
  432. list_add(&entry->node, &smp2p->outbound);
  433. }
  434. }
  435. /* Kick the outgoing edge after allocating entries */
  436. qcom_smp2p_kick(smp2p);
  437. ret = devm_request_threaded_irq(&pdev->dev, irq,
  438. NULL, qcom_smp2p_intr,
  439. IRQF_ONESHOT,
  440. "smp2p", (void *)smp2p);
  441. if (ret) {
  442. dev_err(&pdev->dev, "failed to request interrupt\n");
  443. goto unwind_interfaces;
  444. }
  445. return 0;
  446. unwind_interfaces:
  447. list_for_each_entry(entry, &smp2p->inbound, node)
  448. irq_domain_remove(entry->domain);
  449. list_for_each_entry(entry, &smp2p->outbound, node)
  450. qcom_smem_state_unregister(entry->state);
  451. smp2p->out->valid_entries = 0;
  452. release_mbox:
  453. mbox_free_channel(smp2p->mbox_chan);
  454. return ret;
  455. report_read_failure:
  456. dev_err(&pdev->dev, "failed to read %s\n", key);
  457. return -EINVAL;
  458. }
  459. static int qcom_smp2p_remove(struct platform_device *pdev)
  460. {
  461. struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
  462. struct smp2p_entry *entry;
  463. list_for_each_entry(entry, &smp2p->inbound, node)
  464. irq_domain_remove(entry->domain);
  465. list_for_each_entry(entry, &smp2p->outbound, node)
  466. qcom_smem_state_unregister(entry->state);
  467. mbox_free_channel(smp2p->mbox_chan);
  468. smp2p->out->valid_entries = 0;
  469. return 0;
  470. }
  471. static const struct of_device_id qcom_smp2p_of_match[] = {
  472. { .compatible = "qcom,smp2p" },
  473. {}
  474. };
  475. MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
  476. static struct platform_driver qcom_smp2p_driver = {
  477. .probe = qcom_smp2p_probe,
  478. .remove = qcom_smp2p_remove,
  479. .driver = {
  480. .name = "qcom_smp2p",
  481. .of_match_table = qcom_smp2p_of_match,
  482. },
  483. };
  484. module_platform_driver(qcom_smp2p_driver);
  485. MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
  486. MODULE_LICENSE("GPL v2");