]> Git Repo - qemu.git/blob - hw/acpi/nvdimm.c
ahci-test: Create smaller test ISO images
[qemu.git] / hw / acpi / nvdimm.c
1 /*
2  * NVDIMM ACPI Implementation
3  *
4  * Copyright(C) 2015 Intel Corporation.
5  *
6  * Author:
7  *  Xiao Guangrong <[email protected]>
8  *
9  * NFIT is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
10  * and the DSM specification can be found at:
11  *       http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
12  *
13  * Currently, it only supports PMEM Virtualization.
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, see <http://www.gnu.org/licenses/>
27  */
28
29 #include "qemu/osdep.h"
30 #include "hw/acpi/acpi.h"
31 #include "hw/acpi/aml-build.h"
32 #include "hw/acpi/bios-linker-loader.h"
33 #include "hw/nvram/fw_cfg.h"
34 #include "hw/mem/nvdimm.h"
35
36 static int nvdimm_plugged_device_list(Object *obj, void *opaque)
37 {
38     GSList **list = opaque;
39
40     if (object_dynamic_cast(obj, TYPE_NVDIMM)) {
41         DeviceState *dev = DEVICE(obj);
42
43         if (dev->realized) { /* only realized NVDIMMs matter */
44             *list = g_slist_append(*list, DEVICE(obj));
45         }
46     }
47
48     object_child_foreach(obj, nvdimm_plugged_device_list, opaque);
49     return 0;
50 }
51
52 /*
53  * inquire plugged NVDIMM devices and link them into the list which is
54  * returned to the caller.
55  *
56  * Note: it is the caller's responsibility to free the list to avoid
57  * memory leak.
58  */
59 static GSList *nvdimm_get_plugged_device_list(void)
60 {
61     GSList *list = NULL;
62
63     object_child_foreach(qdev_get_machine(), nvdimm_plugged_device_list,
64                          &list);
65     return list;
66 }
67
68 #define NVDIMM_UUID_LE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7)             \
69    { (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
70      (b) & 0xff, ((b) >> 8) & 0xff, (c) & 0xff, ((c) >> 8) & 0xff,          \
71      (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }
72
73 /*
74  * define Byte Addressable Persistent Memory (PM) Region according to
75  * ACPI 6.0: 5.2.25.1 System Physical Address Range Structure.
76  */
77 static const uint8_t nvdimm_nfit_spa_uuid[] =
78       NVDIMM_UUID_LE(0x66f0d379, 0xb4f3, 0x4074, 0xac, 0x43, 0x0d, 0x33,
79                      0x18, 0xb7, 0x8c, 0xdb);
80
81 /*
82  * NVDIMM Firmware Interface Table
83  * @signature: "NFIT"
84  *
85  * It provides information that allows OSPM to enumerate NVDIMM present in
86  * the platform and associate system physical address ranges created by the
87  * NVDIMMs.
88  *
89  * It is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
90  */
91 struct NvdimmNfitHeader {
92     ACPI_TABLE_HEADER_DEF
93     uint32_t reserved;
94 } QEMU_PACKED;
95 typedef struct NvdimmNfitHeader NvdimmNfitHeader;
96
97 /*
98  * define NFIT structures according to ACPI 6.0: 5.2.25 NVDIMM Firmware
99  * Interface Table (NFIT).
100  */
101
102 /*
103  * System Physical Address Range Structure
104  *
105  * It describes the system physical address ranges occupied by NVDIMMs and
106  * the types of the regions.
107  */
108 struct NvdimmNfitSpa {
109     uint16_t type;
110     uint16_t length;
111     uint16_t spa_index;
112     uint16_t flags;
113     uint32_t reserved;
114     uint32_t proximity_domain;
115     uint8_t type_guid[16];
116     uint64_t spa_base;
117     uint64_t spa_length;
118     uint64_t mem_attr;
119 } QEMU_PACKED;
120 typedef struct NvdimmNfitSpa NvdimmNfitSpa;
121
122 /*
123  * Memory Device to System Physical Address Range Mapping Structure
124  *
125  * It enables identifying each NVDIMM region and the corresponding SPA
126  * describing the memory interleave
127  */
128 struct NvdimmNfitMemDev {
129     uint16_t type;
130     uint16_t length;
131     uint32_t nfit_handle;
132     uint16_t phys_id;
133     uint16_t region_id;
134     uint16_t spa_index;
135     uint16_t dcr_index;
136     uint64_t region_len;
137     uint64_t region_offset;
138     uint64_t region_dpa;
139     uint16_t interleave_index;
140     uint16_t interleave_ways;
141     uint16_t flags;
142     uint16_t reserved;
143 } QEMU_PACKED;
144 typedef struct NvdimmNfitMemDev NvdimmNfitMemDev;
145
146 /*
147  * NVDIMM Control Region Structure
148  *
149  * It describes the NVDIMM and if applicable, Block Control Window.
150  */
151 struct NvdimmNfitControlRegion {
152     uint16_t type;
153     uint16_t length;
154     uint16_t dcr_index;
155     uint16_t vendor_id;
156     uint16_t device_id;
157     uint16_t revision_id;
158     uint16_t sub_vendor_id;
159     uint16_t sub_device_id;
160     uint16_t sub_revision_id;
161     uint8_t reserved[6];
162     uint32_t serial_number;
163     uint16_t fic;
164     uint16_t num_bcw;
165     uint64_t bcw_size;
166     uint64_t cmd_offset;
167     uint64_t cmd_size;
168     uint64_t status_offset;
169     uint64_t status_size;
170     uint16_t flags;
171     uint8_t reserved2[6];
172 } QEMU_PACKED;
173 typedef struct NvdimmNfitControlRegion NvdimmNfitControlRegion;
174
175 /*
176  * Module serial number is a unique number for each device. We use the
177  * slot id of NVDIMM device to generate this number so that each device
178  * associates with a different number.
179  *
180  * 0x123456 is a magic number we arbitrarily chose.
181  */
182 static uint32_t nvdimm_slot_to_sn(int slot)
183 {
184     return 0x123456 + slot;
185 }
186
187 /*
188  * handle is used to uniquely associate nfit_memdev structure with NVDIMM
189  * ACPI device - nfit_memdev.nfit_handle matches with the value returned
190  * by ACPI device _ADR method.
191  *
192  * We generate the handle with the slot id of NVDIMM device and reserve
193  * 0 for NVDIMM root device.
194  */
195 static uint32_t nvdimm_slot_to_handle(int slot)
196 {
197     return slot + 1;
198 }
199
200 /*
201  * index uniquely identifies the structure, 0 is reserved which indicates
202  * that the structure is not valid or the associated structure is not
203  * present.
204  *
205  * Each NVDIMM device needs two indexes, one for nfit_spa and another for
206  * nfit_dc which are generated by the slot id of NVDIMM device.
207  */
208 static uint16_t nvdimm_slot_to_spa_index(int slot)
209 {
210     return (slot + 1) << 1;
211 }
212
213 /* See the comments of nvdimm_slot_to_spa_index(). */
214 static uint32_t nvdimm_slot_to_dcr_index(int slot)
215 {
216     return nvdimm_slot_to_spa_index(slot) + 1;
217 }
218
219 static NVDIMMDevice *nvdimm_get_device_by_handle(uint32_t handle)
220 {
221     NVDIMMDevice *nvdimm = NULL;
222     GSList *list, *device_list = nvdimm_get_plugged_device_list();
223
224     for (list = device_list; list; list = list->next) {
225         NVDIMMDevice *nvd = list->data;
226         int slot = object_property_get_int(OBJECT(nvd), PC_DIMM_SLOT_PROP,
227                                            NULL);
228
229         if (nvdimm_slot_to_handle(slot) == handle) {
230             nvdimm = nvd;
231             break;
232         }
233     }
234
235     g_slist_free(device_list);
236     return nvdimm;
237 }
238
239 /* ACPI 6.0: 5.2.25.1 System Physical Address Range Structure */
240 static void
241 nvdimm_build_structure_spa(GArray *structures, DeviceState *dev)
242 {
243     NvdimmNfitSpa *nfit_spa;
244     uint64_t addr = object_property_get_int(OBJECT(dev), PC_DIMM_ADDR_PROP,
245                                             NULL);
246     uint64_t size = object_property_get_int(OBJECT(dev), PC_DIMM_SIZE_PROP,
247                                             NULL);
248     uint32_t node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP,
249                                             NULL);
250     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
251                                             NULL);
252
253     nfit_spa = acpi_data_push(structures, sizeof(*nfit_spa));
254
255     nfit_spa->type = cpu_to_le16(0 /* System Physical Address Range
256                                       Structure */);
257     nfit_spa->length = cpu_to_le16(sizeof(*nfit_spa));
258     nfit_spa->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
259
260     /*
261      * Control region is strict as all the device info, such as SN, index,
262      * is associated with slot id.
263      */
264     nfit_spa->flags = cpu_to_le16(1 /* Control region is strictly for
265                                        management during hot add/online
266                                        operation */ |
267                                   2 /* Data in Proximity Domain field is
268                                        valid*/);
269
270     /* NUMA node. */
271     nfit_spa->proximity_domain = cpu_to_le32(node);
272     /* the region reported as PMEM. */
273     memcpy(nfit_spa->type_guid, nvdimm_nfit_spa_uuid,
274            sizeof(nvdimm_nfit_spa_uuid));
275
276     nfit_spa->spa_base = cpu_to_le64(addr);
277     nfit_spa->spa_length = cpu_to_le64(size);
278
279     /* It is the PMEM and can be cached as writeback. */
280     nfit_spa->mem_attr = cpu_to_le64(0x8ULL /* EFI_MEMORY_WB */ |
281                                      0x8000ULL /* EFI_MEMORY_NV */);
282 }
283
284 /*
285  * ACPI 6.0: 5.2.25.2 Memory Device to System Physical Address Range Mapping
286  * Structure
287  */
288 static void
289 nvdimm_build_structure_memdev(GArray *structures, DeviceState *dev)
290 {
291     NvdimmNfitMemDev *nfit_memdev;
292     uint64_t size = object_property_get_int(OBJECT(dev), PC_DIMM_SIZE_PROP,
293                                             NULL);
294     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
295                                             NULL);
296     uint32_t handle = nvdimm_slot_to_handle(slot);
297
298     nfit_memdev = acpi_data_push(structures, sizeof(*nfit_memdev));
299
300     nfit_memdev->type = cpu_to_le16(1 /* Memory Device to System Address
301                                          Range Map Structure*/);
302     nfit_memdev->length = cpu_to_le16(sizeof(*nfit_memdev));
303     nfit_memdev->nfit_handle = cpu_to_le32(handle);
304
305     /*
306      * associate memory device with System Physical Address Range
307      * Structure.
308      */
309     nfit_memdev->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
310     /* associate memory device with Control Region Structure. */
311     nfit_memdev->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
312
313     /* The memory region on the device. */
314     nfit_memdev->region_len = cpu_to_le64(size);
315     /* The device address starts from 0. */
316     nfit_memdev->region_dpa = cpu_to_le64(0);
317
318     /* Only one interleave for PMEM. */
319     nfit_memdev->interleave_ways = cpu_to_le16(1);
320 }
321
322 /*
323  * ACPI 6.0: 5.2.25.5 NVDIMM Control Region Structure.
324  */
325 static void nvdimm_build_structure_dcr(GArray *structures, DeviceState *dev)
326 {
327     NvdimmNfitControlRegion *nfit_dcr;
328     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
329                                        NULL);
330     uint32_t sn = nvdimm_slot_to_sn(slot);
331
332     nfit_dcr = acpi_data_push(structures, sizeof(*nfit_dcr));
333
334     nfit_dcr->type = cpu_to_le16(4 /* NVDIMM Control Region Structure */);
335     nfit_dcr->length = cpu_to_le16(sizeof(*nfit_dcr));
336     nfit_dcr->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
337
338     /* vendor: Intel. */
339     nfit_dcr->vendor_id = cpu_to_le16(0x8086);
340     nfit_dcr->device_id = cpu_to_le16(1);
341
342     /* The _DSM method is following Intel's DSM specification. */
343     nfit_dcr->revision_id = cpu_to_le16(1 /* Current Revision supported
344                                              in ACPI 6.0 is 1. */);
345     nfit_dcr->serial_number = cpu_to_le32(sn);
346     nfit_dcr->fic = cpu_to_le16(0x201 /* Format Interface Code. See Chapter
347                                          2: NVDIMM Device Specific Method
348                                          (DSM) in DSM Spec Rev1.*/);
349 }
350
351 static GArray *nvdimm_build_device_structure(void)
352 {
353     GSList *device_list = nvdimm_get_plugged_device_list();
354     GArray *structures = g_array_new(false, true /* clear */, 1);
355
356     for (; device_list; device_list = device_list->next) {
357         DeviceState *dev = device_list->data;
358
359         /* build System Physical Address Range Structure. */
360         nvdimm_build_structure_spa(structures, dev);
361
362         /*
363          * build Memory Device to System Physical Address Range Mapping
364          * Structure.
365          */
366         nvdimm_build_structure_memdev(structures, dev);
367
368         /* build NVDIMM Control Region Structure. */
369         nvdimm_build_structure_dcr(structures, dev);
370     }
371     g_slist_free(device_list);
372
373     return structures;
374 }
375
376 static void nvdimm_init_fit_buffer(NvdimmFitBuffer *fit_buf)
377 {
378     qemu_mutex_init(&fit_buf->lock);
379     fit_buf->fit = g_array_new(false, true /* clear */, 1);
380 }
381
382 static void nvdimm_build_fit_buffer(NvdimmFitBuffer *fit_buf)
383 {
384     qemu_mutex_lock(&fit_buf->lock);
385     g_array_free(fit_buf->fit, true);
386     fit_buf->fit = nvdimm_build_device_structure();
387     fit_buf->dirty = true;
388     qemu_mutex_unlock(&fit_buf->lock);
389 }
390
391 void nvdimm_acpi_hotplug(AcpiNVDIMMState *state)
392 {
393     nvdimm_build_fit_buffer(&state->fit_buf);
394 }
395
396 static void nvdimm_build_nfit(AcpiNVDIMMState *state, GArray *table_offsets,
397                               GArray *table_data, BIOSLinker *linker)
398 {
399     NvdimmFitBuffer *fit_buf = &state->fit_buf;
400     unsigned int header;
401
402     qemu_mutex_lock(&fit_buf->lock);
403
404     /* NVDIMM device is not plugged? */
405     if (!fit_buf->fit->len) {
406         goto exit;
407     }
408
409     acpi_add_table(table_offsets, table_data);
410
411     /* NFIT header. */
412     header = table_data->len;
413     acpi_data_push(table_data, sizeof(NvdimmNfitHeader));
414     /* NVDIMM device structures. */
415     g_array_append_vals(table_data, fit_buf->fit->data, fit_buf->fit->len);
416
417     build_header(linker, table_data,
418                  (void *)(table_data->data + header), "NFIT",
419                  sizeof(NvdimmNfitHeader) + fit_buf->fit->len, 1, NULL, NULL);
420
421 exit:
422     qemu_mutex_unlock(&fit_buf->lock);
423 }
424
425 struct NvdimmDsmIn {
426     uint32_t handle;
427     uint32_t revision;
428     uint32_t function;
429     /* the remaining size in the page is used by arg3. */
430     union {
431         uint8_t arg3[4084];
432     };
433 } QEMU_PACKED;
434 typedef struct NvdimmDsmIn NvdimmDsmIn;
435 QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmIn) != 4096);
436
437 struct NvdimmDsmOut {
438     /* the size of buffer filled by QEMU. */
439     uint32_t len;
440     uint8_t data[4092];
441 } QEMU_PACKED;
442 typedef struct NvdimmDsmOut NvdimmDsmOut;
443 QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmOut) != 4096);
444
445 struct NvdimmDsmFunc0Out {
446     /* the size of buffer filled by QEMU. */
447      uint32_t len;
448      uint32_t supported_func;
449 } QEMU_PACKED;
450 typedef struct NvdimmDsmFunc0Out NvdimmDsmFunc0Out;
451
452 struct NvdimmDsmFuncNoPayloadOut {
453     /* the size of buffer filled by QEMU. */
454      uint32_t len;
455      uint32_t func_ret_status;
456 } QEMU_PACKED;
457 typedef struct NvdimmDsmFuncNoPayloadOut NvdimmDsmFuncNoPayloadOut;
458
459 struct NvdimmFuncGetLabelSizeOut {
460     /* the size of buffer filled by QEMU. */
461     uint32_t len;
462     uint32_t func_ret_status; /* return status code. */
463     uint32_t label_size; /* the size of label data area. */
464     /*
465      * Maximum size of the namespace label data length supported by
466      * the platform in Get/Set Namespace Label Data functions.
467      */
468     uint32_t max_xfer;
469 } QEMU_PACKED;
470 typedef struct NvdimmFuncGetLabelSizeOut NvdimmFuncGetLabelSizeOut;
471 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelSizeOut) > 4096);
472
473 struct NvdimmFuncGetLabelDataIn {
474     uint32_t offset; /* the offset in the namespace label data area. */
475     uint32_t length; /* the size of data is to be read via the function. */
476 } QEMU_PACKED;
477 typedef struct NvdimmFuncGetLabelDataIn NvdimmFuncGetLabelDataIn;
478 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataIn) +
479                   offsetof(NvdimmDsmIn, arg3) > 4096);
480
481 struct NvdimmFuncGetLabelDataOut {
482     /* the size of buffer filled by QEMU. */
483     uint32_t len;
484     uint32_t func_ret_status; /* return status code. */
485     uint8_t out_buf[0]; /* the data got via Get Namesapce Label function. */
486 } QEMU_PACKED;
487 typedef struct NvdimmFuncGetLabelDataOut NvdimmFuncGetLabelDataOut;
488 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataOut) > 4096);
489
490 struct NvdimmFuncSetLabelDataIn {
491     uint32_t offset; /* the offset in the namespace label data area. */
492     uint32_t length; /* the size of data is to be written via the function. */
493     uint8_t in_buf[0]; /* the data written to label data area. */
494 } QEMU_PACKED;
495 typedef struct NvdimmFuncSetLabelDataIn NvdimmFuncSetLabelDataIn;
496 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncSetLabelDataIn) +
497                   offsetof(NvdimmDsmIn, arg3) > 4096);
498
499 struct NvdimmFuncReadFITIn {
500     uint32_t offset; /* the offset of FIT buffer. */
501 } QEMU_PACKED;
502 typedef struct NvdimmFuncReadFITIn NvdimmFuncReadFITIn;
503 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITIn) +
504                   offsetof(NvdimmDsmIn, arg3) > 4096);
505
506 struct NvdimmFuncReadFITOut {
507     /* the size of buffer filled by QEMU. */
508     uint32_t len;
509     uint32_t func_ret_status; /* return status code. */
510     uint8_t fit[0]; /* the FIT data. */
511 } QEMU_PACKED;
512 typedef struct NvdimmFuncReadFITOut NvdimmFuncReadFITOut;
513 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITOut) > 4096);
514
515 static void
516 nvdimm_dsm_function0(uint32_t supported_func, hwaddr dsm_mem_addr)
517 {
518     NvdimmDsmFunc0Out func0 = {
519         .len = cpu_to_le32(sizeof(func0)),
520         .supported_func = cpu_to_le32(supported_func),
521     };
522     cpu_physical_memory_write(dsm_mem_addr, &func0, sizeof(func0));
523 }
524
525 static void
526 nvdimm_dsm_no_payload(uint32_t func_ret_status, hwaddr dsm_mem_addr)
527 {
528     NvdimmDsmFuncNoPayloadOut out = {
529         .len = cpu_to_le32(sizeof(out)),
530         .func_ret_status = cpu_to_le32(func_ret_status),
531     };
532     cpu_physical_memory_write(dsm_mem_addr, &out, sizeof(out));
533 }
534
535 #define NVDIMM_QEMU_RSVD_HANDLE_ROOT 0x10000
536
537 /* Read FIT data, defined in docs/specs/acpi_nvdimm.txt. */
538 static void nvdimm_dsm_func_read_fit(AcpiNVDIMMState *state, NvdimmDsmIn *in,
539                                      hwaddr dsm_mem_addr)
540 {
541     NvdimmFitBuffer *fit_buf = &state->fit_buf;
542     NvdimmFuncReadFITIn *read_fit;
543     NvdimmFuncReadFITOut *read_fit_out;
544     GArray *fit;
545     uint32_t read_len = 0, func_ret_status;
546     int size;
547
548     read_fit = (NvdimmFuncReadFITIn *)in->arg3;
549     le32_to_cpus(&read_fit->offset);
550
551     qemu_mutex_lock(&fit_buf->lock);
552     fit = fit_buf->fit;
553
554     nvdimm_debug("Read FIT: offset %#x FIT size %#x Dirty %s.\n",
555                  read_fit->offset, fit->len, fit_buf->dirty ? "Yes" : "No");
556
557     if (read_fit->offset > fit->len) {
558         func_ret_status = 3 /* Invalid Input Parameters */;
559         goto exit;
560     }
561
562     /* It is the first time to read FIT. */
563     if (!read_fit->offset) {
564         fit_buf->dirty = false;
565     } else if (fit_buf->dirty) { /* FIT has been changed during RFIT. */
566         func_ret_status = 0x100 /* fit changed */;
567         goto exit;
568     }
569
570     func_ret_status = 0 /* Success */;
571     read_len = MIN(fit->len - read_fit->offset,
572                    4096 - sizeof(NvdimmFuncReadFITOut));
573
574 exit:
575     size = sizeof(NvdimmFuncReadFITOut) + read_len;
576     read_fit_out = g_malloc(size);
577
578     read_fit_out->len = cpu_to_le32(size);
579     read_fit_out->func_ret_status = cpu_to_le32(func_ret_status);
580     memcpy(read_fit_out->fit, fit->data + read_fit->offset, read_len);
581
582     cpu_physical_memory_write(dsm_mem_addr, read_fit_out, size);
583
584     g_free(read_fit_out);
585     qemu_mutex_unlock(&fit_buf->lock);
586 }
587
588 static void nvdimm_dsm_reserved_root(AcpiNVDIMMState *state, NvdimmDsmIn *in,
589                                      hwaddr dsm_mem_addr)
590 {
591     switch (in->function) {
592     case 0x0:
593         nvdimm_dsm_function0(0x1 | 1 << 1 /* Read FIT */, dsm_mem_addr);
594         return;
595     case 0x1 /*Read FIT */:
596         nvdimm_dsm_func_read_fit(state, in, dsm_mem_addr);
597         return;
598     }
599
600     nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
601 }
602
603 static void nvdimm_dsm_root(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
604 {
605     /*
606      * function 0 is called to inquire which functions are supported by
607      * OSPM
608      */
609     if (!in->function) {
610         nvdimm_dsm_function0(0 /* No function supported other than
611                                   function 0 */, dsm_mem_addr);
612         return;
613     }
614
615     /* No function except function 0 is supported yet. */
616     nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
617 }
618
619 /*
620  * the max transfer size is the max size transferred by both a
621  * 'Get Namespace Label Data' function and a 'Set Namespace Label Data'
622  * function.
623  */
624 static uint32_t nvdimm_get_max_xfer_label_size(void)
625 {
626     uint32_t max_get_size, max_set_size, dsm_memory_size = 4096;
627
628     /*
629      * the max data ACPI can read one time which is transferred by
630      * the response of 'Get Namespace Label Data' function.
631      */
632     max_get_size = dsm_memory_size - sizeof(NvdimmFuncGetLabelDataOut);
633
634     /*
635      * the max data ACPI can write one time which is transferred by
636      * 'Set Namespace Label Data' function.
637      */
638     max_set_size = dsm_memory_size - offsetof(NvdimmDsmIn, arg3) -
639                    sizeof(NvdimmFuncSetLabelDataIn);
640
641     return MIN(max_get_size, max_set_size);
642 }
643
644 /*
645  * DSM Spec Rev1 4.4 Get Namespace Label Size (Function Index 4).
646  *
647  * It gets the size of Namespace Label data area and the max data size
648  * that Get/Set Namespace Label Data functions can transfer.
649  */
650 static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm, hwaddr dsm_mem_addr)
651 {
652     NvdimmFuncGetLabelSizeOut label_size_out = {
653         .len = cpu_to_le32(sizeof(label_size_out)),
654     };
655     uint32_t label_size, mxfer;
656
657     label_size = nvdimm->label_size;
658     mxfer = nvdimm_get_max_xfer_label_size();
659
660     nvdimm_debug("label_size %#x, max_xfer %#x.\n", label_size, mxfer);
661
662     label_size_out.func_ret_status = cpu_to_le32(0 /* Success */);
663     label_size_out.label_size = cpu_to_le32(label_size);
664     label_size_out.max_xfer = cpu_to_le32(mxfer);
665
666     cpu_physical_memory_write(dsm_mem_addr, &label_size_out,
667                               sizeof(label_size_out));
668 }
669
670 static uint32_t nvdimm_rw_label_data_check(NVDIMMDevice *nvdimm,
671                                            uint32_t offset, uint32_t length)
672 {
673     uint32_t ret = 3 /* Invalid Input Parameters */;
674
675     if (offset + length < offset) {
676         nvdimm_debug("offset %#x + length %#x is overflow.\n", offset,
677                      length);
678         return ret;
679     }
680
681     if (nvdimm->label_size < offset + length) {
682         nvdimm_debug("position %#x is beyond label data (len = %" PRIx64 ").\n",
683                      offset + length, nvdimm->label_size);
684         return ret;
685     }
686
687     if (length > nvdimm_get_max_xfer_label_size()) {
688         nvdimm_debug("length (%#x) is larger than max_xfer (%#x).\n",
689                      length, nvdimm_get_max_xfer_label_size());
690         return ret;
691     }
692
693     return 0 /* Success */;
694 }
695
696 /*
697  * DSM Spec Rev1 4.5 Get Namespace Label Data (Function Index 5).
698  */
699 static void nvdimm_dsm_get_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
700                                       hwaddr dsm_mem_addr)
701 {
702     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
703     NvdimmFuncGetLabelDataIn *get_label_data;
704     NvdimmFuncGetLabelDataOut *get_label_data_out;
705     uint32_t status;
706     int size;
707
708     get_label_data = (NvdimmFuncGetLabelDataIn *)in->arg3;
709     le32_to_cpus(&get_label_data->offset);
710     le32_to_cpus(&get_label_data->length);
711
712     nvdimm_debug("Read Label Data: offset %#x length %#x.\n",
713                  get_label_data->offset, get_label_data->length);
714
715     status = nvdimm_rw_label_data_check(nvdimm, get_label_data->offset,
716                                         get_label_data->length);
717     if (status != 0 /* Success */) {
718         nvdimm_dsm_no_payload(status, dsm_mem_addr);
719         return;
720     }
721
722     size = sizeof(*get_label_data_out) + get_label_data->length;
723     assert(size <= 4096);
724     get_label_data_out = g_malloc(size);
725
726     get_label_data_out->len = cpu_to_le32(size);
727     get_label_data_out->func_ret_status = cpu_to_le32(0 /* Success */);
728     nvc->read_label_data(nvdimm, get_label_data_out->out_buf,
729                          get_label_data->length, get_label_data->offset);
730
731     cpu_physical_memory_write(dsm_mem_addr, get_label_data_out, size);
732     g_free(get_label_data_out);
733 }
734
735 /*
736  * DSM Spec Rev1 4.6 Set Namespace Label Data (Function Index 6).
737  */
738 static void nvdimm_dsm_set_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
739                                       hwaddr dsm_mem_addr)
740 {
741     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
742     NvdimmFuncSetLabelDataIn *set_label_data;
743     uint32_t status;
744
745     set_label_data = (NvdimmFuncSetLabelDataIn *)in->arg3;
746
747     le32_to_cpus(&set_label_data->offset);
748     le32_to_cpus(&set_label_data->length);
749
750     nvdimm_debug("Write Label Data: offset %#x length %#x.\n",
751                  set_label_data->offset, set_label_data->length);
752
753     status = nvdimm_rw_label_data_check(nvdimm, set_label_data->offset,
754                                         set_label_data->length);
755     if (status != 0 /* Success */) {
756         nvdimm_dsm_no_payload(status, dsm_mem_addr);
757         return;
758     }
759
760     assert(offsetof(NvdimmDsmIn, arg3) +
761            sizeof(*set_label_data) + set_label_data->length <= 4096);
762
763     nvc->write_label_data(nvdimm, set_label_data->in_buf,
764                           set_label_data->length, set_label_data->offset);
765     nvdimm_dsm_no_payload(0 /* Success */, dsm_mem_addr);
766 }
767
768 static void nvdimm_dsm_device(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
769 {
770     NVDIMMDevice *nvdimm = nvdimm_get_device_by_handle(in->handle);
771
772     /* See the comments in nvdimm_dsm_root(). */
773     if (!in->function) {
774         uint32_t supported_func = 0;
775
776         if (nvdimm && nvdimm->label_size) {
777             supported_func |= 0x1 /* Bit 0 indicates whether there is
778                                      support for any functions other
779                                      than function 0. */ |
780                               1 << 4 /* Get Namespace Label Size */ |
781                               1 << 5 /* Get Namespace Label Data */ |
782                               1 << 6 /* Set Namespace Label Data */;
783         }
784         nvdimm_dsm_function0(supported_func, dsm_mem_addr);
785         return;
786     }
787
788     if (!nvdimm) {
789         nvdimm_dsm_no_payload(2 /* Non-Existing Memory Device */,
790                               dsm_mem_addr);
791         return;
792     }
793
794     /* Encode DSM function according to DSM Spec Rev1. */
795     switch (in->function) {
796     case 4 /* Get Namespace Label Size */:
797         if (nvdimm->label_size) {
798             nvdimm_dsm_label_size(nvdimm, dsm_mem_addr);
799             return;
800         }
801         break;
802     case 5 /* Get Namespace Label Data */:
803         if (nvdimm->label_size) {
804             nvdimm_dsm_get_label_data(nvdimm, in, dsm_mem_addr);
805             return;
806         }
807         break;
808     case 0x6 /* Set Namespace Label Data */:
809         if (nvdimm->label_size) {
810             nvdimm_dsm_set_label_data(nvdimm, in, dsm_mem_addr);
811             return;
812         }
813         break;
814     }
815
816     nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
817 }
818
819 static uint64_t
820 nvdimm_dsm_read(void *opaque, hwaddr addr, unsigned size)
821 {
822     nvdimm_debug("BUG: we never read _DSM IO Port.\n");
823     return 0;
824 }
825
826 static void
827 nvdimm_dsm_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
828 {
829     AcpiNVDIMMState *state = opaque;
830     NvdimmDsmIn *in;
831     hwaddr dsm_mem_addr = val;
832
833     nvdimm_debug("dsm memory address %#" HWADDR_PRIx ".\n", dsm_mem_addr);
834
835     /*
836      * The DSM memory is mapped to guest address space so an evil guest
837      * can change its content while we are doing DSM emulation. Avoid
838      * this by copying DSM memory to QEMU local memory.
839      */
840     in = g_new(NvdimmDsmIn, 1);
841     cpu_physical_memory_read(dsm_mem_addr, in, sizeof(*in));
842
843     le32_to_cpus(&in->revision);
844     le32_to_cpus(&in->function);
845     le32_to_cpus(&in->handle);
846
847     nvdimm_debug("Revision %#x Handler %#x Function %#x.\n", in->revision,
848                  in->handle, in->function);
849
850     if (in->revision != 0x1 /* Currently we only support DSM Spec Rev1. */) {
851         nvdimm_debug("Revision %#x is not supported, expect %#x.\n",
852                      in->revision, 0x1);
853         nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
854         goto exit;
855     }
856
857     if (in->handle == NVDIMM_QEMU_RSVD_HANDLE_ROOT) {
858         nvdimm_dsm_reserved_root(state, in, dsm_mem_addr);
859         goto exit;
860     }
861
862      /* Handle 0 is reserved for NVDIMM Root Device. */
863     if (!in->handle) {
864         nvdimm_dsm_root(in, dsm_mem_addr);
865         goto exit;
866     }
867
868     nvdimm_dsm_device(in, dsm_mem_addr);
869
870 exit:
871     g_free(in);
872 }
873
874 static const MemoryRegionOps nvdimm_dsm_ops = {
875     .read = nvdimm_dsm_read,
876     .write = nvdimm_dsm_write,
877     .endianness = DEVICE_LITTLE_ENDIAN,
878     .valid = {
879         .min_access_size = 4,
880         .max_access_size = 4,
881     },
882 };
883
884 void nvdimm_init_acpi_state(AcpiNVDIMMState *state, MemoryRegion *io,
885                             FWCfgState *fw_cfg, Object *owner)
886 {
887     memory_region_init_io(&state->io_mr, owner, &nvdimm_dsm_ops, state,
888                           "nvdimm-acpi-io", NVDIMM_ACPI_IO_LEN);
889     memory_region_add_subregion(io, NVDIMM_ACPI_IO_BASE, &state->io_mr);
890
891     state->dsm_mem = g_array_new(false, true /* clear */, 1);
892     acpi_data_push(state->dsm_mem, sizeof(NvdimmDsmIn));
893     fw_cfg_add_file(fw_cfg, NVDIMM_DSM_MEM_FILE, state->dsm_mem->data,
894                     state->dsm_mem->len);
895
896     nvdimm_init_fit_buffer(&state->fit_buf);
897 }
898
899 #define NVDIMM_COMMON_DSM       "NCAL"
900 #define NVDIMM_ACPI_MEM_ADDR    "MEMA"
901
902 #define NVDIMM_DSM_MEMORY       "NRAM"
903 #define NVDIMM_DSM_IOPORT       "NPIO"
904
905 #define NVDIMM_DSM_NOTIFY       "NTFI"
906 #define NVDIMM_DSM_HANDLE       "HDLE"
907 #define NVDIMM_DSM_REVISION     "REVS"
908 #define NVDIMM_DSM_FUNCTION     "FUNC"
909 #define NVDIMM_DSM_ARG3         "FARG"
910
911 #define NVDIMM_DSM_OUT_BUF_SIZE "RLEN"
912 #define NVDIMM_DSM_OUT_BUF      "ODAT"
913
914 #define NVDIMM_DSM_RFIT_STATUS  "RSTA"
915
916 #define NVDIMM_QEMU_RSVD_UUID   "648B9CF2-CDA1-4312-8AD9-49C4AF32BD62"
917
918 static void nvdimm_build_common_dsm(Aml *dev)
919 {
920     Aml *method, *ifctx, *function, *handle, *uuid, *dsm_mem, *elsectx2;
921     Aml *elsectx, *unsupport, *unpatched, *expected_uuid, *uuid_invalid;
922     Aml *pckg, *pckg_index, *pckg_buf, *field, *dsm_out_buf, *dsm_out_buf_size;
923     uint8_t byte_list[1];
924
925     method = aml_method(NVDIMM_COMMON_DSM, 5, AML_SERIALIZED);
926     uuid = aml_arg(0);
927     function = aml_arg(2);
928     handle = aml_arg(4);
929     dsm_mem = aml_local(6);
930     dsm_out_buf = aml_local(7);
931
932     aml_append(method, aml_store(aml_name(NVDIMM_ACPI_MEM_ADDR), dsm_mem));
933
934     /* map DSM memory and IO into ACPI namespace. */
935     aml_append(method, aml_operation_region(NVDIMM_DSM_IOPORT, AML_SYSTEM_IO,
936                aml_int(NVDIMM_ACPI_IO_BASE), NVDIMM_ACPI_IO_LEN));
937     aml_append(method, aml_operation_region(NVDIMM_DSM_MEMORY,
938                AML_SYSTEM_MEMORY, dsm_mem, sizeof(NvdimmDsmIn)));
939
940     /*
941      * DSM notifier:
942      * NVDIMM_DSM_NOTIFY: write the address of DSM memory and notify QEMU to
943      *                    emulate the access.
944      *
945      * It is the IO port so that accessing them will cause VM-exit, the
946      * control will be transferred to QEMU.
947      */
948     field = aml_field(NVDIMM_DSM_IOPORT, AML_DWORD_ACC, AML_NOLOCK,
949                       AML_PRESERVE);
950     aml_append(field, aml_named_field(NVDIMM_DSM_NOTIFY,
951                sizeof(uint32_t) * BITS_PER_BYTE));
952     aml_append(method, field);
953
954     /*
955      * DSM input:
956      * NVDIMM_DSM_HANDLE: store device's handle, it's zero if the _DSM call
957      *                    happens on NVDIMM Root Device.
958      * NVDIMM_DSM_REVISION: store the Arg1 of _DSM call.
959      * NVDIMM_DSM_FUNCTION: store the Arg2 of _DSM call.
960      * NVDIMM_DSM_ARG3: store the Arg3 of _DSM call which is a Package
961      *                  containing function-specific arguments.
962      *
963      * They are RAM mapping on host so that these accesses never cause
964      * VM-EXIT.
965      */
966     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
967                       AML_PRESERVE);
968     aml_append(field, aml_named_field(NVDIMM_DSM_HANDLE,
969                sizeof(typeof_field(NvdimmDsmIn, handle)) * BITS_PER_BYTE));
970     aml_append(field, aml_named_field(NVDIMM_DSM_REVISION,
971                sizeof(typeof_field(NvdimmDsmIn, revision)) * BITS_PER_BYTE));
972     aml_append(field, aml_named_field(NVDIMM_DSM_FUNCTION,
973                sizeof(typeof_field(NvdimmDsmIn, function)) * BITS_PER_BYTE));
974     aml_append(field, aml_named_field(NVDIMM_DSM_ARG3,
975          (sizeof(NvdimmDsmIn) - offsetof(NvdimmDsmIn, arg3)) * BITS_PER_BYTE));
976     aml_append(method, field);
977
978     /*
979      * DSM output:
980      * NVDIMM_DSM_OUT_BUF_SIZE: the size of the buffer filled by QEMU.
981      * NVDIMM_DSM_OUT_BUF: the buffer QEMU uses to store the result.
982      *
983      * Since the page is reused by both input and out, the input data
984      * will be lost after storing new result into ODAT so we should fetch
985      * all the input data before writing the result.
986      */
987     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
988                       AML_PRESERVE);
989     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF_SIZE,
990                sizeof(typeof_field(NvdimmDsmOut, len)) * BITS_PER_BYTE));
991     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF,
992        (sizeof(NvdimmDsmOut) - offsetof(NvdimmDsmOut, data)) * BITS_PER_BYTE));
993     aml_append(method, field);
994
995     /*
996      * do not support any method if DSM memory address has not been
997      * patched.
998      */
999     unpatched = aml_equal(dsm_mem, aml_int(0x0));
1000
1001     expected_uuid = aml_local(0);
1002
1003     ifctx = aml_if(aml_equal(handle, aml_int(0x0)));
1004     aml_append(ifctx, aml_store(
1005                aml_touuid("2F10E7A4-9E91-11E4-89D3-123B93F75CBA")
1006                /* UUID for NVDIMM Root Device */, expected_uuid));
1007     aml_append(method, ifctx);
1008     elsectx = aml_else();
1009     ifctx = aml_if(aml_equal(handle, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT)));
1010     aml_append(ifctx, aml_store(aml_touuid(NVDIMM_QEMU_RSVD_UUID
1011                /* UUID for QEMU internal use */), expected_uuid));
1012     aml_append(elsectx, ifctx);
1013     elsectx2 = aml_else();
1014     aml_append(elsectx2, aml_store(
1015                aml_touuid("4309AC30-0D11-11E4-9191-0800200C9A66")
1016                /* UUID for NVDIMM Devices */, expected_uuid));
1017     aml_append(elsectx, elsectx2);
1018     aml_append(method, elsectx);
1019
1020     uuid_invalid = aml_lnot(aml_equal(uuid, expected_uuid));
1021
1022     unsupport = aml_if(aml_or(unpatched, uuid_invalid, NULL));
1023
1024     /*
1025      * function 0 is called to inquire what functions are supported by
1026      * OSPM
1027      */
1028     ifctx = aml_if(aml_equal(function, aml_int(0)));
1029     byte_list[0] = 0 /* No function Supported */;
1030     aml_append(ifctx, aml_return(aml_buffer(1, byte_list)));
1031     aml_append(unsupport, ifctx);
1032
1033     /* No function is supported yet. */
1034     byte_list[0] = 1 /* Not Supported */;
1035     aml_append(unsupport, aml_return(aml_buffer(1, byte_list)));
1036     aml_append(method, unsupport);
1037
1038     /*
1039      * The HDLE indicates the DSM function is issued from which device,
1040      * it reserves 0 for root device and is the handle for NVDIMM devices.
1041      * See the comments in nvdimm_slot_to_handle().
1042      */
1043     aml_append(method, aml_store(handle, aml_name(NVDIMM_DSM_HANDLE)));
1044     aml_append(method, aml_store(aml_arg(1), aml_name(NVDIMM_DSM_REVISION)));
1045     aml_append(method, aml_store(aml_arg(2), aml_name(NVDIMM_DSM_FUNCTION)));
1046
1047     /*
1048      * The fourth parameter (Arg3) of _DSM is a package which contains
1049      * a buffer, the layout of the buffer is specified by UUID (Arg0),
1050      * Revision ID (Arg1) and Function Index (Arg2) which are documented
1051      * in the DSM Spec.
1052      */
1053     pckg = aml_arg(3);
1054     ifctx = aml_if(aml_and(aml_equal(aml_object_type(pckg),
1055                    aml_int(4 /* Package */)) /* It is a Package? */,
1056                    aml_equal(aml_sizeof(pckg), aml_int(1)) /* 1 element? */,
1057                    NULL));
1058
1059     pckg_index = aml_local(2);
1060     pckg_buf = aml_local(3);
1061     aml_append(ifctx, aml_store(aml_index(pckg, aml_int(0)), pckg_index));
1062     aml_append(ifctx, aml_store(aml_derefof(pckg_index), pckg_buf));
1063     aml_append(ifctx, aml_store(pckg_buf, aml_name(NVDIMM_DSM_ARG3)));
1064     aml_append(method, ifctx);
1065
1066     /*
1067      * tell QEMU about the real address of DSM memory, then QEMU
1068      * gets the control and fills the result in DSM memory.
1069      */
1070     aml_append(method, aml_store(dsm_mem, aml_name(NVDIMM_DSM_NOTIFY)));
1071
1072     dsm_out_buf_size = aml_local(1);
1073     /* RLEN is not included in the payload returned to guest. */
1074     aml_append(method, aml_subtract(aml_name(NVDIMM_DSM_OUT_BUF_SIZE),
1075                aml_int(4), dsm_out_buf_size));
1076     aml_append(method, aml_store(aml_shiftleft(dsm_out_buf_size, aml_int(3)),
1077                                  dsm_out_buf_size));
1078     aml_append(method, aml_create_field(aml_name(NVDIMM_DSM_OUT_BUF),
1079                aml_int(0), dsm_out_buf_size, "OBUF"));
1080     aml_append(method, aml_concatenate(aml_buffer(0, NULL), aml_name("OBUF"),
1081                                        dsm_out_buf));
1082     aml_append(method, aml_return(dsm_out_buf));
1083     aml_append(dev, method);
1084 }
1085
1086 static void nvdimm_build_device_dsm(Aml *dev, uint32_t handle)
1087 {
1088     Aml *method;
1089
1090     method = aml_method("_DSM", 4, AML_NOTSERIALIZED);
1091     aml_append(method, aml_return(aml_call5(NVDIMM_COMMON_DSM, aml_arg(0),
1092                                   aml_arg(1), aml_arg(2), aml_arg(3),
1093                                   aml_int(handle))));
1094     aml_append(dev, method);
1095 }
1096
1097 static void nvdimm_build_fit(Aml *dev)
1098 {
1099     Aml *method, *pkg, *buf, *buf_size, *offset, *call_result;
1100     Aml *whilectx, *ifcond, *ifctx, *elsectx, *fit;
1101
1102     buf = aml_local(0);
1103     buf_size = aml_local(1);
1104     fit = aml_local(2);
1105
1106     aml_append(dev, aml_create_dword_field(aml_buffer(4, NULL),
1107                aml_int(0), NVDIMM_DSM_RFIT_STATUS));
1108
1109     /* build helper function, RFIT. */
1110     method = aml_method("RFIT", 1, AML_SERIALIZED);
1111     aml_append(method, aml_create_dword_field(aml_buffer(4, NULL),
1112                                               aml_int(0), "OFST"));
1113
1114     /* prepare input package. */
1115     pkg = aml_package(1);
1116     aml_append(method, aml_store(aml_arg(0), aml_name("OFST")));
1117     aml_append(pkg, aml_name("OFST"));
1118
1119     /* call Read_FIT function. */
1120     call_result = aml_call5(NVDIMM_COMMON_DSM,
1121                             aml_touuid(NVDIMM_QEMU_RSVD_UUID),
1122                             aml_int(1) /* Revision 1 */,
1123                             aml_int(0x1) /* Read FIT */,
1124                             pkg, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT));
1125     aml_append(method, aml_store(call_result, buf));
1126
1127     /* handle _DSM result. */
1128     aml_append(method, aml_create_dword_field(buf,
1129                aml_int(0) /* offset at byte 0 */, "STAU"));
1130
1131     aml_append(method, aml_store(aml_name("STAU"),
1132                                  aml_name(NVDIMM_DSM_RFIT_STATUS)));
1133
1134      /* if something is wrong during _DSM. */
1135     ifcond = aml_equal(aml_int(0 /* Success */), aml_name("STAU"));
1136     ifctx = aml_if(aml_lnot(ifcond));
1137     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1138     aml_append(method, ifctx);
1139
1140     aml_append(method, aml_store(aml_sizeof(buf), buf_size));
1141     aml_append(method, aml_subtract(buf_size,
1142                                     aml_int(4) /* the size of "STAU" */,
1143                                     buf_size));
1144
1145     /* if we read the end of fit. */
1146     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1147     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1148     aml_append(method, ifctx);
1149
1150     aml_append(method, aml_store(aml_shiftleft(buf_size, aml_int(3)),
1151                                  buf_size));
1152     aml_append(method, aml_create_field(buf,
1153                             aml_int(4 * BITS_PER_BYTE), /* offset at byte 4.*/
1154                             buf_size, "BUFF"));
1155     aml_append(method, aml_return(aml_name("BUFF")));
1156     aml_append(dev, method);
1157
1158     /* build _FIT. */
1159     method = aml_method("_FIT", 0, AML_SERIALIZED);
1160     offset = aml_local(3);
1161
1162     aml_append(method, aml_store(aml_buffer(0, NULL), fit));
1163     aml_append(method, aml_store(aml_int(0), offset));
1164
1165     whilectx = aml_while(aml_int(1));
1166     aml_append(whilectx, aml_store(aml_call1("RFIT", offset), buf));
1167     aml_append(whilectx, aml_store(aml_sizeof(buf), buf_size));
1168
1169     /*
1170      * if fit buffer was changed during RFIT, read from the beginning
1171      * again.
1172      */
1173     ifctx = aml_if(aml_equal(aml_name(NVDIMM_DSM_RFIT_STATUS),
1174                              aml_int(0x100 /* fit changed */)));
1175     aml_append(ifctx, aml_store(aml_buffer(0, NULL), fit));
1176     aml_append(ifctx, aml_store(aml_int(0), offset));
1177     aml_append(whilectx, ifctx);
1178
1179     elsectx = aml_else();
1180
1181     /* finish fit read if no data is read out. */
1182     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1183     aml_append(ifctx, aml_return(fit));
1184     aml_append(elsectx, ifctx);
1185
1186     /* update the offset. */
1187     aml_append(elsectx, aml_add(offset, buf_size, offset));
1188     /* append the data we read out to the fit buffer. */
1189     aml_append(elsectx, aml_concatenate(fit, buf, fit));
1190     aml_append(whilectx, elsectx);
1191     aml_append(method, whilectx);
1192
1193     aml_append(dev, method);
1194 }
1195
1196 static void nvdimm_build_nvdimm_devices(Aml *root_dev, uint32_t ram_slots)
1197 {
1198     uint32_t slot;
1199
1200     for (slot = 0; slot < ram_slots; slot++) {
1201         uint32_t handle = nvdimm_slot_to_handle(slot);
1202         Aml *nvdimm_dev;
1203
1204         nvdimm_dev = aml_device("NV%02X", slot);
1205
1206         /*
1207          * ACPI 6.0: 9.20 NVDIMM Devices:
1208          *
1209          * _ADR object that is used to supply OSPM with unique address
1210          * of the NVDIMM device. This is done by returning the NFIT Device
1211          * handle that is used to identify the associated entries in ACPI
1212          * table NFIT or _FIT.
1213          */
1214         aml_append(nvdimm_dev, aml_name_decl("_ADR", aml_int(handle)));
1215
1216         nvdimm_build_device_dsm(nvdimm_dev, handle);
1217         aml_append(root_dev, nvdimm_dev);
1218     }
1219 }
1220
1221 static void nvdimm_build_ssdt(GArray *table_offsets, GArray *table_data,
1222                               BIOSLinker *linker, GArray *dsm_dma_arrea,
1223                               uint32_t ram_slots)
1224 {
1225     Aml *ssdt, *sb_scope, *dev;
1226     int mem_addr_offset, nvdimm_ssdt;
1227
1228     acpi_add_table(table_offsets, table_data);
1229
1230     ssdt = init_aml_allocator();
1231     acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
1232
1233     sb_scope = aml_scope("\\_SB");
1234
1235     dev = aml_device("NVDR");
1236
1237     /*
1238      * ACPI 6.0: 9.20 NVDIMM Devices:
1239      *
1240      * The ACPI Name Space device uses _HID of ACPI0012 to identify the root
1241      * NVDIMM interface device. Platform firmware is required to contain one
1242      * such device in _SB scope if NVDIMMs support is exposed by platform to
1243      * OSPM.
1244      * For each NVDIMM present or intended to be supported by platform,
1245      * platform firmware also exposes an ACPI Namespace Device under the
1246      * root device.
1247      */
1248     aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0012")));
1249
1250     nvdimm_build_common_dsm(dev);
1251
1252     /* 0 is reserved for root device. */
1253     nvdimm_build_device_dsm(dev, 0);
1254     nvdimm_build_fit(dev);
1255
1256     nvdimm_build_nvdimm_devices(dev, ram_slots);
1257
1258     aml_append(sb_scope, dev);
1259     aml_append(ssdt, sb_scope);
1260
1261     nvdimm_ssdt = table_data->len;
1262
1263     /* copy AML table into ACPI tables blob and patch header there */
1264     g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
1265     mem_addr_offset = build_append_named_dword(table_data,
1266                                                NVDIMM_ACPI_MEM_ADDR);
1267
1268     bios_linker_loader_alloc(linker,
1269                              NVDIMM_DSM_MEM_FILE, dsm_dma_arrea,
1270                              sizeof(NvdimmDsmIn), false /* high memory */);
1271     bios_linker_loader_add_pointer(linker,
1272         ACPI_BUILD_TABLE_FILE, mem_addr_offset, sizeof(uint32_t),
1273         NVDIMM_DSM_MEM_FILE, 0);
1274     build_header(linker, table_data,
1275         (void *)(table_data->data + nvdimm_ssdt),
1276         "SSDT", table_data->len - nvdimm_ssdt, 1, NULL, "NVDIMM");
1277     free_aml_allocator();
1278 }
1279
1280 void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
1281                        BIOSLinker *linker, AcpiNVDIMMState *state,
1282                        uint32_t ram_slots)
1283 {
1284     nvdimm_build_nfit(state, table_offsets, table_data, linker);
1285
1286     /*
1287      * NVDIMM device is allowed to be plugged only if there is available
1288      * slot.
1289      */
1290     if (ram_slots) {
1291         nvdimm_build_ssdt(table_offsets, table_data, linker, state->dsm_mem,
1292                           ram_slots);
1293     }
1294 }
This page took 0.097366 seconds and 4 git commands to generate.