1
2
3
4
5
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
42
43
44static void ssh_seq_reset(struct ssh_seq_counter *c)
45{
46 WRITE_ONCE(c->value, 0);
47}
48
49
50
51
52
53
54
55static 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
71
72
73static void ssh_rqid_reset(struct ssh_rqid_counter *c)
74{
75 WRITE_ONCE(c->value, 0);
76}
77
78
79
80
81
82
83
84
85static 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118static 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148static 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
170
171
172
173
174
175
176
177
178
179
180static 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
204
205
206
207
208
209
210
211
212
213
214
215static 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
230
231
232
233
234
235
236
237
238static void ssam_nfblk_remove(struct ssam_notifier_block *nb)
239{
240 list_del_rcu(&nb->node);
241}
242
243
244
245
246
247static 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
261
262
263static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
264{
265 cleanup_srcu_struct(&nh->srcu);
266}
267
268
269
270
271
272
273
274
275
276
277struct ssam_nf_refcount_key {
278 struct ssam_event_registry reg;
279 struct ssam_event_id id;
280};
281
282
283
284
285
286
287
288
289
290struct 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314static struct ssam_nf_refcount_entry *
315ssam_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(sizeof(*entry), GFP_KERNEL);
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376static struct ssam_nf_refcount_entry *
377ssam_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
412
413
414
415
416
417
418
419
420
421
422static 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
437
438
439
440static bool ssam_nf_refcount_empty(struct ssam_nf *nf)
441{
442 return RB_EMPTY_ROOT(&nf->refcount);
443}
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464static 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
494
495
496static 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
519
520
521static 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
538
539
540
541#define SSAM_CPLT_WQ_BATCH 10
542
543
544
545
546
547
548
549
550
551#define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN 32
552
553static struct kmem_cache *ssam_event_item_cache;
554
555
556
557
558int 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
575
576void ssam_event_item_cache_destroy(void)
577{
578 kmem_cache_destroy(ssam_event_item_cache);
579 ssam_event_item_cache = NULL;
580}
581
582static void __ssam_event_item_free_cached(struct ssam_event_item *item)
583{
584 kmem_cache_free(ssam_event_item_cache, item);
585}
586
587static void __ssam_event_item_free_generic(struct ssam_event_item *item)
588{
589 kfree(item);
590}
591
592
593
594
595
596static 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
604
605
606
607
608
609
610
611
612
613
614
615static 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(struct_size(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
641
642
643
644static 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
654
655
656
657
658
659static 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
674
675
676static 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
689
690
691
692
693
694
695
696
697
698static
699struct 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
720
721
722
723static bool ssam_cplt_submit(struct ssam_cplt *cplt, struct work_struct *work)
724{
725 return queue_work(cplt->wq, work);
726}
727
728
729
730
731
732
733
734
735
736
737
738
739
740static 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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771static void ssam_cplt_flush(struct ssam_cplt *cplt)
772{
773 flush_workqueue(cplt->wq);
774}
775
776static 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
804
805
806
807static 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
818
819
820
821static 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 = create_workqueue(SSAM_CPLT_WQ_NAME);
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
848
849
850
851
852
853static void ssam_cplt_destroy(struct ssam_cplt *cplt)
854{
855
856
857
858
859
860
861 destroy_workqueue(cplt->wq);
862 ssam_nf_destroy(&cplt->event.notif);
863}
864
865
866
867
868
869
870
871
872
873
874
875
876struct device *ssam_controller_device(struct ssam_controller *c)
877{
878 return ssh_rtl_get_device(&c->rtl);
879}
880EXPORT_SYMBOL_GPL(ssam_controller_device);
881
882static void __ssam_controller_release(struct kref *kref)
883{
884 struct ssam_controller *ctrl = to_ssam_controller(kref, kref);
885
886
887
888
889
890
891 ssam_controller_lock(ctrl);
892 ssam_controller_destroy(ctrl);
893 ssam_controller_unlock(ctrl);
894
895 kfree(ctrl);
896}
897
898
899
900
901
902
903
904struct ssam_controller *ssam_controller_get(struct ssam_controller *c)
905{
906 if (c)
907 kref_get(&c->kref);
908 return c;
909}
910EXPORT_SYMBOL_GPL(ssam_controller_get);
911
912
913
914
915
916void ssam_controller_put(struct ssam_controller *c)
917{
918 if (c)
919 kref_put(&c->kref, __ssam_controller_release);
920}
921EXPORT_SYMBOL_GPL(ssam_controller_put);
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940void ssam_controller_statelock(struct ssam_controller *c)
941{
942 down_read(&c->lock);
943}
944EXPORT_SYMBOL_GPL(ssam_controller_statelock);
945
946
947
948
949
950
951
952void ssam_controller_stateunlock(struct ssam_controller *c)
953{
954 up_read(&c->lock);
955}
956EXPORT_SYMBOL_GPL(ssam_controller_stateunlock);
957
958
959
960
961
962
963
964
965
966
967
968void ssam_controller_lock(struct ssam_controller *c)
969{
970 down_write(&c->lock);
971}
972
973
974
975
976
977
978
979void ssam_controller_unlock(struct ssam_controller *c)
980{
981 up_write(&c->lock);
982}
983
984static 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->tid_in;
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
1006static const struct ssh_rtl_ops ssam_rtl_ops = {
1007 .handle_event = ssam_handle_event,
1008};
1009
1010static bool ssam_notifier_is_empty(struct ssam_controller *ctrl);
1011static void ssam_notifier_unregister_all(struct ssam_controller *ctrl);
1012
1013#define SSAM_SSH_DSM_REVISION 0
1014
1015
1016static const guid_t SSAM_SSH_DSM_GUID =
1017 GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
1018 0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
1019
1020enum 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
1028static 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
1038
1039
1040
1041
1042
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
1063static 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
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099static
1100int 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 caps->ssh_power_profile = U32_MAX;
1109 caps->screen_on_sleep_idle_timeout = U32_MAX;
1110 caps->screen_off_sleep_idle_timeout = U32_MAX;
1111 caps->d3_closes_handle = false;
1112 caps->ssh_buffer_size = U32_MAX;
1113
1114
1115 status = ssam_dsm_get_functions(handle, &funcs);
1116 if (status)
1117 return status;
1118
1119
1120 status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_POWER_PROFILE,
1121 &caps->ssh_power_profile);
1122 if (status)
1123 return status;
1124
1125 status = ssam_dsm_load_u32(handle, funcs,
1126 SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT,
1127 &caps->screen_on_sleep_idle_timeout);
1128 if (status)
1129 return status;
1130
1131 status = ssam_dsm_load_u32(handle, funcs,
1132 SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT,
1133 &caps->screen_off_sleep_idle_timeout);
1134 if (status)
1135 return status;
1136
1137 status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_D3_CLOSES_HANDLE,
1138 &d3_closes_handle);
1139 if (status)
1140 return status;
1141
1142 caps->d3_closes_handle = !!d3_closes_handle;
1143
1144 status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_BUFFER_SIZE,
1145 &caps->ssh_buffer_size);
1146 if (status)
1147 return status;
1148
1149 return 0;
1150}
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165int ssam_controller_init(struct ssam_controller *ctrl,
1166 struct serdev_device *serdev)
1167{
1168 acpi_handle handle = ACPI_HANDLE(&serdev->dev);
1169 int status;
1170
1171 init_rwsem(&ctrl->lock);
1172 kref_init(&ctrl->kref);
1173
1174 status = ssam_controller_caps_load_from_acpi(handle, &ctrl->caps);
1175 if (status)
1176 return status;
1177
1178 dev_dbg(&serdev->dev,
1179 "device capabilities:\n"
1180 " ssh_power_profile: %u\n"
1181 " ssh_buffer_size: %u\n"
1182 " screen_on_sleep_idle_timeout: %u\n"
1183 " screen_off_sleep_idle_timeout: %u\n"
1184 " d3_closes_handle: %u\n",
1185 ctrl->caps.ssh_power_profile,
1186 ctrl->caps.ssh_buffer_size,
1187 ctrl->caps.screen_on_sleep_idle_timeout,
1188 ctrl->caps.screen_off_sleep_idle_timeout,
1189 ctrl->caps.d3_closes_handle);
1190
1191 ssh_seq_reset(&ctrl->counter.seq);
1192 ssh_rqid_reset(&ctrl->counter.rqid);
1193
1194
1195 status = ssam_cplt_init(&ctrl->cplt, &serdev->dev);
1196 if (status)
1197 return status;
1198
1199
1200 status = ssh_rtl_init(&ctrl->rtl, serdev, &ssam_rtl_ops);
1201 if (status) {
1202 ssam_cplt_destroy(&ctrl->cplt);
1203 return status;
1204 }
1205
1206
1207
1208
1209
1210
1211 WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_INITIALIZED);
1212 return 0;
1213}
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227int ssam_controller_start(struct ssam_controller *ctrl)
1228{
1229 int status;
1230
1231 lockdep_assert_held_write(&ctrl->lock);
1232
1233 if (ctrl->state != SSAM_CONTROLLER_INITIALIZED)
1234 return -EINVAL;
1235
1236 status = ssh_rtl_start(&ctrl->rtl);
1237 if (status)
1238 return status;
1239
1240
1241
1242
1243
1244
1245 WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
1246 return 0;
1247}
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257#define SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT msecs_to_jiffies(5000)
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281void ssam_controller_shutdown(struct ssam_controller *ctrl)
1282{
1283 enum ssam_controller_state s = ctrl->state;
1284 int status;
1285
1286 lockdep_assert_held_write(&ctrl->lock);
1287
1288 if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
1289 return;
1290
1291
1292
1293
1294
1295
1296
1297
1298 status = ssh_rtl_flush(&ctrl->rtl, SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT);
1299 if (status) {
1300 ssam_err(ctrl, "failed to flush request transport layer: %d\n",
1301 status);
1302 }
1303
1304
1305 ssam_cplt_flush(&ctrl->cplt);
1306
1307
1308
1309
1310
1311
1312 WARN_ON(!ssam_notifier_is_empty(ctrl));
1313
1314
1315
1316
1317
1318 ssam_notifier_unregister_all(ctrl);
1319
1320
1321
1322
1323
1324 ssh_rtl_shutdown(&ctrl->rtl);
1325
1326
1327
1328
1329
1330
1331 WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STOPPED);
1332 ctrl->rtl.ptl.serdev = NULL;
1333}
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350void ssam_controller_destroy(struct ssam_controller *ctrl)
1351{
1352 lockdep_assert_held_write(&ctrl->lock);
1353
1354 if (ctrl->state == SSAM_CONTROLLER_UNINITIALIZED)
1355 return;
1356
1357 WARN_ON(ctrl->state != SSAM_CONTROLLER_STOPPED);
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368 ssam_cplt_destroy(&ctrl->cplt);
1369 ssh_rtl_destroy(&ctrl->rtl);
1370
1371
1372
1373
1374
1375
1376 WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_UNINITIALIZED);
1377}
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392int ssam_controller_suspend(struct ssam_controller *ctrl)
1393{
1394 ssam_controller_lock(ctrl);
1395
1396 if (ctrl->state != SSAM_CONTROLLER_STARTED) {
1397 ssam_controller_unlock(ctrl);
1398 return -EINVAL;
1399 }
1400
1401 ssam_dbg(ctrl, "pm: suspending controller\n");
1402
1403
1404
1405
1406
1407 WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_SUSPENDED);
1408
1409 ssam_controller_unlock(ctrl);
1410 return 0;
1411}
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424int ssam_controller_resume(struct ssam_controller *ctrl)
1425{
1426 ssam_controller_lock(ctrl);
1427
1428 if (ctrl->state != SSAM_CONTROLLER_SUSPENDED) {
1429 ssam_controller_unlock(ctrl);
1430 return -EINVAL;
1431 }
1432
1433 ssam_dbg(ctrl, "pm: resuming controller\n");
1434
1435
1436
1437
1438
1439 WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
1440
1441 ssam_controller_unlock(ctrl);
1442 return 0;
1443}
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468ssize_t ssam_request_write_data(struct ssam_span *buf,
1469 struct ssam_controller *ctrl,
1470 const struct ssam_request *spec)
1471{
1472 struct msgbuf msgb;
1473 u16 rqid;
1474 u8 seq;
1475
1476 if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE)
1477 return -EINVAL;
1478
1479 if (SSH_COMMAND_MESSAGE_LENGTH(spec->length) > buf->len)
1480 return -EINVAL;
1481
1482 msgb_init(&msgb, buf->ptr, buf->len);
1483 seq = ssh_seq_next(&ctrl->counter.seq);
1484 rqid = ssh_rqid_next(&ctrl->counter.rqid);
1485 msgb_push_cmd(&msgb, seq, rqid, spec);
1486
1487 return msgb_bytes_used(&msgb);
1488}
1489EXPORT_SYMBOL_GPL(ssam_request_write_data);
1490
1491static void ssam_request_sync_complete(struct ssh_request *rqst,
1492 const struct ssh_command *cmd,
1493 const struct ssam_span *data, int status)
1494{
1495 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
1496 struct ssam_request_sync *r;
1497
1498 r = container_of(rqst, struct ssam_request_sync, base);
1499 r->status = status;
1500
1501 if (r->resp)
1502 r->resp->length = 0;
1503
1504 if (status) {
1505 rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
1506 return;
1507 }
1508
1509 if (!data)
1510 return;
1511
1512 if (!r->resp || !r->resp->pointer) {
1513 if (data->len)
1514 rtl_warn(rtl, "rsp: no response buffer provided, dropping data\n");
1515 return;
1516 }
1517
1518 if (data->len > r->resp->capacity) {
1519 rtl_err(rtl,
1520 "rsp: response buffer too small, capacity: %zu bytes, got: %zu bytes\n",
1521 r->resp->capacity, data->len);
1522 r->status = -ENOSPC;
1523 return;
1524 }
1525
1526 r->resp->length = data->len;
1527 memcpy(r->resp->pointer, data->ptr, data->len);
1528}
1529
1530static void ssam_request_sync_release(struct ssh_request *rqst)
1531{
1532 complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
1533}
1534
1535static const struct ssh_request_ops ssam_request_sync_ops = {
1536 .release = ssam_request_sync_release,
1537 .complete = ssam_request_sync_complete,
1538};
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
1561 struct ssam_request_sync **rqst,
1562 struct ssam_span *buffer)
1563{
1564 size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
1565
1566 *rqst = kzalloc(sizeof(**rqst) + msglen, flags);
1567 if (!*rqst)
1568 return -ENOMEM;
1569
1570 buffer->ptr = (u8 *)(*rqst + 1);
1571 buffer->len = msglen;
1572
1573 return 0;
1574}
1575EXPORT_SYMBOL_GPL(ssam_request_sync_alloc);
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592void ssam_request_sync_free(struct ssam_request_sync *rqst)
1593{
1594 kfree(rqst);
1595}
1596EXPORT_SYMBOL_GPL(ssam_request_sync_free);
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610int ssam_request_sync_init(struct ssam_request_sync *rqst,
1611 enum ssam_request_flags flags)
1612{
1613 int status;
1614
1615 status = ssh_request_init(&rqst->base, flags, &ssam_request_sync_ops);
1616 if (status)
1617 return status;
1618
1619 init_completion(&rqst->comp);
1620 rqst->resp = NULL;
1621 rqst->status = 0;
1622
1623 return 0;
1624}
1625EXPORT_SYMBOL_GPL(ssam_request_sync_init);
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645int ssam_request_sync_submit(struct ssam_controller *ctrl,
1646 struct ssam_request_sync *rqst)
1647{
1648 int status;
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664 if (WARN_ON(READ_ONCE(ctrl->state) != SSAM_CONTROLLER_STARTED)) {
1665 ssh_request_put(&rqst->base);
1666 return -ENODEV;
1667 }
1668
1669 status = ssh_rtl_submit(&ctrl->rtl, &rqst->base);
1670 ssh_request_put(&rqst->base);
1671
1672 return status;
1673}
1674EXPORT_SYMBOL_GPL(ssam_request_sync_submit);
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689int ssam_request_sync(struct ssam_controller *ctrl,
1690 const struct ssam_request *spec,
1691 struct ssam_response *rsp)
1692{
1693 struct ssam_request_sync *rqst;
1694 struct ssam_span buf;
1695 ssize_t len;
1696 int status;
1697
1698 status = ssam_request_sync_alloc(spec->length, GFP_KERNEL, &rqst, &buf);
1699 if (status)
1700 return status;
1701
1702 status = ssam_request_sync_init(rqst, spec->flags);
1703 if (status)
1704 return status;
1705
1706 ssam_request_sync_set_resp(rqst, rsp);
1707
1708 len = ssam_request_write_data(&buf, ctrl, spec);
1709 if (len < 0) {
1710 ssam_request_sync_free(rqst);
1711 return len;
1712 }
1713
1714 ssam_request_sync_set_data(rqst, buf.ptr, len);
1715
1716 status = ssam_request_sync_submit(ctrl, rqst);
1717 if (!status)
1718 status = ssam_request_sync_wait(rqst);
1719
1720 ssam_request_sync_free(rqst);
1721 return status;
1722}
1723EXPORT_SYMBOL_GPL(ssam_request_sync);
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
1747 const struct ssam_request *spec,
1748 struct ssam_response *rsp,
1749 struct ssam_span *buf)
1750{
1751 struct ssam_request_sync rqst;
1752 ssize_t len;
1753 int status;
1754
1755 status = ssam_request_sync_init(&rqst, spec->flags);
1756 if (status)
1757 return status;
1758
1759 ssam_request_sync_set_resp(&rqst, rsp);
1760
1761 len = ssam_request_write_data(buf, ctrl, spec);
1762 if (len < 0)
1763 return len;
1764
1765 ssam_request_sync_set_data(&rqst, buf->ptr, len);
1766
1767 status = ssam_request_sync_submit(ctrl, &rqst);
1768 if (!status)
1769 status = ssam_request_sync_wait(&rqst);
1770
1771 return status;
1772}
1773EXPORT_SYMBOL_GPL(ssam_request_sync_with_buffer);
1774
1775
1776
1777
1778SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
1779 .target_category = SSAM_SSH_TC_SAM,
1780 .target_id = 0x01,
1781 .command_id = 0x13,
1782 .instance_id = 0x00,
1783});
1784
1785SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
1786 .target_category = SSAM_SSH_TC_SAM,
1787 .target_id = 0x01,
1788 .command_id = 0x15,
1789 .instance_id = 0x00,
1790});
1791
1792SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
1793 .target_category = SSAM_SSH_TC_SAM,
1794 .target_id = 0x01,
1795 .command_id = 0x16,
1796 .instance_id = 0x00,
1797});
1798
1799SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
1800 .target_category = SSAM_SSH_TC_SAM,
1801 .target_id = 0x01,
1802 .command_id = 0x33,
1803 .instance_id = 0x00,
1804});
1805
1806SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
1807 .target_category = SSAM_SSH_TC_SAM,
1808 .target_id = 0x01,
1809 .command_id = 0x34,
1810 .instance_id = 0x00,
1811});
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823struct ssh_notification_params {
1824 u8 target_category;
1825 u8 flags;
1826 __le16 request_id;
1827 u8 instance_id;
1828} __packed;
1829
1830static_assert(sizeof(struct ssh_notification_params) == 5);
1831
1832static int __ssam_ssh_event_request(struct ssam_controller *ctrl,
1833 struct ssam_event_registry reg, u8 cid,
1834 struct ssam_event_id id, u8 flags)
1835{
1836 struct ssh_notification_params params;
1837 struct ssam_request rqst;
1838 struct ssam_response result;
1839 int status;
1840
1841 u16 rqid = ssh_tc_to_rqid(id.target_category);
1842 u8 buf = 0;
1843
1844
1845 if (!ssh_rqid_is_event(rqid))
1846 return -EINVAL;
1847
1848 params.target_category = id.target_category;
1849 params.instance_id = id.instance;
1850 params.flags = flags;
1851 put_unaligned_le16(rqid, ¶ms.request_id);
1852
1853 rqst.target_category = reg.target_category;
1854 rqst.target_id = reg.target_id;
1855 rqst.command_id = cid;
1856 rqst.instance_id = 0x00;
1857 rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
1858 rqst.length = sizeof(params);
1859 rqst.payload = (u8 *)¶ms;
1860
1861 result.capacity = sizeof(buf);
1862 result.length = 0;
1863 result.pointer = &buf;
1864
1865 status = ssam_retry(ssam_request_sync_onstack, ctrl, &rqst, &result,
1866 sizeof(params));
1867
1868 return status < 0 ? status : buf;
1869}
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888static int ssam_ssh_event_enable(struct ssam_controller *ctrl,
1889 struct ssam_event_registry reg,
1890 struct ssam_event_id id, u8 flags)
1891{
1892 int status;
1893
1894 status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
1895
1896 if (status < 0 && status != -EINVAL) {
1897 ssam_err(ctrl,
1898 "failed to enable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
1899 id.target_category, id.instance, reg.target_category);
1900 }
1901
1902 if (status > 0) {
1903 ssam_err(ctrl,
1904 "unexpected result while enabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
1905 status, id.target_category, id.instance, reg.target_category);
1906 return -EPROTO;
1907 }
1908
1909 return status;
1910}
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929static int ssam_ssh_event_disable(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_disable, id, flags);
1936
1937 if (status < 0 && status != -EINVAL) {
1938 ssam_err(ctrl,
1939 "failed to disable 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 disabling 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
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version)
1965{
1966 __le32 __version;
1967 int status;
1968
1969 status = ssam_retry(ssam_ssh_get_firmware_version, ctrl, &__version);
1970 if (status)
1971 return status;
1972
1973 *version = le32_to_cpu(__version);
1974 return 0;
1975}
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl)
2005{
2006 int status;
2007 u8 response;
2008
2009 ssam_dbg(ctrl, "pm: notifying display off\n");
2010
2011 status = ssam_retry(ssam_ssh_notif_display_off, ctrl, &response);
2012 if (status)
2013 return status;
2014
2015 if (response != 0) {
2016 ssam_err(ctrl, "unexpected response from display-off notification: %#04x\n",
2017 response);
2018 return -EPROTO;
2019 }
2020
2021 return 0;
2022}
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl)
2044{
2045 int status;
2046 u8 response;
2047
2048 ssam_dbg(ctrl, "pm: notifying display on\n");
2049
2050 status = ssam_retry(ssam_ssh_notif_display_on, ctrl, &response);
2051 if (status)
2052 return status;
2053
2054 if (response != 0) {
2055 ssam_err(ctrl, "unexpected response from display-on notification: %#04x\n",
2056 response);
2057 return -EPROTO;
2058 }
2059
2060 return 0;
2061}
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl)
2083{
2084 int status;
2085 u8 response;
2086
2087 if (!ctrl->caps.d3_closes_handle)
2088 return 0;
2089
2090 ssam_dbg(ctrl, "pm: notifying D0 exit\n");
2091
2092 status = ssam_retry(ssam_ssh_notif_d0_exit, ctrl, &response);
2093 if (status)
2094 return status;
2095
2096 if (response != 0) {
2097 ssam_err(ctrl, "unexpected response from D0-exit notification: %#04x\n",
2098 response);
2099 return -EPROTO;
2100 }
2101
2102 return 0;
2103}
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl)
2125{
2126 int status;
2127 u8 response;
2128
2129 if (!ctrl->caps.d3_closes_handle)
2130 return 0;
2131
2132 ssam_dbg(ctrl, "pm: notifying D0 entry\n");
2133
2134 status = ssam_retry(ssam_ssh_notif_d0_entry, ctrl, &response);
2135 if (status)
2136 return status;
2137
2138 if (response != 0) {
2139 ssam_err(ctrl, "unexpected response from D0-entry notification: %#04x\n",
2140 response);
2141 return -EPROTO;
2142 }
2143
2144 return 0;
2145}
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171static int ssam_nf_refcount_enable(struct ssam_controller *ctrl,
2172 struct ssam_nf_refcount_entry *entry, u8 flags)
2173{
2174 const struct ssam_event_registry reg = entry->key.reg;
2175 const struct ssam_event_id id = entry->key.id;
2176 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2177 int status;
2178
2179 lockdep_assert_held(&nf->lock);
2180
2181 ssam_dbg(ctrl, "enabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
2182 reg.target_category, id.target_category, id.instance, entry->refcount);
2183
2184 if (entry->refcount == 1) {
2185 status = ssam_ssh_event_enable(ctrl, reg, id, flags);
2186 if (status)
2187 return status;
2188
2189 entry->flags = flags;
2190
2191 } else if (entry->flags != flags) {
2192 ssam_warn(ctrl,
2193 "inconsistent flags when enabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
2194 flags, entry->flags, reg.target_category, id.target_category,
2195 id.instance);
2196 }
2197
2198 return 0;
2199}
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225static int ssam_nf_refcount_disable_free(struct ssam_controller *ctrl,
2226 struct ssam_nf_refcount_entry *entry, u8 flags)
2227{
2228 const struct ssam_event_registry reg = entry->key.reg;
2229 const struct ssam_event_id id = entry->key.id;
2230 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2231 int status = 0;
2232
2233 lockdep_assert_held(&nf->lock);
2234
2235 ssam_dbg(ctrl, "disabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
2236 reg.target_category, id.target_category, id.instance, entry->refcount);
2237
2238 if (entry->flags != flags) {
2239 ssam_warn(ctrl,
2240 "inconsistent flags when disabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
2241 flags, entry->flags, reg.target_category, id.target_category,
2242 id.instance);
2243 }
2244
2245 if (entry->refcount == 0) {
2246 status = ssam_ssh_event_disable(ctrl, reg, id, flags);
2247 kfree(entry);
2248 }
2249
2250 return status;
2251}
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275int ssam_notifier_register(struct ssam_controller *ctrl, struct ssam_event_notifier *n)
2276{
2277 u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
2278 struct ssam_nf_refcount_entry *entry = NULL;
2279 struct ssam_nf_head *nf_head;
2280 struct ssam_nf *nf;
2281 int status;
2282
2283 if (!ssh_rqid_is_event(rqid))
2284 return -EINVAL;
2285
2286 nf = &ctrl->cplt.event.notif;
2287 nf_head = &nf->head[ssh_rqid_to_event(rqid)];
2288
2289 mutex_lock(&nf->lock);
2290
2291 if (!(n->flags & SSAM_EVENT_NOTIFIER_OBSERVER)) {
2292 entry = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
2293 if (IS_ERR(entry)) {
2294 mutex_unlock(&nf->lock);
2295 return PTR_ERR(entry);
2296 }
2297 }
2298
2299 status = ssam_nfblk_insert(nf_head, &n->base);
2300 if (status) {
2301 if (entry)
2302 ssam_nf_refcount_dec_free(nf, n->event.reg, n->event.id);
2303
2304 mutex_unlock(&nf->lock);
2305 return status;
2306 }
2307
2308 if (entry) {
2309 status = ssam_nf_refcount_enable(ctrl, entry, n->event.flags);
2310 if (status) {
2311 ssam_nfblk_remove(&n->base);
2312 ssam_nf_refcount_dec_free(nf, n->event.reg, n->event.id);
2313 mutex_unlock(&nf->lock);
2314 synchronize_srcu(&nf_head->srcu);
2315 return status;
2316 }
2317 }
2318
2319 mutex_unlock(&nf->lock);
2320 return 0;
2321}
2322EXPORT_SYMBOL_GPL(ssam_notifier_register);
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338int ssam_notifier_unregister(struct ssam_controller *ctrl, struct ssam_event_notifier *n)
2339{
2340 u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
2341 struct ssam_nf_refcount_entry *entry;
2342 struct ssam_nf_head *nf_head;
2343 struct ssam_nf *nf;
2344 int status = 0;
2345
2346 if (!ssh_rqid_is_event(rqid))
2347 return -EINVAL;
2348
2349 nf = &ctrl->cplt.event.notif;
2350 nf_head = &nf->head[ssh_rqid_to_event(rqid)];
2351
2352 mutex_lock(&nf->lock);
2353
2354 if (!ssam_nfblk_find(nf_head, &n->base)) {
2355 mutex_unlock(&nf->lock);
2356 return -ENOENT;
2357 }
2358
2359
2360
2361
2362
2363 if (!(n->flags & SSAM_EVENT_NOTIFIER_OBSERVER)) {
2364 entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
2365 if (WARN_ON(!entry)) {
2366
2367
2368
2369
2370
2371
2372 status = -ENOENT;
2373 goto remove;
2374 }
2375
2376 status = ssam_nf_refcount_disable_free(ctrl, entry, n->event.flags);
2377 }
2378
2379remove:
2380 ssam_nfblk_remove(&n->base);
2381 mutex_unlock(&nf->lock);
2382 synchronize_srcu(&nf_head->srcu);
2383
2384 return status;
2385}
2386EXPORT_SYMBOL_GPL(ssam_notifier_unregister);
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409int ssam_controller_event_enable(struct ssam_controller *ctrl,
2410 struct ssam_event_registry reg,
2411 struct ssam_event_id id, u8 flags)
2412{
2413 u16 rqid = ssh_tc_to_rqid(id.target_category);
2414 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2415 struct ssam_nf_refcount_entry *entry;
2416 int status;
2417
2418 if (!ssh_rqid_is_event(rqid))
2419 return -EINVAL;
2420
2421 mutex_lock(&nf->lock);
2422
2423 entry = ssam_nf_refcount_inc(nf, reg, id);
2424 if (IS_ERR(entry)) {
2425 mutex_unlock(&nf->lock);
2426 return PTR_ERR(entry);
2427 }
2428
2429 status = ssam_nf_refcount_enable(ctrl, entry, flags);
2430 if (status) {
2431 ssam_nf_refcount_dec_free(nf, reg, id);
2432 mutex_unlock(&nf->lock);
2433 return status;
2434 }
2435
2436 mutex_unlock(&nf->lock);
2437 return 0;
2438}
2439EXPORT_SYMBOL_GPL(ssam_controller_event_enable);
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460int ssam_controller_event_disable(struct ssam_controller *ctrl,
2461 struct ssam_event_registry reg,
2462 struct ssam_event_id id, u8 flags)
2463{
2464 u16 rqid = ssh_tc_to_rqid(id.target_category);
2465 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2466 struct ssam_nf_refcount_entry *entry;
2467 int status;
2468
2469 if (!ssh_rqid_is_event(rqid))
2470 return -EINVAL;
2471
2472 mutex_lock(&nf->lock);
2473
2474 entry = ssam_nf_refcount_dec(nf, reg, id);
2475 if (!entry) {
2476 mutex_unlock(&nf->lock);
2477 return -ENOENT;
2478 }
2479
2480 status = ssam_nf_refcount_disable_free(ctrl, entry, flags);
2481
2482 mutex_unlock(&nf->lock);
2483 return status;
2484}
2485EXPORT_SYMBOL_GPL(ssam_controller_event_disable);
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508int ssam_notifier_disable_registered(struct ssam_controller *ctrl)
2509{
2510 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2511 struct rb_node *n;
2512 int status;
2513
2514 mutex_lock(&nf->lock);
2515 for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
2516 struct ssam_nf_refcount_entry *e;
2517
2518 e = rb_entry(n, struct ssam_nf_refcount_entry, node);
2519 status = ssam_ssh_event_disable(ctrl, e->key.reg,
2520 e->key.id, e->flags);
2521 if (status)
2522 goto err;
2523 }
2524 mutex_unlock(&nf->lock);
2525
2526 return 0;
2527
2528err:
2529 for (n = rb_prev(n); n; n = rb_prev(n)) {
2530 struct ssam_nf_refcount_entry *e;
2531
2532 e = rb_entry(n, struct ssam_nf_refcount_entry, node);
2533 ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
2534 }
2535 mutex_unlock(&nf->lock);
2536
2537 return status;
2538}
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553void ssam_notifier_restore_registered(struct ssam_controller *ctrl)
2554{
2555 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2556 struct rb_node *n;
2557
2558 mutex_lock(&nf->lock);
2559 for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
2560 struct ssam_nf_refcount_entry *e;
2561
2562 e = rb_entry(n, struct ssam_nf_refcount_entry, node);
2563
2564
2565 ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
2566 }
2567 mutex_unlock(&nf->lock);
2568}
2569
2570
2571
2572
2573
2574
2575
2576
2577static bool ssam_notifier_is_empty(struct ssam_controller *ctrl)
2578{
2579 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2580 bool result;
2581
2582 mutex_lock(&nf->lock);
2583 result = ssam_nf_refcount_empty(nf);
2584 mutex_unlock(&nf->lock);
2585
2586 return result;
2587}
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598static void ssam_notifier_unregister_all(struct ssam_controller *ctrl)
2599{
2600 struct ssam_nf *nf = &ctrl->cplt.event.notif;
2601 struct ssam_nf_refcount_entry *e, *n;
2602
2603 mutex_lock(&nf->lock);
2604 rbtree_postorder_for_each_entry_safe(e, n, &nf->refcount, node) {
2605
2606 ssam_ssh_event_disable(ctrl, e->key.reg, e->key.id, e->flags);
2607 kfree(e);
2608 }
2609 nf->refcount = RB_ROOT;
2610 mutex_unlock(&nf->lock);
2611}
2612
2613
2614
2615
2616static irqreturn_t ssam_irq_handle(int irq, void *dev_id)
2617{
2618 struct ssam_controller *ctrl = dev_id;
2619
2620 ssam_dbg(ctrl, "pm: wake irq triggered\n");
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643 return IRQ_HANDLED;
2644}
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678int ssam_irq_setup(struct ssam_controller *ctrl)
2679{
2680 struct device *dev = ssam_controller_device(ctrl);
2681 struct gpio_desc *gpiod;
2682 int irq;
2683 int status;
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696 const int irqf = IRQF_ONESHOT | IRQF_TRIGGER_RISING | IRQF_NO_AUTOEN;
2697
2698 gpiod = gpiod_get(dev, "ssam_wakeup-int", GPIOD_ASIS);
2699 if (IS_ERR(gpiod))
2700 return PTR_ERR(gpiod);
2701
2702 irq = gpiod_to_irq(gpiod);
2703 gpiod_put(gpiod);
2704
2705 if (irq < 0)
2706 return irq;
2707
2708 status = request_threaded_irq(irq, NULL, ssam_irq_handle, irqf,
2709 "ssam_wakeup", ctrl);
2710 if (status)
2711 return status;
2712
2713 ctrl->irq.num = irq;
2714 return 0;
2715}
2716
2717
2718
2719
2720
2721
2722
2723void ssam_irq_free(struct ssam_controller *ctrl)
2724{
2725 free_irq(ctrl->irq.num, ctrl);
2726 ctrl->irq.num = -1;
2727}
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl)
2744{
2745 struct device *dev = ssam_controller_device(ctrl);
2746 int status;
2747
2748 enable_irq(ctrl->irq.num);
2749 if (device_may_wakeup(dev)) {
2750 status = enable_irq_wake(ctrl->irq.num);
2751 if (status) {
2752 ssam_err(ctrl, "failed to enable wake IRQ: %d\n", status);
2753 disable_irq(ctrl->irq.num);
2754 return status;
2755 }
2756
2757 ctrl->irq.wakeup_enabled = true;
2758 } else {
2759 ctrl->irq.wakeup_enabled = false;
2760 }
2761
2762 return 0;
2763}
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
2777{
2778 int status;
2779
2780 if (ctrl->irq.wakeup_enabled) {
2781 status = disable_irq_wake(ctrl->irq.num);
2782 if (status)
2783 ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
2784
2785 ctrl->irq.wakeup_enabled = false;
2786 }
2787 disable_irq(ctrl->irq.num);
2788}
2789