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 (size
% SPAPR_MINIMUM_SCM_BLOCK_SIZE
) {
41 error_setg(errp
, "NVDIMM memory size excluding the label area"
42 " must be a multiple of %" PRIu64
"MB",
43 SPAPR_MINIMUM_SCM_BLOCK_SIZE
/ MiB
);
47 uuidstr
= object_property_get_str(OBJECT(nvdimm
), NVDIMM_UUID_PROP
,
49 ret
= qemu_uuid_parse(uuidstr
, &uuid
);
53 if (qemu_uuid_is_null(&uuid
)) {
54 error_setg(errp
, "NVDIMM device requires the uuid to be set");
60 void spapr_add_nvdimm(DeviceState
*dev
, uint64_t slot
, Error
**errp
)
63 bool hotplugged
= spapr_drc_hotplugged(dev
);
64 Error
*local_err
= NULL
;
66 drc
= spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM
, slot
);
69 spapr_drc_attach(drc
, dev
, &local_err
);
71 error_propagate(errp
, local_err
);
76 spapr_hotplug_req_add_by_index(drc
);
80 int spapr_pmem_dt_populate(SpaprDrc
*drc
, SpaprMachineState
*spapr
,
81 void *fdt
, int *fdt_start_offset
, Error
**errp
)
83 NVDIMMDevice
*nvdimm
= NVDIMM(drc
->dev
);
85 *fdt_start_offset
= spapr_dt_nvdimm(fdt
, 0, nvdimm
);
90 void spapr_create_nvdimm_dr_connectors(SpaprMachineState
*spapr
)
92 MachineState
*machine
= MACHINE(spapr
);
95 for (i
= 0; i
< machine
->ram_slots
; i
++) {
96 spapr_dr_connector_new(OBJECT(spapr
), TYPE_SPAPR_DRC_PMEM
, i
);
101 int spapr_dt_nvdimm(void *fdt
, int parent_offset
,
102 NVDIMMDevice
*nvdimm
)
108 uint32_t node
= object_property_get_uint(OBJECT(nvdimm
), PC_DIMM_NODE_PROP
,
110 uint64_t slot
= object_property_get_uint(OBJECT(nvdimm
), PC_DIMM_SLOT_PROP
,
112 uint32_t associativity
[] = {
113 cpu_to_be32(0x4), /* length */
114 cpu_to_be32(0x0), cpu_to_be32(0x0),
115 cpu_to_be32(0x0), cpu_to_be32(node
)
117 uint64_t lsize
= nvdimm
->label_size
;
118 uint64_t size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
121 drc
= spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM
, slot
);
124 drc_idx
= spapr_drc_index(drc
);
126 buf
= g_strdup_printf("ibm,pmemory@%x", drc_idx
);
127 child_offset
= fdt_add_subnode(fdt
, parent_offset
, buf
);
132 _FDT((fdt_setprop_cell(fdt
, child_offset
, "reg", drc_idx
)));
133 _FDT((fdt_setprop_string(fdt
, child_offset
, "compatible", "ibm,pmemory")));
134 _FDT((fdt_setprop_string(fdt
, child_offset
, "device_type", "ibm,pmemory")));
136 _FDT((fdt_setprop(fdt
, child_offset
, "ibm,associativity", associativity
,
137 sizeof(associativity
))));
139 buf
= qemu_uuid_unparse_strdup(&nvdimm
->uuid
);
140 _FDT((fdt_setprop_string(fdt
, child_offset
, "ibm,unit-guid", buf
)));
143 _FDT((fdt_setprop_cell(fdt
, child_offset
, "ibm,my-drc-index", drc_idx
)));
145 _FDT((fdt_setprop_u64(fdt
, child_offset
, "ibm,block-size",
146 SPAPR_MINIMUM_SCM_BLOCK_SIZE
)));
147 _FDT((fdt_setprop_u64(fdt
, child_offset
, "ibm,number-of-blocks",
148 size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
)));
149 _FDT((fdt_setprop_cell(fdt
, child_offset
, "ibm,metadata-size", lsize
)));
151 _FDT((fdt_setprop_string(fdt
, child_offset
, "ibm,pmem-application",
152 "operating-system")));
153 _FDT(fdt_setprop(fdt
, child_offset
, "ibm,cache-flush-required", NULL
, 0));
158 void spapr_dt_persistent_memory(void *fdt
)
160 int offset
= fdt_subnode_offset(fdt
, 0, "persistent-memory");
161 GSList
*iter
, *nvdimms
= nvdimm_get_device_list();
164 offset
= fdt_add_subnode(fdt
, 0, "persistent-memory");
166 _FDT((fdt_setprop_cell(fdt
, offset
, "#address-cells", 0x1)));
167 _FDT((fdt_setprop_cell(fdt
, offset
, "#size-cells", 0x0)));
168 _FDT((fdt_setprop_string(fdt
, offset
, "device_type",
169 "ibm,persistent-memory")));
172 /* Create DT entries for cold plugged NVDIMM devices */
173 for (iter
= nvdimms
; iter
; iter
= iter
->next
) {
174 NVDIMMDevice
*nvdimm
= iter
->data
;
176 spapr_dt_nvdimm(fdt
, offset
, nvdimm
);
178 g_slist_free(nvdimms
);
183 static target_ulong
h_scm_read_metadata(PowerPCCPU
*cpu
,
184 SpaprMachineState
*spapr
,
188 uint32_t drc_index
= args
[0];
189 uint64_t offset
= args
[1];
190 uint64_t len
= args
[2];
191 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
192 NVDIMMDevice
*nvdimm
;
195 uint8_t buf
[8] = { 0 };
197 if (!drc
|| !drc
->dev
||
198 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
202 if (len
!= 1 && len
!= 2 &&
203 len
!= 4 && len
!= 8) {
207 nvdimm
= NVDIMM(drc
->dev
);
208 if ((offset
+ len
< offset
) ||
209 (nvdimm
->label_size
< len
+ offset
)) {
213 ddc
= NVDIMM_GET_CLASS(nvdimm
);
214 ddc
->read_label_data(nvdimm
, buf
, len
, offset
);
221 data
= lduw_be_p(buf
);
224 data
= ldl_be_p(buf
);
227 data
= ldq_be_p(buf
);
230 g_assert_not_reached();
238 static target_ulong
h_scm_write_metadata(PowerPCCPU
*cpu
,
239 SpaprMachineState
*spapr
,
243 uint32_t drc_index
= args
[0];
244 uint64_t offset
= args
[1];
245 uint64_t data
= args
[2];
246 uint64_t len
= args
[3];
247 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
248 NVDIMMDevice
*nvdimm
;
250 uint8_t buf
[8] = { 0 };
252 if (!drc
|| !drc
->dev
||
253 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
257 if (len
!= 1 && len
!= 2 &&
258 len
!= 4 && len
!= 8) {
262 nvdimm
= NVDIMM(drc
->dev
);
263 if ((offset
+ len
< offset
) ||
264 (nvdimm
->label_size
< len
+ offset
)) {
270 if (data
& 0xffffffffffffff00) {
276 if (data
& 0xffffffffffff0000) {
282 if (data
& 0xffffffff00000000) {
291 g_assert_not_reached();
294 ddc
= NVDIMM_GET_CLASS(nvdimm
);
295 ddc
->write_label_data(nvdimm
, buf
, len
, offset
);
300 static target_ulong
h_scm_bind_mem(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
301 target_ulong opcode
, target_ulong
*args
)
303 uint32_t drc_index
= args
[0];
304 uint64_t starting_idx
= args
[1];
305 uint64_t no_of_scm_blocks_to_bind
= args
[2];
306 uint64_t target_logical_mem_addr
= args
[3];
307 uint64_t continue_token
= args
[4];
309 uint64_t total_no_of_scm_blocks
;
310 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
312 NVDIMMDevice
*nvdimm
;
314 if (!drc
|| !drc
->dev
||
315 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
320 * Currently continue token should be zero qemu has already bound
321 * everything and this hcall doesnt return H_BUSY.
323 if (continue_token
> 0) {
327 /* Currently qemu assigns the address. */
328 if (target_logical_mem_addr
!= 0xffffffffffffffff) {
332 nvdimm
= NVDIMM(drc
->dev
);
334 size
= object_property_get_uint(OBJECT(nvdimm
),
335 PC_DIMM_SIZE_PROP
, &error_abort
);
337 total_no_of_scm_blocks
= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
339 if (starting_idx
> total_no_of_scm_blocks
) {
343 if (((starting_idx
+ no_of_scm_blocks_to_bind
) < starting_idx
) ||
344 ((starting_idx
+ no_of_scm_blocks_to_bind
) > total_no_of_scm_blocks
)) {
348 addr
= object_property_get_uint(OBJECT(nvdimm
),
349 PC_DIMM_ADDR_PROP
, &error_abort
);
351 addr
+= starting_idx
* SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
353 /* Already bound, Return target logical address in R5 */
355 args
[2] = no_of_scm_blocks_to_bind
;
360 static target_ulong
h_scm_unbind_mem(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
361 target_ulong opcode
, target_ulong
*args
)
363 uint32_t drc_index
= args
[0];
364 uint64_t starting_scm_logical_addr
= args
[1];
365 uint64_t no_of_scm_blocks_to_unbind
= args
[2];
366 uint64_t continue_token
= args
[3];
367 uint64_t size_to_unbind
;
368 Range blockrange
= range_empty
;
369 Range nvdimmrange
= range_empty
;
370 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
371 NVDIMMDevice
*nvdimm
;
374 if (!drc
|| !drc
->dev
||
375 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
379 /* continue_token should be zero as this hcall doesn't return H_BUSY. */
380 if (continue_token
> 0) {
384 /* Check if starting_scm_logical_addr is block aligned */
385 if (!QEMU_IS_ALIGNED(starting_scm_logical_addr
,
386 SPAPR_MINIMUM_SCM_BLOCK_SIZE
)) {
390 size_to_unbind
= no_of_scm_blocks_to_unbind
* SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
391 if (no_of_scm_blocks_to_unbind
== 0 || no_of_scm_blocks_to_unbind
!=
392 size_to_unbind
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
) {
396 nvdimm
= NVDIMM(drc
->dev
);
397 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
399 addr
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_ADDR_PROP
,
402 range_init_nofail(&nvdimmrange
, addr
, size
);
403 range_init_nofail(&blockrange
, starting_scm_logical_addr
, size_to_unbind
);
405 if (!range_contains_range(&nvdimmrange
, &blockrange
)) {
409 args
[1] = no_of_scm_blocks_to_unbind
;
411 /* let unplug take care of actual unbind */
415 #define H_UNBIND_SCOPE_ALL 0x1
416 #define H_UNBIND_SCOPE_DRC 0x2
418 static target_ulong
h_scm_unbind_all(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
419 target_ulong opcode
, target_ulong
*args
)
421 uint64_t target_scope
= args
[0];
422 uint32_t drc_index
= args
[1];
423 uint64_t continue_token
= args
[2];
424 NVDIMMDevice
*nvdimm
;
426 uint64_t no_of_scm_blocks_unbound
= 0;
428 /* continue_token should be zero as this hcall doesn't return H_BUSY. */
429 if (continue_token
> 0) {
433 if (target_scope
== H_UNBIND_SCOPE_DRC
) {
434 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
436 if (!drc
|| !drc
->dev
||
437 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
441 nvdimm
= NVDIMM(drc
->dev
);
442 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
445 no_of_scm_blocks_unbound
= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
446 } else if (target_scope
== H_UNBIND_SCOPE_ALL
) {
447 GSList
*list
, *nvdimms
;
449 nvdimms
= nvdimm_get_device_list();
450 for (list
= nvdimms
; list
; list
= list
->next
) {
452 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
455 no_of_scm_blocks_unbound
+= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
457 g_slist_free(nvdimms
);
462 args
[1] = no_of_scm_blocks_unbound
;
464 /* let unplug take care of actual unbind */
468 static void spapr_scm_register_types(void)
470 /* qemu/scm specific hcalls */
471 spapr_register_hypercall(H_SCM_READ_METADATA
, h_scm_read_metadata
);
472 spapr_register_hypercall(H_SCM_WRITE_METADATA
, h_scm_write_metadata
);
473 spapr_register_hypercall(H_SCM_BIND_MEM
, h_scm_bind_mem
);
474 spapr_register_hypercall(H_SCM_UNBIND_MEM
, h_scm_unbind_mem
);
475 spapr_register_hypercall(H_SCM_UNBIND_ALL
, h_scm_unbind_all
);
478 type_init(spapr_scm_register_types
)