Repository navigation
Expand file tree
/
Copy pathcppc_acpi.c
More file actions
3941 lines (3388 loc) · 110 KB
/
Copy pathcppc_acpi.c
File metadata and controls
3941 lines (3388 loc) · 110 KB
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
// SPDX-License-Identifier: GPL-2.0-only
/*
* CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
*
* (C) Copyright 2014, 2015 Linaro Ltd.
* Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
*
* CPPC describes a few methods for controlling CPU performance using
* information from a per CPU table called CPC. This table is described in
* the ACPI v5.0+ specification. The table consists of a list of
* registers which may be memory mapped or hardware registers and also may
* include some static integer values.
*
* CPU performance is on an abstract continuous scale as against a discretized
* P-state scale which is tied to CPU frequency only. In brief, the basic
* operation involves:
*
* - OS makes a CPU performance request. (Can provide min and max bounds)
*
* - Platform (such as BMC) is free to optimize request within requested bounds
* depending on power/thermal budgets etc.
*
* - Platform conveys its decision back to OS
*
* The communication between OS and platform occurs through another medium
* called (PCC) Platform Communication Channel. This is a generic mailbox like
* mechanism which includes doorbell semantics to indicate register updates.
* See drivers/mailbox/pcc.c for details on PCC.
*
* Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
* above specifications.
*/
#define pr_fmt(fmt) "ACPI CPPC: " fmt
#include <linux/delay.h>
#include <linux/interval_tree_generic.h>
#include <linux/iopoll.h>
#include <linux/ktime.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/rbtree.h>
#include <linux/rwsem.h>
#include <linux/wait.h>
#include <linux/topology.h>
#include <linux/dmi.h>
#include <linux/units.h>
#include <linux/unaligned.h>
#include <acpi/cppc_acpi.h>
struct cppc_pcc_data {
struct pcc_mbox_chan *pcc_channel;
bool pcc_channel_acquired;
unsigned int deadline_us;
unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
bool platform_owns_pcc; /* Ownership of PCC subspace */
unsigned int pcc_write_cnt; /* Running count of PCC write commands */
/*
* Lock to provide controlled access to the PCC channel.
*
* For performance critical usecases(currently cppc_set_perf)
* We need to take read_lock and check if channel belongs to OSPM
* before reading or writing to PCC subspace
* We need to take write_lock before transferring the channel
* ownership to the platform via a Doorbell
* This allows us to batch a number of CPPC requests if they happen
* to originate in about the same time
*
* For non-performance critical usecases(init)
* Take write_lock for all purposes which gives exclusive access
*/
struct rw_semaphore pcc_lock;
/* Serialize byte-oriented accesses to aliased PCC payload fields. */
raw_spinlock_t payload_lock;
/* Wait queue for CPUs whose requests were batched */
wait_queue_head_t pcc_write_wait_q;
ktime_t last_cmd_cmpl_time;
ktime_t last_mpar_reset;
int mpar_count;
int refcount;
};
/* Array to represent the PCC channel per subspace ID */
static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
static DEFINE_MUTEX(pcc_data_lock);
/* The cpu_pcc_subspace_idx contains per CPU subspace ID */
static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx);
/*
* The cpc_desc structure contains the ACPI register details
* as described in the per CPU _CPC tables. The details
* include the type of register (e.g. PCC, System IO, FFH etc.)
* and destination addresses which lets us READ/WRITE CPU performance
* information using the appropriate I/O methods.
*/
static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
/* Protect immutable capability queries against descriptor removal. */
static DEFINE_MUTEX(cpc_desc_lock);
static void cpc_set_desc(unsigned int cpu, struct cpc_desc *desc)
{
guard(mutex)(&cpc_desc_lock);
per_cpu(cpc_desc_ptr, cpu) = desc;
}
struct cpc_sysmem_node {
struct rb_node rb;
u64 subtree_last;
u64 start;
u64 last;
struct cpc_desc *desc;
unsigned int reg_idx;
struct list_head aliases;
struct list_head alias_node;
struct cpc_sysmem_node *alias_of;
bool registered;
};
struct cpc_non_mmio_node {
struct rb_node rb;
u64 subtree_last;
u64 start;
u64 last;
struct cpc_desc *desc;
unsigned int reg_idx;
u8 space_id;
u8 pcc_ss_id;
bool registered;
};
#define CPC_SYSMEM_START(node) ((node)->start)
#define CPC_SYSMEM_LAST(node) ((node)->last)
INTERVAL_TREE_DEFINE(struct cpc_sysmem_node, rb, u64, subtree_last,
CPC_SYSMEM_START, CPC_SYSMEM_LAST, static inline,
cpc_sysmem_itree)
static struct rb_root_cached cpc_sysmem_tree = RB_ROOT_CACHED;
static DEFINE_MUTEX(cpc_sysmem_lock);
#define CPC_NON_MMIO_START(node) ((node)->start)
#define CPC_NON_MMIO_LAST(node) ((node)->last)
INTERVAL_TREE_DEFINE(struct cpc_non_mmio_node, rb, u64, subtree_last,
CPC_NON_MMIO_START, CPC_NON_MMIO_LAST, static inline,
cpc_non_mmio_itree)
static struct rb_root_cached cpc_pcc_trees[MAX_PCC_SUBSPACES];
static struct rb_root_cached cpc_sysio_tree = RB_ROOT_CACHED;
static DEFINE_MUTEX(cpc_non_mmio_lock);
static struct cpc_sysmem_node *cpc_sysmem_first(u64 start, u64 last)
{
return cpc_sysmem_itree_iter_first(&cpc_sysmem_tree, start, last);
}
static struct cpc_sysmem_node *cpc_sysmem_next(struct cpc_sysmem_node *node,
u64 start, u64 last)
{
return cpc_sysmem_itree_iter_next(node, start, last);
}
#define CPC_PCC_HEADER_SIZE 0x8
/* pcc mapped address + header size + offset within PCC subspace */
#define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_channel->shmem + \
CPC_PCC_HEADER_SIZE + (offs))
/* Check if a CPC register is in PCC */
#define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_PLATFORM_COMM)
/* Check if a CPC register is in FFH */
#define CPC_IN_FFH(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_FIXED_HARDWARE)
/* Check if a CPC register is in SystemMemory */
#define CPC_IN_SYSTEM_MEMORY(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_SYSTEM_MEMORY)
/* Check if a CPC register is in SystemIo */
#define CPC_IN_SYSTEM_IO(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_SYSTEM_IO)
/* Evaluates to True if reg is a NULL register descriptor */
#define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
(reg)->address == 0 && \
(reg)->bit_width == 0 && \
(reg)->bit_offset == 0 && \
(reg)->access_width == 0)
/* Evaluates to True if an optional cpc field is supported */
#define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
!!(cpc)->cpc_entry.int_value : \
!IS_NULL_REG(&(cpc)->cpc_entry.reg))
static bool cpc_is_writable(const struct cpc_register_resource *cpc)
{
return cpc->type == ACPI_TYPE_BUFFER &&
!IS_NULL_REG(&cpc->cpc_entry.reg) &&
!cpc->cpc_entry.write_unsupported;
}
static bool cpc_is_readable(const struct cpc_register_resource *cpc)
{
return cpc->type != ACPI_TYPE_BUFFER ||
!cpc->cpc_entry.read_unsupported;
}
static bool cpc_entry_present(const struct cpc_register_resource *cpc)
{
if (cpc->type == ACPI_TYPE_INTEGER)
return true;
return cpc->type == ACPI_TYPE_BUFFER &&
!IS_NULL_REG(&cpc->cpc_entry.reg);
}
/*
* Each bit indicates the optionality of the register in per-cpu
* cpc_regs[] with the corresponding index. 0 means mandatory and 1
* means optional.
*/
#define REG_OPTIONAL (0x7FC7D0)
/*
* Use the index of the register in per-cpu cpc_regs[] to check if
* it's an optional one.
*/
#define IS_OPTIONAL_CPC_REG(reg_idx) (REG_OPTIONAL & (1U << (reg_idx)))
static bool cpc_integer_entry_valid(unsigned int reg_idx, u64 value,
bool *legacy_null)
{
*legacy_null = false;
switch (reg_idx) {
case HIGHEST_PERF:
case NOMINAL_PERF:
case LOW_NON_LINEAR_PERF:
case LOWEST_PERF:
case REFERENCE_PERF:
case LOWEST_FREQ:
case NOMINAL_FREQ:
return value <= U32_MAX;
case CTR_WRAP_TIME:
/* AML Integers and the kernel interface are both 64-bit. */
return true;
case AUTO_SEL_ENABLE:
return value <= 1;
case DESIRED_PERF:
/* Validated against Autonomous Selection after parsing. */
*legacy_null = value == 0;
return *legacy_null;
default:
/* Tolerate legacy Integer 0 placeholders for absent options. */
*legacy_null = value == 0 && IS_OPTIONAL_CPC_REG(reg_idx);
return *legacy_null;
}
}
/*
* Arbitrary Retries in case the remote processor is slow to respond
* to PCC commands. Keeping it high enough to cover emulators where
* the processors run painfully slow.
*/
#define NUM_RETRIES 500ULL
#define CPC_GENERIC_REGISTER_DESCRIPTOR 0x82
#define CPC_GENERIC_REGISTER_LENGTH (sizeof(struct cpc_reg) - 3)
#define define_one_cppc_ro(_name) \
static struct kobj_attribute _name = \
__ATTR(_name, 0444, show_##_name, NULL)
#define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
#define show_cppc_data(access_fn, struct_name, member_name) \
static ssize_t show_##member_name(struct kobject *kobj, \
struct kobj_attribute *attr, char *buf) \
{ \
struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \
struct struct_name st_name = {0}; \
int ret; \
\
ret = access_fn(cpc_ptr->cpu_id, &st_name); \
if (ret) \
return ret; \
\
return sysfs_emit(buf, "%llu\n", \
(u64)st_name.member_name); \
} \
define_one_cppc_ro(member_name)
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, reference_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, guaranteed_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq);
show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
/*
* PCC reuses the access_width field as the subspace id, so only decode access
* size for non-PCC registers. Otherwise, use the bit_width.
*/
#define GET_BIT_WIDTH(reg) (((reg)->access_width && \
(reg)->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) ? \
(8 << ((reg)->access_width - 1)) : (reg)->bit_width)
/* Shift and apply the mask for CPC reads/writes */
#define MASK_VAL_READ(reg, val) (((val) >> (reg)->bit_offset) & \
GENMASK(((reg)->bit_width) - 1, 0))
#define MASK_VAL_WRITE(reg, prev_val, val) \
((((val) & GENMASK(((reg)->bit_width) - 1, 0)) << (reg)->bit_offset) | \
((prev_val) & ~(GENMASK(((reg)->bit_width) - 1, 0) << (reg)->bit_offset))) \
static unsigned int cpc_reg_access_width(const struct cpc_reg *reg)
{
if (reg->access_width > 4)
return 0;
if (reg->access_width)
return 8U << (reg->access_width - 1);
return reg->bit_width;
}
enum cpc_platform_quirk {
CPC_QUIRK_PERF_LIMITED_OWNS_UNIT = BIT(0),
};
static const struct acpi_platform_list cpc_platform_quirk_list[] = {
{
.oem_id = "NVIDIA",
.oem_table_id = "T41",
.table = ACPI_SIG_DSDT,
.pred = all_versions,
.reason = "Performance Limited owns its access unit",
.data = CPC_QUIRK_PERF_LIMITED_OWNS_UNIT,
},
{ }
};
static DEFINE_MUTEX(cpc_platform_quirk_lock);
static bool cpc_platform_quirks_initialized;
static u32 cpc_platform_quirks;
static int cpc_get_platform_quirks(u32 *quirks)
{
int idx, ret = 0;
mutex_lock(&cpc_platform_quirk_lock);
if (!cpc_platform_quirks_initialized) {
idx = acpi_match_platform_list(cpc_platform_quirk_list);
if (idx < 0 && idx != -ENODEV) {
ret = idx;
goto out;
}
if (idx >= 0)
cpc_platform_quirks = cpc_platform_quirk_list[idx].data;
cpc_platform_quirks_initialized = true;
}
*quirks = cpc_platform_quirks;
out:
mutex_unlock(&cpc_platform_quirk_lock);
return ret;
}
static void cpc_apply_platform_quirks(struct cpc_reg *reg,
unsigned int reg_idx, u32 quirks)
{
unsigned int access_width;
if (!(quirks & CPC_QUIRK_PERF_LIMITED_OWNS_UNIT) ||
reg_idx != PERF_LIMITED ||
reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY ||
reg->bit_width != 2 || reg->bit_offset)
return;
access_width = cpc_reg_access_width(reg);
if (access_width != 32)
return;
reg->bit_width = access_width;
pr_info_once("firmware quirk: Performance Limited owns its access unit, using Bit Width %u\n",
access_width);
}
static u64 cpc_sysmem_access_size(const struct cpc_register_resource *reg)
{
unsigned int width = cpc_reg_access_width(®->cpc_entry.reg);
if (width != 8 && width != 16 && width != 32 && width != 64)
return 0;
return width / 8;
}
static u64 cpc_sysmem_field_size(const struct cpc_reg *gas)
{
return DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8);
}
static u64 cpc_sysmem_claim_size(const struct cpc_register_resource *reg)
{
const struct cpc_reg *gas = ®->cpc_entry.reg;
u64 access_size = cpc_sysmem_access_size(reg);
if (!gas->bit_width)
return access_size;
return max(access_size, cpc_sysmem_field_size(gas));
}
static bool cpc_reg_access_aligned(const struct cpc_reg *reg, u64 access_size)
{
/* x86 MMIO and port-I/O accessors support unaligned addresses. */
return IS_ENABLED(CONFIG_X86) || IS_ALIGNED(reg->address, access_size);
}
static bool cpc_sysmem_access_units_overlap(const struct cpc_register_resource *a,
const struct cpc_register_resource *b)
{
const struct cpc_reg *a_gas = &a->cpc_entry.reg;
const struct cpc_reg *b_gas = &b->cpc_entry.reg;
u64 a_size = cpc_sysmem_claim_size(a);
u64 b_size = cpc_sysmem_claim_size(b);
/* Keep the conservative locking path for malformed access widths. */
if (!a_size || !b_size)
return true;
if (a_gas->address < b_gas->address)
return b_gas->address - a_gas->address < a_size;
return a_gas->address - b_gas->address < b_size;
}
static bool cpc_reg_is_writable(unsigned int reg_idx)
{
/* Only controls written by this driver can be competing writers. */
switch (reg_idx) {
case DESIRED_PERF:
case MIN_PERF:
case MAX_PERF:
case PERF_LIMITED:
case ENABLE:
case AUTO_SEL_ENABLE:
case AUTO_ACT_WINDOW:
case ENERGY_PERF:
return true;
default:
return false;
}
}
static bool cpc_reg_is_write_only(const struct cpc_desc *cpc_desc,
unsigned int reg_idx)
{
return cpc_desc->version >= CPPC_V4_REV &&
(reg_idx == DESIRED_PERF || reg_idx == OSPM_NOMINAL_PERF);
}
static void cpc_disable_reg(struct cpc_desc *cpc_desc, unsigned int reg_idx)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
reg->type = ACPI_TYPE_INTEGER;
reg->cpc_entry.int_value = 0;
}
static bool cpc_optional_writer_can_be_disabled(unsigned int reg_idx)
{
if (!IS_OPTIONAL_CPC_REG(reg_idx) || !cpc_reg_is_writable(reg_idx) ||
reg_idx == MIN_PERF || reg_idx == MAX_PERF || reg_idx == ENABLE ||
reg_idx == AUTO_SEL_ENABLE)
return false;
return true;
}
static bool cpc_sysmem_reg_needs_rmw(const struct cpc_register_resource *reg)
{
const struct cpc_reg *gas = ®->cpc_entry.reg;
u64 access_size = cpc_sysmem_access_size(reg);
return gas->bit_offset || gas->bit_width != access_size * 8;
}
static int cpc_validate_sysmem_reg(struct cpc_desc *cpc_desc,
const struct cpc_reg *gas,
unsigned int reg_idx)
{
unsigned int access_width = cpc_reg_access_width(gas);
u64 access_size;
if (access_width != 8 && access_width != 16 &&
access_width != 32 && access_width != 64)
goto invalid;
if (!gas->bit_width || gas->bit_width > access_width ||
gas->bit_offset >= access_width ||
gas->bit_width > access_width - gas->bit_offset)
goto invalid;
access_size = access_width / 8;
if (!gas->address || gas->address > U64_MAX - (access_size - 1))
goto invalid;
if (!cpc_reg_access_aligned(gas, access_size))
goto invalid;
if (reg_idx == PERF_LIMITED) {
if (access_width == 64 && !IS_ENABLED(CONFIG_64BIT)) {
pr_warn_once("CPU%d: Performance Limited register cannot be accessed atomically; keeping its range reserved\n",
cpc_desc->cpu_id);
cpc_desc->cpc_regs[reg_idx].cpc_entry.read_unsupported = true;
cpc_desc->cpc_regs[reg_idx].cpc_entry.write_unsupported = true;
return 0;
}
if (gas->bit_offset || gas->bit_width != access_width) {
pr_warn_once("CPU%d: Performance Limited register cannot be cleared safely; keeping it readable\n",
cpc_desc->cpu_id);
cpc_desc->cpc_regs[reg_idx].cpc_entry.write_unsupported = true;
}
}
return 0;
invalid:
access_size = 0;
if (access_width == 8 || access_width == 16 ||
access_width == 32 || access_width == 64)
access_size = access_width / 8;
if (gas->bit_width)
access_size = max(access_size, cpc_sysmem_field_size(gas));
if ((cpc_reg_is_write_only(cpc_desc, reg_idx) ||
reg_idx == PERF_LIMITED) && gas->address && access_size &&
gas->address <= U64_MAX - (access_size - 1)) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
if (reg_idx == PERF_LIMITED)
pr_warn_once("CPU%d: _CPC v%d register %u is inaccessible; keeping its range reserved\n",
cpc_desc->cpu_id, cpc_desc->version, reg_idx);
else
pr_warn("CPU%d: _CPC v%d register %u is inaccessible; keeping its range reserved\n",
cpc_desc->cpu_id, cpc_desc->version, reg_idx);
reg->cpc_entry.read_unsupported = true;
reg->cpc_entry.write_unsupported = true;
return 0;
}
pr_debug("CPU:%d invalid SystemMemory GAS for _CPC register %u\n",
cpc_desc->cpu_id, reg_idx);
return -EINVAL;
}
static bool cpc_immutable_autonomous(const struct cpc_desc *cpc_desc)
{
const struct cpc_register_resource *reg;
reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
return osc_sb_cppc2_support_acked && reg->type == ACPI_TYPE_INTEGER &&
reg->cpc_entry.int_value == 1;
}
static bool cpc_retain_pcc_status(struct cpc_desc *cpc_desc,
unsigned int reg_idx);
static int cpc_resolve_unsupported(struct cpc_desc *cpc_desc,
u32 unsupported)
{
unsigned int i;
u32 bounds = BIT(MIN_PERF) | BIT(MAX_PERF);
bool min_unusable, max_unusable;
if (unsupported & bounds) {
min_unusable = (unsupported & BIT(MIN_PERF)) ||
!cpc_is_writable(&cpc_desc->cpc_regs[MIN_PERF]);
max_unusable = (unsupported & BIT(MAX_PERF)) ||
!cpc_is_writable(&cpc_desc->cpc_regs[MAX_PERF]);
if (min_unusable && max_unusable) {
pr_warn("CPU%d: ignoring inaccessible Minimum and Maximum Performance registers\n",
cpc_desc->cpu_id);
cpc_disable_reg(cpc_desc, MIN_PERF);
cpc_disable_reg(cpc_desc, MAX_PERF);
unsupported &= ~bounds;
}
}
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
if (!(unsupported & BIT(i)))
continue;
/* CPPC control does not depend on Performance Limited status. */
if (i == PERF_LIMITED) {
if (CPC_IN_PCC(&cpc_desc->cpc_regs[i]) &&
cpc_retain_pcc_status(cpc_desc, i))
continue;
pr_warn_once("CPU%d: ignoring inaccessible Performance Limited register\n",
cpc_desc->cpu_id);
cpc_disable_reg(cpc_desc, i);
continue;
}
if (i == DESIRED_PERF && cpc_immutable_autonomous(cpc_desc)) {
pr_warn("CPU%d: ignoring inaccessible Desired Performance register in autonomous mode\n",
cpc_desc->cpu_id);
cpc_disable_reg(cpc_desc, i);
continue;
}
/*
* A present Enable or Autonomous Selection control must remain
* usable. Disabling the latter could leave autonomous selection
* enabled while OSPM believes that it has disabled it.
*/
if (i == ENABLE ||
(i == AUTO_SEL_ENABLE && cpc_entry_present(&cpc_desc->cpc_regs[i])) ||
i == MIN_PERF || i == MAX_PERF ||
!IS_OPTIONAL_CPC_REG(i)) {
pr_err("CPU%d: cannot access _CPC register %u\n",
cpc_desc->cpu_id, i);
return -EINVAL;
}
pr_warn("CPU%d: ignoring inaccessible optional _CPC register %u\n",
cpc_desc->cpu_id, i);
cpc_disable_reg(cpc_desc, i);
}
return 0;
}
static int cpc_validate_required_controls(struct cpc_desc *cpc_desc)
{
unsigned int i;
/*
* Performance Limited is required by the specification, but tolerate a
* NULL descriptor used by firmware which cannot report limiting events.
* CPPC control does not depend on this status.
*/
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
if (i != DESIRED_PERF && i != PERF_LIMITED &&
!IS_OPTIONAL_CPC_REG(i) &&
!cpc_entry_present(&cpc_desc->cpc_regs[i])) {
pr_debug("CPU:%d lacks mandatory _CPC register %u\n",
cpc_desc->cpu_id, i);
return -EINVAL;
}
}
/* Desired may be absent only for immutable autonomous operation. */
if (!cpc_is_writable(&cpc_desc->cpc_regs[DESIRED_PERF]) &&
!cpc_immutable_autonomous(cpc_desc)) {
pr_debug("CPU:%d lacks a writable Desired Performance register\n",
cpc_desc->cpu_id);
return -EINVAL;
}
return 0;
}
static int cpc_validate_bound_controls(struct cpc_desc *cpc_desc)
{
bool have_min, have_max;
have_min = cpc_is_writable(&cpc_desc->cpc_regs[MIN_PERF]);
have_max = cpc_is_writable(&cpc_desc->cpc_regs[MAX_PERF]);
if (have_min != have_max) {
pr_err("CPU%d: _CPC must provide both Minimum and Maximum Performance or neither\n",
cpc_desc->cpu_id);
return -EINVAL;
}
return 0;
}
static bool
cpc_retain_pcc_status(struct cpc_desc *cpc_desc, unsigned int reg_idx)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
const struct cpc_reg *gas = ®->cpc_entry.reg;
u64 size;
if (reg_idx != PERF_LIMITED || !gas->bit_width)
return false;
size = DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8);
if (!size || gas->address > U64_MAX - (size - 1))
return false;
reg->cpc_entry.read_unsupported = true;
reg->cpc_entry.write_unsupported = true;
pr_warn_once("CPU%d: Performance Limited register cannot be accessed; keeping its PCC range reserved\n",
cpc_desc->cpu_id);
return true;
}
static bool
cpc_retain_sysio_status(struct cpc_desc *cpc_desc, unsigned int reg_idx)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
const struct cpc_reg *gas = ®->cpc_entry.reg;
if (reg_idx != PERF_LIMITED || !gas->bit_width)
return false;
/* Retain any in-range portion for overlap validation only. */
if (gas->address > U16_MAX)
return false;
pr_warn_once("CPU%d: Performance Limited register cannot be accessed; keeping its SystemIO range reserved\n",
cpc_desc->cpu_id);
reg->cpc_entry.read_unsupported = true;
reg->cpc_entry.write_unsupported = true;
return true;
}
static u64 cpc_non_mmio_access_size(const struct cpc_register_resource *reg)
{
const struct cpc_reg *gas = ®->cpc_entry.reg;
if (gas->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
return DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8);
return max((u64)cpc_reg_access_width(gas) / 8,
DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8));
}
static void cpc_validate_pcc_bounds(struct cpc_desc *cpc_desc,
int pcc_ss_id, struct cppc_pcc_data *data,
u32 *unsupported)
{
u64 shmem_size = data->pcc_channel->shmem_size;
unsigned int i;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
struct cpc_reg *gas;
u64 access_size;
if ((*unsupported & BIT(i)) || !CPC_SUPPORTED(reg) ||
!CPC_IN_PCC(reg))
continue;
gas = ®->cpc_entry.reg;
if (gas->access_width != pcc_ss_id)
continue;
access_size = cpc_non_mmio_access_size(reg);
if (shmem_size >= CPC_PCC_HEADER_SIZE &&
gas->address <= shmem_size - CPC_PCC_HEADER_SIZE &&
access_size <= shmem_size - CPC_PCC_HEADER_SIZE - gas->address)
continue;
pr_debug("CPU%d: _CPC register %u exceeds the PCC shared region\n",
cpc_desc->cpu_id, i);
*unsupported |= BIT(i);
}
}
static bool cpc_pcc_access_needed(const struct cpc_desc *cpc_desc)
{
unsigned int i;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
const struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
if (CPC_SUPPORTED(reg) && CPC_IN_PCC(reg) &&
(cpc_is_readable(reg) || cpc_is_writable(reg)))
return true;
}
return false;
}
static bool cpc_non_mmio_overlap_conflicts(u8 space_id, bool a_writable,
bool b_writable, bool a_write_only,
bool b_write_only)
{
/* Only a write-only control can use a separate read-side port alias. */
if (space_id == ACPI_ADR_SPACE_SYSTEM_IO && a_writable != b_writable)
return a_writable ? !a_write_only : !b_write_only;
return a_writable || b_writable;
}
static bool
cpc_sysio_perf_limited_conflicts(unsigned int a_idx, bool a_writable,
unsigned int b_idx, bool b_writable)
{
return (a_idx == PERF_LIMITED && b_writable) ||
(b_idx == PERF_LIMITED && a_writable);
}
static struct rb_root_cached *cpc_non_mmio_tree(u8 space_id, u8 pcc_ss_id)
{
if (space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
return &cpc_pcc_trees[pcc_ss_id];
if (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
return &cpc_sysio_tree;
return NULL;
}
static bool cpc_same_non_mmio_register(const struct cpc_non_mmio_node *a,
const struct cpc_non_mmio_node *b)
{
const struct cpc_reg *a_gas =
&a->desc->cpc_regs[a->reg_idx].cpc_entry.reg;
const struct cpc_reg *b_gas =
&b->desc->cpc_regs[b->reg_idx].cpc_entry.reg;
return a->space_id == b->space_id && a->pcc_ss_id == b->pcc_ss_id &&
a->reg_idx == b->reg_idx && a->start == b->start &&
a->last == b->last && a_gas->bit_offset == b_gas->bit_offset &&
a_gas->bit_width == b_gas->bit_width &&
(a->space_id == ACPI_ADR_SPACE_PLATFORM_COMM ||
cpc_reg_access_width(a_gas) == cpc_reg_access_width(b_gas));
}
static int cpc_validate_non_mmio_pair(const struct cpc_non_mmio_node *a,
const struct cpc_non_mmio_node *b)
{
const struct cpc_register_resource *a_reg;
const struct cpc_register_resource *b_reg;
bool a_writable, b_writable;
const char *name;
a_reg = &a->desc->cpc_regs[a->reg_idx];
b_reg = &b->desc->cpc_regs[b->reg_idx];
a_writable = cpc_reg_is_writable(a->reg_idx) && cpc_is_writable(a_reg);
b_writable = cpc_reg_is_writable(b->reg_idx) && cpc_is_writable(b_reg);
if (!cpc_non_mmio_overlap_conflicts(a->space_id, a_writable,
b_writable,
cpc_reg_is_write_only(a->desc, a->reg_idx),
cpc_reg_is_write_only(b->desc, b->reg_idx)) &&
!(a->space_id == ACPI_ADR_SPACE_SYSTEM_IO &&
cpc_sysio_perf_limited_conflicts(a->reg_idx, a_writable,
b->reg_idx, b_writable)))
return 0;
if (cpc_same_non_mmio_register(a, b))
return 0;
name = a->space_id == ACPI_ADR_SPACE_PLATFORM_COMM ?
"PCC" : "SystemIO";
pr_err("CPU%d: %s _CPC register %u conflicts with CPU%d register %u\n",
a->desc->cpu_id, name, a->reg_idx, b->desc->cpu_id,
b->reg_idx);
return -EINVAL;
}
static void cpc_unregister_non_mmio_desc_locked(struct cpc_desc *cpc_desc)
{
unsigned int i;
if (!cpc_desc->non_mmio_nodes)
return;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_non_mmio_node *node = &cpc_desc->non_mmio_nodes[i];
struct rb_root_cached *tree;
if (!node->registered)
continue;
tree = cpc_non_mmio_tree(node->space_id, node->pcc_ss_id);
cpc_non_mmio_itree_remove(node, tree);
}
kfree(cpc_desc->non_mmio_nodes);
cpc_desc->non_mmio_nodes = NULL;
}
static int cpc_register_non_mmio_desc(struct cpc_desc *cpc_desc)
{
unsigned int nr_regs = cpc_desc->num_entries - 2;
unsigned int i;
int ret = 0;
bool found = false;
for (i = 0; i < nr_regs; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
u8 space_id;
if (!CPC_SUPPORTED(reg) || reg->type != ACPI_TYPE_BUFFER)
continue;
space_id = reg->cpc_entry.reg.space_id;
if (space_id == ACPI_ADR_SPACE_PLATFORM_COMM ||
space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
found = true;
break;
}
}
if (!found)
return 0;
cpc_desc->non_mmio_nodes = kzalloc_objs(*cpc_desc->non_mmio_nodes,
nr_regs);
if (!cpc_desc->non_mmio_nodes)
return -ENOMEM;
mutex_lock(&cpc_non_mmio_lock);
for (i = 0; i < nr_regs; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
struct cpc_non_mmio_node *match, *node;
struct rb_root_cached *tree;
u8 space_id;
u64 size;
if (!CPC_SUPPORTED(reg) || reg->type != ACPI_TYPE_BUFFER)
continue;
space_id = reg->cpc_entry.reg.space_id;
if (space_id != ACPI_ADR_SPACE_PLATFORM_COMM &&
space_id != ACPI_ADR_SPACE_SYSTEM_IO)
continue;
node = &cpc_desc->non_mmio_nodes[i];
size = cpc_non_mmio_access_size(reg);
node->start = reg->cpc_entry.reg.address;
node->last = node->start + size - 1;
node->desc = cpc_desc;
node->reg_idx = i;
node->space_id = space_id;
node->pcc_ss_id = space_id == ACPI_ADR_SPACE_PLATFORM_COMM ?
reg->cpc_entry.reg.access_width : 0;
tree = cpc_non_mmio_tree(space_id, node->pcc_ss_id);
match = cpc_non_mmio_itree_iter_first(tree, node->start,
node->last);
while (match) {
ret = cpc_validate_non_mmio_pair(node, match);
if (ret)
goto out_unregister;
match = cpc_non_mmio_itree_iter_next(match, node->start,
node->last);
}
cpc_non_mmio_itree_insert(node, tree);
node->registered = true;
}
mutex_unlock(&cpc_non_mmio_lock);
return 0;
out_unregister:
cpc_unregister_non_mmio_desc_locked(cpc_desc);
mutex_unlock(&cpc_non_mmio_lock);
return ret;
}
static void cpc_unregister_non_mmio_desc(struct cpc_desc *cpc_desc)
{
if (!cpc_desc->non_mmio_nodes)
return;
mutex_lock(&cpc_non_mmio_lock);
cpc_unregister_non_mmio_desc_locked(cpc_desc);
mutex_unlock(&cpc_non_mmio_lock);
}
static void cpc_mark_rmw_lock_users(struct cpc_desc *cpc_desc)
{
int i;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
if (CPC_SUPPORTED(reg) && CPC_IN_SYSTEM_MEMORY(reg) &&
cpc_is_writable(reg))
reg->cpc_entry.use_rmw_lock =
cpc_sysmem_reg_needs_rmw(reg);
}
}
struct cpc_bit_position {
u64 byte;
u8 bit;
};