/src/qemu/hw/cxl/cxl-cdat.c
Line | Count | Source |
1 | | /* |
2 | | * CXL CDAT Structure |
3 | | * |
4 | | * Copyright (C) 2021 Avery Design Systems, Inc. |
5 | | * |
6 | | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
7 | | * See the COPYING file in the top-level directory. |
8 | | */ |
9 | | |
10 | | #include "qemu/osdep.h" |
11 | | #include "hw/pci/pci.h" |
12 | | #include "hw/cxl/cxl.h" |
13 | | #include "qapi/error.h" |
14 | | #include "qemu/error-report.h" |
15 | | |
16 | | static void cdat_len_check(CDATSubHeader *hdr, Error **errp) |
17 | 0 | { |
18 | 0 | assert(hdr->length); |
19 | 0 | assert(hdr->reserved == 0); |
20 | |
|
21 | 0 | switch (hdr->type) { |
22 | 0 | case CDAT_TYPE_DSMAS: |
23 | 0 | assert(hdr->length == sizeof(CDATDsmas)); |
24 | 0 | break; |
25 | 0 | case CDAT_TYPE_DSLBIS: |
26 | 0 | assert(hdr->length == sizeof(CDATDslbis)); |
27 | 0 | break; |
28 | 0 | case CDAT_TYPE_DSMSCIS: |
29 | 0 | assert(hdr->length == sizeof(CDATDsmscis)); |
30 | 0 | break; |
31 | 0 | case CDAT_TYPE_DSIS: |
32 | 0 | assert(hdr->length == sizeof(CDATDsis)); |
33 | 0 | break; |
34 | 0 | case CDAT_TYPE_DSEMTS: |
35 | 0 | assert(hdr->length == sizeof(CDATDsemts)); |
36 | 0 | break; |
37 | 0 | case CDAT_TYPE_SSLBIS: |
38 | 0 | assert(hdr->length >= sizeof(CDATSslbisHeader)); |
39 | 0 | assert((hdr->length - sizeof(CDATSslbisHeader)) % |
40 | 0 | sizeof(CDATSslbe) == 0); |
41 | 0 | break; |
42 | 0 | default: |
43 | 0 | error_setg(errp, "Type %d is reserved", hdr->type); |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | static bool ct3_build_cdat(CDATObject *cdat, Error **errp) |
48 | 0 | { |
49 | 0 | g_autofree CDATTableHeader *cdat_header = NULL; |
50 | 0 | g_autofree CDATEntry *cdat_st = NULL; |
51 | 0 | uint8_t sum = 0; |
52 | 0 | uint8_t *hdr_buf; |
53 | 0 | int ent, i; |
54 | | |
55 | | /* Use default table if fopen == NULL */ |
56 | 0 | assert(cdat->build_cdat_table); |
57 | |
|
58 | 0 | cdat_header = g_malloc0(sizeof(*cdat_header)); |
59 | 0 | if (!cdat_header) { |
60 | 0 | error_setg(errp, "Failed to allocate CDAT header"); |
61 | 0 | return false; |
62 | 0 | } |
63 | | |
64 | 0 | cdat->built_buf_len = cdat->build_cdat_table(&cdat->built_buf, |
65 | 0 | cdat->private); |
66 | |
|
67 | 0 | if (cdat->built_buf_len <= 0) { |
68 | | /* Build later as not all data available yet */ |
69 | 0 | cdat->to_update = true; |
70 | 0 | return true; |
71 | 0 | } |
72 | 0 | cdat->to_update = false; |
73 | |
|
74 | 0 | cdat_st = g_malloc0(sizeof(*cdat_st) * (cdat->built_buf_len + 1)); |
75 | 0 | if (!cdat_st) { |
76 | 0 | error_setg(errp, "Failed to allocate CDAT entry array"); |
77 | 0 | return false; |
78 | 0 | } |
79 | | |
80 | | /* Entry 0 for CDAT header, starts with Entry 1 */ |
81 | 0 | for (ent = 1; ent < cdat->built_buf_len + 1; ent++) { |
82 | 0 | CDATSubHeader *hdr = cdat->built_buf[ent - 1]; |
83 | 0 | uint8_t *buf = (uint8_t *)cdat->built_buf[ent - 1]; |
84 | |
|
85 | 0 | cdat_st[ent].base = hdr; |
86 | 0 | cdat_st[ent].length = hdr->length; |
87 | |
|
88 | 0 | cdat_header->length += hdr->length; |
89 | 0 | for (i = 0; i < hdr->length; i++) { |
90 | 0 | sum += buf[i]; |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | /* CDAT header */ |
95 | 0 | cdat_header->revision = CXL_CDAT_REV; |
96 | | /* For now, no runtime updates */ |
97 | 0 | cdat_header->sequence = 0; |
98 | 0 | cdat_header->length += sizeof(CDATTableHeader); |
99 | |
|
100 | 0 | hdr_buf = (uint8_t *)cdat_header; |
101 | 0 | for (i = 0; i < sizeof(*cdat_header); i++) { |
102 | 0 | sum += hdr_buf[i]; |
103 | 0 | } |
104 | | |
105 | | /* Sum of all bytes including checksum must be 0 */ |
106 | 0 | cdat_header->checksum = ~sum + 1; |
107 | |
|
108 | 0 | cdat_st[0].base = g_steal_pointer(&cdat_header); |
109 | 0 | cdat_st[0].length = sizeof(*cdat_header); |
110 | 0 | cdat->entry_len = 1 + cdat->built_buf_len; |
111 | 0 | cdat->entry = g_steal_pointer(&cdat_st); |
112 | 0 | return true; |
113 | 0 | } |
114 | | |
115 | | static bool ct3_load_cdat(CDATObject *cdat, Error **errp) |
116 | 0 | { |
117 | 0 | g_autofree CDATEntry *cdat_st = NULL; |
118 | 0 | g_autofree uint8_t *buf = NULL; |
119 | 0 | uint8_t sum = 0; |
120 | 0 | int num_ent; |
121 | 0 | int i = 0, ent = 1; |
122 | 0 | gsize file_size = 0; |
123 | 0 | CDATSubHeader *hdr; |
124 | 0 | GError *error = NULL; |
125 | | |
126 | | /* Read CDAT file and create its cache */ |
127 | 0 | if (!g_file_get_contents(cdat->filename, (gchar **)&buf, |
128 | 0 | &file_size, &error)) { |
129 | 0 | error_setg(errp, "CDAT: File read failed: %s", error->message); |
130 | 0 | g_error_free(error); |
131 | 0 | return false; |
132 | 0 | } |
133 | 0 | if (file_size < sizeof(CDATTableHeader)) { |
134 | 0 | error_setg(errp, "CDAT: File too short"); |
135 | 0 | return false; |
136 | 0 | } |
137 | 0 | i = sizeof(CDATTableHeader); |
138 | 0 | num_ent = 1; |
139 | 0 | while (i < file_size) { |
140 | 0 | hdr = (CDATSubHeader *)(buf + i); |
141 | 0 | if (i + sizeof(CDATSubHeader) > file_size) { |
142 | 0 | error_setg(errp, "CDAT: Truncated table"); |
143 | 0 | return false; |
144 | 0 | } |
145 | 0 | cdat_len_check(hdr, errp); |
146 | 0 | i += hdr->length; |
147 | 0 | if (i > file_size) { |
148 | 0 | error_setg(errp, "CDAT: Truncated table"); |
149 | 0 | return false; |
150 | 0 | } |
151 | 0 | num_ent++; |
152 | 0 | } |
153 | 0 | if (i != file_size) { |
154 | 0 | error_setg(errp, "CDAT: File length mismatch"); |
155 | 0 | return false; |
156 | 0 | } |
157 | | |
158 | 0 | cdat_st = g_new0(CDATEntry, num_ent); |
159 | | |
160 | | /* Set CDAT header, Entry = 0 */ |
161 | 0 | cdat_st[0].base = buf; |
162 | 0 | cdat_st[0].length = sizeof(CDATTableHeader); |
163 | 0 | i = 0; |
164 | |
|
165 | 0 | while (i < cdat_st[0].length) { |
166 | 0 | sum += buf[i++]; |
167 | 0 | } |
168 | | |
169 | | /* Read CDAT structures */ |
170 | 0 | while (i < file_size) { |
171 | 0 | hdr = (CDATSubHeader *)(buf + i); |
172 | 0 | cdat_st[ent].base = hdr; |
173 | 0 | cdat_st[ent].length = hdr->length; |
174 | |
|
175 | 0 | while (buf + i < (uint8_t *)cdat_st[ent].base + cdat_st[ent].length) { |
176 | 0 | assert(i < file_size); |
177 | 0 | sum += buf[i++]; |
178 | 0 | } |
179 | |
|
180 | 0 | ent++; |
181 | 0 | } |
182 | |
|
183 | 0 | if (sum != 0) { |
184 | 0 | warn_report("CDAT: Found checksum mismatch in %s", cdat->filename); |
185 | 0 | } |
186 | 0 | cdat->entry_len = num_ent; |
187 | 0 | cdat->entry = g_steal_pointer(&cdat_st); |
188 | 0 | cdat->buf = g_steal_pointer(&buf); |
189 | 0 | return true; |
190 | 0 | } |
191 | | |
192 | | bool cxl_doe_cdat_init(CXLComponentState *cxl_cstate, Error **errp) |
193 | 0 | { |
194 | 0 | CDATObject *cdat = &cxl_cstate->cdat; |
195 | |
|
196 | 0 | if (cdat->filename) { |
197 | 0 | return ct3_load_cdat(cdat, errp); |
198 | 0 | } else { |
199 | 0 | return ct3_build_cdat(cdat, errp); |
200 | 0 | } |
201 | 0 | } |
202 | | |
203 | | void cxl_doe_cdat_update(CXLComponentState *cxl_cstate, Error **errp) |
204 | 0 | { |
205 | 0 | CDATObject *cdat = &cxl_cstate->cdat; |
206 | |
|
207 | 0 | if (cdat->to_update) { |
208 | 0 | ct3_build_cdat(cdat, errp); |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | | void cxl_doe_cdat_release(CXLComponentState *cxl_cstate) |
213 | 0 | { |
214 | 0 | CDATObject *cdat = &cxl_cstate->cdat; |
215 | |
|
216 | 0 | free(cdat->entry); |
217 | 0 | if (cdat->built_buf) { |
218 | 0 | cdat->free_cdat_table(cdat->built_buf, cdat->built_buf_len, |
219 | 0 | cdat->private); |
220 | 0 | } |
221 | 0 | g_free(cdat->buf); |
222 | 0 | } |