xref: /qemu/hw/ppc/spapr_nvdimm.c (revision 9f9f82dacebbb816c62d730658f14a615c3ea003)
1ee3a71e3SShivaprasad G Bhat /*
2ee3a71e3SShivaprasad G Bhat  * QEMU PAPR Storage Class Memory Interfaces
3ee3a71e3SShivaprasad G Bhat  *
4ee3a71e3SShivaprasad G Bhat  * Copyright (c) 2019-2020, IBM Corporation.
5ee3a71e3SShivaprasad G Bhat  *
6ee3a71e3SShivaprasad G Bhat  * Permission is hereby granted, free of charge, to any person obtaining a copy
7ee3a71e3SShivaprasad G Bhat  * of this software and associated documentation files (the "Software"), to deal
8ee3a71e3SShivaprasad G Bhat  * in the Software without restriction, including without limitation the rights
9ee3a71e3SShivaprasad G Bhat  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10ee3a71e3SShivaprasad G Bhat  * copies of the Software, and to permit persons to whom the Software is
11ee3a71e3SShivaprasad G Bhat  * furnished to do so, subject to the following conditions:
12ee3a71e3SShivaprasad G Bhat  *
13ee3a71e3SShivaprasad G Bhat  * The above copyright notice and this permission notice shall be included in
14ee3a71e3SShivaprasad G Bhat  * all copies or substantial portions of the Software.
15ee3a71e3SShivaprasad G Bhat  *
16ee3a71e3SShivaprasad G Bhat  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17ee3a71e3SShivaprasad G Bhat  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18ee3a71e3SShivaprasad G Bhat  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19ee3a71e3SShivaprasad G Bhat  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20ee3a71e3SShivaprasad G Bhat  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21ee3a71e3SShivaprasad G Bhat  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22ee3a71e3SShivaprasad G Bhat  * THE SOFTWARE.
23ee3a71e3SShivaprasad G Bhat  */
24ee3a71e3SShivaprasad G Bhat #include "qemu/osdep.h"
25ee3a71e3SShivaprasad G Bhat #include "qapi/error.h"
26ee3a71e3SShivaprasad G Bhat #include "hw/ppc/spapr_drc.h"
27ee3a71e3SShivaprasad G Bhat #include "hw/ppc/spapr_nvdimm.h"
28ee3a71e3SShivaprasad G Bhat #include "hw/mem/nvdimm.h"
29ee3a71e3SShivaprasad G Bhat #include "qemu/nvdimm-utils.h"
30ee3a71e3SShivaprasad G Bhat #include "hw/ppc/fdt.h"
31b5fca656SShivaprasad G Bhat #include "qemu/range.h"
32f1aa45ffSDaniel Henrique Barboza #include "hw/ppc/spapr_numa.h"
33ee3a71e3SShivaprasad G Bhat 
3453d7d7e2SVaibhav Jain /* DIMM health bitmap bitmap indicators. Taken from kernel's papr_scm.c */
3553d7d7e2SVaibhav Jain /* SCM device is unable to persist memory contents */
3653d7d7e2SVaibhav Jain #define PAPR_PMEM_UNARMED PPC_BIT(0)
3753d7d7e2SVaibhav Jain 
38f93c8f14SShivaprasad G Bhat /*
39f93c8f14SShivaprasad G Bhat  * The nvdimm size should be aligned to SCM block size.
40f93c8f14SShivaprasad G Bhat  * The SCM block size should be aligned to SPAPR_MEMORY_BLOCK_SIZE
41f93c8f14SShivaprasad G Bhat  * in order to have SCM regions not to overlap with dimm memory regions.
42f93c8f14SShivaprasad G Bhat  * The SCM devices can have variable block sizes. For now, fixing the
43f93c8f14SShivaprasad G Bhat  * block size to the minimum value.
44f93c8f14SShivaprasad G Bhat  */
45f93c8f14SShivaprasad G Bhat #define SPAPR_MINIMUM_SCM_BLOCK_SIZE SPAPR_MEMORY_BLOCK_SIZE
46f93c8f14SShivaprasad G Bhat 
47f93c8f14SShivaprasad G Bhat /* Have an explicit check for alignment */
48f93c8f14SShivaprasad G Bhat QEMU_BUILD_BUG_ON(SPAPR_MINIMUM_SCM_BLOCK_SIZE % SPAPR_MEMORY_BLOCK_SIZE);
49f93c8f14SShivaprasad G Bhat 
50451c6905SGreg Kurz bool spapr_nvdimm_validate(HotplugHandler *hotplug_dev, NVDIMMDevice *nvdimm,
51beb6073fSDaniel Henrique Barboza                            uint64_t size, Error **errp)
52ee3a71e3SShivaprasad G Bhat {
53beb6073fSDaniel Henrique Barboza     const MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
5428f5a716SDaniel Henrique Barboza     const MachineState *ms = MACHINE(hotplug_dev);
5590d282d0SDaniel Henrique Barboza     g_autofree char *uuidstr = NULL;
56ee3a71e3SShivaprasad G Bhat     QemuUUID uuid;
57af7084e7SShivaprasad G Bhat     int ret;
58ee3a71e3SShivaprasad G Bhat 
59beb6073fSDaniel Henrique Barboza     if (!mc->nvdimm_supported) {
60beb6073fSDaniel Henrique Barboza         error_setg(errp, "NVDIMM hotplug not supported for this machine");
61451c6905SGreg Kurz         return false;
62beb6073fSDaniel Henrique Barboza     }
63beb6073fSDaniel Henrique Barboza 
6455810e90SIgor Mammedov     if (!ms->nvdimms_state->is_enabled) {
6528f5a716SDaniel Henrique Barboza         error_setg(errp, "nvdimm device found but 'nvdimm=off' was set");
66451c6905SGreg Kurz         return false;
6728f5a716SDaniel Henrique Barboza     }
6828f5a716SDaniel Henrique Barboza 
6970fc9cb0SDaniel Henrique Barboza     if (object_property_get_int(OBJECT(nvdimm), NVDIMM_LABEL_SIZE_PROP,
7070fc9cb0SDaniel Henrique Barboza                                 &error_abort) == 0) {
716c0f0cb3SDavid Gibson         error_setg(errp, "PAPR requires NVDIMM devices to have label-size set");
72451c6905SGreg Kurz         return false;
7370fc9cb0SDaniel Henrique Barboza     }
7470fc9cb0SDaniel Henrique Barboza 
75ee3a71e3SShivaprasad G Bhat     if (size % SPAPR_MINIMUM_SCM_BLOCK_SIZE) {
766c0f0cb3SDavid Gibson         error_setg(errp, "PAPR requires NVDIMM memory size (excluding label)"
776c0f0cb3SDavid Gibson                    " to be a multiple of %" PRIu64 "MB",
78ee3a71e3SShivaprasad G Bhat                    SPAPR_MINIMUM_SCM_BLOCK_SIZE / MiB);
79451c6905SGreg Kurz         return false;
80ee3a71e3SShivaprasad G Bhat     }
81ee3a71e3SShivaprasad G Bhat 
82af7084e7SShivaprasad G Bhat     uuidstr = object_property_get_str(OBJECT(nvdimm), NVDIMM_UUID_PROP,
83af7084e7SShivaprasad G Bhat                                       &error_abort);
84af7084e7SShivaprasad G Bhat     ret = qemu_uuid_parse(uuidstr, &uuid);
85af7084e7SShivaprasad G Bhat     g_assert(!ret);
86ee3a71e3SShivaprasad G Bhat 
87ee3a71e3SShivaprasad G Bhat     if (qemu_uuid_is_null(&uuid)) {
88ee3a71e3SShivaprasad G Bhat         error_setg(errp, "NVDIMM device requires the uuid to be set");
89451c6905SGreg Kurz         return false;
90ee3a71e3SShivaprasad G Bhat     }
91451c6905SGreg Kurz 
92451c6905SGreg Kurz     return true;
93ee3a71e3SShivaprasad G Bhat }
94ee3a71e3SShivaprasad G Bhat 
95ee3a71e3SShivaprasad G Bhat 
96ea042c53SGreg Kurz void spapr_add_nvdimm(DeviceState *dev, uint64_t slot)
97ee3a71e3SShivaprasad G Bhat {
98ee3a71e3SShivaprasad G Bhat     SpaprDrc *drc;
99ee3a71e3SShivaprasad G Bhat     bool hotplugged = spapr_drc_hotplugged(dev);
100ee3a71e3SShivaprasad G Bhat 
101ee3a71e3SShivaprasad G Bhat     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot);
102ee3a71e3SShivaprasad G Bhat     g_assert(drc);
103ee3a71e3SShivaprasad G Bhat 
104ea042c53SGreg Kurz     /*
105ea042c53SGreg Kurz      * pc_dimm_get_free_slot() provided a free slot at pre-plug. The
106ea042c53SGreg Kurz      * corresponding DRC is thus assumed to be attachable.
107ea042c53SGreg Kurz      */
108bc370a65SGreg Kurz     spapr_drc_attach(drc, dev);
109ee3a71e3SShivaprasad G Bhat 
110ee3a71e3SShivaprasad G Bhat     if (hotplugged) {
111ee3a71e3SShivaprasad G Bhat         spapr_hotplug_req_add_by_index(drc);
112ee3a71e3SShivaprasad G Bhat     }
113ee3a71e3SShivaprasad G Bhat }
114ee3a71e3SShivaprasad G Bhat 
115f1aa45ffSDaniel Henrique Barboza static int spapr_dt_nvdimm(SpaprMachineState *spapr, void *fdt,
116f1aa45ffSDaniel Henrique Barboza                            int parent_offset, NVDIMMDevice *nvdimm)
117ee3a71e3SShivaprasad G Bhat {
118ee3a71e3SShivaprasad G Bhat     int child_offset;
119ee3a71e3SShivaprasad G Bhat     char *buf;
120ee3a71e3SShivaprasad G Bhat     SpaprDrc *drc;
121ee3a71e3SShivaprasad G Bhat     uint32_t drc_idx;
122ee3a71e3SShivaprasad G Bhat     uint32_t node = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_NODE_PROP,
123ee3a71e3SShivaprasad G Bhat                                              &error_abort);
124ee3a71e3SShivaprasad G Bhat     uint64_t slot = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_SLOT_PROP,
125ee3a71e3SShivaprasad G Bhat                                              &error_abort);
126ee3a71e3SShivaprasad G Bhat     uint64_t lsize = nvdimm->label_size;
127ee3a71e3SShivaprasad G Bhat     uint64_t size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
128ee3a71e3SShivaprasad G Bhat                                             NULL);
129ee3a71e3SShivaprasad G Bhat 
130ee3a71e3SShivaprasad G Bhat     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot);
131ee3a71e3SShivaprasad G Bhat     g_assert(drc);
132ee3a71e3SShivaprasad G Bhat 
133ee3a71e3SShivaprasad G Bhat     drc_idx = spapr_drc_index(drc);
134ee3a71e3SShivaprasad G Bhat 
135ee3a71e3SShivaprasad G Bhat     buf = g_strdup_printf("ibm,pmemory@%x", drc_idx);
136ee3a71e3SShivaprasad G Bhat     child_offset = fdt_add_subnode(fdt, parent_offset, buf);
137ee3a71e3SShivaprasad G Bhat     g_free(buf);
138ee3a71e3SShivaprasad G Bhat 
139ee3a71e3SShivaprasad G Bhat     _FDT(child_offset);
140ee3a71e3SShivaprasad G Bhat 
141ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "reg", drc_idx)));
142ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "compatible", "ibm,pmemory")));
143ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "device_type", "ibm,pmemory")));
144ee3a71e3SShivaprasad G Bhat 
145f1aa45ffSDaniel Henrique Barboza     spapr_numa_write_associativity_dt(spapr, fdt, child_offset, node);
146ee3a71e3SShivaprasad G Bhat 
147ee3a71e3SShivaprasad G Bhat     buf = qemu_uuid_unparse_strdup(&nvdimm->uuid);
148ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "ibm,unit-guid", buf)));
149ee3a71e3SShivaprasad G Bhat     g_free(buf);
150ee3a71e3SShivaprasad G Bhat 
151ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,my-drc-index", drc_idx)));
152ee3a71e3SShivaprasad G Bhat 
153ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,block-size",
154ee3a71e3SShivaprasad G Bhat                           SPAPR_MINIMUM_SCM_BLOCK_SIZE)));
155ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,number-of-blocks",
156ee3a71e3SShivaprasad G Bhat                           size / SPAPR_MINIMUM_SCM_BLOCK_SIZE)));
157ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,metadata-size", lsize)));
158ee3a71e3SShivaprasad G Bhat 
159ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "ibm,pmem-application",
160ee3a71e3SShivaprasad G Bhat                              "operating-system")));
161ee3a71e3SShivaprasad G Bhat     _FDT(fdt_setprop(fdt, child_offset, "ibm,cache-flush-required", NULL, 0));
162ee3a71e3SShivaprasad G Bhat 
163ee3a71e3SShivaprasad G Bhat     return child_offset;
164ee3a71e3SShivaprasad G Bhat }
165ee3a71e3SShivaprasad G Bhat 
1666ee1d62eSDaniel Henrique Barboza int spapr_pmem_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
1676ee1d62eSDaniel Henrique Barboza                            void *fdt, int *fdt_start_offset, Error **errp)
1686ee1d62eSDaniel Henrique Barboza {
1696ee1d62eSDaniel Henrique Barboza     NVDIMMDevice *nvdimm = NVDIMM(drc->dev);
1706ee1d62eSDaniel Henrique Barboza 
171f1aa45ffSDaniel Henrique Barboza     *fdt_start_offset = spapr_dt_nvdimm(spapr, fdt, 0, nvdimm);
1726ee1d62eSDaniel Henrique Barboza 
1736ee1d62eSDaniel Henrique Barboza     return 0;
1746ee1d62eSDaniel Henrique Barboza }
1756ee1d62eSDaniel Henrique Barboza 
176f1aa45ffSDaniel Henrique Barboza void spapr_dt_persistent_memory(SpaprMachineState *spapr, void *fdt)
177ee3a71e3SShivaprasad G Bhat {
178*9f9f82daSShivaprasad G Bhat     int offset = fdt_subnode_offset(fdt, 0, "ibm,persistent-memory");
179ee3a71e3SShivaprasad G Bhat     GSList *iter, *nvdimms = nvdimm_get_device_list();
180ee3a71e3SShivaprasad G Bhat 
181ee3a71e3SShivaprasad G Bhat     if (offset < 0) {
182*9f9f82daSShivaprasad G Bhat         offset = fdt_add_subnode(fdt, 0, "ibm,persistent-memory");
183ee3a71e3SShivaprasad G Bhat         _FDT(offset);
184ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
185ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
186ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_string(fdt, offset, "device_type",
187ee3a71e3SShivaprasad G Bhat                                  "ibm,persistent-memory")));
188ee3a71e3SShivaprasad G Bhat     }
189ee3a71e3SShivaprasad G Bhat 
190ee3a71e3SShivaprasad G Bhat     /* Create DT entries for cold plugged NVDIMM devices */
191ee3a71e3SShivaprasad G Bhat     for (iter = nvdimms; iter; iter = iter->next) {
192ee3a71e3SShivaprasad G Bhat         NVDIMMDevice *nvdimm = iter->data;
193ee3a71e3SShivaprasad G Bhat 
194f1aa45ffSDaniel Henrique Barboza         spapr_dt_nvdimm(spapr, fdt, offset, nvdimm);
195ee3a71e3SShivaprasad G Bhat     }
196ee3a71e3SShivaprasad G Bhat     g_slist_free(nvdimms);
197ee3a71e3SShivaprasad G Bhat 
198ee3a71e3SShivaprasad G Bhat     return;
199ee3a71e3SShivaprasad G Bhat }
200b5fca656SShivaprasad G Bhat 
201b5fca656SShivaprasad G Bhat static target_ulong h_scm_read_metadata(PowerPCCPU *cpu,
202b5fca656SShivaprasad G Bhat                                         SpaprMachineState *spapr,
203b5fca656SShivaprasad G Bhat                                         target_ulong opcode,
204b5fca656SShivaprasad G Bhat                                         target_ulong *args)
205b5fca656SShivaprasad G Bhat {
206b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
207b5fca656SShivaprasad G Bhat     uint64_t offset = args[1];
208b5fca656SShivaprasad G Bhat     uint64_t len = args[2];
209b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
210b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
211b5fca656SShivaprasad G Bhat     NVDIMMClass *ddc;
212b5fca656SShivaprasad G Bhat     uint64_t data = 0;
213b5fca656SShivaprasad G Bhat     uint8_t buf[8] = { 0 };
214b5fca656SShivaprasad G Bhat 
215b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
216b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
217b5fca656SShivaprasad G Bhat         return H_PARAMETER;
218b5fca656SShivaprasad G Bhat     }
219b5fca656SShivaprasad G Bhat 
220b5fca656SShivaprasad G Bhat     if (len != 1 && len != 2 &&
221b5fca656SShivaprasad G Bhat         len != 4 && len != 8) {
222b5fca656SShivaprasad G Bhat         return H_P3;
223b5fca656SShivaprasad G Bhat     }
224b5fca656SShivaprasad G Bhat 
225b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
226b5fca656SShivaprasad G Bhat     if ((offset + len < offset) ||
227b5fca656SShivaprasad G Bhat         (nvdimm->label_size < len + offset)) {
228b5fca656SShivaprasad G Bhat         return H_P2;
229b5fca656SShivaprasad G Bhat     }
230b5fca656SShivaprasad G Bhat 
231b5fca656SShivaprasad G Bhat     ddc = NVDIMM_GET_CLASS(nvdimm);
232b5fca656SShivaprasad G Bhat     ddc->read_label_data(nvdimm, buf, len, offset);
233b5fca656SShivaprasad G Bhat 
234b5fca656SShivaprasad G Bhat     switch (len) {
235b5fca656SShivaprasad G Bhat     case 1:
236b5fca656SShivaprasad G Bhat         data = ldub_p(buf);
237b5fca656SShivaprasad G Bhat         break;
238b5fca656SShivaprasad G Bhat     case 2:
239b5fca656SShivaprasad G Bhat         data = lduw_be_p(buf);
240b5fca656SShivaprasad G Bhat         break;
241b5fca656SShivaprasad G Bhat     case 4:
242b5fca656SShivaprasad G Bhat         data = ldl_be_p(buf);
243b5fca656SShivaprasad G Bhat         break;
244b5fca656SShivaprasad G Bhat     case 8:
245b5fca656SShivaprasad G Bhat         data = ldq_be_p(buf);
246b5fca656SShivaprasad G Bhat         break;
247b5fca656SShivaprasad G Bhat     default:
248b5fca656SShivaprasad G Bhat         g_assert_not_reached();
249b5fca656SShivaprasad G Bhat     }
250b5fca656SShivaprasad G Bhat 
251b5fca656SShivaprasad G Bhat     args[0] = data;
252b5fca656SShivaprasad G Bhat 
253b5fca656SShivaprasad G Bhat     return H_SUCCESS;
254b5fca656SShivaprasad G Bhat }
255b5fca656SShivaprasad G Bhat 
256b5fca656SShivaprasad G Bhat static target_ulong h_scm_write_metadata(PowerPCCPU *cpu,
257b5fca656SShivaprasad G Bhat                                          SpaprMachineState *spapr,
258b5fca656SShivaprasad G Bhat                                          target_ulong opcode,
259b5fca656SShivaprasad G Bhat                                          target_ulong *args)
260b5fca656SShivaprasad G Bhat {
261b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
262b5fca656SShivaprasad G Bhat     uint64_t offset = args[1];
263b5fca656SShivaprasad G Bhat     uint64_t data = args[2];
264b5fca656SShivaprasad G Bhat     uint64_t len = args[3];
265b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
266b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
267b5fca656SShivaprasad G Bhat     NVDIMMClass *ddc;
268b5fca656SShivaprasad G Bhat     uint8_t buf[8] = { 0 };
269b5fca656SShivaprasad G Bhat 
270b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
271b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
272b5fca656SShivaprasad G Bhat         return H_PARAMETER;
273b5fca656SShivaprasad G Bhat     }
274b5fca656SShivaprasad G Bhat 
275b5fca656SShivaprasad G Bhat     if (len != 1 && len != 2 &&
276b5fca656SShivaprasad G Bhat         len != 4 && len != 8) {
277b5fca656SShivaprasad G Bhat         return H_P4;
278b5fca656SShivaprasad G Bhat     }
279b5fca656SShivaprasad G Bhat 
280b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
281b5fca656SShivaprasad G Bhat     if ((offset + len < offset) ||
282b5fca656SShivaprasad G Bhat         (nvdimm->label_size < len + offset)) {
283b5fca656SShivaprasad G Bhat         return H_P2;
284b5fca656SShivaprasad G Bhat     }
285b5fca656SShivaprasad G Bhat 
286b5fca656SShivaprasad G Bhat     switch (len) {
287b5fca656SShivaprasad G Bhat     case 1:
288b5fca656SShivaprasad G Bhat         if (data & 0xffffffffffffff00) {
289b5fca656SShivaprasad G Bhat             return H_P2;
290b5fca656SShivaprasad G Bhat         }
291b5fca656SShivaprasad G Bhat         stb_p(buf, data);
292b5fca656SShivaprasad G Bhat         break;
293b5fca656SShivaprasad G Bhat     case 2:
294b5fca656SShivaprasad G Bhat         if (data & 0xffffffffffff0000) {
295b5fca656SShivaprasad G Bhat             return H_P2;
296b5fca656SShivaprasad G Bhat         }
297b5fca656SShivaprasad G Bhat         stw_be_p(buf, data);
298b5fca656SShivaprasad G Bhat         break;
299b5fca656SShivaprasad G Bhat     case 4:
300b5fca656SShivaprasad G Bhat         if (data & 0xffffffff00000000) {
301b5fca656SShivaprasad G Bhat             return H_P2;
302b5fca656SShivaprasad G Bhat         }
303b5fca656SShivaprasad G Bhat         stl_be_p(buf, data);
304b5fca656SShivaprasad G Bhat         break;
305b5fca656SShivaprasad G Bhat     case 8:
306b5fca656SShivaprasad G Bhat         stq_be_p(buf, data);
307b5fca656SShivaprasad G Bhat         break;
308b5fca656SShivaprasad G Bhat     default:
309b5fca656SShivaprasad G Bhat             g_assert_not_reached();
310b5fca656SShivaprasad G Bhat     }
311b5fca656SShivaprasad G Bhat 
312b5fca656SShivaprasad G Bhat     ddc = NVDIMM_GET_CLASS(nvdimm);
313b5fca656SShivaprasad G Bhat     ddc->write_label_data(nvdimm, buf, len, offset);
314b5fca656SShivaprasad G Bhat 
315b5fca656SShivaprasad G Bhat     return H_SUCCESS;
316b5fca656SShivaprasad G Bhat }
317b5fca656SShivaprasad G Bhat 
318b5fca656SShivaprasad G Bhat static target_ulong h_scm_bind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr,
319b5fca656SShivaprasad G Bhat                                    target_ulong opcode, target_ulong *args)
320b5fca656SShivaprasad G Bhat {
321b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
322b5fca656SShivaprasad G Bhat     uint64_t starting_idx = args[1];
323b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_to_bind = args[2];
324b5fca656SShivaprasad G Bhat     uint64_t target_logical_mem_addr = args[3];
325b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[4];
326b5fca656SShivaprasad G Bhat     uint64_t size;
327b5fca656SShivaprasad G Bhat     uint64_t total_no_of_scm_blocks;
328b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
329b5fca656SShivaprasad G Bhat     hwaddr addr;
330b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
331b5fca656SShivaprasad G Bhat 
332b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
333b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
334b5fca656SShivaprasad G Bhat         return H_PARAMETER;
335b5fca656SShivaprasad G Bhat     }
336b5fca656SShivaprasad G Bhat 
337b5fca656SShivaprasad G Bhat     /*
338b5fca656SShivaprasad G Bhat      * Currently continue token should be zero qemu has already bound
339b5fca656SShivaprasad G Bhat      * everything and this hcall doesnt return H_BUSY.
340b5fca656SShivaprasad G Bhat      */
341b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
342b5fca656SShivaprasad G Bhat         return H_P5;
343b5fca656SShivaprasad G Bhat     }
344b5fca656SShivaprasad G Bhat 
345b5fca656SShivaprasad G Bhat     /* Currently qemu assigns the address. */
346b5fca656SShivaprasad G Bhat     if (target_logical_mem_addr != 0xffffffffffffffff) {
347b5fca656SShivaprasad G Bhat         return H_OVERLAP;
348b5fca656SShivaprasad G Bhat     }
349b5fca656SShivaprasad G Bhat 
350b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
351b5fca656SShivaprasad G Bhat 
352b5fca656SShivaprasad G Bhat     size = object_property_get_uint(OBJECT(nvdimm),
353b5fca656SShivaprasad G Bhat                                     PC_DIMM_SIZE_PROP, &error_abort);
354b5fca656SShivaprasad G Bhat 
355b5fca656SShivaprasad G Bhat     total_no_of_scm_blocks = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
356b5fca656SShivaprasad G Bhat 
357b5fca656SShivaprasad G Bhat     if (starting_idx > total_no_of_scm_blocks) {
358b5fca656SShivaprasad G Bhat         return H_P2;
359b5fca656SShivaprasad G Bhat     }
360b5fca656SShivaprasad G Bhat 
361b5fca656SShivaprasad G Bhat     if (((starting_idx + no_of_scm_blocks_to_bind) < starting_idx) ||
362b5fca656SShivaprasad G Bhat         ((starting_idx + no_of_scm_blocks_to_bind) > total_no_of_scm_blocks)) {
363b5fca656SShivaprasad G Bhat         return H_P3;
364b5fca656SShivaprasad G Bhat     }
365b5fca656SShivaprasad G Bhat 
366b5fca656SShivaprasad G Bhat     addr = object_property_get_uint(OBJECT(nvdimm),
367b5fca656SShivaprasad G Bhat                                     PC_DIMM_ADDR_PROP, &error_abort);
368b5fca656SShivaprasad G Bhat 
369b5fca656SShivaprasad G Bhat     addr += starting_idx * SPAPR_MINIMUM_SCM_BLOCK_SIZE;
370b5fca656SShivaprasad G Bhat 
371b5fca656SShivaprasad G Bhat     /* Already bound, Return target logical address in R5 */
372b5fca656SShivaprasad G Bhat     args[1] = addr;
373b5fca656SShivaprasad G Bhat     args[2] = no_of_scm_blocks_to_bind;
374b5fca656SShivaprasad G Bhat 
375b5fca656SShivaprasad G Bhat     return H_SUCCESS;
376b5fca656SShivaprasad G Bhat }
377b5fca656SShivaprasad G Bhat 
378b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr,
379b5fca656SShivaprasad G Bhat                                      target_ulong opcode, target_ulong *args)
380b5fca656SShivaprasad G Bhat {
381b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
382b5fca656SShivaprasad G Bhat     uint64_t starting_scm_logical_addr = args[1];
383b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_to_unbind = args[2];
384b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[3];
385b5fca656SShivaprasad G Bhat     uint64_t size_to_unbind;
386b5fca656SShivaprasad G Bhat     Range blockrange = range_empty;
387b5fca656SShivaprasad G Bhat     Range nvdimmrange = range_empty;
388b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
389b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
390b5fca656SShivaprasad G Bhat     uint64_t size, addr;
391b5fca656SShivaprasad G Bhat 
392b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
393b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
394b5fca656SShivaprasad G Bhat         return H_PARAMETER;
395b5fca656SShivaprasad G Bhat     }
396b5fca656SShivaprasad G Bhat 
397b5fca656SShivaprasad G Bhat     /* continue_token should be zero as this hcall doesn't return H_BUSY. */
398b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
399b5fca656SShivaprasad G Bhat         return H_P4;
400b5fca656SShivaprasad G Bhat     }
401b5fca656SShivaprasad G Bhat 
402b5fca656SShivaprasad G Bhat     /* Check if starting_scm_logical_addr is block aligned */
403b5fca656SShivaprasad G Bhat     if (!QEMU_IS_ALIGNED(starting_scm_logical_addr,
404b5fca656SShivaprasad G Bhat                          SPAPR_MINIMUM_SCM_BLOCK_SIZE)) {
405b5fca656SShivaprasad G Bhat         return H_P2;
406b5fca656SShivaprasad G Bhat     }
407b5fca656SShivaprasad G Bhat 
408b5fca656SShivaprasad G Bhat     size_to_unbind = no_of_scm_blocks_to_unbind * SPAPR_MINIMUM_SCM_BLOCK_SIZE;
409b5fca656SShivaprasad G Bhat     if (no_of_scm_blocks_to_unbind == 0 || no_of_scm_blocks_to_unbind !=
410b5fca656SShivaprasad G Bhat                                size_to_unbind / SPAPR_MINIMUM_SCM_BLOCK_SIZE) {
411b5fca656SShivaprasad G Bhat         return H_P3;
412b5fca656SShivaprasad G Bhat     }
413b5fca656SShivaprasad G Bhat 
414b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
415b5fca656SShivaprasad G Bhat     size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
416b5fca656SShivaprasad G Bhat                                    &error_abort);
417b5fca656SShivaprasad G Bhat     addr = object_property_get_int(OBJECT(nvdimm), PC_DIMM_ADDR_PROP,
418b5fca656SShivaprasad G Bhat                                    &error_abort);
419b5fca656SShivaprasad G Bhat 
420b5fca656SShivaprasad G Bhat     range_init_nofail(&nvdimmrange, addr, size);
421b5fca656SShivaprasad G Bhat     range_init_nofail(&blockrange, starting_scm_logical_addr, size_to_unbind);
422b5fca656SShivaprasad G Bhat 
423b5fca656SShivaprasad G Bhat     if (!range_contains_range(&nvdimmrange, &blockrange)) {
424b5fca656SShivaprasad G Bhat         return H_P3;
425b5fca656SShivaprasad G Bhat     }
426b5fca656SShivaprasad G Bhat 
427b5fca656SShivaprasad G Bhat     args[1] = no_of_scm_blocks_to_unbind;
428b5fca656SShivaprasad G Bhat 
429b5fca656SShivaprasad G Bhat     /* let unplug take care of actual unbind */
430b5fca656SShivaprasad G Bhat     return H_SUCCESS;
431b5fca656SShivaprasad G Bhat }
432b5fca656SShivaprasad G Bhat 
433b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_ALL 0x1
434b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_DRC 0x2
435b5fca656SShivaprasad G Bhat 
436b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_all(PowerPCCPU *cpu, SpaprMachineState *spapr,
437b5fca656SShivaprasad G Bhat                                      target_ulong opcode, target_ulong *args)
438b5fca656SShivaprasad G Bhat {
439b5fca656SShivaprasad G Bhat     uint64_t target_scope = args[0];
440b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[1];
441b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[2];
442b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
443b5fca656SShivaprasad G Bhat     uint64_t size;
444b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_unbound = 0;
445b5fca656SShivaprasad G Bhat 
446b5fca656SShivaprasad G Bhat     /* continue_token should be zero as this hcall doesn't return H_BUSY. */
447b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
448b5fca656SShivaprasad G Bhat         return H_P4;
449b5fca656SShivaprasad G Bhat     }
450b5fca656SShivaprasad G Bhat 
451b5fca656SShivaprasad G Bhat     if (target_scope == H_UNBIND_SCOPE_DRC) {
452b5fca656SShivaprasad G Bhat         SpaprDrc *drc = spapr_drc_by_index(drc_index);
453b5fca656SShivaprasad G Bhat 
454b5fca656SShivaprasad G Bhat         if (!drc || !drc->dev ||
455b5fca656SShivaprasad G Bhat             spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
456b5fca656SShivaprasad G Bhat             return H_P2;
457b5fca656SShivaprasad G Bhat         }
458b5fca656SShivaprasad G Bhat 
459b5fca656SShivaprasad G Bhat         nvdimm = NVDIMM(drc->dev);
460b5fca656SShivaprasad G Bhat         size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
461b5fca656SShivaprasad G Bhat                                        &error_abort);
462b5fca656SShivaprasad G Bhat 
463b5fca656SShivaprasad G Bhat         no_of_scm_blocks_unbound = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
464b5fca656SShivaprasad G Bhat     } else if (target_scope ==  H_UNBIND_SCOPE_ALL) {
465b5fca656SShivaprasad G Bhat         GSList *list, *nvdimms;
466b5fca656SShivaprasad G Bhat 
467b5fca656SShivaprasad G Bhat         nvdimms = nvdimm_get_device_list();
468b5fca656SShivaprasad G Bhat         for (list = nvdimms; list; list = list->next) {
469b5fca656SShivaprasad G Bhat             nvdimm = list->data;
470b5fca656SShivaprasad G Bhat             size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
471b5fca656SShivaprasad G Bhat                                            &error_abort);
472b5fca656SShivaprasad G Bhat 
473b5fca656SShivaprasad G Bhat             no_of_scm_blocks_unbound += size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
474b5fca656SShivaprasad G Bhat         }
475b5fca656SShivaprasad G Bhat         g_slist_free(nvdimms);
476b5fca656SShivaprasad G Bhat     } else {
477b5fca656SShivaprasad G Bhat         return H_PARAMETER;
478b5fca656SShivaprasad G Bhat     }
479b5fca656SShivaprasad G Bhat 
480b5fca656SShivaprasad G Bhat     args[1] = no_of_scm_blocks_unbound;
481b5fca656SShivaprasad G Bhat 
482b5fca656SShivaprasad G Bhat     /* let unplug take care of actual unbind */
483b5fca656SShivaprasad G Bhat     return H_SUCCESS;
484b5fca656SShivaprasad G Bhat }
485b5fca656SShivaprasad G Bhat 
48653d7d7e2SVaibhav Jain static target_ulong h_scm_health(PowerPCCPU *cpu, SpaprMachineState *spapr,
48753d7d7e2SVaibhav Jain                                  target_ulong opcode, target_ulong *args)
48853d7d7e2SVaibhav Jain {
48953d7d7e2SVaibhav Jain 
49053d7d7e2SVaibhav Jain     NVDIMMDevice *nvdimm;
49153d7d7e2SVaibhav Jain     uint64_t hbitmap = 0;
49253d7d7e2SVaibhav Jain     uint32_t drc_index = args[0];
49353d7d7e2SVaibhav Jain     SpaprDrc *drc = spapr_drc_by_index(drc_index);
49453d7d7e2SVaibhav Jain     const uint64_t hbitmap_mask = PAPR_PMEM_UNARMED;
49553d7d7e2SVaibhav Jain 
49653d7d7e2SVaibhav Jain 
49753d7d7e2SVaibhav Jain     /* Ensure that the drc is valid & is valid PMEM dimm and is plugged in */
49853d7d7e2SVaibhav Jain     if (!drc || !drc->dev ||
49953d7d7e2SVaibhav Jain         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
50053d7d7e2SVaibhav Jain         return H_PARAMETER;
50153d7d7e2SVaibhav Jain     }
50253d7d7e2SVaibhav Jain 
50353d7d7e2SVaibhav Jain     nvdimm = NVDIMM(drc->dev);
50453d7d7e2SVaibhav Jain 
50553d7d7e2SVaibhav Jain     /* Update if the nvdimm is unarmed and send its status via health bitmaps */
50653d7d7e2SVaibhav Jain     if (object_property_get_bool(OBJECT(nvdimm), NVDIMM_UNARMED_PROP, NULL)) {
50753d7d7e2SVaibhav Jain         hbitmap |= PAPR_PMEM_UNARMED;
50853d7d7e2SVaibhav Jain     }
50953d7d7e2SVaibhav Jain 
51053d7d7e2SVaibhav Jain     /* Update the out args with health bitmap/mask */
51153d7d7e2SVaibhav Jain     args[0] = hbitmap;
51253d7d7e2SVaibhav Jain     args[1] = hbitmap_mask;
51353d7d7e2SVaibhav Jain 
51453d7d7e2SVaibhav Jain     return H_SUCCESS;
51553d7d7e2SVaibhav Jain }
51653d7d7e2SVaibhav Jain 
517b5fca656SShivaprasad G Bhat static void spapr_scm_register_types(void)
518b5fca656SShivaprasad G Bhat {
519b5fca656SShivaprasad G Bhat     /* qemu/scm specific hcalls */
520b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_READ_METADATA, h_scm_read_metadata);
521b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_WRITE_METADATA, h_scm_write_metadata);
522b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_BIND_MEM, h_scm_bind_mem);
523b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_UNBIND_MEM, h_scm_unbind_mem);
524b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_UNBIND_ALL, h_scm_unbind_all);
52553d7d7e2SVaibhav Jain     spapr_register_hypercall(H_SCM_HEALTH, h_scm_health);
526b5fca656SShivaprasad G Bhat }
527b5fca656SShivaprasad G Bhat 
528b5fca656SShivaprasad G Bhat type_init(spapr_scm_register_types)
529