smp2p.c 15 KB

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