smp2p.c 14 KB

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