xref: /openbmc/qemu/hw/cxl/cxl-component-utils.c (revision e8d1e0cd)
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 static uint64_t cxl_cache_mem_read_reg(void *opaque, hwaddr offset,
17                                        unsigned size)
18 {
19     CXLComponentState *cxl_cstate = opaque;
20     ComponentRegisters *cregs = &cxl_cstate->crb;
21 
22     if (size == 8) {
23         qemu_log_mask(LOG_UNIMP,
24                       "CXL 8 byte cache mem registers not implemented\n");
25         return 0;
26     }
27 
28     if (cregs->special_ops && cregs->special_ops->read) {
29         return cregs->special_ops->read(cxl_cstate, offset, size);
30     } else {
31         return cregs->cache_mem_registers[offset / sizeof(*cregs->cache_mem_registers)];
32     }
33 }
34 
35 static void dumb_hdm_handler(CXLComponentState *cxl_cstate, hwaddr offset,
36                              uint32_t value)
37 {
38     ComponentRegisters *cregs = &cxl_cstate->crb;
39     uint32_t *cache_mem = cregs->cache_mem_registers;
40     bool should_commit = false;
41 
42     switch (offset) {
43     case A_CXL_HDM_DECODER0_CTRL:
44         should_commit = FIELD_EX32(value, CXL_HDM_DECODER0_CTRL, COMMIT);
45         break;
46     default:
47         break;
48     }
49 
50     memory_region_transaction_begin();
51     stl_le_p((uint8_t *)cache_mem + offset, value);
52     if (should_commit) {
53         ARRAY_FIELD_DP32(cache_mem, CXL_HDM_DECODER0_CTRL, COMMIT, 0);
54         ARRAY_FIELD_DP32(cache_mem, CXL_HDM_DECODER0_CTRL, ERR, 0);
55         ARRAY_FIELD_DP32(cache_mem, CXL_HDM_DECODER0_CTRL, COMMITTED, 1);
56     }
57     memory_region_transaction_commit();
58 }
59 
60 static void cxl_cache_mem_write_reg(void *opaque, hwaddr offset, uint64_t value,
61                                     unsigned size)
62 {
63     CXLComponentState *cxl_cstate = opaque;
64     ComponentRegisters *cregs = &cxl_cstate->crb;
65     uint32_t mask;
66 
67     if (size == 8) {
68         qemu_log_mask(LOG_UNIMP,
69                       "CXL 8 byte cache mem registers not implemented\n");
70         return;
71     }
72     mask = cregs->cache_mem_regs_write_mask[offset / sizeof(*cregs->cache_mem_regs_write_mask)];
73     value &= mask;
74     /* RO bits should remain constant. Done by reading existing value */
75     value |= ~mask & cregs->cache_mem_registers[offset / sizeof(*cregs->cache_mem_registers)];
76     if (cregs->special_ops && cregs->special_ops->write) {
77         cregs->special_ops->write(cxl_cstate, offset, value, size);
78         return;
79     }
80 
81     if (offset >= A_CXL_HDM_DECODER_CAPABILITY &&
82         offset <= A_CXL_HDM_DECODER0_TARGET_LIST_HI) {
83         dumb_hdm_handler(cxl_cstate, offset, value);
84     } else {
85         cregs->cache_mem_registers[offset / sizeof(*cregs->cache_mem_registers)] = value;
86     }
87 }
88 
89 /*
90  * 8.2.3
91  *   The access restrictions specified in Section 8.2.2 also apply to CXL 2.0
92  *   Component Registers.
93  *
94  * 8.2.2
95  *   • A 32 bit register shall be accessed as a 4 Bytes quantity. Partial
96  *   reads are not permitted.
97  *   • A 64 bit register shall be accessed as a 8 Bytes quantity. Partial
98  *   reads are not permitted.
99  *
100  * As of the spec defined today, only 4 byte registers exist.
101  */
102 static const MemoryRegionOps cache_mem_ops = {
103     .read = cxl_cache_mem_read_reg,
104     .write = cxl_cache_mem_write_reg,
105     .endianness = DEVICE_LITTLE_ENDIAN,
106     .valid = {
107         .min_access_size = 4,
108         .max_access_size = 8,
109         .unaligned = false,
110     },
111     .impl = {
112         .min_access_size = 4,
113         .max_access_size = 8,
114     },
115 };
116 
117 void cxl_component_register_block_init(Object *obj,
118                                        CXLComponentState *cxl_cstate,
119                                        const char *type)
120 {
121     ComponentRegisters *cregs = &cxl_cstate->crb;
122 
123     memory_region_init(&cregs->component_registers, obj, type,
124                        CXL2_COMPONENT_BLOCK_SIZE);
125 
126     /* io registers controls link which we don't care about in QEMU */
127     memory_region_init_io(&cregs->io, obj, NULL, cregs, ".io",
128                           CXL2_COMPONENT_IO_REGION_SIZE);
129     memory_region_init_io(&cregs->cache_mem, obj, &cache_mem_ops, cregs,
130                           ".cache_mem", CXL2_COMPONENT_CM_REGION_SIZE);
131 
132     memory_region_add_subregion(&cregs->component_registers, 0, &cregs->io);
133     memory_region_add_subregion(&cregs->component_registers,
134                                 CXL2_COMPONENT_IO_REGION_SIZE,
135                                 &cregs->cache_mem);
136 }
137 
138 static void ras_init_common(uint32_t *reg_state, uint32_t *write_msk)
139 {
140     /*
141      * Error status is RW1C but given bits are not yet set, it can
142      * be handled as RO.
143      */
144     stl_le_p(reg_state + R_CXL_RAS_UNC_ERR_STATUS, 0);
145     stl_le_p(write_msk + R_CXL_RAS_UNC_ERR_STATUS, 0x1cfff);
146     /* Bits 12-13 and 17-31 reserved in CXL 2.0 */
147     stl_le_p(reg_state + R_CXL_RAS_UNC_ERR_MASK, 0x1cfff);
148     stl_le_p(write_msk + R_CXL_RAS_UNC_ERR_MASK, 0x1cfff);
149     stl_le_p(reg_state + R_CXL_RAS_UNC_ERR_SEVERITY, 0x1cfff);
150     stl_le_p(write_msk + R_CXL_RAS_UNC_ERR_SEVERITY, 0x1cfff);
151     stl_le_p(reg_state + R_CXL_RAS_COR_ERR_STATUS, 0);
152     stl_le_p(write_msk + R_CXL_RAS_COR_ERR_STATUS, 0x7f);
153     stl_le_p(reg_state + R_CXL_RAS_COR_ERR_MASK, 0x7f);
154     stl_le_p(write_msk + R_CXL_RAS_COR_ERR_MASK, 0x7f);
155     /* CXL switches and devices must set */
156     stl_le_p(reg_state + R_CXL_RAS_ERR_CAP_CTRL, 0x200);
157 }
158 
159 static void hdm_init_common(uint32_t *reg_state, uint32_t *write_msk,
160                             enum reg_type type)
161 {
162     int decoder_count = 1;
163     int i;
164 
165     ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, DECODER_COUNT,
166                      cxl_decoder_count_enc(decoder_count));
167     ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, TARGET_COUNT, 1);
168     ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, INTERLEAVE_256B, 1);
169     ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, INTERLEAVE_4K, 1);
170     ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_CAPABILITY, POISON_ON_ERR_CAP, 0);
171     ARRAY_FIELD_DP32(reg_state, CXL_HDM_DECODER_GLOBAL_CONTROL,
172                      HDM_DECODER_ENABLE, 0);
173     write_msk[R_CXL_HDM_DECODER_GLOBAL_CONTROL] = 0x3;
174     for (i = 0; i < decoder_count; i++) {
175         write_msk[R_CXL_HDM_DECODER0_BASE_LO + i * 0x20] = 0xf0000000;
176         write_msk[R_CXL_HDM_DECODER0_BASE_HI + i * 0x20] = 0xffffffff;
177         write_msk[R_CXL_HDM_DECODER0_SIZE_LO + i * 0x20] = 0xf0000000;
178         write_msk[R_CXL_HDM_DECODER0_SIZE_HI + i * 0x20] = 0xffffffff;
179         write_msk[R_CXL_HDM_DECODER0_CTRL + i * 0x20] = 0x13ff;
180         if (type == CXL2_DEVICE ||
181             type == CXL2_TYPE3_DEVICE ||
182             type == CXL2_LOGICAL_DEVICE) {
183             write_msk[R_CXL_HDM_DECODER0_TARGET_LIST_LO + i * 0x20] = 0xf0000000;
184         } else {
185             write_msk[R_CXL_HDM_DECODER0_TARGET_LIST_LO + i * 0x20] = 0xffffffff;
186         }
187         write_msk[R_CXL_HDM_DECODER0_TARGET_LIST_HI + i * 0x20] = 0xffffffff;
188     }
189 }
190 
191 void cxl_component_register_init_common(uint32_t *reg_state, uint32_t *write_msk,
192                                         enum reg_type type)
193 {
194     int caps = 0;
195 
196     /*
197      * In CXL 2.0 the capabilities required for each CXL component are such that,
198      * with the ordering chosen here, a single number can be used to define
199      * which capabilities should be provided.
200      */
201     switch (type) {
202     case CXL2_DOWNSTREAM_PORT:
203     case CXL2_DEVICE:
204         /* RAS, Link */
205         caps = 2;
206         break;
207     case CXL2_UPSTREAM_PORT:
208     case CXL2_TYPE3_DEVICE:
209     case CXL2_LOGICAL_DEVICE:
210         /* + HDM */
211         caps = 3;
212         break;
213     case CXL2_ROOT_PORT:
214         /* + Extended Security, + Snoop */
215         caps = 5;
216         break;
217     default:
218         abort();
219     }
220 
221     memset(reg_state, 0, CXL2_COMPONENT_CM_REGION_SIZE);
222 
223     /* CXL Capability Header Register */
224     ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, ID, 1);
225     ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, VERSION, 1);
226     ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, CACHE_MEM_VERSION, 1);
227     ARRAY_FIELD_DP32(reg_state, CXL_CAPABILITY_HEADER, ARRAY_SIZE, caps);
228 
229 #define init_cap_reg(reg, id, version)                                        \
230     QEMU_BUILD_BUG_ON(CXL_##reg##_REGISTERS_OFFSET == 0);                     \
231     do {                                                                      \
232         int which = R_CXL_##reg##_CAPABILITY_HEADER;                          \
233         reg_state[which] = FIELD_DP32(reg_state[which],                       \
234                                       CXL_##reg##_CAPABILITY_HEADER, ID, id); \
235         reg_state[which] =                                                    \
236             FIELD_DP32(reg_state[which], CXL_##reg##_CAPABILITY_HEADER,       \
237                        VERSION, version);                                     \
238         reg_state[which] =                                                    \
239             FIELD_DP32(reg_state[which], CXL_##reg##_CAPABILITY_HEADER, PTR,  \
240                        CXL_##reg##_REGISTERS_OFFSET);                         \
241     } while (0)
242 
243     init_cap_reg(RAS, 2, 2);
244     ras_init_common(reg_state, write_msk);
245 
246     init_cap_reg(LINK, 4, 2);
247 
248     if (caps < 3) {
249         return;
250     }
251 
252     init_cap_reg(HDM, 5, 1);
253     hdm_init_common(reg_state, write_msk, type);
254 
255     if (caps < 5) {
256         return;
257     }
258 
259     init_cap_reg(EXTSEC, 6, 1);
260     init_cap_reg(SNOOP, 8, 1);
261 
262 #undef init_cap_reg
263 }
264 
265 /*
266  * Helper to creates a DVSEC header for a CXL entity. The caller is responsible
267  * for tracking the valid offset.
268  *
269  * This function will build the DVSEC header on behalf of the caller and then
270  * copy in the remaining data for the vendor specific bits.
271  * It will also set up appropriate write masks.
272  */
273 void cxl_component_create_dvsec(CXLComponentState *cxl,
274                                 enum reg_type cxl_dev_type, uint16_t length,
275                                 uint16_t type, uint8_t rev, uint8_t *body)
276 {
277     PCIDevice *pdev = cxl->pdev;
278     uint16_t offset = cxl->dvsec_offset;
279     uint8_t *wmask = pdev->wmask;
280 
281     assert(offset >= PCI_CFG_SPACE_SIZE &&
282            ((offset + length) < PCI_CFG_SPACE_EXP_SIZE));
283     assert((length & 0xf000) == 0);
284     assert((rev & ~0xf) == 0);
285 
286     /* Create the DVSEC in the MCFG space */
287     pcie_add_capability(pdev, PCI_EXT_CAP_ID_DVSEC, 1, offset, length);
288     pci_set_long(pdev->config + offset + PCIE_DVSEC_HEADER1_OFFSET,
289                  (length << 20) | (rev << 16) | CXL_VENDOR_ID);
290     pci_set_word(pdev->config + offset + PCIE_DVSEC_ID_OFFSET, type);
291     memcpy(pdev->config + offset + sizeof(DVSECHeader),
292            body + sizeof(DVSECHeader),
293            length - sizeof(DVSECHeader));
294 
295     /* Configure write masks */
296     switch (type) {
297     case PCIE_CXL_DEVICE_DVSEC:
298         /* Cntrl RW Lock - so needs explicit blocking when lock is set */
299         wmask[offset + offsetof(CXLDVSECDevice, ctrl)] = 0xFD;
300         wmask[offset + offsetof(CXLDVSECDevice, ctrl) + 1] = 0x4F;
301         /* Status is RW1CS */
302         wmask[offset + offsetof(CXLDVSECDevice, ctrl2)] = 0x0F;
303        /* Lock is RW Once */
304         wmask[offset + offsetof(CXLDVSECDevice, lock)] = 0x01;
305         /* range1/2_base_high/low is RW Lock */
306         wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi)] = 0xFF;
307         wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi) + 1] = 0xFF;
308         wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi) + 2] = 0xFF;
309         wmask[offset + offsetof(CXLDVSECDevice, range1_base_hi) + 3] = 0xFF;
310         wmask[offset + offsetof(CXLDVSECDevice, range1_base_lo) + 3] = 0xF0;
311         wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi)] = 0xFF;
312         wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi) + 1] = 0xFF;
313         wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi) + 2] = 0xFF;
314         wmask[offset + offsetof(CXLDVSECDevice, range2_base_hi) + 3] = 0xFF;
315         wmask[offset + offsetof(CXLDVSECDevice, range2_base_lo) + 3] = 0xF0;
316         break;
317     case NON_CXL_FUNCTION_MAP_DVSEC:
318         break; /* Not yet implemented */
319     case EXTENSIONS_PORT_DVSEC:
320         wmask[offset + offsetof(CXLDVSECPortExtensions, control)] = 0x0F;
321         wmask[offset + offsetof(CXLDVSECPortExtensions, control) + 1] = 0x40;
322         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_bus_base)] = 0xFF;
323         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_bus_limit)] = 0xFF;
324         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_memory_base)] = 0xF0;
325         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_memory_base) + 1] = 0xFF;
326         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_memory_limit)] = 0xF0;
327         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_memory_limit) + 1] = 0xFF;
328         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_base)] = 0xF0;
329         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_base) + 1] = 0xFF;
330         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_limit)] = 0xF0;
331         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_limit) + 1] = 0xFF;
332         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_base_high)] = 0xFF;
333         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_base_high) + 1] = 0xFF;
334         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_base_high) + 2] = 0xFF;
335         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_base_high) + 3] = 0xFF;
336         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_limit_high)] = 0xFF;
337         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_limit_high) + 1] = 0xFF;
338         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_limit_high) + 2] = 0xFF;
339         wmask[offset + offsetof(CXLDVSECPortExtensions, alt_prefetch_limit_high) + 3] = 0xFF;
340         break;
341     case GPF_PORT_DVSEC:
342         wmask[offset + offsetof(CXLDVSECPortGPF, phase1_ctrl)] = 0x0F;
343         wmask[offset + offsetof(CXLDVSECPortGPF, phase1_ctrl) + 1] = 0x0F;
344         wmask[offset + offsetof(CXLDVSECPortGPF, phase2_ctrl)] = 0x0F;
345         wmask[offset + offsetof(CXLDVSECPortGPF, phase2_ctrl) + 1] = 0x0F;
346         break;
347     case GPF_DEVICE_DVSEC:
348         wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_duration)] = 0x0F;
349         wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_duration) + 1] = 0x0F;
350         wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power)] = 0xFF;
351         wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power) + 1] = 0xFF;
352         wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power) + 2] = 0xFF;
353         wmask[offset + offsetof(CXLDVSECDeviceGPF, phase2_power) + 3] = 0xFF;
354         break;
355     case PCIE_FLEXBUS_PORT_DVSEC:
356         switch (cxl_dev_type) {
357         case CXL2_ROOT_PORT:
358             /* No MLD */
359             wmask[offset + offsetof(CXLDVSECPortFlexBus, ctrl)] = 0xbd;
360             break;
361         case CXL2_DOWNSTREAM_PORT:
362             wmask[offset + offsetof(CXLDVSECPortFlexBus, ctrl)] = 0xfd;
363             break;
364         default: /* Registers are RO for other component types */
365             break;
366         }
367         /* There are rw1cs bits in the status register but never set currently */
368         break;
369     }
370 
371     /* Update state for future DVSEC additions */
372     range_init_nofail(&cxl->dvsecs[type], cxl->dvsec_offset, length);
373     cxl->dvsec_offset += length;
374 }
375 
376 uint8_t cxl_interleave_ways_enc(int iw, Error **errp)
377 {
378     switch (iw) {
379     case 1: return 0x0;
380     case 2: return 0x1;
381     case 4: return 0x2;
382     case 8: return 0x3;
383     case 16: return 0x4;
384     case 3: return 0x8;
385     case 6: return 0x9;
386     case 12: return 0xa;
387     default:
388         error_setg(errp, "Interleave ways: %d not supported", iw);
389         return 0;
390     }
391 }
392 
393 uint8_t cxl_interleave_granularity_enc(uint64_t gran, Error **errp)
394 {
395     switch (gran) {
396     case 256: return 0;
397     case 512: return 1;
398     case 1024: return 2;
399     case 2048: return 3;
400     case 4096: return 4;
401     case 8192: return 5;
402     case 16384: return 6;
403     default:
404         error_setg(errp, "Interleave granularity: %" PRIu64 " invalid", gran);
405         return 0;
406     }
407 }
408