subsystem_notif_virt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/err.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/notifier.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/of.h>
  13. #include <linux/io.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/workqueue.h>
  21. #include "qcom_common.h"
  22. #define CLIENT_STATE_OFFSET 4
  23. #define SUBSYS_STATE_OFFSET 8
  24. static void __iomem *base_reg;
  25. enum subsystem_type {
  26. VIRTUAL,
  27. NATIVE,
  28. };
  29. struct subsystem_descriptor {
  30. const char *name;
  31. u32 offset;
  32. enum subsystem_type type;
  33. struct notifier_block nb;
  34. void *handle;
  35. int ssr_irq;
  36. struct list_head subsystem_list;
  37. struct work_struct work;
  38. };
  39. static LIST_HEAD(subsystem_descriptor_list);
  40. static struct workqueue_struct *ssr_wq;
  41. static void subsystem_notif_wq_func(struct work_struct *work)
  42. {
  43. struct subsystem_descriptor *subsystem =
  44. container_of(work, struct subsystem_descriptor, work);
  45. void *subsystem_handle;
  46. int state, ret;
  47. state = readl_relaxed(base_reg + subsystem->offset);
  48. subsystem_handle = qcom_ssr_get_subsys(subsystem->name);
  49. ret = qcom_notify_ssr_clients(subsystem_handle, state, NULL);
  50. writel_relaxed(ret, base_reg + subsystem->offset + CLIENT_STATE_OFFSET);
  51. }
  52. static int subsystem_state_callback(struct notifier_block *this,
  53. unsigned long value, void *priv)
  54. {
  55. struct subsystem_descriptor *subsystem =
  56. container_of(this, struct subsystem_descriptor, nb);
  57. writel_relaxed(value, base_reg + subsystem->offset +
  58. SUBSYS_STATE_OFFSET);
  59. return NOTIFY_OK;
  60. }
  61. static irqreturn_t subsystem_restart_irq_handler(int irq, void *dev_id)
  62. {
  63. struct subsystem_descriptor *subsystem = dev_id;
  64. queue_work(ssr_wq, &subsystem->work);
  65. return IRQ_HANDLED;
  66. }
  67. static int get_resources(struct platform_device *pdev)
  68. {
  69. struct device_node *node;
  70. struct device_node *child = NULL;
  71. const char *ss_type;
  72. struct resource *res;
  73. struct subsystem_descriptor *subsystem = NULL;
  74. int ret = 0;
  75. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vdev_base");
  76. base_reg = devm_ioremap_resource(&pdev->dev, res);
  77. if (IS_ERR_OR_NULL(base_reg)) {
  78. dev_err(&pdev->dev, "Memory mapping failed\n");
  79. return -ENOMEM;
  80. }
  81. node = pdev->dev.of_node;
  82. for_each_child_of_node(node, child) {
  83. subsystem = devm_kzalloc(&pdev->dev,
  84. sizeof(struct subsystem_descriptor),
  85. GFP_KERNEL);
  86. if (!subsystem)
  87. return -ENOMEM;
  88. subsystem->name =
  89. of_get_property(child, "subsys-name", NULL);
  90. if (IS_ERR_OR_NULL(subsystem->name)) {
  91. dev_err(&pdev->dev, "Could not find subsystem name\n");
  92. return -EINVAL;
  93. }
  94. ret = of_property_read_u32(child, "offset",
  95. &subsystem->offset);
  96. if (ret) {
  97. dev_err(&pdev->dev, "offset reading for %s failed\n",
  98. subsystem->name);
  99. return -EINVAL;
  100. }
  101. ret = of_property_read_string(child, "type",
  102. &ss_type);
  103. if (ret) {
  104. dev_err(&pdev->dev, "type reading for %s failed\n",
  105. subsystem->name);
  106. return -EINVAL;
  107. }
  108. if (!strcmp(ss_type, "virtual"))
  109. subsystem->type = VIRTUAL;
  110. if (!strcmp(ss_type, "native"))
  111. subsystem->type = NATIVE;
  112. switch (subsystem->type) {
  113. case NATIVE:
  114. subsystem->nb.notifier_call =
  115. subsystem_state_callback;
  116. subsystem->handle =
  117. qcom_register_ssr_notifier(
  118. subsystem->name, &subsystem->nb);
  119. if (IS_ERR_OR_NULL(subsystem->handle)) {
  120. dev_err(&pdev->dev,
  121. "Could not register SSR notifier cb\n");
  122. return -EINVAL;
  123. }
  124. list_add_tail(&subsystem->subsystem_list,
  125. &subsystem_descriptor_list);
  126. break;
  127. case VIRTUAL:
  128. subsystem->ssr_irq =
  129. of_irq_get_byname(child, "state-irq");
  130. if (subsystem->ssr_irq < 0) {
  131. dev_err(&pdev->dev, "Could not find IRQ\n");
  132. return -EINVAL;
  133. }
  134. ret = devm_request_threaded_irq(&pdev->dev,
  135. subsystem->ssr_irq, NULL,
  136. subsystem_restart_irq_handler,
  137. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  138. subsystem->name, subsystem);
  139. INIT_WORK(&subsystem->work, subsystem_notif_wq_func);
  140. break;
  141. default:
  142. dev_err(&pdev->dev, "Unsupported type %d\n",
  143. subsystem->type);
  144. }
  145. }
  146. return 0;
  147. }
  148. static void release_resources(void)
  149. {
  150. struct subsystem_descriptor *subsystem, *node;
  151. list_for_each_entry_safe(subsystem, node, &subsystem_descriptor_list,
  152. subsystem_list) {
  153. qcom_unregister_ssr_notifier(subsystem->handle,
  154. &subsystem->nb);
  155. list_del(&subsystem->subsystem_list);
  156. }
  157. }
  158. static int subsys_notif_virt_probe(struct platform_device *pdev)
  159. {
  160. int ret = 0;
  161. if (!pdev) {
  162. pr_err("%s: pdev is NULL\n", __func__);
  163. return -EINVAL;
  164. }
  165. ssr_wq = create_singlethread_workqueue("ssr_wq");
  166. if (!ssr_wq) {
  167. dev_err(&pdev->dev, "Workqueue creation failed\n");
  168. return -ENOMEM;
  169. }
  170. ret = get_resources(pdev);
  171. if (ret)
  172. destroy_workqueue(ssr_wq);
  173. return ret;
  174. }
  175. static int subsys_notif_virt_remove(struct platform_device *pdev)
  176. {
  177. destroy_workqueue(ssr_wq);
  178. release_resources();
  179. return 0;
  180. }
  181. static const struct of_device_id match_table[] = {
  182. { .compatible = "qcom,subsys-notif-virt" },
  183. {},
  184. };
  185. static struct platform_driver subsys_notif_virt_driver = {
  186. .probe = subsys_notif_virt_probe,
  187. .remove = subsys_notif_virt_remove,
  188. .driver = {
  189. .name = "subsys_notif_virt",
  190. .of_match_table = match_table,
  191. },
  192. };
  193. static int __init subsys_notif_virt_init(void)
  194. {
  195. return platform_driver_register(&subsys_notif_virt_driver);
  196. }
  197. module_init(subsys_notif_virt_init);
  198. static void __exit subsys_notif_virt_exit(void)
  199. {
  200. platform_driver_unregister(&subsys_notif_virt_driver);
  201. }
  202. module_exit(subsys_notif_virt_exit);
  203. MODULE_DESCRIPTION("Subsystem Notification Virtual Driver");
  204. MODULE_LICENSE("GPL");