| 1 | |
| 2 | |
| 3 | * Main SSAM/SSH controller structure and functionality.
|
| 4 | *
|
| 5 | * Copyright (C) 2019-2022 Maximilian Luz <[email protected]>
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/acpi.h>
|
| 9 | #include <linux/atomic.h>
|
| 10 | #include <linux/completion.h>
|
| 11 | #include <linux/gpio/consumer.h>
|
| 12 | #include <linux/interrupt.h>
|
| 13 | #include <linux/kref.h>
|
| 14 | #include <linux/limits.h>
|
| 15 | #include <linux/list.h>
|
| 16 | #include <linux/lockdep.h>
|
| 17 | #include <linux/mutex.h>
|
| 18 | #include <linux/rculist.h>
|
| 19 | #include <linux/rbtree.h>
|
| 20 | #include <linux/rwsem.h>
|
| 21 | #include <linux/serdev.h>
|
| 22 | #include <linux/slab.h>
|
| 23 | #include <linux/spinlock.h>
|
| 24 | #include <linux/srcu.h>
|
| 25 | #include <linux/types.h>
|
| 26 | #include <linux/workqueue.h>
|
| 27 |
|
| 28 | #include <linux/surface_aggregator/controller.h>
|
| 29 | #include <linux/surface_aggregator/serial_hub.h>
|
| 30 |
|
| 31 | #include "controller.h"
|
| 32 | #include "ssh_msgb.h"
|
| 33 | #include "ssh_request_layer.h"
|
| 34 |
|
| 35 | #include "trace.h"
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 | |
| 41 | * ssh_seq_reset() - Reset/initialize sequence ID counter.
|
| 42 | * @c: The counter to reset.
|
| 43 |
|
| 44 | static void ssh_seq_reset(struct ssh_seq_counter *c)
|
| 45 | {
|
| 46 | WRITE_ONCE(c->value, 0);
|
| 47 | }
|
| 48 |
|
| 49 | |
| 50 | * ssh_seq_next() - Get next sequence ID.
|
| 51 | * @c: The counter providing the sequence IDs.
|
| 52 | *
|
| 53 | * Return: Returns the next sequence ID of the counter.
|
| 54 |
|
| 55 | static u8 ssh_seq_next(struct ssh_seq_counter *c)
|
| 56 | {
|
| 57 | u8 old = READ_ONCE(c->value);
|
| 58 | u8 new = old + 1;
|
| 59 | u8 ret;
|
| 60 |
|
| 61 | while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
|
| 62 | old = ret;
|
| 63 | new = old + 1;
|
| 64 | }
|
| 65 |
|
| 66 | return old;
|
| 67 | }
|
| 68 |
|
| 69 | |
| 70 | * ssh_rqid_reset() - Reset/initialize request ID counter.
|
| 71 | * @c: The counter to reset.
|
| 72 |
|
| 73 | static void ssh_rqid_reset(struct ssh_rqid_counter *c)
|
| 74 | {
|
| 75 | WRITE_ONCE(c->value, 0);
|
| 76 | }
|
| 77 |
|
| 78 | |
| 79 | * ssh_rqid_next() - Get next request ID.
|
| 80 | * @c: The counter providing the request IDs.
|
| 81 | *
|
| 82 | * Return: Returns the next request ID of the counter, skipping any reserved
|
| 83 | * request IDs.
|
| 84 |
|
| 85 | static u16 ssh_rqid_next(struct ssh_rqid_counter *c)
|
| 86 | {
|
| 87 | u16 old = READ_ONCE(c->value);
|
| 88 | u16 new = ssh_rqid_next_valid(old);
|
| 89 | u16 ret;
|
| 90 |
|
| 91 | while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
|
| 92 | old = ret;
|
| 93 | new = ssh_rqid_next_valid(old);
|
| 94 | }
|
| 95 |
|
| 96 | return old;
|
| 97 | }
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 | |
| 102 | * The notifier system is based on linux/notifier.h, specifically the SRCU
|
| 103 | * implementation. The difference to that is, that some bits of the notifier
|
| 104 | * call return value can be tracked across multiple calls. This is done so
|
| 105 | * that handling of events can be tracked and a warning can be issued in case
|
| 106 | * an event goes unhandled. The idea of that warning is that it should help
|
| 107 | * discover and identify new/currently unimplemented features.
|
| 108 |
|
| 109 |
|
| 110 | |
| 111 | * ssam_event_matches_notifier() - Test if an event matches a notifier.
|
| 112 | * @n: The event notifier to test against.
|
| 113 | * @event: The event to test.
|
| 114 | *
|
| 115 | * Return: Returns %true if the given event matches the given notifier
|
| 116 | * according to the rules set in the notifier's event mask, %false otherwise.
|
| 117 |
|
| 118 | static bool ssam_event_matches_notifier(const struct ssam_event_notifier *n,
|
| 119 | const struct ssam_event *event)
|
| 120 | {
|
| 121 | bool match = n->event.id.target_category == event->target_category;
|
| 122 |
|
| 123 | if (n->event.mask & SSAM_EVENT_MASK_TARGET)
|
| 124 | match &= n->event.reg.target_id == event->target_id;
|
| 125 |
|
| 126 | if (n->event.mask & SSAM_EVENT_MASK_INSTANCE)
|
| 127 | match &= n->event.id.instance == event->instance_id;
|
| 128 |
|
| 129 | return match;
|
| 130 | }
|
| 131 |
|
| 132 | |
| 133 | * ssam_nfblk_call_chain() - Call event notifier callbacks of the given chain.
|
| 134 | * @nh: The notifier head for which the notifier callbacks should be called.
|
| 135 | * @event: The event data provided to the callbacks.
|
| 136 | *
|
| 137 | * Call all registered notifier callbacks in order of their priority until
|
| 138 | * either no notifier is left or a notifier returns a value with the
|
| 139 | * %SSAM_NOTIF_STOP bit set. Note that this bit is automatically set via
|
| 140 | * ssam_notifier_from_errno() on any non-zero error value.
|
| 141 | *
|
| 142 | * Return: Returns the notifier status value, which contains the notifier
|
| 143 | * status bits (%SSAM_NOTIF_HANDLED and %SSAM_NOTIF_STOP) as well as a
|
| 144 | * potential error value returned from the last executed notifier callback.
|
| 145 | * Use ssam_notifier_to_errno() to convert this value to the original error
|
| 146 | * value.
|
| 147 |
|
| 148 | static int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
|
| 149 | {
|
| 150 | struct ssam_event_notifier *nf;
|
| 151 | int ret = 0, idx;
|
| 152 |
|
| 153 | idx = srcu_read_lock(&nh->srcu);
|
| 154 |
|
| 155 | list_for_each_entry_rcu(nf, &nh->head, base.node,
|
| 156 | srcu_read_lock_held(&nh->srcu)) {
|
| 157 | if (ssam_event_matches_notifier(nf, event)) {
|
| 158 | ret = (ret & SSAM_NOTIF_STATE_MASK) | nf->base.fn(nf, event);
|
| 159 | if (ret & SSAM_NOTIF_STOP)
|
| 160 | break;
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | srcu_read_unlock(&nh->srcu, idx);
|
| 165 | return ret;
|
| 166 | }
|
| 167 |
|
| 168 | |
| 169 | * ssam_nfblk_insert() - Insert a new notifier block into the given notifier
|
| 170 | * list.
|
| 171 | * @nh: The notifier head into which the block should be inserted.
|
| 172 | * @nb: The notifier block to add.
|
| 173 | *
|
| 174 | * Note: This function must be synchronized by the caller with respect to other
|
| 175 | * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
|
| 176 | *
|
| 177 | * Return: Returns zero on success, %-EEXIST if the notifier block has already
|
| 178 | * been registered.
|
| 179 |
|
| 180 | static int ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
|
| 181 | {
|
| 182 | struct ssam_notifier_block *p;
|
| 183 | struct list_head *h;
|
| 184 |
|
| 185 |
|
| 186 | list_for_each(h, &nh->head) {
|
| 187 | p = list_entry(h, struct ssam_notifier_block, node);
|
| 188 |
|
| 189 | if (unlikely(p == nb)) {
|
| 190 | WARN(1, "double register detected");
|
| 191 | return -EEXIST;
|
| 192 | }
|
| 193 |
|
| 194 | if (nb->priority > p->priority)
|
| 195 | break;
|
| 196 | }
|
| 197 |
|
| 198 | list_add_tail_rcu(&nb->node, h);
|
| 199 | return 0;
|
| 200 | }
|
| 201 |
|
| 202 | |
| 203 | * ssam_nfblk_find() - Check if a notifier block is registered on the given
|
| 204 | * notifier head.
|
| 205 | * list.
|
| 206 | * @nh: The notifier head on which to search.
|
| 207 | * @nb: The notifier block to search for.
|
| 208 | *
|
| 209 | * Note: This function must be synchronized by the caller with respect to other
|
| 210 | * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
|
| 211 | *
|
| 212 | * Return: Returns true if the given notifier block is registered on the given
|
| 213 | * notifier head, false otherwise.
|
| 214 |
|
| 215 | static bool ssam_nfblk_find(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
|
| 216 | {
|
| 217 | struct ssam_notifier_block *p;
|
| 218 |
|
| 219 |
|
| 220 | list_for_each_entry(p, &nh->head, node) {
|
| 221 | if (p == nb)
|
| 222 | return true;
|
| 223 | }
|
| 224 |
|
| 225 | return false;
|
| 226 | }
|
| 227 |
|
| 228 | |
| 229 | * ssam_nfblk_remove() - Remove a notifier block from its notifier list.
|
| 230 | * @nb: The notifier block to be removed.
|
| 231 | *
|
| 232 | * Note: This function must be synchronized by the caller with respect to
|
| 233 | * other insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
|
| 234 | * Furthermore, the caller _must_ ensure SRCU synchronization by calling
|
| 235 | * synchronize_srcu() with ``nh->srcu`` after leaving the critical section, to
|
| 236 | * ensure that the removed notifier block is not in use any more.
|
| 237 |
|
| 238 | static void ssam_nfblk_remove(struct ssam_notifier_block *nb)
|
| 239 | {
|
| 240 | list_del_rcu(&nb->node);
|
| 241 | }
|
| 242 |
|
| 243 | |
| 244 | * ssam_nf_head_init() - Initialize the given notifier head.
|
| 245 | * @nh: The notifier head to initialize.
|
| 246 |
|
| 247 | static int ssam_nf_head_init(struct ssam_nf_head *nh)
|
| 248 | {
|
| 249 | int status;
|
| 250 |
|
| 251 | status = init_srcu_struct(&nh->srcu);
|
| 252 | if (status)
|
| 253 | return status;
|
| 254 |
|
| 255 | INIT_LIST_HEAD(&nh->head);
|
| 256 | return 0;
|
| 257 | }
|
| 258 |
|
| 259 | |
| 260 | * ssam_nf_head_destroy() - Deinitialize the given notifier head.
|
| 261 | * @nh: The notifier head to deinitialize.
|
| 262 |
|
| 263 | static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
|
| 264 | {
|
| 265 | cleanup_srcu_struct(&nh->srcu);
|
| 266 | }
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 | |
| 272 | * struct ssam_nf_refcount_key - Key used for event activation reference
|
| 273 | * counting.
|
| 274 | * @reg: The registry via which the event is enabled/disabled.
|
| 275 | * @id: The ID uniquely describing the event.
|
| 276 |
|
| 277 | struct ssam_nf_refcount_key {
|
| 278 | struct ssam_event_registry reg;
|
| 279 | struct ssam_event_id id;
|
| 280 | };
|
| 281 |
|
| 282 | |
| 283 | * struct ssam_nf_refcount_entry - RB-tree entry for reference counting event
|
| 284 | * activations.
|
| 285 | * @node: The node of this entry in the rb-tree.
|
| 286 | * @key: The key of the event.
|
| 287 | * @refcount: The reference-count of the event.
|
| 288 | * @flags: The flags used when enabling the event.
|
| 289 |
|
| 290 | struct ssam_nf_refcount_entry {
|
| 291 | struct rb_node node;
|
| 292 | struct ssam_nf_refcount_key key;
|
| 293 | int refcount;
|
| 294 | u8 flags;
|
| 295 | };
|
| 296 |
|
| 297 | |
| 298 | * ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
|
| 299 | * event.
|
| 300 | * @nf: The notifier system reference.
|
| 301 | * @reg: The registry used to enable/disable the event.
|
| 302 | * @id: The event ID.
|
| 303 | *
|
| 304 | * Increments the reference-/activation-count associated with the specified
|
| 305 | * event type/ID, allocating a new entry for this event ID if necessary. A
|
| 306 | * newly allocated entry will have a refcount of one.
|
| 307 | *
|
| 308 | * Note: ``nf->lock`` must be held when calling this function.
|
| 309 | *
|
| 310 | * Return: Returns the refcount entry on success. Returns an error pointer
|
| 311 | * with %-ENOSPC if there have already been %INT_MAX events of the specified
|
| 312 | * ID and type registered, or %-ENOMEM if the entry could not be allocated.
|
| 313 |
|
| 314 | static struct ssam_nf_refcount_entry *
|
| 315 | ssam_nf_refcount_inc(struct ssam_nf *nf, struct ssam_event_registry reg,
|
| 316 | struct ssam_event_id id)
|
| 317 | {
|
| 318 | struct ssam_nf_refcount_entry *entry;
|
| 319 | struct ssam_nf_refcount_key key;
|
| 320 | struct rb_node **link = &nf->refcount.rb_node;
|
| 321 | struct rb_node *parent = NULL;
|
| 322 | int cmp;
|
| 323 |
|
| 324 | lockdep_assert_held(&nf->lock);
|
| 325 |
|
| 326 | key.reg = reg;
|
| 327 | key.id = id;
|
| 328 |
|
| 329 | while (*link) {
|
| 330 | entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
|
| 331 | parent = *link;
|
| 332 |
|
| 333 | cmp = memcmp(&key, &entry->key, sizeof(key));
|
| 334 | if (cmp < 0) {
|
| 335 | link = &(*link)->rb_left;
|
| 336 | } else if (cmp > 0) {
|
| 337 | link = &(*link)->rb_right;
|
| 338 | } else if (entry->refcount < INT_MAX) {
|
| 339 | entry->refcount++;
|
| 340 | return entry;
|
| 341 | } else {
|
| 342 | WARN_ON(1);
|
| 343 | return ERR_PTR(-ENOSPC);
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | entry = kzalloc_obj(*entry);
|
| 348 | if (!entry)
|
| 349 | return ERR_PTR(-ENOMEM);
|
| 350 |
|
| 351 | entry->key = key;
|
| 352 | entry->refcount = 1;
|
| 353 |
|
| 354 | rb_link_node(&entry->node, parent, link);
|
| 355 | rb_insert_color(&entry->node, &nf->refcount);
|
| 356 |
|
| 357 | return entry;
|
| 358 | }
|
| 359 |
|
| 360 | |
| 361 | * ssam_nf_refcount_dec() - Decrement reference-/activation-count of the given
|
| 362 | * event.
|
| 363 | * @nf: The notifier system reference.
|
| 364 | * @reg: The registry used to enable/disable the event.
|
| 365 | * @id: The event ID.
|
| 366 | *
|
| 367 | * Decrements the reference-/activation-count of the specified event,
|
| 368 | * returning its entry. If the returned entry has a refcount of zero, the
|
| 369 | * caller is responsible for freeing it using kfree().
|
| 370 | *
|
| 371 | * Note: ``nf->lock`` must be held when calling this function.
|
| 372 | *
|
| 373 | * Return: Returns the refcount entry on success or %NULL if the entry has not
|
| 374 | * been found.
|
| 375 |
|
| 376 | static struct ssam_nf_refcount_entry *
|
| 377 | ssam_nf_refcount_dec(struct ssam_nf *nf, struct ssam_event_registry reg,
|
| 378 | struct ssam_event_id id)
|
| 379 | {
|
| 380 | struct ssam_nf_refcount_entry *entry;
|
| 381 | struct ssam_nf_refcount_key key;
|
| 382 | struct rb_node *node = nf->refcount.rb_node;
|
| 383 | int cmp;
|
| 384 |
|
| 385 | lockdep_assert_held(&nf->lock);
|
| 386 |
|
| 387 | key.reg = reg;
|
| 388 | key.id = id;
|
| 389 |
|
| 390 | while (node) {
|
| 391 | entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
|
| 392 |
|
| 393 | cmp = memcmp(&key, &entry->key, sizeof(key));
|
| 394 | if (cmp < 0) {
|
| 395 | node = node->rb_left;
|
| 396 | } else if (cmp > 0) {
|
| 397 | node = node->rb_right;
|
| 398 | } else {
|
| 399 | entry->refcount--;
|
| 400 | if (entry->refcount == 0)
|
| 401 | rb_erase(&entry->node, &nf->refcount);
|
| 402 |
|
| 403 | return entry;
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | return NULL;
|
| 408 | }
|
| 409 |
|
| 410 | |
| 411 | * ssam_nf_refcount_dec_free() - Decrement reference-/activation-count of the
|
| 412 | * given event and free its entry if the reference count reaches zero.
|
| 413 | * @nf: The notifier system reference.
|
| 414 | * @reg: The registry used to enable/disable the event.
|
| 415 | * @id: The event ID.
|
| 416 | *
|
| 417 | * Decrements the reference-/activation-count of the specified event, freeing
|
| 418 | * its entry if it reaches zero.
|
| 419 | *
|
| 420 | * Note: ``nf->lock`` must be held when calling this function.
|
| 421 |
|
| 422 | static void ssam_nf_refcount_dec_free(struct ssam_nf *nf,
|
| 423 | struct ssam_event_registry reg,
|
| 424 | struct ssam_event_id id)
|
| 425 | {
|
| 426 | struct ssam_nf_refcount_entry *entry;
|
| 427 |
|
| 428 | lockdep_assert_held(&nf->lock);
|
| 429 |
|
| 430 | entry = ssam_nf_refcount_dec(nf, reg, id);
|
| 431 | if (entry && entry->refcount == 0)
|
| 432 | kfree(entry);
|
| 433 | }
|
| 434 |
|
| 435 | |
| 436 | * ssam_nf_refcount_empty() - Test if the notification system has any
|
| 437 | * enabled/active events.
|
| 438 | * @nf: The notification system.
|
| 439 |
|
| 440 | static bool ssam_nf_refcount_empty(struct ssam_nf *nf)
|
| 441 | {
|
| 442 | return RB_EMPTY_ROOT(&nf->refcount);
|
| 443 | }
|
| 444 |
|
| 445 | |
| 446 | * ssam_nf_call() - Call notification callbacks for the provided event.
|
| 447 | * @nf: The notifier system
|
| 448 | * @dev: The associated device, only used for logging.
|
| 449 | * @rqid: The request ID of the event.
|
| 450 | * @event: The event provided to the callbacks.
|
| 451 | *
|
| 452 | * Execute registered callbacks in order of their priority until either no
|
| 453 | * callback is left or a callback returns a value with the %SSAM_NOTIF_STOP
|
| 454 | * bit set. Note that this bit is set automatically when converting non-zero
|
| 455 | * error values via ssam_notifier_from_errno() to notifier values.
|
| 456 | *
|
| 457 | * Also note that any callback that could handle an event should return a value
|
| 458 | * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
|
| 459 | * unhandled/ignored. In case no registered callback could handle an event,
|
| 460 | * this function will emit a warning.
|
| 461 | *
|
| 462 | * In case a callback failed, this function will emit an error message.
|
| 463 |
|
| 464 | static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
|
| 465 | struct ssam_event *event)
|
| 466 | {
|
| 467 | struct ssam_nf_head *nf_head;
|
| 468 | int status, nf_ret;
|
| 469 |
|
| 470 | if (!ssh_rqid_is_event(rqid)) {
|
| 471 | dev_warn(dev, "event: unsupported rqid: %#06x\n", rqid);
|
| 472 | return;
|
| 473 | }
|
| 474 |
|
| 475 | nf_head = &nf->head[ssh_rqid_to_event(rqid)];
|
| 476 | nf_ret = ssam_nfblk_call_chain(nf_head, event);
|
| 477 | status = ssam_notifier_to_errno(nf_ret);
|
| 478 |
|
| 479 | if (status < 0) {
|
| 480 | dev_err(dev,
|
| 481 | "event: error handling event: %d (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
|
| 482 | status, event->target_category, event->target_id,
|
| 483 | event->command_id, event->instance_id);
|
| 484 | } else if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
|
| 485 | dev_warn(dev,
|
| 486 | "event: unhandled event (rqid: %#04x, tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
|
| 487 | rqid, event->target_category, event->target_id,
|
| 488 | event->command_id, event->instance_id);
|
| 489 | }
|
| 490 | }
|
| 491 |
|
| 492 | |
| 493 | * ssam_nf_init() - Initialize the notifier system.
|
| 494 | * @nf: The notifier system to initialize.
|
| 495 |
|
| 496 | static int ssam_nf_init(struct ssam_nf *nf)
|
| 497 | {
|
| 498 | int i, status;
|
| 499 |
|
| 500 | for (i = 0; i < SSH_NUM_EVENTS; i++) {
|
| 501 | status = ssam_nf_head_init(&nf->head[i]);
|
| 502 | if (status)
|
| 503 | break;
|
| 504 | }
|
| 505 |
|
| 506 | if (status) {
|
| 507 | while (i--)
|
| 508 | ssam_nf_head_destroy(&nf->head[i]);
|
| 509 |
|
| 510 | return status;
|
| 511 | }
|
| 512 |
|
| 513 | mutex_init(&nf->lock);
|
| 514 | return 0;
|
| 515 | }
|
| 516 |
|
| 517 | |
| 518 | * ssam_nf_destroy() - Deinitialize the notifier system.
|
| 519 | * @nf: The notifier system to deinitialize.
|
| 520 |
|
| 521 | static void ssam_nf_destroy(struct ssam_nf *nf)
|
| 522 | {
|
| 523 | int i;
|
| 524 |
|
| 525 | for (i = 0; i < SSH_NUM_EVENTS; i++)
|
| 526 | ssam_nf_head_destroy(&nf->head[i]);
|
| 527 |
|
| 528 | mutex_destroy(&nf->lock);
|
| 529 | }
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 | #define SSAM_CPLT_WQ_NAME "ssam_cpltq"
|
| 535 |
|
| 536 | |
| 537 | * SSAM_CPLT_WQ_BATCH - Maximum number of event item completions executed per
|
| 538 | * work execution. Used to prevent livelocking of the workqueue. Value chosen
|
| 539 | * via educated guess, may be adjusted.
|
| 540 |
|
| 541 | #define SSAM_CPLT_WQ_BATCH 10
|
| 542 |
|
| 543 | |
| 544 | * SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN - Maximum payload length for a cached
|
| 545 | * &struct ssam_event_item.
|
| 546 | *
|
| 547 | * This length has been chosen to be accommodate standard touchpad and
|
| 548 | * keyboard input events. Events with larger payloads will be allocated
|
| 549 | * separately.
|
| 550 |
|
| 551 | #define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN 32
|
| 552 |
|
| 553 | static struct kmem_cache *ssam_event_item_cache;
|
| 554 |
|
| 555 | |
| 556 | * ssam_event_item_cache_init() - Initialize the event item cache.
|
| 557 |
|
| 558 | int ssam_event_item_cache_init(void)
|
| 559 | {
|
| 560 | const unsigned int size = sizeof(struct ssam_event_item)
|
| 561 | + SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN;
|
| 562 | const unsigned int align = __alignof__(struct ssam_event_item);
|
| 563 | struct kmem_cache *cache;
|
| 564 |
|
| 565 | cache = kmem_cache_create("ssam_event_item", size, align, 0, NULL);
|
| 566 | if (!cache)
|
| 567 | return -ENOMEM;
|
| 568 |
|
| 569 | ssam_event_item_cache = cache;
|
| 570 | return 0;
|
| 571 | }
|
| 572 |
|
| 573 | |
| 574 | * ssam_event_item_cache_destroy() - Deinitialize the event item cache.
|
| 575 |
|
| 576 | void ssam_event_item_cache_destroy(void)
|
| 577 | {
|
| 578 | kmem_cache_destroy(ssam_event_item_cache);
|
| 579 | ssam_event_item_cache = NULL;
|
| 580 | }
|
| 581 |
|
| 582 | static void __ssam_event_item_free_cached(struct ssam_event_item *item)
|
| 583 | {
|
| 584 | kmem_cache_free(ssam_event_item_cache, item);
|
| 585 | }
|
| 586 |
|
| 587 | static void __ssam_event_item_free_generic(struct ssam_event_item *item)
|
| 588 | {
|
| 589 | kfree(item);
|
| 590 | }
|
| 591 |
|
| 592 | |
| 593 | * ssam_event_item_free() - Free the provided event item.
|
| 594 | * @item: The event item to free.
|
| 595 |
|
| 596 | static void ssam_event_item_free(struct ssam_event_item *item)
|
| 597 | {
|
| 598 | trace_ssam_event_item_free(item);
|
| 599 | item->ops.free(item);
|
| 600 | }
|
| 601 |
|
| 602 | |
| 603 | * ssam_event_item_alloc() - Allocate an event item with the given payload size.
|
| 604 | * @len: The event payload length.
|
| 605 | * @flags: The flags used for allocation.
|
| 606 | *
|
| 607 | * Allocate an event item with the given payload size, preferring allocation
|
| 608 | * from the event item cache if the payload is small enough (i.e. smaller than
|
| 609 | * %SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN). Sets the item operations and payload
|
| 610 | * length values. The item free callback (``ops.free``) should not be
|
| 611 | * overwritten after this call.
|
| 612 | *
|
| 613 | * Return: Returns the newly allocated event item.
|
| 614 |
|
| 615 | static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
|
| 616 | {
|
| 617 | struct ssam_event_item *item;
|
| 618 |
|
| 619 | if (len <= SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN) {
|
| 620 | item = kmem_cache_alloc(ssam_event_item_cache, flags);
|
| 621 | if (!item)
|
| 622 | return NULL;
|
| 623 |
|
| 624 | item->ops.free = __ssam_event_item_free_cached;
|
| 625 | } else {
|
| 626 | item = kzalloc_flex(*item, event.data, len, flags);
|
| 627 | if (!item)
|
| 628 | return NULL;
|
| 629 |
|
| 630 | item->ops.free = __ssam_event_item_free_generic;
|
| 631 | }
|
| 632 |
|
| 633 | item->event.length = len;
|
| 634 |
|
| 635 | trace_ssam_event_item_alloc(item, len);
|
| 636 | return item;
|
| 637 | }
|
| 638 |
|
| 639 | |
| 640 | * ssam_event_queue_push() - Push an event item to the event queue.
|
| 641 | * @q: The event queue.
|
| 642 | * @item: The item to add.
|
| 643 |
|
| 644 | static void ssam_event_queue_push(struct ssam_event_queue *q,
|
| 645 | struct ssam_event_item *item)
|
| 646 | {
|
| 647 | spin_lock(&q->lock);
|
| 648 | list_add_tail(&item->node, &q->head);
|
| 649 | spin_unlock(&q->lock);
|
| 650 | }
|
| 651 |
|
| 652 | |
| 653 | * ssam_event_queue_pop() - Pop the next event item from the event queue.
|
| 654 | * @q: The event queue.
|
| 655 | *
|
| 656 | * Returns and removes the next event item from the queue. Returns %NULL If
|
| 657 | * there is no event item left.
|
| 658 |
|
| 659 | static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
|
| 660 | {
|
| 661 | struct ssam_event_item *item;
|
| 662 |
|
| 663 | spin_lock(&q->lock);
|
| 664 | item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
|
| 665 | if (item)
|
| 666 | list_del(&item->node);
|
| 667 | spin_unlock(&q->lock);
|
| 668 |
|
| 669 | return item;
|
| 670 | }
|
| 671 |
|
| 672 | |
| 673 | * ssam_event_queue_is_empty() - Check if the event queue is empty.
|
| 674 | * @q: The event queue.
|
| 675 |
|
| 676 | static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
|
| 677 | {
|
| 678 | bool empty;
|
| 679 |
|
| 680 | spin_lock(&q->lock);
|
| 681 | empty = list_empty(&q->head);
|
| 682 | spin_unlock(&q->lock);
|
| 683 |
|
| 684 | return empty;
|
| 685 | }
|
| 686 |
|
| 687 | |
| 688 | * ssam_cplt_get_event_queue() - Get the event queue for the given parameters.
|
| 689 | * @cplt: The completion system on which to look for the queue.
|
| 690 | * @tid: The target ID of the queue.
|
| 691 | * @rqid: The request ID representing the event ID for which to get the queue.
|
| 692 | *
|
| 693 | * Return: Returns the event queue corresponding to the event type described
|
| 694 | * by the given parameters. If the request ID does not represent an event,
|
| 695 | * this function returns %NULL. If the target ID is not supported, this
|
| 696 | * function will fall back to the default target ID (``tid = 1``).
|
| 697 |
|
| 698 | static
|
| 699 | struct ssam_event_queue *ssam_cplt_get_event_queue(struct ssam_cplt *cplt,
|
| 700 | u8 tid, u16 rqid)
|
| 701 | {
|
| 702 | u16 event = ssh_rqid_to_event(rqid);
|
| 703 | u16 tidx = ssh_tid_to_index(tid);
|
| 704 |
|
| 705 | if (!ssh_rqid_is_event(rqid)) {
|
| 706 | dev_err(cplt->dev, "event: unsupported request ID: %#06x\n", rqid);
|
| 707 | return NULL;
|
| 708 | }
|
| 709 |
|
| 710 | if (!ssh_tid_is_valid(tid)) {
|
| 711 | dev_warn(cplt->dev, "event: unsupported target ID: %u\n", tid);
|
| 712 | tidx = 0;
|
| 713 | }
|
| 714 |
|
| 715 | return &cplt->event.target[tidx].queue[event];
|
| 716 | }
|
| 717 |
|
| 718 | |
| 719 | * ssam_cplt_submit() - Submit a work item to the completion system workqueue.
|
| 720 | * @cplt: The completion system.
|
| 721 | * @work: The work item to submit.
|
| 722 |
|
| 723 | static bool ssam_cplt_submit(struct ssam_cplt *cplt, struct work_struct *work)
|
| 724 | {
|
| 725 | return queue_work(cplt->wq, work);
|
| 726 | }
|
| 727 |
|
| 728 | |
| 729 | * ssam_cplt_submit_event() - Submit an event to the completion system.
|
| 730 | * @cplt: The completion system.
|
| 731 | * @item: The event item to submit.
|
| 732 | *
|
| 733 | * Submits the event to the completion system by queuing it on the event item
|
| 734 | * queue and queuing the respective event queue work item on the completion
|
| 735 | * workqueue, which will eventually complete the event.
|
| 736 | *
|
| 737 | * Return: Returns zero on success, %-EINVAL if there is no event queue that
|
| 738 | * can handle the given event item.
|
| 739 |
|
| 740 | static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
|
| 741 | struct ssam_event_item *item)
|
| 742 | {
|
| 743 | struct ssam_event_queue *evq;
|
| 744 |
|
| 745 | evq = ssam_cplt_get_event_queue(cplt, item->event.target_id, item->rqid);
|
| 746 | if (!evq)
|
| 747 | return -EINVAL;
|
| 748 |
|
| 749 | ssam_event_queue_push(evq, item);
|
| 750 | ssam_cplt_submit(cplt, &evq->work);
|
| 751 | return 0;
|
| 752 | }
|
| 753 |
|
| 754 | |
| 755 | * ssam_cplt_flush() - Flush the completion system.
|
| 756 | * @cplt: The completion system.
|
| 757 | *
|
| 758 | * Flush the completion system by waiting until all currently submitted work
|
| 759 | * items have been completed.
|
| 760 | *
|
| 761 | * Note: This function does not guarantee that all events will have been
|
| 762 | * handled once this call terminates. In case of a larger number of
|
| 763 | * to-be-completed events, the event queue work function may re-schedule its
|
| 764 | * work item, which this flush operation will ignore.
|
| 765 | *
|
| 766 | * This operation is only intended to, during normal operation prior to
|
| 767 | * shutdown, try to complete most events and requests to get them out of the
|
| 768 | * system while the system is still fully operational. It does not aim to
|
| 769 | * provide any guarantee that all of them have been handled.
|
| 770 |
|
| 771 | static void ssam_cplt_flush(struct ssam_cplt *cplt)
|
| 772 | {
|
| 773 | flush_workqueue(cplt->wq);
|
| 774 | }
|
| 775 |
|
| 776 | static void ssam_event_queue_work_fn(struct work_struct *work)
|
| 777 | {
|
| 778 | struct ssam_event_queue *queue;
|
| 779 | struct ssam_event_item *item;
|
| 780 | struct ssam_nf *nf;
|
| 781 | struct device *dev;
|
| 782 | unsigned int iterations = SSAM_CPLT_WQ_BATCH;
|
| 783 |
|
| 784 | queue = container_of(work, struct ssam_event_queue, work);
|
| 785 | nf = &queue->cplt->event.notif;
|
| 786 | dev = queue->cplt->dev;
|
| 787 |
|
| 788 |
|
| 789 | do {
|
| 790 | item = ssam_event_queue_pop(queue);
|
| 791 | if (!item)
|
| 792 | return;
|
| 793 |
|
| 794 | ssam_nf_call(nf, dev, item->rqid, &item->event);
|
| 795 | ssam_event_item_free(item);
|
| 796 | } while (--iterations);
|
| 797 |
|
| 798 | if (!ssam_event_queue_is_empty(queue))
|
| 799 | ssam_cplt_submit(queue->cplt, &queue->work);
|
| 800 | }
|
| 801 |
|
| 802 | |
| 803 | * ssam_event_queue_init() - Initialize an event queue.
|
| 804 | * @cplt: The completion system on which the queue resides.
|
| 805 | * @evq: The event queue to initialize.
|
| 806 |
|
| 807 | static void ssam_event_queue_init(struct ssam_cplt *cplt,
|
| 808 | struct ssam_event_queue *evq)
|
| 809 | {
|
| 810 | evq->cplt = cplt;
|
| 811 | spin_lock_init(&evq->lock);
|
| 812 | INIT_LIST_HEAD(&evq->head);
|
| 813 | INIT_WORK(&evq->work, ssam_event_queue_work_fn);
|
| 814 | }
|
| 815 |
|
| 816 | |
| 817 | * ssam_cplt_init() - Initialize completion system.
|
| 818 | * @cplt: The completion system to initialize.
|
| 819 | * @dev: The device used for logging.
|
| 820 |
|
| 821 | static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
|
| 822 | {
|
| 823 | struct ssam_event_target *target;
|
| 824 | int status, c, i;
|
| 825 |
|
| 826 | cplt->dev = dev;
|
| 827 |
|
| 828 | cplt->wq = alloc_workqueue(SSAM_CPLT_WQ_NAME, WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
|
| 829 | if (!cplt->wq)
|
| 830 | return -ENOMEM;
|
| 831 |
|
| 832 | for (c = 0; c < ARRAY_SIZE(cplt->event.target); c++) {
|
| 833 | target = &cplt->event.target[c];
|
| 834 |
|
| 835 | for (i = 0; i < ARRAY_SIZE(target->queue); i++)
|
| 836 | ssam_event_queue_init(cplt, &target->queue[i]);
|
| 837 | }
|
| 838 |
|
| 839 | status = ssam_nf_init(&cplt->event.notif);
|
| 840 | if (status)
|
| 841 | destroy_workqueue(cplt->wq);
|
| 842 |
|
| 843 | return status;
|
| 844 | }
|
| 845 |
|
| 846 | |
| 847 | * ssam_cplt_destroy() - Deinitialize the completion system.
|
| 848 | * @cplt: The completion system to deinitialize.
|
| 849 | *
|
| 850 | * Deinitialize the given completion system and ensure that all pending, i.e.
|
| 851 | * yet-to-be-completed, event items and requests have been handled.
|
| 852 |
|
| 853 | static void ssam_cplt_destroy(struct ssam_cplt *cplt)
|
| 854 | {
|
| 855 | |
| 856 | * Note: destroy_workqueue ensures that all currently queued work will
|
| 857 | * be fully completed and the workqueue drained. This means that this
|
| 858 | * call will inherently also free any queued ssam_event_items, thus we
|
| 859 | * don't have to take care of that here explicitly.
|
| 860 |
|
| 861 | destroy_workqueue(cplt->wq);
|
| 862 | ssam_nf_destroy(&cplt->event.notif);
|
| 863 | }
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 | |
| 869 | * ssam_controller_device() - Get the &struct device associated with this
|
| 870 | * controller.
|
| 871 | * @c: The controller for which to get the device.
|
| 872 | *
|
| 873 | * Return: Returns the &struct device associated with this controller,
|
| 874 | * providing its lower-level transport.
|
| 875 |
|
| 876 | struct device *ssam_controller_device(struct ssam_controller *c)
|
| 877 | {
|
| 878 | return ssh_rtl_get_device(&c->rtl);
|
| 879 | }
|
| 880 | EXPORT_SYMBOL_GPL(ssam_controller_device);
|
| 881 |
|
| 882 | static void __ssam_controller_release(struct kref *kref)
|
| 883 | {
|
| 884 | struct ssam_controller *ctrl = to_ssam_controller(kref, kref);
|
| 885 |
|
| 886 | |
| 887 | * The lock-call here is to satisfy lockdep. At this point we really
|
| 888 | * expect this to be the last remaining reference to the controller.
|
| 889 | * Anything else is a bug.
|
| 890 |
|
| 891 | ssam_controller_lock(ctrl);
|
| 892 | ssam_controller_destroy(ctrl);
|
| 893 | ssam_controller_unlock(ctrl);
|
| 894 |
|
| 895 | kfree(ctrl);
|
| 896 | }
|
| 897 |
|
| 898 | |
| 899 | * ssam_controller_get() - Increment reference count of controller.
|
| 900 | * @c: The controller.
|
| 901 | *
|
| 902 | * Return: Returns the controller provided as input.
|
| 903 |
|
| 904 | struct ssam_controller *ssam_controller_get(struct ssam_controller *c)
|
| 905 | {
|
| 906 | if (c)
|
| 907 | kref_get(&c->kref);
|
| 908 | return c;
|
| 909 | }
|
| 910 | EXPORT_SYMBOL_GPL(ssam_controller_get);
|
| 911 |
|
| 912 | |
| 913 | * ssam_controller_put() - Decrement reference count of controller.
|
| 914 | * @c: The controller.
|
| 915 |
|
| 916 | void ssam_controller_put(struct ssam_controller *c)
|
| 917 | {
|
| 918 | if (c)
|
| 919 | kref_put(&c->kref, __ssam_controller_release);
|
| 920 | }
|
| 921 | EXPORT_SYMBOL_GPL(ssam_controller_put);
|
| 922 |
|
| 923 | |
| 924 | * ssam_controller_statelock() - Lock the controller against state transitions.
|
| 925 | * @c: The controller to lock.
|
| 926 | *
|
| 927 | * Lock the controller against state transitions. Holding this lock guarantees
|
| 928 | * that the controller will not transition between states, i.e. if the
|
| 929 | * controller is in state "started", when this lock has been acquired, it will
|
| 930 | * remain in this state at least until the lock has been released.
|
| 931 | *
|
| 932 | * Multiple clients may concurrently hold this lock. In other words: The
|
| 933 | * ``statelock`` functions represent the read-lock part of a r/w-semaphore.
|
| 934 | * Actions causing state transitions of the controller must be executed while
|
| 935 | * holding the write-part of this r/w-semaphore (see ssam_controller_lock()
|
| 936 | * and ssam_controller_unlock() for that).
|
| 937 | *
|
| 938 | * See ssam_controller_stateunlock() for the corresponding unlock function.
|
| 939 |
|
| 940 | void ssam_controller_statelock(struct ssam_controller *c)
|
| 941 | {
|
| 942 | down_read(&c->lock);
|
| 943 | }
|
| 944 | EXPORT_SYMBOL_GPL(ssam_controller_statelock);
|
| 945 |
|
| 946 | |
| 947 | * ssam_controller_stateunlock() - Unlock controller state transitions.
|
| 948 | * @c: The controller to unlock.
|
| 949 | *
|
| 950 | * See ssam_controller_statelock() for the corresponding lock function.
|
| 951 |
|
| 952 | void ssam_controller_stateunlock(struct ssam_controller *c)
|
| 953 | {
|
| 954 | up_read(&c->lock);
|
| 955 | }
|
| 956 | EXPORT_SYMBOL_GPL(ssam_controller_stateunlock);
|
| 957 |
|
| 958 | |
| 959 | * ssam_controller_lock() - Acquire the main controller lock.
|
| 960 | * @c: The controller to lock.
|
| 961 | *
|
| 962 | * This lock must be held for any state transitions, including transition to
|
| 963 | * suspend/resumed states and during shutdown. See ssam_controller_statelock()
|
| 964 | * for more details on controller locking.
|
| 965 | *
|
| 966 | * See ssam_controller_unlock() for the corresponding unlock function.
|
| 967 |
|
| 968 | void ssam_controller_lock(struct ssam_controller *c)
|
| 969 | {
|
| 970 | down_write(&c->lock);
|
| 971 | }
|
| 972 |
|
| 973 | |
| 974 | * ssam_controller_unlock() - Release the main controller lock.
|
| 975 | * @c: The controller to unlock.
|
| 976 | *
|
| 977 | * See ssam_controller_lock() for the corresponding lock function.
|
| 978 |
|
| 979 | void ssam_controller_unlock(struct ssam_controller *c)
|
| 980 | {
|
| 981 | up_write(&c->lock);
|
| 982 | }
|
| 983 |
|
| 984 | static void ssam_handle_event(struct ssh_rtl *rtl,
|
| 985 | const struct ssh_command *cmd,
|
| 986 | const struct ssam_span *data)
|
| 987 | {
|
| 988 | struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
|
| 989 | struct ssam_event_item *item;
|
| 990 |
|
| 991 | item = ssam_event_item_alloc(data->len, GFP_KERNEL);
|
| 992 | if (!item)
|
| 993 | return;
|
| 994 |
|
| 995 | item->rqid = get_unaligned_le16(&cmd->rqid);
|
| 996 | item->event.target_category = cmd->tc;
|
| 997 | item->event.target_id = cmd->sid;
|
| 998 | item->event.command_id = cmd->cid;
|
| 999 | item->event.instance_id = cmd->iid;
|
| 1000 | memcpy(&item->event.data[0], data->ptr, data->len);
|
| 1001 |
|
| 1002 | if (WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item)))
|
| 1003 | ssam_event_item_free(item);
|
| 1004 | }
|
| 1005 |
|
| 1006 | static const struct ssh_rtl_ops ssam_rtl_ops = {
|
| 1007 | .handle_event = ssam_handle_event,
|
| 1008 | };
|
| 1009 |
|
| 1010 | static bool ssam_notifier_is_empty(struct ssam_controller *ctrl);
|
| 1011 | static void ssam_notifier_unregister_all(struct ssam_controller *ctrl);
|
| 1012 |
|
| 1013 | #define SSAM_SSH_DSM_REVISION 0
|
| 1014 |
|
| 1015 |
|
| 1016 | static const guid_t SSAM_SSH_DSM_GUID =
|
| 1017 | GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
|
| 1018 | 0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
|
| 1019 |
|
| 1020 | enum ssh_dsm_fn {
|
| 1021 | SSH_DSM_FN_SSH_POWER_PROFILE = 0x05,
|
| 1022 | SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT = 0x06,
|
| 1023 | SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT = 0x07,
|
| 1024 | SSH_DSM_FN_D3_CLOSES_HANDLE = 0x08,
|
| 1025 | SSH_DSM_FN_SSH_BUFFER_SIZE = 0x09,
|
| 1026 | };
|
| 1027 |
|
| 1028 | static int ssam_dsm_get_functions(acpi_handle handle, u64 *funcs)
|
| 1029 | {
|
| 1030 | union acpi_object *obj;
|
| 1031 | u64 mask = 0;
|
| 1032 | int i;
|
| 1033 |
|
| 1034 | *funcs = 0;
|
| 1035 |
|
| 1036 | |
| 1037 | * The _DSM function is only present on newer models. It is not
|
| 1038 | * present on 5th and 6th generation devices (i.e. up to and including
|
| 1039 | * Surface Pro 6, Surface Laptop 2, Surface Book 2).
|
| 1040 | *
|
| 1041 | * If the _DSM is not present, indicate that no function is supported.
|
| 1042 | * This will result in default values being set.
|
| 1043 |
|
| 1044 | if (!acpi_has_method(handle, "_DSM"))
|
| 1045 | return 0;
|
| 1046 |
|
| 1047 | obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
|
| 1048 | SSAM_SSH_DSM_REVISION, 0, NULL,
|
| 1049 | ACPI_TYPE_BUFFER);
|
| 1050 | if (!obj)
|
| 1051 | return -EIO;
|
| 1052 |
|
| 1053 | for (i = 0; i < obj->buffer.length && i < 8; i++)
|
| 1054 | mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
|
| 1055 |
|
| 1056 | if (mask & BIT(0))
|
| 1057 | *funcs = mask;
|
| 1058 |
|
| 1059 | ACPI_FREE(obj);
|
| 1060 | return 0;
|
| 1061 | }
|
| 1062 |
|
| 1063 | static int ssam_dsm_load_u32(acpi_handle handle, u64 funcs, u64 func, u32 *ret)
|
| 1064 | {
|
| 1065 | union acpi_object *obj;
|
| 1066 | u64 val;
|
| 1067 |
|
| 1068 | if (!(funcs & BIT_ULL(func)))
|
| 1069 | return 0;
|
| 1070 |
|
| 1071 | obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
|
| 1072 | SSAM_SSH_DSM_REVISION, func, NULL,
|
| 1073 | ACPI_TYPE_INTEGER);
|
| 1074 | if (!obj)
|
| 1075 | return -EIO;
|
| 1076 |
|
| 1077 | val = obj->integer.value;
|
| 1078 | ACPI_FREE(obj);
|
| 1079 |
|
| 1080 | if (val > U32_MAX)
|
| 1081 | return -ERANGE;
|
| 1082 |
|
| 1083 | *ret = val;
|
| 1084 | return 0;
|
| 1085 | }
|
| 1086 |
|
| 1087 | |
| 1088 | * ssam_controller_caps_load_from_acpi() - Load controller capabilities from
|
| 1089 | * ACPI _DSM.
|
| 1090 | * @handle: The handle of the ACPI controller/SSH device.
|
| 1091 | * @caps: Where to store the capabilities in.
|
| 1092 | *
|
| 1093 | * Initializes the given controller capabilities with default values, then
|
| 1094 | * checks and, if the respective _DSM functions are available, loads the
|
| 1095 | * actual capabilities from the _DSM.
|
| 1096 | *
|
| 1097 | * Return: Returns zero on success, a negative error code on failure.
|
| 1098 |
|
| 1099 | static
|
| 1100 | int ssam_controller_caps_load_from_acpi(acpi_handle handle,
|
| 1101 | struct ssam_controller_caps *caps)
|
| 1102 | {
|
| 1103 | u32 d3_closes_handle = false;
|
| 1104 | u64 funcs;
|
| 1105 | int status;
|
| 1106 |
|
| 1107 |
|
| 1108 | status = ssam_dsm_get_functions(handle, &funcs);
|
| 1109 | if (status)
|
| 1110 | return status;
|
| 1111 |
|
| 1112 |
|
| 1113 | status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_POWER_PROFILE,
|
| 1114 | &caps->ssh_power_profile);
|
| 1115 | if (status)
|
| 1116 | return status;
|
| 1117 |
|
| 1118 | status = ssam_dsm_load_u32(handle, funcs,
|
| 1119 | SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT,
|
| 1120 | &caps->screen_on_sleep_idle_timeout);
|
| 1121 | if (status)
|
| 1122 | return status;
|
| 1123 |
|
| 1124 | status = ssam_dsm_load_u32(handle, funcs,
|
| 1125 | SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT,
|
| 1126 | &caps->screen_off_sleep_idle_timeout);
|
| 1127 | if (status)
|
| 1128 | return status;
|
| 1129 |
|
| 1130 | status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_D3_CLOSES_HANDLE,
|
| 1131 | &d3_closes_handle);
|
| 1132 | if (status)
|
| 1133 | return status;
|
| 1134 |
|
| 1135 | caps->d3_closes_handle = !!d3_closes_handle;
|
| 1136 |
|
| 1137 | status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_BUFFER_SIZE,
|
| 1138 | &caps->ssh_buffer_size);
|
| 1139 | if (status)
|
| 1140 | return status;
|
| 1141 |
|
| 1142 | return 0;
|
| 1143 | }
|
| 1144 |
|
| 1145 | |
| 1146 | * ssam_controller_caps_load_from_of() - Load controller capabilities from OF/DT.
|
| 1147 | * @dev: A pointer to the controller device
|
| 1148 | * @caps: Where to store the capabilities in.
|
| 1149 | *
|
| 1150 | * Return: Returns zero on success, a negative error code on failure.
|
| 1151 |
|
| 1152 | static int ssam_controller_caps_load_from_of(struct device *dev, struct ssam_controller_caps *caps)
|
| 1153 | {
|
| 1154 | |
| 1155 | * Every device starting with Surface Pro X through Laptop 7 uses these
|
| 1156 | * identical values, which makes them good defaults.
|
| 1157 |
|
| 1158 | caps->d3_closes_handle = true;
|
| 1159 | caps->screen_on_sleep_idle_timeout = 5000;
|
| 1160 | caps->screen_off_sleep_idle_timeout = 30;
|
| 1161 | caps->ssh_buffer_size = 48;
|
| 1162 |
|
| 1163 |
|
| 1164 | return 0;
|
| 1165 | }
|
| 1166 |
|
| 1167 | |
| 1168 | * ssam_controller_caps_load() - Load controller capabilities
|
| 1169 | * @dev: A pointer to the controller device
|
| 1170 | * @caps: Where to store the capabilities in.
|
| 1171 | *
|
| 1172 | * Return: Returns zero on success, a negative error code on failure.
|
| 1173 |
|
| 1174 | static int ssam_controller_caps_load(struct device *dev, struct ssam_controller_caps *caps)
|
| 1175 | {
|
| 1176 | acpi_handle handle = ACPI_HANDLE(dev);
|
| 1177 |
|
| 1178 |
|
| 1179 | caps->ssh_power_profile = U32_MAX;
|
| 1180 | caps->screen_on_sleep_idle_timeout = U32_MAX;
|
| 1181 | caps->screen_off_sleep_idle_timeout = U32_MAX;
|
| 1182 | caps->d3_closes_handle = false;
|
| 1183 | caps->ssh_buffer_size = U32_MAX;
|
| 1184 |
|
| 1185 | if (handle)
|
| 1186 | return ssam_controller_caps_load_from_acpi(handle, caps);
|
| 1187 | else
|
| 1188 | return ssam_controller_caps_load_from_of(dev, caps);
|
| 1189 | }
|
| 1190 |
|
| 1191 | |
| 1192 | * ssam_controller_init() - Initialize SSAM controller.
|
| 1193 | * @ctrl: The controller to initialize.
|
| 1194 | * @serdev: The serial device representing the underlying data transport.
|
| 1195 | *
|
| 1196 | * Initializes the given controller. Does neither start receiver nor
|
| 1197 | * transmitter threads. After this call, the controller has to be hooked up to
|
| 1198 | * the serdev core separately via &struct serdev_device_ops, relaying calls to
|
| 1199 | * ssam_controller_receive_buf() and ssam_controller_write_wakeup(). Once the
|
| 1200 | * controller has been hooked up, transmitter and receiver threads may be
|
| 1201 | * started via ssam_controller_start(). These setup steps need to be completed
|
| 1202 | * before controller can be used for requests.
|
| 1203 |
|
| 1204 | int ssam_controller_init(struct ssam_controller *ctrl,
|
| 1205 | struct serdev_device *serdev)
|
| 1206 | {
|
| 1207 | int status;
|
| 1208 |
|
| 1209 | init_rwsem(&ctrl->lock);
|
| 1210 | kref_init(&ctrl->kref);
|
| 1211 |
|
| 1212 | status = ssam_controller_caps_load(&serdev->dev, &ctrl->caps);
|
| 1213 | if (status)
|
| 1214 | return status;
|
| 1215 |
|
| 1216 | dev_dbg(&serdev->dev,
|
| 1217 | "device capabilities:\n"
|
| 1218 | " ssh_power_profile: %u\n"
|
| 1219 | " ssh_buffer_size: %u\n"
|
| 1220 | " screen_on_sleep_idle_timeout: %u\n"
|
| 1221 | " screen_off_sleep_idle_timeout: %u\n"
|
| 1222 | " d3_closes_handle: %u\n",
|
| 1223 | ctrl->caps.ssh_power_profile,
|
| 1224 | ctrl->caps.ssh_buffer_size,
|
| 1225 | ctrl->caps.screen_on_sleep_idle_timeout,
|
| 1226 | ctrl->caps.screen_off_sleep_idle_timeout,
|
| 1227 | ctrl->caps.d3_closes_handle);
|
| 1228 |
|
| 1229 | ssh_seq_reset(&ctrl->counter.seq);
|
| 1230 | ssh_rqid_reset(&ctrl->counter.rqid);
|
| 1231 |
|
| 1232 |
|
| 1233 | status = ssam_cplt_init(&ctrl->cplt, &serdev->dev);
|
| 1234 | if (status)
|
| 1235 | return status;
|
| 1236 |
|
| 1237 |
|
| 1238 | status = ssh_rtl_init(&ctrl->rtl, serdev, &ssam_rtl_ops);
|
| 1239 | if (status) {
|
| 1240 | ssam_cplt_destroy(&ctrl->cplt);
|
| 1241 | return status;
|
| 1242 | }
|
| 1243 |
|
| 1244 | |
| 1245 | * Set state via write_once even though we expect to be in an
|
| 1246 | * exclusive context, due to smoke-testing in
|
| 1247 | * ssam_request_sync_submit().
|
| 1248 |
|
| 1249 | WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_INITIALIZED);
|
| 1250 | return 0;
|
| 1251 | }
|
| 1252 |
|
| 1253 | |
| 1254 | * ssam_controller_start() - Start the receiver and transmitter threads of the
|
| 1255 | * controller.
|
| 1256 | * @ctrl: The controller.
|
| 1257 | *
|
| 1258 | * Note: When this function is called, the controller should be properly
|
| 1259 | * hooked up to the serdev core via &struct serdev_device_ops. Please refer
|
| 1260 | * to ssam_controller_init() for more details on controller initialization.
|
| 1261 | *
|
| 1262 | * This function must be called with the main controller lock held (i.e. by
|
| 1263 | * calling ssam_controller_lock()).
|
| 1264 |
|
| 1265 | int ssam_controller_start(struct ssam_controller *ctrl)
|
| 1266 | {
|
| 1267 | int status;
|
| 1268 |
|
| 1269 | lockdep_assert_held_write(&ctrl->lock);
|
| 1270 |
|
| 1271 | if (ctrl->state != SSAM_CONTROLLER_INITIALIZED)
|
| 1272 | return -EINVAL;
|
| 1273 |
|
| 1274 | status = ssh_rtl_start(&ctrl->rtl);
|
| 1275 | if (status)
|
| 1276 | return status;
|
| 1277 |
|
| 1278 | |
| 1279 | * Set state via write_once even though we expect to be locked/in an
|
| 1280 | * exclusive context, due to smoke-testing in
|
| 1281 | * ssam_request_sync_submit().
|
| 1282 |
|
| 1283 | WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
|
| 1284 | return 0;
|
| 1285 | }
|
| 1286 |
|
| 1287 | |
| 1288 | * SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT - Timeout for flushing requests during
|
| 1289 | * shutdown.
|
| 1290 | *
|
| 1291 | * Chosen to be larger than one full request timeout, including packets timing
|
| 1292 | * out. This value should give ample time to complete any outstanding requests
|
| 1293 | * during normal operation and account for the odd package timeout.
|
| 1294 |
|
| 1295 | #define SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT msecs_to_jiffies(5000)
|
| 1296 |
|
| 1297 | |
| 1298 | * ssam_controller_shutdown() - Shut down the controller.
|
| 1299 | * @ctrl: The controller.
|
| 1300 | *
|
| 1301 | * Shuts down the controller by flushing all pending requests and stopping the
|
| 1302 | * transmitter and receiver threads. All requests submitted after this call
|
| 1303 | * will fail with %-ESHUTDOWN. While it is discouraged to do so, this function
|
| 1304 | * is safe to use in parallel with ongoing request submission.
|
| 1305 | *
|
| 1306 | * In the course of this shutdown procedure, all currently registered
|
| 1307 | * notifiers will be unregistered. It is, however, strongly recommended to not
|
| 1308 | * rely on this behavior, and instead the party registering the notifier
|
| 1309 | * should unregister it before the controller gets shut down, e.g. via the
|
| 1310 | * SSAM bus which guarantees client devices to be removed before a shutdown.
|
| 1311 | *
|
| 1312 | * Note that events may still be pending after this call, but, due to the
|
| 1313 | * notifiers being unregistered, these events will be dropped when the
|
| 1314 | * controller is subsequently destroyed via ssam_controller_destroy().
|
| 1315 | *
|
| 1316 | * This function must be called with the main controller lock held (i.e. by
|
| 1317 | * calling ssam_controller_lock()).
|
| 1318 |
|
| 1319 | void ssam_controller_shutdown(struct ssam_controller *ctrl)
|
| 1320 | {
|
| 1321 | enum ssam_controller_state s = ctrl->state;
|
| 1322 | int status;
|
| 1323 |
|
| 1324 | lockdep_assert_held_write(&ctrl->lock);
|
| 1325 |
|
| 1326 | if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
|
| 1327 | return;
|
| 1328 |
|
| 1329 | |
| 1330 | * Try to flush pending events and requests while everything still
|
| 1331 | * works. Note: There may still be packets and/or requests in the
|
| 1332 | * system after this call (e.g. via control packets submitted by the
|
| 1333 | * packet transport layer or flush timeout / failure, ...). Those will
|
| 1334 | * be handled with the ssh_rtl_shutdown() call below.
|
| 1335 |
|
| 1336 | status = ssh_rtl_flush(&ctrl->rtl, SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT);
|
| 1337 | if (status) {
|
| 1338 | ssam_err(ctrl, "failed to flush request transport layer: %d\n",
|
| 1339 | status);
|
| 1340 | }
|
| 1341 |
|
| 1342 |
|
| 1343 | ssam_cplt_flush(&ctrl->cplt);
|
| 1344 |
|
| 1345 | |
| 1346 | * We expect all notifiers to have been removed by the respective client
|
| 1347 | * driver that set them up at this point. If this warning occurs, some
|
| 1348 | * client driver has not done that...
|
| 1349 |
|
| 1350 | WARN_ON(!ssam_notifier_is_empty(ctrl));
|
| 1351 |
|
| 1352 | |
| 1353 | * Nevertheless, we should still take care of drivers that don't behave
|
| 1354 | * well. Thus disable all enabled events, unregister all notifiers.
|
| 1355 |
|
| 1356 | ssam_notifier_unregister_all(ctrl);
|
| 1357 |
|
| 1358 | |
| 1359 | * Cancel remaining requests. Ensure no new ones can be queued and stop
|
| 1360 | * threads.
|
| 1361 |
|
| 1362 | ssh_rtl_shutdown(&ctrl->rtl);
|
| 1363 |
|
| 1364 | |
| 1365 | * Set state via write_once even though we expect to be locked/in an
|
| 1366 | * exclusive context, due to smoke-testing in
|
| 1367 | * ssam_request_sync_submit().
|
| 1368 |
|
| 1369 | WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STOPPED);
|
| 1370 | ctrl->rtl.ptl.serdev = NULL;
|
| 1371 | }
|
| 1372 |
|
| 1373 | |
| 1374 | * ssam_controller_destroy() - Destroy the controller and free its resources.
|
| 1375 | * @ctrl: The controller.
|
| 1376 | *
|
| 1377 | * Ensures that all resources associated with the controller get freed. This
|
| 1378 | * function should only be called after the controller has been stopped via
|
| 1379 | * ssam_controller_shutdown(). In general, this function should not be called
|
| 1380 | * directly. The only valid place to call this function directly is during
|
| 1381 | * initialization, before the controller has been fully initialized and passed
|
| 1382 | * to other processes. This function is called automatically when the
|
| 1383 | * reference count of the controller reaches zero.
|
| 1384 | *
|
| 1385 | * This function must be called with the main controller lock held (i.e. by
|
| 1386 | * calling ssam_controller_lock()).
|
| 1387 |
|
| 1388 | void ssam_controller_destroy(struct ssam_controller *ctrl)
|
| 1389 | {
|
| 1390 | lockdep_assert_held_write(&ctrl->lock);
|
| 1391 |
|
| 1392 | if (ctrl->state == SSAM_CONTROLLER_UNINITIALIZED)
|
| 1393 | return;
|
| 1394 |
|
| 1395 | WARN_ON(ctrl->state != SSAM_CONTROLLER_STOPPED &&
|
| 1396 | ctrl->state != SSAM_CONTROLLER_INITIALIZED);
|
| 1397 |
|
| 1398 | |
| 1399 | * Note: New events could still have been received after the previous
|
| 1400 | * flush in ssam_controller_shutdown, before the request transport layer
|
| 1401 | * has been shut down. At this point, after the shutdown, we can be sure
|
| 1402 | * that no new events will be queued. The call to ssam_cplt_destroy will
|
| 1403 | * ensure that those remaining are being completed and freed.
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 | ssam_cplt_destroy(&ctrl->cplt);
|
| 1408 | ssh_rtl_destroy(&ctrl->rtl);
|
| 1409 |
|
| 1410 | |
| 1411 | * Set state via write_once even though we expect to be locked/in an
|
| 1412 | * exclusive context, due to smoke-testing in
|
| 1413 | * ssam_request_sync_submit().
|
| 1414 |
|
| 1415 | WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_UNINITIALIZED);
|
| 1416 | }
|
| 1417 |
|
| 1418 | |
| 1419 | * ssam_controller_suspend() - Suspend the controller.
|
| 1420 | * @ctrl: The controller to suspend.
|
| 1421 | *
|
| 1422 | * Marks the controller as suspended. Note that display-off and D0-exit
|
| 1423 | * notifications have to be sent manually before transitioning the controller
|
| 1424 | * into the suspended state via this function.
|
| 1425 | *
|
| 1426 | * See ssam_controller_resume() for the corresponding resume function.
|
| 1427 | *
|
| 1428 | * Return: Returns %-EINVAL if the controller is currently not in the
|
| 1429 | * "started" state.
|
| 1430 |
|
| 1431 | int ssam_controller_suspend(struct ssam_controller *ctrl)
|
| 1432 | {
|
| 1433 | ssam_controller_lock(ctrl);
|
| 1434 |
|
| 1435 | if (ctrl->state != SSAM_CONTROLLER_STARTED) {
|
| 1436 | ssam_controller_unlock(ctrl);
|
| 1437 | return -EINVAL;
|
| 1438 | }
|
| 1439 |
|
| 1440 | ssam_dbg(ctrl, "pm: suspending controller\n");
|
| 1441 |
|
| 1442 | |
| 1443 | * Set state via write_once even though we're locked, due to
|
| 1444 | * smoke-testing in ssam_request_sync_submit().
|
| 1445 |
|
| 1446 | WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_SUSPENDED);
|
| 1447 |
|
| 1448 | ssam_controller_unlock(ctrl);
|
| 1449 | return 0;
|
| 1450 | }
|
| 1451 |
|
| 1452 | |
| 1453 | * ssam_controller_resume() - Resume the controller from suspend.
|
| 1454 | * @ctrl: The controller to resume.
|
| 1455 | *
|
| 1456 | * Resume the controller from the suspended state it was put into via
|
| 1457 | * ssam_controller_suspend(). This function does not issue display-on and
|
| 1458 | * D0-entry notifications. If required, those have to be sent manually after
|
| 1459 | * this call.
|
| 1460 | *
|
| 1461 | * Return: Returns %-EINVAL if the controller is currently not suspended.
|
| 1462 |
|
| 1463 | int ssam_controller_resume(struct ssam_controller *ctrl)
|
| 1464 | {
|
| 1465 | ssam_controller_lock(ctrl);
|
| 1466 |
|
| 1467 | if (ctrl->state != SSAM_CONTROLLER_SUSPENDED) {
|
| 1468 | ssam_controller_unlock(ctrl);
|
| 1469 | return -EINVAL;
|
| 1470 | }
|
| 1471 |
|
| 1472 | ssam_dbg(ctrl, "pm: resuming controller\n");
|
| 1473 |
|
| 1474 | |
| 1475 | * Set state via write_once even though we're locked, due to
|
| 1476 | * smoke-testing in ssam_request_sync_submit().
|
| 1477 |
|
| 1478 | WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
|
| 1479 |
|
| 1480 | ssam_controller_unlock(ctrl);
|
| 1481 | return 0;
|
| 1482 | }
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 | |
| 1488 | * ssam_request_write_data() - Construct and write SAM request message to
|
| 1489 | * buffer.
|
| 1490 | * @buf: The buffer to write the data to.
|
| 1491 | * @ctrl: The controller via which the request will be sent.
|
| 1492 | * @spec: The request data and specification.
|
| 1493 | *
|
| 1494 | * Constructs a SAM/SSH request message and writes it to the provided buffer.
|
| 1495 | * The request and transport counters, specifically RQID and SEQ, will be set
|
| 1496 | * in this call. These counters are obtained from the controller. It is thus
|
| 1497 | * only valid to send the resulting message via the controller specified here.
|
| 1498 | *
|
| 1499 | * For calculation of the required buffer size, refer to the
|
| 1500 | * SSH_COMMAND_MESSAGE_LENGTH() macro.
|
| 1501 | *
|
| 1502 | * Return: Returns the number of bytes used in the buffer on success. Returns
|
| 1503 | * %-EINVAL if the payload length provided in the request specification is too
|
| 1504 | * large (larger than %SSH_COMMAND_MAX_PAYLOAD_SIZE) or if the provided buffer
|
| 1505 | * is too small.
|
| 1506 |
|
| 1507 | ssize_t ssam_request_write_data(struct ssam_span *buf,
|
| 1508 | struct ssam_controller *ctrl,
|
| 1509 | const struct ssam_request *spec)
|
| 1510 | {
|
| 1511 | struct msgbuf msgb;
|
| 1512 | u16 rqid;
|
| 1513 | u8 seq;
|
| 1514 |
|
| 1515 | if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE)
|
| 1516 | return -EINVAL;
|
| 1517 |
|
| 1518 | if (SSH_COMMAND_MESSAGE_LENGTH(spec->length) > buf->len)
|
| 1519 | return -EINVAL;
|
| 1520 |
|
| 1521 | msgb_init(&msgb, buf->ptr, buf->len);
|
| 1522 | seq = ssh_seq_next(&ctrl->counter.seq);
|
| 1523 | rqid = ssh_rqid_next(&ctrl->counter.rqid);
|
| 1524 | msgb_push_cmd(&msgb, seq, rqid, spec);
|
| 1525 |
|
| 1526 | return msgb_bytes_used(&msgb);
|
| 1527 | }
|
| 1528 | EXPORT_SYMBOL_GPL(ssam_request_write_data);
|
| 1529 |
|
| 1530 | static void ssam_request_sync_complete(struct ssh_request *rqst,
|
| 1531 | const struct ssh_command *cmd,
|
| 1532 | const struct ssam_span *data, int status)
|
| 1533 | {
|
| 1534 | struct ssh_rtl *rtl = ssh_request_rtl(rqst);
|
| 1535 | struct ssam_request_sync *r;
|
| 1536 |
|
| 1537 | r = container_of(rqst, struct ssam_request_sync, base);
|
| 1538 | r->status = status;
|
| 1539 |
|
| 1540 | if (r->resp)
|
| 1541 | r->resp->length = 0;
|
| 1542 |
|
| 1543 | if (status) {
|
| 1544 | rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
|
| 1545 | return;
|
| 1546 | }
|
| 1547 |
|
| 1548 | if (!data)
|
| 1549 | return;
|
| 1550 |
|
| 1551 | if (!r->resp || !r->resp->pointer) {
|
| 1552 | if (data->len)
|
| 1553 | rtl_warn(rtl, "rsp: no response buffer provided, dropping data\n");
|
| 1554 | return;
|
| 1555 | }
|
| 1556 |
|
| 1557 | if (data->len > r->resp->capacity) {
|
| 1558 | rtl_err(rtl,
|
| 1559 | "rsp: response buffer too small, capacity: %zu bytes, got: %zu bytes\n",
|
| 1560 | r->resp->capacity, data->len);
|
| 1561 | r->status = -ENOSPC;
|
| 1562 | return;
|
| 1563 | }
|
| 1564 |
|
| 1565 | r->resp->length = data->len;
|
| 1566 | memcpy(r->resp->pointer, data->ptr, data->len);
|
| 1567 | }
|
| 1568 |
|
| 1569 | static void ssam_request_sync_release(struct ssh_request *rqst)
|
| 1570 | {
|
| 1571 | complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
|
| 1572 | }
|
| 1573 |
|
| 1574 | static const struct ssh_request_ops ssam_request_sync_ops = {
|
| 1575 | .release = ssam_request_sync_release,
|
| 1576 | .complete = ssam_request_sync_complete,
|
| 1577 | };
|
| 1578 |
|
| 1579 | |
| 1580 | * ssam_request_sync_alloc() - Allocate a synchronous request.
|
| 1581 | * @payload_len: The length of the request payload.
|
| 1582 | * @flags: Flags used for allocation.
|
| 1583 | * @rqst: Where to store the pointer to the allocated request.
|
| 1584 | * @buffer: Where to store the buffer descriptor for the message buffer of
|
| 1585 | * the request.
|
| 1586 | *
|
| 1587 | * Allocates a synchronous request with corresponding message buffer. The
|
| 1588 | * request still needs to be initialized ssam_request_sync_init() before
|
| 1589 | * it can be submitted, and the message buffer data must still be set to the
|
| 1590 | * returned buffer via ssam_request_sync_set_data() after it has been filled,
|
| 1591 | * if need be with adjusted message length.
|
| 1592 | *
|
| 1593 | * After use, the request and its corresponding message buffer should be freed
|
| 1594 | * via ssam_request_sync_free(). The buffer must not be freed separately.
|
| 1595 | *
|
| 1596 | * Return: Returns zero on success, %-ENOMEM if the request could not be
|
| 1597 | * allocated.
|
| 1598 |
|
| 1599 | int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
|
| 1600 | struct ssam_request_sync **rqst,
|
| 1601 | struct ssam_span *buffer)
|
| 1602 | {
|
| 1603 | size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
|
| 1604 |
|
| 1605 | *rqst = kzalloc(sizeof(**rqst) + msglen, flags);
|
| 1606 | if (!*rqst)
|
| 1607 | return -ENOMEM;
|
| 1608 |
|
| 1609 | buffer->ptr = (u8 *)(*rqst + 1);
|
| 1610 | buffer->len = msglen;
|
| 1611 |
|
| 1612 | return 0;
|
| 1613 | }
|
| 1614 | EXPORT_SYMBOL_GPL(ssam_request_sync_alloc);
|
| 1615 |
|
| 1616 | |
| 1617 | * ssam_request_sync_free() - Free a synchronous request.
|
| 1618 | * @rqst: The request to be freed.
|
| 1619 | *
|
| 1620 | * Free a synchronous request and its corresponding buffer allocated with
|
| 1621 | * ssam_request_sync_alloc(). Do not use for requests allocated on the stack
|
| 1622 | * or via any other function.
|
| 1623 | *
|
| 1624 | * Warning: The caller must ensure that the request is not in use any more.
|
| 1625 | * I.e. the caller must ensure that it has the only reference to the request
|
| 1626 | * and the request is not currently pending. This means that the caller has
|
| 1627 | * either never submitted the request, request submission has failed, or the
|
| 1628 | * caller has waited until the submitted request has been completed via
|
| 1629 | * ssam_request_sync_wait().
|
| 1630 |
|
| 1631 | void ssam_request_sync_free(struct ssam_request_sync *rqst)
|
| 1632 | {
|
| 1633 | kfree(rqst);
|
| 1634 | }
|
| 1635 | EXPORT_SYMBOL_GPL(ssam_request_sync_free);
|
| 1636 |
|
| 1637 | |
| 1638 | * ssam_request_sync_init() - Initialize a synchronous request struct.
|
| 1639 | * @rqst: The request to initialize.
|
| 1640 | * @flags: The request flags.
|
| 1641 | *
|
| 1642 | * Initializes the given request struct. Does not initialize the request
|
| 1643 | * message data. This has to be done explicitly after this call via
|
| 1644 | * ssam_request_sync_set_data() and the actual message data has to be written
|
| 1645 | * via ssam_request_write_data().
|
| 1646 | *
|
| 1647 | * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
|
| 1648 |
|
| 1649 | int ssam_request_sync_init(struct ssam_request_sync *rqst,
|
| 1650 | enum ssam_request_flags flags)
|
| 1651 | {
|
| 1652 | int status;
|
| 1653 |
|
| 1654 | status = ssh_request_init(&rqst->base, flags, &ssam_request_sync_ops);
|
| 1655 | if (status)
|
| 1656 | return status;
|
| 1657 |
|
| 1658 | init_completion(&rqst->comp);
|
| 1659 | rqst->resp = NULL;
|
| 1660 | rqst->status = 0;
|
| 1661 |
|
| 1662 | return 0;
|
| 1663 | }
|
| 1664 | EXPORT_SYMBOL_GPL(ssam_request_sync_init);
|
| 1665 |
|
| 1666 | |
| 1667 | * ssam_request_sync_submit() - Submit a synchronous request.
|
| 1668 | * @ctrl: The controller with which to submit the request.
|
| 1669 | * @rqst: The request to submit.
|
| 1670 | *
|
| 1671 | * Submit a synchronous request. The request has to be initialized and
|
| 1672 | * properly set up, including response buffer (may be %NULL if no response is
|
| 1673 | * expected) and command message data. This function does not wait for the
|
| 1674 | * request to be completed.
|
| 1675 | *
|
| 1676 | * If this function succeeds, ssam_request_sync_wait() must be used to ensure
|
| 1677 | * that the request has been completed before the response data can be
|
| 1678 | * accessed and/or the request can be freed. On failure, the request may
|
| 1679 | * immediately be freed.
|
| 1680 | *
|
| 1681 | * This function may only be used if the controller is active, i.e. has been
|
| 1682 | * initialized and not suspended.
|
| 1683 |
|
| 1684 | int ssam_request_sync_submit(struct ssam_controller *ctrl,
|
| 1685 | struct ssam_request_sync *rqst)
|
| 1686 | {
|
| 1687 | int status;
|
| 1688 |
|
| 1689 | |
| 1690 | * This is only a superficial check. In general, the caller needs to
|
| 1691 | * ensure that the controller is initialized and is not (and does not
|
| 1692 | * get) suspended during use, i.e. until the request has been completed
|
| 1693 | * (if _absolutely_ necessary, by use of ssam_controller_statelock/
|
| 1694 | * ssam_controller_stateunlock, but something like ssam_client_link
|
| 1695 | * should be preferred as this needs to last until the request has been
|
| 1696 | * completed).
|
| 1697 | *
|
| 1698 | * Note that it is actually safe to use this function while the
|
| 1699 | * controller is in the process of being shut down (as ssh_rtl_submit
|
| 1700 | * is safe with regards to this), but it is generally discouraged to do
|
| 1701 | * so.
|
| 1702 |
|
| 1703 | if (WARN_ON(READ_ONCE(ctrl->state) != SSAM_CONTROLLER_STARTED)) {
|
| 1704 | ssh_request_put(&rqst->base);
|
| 1705 | return -ENODEV;
|
| 1706 | }
|
| 1707 |
|
| 1708 | status = ssh_rtl_submit(&ctrl->rtl, &rqst->base);
|
| 1709 | ssh_request_put(&rqst->base);
|
| 1710 |
|
| 1711 | return status;
|
| 1712 | }
|
| 1713 | EXPORT_SYMBOL_GPL(ssam_request_sync_submit);
|
| 1714 |
|
| 1715 | |
| 1716 | * ssam_request_do_sync() - Execute a synchronous request.
|
| 1717 | * @ctrl: The controller via which the request will be submitted.
|
| 1718 | * @spec: The request specification and payload.
|
| 1719 | * @rsp: The response buffer.
|
| 1720 | *
|
| 1721 | * Allocates a synchronous request with its message data buffer on the heap
|
| 1722 | * via ssam_request_sync_alloc(), fully initializes it via the provided
|
| 1723 | * request specification, submits it, and finally waits for its completion
|
| 1724 | * before freeing it and returning its status.
|
| 1725 | *
|
| 1726 | * Return: Returns the status of the request or any failure during setup.
|
| 1727 |
|
| 1728 | int ssam_request_do_sync(struct ssam_controller *ctrl,
|
| 1729 | const struct ssam_request *spec,
|
| 1730 | struct ssam_response *rsp)
|
| 1731 | {
|
| 1732 | struct ssam_request_sync *rqst;
|
| 1733 | struct ssam_span buf;
|
| 1734 | ssize_t len;
|
| 1735 | int status;
|
| 1736 |
|
| 1737 | status = ssam_request_sync_alloc(spec->length, GFP_KERNEL, &rqst, &buf);
|
| 1738 | if (status)
|
| 1739 | return status;
|
| 1740 |
|
| 1741 | status = ssam_request_sync_init(rqst, spec->flags);
|
| 1742 | if (status) {
|
| 1743 | ssam_request_sync_free(rqst);
|
| 1744 | return status;
|
| 1745 | }
|
| 1746 |
|
| 1747 | ssam_request_sync_set_resp(rqst, rsp);
|
| 1748 |
|
| 1749 | len = ssam_request_write_data(&buf, ctrl, spec);
|
| 1750 | if (len < 0) {
|
| 1751 | ssam_request_sync_free(rqst);
|
| 1752 | return len;
|
| 1753 | }
|
| 1754 |
|
| 1755 | ssam_request_sync_set_data(rqst, buf.ptr, len);
|
| 1756 |
|
| 1757 | status = ssam_request_sync_submit(ctrl, rqst);
|
| 1758 | if (!status)
|
| 1759 | status = ssam_request_sync_wait(rqst);
|
| 1760 |
|
| 1761 | ssam_request_sync_free(rqst);
|
| 1762 | return status;
|
| 1763 | }
|
| 1764 | EXPORT_SYMBOL_GPL(ssam_request_do_sync);
|
| 1765 |
|
| 1766 | |
| 1767 | * ssam_request_do_sync_with_buffer() - Execute a synchronous request with the
|
| 1768 | * provided buffer as back-end for the message buffer.
|
| 1769 | * @ctrl: The controller via which the request will be submitted.
|
| 1770 | * @spec: The request specification and payload.
|
| 1771 | * @rsp: The response buffer.
|
| 1772 | * @buf: The buffer for the request message data.
|
| 1773 | *
|
| 1774 | * Allocates a synchronous request struct on the stack, fully initializes it
|
| 1775 | * using the provided buffer as message data buffer, submits it, and then
|
| 1776 | * waits for its completion before returning its status. The
|
| 1777 | * SSH_COMMAND_MESSAGE_LENGTH() macro can be used to compute the required
|
| 1778 | * message buffer size.
|
| 1779 | *
|
| 1780 | * This function does essentially the same as ssam_request_do_sync(), but
|
| 1781 | * instead of dynamically allocating the request and message data buffer, it
|
| 1782 | * uses the provided message data buffer and stores the (small) request struct
|
| 1783 | * on the heap.
|
| 1784 | *
|
| 1785 | * Return: Returns the status of the request or any failure during setup.
|
| 1786 |
|
| 1787 | int ssam_request_do_sync_with_buffer(struct ssam_controller *ctrl,
|
| 1788 | const struct ssam_request *spec,
|
| 1789 | struct ssam_response *rsp,
|
| 1790 | struct ssam_span *buf)
|
| 1791 | {
|
| 1792 | struct ssam_request_sync rqst;
|
| 1793 | ssize_t len;
|
| 1794 | int status;
|
| 1795 |
|
| 1796 | status = ssam_request_sync_init(&rqst, spec->flags);
|
| 1797 | if (status)
|
| 1798 | return status;
|
| 1799 |
|
| 1800 | ssam_request_sync_set_resp(&rqst, rsp);
|
| 1801 |
|
| 1802 | len = ssam_request_write_data(buf, ctrl, spec);
|
| 1803 | if (len < 0)
|
| 1804 | return len;
|
| 1805 |
|
| 1806 | ssam_request_sync_set_data(&rqst, buf->ptr, len);
|
| 1807 |
|
| 1808 | status = ssam_request_sync_submit(ctrl, &rqst);
|
| 1809 | if (!status)
|
| 1810 | status = ssam_request_sync_wait(&rqst);
|
| 1811 |
|
| 1812 | return status;
|
| 1813 | }
|
| 1814 | EXPORT_SYMBOL_GPL(ssam_request_do_sync_with_buffer);
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 | SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
|
| 1820 | .target_category = SSAM_SSH_TC_SAM,
|
| 1821 | .target_id = SSAM_SSH_TID_SAM,
|
| 1822 | .command_id = 0x13,
|
| 1823 | .instance_id = 0x00,
|
| 1824 | });
|
| 1825 |
|
| 1826 | SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
|
| 1827 | .target_category = SSAM_SSH_TC_SAM,
|
| 1828 | .target_id = SSAM_SSH_TID_SAM,
|
| 1829 | .command_id = 0x15,
|
| 1830 | .instance_id = 0x00,
|
| 1831 | });
|
| 1832 |
|
| 1833 | SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
|
| 1834 | .target_category = SSAM_SSH_TC_SAM,
|
| 1835 | .target_id = SSAM_SSH_TID_SAM,
|
| 1836 | .command_id = 0x16,
|
| 1837 | .instance_id = 0x00,
|
| 1838 | });
|
| 1839 |
|
| 1840 | SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
|
| 1841 | .target_category = SSAM_SSH_TC_SAM,
|
| 1842 | .target_id = SSAM_SSH_TID_SAM,
|
| 1843 | .command_id = 0x33,
|
| 1844 | .instance_id = 0x00,
|
| 1845 | });
|
| 1846 |
|
| 1847 | SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
|
| 1848 | .target_category = SSAM_SSH_TC_SAM,
|
| 1849 | .target_id = SSAM_SSH_TID_SAM,
|
| 1850 | .command_id = 0x34,
|
| 1851 | .instance_id = 0x00,
|
| 1852 | });
|
| 1853 |
|
| 1854 | |
| 1855 | * struct ssh_notification_params - Command payload to enable/disable SSH
|
| 1856 | * notifications.
|
| 1857 | * @target_category: The target category for which notifications should be
|
| 1858 | * enabled/disabled.
|
| 1859 | * @flags: Flags determining how notifications are being sent.
|
| 1860 | * @request_id: The request ID that is used to send these notifications.
|
| 1861 | * @instance_id: The specific instance in the given target category for
|
| 1862 | * which notifications should be enabled.
|
| 1863 |
|
| 1864 | struct ssh_notification_params {
|
| 1865 | u8 target_category;
|
| 1866 | u8 flags;
|
| 1867 | __le16 request_id;
|
| 1868 | u8 instance_id;
|
| 1869 | } __packed;
|
| 1870 |
|
| 1871 | static_assert(sizeof(struct ssh_notification_params) == 5);
|
| 1872 |
|
| 1873 | static int __ssam_ssh_event_request(struct ssam_controller *ctrl,
|
| 1874 | struct ssam_event_registry reg, u8 cid,
|
| 1875 | struct ssam_event_id id, u8 flags)
|
| 1876 | {
|
| 1877 | struct ssh_notification_params params;
|
| 1878 | struct ssam_request rqst;
|
| 1879 | struct ssam_response result;
|
| 1880 | int status;
|
| 1881 |
|
| 1882 | u16 rqid = ssh_tc_to_rqid(id.target_category);
|
| 1883 | u8 buf = 0;
|
| 1884 |
|
| 1885 |
|
| 1886 | if (!ssh_rqid_is_event(rqid))
|
| 1887 | return -EINVAL;
|
| 1888 |
|
| 1889 | params.target_category = id.target_category;
|
| 1890 | params.instance_id = id.instance;
|
| 1891 | params.flags = flags;
|
| 1892 | put_unaligned_le16(rqid, ¶ms.request_id);
|
| 1893 |
|
| 1894 | rqst.target_category = reg.target_category;
|
| 1895 | rqst.target_id = reg.target_id;
|
| 1896 | rqst.command_id = cid;
|
| 1897 | rqst.instance_id = 0x00;
|
| 1898 | rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
|
| 1899 | rqst.length = sizeof(params);
|
| 1900 | rqst.payload = (u8 *)¶ms;
|
| 1901 |
|
| 1902 | result.capacity = sizeof(buf);
|
| 1903 | result.length = 0;
|
| 1904 | result.pointer = &buf;
|
| 1905 |
|
| 1906 | status = ssam_retry(ssam_request_do_sync_onstack, ctrl, &rqst, &result,
|
| 1907 | sizeof(params));
|
| 1908 |
|
| 1909 | return status < 0 ? status : buf;
|
| 1910 | }
|
| 1911 |
|
| 1912 | |
| 1913 | * ssam_ssh_event_enable() - Enable SSH event.
|
| 1914 | * @ctrl: The controller for which to enable the event.
|
| 1915 | * @reg: The event registry describing what request to use for enabling and
|
| 1916 | * disabling the event.
|
| 1917 | * @id: The event identifier.
|
| 1918 | * @flags: The event flags.
|
| 1919 | *
|
| 1920 | * Enables the specified event on the EC. This function does not manage
|
| 1921 | * reference counting of enabled events and is basically only a wrapper for
|
| 1922 | * the raw EC request. If the specified event is already enabled, the EC will
|
| 1923 | * ignore this request.
|
| 1924 | *
|
| 1925 | * Return: Returns the status of the executed SAM request (zero on success and
|
| 1926 | * negative on direct failure) or %-EPROTO if the request response indicates a
|
| 1927 | * failure.
|
| 1928 |
|
| 1929 | static int ssam_ssh_event_enable(struct ssam_controller *ctrl,
|
| 1930 | struct ssam_event_registry reg,
|
| 1931 | struct ssam_event_id id, u8 flags)
|
| 1932 | {
|
| 1933 | int status;
|
| 1934 |
|
| 1935 | status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
|
| 1936 |
|
| 1937 | if (status < 0 && status != -EINVAL) {
|
| 1938 | ssam_err(ctrl,
|
| 1939 | "failed to enable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
|
| 1940 | id.target_category, id.instance, reg.target_category);
|
| 1941 | }
|
| 1942 |
|
| 1943 | if (status > 0) {
|
| 1944 | ssam_err(ctrl,
|
| 1945 | "unexpected result while enabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
|
| 1946 | status, id.target_category, id.instance, reg.target_category);
|
| 1947 | return -EPROTO;
|
| 1948 | }
|
| 1949 |
|
| 1950 | return status;
|
| 1951 | }
|
| 1952 |
|
| 1953 | |
| 1954 | * ssam_ssh_event_disable() - Disable SSH event.
|
| 1955 | * @ctrl: The controller for which to disable the event.
|
| 1956 | * @reg: The event registry describing what request to use for enabling and
|
| 1957 | * disabling the event (must be same as used when enabling the event).
|
| 1958 | * @id: The event identifier.
|
| 1959 | * @flags: The event flags (likely ignored for disabling of events).
|
| 1960 | *
|
| 1961 | * Disables the specified event on the EC. This function does not manage
|
| 1962 | * reference counting of enabled events and is basically only a wrapper for
|
| 1963 | * the raw EC request. If the specified event is already disabled, the EC will
|
| 1964 | * ignore this request.
|
| 1965 | *
|
| 1966 | * Return: Returns the status of the executed SAM request (zero on success and
|
| 1967 | * negative on direct failure) or %-EPROTO if the request response indicates a
|
| 1968 | * failure.
|
| 1969 |
|
| 1970 | static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
|
| 1971 | struct ssam_event_registry reg,
|
| 1972 | struct ssam_event_id id, u8 flags)
|
| 1973 | {
|
| 1974 | int status;
|
| 1975 |
|
| 1976 | status = __ssam_ssh_event_request(ctrl, reg, reg.cid_disable, id, flags);
|
| 1977 |
|
| 1978 | if (status < 0 && status != -EINVAL) {
|
| 1979 | ssam_err(ctrl,
|
| 1980 | "failed to disable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
|
| 1981 | id.target_category, id.instance, reg.target_category);
|
| 1982 | }
|
| 1983 |
|
| 1984 | if (status > 0) {
|
| 1985 | ssam_err(ctrl,
|
| 1986 | "unexpected result while disabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
|
| 1987 | status, id.target_category, id.instance, reg.target_category);
|
| 1988 | return -EPROTO;
|
| 1989 | }
|
| 1990 |
|
| 1991 | return status;
|
| 1992 | }
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 | |
| 1998 | * ssam_get_firmware_version() - Get the SAM/EC firmware version.
|
| 1999 | * @ctrl: The controller.
|
| 2000 | * @version: Where to store the version number.
|
| 2001 | *
|
| 2002 | * Return: Returns zero on success or the status of the executed SAM request
|
| 2003 | * if that request failed.
|
| 2004 |
|
| 2005 | int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version)
|
| 2006 | {
|
| 2007 | __le32 __version;
|
| 2008 | int status;
|
| 2009 |
|
| 2010 | status = ssam_retry(ssam_ssh_get_firmware_version, ctrl, &__version);
|
| 2011 | if (status)
|
| 2012 | return status;
|
| 2013 |
|
| 2014 | *version = le32_to_cpu(__version);
|
| 2015 | return 0;
|
| 2016 | }
|
| 2017 |
|
| 2018 | |
| 2019 | * ssam_ctrl_notif_display_off() - Notify EC that the display has been turned
|
| 2020 | * off.
|
| 2021 | * @ctrl: The controller.
|
| 2022 | *
|
| 2023 | * Notify the EC that the display has been turned off and the driver may enter
|
| 2024 | * a lower-power state. This will prevent events from being sent directly.
|
| 2025 | * Rather, the EC signals an event by pulling the wakeup GPIO high for as long
|
| 2026 | * as there are pending events. The events then need to be manually released,
|
| 2027 | * one by one, via the GPIO callback request. All pending events accumulated
|
| 2028 | * during this state can also be released by issuing the display-on
|
| 2029 | * notification, e.g. via ssam_ctrl_notif_display_on(), which will also reset
|
| 2030 | * the GPIO.
|
| 2031 | *
|
| 2032 | * On some devices, specifically ones with an integrated keyboard, the keyboard
|
| 2033 | * backlight will be turned off by this call.
|
| 2034 | *
|
| 2035 | * This function will only send the display-off notification command if
|
| 2036 | * display notifications are supported by the EC. Currently all known devices
|
| 2037 | * support these notifications.
|
| 2038 | *
|
| 2039 | * Use ssam_ctrl_notif_display_on() to reverse the effects of this function.
|
| 2040 | *
|
| 2041 | * Return: Returns zero on success or if no request has been executed, the
|
| 2042 | * status of the executed SAM request if that request failed, or %-EPROTO if
|
| 2043 | * an unexpected response has been received.
|
| 2044 |
|
| 2045 | int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl)
|
| 2046 | {
|
| 2047 | int status;
|
| 2048 | u8 response;
|
| 2049 |
|
| 2050 | ssam_dbg(ctrl, "pm: notifying display off\n");
|
| 2051 |
|
| 2052 | status = ssam_retry(ssam_ssh_notif_display_off, ctrl, &response);
|
| 2053 | if (status)
|
| 2054 | return status;
|
| 2055 |
|
| 2056 | if (response != 0) {
|
| 2057 | ssam_err(ctrl, "unexpected response from display-off notification: %#04x\n",
|
| 2058 | response);
|
| 2059 | return -EPROTO;
|
| 2060 | }
|
| 2061 |
|
| 2062 | return 0;
|
| 2063 | }
|
| 2064 |
|
| 2065 | |
| 2066 | * ssam_ctrl_notif_display_on() - Notify EC that the display has been turned on.
|
| 2067 | * @ctrl: The controller.
|
| 2068 | *
|
| 2069 | * Notify the EC that the display has been turned back on and the driver has
|
| 2070 | * exited its lower-power state. This notification is the counterpart to the
|
| 2071 | * display-off notification sent via ssam_ctrl_notif_display_off() and will
|
| 2072 | * reverse its effects, including resetting events to their default behavior.
|
| 2073 | *
|
| 2074 | * This function will only send the display-on notification command if display
|
| 2075 | * notifications are supported by the EC. Currently all known devices support
|
| 2076 | * these notifications.
|
| 2077 | *
|
| 2078 | * See ssam_ctrl_notif_display_off() for more details.
|
| 2079 | *
|
| 2080 | * Return: Returns zero on success or if no request has been executed, the
|
| 2081 | * status of the executed SAM request if that request failed, or %-EPROTO if
|
| 2082 | * an unexpected response has been received.
|
| 2083 |
|
| 2084 | int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl)
|
| 2085 | {
|
| 2086 | int status;
|
| 2087 | u8 response;
|
| 2088 |
|
| 2089 | ssam_dbg(ctrl, "pm: notifying display on\n");
|
| 2090 |
|
| 2091 | status = ssam_retry(ssam_ssh_notif_display_on, ctrl, &response);
|
| 2092 | if (status)
|
| 2093 | return status;
|
| 2094 |
|
| 2095 | if (response != 0) {
|
| 2096 | ssam_err(ctrl, "unexpected response from display-on notification: %#04x\n",
|
| 2097 | response);
|
| 2098 | return -EPROTO;
|
| 2099 | }
|
| 2100 |
|
| 2101 | return 0;
|
| 2102 | }
|
| 2103 |
|
| 2104 | |
| 2105 | * ssam_ctrl_notif_d0_exit() - Notify EC that the driver/device exits the D0
|
| 2106 | * power state.
|
| 2107 | * @ctrl: The controller
|
| 2108 | *
|
| 2109 | * Notifies the EC that the driver prepares to exit the D0 power state in
|
| 2110 | * favor of a lower-power state. Exact effects of this function related to the
|
| 2111 | * EC are currently unknown.
|
| 2112 | *
|
| 2113 | * This function will only send the D0-exit notification command if D0-state
|
| 2114 | * notifications are supported by the EC. Only newer Surface generations
|
| 2115 | * support these notifications.
|
| 2116 | *
|
| 2117 | * Use ssam_ctrl_notif_d0_entry() to reverse the effects of this function.
|
| 2118 | *
|
| 2119 | * Return: Returns zero on success or if no request has been executed, the
|
| 2120 | * status of the executed SAM request if that request failed, or %-EPROTO if
|
| 2121 | * an unexpected response has been received.
|
| 2122 |
|
| 2123 | int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl)
|
| 2124 | {
|
| 2125 | int status;
|
| 2126 | u8 response;
|
| 2127 |
|
| 2128 | if (!ctrl->caps.d3_closes_handle)
|
| 2129 | return 0;
|
| 2130 |
|
| 2131 | ssam_dbg(ctrl, "pm: notifying D0 exit\n");
|
| 2132 |
|
| 2133 | status = ssam_retry(ssam_ssh_notif_d0_exit, ctrl, &response);
|
| 2134 | if (status)
|
| 2135 | return status;
|
| 2136 |
|
| 2137 | if (response != 0) {
|
| 2138 | ssam_err(ctrl, "unexpected response from D0-exit notification: %#04x\n",
|
| 2139 | response);
|
| 2140 | return -EPROTO;
|
| 2141 | }
|
| 2142 |
|
| 2143 | return 0;
|
| 2144 | }
|
| 2145 |
|
| 2146 | |
| 2147 | * ssam_ctrl_notif_d0_entry() - Notify EC that the driver/device enters the D0
|
| 2148 | * power state.
|
| 2149 | * @ctrl: The controller
|
| 2150 | *
|
| 2151 | * Notifies the EC that the driver has exited a lower-power state and entered
|
| 2152 | * the D0 power state. Exact effects of this function related to the EC are
|
| 2153 | * currently unknown.
|
| 2154 | *
|
| 2155 | * This function will only send the D0-entry notification command if D0-state
|
| 2156 | * notifications are supported by the EC. Only newer Surface generations
|
| 2157 | * support these notifications.
|
| 2158 | *
|
| 2159 | * See ssam_ctrl_notif_d0_exit() for more details.
|
| 2160 | *
|
| 2161 | * Return: Returns zero on success or if no request has been executed, the
|
| 2162 | * status of the executed SAM request if that request failed, or %-EPROTO if
|
| 2163 | * an unexpected response has been received.
|
| 2164 |
|
| 2165 | int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl)
|
| 2166 | {
|
| 2167 | int status;
|
| 2168 | u8 response;
|
| 2169 |
|
| 2170 | if (!ctrl->caps.d3_closes_handle)
|
| 2171 | return 0;
|
| 2172 |
|
| 2173 | ssam_dbg(ctrl, "pm: notifying D0 entry\n");
|
| 2174 |
|
| 2175 | status = ssam_retry(ssam_ssh_notif_d0_entry, ctrl, &response);
|
| 2176 | if (status)
|
| 2177 | return status;
|
| 2178 |
|
| 2179 | if (response != 0) {
|
| 2180 | ssam_err(ctrl, "unexpected response from D0-entry notification: %#04x\n",
|
| 2181 | response);
|
| 2182 | return -EPROTO;
|
| 2183 | }
|
| 2184 |
|
| 2185 | return 0;
|
| 2186 | }
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 | |
| 2192 | * ssam_nf_refcount_enable() - Enable event for reference count entry if it has
|
| 2193 | * not already been enabled.
|
| 2194 | * @ctrl: The controller to enable the event on.
|
| 2195 | * @entry: The reference count entry for the event to be enabled.
|
| 2196 | * @flags: The flags used for enabling the event on the EC.
|
| 2197 | *
|
| 2198 | * Enable the event associated with the given reference count entry if the
|
| 2199 | * reference count equals one, i.e. the event has not previously been enabled.
|
| 2200 | * If the event has already been enabled (i.e. reference count not equal to
|
| 2201 | * one), check that the flags used for enabling match and warn about this if
|
| 2202 | * they do not.
|
| 2203 | *
|
| 2204 | * This does not modify the reference count itself, which is done with
|
| 2205 | * ssam_nf_refcount_inc() / ssam_nf_refcount_dec().
|
| 2206 | *
|
| 2207 | * Note: ``nf->lock`` must be held when calling this function.
|
| 2208 | *
|
| 2209 | * Return: Returns zero on success. If the event is enabled by this call,
|
| 2210 | * returns the status of the event-enable EC command.
|
| 2211 |
|
| 2212 | static int ssam_nf_refcount_enable(struct ssam_controller *ctrl,
|
| 2213 | struct ssam_nf_refcount_entry *entry, u8 flags)
|
| 2214 | {
|
| 2215 | const struct ssam_event_registry reg = entry->key.reg;
|
| 2216 | const struct ssam_event_id id = entry->key.id;
|
| 2217 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2218 | int status;
|
| 2219 |
|
| 2220 | lockdep_assert_held(&nf->lock);
|
| 2221 |
|
| 2222 | ssam_dbg(ctrl, "enabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
|
| 2223 | reg.target_category, id.target_category, id.instance, entry->refcount);
|
| 2224 |
|
| 2225 | if (entry->refcount == 1) {
|
| 2226 | status = ssam_ssh_event_enable(ctrl, reg, id, flags);
|
| 2227 | if (status)
|
| 2228 | return status;
|
| 2229 |
|
| 2230 | entry->flags = flags;
|
| 2231 |
|
| 2232 | } else if (entry->flags != flags) {
|
| 2233 | ssam_warn(ctrl,
|
| 2234 | "inconsistent flags when enabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
|
| 2235 | flags, entry->flags, reg.target_category, id.target_category,
|
| 2236 | id.instance);
|
| 2237 | }
|
| 2238 |
|
| 2239 | return 0;
|
| 2240 | }
|
| 2241 |
|
| 2242 | |
| 2243 | * ssam_nf_refcount_disable_free() - Disable event for reference count entry if
|
| 2244 | * it is no longer in use and free the corresponding entry.
|
| 2245 | * @ctrl: The controller to disable the event on.
|
| 2246 | * @entry: The reference count entry for the event to be disabled.
|
| 2247 | * @flags: The flags used for enabling the event on the EC.
|
| 2248 | * @ec: Flag specifying if the event should actually be disabled on the EC.
|
| 2249 | *
|
| 2250 | * If ``ec`` equals ``true`` and the reference count equals zero (i.e. the
|
| 2251 | * event is no longer requested by any client), the specified event will be
|
| 2252 | * disabled on the EC via the corresponding request.
|
| 2253 | *
|
| 2254 | * If ``ec`` equals ``false``, no request will be sent to the EC and the event
|
| 2255 | * can be considered in a detached state (i.e. no longer used but still
|
| 2256 | * enabled). Disabling an event via this method may be required for
|
| 2257 | * hot-removable devices, where event disable requests may time out after the
|
| 2258 | * device has been physically removed.
|
| 2259 | *
|
| 2260 | * In both cases, if the reference count equals zero, the corresponding
|
| 2261 | * reference count entry will be freed. The reference count entry must not be
|
| 2262 | * used any more after a call to this function.
|
| 2263 | *
|
| 2264 | * Also checks if the flags used for disabling the event match the flags used
|
| 2265 | * for enabling the event and warns if they do not (regardless of reference
|
| 2266 | * count).
|
| 2267 | *
|
| 2268 | * This does not modify the reference count itself, which is done with
|
| 2269 | * ssam_nf_refcount_inc() / ssam_nf_refcount_dec().
|
| 2270 | *
|
| 2271 | * Note: ``nf->lock`` must be held when calling this function.
|
| 2272 | *
|
| 2273 | * Return: Returns zero on success. If the event is disabled by this call,
|
| 2274 | * returns the status of the event-enable EC command.
|
| 2275 |
|
| 2276 | static int ssam_nf_refcount_disable_free(struct ssam_controller *ctrl,
|
| 2277 | struct ssam_nf_refcount_entry *entry, u8 flags, bool ec)
|
| 2278 | {
|
| 2279 | const struct ssam_event_registry reg = entry->key.reg;
|
| 2280 | const struct ssam_event_id id = entry->key.id;
|
| 2281 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2282 | int status = 0;
|
| 2283 |
|
| 2284 | lockdep_assert_held(&nf->lock);
|
| 2285 |
|
| 2286 | ssam_dbg(ctrl, "%s event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
|
| 2287 | ec ? "disabling" : "detaching", reg.target_category, id.target_category,
|
| 2288 | id.instance, entry->refcount);
|
| 2289 |
|
| 2290 | if (entry->flags != flags) {
|
| 2291 | ssam_warn(ctrl,
|
| 2292 | "inconsistent flags when disabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
|
| 2293 | flags, entry->flags, reg.target_category, id.target_category,
|
| 2294 | id.instance);
|
| 2295 | }
|
| 2296 |
|
| 2297 | if (ec && entry->refcount == 0) {
|
| 2298 | status = ssam_ssh_event_disable(ctrl, reg, id, flags);
|
| 2299 | kfree(entry);
|
| 2300 | }
|
| 2301 |
|
| 2302 | return status;
|
| 2303 | }
|
| 2304 |
|
| 2305 | |
| 2306 | * ssam_notifier_register() - Register an event notifier.
|
| 2307 | * @ctrl: The controller to register the notifier on.
|
| 2308 | * @n: The event notifier to register.
|
| 2309 | *
|
| 2310 | * Register an event notifier. Increment the usage counter of the associated
|
| 2311 | * SAM event if the notifier is not marked as an observer. If the event is not
|
| 2312 | * marked as an observer and is currently not enabled, it will be enabled
|
| 2313 | * during this call. If the notifier is marked as an observer, no attempt will
|
| 2314 | * be made at enabling any event and no reference count will be modified.
|
| 2315 | *
|
| 2316 | * Notifiers marked as observers do not need to be associated with one specific
|
| 2317 | * event, i.e. as long as no event matching is performed, only the event target
|
| 2318 | * category needs to be set.
|
| 2319 | *
|
| 2320 | * Return: Returns zero on success, %-ENOSPC if there have already been
|
| 2321 | * %INT_MAX notifiers for the event ID/type associated with the notifier block
|
| 2322 | * registered, %-ENOMEM if the corresponding event entry could not be
|
| 2323 | * allocated. If this is the first time that a notifier block is registered
|
| 2324 | * for the specific associated event, returns the status of the event-enable
|
| 2325 | * EC-command.
|
| 2326 |
|
| 2327 | int ssam_notifier_register(struct ssam_controller *ctrl, struct ssam_event_notifier *n)
|
| 2328 | {
|
| 2329 | u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
|
| 2330 | struct ssam_nf_refcount_entry *entry = NULL;
|
| 2331 | struct ssam_nf_head *nf_head;
|
| 2332 | struct ssam_nf *nf;
|
| 2333 | int status;
|
| 2334 |
|
| 2335 | if (!ssh_rqid_is_event(rqid))
|
| 2336 | return -EINVAL;
|
| 2337 |
|
| 2338 | nf = &ctrl->cplt.event.notif;
|
| 2339 | nf_head = &nf->head[ssh_rqid_to_event(rqid)];
|
| 2340 |
|
| 2341 | mutex_lock(&nf->lock);
|
| 2342 |
|
| 2343 | if (!(n->flags & SSAM_EVENT_NOTIFIER_OBSERVER)) {
|
| 2344 | entry = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
|
| 2345 | if (IS_ERR(entry)) {
|
| 2346 | mutex_unlock(&nf->lock);
|
| 2347 | return PTR_ERR(entry);
|
| 2348 | }
|
| 2349 | }
|
| 2350 |
|
| 2351 | status = ssam_nfblk_insert(nf_head, &n->base);
|
| 2352 | if (status) {
|
| 2353 | if (entry)
|
| 2354 | ssam_nf_refcount_dec_free(nf, n->event.reg, n->event.id);
|
| 2355 |
|
| 2356 | mutex_unlock(&nf->lock);
|
| 2357 | return status;
|
| 2358 | }
|
| 2359 |
|
| 2360 | if (entry) {
|
| 2361 | status = ssam_nf_refcount_enable(ctrl, entry, n->event.flags);
|
| 2362 | if (status) {
|
| 2363 | ssam_nfblk_remove(&n->base);
|
| 2364 | ssam_nf_refcount_dec_free(nf, n->event.reg, n->event.id);
|
| 2365 | mutex_unlock(&nf->lock);
|
| 2366 | synchronize_srcu(&nf_head->srcu);
|
| 2367 | return status;
|
| 2368 | }
|
| 2369 | }
|
| 2370 |
|
| 2371 | mutex_unlock(&nf->lock);
|
| 2372 | return 0;
|
| 2373 | }
|
| 2374 | EXPORT_SYMBOL_GPL(ssam_notifier_register);
|
| 2375 |
|
| 2376 | |
| 2377 | * __ssam_notifier_unregister() - Unregister an event notifier.
|
| 2378 | * @ctrl: The controller the notifier has been registered on.
|
| 2379 | * @n: The event notifier to unregister.
|
| 2380 | * @disable: Whether to disable the corresponding event on the EC.
|
| 2381 | *
|
| 2382 | * Unregister an event notifier. Decrement the usage counter of the associated
|
| 2383 | * SAM event if the notifier is not marked as an observer. If the usage counter
|
| 2384 | * reaches zero and ``disable`` equals ``true``, the event will be disabled.
|
| 2385 | *
|
| 2386 | * Useful for hot-removable devices, where communication may fail once the
|
| 2387 | * device has been physically removed. In that case, specifying ``disable`` as
|
| 2388 | * ``false`` avoids communication with the EC.
|
| 2389 | *
|
| 2390 | * Return: Returns zero on success, %-ENOENT if the given notifier block has
|
| 2391 | * not been registered on the controller. If the given notifier block was the
|
| 2392 | * last one associated with its specific event, returns the status of the
|
| 2393 | * event-disable EC-command.
|
| 2394 |
|
| 2395 | int __ssam_notifier_unregister(struct ssam_controller *ctrl, struct ssam_event_notifier *n,
|
| 2396 | bool disable)
|
| 2397 | {
|
| 2398 | u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
|
| 2399 | struct ssam_nf_refcount_entry *entry;
|
| 2400 | struct ssam_nf_head *nf_head;
|
| 2401 | struct ssam_nf *nf;
|
| 2402 | int status = 0;
|
| 2403 |
|
| 2404 | if (!ssh_rqid_is_event(rqid))
|
| 2405 | return -EINVAL;
|
| 2406 |
|
| 2407 | nf = &ctrl->cplt.event.notif;
|
| 2408 | nf_head = &nf->head[ssh_rqid_to_event(rqid)];
|
| 2409 |
|
| 2410 | mutex_lock(&nf->lock);
|
| 2411 |
|
| 2412 | if (!ssam_nfblk_find(nf_head, &n->base)) {
|
| 2413 | mutex_unlock(&nf->lock);
|
| 2414 | return -ENOENT;
|
| 2415 | }
|
| 2416 |
|
| 2417 | |
| 2418 | * If this is an observer notifier, do not attempt to disable the
|
| 2419 | * event, just remove it.
|
| 2420 |
|
| 2421 | if (!(n->flags & SSAM_EVENT_NOTIFIER_OBSERVER)) {
|
| 2422 | entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
|
| 2423 | if (WARN_ON(!entry)) {
|
| 2424 | |
| 2425 | * If this does not return an entry, there's a logic
|
| 2426 | * error somewhere: The notifier block is registered,
|
| 2427 | * but the event refcount entry is not there. Remove
|
| 2428 | * the notifier block anyways.
|
| 2429 |
|
| 2430 | status = -ENOENT;
|
| 2431 | goto remove;
|
| 2432 | }
|
| 2433 |
|
| 2434 | status = ssam_nf_refcount_disable_free(ctrl, entry, n->event.flags, disable);
|
| 2435 | }
|
| 2436 |
|
| 2437 | remove:
|
| 2438 | ssam_nfblk_remove(&n->base);
|
| 2439 | mutex_unlock(&nf->lock);
|
| 2440 | synchronize_srcu(&nf_head->srcu);
|
| 2441 |
|
| 2442 | return status;
|
| 2443 | }
|
| 2444 | EXPORT_SYMBOL_GPL(__ssam_notifier_unregister);
|
| 2445 |
|
| 2446 | |
| 2447 | * ssam_controller_event_enable() - Enable the specified event.
|
| 2448 | * @ctrl: The controller to enable the event for.
|
| 2449 | * @reg: The event registry to use for enabling the event.
|
| 2450 | * @id: The event ID specifying the event to be enabled.
|
| 2451 | * @flags: The SAM event flags used for enabling the event.
|
| 2452 | *
|
| 2453 | * Increment the event reference count of the specified event. If the event has
|
| 2454 | * not been enabled previously, it will be enabled by this call.
|
| 2455 | *
|
| 2456 | * Note: In general, ssam_notifier_register() with a non-observer notifier
|
| 2457 | * should be preferred for enabling/disabling events, as this will guarantee
|
| 2458 | * proper ordering and event forwarding in case of errors during event
|
| 2459 | * enabling/disabling.
|
| 2460 | *
|
| 2461 | * Return: Returns zero on success, %-ENOSPC if the reference count for the
|
| 2462 | * specified event has reached its maximum, %-ENOMEM if the corresponding event
|
| 2463 | * entry could not be allocated. If this is the first time that this event has
|
| 2464 | * been enabled (i.e. the reference count was incremented from zero to one by
|
| 2465 | * this call), returns the status of the event-enable EC-command.
|
| 2466 |
|
| 2467 | int ssam_controller_event_enable(struct ssam_controller *ctrl,
|
| 2468 | struct ssam_event_registry reg,
|
| 2469 | struct ssam_event_id id, u8 flags)
|
| 2470 | {
|
| 2471 | u16 rqid = ssh_tc_to_rqid(id.target_category);
|
| 2472 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2473 | struct ssam_nf_refcount_entry *entry;
|
| 2474 | int status;
|
| 2475 |
|
| 2476 | if (!ssh_rqid_is_event(rqid))
|
| 2477 | return -EINVAL;
|
| 2478 |
|
| 2479 | mutex_lock(&nf->lock);
|
| 2480 |
|
| 2481 | entry = ssam_nf_refcount_inc(nf, reg, id);
|
| 2482 | if (IS_ERR(entry)) {
|
| 2483 | mutex_unlock(&nf->lock);
|
| 2484 | return PTR_ERR(entry);
|
| 2485 | }
|
| 2486 |
|
| 2487 | status = ssam_nf_refcount_enable(ctrl, entry, flags);
|
| 2488 | if (status) {
|
| 2489 | ssam_nf_refcount_dec_free(nf, reg, id);
|
| 2490 | mutex_unlock(&nf->lock);
|
| 2491 | return status;
|
| 2492 | }
|
| 2493 |
|
| 2494 | mutex_unlock(&nf->lock);
|
| 2495 | return 0;
|
| 2496 | }
|
| 2497 | EXPORT_SYMBOL_GPL(ssam_controller_event_enable);
|
| 2498 |
|
| 2499 | |
| 2500 | * ssam_controller_event_disable() - Disable the specified event.
|
| 2501 | * @ctrl: The controller to disable the event for.
|
| 2502 | * @reg: The event registry to use for disabling the event.
|
| 2503 | * @id: The event ID specifying the event to be disabled.
|
| 2504 | * @flags: The flags used when enabling the event.
|
| 2505 | *
|
| 2506 | * Decrement the reference count of the specified event. If the reference count
|
| 2507 | * reaches zero, the event will be disabled.
|
| 2508 | *
|
| 2509 | * Note: In general, ssam_notifier_register()/ssam_notifier_unregister() with a
|
| 2510 | * non-observer notifier should be preferred for enabling/disabling events, as
|
| 2511 | * this will guarantee proper ordering and event forwarding in case of errors
|
| 2512 | * during event enabling/disabling.
|
| 2513 | *
|
| 2514 | * Return: Returns zero on success, %-ENOENT if the given event has not been
|
| 2515 | * enabled on the controller. If the reference count of the event reaches zero
|
| 2516 | * during this call, returns the status of the event-disable EC-command.
|
| 2517 |
|
| 2518 | int ssam_controller_event_disable(struct ssam_controller *ctrl,
|
| 2519 | struct ssam_event_registry reg,
|
| 2520 | struct ssam_event_id id, u8 flags)
|
| 2521 | {
|
| 2522 | u16 rqid = ssh_tc_to_rqid(id.target_category);
|
| 2523 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2524 | struct ssam_nf_refcount_entry *entry;
|
| 2525 | int status;
|
| 2526 |
|
| 2527 | if (!ssh_rqid_is_event(rqid))
|
| 2528 | return -EINVAL;
|
| 2529 |
|
| 2530 | mutex_lock(&nf->lock);
|
| 2531 |
|
| 2532 | entry = ssam_nf_refcount_dec(nf, reg, id);
|
| 2533 | if (!entry) {
|
| 2534 | mutex_unlock(&nf->lock);
|
| 2535 | return -ENOENT;
|
| 2536 | }
|
| 2537 |
|
| 2538 | status = ssam_nf_refcount_disable_free(ctrl, entry, flags, true);
|
| 2539 |
|
| 2540 | mutex_unlock(&nf->lock);
|
| 2541 | return status;
|
| 2542 | }
|
| 2543 | EXPORT_SYMBOL_GPL(ssam_controller_event_disable);
|
| 2544 |
|
| 2545 | |
| 2546 | * ssam_notifier_disable_registered() - Disable events for all registered
|
| 2547 | * notifiers.
|
| 2548 | * @ctrl: The controller for which to disable the notifiers/events.
|
| 2549 | *
|
| 2550 | * Disables events for all currently registered notifiers. In case of an error
|
| 2551 | * (EC command failing), all previously disabled events will be restored and
|
| 2552 | * the error code returned.
|
| 2553 | *
|
| 2554 | * This function is intended to disable all events prior to hibernation entry.
|
| 2555 | * See ssam_notifier_restore_registered() to restore/re-enable all events
|
| 2556 | * disabled with this function.
|
| 2557 | *
|
| 2558 | * Note that this function will not disable events for notifiers registered
|
| 2559 | * after calling this function. It should thus be made sure that no new
|
| 2560 | * notifiers are going to be added after this call and before the corresponding
|
| 2561 | * call to ssam_notifier_restore_registered().
|
| 2562 | *
|
| 2563 | * Return: Returns zero on success. In case of failure returns the error code
|
| 2564 | * returned by the failed EC command to disable an event.
|
| 2565 |
|
| 2566 | int ssam_notifier_disable_registered(struct ssam_controller *ctrl)
|
| 2567 | {
|
| 2568 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2569 | struct rb_node *n;
|
| 2570 | int status;
|
| 2571 |
|
| 2572 | mutex_lock(&nf->lock);
|
| 2573 | for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
|
| 2574 | struct ssam_nf_refcount_entry *e;
|
| 2575 |
|
| 2576 | e = rb_entry(n, struct ssam_nf_refcount_entry, node);
|
| 2577 | status = ssam_ssh_event_disable(ctrl, e->key.reg,
|
| 2578 | e->key.id, e->flags);
|
| 2579 | if (status)
|
| 2580 | goto err;
|
| 2581 | }
|
| 2582 | mutex_unlock(&nf->lock);
|
| 2583 |
|
| 2584 | return 0;
|
| 2585 |
|
| 2586 | err:
|
| 2587 | for (n = rb_prev(n); n; n = rb_prev(n)) {
|
| 2588 | struct ssam_nf_refcount_entry *e;
|
| 2589 |
|
| 2590 | e = rb_entry(n, struct ssam_nf_refcount_entry, node);
|
| 2591 | ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
|
| 2592 | }
|
| 2593 | mutex_unlock(&nf->lock);
|
| 2594 |
|
| 2595 | return status;
|
| 2596 | }
|
| 2597 |
|
| 2598 | |
| 2599 | * ssam_notifier_restore_registered() - Restore/re-enable events for all
|
| 2600 | * registered notifiers.
|
| 2601 | * @ctrl: The controller for which to restore the notifiers/events.
|
| 2602 | *
|
| 2603 | * Restores/re-enables all events for which notifiers have been registered on
|
| 2604 | * the given controller. In case of a failure, the error is logged and the
|
| 2605 | * function continues to try and enable the remaining events.
|
| 2606 | *
|
| 2607 | * This function is intended to restore/re-enable all registered events after
|
| 2608 | * hibernation. See ssam_notifier_disable_registered() for the counter part
|
| 2609 | * disabling the events and more details.
|
| 2610 |
|
| 2611 | void ssam_notifier_restore_registered(struct ssam_controller *ctrl)
|
| 2612 | {
|
| 2613 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2614 | struct rb_node *n;
|
| 2615 |
|
| 2616 | mutex_lock(&nf->lock);
|
| 2617 | for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
|
| 2618 | struct ssam_nf_refcount_entry *e;
|
| 2619 |
|
| 2620 | e = rb_entry(n, struct ssam_nf_refcount_entry, node);
|
| 2621 |
|
| 2622 |
|
| 2623 | ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
|
| 2624 | }
|
| 2625 | mutex_unlock(&nf->lock);
|
| 2626 | }
|
| 2627 |
|
| 2628 | |
| 2629 | * ssam_notifier_is_empty() - Check if there are any registered notifiers.
|
| 2630 | * @ctrl: The controller to check on.
|
| 2631 | *
|
| 2632 | * Return: Returns %true if there are currently no notifiers registered on the
|
| 2633 | * controller, %false otherwise.
|
| 2634 |
|
| 2635 | static bool ssam_notifier_is_empty(struct ssam_controller *ctrl)
|
| 2636 | {
|
| 2637 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2638 | bool result;
|
| 2639 |
|
| 2640 | mutex_lock(&nf->lock);
|
| 2641 | result = ssam_nf_refcount_empty(nf);
|
| 2642 | mutex_unlock(&nf->lock);
|
| 2643 |
|
| 2644 | return result;
|
| 2645 | }
|
| 2646 |
|
| 2647 | |
| 2648 | * ssam_notifier_unregister_all() - Unregister all currently registered
|
| 2649 | * notifiers.
|
| 2650 | * @ctrl: The controller to unregister the notifiers on.
|
| 2651 | *
|
| 2652 | * Unregisters all currently registered notifiers. This function is used to
|
| 2653 | * ensure that all notifiers will be unregistered and associated
|
| 2654 | * entries/resources freed when the controller is being shut down.
|
| 2655 |
|
| 2656 | static void ssam_notifier_unregister_all(struct ssam_controller *ctrl)
|
| 2657 | {
|
| 2658 | struct ssam_nf *nf = &ctrl->cplt.event.notif;
|
| 2659 | struct ssam_nf_refcount_entry *e, *n;
|
| 2660 |
|
| 2661 | mutex_lock(&nf->lock);
|
| 2662 | rbtree_postorder_for_each_entry_safe(e, n, &nf->refcount, node) {
|
| 2663 |
|
| 2664 | ssam_ssh_event_disable(ctrl, e->key.reg, e->key.id, e->flags);
|
| 2665 | kfree(e);
|
| 2666 | }
|
| 2667 | nf->refcount = RB_ROOT;
|
| 2668 | mutex_unlock(&nf->lock);
|
| 2669 | }
|
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 | static irqreturn_t ssam_irq_handle(int irq, void *dev_id)
|
| 2675 | {
|
| 2676 | struct ssam_controller *ctrl = dev_id;
|
| 2677 |
|
| 2678 | ssam_dbg(ctrl, "pm: wake irq triggered\n");
|
| 2679 |
|
| 2680 | |
| 2681 | * Note: Proper wakeup detection is currently unimplemented.
|
| 2682 | * When the EC is in display-off or any other non-D0 state, it
|
| 2683 | * does not send events/notifications to the host. Instead it
|
| 2684 | * signals that there are events available via the wakeup IRQ.
|
| 2685 | * This driver is responsible for calling back to the EC to
|
| 2686 | * release these events one-by-one.
|
| 2687 | *
|
| 2688 | * This IRQ should not cause a full system resume by its own.
|
| 2689 | * Instead, events should be handled by their respective subsystem
|
| 2690 | * drivers, which in turn should signal whether a full system
|
| 2691 | * resume should be performed.
|
| 2692 | *
|
| 2693 | * TODO: Send GPIO callback command repeatedly to EC until callback
|
| 2694 | * returns 0x00. Return flag of callback is "has more events".
|
| 2695 | * Each time the command is sent, one event is "released". Once
|
| 2696 | * all events have been released (return = 0x00), the GPIO is
|
| 2697 | * re-armed. Detect wakeup events during this process, go back to
|
| 2698 | * sleep if no wakeup event has been received.
|
| 2699 |
|
| 2700 |
|
| 2701 | return IRQ_HANDLED;
|
| 2702 | }
|
| 2703 |
|
| 2704 | |
| 2705 | * ssam_irq_setup() - Set up SAM EC wakeup-GPIO interrupt.
|
| 2706 | * @ctrl: The controller for which the IRQ should be set up.
|
| 2707 | *
|
| 2708 | * Set up an IRQ for the wakeup-GPIO pin of the SAM EC. This IRQ can be used
|
| 2709 | * to wake the device from a low power state.
|
| 2710 | *
|
| 2711 | * Note that this IRQ can only be triggered while the EC is in the display-off
|
| 2712 | * state. In this state, events are not sent to the host in the usual way.
|
| 2713 | * Instead the wakeup-GPIO gets pulled to "high" as long as there are pending
|
| 2714 | * events and these events need to be released one-by-one via the GPIO
|
| 2715 | * callback request, either until there are no events left and the GPIO is
|
| 2716 | * reset, or all at once by transitioning the EC out of the display-off state,
|
| 2717 | * which will also clear the GPIO.
|
| 2718 | *
|
| 2719 | * Not all events, however, should trigger a full system wakeup. Instead the
|
| 2720 | * driver should, if necessary, inspect and forward each event to the
|
| 2721 | * corresponding subsystem, which in turn should decide if the system needs to
|
| 2722 | * be woken up. This logic has not been implemented yet, thus wakeup by this
|
| 2723 | * IRQ should be disabled by default to avoid spurious wake-ups, caused, for
|
| 2724 | * example, by the remaining battery percentage changing. Refer to comments in
|
| 2725 | * this function and comments in the corresponding IRQ handler for more
|
| 2726 | * details on how this should be implemented.
|
| 2727 | *
|
| 2728 | * See also ssam_ctrl_notif_display_off() and ssam_ctrl_notif_display_off()
|
| 2729 | * for functions to transition the EC into and out of the display-off state as
|
| 2730 | * well as more details on it.
|
| 2731 | *
|
| 2732 | * The IRQ is disabled by default and has to be enabled before it can wake up
|
| 2733 | * the device from suspend via ssam_irq_arm_for_wakeup(). On teardown, the IRQ
|
| 2734 | * should be freed via ssam_irq_free().
|
| 2735 |
|
| 2736 | int ssam_irq_setup(struct ssam_controller *ctrl)
|
| 2737 | {
|
| 2738 | struct device *dev = ssam_controller_device(ctrl);
|
| 2739 | struct gpio_desc *gpiod;
|
| 2740 | int irq;
|
| 2741 | int status;
|
| 2742 |
|
| 2743 | |
| 2744 | * The actual GPIO interrupt is declared in ACPI as TRIGGER_HIGH.
|
| 2745 | * However, the GPIO line only gets reset by sending the GPIO callback
|
| 2746 | * command to SAM (or alternatively the display-on notification). As
|
| 2747 | * proper handling for this interrupt is not implemented yet, leaving
|
| 2748 | * the IRQ at TRIGGER_HIGH would cause an IRQ storm (as the callback
|
| 2749 | * never gets sent and thus the line never gets reset). To avoid this,
|
| 2750 | * mark the IRQ as TRIGGER_RISING for now, only creating a single
|
| 2751 | * interrupt, and let the SAM resume callback during the controller
|
| 2752 | * resume process clear it.
|
| 2753 |
|
| 2754 | const int irqf = IRQF_ONESHOT | IRQF_TRIGGER_RISING | IRQF_NO_AUTOEN;
|
| 2755 |
|
| 2756 | gpiod = gpiod_get(dev, "ssam_wakeup-int", GPIOD_ASIS);
|
| 2757 | if (IS_ERR(gpiod)) {
|
| 2758 | irq = fwnode_irq_get(dev_fwnode(dev), 0);
|
| 2759 | } else {
|
| 2760 | irq = gpiod_to_irq(gpiod);
|
| 2761 | gpiod_put(gpiod);
|
| 2762 | }
|
| 2763 |
|
| 2764 | if (irq < 0)
|
| 2765 | return irq;
|
| 2766 |
|
| 2767 | status = request_threaded_irq(irq, NULL, ssam_irq_handle, irqf,
|
| 2768 | "ssam_wakeup", ctrl);
|
| 2769 | if (status)
|
| 2770 | return status;
|
| 2771 |
|
| 2772 | ctrl->irq.num = irq;
|
| 2773 | return 0;
|
| 2774 | }
|
| 2775 |
|
| 2776 | |
| 2777 | * ssam_irq_free() - Free SAM EC wakeup-GPIO interrupt.
|
| 2778 | * @ctrl: The controller for which the IRQ should be freed.
|
| 2779 | *
|
| 2780 | * Free the wakeup-GPIO IRQ previously set-up via ssam_irq_setup().
|
| 2781 |
|
| 2782 | void ssam_irq_free(struct ssam_controller *ctrl)
|
| 2783 | {
|
| 2784 | free_irq(ctrl->irq.num, ctrl);
|
| 2785 | ctrl->irq.num = -1;
|
| 2786 | }
|
| 2787 |
|
| 2788 | |
| 2789 | * ssam_irq_arm_for_wakeup() - Arm the EC IRQ for wakeup, if enabled.
|
| 2790 | * @ctrl: The controller for which the IRQ should be armed.
|
| 2791 | *
|
| 2792 | * Sets up the IRQ so that it can be used to wake the device. Specifically,
|
| 2793 | * this function enables the irq and then, if the device is allowed to wake up
|
| 2794 | * the system, calls enable_irq_wake(). See ssam_irq_disarm_wakeup() for the
|
| 2795 | * corresponding function to disable the IRQ.
|
| 2796 | *
|
| 2797 | * This function is intended to arm the IRQ before entering S2idle suspend.
|
| 2798 | *
|
| 2799 | * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
|
| 2800 | * be balanced.
|
| 2801 |
|
| 2802 | int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl)
|
| 2803 | {
|
| 2804 | struct device *dev = ssam_controller_device(ctrl);
|
| 2805 | int status;
|
| 2806 |
|
| 2807 | enable_irq(ctrl->irq.num);
|
| 2808 | if (device_may_wakeup(dev)) {
|
| 2809 | status = enable_irq_wake(ctrl->irq.num);
|
| 2810 | if (status) {
|
| 2811 | ssam_err(ctrl, "failed to enable wake IRQ: %d\n", status);
|
| 2812 | disable_irq(ctrl->irq.num);
|
| 2813 | return status;
|
| 2814 | }
|
| 2815 |
|
| 2816 | ctrl->irq.wakeup_enabled = true;
|
| 2817 | } else {
|
| 2818 | ctrl->irq.wakeup_enabled = false;
|
| 2819 | }
|
| 2820 |
|
| 2821 | return 0;
|
| 2822 | }
|
| 2823 |
|
| 2824 | |
| 2825 | * ssam_irq_disarm_wakeup() - Disarm the wakeup IRQ.
|
| 2826 | * @ctrl: The controller for which the IRQ should be disarmed.
|
| 2827 | *
|
| 2828 | * Disarm the IRQ previously set up for wake via ssam_irq_arm_for_wakeup().
|
| 2829 | *
|
| 2830 | * This function is intended to disarm the IRQ after exiting S2idle suspend.
|
| 2831 | *
|
| 2832 | * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
|
| 2833 | * be balanced.
|
| 2834 |
|
| 2835 | void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
|
| 2836 | {
|
| 2837 | int status;
|
| 2838 |
|
| 2839 | if (ctrl->irq.wakeup_enabled) {
|
| 2840 | status = disable_irq_wake(ctrl->irq.num);
|
| 2841 | if (status)
|
| 2842 | ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
|
| 2843 |
|
| 2844 | ctrl->irq.wakeup_enabled = false;
|
| 2845 | }
|
| 2846 | disable_irq(ctrl->irq.num);
|
| 2847 | }
|