1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * System Control and Management Interface (SCMI) Notification support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2020 ARM Ltd.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun /**
8*4882a593Smuzhiyun * DOC: Theory of operation
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * SCMI Protocol specification allows the platform to signal events to
11*4882a593Smuzhiyun * interested agents via notification messages: this is an implementation
12*4882a593Smuzhiyun * of the dispatch and delivery of such notifications to the interested users
13*4882a593Smuzhiyun * inside the Linux kernel.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * An SCMI Notification core instance is initialized for each active platform
16*4882a593Smuzhiyun * instance identified by the means of the usual &struct scmi_handle.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Each SCMI Protocol implementation, during its initialization, registers with
19*4882a593Smuzhiyun * this core its set of supported events using scmi_register_protocol_events():
20*4882a593Smuzhiyun * all the needed descriptors are stored in the &struct registered_protocols and
21*4882a593Smuzhiyun * &struct registered_events arrays.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Kernel users interested in some specific event can register their callbacks
24*4882a593Smuzhiyun * providing the usual notifier_block descriptor, since this core implements
25*4882a593Smuzhiyun * events' delivery using the standard Kernel notification chains machinery.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * Given the number of possible events defined by SCMI and the extensibility
28*4882a593Smuzhiyun * of the SCMI Protocol itself, the underlying notification chains are created
29*4882a593Smuzhiyun * and destroyed dynamically on demand depending on the number of users
30*4882a593Smuzhiyun * effectively registered for an event, so that no support structures or chains
31*4882a593Smuzhiyun * are allocated until at least one user has registered a notifier_block for
32*4882a593Smuzhiyun * such event. Similarly, events' generation itself is enabled at the platform
33*4882a593Smuzhiyun * level only after at least one user has registered, and it is shutdown after
34*4882a593Smuzhiyun * the last user for that event has gone.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * All users provided callbacks and allocated notification-chains are stored in
37*4882a593Smuzhiyun * the @registered_events_handlers hashtable. Callbacks' registration requests
38*4882a593Smuzhiyun * for still to be registered events are instead kept in the dedicated common
39*4882a593Smuzhiyun * hashtable @pending_events_handlers.
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * An event is identified univocally by the tuple (proto_id, evt_id, src_id)
42*4882a593Smuzhiyun * and is served by its own dedicated notification chain; information contained
43*4882a593Smuzhiyun * in such tuples is used, in a few different ways, to generate the needed
44*4882a593Smuzhiyun * hash-keys.
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * Here proto_id and evt_id are simply the protocol_id and message_id numbers
47*4882a593Smuzhiyun * as described in the SCMI Protocol specification, while src_id represents an
48*4882a593Smuzhiyun * optional, protocol dependent, source identifier (like domain_id, perf_id
49*4882a593Smuzhiyun * or sensor_id and so forth).
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Upon reception of a notification message from the platform the SCMI RX ISR
52*4882a593Smuzhiyun * passes the received message payload and some ancillary information (including
53*4882a593Smuzhiyun * an arrival timestamp in nanoseconds) to the core via @scmi_notify() which
54*4882a593Smuzhiyun * pushes the event-data itself on a protocol-dedicated kfifo queue for further
55*4882a593Smuzhiyun * deferred processing as specified in @scmi_events_dispatcher().
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * Each protocol has it own dedicated work_struct and worker which, once kicked
58*4882a593Smuzhiyun * by the ISR, takes care to empty its own dedicated queue, deliverying the
59*4882a593Smuzhiyun * queued items into the proper notification-chain: notifications processing can
60*4882a593Smuzhiyun * proceed concurrently on distinct workers only between events belonging to
61*4882a593Smuzhiyun * different protocols while delivery of events within the same protocol is
62*4882a593Smuzhiyun * still strictly sequentially ordered by time of arrival.
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Events' information is then extracted from the SCMI Notification messages and
65*4882a593Smuzhiyun * conveyed, converted into a custom per-event report struct, as the void *data
66*4882a593Smuzhiyun * param to the user callback provided by the registered notifier_block, so that
67*4882a593Smuzhiyun * from the user perspective his callback will look invoked like:
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * int user_cb(struct notifier_block *nb, unsigned long event_id, void *report)
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun #define dev_fmt(fmt) "SCMI Notifications - " fmt
74*4882a593Smuzhiyun #define pr_fmt(fmt) "SCMI Notifications - " fmt
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun #include <linux/bitfield.h>
77*4882a593Smuzhiyun #include <linux/bug.h>
78*4882a593Smuzhiyun #include <linux/compiler.h>
79*4882a593Smuzhiyun #include <linux/device.h>
80*4882a593Smuzhiyun #include <linux/err.h>
81*4882a593Smuzhiyun #include <linux/hashtable.h>
82*4882a593Smuzhiyun #include <linux/kernel.h>
83*4882a593Smuzhiyun #include <linux/ktime.h>
84*4882a593Smuzhiyun #include <linux/kfifo.h>
85*4882a593Smuzhiyun #include <linux/list.h>
86*4882a593Smuzhiyun #include <linux/mutex.h>
87*4882a593Smuzhiyun #include <linux/notifier.h>
88*4882a593Smuzhiyun #include <linux/refcount.h>
89*4882a593Smuzhiyun #include <linux/scmi_protocol.h>
90*4882a593Smuzhiyun #include <linux/slab.h>
91*4882a593Smuzhiyun #include <linux/types.h>
92*4882a593Smuzhiyun #include <linux/workqueue.h>
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun #include "common.h"
95*4882a593Smuzhiyun #include "notify.h"
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun #define SCMI_MAX_PROTO 256
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun #define PROTO_ID_MASK GENMASK(31, 24)
100*4882a593Smuzhiyun #define EVT_ID_MASK GENMASK(23, 16)
101*4882a593Smuzhiyun #define SRC_ID_MASK GENMASK(15, 0)
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Builds an unsigned 32bit key from the given input tuple to be used
105*4882a593Smuzhiyun * as a key in hashtables.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun #define MAKE_HASH_KEY(p, e, s) \
108*4882a593Smuzhiyun (FIELD_PREP(PROTO_ID_MASK, (p)) | \
109*4882a593Smuzhiyun FIELD_PREP(EVT_ID_MASK, (e)) | \
110*4882a593Smuzhiyun FIELD_PREP(SRC_ID_MASK, (s)))
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #define MAKE_ALL_SRCS_KEY(p, e) MAKE_HASH_KEY((p), (e), SRC_ID_MASK)
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * Assumes that the stored obj includes its own hash-key in a field named 'key':
116*4882a593Smuzhiyun * with this simplification this macro can be equally used for all the objects'
117*4882a593Smuzhiyun * types hashed by this implementation.
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * @__ht: The hashtable name
120*4882a593Smuzhiyun * @__obj: A pointer to the object type to be retrieved from the hashtable;
121*4882a593Smuzhiyun * it will be used as a cursor while scanning the hastable and it will
122*4882a593Smuzhiyun * be possibly left as NULL when @__k is not found
123*4882a593Smuzhiyun * @__k: The key to search for
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun #define KEY_FIND(__ht, __obj, __k) \
126*4882a593Smuzhiyun ({ \
127*4882a593Smuzhiyun typeof(__k) k_ = __k; \
128*4882a593Smuzhiyun typeof(__obj) obj_; \
129*4882a593Smuzhiyun \
130*4882a593Smuzhiyun hash_for_each_possible((__ht), obj_, hash, k_) \
131*4882a593Smuzhiyun if (obj_->key == k_) \
132*4882a593Smuzhiyun break; \
133*4882a593Smuzhiyun __obj = obj_; \
134*4882a593Smuzhiyun })
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define KEY_XTRACT_PROTO_ID(key) FIELD_GET(PROTO_ID_MASK, (key))
137*4882a593Smuzhiyun #define KEY_XTRACT_EVT_ID(key) FIELD_GET(EVT_ID_MASK, (key))
138*4882a593Smuzhiyun #define KEY_XTRACT_SRC_ID(key) FIELD_GET(SRC_ID_MASK, (key))
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * A set of macros used to access safely @registered_protocols and
142*4882a593Smuzhiyun * @registered_events arrays; these are fixed in size and each entry is possibly
143*4882a593Smuzhiyun * populated at protocols' registration time and then only read but NEVER
144*4882a593Smuzhiyun * modified or removed.
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun #define SCMI_GET_PROTO(__ni, __pid) \
147*4882a593Smuzhiyun ({ \
148*4882a593Smuzhiyun typeof(__ni) ni_ = __ni; \
149*4882a593Smuzhiyun struct scmi_registered_events_desc *__pd = NULL; \
150*4882a593Smuzhiyun \
151*4882a593Smuzhiyun if (ni_) \
152*4882a593Smuzhiyun __pd = READ_ONCE(ni_->registered_protocols[(__pid)]); \
153*4882a593Smuzhiyun __pd; \
154*4882a593Smuzhiyun })
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun #define SCMI_GET_REVT_FROM_PD(__pd, __eid) \
157*4882a593Smuzhiyun ({ \
158*4882a593Smuzhiyun typeof(__pd) pd_ = __pd; \
159*4882a593Smuzhiyun typeof(__eid) eid_ = __eid; \
160*4882a593Smuzhiyun struct scmi_registered_event *__revt = NULL; \
161*4882a593Smuzhiyun \
162*4882a593Smuzhiyun if (pd_ && eid_ < pd_->num_events) \
163*4882a593Smuzhiyun __revt = READ_ONCE(pd_->registered_events[eid_]); \
164*4882a593Smuzhiyun __revt; \
165*4882a593Smuzhiyun })
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun #define SCMI_GET_REVT(__ni, __pid, __eid) \
168*4882a593Smuzhiyun ({ \
169*4882a593Smuzhiyun struct scmi_registered_event *__revt; \
170*4882a593Smuzhiyun struct scmi_registered_events_desc *__pd; \
171*4882a593Smuzhiyun \
172*4882a593Smuzhiyun __pd = SCMI_GET_PROTO((__ni), (__pid)); \
173*4882a593Smuzhiyun __revt = SCMI_GET_REVT_FROM_PD(__pd, (__eid)); \
174*4882a593Smuzhiyun __revt; \
175*4882a593Smuzhiyun })
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /* A couple of utility macros to limit cruft when calling protocols' helpers */
178*4882a593Smuzhiyun #define REVT_NOTIFY_SET_STATUS(revt, eid, sid, state) \
179*4882a593Smuzhiyun ({ \
180*4882a593Smuzhiyun typeof(revt) r = revt; \
181*4882a593Smuzhiyun r->proto->ops->set_notify_enabled(r->proto->ph, \
182*4882a593Smuzhiyun (eid), (sid), (state)); \
183*4882a593Smuzhiyun })
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun #define REVT_NOTIFY_ENABLE(revt, eid, sid) \
186*4882a593Smuzhiyun REVT_NOTIFY_SET_STATUS((revt), (eid), (sid), true)
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun #define REVT_NOTIFY_DISABLE(revt, eid, sid) \
189*4882a593Smuzhiyun REVT_NOTIFY_SET_STATUS((revt), (eid), (sid), false)
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun #define REVT_FILL_REPORT(revt, ...) \
192*4882a593Smuzhiyun ({ \
193*4882a593Smuzhiyun typeof(revt) r = revt; \
194*4882a593Smuzhiyun r->proto->ops->fill_custom_report(r->proto->ph, \
195*4882a593Smuzhiyun __VA_ARGS__); \
196*4882a593Smuzhiyun })
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun #define SCMI_PENDING_HASH_SZ 4
199*4882a593Smuzhiyun #define SCMI_REGISTERED_HASH_SZ 6
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun struct scmi_registered_events_desc;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun * struct scmi_notify_instance - Represents an instance of the notification
205*4882a593Smuzhiyun * core
206*4882a593Smuzhiyun * @gid: GroupID used for devres
207*4882a593Smuzhiyun * @handle: A reference to the platform instance
208*4882a593Smuzhiyun * @init_work: A work item to perform final initializations of pending handlers
209*4882a593Smuzhiyun * @notify_wq: A reference to the allocated Kernel cmwq
210*4882a593Smuzhiyun * @pending_mtx: A mutex to protect @pending_events_handlers
211*4882a593Smuzhiyun * @registered_protocols: A statically allocated array containing pointers to
212*4882a593Smuzhiyun * all the registered protocol-level specific information
213*4882a593Smuzhiyun * related to events' handling
214*4882a593Smuzhiyun * @pending_events_handlers: An hashtable containing all pending events'
215*4882a593Smuzhiyun * handlers descriptors
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * Each platform instance, represented by a handle, has its own instance of
218*4882a593Smuzhiyun * the notification subsystem represented by this structure.
219*4882a593Smuzhiyun */
220*4882a593Smuzhiyun struct scmi_notify_instance {
221*4882a593Smuzhiyun void *gid;
222*4882a593Smuzhiyun struct scmi_handle *handle;
223*4882a593Smuzhiyun struct work_struct init_work;
224*4882a593Smuzhiyun struct workqueue_struct *notify_wq;
225*4882a593Smuzhiyun /* lock to protect pending_events_handlers */
226*4882a593Smuzhiyun struct mutex pending_mtx;
227*4882a593Smuzhiyun struct scmi_registered_events_desc **registered_protocols;
228*4882a593Smuzhiyun DECLARE_HASHTABLE(pending_events_handlers, SCMI_PENDING_HASH_SZ);
229*4882a593Smuzhiyun };
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * struct events_queue - Describes a queue and its associated worker
233*4882a593Smuzhiyun * @sz: Size in bytes of the related kfifo
234*4882a593Smuzhiyun * @kfifo: A dedicated Kernel kfifo descriptor
235*4882a593Smuzhiyun * @notify_work: A custom work item bound to this queue
236*4882a593Smuzhiyun * @wq: A reference to the associated workqueue
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Each protocol has its own dedicated events_queue descriptor.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun struct events_queue {
241*4882a593Smuzhiyun size_t sz;
242*4882a593Smuzhiyun struct kfifo kfifo;
243*4882a593Smuzhiyun struct work_struct notify_work;
244*4882a593Smuzhiyun struct workqueue_struct *wq;
245*4882a593Smuzhiyun };
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /**
248*4882a593Smuzhiyun * struct scmi_event_header - A utility header
249*4882a593Smuzhiyun * @timestamp: The timestamp, in nanoseconds (boottime), which was associated
250*4882a593Smuzhiyun * to this event as soon as it entered the SCMI RX ISR
251*4882a593Smuzhiyun * @payld_sz: Effective size of the embedded message payload which follows
252*4882a593Smuzhiyun * @evt_id: Event ID (corresponds to the Event MsgID for this Protocol)
253*4882a593Smuzhiyun * @payld: A reference to the embedded event payload
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * This header is prepended to each received event message payload before
256*4882a593Smuzhiyun * queueing it on the related &struct events_queue.
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun struct scmi_event_header {
259*4882a593Smuzhiyun ktime_t timestamp;
260*4882a593Smuzhiyun size_t payld_sz;
261*4882a593Smuzhiyun unsigned char evt_id;
262*4882a593Smuzhiyun unsigned char payld[];
263*4882a593Smuzhiyun };
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun struct scmi_registered_event;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /**
268*4882a593Smuzhiyun * struct scmi_registered_events_desc - Protocol Specific information
269*4882a593Smuzhiyun * @id: Protocol ID
270*4882a593Smuzhiyun * @ops: Protocol specific and event-related operations
271*4882a593Smuzhiyun * @equeue: The embedded per-protocol events_queue
272*4882a593Smuzhiyun * @ni: A reference to the initialized instance descriptor
273*4882a593Smuzhiyun * @eh: A reference to pre-allocated buffer to be used as a scratch area by the
274*4882a593Smuzhiyun * deferred worker when fetching data from the kfifo
275*4882a593Smuzhiyun * @eh_sz: Size of the pre-allocated buffer @eh
276*4882a593Smuzhiyun * @in_flight: A reference to an in flight &struct scmi_registered_event
277*4882a593Smuzhiyun * @num_events: Number of events in @registered_events
278*4882a593Smuzhiyun * @registered_events: A dynamically allocated array holding all the registered
279*4882a593Smuzhiyun * events' descriptors, whose fixed-size is determined at
280*4882a593Smuzhiyun * compile time.
281*4882a593Smuzhiyun * @registered_mtx: A mutex to protect @registered_events_handlers
282*4882a593Smuzhiyun * @ph: SCMI protocol handle reference
283*4882a593Smuzhiyun * @registered_events_handlers: An hashtable containing all events' handlers
284*4882a593Smuzhiyun * descriptors registered for this protocol
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * All protocols that register at least one event have their protocol-specific
287*4882a593Smuzhiyun * information stored here, together with the embedded allocated events_queue.
288*4882a593Smuzhiyun * These descriptors are stored in the @registered_protocols array at protocol
289*4882a593Smuzhiyun * registration time.
290*4882a593Smuzhiyun *
291*4882a593Smuzhiyun * Once these descriptors are successfully registered, they are NEVER again
292*4882a593Smuzhiyun * removed or modified since protocols do not unregister ever, so that, once
293*4882a593Smuzhiyun * we safely grab a NON-NULL reference from the array we can keep it and use it.
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun struct scmi_registered_events_desc {
296*4882a593Smuzhiyun u8 id;
297*4882a593Smuzhiyun const struct scmi_event_ops *ops;
298*4882a593Smuzhiyun struct events_queue equeue;
299*4882a593Smuzhiyun struct scmi_notify_instance *ni;
300*4882a593Smuzhiyun struct scmi_event_header *eh;
301*4882a593Smuzhiyun size_t eh_sz;
302*4882a593Smuzhiyun void *in_flight;
303*4882a593Smuzhiyun int num_events;
304*4882a593Smuzhiyun struct scmi_registered_event **registered_events;
305*4882a593Smuzhiyun /* mutex to protect registered_events_handlers */
306*4882a593Smuzhiyun struct mutex registered_mtx;
307*4882a593Smuzhiyun const struct scmi_protocol_handle *ph;
308*4882a593Smuzhiyun DECLARE_HASHTABLE(registered_events_handlers, SCMI_REGISTERED_HASH_SZ);
309*4882a593Smuzhiyun };
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun * struct scmi_registered_event - Event Specific Information
313*4882a593Smuzhiyun * @proto: A reference to the associated protocol descriptor
314*4882a593Smuzhiyun * @evt: A reference to the associated event descriptor (as provided at
315*4882a593Smuzhiyun * registration time)
316*4882a593Smuzhiyun * @report: A pre-allocated buffer used by the deferred worker to fill a
317*4882a593Smuzhiyun * customized event report
318*4882a593Smuzhiyun * @num_sources: The number of possible sources for this event as stated at
319*4882a593Smuzhiyun * events' registration time
320*4882a593Smuzhiyun * @sources: A reference to a dynamically allocated array used to refcount the
321*4882a593Smuzhiyun * events' enable requests for all the existing sources
322*4882a593Smuzhiyun * @sources_mtx: A mutex to serialize the access to @sources
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * All registered events are represented by one of these structures that are
325*4882a593Smuzhiyun * stored in the @registered_events array at protocol registration time.
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * Once these descriptors are successfully registered, they are NEVER again
328*4882a593Smuzhiyun * removed or modified since protocols do not unregister ever, so that once we
329*4882a593Smuzhiyun * safely grab a NON-NULL reference from the table we can keep it and use it.
330*4882a593Smuzhiyun */
331*4882a593Smuzhiyun struct scmi_registered_event {
332*4882a593Smuzhiyun struct scmi_registered_events_desc *proto;
333*4882a593Smuzhiyun const struct scmi_event *evt;
334*4882a593Smuzhiyun void *report;
335*4882a593Smuzhiyun u32 num_sources;
336*4882a593Smuzhiyun refcount_t *sources;
337*4882a593Smuzhiyun /* locking to serialize the access to sources */
338*4882a593Smuzhiyun struct mutex sources_mtx;
339*4882a593Smuzhiyun };
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /**
342*4882a593Smuzhiyun * struct scmi_event_handler - Event handler information
343*4882a593Smuzhiyun * @key: The used hashkey
344*4882a593Smuzhiyun * @users: A reference count for number of active users for this handler
345*4882a593Smuzhiyun * @r_evt: A reference to the associated registered event; when this is NULL
346*4882a593Smuzhiyun * this handler is pending, which means that identifies a set of
347*4882a593Smuzhiyun * callbacks intended to be attached to an event which is still not
348*4882a593Smuzhiyun * known nor registered by any protocol at that point in time
349*4882a593Smuzhiyun * @chain: The notification chain dedicated to this specific event tuple
350*4882a593Smuzhiyun * @hash: The hlist_node used for collision handling
351*4882a593Smuzhiyun * @enabled: A boolean which records if event's generation has been already
352*4882a593Smuzhiyun * enabled for this handler as a whole
353*4882a593Smuzhiyun *
354*4882a593Smuzhiyun * This structure collects all the information needed to process a received
355*4882a593Smuzhiyun * event identified by the tuple (proto_id, evt_id, src_id).
356*4882a593Smuzhiyun * These descriptors are stored in a per-protocol @registered_events_handlers
357*4882a593Smuzhiyun * table using as a key a value derived from that tuple.
358*4882a593Smuzhiyun */
359*4882a593Smuzhiyun struct scmi_event_handler {
360*4882a593Smuzhiyun u32 key;
361*4882a593Smuzhiyun refcount_t users;
362*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
363*4882a593Smuzhiyun struct blocking_notifier_head chain;
364*4882a593Smuzhiyun struct hlist_node hash;
365*4882a593Smuzhiyun bool enabled;
366*4882a593Smuzhiyun };
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun #define IS_HNDL_PENDING(hndl) (!(hndl)->r_evt)
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun static struct scmi_event_handler *
371*4882a593Smuzhiyun scmi_get_active_handler(struct scmi_notify_instance *ni, u32 evt_key);
372*4882a593Smuzhiyun static void scmi_put_active_handler(struct scmi_notify_instance *ni,
373*4882a593Smuzhiyun struct scmi_event_handler *hndl);
374*4882a593Smuzhiyun static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
375*4882a593Smuzhiyun struct scmi_event_handler *hndl);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /**
378*4882a593Smuzhiyun * scmi_lookup_and_call_event_chain() - Lookup the proper chain and call it
379*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
380*4882a593Smuzhiyun * @evt_key: The key to use to lookup the related notification chain
381*4882a593Smuzhiyun * @report: The customized event-specific report to pass down to the callbacks
382*4882a593Smuzhiyun * as their *data parameter.
383*4882a593Smuzhiyun */
384*4882a593Smuzhiyun static inline void
scmi_lookup_and_call_event_chain(struct scmi_notify_instance * ni,u32 evt_key,void * report)385*4882a593Smuzhiyun scmi_lookup_and_call_event_chain(struct scmi_notify_instance *ni,
386*4882a593Smuzhiyun u32 evt_key, void *report)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun int ret;
389*4882a593Smuzhiyun struct scmi_event_handler *hndl;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /*
392*4882a593Smuzhiyun * Here ensure the event handler cannot vanish while using it.
393*4882a593Smuzhiyun * It is legitimate, though, for an handler not to be found at all here,
394*4882a593Smuzhiyun * e.g. when it has been unregistered by the user after some events had
395*4882a593Smuzhiyun * already been queued.
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun hndl = scmi_get_active_handler(ni, evt_key);
398*4882a593Smuzhiyun if (!hndl)
399*4882a593Smuzhiyun return;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun ret = blocking_notifier_call_chain(&hndl->chain,
402*4882a593Smuzhiyun KEY_XTRACT_EVT_ID(evt_key),
403*4882a593Smuzhiyun report);
404*4882a593Smuzhiyun /* Notifiers are NOT supposed to cut the chain ... */
405*4882a593Smuzhiyun WARN_ON_ONCE(ret & NOTIFY_STOP_MASK);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun scmi_put_active_handler(ni, hndl);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun * scmi_process_event_header() - Dequeue and process an event header
412*4882a593Smuzhiyun * @eq: The queue to use
413*4882a593Smuzhiyun * @pd: The protocol descriptor to use
414*4882a593Smuzhiyun *
415*4882a593Smuzhiyun * Read an event header from the protocol queue into the dedicated scratch
416*4882a593Smuzhiyun * buffer and looks for a matching registered event; in case an anomalously
417*4882a593Smuzhiyun * sized read is detected just flush the queue.
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * Return:
420*4882a593Smuzhiyun * * a reference to the matching registered event when found
421*4882a593Smuzhiyun * * ERR_PTR(-EINVAL) when NO registered event could be found
422*4882a593Smuzhiyun * * NULL when the queue is empty
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun static inline struct scmi_registered_event *
scmi_process_event_header(struct events_queue * eq,struct scmi_registered_events_desc * pd)425*4882a593Smuzhiyun scmi_process_event_header(struct events_queue *eq,
426*4882a593Smuzhiyun struct scmi_registered_events_desc *pd)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun unsigned int outs;
429*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun outs = kfifo_out(&eq->kfifo, pd->eh,
432*4882a593Smuzhiyun sizeof(struct scmi_event_header));
433*4882a593Smuzhiyun if (!outs)
434*4882a593Smuzhiyun return NULL;
435*4882a593Smuzhiyun if (outs != sizeof(struct scmi_event_header)) {
436*4882a593Smuzhiyun dev_err(pd->ni->handle->dev, "corrupted EVT header. Flush.\n");
437*4882a593Smuzhiyun kfifo_reset_out(&eq->kfifo);
438*4882a593Smuzhiyun return NULL;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun r_evt = SCMI_GET_REVT_FROM_PD(pd, pd->eh->evt_id);
442*4882a593Smuzhiyun if (!r_evt)
443*4882a593Smuzhiyun r_evt = ERR_PTR(-EINVAL);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun return r_evt;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /**
449*4882a593Smuzhiyun * scmi_process_event_payload() - Dequeue and process an event payload
450*4882a593Smuzhiyun * @eq: The queue to use
451*4882a593Smuzhiyun * @pd: The protocol descriptor to use
452*4882a593Smuzhiyun * @r_evt: The registered event descriptor to use
453*4882a593Smuzhiyun *
454*4882a593Smuzhiyun * Read an event payload from the protocol queue into the dedicated scratch
455*4882a593Smuzhiyun * buffer, fills a custom report and then look for matching event handlers and
456*4882a593Smuzhiyun * call them; skip any unknown event (as marked by scmi_process_event_header())
457*4882a593Smuzhiyun * and in case an anomalously sized read is detected just flush the queue.
458*4882a593Smuzhiyun *
459*4882a593Smuzhiyun * Return: False when the queue is empty
460*4882a593Smuzhiyun */
461*4882a593Smuzhiyun static inline bool
scmi_process_event_payload(struct events_queue * eq,struct scmi_registered_events_desc * pd,struct scmi_registered_event * r_evt)462*4882a593Smuzhiyun scmi_process_event_payload(struct events_queue *eq,
463*4882a593Smuzhiyun struct scmi_registered_events_desc *pd,
464*4882a593Smuzhiyun struct scmi_registered_event *r_evt)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun u32 src_id, key;
467*4882a593Smuzhiyun unsigned int outs;
468*4882a593Smuzhiyun void *report = NULL;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun outs = kfifo_out(&eq->kfifo, pd->eh->payld, pd->eh->payld_sz);
471*4882a593Smuzhiyun if (!outs)
472*4882a593Smuzhiyun return false;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* Any in-flight event has now been officially processed */
475*4882a593Smuzhiyun pd->in_flight = NULL;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun if (outs != pd->eh->payld_sz) {
478*4882a593Smuzhiyun dev_err(pd->ni->handle->dev, "corrupted EVT Payload. Flush.\n");
479*4882a593Smuzhiyun kfifo_reset_out(&eq->kfifo);
480*4882a593Smuzhiyun return false;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (IS_ERR(r_evt)) {
484*4882a593Smuzhiyun dev_warn(pd->ni->handle->dev,
485*4882a593Smuzhiyun "SKIP UNKNOWN EVT - proto:%X evt:%d\n",
486*4882a593Smuzhiyun pd->id, pd->eh->evt_id);
487*4882a593Smuzhiyun return true;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun report = REVT_FILL_REPORT(r_evt, pd->eh->evt_id, pd->eh->timestamp,
491*4882a593Smuzhiyun pd->eh->payld, pd->eh->payld_sz,
492*4882a593Smuzhiyun r_evt->report, &src_id);
493*4882a593Smuzhiyun if (!report) {
494*4882a593Smuzhiyun dev_err(pd->ni->handle->dev,
495*4882a593Smuzhiyun "report not available - proto:%X evt:%d\n",
496*4882a593Smuzhiyun pd->id, pd->eh->evt_id);
497*4882a593Smuzhiyun return true;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /* At first search for a generic ALL src_ids handler... */
501*4882a593Smuzhiyun key = MAKE_ALL_SRCS_KEY(pd->id, pd->eh->evt_id);
502*4882a593Smuzhiyun scmi_lookup_and_call_event_chain(pd->ni, key, report);
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /* ...then search for any specific src_id */
505*4882a593Smuzhiyun key = MAKE_HASH_KEY(pd->id, pd->eh->evt_id, src_id);
506*4882a593Smuzhiyun scmi_lookup_and_call_event_chain(pd->ni, key, report);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun return true;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun * scmi_events_dispatcher() - Common worker logic for all work items.
513*4882a593Smuzhiyun * @work: The work item to use, which is associated to a dedicated events_queue
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * Logic:
516*4882a593Smuzhiyun * 1. dequeue one pending RX notification (queued in SCMI RX ISR context)
517*4882a593Smuzhiyun * 2. generate a custom event report from the received event message
518*4882a593Smuzhiyun * 3. lookup for any registered ALL_SRC_IDs handler:
519*4882a593Smuzhiyun * - > call the related notification chain passing in the report
520*4882a593Smuzhiyun * 4. lookup for any registered specific SRC_ID handler:
521*4882a593Smuzhiyun * - > call the related notification chain passing in the report
522*4882a593Smuzhiyun *
523*4882a593Smuzhiyun * Note that:
524*4882a593Smuzhiyun * * a dedicated per-protocol kfifo queue is used: in this way an anomalous
525*4882a593Smuzhiyun * flood of events cannot saturate other protocols' queues.
526*4882a593Smuzhiyun * * each per-protocol queue is associated to a distinct work_item, which
527*4882a593Smuzhiyun * means, in turn, that:
528*4882a593Smuzhiyun * + all protocols can process their dedicated queues concurrently
529*4882a593Smuzhiyun * (since notify_wq:max_active != 1)
530*4882a593Smuzhiyun * + anyway at most one worker instance is allowed to run on the same queue
531*4882a593Smuzhiyun * concurrently: this ensures that we can have only one concurrent
532*4882a593Smuzhiyun * reader/writer on the associated kfifo, so that we can use it lock-less
533*4882a593Smuzhiyun *
534*4882a593Smuzhiyun * Context: Process context.
535*4882a593Smuzhiyun */
scmi_events_dispatcher(struct work_struct * work)536*4882a593Smuzhiyun static void scmi_events_dispatcher(struct work_struct *work)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun struct events_queue *eq;
539*4882a593Smuzhiyun struct scmi_registered_events_desc *pd;
540*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun eq = container_of(work, struct events_queue, notify_work);
543*4882a593Smuzhiyun pd = container_of(eq, struct scmi_registered_events_desc, equeue);
544*4882a593Smuzhiyun /*
545*4882a593Smuzhiyun * In order to keep the queue lock-less and the number of memcopies
546*4882a593Smuzhiyun * to the bare minimum needed, the dispatcher accounts for the
547*4882a593Smuzhiyun * possibility of per-protocol in-flight events: i.e. an event whose
548*4882a593Smuzhiyun * reception could end up being split across two subsequent runs of this
549*4882a593Smuzhiyun * worker, first the header, then the payload.
550*4882a593Smuzhiyun */
551*4882a593Smuzhiyun do {
552*4882a593Smuzhiyun if (!pd->in_flight) {
553*4882a593Smuzhiyun r_evt = scmi_process_event_header(eq, pd);
554*4882a593Smuzhiyun if (!r_evt)
555*4882a593Smuzhiyun break;
556*4882a593Smuzhiyun pd->in_flight = r_evt;
557*4882a593Smuzhiyun } else {
558*4882a593Smuzhiyun r_evt = pd->in_flight;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun } while (scmi_process_event_payload(eq, pd, r_evt));
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /**
564*4882a593Smuzhiyun * scmi_notify() - Queues a notification for further deferred processing
565*4882a593Smuzhiyun * @handle: The handle identifying the platform instance from which the
566*4882a593Smuzhiyun * dispatched event is generated
567*4882a593Smuzhiyun * @proto_id: Protocol ID
568*4882a593Smuzhiyun * @evt_id: Event ID (msgID)
569*4882a593Smuzhiyun * @buf: Event Message Payload (without the header)
570*4882a593Smuzhiyun * @len: Event Message Payload size
571*4882a593Smuzhiyun * @ts: RX Timestamp in nanoseconds (boottime)
572*4882a593Smuzhiyun *
573*4882a593Smuzhiyun * Context: Called in interrupt context to queue a received event for
574*4882a593Smuzhiyun * deferred processing.
575*4882a593Smuzhiyun *
576*4882a593Smuzhiyun * Return: 0 on Success
577*4882a593Smuzhiyun */
scmi_notify(const struct scmi_handle * handle,u8 proto_id,u8 evt_id,const void * buf,size_t len,ktime_t ts)578*4882a593Smuzhiyun int scmi_notify(const struct scmi_handle *handle, u8 proto_id, u8 evt_id,
579*4882a593Smuzhiyun const void *buf, size_t len, ktime_t ts)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
582*4882a593Smuzhiyun struct scmi_event_header eh;
583*4882a593Smuzhiyun struct scmi_notify_instance *ni;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun ni = scmi_get_notification_instance_data(handle);
586*4882a593Smuzhiyun if (!ni)
587*4882a593Smuzhiyun return 0;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun r_evt = SCMI_GET_REVT(ni, proto_id, evt_id);
590*4882a593Smuzhiyun if (!r_evt)
591*4882a593Smuzhiyun return -EINVAL;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun if (len > r_evt->evt->max_payld_sz) {
594*4882a593Smuzhiyun dev_err(handle->dev, "discard badly sized message\n");
595*4882a593Smuzhiyun return -EINVAL;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun if (kfifo_avail(&r_evt->proto->equeue.kfifo) < sizeof(eh) + len) {
598*4882a593Smuzhiyun dev_warn(handle->dev,
599*4882a593Smuzhiyun "queue full, dropping proto_id:%d evt_id:%d ts:%lld\n",
600*4882a593Smuzhiyun proto_id, evt_id, ktime_to_ns(ts));
601*4882a593Smuzhiyun return -ENOMEM;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun eh.timestamp = ts;
605*4882a593Smuzhiyun eh.evt_id = evt_id;
606*4882a593Smuzhiyun eh.payld_sz = len;
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun * Header and payload are enqueued with two distinct kfifo_in() (so non
609*4882a593Smuzhiyun * atomic), but this situation is handled properly on the consumer side
610*4882a593Smuzhiyun * with in-flight events tracking.
611*4882a593Smuzhiyun */
612*4882a593Smuzhiyun kfifo_in(&r_evt->proto->equeue.kfifo, &eh, sizeof(eh));
613*4882a593Smuzhiyun kfifo_in(&r_evt->proto->equeue.kfifo, buf, len);
614*4882a593Smuzhiyun /*
615*4882a593Smuzhiyun * Don't care about return value here since we just want to ensure that
616*4882a593Smuzhiyun * a work is queued all the times whenever some items have been pushed
617*4882a593Smuzhiyun * on the kfifo:
618*4882a593Smuzhiyun * - if work was already queued it will simply fail to queue a new one
619*4882a593Smuzhiyun * since it is not needed
620*4882a593Smuzhiyun * - if work was not queued already it will be now, even in case work
621*4882a593Smuzhiyun * was in fact already running: this behavior avoids any possible race
622*4882a593Smuzhiyun * when this function pushes new items onto the kfifos after the
623*4882a593Smuzhiyun * related executing worker had already determined the kfifo to be
624*4882a593Smuzhiyun * empty and it was terminating.
625*4882a593Smuzhiyun */
626*4882a593Smuzhiyun queue_work(r_evt->proto->equeue.wq,
627*4882a593Smuzhiyun &r_evt->proto->equeue.notify_work);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun return 0;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /**
633*4882a593Smuzhiyun * scmi_kfifo_free() - Devres action helper to free the kfifo
634*4882a593Smuzhiyun * @kfifo: The kfifo to free
635*4882a593Smuzhiyun */
scmi_kfifo_free(void * kfifo)636*4882a593Smuzhiyun static void scmi_kfifo_free(void *kfifo)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun kfifo_free((struct kfifo *)kfifo);
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun /**
642*4882a593Smuzhiyun * scmi_initialize_events_queue() - Allocate/Initialize a kfifo buffer
643*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
644*4882a593Smuzhiyun * @equeue: The events_queue to initialize
645*4882a593Smuzhiyun * @sz: Size of the kfifo buffer to allocate
646*4882a593Smuzhiyun *
647*4882a593Smuzhiyun * Allocate a buffer for the kfifo and initialize it.
648*4882a593Smuzhiyun *
649*4882a593Smuzhiyun * Return: 0 on Success
650*4882a593Smuzhiyun */
scmi_initialize_events_queue(struct scmi_notify_instance * ni,struct events_queue * equeue,size_t sz)651*4882a593Smuzhiyun static int scmi_initialize_events_queue(struct scmi_notify_instance *ni,
652*4882a593Smuzhiyun struct events_queue *equeue, size_t sz)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun int ret;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun if (kfifo_alloc(&equeue->kfifo, sz, GFP_KERNEL))
657*4882a593Smuzhiyun return -ENOMEM;
658*4882a593Smuzhiyun /* Size could have been roundup to power-of-two */
659*4882a593Smuzhiyun equeue->sz = kfifo_size(&equeue->kfifo);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun ret = devm_add_action_or_reset(ni->handle->dev, scmi_kfifo_free,
662*4882a593Smuzhiyun &equeue->kfifo);
663*4882a593Smuzhiyun if (ret)
664*4882a593Smuzhiyun return ret;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun INIT_WORK(&equeue->notify_work, scmi_events_dispatcher);
667*4882a593Smuzhiyun equeue->wq = ni->notify_wq;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun return ret;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /**
673*4882a593Smuzhiyun * scmi_allocate_registered_events_desc() - Allocate a registered events'
674*4882a593Smuzhiyun * descriptor
675*4882a593Smuzhiyun * @ni: A reference to the &struct scmi_notify_instance notification instance
676*4882a593Smuzhiyun * to use
677*4882a593Smuzhiyun * @proto_id: Protocol ID
678*4882a593Smuzhiyun * @queue_sz: Size of the associated queue to allocate
679*4882a593Smuzhiyun * @eh_sz: Size of the event header scratch area to pre-allocate
680*4882a593Smuzhiyun * @num_events: Number of events to support (size of @registered_events)
681*4882a593Smuzhiyun * @ops: Pointer to a struct holding references to protocol specific helpers
682*4882a593Smuzhiyun * needed during events handling
683*4882a593Smuzhiyun *
684*4882a593Smuzhiyun * It is supposed to be called only once for each protocol at protocol
685*4882a593Smuzhiyun * initialization time, so it warns if the requested protocol is found already
686*4882a593Smuzhiyun * registered.
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * Return: The allocated and registered descriptor on Success
689*4882a593Smuzhiyun */
690*4882a593Smuzhiyun static struct scmi_registered_events_desc *
scmi_allocate_registered_events_desc(struct scmi_notify_instance * ni,u8 proto_id,size_t queue_sz,size_t eh_sz,int num_events,const struct scmi_event_ops * ops)691*4882a593Smuzhiyun scmi_allocate_registered_events_desc(struct scmi_notify_instance *ni,
692*4882a593Smuzhiyun u8 proto_id, size_t queue_sz, size_t eh_sz,
693*4882a593Smuzhiyun int num_events,
694*4882a593Smuzhiyun const struct scmi_event_ops *ops)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun int ret;
697*4882a593Smuzhiyun struct scmi_registered_events_desc *pd;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun /* Ensure protocols are up to date */
700*4882a593Smuzhiyun smp_rmb();
701*4882a593Smuzhiyun if (WARN_ON(ni->registered_protocols[proto_id]))
702*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun pd = devm_kzalloc(ni->handle->dev, sizeof(*pd), GFP_KERNEL);
705*4882a593Smuzhiyun if (!pd)
706*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
707*4882a593Smuzhiyun pd->id = proto_id;
708*4882a593Smuzhiyun pd->ops = ops;
709*4882a593Smuzhiyun pd->ni = ni;
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun ret = scmi_initialize_events_queue(ni, &pd->equeue, queue_sz);
712*4882a593Smuzhiyun if (ret)
713*4882a593Smuzhiyun return ERR_PTR(ret);
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun pd->eh = devm_kzalloc(ni->handle->dev, eh_sz, GFP_KERNEL);
716*4882a593Smuzhiyun if (!pd->eh)
717*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
718*4882a593Smuzhiyun pd->eh_sz = eh_sz;
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun pd->registered_events = devm_kcalloc(ni->handle->dev, num_events,
721*4882a593Smuzhiyun sizeof(char *), GFP_KERNEL);
722*4882a593Smuzhiyun if (!pd->registered_events)
723*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
724*4882a593Smuzhiyun pd->num_events = num_events;
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun /* Initialize per protocol handlers table */
727*4882a593Smuzhiyun mutex_init(&pd->registered_mtx);
728*4882a593Smuzhiyun hash_init(pd->registered_events_handlers);
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun return pd;
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun /**
734*4882a593Smuzhiyun * scmi_register_protocol_events() - Register Protocol Events with the core
735*4882a593Smuzhiyun * @handle: The handle identifying the platform instance against which the
736*4882a593Smuzhiyun * protocol's events are registered
737*4882a593Smuzhiyun * @proto_id: Protocol ID
738*4882a593Smuzhiyun * @ph: SCMI protocol handle.
739*4882a593Smuzhiyun * @ee: A structure describing the events supported by this protocol.
740*4882a593Smuzhiyun *
741*4882a593Smuzhiyun * Used by SCMI Protocols initialization code to register with the notification
742*4882a593Smuzhiyun * core the list of supported events and their descriptors: takes care to
743*4882a593Smuzhiyun * pre-allocate and store all needed descriptors, scratch buffers and event
744*4882a593Smuzhiyun * queues.
745*4882a593Smuzhiyun *
746*4882a593Smuzhiyun * Return: 0 on Success
747*4882a593Smuzhiyun */
scmi_register_protocol_events(const struct scmi_handle * handle,u8 proto_id,const struct scmi_protocol_handle * ph,const struct scmi_protocol_events * ee)748*4882a593Smuzhiyun int scmi_register_protocol_events(const struct scmi_handle *handle, u8 proto_id,
749*4882a593Smuzhiyun const struct scmi_protocol_handle *ph,
750*4882a593Smuzhiyun const struct scmi_protocol_events *ee)
751*4882a593Smuzhiyun {
752*4882a593Smuzhiyun int i;
753*4882a593Smuzhiyun unsigned int num_sources;
754*4882a593Smuzhiyun size_t payld_sz = 0;
755*4882a593Smuzhiyun struct scmi_registered_events_desc *pd;
756*4882a593Smuzhiyun struct scmi_notify_instance *ni;
757*4882a593Smuzhiyun const struct scmi_event *evt;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun if (!ee || !ee->ops || !ee->evts || !ph ||
760*4882a593Smuzhiyun (!ee->num_sources && !ee->ops->get_num_sources))
761*4882a593Smuzhiyun return -EINVAL;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun ni = scmi_get_notification_instance_data(handle);
764*4882a593Smuzhiyun if (!ni)
765*4882a593Smuzhiyun return -ENOMEM;
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun /* num_sources cannot be <= 0 */
768*4882a593Smuzhiyun if (ee->num_sources) {
769*4882a593Smuzhiyun num_sources = ee->num_sources;
770*4882a593Smuzhiyun } else {
771*4882a593Smuzhiyun int nsrc = ee->ops->get_num_sources(ph);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun if (nsrc <= 0)
774*4882a593Smuzhiyun return -EINVAL;
775*4882a593Smuzhiyun num_sources = nsrc;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun evt = ee->evts;
779*4882a593Smuzhiyun for (i = 0; i < ee->num_events; i++)
780*4882a593Smuzhiyun payld_sz = max_t(size_t, payld_sz, evt[i].max_payld_sz);
781*4882a593Smuzhiyun payld_sz += sizeof(struct scmi_event_header);
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun pd = scmi_allocate_registered_events_desc(ni, proto_id, ee->queue_sz,
784*4882a593Smuzhiyun payld_sz, ee->num_events,
785*4882a593Smuzhiyun ee->ops);
786*4882a593Smuzhiyun if (IS_ERR(pd))
787*4882a593Smuzhiyun goto err;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun pd->ph = ph;
790*4882a593Smuzhiyun for (i = 0; i < ee->num_events; i++, evt++) {
791*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun r_evt = devm_kzalloc(ni->handle->dev, sizeof(*r_evt),
794*4882a593Smuzhiyun GFP_KERNEL);
795*4882a593Smuzhiyun if (!r_evt)
796*4882a593Smuzhiyun goto err;
797*4882a593Smuzhiyun r_evt->proto = pd;
798*4882a593Smuzhiyun r_evt->evt = evt;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun r_evt->sources = devm_kcalloc(ni->handle->dev, num_sources,
801*4882a593Smuzhiyun sizeof(refcount_t), GFP_KERNEL);
802*4882a593Smuzhiyun if (!r_evt->sources)
803*4882a593Smuzhiyun goto err;
804*4882a593Smuzhiyun r_evt->num_sources = num_sources;
805*4882a593Smuzhiyun mutex_init(&r_evt->sources_mtx);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun r_evt->report = devm_kzalloc(ni->handle->dev,
808*4882a593Smuzhiyun evt->max_report_sz, GFP_KERNEL);
809*4882a593Smuzhiyun if (!r_evt->report)
810*4882a593Smuzhiyun goto err;
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun pd->registered_events[i] = r_evt;
813*4882a593Smuzhiyun /* Ensure events are updated */
814*4882a593Smuzhiyun smp_wmb();
815*4882a593Smuzhiyun dev_dbg(handle->dev, "registered event - %lX\n",
816*4882a593Smuzhiyun MAKE_ALL_SRCS_KEY(r_evt->proto->id, r_evt->evt->id));
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun /* Register protocol and events...it will never be removed */
820*4882a593Smuzhiyun ni->registered_protocols[proto_id] = pd;
821*4882a593Smuzhiyun /* Ensure protocols are updated */
822*4882a593Smuzhiyun smp_wmb();
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun /*
825*4882a593Smuzhiyun * Finalize any pending events' handler which could have been waiting
826*4882a593Smuzhiyun * for this protocol's events registration.
827*4882a593Smuzhiyun */
828*4882a593Smuzhiyun schedule_work(&ni->init_work);
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun return 0;
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun err:
833*4882a593Smuzhiyun dev_warn(handle->dev, "Proto:%X - Registration Failed !\n", proto_id);
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun return -ENOMEM;
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun /**
839*4882a593Smuzhiyun * scmi_deregister_protocol_events - Deregister protocol events with the core
840*4882a593Smuzhiyun * @handle: The handle identifying the platform instance against which the
841*4882a593Smuzhiyun * protocol's events are registered
842*4882a593Smuzhiyun * @proto_id: Protocol ID
843*4882a593Smuzhiyun */
scmi_deregister_protocol_events(const struct scmi_handle * handle,u8 proto_id)844*4882a593Smuzhiyun void scmi_deregister_protocol_events(const struct scmi_handle *handle,
845*4882a593Smuzhiyun u8 proto_id)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun struct scmi_notify_instance *ni;
848*4882a593Smuzhiyun struct scmi_registered_events_desc *pd;
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun ni = scmi_get_notification_instance_data(handle);
851*4882a593Smuzhiyun if (!ni)
852*4882a593Smuzhiyun return;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun pd = ni->registered_protocols[proto_id];
855*4882a593Smuzhiyun if (!pd)
856*4882a593Smuzhiyun return;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun ni->registered_protocols[proto_id] = NULL;
859*4882a593Smuzhiyun /* Ensure protocols are updated */
860*4882a593Smuzhiyun smp_wmb();
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun cancel_work_sync(&pd->equeue.notify_work);
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /**
866*4882a593Smuzhiyun * scmi_allocate_event_handler() - Allocate Event handler
867*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
868*4882a593Smuzhiyun * @evt_key: 32bit key uniquely bind to the event identified by the tuple
869*4882a593Smuzhiyun * (proto_id, evt_id, src_id)
870*4882a593Smuzhiyun *
871*4882a593Smuzhiyun * Allocate an event handler and related notification chain associated with
872*4882a593Smuzhiyun * the provided event handler key.
873*4882a593Smuzhiyun * Note that, at this point, a related registered_event is still to be
874*4882a593Smuzhiyun * associated to this handler descriptor (hndl->r_evt == NULL), so the handler
875*4882a593Smuzhiyun * is initialized as pending.
876*4882a593Smuzhiyun *
877*4882a593Smuzhiyun * Context: Assumes to be called with @pending_mtx already acquired.
878*4882a593Smuzhiyun * Return: the freshly allocated structure on Success
879*4882a593Smuzhiyun */
880*4882a593Smuzhiyun static struct scmi_event_handler *
scmi_allocate_event_handler(struct scmi_notify_instance * ni,u32 evt_key)881*4882a593Smuzhiyun scmi_allocate_event_handler(struct scmi_notify_instance *ni, u32 evt_key)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun struct scmi_event_handler *hndl;
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun hndl = kzalloc(sizeof(*hndl), GFP_KERNEL);
886*4882a593Smuzhiyun if (!hndl)
887*4882a593Smuzhiyun return NULL;
888*4882a593Smuzhiyun hndl->key = evt_key;
889*4882a593Smuzhiyun BLOCKING_INIT_NOTIFIER_HEAD(&hndl->chain);
890*4882a593Smuzhiyun refcount_set(&hndl->users, 1);
891*4882a593Smuzhiyun /* New handlers are created pending */
892*4882a593Smuzhiyun hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key);
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun return hndl;
895*4882a593Smuzhiyun }
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun /**
898*4882a593Smuzhiyun * scmi_free_event_handler() - Free the provided Event handler
899*4882a593Smuzhiyun * @hndl: The event handler structure to free
900*4882a593Smuzhiyun *
901*4882a593Smuzhiyun * Context: Assumes to be called with proper locking acquired depending
902*4882a593Smuzhiyun * on the situation.
903*4882a593Smuzhiyun */
scmi_free_event_handler(struct scmi_event_handler * hndl)904*4882a593Smuzhiyun static void scmi_free_event_handler(struct scmi_event_handler *hndl)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun hash_del(&hndl->hash);
907*4882a593Smuzhiyun kfree(hndl);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun /**
911*4882a593Smuzhiyun * scmi_bind_event_handler() - Helper to attempt binding an handler to an event
912*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
913*4882a593Smuzhiyun * @hndl: The event handler to bind
914*4882a593Smuzhiyun *
915*4882a593Smuzhiyun * If an associated registered event is found, move the handler from the pending
916*4882a593Smuzhiyun * into the registered table.
917*4882a593Smuzhiyun *
918*4882a593Smuzhiyun * Context: Assumes to be called with @pending_mtx already acquired.
919*4882a593Smuzhiyun *
920*4882a593Smuzhiyun * Return: 0 on Success
921*4882a593Smuzhiyun */
scmi_bind_event_handler(struct scmi_notify_instance * ni,struct scmi_event_handler * hndl)922*4882a593Smuzhiyun static inline int scmi_bind_event_handler(struct scmi_notify_instance *ni,
923*4882a593Smuzhiyun struct scmi_event_handler *hndl)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun r_evt = SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(hndl->key),
928*4882a593Smuzhiyun KEY_XTRACT_EVT_ID(hndl->key));
929*4882a593Smuzhiyun if (!r_evt)
930*4882a593Smuzhiyun return -EINVAL;
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun /*
933*4882a593Smuzhiyun * Remove from pending and insert into registered while getting hold
934*4882a593Smuzhiyun * of protocol instance.
935*4882a593Smuzhiyun */
936*4882a593Smuzhiyun hash_del(&hndl->hash);
937*4882a593Smuzhiyun /*
938*4882a593Smuzhiyun * Acquire protocols only for NON pending handlers, so as NOT to trigger
939*4882a593Smuzhiyun * protocol initialization when a notifier is registered against a still
940*4882a593Smuzhiyun * not registered protocol, since it would make little sense to force init
941*4882a593Smuzhiyun * protocols for which still no SCMI driver user exists: they wouldn't
942*4882a593Smuzhiyun * emit any event anyway till some SCMI driver starts using it.
943*4882a593Smuzhiyun */
944*4882a593Smuzhiyun scmi_acquire_protocol(ni->handle, KEY_XTRACT_PROTO_ID(hndl->key));
945*4882a593Smuzhiyun hndl->r_evt = r_evt;
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun mutex_lock(&r_evt->proto->registered_mtx);
948*4882a593Smuzhiyun hash_add(r_evt->proto->registered_events_handlers,
949*4882a593Smuzhiyun &hndl->hash, hndl->key);
950*4882a593Smuzhiyun mutex_unlock(&r_evt->proto->registered_mtx);
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun return 0;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun * scmi_valid_pending_handler() - Helper to check pending status of handlers
957*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
958*4882a593Smuzhiyun * @hndl: The event handler to check
959*4882a593Smuzhiyun *
960*4882a593Smuzhiyun * An handler is considered pending when its r_evt == NULL, because the related
961*4882a593Smuzhiyun * event was still unknown at handler's registration time; anyway, since all
962*4882a593Smuzhiyun * protocols register their supported events once for all at protocols'
963*4882a593Smuzhiyun * initialization time, a pending handler cannot be considered valid anymore if
964*4882a593Smuzhiyun * the underlying event (which it is waiting for), belongs to an already
965*4882a593Smuzhiyun * initialized and registered protocol.
966*4882a593Smuzhiyun *
967*4882a593Smuzhiyun * Return: 0 on Success
968*4882a593Smuzhiyun */
scmi_valid_pending_handler(struct scmi_notify_instance * ni,struct scmi_event_handler * hndl)969*4882a593Smuzhiyun static inline int scmi_valid_pending_handler(struct scmi_notify_instance *ni,
970*4882a593Smuzhiyun struct scmi_event_handler *hndl)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun struct scmi_registered_events_desc *pd;
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun if (!IS_HNDL_PENDING(hndl))
975*4882a593Smuzhiyun return -EINVAL;
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun pd = SCMI_GET_PROTO(ni, KEY_XTRACT_PROTO_ID(hndl->key));
978*4882a593Smuzhiyun if (pd)
979*4882a593Smuzhiyun return -EINVAL;
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun return 0;
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun /**
985*4882a593Smuzhiyun * scmi_register_event_handler() - Register whenever possible an Event handler
986*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
987*4882a593Smuzhiyun * @hndl: The event handler to register
988*4882a593Smuzhiyun *
989*4882a593Smuzhiyun * At first try to bind an event handler to its associated event, then check if
990*4882a593Smuzhiyun * it was at least a valid pending handler: if it was not bound nor valid return
991*4882a593Smuzhiyun * false.
992*4882a593Smuzhiyun *
993*4882a593Smuzhiyun * Valid pending incomplete bindings will be periodically retried by a dedicated
994*4882a593Smuzhiyun * worker which is kicked each time a new protocol completes its own
995*4882a593Smuzhiyun * registration phase.
996*4882a593Smuzhiyun *
997*4882a593Smuzhiyun * Context: Assumes to be called with @pending_mtx acquired.
998*4882a593Smuzhiyun *
999*4882a593Smuzhiyun * Return: 0 on Success
1000*4882a593Smuzhiyun */
scmi_register_event_handler(struct scmi_notify_instance * ni,struct scmi_event_handler * hndl)1001*4882a593Smuzhiyun static int scmi_register_event_handler(struct scmi_notify_instance *ni,
1002*4882a593Smuzhiyun struct scmi_event_handler *hndl)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun int ret;
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun ret = scmi_bind_event_handler(ni, hndl);
1007*4882a593Smuzhiyun if (!ret) {
1008*4882a593Smuzhiyun dev_dbg(ni->handle->dev, "registered NEW handler - key:%X\n",
1009*4882a593Smuzhiyun hndl->key);
1010*4882a593Smuzhiyun } else {
1011*4882a593Smuzhiyun ret = scmi_valid_pending_handler(ni, hndl);
1012*4882a593Smuzhiyun if (!ret)
1013*4882a593Smuzhiyun dev_dbg(ni->handle->dev,
1014*4882a593Smuzhiyun "registered PENDING handler - key:%X\n",
1015*4882a593Smuzhiyun hndl->key);
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun return ret;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun /**
1022*4882a593Smuzhiyun * __scmi_event_handler_get_ops() - Utility to get or create an event handler
1023*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
1024*4882a593Smuzhiyun * @evt_key: The event key to use
1025*4882a593Smuzhiyun * @create: A boolean flag to specify if a handler must be created when
1026*4882a593Smuzhiyun * not already existent
1027*4882a593Smuzhiyun *
1028*4882a593Smuzhiyun * Search for the desired handler matching the key in both the per-protocol
1029*4882a593Smuzhiyun * registered table and the common pending table:
1030*4882a593Smuzhiyun * * if found adjust users refcount
1031*4882a593Smuzhiyun * * if not found and @create is true, create and register the new handler:
1032*4882a593Smuzhiyun * handler could end up being registered as pending if no matching event
1033*4882a593Smuzhiyun * could be found.
1034*4882a593Smuzhiyun *
1035*4882a593Smuzhiyun * An handler is guaranteed to reside in one and only one of the tables at
1036*4882a593Smuzhiyun * any one time; to ensure this the whole search and create is performed
1037*4882a593Smuzhiyun * holding the @pending_mtx lock, with @registered_mtx additionally acquired
1038*4882a593Smuzhiyun * if needed.
1039*4882a593Smuzhiyun *
1040*4882a593Smuzhiyun * Note that when a nested acquisition of these mutexes is needed the locking
1041*4882a593Smuzhiyun * order is always (same as in @init_work):
1042*4882a593Smuzhiyun * 1. pending_mtx
1043*4882a593Smuzhiyun * 2. registered_mtx
1044*4882a593Smuzhiyun *
1045*4882a593Smuzhiyun * Events generation is NOT enabled right after creation within this routine
1046*4882a593Smuzhiyun * since at creation time we usually want to have all setup and ready before
1047*4882a593Smuzhiyun * events really start flowing.
1048*4882a593Smuzhiyun *
1049*4882a593Smuzhiyun * Return: A properly refcounted handler on Success, NULL on Failure
1050*4882a593Smuzhiyun */
1051*4882a593Smuzhiyun static inline struct scmi_event_handler *
__scmi_event_handler_get_ops(struct scmi_notify_instance * ni,u32 evt_key,bool create)1052*4882a593Smuzhiyun __scmi_event_handler_get_ops(struct scmi_notify_instance *ni,
1053*4882a593Smuzhiyun u32 evt_key, bool create)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
1056*4882a593Smuzhiyun struct scmi_event_handler *hndl = NULL;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun r_evt = SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(evt_key),
1059*4882a593Smuzhiyun KEY_XTRACT_EVT_ID(evt_key));
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun mutex_lock(&ni->pending_mtx);
1062*4882a593Smuzhiyun /* Search registered events at first ... if possible at all */
1063*4882a593Smuzhiyun if (r_evt) {
1064*4882a593Smuzhiyun mutex_lock(&r_evt->proto->registered_mtx);
1065*4882a593Smuzhiyun hndl = KEY_FIND(r_evt->proto->registered_events_handlers,
1066*4882a593Smuzhiyun hndl, evt_key);
1067*4882a593Smuzhiyun if (hndl)
1068*4882a593Smuzhiyun refcount_inc(&hndl->users);
1069*4882a593Smuzhiyun mutex_unlock(&r_evt->proto->registered_mtx);
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun /* ...then amongst pending. */
1073*4882a593Smuzhiyun if (!hndl) {
1074*4882a593Smuzhiyun hndl = KEY_FIND(ni->pending_events_handlers, hndl, evt_key);
1075*4882a593Smuzhiyun if (hndl)
1076*4882a593Smuzhiyun refcount_inc(&hndl->users);
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /* Create if still not found and required */
1080*4882a593Smuzhiyun if (!hndl && create) {
1081*4882a593Smuzhiyun hndl = scmi_allocate_event_handler(ni, evt_key);
1082*4882a593Smuzhiyun if (hndl && scmi_register_event_handler(ni, hndl)) {
1083*4882a593Smuzhiyun dev_dbg(ni->handle->dev,
1084*4882a593Smuzhiyun "purging UNKNOWN handler - key:%X\n",
1085*4882a593Smuzhiyun hndl->key);
1086*4882a593Smuzhiyun /* this hndl can be only a pending one */
1087*4882a593Smuzhiyun scmi_put_handler_unlocked(ni, hndl);
1088*4882a593Smuzhiyun hndl = NULL;
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun mutex_unlock(&ni->pending_mtx);
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun return hndl;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun static struct scmi_event_handler *
scmi_get_handler(struct scmi_notify_instance * ni,u32 evt_key)1097*4882a593Smuzhiyun scmi_get_handler(struct scmi_notify_instance *ni, u32 evt_key)
1098*4882a593Smuzhiyun {
1099*4882a593Smuzhiyun return __scmi_event_handler_get_ops(ni, evt_key, false);
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun static struct scmi_event_handler *
scmi_get_or_create_handler(struct scmi_notify_instance * ni,u32 evt_key)1103*4882a593Smuzhiyun scmi_get_or_create_handler(struct scmi_notify_instance *ni, u32 evt_key)
1104*4882a593Smuzhiyun {
1105*4882a593Smuzhiyun return __scmi_event_handler_get_ops(ni, evt_key, true);
1106*4882a593Smuzhiyun }
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /**
1109*4882a593Smuzhiyun * scmi_get_active_handler() - Helper to get active handlers only
1110*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
1111*4882a593Smuzhiyun * @evt_key: The event key to use
1112*4882a593Smuzhiyun *
1113*4882a593Smuzhiyun * Search for the desired handler matching the key only in the per-protocol
1114*4882a593Smuzhiyun * table of registered handlers: this is called only from the dispatching path
1115*4882a593Smuzhiyun * so want to be as quick as possible and do not care about pending.
1116*4882a593Smuzhiyun *
1117*4882a593Smuzhiyun * Return: A properly refcounted active handler
1118*4882a593Smuzhiyun */
1119*4882a593Smuzhiyun static struct scmi_event_handler *
scmi_get_active_handler(struct scmi_notify_instance * ni,u32 evt_key)1120*4882a593Smuzhiyun scmi_get_active_handler(struct scmi_notify_instance *ni, u32 evt_key)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun struct scmi_registered_event *r_evt;
1123*4882a593Smuzhiyun struct scmi_event_handler *hndl = NULL;
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun r_evt = SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(evt_key),
1126*4882a593Smuzhiyun KEY_XTRACT_EVT_ID(evt_key));
1127*4882a593Smuzhiyun if (r_evt) {
1128*4882a593Smuzhiyun mutex_lock(&r_evt->proto->registered_mtx);
1129*4882a593Smuzhiyun hndl = KEY_FIND(r_evt->proto->registered_events_handlers,
1130*4882a593Smuzhiyun hndl, evt_key);
1131*4882a593Smuzhiyun if (hndl)
1132*4882a593Smuzhiyun refcount_inc(&hndl->users);
1133*4882a593Smuzhiyun mutex_unlock(&r_evt->proto->registered_mtx);
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun return hndl;
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun /**
1140*4882a593Smuzhiyun * __scmi_enable_evt() - Enable/disable events generation
1141*4882a593Smuzhiyun * @r_evt: The registered event to act upon
1142*4882a593Smuzhiyun * @src_id: The src_id to act upon
1143*4882a593Smuzhiyun * @enable: The action to perform: true->Enable, false->Disable
1144*4882a593Smuzhiyun *
1145*4882a593Smuzhiyun * Takes care of proper refcounting while performing enable/disable: handles
1146*4882a593Smuzhiyun * the special case of ALL sources requests by itself.
1147*4882a593Smuzhiyun * Returns successfully if at least one of the required src_id has been
1148*4882a593Smuzhiyun * successfully enabled/disabled.
1149*4882a593Smuzhiyun *
1150*4882a593Smuzhiyun * Return: 0 on Success
1151*4882a593Smuzhiyun */
__scmi_enable_evt(struct scmi_registered_event * r_evt,u32 src_id,bool enable)1152*4882a593Smuzhiyun static inline int __scmi_enable_evt(struct scmi_registered_event *r_evt,
1153*4882a593Smuzhiyun u32 src_id, bool enable)
1154*4882a593Smuzhiyun {
1155*4882a593Smuzhiyun int retvals = 0;
1156*4882a593Smuzhiyun u32 num_sources;
1157*4882a593Smuzhiyun refcount_t *sid;
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun if (src_id == SRC_ID_MASK) {
1160*4882a593Smuzhiyun src_id = 0;
1161*4882a593Smuzhiyun num_sources = r_evt->num_sources;
1162*4882a593Smuzhiyun } else if (src_id < r_evt->num_sources) {
1163*4882a593Smuzhiyun num_sources = 1;
1164*4882a593Smuzhiyun } else {
1165*4882a593Smuzhiyun return -EINVAL;
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun mutex_lock(&r_evt->sources_mtx);
1169*4882a593Smuzhiyun if (enable) {
1170*4882a593Smuzhiyun for (; num_sources; src_id++, num_sources--) {
1171*4882a593Smuzhiyun int ret = 0;
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun sid = &r_evt->sources[src_id];
1174*4882a593Smuzhiyun if (refcount_read(sid) == 0) {
1175*4882a593Smuzhiyun ret = REVT_NOTIFY_ENABLE(r_evt, r_evt->evt->id,
1176*4882a593Smuzhiyun src_id);
1177*4882a593Smuzhiyun if (!ret)
1178*4882a593Smuzhiyun refcount_set(sid, 1);
1179*4882a593Smuzhiyun } else {
1180*4882a593Smuzhiyun refcount_inc(sid);
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun retvals += !ret;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun } else {
1185*4882a593Smuzhiyun for (; num_sources; src_id++, num_sources--) {
1186*4882a593Smuzhiyun sid = &r_evt->sources[src_id];
1187*4882a593Smuzhiyun if (refcount_dec_and_test(sid))
1188*4882a593Smuzhiyun REVT_NOTIFY_DISABLE(r_evt,
1189*4882a593Smuzhiyun r_evt->evt->id, src_id);
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun retvals = 1;
1192*4882a593Smuzhiyun }
1193*4882a593Smuzhiyun mutex_unlock(&r_evt->sources_mtx);
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun return retvals ? 0 : -EINVAL;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun
scmi_enable_events(struct scmi_event_handler * hndl)1198*4882a593Smuzhiyun static int scmi_enable_events(struct scmi_event_handler *hndl)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun int ret = 0;
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun if (!hndl->enabled) {
1203*4882a593Smuzhiyun ret = __scmi_enable_evt(hndl->r_evt,
1204*4882a593Smuzhiyun KEY_XTRACT_SRC_ID(hndl->key), true);
1205*4882a593Smuzhiyun if (!ret)
1206*4882a593Smuzhiyun hndl->enabled = true;
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun return ret;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun
scmi_disable_events(struct scmi_event_handler * hndl)1212*4882a593Smuzhiyun static int scmi_disable_events(struct scmi_event_handler *hndl)
1213*4882a593Smuzhiyun {
1214*4882a593Smuzhiyun int ret = 0;
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun if (hndl->enabled) {
1217*4882a593Smuzhiyun ret = __scmi_enable_evt(hndl->r_evt,
1218*4882a593Smuzhiyun KEY_XTRACT_SRC_ID(hndl->key), false);
1219*4882a593Smuzhiyun if (!ret)
1220*4882a593Smuzhiyun hndl->enabled = false;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun return ret;
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun /**
1227*4882a593Smuzhiyun * scmi_put_handler_unlocked() - Put an event handler
1228*4882a593Smuzhiyun * @ni: A reference to the notification instance to use
1229*4882a593Smuzhiyun * @hndl: The event handler to act upon
1230*4882a593Smuzhiyun *
1231*4882a593Smuzhiyun * After having got exclusive access to the registered handlers hashtable,
1232*4882a593Smuzhiyun * update the refcount and if @hndl is no more in use by anyone:
1233*4882a593Smuzhiyun * * ask for events' generation disabling
1234*4882a593Smuzhiyun * * unregister and free the handler itself
1235*4882a593Smuzhiyun *
1236*4882a593Smuzhiyun * Context: Assumes all the proper locking has been managed by the caller.
1237*4882a593Smuzhiyun *
1238*4882a593Smuzhiyun * Return: True if handler was freed (users dropped to zero)
1239*4882a593Smuzhiyun */
scmi_put_handler_unlocked(struct scmi_notify_instance * ni,struct scmi_event_handler * hndl)1240*4882a593Smuzhiyun static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
1241*4882a593Smuzhiyun struct scmi_event_handler *hndl)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun bool freed = false;
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if (refcount_dec_and_test(&hndl->users)) {
1246*4882a593Smuzhiyun if (!IS_HNDL_PENDING(hndl))
1247*4882a593Smuzhiyun scmi_disable_events(hndl);
1248*4882a593Smuzhiyun scmi_free_event_handler(hndl);
1249*4882a593Smuzhiyun freed = true;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun return freed;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
scmi_put_handler(struct scmi_notify_instance * ni,struct scmi_event_handler * hndl)1255*4882a593Smuzhiyun static void scmi_put_handler(struct scmi_notify_instance *ni,
1256*4882a593Smuzhiyun struct scmi_event_handler *hndl)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun bool freed;
1259*4882a593Smuzhiyun u8 protocol_id;
1260*4882a593Smuzhiyun struct scmi_registered_event *r_evt = hndl->r_evt;
1261*4882a593Smuzhiyun
1262*4882a593Smuzhiyun mutex_lock(&ni->pending_mtx);
1263*4882a593Smuzhiyun if (r_evt) {
1264*4882a593Smuzhiyun protocol_id = r_evt->proto->id;
1265*4882a593Smuzhiyun mutex_lock(&r_evt->proto->registered_mtx);
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun freed = scmi_put_handler_unlocked(ni, hndl);
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun if (r_evt) {
1271*4882a593Smuzhiyun mutex_unlock(&r_evt->proto->registered_mtx);
1272*4882a593Smuzhiyun /*
1273*4882a593Smuzhiyun * Only registered handler acquired protocol; must be here
1274*4882a593Smuzhiyun * released only AFTER unlocking registered_mtx, since
1275*4882a593Smuzhiyun * releasing a protocol can trigger its de-initialization
1276*4882a593Smuzhiyun * (ie. including r_evt and registered_mtx)
1277*4882a593Smuzhiyun */
1278*4882a593Smuzhiyun if (freed)
1279*4882a593Smuzhiyun scmi_release_protocol(ni->handle, protocol_id);
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun mutex_unlock(&ni->pending_mtx);
1282*4882a593Smuzhiyun }
1283*4882a593Smuzhiyun
scmi_put_active_handler(struct scmi_notify_instance * ni,struct scmi_event_handler * hndl)1284*4882a593Smuzhiyun static void scmi_put_active_handler(struct scmi_notify_instance *ni,
1285*4882a593Smuzhiyun struct scmi_event_handler *hndl)
1286*4882a593Smuzhiyun {
1287*4882a593Smuzhiyun bool freed;
1288*4882a593Smuzhiyun struct scmi_registered_event *r_evt = hndl->r_evt;
1289*4882a593Smuzhiyun u8 protocol_id = r_evt->proto->id;
1290*4882a593Smuzhiyun
1291*4882a593Smuzhiyun mutex_lock(&r_evt->proto->registered_mtx);
1292*4882a593Smuzhiyun freed = scmi_put_handler_unlocked(ni, hndl);
1293*4882a593Smuzhiyun mutex_unlock(&r_evt->proto->registered_mtx);
1294*4882a593Smuzhiyun if (freed)
1295*4882a593Smuzhiyun scmi_release_protocol(ni->handle, protocol_id);
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun
1298*4882a593Smuzhiyun /**
1299*4882a593Smuzhiyun * scmi_event_handler_enable_events() - Enable events associated to an handler
1300*4882a593Smuzhiyun * @hndl: The Event handler to act upon
1301*4882a593Smuzhiyun *
1302*4882a593Smuzhiyun * Return: 0 on Success
1303*4882a593Smuzhiyun */
scmi_event_handler_enable_events(struct scmi_event_handler * hndl)1304*4882a593Smuzhiyun static int scmi_event_handler_enable_events(struct scmi_event_handler *hndl)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun if (scmi_enable_events(hndl)) {
1307*4882a593Smuzhiyun pr_err("Failed to ENABLE events for key:%X !\n", hndl->key);
1308*4882a593Smuzhiyun return -EINVAL;
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun return 0;
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun /**
1315*4882a593Smuzhiyun * scmi_register_notifier() - Register a notifier_block for an event
1316*4882a593Smuzhiyun * @handle: The handle identifying the platform instance against which the
1317*4882a593Smuzhiyun * callback is registered
1318*4882a593Smuzhiyun * @proto_id: Protocol ID
1319*4882a593Smuzhiyun * @evt_id: Event ID
1320*4882a593Smuzhiyun * @src_id: Source ID, when NULL register for events coming form ALL possible
1321*4882a593Smuzhiyun * sources
1322*4882a593Smuzhiyun * @nb: A standard notifier block to register for the specified event
1323*4882a593Smuzhiyun *
1324*4882a593Smuzhiyun * Generic helper to register a notifier_block against a protocol event.
1325*4882a593Smuzhiyun *
1326*4882a593Smuzhiyun * A notifier_block @nb will be registered for each distinct event identified
1327*4882a593Smuzhiyun * by the tuple (proto_id, evt_id, src_id) on a dedicated notification chain
1328*4882a593Smuzhiyun * so that:
1329*4882a593Smuzhiyun *
1330*4882a593Smuzhiyun * (proto_X, evt_Y, src_Z) --> chain_X_Y_Z
1331*4882a593Smuzhiyun *
1332*4882a593Smuzhiyun * @src_id meaning is protocol specific and identifies the origin of the event
1333*4882a593Smuzhiyun * (like domain_id, sensor_id and so forth).
1334*4882a593Smuzhiyun *
1335*4882a593Smuzhiyun * @src_id can be NULL to signify that the caller is interested in receiving
1336*4882a593Smuzhiyun * notifications from ALL the available sources for that protocol OR simply that
1337*4882a593Smuzhiyun * the protocol does not support distinct sources.
1338*4882a593Smuzhiyun *
1339*4882a593Smuzhiyun * As soon as one user for the specified tuple appears, an handler is created,
1340*4882a593Smuzhiyun * and that specific event's generation is enabled at the platform level, unless
1341*4882a593Smuzhiyun * an associated registered event is found missing, meaning that the needed
1342*4882a593Smuzhiyun * protocol is still to be initialized and the handler has just been registered
1343*4882a593Smuzhiyun * as still pending.
1344*4882a593Smuzhiyun *
1345*4882a593Smuzhiyun * Return: 0 on Success
1346*4882a593Smuzhiyun */
scmi_register_notifier(const struct scmi_handle * handle,u8 proto_id,u8 evt_id,u32 * src_id,struct notifier_block * nb)1347*4882a593Smuzhiyun static int scmi_register_notifier(const struct scmi_handle *handle,
1348*4882a593Smuzhiyun u8 proto_id, u8 evt_id, u32 *src_id,
1349*4882a593Smuzhiyun struct notifier_block *nb)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun int ret = 0;
1352*4882a593Smuzhiyun u32 evt_key;
1353*4882a593Smuzhiyun struct scmi_event_handler *hndl;
1354*4882a593Smuzhiyun struct scmi_notify_instance *ni;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun ni = scmi_get_notification_instance_data(handle);
1357*4882a593Smuzhiyun if (!ni)
1358*4882a593Smuzhiyun return -ENODEV;
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun evt_key = MAKE_HASH_KEY(proto_id, evt_id,
1361*4882a593Smuzhiyun src_id ? *src_id : SRC_ID_MASK);
1362*4882a593Smuzhiyun hndl = scmi_get_or_create_handler(ni, evt_key);
1363*4882a593Smuzhiyun if (!hndl)
1364*4882a593Smuzhiyun return -EINVAL;
1365*4882a593Smuzhiyun
1366*4882a593Smuzhiyun blocking_notifier_chain_register(&hndl->chain, nb);
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun /* Enable events for not pending handlers */
1369*4882a593Smuzhiyun if (!IS_HNDL_PENDING(hndl)) {
1370*4882a593Smuzhiyun ret = scmi_event_handler_enable_events(hndl);
1371*4882a593Smuzhiyun if (ret)
1372*4882a593Smuzhiyun scmi_put_handler(ni, hndl);
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun return ret;
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun /**
1379*4882a593Smuzhiyun * scmi_unregister_notifier() - Unregister a notifier_block for an event
1380*4882a593Smuzhiyun * @handle: The handle identifying the platform instance against which the
1381*4882a593Smuzhiyun * callback is unregistered
1382*4882a593Smuzhiyun * @proto_id: Protocol ID
1383*4882a593Smuzhiyun * @evt_id: Event ID
1384*4882a593Smuzhiyun * @src_id: Source ID
1385*4882a593Smuzhiyun * @nb: The notifier_block to unregister
1386*4882a593Smuzhiyun *
1387*4882a593Smuzhiyun * Takes care to unregister the provided @nb from the notification chain
1388*4882a593Smuzhiyun * associated to the specified event and, if there are no more users for the
1389*4882a593Smuzhiyun * event handler, frees also the associated event handler structures.
1390*4882a593Smuzhiyun * (this could possibly cause disabling of event's generation at platform level)
1391*4882a593Smuzhiyun *
1392*4882a593Smuzhiyun * Return: 0 on Success
1393*4882a593Smuzhiyun */
scmi_unregister_notifier(const struct scmi_handle * handle,u8 proto_id,u8 evt_id,u32 * src_id,struct notifier_block * nb)1394*4882a593Smuzhiyun static int scmi_unregister_notifier(const struct scmi_handle *handle,
1395*4882a593Smuzhiyun u8 proto_id, u8 evt_id, u32 *src_id,
1396*4882a593Smuzhiyun struct notifier_block *nb)
1397*4882a593Smuzhiyun {
1398*4882a593Smuzhiyun u32 evt_key;
1399*4882a593Smuzhiyun struct scmi_event_handler *hndl;
1400*4882a593Smuzhiyun struct scmi_notify_instance *ni;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun ni = scmi_get_notification_instance_data(handle);
1403*4882a593Smuzhiyun if (!ni)
1404*4882a593Smuzhiyun return -ENODEV;
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun evt_key = MAKE_HASH_KEY(proto_id, evt_id,
1407*4882a593Smuzhiyun src_id ? *src_id : SRC_ID_MASK);
1408*4882a593Smuzhiyun hndl = scmi_get_handler(ni, evt_key);
1409*4882a593Smuzhiyun if (!hndl)
1410*4882a593Smuzhiyun return -EINVAL;
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun /*
1413*4882a593Smuzhiyun * Note that this chain unregistration call is safe on its own
1414*4882a593Smuzhiyun * being internally protected by an rwsem.
1415*4882a593Smuzhiyun */
1416*4882a593Smuzhiyun blocking_notifier_chain_unregister(&hndl->chain, nb);
1417*4882a593Smuzhiyun scmi_put_handler(ni, hndl);
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun /*
1420*4882a593Smuzhiyun * This balances the initial get issued in @scmi_register_notifier.
1421*4882a593Smuzhiyun * If this notifier_block happened to be the last known user callback
1422*4882a593Smuzhiyun * for this event, the handler is here freed and the event's generation
1423*4882a593Smuzhiyun * stopped.
1424*4882a593Smuzhiyun *
1425*4882a593Smuzhiyun * Note that, an ongoing concurrent lookup on the delivery workqueue
1426*4882a593Smuzhiyun * path could still hold the refcount to 1 even after this routine
1427*4882a593Smuzhiyun * completes: in such a case it will be the final put on the delivery
1428*4882a593Smuzhiyun * path which will finally free this unused handler.
1429*4882a593Smuzhiyun */
1430*4882a593Smuzhiyun scmi_put_handler(ni, hndl);
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun return 0;
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun
1435*4882a593Smuzhiyun struct scmi_notifier_devres {
1436*4882a593Smuzhiyun const struct scmi_handle *handle;
1437*4882a593Smuzhiyun u8 proto_id;
1438*4882a593Smuzhiyun u8 evt_id;
1439*4882a593Smuzhiyun u32 __src_id;
1440*4882a593Smuzhiyun u32 *src_id;
1441*4882a593Smuzhiyun struct notifier_block *nb;
1442*4882a593Smuzhiyun };
1443*4882a593Smuzhiyun
scmi_devm_release_notifier(struct device * dev,void * res)1444*4882a593Smuzhiyun static void scmi_devm_release_notifier(struct device *dev, void *res)
1445*4882a593Smuzhiyun {
1446*4882a593Smuzhiyun struct scmi_notifier_devres *dres = res;
1447*4882a593Smuzhiyun
1448*4882a593Smuzhiyun scmi_unregister_notifier(dres->handle, dres->proto_id, dres->evt_id,
1449*4882a593Smuzhiyun dres->src_id, dres->nb);
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun
1452*4882a593Smuzhiyun /**
1453*4882a593Smuzhiyun * scmi_devm_register_notifier() - Managed registration of a notifier_block
1454*4882a593Smuzhiyun * for an event
1455*4882a593Smuzhiyun * @sdev: A reference to an scmi_device whose embedded struct device is to
1456*4882a593Smuzhiyun * be used for devres accounting.
1457*4882a593Smuzhiyun * @proto_id: Protocol ID
1458*4882a593Smuzhiyun * @evt_id: Event ID
1459*4882a593Smuzhiyun * @src_id: Source ID, when NULL register for events coming form ALL possible
1460*4882a593Smuzhiyun * sources
1461*4882a593Smuzhiyun * @nb: A standard notifier block to register for the specified event
1462*4882a593Smuzhiyun *
1463*4882a593Smuzhiyun * Generic devres managed helper to register a notifier_block against a
1464*4882a593Smuzhiyun * protocol event.
1465*4882a593Smuzhiyun */
scmi_devm_register_notifier(struct scmi_device * sdev,u8 proto_id,u8 evt_id,u32 * src_id,struct notifier_block * nb)1466*4882a593Smuzhiyun static int scmi_devm_register_notifier(struct scmi_device *sdev,
1467*4882a593Smuzhiyun u8 proto_id, u8 evt_id, u32 *src_id,
1468*4882a593Smuzhiyun struct notifier_block *nb)
1469*4882a593Smuzhiyun {
1470*4882a593Smuzhiyun int ret;
1471*4882a593Smuzhiyun struct scmi_notifier_devres *dres;
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun dres = devres_alloc(scmi_devm_release_notifier,
1474*4882a593Smuzhiyun sizeof(*dres), GFP_KERNEL);
1475*4882a593Smuzhiyun if (!dres)
1476*4882a593Smuzhiyun return -ENOMEM;
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun ret = scmi_register_notifier(sdev->handle, proto_id,
1479*4882a593Smuzhiyun evt_id, src_id, nb);
1480*4882a593Smuzhiyun if (ret) {
1481*4882a593Smuzhiyun devres_free(dres);
1482*4882a593Smuzhiyun return ret;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun dres->handle = sdev->handle;
1486*4882a593Smuzhiyun dres->proto_id = proto_id;
1487*4882a593Smuzhiyun dres->evt_id = evt_id;
1488*4882a593Smuzhiyun dres->nb = nb;
1489*4882a593Smuzhiyun if (src_id) {
1490*4882a593Smuzhiyun dres->__src_id = *src_id;
1491*4882a593Smuzhiyun dres->src_id = &dres->__src_id;
1492*4882a593Smuzhiyun } else {
1493*4882a593Smuzhiyun dres->src_id = NULL;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun devres_add(&sdev->dev, dres);
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun return ret;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun
scmi_devm_notifier_match(struct device * dev,void * res,void * data)1500*4882a593Smuzhiyun static int scmi_devm_notifier_match(struct device *dev, void *res, void *data)
1501*4882a593Smuzhiyun {
1502*4882a593Smuzhiyun struct scmi_notifier_devres *dres = res;
1503*4882a593Smuzhiyun struct scmi_notifier_devres *xres = data;
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun if (WARN_ON(!dres || !xres))
1506*4882a593Smuzhiyun return 0;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun return dres->proto_id == xres->proto_id &&
1509*4882a593Smuzhiyun dres->evt_id == xres->evt_id &&
1510*4882a593Smuzhiyun dres->nb == xres->nb &&
1511*4882a593Smuzhiyun ((!dres->src_id && !xres->src_id) ||
1512*4882a593Smuzhiyun (dres->src_id && xres->src_id &&
1513*4882a593Smuzhiyun dres->__src_id == xres->__src_id));
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun
1516*4882a593Smuzhiyun /**
1517*4882a593Smuzhiyun * scmi_devm_unregister_notifier() - Managed un-registration of a
1518*4882a593Smuzhiyun * notifier_block for an event
1519*4882a593Smuzhiyun * @sdev: A reference to an scmi_device whose embedded struct device is to
1520*4882a593Smuzhiyun * be used for devres accounting.
1521*4882a593Smuzhiyun * @proto_id: Protocol ID
1522*4882a593Smuzhiyun * @evt_id: Event ID
1523*4882a593Smuzhiyun * @src_id: Source ID, when NULL register for events coming form ALL possible
1524*4882a593Smuzhiyun * sources
1525*4882a593Smuzhiyun * @nb: A standard notifier block to register for the specified event
1526*4882a593Smuzhiyun *
1527*4882a593Smuzhiyun * Generic devres managed helper to explicitly un-register a notifier_block
1528*4882a593Smuzhiyun * against a protocol event, which was previously registered using the above
1529*4882a593Smuzhiyun * @scmi_devm_register_notifier.
1530*4882a593Smuzhiyun */
scmi_devm_unregister_notifier(struct scmi_device * sdev,u8 proto_id,u8 evt_id,u32 * src_id,struct notifier_block * nb)1531*4882a593Smuzhiyun static int scmi_devm_unregister_notifier(struct scmi_device *sdev,
1532*4882a593Smuzhiyun u8 proto_id, u8 evt_id, u32 *src_id,
1533*4882a593Smuzhiyun struct notifier_block *nb)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun int ret;
1536*4882a593Smuzhiyun struct scmi_notifier_devres dres;
1537*4882a593Smuzhiyun
1538*4882a593Smuzhiyun dres.handle = sdev->handle;
1539*4882a593Smuzhiyun dres.proto_id = proto_id;
1540*4882a593Smuzhiyun dres.evt_id = evt_id;
1541*4882a593Smuzhiyun if (src_id) {
1542*4882a593Smuzhiyun dres.__src_id = *src_id;
1543*4882a593Smuzhiyun dres.src_id = &dres.__src_id;
1544*4882a593Smuzhiyun } else {
1545*4882a593Smuzhiyun dres.src_id = NULL;
1546*4882a593Smuzhiyun }
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun ret = devres_release(&sdev->dev, scmi_devm_release_notifier,
1549*4882a593Smuzhiyun scmi_devm_notifier_match, &dres);
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun WARN_ON(ret);
1552*4882a593Smuzhiyun
1553*4882a593Smuzhiyun return ret;
1554*4882a593Smuzhiyun }
1555*4882a593Smuzhiyun
1556*4882a593Smuzhiyun /**
1557*4882a593Smuzhiyun * scmi_protocols_late_init() - Worker for late initialization
1558*4882a593Smuzhiyun * @work: The work item to use associated to the proper SCMI instance
1559*4882a593Smuzhiyun *
1560*4882a593Smuzhiyun * This kicks in whenever a new protocol has completed its own registration via
1561*4882a593Smuzhiyun * scmi_register_protocol_events(): it is in charge of scanning the table of
1562*4882a593Smuzhiyun * pending handlers (registered by users while the related protocol was still
1563*4882a593Smuzhiyun * not initialized) and finalizing their initialization whenever possible;
1564*4882a593Smuzhiyun * invalid pending handlers are purged at this point in time.
1565*4882a593Smuzhiyun */
scmi_protocols_late_init(struct work_struct * work)1566*4882a593Smuzhiyun static void scmi_protocols_late_init(struct work_struct *work)
1567*4882a593Smuzhiyun {
1568*4882a593Smuzhiyun int bkt;
1569*4882a593Smuzhiyun struct scmi_event_handler *hndl;
1570*4882a593Smuzhiyun struct scmi_notify_instance *ni;
1571*4882a593Smuzhiyun struct hlist_node *tmp;
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun ni = container_of(work, struct scmi_notify_instance, init_work);
1574*4882a593Smuzhiyun
1575*4882a593Smuzhiyun /* Ensure protocols and events are up to date */
1576*4882a593Smuzhiyun smp_rmb();
1577*4882a593Smuzhiyun
1578*4882a593Smuzhiyun mutex_lock(&ni->pending_mtx);
1579*4882a593Smuzhiyun hash_for_each_safe(ni->pending_events_handlers, bkt, tmp, hndl, hash) {
1580*4882a593Smuzhiyun int ret;
1581*4882a593Smuzhiyun
1582*4882a593Smuzhiyun ret = scmi_bind_event_handler(ni, hndl);
1583*4882a593Smuzhiyun if (!ret) {
1584*4882a593Smuzhiyun dev_dbg(ni->handle->dev,
1585*4882a593Smuzhiyun "finalized PENDING handler - key:%X\n",
1586*4882a593Smuzhiyun hndl->key);
1587*4882a593Smuzhiyun ret = scmi_event_handler_enable_events(hndl);
1588*4882a593Smuzhiyun if (ret) {
1589*4882a593Smuzhiyun dev_dbg(ni->handle->dev,
1590*4882a593Smuzhiyun "purging INVALID handler - key:%X\n",
1591*4882a593Smuzhiyun hndl->key);
1592*4882a593Smuzhiyun scmi_put_active_handler(ni, hndl);
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun } else {
1595*4882a593Smuzhiyun ret = scmi_valid_pending_handler(ni, hndl);
1596*4882a593Smuzhiyun if (ret) {
1597*4882a593Smuzhiyun dev_dbg(ni->handle->dev,
1598*4882a593Smuzhiyun "purging PENDING handler - key:%X\n",
1599*4882a593Smuzhiyun hndl->key);
1600*4882a593Smuzhiyun /* this hndl can be only a pending one */
1601*4882a593Smuzhiyun scmi_put_handler_unlocked(ni, hndl);
1602*4882a593Smuzhiyun }
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun }
1605*4882a593Smuzhiyun mutex_unlock(&ni->pending_mtx);
1606*4882a593Smuzhiyun }
1607*4882a593Smuzhiyun
1608*4882a593Smuzhiyun /*
1609*4882a593Smuzhiyun * notify_ops are attached to the handle so that can be accessed
1610*4882a593Smuzhiyun * directly from an scmi_driver to register its own notifiers.
1611*4882a593Smuzhiyun */
1612*4882a593Smuzhiyun static const struct scmi_notify_ops notify_ops = {
1613*4882a593Smuzhiyun .devm_register_event_notifier = scmi_devm_register_notifier,
1614*4882a593Smuzhiyun .devm_unregister_event_notifier = scmi_devm_unregister_notifier,
1615*4882a593Smuzhiyun .register_event_notifier = scmi_register_notifier,
1616*4882a593Smuzhiyun .unregister_event_notifier = scmi_unregister_notifier,
1617*4882a593Smuzhiyun };
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun /**
1620*4882a593Smuzhiyun * scmi_notification_init() - Initializes Notification Core Support
1621*4882a593Smuzhiyun * @handle: The handle identifying the platform instance to initialize
1622*4882a593Smuzhiyun *
1623*4882a593Smuzhiyun * This function lays out all the basic resources needed by the notification
1624*4882a593Smuzhiyun * core instance identified by the provided handle: once done, all of the
1625*4882a593Smuzhiyun * SCMI Protocols can register their events with the core during their own
1626*4882a593Smuzhiyun * initializations.
1627*4882a593Smuzhiyun *
1628*4882a593Smuzhiyun * Note that failing to initialize the core notifications support does not
1629*4882a593Smuzhiyun * cause the whole SCMI Protocols stack to fail its initialization.
1630*4882a593Smuzhiyun *
1631*4882a593Smuzhiyun * SCMI Notification Initialization happens in 2 steps:
1632*4882a593Smuzhiyun * * initialization: basic common allocations (this function)
1633*4882a593Smuzhiyun * * registration: protocols asynchronously come into life and registers their
1634*4882a593Smuzhiyun * own supported list of events with the core; this causes
1635*4882a593Smuzhiyun * further per-protocol allocations
1636*4882a593Smuzhiyun *
1637*4882a593Smuzhiyun * Any user's callback registration attempt, referring a still not registered
1638*4882a593Smuzhiyun * event, will be registered as pending and finalized later (if possible)
1639*4882a593Smuzhiyun * by scmi_protocols_late_init() work.
1640*4882a593Smuzhiyun * This allows for lazy initialization of SCMI Protocols due to late (or
1641*4882a593Smuzhiyun * missing) SCMI drivers' modules loading.
1642*4882a593Smuzhiyun *
1643*4882a593Smuzhiyun * Return: 0 on Success
1644*4882a593Smuzhiyun */
scmi_notification_init(struct scmi_handle * handle)1645*4882a593Smuzhiyun int scmi_notification_init(struct scmi_handle *handle)
1646*4882a593Smuzhiyun {
1647*4882a593Smuzhiyun void *gid;
1648*4882a593Smuzhiyun struct scmi_notify_instance *ni;
1649*4882a593Smuzhiyun
1650*4882a593Smuzhiyun gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
1651*4882a593Smuzhiyun if (!gid)
1652*4882a593Smuzhiyun return -ENOMEM;
1653*4882a593Smuzhiyun
1654*4882a593Smuzhiyun ni = devm_kzalloc(handle->dev, sizeof(*ni), GFP_KERNEL);
1655*4882a593Smuzhiyun if (!ni)
1656*4882a593Smuzhiyun goto err;
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun ni->gid = gid;
1659*4882a593Smuzhiyun ni->handle = handle;
1660*4882a593Smuzhiyun
1661*4882a593Smuzhiyun ni->registered_protocols = devm_kcalloc(handle->dev, SCMI_MAX_PROTO,
1662*4882a593Smuzhiyun sizeof(char *), GFP_KERNEL);
1663*4882a593Smuzhiyun if (!ni->registered_protocols)
1664*4882a593Smuzhiyun goto err;
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun ni->notify_wq = alloc_workqueue(dev_name(handle->dev),
1667*4882a593Smuzhiyun WQ_UNBOUND | WQ_FREEZABLE | WQ_SYSFS,
1668*4882a593Smuzhiyun 0);
1669*4882a593Smuzhiyun if (!ni->notify_wq)
1670*4882a593Smuzhiyun goto err;
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun mutex_init(&ni->pending_mtx);
1673*4882a593Smuzhiyun hash_init(ni->pending_events_handlers);
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun INIT_WORK(&ni->init_work, scmi_protocols_late_init);
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun scmi_set_notification_instance_data(handle, ni);
1678*4882a593Smuzhiyun handle->notify_ops = ¬ify_ops;
1679*4882a593Smuzhiyun /* Ensure handle is up to date */
1680*4882a593Smuzhiyun smp_wmb();
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun dev_info(handle->dev, "Core Enabled.\n");
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun devres_close_group(handle->dev, ni->gid);
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun return 0;
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun err:
1689*4882a593Smuzhiyun dev_warn(handle->dev, "Initialization Failed.\n");
1690*4882a593Smuzhiyun devres_release_group(handle->dev, gid);
1691*4882a593Smuzhiyun return -ENOMEM;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun /**
1695*4882a593Smuzhiyun * scmi_notification_exit() - Shutdown and clean Notification core
1696*4882a593Smuzhiyun * @handle: The handle identifying the platform instance to shutdown
1697*4882a593Smuzhiyun */
scmi_notification_exit(struct scmi_handle * handle)1698*4882a593Smuzhiyun void scmi_notification_exit(struct scmi_handle *handle)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun struct scmi_notify_instance *ni;
1701*4882a593Smuzhiyun
1702*4882a593Smuzhiyun ni = scmi_get_notification_instance_data(handle);
1703*4882a593Smuzhiyun if (!ni)
1704*4882a593Smuzhiyun return;
1705*4882a593Smuzhiyun scmi_set_notification_instance_data(handle, NULL);
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun /* Destroy while letting pending work complete */
1708*4882a593Smuzhiyun destroy_workqueue(ni->notify_wq);
1709*4882a593Smuzhiyun
1710*4882a593Smuzhiyun devres_release_group(ni->handle->dev, ni->gid);
1711*4882a593Smuzhiyun }
1712