cxlmem.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/cdev.h>
  7. #include "cxl.h"
  8. /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
  9. #define CXLMDEV_STATUS_OFFSET 0x0
  10. #define CXLMDEV_DEV_FATAL BIT(0)
  11. #define CXLMDEV_FW_HALT BIT(1)
  12. #define CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
  13. #define CXLMDEV_MS_NOT_READY 0
  14. #define CXLMDEV_MS_READY 1
  15. #define CXLMDEV_MS_ERROR 2
  16. #define CXLMDEV_MS_DISABLED 3
  17. #define CXLMDEV_READY(status) \
  18. (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \
  19. CXLMDEV_MS_READY)
  20. #define CXLMDEV_MBOX_IF_READY BIT(4)
  21. #define CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
  22. #define CXLMDEV_RESET_NEEDED_NOT 0
  23. #define CXLMDEV_RESET_NEEDED_COLD 1
  24. #define CXLMDEV_RESET_NEEDED_WARM 2
  25. #define CXLMDEV_RESET_NEEDED_HOT 3
  26. #define CXLMDEV_RESET_NEEDED_CXL 4
  27. #define CXLMDEV_RESET_NEEDED(status) \
  28. (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \
  29. CXLMDEV_RESET_NEEDED_NOT)
  30. /**
  31. * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
  32. * @dev: driver core device object
  33. * @cdev: char dev core object for ioctl operations
  34. * @cxlds: The device state backing this device
  35. * @detach_work: active memdev lost a port in its ancestry
  36. * @id: id number of this memdev instance.
  37. */
  38. struct cxl_memdev {
  39. struct device dev;
  40. struct cdev cdev;
  41. struct cxl_dev_state *cxlds;
  42. struct work_struct detach_work;
  43. int id;
  44. };
  45. static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
  46. {
  47. return container_of(dev, struct cxl_memdev, dev);
  48. }
  49. static inline struct cxl_port *cxled_to_port(struct cxl_endpoint_decoder *cxled)
  50. {
  51. return to_cxl_port(cxled->cxld.dev.parent);
  52. }
  53. static inline struct cxl_port *cxlrd_to_port(struct cxl_root_decoder *cxlrd)
  54. {
  55. return to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
  56. }
  57. static inline struct cxl_memdev *
  58. cxled_to_memdev(struct cxl_endpoint_decoder *cxled)
  59. {
  60. struct cxl_port *port = to_cxl_port(cxled->cxld.dev.parent);
  61. return to_cxl_memdev(port->uport);
  62. }
  63. bool is_cxl_memdev(struct device *dev);
  64. static inline bool is_cxl_endpoint(struct cxl_port *port)
  65. {
  66. return is_cxl_memdev(port->uport);
  67. }
  68. struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds);
  69. int devm_cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
  70. resource_size_t base, resource_size_t len,
  71. resource_size_t skipped);
  72. static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
  73. struct cxl_memdev *cxlmd)
  74. {
  75. if (!port)
  76. return NULL;
  77. return xa_load(&port->endpoints, (unsigned long)&cxlmd->dev);
  78. }
  79. /**
  80. * struct cxl_mbox_cmd - A command to be submitted to hardware.
  81. * @opcode: (input) The command set and command submitted to hardware.
  82. * @payload_in: (input) Pointer to the input payload.
  83. * @payload_out: (output) Pointer to the output payload. Must be allocated by
  84. * the caller.
  85. * @size_in: (input) Number of bytes to load from @payload_in.
  86. * @size_out: (input) Max number of bytes loaded into @payload_out.
  87. * (output) Number of bytes generated by the device. For fixed size
  88. * outputs commands this is always expected to be deterministic. For
  89. * variable sized output commands, it tells the exact number of bytes
  90. * written.
  91. * @return_code: (output) Error code returned from hardware.
  92. *
  93. * This is the primary mechanism used to send commands to the hardware.
  94. * All the fields except @payload_* correspond exactly to the fields described in
  95. * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and
  96. * @payload_out are written to, and read from the Command Payload Registers
  97. * defined in CXL 2.0 8.2.8.4.8.
  98. */
  99. struct cxl_mbox_cmd {
  100. u16 opcode;
  101. void *payload_in;
  102. void *payload_out;
  103. size_t size_in;
  104. size_t size_out;
  105. u16 return_code;
  106. };
  107. /*
  108. * Per CXL 2.0 Section 8.2.8.4.5.1
  109. */
  110. #define CMD_CMD_RC_TABLE \
  111. C(SUCCESS, 0, NULL), \
  112. C(BACKGROUND, -ENXIO, "background cmd started successfully"), \
  113. C(INPUT, -ENXIO, "cmd input was invalid"), \
  114. C(UNSUPPORTED, -ENXIO, "cmd is not supported"), \
  115. C(INTERNAL, -ENXIO, "internal device error"), \
  116. C(RETRY, -ENXIO, "temporary error, retry once"), \
  117. C(BUSY, -ENXIO, "ongoing background operation"), \
  118. C(MEDIADISABLED, -ENXIO, "media access is disabled"), \
  119. C(FWINPROGRESS, -ENXIO, "one FW package can be transferred at a time"), \
  120. C(FWOOO, -ENXIO, "FW package content was transferred out of order"), \
  121. C(FWAUTH, -ENXIO, "FW package authentication failed"), \
  122. C(FWSLOT, -ENXIO, "FW slot is not supported for requested operation"), \
  123. C(FWROLLBACK, -ENXIO, "rolled back to the previous active FW"), \
  124. C(FWRESET, -ENXIO, "FW failed to activate, needs cold reset"), \
  125. C(HANDLE, -ENXIO, "one or more Event Record Handles were invalid"), \
  126. C(PADDR, -ENXIO, "physical address specified is invalid"), \
  127. C(POISONLMT, -ENXIO, "poison injection limit has been reached"), \
  128. C(MEDIAFAILURE, -ENXIO, "permanent issue with the media"), \
  129. C(ABORT, -ENXIO, "background cmd was aborted by device"), \
  130. C(SECURITY, -ENXIO, "not valid in the current security state"), \
  131. C(PASSPHRASE, -ENXIO, "phrase doesn't match current set passphrase"), \
  132. C(MBUNSUPPORTED, -ENXIO, "unsupported on the mailbox it was issued on"),\
  133. C(PAYLOADLEN, -ENXIO, "invalid payload length")
  134. #undef C
  135. #define C(a, b, c) CXL_MBOX_CMD_RC_##a
  136. enum { CMD_CMD_RC_TABLE };
  137. #undef C
  138. #define C(a, b, c) { b, c }
  139. struct cxl_mbox_cmd_rc {
  140. int err;
  141. const char *desc;
  142. };
  143. static const
  144. struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
  145. #undef C
  146. static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
  147. {
  148. return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
  149. }
  150. static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
  151. {
  152. return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
  153. }
  154. /*
  155. * CXL 2.0 - Memory capacity multiplier
  156. * See Section 8.2.9.5
  157. *
  158. * Volatile, Persistent, and Partition capacities are specified to be in
  159. * multiples of 256MB - define a multiplier to convert to/from bytes.
  160. */
  161. #define CXL_CAPACITY_MULTIPLIER SZ_256M
  162. /**
  163. * struct cxl_endpoint_dvsec_info - Cached DVSEC info
  164. * @mem_enabled: cached value of mem_enabled in the DVSEC, PCIE_DEVICE
  165. * @ranges: Number of active HDM ranges this device uses.
  166. * @dvsec_range: cached attributes of the ranges in the DVSEC, PCIE_DEVICE
  167. */
  168. struct cxl_endpoint_dvsec_info {
  169. bool mem_enabled;
  170. int ranges;
  171. struct range dvsec_range[2];
  172. };
  173. /**
  174. * struct cxl_dev_state - The driver device state
  175. *
  176. * cxl_dev_state represents the CXL driver/device state. It provides an
  177. * interface to mailbox commands as well as some cached data about the device.
  178. * Currently only memory devices are represented.
  179. *
  180. * @dev: The device associated with this CXL state
  181. * @regs: Parsed register blocks
  182. * @cxl_dvsec: Offset to the PCIe device DVSEC
  183. * @payload_size: Size of space for payload
  184. * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
  185. * @lsa_size: Size of Label Storage Area
  186. * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
  187. * @mbox_mutex: Mutex to synchronize mailbox access.
  188. * @firmware_version: Firmware version for the memory device.
  189. * @enabled_cmds: Hardware commands found enabled in CEL.
  190. * @exclusive_cmds: Commands that are kernel-internal only
  191. * @dpa_res: Overall DPA resource tree for the device
  192. * @pmem_res: Active Persistent memory capacity configuration
  193. * @ram_res: Active Volatile memory capacity configuration
  194. * @total_bytes: sum of all possible capacities
  195. * @volatile_only_bytes: hard volatile capacity
  196. * @persistent_only_bytes: hard persistent capacity
  197. * @partition_align_bytes: alignment size for partition-able capacity
  198. * @active_volatile_bytes: sum of hard + soft volatile
  199. * @active_persistent_bytes: sum of hard + soft persistent
  200. * @next_volatile_bytes: volatile capacity change pending device reset
  201. * @next_persistent_bytes: persistent capacity change pending device reset
  202. * @component_reg_phys: register base of component registers
  203. * @info: Cached DVSEC information about the device.
  204. * @serial: PCIe Device Serial Number
  205. * @doe_mbs: PCI DOE mailbox array
  206. * @mbox_send: @dev specific transport for transmitting mailbox commands
  207. *
  208. * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
  209. * details on capacity parameters.
  210. */
  211. struct cxl_dev_state {
  212. struct device *dev;
  213. struct cxl_regs regs;
  214. int cxl_dvsec;
  215. size_t payload_size;
  216. size_t lsa_size;
  217. struct mutex mbox_mutex; /* Protects device mailbox and firmware */
  218. char firmware_version[0x10];
  219. DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
  220. DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
  221. struct resource dpa_res;
  222. struct resource pmem_res;
  223. struct resource ram_res;
  224. u64 total_bytes;
  225. u64 volatile_only_bytes;
  226. u64 persistent_only_bytes;
  227. u64 partition_align_bytes;
  228. u64 active_volatile_bytes;
  229. u64 active_persistent_bytes;
  230. u64 next_volatile_bytes;
  231. u64 next_persistent_bytes;
  232. resource_size_t component_reg_phys;
  233. u64 serial;
  234. struct xarray doe_mbs;
  235. int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
  236. };
  237. enum cxl_opcode {
  238. CXL_MBOX_OP_INVALID = 0x0000,
  239. CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID,
  240. CXL_MBOX_OP_GET_FW_INFO = 0x0200,
  241. CXL_MBOX_OP_ACTIVATE_FW = 0x0202,
  242. CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400,
  243. CXL_MBOX_OP_GET_LOG = 0x0401,
  244. CXL_MBOX_OP_IDENTIFY = 0x4000,
  245. CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100,
  246. CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101,
  247. CXL_MBOX_OP_GET_LSA = 0x4102,
  248. CXL_MBOX_OP_SET_LSA = 0x4103,
  249. CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200,
  250. CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201,
  251. CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202,
  252. CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203,
  253. CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204,
  254. CXL_MBOX_OP_GET_POISON = 0x4300,
  255. CXL_MBOX_OP_INJECT_POISON = 0x4301,
  256. CXL_MBOX_OP_CLEAR_POISON = 0x4302,
  257. CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303,
  258. CXL_MBOX_OP_SCAN_MEDIA = 0x4304,
  259. CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305,
  260. CXL_MBOX_OP_MAX = 0x10000
  261. };
  262. #define DEFINE_CXL_CEL_UUID \
  263. UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \
  264. 0x3b, 0x3f, 0x17)
  265. #define DEFINE_CXL_VENDOR_DEBUG_UUID \
  266. UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \
  267. 0x40, 0x3d, 0x86)
  268. struct cxl_mbox_get_supported_logs {
  269. __le16 entries;
  270. u8 rsvd[6];
  271. struct cxl_gsl_entry {
  272. uuid_t uuid;
  273. __le32 size;
  274. } __packed entry[];
  275. } __packed;
  276. struct cxl_cel_entry {
  277. __le16 opcode;
  278. __le16 effect;
  279. } __packed;
  280. struct cxl_mbox_get_log {
  281. uuid_t uuid;
  282. __le32 offset;
  283. __le32 length;
  284. } __packed;
  285. /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
  286. struct cxl_mbox_identify {
  287. char fw_revision[0x10];
  288. __le64 total_capacity;
  289. __le64 volatile_capacity;
  290. __le64 persistent_capacity;
  291. __le64 partition_align;
  292. __le16 info_event_log_size;
  293. __le16 warning_event_log_size;
  294. __le16 failure_event_log_size;
  295. __le16 fatal_event_log_size;
  296. __le32 lsa_size;
  297. u8 poison_list_max_mer[3];
  298. __le16 inject_poison_limit;
  299. u8 poison_caps;
  300. u8 qos_telemetry_caps;
  301. } __packed;
  302. struct cxl_mbox_get_partition_info {
  303. __le64 active_volatile_cap;
  304. __le64 active_persistent_cap;
  305. __le64 next_volatile_cap;
  306. __le64 next_persistent_cap;
  307. } __packed;
  308. struct cxl_mbox_get_lsa {
  309. __le32 offset;
  310. __le32 length;
  311. } __packed;
  312. struct cxl_mbox_set_lsa {
  313. __le32 offset;
  314. __le32 reserved;
  315. u8 data[];
  316. } __packed;
  317. struct cxl_mbox_set_partition_info {
  318. __le64 volatile_capacity;
  319. u8 flags;
  320. } __packed;
  321. #define CXL_SET_PARTITION_IMMEDIATE_FLAG BIT(0)
  322. /**
  323. * struct cxl_mem_command - Driver representation of a memory device command
  324. * @info: Command information as it exists for the UAPI
  325. * @opcode: The actual bits used for the mailbox protocol
  326. * @flags: Set of flags effecting driver behavior.
  327. *
  328. * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
  329. * will be enabled by the driver regardless of what hardware may have
  330. * advertised.
  331. *
  332. * The cxl_mem_command is the driver's internal representation of commands that
  333. * are supported by the driver. Some of these commands may not be supported by
  334. * the hardware. The driver will use @info to validate the fields passed in by
  335. * the user then submit the @opcode to the hardware.
  336. *
  337. * See struct cxl_command_info.
  338. */
  339. struct cxl_mem_command {
  340. struct cxl_command_info info;
  341. enum cxl_opcode opcode;
  342. u32 flags;
  343. #define CXL_CMD_FLAG_NONE 0
  344. #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
  345. };
  346. int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in,
  347. size_t in_size, void *out, size_t out_size);
  348. int cxl_dev_state_identify(struct cxl_dev_state *cxlds);
  349. int cxl_await_media_ready(struct cxl_dev_state *cxlds);
  350. int cxl_enumerate_cmds(struct cxl_dev_state *cxlds);
  351. int cxl_mem_create_range_info(struct cxl_dev_state *cxlds);
  352. struct cxl_dev_state *cxl_dev_state_create(struct device *dev);
  353. void set_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds);
  354. void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds);
  355. #ifdef CONFIG_CXL_SUSPEND
  356. void cxl_mem_active_inc(void);
  357. void cxl_mem_active_dec(void);
  358. #else
  359. static inline void cxl_mem_active_inc(void)
  360. {
  361. }
  362. static inline void cxl_mem_active_dec(void)
  363. {
  364. }
  365. #endif
  366. struct cxl_hdm {
  367. struct cxl_component_regs regs;
  368. unsigned int decoder_count;
  369. unsigned int target_count;
  370. unsigned int interleave_mask;
  371. struct cxl_port *port;
  372. };
  373. struct seq_file;
  374. struct dentry *cxl_debugfs_create_dir(const char *dir);
  375. void cxl_dpa_debug(struct seq_file *file, struct cxl_dev_state *cxlds);
  376. #endif /* __CXL_MEM_H__ */