cxlmem.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright(c) 2020-2021 Intel Corporation. */
  3. #ifndef __CXL_MEM_H__
  4. #define __CXL_MEM_H__
  5. #include <uapi/linux/cxl_mem.h>
  6. #include <linux/pci.h>
  7. #include <linux/cdev.h>
  8. #include <linux/uuid.h>
  9. #include <linux/node.h>
  10. #include <cxl/event.h>
  11. #include <cxl/mailbox.h>
  12. #include "cxl.h"
  13. /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
  14. #define CXLMDEV_STATUS_OFFSET 0x0
  15. #define CXLMDEV_DEV_FATAL BIT(0)
  16. #define CXLMDEV_FW_HALT BIT(1)
  17. #define CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
  18. #define CXLMDEV_MS_NOT_READY 0
  19. #define CXLMDEV_MS_READY 1
  20. #define CXLMDEV_MS_ERROR 2
  21. #define CXLMDEV_MS_DISABLED 3
  22. #define CXLMDEV_READY(status) \
  23. (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \
  24. CXLMDEV_MS_READY)
  25. #define CXLMDEV_MBOX_IF_READY BIT(4)
  26. #define CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
  27. #define CXLMDEV_RESET_NEEDED_NOT 0
  28. #define CXLMDEV_RESET_NEEDED_COLD 1
  29. #define CXLMDEV_RESET_NEEDED_WARM 2
  30. #define CXLMDEV_RESET_NEEDED_HOT 3
  31. #define CXLMDEV_RESET_NEEDED_CXL 4
  32. #define CXLMDEV_RESET_NEEDED(status) \
  33. (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \
  34. CXLMDEV_RESET_NEEDED_NOT)
  35. struct cxl_memdev_attach {
  36. int (*probe)(struct cxl_memdev *cxlmd);
  37. };
  38. /**
  39. * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
  40. * @dev: driver core device object
  41. * @cdev: char dev core object for ioctl operations
  42. * @cxlds: The device state backing this device
  43. * @detach_work: active memdev lost a port in its ancestry
  44. * @cxl_nvb: coordinate removal of @cxl_nvd if present
  45. * @cxl_nvd: optional bridge to an nvdimm if the device supports pmem
  46. * @endpoint: connection to the CXL port topology for this memory device
  47. * @attach: creator of this memdev depends on CXL link attach to operate
  48. * @id: id number of this memdev instance.
  49. * @depth: endpoint port depth
  50. * @scrub_cycle: current scrub cycle set for this device
  51. * @scrub_region_id: id number of a backed region (if any) for which current scrub cycle set
  52. * @err_rec_array: List of xarrarys to store the memdev error records to
  53. * check attributes for a memory repair operation are from
  54. * current boot.
  55. */
  56. struct cxl_memdev {
  57. struct device dev;
  58. struct cdev cdev;
  59. struct cxl_dev_state *cxlds;
  60. struct work_struct detach_work;
  61. struct cxl_nvdimm_bridge *cxl_nvb;
  62. struct cxl_nvdimm *cxl_nvd;
  63. struct cxl_port *endpoint;
  64. const struct cxl_memdev_attach *attach;
  65. int id;
  66. int depth;
  67. u8 scrub_cycle;
  68. int scrub_region_id;
  69. struct cxl_mem_err_rec *err_rec_array;
  70. };
  71. static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
  72. {
  73. return container_of(dev, struct cxl_memdev, dev);
  74. }
  75. static inline struct cxl_port *cxled_to_port(struct cxl_endpoint_decoder *cxled)
  76. {
  77. return to_cxl_port(cxled->cxld.dev.parent);
  78. }
  79. static inline struct cxl_port *cxlrd_to_port(struct cxl_root_decoder *cxlrd)
  80. {
  81. return to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
  82. }
  83. static inline struct cxl_memdev *
  84. cxled_to_memdev(struct cxl_endpoint_decoder *cxled)
  85. {
  86. struct cxl_port *port = to_cxl_port(cxled->cxld.dev.parent);
  87. return to_cxl_memdev(port->uport_dev);
  88. }
  89. bool is_cxl_memdev(const struct device *dev);
  90. static inline bool is_cxl_endpoint(struct cxl_port *port)
  91. {
  92. return is_cxl_memdev(port->uport_dev);
  93. }
  94. struct cxl_memdev *__devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
  95. const struct cxl_memdev_attach *attach);
  96. struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
  97. const struct cxl_memdev_attach *attach);
  98. int devm_cxl_sanitize_setup_notifier(struct device *host,
  99. struct cxl_memdev *cxlmd);
  100. struct cxl_memdev_state;
  101. int devm_cxl_setup_fw_upload(struct device *host, struct cxl_memdev_state *mds);
  102. int devm_cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
  103. resource_size_t base, resource_size_t len,
  104. resource_size_t skipped);
  105. #define CXL_NR_PARTITIONS_MAX 2
  106. struct cxl_dpa_info {
  107. u64 size;
  108. struct cxl_dpa_part_info {
  109. struct range range;
  110. enum cxl_partition_mode mode;
  111. } part[CXL_NR_PARTITIONS_MAX];
  112. int nr_partitions;
  113. };
  114. int cxl_dpa_setup(struct cxl_dev_state *cxlds, const struct cxl_dpa_info *info);
  115. static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
  116. struct cxl_memdev *cxlmd)
  117. {
  118. if (!port)
  119. return NULL;
  120. return xa_load(&port->endpoints, (unsigned long)&cxlmd->dev);
  121. }
  122. /*
  123. * Per CXL 3.0 Section 8.2.8.4.5.1
  124. */
  125. #define CMD_CMD_RC_TABLE \
  126. C(SUCCESS, 0, NULL), \
  127. C(BACKGROUND, -ENXIO, "background cmd started successfully"), \
  128. C(INPUT, -ENXIO, "cmd input was invalid"), \
  129. C(UNSUPPORTED, -ENXIO, "cmd is not supported"), \
  130. C(INTERNAL, -ENXIO, "internal device error"), \
  131. C(RETRY, -ENXIO, "temporary error, retry once"), \
  132. C(BUSY, -ENXIO, "ongoing background operation"), \
  133. C(MEDIADISABLED, -ENXIO, "media access is disabled"), \
  134. C(FWINPROGRESS, -ENXIO, "one FW package can be transferred at a time"), \
  135. C(FWOOO, -ENXIO, "FW package content was transferred out of order"), \
  136. C(FWAUTH, -ENXIO, "FW package authentication failed"), \
  137. C(FWSLOT, -ENXIO, "FW slot is not supported for requested operation"), \
  138. C(FWROLLBACK, -ENXIO, "rolled back to the previous active FW"), \
  139. C(FWRESET, -ENXIO, "FW failed to activate, needs cold reset"), \
  140. C(HANDLE, -ENXIO, "one or more Event Record Handles were invalid"), \
  141. C(PADDR, -EFAULT, "physical address specified is invalid"), \
  142. C(POISONLMT, -EBUSY, "poison injection limit has been reached"), \
  143. C(MEDIAFAILURE, -ENXIO, "permanent issue with the media"), \
  144. C(ABORT, -ENXIO, "background cmd was aborted by device"), \
  145. C(SECURITY, -ENXIO, "not valid in the current security state"), \
  146. C(PASSPHRASE, -ENXIO, "phrase doesn't match current set passphrase"), \
  147. C(MBUNSUPPORTED, -ENXIO, "unsupported on the mailbox it was issued on"),\
  148. C(PAYLOADLEN, -ENXIO, "invalid payload length"), \
  149. C(LOG, -ENXIO, "invalid or unsupported log page"), \
  150. C(INTERRUPTED, -ENXIO, "asynchronous event occured"), \
  151. C(FEATUREVERSION, -ENXIO, "unsupported feature version"), \
  152. C(FEATURESELVALUE, -ENXIO, "unsupported feature selection value"), \
  153. C(FEATURETRANSFERIP, -ENXIO, "feature transfer in progress"), \
  154. C(FEATURETRANSFEROOO, -ENXIO, "feature transfer out of order"), \
  155. C(RESOURCEEXHAUSTED, -ENXIO, "resources are exhausted"), \
  156. C(EXTLIST, -ENXIO, "invalid Extent List"), \
  157. #undef C
  158. #define C(a, b, c) CXL_MBOX_CMD_RC_##a
  159. enum { CMD_CMD_RC_TABLE };
  160. #undef C
  161. #define C(a, b, c) { b, c }
  162. struct cxl_mbox_cmd_rc {
  163. int err;
  164. const char *desc;
  165. };
  166. static const
  167. struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
  168. #undef C
  169. static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
  170. {
  171. return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
  172. }
  173. static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
  174. {
  175. return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
  176. }
  177. /*
  178. * CXL 2.0 - Memory capacity multiplier
  179. * See Section 8.2.9.5
  180. *
  181. * Volatile, Persistent, and Partition capacities are specified to be in
  182. * multiples of 256MB - define a multiplier to convert to/from bytes.
  183. */
  184. #define CXL_CAPACITY_MULTIPLIER SZ_256M
  185. /*
  186. * Event Interrupt Policy
  187. *
  188. * CXL rev 3.0 section 8.2.9.2.4; Table 8-52
  189. */
  190. enum cxl_event_int_mode {
  191. CXL_INT_NONE = 0x00,
  192. CXL_INT_MSI_MSIX = 0x01,
  193. CXL_INT_FW = 0x02
  194. };
  195. struct cxl_event_interrupt_policy {
  196. u8 info_settings;
  197. u8 warn_settings;
  198. u8 failure_settings;
  199. u8 fatal_settings;
  200. } __packed;
  201. /**
  202. * struct cxl_event_state - Event log driver state
  203. *
  204. * @buf: Buffer to receive event data
  205. * @log_lock: Serialize event_buf and log use
  206. */
  207. struct cxl_event_state {
  208. struct cxl_get_event_payload *buf;
  209. struct mutex log_lock;
  210. };
  211. /* Device enabled poison commands */
  212. enum poison_cmd_enabled_bits {
  213. CXL_POISON_ENABLED_LIST,
  214. CXL_POISON_ENABLED_INJECT,
  215. CXL_POISON_ENABLED_CLEAR,
  216. CXL_POISON_ENABLED_SCAN_CAPS,
  217. CXL_POISON_ENABLED_SCAN_MEDIA,
  218. CXL_POISON_ENABLED_SCAN_RESULTS,
  219. CXL_POISON_ENABLED_MAX
  220. };
  221. /* Device enabled security commands */
  222. enum security_cmd_enabled_bits {
  223. CXL_SEC_ENABLED_SANITIZE,
  224. CXL_SEC_ENABLED_SECURE_ERASE,
  225. CXL_SEC_ENABLED_GET_SECURITY_STATE,
  226. CXL_SEC_ENABLED_SET_PASSPHRASE,
  227. CXL_SEC_ENABLED_DISABLE_PASSPHRASE,
  228. CXL_SEC_ENABLED_UNLOCK,
  229. CXL_SEC_ENABLED_FREEZE_SECURITY,
  230. CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE,
  231. CXL_SEC_ENABLED_MAX
  232. };
  233. /**
  234. * struct cxl_poison_state - Driver poison state info
  235. *
  236. * @max_errors: Maximum media error records held in device cache
  237. * @enabled_cmds: All poison commands enabled in the CEL
  238. * @list_out: The poison list payload returned by device
  239. * @mutex: Protect reads of the poison list
  240. *
  241. * Reads of the poison list are synchronized to ensure that a reader
  242. * does not get an incomplete list because their request overlapped
  243. * (was interrupted or preceded by) another read request of the same
  244. * DPA range. CXL Spec 3.0 Section 8.2.9.8.4.1
  245. */
  246. struct cxl_poison_state {
  247. u32 max_errors;
  248. DECLARE_BITMAP(enabled_cmds, CXL_POISON_ENABLED_MAX);
  249. struct cxl_mbox_poison_out *list_out;
  250. struct mutex mutex; /* Protect reads of poison list */
  251. };
  252. /*
  253. * Get FW Info
  254. * CXL rev 3.0 section 8.2.9.3.1; Table 8-56
  255. */
  256. struct cxl_mbox_get_fw_info {
  257. u8 num_slots;
  258. u8 slot_info;
  259. u8 activation_cap;
  260. u8 reserved[13];
  261. char slot_1_revision[16];
  262. char slot_2_revision[16];
  263. char slot_3_revision[16];
  264. char slot_4_revision[16];
  265. } __packed;
  266. #define CXL_FW_INFO_SLOT_INFO_CUR_MASK GENMASK(2, 0)
  267. #define CXL_FW_INFO_SLOT_INFO_NEXT_MASK GENMASK(5, 3)
  268. #define CXL_FW_INFO_SLOT_INFO_NEXT_SHIFT 3
  269. #define CXL_FW_INFO_ACTIVATION_CAP_HAS_LIVE_ACTIVATE BIT(0)
  270. /*
  271. * Transfer FW Input Payload
  272. * CXL rev 3.0 section 8.2.9.3.2; Table 8-57
  273. */
  274. struct cxl_mbox_transfer_fw {
  275. u8 action;
  276. u8 slot;
  277. u8 reserved[2];
  278. __le32 offset;
  279. u8 reserved2[0x78];
  280. u8 data[];
  281. } __packed;
  282. #define CXL_FW_TRANSFER_ACTION_FULL 0x0
  283. #define CXL_FW_TRANSFER_ACTION_INITIATE 0x1
  284. #define CXL_FW_TRANSFER_ACTION_CONTINUE 0x2
  285. #define CXL_FW_TRANSFER_ACTION_END 0x3
  286. #define CXL_FW_TRANSFER_ACTION_ABORT 0x4
  287. /*
  288. * CXL rev 3.0 section 8.2.9.3.2 mandates 128-byte alignment for FW packages
  289. * and for each part transferred in a Transfer FW command.
  290. */
  291. #define CXL_FW_TRANSFER_ALIGNMENT 128
  292. /*
  293. * Activate FW Input Payload
  294. * CXL rev 3.0 section 8.2.9.3.3; Table 8-58
  295. */
  296. struct cxl_mbox_activate_fw {
  297. u8 action;
  298. u8 slot;
  299. } __packed;
  300. #define CXL_FW_ACTIVATE_ONLINE 0x0
  301. #define CXL_FW_ACTIVATE_OFFLINE 0x1
  302. /* FW state bits */
  303. #define CXL_FW_STATE_BITS 32
  304. #define CXL_FW_CANCEL 0
  305. /**
  306. * struct cxl_fw_state - Firmware upload / activation state
  307. *
  308. * @state: fw_uploader state bitmask
  309. * @oneshot: whether the fw upload fits in a single transfer
  310. * @num_slots: Number of FW slots available
  311. * @cur_slot: Slot number currently active
  312. * @next_slot: Slot number for the new firmware
  313. */
  314. struct cxl_fw_state {
  315. DECLARE_BITMAP(state, CXL_FW_STATE_BITS);
  316. bool oneshot;
  317. int num_slots;
  318. int cur_slot;
  319. int next_slot;
  320. };
  321. /**
  322. * struct cxl_security_state - Device security state
  323. *
  324. * @state: state of last security operation
  325. * @enabled_cmds: All security commands enabled in the CEL
  326. * @poll_tmo_secs: polling timeout
  327. * @sanitize_active: sanitize completion pending
  328. * @poll_dwork: polling work item
  329. * @sanitize_node: sanitation sysfs file to notify
  330. */
  331. struct cxl_security_state {
  332. unsigned long state;
  333. DECLARE_BITMAP(enabled_cmds, CXL_SEC_ENABLED_MAX);
  334. int poll_tmo_secs;
  335. bool sanitize_active;
  336. struct delayed_work poll_dwork;
  337. struct kernfs_node *sanitize_node;
  338. };
  339. /*
  340. * enum cxl_devtype - delineate type-2 from a generic type-3 device
  341. * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
  342. * HDM-DB, no requirement that this device implements a
  343. * mailbox, or other memory-device-standard manageability
  344. * flows.
  345. * @CXL_DEVTYPE_CLASSMEM - Common class definition of a CXL Type-3 device with
  346. * HDM-H and class-mandatory memory device registers
  347. */
  348. enum cxl_devtype {
  349. CXL_DEVTYPE_DEVMEM,
  350. CXL_DEVTYPE_CLASSMEM,
  351. };
  352. /**
  353. * struct cxl_dpa_perf - DPA performance property entry
  354. * @dpa_range: range for DPA address
  355. * @coord: QoS performance data (i.e. latency, bandwidth)
  356. * @cdat_coord: raw QoS performance data from CDAT
  357. * @qos_class: QoS Class cookies
  358. */
  359. struct cxl_dpa_perf {
  360. struct range dpa_range;
  361. struct access_coordinate coord[ACCESS_COORDINATE_MAX];
  362. struct access_coordinate cdat_coord[ACCESS_COORDINATE_MAX];
  363. int qos_class;
  364. };
  365. /**
  366. * struct cxl_dpa_partition - DPA partition descriptor
  367. * @res: shortcut to the partition in the DPA resource tree (cxlds->dpa_res)
  368. * @perf: performance attributes of the partition from CDAT
  369. * @mode: operation mode for the DPA capacity, e.g. ram, pmem, dynamic...
  370. */
  371. struct cxl_dpa_partition {
  372. struct resource res;
  373. struct cxl_dpa_perf perf;
  374. enum cxl_partition_mode mode;
  375. };
  376. /**
  377. * struct cxl_dev_state - The driver device state
  378. *
  379. * cxl_dev_state represents the CXL driver/device state. It provides an
  380. * interface to mailbox commands as well as some cached data about the device.
  381. * Currently only memory devices are represented.
  382. *
  383. * @dev: The device associated with this CXL state
  384. * @cxlmd: The device representing the CXL.mem capabilities of @dev
  385. * @reg_map: component and ras register mapping parameters
  386. * @regs: Class device "Device" registers
  387. * @cxl_dvsec: Offset to the PCIe device DVSEC
  388. * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)
  389. * @media_ready: Indicate whether the device media is usable
  390. * @dpa_res: Overall DPA resource tree for the device
  391. * @part: DPA partition array
  392. * @nr_partitions: Number of DPA partitions
  393. * @serial: PCIe Device Serial Number
  394. * @type: Generic Memory Class device or Vendor Specific Memory device
  395. * @cxl_mbox: CXL mailbox context
  396. * @cxlfs: CXL features context
  397. */
  398. struct cxl_dev_state {
  399. struct device *dev;
  400. struct cxl_memdev *cxlmd;
  401. struct cxl_register_map reg_map;
  402. struct cxl_device_regs regs;
  403. int cxl_dvsec;
  404. bool rcd;
  405. bool media_ready;
  406. struct resource dpa_res;
  407. struct cxl_dpa_partition part[CXL_NR_PARTITIONS_MAX];
  408. unsigned int nr_partitions;
  409. u64 serial;
  410. enum cxl_devtype type;
  411. struct cxl_mailbox cxl_mbox;
  412. #ifdef CONFIG_CXL_FEATURES
  413. struct cxl_features_state *cxlfs;
  414. #endif
  415. };
  416. static inline resource_size_t cxl_pmem_size(struct cxl_dev_state *cxlds)
  417. {
  418. /*
  419. * Static PMEM may be at partition index 0 when there is no static RAM
  420. * capacity.
  421. */
  422. for (int i = 0; i < cxlds->nr_partitions; i++)
  423. if (cxlds->part[i].mode == CXL_PARTMODE_PMEM)
  424. return resource_size(&cxlds->part[i].res);
  425. return 0;
  426. }
  427. static inline struct cxl_dev_state *mbox_to_cxlds(struct cxl_mailbox *cxl_mbox)
  428. {
  429. return dev_get_drvdata(cxl_mbox->host);
  430. }
  431. /**
  432. * struct cxl_memdev_state - Generic Type-3 Memory Device Class driver data
  433. *
  434. * CXL 8.1.12.1 PCI Header - Class Code Register Memory Device defines
  435. * common memory device functionality like the presence of a mailbox and
  436. * the functionality related to that like Identify Memory Device and Get
  437. * Partition Info
  438. * @cxlds: Core driver state common across Type-2 and Type-3 devices
  439. * @lsa_size: Size of Label Storage Area
  440. * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
  441. * @firmware_version: Firmware version for the memory device.
  442. * @total_bytes: sum of all possible capacities
  443. * @volatile_only_bytes: hard volatile capacity
  444. * @persistent_only_bytes: hard persistent capacity
  445. * @partition_align_bytes: alignment size for partition-able capacity
  446. * @active_volatile_bytes: sum of hard + soft volatile
  447. * @active_persistent_bytes: sum of hard + soft persistent
  448. * @event: event log driver state
  449. * @poison: poison driver state info
  450. * @security: security driver state info
  451. * @fw: firmware upload / activation state
  452. * @mce_notifier: MCE notifier
  453. *
  454. * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
  455. * details on capacity parameters.
  456. */
  457. struct cxl_memdev_state {
  458. struct cxl_dev_state cxlds;
  459. size_t lsa_size;
  460. char firmware_version[0x10];
  461. u64 total_bytes;
  462. u64 volatile_only_bytes;
  463. u64 persistent_only_bytes;
  464. u64 partition_align_bytes;
  465. u64 active_volatile_bytes;
  466. u64 active_persistent_bytes;
  467. struct cxl_event_state event;
  468. struct cxl_poison_state poison;
  469. struct cxl_security_state security;
  470. struct cxl_fw_state fw;
  471. struct notifier_block mce_notifier;
  472. };
  473. static inline struct cxl_memdev_state *
  474. to_cxl_memdev_state(struct cxl_dev_state *cxlds)
  475. {
  476. if (cxlds->type != CXL_DEVTYPE_CLASSMEM)
  477. return NULL;
  478. return container_of(cxlds, struct cxl_memdev_state, cxlds);
  479. }
  480. enum cxl_opcode {
  481. CXL_MBOX_OP_INVALID = 0x0000,
  482. CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID,
  483. CXL_MBOX_OP_GET_EVENT_RECORD = 0x0100,
  484. CXL_MBOX_OP_CLEAR_EVENT_RECORD = 0x0101,
  485. CXL_MBOX_OP_GET_EVT_INT_POLICY = 0x0102,
  486. CXL_MBOX_OP_SET_EVT_INT_POLICY = 0x0103,
  487. CXL_MBOX_OP_GET_FW_INFO = 0x0200,
  488. CXL_MBOX_OP_TRANSFER_FW = 0x0201,
  489. CXL_MBOX_OP_ACTIVATE_FW = 0x0202,
  490. CXL_MBOX_OP_GET_TIMESTAMP = 0x0300,
  491. CXL_MBOX_OP_SET_TIMESTAMP = 0x0301,
  492. CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400,
  493. CXL_MBOX_OP_GET_LOG = 0x0401,
  494. CXL_MBOX_OP_GET_LOG_CAPS = 0x0402,
  495. CXL_MBOX_OP_CLEAR_LOG = 0x0403,
  496. CXL_MBOX_OP_GET_SUP_LOG_SUBLIST = 0x0405,
  497. CXL_MBOX_OP_GET_SUPPORTED_FEATURES = 0x0500,
  498. CXL_MBOX_OP_GET_FEATURE = 0x0501,
  499. CXL_MBOX_OP_SET_FEATURE = 0x0502,
  500. CXL_MBOX_OP_DO_MAINTENANCE = 0x0600,
  501. CXL_MBOX_OP_IDENTIFY = 0x4000,
  502. CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100,
  503. CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101,
  504. CXL_MBOX_OP_GET_LSA = 0x4102,
  505. CXL_MBOX_OP_SET_LSA = 0x4103,
  506. CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200,
  507. CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201,
  508. CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202,
  509. CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203,
  510. CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204,
  511. CXL_MBOX_OP_GET_POISON = 0x4300,
  512. CXL_MBOX_OP_INJECT_POISON = 0x4301,
  513. CXL_MBOX_OP_CLEAR_POISON = 0x4302,
  514. CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303,
  515. CXL_MBOX_OP_SCAN_MEDIA = 0x4304,
  516. CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305,
  517. CXL_MBOX_OP_SANITIZE = 0x4400,
  518. CXL_MBOX_OP_SECURE_ERASE = 0x4401,
  519. CXL_MBOX_OP_GET_SECURITY_STATE = 0x4500,
  520. CXL_MBOX_OP_SET_PASSPHRASE = 0x4501,
  521. CXL_MBOX_OP_DISABLE_PASSPHRASE = 0x4502,
  522. CXL_MBOX_OP_UNLOCK = 0x4503,
  523. CXL_MBOX_OP_FREEZE_SECURITY = 0x4504,
  524. CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE = 0x4505,
  525. CXL_MBOX_OP_MAX = 0x10000
  526. };
  527. #define DEFINE_CXL_CEL_UUID \
  528. UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \
  529. 0x3b, 0x3f, 0x17)
  530. #define DEFINE_CXL_VENDOR_DEBUG_UUID \
  531. UUID_INIT(0x5e1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \
  532. 0x40, 0x3d, 0x86)
  533. struct cxl_mbox_get_supported_logs {
  534. __le16 entries;
  535. u8 rsvd[6];
  536. struct cxl_gsl_entry {
  537. uuid_t uuid;
  538. __le32 size;
  539. } __packed entry[];
  540. } __packed;
  541. struct cxl_cel_entry {
  542. __le16 opcode;
  543. __le16 effect;
  544. } __packed;
  545. struct cxl_mbox_get_log {
  546. uuid_t uuid;
  547. __le32 offset;
  548. __le32 length;
  549. } __packed;
  550. /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
  551. struct cxl_mbox_identify {
  552. char fw_revision[0x10];
  553. __le64 total_capacity;
  554. __le64 volatile_capacity;
  555. __le64 persistent_capacity;
  556. __le64 partition_align;
  557. __le16 info_event_log_size;
  558. __le16 warning_event_log_size;
  559. __le16 failure_event_log_size;
  560. __le16 fatal_event_log_size;
  561. __le32 lsa_size;
  562. u8 poison_list_max_mer[3];
  563. __le16 inject_poison_limit;
  564. u8 poison_caps;
  565. u8 qos_telemetry_caps;
  566. } __packed;
  567. /*
  568. * General Media Event Record UUID
  569. * CXL rev 3.0 Section 8.2.9.2.1.1; Table 8-43
  570. */
  571. #define CXL_EVENT_GEN_MEDIA_UUID \
  572. UUID_INIT(0xfbcd0a77, 0xc260, 0x417f, 0x85, 0xa9, 0x08, 0x8b, 0x16, \
  573. 0x21, 0xeb, 0xa6)
  574. /*
  575. * DRAM Event Record UUID
  576. * CXL rev 3.0 section 8.2.9.2.1.2; Table 8-44
  577. */
  578. #define CXL_EVENT_DRAM_UUID \
  579. UUID_INIT(0x601dcbb3, 0x9c06, 0x4eab, 0xb8, 0xaf, 0x4e, 0x9b, 0xfb, \
  580. 0x5c, 0x96, 0x24)
  581. /*
  582. * Memory Module Event Record UUID
  583. * CXL rev 3.0 section 8.2.9.2.1.3; Table 8-45
  584. */
  585. #define CXL_EVENT_MEM_MODULE_UUID \
  586. UUID_INIT(0xfe927475, 0xdd59, 0x4339, 0xa5, 0x86, 0x79, 0xba, 0xb1, \
  587. 0x13, 0xb7, 0x74)
  588. /*
  589. * Memory Sparing Event Record UUID
  590. * CXL rev 3.2 section 8.2.10.2.1.4: Table 8-60
  591. */
  592. #define CXL_EVENT_MEM_SPARING_UUID \
  593. UUID_INIT(0xe71f3a40, 0x2d29, 0x4092, 0x8a, 0x39, 0x4d, 0x1c, 0x96, \
  594. 0x6c, 0x7c, 0x65)
  595. /*
  596. * Get Event Records output payload
  597. * CXL rev 3.0 section 8.2.9.2.2; Table 8-50
  598. */
  599. #define CXL_GET_EVENT_FLAG_OVERFLOW BIT(0)
  600. #define CXL_GET_EVENT_FLAG_MORE_RECORDS BIT(1)
  601. struct cxl_get_event_payload {
  602. u8 flags;
  603. u8 reserved1;
  604. __le16 overflow_err_count;
  605. __le64 first_overflow_timestamp;
  606. __le64 last_overflow_timestamp;
  607. __le16 record_count;
  608. u8 reserved2[10];
  609. struct cxl_event_record_raw records[];
  610. } __packed;
  611. /*
  612. * CXL rev 3.0 section 8.2.9.2.2; Table 8-49
  613. */
  614. enum cxl_event_log_type {
  615. CXL_EVENT_TYPE_INFO = 0x00,
  616. CXL_EVENT_TYPE_WARN,
  617. CXL_EVENT_TYPE_FAIL,
  618. CXL_EVENT_TYPE_FATAL,
  619. CXL_EVENT_TYPE_MAX
  620. };
  621. /*
  622. * Clear Event Records input payload
  623. * CXL rev 3.0 section 8.2.9.2.3; Table 8-51
  624. */
  625. struct cxl_mbox_clear_event_payload {
  626. u8 event_log; /* enum cxl_event_log_type */
  627. u8 clear_flags;
  628. u8 nr_recs;
  629. u8 reserved[3];
  630. __le16 handles[];
  631. } __packed;
  632. #define CXL_CLEAR_EVENT_MAX_HANDLES U8_MAX
  633. struct cxl_mbox_get_partition_info {
  634. __le64 active_volatile_cap;
  635. __le64 active_persistent_cap;
  636. __le64 next_volatile_cap;
  637. __le64 next_persistent_cap;
  638. } __packed;
  639. struct cxl_mbox_get_lsa {
  640. __le32 offset;
  641. __le32 length;
  642. } __packed;
  643. struct cxl_mbox_set_lsa {
  644. __le32 offset;
  645. __le32 reserved;
  646. u8 data[];
  647. } __packed;
  648. struct cxl_mbox_set_partition_info {
  649. __le64 volatile_capacity;
  650. u8 flags;
  651. } __packed;
  652. #define CXL_SET_PARTITION_IMMEDIATE_FLAG BIT(0)
  653. /* Get Health Info Output Payload CXL 3.2 Spec 8.2.10.9.3.1 Table 8-148 */
  654. struct cxl_mbox_get_health_info_out {
  655. u8 health_status;
  656. u8 media_status;
  657. u8 additional_status;
  658. u8 life_used;
  659. __le16 device_temperature;
  660. __le32 dirty_shutdown_cnt;
  661. __le32 corrected_volatile_error_cnt;
  662. __le32 corrected_persistent_error_cnt;
  663. } __packed;
  664. /* Set Shutdown State Input Payload CXL 3.2 Spec 8.2.10.9.3.5 Table 8-152 */
  665. struct cxl_mbox_set_shutdown_state_in {
  666. u8 state;
  667. } __packed;
  668. /* Set Timestamp CXL 3.0 Spec 8.2.9.4.2 */
  669. struct cxl_mbox_set_timestamp_in {
  670. __le64 timestamp;
  671. } __packed;
  672. /* Get Poison List CXL 3.0 Spec 8.2.9.8.4.1 */
  673. struct cxl_mbox_poison_in {
  674. __le64 offset;
  675. __le64 length;
  676. } __packed;
  677. struct cxl_mbox_poison_out {
  678. u8 flags;
  679. u8 rsvd1;
  680. __le64 overflow_ts;
  681. __le16 count;
  682. u8 rsvd2[20];
  683. struct cxl_poison_record {
  684. __le64 address;
  685. __le32 length;
  686. __le32 rsvd;
  687. } __packed record[];
  688. } __packed;
  689. /*
  690. * Get Poison List address field encodes the starting
  691. * address of poison, and the source of the poison.
  692. */
  693. #define CXL_POISON_START_MASK GENMASK_ULL(63, 6)
  694. #define CXL_POISON_SOURCE_MASK GENMASK(2, 0)
  695. /* Get Poison List record length is in units of 64 bytes */
  696. #define CXL_POISON_LEN_MULT 64
  697. /* Kernel defined maximum for a list of poison errors */
  698. #define CXL_POISON_LIST_MAX 1024
  699. /* Get Poison List: Payload out flags */
  700. #define CXL_POISON_FLAG_MORE BIT(0)
  701. #define CXL_POISON_FLAG_OVERFLOW BIT(1)
  702. #define CXL_POISON_FLAG_SCANNING BIT(2)
  703. /* Get Poison List: Poison Source */
  704. #define CXL_POISON_SOURCE_UNKNOWN 0
  705. #define CXL_POISON_SOURCE_EXTERNAL 1
  706. #define CXL_POISON_SOURCE_INTERNAL 2
  707. #define CXL_POISON_SOURCE_INJECTED 3
  708. #define CXL_POISON_SOURCE_VENDOR 7
  709. /* Inject & Clear Poison CXL 3.0 Spec 8.2.9.8.4.2/3 */
  710. struct cxl_mbox_inject_poison {
  711. __le64 address;
  712. };
  713. /* Clear Poison CXL 3.0 Spec 8.2.9.8.4.3 */
  714. struct cxl_mbox_clear_poison {
  715. __le64 address;
  716. u8 write_data[CXL_POISON_LEN_MULT];
  717. } __packed;
  718. /**
  719. * struct cxl_mem_command - Driver representation of a memory device command
  720. * @info: Command information as it exists for the UAPI
  721. * @opcode: The actual bits used for the mailbox protocol
  722. * @flags: Set of flags effecting driver behavior.
  723. *
  724. * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
  725. * will be enabled by the driver regardless of what hardware may have
  726. * advertised.
  727. *
  728. * The cxl_mem_command is the driver's internal representation of commands that
  729. * are supported by the driver. Some of these commands may not be supported by
  730. * the hardware. The driver will use @info to validate the fields passed in by
  731. * the user then submit the @opcode to the hardware.
  732. *
  733. * See struct cxl_command_info.
  734. */
  735. struct cxl_mem_command {
  736. struct cxl_command_info info;
  737. enum cxl_opcode opcode;
  738. u32 flags;
  739. #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
  740. };
  741. #define CXL_PMEM_SEC_STATE_USER_PASS_SET 0x01
  742. #define CXL_PMEM_SEC_STATE_MASTER_PASS_SET 0x02
  743. #define CXL_PMEM_SEC_STATE_LOCKED 0x04
  744. #define CXL_PMEM_SEC_STATE_FROZEN 0x08
  745. #define CXL_PMEM_SEC_STATE_USER_PLIMIT 0x10
  746. #define CXL_PMEM_SEC_STATE_MASTER_PLIMIT 0x20
  747. /* set passphrase input payload */
  748. struct cxl_set_pass {
  749. u8 type;
  750. u8 reserved[31];
  751. /* CXL field using NVDIMM define, same length */
  752. u8 old_pass[NVDIMM_PASSPHRASE_LEN];
  753. u8 new_pass[NVDIMM_PASSPHRASE_LEN];
  754. } __packed;
  755. /* disable passphrase input payload */
  756. struct cxl_disable_pass {
  757. u8 type;
  758. u8 reserved[31];
  759. u8 pass[NVDIMM_PASSPHRASE_LEN];
  760. } __packed;
  761. /* passphrase secure erase payload */
  762. struct cxl_pass_erase {
  763. u8 type;
  764. u8 reserved[31];
  765. u8 pass[NVDIMM_PASSPHRASE_LEN];
  766. } __packed;
  767. enum {
  768. CXL_PMEM_SEC_PASS_MASTER = 0,
  769. CXL_PMEM_SEC_PASS_USER,
  770. };
  771. int cxl_internal_send_cmd(struct cxl_mailbox *cxl_mbox,
  772. struct cxl_mbox_cmd *cmd);
  773. int cxl_dev_state_identify(struct cxl_memdev_state *mds);
  774. int cxl_await_media_ready(struct cxl_dev_state *cxlds);
  775. int cxl_enumerate_cmds(struct cxl_memdev_state *mds);
  776. int cxl_mem_dpa_fetch(struct cxl_memdev_state *mds, struct cxl_dpa_info *info);
  777. struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev);
  778. void set_exclusive_cxl_commands(struct cxl_memdev_state *mds,
  779. unsigned long *cmds);
  780. void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds,
  781. unsigned long *cmds);
  782. void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status);
  783. void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
  784. enum cxl_event_log_type type,
  785. enum cxl_event_type event_type,
  786. const uuid_t *uuid, union cxl_event *evt);
  787. int cxl_get_dirty_count(struct cxl_memdev_state *mds, u32 *count);
  788. int cxl_arm_dirty_shutdown(struct cxl_memdev_state *mds);
  789. int cxl_set_timestamp(struct cxl_memdev_state *mds);
  790. int cxl_poison_state_init(struct cxl_memdev_state *mds);
  791. int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
  792. struct cxl_region *cxlr);
  793. int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
  794. int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
  795. int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
  796. int cxl_inject_poison_locked(struct cxl_memdev *cxlmd, u64 dpa);
  797. int cxl_clear_poison_locked(struct cxl_memdev *cxlmd, u64 dpa);
  798. #ifdef CONFIG_CXL_EDAC_MEM_FEATURES
  799. int devm_cxl_memdev_edac_register(struct cxl_memdev *cxlmd);
  800. int devm_cxl_region_edac_register(struct cxl_region *cxlr);
  801. int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt);
  802. int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt);
  803. #else
  804. static inline int devm_cxl_memdev_edac_register(struct cxl_memdev *cxlmd)
  805. { return 0; }
  806. static inline int devm_cxl_region_edac_register(struct cxl_region *cxlr)
  807. { return 0; }
  808. static inline int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd,
  809. union cxl_event *evt)
  810. { return 0; }
  811. static inline int cxl_store_rec_dram(struct cxl_memdev *cxlmd,
  812. union cxl_event *evt)
  813. { return 0; }
  814. #endif
  815. #ifdef CONFIG_CXL_SUSPEND
  816. void cxl_mem_active_inc(void);
  817. void cxl_mem_active_dec(void);
  818. #else
  819. static inline void cxl_mem_active_inc(void)
  820. {
  821. }
  822. static inline void cxl_mem_active_dec(void)
  823. {
  824. }
  825. #endif
  826. int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd);
  827. /**
  828. * struct cxl_hdm - HDM Decoder registers and cached / decoded capabilities
  829. * @regs: mapped registers, see devm_cxl_setup_hdm()
  830. * @decoder_count: number of decoders for this port
  831. * @target_count: for switch decoders, max downstream port targets
  832. * @interleave_mask: interleave granularity capability, see check_interleave_cap()
  833. * @iw_cap_mask: bitmask of supported interleave ways, see check_interleave_cap()
  834. * @port: mapped cxl_port, see devm_cxl_setup_hdm()
  835. */
  836. struct cxl_hdm {
  837. struct cxl_component_regs regs;
  838. unsigned int decoder_count;
  839. unsigned int target_count;
  840. unsigned int interleave_mask;
  841. unsigned long iw_cap_mask;
  842. struct cxl_port *port;
  843. };
  844. struct seq_file;
  845. struct dentry *cxl_debugfs_create_dir(const char *dir);
  846. void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds);
  847. #endif /* __CXL_MEM_H__ */