2 * QEMU PAPR Storage Class Memory Interfaces
4 * Copyright (c) 2019-2020, IBM Corporation.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/ppc/spapr_drc.h"
27 #include "hw/ppc/spapr_nvdimm.h"
28 #include "hw/mem/nvdimm.h"
29 #include "qemu/nvdimm-utils.h"
30 #include "hw/ppc/fdt.h"
31 #include "qemu/range.h"
33 void spapr_nvdimm_validate_opts(NVDIMMDevice
*nvdimm
, uint64_t size
,
40 if (object_property_get_int(OBJECT(nvdimm
), NVDIMM_LABEL_SIZE_PROP
,
42 error_setg(errp
, "PAPR requires NVDIMM devices to have label-size set");
46 if (size
% SPAPR_MINIMUM_SCM_BLOCK_SIZE
) {
47 error_setg(errp
, "PAPR requires NVDIMM memory size (excluding label)"
48 " to be a multiple of %" PRIu64
"MB",
49 SPAPR_MINIMUM_SCM_BLOCK_SIZE
/ MiB
);
53 uuidstr
= object_property_get_str(OBJECT(nvdimm
), NVDIMM_UUID_PROP
,
55 ret
= qemu_uuid_parse(uuidstr
, &uuid
);
59 if (qemu_uuid_is_null(&uuid
)) {
60 error_setg(errp
, "NVDIMM device requires the uuid to be set");
66 void spapr_add_nvdimm(DeviceState
*dev
, uint64_t slot
, Error
**errp
)
69 bool hotplugged
= spapr_drc_hotplugged(dev
);
70 Error
*local_err
= NULL
;
72 drc
= spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM
, slot
);
75 spapr_drc_attach(drc
, dev
, &local_err
);
77 error_propagate(errp
, local_err
);
82 spapr_hotplug_req_add_by_index(drc
);
86 int spapr_pmem_dt_populate(SpaprDrc
*drc
, SpaprMachineState
*spapr
,
87 void *fdt
, int *fdt_start_offset
, Error
**errp
)
89 NVDIMMDevice
*nvdimm
= NVDIMM(drc
->dev
);
91 *fdt_start_offset
= spapr_dt_nvdimm(fdt
, 0, nvdimm
);
96 void spapr_create_nvdimm_dr_connectors(SpaprMachineState
*spapr
)
98 MachineState
*machine
= MACHINE(spapr
);
101 for (i
= 0; i
< machine
->ram_slots
; i
++) {
102 spapr_dr_connector_new(OBJECT(spapr
), TYPE_SPAPR_DRC_PMEM
, i
);
107 int spapr_dt_nvdimm(void *fdt
, int parent_offset
,
108 NVDIMMDevice
*nvdimm
)
114 uint32_t node
= object_property_get_uint(OBJECT(nvdimm
), PC_DIMM_NODE_PROP
,
116 uint64_t slot
= object_property_get_uint(OBJECT(nvdimm
), PC_DIMM_SLOT_PROP
,
118 uint32_t associativity
[] = {
119 cpu_to_be32(0x4), /* length */
120 cpu_to_be32(0x0), cpu_to_be32(0x0),
121 cpu_to_be32(0x0), cpu_to_be32(node
)
123 uint64_t lsize
= nvdimm
->label_size
;
124 uint64_t size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
127 drc
= spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM
, slot
);
130 drc_idx
= spapr_drc_index(drc
);
132 buf
= g_strdup_printf("ibm,pmemory@%x", drc_idx
);
133 child_offset
= fdt_add_subnode(fdt
, parent_offset
, buf
);
138 _FDT((fdt_setprop_cell(fdt
, child_offset
, "reg", drc_idx
)));
139 _FDT((fdt_setprop_string(fdt
, child_offset
, "compatible", "ibm,pmemory")));
140 _FDT((fdt_setprop_string(fdt
, child_offset
, "device_type", "ibm,pmemory")));
142 _FDT((fdt_setprop(fdt
, child_offset
, "ibm,associativity", associativity
,
143 sizeof(associativity
))));
145 buf
= qemu_uuid_unparse_strdup(&nvdimm
->uuid
);
146 _FDT((fdt_setprop_string(fdt
, child_offset
, "ibm,unit-guid", buf
)));
149 _FDT((fdt_setprop_cell(fdt
, child_offset
, "ibm,my-drc-index", drc_idx
)));
151 _FDT((fdt_setprop_u64(fdt
, child_offset
, "ibm,block-size",
152 SPAPR_MINIMUM_SCM_BLOCK_SIZE
)));
153 _FDT((fdt_setprop_u64(fdt
, child_offset
, "ibm,number-of-blocks",
154 size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
)));
155 _FDT((fdt_setprop_cell(fdt
, child_offset
, "ibm,metadata-size", lsize
)));
157 _FDT((fdt_setprop_string(fdt
, child_offset
, "ibm,pmem-application",
158 "operating-system")));
159 _FDT(fdt_setprop(fdt
, child_offset
, "ibm,cache-flush-required", NULL
, 0));
164 void spapr_dt_persistent_memory(void *fdt
)
166 int offset
= fdt_subnode_offset(fdt
, 0, "persistent-memory");
167 GSList
*iter
, *nvdimms
= nvdimm_get_device_list();
170 offset
= fdt_add_subnode(fdt
, 0, "persistent-memory");
172 _FDT((fdt_setprop_cell(fdt
, offset
, "#address-cells", 0x1)));
173 _FDT((fdt_setprop_cell(fdt
, offset
, "#size-cells", 0x0)));
174 _FDT((fdt_setprop_string(fdt
, offset
, "device_type",
175 "ibm,persistent-memory")));
178 /* Create DT entries for cold plugged NVDIMM devices */
179 for (iter
= nvdimms
; iter
; iter
= iter
->next
) {
180 NVDIMMDevice
*nvdimm
= iter
->data
;
182 spapr_dt_nvdimm(fdt
, offset
, nvdimm
);
184 g_slist_free(nvdimms
);
189 static target_ulong
h_scm_read_metadata(PowerPCCPU
*cpu
,
190 SpaprMachineState
*spapr
,
194 uint32_t drc_index
= args
[0];
195 uint64_t offset
= args
[1];
196 uint64_t len
= args
[2];
197 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
198 NVDIMMDevice
*nvdimm
;
201 uint8_t buf
[8] = { 0 };
203 if (!drc
|| !drc
->dev
||
204 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
208 if (len
!= 1 && len
!= 2 &&
209 len
!= 4 && len
!= 8) {
213 nvdimm
= NVDIMM(drc
->dev
);
214 if ((offset
+ len
< offset
) ||
215 (nvdimm
->label_size
< len
+ offset
)) {
219 ddc
= NVDIMM_GET_CLASS(nvdimm
);
220 ddc
->read_label_data(nvdimm
, buf
, len
, offset
);
227 data
= lduw_be_p(buf
);
230 data
= ldl_be_p(buf
);
233 data
= ldq_be_p(buf
);
236 g_assert_not_reached();
244 static target_ulong
h_scm_write_metadata(PowerPCCPU
*cpu
,
245 SpaprMachineState
*spapr
,
249 uint32_t drc_index
= args
[0];
250 uint64_t offset
= args
[1];
251 uint64_t data
= args
[2];
252 uint64_t len
= args
[3];
253 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
254 NVDIMMDevice
*nvdimm
;
256 uint8_t buf
[8] = { 0 };
258 if (!drc
|| !drc
->dev
||
259 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
263 if (len
!= 1 && len
!= 2 &&
264 len
!= 4 && len
!= 8) {
268 nvdimm
= NVDIMM(drc
->dev
);
269 if ((offset
+ len
< offset
) ||
270 (nvdimm
->label_size
< len
+ offset
)) {
276 if (data
& 0xffffffffffffff00) {
282 if (data
& 0xffffffffffff0000) {
288 if (data
& 0xffffffff00000000) {
297 g_assert_not_reached();
300 ddc
= NVDIMM_GET_CLASS(nvdimm
);
301 ddc
->write_label_data(nvdimm
, buf
, len
, offset
);
306 static target_ulong
h_scm_bind_mem(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
307 target_ulong opcode
, target_ulong
*args
)
309 uint32_t drc_index
= args
[0];
310 uint64_t starting_idx
= args
[1];
311 uint64_t no_of_scm_blocks_to_bind
= args
[2];
312 uint64_t target_logical_mem_addr
= args
[3];
313 uint64_t continue_token
= args
[4];
315 uint64_t total_no_of_scm_blocks
;
316 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
318 NVDIMMDevice
*nvdimm
;
320 if (!drc
|| !drc
->dev
||
321 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
326 * Currently continue token should be zero qemu has already bound
327 * everything and this hcall doesnt return H_BUSY.
329 if (continue_token
> 0) {
333 /* Currently qemu assigns the address. */
334 if (target_logical_mem_addr
!= 0xffffffffffffffff) {
338 nvdimm
= NVDIMM(drc
->dev
);
340 size
= object_property_get_uint(OBJECT(nvdimm
),
341 PC_DIMM_SIZE_PROP
, &error_abort
);
343 total_no_of_scm_blocks
= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
345 if (starting_idx
> total_no_of_scm_blocks
) {
349 if (((starting_idx
+ no_of_scm_blocks_to_bind
) < starting_idx
) ||
350 ((starting_idx
+ no_of_scm_blocks_to_bind
) > total_no_of_scm_blocks
)) {
354 addr
= object_property_get_uint(OBJECT(nvdimm
),
355 PC_DIMM_ADDR_PROP
, &error_abort
);
357 addr
+= starting_idx
* SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
359 /* Already bound, Return target logical address in R5 */
361 args
[2] = no_of_scm_blocks_to_bind
;
366 static target_ulong
h_scm_unbind_mem(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
367 target_ulong opcode
, target_ulong
*args
)
369 uint32_t drc_index
= args
[0];
370 uint64_t starting_scm_logical_addr
= args
[1];
371 uint64_t no_of_scm_blocks_to_unbind
= args
[2];
372 uint64_t continue_token
= args
[3];
373 uint64_t size_to_unbind
;
374 Range blockrange
= range_empty
;
375 Range nvdimmrange
= range_empty
;
376 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
377 NVDIMMDevice
*nvdimm
;
380 if (!drc
|| !drc
->dev
||
381 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
385 /* continue_token should be zero as this hcall doesn't return H_BUSY. */
386 if (continue_token
> 0) {
390 /* Check if starting_scm_logical_addr is block aligned */
391 if (!QEMU_IS_ALIGNED(starting_scm_logical_addr
,
392 SPAPR_MINIMUM_SCM_BLOCK_SIZE
)) {
396 size_to_unbind
= no_of_scm_blocks_to_unbind
* SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
397 if (no_of_scm_blocks_to_unbind
== 0 || no_of_scm_blocks_to_unbind
!=
398 size_to_unbind
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
) {
402 nvdimm
= NVDIMM(drc
->dev
);
403 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
405 addr
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_ADDR_PROP
,
408 range_init_nofail(&nvdimmrange
, addr
, size
);
409 range_init_nofail(&blockrange
, starting_scm_logical_addr
, size_to_unbind
);
411 if (!range_contains_range(&nvdimmrange
, &blockrange
)) {
415 args
[1] = no_of_scm_blocks_to_unbind
;
417 /* let unplug take care of actual unbind */
421 #define H_UNBIND_SCOPE_ALL 0x1
422 #define H_UNBIND_SCOPE_DRC 0x2
424 static target_ulong
h_scm_unbind_all(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
425 target_ulong opcode
, target_ulong
*args
)
427 uint64_t target_scope
= args
[0];
428 uint32_t drc_index
= args
[1];
429 uint64_t continue_token
= args
[2];
430 NVDIMMDevice
*nvdimm
;
432 uint64_t no_of_scm_blocks_unbound
= 0;
434 /* continue_token should be zero as this hcall doesn't return H_BUSY. */
435 if (continue_token
> 0) {
439 if (target_scope
== H_UNBIND_SCOPE_DRC
) {
440 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
442 if (!drc
|| !drc
->dev
||
443 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
447 nvdimm
= NVDIMM(drc
->dev
);
448 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
451 no_of_scm_blocks_unbound
= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
452 } else if (target_scope
== H_UNBIND_SCOPE_ALL
) {
453 GSList
*list
, *nvdimms
;
455 nvdimms
= nvdimm_get_device_list();
456 for (list
= nvdimms
; list
; list
= list
->next
) {
458 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
461 no_of_scm_blocks_unbound
+= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
463 g_slist_free(nvdimms
);
468 args
[1] = no_of_scm_blocks_unbound
;
470 /* let unplug take care of actual unbind */
474 static void spapr_scm_register_types(void)
476 /* qemu/scm specific hcalls */
477 spapr_register_hypercall(H_SCM_READ_METADATA
, h_scm_read_metadata
);
478 spapr_register_hypercall(H_SCM_WRITE_METADATA
, h_scm_write_metadata
);
479 spapr_register_hypercall(H_SCM_BIND_MEM
, h_scm_bind_mem
);
480 spapr_register_hypercall(H_SCM_UNBIND_MEM
, h_scm_unbind_mem
);
481 spapr_register_hypercall(H_SCM_UNBIND_ALL
, h_scm_unbind_all
);
484 type_init(spapr_scm_register_types
)