smp2p.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. unsigned long flags;
  278. u32 orig;
  279. u32 val;
  280. spin_lock_irqsave(&entry->lock, flags);
  281. val = orig = readl(entry->value);
  282. val &= ~mask;
  283. val |= value;
  284. writel(val, entry->value);
  285. spin_unlock_irqrestore(&entry->lock, flags);
  286. if (val != orig)
  287. qcom_smp2p_kick(entry->smp2p);
  288. return 0;
  289. }
  290. static const struct qcom_smem_state_ops smp2p_state_ops = {
  291. .update_bits = smp2p_update_bits,
  292. };
  293. static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
  294. struct smp2p_entry *entry,
  295. struct device_node *node)
  296. {
  297. struct smp2p_smem_item *out = smp2p->out;
  298. char buf[SMP2P_MAX_ENTRY_NAME] = {};
  299. /* Allocate an entry from the smem item */
  300. strlcpy(buf, entry->name, SMP2P_MAX_ENTRY_NAME);
  301. memcpy(out->entries[out->valid_entries].name, buf, SMP2P_MAX_ENTRY_NAME);
  302. /* Make the logical entry reference the physical value */
  303. entry->value = &out->entries[out->valid_entries].value;
  304. out->valid_entries++;
  305. entry->state = qcom_smem_state_register(node, &smp2p_state_ops, entry);
  306. if (IS_ERR(entry->state)) {
  307. dev_err(smp2p->dev, "failed to register qcom_smem_state\n");
  308. return PTR_ERR(entry->state);
  309. }
  310. return 0;
  311. }
  312. static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
  313. {
  314. struct smp2p_smem_item *out;
  315. unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
  316. unsigned pid = smp2p->remote_pid;
  317. int ret;
  318. ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
  319. if (ret < 0 && ret != -EEXIST) {
  320. if (ret != -EPROBE_DEFER)
  321. dev_err(smp2p->dev,
  322. "unable to allocate local smp2p item\n");
  323. return ret;
  324. }
  325. out = qcom_smem_get(pid, smem_id, NULL);
  326. if (IS_ERR(out)) {
  327. dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
  328. return PTR_ERR(out);
  329. }
  330. memset(out, 0, sizeof(*out));
  331. out->magic = SMP2P_MAGIC;
  332. out->local_pid = smp2p->local_pid;
  333. out->remote_pid = smp2p->remote_pid;
  334. out->total_entries = SMP2P_MAX_ENTRY;
  335. out->valid_entries = 0;
  336. /*
  337. * Make sure the rest of the header is written before we validate the
  338. * item by writing a valid version number.
  339. */
  340. wmb();
  341. out->version = 1;
  342. qcom_smp2p_kick(smp2p);
  343. smp2p->out = out;
  344. return 0;
  345. }
  346. static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
  347. {
  348. struct device_node *syscon;
  349. struct device *dev = smp2p->dev;
  350. const char *key;
  351. int ret;
  352. syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
  353. if (!syscon) {
  354. dev_err(dev, "no qcom,ipc node\n");
  355. return -ENODEV;
  356. }
  357. smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
  358. if (IS_ERR(smp2p->ipc_regmap))
  359. return PTR_ERR(smp2p->ipc_regmap);
  360. key = "qcom,ipc";
  361. ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
  362. if (ret < 0) {
  363. dev_err(dev, "no offset in %s\n", key);
  364. return -EINVAL;
  365. }
  366. ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
  367. if (ret < 0) {
  368. dev_err(dev, "no bit in %s\n", key);
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. static int qcom_smp2p_probe(struct platform_device *pdev)
  374. {
  375. struct smp2p_entry *entry;
  376. struct device_node *node;
  377. struct qcom_smp2p *smp2p;
  378. const char *key;
  379. int irq;
  380. int ret;
  381. smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
  382. if (!smp2p)
  383. return -ENOMEM;
  384. smp2p->dev = &pdev->dev;
  385. INIT_LIST_HEAD(&smp2p->inbound);
  386. INIT_LIST_HEAD(&smp2p->outbound);
  387. platform_set_drvdata(pdev, smp2p);
  388. key = "qcom,smem";
  389. ret = of_property_read_u32_array(pdev->dev.of_node, key,
  390. smp2p->smem_items, 2);
  391. if (ret)
  392. return ret;
  393. key = "qcom,local-pid";
  394. ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
  395. if (ret)
  396. goto report_read_failure;
  397. key = "qcom,remote-pid";
  398. ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
  399. if (ret)
  400. goto report_read_failure;
  401. irq = platform_get_irq(pdev, 0);
  402. if (irq < 0) {
  403. dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
  404. return irq;
  405. }
  406. smp2p->mbox_client.dev = &pdev->dev;
  407. smp2p->mbox_client.knows_txdone = true;
  408. smp2p->mbox_chan = mbox_request_channel(&smp2p->mbox_client, 0);
  409. if (IS_ERR(smp2p->mbox_chan)) {
  410. if (PTR_ERR(smp2p->mbox_chan) != -ENODEV)
  411. return PTR_ERR(smp2p->mbox_chan);
  412. smp2p->mbox_chan = NULL;
  413. ret = smp2p_parse_ipc(smp2p);
  414. if (ret)
  415. return ret;
  416. }
  417. ret = qcom_smp2p_alloc_outbound_item(smp2p);
  418. if (ret < 0)
  419. goto release_mbox;
  420. for_each_available_child_of_node(pdev->dev.of_node, node) {
  421. entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
  422. if (!entry) {
  423. ret = -ENOMEM;
  424. goto unwind_interfaces;
  425. }
  426. entry->smp2p = smp2p;
  427. spin_lock_init(&entry->lock);
  428. ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
  429. if (ret < 0)
  430. goto unwind_interfaces;
  431. if (of_property_read_bool(node, "interrupt-controller")) {
  432. ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
  433. if (ret < 0)
  434. goto unwind_interfaces;
  435. list_add(&entry->node, &smp2p->inbound);
  436. } else {
  437. ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
  438. if (ret < 0)
  439. goto unwind_interfaces;
  440. list_add(&entry->node, &smp2p->outbound);
  441. }
  442. }
  443. /* Kick the outgoing edge after allocating entries */
  444. qcom_smp2p_kick(smp2p);
  445. ret = devm_request_threaded_irq(&pdev->dev, irq,
  446. NULL, qcom_smp2p_intr,
  447. IRQF_ONESHOT,
  448. "smp2p", (void *)smp2p);
  449. if (ret) {
  450. dev_err(&pdev->dev, "failed to request interrupt\n");
  451. goto unwind_interfaces;
  452. }
  453. return 0;
  454. unwind_interfaces:
  455. list_for_each_entry(entry, &smp2p->inbound, node)
  456. irq_domain_remove(entry->domain);
  457. list_for_each_entry(entry, &smp2p->outbound, node)
  458. qcom_smem_state_unregister(entry->state);
  459. smp2p->out->valid_entries = 0;
  460. release_mbox:
  461. mbox_free_channel(smp2p->mbox_chan);
  462. return ret;
  463. report_read_failure:
  464. dev_err(&pdev->dev, "failed to read %s\n", key);
  465. return -EINVAL;
  466. }
  467. static int qcom_smp2p_remove(struct platform_device *pdev)
  468. {
  469. struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
  470. struct smp2p_entry *entry;
  471. list_for_each_entry(entry, &smp2p->inbound, node)
  472. irq_domain_remove(entry->domain);
  473. list_for_each_entry(entry, &smp2p->outbound, node)
  474. qcom_smem_state_unregister(entry->state);
  475. mbox_free_channel(smp2p->mbox_chan);
  476. smp2p->out->valid_entries = 0;
  477. return 0;
  478. }
  479. static const struct of_device_id qcom_smp2p_of_match[] = {
  480. { .compatible = "qcom,smp2p" },
  481. {}
  482. };
  483. MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
  484. static struct platform_driver qcom_smp2p_driver = {
  485. .probe = qcom_smp2p_probe,
  486. .remove = qcom_smp2p_remove,
  487. .driver = {
  488. .name = "qcom_smp2p",
  489. .of_match_table = qcom_smp2p_of_match,
  490. },
  491. };
  492. module_platform_driver(qcom_smp2p_driver);
  493. MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
  494. MODULE_LICENSE("GPL v2");