cxlmem.h 27 KB

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