Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/cxl/cxl-component-utils.c
Line
Count
Source
1
/*
2
 * CXL Utility library for components
3
 *
4
 * Copyright(C) 2020 Intel Corporation.
5
 *
6
 * This work is licensed under the terms of the GNU GPL, version 2. See the
7
 * COPYING file in the top-level directory.
8
 */
9
10
#include "qemu/osdep.h"
11
#include "qemu/log.h"
12
#include "qapi/error.h"
13
#include "hw/pci/pci.h"
14
#include "hw/cxl/cxl.h"
15
16
/* CXL r3.1 Section 8.2.4.20.1 CXL HDM Decoder Capability Register */
17
int cxl_decoder_count_enc(int count)
18
0
{
19
0
    switch (count) {
20
0
    case 1: return 0x0;
21
0
    case 2: return 0x1;
22
0
    case 4: return 0x2;
23
0
    case 6: return 0x3;
24
0
    case 8: return 0x4;
25
0
    case 10: return 0x5;
26
    /* Switches and Host Bridges may have more than 10 decoders */
27
0
    case 12: return 0x6;
28
0
    case 14: return 0x7;
29
0
    case 16: return 0x8;
30
0
    case 20: return 0x9;
31
0
    case 24: return 0xa;
32
0
    case 28: return 0xb;
33
0
    case 32: return 0xc;
34
0
    }
35
0
    return 0;
36
0
}
37
38
int cxl_decoder_count_dec(int enc_cnt)
39
0
{
40
0
    switch (enc_cnt) {
41
0
    case 0x0: return 1;
42
0
    case 0x1: return 2;
43
0
    case 0x2: return 4;
44
0
    case 0x3: return 6;
45
0
    case 0x4: return 8;
46
0
    case 0x5: return 10;
47
    /* Switches and Host Bridges may have more than 10 decoders */
48
0
    case 0x6: return 12;
49
0
    case 0x7: return 14;
50
0
    case 0x8: return 16;
51
0
    case 0x9: return 20;
52
0
    case 0xa: return 24;
53
0
    case 0xb: return 28;
54
0
    case 0xc: return 32;
55
0
    }
56
0
    return 0;
57
0
}
58
59
hwaddr cxl_decode_ig(int ig)
60
0
{
61
0
    return 1ULL << (ig + 8);
62
0
}
63
64
static uint64_t cxl_cache_mem_read_reg(void *opaque, hwaddr offset,
65
                                       unsigned size)
66
0
{
67
0
    CXLComponentState *cxl_cstate = opaque;
68
0
    ComponentRegisters *cregs = &cxl_cstate->crb;
69
70
0
    switch (size) {
71
0
    case 4: {
72
0
        QEMU_BUILD_BUG_ON(sizeof(*cregs->cache_mem_registers) != 4);
73
74
0
        if (offset == A_CXL_BI_RT_STATUS ||
75
0
            offset == A_CXL_BI_DECODER_STATUS) {
76
0
            int type;
77
0
            uint64_t started;
78
79
0
            type = (offset == A_CXL_BI_RT_STATUS) ?
80
0
                    CXL_BISTATE_RT : CXL_BISTATE_DECODER;
81
0
            started = cxl_cstate->bi_state[type].last_commit;
82
83
0
            if (started) {
84
0
                uint32_t *cache_mem = cregs->cache_mem_registers;
85
0
                uint32_t val = cache_mem[offset / 4];
86
0
                uint64_t now;
87
0
                int set;
88
89
0
                now = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
90
                /* arbitrary 100 ms to do the commit */
91
0
                set = !!(now >= started + 100);
92
93
0
                if (offset == A_CXL_BI_RT_STATUS) {
94
0
                    val = FIELD_DP32(val, CXL_BI_RT_STATUS, COMMITTED, set);
95
0
                } else {
96
0
                    val = FIELD_DP32(val, CXL_BI_DECODER_STATUS, COMMITTED,
97
0
                                     set);
98
0
                }
99
0
                stl_le_p((uint8_t *)cache_mem + offset, val);
100
0
            }
101
0
        }
102
103
0
        return cregs->cache_mem_registers[offset / 4];
104
0
    }
105
0
    case 8:
106
0
        qemu_log_mask(LOG_UNIMP,
107
0
                      "CXL 8 byte cache mem registers not implemented\n");
108
0
        return 0;
109
0
    default:
110
        /*
111
         * In line with specification limitaions on access sizes, this
112
         * routine is not called with other sizes.
113
         */
114
0
        g_assert_not_reached();
115
0
    }
116
0
}
117
118
static void dumb_hdm_handler(CXLComponentState *cxl_cstate, hwaddr offset,
119
                             uint32_t value)
120
0
{
121
0
    ComponentRegisters *cregs = &cxl_cstate->crb;
122
0
    uint32_t *cache_mem = cregs->cache_mem_registers;
123
0
    bool should_commit = false;
124
0
    bool should_uncommit = false;
125
126
0
    switch (offset) {
127
0
    case A_CXL_HDM_DECODER0_CTRL:
128
0
    case A_CXL_HDM_DECODER1_CTRL:
129
0
    case A_CXL_HDM_DECODER2_CTRL:
130
0
    case A_CXL_HDM_DECODER3_CTRL:
131
0
        should_commit = FIELD_EX32(value, CXL_HDM_DECODER0_CTRL, COMMIT);
132
0
        should_uncommit = !should_commit;
133
0
        break;
134
0
    default:
135
0
        break;
136
0
    }
137
138
0
    if (should_commit) {
139
0
        value = FIELD_DP32(value, CXL_HDM_DECODER0_CTRL, ERR, 0);
140
0
        value = FIELD_DP32(value, CXL_HDM_DECODER0_CTRL, COMMITTED, 1);
141
0
    } else if (should_uncommit) {
142
0
        value = FIELD_DP32(value, CXL_HDM_DECODER0_CTRL, ERR, 0);
143
0
        value = FIELD_DP32(value, CXL_HDM_DECODER0_CTRL, COMMITTED, 0);
144
0
    }
145
0
    stl_le_p((uint8_t *)cache_mem + offset, value);
146
147
0
    if (should_commit) {
148
0
        cfmws_update_non_interleaved(true);
149
0
    } else if (should_uncommit) {
150
0
        cfmws_update_non_interleaved(false);
151
0
    }
152
0
}
153
154
static void bi_handler(CXLComponentState *cxl_cstate, hwaddr offset,
155
                            uint32_t value)
156
0
{
157
0
    ComponentRegisters *cregs = &cxl_cstate->crb;
158
0
    uint32_t sts, *cache_mem = cregs->cache_mem_registers;
159
0
    bool to_commit = false;
160
0
    int type = 0; /* Unused value - work around for compiler warning */
161
162
0
    switch (offset) {
163
0
    case A_CXL_BI_RT_CTRL:
164
0
        to_commit = FIELD_EX32(value, CXL_BI_RT_CTRL, COMMIT);
165
0
        if (to_commit) {
166
0
            sts = cxl_cache_mem_read_reg(cxl_cstate,
167
0
                                         R_CXL_BI_RT_STATUS, 4);
168
0
            sts = FIELD_DP32(sts, CXL_BI_RT_STATUS, COMMITTED, 0);
169
0
            stl_le_p((uint8_t *)cache_mem + R_CXL_BI_RT_STATUS, sts);
170
0
            type = CXL_BISTATE_RT;
171
0
        }
172
0
        break;
173
0
    case A_CXL_BI_DECODER_CTRL:
174
0
        to_commit = FIELD_EX32(value, CXL_BI_DECODER_CTRL, COMMIT);
175
0
        if (to_commit) {
176
0
            sts = cxl_cache_mem_read_reg(cxl_cstate,
177
0
                                         R_CXL_BI_DECODER_STATUS, 4);
178
0
            sts = FIELD_DP32(sts, CXL_BI_DECODER_STATUS, COMMITTED, 0);
179
0
            stl_le_p((uint8_t *)cache_mem + R_CXL_BI_DECODER_STATUS, sts);
180
0
            type = CXL_BISTATE_DECODER;
181
0
        }
182
0
        break;
183
0
    default:
184
0
        break;
185
0
    }
186
187
0
    if (to_commit) {
188
0
        cxl_cstate->bi_state[type].last_commit =
189
0
                qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
190
0
    }
191
192
0
    stl_le_p((uint8_t *)cache_mem + offset, value);
193
0
}
194
195
static void cxl_cache_mem_write_reg(void *opaque, hwaddr offset, uint64_t value,
196
                                    unsigned size)
197
0
{
198
0
    CXLComponentState *cxl_cstate = opaque;
199
0
    ComponentRegisters *cregs = &cxl_cstate->crb;
200
0
    uint32_t mask;
201
202
0
    switch (size) {
203
0
    case 4: {
204
0
        QEMU_BUILD_BUG_ON(sizeof(*cregs->cache_mem_regs_write_mask) != 4);
205
0
        QEMU_BUILD_BUG_ON(sizeof(*cregs->cache_mem_registers) != 4);
206
0
        mask = cregs->cache_mem_regs_write_mask[offset / 4];
207
0
        value &= mask;
208
        /* RO bits should remain constant. Done by reading existing value */
209
0
        value |= ~mask & cregs->cache_mem_registers[offset / 4];
210
0
        if (cregs->special_ops && cregs->special_ops->write) {
211
0
            cregs->special_ops->write(cxl_cstate, offset, value, size);
212
0
            return;
213
0
        }
214
215
0
        if (offset >= A_CXL_HDM_DECODER_CAPABILITY &&
216
0
            offset <= A_CXL_HDM_DECODER3_TARGET_LIST_HI) {
217
0
            dumb_hdm_handler(cxl_cstate, offset, value);
218
0
        } else if (offset == A_CXL_BI_RT_CTRL ||
219
0
                   offset == A_CXL_BI_DECODER_CTRL) {
220
0
            bi_handler(cxl_cstate, offset, value);
221
0
        } else {
222
0
            cregs->cache_mem_registers[offset / 4] = value;
223
0
        }
224
0
        return;
225
0
    }
226
0
    case 8:
227
0
        qemu_log_mask(LOG_UNIMP,
228
0
                      "CXL 8 byte cache mem registers not implemented\n");
229
0
        return;
230
0
    default:
231
        /*
232
         * In line with specification limitaions on access sizes, this
233
         * routine is not called with other sizes.
234
         */
235
0
        g_assert_not_reached();
236
0
    }
237
0
}
238
239
/*
240
 * CXL r3.1 Section 8.2.3: Component Register Layout and Definition
241
 *   The access restrictions specified in Section 8.2.2 also apply to CXL 2.0
242
 *   Component Registers.
243
 *
244
 * CXL r3.1 Section 8.2.2: Accessing Component Registers
245
 *   • A 32 bit register shall be accessed as a 4 Bytes quantity. Partial
246
 *   reads are not permitted.
247
 *   • A 64 bit register shall be accessed as a 8 Bytes quantity. Partial
248
 *   reads are not permitted.
249
 *
250
 * As of the spec defined today, only 4 byte registers exist.
251
 */
252
static const MemoryRegionOps cache_mem_ops = {
253
    .read = cxl_cache_mem_read_reg,
254
    .write = cxl_cache_mem_write_reg,
255
    .endianness = DEVICE_LITTLE_ENDIAN,
256
    .valid = {
257
        .min_access_size = 4,
258
        .max_access_size = 8,
259
        .unaligned = false,
260
    },
261
    .impl = {
262
        .min_access_size = 4,
263
        .max_access_size = 8,
264
    },
265
};
266
267
void cxl_component_register_block_init(Object *obj,
268
                                       CXLComponentState *cxl_cstate,
269
                                       const char *type)
270
0
{
271
0
    ComponentRegisters *cregs = &cxl_cstate->crb;
272
273
0
    memory_region_init(&cregs->component_registers, obj, type,
274
0
                       CXL2_COMPONENT_BLOCK_SIZE);
275
276
    /* io registers controls link which we don't care about in QEMU */
277
0
    memory_region_init_io(&cregs->io, obj, NULL, NULL, ".io",
278
0
                          CXL2_COMPONENT_IO_REGION_SIZE);
279
0
    memory_region_init_io(&cregs->cache_mem, obj, &cache_mem_ops, cxl_cstate,
280
0
                          ".cache_mem", CXL2_COMPONENT_CM_REGION_SIZE);
281
282
0
    memory_region_add_subregion(&cregs->component_registers, 0, &cregs->io);
283
0
    memory_region_add_subregion(&cregs->component_registers,
284
0
                                CXL2_COMPONENT_IO_REGION_SIZE,
285
0
                                &cregs->cache_mem);
286
0
}
287
288
static void ras_init_common(uint32_t *reg_state, uint32_t *write_msk)
289
0
{
290
    /*
291
     * Error status is RW1C but given bits are not yet set, it can
292
     * be handled as RO.
293
     */
294
0
    stl_le_p(reg_state + R_CXL_RAS_UNC_ERR_STATUS, 0);
295
0
    stl_le_p(write_msk + R_CXL_RAS_UNC_ERR_STATUS, 0x1cfff);
296
    /* Bits 12-13 and 17-31 reserved in CXL 2.0 */
297
0
    stl_le_p(reg_state + R_CXL_RAS_UNC_ERR_MASK, 0x1cfff);
298
0
    stl_le_p(write_msk + R_CXL_RAS_UNC_ERR_MASK, 0x1cfff);
299
0
    stl_le_p(reg_state + R_CXL_RAS_UNC_ERR_SEVERITY, 0x1cfff);
300
0
    stl_le_p(write_msk + R_CXL_RAS_UNC_ERR_SEVERITY, 0x1cfff);
301
0
    stl_le_p(reg_state + R_CXL_RAS_COR_ERR_STATUS, 0);
302
0
    stl_le_p(write_msk + R_CXL_RAS_COR_ERR_STATUS, 0x7f);
303
0
    stl_le_p(reg_state + R_CXL_RAS_COR_ERR_MASK, 0x7f);
304
0
    stl_le_p(write_msk + R_CXL_RAS_COR_ERR_MASK, 0x7f);
305
    /* CXL switches and devices must set */
306
0
    stl_le_p(reg_state + R_CXL_RAS_ERR_CAP_CTRL, 0x200);
307
0
}
308
309
static void hdm_init_common(uint32_t *reg_state, uint32_t *write_msk,
310
                            enum reg_type type, bool bi)
311
0
{
312
0
    int decoder_count = CXL_HDM_DECODER_COUNT;
313
0
    int hdm_inc = R_CXL_HDM_DECODER1_BASE_LO - R_CXL_HDM_DECODER0_BASE_LO;
314
0
    int i;
315
316
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, DECODER_COUNT,
317
0
                     cxl_decoder_count_enc(decoder_count));
318
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, TARGET_COUNT, 1);
319
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, INTERLEAVE_256B, 1);
320
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, INTERLEAVE_4K, 1);
321
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY,
322
0
                     POISON_ON_ERR_CAP, 0);
323
0
    if (type == CXL2_TYPE3_DEVICE) {
324
0
        ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, 3_6_12_WAY, 1);
325
0
        ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, 16_WAY, 1);
326
0
    } else {
327
0
        ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, 3_6_12_WAY, 0);
328
0
        ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, 16_WAY, 0);
329
0
    }
330
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, UIO, 0);
331
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY,
332
0
                     UIO_DECODER_COUNT, 0);
333
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, MEMDATA_NXM_CAP, 0);
334
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY,
335
0
                     SUPPORTED_COHERENCY_MODEL,
336
                     /* host+dev or Unknown */
337
0
                     type == CXL2_TYPE3_DEVICE && bi ? 3 : 0);
338
0
    ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_GLOBAL_CONTROL,
339
0
                     HDM_DECODER_ENABLE, 0);
340
0
    write_msk[R_CXL_HDM_DECODER_GLOBAL_CONTROL] = 0x3;
341
0
    for (i = 0; i < decoder_count; i++) {
342
0
        write_msk[R_CXL_HDM_DECODER0_BASE_LO + i * hdm_inc] = 0xf0000000;
343
0
        write_msk[R_CXL_HDM_DECODER0_BASE_HI + i * hdm_inc] = 0xffffffff;
344
0
        write_msk[R_CXL_HDM_DECODER0_SIZE_LO + i * hdm_inc] = 0xf0000000;
345
0
        write_msk[R_CXL_HDM_DECODER0_SIZE_HI + i * hdm_inc] = 0xffffffff;
346
0
        write_msk[R_CXL_HDM_DECODER0_CTRL + i * hdm_inc] = 0x13ff;
347
0
        if (type == CXL2_DEVICE ||
348
0
            type == CXL2_TYPE3_DEVICE ||
349
0
            type == CXL2_LOGICAL_DEVICE) {
350
0
            write_msk[R_CXL_HDM_DECODER0_TARGET_LIST_LO + i * hdm_inc] =
351
0
                0xf0000000;
352
0
        } else {
353
0
            write_msk[R_CXL_HDM_DECODER0_TARGET_LIST_LO + i * hdm_inc] =
354
0
                0xffffffff;
355
0
        }
356
0
        write_msk[R_CXL_HDM_DECODER0_TARGET_LIST_HI + i * hdm_inc] = 0xffffffff;
357
0
    }
358
0
}
359
360
static void bi_rt_init_common(uint32_t *reg_state, uint32_t *write_msk)
361
0
{
362
    /* switch usp must commit the new BI-ID, timeout of 2secs */
363
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_RT_CAPABILITY, EXPLICIT_COMMIT, 1);
364
365
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_RT_CTRL, COMMIT, 0);
366
0
    write_msk[R_CXL_BI_RT_CTRL] = 0x1;
367
368
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_RT_STATUS, COMMITTED, 0);
369
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_RT_STATUS, ERR_NOT_COMMITTED, 0);
370
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_RT_STATUS, COMMIT_TMO_SCALE, 0x6);
371
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_RT_STATUS, COMMIT_TMO_BASE, 0x2);
372
0
}
373
374
static void bi_decoder_init_common(uint32_t *reg_state, uint32_t *write_msk,
375
                                   enum reg_type type)
376
0
{
377
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_CAPABILITY, HDM_D, 0);
378
    /* switch dsp must commit the new BI-ID, timeout of 2secs */
379
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_CAPABILITY, EXPLICIT_COMMIT,
380
0
                     (type != CXL2_ROOT_PORT && type != CXL2_TYPE3_DEVICE));
381
382
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_CTRL, BI_FW, 0);
383
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_CTRL, BI_ENABLE, 0);
384
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_CTRL, COMMIT, 0);
385
0
    write_msk[R_CXL_BI_DECODER_CTRL] = 0x7;
386
387
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_STATUS, COMMITTED, 0);
388
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_STATUS, ERR_NOT_COMMITTED, 0);
389
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_STATUS, COMMIT_TMO_SCALE, 0x6);
390
0
    ARRAY_FIELD_DP32(reg_state, CXL_BI_DECODER_STATUS, COMMIT_TMO_BASE, 0x2);
391
0
}
392
393
void cxl_component_register_init_common(uint32_t *reg_state,
394
                                        uint32_t *write_msk,
395
                                        enum reg_type type,
396
                                        bool bi)
397
0
{
398
0
    int caps = 0;
399
400
0
    memset(reg_state, 0, CXL2_COMPONENT_CM_REGION_SIZE);
401
402
    /* CXL Capability Header Register */
403
0
    ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, ID, 1);
404
0
    ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, VERSION,
405
0
        CXL_CAPABILITY_VERSION);
406
0
    ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, CACHE_MEM_VERSION, 1);
407
408
0
#define init_cap_reg(reg, id, version)                                        \
409
0
    do {                                                                      \
410
0
        int which = CXL_##reg##_CAP_HDR_IDX;                                  \
411
0
        if (CXL_##reg##_CAP_HDR_IDX > caps)                                   \
412
0
            caps = CXL_##reg##_CAP_HDR_IDX;                                   \
413
0
        reg_state[which] = FIELD_DP32(reg_state[which],                       \
414
0
                                      CXL_##reg##_CAPABILITY_HEADER, ID, id); \
415
0
        reg_state[which] =                                                    \
416
0
            FIELD_DP32(reg_state[which], CXL_##reg##_CAPABILITY_HEADER,       \
417
0
                       VERSION, version);                                     \
418
0
        reg_state[which] =                                                    \
419
0
            FIELD_DP32(reg_state[which], CXL_##reg##_CAPABILITY_HEADER, PTR,  \
420
0
                       CXL_##reg##_REGISTERS_OFFSET);                         \
421
0
    } while (0)
422
423
    /* CXL r3.2 8.2.4 Table 8-22 */
424
0
    switch (type) {
425
0
    case CXL2_ROOT_PORT:
426
0
    case CXL2_RC:
427
        /* + Extended Security, + Snoop */
428
0
        init_cap_reg(EXTSEC, 6, 1);
429
0
        init_cap_reg(SNOOP, 8, 1);
430
        /* fallthrough */
431
0
    case CXL2_UPSTREAM_PORT:
432
0
    case CXL2_TYPE3_DEVICE:
433
0
    case CXL2_LOGICAL_DEVICE:
434
        /* + HDM */
435
0
        init_cap_reg(HDM, 5, 1);
436
0
        hdm_init_common(reg_state, write_msk, type, bi);
437
        /* fallthrough */
438
0
    case CXL2_DOWNSTREAM_PORT:
439
0
    case CXL2_DEVICE:
440
        /* RAS, Link */
441
0
        if (type != CXL2_RC) {
442
0
            init_cap_reg(RAS, 2, 2);
443
0
            ras_init_common(reg_state, write_msk);
444
0
        }
445
0
        init_cap_reg(LINK, 4, 2);
446
0
        break;
447
0
    default:
448
0
        abort();
449
0
    }
450
451
    /* back invalidate */
452
0
    if (bi) {
453
0
        switch (type) {
454
0
        case CXL2_UPSTREAM_PORT:
455
0
            init_cap_reg(BI_RT, 11, CXL_BI_RT_CAP_VERSION);
456
0
            bi_rt_init_common(reg_state, write_msk);
457
0
            break;
458
0
        case CXL2_ROOT_PORT:
459
0
        case CXL2_DOWNSTREAM_PORT:
460
0
        case CXL2_TYPE3_DEVICE:
461
0
            init_cap_reg(BI_DECODER, 12, CXL_BI_DECODER_CAP_VERSION);
462
0
            bi_decoder_init_common(reg_state, write_msk, type);
463
0
            break;
464
0
        default:
465
0
            break;
466
0
        }
467
0
    }
468
469
0
    ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, ARRAY_SIZE, caps);
470
0
#undef init_cap_reg
471
0
}
472
473
/*
474
 * Helper to creates a DVSEC header for a CXL entity. The caller is responsible
475
 * for tracking the valid offset.
476
 *
477
 * This function will build the DVSEC header on behalf of the caller and then
478
 * copy in the remaining data for the vendor specific bits.
479
 * It will also set up appropriate write masks.
480
 */
481
void cxl_component_create_dvsec(CXLComponentState *cxl,
482
                                enum reg_type cxl_dev_type, uint16_t length,
483
                                uint16_t type, uint8_t rev, uint8_t *body)
484
0
{
485
0
    PCIDevice *pdev = cxl->pdev;
486
0
    uint16_t offset = cxl->dvsec_offset;
487
0
    uint8_t *wmask = pdev->wmask;
488
489
0
    assert(offset >= PCI_CFG_SPACE_SIZE &&
490
0
           ((offset + length) < PCI_CFG_SPACE_EXP_SIZE));
491
0
    assert((length & 0xf000) == 0);
492
0
    assert((rev & ~0xf) == 0);
493
494
    /* Create the DVSEC in the MCFG space */
495
0
    pcie_add_capability(pdev, PCI_EXT_CAP_ID_DVSEC, 1, offset, length);
496
0
    pci_set_long(pdev->config + offset + PCIE_DVSEC_HEADER1_OFFSET,
497
0
                 (length << 20) | (rev << 16) | CXL_VENDOR_ID);
498
0
    pci_set_word(pdev->config + offset + PCIE_DVSEC_ID_OFFSET, type);
499
0
    memcpy(pdev->config + offset + sizeof(DVSECHeader),
500
0
           body + sizeof(DVSECHeader),
501
0
           length - sizeof(DVSECHeader));
502
503
    /* Configure write masks */
504
0
    switch (type) {
505
0
    case PCIE_CXL_DEVICE_DVSEC:
506
        /* Cntrl RW Lock - so needs explicit blocking when lock is set */
507
0
        wmask[offset + offsetof(CXLDVSECDevice, ctrl)] = 0xFD;
508
0
        wmask[offset + offsetof(CXLDVSECDevice, ctrl) + 1] = 0x4F;
509
        /* Status is RW1CS */
510
0
        wmask[offset + offsetof(CXLDVSECDevice, ctrl2)] = 0x0F;
511
       /* Lock is RW Once */
512
0
        wmask[offset + offsetof(CXLDVSECDevice, lock)] = 0x01;
513
        /* range1/2_base_high/low is RW Lock */
514
0
        wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi)] = 0xFF;
515
0
        wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi) + 1] = 0xFF;
516
0
        wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi) + 2] = 0xFF;
517
0
        wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi) + 3] = 0xFF;
518
0
        wmask[offset + offsetof(CXLDVSECDevice, range1_base_lo) + 3] = 0xF0;
519
0
        wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi)] = 0xFF;
520
0
        wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi) + 1] = 0xFF;
521
0
        wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi) + 2] = 0xFF;
522
0
        wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi) + 3] = 0xFF;
523
0
        wmask[offset + offsetof(CXLDVSECDevice, range2_base_lo) + 3] = 0xF0;
524
0
        break;
525
0
    case NON_CXL_FUNCTION_MAP_DVSEC:
526
0
        break; /* Not yet implemented */
527
0
    case EXTENSIONS_PORT_DVSEC:
528
0
        wmask[offset + offsetof(CXLDVSECPortExt, control)] = 0x0F;
529
0
        wmask[offset + offsetof(CXLDVSECPortExt, control) + 1] = 0x40;
530
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_bus_base)] = 0xFF;
531
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_bus_limit)] = 0xFF;
532
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_memory_base)] = 0xF0;
533
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_memory_base) + 1] = 0xFF;
534
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_memory_limit)] = 0xF0;
535
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_memory_limit) + 1] = 0xFF;
536
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_base)] = 0xF0;
537
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_base) + 1] = 0xFF;
538
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_limit)] = 0xF0;
539
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_limit) + 1] =
540
0
            0xFF;
541
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_base_high)] =
542
0
            0xFF;
543
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_base_high) + 1] =
544
0
            0xFF;
545
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_base_high) + 2] =
546
0
            0xFF;
547
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_base_high) + 3] =
548
0
            0xFF;
549
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_limit_high)] =
550
0
            0xFF;
551
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_limit_high) + 1] =
552
0
            0xFF;
553
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_limit_high) + 2] =
554
0
            0xFF;
555
0
        wmask[offset + offsetof(CXLDVSECPortExt, alt_prefetch_limit_high) + 3] =
556
0
            0xFF;
557
0
        break;
558
0
    case GPF_PORT_DVSEC:
559
0
        wmask[offset + offsetof(CXLDVSECPortGPF, phase1_ctrl)] = 0x0F;
560
0
        wmask[offset + offsetof(CXLDVSECPortGPF, phase1_ctrl) + 1] = 0x0F;
561
0
        wmask[offset + offsetof(CXLDVSECPortGPF, phase2_ctrl)] = 0x0F;
562
0
        wmask[offset + offsetof(CXLDVSECPortGPF, phase2_ctrl) + 1] = 0x0F;
563
0
        break;
564
0
    case GPF_DEVICE_DVSEC:
565
0
        wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_duration)] = 0x0F;
566
0
        wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_duration) + 1] = 0x0F;
567
0
        wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power)] = 0xFF;
568
0
        wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power) + 1] = 0xFF;
569
0
        wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power) + 2] = 0xFF;
570
0
        wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power) + 3] = 0xFF;
571
0
        break;
572
0
    case PCIE_FLEXBUS_PORT_DVSEC:
573
0
        switch (cxl_dev_type) {
574
0
        case CXL2_ROOT_PORT:
575
            /* No MLD */
576
0
            wmask[offset + offsetof(CXLDVSECPortFlexBus, ctrl)] = 0xbd;
577
0
            break;
578
0
        case CXL2_DOWNSTREAM_PORT:
579
0
            wmask[offset + offsetof(CXLDVSECPortFlexBus, ctrl)] = 0xfd;
580
0
            break;
581
0
        default: /* Registers are RO for other component types */
582
0
            break;
583
0
        }
584
        /* There are rw1cs bits in the status register but never set */
585
0
        break;
586
0
    }
587
588
    /* Update state for future DVSEC additions */
589
0
    range_init_nofail(&cxl->dvsecs[type], cxl->dvsec_offset, length);
590
0
    cxl->dvsec_offset += length;
591
0
}
592
593
/* CXL r3.1 Section 8.2.4.20.7 CXL HDM Decoder n Control Register */
594
uint8_t cxl_interleave_ways_enc(int iw, Error **errp)
595
0
{
596
0
    switch (iw) {
597
0
    case 1: return 0x0;
598
0
    case 2: return 0x1;
599
0
    case 4: return 0x2;
600
0
    case 8: return 0x3;
601
0
    case 16: return 0x4;
602
0
    case 3: return 0x8;
603
0
    case 6: return 0x9;
604
0
    case 12: return 0xa;
605
0
    default:
606
0
        error_setg(errp, "Interleave ways: %d not supported", iw);
607
0
        return 0;
608
0
    }
609
0
}
610
611
int cxl_interleave_ways_dec(uint8_t iw_enc, Error **errp)
612
0
{
613
0
    switch (iw_enc) {
614
0
    case 0x0: return 1;
615
0
    case 0x1: return 2;
616
0
    case 0x2: return 4;
617
0
    case 0x3: return 8;
618
0
    case 0x4: return 16;
619
0
    case 0x8: return 3;
620
0
    case 0x9: return 6;
621
0
    case 0xa: return 12;
622
0
    default:
623
0
        error_setg(errp, "Encoded interleave ways: %d not supported", iw_enc);
624
0
        return 0;
625
0
    }
626
0
}
627
628
uint8_t cxl_interleave_granularity_enc(uint64_t gran, Error **errp)
629
0
{
630
0
    switch (gran) {
631
0
    case 256: return 0;
632
0
    case 512: return 1;
633
0
    case 1024: return 2;
634
0
    case 2048: return 3;
635
0
    case 4096: return 4;
636
0
    case 8192: return 5;
637
0
    case 16384: return 6;
638
0
    default:
639
0
        error_setg(errp, "Interleave granularity: %" PRIu64 " invalid", gran);
640
0
        return 0;
641
0
    }
642
0
}