-
Notifications
You must be signed in to change notification settings - Fork 172
/
mod_scmi_clock.c
1641 lines (1382 loc) · 48.3 KB
/
mod_scmi_clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Arm SCP/MCP Software
* Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Description:
* SCMI Clock Management Protocol Support.
*/
#include <internal/scmi_clock.h>
#include <mod_clock.h>
#include <mod_scmi.h>
#include <mod_scmi_clock.h>
#include <fwk_assert.h>
#include <fwk_attributes.h>
#include <fwk_core.h>
#include <fwk_event.h>
#include <fwk_id.h>
#include <fwk_log.h>
#include <fwk_macros.h>
#include <fwk_mm.h>
#include <fwk_module.h>
#include <fwk_module_idx.h>
#include <fwk_status.h>
#include <fwk_string.h>
#include <stdbool.h>
#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
# include <mod_resource_perms.h>
#endif
struct clock_operations {
/*
* Service identifier currently requesting operation from this clock.
* A 'none' value indicates that there is no pending request.
*/
fwk_id_t service_id;
/*
* The state to be set in this operation.
*/
enum mod_clock_state state;
/*
* The clock device for this operation.
*/
uint32_t clock_dev_id;
/*
* Request type for this operation.
*/
enum scmi_clock_request_type request;
};
struct mod_scmi_clock_ctx {
/*! SCMI Clock Module Configuration */
const struct mod_scmi_clock_config *config;
/*! Maximum supported number of pending, asynchronous clock rate changes */
uint8_t max_pending_transactions;
/*!
* Pointer to the table of agent descriptors, used to provide per-agent
* views of clocks in the system.
*/
const struct mod_scmi_clock_agent *agent_table;
/* SCMI module API */
const struct mod_scmi_from_protocol_api *scmi_api;
/* Clock module API */
const struct mod_clock_api *clock_api;
/* Number of clock devices */
int clock_devices;
/* Pointer to a table of clock operations */
struct clock_operations *clock_ops;
#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
/* SCMI Resource Permissions API */
const struct mod_res_permissions_api *res_perms_api;
#endif
};
static const fwk_id_t mod_scmi_clock_event_id_get_state =
FWK_ID_EVENT_INIT(FWK_MODULE_IDX_SCMI_CLOCK,
SCMI_CLOCK_EVENT_IDX_GET_STATE);
static const fwk_id_t mod_scmi_clock_event_id_get_rate =
FWK_ID_EVENT_INIT(FWK_MODULE_IDX_SCMI_CLOCK,
SCMI_CLOCK_EVENT_IDX_GET_RATE);
static const fwk_id_t mod_scmi_clock_event_id_set_rate =
FWK_ID_EVENT_INIT(FWK_MODULE_IDX_SCMI_CLOCK,
SCMI_CLOCK_EVENT_IDX_SET_RATE);
static const fwk_id_t mod_scmi_clock_event_id_set_state =
FWK_ID_EVENT_INIT(FWK_MODULE_IDX_SCMI_CLOCK,
SCMI_CLOCK_EVENT_IDX_SET_STATE);
/*
* SCMI Clock Message Handlers
*/
static int scmi_clock_protocol_version_handler(fwk_id_t service_id,
const uint32_t *payload);
static int scmi_clock_protocol_attributes_handler(fwk_id_t service_id,
const uint32_t *payload);
static int scmi_clock_protocol_message_attributes_handler(
fwk_id_t service_id, const uint32_t *payload);
static int scmi_clock_attributes_handler(fwk_id_t service_id,
const uint32_t *payload);
static int scmi_clock_rate_get_handler(fwk_id_t service_id,
const uint32_t *payload);
static int scmi_clock_rate_set_handler(fwk_id_t service_id,
const uint32_t *payload);
static int scmi_clock_config_set_handler(fwk_id_t service_id,
const uint32_t *payload);
static int scmi_clock_describe_rates_handler(fwk_id_t service_id,
const uint32_t *payload);
/*
* Internal variables.
*/
static struct mod_scmi_clock_ctx scmi_clock_ctx;
static int (*const handler_table[MOD_SCMI_CLOCK_COMMAND_COUNT])(
fwk_id_t,
const uint32_t *) = {
[MOD_SCMI_PROTOCOL_VERSION] = scmi_clock_protocol_version_handler,
[MOD_SCMI_PROTOCOL_ATTRIBUTES] = scmi_clock_protocol_attributes_handler,
[MOD_SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] =
scmi_clock_protocol_message_attributes_handler,
[MOD_SCMI_CLOCK_ATTRIBUTES] = scmi_clock_attributes_handler,
[MOD_SCMI_CLOCK_RATE_GET] = scmi_clock_rate_get_handler,
[MOD_SCMI_CLOCK_RATE_SET] = scmi_clock_rate_set_handler,
[MOD_SCMI_CLOCK_CONFIG_SET] = scmi_clock_config_set_handler,
[MOD_SCMI_CLOCK_DESCRIBE_RATES] = scmi_clock_describe_rates_handler,
};
static const unsigned int payload_size_table[MOD_SCMI_CLOCK_COMMAND_COUNT] = {
[MOD_SCMI_PROTOCOL_VERSION] = 0,
[MOD_SCMI_PROTOCOL_ATTRIBUTES] = 0,
[MOD_SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] =
(unsigned int)sizeof(struct scmi_protocol_message_attributes_a2p),
[MOD_SCMI_CLOCK_ATTRIBUTES] =
(unsigned int)sizeof(struct scmi_clock_attributes_a2p),
[MOD_SCMI_CLOCK_RATE_GET] =
(unsigned int)sizeof(struct scmi_clock_rate_get_a2p),
[MOD_SCMI_CLOCK_RATE_SET] =
(unsigned int)sizeof(struct scmi_clock_rate_set_a2p),
[MOD_SCMI_CLOCK_CONFIG_SET] =
(unsigned int)sizeof(struct scmi_clock_config_set_a2p),
[MOD_SCMI_CLOCK_DESCRIBE_RATES] =
(unsigned int)sizeof(struct scmi_clock_describe_rates_a2p),
};
/*
* Given a service identifier, retrieve a pointer to its agent's
* \c mod_scmi_clock_agent structure within the agent table.
*/
static int get_agent_entry(
fwk_id_t service_id,
const struct mod_scmi_clock_agent **agent)
{
int status;
unsigned int agent_id;
if (agent == NULL) {
return FWK_E_PARAM;
}
status = scmi_clock_ctx.scmi_api->get_agent_id(service_id, &agent_id);
if (status != FWK_SUCCESS) {
return status;
}
if (agent_id >= scmi_clock_ctx.config->agent_count) {
return FWK_E_PARAM;
}
*agent =
(struct mod_scmi_clock_agent *)&scmi_clock_ctx.agent_table[agent_id];
return FWK_SUCCESS;
}
/*
* Given a service identifier and a clock index, retrieve a pointer to the
* clock's \c mod_scmi_clock_device structure within the agent's device table.
* Optionally, a pointer to the agent may be retrieved as well.
*/
static int get_clock_device_entry(
fwk_id_t service_id,
unsigned int clock_idx,
const struct mod_scmi_clock_device **clock_device,
const struct mod_scmi_clock_agent **agent)
{
int status;
const struct mod_scmi_clock_agent *agent_entry;
if (clock_device == NULL) {
return FWK_E_PARAM;
}
status = get_agent_entry(service_id, &agent_entry);
if (status != FWK_SUCCESS) {
return status;
}
if (clock_idx >= agent_entry->device_count) {
return FWK_E_RANGE;
}
*clock_device = &agent_entry->device_table[clock_idx];
fwk_assert(fwk_module_is_valid_element_id((*clock_device)->element_id));
if (agent != NULL) {
*agent = agent_entry;
}
return FWK_SUCCESS;
}
/*
* Given a service identifier and a clock index, retrieve the state
* for that agent:clock
*/
static int get_agent_clock_state(
uint8_t *state,
fwk_id_t service_id,
unsigned int clock_idx,
uint8_t *agent_clock_state)
{
unsigned int agent_id;
const struct mod_scmi_clock_agent *agent_entry;
int idx;
int status;
status = get_agent_entry(service_id, &agent_entry);
if (status != FWK_SUCCESS) {
return status;
}
if (clock_idx >= agent_entry->device_count) {
return FWK_E_RANGE;
}
status = scmi_clock_ctx.scmi_api->get_agent_id(service_id, &agent_id);
if (status != FWK_SUCCESS) {
return status;
}
idx = (int)((agent_id * scmi_clock_ctx.clock_devices) + clock_idx);
*state = agent_clock_state[idx];
return FWK_SUCCESS;
}
/*
* Given a service identifier and a clock index, set the state
* for that agent:clock
*/
static int set_agent_clock_state(
uint8_t *agent_clock_state,
uint8_t state,
fwk_id_t service_id,
unsigned int clock_idx)
{
unsigned int agent_id;
const struct mod_scmi_clock_agent *agent_entry;
int idx;
int status;
status = get_agent_entry(service_id, &agent_entry);
if (status != FWK_SUCCESS) {
return status;
}
if (clock_idx >= agent_entry->device_count) {
return FWK_E_RANGE;
}
status = scmi_clock_ctx.scmi_api->get_agent_id(service_id, &agent_id);
if (status != FWK_SUCCESS) {
return status;
}
idx = (int)((agent_id * scmi_clock_ctx.clock_devices) + clock_idx);
agent_clock_state[idx] = state;
return FWK_SUCCESS;
}
/*
* Helper for clock operations
*/
static void clock_ops_set_busy(
unsigned int clock_dev_idx,
fwk_id_t service_id,
uint32_t clock_dev_id,
enum mod_clock_state state,
enum scmi_clock_request_type request)
{
scmi_clock_ctx.clock_ops[clock_dev_idx].service_id = service_id;
scmi_clock_ctx.clock_ops[clock_dev_idx].state = state;
scmi_clock_ctx.clock_ops[clock_dev_idx].clock_dev_id = clock_dev_id;
scmi_clock_ctx.clock_ops[clock_dev_idx].request = request;
}
static void clock_ops_update_state(unsigned int clock_dev_idx, int status)
{
enum mod_scmi_clock_policy_status policy_status;
int set_policy_status;
if ((status == FWK_SUCCESS) &&
(scmi_clock_ctx.clock_ops[clock_dev_idx].request ==
SCMI_CLOCK_REQUEST_SET_STATE)) {
set_policy_status = mod_scmi_clock_config_set_policy(
&policy_status,
&scmi_clock_ctx.clock_ops[clock_dev_idx].state,
MOD_SCMI_CLOCK_POST_MESSAGE_HANDLER,
scmi_clock_ctx.clock_ops[clock_dev_idx].service_id,
scmi_clock_ctx.clock_ops[clock_dev_idx].clock_dev_id);
if (set_policy_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
}
}
static inline void clock_ops_set_available(unsigned int clock_dev_idx)
{
scmi_clock_ctx.clock_ops[clock_dev_idx].service_id = FWK_ID_NONE;
}
static inline fwk_id_t clock_ops_get_service(unsigned int clock_dev_idx)
{
return scmi_clock_ctx.clock_ops[clock_dev_idx].service_id;
}
static inline bool clock_ops_is_available(unsigned int clock_dev_idx)
{
return fwk_id_is_equal(scmi_clock_ctx.clock_ops[clock_dev_idx].service_id,
FWK_ID_NONE);
}
/*
* Helper for the 'get_state' response
*/
static void get_state_respond(fwk_id_t clock_dev_id,
fwk_id_t service_id,
enum mod_clock_state *clock_state,
int status)
{
int respond_status;
size_t response_size;
struct scmi_clock_attributes_p2a return_values = { 0 };
if (status == FWK_SUCCESS) {
return_values.attributes = SCMI_CLOCK_ATTRIBUTES(
(uint32_t)(*clock_state == MOD_CLOCK_STATE_RUNNING));
fwk_str_strncpy(
return_values.clock_name,
fwk_module_get_element_name(clock_dev_id),
sizeof(return_values.clock_name) - 1);
return_values.status = (int32_t)SCMI_SUCCESS;
response_size = sizeof(return_values);
} else {
return_values.status = (int32_t)SCMI_GENERIC_ERROR;
response_size = sizeof(return_values.status);
}
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, response_size);
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
}
/*
* Helper for the 'get_rate' response
*/
static void get_rate_respond(fwk_id_t service_id,
uint64_t *rate,
int status)
{
int respond_status;
size_t response_size;
struct scmi_clock_rate_get_p2a return_values = { 0 };
if (status == FWK_SUCCESS) {
return_values.rate[0] = (uint32_t)*rate;
return_values.rate[1] = (uint32_t)(*rate >> 32);
return_values.status = (int32_t)SCMI_SUCCESS;
response_size = sizeof(return_values);
} else {
return_values.status = (int32_t)SCMI_GENERIC_ERROR;
response_size = sizeof(return_values.status);
}
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, response_size);
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
}
static void request_response(int response_status,
fwk_id_t service_id)
{
int respond_status;
struct scmi_clock_generic_p2a return_values = {
.status = (int32_t)SCMI_GENERIC_ERROR
};
if (response_status == FWK_E_SUPPORT) {
return_values.status = (int32_t)SCMI_NOT_SUPPORTED;
}
if (response_status == FWK_E_RANGE) {
return_values.status = (int32_t)SCMI_INVALID_PARAMETERS;
}
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, sizeof(return_values.status));
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
}
/*
* Helper for the 'set_' responses
*/
static void set_request_respond(fwk_id_t service_id, int status)
{
int respond_status;
struct scmi_clock_generic_p2a return_values = { 0 };
if (status == FWK_E_RANGE || status == FWK_E_PARAM) {
return_values.status = (int32_t)SCMI_INVALID_PARAMETERS;
} else if (status == FWK_E_SUPPORT) {
return_values.status = (int32_t)SCMI_NOT_SUPPORTED;
} else if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_GENERIC_ERROR;
} else {
return_values.status = (int32_t)SCMI_SUCCESS;
}
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, sizeof(return_values.status));
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
}
FWK_WEAK int mod_scmi_clock_rate_set_policy(
enum mod_scmi_clock_policy_status *policy_status,
enum mod_clock_round_mode *round_mode,
uint64_t *rate,
enum mod_scmi_clock_policy_commit policy_commit,
fwk_id_t service_id,
uint32_t clock_dev_id)
{
*policy_status = MOD_SCMI_CLOCK_EXECUTE_MESSAGE_HANDLER;
return FWK_SUCCESS;
}
FWK_WEAK int mod_scmi_clock_config_set_policy(
enum mod_scmi_clock_policy_status *policy_status,
enum mod_clock_state *state,
enum mod_scmi_clock_policy_commit policy_commit,
fwk_id_t service_id,
uint32_t clock_dev_id)
{
/* Pointer to a table of clock reference counts */
static uint8_t *clock_count = NULL;
/* Pointer to a table of agent:clock states */
static uint8_t *agent_clock_state = NULL;
const struct mod_scmi_clock_agent *agent;
unsigned int agent_id, clock_id, idx;
int status = FWK_SUCCESS;
uint8_t clock_state;
*policy_status = MOD_SCMI_CLOCK_SKIP_MESSAGE_HANDLER;
if (agent_clock_state == NULL) {
/* Allocate table of agent:clock states */
agent_clock_state = fwk_mm_calloc(
(unsigned int)scmi_clock_ctx.config->agent_count *
(unsigned int)scmi_clock_ctx.clock_devices,
sizeof(uint8_t));
/* Allocate table of clock reference counts */
clock_count = fwk_mm_calloc(
(unsigned int)scmi_clock_ctx.clock_devices, sizeof(uint32_t));
/* Set all clocks as running if required */
for (agent_id = 0;
agent_id < (unsigned int)scmi_clock_ctx.config->agent_count;
agent_id++) {
agent = &scmi_clock_ctx.agent_table[agent_id];
for (clock_id = 0; clock_id < (unsigned int)agent->device_count;
clock_id++) {
if (agent->device_table[clock_id].starts_enabled) {
idx = (agent_id * scmi_clock_ctx.clock_devices) + clock_id;
agent_clock_state[idx] = (uint8_t)MOD_CLOCK_STATE_RUNNING;
clock_count[clock_id]++;
}
}
}
}
/*
* clock state is the last state this agent successfully set
* for this clock.
*/
status = get_agent_clock_state(
&clock_state, service_id, clock_dev_id, agent_clock_state);
if (status != FWK_SUCCESS) {
return status;
}
switch (*state) {
case MOD_CLOCK_STATE_RUNNING:
/* The agent has already requested to set state to RUNNING */
if (clock_state == MOD_CLOCK_STATE_RUNNING) {
return FWK_SUCCESS;
}
/* set the clock state for this agent to RUNNING */
if (policy_commit == MOD_SCMI_CLOCK_POST_MESSAGE_HANDLER) {
status = set_agent_clock_state(
agent_clock_state,
(uint8_t)MOD_CLOCK_STATE_RUNNING,
service_id,
clock_dev_id);
if (status != FWK_SUCCESS) {
return status;
}
}
/*
* Only allow the clock to be stopped if clock_count == 0
* before being updated for this call.
*
* This is the first agent to set the clock RUNNING.
*/
if (clock_count[clock_dev_id] != 0) {
status = FWK_E_STATE;
}
if (policy_commit == MOD_SCMI_CLOCK_POST_MESSAGE_HANDLER) {
clock_count[clock_dev_id]++;
}
break;
case MOD_CLOCK_STATE_STOPPED:
/* The agent has already requested to set state to STOPPED */
if (clock_state == MOD_CLOCK_STATE_STOPPED) {
return FWK_SUCCESS;
}
/* error to try and stop a stopped clock */
if (clock_count[clock_dev_id] == 0) {
/*
* Here we ignore whether the agent_id will be correctly returned as
* we will return an error regardless
*/
(void)scmi_clock_ctx.scmi_api->get_agent_id(service_id, &agent_id);
FWK_LOG_WARN(
"[SCMI-CLK] Invalid STOP request agent:"
" %d clock_id: %d state:%d\n",
(int)agent_id,
(int)clock_dev_id,
*state);
return FWK_E_STATE;
}
/* set the clock state for this agent to STOPPED */
if (policy_commit == MOD_SCMI_CLOCK_POST_MESSAGE_HANDLER) {
status = set_agent_clock_state(
agent_clock_state,
(uint8_t)MOD_CLOCK_STATE_STOPPED,
service_id,
clock_dev_id);
if (status != FWK_SUCCESS) {
return status;
}
}
/*
* Only allow the clock to be stopped if ref_clk_count == 0
* after being updated for this call.
*
* This is the last agent to set the clock STOPPED.
*/
if (clock_count[clock_dev_id] != 1) {
status = FWK_E_STATE;
}
if (policy_commit == MOD_SCMI_CLOCK_POST_MESSAGE_HANDLER) {
clock_count[clock_dev_id]--;
}
break;
default:
return FWK_E_PARAM;
}
if (status == FWK_SUCCESS) {
*policy_status = MOD_SCMI_CLOCK_EXECUTE_MESSAGE_HANDLER;
}
return FWK_SUCCESS;
}
#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
static int scmi_clock_permissions_handler(
uint32_t clock_id,
fwk_id_t service_id,
unsigned int message_id)
{
enum mod_res_perms_permissions perms;
unsigned int agent_id;
int status;
status = scmi_clock_ctx.scmi_api->get_agent_id(service_id, &agent_id);
if (status != FWK_SUCCESS) {
return FWK_E_ACCESS;
}
if (message_id < 3) {
perms = scmi_clock_ctx.res_perms_api->agent_has_protocol_permission(
agent_id, MOD_SCMI_PROTOCOL_ID_CLOCK);
if (perms == MOD_RES_PERMS_ACCESS_ALLOWED) {
return FWK_SUCCESS;
}
return FWK_E_ACCESS;
}
perms = scmi_clock_ctx.res_perms_api->agent_has_resource_permission(
agent_id, MOD_SCMI_PROTOCOL_ID_CLOCK, message_id, clock_id);
if (perms == MOD_RES_PERMS_ACCESS_ALLOWED) {
return FWK_SUCCESS;
} else {
return FWK_E_ACCESS;
}
}
#endif
/*
* Helper to create events for processing pending requests
*/
static int create_event_request(
fwk_id_t clock_id,
fwk_id_t service_id,
enum scmi_clock_request_type request,
void *data,
uint32_t clock_idx)
{
int status;
union event_request_data request_data;
unsigned int clock_dev_idx = fwk_id_get_element_idx(clock_id);
struct scmi_clock_event_request_params *params;
enum mod_clock_state state = MOD_CLOCK_STATE_COUNT;
if (!clock_ops_is_available(clock_dev_idx)) {
return FWK_E_BUSY;
}
struct fwk_event event = {
.target_id = fwk_module_id_scmi_clock,
};
params = (struct scmi_clock_event_request_params *)event.params;
switch (request) {
case SCMI_CLOCK_REQUEST_GET_STATE:
event.id = mod_scmi_clock_event_id_get_state;
break;
case SCMI_CLOCK_REQUEST_GET_RATE:
event.id = mod_scmi_clock_event_id_get_rate;
break;
case SCMI_CLOCK_REQUEST_SET_RATE:
{
struct event_set_rate_request_data *rate_data =
(struct event_set_rate_request_data *)data;
request_data.set_rate_data.rate[0] = rate_data->rate[0];
request_data.set_rate_data.rate[1] = rate_data->rate[1];
request_data.set_rate_data.round_mode = rate_data->round_mode;
params->request_data = request_data;
}
event.id = mod_scmi_clock_event_id_set_rate;
break;
case SCMI_CLOCK_REQUEST_SET_STATE:
{
struct event_set_state_request_data *state_data =
(struct event_set_state_request_data *)data;
request_data.set_state_data.state = state_data->state;
state = state_data->state;
params->request_data = request_data;
}
event.id = mod_scmi_clock_event_id_set_state;
break;
default:
return FWK_E_PARAM;
}
params->clock_dev_id = clock_id;
status = fwk_put_event(&event);
if (status != FWK_SUCCESS) {
return status;
}
clock_ops_set_busy(clock_dev_idx, service_id, clock_idx, state, request);
return FWK_SUCCESS;
}
/*
* Protocol Version
*/
static int scmi_clock_protocol_version_handler(fwk_id_t service_id,
const uint32_t *payload)
{
struct scmi_protocol_version_p2a return_values = {
.status = (int32_t)SCMI_SUCCESS,
.version = SCMI_PROTOCOL_VERSION_CLOCK,
};
return scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, sizeof(return_values));
}
/*
* Protocol Attributes
*/
static int scmi_clock_protocol_attributes_handler(fwk_id_t service_id,
const uint32_t *payload)
{
int status, respond_status;
const struct mod_scmi_clock_agent *agent;
struct scmi_protocol_attributes_p2a return_values = {
.status = (int32_t)SCMI_SUCCESS,
};
status = get_agent_entry(service_id, &agent);
if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_GENERIC_ERROR;
goto exit;
}
return_values.attributes = SCMI_CLOCK_PROTOCOL_ATTRIBUTES(
scmi_clock_ctx.max_pending_transactions,
agent->device_count
);
exit:
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, sizeof(return_values));
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
return status;
}
/*
* Protocol Message Attributes
*/
static int scmi_clock_protocol_message_attributes_handler(fwk_id_t service_id,
const uint32_t *payload)
{
size_t response_size;
const struct scmi_protocol_message_attributes_a2p *parameters;
unsigned int message_id;
struct scmi_protocol_message_attributes_p2a return_values = {
.status = (int32_t)SCMI_SUCCESS,
.attributes = 0,
};
parameters = (const struct scmi_protocol_message_attributes_a2p*)
payload;
message_id = parameters->message_id;
if ((message_id >= FWK_ARRAY_SIZE(handler_table)) ||
(handler_table[message_id] == NULL)) {
return_values.status = (int32_t)SCMI_NOT_FOUND;
goto exit;
}
exit:
response_size = (return_values.status == SCMI_SUCCESS) ?
sizeof(return_values) : sizeof(return_values.status);
return scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, response_size);
}
/*
* Clock Attributes
*/
static int scmi_clock_attributes_handler(fwk_id_t service_id,
const uint32_t *payload)
{
int status, respond_status;
const struct mod_scmi_clock_agent *agent;
const struct mod_scmi_clock_device *clock_device;
size_t response_size;
const struct scmi_clock_attributes_a2p *parameters;
struct scmi_clock_attributes_p2a return_values = {
.status = (int32_t)SCMI_GENERIC_ERROR
};
parameters = (const struct scmi_clock_attributes_a2p*)payload;
status = get_clock_device_entry(service_id,
parameters->clock_id,
&clock_device,
&agent);
if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_NOT_FOUND;
goto exit;
}
#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
status = scmi_clock_permissions_handler(
parameters->clock_id,
service_id,
(unsigned int)MOD_SCMI_CLOCK_ATTRIBUTES);
if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_DENIED;
goto exit;
}
#endif
status = create_event_request(
clock_device->element_id,
service_id,
SCMI_CLOCK_REQUEST_GET_STATE,
NULL,
parameters->clock_id);
if (status == FWK_E_BUSY) {
return_values.status = (int32_t)SCMI_BUSY;
status = FWK_SUCCESS;
goto exit;
}
if (status != FWK_SUCCESS) {
goto exit;
}
return FWK_SUCCESS;
exit:
response_size = (return_values.status == SCMI_SUCCESS) ?
sizeof(return_values) : sizeof(return_values.status);
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, response_size);
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
return status;
}
/*
* Clock Rate Get
*/
static int scmi_clock_rate_get_handler(fwk_id_t service_id,
const uint32_t *payload)
{
int status, respond_status;
const struct mod_scmi_clock_agent *agent;
const struct mod_scmi_clock_device *clock_device;
size_t response_size;
const struct scmi_clock_rate_get_a2p *parameters;
struct scmi_clock_rate_get_p2a return_values = {
.status = (int32_t)SCMI_GENERIC_ERROR
};
parameters = (const struct scmi_clock_rate_get_a2p*)payload;
status = get_clock_device_entry(service_id,
parameters->clock_id,
&clock_device,
&agent);
if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_NOT_FOUND;
goto exit;
}
#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
status = scmi_clock_permissions_handler(
parameters->clock_id,
service_id,
(unsigned int)MOD_SCMI_CLOCK_RATE_GET);
if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_DENIED;
goto exit;
}
#endif
status = create_event_request(
clock_device->element_id,
service_id,
SCMI_CLOCK_REQUEST_GET_RATE,
NULL,
parameters->clock_id);
if (status == FWK_E_BUSY) {
return_values.status = (int32_t)SCMI_BUSY;
status = FWK_SUCCESS;
goto exit;
}
if (status != FWK_SUCCESS) {
goto exit;
}
return FWK_SUCCESS;
exit:
response_size = (return_values.status == SCMI_SUCCESS) ?
sizeof(return_values) : sizeof(return_values.status);
respond_status = scmi_clock_ctx.scmi_api->respond(
service_id, &return_values, response_size);
if (respond_status != FWK_SUCCESS) {
FWK_LOG_TRACE("[SCMI-CLK] %s @%d", __func__, __LINE__);
}
return status;
}
/*
* Clock Rate Set (Synchronous Only)
*/
static int scmi_clock_rate_set_handler(fwk_id_t service_id,
const uint32_t *payload)
{
int status;
const struct mod_scmi_clock_agent *agent;
const struct mod_scmi_clock_device *clock_device;
bool round_auto;
bool round_up;
bool asynchronous;
size_t response_size;
unsigned int agent_id;
uint64_t rate;
enum mod_clock_round_mode round_mode;
const struct scmi_clock_rate_set_a2p *parameters;
struct scmi_clock_rate_set_p2a return_values = {
.status = (int32_t)SCMI_GENERIC_ERROR
};
enum mod_scmi_clock_policy_status policy_status;
parameters = (const struct scmi_clock_rate_set_a2p*)payload;
round_up = (parameters->flags & SCMI_CLOCK_RATE_SET_ROUND_UP_MASK) != 0;
round_auto = (parameters->flags & SCMI_CLOCK_RATE_SET_ROUND_AUTO_MASK) != 0;
asynchronous = (parameters->flags & SCMI_CLOCK_RATE_SET_ASYNC_MASK) != 0;
if ((parameters->flags & ~SCMI_CLOCK_RATE_SET_FLAGS_MASK) != 0) {
return_values.status = (int32_t)SCMI_INVALID_PARAMETERS;
goto exit;
}
status = get_clock_device_entry(service_id,
parameters->clock_id,
&clock_device,
&agent);
if (status != FWK_SUCCESS) {
return_values.status = (int32_t)SCMI_NOT_FOUND;
goto exit;
}