controller.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Main SSAM/SSH controller structure and functionality.
  4. *
  5. * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
  6. */
  7. #ifndef _SURFACE_AGGREGATOR_CONTROLLER_H
  8. #define _SURFACE_AGGREGATOR_CONTROLLER_H
  9. #include <linux/kref.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/rbtree.h>
  13. #include <linux/rwsem.h>
  14. #include <linux/serdev.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/srcu.h>
  17. #include <linux/types.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/surface_aggregator/controller.h>
  20. #include <linux/surface_aggregator/serial_hub.h>
  21. #include "ssh_request_layer.h"
  22. /* -- Safe counters. -------------------------------------------------------- */
  23. /**
  24. * struct ssh_seq_counter - Safe counter for SSH sequence IDs.
  25. * @value: The current counter value.
  26. */
  27. struct ssh_seq_counter {
  28. u8 value;
  29. };
  30. /**
  31. * struct ssh_rqid_counter - Safe counter for SSH request IDs.
  32. * @value: The current counter value.
  33. */
  34. struct ssh_rqid_counter {
  35. u16 value;
  36. };
  37. /* -- Event/notification system. -------------------------------------------- */
  38. /**
  39. * struct ssam_nf_head - Notifier head for SSAM events.
  40. * @srcu: The SRCU struct for synchronization.
  41. * @head: List-head for notifier blocks registered under this head.
  42. */
  43. struct ssam_nf_head {
  44. struct srcu_struct srcu;
  45. struct list_head head;
  46. };
  47. /**
  48. * struct ssam_nf - Notifier callback- and activation-registry for SSAM events.
  49. * @lock: Lock guarding (de-)registration of notifier blocks. Note: This
  50. * lock does not need to be held for notifier calls, only
  51. * registration and deregistration.
  52. * @refcount: The root of the RB-tree used for reference-counting enabled
  53. * events/notifications.
  54. * @head: The list of notifier heads for event/notification callbacks.
  55. */
  56. struct ssam_nf {
  57. struct mutex lock;
  58. struct rb_root refcount;
  59. struct ssam_nf_head head[SSH_NUM_EVENTS];
  60. };
  61. /* -- Event/async request completion system. -------------------------------- */
  62. struct ssam_cplt;
  63. /**
  64. * struct ssam_event_item - Struct for event queuing and completion.
  65. * @node: The node in the queue.
  66. * @rqid: The request ID of the event.
  67. * @ops: Instance specific functions.
  68. * @ops.free: Callback for freeing this event item.
  69. * @event: Actual event data.
  70. */
  71. struct ssam_event_item {
  72. struct list_head node;
  73. u16 rqid;
  74. struct {
  75. void (*free)(struct ssam_event_item *event);
  76. } ops;
  77. struct ssam_event event; /* must be last */
  78. };
  79. /**
  80. * struct ssam_event_queue - Queue for completing received events.
  81. * @cplt: Reference to the completion system on which this queue is active.
  82. * @lock: The lock for any operation on the queue.
  83. * @head: The list-head of the queue.
  84. * @work: The &struct work_struct performing completion work for this queue.
  85. */
  86. struct ssam_event_queue {
  87. struct ssam_cplt *cplt;
  88. spinlock_t lock;
  89. struct list_head head;
  90. struct work_struct work;
  91. };
  92. /**
  93. * struct ssam_event_target - Set of queues for a single SSH target ID.
  94. * @queue: The array of queues, one queue per event ID.
  95. */
  96. struct ssam_event_target {
  97. struct ssam_event_queue queue[SSH_NUM_EVENTS];
  98. };
  99. /**
  100. * struct ssam_cplt - SSAM event/async request completion system.
  101. * @dev: The device with which this system is associated. Only used
  102. * for logging.
  103. * @wq: The &struct workqueue_struct on which all completion work
  104. * items are queued.
  105. * @event: Event completion management.
  106. * @event.target: Array of &struct ssam_event_target, one for each target.
  107. * @event.notif: Notifier callbacks and event activation reference counting.
  108. */
  109. struct ssam_cplt {
  110. struct device *dev;
  111. struct workqueue_struct *wq;
  112. struct {
  113. struct ssam_event_target target[SSH_NUM_TARGETS];
  114. struct ssam_nf notif;
  115. } event;
  116. };
  117. /* -- Main SSAM device structures. ------------------------------------------ */
  118. /**
  119. * enum ssam_controller_state - State values for &struct ssam_controller.
  120. * @SSAM_CONTROLLER_UNINITIALIZED:
  121. * The controller has not been initialized yet or has been deinitialized.
  122. * @SSAM_CONTROLLER_INITIALIZED:
  123. * The controller is initialized, but has not been started yet.
  124. * @SSAM_CONTROLLER_STARTED:
  125. * The controller has been started and is ready to use.
  126. * @SSAM_CONTROLLER_STOPPED:
  127. * The controller has been stopped.
  128. * @SSAM_CONTROLLER_SUSPENDED:
  129. * The controller has been suspended.
  130. */
  131. enum ssam_controller_state {
  132. SSAM_CONTROLLER_UNINITIALIZED,
  133. SSAM_CONTROLLER_INITIALIZED,
  134. SSAM_CONTROLLER_STARTED,
  135. SSAM_CONTROLLER_STOPPED,
  136. SSAM_CONTROLLER_SUSPENDED,
  137. };
  138. /**
  139. * struct ssam_controller_caps - Controller device capabilities.
  140. * @ssh_power_profile: SSH power profile.
  141. * @ssh_buffer_size: SSH driver UART buffer size.
  142. * @screen_on_sleep_idle_timeout: SAM UART screen-on sleep idle timeout.
  143. * @screen_off_sleep_idle_timeout: SAM UART screen-off sleep idle timeout.
  144. * @d3_closes_handle: SAM closes UART handle in D3.
  145. *
  146. * Controller and SSH device capabilities found in ACPI.
  147. */
  148. struct ssam_controller_caps {
  149. u32 ssh_power_profile;
  150. u32 ssh_buffer_size;
  151. u32 screen_on_sleep_idle_timeout;
  152. u32 screen_off_sleep_idle_timeout;
  153. u32 d3_closes_handle:1;
  154. };
  155. /**
  156. * struct ssam_controller - SSAM controller device.
  157. * @kref: Reference count of the controller.
  158. * @lock: Main lock for the controller, used to guard state changes.
  159. * @state: Controller state.
  160. * @rtl: Request transport layer for SSH I/O.
  161. * @cplt: Completion system for SSH/SSAM events and asynchronous requests.
  162. * @counter: Safe SSH message ID counters.
  163. * @counter.seq: Sequence ID counter.
  164. * @counter.rqid: Request ID counter.
  165. * @irq: Wakeup IRQ resources.
  166. * @irq.num: The wakeup IRQ number.
  167. * @irq.wakeup_enabled: Whether wakeup by IRQ is enabled during suspend.
  168. * @caps: The controller device capabilities.
  169. */
  170. struct ssam_controller {
  171. struct kref kref;
  172. struct rw_semaphore lock;
  173. enum ssam_controller_state state;
  174. struct ssh_rtl rtl;
  175. struct ssam_cplt cplt;
  176. struct {
  177. struct ssh_seq_counter seq;
  178. struct ssh_rqid_counter rqid;
  179. } counter;
  180. struct {
  181. int num;
  182. bool wakeup_enabled;
  183. } irq;
  184. struct ssam_controller_caps caps;
  185. };
  186. #define to_ssam_controller(ptr, member) \
  187. container_of(ptr, struct ssam_controller, member)
  188. #define ssam_dbg(ctrl, fmt, ...) rtl_dbg(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  189. #define ssam_info(ctrl, fmt, ...) rtl_info(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  190. #define ssam_warn(ctrl, fmt, ...) rtl_warn(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  191. #define ssam_err(ctrl, fmt, ...) rtl_err(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  192. /**
  193. * ssam_controller_receive_buf() - Provide input-data to the controller.
  194. * @ctrl: The controller.
  195. * @buf: The input buffer.
  196. * @n: The number of bytes in the input buffer.
  197. *
  198. * Provide input data to be evaluated by the controller, which has been
  199. * received via the lower-level transport.
  200. *
  201. * Return: Returns the number of bytes consumed, or, if the packet transport
  202. * layer of the controller has been shut down, %-ESHUTDOWN.
  203. */
  204. static inline
  205. ssize_t ssam_controller_receive_buf(struct ssam_controller *ctrl, const u8 *buf,
  206. size_t n)
  207. {
  208. return ssh_ptl_rx_rcvbuf(&ctrl->rtl.ptl, buf, n);
  209. }
  210. /**
  211. * ssam_controller_write_wakeup() - Notify the controller that the underlying
  212. * device has space available for data to be written.
  213. * @ctrl: The controller.
  214. */
  215. static inline void ssam_controller_write_wakeup(struct ssam_controller *ctrl)
  216. {
  217. ssh_ptl_tx_wakeup_transfer(&ctrl->rtl.ptl);
  218. }
  219. int ssam_controller_init(struct ssam_controller *ctrl, struct serdev_device *s);
  220. int ssam_controller_start(struct ssam_controller *ctrl);
  221. void ssam_controller_shutdown(struct ssam_controller *ctrl);
  222. void ssam_controller_destroy(struct ssam_controller *ctrl);
  223. int ssam_notifier_disable_registered(struct ssam_controller *ctrl);
  224. void ssam_notifier_restore_registered(struct ssam_controller *ctrl);
  225. int ssam_irq_setup(struct ssam_controller *ctrl);
  226. void ssam_irq_free(struct ssam_controller *ctrl);
  227. int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl);
  228. void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl);
  229. void ssam_controller_lock(struct ssam_controller *c);
  230. void ssam_controller_unlock(struct ssam_controller *c);
  231. int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version);
  232. int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl);
  233. int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl);
  234. int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl);
  235. int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl);
  236. int ssam_controller_suspend(struct ssam_controller *ctrl);
  237. int ssam_controller_resume(struct ssam_controller *ctrl);
  238. int ssam_event_item_cache_init(void);
  239. void ssam_event_item_cache_destroy(void);
  240. #endif /* _SURFACE_AGGREGATOR_CONTROLLER_H */