xref: /qemu/hw/ppc/spapr_nvdimm.c (revision f1aa45fffeeb084a9ad8bd08e83c5ec6af223884)
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"
3028f5a716SDaniel Henrique Barboza #include "qemu/option.h"
31ee3a71e3SShivaprasad G Bhat #include "hw/ppc/fdt.h"
32b5fca656SShivaprasad G Bhat #include "qemu/range.h"
3328f5a716SDaniel Henrique Barboza #include "sysemu/sysemu.h"
34*f1aa45ffSDaniel Henrique Barboza #include "hw/ppc/spapr_numa.h"
35ee3a71e3SShivaprasad G Bhat 
36beb6073fSDaniel Henrique Barboza void spapr_nvdimm_validate(HotplugHandler *hotplug_dev, NVDIMMDevice *nvdimm,
37beb6073fSDaniel Henrique Barboza                            uint64_t size, Error **errp)
38ee3a71e3SShivaprasad G Bhat {
39beb6073fSDaniel Henrique Barboza     const MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
4028f5a716SDaniel Henrique Barboza     const MachineState *ms = MACHINE(hotplug_dev);
4128f5a716SDaniel Henrique Barboza     const char *nvdimm_opt = qemu_opt_get(qemu_get_machine_opts(), "nvdimm");
4290d282d0SDaniel Henrique Barboza     g_autofree char *uuidstr = NULL;
43ee3a71e3SShivaprasad G Bhat     QemuUUID uuid;
44af7084e7SShivaprasad G Bhat     int ret;
45ee3a71e3SShivaprasad G Bhat 
46beb6073fSDaniel Henrique Barboza     if (!mc->nvdimm_supported) {
47beb6073fSDaniel Henrique Barboza         error_setg(errp, "NVDIMM hotplug not supported for this machine");
48beb6073fSDaniel Henrique Barboza         return;
49beb6073fSDaniel Henrique Barboza     }
50beb6073fSDaniel Henrique Barboza 
5128f5a716SDaniel Henrique Barboza     /*
5228f5a716SDaniel Henrique Barboza      * NVDIMM support went live in 5.1 without considering that, in
5328f5a716SDaniel Henrique Barboza      * other archs, the user needs to enable NVDIMM support with the
5428f5a716SDaniel Henrique Barboza      * 'nvdimm' machine option and the default behavior is NVDIMM
5528f5a716SDaniel Henrique Barboza      * support disabled. It is too late to roll back to the standard
5628f5a716SDaniel Henrique Barboza      * behavior without breaking 5.1 guests. What we can do is to
5728f5a716SDaniel Henrique Barboza      * ensure that, if the user sets nvdimm=off, we error out
5828f5a716SDaniel Henrique Barboza      * regardless of being 5.1 or newer.
5928f5a716SDaniel Henrique Barboza      */
6028f5a716SDaniel Henrique Barboza     if (!ms->nvdimms_state->is_enabled && nvdimm_opt) {
6128f5a716SDaniel Henrique Barboza         error_setg(errp, "nvdimm device found but 'nvdimm=off' was set");
6228f5a716SDaniel Henrique Barboza         return;
6328f5a716SDaniel Henrique Barboza     }
6428f5a716SDaniel Henrique Barboza 
6570fc9cb0SDaniel Henrique Barboza     if (object_property_get_int(OBJECT(nvdimm), NVDIMM_LABEL_SIZE_PROP,
6670fc9cb0SDaniel Henrique Barboza                                 &error_abort) == 0) {
676c0f0cb3SDavid Gibson         error_setg(errp, "PAPR requires NVDIMM devices to have label-size set");
6870fc9cb0SDaniel Henrique Barboza         return;
6970fc9cb0SDaniel Henrique Barboza     }
7070fc9cb0SDaniel Henrique Barboza 
71ee3a71e3SShivaprasad G Bhat     if (size % SPAPR_MINIMUM_SCM_BLOCK_SIZE) {
726c0f0cb3SDavid Gibson         error_setg(errp, "PAPR requires NVDIMM memory size (excluding label)"
736c0f0cb3SDavid Gibson                    " to be a multiple of %" PRIu64 "MB",
74ee3a71e3SShivaprasad G Bhat                    SPAPR_MINIMUM_SCM_BLOCK_SIZE / MiB);
75ee3a71e3SShivaprasad G Bhat         return;
76ee3a71e3SShivaprasad G Bhat     }
77ee3a71e3SShivaprasad G Bhat 
78af7084e7SShivaprasad G Bhat     uuidstr = object_property_get_str(OBJECT(nvdimm), NVDIMM_UUID_PROP,
79af7084e7SShivaprasad G Bhat                                       &error_abort);
80af7084e7SShivaprasad G Bhat     ret = qemu_uuid_parse(uuidstr, &uuid);
81af7084e7SShivaprasad G Bhat     g_assert(!ret);
82ee3a71e3SShivaprasad G Bhat 
83ee3a71e3SShivaprasad G Bhat     if (qemu_uuid_is_null(&uuid)) {
84ee3a71e3SShivaprasad G Bhat         error_setg(errp, "NVDIMM device requires the uuid to be set");
85ee3a71e3SShivaprasad G Bhat         return;
86ee3a71e3SShivaprasad G Bhat     }
87ee3a71e3SShivaprasad G Bhat }
88ee3a71e3SShivaprasad G Bhat 
89ee3a71e3SShivaprasad G Bhat 
90ee3a71e3SShivaprasad G Bhat void spapr_add_nvdimm(DeviceState *dev, uint64_t slot, Error **errp)
91ee3a71e3SShivaprasad G Bhat {
92ee3a71e3SShivaprasad G Bhat     SpaprDrc *drc;
93ee3a71e3SShivaprasad G Bhat     bool hotplugged = spapr_drc_hotplugged(dev);
94ee3a71e3SShivaprasad G Bhat     Error *local_err = NULL;
95ee3a71e3SShivaprasad G Bhat 
96ee3a71e3SShivaprasad G Bhat     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot);
97ee3a71e3SShivaprasad G Bhat     g_assert(drc);
98ee3a71e3SShivaprasad G Bhat 
99ee3a71e3SShivaprasad G Bhat     spapr_drc_attach(drc, dev, &local_err);
100ee3a71e3SShivaprasad G Bhat     if (local_err) {
101ee3a71e3SShivaprasad G Bhat         error_propagate(errp, local_err);
102ee3a71e3SShivaprasad G Bhat         return;
103ee3a71e3SShivaprasad G Bhat     }
104ee3a71e3SShivaprasad G Bhat 
105ee3a71e3SShivaprasad G Bhat     if (hotplugged) {
106ee3a71e3SShivaprasad G Bhat         spapr_hotplug_req_add_by_index(drc);
107ee3a71e3SShivaprasad G Bhat     }
108ee3a71e3SShivaprasad G Bhat }
109ee3a71e3SShivaprasad G Bhat 
110ee3a71e3SShivaprasad G Bhat void spapr_create_nvdimm_dr_connectors(SpaprMachineState *spapr)
111ee3a71e3SShivaprasad G Bhat {
112ee3a71e3SShivaprasad G Bhat     MachineState *machine = MACHINE(spapr);
113ee3a71e3SShivaprasad G Bhat     int i;
114ee3a71e3SShivaprasad G Bhat 
115ee3a71e3SShivaprasad G Bhat     for (i = 0; i < machine->ram_slots; i++) {
116ee3a71e3SShivaprasad G Bhat         spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_PMEM, i);
117ee3a71e3SShivaprasad G Bhat     }
118ee3a71e3SShivaprasad G Bhat }
119ee3a71e3SShivaprasad G Bhat 
120ee3a71e3SShivaprasad G Bhat 
121*f1aa45ffSDaniel Henrique Barboza static int spapr_dt_nvdimm(SpaprMachineState *spapr, void *fdt,
122*f1aa45ffSDaniel Henrique Barboza                            int parent_offset, NVDIMMDevice *nvdimm)
123ee3a71e3SShivaprasad G Bhat {
124ee3a71e3SShivaprasad G Bhat     int child_offset;
125ee3a71e3SShivaprasad G Bhat     char *buf;
126ee3a71e3SShivaprasad G Bhat     SpaprDrc *drc;
127ee3a71e3SShivaprasad G Bhat     uint32_t drc_idx;
128ee3a71e3SShivaprasad G Bhat     uint32_t node = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_NODE_PROP,
129ee3a71e3SShivaprasad G Bhat                                              &error_abort);
130ee3a71e3SShivaprasad G Bhat     uint64_t slot = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_SLOT_PROP,
131ee3a71e3SShivaprasad G Bhat                                              &error_abort);
132ee3a71e3SShivaprasad G Bhat     uint64_t lsize = nvdimm->label_size;
133ee3a71e3SShivaprasad G Bhat     uint64_t size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
134ee3a71e3SShivaprasad G Bhat                                             NULL);
135ee3a71e3SShivaprasad G Bhat 
136ee3a71e3SShivaprasad G Bhat     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot);
137ee3a71e3SShivaprasad G Bhat     g_assert(drc);
138ee3a71e3SShivaprasad G Bhat 
139ee3a71e3SShivaprasad G Bhat     drc_idx = spapr_drc_index(drc);
140ee3a71e3SShivaprasad G Bhat 
141ee3a71e3SShivaprasad G Bhat     buf = g_strdup_printf("ibm,pmemory@%x", drc_idx);
142ee3a71e3SShivaprasad G Bhat     child_offset = fdt_add_subnode(fdt, parent_offset, buf);
143ee3a71e3SShivaprasad G Bhat     g_free(buf);
144ee3a71e3SShivaprasad G Bhat 
145ee3a71e3SShivaprasad G Bhat     _FDT(child_offset);
146ee3a71e3SShivaprasad G Bhat 
147ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "reg", drc_idx)));
148ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "compatible", "ibm,pmemory")));
149ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "device_type", "ibm,pmemory")));
150ee3a71e3SShivaprasad G Bhat 
151*f1aa45ffSDaniel Henrique Barboza     spapr_numa_write_associativity_dt(spapr, fdt, child_offset, node);
152ee3a71e3SShivaprasad G Bhat 
153ee3a71e3SShivaprasad G Bhat     buf = qemu_uuid_unparse_strdup(&nvdimm->uuid);
154ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "ibm,unit-guid", buf)));
155ee3a71e3SShivaprasad G Bhat     g_free(buf);
156ee3a71e3SShivaprasad G Bhat 
157ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,my-drc-index", drc_idx)));
158ee3a71e3SShivaprasad G Bhat 
159ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,block-size",
160ee3a71e3SShivaprasad G Bhat                           SPAPR_MINIMUM_SCM_BLOCK_SIZE)));
161ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,number-of-blocks",
162ee3a71e3SShivaprasad G Bhat                           size / SPAPR_MINIMUM_SCM_BLOCK_SIZE)));
163ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,metadata-size", lsize)));
164ee3a71e3SShivaprasad G Bhat 
165ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "ibm,pmem-application",
166ee3a71e3SShivaprasad G Bhat                              "operating-system")));
167ee3a71e3SShivaprasad G Bhat     _FDT(fdt_setprop(fdt, child_offset, "ibm,cache-flush-required", NULL, 0));
168ee3a71e3SShivaprasad G Bhat 
169ee3a71e3SShivaprasad G Bhat     return child_offset;
170ee3a71e3SShivaprasad G Bhat }
171ee3a71e3SShivaprasad G Bhat 
1726ee1d62eSDaniel Henrique Barboza int spapr_pmem_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
1736ee1d62eSDaniel Henrique Barboza                            void *fdt, int *fdt_start_offset, Error **errp)
1746ee1d62eSDaniel Henrique Barboza {
1756ee1d62eSDaniel Henrique Barboza     NVDIMMDevice *nvdimm = NVDIMM(drc->dev);
1766ee1d62eSDaniel Henrique Barboza 
177*f1aa45ffSDaniel Henrique Barboza     *fdt_start_offset = spapr_dt_nvdimm(spapr, fdt, 0, nvdimm);
1786ee1d62eSDaniel Henrique Barboza 
1796ee1d62eSDaniel Henrique Barboza     return 0;
1806ee1d62eSDaniel Henrique Barboza }
1816ee1d62eSDaniel Henrique Barboza 
182*f1aa45ffSDaniel Henrique Barboza void spapr_dt_persistent_memory(SpaprMachineState *spapr, void *fdt)
183ee3a71e3SShivaprasad G Bhat {
184ee3a71e3SShivaprasad G Bhat     int offset = fdt_subnode_offset(fdt, 0, "persistent-memory");
185ee3a71e3SShivaprasad G Bhat     GSList *iter, *nvdimms = nvdimm_get_device_list();
186ee3a71e3SShivaprasad G Bhat 
187ee3a71e3SShivaprasad G Bhat     if (offset < 0) {
188ee3a71e3SShivaprasad G Bhat         offset = fdt_add_subnode(fdt, 0, "persistent-memory");
189ee3a71e3SShivaprasad G Bhat         _FDT(offset);
190ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
191ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
192ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_string(fdt, offset, "device_type",
193ee3a71e3SShivaprasad G Bhat                                  "ibm,persistent-memory")));
194ee3a71e3SShivaprasad G Bhat     }
195ee3a71e3SShivaprasad G Bhat 
196ee3a71e3SShivaprasad G Bhat     /* Create DT entries for cold plugged NVDIMM devices */
197ee3a71e3SShivaprasad G Bhat     for (iter = nvdimms; iter; iter = iter->next) {
198ee3a71e3SShivaprasad G Bhat         NVDIMMDevice *nvdimm = iter->data;
199ee3a71e3SShivaprasad G Bhat 
200*f1aa45ffSDaniel Henrique Barboza         spapr_dt_nvdimm(spapr, fdt, offset, nvdimm);
201ee3a71e3SShivaprasad G Bhat     }
202ee3a71e3SShivaprasad G Bhat     g_slist_free(nvdimms);
203ee3a71e3SShivaprasad G Bhat 
204ee3a71e3SShivaprasad G Bhat     return;
205ee3a71e3SShivaprasad G Bhat }
206b5fca656SShivaprasad G Bhat 
207b5fca656SShivaprasad G Bhat static target_ulong h_scm_read_metadata(PowerPCCPU *cpu,
208b5fca656SShivaprasad G Bhat                                         SpaprMachineState *spapr,
209b5fca656SShivaprasad G Bhat                                         target_ulong opcode,
210b5fca656SShivaprasad G Bhat                                         target_ulong *args)
211b5fca656SShivaprasad G Bhat {
212b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
213b5fca656SShivaprasad G Bhat     uint64_t offset = args[1];
214b5fca656SShivaprasad G Bhat     uint64_t len = args[2];
215b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
216b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
217b5fca656SShivaprasad G Bhat     NVDIMMClass *ddc;
218b5fca656SShivaprasad G Bhat     uint64_t data = 0;
219b5fca656SShivaprasad G Bhat     uint8_t buf[8] = { 0 };
220b5fca656SShivaprasad G Bhat 
221b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
222b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
223b5fca656SShivaprasad G Bhat         return H_PARAMETER;
224b5fca656SShivaprasad G Bhat     }
225b5fca656SShivaprasad G Bhat 
226b5fca656SShivaprasad G Bhat     if (len != 1 && len != 2 &&
227b5fca656SShivaprasad G Bhat         len != 4 && len != 8) {
228b5fca656SShivaprasad G Bhat         return H_P3;
229b5fca656SShivaprasad G Bhat     }
230b5fca656SShivaprasad G Bhat 
231b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
232b5fca656SShivaprasad G Bhat     if ((offset + len < offset) ||
233b5fca656SShivaprasad G Bhat         (nvdimm->label_size < len + offset)) {
234b5fca656SShivaprasad G Bhat         return H_P2;
235b5fca656SShivaprasad G Bhat     }
236b5fca656SShivaprasad G Bhat 
237b5fca656SShivaprasad G Bhat     ddc = NVDIMM_GET_CLASS(nvdimm);
238b5fca656SShivaprasad G Bhat     ddc->read_label_data(nvdimm, buf, len, offset);
239b5fca656SShivaprasad G Bhat 
240b5fca656SShivaprasad G Bhat     switch (len) {
241b5fca656SShivaprasad G Bhat     case 1:
242b5fca656SShivaprasad G Bhat         data = ldub_p(buf);
243b5fca656SShivaprasad G Bhat         break;
244b5fca656SShivaprasad G Bhat     case 2:
245b5fca656SShivaprasad G Bhat         data = lduw_be_p(buf);
246b5fca656SShivaprasad G Bhat         break;
247b5fca656SShivaprasad G Bhat     case 4:
248b5fca656SShivaprasad G Bhat         data = ldl_be_p(buf);
249b5fca656SShivaprasad G Bhat         break;
250b5fca656SShivaprasad G Bhat     case 8:
251b5fca656SShivaprasad G Bhat         data = ldq_be_p(buf);
252b5fca656SShivaprasad G Bhat         break;
253b5fca656SShivaprasad G Bhat     default:
254b5fca656SShivaprasad G Bhat         g_assert_not_reached();
255b5fca656SShivaprasad G Bhat     }
256b5fca656SShivaprasad G Bhat 
257b5fca656SShivaprasad G Bhat     args[0] = data;
258b5fca656SShivaprasad G Bhat 
259b5fca656SShivaprasad G Bhat     return H_SUCCESS;
260b5fca656SShivaprasad G Bhat }
261b5fca656SShivaprasad G Bhat 
262b5fca656SShivaprasad G Bhat static target_ulong h_scm_write_metadata(PowerPCCPU *cpu,
263b5fca656SShivaprasad G Bhat                                          SpaprMachineState *spapr,
264b5fca656SShivaprasad G Bhat                                          target_ulong opcode,
265b5fca656SShivaprasad G Bhat                                          target_ulong *args)
266b5fca656SShivaprasad G Bhat {
267b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
268b5fca656SShivaprasad G Bhat     uint64_t offset = args[1];
269b5fca656SShivaprasad G Bhat     uint64_t data = args[2];
270b5fca656SShivaprasad G Bhat     uint64_t len = args[3];
271b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
272b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
273b5fca656SShivaprasad G Bhat     NVDIMMClass *ddc;
274b5fca656SShivaprasad G Bhat     uint8_t buf[8] = { 0 };
275b5fca656SShivaprasad G Bhat 
276b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
277b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
278b5fca656SShivaprasad G Bhat         return H_PARAMETER;
279b5fca656SShivaprasad G Bhat     }
280b5fca656SShivaprasad G Bhat 
281b5fca656SShivaprasad G Bhat     if (len != 1 && len != 2 &&
282b5fca656SShivaprasad G Bhat         len != 4 && len != 8) {
283b5fca656SShivaprasad G Bhat         return H_P4;
284b5fca656SShivaprasad G Bhat     }
285b5fca656SShivaprasad G Bhat 
286b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
287b5fca656SShivaprasad G Bhat     if ((offset + len < offset) ||
288b5fca656SShivaprasad G Bhat         (nvdimm->label_size < len + offset)) {
289b5fca656SShivaprasad G Bhat         return H_P2;
290b5fca656SShivaprasad G Bhat     }
291b5fca656SShivaprasad G Bhat 
292b5fca656SShivaprasad G Bhat     switch (len) {
293b5fca656SShivaprasad G Bhat     case 1:
294b5fca656SShivaprasad G Bhat         if (data & 0xffffffffffffff00) {
295b5fca656SShivaprasad G Bhat             return H_P2;
296b5fca656SShivaprasad G Bhat         }
297b5fca656SShivaprasad G Bhat         stb_p(buf, data);
298b5fca656SShivaprasad G Bhat         break;
299b5fca656SShivaprasad G Bhat     case 2:
300b5fca656SShivaprasad G Bhat         if (data & 0xffffffffffff0000) {
301b5fca656SShivaprasad G Bhat             return H_P2;
302b5fca656SShivaprasad G Bhat         }
303b5fca656SShivaprasad G Bhat         stw_be_p(buf, data);
304b5fca656SShivaprasad G Bhat         break;
305b5fca656SShivaprasad G Bhat     case 4:
306b5fca656SShivaprasad G Bhat         if (data & 0xffffffff00000000) {
307b5fca656SShivaprasad G Bhat             return H_P2;
308b5fca656SShivaprasad G Bhat         }
309b5fca656SShivaprasad G Bhat         stl_be_p(buf, data);
310b5fca656SShivaprasad G Bhat         break;
311b5fca656SShivaprasad G Bhat     case 8:
312b5fca656SShivaprasad G Bhat         stq_be_p(buf, data);
313b5fca656SShivaprasad G Bhat         break;
314b5fca656SShivaprasad G Bhat     default:
315b5fca656SShivaprasad G Bhat             g_assert_not_reached();
316b5fca656SShivaprasad G Bhat     }
317b5fca656SShivaprasad G Bhat 
318b5fca656SShivaprasad G Bhat     ddc = NVDIMM_GET_CLASS(nvdimm);
319b5fca656SShivaprasad G Bhat     ddc->write_label_data(nvdimm, buf, len, offset);
320b5fca656SShivaprasad G Bhat 
321b5fca656SShivaprasad G Bhat     return H_SUCCESS;
322b5fca656SShivaprasad G Bhat }
323b5fca656SShivaprasad G Bhat 
324b5fca656SShivaprasad G Bhat static target_ulong h_scm_bind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr,
325b5fca656SShivaprasad G Bhat                                    target_ulong opcode, target_ulong *args)
326b5fca656SShivaprasad G Bhat {
327b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
328b5fca656SShivaprasad G Bhat     uint64_t starting_idx = args[1];
329b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_to_bind = args[2];
330b5fca656SShivaprasad G Bhat     uint64_t target_logical_mem_addr = args[3];
331b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[4];
332b5fca656SShivaprasad G Bhat     uint64_t size;
333b5fca656SShivaprasad G Bhat     uint64_t total_no_of_scm_blocks;
334b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
335b5fca656SShivaprasad G Bhat     hwaddr addr;
336b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
337b5fca656SShivaprasad G Bhat 
338b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
339b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
340b5fca656SShivaprasad G Bhat         return H_PARAMETER;
341b5fca656SShivaprasad G Bhat     }
342b5fca656SShivaprasad G Bhat 
343b5fca656SShivaprasad G Bhat     /*
344b5fca656SShivaprasad G Bhat      * Currently continue token should be zero qemu has already bound
345b5fca656SShivaprasad G Bhat      * everything and this hcall doesnt return H_BUSY.
346b5fca656SShivaprasad G Bhat      */
347b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
348b5fca656SShivaprasad G Bhat         return H_P5;
349b5fca656SShivaprasad G Bhat     }
350b5fca656SShivaprasad G Bhat 
351b5fca656SShivaprasad G Bhat     /* Currently qemu assigns the address. */
352b5fca656SShivaprasad G Bhat     if (target_logical_mem_addr != 0xffffffffffffffff) {
353b5fca656SShivaprasad G Bhat         return H_OVERLAP;
354b5fca656SShivaprasad G Bhat     }
355b5fca656SShivaprasad G Bhat 
356b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
357b5fca656SShivaprasad G Bhat 
358b5fca656SShivaprasad G Bhat     size = object_property_get_uint(OBJECT(nvdimm),
359b5fca656SShivaprasad G Bhat                                     PC_DIMM_SIZE_PROP, &error_abort);
360b5fca656SShivaprasad G Bhat 
361b5fca656SShivaprasad G Bhat     total_no_of_scm_blocks = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
362b5fca656SShivaprasad G Bhat 
363b5fca656SShivaprasad G Bhat     if (starting_idx > total_no_of_scm_blocks) {
364b5fca656SShivaprasad G Bhat         return H_P2;
365b5fca656SShivaprasad G Bhat     }
366b5fca656SShivaprasad G Bhat 
367b5fca656SShivaprasad G Bhat     if (((starting_idx + no_of_scm_blocks_to_bind) < starting_idx) ||
368b5fca656SShivaprasad G Bhat         ((starting_idx + no_of_scm_blocks_to_bind) > total_no_of_scm_blocks)) {
369b5fca656SShivaprasad G Bhat         return H_P3;
370b5fca656SShivaprasad G Bhat     }
371b5fca656SShivaprasad G Bhat 
372b5fca656SShivaprasad G Bhat     addr = object_property_get_uint(OBJECT(nvdimm),
373b5fca656SShivaprasad G Bhat                                     PC_DIMM_ADDR_PROP, &error_abort);
374b5fca656SShivaprasad G Bhat 
375b5fca656SShivaprasad G Bhat     addr += starting_idx * SPAPR_MINIMUM_SCM_BLOCK_SIZE;
376b5fca656SShivaprasad G Bhat 
377b5fca656SShivaprasad G Bhat     /* Already bound, Return target logical address in R5 */
378b5fca656SShivaprasad G Bhat     args[1] = addr;
379b5fca656SShivaprasad G Bhat     args[2] = no_of_scm_blocks_to_bind;
380b5fca656SShivaprasad G Bhat 
381b5fca656SShivaprasad G Bhat     return H_SUCCESS;
382b5fca656SShivaprasad G Bhat }
383b5fca656SShivaprasad G Bhat 
384b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr,
385b5fca656SShivaprasad G Bhat                                      target_ulong opcode, target_ulong *args)
386b5fca656SShivaprasad G Bhat {
387b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
388b5fca656SShivaprasad G Bhat     uint64_t starting_scm_logical_addr = args[1];
389b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_to_unbind = args[2];
390b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[3];
391b5fca656SShivaprasad G Bhat     uint64_t size_to_unbind;
392b5fca656SShivaprasad G Bhat     Range blockrange = range_empty;
393b5fca656SShivaprasad G Bhat     Range nvdimmrange = range_empty;
394b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
395b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
396b5fca656SShivaprasad G Bhat     uint64_t size, addr;
397b5fca656SShivaprasad G Bhat 
398b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
399b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
400b5fca656SShivaprasad G Bhat         return H_PARAMETER;
401b5fca656SShivaprasad G Bhat     }
402b5fca656SShivaprasad G Bhat 
403b5fca656SShivaprasad G Bhat     /* continue_token should be zero as this hcall doesn't return H_BUSY. */
404b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
405b5fca656SShivaprasad G Bhat         return H_P4;
406b5fca656SShivaprasad G Bhat     }
407b5fca656SShivaprasad G Bhat 
408b5fca656SShivaprasad G Bhat     /* Check if starting_scm_logical_addr is block aligned */
409b5fca656SShivaprasad G Bhat     if (!QEMU_IS_ALIGNED(starting_scm_logical_addr,
410b5fca656SShivaprasad G Bhat                          SPAPR_MINIMUM_SCM_BLOCK_SIZE)) {
411b5fca656SShivaprasad G Bhat         return H_P2;
412b5fca656SShivaprasad G Bhat     }
413b5fca656SShivaprasad G Bhat 
414b5fca656SShivaprasad G Bhat     size_to_unbind = no_of_scm_blocks_to_unbind * SPAPR_MINIMUM_SCM_BLOCK_SIZE;
415b5fca656SShivaprasad G Bhat     if (no_of_scm_blocks_to_unbind == 0 || no_of_scm_blocks_to_unbind !=
416b5fca656SShivaprasad G Bhat                                size_to_unbind / SPAPR_MINIMUM_SCM_BLOCK_SIZE) {
417b5fca656SShivaprasad G Bhat         return H_P3;
418b5fca656SShivaprasad G Bhat     }
419b5fca656SShivaprasad G Bhat 
420b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
421b5fca656SShivaprasad G Bhat     size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
422b5fca656SShivaprasad G Bhat                                    &error_abort);
423b5fca656SShivaprasad G Bhat     addr = object_property_get_int(OBJECT(nvdimm), PC_DIMM_ADDR_PROP,
424b5fca656SShivaprasad G Bhat                                    &error_abort);
425b5fca656SShivaprasad G Bhat 
426b5fca656SShivaprasad G Bhat     range_init_nofail(&nvdimmrange, addr, size);
427b5fca656SShivaprasad G Bhat     range_init_nofail(&blockrange, starting_scm_logical_addr, size_to_unbind);
428b5fca656SShivaprasad G Bhat 
429b5fca656SShivaprasad G Bhat     if (!range_contains_range(&nvdimmrange, &blockrange)) {
430b5fca656SShivaprasad G Bhat         return H_P3;
431b5fca656SShivaprasad G Bhat     }
432b5fca656SShivaprasad G Bhat 
433b5fca656SShivaprasad G Bhat     args[1] = no_of_scm_blocks_to_unbind;
434b5fca656SShivaprasad G Bhat 
435b5fca656SShivaprasad G Bhat     /* let unplug take care of actual unbind */
436b5fca656SShivaprasad G Bhat     return H_SUCCESS;
437b5fca656SShivaprasad G Bhat }
438b5fca656SShivaprasad G Bhat 
439b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_ALL 0x1
440b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_DRC 0x2
441b5fca656SShivaprasad G Bhat 
442b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_all(PowerPCCPU *cpu, SpaprMachineState *spapr,
443b5fca656SShivaprasad G Bhat                                      target_ulong opcode, target_ulong *args)
444b5fca656SShivaprasad G Bhat {
445b5fca656SShivaprasad G Bhat     uint64_t target_scope = args[0];
446b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[1];
447b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[2];
448b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
449b5fca656SShivaprasad G Bhat     uint64_t size;
450b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_unbound = 0;
451b5fca656SShivaprasad G Bhat 
452b5fca656SShivaprasad G Bhat     /* continue_token should be zero as this hcall doesn't return H_BUSY. */
453b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
454b5fca656SShivaprasad G Bhat         return H_P4;
455b5fca656SShivaprasad G Bhat     }
456b5fca656SShivaprasad G Bhat 
457b5fca656SShivaprasad G Bhat     if (target_scope == H_UNBIND_SCOPE_DRC) {
458b5fca656SShivaprasad G Bhat         SpaprDrc *drc = spapr_drc_by_index(drc_index);
459b5fca656SShivaprasad G Bhat 
460b5fca656SShivaprasad G Bhat         if (!drc || !drc->dev ||
461b5fca656SShivaprasad G Bhat             spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
462b5fca656SShivaprasad G Bhat             return H_P2;
463b5fca656SShivaprasad G Bhat         }
464b5fca656SShivaprasad G Bhat 
465b5fca656SShivaprasad G Bhat         nvdimm = NVDIMM(drc->dev);
466b5fca656SShivaprasad G Bhat         size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
467b5fca656SShivaprasad G Bhat                                        &error_abort);
468b5fca656SShivaprasad G Bhat 
469b5fca656SShivaprasad G Bhat         no_of_scm_blocks_unbound = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
470b5fca656SShivaprasad G Bhat     } else if (target_scope ==  H_UNBIND_SCOPE_ALL) {
471b5fca656SShivaprasad G Bhat         GSList *list, *nvdimms;
472b5fca656SShivaprasad G Bhat 
473b5fca656SShivaprasad G Bhat         nvdimms = nvdimm_get_device_list();
474b5fca656SShivaprasad G Bhat         for (list = nvdimms; list; list = list->next) {
475b5fca656SShivaprasad G Bhat             nvdimm = list->data;
476b5fca656SShivaprasad G Bhat             size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
477b5fca656SShivaprasad G Bhat                                            &error_abort);
478b5fca656SShivaprasad G Bhat 
479b5fca656SShivaprasad G Bhat             no_of_scm_blocks_unbound += size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
480b5fca656SShivaprasad G Bhat         }
481b5fca656SShivaprasad G Bhat         g_slist_free(nvdimms);
482b5fca656SShivaprasad G Bhat     } else {
483b5fca656SShivaprasad G Bhat         return H_PARAMETER;
484b5fca656SShivaprasad G Bhat     }
485b5fca656SShivaprasad G Bhat 
486b5fca656SShivaprasad G Bhat     args[1] = no_of_scm_blocks_unbound;
487b5fca656SShivaprasad G Bhat 
488b5fca656SShivaprasad G Bhat     /* let unplug take care of actual unbind */
489b5fca656SShivaprasad G Bhat     return H_SUCCESS;
490b5fca656SShivaprasad G Bhat }
491b5fca656SShivaprasad G Bhat 
492b5fca656SShivaprasad G Bhat static void spapr_scm_register_types(void)
493b5fca656SShivaprasad G Bhat {
494b5fca656SShivaprasad G Bhat     /* qemu/scm specific hcalls */
495b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_READ_METADATA, h_scm_read_metadata);
496b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_WRITE_METADATA, h_scm_write_metadata);
497b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_BIND_MEM, h_scm_bind_mem);
498b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_UNBIND_MEM, h_scm_unbind_mem);
499b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_UNBIND_ALL, h_scm_unbind_all);
500b5fca656SShivaprasad G Bhat }
501b5fca656SShivaprasad G Bhat 
502b5fca656SShivaprasad G Bhat type_init(spapr_scm_register_types)
503