nvdimm.c 51 KB

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