xref: /qemu/hw/ppc/spapr_nvdimm.c (revision ea042c53f4d9e48e7b3c84f5d0eb70a84aa34413)
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"
34f1aa45ffSDaniel Henrique Barboza #include "hw/ppc/spapr_numa.h"
35ee3a71e3SShivaprasad G Bhat 
36451c6905SGreg Kurz bool 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");
48451c6905SGreg Kurz         return false;
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");
62451c6905SGreg Kurz         return false;
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");
68451c6905SGreg Kurz         return false;
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);
75451c6905SGreg Kurz         return false;
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");
85451c6905SGreg Kurz         return false;
86ee3a71e3SShivaprasad G Bhat     }
87451c6905SGreg Kurz 
88451c6905SGreg Kurz     return true;
89ee3a71e3SShivaprasad G Bhat }
90ee3a71e3SShivaprasad G Bhat 
91ee3a71e3SShivaprasad G Bhat 
92*ea042c53SGreg Kurz void spapr_add_nvdimm(DeviceState *dev, uint64_t slot)
93ee3a71e3SShivaprasad G Bhat {
94ee3a71e3SShivaprasad G Bhat     SpaprDrc *drc;
95ee3a71e3SShivaprasad G Bhat     bool hotplugged = spapr_drc_hotplugged(dev);
96ee3a71e3SShivaprasad G Bhat 
97ee3a71e3SShivaprasad G Bhat     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot);
98ee3a71e3SShivaprasad G Bhat     g_assert(drc);
99ee3a71e3SShivaprasad G Bhat 
100*ea042c53SGreg Kurz     /*
101*ea042c53SGreg Kurz      * pc_dimm_get_free_slot() provided a free slot at pre-plug. The
102*ea042c53SGreg Kurz      * corresponding DRC is thus assumed to be attachable.
103*ea042c53SGreg Kurz      */
104*ea042c53SGreg Kurz     spapr_drc_attach(drc, dev, &error_abort);
105ee3a71e3SShivaprasad G Bhat 
106ee3a71e3SShivaprasad G Bhat     if (hotplugged) {
107ee3a71e3SShivaprasad G Bhat         spapr_hotplug_req_add_by_index(drc);
108ee3a71e3SShivaprasad G Bhat     }
109ee3a71e3SShivaprasad G Bhat }
110ee3a71e3SShivaprasad G Bhat 
111f1aa45ffSDaniel Henrique Barboza static int spapr_dt_nvdimm(SpaprMachineState *spapr, void *fdt,
112f1aa45ffSDaniel Henrique Barboza                            int parent_offset, NVDIMMDevice *nvdimm)
113ee3a71e3SShivaprasad G Bhat {
114ee3a71e3SShivaprasad G Bhat     int child_offset;
115ee3a71e3SShivaprasad G Bhat     char *buf;
116ee3a71e3SShivaprasad G Bhat     SpaprDrc *drc;
117ee3a71e3SShivaprasad G Bhat     uint32_t drc_idx;
118ee3a71e3SShivaprasad G Bhat     uint32_t node = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_NODE_PROP,
119ee3a71e3SShivaprasad G Bhat                                              &error_abort);
120ee3a71e3SShivaprasad G Bhat     uint64_t slot = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_SLOT_PROP,
121ee3a71e3SShivaprasad G Bhat                                              &error_abort);
122ee3a71e3SShivaprasad G Bhat     uint64_t lsize = nvdimm->label_size;
123ee3a71e3SShivaprasad G Bhat     uint64_t size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
124ee3a71e3SShivaprasad G Bhat                                             NULL);
125ee3a71e3SShivaprasad G Bhat 
126ee3a71e3SShivaprasad G Bhat     drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot);
127ee3a71e3SShivaprasad G Bhat     g_assert(drc);
128ee3a71e3SShivaprasad G Bhat 
129ee3a71e3SShivaprasad G Bhat     drc_idx = spapr_drc_index(drc);
130ee3a71e3SShivaprasad G Bhat 
131ee3a71e3SShivaprasad G Bhat     buf = g_strdup_printf("ibm,pmemory@%x", drc_idx);
132ee3a71e3SShivaprasad G Bhat     child_offset = fdt_add_subnode(fdt, parent_offset, buf);
133ee3a71e3SShivaprasad G Bhat     g_free(buf);
134ee3a71e3SShivaprasad G Bhat 
135ee3a71e3SShivaprasad G Bhat     _FDT(child_offset);
136ee3a71e3SShivaprasad G Bhat 
137ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "reg", drc_idx)));
138ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "compatible", "ibm,pmemory")));
139ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "device_type", "ibm,pmemory")));
140ee3a71e3SShivaprasad G Bhat 
141f1aa45ffSDaniel Henrique Barboza     spapr_numa_write_associativity_dt(spapr, fdt, child_offset, node);
142ee3a71e3SShivaprasad G Bhat 
143ee3a71e3SShivaprasad G Bhat     buf = qemu_uuid_unparse_strdup(&nvdimm->uuid);
144ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "ibm,unit-guid", buf)));
145ee3a71e3SShivaprasad G Bhat     g_free(buf);
146ee3a71e3SShivaprasad G Bhat 
147ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,my-drc-index", drc_idx)));
148ee3a71e3SShivaprasad G Bhat 
149ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,block-size",
150ee3a71e3SShivaprasad G Bhat                           SPAPR_MINIMUM_SCM_BLOCK_SIZE)));
151ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,number-of-blocks",
152ee3a71e3SShivaprasad G Bhat                           size / SPAPR_MINIMUM_SCM_BLOCK_SIZE)));
153ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,metadata-size", lsize)));
154ee3a71e3SShivaprasad G Bhat 
155ee3a71e3SShivaprasad G Bhat     _FDT((fdt_setprop_string(fdt, child_offset, "ibm,pmem-application",
156ee3a71e3SShivaprasad G Bhat                              "operating-system")));
157ee3a71e3SShivaprasad G Bhat     _FDT(fdt_setprop(fdt, child_offset, "ibm,cache-flush-required", NULL, 0));
158ee3a71e3SShivaprasad G Bhat 
159ee3a71e3SShivaprasad G Bhat     return child_offset;
160ee3a71e3SShivaprasad G Bhat }
161ee3a71e3SShivaprasad G Bhat 
1626ee1d62eSDaniel Henrique Barboza int spapr_pmem_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
1636ee1d62eSDaniel Henrique Barboza                            void *fdt, int *fdt_start_offset, Error **errp)
1646ee1d62eSDaniel Henrique Barboza {
1656ee1d62eSDaniel Henrique Barboza     NVDIMMDevice *nvdimm = NVDIMM(drc->dev);
1666ee1d62eSDaniel Henrique Barboza 
167f1aa45ffSDaniel Henrique Barboza     *fdt_start_offset = spapr_dt_nvdimm(spapr, fdt, 0, nvdimm);
1686ee1d62eSDaniel Henrique Barboza 
1696ee1d62eSDaniel Henrique Barboza     return 0;
1706ee1d62eSDaniel Henrique Barboza }
1716ee1d62eSDaniel Henrique Barboza 
172f1aa45ffSDaniel Henrique Barboza void spapr_dt_persistent_memory(SpaprMachineState *spapr, void *fdt)
173ee3a71e3SShivaprasad G Bhat {
174ee3a71e3SShivaprasad G Bhat     int offset = fdt_subnode_offset(fdt, 0, "persistent-memory");
175ee3a71e3SShivaprasad G Bhat     GSList *iter, *nvdimms = nvdimm_get_device_list();
176ee3a71e3SShivaprasad G Bhat 
177ee3a71e3SShivaprasad G Bhat     if (offset < 0) {
178ee3a71e3SShivaprasad G Bhat         offset = fdt_add_subnode(fdt, 0, "persistent-memory");
179ee3a71e3SShivaprasad G Bhat         _FDT(offset);
180ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1)));
181ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0)));
182ee3a71e3SShivaprasad G Bhat         _FDT((fdt_setprop_string(fdt, offset, "device_type",
183ee3a71e3SShivaprasad G Bhat                                  "ibm,persistent-memory")));
184ee3a71e3SShivaprasad G Bhat     }
185ee3a71e3SShivaprasad G Bhat 
186ee3a71e3SShivaprasad G Bhat     /* Create DT entries for cold plugged NVDIMM devices */
187ee3a71e3SShivaprasad G Bhat     for (iter = nvdimms; iter; iter = iter->next) {
188ee3a71e3SShivaprasad G Bhat         NVDIMMDevice *nvdimm = iter->data;
189ee3a71e3SShivaprasad G Bhat 
190f1aa45ffSDaniel Henrique Barboza         spapr_dt_nvdimm(spapr, fdt, offset, nvdimm);
191ee3a71e3SShivaprasad G Bhat     }
192ee3a71e3SShivaprasad G Bhat     g_slist_free(nvdimms);
193ee3a71e3SShivaprasad G Bhat 
194ee3a71e3SShivaprasad G Bhat     return;
195ee3a71e3SShivaprasad G Bhat }
196b5fca656SShivaprasad G Bhat 
197b5fca656SShivaprasad G Bhat static target_ulong h_scm_read_metadata(PowerPCCPU *cpu,
198b5fca656SShivaprasad G Bhat                                         SpaprMachineState *spapr,
199b5fca656SShivaprasad G Bhat                                         target_ulong opcode,
200b5fca656SShivaprasad G Bhat                                         target_ulong *args)
201b5fca656SShivaprasad G Bhat {
202b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
203b5fca656SShivaprasad G Bhat     uint64_t offset = args[1];
204b5fca656SShivaprasad G Bhat     uint64_t len = args[2];
205b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
206b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
207b5fca656SShivaprasad G Bhat     NVDIMMClass *ddc;
208b5fca656SShivaprasad G Bhat     uint64_t data = 0;
209b5fca656SShivaprasad G Bhat     uint8_t buf[8] = { 0 };
210b5fca656SShivaprasad G Bhat 
211b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
212b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
213b5fca656SShivaprasad G Bhat         return H_PARAMETER;
214b5fca656SShivaprasad G Bhat     }
215b5fca656SShivaprasad G Bhat 
216b5fca656SShivaprasad G Bhat     if (len != 1 && len != 2 &&
217b5fca656SShivaprasad G Bhat         len != 4 && len != 8) {
218b5fca656SShivaprasad G Bhat         return H_P3;
219b5fca656SShivaprasad G Bhat     }
220b5fca656SShivaprasad G Bhat 
221b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
222b5fca656SShivaprasad G Bhat     if ((offset + len < offset) ||
223b5fca656SShivaprasad G Bhat         (nvdimm->label_size < len + offset)) {
224b5fca656SShivaprasad G Bhat         return H_P2;
225b5fca656SShivaprasad G Bhat     }
226b5fca656SShivaprasad G Bhat 
227b5fca656SShivaprasad G Bhat     ddc = NVDIMM_GET_CLASS(nvdimm);
228b5fca656SShivaprasad G Bhat     ddc->read_label_data(nvdimm, buf, len, offset);
229b5fca656SShivaprasad G Bhat 
230b5fca656SShivaprasad G Bhat     switch (len) {
231b5fca656SShivaprasad G Bhat     case 1:
232b5fca656SShivaprasad G Bhat         data = ldub_p(buf);
233b5fca656SShivaprasad G Bhat         break;
234b5fca656SShivaprasad G Bhat     case 2:
235b5fca656SShivaprasad G Bhat         data = lduw_be_p(buf);
236b5fca656SShivaprasad G Bhat         break;
237b5fca656SShivaprasad G Bhat     case 4:
238b5fca656SShivaprasad G Bhat         data = ldl_be_p(buf);
239b5fca656SShivaprasad G Bhat         break;
240b5fca656SShivaprasad G Bhat     case 8:
241b5fca656SShivaprasad G Bhat         data = ldq_be_p(buf);
242b5fca656SShivaprasad G Bhat         break;
243b5fca656SShivaprasad G Bhat     default:
244b5fca656SShivaprasad G Bhat         g_assert_not_reached();
245b5fca656SShivaprasad G Bhat     }
246b5fca656SShivaprasad G Bhat 
247b5fca656SShivaprasad G Bhat     args[0] = data;
248b5fca656SShivaprasad G Bhat 
249b5fca656SShivaprasad G Bhat     return H_SUCCESS;
250b5fca656SShivaprasad G Bhat }
251b5fca656SShivaprasad G Bhat 
252b5fca656SShivaprasad G Bhat static target_ulong h_scm_write_metadata(PowerPCCPU *cpu,
253b5fca656SShivaprasad G Bhat                                          SpaprMachineState *spapr,
254b5fca656SShivaprasad G Bhat                                          target_ulong opcode,
255b5fca656SShivaprasad G Bhat                                          target_ulong *args)
256b5fca656SShivaprasad G Bhat {
257b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
258b5fca656SShivaprasad G Bhat     uint64_t offset = args[1];
259b5fca656SShivaprasad G Bhat     uint64_t data = args[2];
260b5fca656SShivaprasad G Bhat     uint64_t len = args[3];
261b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
262b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
263b5fca656SShivaprasad G Bhat     NVDIMMClass *ddc;
264b5fca656SShivaprasad G Bhat     uint8_t buf[8] = { 0 };
265b5fca656SShivaprasad G Bhat 
266b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
267b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
268b5fca656SShivaprasad G Bhat         return H_PARAMETER;
269b5fca656SShivaprasad G Bhat     }
270b5fca656SShivaprasad G Bhat 
271b5fca656SShivaprasad G Bhat     if (len != 1 && len != 2 &&
272b5fca656SShivaprasad G Bhat         len != 4 && len != 8) {
273b5fca656SShivaprasad G Bhat         return H_P4;
274b5fca656SShivaprasad G Bhat     }
275b5fca656SShivaprasad G Bhat 
276b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
277b5fca656SShivaprasad G Bhat     if ((offset + len < offset) ||
278b5fca656SShivaprasad G Bhat         (nvdimm->label_size < len + offset)) {
279b5fca656SShivaprasad G Bhat         return H_P2;
280b5fca656SShivaprasad G Bhat     }
281b5fca656SShivaprasad G Bhat 
282b5fca656SShivaprasad G Bhat     switch (len) {
283b5fca656SShivaprasad G Bhat     case 1:
284b5fca656SShivaprasad G Bhat         if (data & 0xffffffffffffff00) {
285b5fca656SShivaprasad G Bhat             return H_P2;
286b5fca656SShivaprasad G Bhat         }
287b5fca656SShivaprasad G Bhat         stb_p(buf, data);
288b5fca656SShivaprasad G Bhat         break;
289b5fca656SShivaprasad G Bhat     case 2:
290b5fca656SShivaprasad G Bhat         if (data & 0xffffffffffff0000) {
291b5fca656SShivaprasad G Bhat             return H_P2;
292b5fca656SShivaprasad G Bhat         }
293b5fca656SShivaprasad G Bhat         stw_be_p(buf, data);
294b5fca656SShivaprasad G Bhat         break;
295b5fca656SShivaprasad G Bhat     case 4:
296b5fca656SShivaprasad G Bhat         if (data & 0xffffffff00000000) {
297b5fca656SShivaprasad G Bhat             return H_P2;
298b5fca656SShivaprasad G Bhat         }
299b5fca656SShivaprasad G Bhat         stl_be_p(buf, data);
300b5fca656SShivaprasad G Bhat         break;
301b5fca656SShivaprasad G Bhat     case 8:
302b5fca656SShivaprasad G Bhat         stq_be_p(buf, data);
303b5fca656SShivaprasad G Bhat         break;
304b5fca656SShivaprasad G Bhat     default:
305b5fca656SShivaprasad G Bhat             g_assert_not_reached();
306b5fca656SShivaprasad G Bhat     }
307b5fca656SShivaprasad G Bhat 
308b5fca656SShivaprasad G Bhat     ddc = NVDIMM_GET_CLASS(nvdimm);
309b5fca656SShivaprasad G Bhat     ddc->write_label_data(nvdimm, buf, len, offset);
310b5fca656SShivaprasad G Bhat 
311b5fca656SShivaprasad G Bhat     return H_SUCCESS;
312b5fca656SShivaprasad G Bhat }
313b5fca656SShivaprasad G Bhat 
314b5fca656SShivaprasad G Bhat static target_ulong h_scm_bind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr,
315b5fca656SShivaprasad G Bhat                                    target_ulong opcode, target_ulong *args)
316b5fca656SShivaprasad G Bhat {
317b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
318b5fca656SShivaprasad G Bhat     uint64_t starting_idx = args[1];
319b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_to_bind = args[2];
320b5fca656SShivaprasad G Bhat     uint64_t target_logical_mem_addr = args[3];
321b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[4];
322b5fca656SShivaprasad G Bhat     uint64_t size;
323b5fca656SShivaprasad G Bhat     uint64_t total_no_of_scm_blocks;
324b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
325b5fca656SShivaprasad G Bhat     hwaddr addr;
326b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
327b5fca656SShivaprasad G Bhat 
328b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
329b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
330b5fca656SShivaprasad G Bhat         return H_PARAMETER;
331b5fca656SShivaprasad G Bhat     }
332b5fca656SShivaprasad G Bhat 
333b5fca656SShivaprasad G Bhat     /*
334b5fca656SShivaprasad G Bhat      * Currently continue token should be zero qemu has already bound
335b5fca656SShivaprasad G Bhat      * everything and this hcall doesnt return H_BUSY.
336b5fca656SShivaprasad G Bhat      */
337b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
338b5fca656SShivaprasad G Bhat         return H_P5;
339b5fca656SShivaprasad G Bhat     }
340b5fca656SShivaprasad G Bhat 
341b5fca656SShivaprasad G Bhat     /* Currently qemu assigns the address. */
342b5fca656SShivaprasad G Bhat     if (target_logical_mem_addr != 0xffffffffffffffff) {
343b5fca656SShivaprasad G Bhat         return H_OVERLAP;
344b5fca656SShivaprasad G Bhat     }
345b5fca656SShivaprasad G Bhat 
346b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
347b5fca656SShivaprasad G Bhat 
348b5fca656SShivaprasad G Bhat     size = object_property_get_uint(OBJECT(nvdimm),
349b5fca656SShivaprasad G Bhat                                     PC_DIMM_SIZE_PROP, &error_abort);
350b5fca656SShivaprasad G Bhat 
351b5fca656SShivaprasad G Bhat     total_no_of_scm_blocks = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
352b5fca656SShivaprasad G Bhat 
353b5fca656SShivaprasad G Bhat     if (starting_idx > total_no_of_scm_blocks) {
354b5fca656SShivaprasad G Bhat         return H_P2;
355b5fca656SShivaprasad G Bhat     }
356b5fca656SShivaprasad G Bhat 
357b5fca656SShivaprasad G Bhat     if (((starting_idx + no_of_scm_blocks_to_bind) < starting_idx) ||
358b5fca656SShivaprasad G Bhat         ((starting_idx + no_of_scm_blocks_to_bind) > total_no_of_scm_blocks)) {
359b5fca656SShivaprasad G Bhat         return H_P3;
360b5fca656SShivaprasad G Bhat     }
361b5fca656SShivaprasad G Bhat 
362b5fca656SShivaprasad G Bhat     addr = object_property_get_uint(OBJECT(nvdimm),
363b5fca656SShivaprasad G Bhat                                     PC_DIMM_ADDR_PROP, &error_abort);
364b5fca656SShivaprasad G Bhat 
365b5fca656SShivaprasad G Bhat     addr += starting_idx * SPAPR_MINIMUM_SCM_BLOCK_SIZE;
366b5fca656SShivaprasad G Bhat 
367b5fca656SShivaprasad G Bhat     /* Already bound, Return target logical address in R5 */
368b5fca656SShivaprasad G Bhat     args[1] = addr;
369b5fca656SShivaprasad G Bhat     args[2] = no_of_scm_blocks_to_bind;
370b5fca656SShivaprasad G Bhat 
371b5fca656SShivaprasad G Bhat     return H_SUCCESS;
372b5fca656SShivaprasad G Bhat }
373b5fca656SShivaprasad G Bhat 
374b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr,
375b5fca656SShivaprasad G Bhat                                      target_ulong opcode, target_ulong *args)
376b5fca656SShivaprasad G Bhat {
377b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[0];
378b5fca656SShivaprasad G Bhat     uint64_t starting_scm_logical_addr = args[1];
379b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_to_unbind = args[2];
380b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[3];
381b5fca656SShivaprasad G Bhat     uint64_t size_to_unbind;
382b5fca656SShivaprasad G Bhat     Range blockrange = range_empty;
383b5fca656SShivaprasad G Bhat     Range nvdimmrange = range_empty;
384b5fca656SShivaprasad G Bhat     SpaprDrc *drc = spapr_drc_by_index(drc_index);
385b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
386b5fca656SShivaprasad G Bhat     uint64_t size, addr;
387b5fca656SShivaprasad G Bhat 
388b5fca656SShivaprasad G Bhat     if (!drc || !drc->dev ||
389b5fca656SShivaprasad G Bhat         spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
390b5fca656SShivaprasad G Bhat         return H_PARAMETER;
391b5fca656SShivaprasad G Bhat     }
392b5fca656SShivaprasad G Bhat 
393b5fca656SShivaprasad G Bhat     /* continue_token should be zero as this hcall doesn't return H_BUSY. */
394b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
395b5fca656SShivaprasad G Bhat         return H_P4;
396b5fca656SShivaprasad G Bhat     }
397b5fca656SShivaprasad G Bhat 
398b5fca656SShivaprasad G Bhat     /* Check if starting_scm_logical_addr is block aligned */
399b5fca656SShivaprasad G Bhat     if (!QEMU_IS_ALIGNED(starting_scm_logical_addr,
400b5fca656SShivaprasad G Bhat                          SPAPR_MINIMUM_SCM_BLOCK_SIZE)) {
401b5fca656SShivaprasad G Bhat         return H_P2;
402b5fca656SShivaprasad G Bhat     }
403b5fca656SShivaprasad G Bhat 
404b5fca656SShivaprasad G Bhat     size_to_unbind = no_of_scm_blocks_to_unbind * SPAPR_MINIMUM_SCM_BLOCK_SIZE;
405b5fca656SShivaprasad G Bhat     if (no_of_scm_blocks_to_unbind == 0 || no_of_scm_blocks_to_unbind !=
406b5fca656SShivaprasad G Bhat                                size_to_unbind / SPAPR_MINIMUM_SCM_BLOCK_SIZE) {
407b5fca656SShivaprasad G Bhat         return H_P3;
408b5fca656SShivaprasad G Bhat     }
409b5fca656SShivaprasad G Bhat 
410b5fca656SShivaprasad G Bhat     nvdimm = NVDIMM(drc->dev);
411b5fca656SShivaprasad G Bhat     size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
412b5fca656SShivaprasad G Bhat                                    &error_abort);
413b5fca656SShivaprasad G Bhat     addr = object_property_get_int(OBJECT(nvdimm), PC_DIMM_ADDR_PROP,
414b5fca656SShivaprasad G Bhat                                    &error_abort);
415b5fca656SShivaprasad G Bhat 
416b5fca656SShivaprasad G Bhat     range_init_nofail(&nvdimmrange, addr, size);
417b5fca656SShivaprasad G Bhat     range_init_nofail(&blockrange, starting_scm_logical_addr, size_to_unbind);
418b5fca656SShivaprasad G Bhat 
419b5fca656SShivaprasad G Bhat     if (!range_contains_range(&nvdimmrange, &blockrange)) {
420b5fca656SShivaprasad G Bhat         return H_P3;
421b5fca656SShivaprasad G Bhat     }
422b5fca656SShivaprasad G Bhat 
423b5fca656SShivaprasad G Bhat     args[1] = no_of_scm_blocks_to_unbind;
424b5fca656SShivaprasad G Bhat 
425b5fca656SShivaprasad G Bhat     /* let unplug take care of actual unbind */
426b5fca656SShivaprasad G Bhat     return H_SUCCESS;
427b5fca656SShivaprasad G Bhat }
428b5fca656SShivaprasad G Bhat 
429b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_ALL 0x1
430b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_DRC 0x2
431b5fca656SShivaprasad G Bhat 
432b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_all(PowerPCCPU *cpu, SpaprMachineState *spapr,
433b5fca656SShivaprasad G Bhat                                      target_ulong opcode, target_ulong *args)
434b5fca656SShivaprasad G Bhat {
435b5fca656SShivaprasad G Bhat     uint64_t target_scope = args[0];
436b5fca656SShivaprasad G Bhat     uint32_t drc_index = args[1];
437b5fca656SShivaprasad G Bhat     uint64_t continue_token = args[2];
438b5fca656SShivaprasad G Bhat     NVDIMMDevice *nvdimm;
439b5fca656SShivaprasad G Bhat     uint64_t size;
440b5fca656SShivaprasad G Bhat     uint64_t no_of_scm_blocks_unbound = 0;
441b5fca656SShivaprasad G Bhat 
442b5fca656SShivaprasad G Bhat     /* continue_token should be zero as this hcall doesn't return H_BUSY. */
443b5fca656SShivaprasad G Bhat     if (continue_token > 0) {
444b5fca656SShivaprasad G Bhat         return H_P4;
445b5fca656SShivaprasad G Bhat     }
446b5fca656SShivaprasad G Bhat 
447b5fca656SShivaprasad G Bhat     if (target_scope == H_UNBIND_SCOPE_DRC) {
448b5fca656SShivaprasad G Bhat         SpaprDrc *drc = spapr_drc_by_index(drc_index);
449b5fca656SShivaprasad G Bhat 
450b5fca656SShivaprasad G Bhat         if (!drc || !drc->dev ||
451b5fca656SShivaprasad G Bhat             spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) {
452b5fca656SShivaprasad G Bhat             return H_P2;
453b5fca656SShivaprasad G Bhat         }
454b5fca656SShivaprasad G Bhat 
455b5fca656SShivaprasad G Bhat         nvdimm = NVDIMM(drc->dev);
456b5fca656SShivaprasad G Bhat         size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP,
457b5fca656SShivaprasad G Bhat                                        &error_abort);
458b5fca656SShivaprasad G Bhat 
459b5fca656SShivaprasad G Bhat         no_of_scm_blocks_unbound = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE;
460b5fca656SShivaprasad G Bhat     } else if (target_scope ==  H_UNBIND_SCOPE_ALL) {
461b5fca656SShivaprasad G Bhat         GSList *list, *nvdimms;
462b5fca656SShivaprasad G Bhat 
463b5fca656SShivaprasad G Bhat         nvdimms = nvdimm_get_device_list();
464b5fca656SShivaprasad G Bhat         for (list = nvdimms; list; list = list->next) {
465b5fca656SShivaprasad G Bhat             nvdimm = list->data;
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         }
471b5fca656SShivaprasad G Bhat         g_slist_free(nvdimms);
472b5fca656SShivaprasad G Bhat     } else {
473b5fca656SShivaprasad G Bhat         return H_PARAMETER;
474b5fca656SShivaprasad G Bhat     }
475b5fca656SShivaprasad G Bhat 
476b5fca656SShivaprasad G Bhat     args[1] = no_of_scm_blocks_unbound;
477b5fca656SShivaprasad G Bhat 
478b5fca656SShivaprasad G Bhat     /* let unplug take care of actual unbind */
479b5fca656SShivaprasad G Bhat     return H_SUCCESS;
480b5fca656SShivaprasad G Bhat }
481b5fca656SShivaprasad G Bhat 
482b5fca656SShivaprasad G Bhat static void spapr_scm_register_types(void)
483b5fca656SShivaprasad G Bhat {
484b5fca656SShivaprasad G Bhat     /* qemu/scm specific hcalls */
485b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_READ_METADATA, h_scm_read_metadata);
486b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_WRITE_METADATA, h_scm_write_metadata);
487b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_BIND_MEM, h_scm_bind_mem);
488b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_UNBIND_MEM, h_scm_unbind_mem);
489b5fca656SShivaprasad G Bhat     spapr_register_hypercall(H_SCM_UNBIND_ALL, h_scm_unbind_all);
490b5fca656SShivaprasad G Bhat }
491b5fca656SShivaprasad G Bhat 
492b5fca656SShivaprasad G Bhat type_init(spapr_scm_register_types)
493