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
|
/*
* QEMU model of the Versal eFuse controller
*
* Copyright (c) 2020 Xilinx Inc.
* Copyright (c) 2023 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "hw/nvram/xlnx-versal-efuse.h"
#include "qemu/log.h"
#include "qapi/error.h"
#include "migration/vmstate.h"
#include "hw/qdev-properties.h"
#ifndef XLNX_VERSAL_EFUSE_CTRL_ERR_DEBUG
#define XLNX_VERSAL_EFUSE_CTRL_ERR_DEBUG 0
#endif
REG32(WR_LOCK, 0x0)
FIELD(WR_LOCK, LOCK, 0, 16)
REG32(CFG, 0x4)
FIELD(CFG, SLVERR_ENABLE, 5, 1)
FIELD(CFG, MARGIN_RD, 2, 1)
FIELD(CFG, PGM_EN, 1, 1)
REG32(STATUS, 0x8)
FIELD(STATUS, AES_USER_KEY_1_CRC_PASS, 11, 1)
FIELD(STATUS, AES_USER_KEY_1_CRC_DONE, 10, 1)
FIELD(STATUS, AES_USER_KEY_0_CRC_PASS, 9, 1)
FIELD(STATUS, AES_USER_KEY_0_CRC_DONE, 8, 1)
FIELD(STATUS, AES_CRC_PASS, 7, 1)
FIELD(STATUS, AES_CRC_DONE, 6, 1)
FIELD(STATUS, CACHE_DONE, 5, 1)
FIELD(STATUS, CACHE_LOAD, 4, 1)
FIELD(STATUS, EFUSE_2_TBIT, 2, 1)
FIELD(STATUS, EFUSE_1_TBIT, 1, 1)
FIELD(STATUS, EFUSE_0_TBIT, 0, 1)
REG32(EFUSE_PGM_ADDR, 0xc)
FIELD(EFUSE_PGM_ADDR, PAGE, 13, 4)
FIELD(EFUSE_PGM_ADDR, ROW, 5, 8)
FIELD(EFUSE_PGM_ADDR, COLUMN, 0, 5)
REG32(EFUSE_RD_ADDR, 0x10)
FIELD(EFUSE_RD_ADDR, PAGE, 13, 4)
FIELD(EFUSE_RD_ADDR, ROW, 5, 8)
REG32(EFUSE_RD_DATA, 0x14)
REG32(TPGM, 0x18)
FIELD(TPGM, VALUE, 0, 16)
REG32(TRD, 0x1c)
FIELD(TRD, VALUE, 0, 8)
REG32(TSU_H_PS, 0x20)
FIELD(TSU_H_PS, VALUE, 0, 8)
REG32(TSU_H_PS_CS, 0x24)
FIELD(TSU_H_PS_CS, VALUE, 0, 8)
REG32(TRDM, 0x28)
FIELD(TRDM, VALUE, 0, 8)
REG32(TSU_H_CS, 0x2c)
FIELD(TSU_H_CS, VALUE, 0, 8)
REG32(EFUSE_ISR, 0x30)
FIELD(EFUSE_ISR, APB_SLVERR, 31, 1)
FIELD(EFUSE_ISR, CACHE_PARITY_E2, 14, 1)
FIELD(EFUSE_ISR, CACHE_PARITY_E1, 13, 1)
FIELD(EFUSE_ISR, CACHE_PARITY_E0S, 12, 1)
FIELD(EFUSE_ISR, CACHE_PARITY_E0R, 11, 1)
FIELD(EFUSE_ISR, CACHE_APB_SLVERR, 10, 1)
FIELD(EFUSE_ISR, CACHE_REQ_ERROR, 9, 1)
FIELD(EFUSE_ISR, MAIN_REQ_ERROR, 8, 1)
FIELD(EFUSE_ISR, READ_ON_CACHE_LD, 7, 1)
FIELD(EFUSE_ISR, CACHE_FSM_ERROR, 6, 1)
FIELD(EFUSE_ISR, MAIN_FSM_ERROR, 5, 1)
FIELD(EFUSE_ISR, CACHE_ERROR, 4, 1)
FIELD(EFUSE_ISR, RD_ERROR, 3, 1)
FIELD(EFUSE_ISR, RD_DONE, 2, 1)
FIELD(EFUSE_ISR, PGM_ERROR, 1, 1)
FIELD(EFUSE_ISR, PGM_DONE, 0, 1)
REG32(EFUSE_IMR, 0x34)
FIELD(EFUSE_IMR, APB_SLVERR, 31, 1)
FIELD(EFUSE_IMR, CACHE_PARITY_E2, 14, 1)
FIELD(EFUSE_IMR, CACHE_PARITY_E1, 13, 1)
FIELD(EFUSE_IMR, CACHE_PARITY_E0S, 12, 1)
FIELD(EFUSE_IMR, CACHE_PARITY_E0R, 11, 1)
FIELD(EFUSE_IMR, CACHE_APB_SLVERR, 10, 1)
FIELD(EFUSE_IMR, CACHE_REQ_ERROR, 9, 1)
FIELD(EFUSE_IMR, MAIN_REQ_ERROR, 8, 1)
FIELD(EFUSE_IMR, READ_ON_CACHE_LD, 7, 1)
FIELD(EFUSE_IMR, CACHE_FSM_ERROR, 6, 1)
FIELD(EFUSE_IMR, MAIN_FSM_ERROR, 5, 1)
FIELD(EFUSE_IMR, CACHE_ERROR, 4, 1)
FIELD(EFUSE_IMR, RD_ERROR, 3, 1)
FIELD(EFUSE_IMR, RD_DONE, 2, 1)
FIELD(EFUSE_IMR, PGM_ERROR, 1, 1)
FIELD(EFUSE_IMR, PGM_DONE, 0, 1)
REG32(EFUSE_IER, 0x38)
FIELD(EFUSE_IER, APB_SLVERR, 31, 1)
FIELD(EFUSE_IER, CACHE_PARITY_E2, 14, 1)
FIELD(EFUSE_IER, CACHE_PARITY_E1, 13, 1)
FIELD(EFUSE_IER, CACHE_PARITY_E0S, 12, 1)
FIELD(EFUSE_IER, CACHE_PARITY_E0R, 11, 1)
FIELD(EFUSE_IER, CACHE_APB_SLVERR, 10, 1)
FIELD(EFUSE_IER, CACHE_REQ_ERROR, 9, 1)
FIELD(EFUSE_IER, MAIN_REQ_ERROR, 8, 1)
FIELD(EFUSE_IER, READ_ON_CACHE_LD, 7, 1)
FIELD(EFUSE_IER, CACHE_FSM_ERROR, 6, 1)
FIELD(EFUSE_IER, MAIN_FSM_ERROR, 5, 1)
FIELD(EFUSE_IER, CACHE_ERROR, 4, 1)
FIELD(EFUSE_IER, RD_ERROR, 3, 1)
FIELD(EFUSE_IER, RD_DONE, 2, 1)
FIELD(EFUSE_IER, PGM_ERROR, 1, 1)
FIELD(EFUSE_IER, PGM_DONE, 0, 1)
REG32(EFUSE_IDR, 0x3c)
FIELD(EFUSE_IDR, APB_SLVERR, 31, 1)
FIELD(EFUSE_IDR, CACHE_PARITY_E2, 14, 1)
FIELD(EFUSE_IDR, CACHE_PARITY_E1, 13, 1)
FIELD(EFUSE_IDR, CACHE_PARITY_E0S, 12, 1)
FIELD(EFUSE_IDR, CACHE_PARITY_E0R, 11, 1)
FIELD(EFUSE_IDR, CACHE_APB_SLVERR, 10, 1)
FIELD(EFUSE_IDR, CACHE_REQ_ERROR, 9, 1)
FIELD(EFUSE_IDR, MAIN_REQ_ERROR, 8, 1)
FIELD(EFUSE_IDR, READ_ON_CACHE_LD, 7, 1)
FIELD(EFUSE_IDR, CACHE_FSM_ERROR, 6, 1)
FIELD(EFUSE_IDR, MAIN_FSM_ERROR, 5, 1)
FIELD(EFUSE_IDR, CACHE_ERROR, 4, 1)
FIELD(EFUSE_IDR, RD_ERROR, 3, 1)
FIELD(EFUSE_IDR, RD_DONE, 2, 1)
FIELD(EFUSE_IDR, PGM_ERROR, 1, 1)
FIELD(EFUSE_IDR, PGM_DONE, 0, 1)
REG32(EFUSE_CACHE_LOAD, 0x40)
FIELD(EFUSE_CACHE_LOAD, LOAD, 0, 1)
REG32(EFUSE_PGM_LOCK, 0x44)
FIELD(EFUSE_PGM_LOCK, SPK_ID_LOCK, 0, 1)
REG32(EFUSE_AES_CRC, 0x48)
REG32(EFUSE_AES_USR_KEY0_CRC, 0x4c)
REG32(EFUSE_AES_USR_KEY1_CRC, 0x50)
REG32(EFUSE_PD, 0x54)
REG32(EFUSE_ANLG_OSC_SW_1LP, 0x60)
REG32(EFUSE_TEST_CTRL, 0x100)
#define R_MAX (R_EFUSE_TEST_CTRL + 1)
#define R_WR_LOCK_UNLOCK_PASSCODE (0xDF0D)
/*
* eFuse layout references:
* https://github.com/Xilinx/embeddedsw/blob/release-2019.2/lib/sw_services/xilnvm/src/xnvm_efuse_hw.h
*/
#define BIT_POS_OF(A_) \
((uint32_t)((A_) & (R_EFUSE_PGM_ADDR_ROW_MASK | \
R_EFUSE_PGM_ADDR_COLUMN_MASK)))
#define BIT_POS(R_, C_) \
((uint32_t)((R_EFUSE_PGM_ADDR_ROW_MASK \
& ((R_) << R_EFUSE_PGM_ADDR_ROW_SHIFT)) \
| \
(R_EFUSE_PGM_ADDR_COLUMN_MASK \
& ((C_) << R_EFUSE_PGM_ADDR_COLUMN_SHIFT))))
#define EFUSE_TBIT_POS(A_) (BIT_POS_OF(A_) >= BIT_POS(0, 28))
#define EFUSE_ANCHOR_ROW (0)
#define EFUSE_ANCHOR_3_COL (27)
#define EFUSE_ANCHOR_1_COL (1)
#define EFUSE_AES_KEY_START BIT_POS(12, 0)
#define EFUSE_AES_KEY_END BIT_POS(19, 31)
#define EFUSE_USER_KEY_0_START BIT_POS(20, 0)
#define EFUSE_USER_KEY_0_END BIT_POS(27, 31)
#define EFUSE_USER_KEY_1_START BIT_POS(28, 0)
#define EFUSE_USER_KEY_1_END BIT_POS(35, 31)
#define EFUSE_RD_BLOCKED_START EFUSE_AES_KEY_START
#define EFUSE_RD_BLOCKED_END EFUSE_USER_KEY_1_END
#define EFUSE_GLITCH_DET_WR_LK BIT_POS(4, 31)
#define EFUSE_PPK0_WR_LK BIT_POS(43, 6)
#define EFUSE_PPK1_WR_LK BIT_POS(43, 7)
#define EFUSE_PPK2_WR_LK BIT_POS(43, 8)
#define EFUSE_AES_WR_LK BIT_POS(43, 11)
#define EFUSE_USER_KEY_0_WR_LK BIT_POS(43, 13)
#define EFUSE_USER_KEY_1_WR_LK BIT_POS(43, 15)
#define EFUSE_PUF_SYN_LK BIT_POS(43, 16)
#define EFUSE_DNA_WR_LK BIT_POS(43, 27)
#define EFUSE_BOOT_ENV_WR_LK BIT_POS(43, 28)
#define EFUSE_PGM_LOCKED_START BIT_POS(44, 0)
#define EFUSE_PGM_LOCKED_END BIT_POS(51, 31)
#define EFUSE_PUF_PAGE (2)
#define EFUSE_PUF_SYN_START BIT_POS(129, 0)
#define EFUSE_PUF_SYN_END BIT_POS(255, 27)
#define EFUSE_KEY_CRC_LK_ROW (43)
#define EFUSE_AES_KEY_CRC_LK_MASK ((1U << 9) | (1U << 10))
#define EFUSE_USER_KEY_0_CRC_LK_MASK (1U << 12)
#define EFUSE_USER_KEY_1_CRC_LK_MASK (1U << 14)
/*
* A handy macro to return value of an array element,
* or a specific default if given index is out of bound.
*/
#define ARRAY_GET(A_, I_, D_) \
((unsigned int)(I_) < ARRAY_SIZE(A_) ? (A_)[I_] : (D_))
QEMU_BUILD_BUG_ON(R_MAX != ARRAY_SIZE(((XlnxVersalEFuseCtrl *)0)->regs));
typedef struct XlnxEFuseLkSpec {
uint16_t row;
uint16_t lk_bit;
} XlnxEFuseLkSpec;
static void efuse_imr_update_irq(XlnxVersalEFuseCtrl *s)
{
bool pending = s->regs[R_EFUSE_ISR] & ~s->regs[R_EFUSE_IMR];
qemu_set_irq(s->irq_efuse_imr, pending);
}
static void efuse_isr_postw(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
efuse_imr_update_irq(s);
}
static uint64_t efuse_ier_prew(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
uint32_t val = val64;
s->regs[R_EFUSE_IMR] &= ~val;
efuse_imr_update_irq(s);
return 0;
}
static uint64_t efuse_idr_prew(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
uint32_t val = val64;
s->regs[R_EFUSE_IMR] |= val;
efuse_imr_update_irq(s);
return 0;
}
static void efuse_status_tbits_sync(XlnxVersalEFuseCtrl *s)
{
uint32_t check = xlnx_efuse_tbits_check(s->efuse);
uint32_t val = s->regs[R_STATUS];
val = FIELD_DP32(val, STATUS, EFUSE_0_TBIT, !!(check & (1 << 0)));
val = FIELD_DP32(val, STATUS, EFUSE_1_TBIT, !!(check & (1 << 1)));
val = FIELD_DP32(val, STATUS, EFUSE_2_TBIT, !!(check & (1 << 2)));
s->regs[R_STATUS] = val;
}
static void efuse_anchor_bits_check(XlnxVersalEFuseCtrl *s)
{
unsigned page;
if (!s->efuse || !s->efuse->init_tbits) {
return;
}
for (page = 0; page < s->efuse->efuse_nr; page++) {
uint32_t row = 0, bit;
row = FIELD_DP32(row, EFUSE_PGM_ADDR, PAGE, page);
row = FIELD_DP32(row, EFUSE_PGM_ADDR, ROW, EFUSE_ANCHOR_ROW);
bit = FIELD_DP32(row, EFUSE_PGM_ADDR, COLUMN, EFUSE_ANCHOR_3_COL);
if (!xlnx_efuse_get_bit(s->efuse, bit)) {
xlnx_efuse_set_bit(s->efuse, bit);
}
bit = FIELD_DP32(row, EFUSE_PGM_ADDR, COLUMN, EFUSE_ANCHOR_1_COL);
if (!xlnx_efuse_get_bit(s->efuse, bit)) {
xlnx_efuse_set_bit(s->efuse, bit);
}
}
}
static void efuse_key_crc_check(RegisterInfo *reg, uint32_t crc,
uint32_t pass_mask, uint32_t done_mask,
unsigned first, uint32_t lk_mask)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
uint32_t r, lk_bits;
/*
* To start, assume both DONE and PASS, and clear PASS by xor
* if CRC-check fails or CRC-check disabled by lock fuse.
*/
r = s->regs[R_STATUS] | done_mask | pass_mask;
lk_bits = xlnx_efuse_get_row(s->efuse, EFUSE_KEY_CRC_LK_ROW) & lk_mask;
if (lk_bits == 0 && xlnx_efuse_k256_check(s->efuse, crc, first)) {
pass_mask = 0;
}
s->regs[R_STATUS] = r ^ pass_mask;
}
static void efuse_data_sync(XlnxVersalEFuseCtrl *s)
{
efuse_status_tbits_sync(s);
}
static int efuse_lk_spec_cmp(const void *a, const void *b)
{
uint16_t r1 = ((const XlnxEFuseLkSpec *)a)->row;
uint16_t r2 = ((const XlnxEFuseLkSpec *)b)->row;
return (r1 > r2) - (r1 < r2);
}
static void efuse_lk_spec_sort(XlnxVersalEFuseCtrl *s)
{
XlnxEFuseLkSpec *ary = s->extra_pg0_lock_spec;
const uint32_t n8 = s->extra_pg0_lock_n16 * 2;
const uint32_t sz = sizeof(ary[0]);
const uint32_t cnt = n8 / sz;
if (ary && cnt) {
qsort(ary, cnt, sz, efuse_lk_spec_cmp);
}
}
static uint32_t efuse_lk_spec_find(XlnxVersalEFuseCtrl *s, uint32_t row)
{
const XlnxEFuseLkSpec *ary = s->extra_pg0_lock_spec;
const uint32_t n8 = s->extra_pg0_lock_n16 * 2;
const uint32_t sz = sizeof(ary[0]);
const uint32_t cnt = n8 / sz;
const XlnxEFuseLkSpec *item = NULL;
if (ary && cnt) {
XlnxEFuseLkSpec k = { .row = row, };
item = bsearch(&k, ary, cnt, sz, efuse_lk_spec_cmp);
}
return item ? item->lk_bit : 0;
}
static uint32_t efuse_bit_locked(XlnxVersalEFuseCtrl *s, uint32_t bit)
{
/* Hard-coded locks */
static const uint16_t pg0_hard_lock[] = {
[4] = EFUSE_GLITCH_DET_WR_LK,
[37] = EFUSE_BOOT_ENV_WR_LK,
[8 ... 11] = EFUSE_DNA_WR_LK,
[12 ... 19] = EFUSE_AES_WR_LK,
[20 ... 27] = EFUSE_USER_KEY_0_WR_LK,
[28 ... 35] = EFUSE_USER_KEY_1_WR_LK,
[64 ... 71] = EFUSE_PPK0_WR_LK,
[72 ... 79] = EFUSE_PPK1_WR_LK,
[80 ... 87] = EFUSE_PPK2_WR_LK,
};
uint32_t row = FIELD_EX32(bit, EFUSE_PGM_ADDR, ROW);
uint32_t lk_bit = ARRAY_GET(pg0_hard_lock, row, 0);
return lk_bit ? lk_bit : efuse_lk_spec_find(s, row);
}
static bool efuse_pgm_locked(XlnxVersalEFuseCtrl *s, unsigned int bit)
{
unsigned int lock = 1;
/* Global lock */
if (!ARRAY_FIELD_EX32(s->regs, CFG, PGM_EN)) {
goto ret_lock;
}
/* Row lock */
switch (FIELD_EX32(bit, EFUSE_PGM_ADDR, PAGE)) {
case 0:
if (ARRAY_FIELD_EX32(s->regs, EFUSE_PGM_LOCK, SPK_ID_LOCK) &&
bit >= EFUSE_PGM_LOCKED_START && bit <= EFUSE_PGM_LOCKED_END) {
goto ret_lock;
}
lock = efuse_bit_locked(s, bit);
break;
case EFUSE_PUF_PAGE:
if (bit < EFUSE_PUF_SYN_START || bit > EFUSE_PUF_SYN_END) {
lock = 0;
goto ret_lock;
}
lock = EFUSE_PUF_SYN_LK;
break;
default:
lock = 0;
goto ret_lock;
}
/* Row lock by an efuse bit */
if (lock) {
lock = xlnx_efuse_get_bit(s->efuse, lock);
}
ret_lock:
return lock != 0;
}
static void efuse_pgm_addr_postw(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
unsigned bit = val64;
bool ok = false;
/* Always zero out PGM_ADDR because it is write-only */
s->regs[R_EFUSE_PGM_ADDR] = 0;
/*
* Indicate error if bit is write-protected (or read-only
* as guarded by efuse_set_bit()).
*
* Keep it simple by not modeling program timing.
*
* Note: model must NEVER clear the PGM_ERROR bit; it is
* up to guest to do so (or by reset).
*/
if (efuse_pgm_locked(s, bit)) {
g_autofree char *path = object_get_canonical_path(OBJECT(s));
qemu_log_mask(LOG_GUEST_ERROR,
"%s: Denied setting of efuse<%u, %u, %u>\n",
path,
FIELD_EX32(bit, EFUSE_PGM_ADDR, PAGE),
FIELD_EX32(bit, EFUSE_PGM_ADDR, ROW),
FIELD_EX32(bit, EFUSE_PGM_ADDR, COLUMN));
} else if (xlnx_efuse_set_bit(s->efuse, bit)) {
ok = true;
if (EFUSE_TBIT_POS(bit)) {
efuse_status_tbits_sync(s);
}
}
if (!ok) {
ARRAY_FIELD_DP32(s->regs, EFUSE_ISR, PGM_ERROR, 1);
}
ARRAY_FIELD_DP32(s->regs, EFUSE_ISR, PGM_DONE, 1);
efuse_imr_update_irq(s);
}
static void efuse_rd_addr_postw(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
unsigned bit = val64;
bool denied;
/* Always zero out RD_ADDR because it is write-only */
s->regs[R_EFUSE_RD_ADDR] = 0;
/*
* Indicate error if row is read-blocked.
*
* Note: model must NEVER clear the RD_ERROR bit; it is
* up to guest to do so (or by reset).
*/
s->regs[R_EFUSE_RD_DATA] = xlnx_versal_efuse_read_row(s->efuse,
bit, &denied);
if (denied) {
g_autofree char *path = object_get_canonical_path(OBJECT(s));
qemu_log_mask(LOG_GUEST_ERROR,
"%s: Denied reading of efuse<%u, %u>\n",
path,
FIELD_EX32(bit, EFUSE_RD_ADDR, PAGE),
FIELD_EX32(bit, EFUSE_RD_ADDR, ROW));
ARRAY_FIELD_DP32(s->regs, EFUSE_ISR, RD_ERROR, 1);
}
ARRAY_FIELD_DP32(s->regs, EFUSE_ISR, RD_DONE, 1);
efuse_imr_update_irq(s);
return;
}
static uint64_t efuse_cache_load_prew(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
if (val64 & R_EFUSE_CACHE_LOAD_LOAD_MASK) {
efuse_data_sync(s);
ARRAY_FIELD_DP32(s->regs, STATUS, CACHE_DONE, 1);
efuse_imr_update_irq(s);
}
return 0;
}
static uint64_t efuse_pgm_lock_prew(RegisterInfo *reg, uint64_t val64)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(reg->opaque);
/* Ignore all other bits */
val64 = FIELD_EX32(val64, EFUSE_PGM_LOCK, SPK_ID_LOCK);
/* Once the bit is written 1, only reset will clear it to 0 */
val64 |= ARRAY_FIELD_EX32(s->regs, EFUSE_PGM_LOCK, SPK_ID_LOCK);
return val64;
}
static void efuse_aes_crc_postw(RegisterInfo *reg, uint64_t val64)
{
efuse_key_crc_check(reg, val64,
R_STATUS_AES_CRC_PASS_MASK,
R_STATUS_AES_CRC_DONE_MASK,
EFUSE_AES_KEY_START,
EFUSE_AES_KEY_CRC_LK_MASK);
}
static void efuse_aes_u0_crc_postw(RegisterInfo *reg, uint64_t val64)
{
efuse_key_crc_check(reg, val64,
R_STATUS_AES_USER_KEY_0_CRC_PASS_MASK,
R_STATUS_AES_USER_KEY_0_CRC_DONE_MASK,
EFUSE_USER_KEY_0_START,
EFUSE_USER_KEY_0_CRC_LK_MASK);
}
static void efuse_aes_u1_crc_postw(RegisterInfo *reg, uint64_t val64)
{
efuse_key_crc_check(reg, val64,
R_STATUS_AES_USER_KEY_1_CRC_PASS_MASK,
R_STATUS_AES_USER_KEY_1_CRC_DONE_MASK,
EFUSE_USER_KEY_1_START,
EFUSE_USER_KEY_1_CRC_LK_MASK);
}
static uint64_t efuse_wr_lock_prew(RegisterInfo *reg, uint64_t val)
{
return val != R_WR_LOCK_UNLOCK_PASSCODE;
}
static const RegisterAccessInfo efuse_ctrl_regs_info[] = {
{ .name = "WR_LOCK", .addr = A_WR_LOCK,
.reset = 0x1,
.pre_write = efuse_wr_lock_prew,
},{ .name = "CFG", .addr = A_CFG,
.rsvd = 0x9,
},{ .name = "STATUS", .addr = A_STATUS,
.rsvd = 0x8,
.ro = 0xfff,
},{ .name = "EFUSE_PGM_ADDR", .addr = A_EFUSE_PGM_ADDR,
.post_write = efuse_pgm_addr_postw,
},{ .name = "EFUSE_RD_ADDR", .addr = A_EFUSE_RD_ADDR,
.rsvd = 0x1f,
.post_write = efuse_rd_addr_postw,
},{ .name = "EFUSE_RD_DATA", .addr = A_EFUSE_RD_DATA,
.ro = 0xffffffff,
},{ .name = "TPGM", .addr = A_TPGM,
},{ .name = "TRD", .addr = A_TRD,
.reset = 0x19,
},{ .name = "TSU_H_PS", .addr = A_TSU_H_PS,
.reset = 0xff,
},{ .name = "TSU_H_PS_CS", .addr = A_TSU_H_PS_CS,
.reset = 0x11,
},{ .name = "TRDM", .addr = A_TRDM,
.reset = 0x3a,
},{ .name = "TSU_H_CS", .addr = A_TSU_H_CS,
.reset = 0x16,
},{ .name = "EFUSE_ISR", .addr = A_EFUSE_ISR,
.rsvd = 0x7fff8000,
.w1c = 0x80007fff,
.post_write = efuse_isr_postw,
},{ .name = "EFUSE_IMR", .addr = A_EFUSE_IMR,
.reset = 0x80007fff,
.rsvd = 0x7fff8000,
.ro = 0xffffffff,
},{ .name = "EFUSE_IER", .addr = A_EFUSE_IER,
.rsvd = 0x7fff8000,
.pre_write = efuse_ier_prew,
},{ .name = "EFUSE_IDR", .addr = A_EFUSE_IDR,
.rsvd = 0x7fff8000,
.pre_write = efuse_idr_prew,
},{ .name = "EFUSE_CACHE_LOAD", .addr = A_EFUSE_CACHE_LOAD,
.pre_write = efuse_cache_load_prew,
},{ .name = "EFUSE_PGM_LOCK", .addr = A_EFUSE_PGM_LOCK,
.pre_write = efuse_pgm_lock_prew,
},{ .name = "EFUSE_AES_CRC", .addr = A_EFUSE_AES_CRC,
.post_write = efuse_aes_crc_postw,
},{ .name = "EFUSE_AES_USR_KEY0_CRC", .addr = A_EFUSE_AES_USR_KEY0_CRC,
.post_write = efuse_aes_u0_crc_postw,
},{ .name = "EFUSE_AES_USR_KEY1_CRC", .addr = A_EFUSE_AES_USR_KEY1_CRC,
.post_write = efuse_aes_u1_crc_postw,
},{ .name = "EFUSE_PD", .addr = A_EFUSE_PD,
.ro = 0xfffffffe,
},{ .name = "EFUSE_ANLG_OSC_SW_1LP", .addr = A_EFUSE_ANLG_OSC_SW_1LP,
},{ .name = "EFUSE_TEST_CTRL", .addr = A_EFUSE_TEST_CTRL,
.reset = 0x8,
}
};
static void efuse_ctrl_reg_write(void *opaque, hwaddr addr,
uint64_t data, unsigned size)
{
RegisterInfoArray *reg_array = opaque;
XlnxVersalEFuseCtrl *s;
Object *dev;
assert(reg_array != NULL);
dev = reg_array->mem.owner;
assert(dev);
s = XLNX_VERSAL_EFUSE_CTRL(dev);
if (addr != A_WR_LOCK && s->regs[R_WR_LOCK]) {
g_autofree char *path = object_get_canonical_path(OBJECT(s));
qemu_log_mask(LOG_GUEST_ERROR,
"%s[reg_0x%02lx]: Attempt to write locked register.\n",
path, (long)addr);
} else {
register_write_memory(opaque, addr, data, size);
}
}
static void efuse_ctrl_register_reset(RegisterInfo *reg)
{
if (!reg->data || !reg->access) {
return;
}
/* Reset must not trigger some registers' writers */
switch (reg->access->addr) {
case A_EFUSE_AES_CRC:
case A_EFUSE_AES_USR_KEY0_CRC:
case A_EFUSE_AES_USR_KEY1_CRC:
*(uint32_t *)reg->data = reg->access->reset;
return;
}
register_reset(reg);
}
static void efuse_ctrl_reset_hold(Object *obj, ResetType type)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(obj);
unsigned int i;
for (i = 0; i < ARRAY_SIZE(s->regs_info); ++i) {
efuse_ctrl_register_reset(&s->regs_info[i]);
}
efuse_anchor_bits_check(s);
efuse_data_sync(s);
efuse_imr_update_irq(s);
}
static const MemoryRegionOps efuse_ctrl_ops = {
.read = register_read_memory,
.write = efuse_ctrl_reg_write,
.endianness = DEVICE_LITTLE_ENDIAN,
.valid = {
.min_access_size = 4,
.max_access_size = 4,
},
};
static void efuse_ctrl_realize(DeviceState *dev, Error **errp)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(dev);
const uint32_t lks_sz = sizeof(XlnxEFuseLkSpec) / 2;
if (!s->efuse) {
g_autofree char *path = object_get_canonical_path(OBJECT(s));
error_setg(errp, "%s.efuse: link property not connected to XLNX-EFUSE",
path);
return;
}
/* Sort property-defined pgm-locks for bsearch lookup */
if ((s->extra_pg0_lock_n16 % lks_sz) != 0) {
g_autofree char *path = object_get_canonical_path(OBJECT(s));
error_setg(errp,
"%s.pg0-lock: array property item-count not multiple of %u",
path, lks_sz);
return;
}
efuse_lk_spec_sort(s);
}
static void efuse_ctrl_init(Object *obj)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(obj);
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
s->reg_array =
register_init_block32(DEVICE(obj), efuse_ctrl_regs_info,
ARRAY_SIZE(efuse_ctrl_regs_info),
s->regs_info, s->regs,
&efuse_ctrl_ops,
XLNX_VERSAL_EFUSE_CTRL_ERR_DEBUG,
R_MAX * 4);
sysbus_init_mmio(sbd, &s->reg_array->mem);
sysbus_init_irq(sbd, &s->irq_efuse_imr);
}
static void efuse_ctrl_finalize(Object *obj)
{
XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(obj);
register_finalize_block(s->reg_array);
g_free(s->extra_pg0_lock_spec);
}
static const VMStateDescription vmstate_efuse_ctrl = {
.name = TYPE_XLNX_VERSAL_EFUSE_CTRL,
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_UINT32_ARRAY(regs, XlnxVersalEFuseCtrl, R_MAX),
VMSTATE_END_OF_LIST(),
}
};
static Property efuse_ctrl_props[] = {
DEFINE_PROP_LINK("efuse",
XlnxVersalEFuseCtrl, efuse,
TYPE_XLNX_EFUSE, XlnxEFuse *),
DEFINE_PROP_ARRAY("pg0-lock",
XlnxVersalEFuseCtrl, extra_pg0_lock_n16,
extra_pg0_lock_spec, qdev_prop_uint16, uint16_t),
DEFINE_PROP_END_OF_LIST(),
};
static void efuse_ctrl_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
ResettableClass *rc = RESETTABLE_CLASS(klass);
rc->phases.hold = efuse_ctrl_reset_hold;
dc->realize = efuse_ctrl_realize;
dc->vmsd = &vmstate_efuse_ctrl;
device_class_set_props(dc, efuse_ctrl_props);
}
static const TypeInfo efuse_ctrl_info = {
.name = TYPE_XLNX_VERSAL_EFUSE_CTRL,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(XlnxVersalEFuseCtrl),
.class_init = efuse_ctrl_class_init,
.instance_init = efuse_ctrl_init,
.instance_finalize = efuse_ctrl_finalize,
};
static void efuse_ctrl_register_types(void)
{
type_register_static(&efuse_ctrl_info);
}
type_init(efuse_ctrl_register_types)
/*
* Retrieve a row, with unreadable bits returned as 0.
*/
uint32_t xlnx_versal_efuse_read_row(XlnxEFuse *efuse,
uint32_t bit, bool *denied)
{
bool dummy;
if (!denied) {
denied = &dummy;
}
if (bit >= EFUSE_RD_BLOCKED_START && bit <= EFUSE_RD_BLOCKED_END) {
*denied = true;
return 0;
}
*denied = false;
return xlnx_efuse_get_row(efuse, bit);
}
|