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