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"
32 #include "hw/ppc/spapr_numa.h"
34 bool spapr_nvdimm_validate(HotplugHandler
*hotplug_dev
, NVDIMMDevice
*nvdimm
,
35 uint64_t size
, Error
**errp
)
37 const MachineClass
*mc
= MACHINE_GET_CLASS(hotplug_dev
);
38 const MachineState
*ms
= MACHINE(hotplug_dev
);
39 g_autofree
char *uuidstr
= NULL
;
43 if (!mc
->nvdimm_supported
) {
44 error_setg(errp
, "NVDIMM hotplug not supported for this machine");
48 if (!ms
->nvdimms_state
->is_enabled
) {
49 error_setg(errp
, "nvdimm device found but 'nvdimm=off' was set");
53 if (object_property_get_int(OBJECT(nvdimm
), NVDIMM_LABEL_SIZE_PROP
,
55 error_setg(errp
, "PAPR requires NVDIMM devices to have label-size set");
59 if (size
% SPAPR_MINIMUM_SCM_BLOCK_SIZE
) {
60 error_setg(errp
, "PAPR requires NVDIMM memory size (excluding label)"
61 " to be a multiple of %" PRIu64
"MB",
62 SPAPR_MINIMUM_SCM_BLOCK_SIZE
/ MiB
);
66 uuidstr
= object_property_get_str(OBJECT(nvdimm
), NVDIMM_UUID_PROP
,
68 ret
= qemu_uuid_parse(uuidstr
, &uuid
);
71 if (qemu_uuid_is_null(&uuid
)) {
72 error_setg(errp
, "NVDIMM device requires the uuid to be set");
80 void spapr_add_nvdimm(DeviceState
*dev
, uint64_t slot
)
83 bool hotplugged
= spapr_drc_hotplugged(dev
);
85 drc
= spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM
, slot
);
89 * pc_dimm_get_free_slot() provided a free slot at pre-plug. The
90 * corresponding DRC is thus assumed to be attachable.
92 spapr_drc_attach(drc
, dev
);
95 spapr_hotplug_req_add_by_index(drc
);
99 static int spapr_dt_nvdimm(SpaprMachineState
*spapr
, void *fdt
,
100 int parent_offset
, NVDIMMDevice
*nvdimm
)
106 uint32_t node
= object_property_get_uint(OBJECT(nvdimm
), PC_DIMM_NODE_PROP
,
108 uint64_t slot
= object_property_get_uint(OBJECT(nvdimm
), PC_DIMM_SLOT_PROP
,
110 uint64_t lsize
= nvdimm
->label_size
;
111 uint64_t size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
114 drc
= spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM
, slot
);
117 drc_idx
= spapr_drc_index(drc
);
119 buf
= g_strdup_printf("ibm,pmemory@%x", drc_idx
);
120 child_offset
= fdt_add_subnode(fdt
, parent_offset
, buf
);
125 _FDT((fdt_setprop_cell(fdt
, child_offset
, "reg", drc_idx
)));
126 _FDT((fdt_setprop_string(fdt
, child_offset
, "compatible", "ibm,pmemory")));
127 _FDT((fdt_setprop_string(fdt
, child_offset
, "device_type", "ibm,pmemory")));
129 spapr_numa_write_associativity_dt(spapr
, fdt
, child_offset
, node
);
131 buf
= qemu_uuid_unparse_strdup(&nvdimm
->uuid
);
132 _FDT((fdt_setprop_string(fdt
, child_offset
, "ibm,unit-guid", buf
)));
135 _FDT((fdt_setprop_cell(fdt
, child_offset
, "ibm,my-drc-index", drc_idx
)));
137 _FDT((fdt_setprop_u64(fdt
, child_offset
, "ibm,block-size",
138 SPAPR_MINIMUM_SCM_BLOCK_SIZE
)));
139 _FDT((fdt_setprop_u64(fdt
, child_offset
, "ibm,number-of-blocks",
140 size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
)));
141 _FDT((fdt_setprop_cell(fdt
, child_offset
, "ibm,metadata-size", lsize
)));
143 _FDT((fdt_setprop_string(fdt
, child_offset
, "ibm,pmem-application",
144 "operating-system")));
145 _FDT(fdt_setprop(fdt
, child_offset
, "ibm,cache-flush-required", NULL
, 0));
150 int spapr_pmem_dt_populate(SpaprDrc
*drc
, SpaprMachineState
*spapr
,
151 void *fdt
, int *fdt_start_offset
, Error
**errp
)
153 NVDIMMDevice
*nvdimm
= NVDIMM(drc
->dev
);
155 *fdt_start_offset
= spapr_dt_nvdimm(spapr
, fdt
, 0, nvdimm
);
160 void spapr_dt_persistent_memory(SpaprMachineState
*spapr
, void *fdt
)
162 int offset
= fdt_subnode_offset(fdt
, 0, "persistent-memory");
163 GSList
*iter
, *nvdimms
= nvdimm_get_device_list();
166 offset
= fdt_add_subnode(fdt
, 0, "persistent-memory");
168 _FDT((fdt_setprop_cell(fdt
, offset
, "#address-cells", 0x1)));
169 _FDT((fdt_setprop_cell(fdt
, offset
, "#size-cells", 0x0)));
170 _FDT((fdt_setprop_string(fdt
, offset
, "device_type",
171 "ibm,persistent-memory")));
174 /* Create DT entries for cold plugged NVDIMM devices */
175 for (iter
= nvdimms
; iter
; iter
= iter
->next
) {
176 NVDIMMDevice
*nvdimm
= iter
->data
;
178 spapr_dt_nvdimm(spapr
, fdt
, offset
, nvdimm
);
180 g_slist_free(nvdimms
);
185 static target_ulong
h_scm_read_metadata(PowerPCCPU
*cpu
,
186 SpaprMachineState
*spapr
,
190 uint32_t drc_index
= args
[0];
191 uint64_t offset
= args
[1];
192 uint64_t len
= args
[2];
193 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
194 NVDIMMDevice
*nvdimm
;
197 uint8_t buf
[8] = { 0 };
199 if (!drc
|| !drc
->dev
||
200 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
204 if (len
!= 1 && len
!= 2 &&
205 len
!= 4 && len
!= 8) {
209 nvdimm
= NVDIMM(drc
->dev
);
210 if ((offset
+ len
< offset
) ||
211 (nvdimm
->label_size
< len
+ offset
)) {
215 ddc
= NVDIMM_GET_CLASS(nvdimm
);
216 ddc
->read_label_data(nvdimm
, buf
, len
, offset
);
223 data
= lduw_be_p(buf
);
226 data
= ldl_be_p(buf
);
229 data
= ldq_be_p(buf
);
232 g_assert_not_reached();
240 static target_ulong
h_scm_write_metadata(PowerPCCPU
*cpu
,
241 SpaprMachineState
*spapr
,
245 uint32_t drc_index
= args
[0];
246 uint64_t offset
= args
[1];
247 uint64_t data
= args
[2];
248 uint64_t len
= args
[3];
249 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
250 NVDIMMDevice
*nvdimm
;
252 uint8_t buf
[8] = { 0 };
254 if (!drc
|| !drc
->dev
||
255 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
259 if (len
!= 1 && len
!= 2 &&
260 len
!= 4 && len
!= 8) {
264 nvdimm
= NVDIMM(drc
->dev
);
265 if ((offset
+ len
< offset
) ||
266 (nvdimm
->label_size
< len
+ offset
)) {
272 if (data
& 0xffffffffffffff00) {
278 if (data
& 0xffffffffffff0000) {
284 if (data
& 0xffffffff00000000) {
293 g_assert_not_reached();
296 ddc
= NVDIMM_GET_CLASS(nvdimm
);
297 ddc
->write_label_data(nvdimm
, buf
, len
, offset
);
302 static target_ulong
h_scm_bind_mem(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
303 target_ulong opcode
, target_ulong
*args
)
305 uint32_t drc_index
= args
[0];
306 uint64_t starting_idx
= args
[1];
307 uint64_t no_of_scm_blocks_to_bind
= args
[2];
308 uint64_t target_logical_mem_addr
= args
[3];
309 uint64_t continue_token
= args
[4];
311 uint64_t total_no_of_scm_blocks
;
312 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
314 NVDIMMDevice
*nvdimm
;
316 if (!drc
|| !drc
->dev
||
317 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
322 * Currently continue token should be zero qemu has already bound
323 * everything and this hcall doesnt return H_BUSY.
325 if (continue_token
> 0) {
329 /* Currently qemu assigns the address. */
330 if (target_logical_mem_addr
!= 0xffffffffffffffff) {
334 nvdimm
= NVDIMM(drc
->dev
);
336 size
= object_property_get_uint(OBJECT(nvdimm
),
337 PC_DIMM_SIZE_PROP
, &error_abort
);
339 total_no_of_scm_blocks
= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
341 if (starting_idx
> total_no_of_scm_blocks
) {
345 if (((starting_idx
+ no_of_scm_blocks_to_bind
) < starting_idx
) ||
346 ((starting_idx
+ no_of_scm_blocks_to_bind
) > total_no_of_scm_blocks
)) {
350 addr
= object_property_get_uint(OBJECT(nvdimm
),
351 PC_DIMM_ADDR_PROP
, &error_abort
);
353 addr
+= starting_idx
* SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
355 /* Already bound, Return target logical address in R5 */
357 args
[2] = no_of_scm_blocks_to_bind
;
362 static target_ulong
h_scm_unbind_mem(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
363 target_ulong opcode
, target_ulong
*args
)
365 uint32_t drc_index
= args
[0];
366 uint64_t starting_scm_logical_addr
= args
[1];
367 uint64_t no_of_scm_blocks_to_unbind
= args
[2];
368 uint64_t continue_token
= args
[3];
369 uint64_t size_to_unbind
;
370 Range blockrange
= range_empty
;
371 Range nvdimmrange
= range_empty
;
372 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
373 NVDIMMDevice
*nvdimm
;
376 if (!drc
|| !drc
->dev
||
377 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
381 /* continue_token should be zero as this hcall doesn't return H_BUSY. */
382 if (continue_token
> 0) {
386 /* Check if starting_scm_logical_addr is block aligned */
387 if (!QEMU_IS_ALIGNED(starting_scm_logical_addr
,
388 SPAPR_MINIMUM_SCM_BLOCK_SIZE
)) {
392 size_to_unbind
= no_of_scm_blocks_to_unbind
* SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
393 if (no_of_scm_blocks_to_unbind
== 0 || no_of_scm_blocks_to_unbind
!=
394 size_to_unbind
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
) {
398 nvdimm
= NVDIMM(drc
->dev
);
399 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
401 addr
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_ADDR_PROP
,
404 range_init_nofail(&nvdimmrange
, addr
, size
);
405 range_init_nofail(&blockrange
, starting_scm_logical_addr
, size_to_unbind
);
407 if (!range_contains_range(&nvdimmrange
, &blockrange
)) {
411 args
[1] = no_of_scm_blocks_to_unbind
;
413 /* let unplug take care of actual unbind */
417 #define H_UNBIND_SCOPE_ALL 0x1
418 #define H_UNBIND_SCOPE_DRC 0x2
420 static target_ulong
h_scm_unbind_all(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
421 target_ulong opcode
, target_ulong
*args
)
423 uint64_t target_scope
= args
[0];
424 uint32_t drc_index
= args
[1];
425 uint64_t continue_token
= args
[2];
426 NVDIMMDevice
*nvdimm
;
428 uint64_t no_of_scm_blocks_unbound
= 0;
430 /* continue_token should be zero as this hcall doesn't return H_BUSY. */
431 if (continue_token
> 0) {
435 if (target_scope
== H_UNBIND_SCOPE_DRC
) {
436 SpaprDrc
*drc
= spapr_drc_by_index(drc_index
);
438 if (!drc
|| !drc
->dev
||
439 spapr_drc_type(drc
) != SPAPR_DR_CONNECTOR_TYPE_PMEM
) {
443 nvdimm
= NVDIMM(drc
->dev
);
444 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
447 no_of_scm_blocks_unbound
= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
448 } else if (target_scope
== H_UNBIND_SCOPE_ALL
) {
449 GSList
*list
, *nvdimms
;
451 nvdimms
= nvdimm_get_device_list();
452 for (list
= nvdimms
; list
; list
= list
->next
) {
454 size
= object_property_get_int(OBJECT(nvdimm
), PC_DIMM_SIZE_PROP
,
457 no_of_scm_blocks_unbound
+= size
/ SPAPR_MINIMUM_SCM_BLOCK_SIZE
;
459 g_slist_free(nvdimms
);
464 args
[1] = no_of_scm_blocks_unbound
;
466 /* let unplug take care of actual unbind */
470 static void spapr_scm_register_types(void)
472 /* qemu/scm specific hcalls */
473 spapr_register_hypercall(H_SCM_READ_METADATA
, h_scm_read_metadata
);
474 spapr_register_hypercall(H_SCM_WRITE_METADATA
, h_scm_write_metadata
);
475 spapr_register_hypercall(H_SCM_BIND_MEM
, h_scm_bind_mem
);
476 spapr_register_hypercall(H_SCM_UNBIND_MEM
, h_scm_unbind_mem
);
477 spapr_register_hypercall(H_SCM_UNBIND_ALL
, h_scm_unbind_all
);
480 type_init(spapr_scm_register_types
)