15161a55cSBen Widawsky /* SPDX-License-Identifier: GPL-2.0-only */ 25161a55cSBen Widawsky /* Copyright(c) 2020-2021 Intel Corporation. */ 35161a55cSBen Widawsky #ifndef __CXL_MEM_H__ 45161a55cSBen Widawsky #define __CXL_MEM_H__ 54faf31b4SDan Williams #include <uapi/linux/cxl_mem.h> 65161a55cSBen Widawsky #include <linux/cdev.h> 75161a55cSBen Widawsky #include "cxl.h" 85161a55cSBen Widawsky 95161a55cSBen Widawsky /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */ 105161a55cSBen Widawsky #define CXLMDEV_STATUS_OFFSET 0x0 115161a55cSBen Widawsky #define CXLMDEV_DEV_FATAL BIT(0) 125161a55cSBen Widawsky #define CXLMDEV_FW_HALT BIT(1) 135161a55cSBen Widawsky #define CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2) 145161a55cSBen Widawsky #define CXLMDEV_MS_NOT_READY 0 155161a55cSBen Widawsky #define CXLMDEV_MS_READY 1 165161a55cSBen Widawsky #define CXLMDEV_MS_ERROR 2 175161a55cSBen Widawsky #define CXLMDEV_MS_DISABLED 3 185161a55cSBen Widawsky #define CXLMDEV_READY(status) \ 195161a55cSBen Widawsky (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \ 205161a55cSBen Widawsky CXLMDEV_MS_READY) 215161a55cSBen Widawsky #define CXLMDEV_MBOX_IF_READY BIT(4) 225161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5) 235161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_NOT 0 245161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_COLD 1 255161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_WARM 2 265161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_HOT 3 275161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED_CXL 4 285161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED(status) \ 295161a55cSBen Widawsky (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \ 305161a55cSBen Widawsky CXLMDEV_RESET_NEEDED_NOT) 315161a55cSBen Widawsky 325161a55cSBen Widawsky /** 335161a55cSBen Widawsky * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device 345161a55cSBen Widawsky * @dev: driver core device object 355161a55cSBen Widawsky * @cdev: char dev core object for ioctl operations 365e2411aeSIra Weiny * @cxlds: The device state backing this device 375161a55cSBen Widawsky * @id: id number of this memdev instance. 385161a55cSBen Widawsky */ 395161a55cSBen Widawsky struct cxl_memdev { 405161a55cSBen Widawsky struct device dev; 415161a55cSBen Widawsky struct cdev cdev; 425e2411aeSIra Weiny struct cxl_dev_state *cxlds; 435161a55cSBen Widawsky int id; 445161a55cSBen Widawsky }; 455161a55cSBen Widawsky 463d135db5SBen Widawsky static inline struct cxl_memdev *to_cxl_memdev(struct device *dev) 473d135db5SBen Widawsky { 483d135db5SBen Widawsky return container_of(dev, struct cxl_memdev, dev); 493d135db5SBen Widawsky } 503d135db5SBen Widawsky 515e2411aeSIra Weiny struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds); 523d135db5SBen Widawsky 535161a55cSBen Widawsky /** 54b64955a9SDan Williams * struct cxl_mbox_cmd - A command to be submitted to hardware. 55b64955a9SDan Williams * @opcode: (input) The command set and command submitted to hardware. 56b64955a9SDan Williams * @payload_in: (input) Pointer to the input payload. 57b64955a9SDan Williams * @payload_out: (output) Pointer to the output payload. Must be allocated by 58b64955a9SDan Williams * the caller. 59b64955a9SDan Williams * @size_in: (input) Number of bytes to load from @payload_in. 60b64955a9SDan Williams * @size_out: (input) Max number of bytes loaded into @payload_out. 61b64955a9SDan Williams * (output) Number of bytes generated by the device. For fixed size 62b64955a9SDan Williams * outputs commands this is always expected to be deterministic. For 63b64955a9SDan Williams * variable sized output commands, it tells the exact number of bytes 64b64955a9SDan Williams * written. 65b64955a9SDan Williams * @return_code: (output) Error code returned from hardware. 66b64955a9SDan Williams * 67b64955a9SDan Williams * This is the primary mechanism used to send commands to the hardware. 68b64955a9SDan Williams * All the fields except @payload_* correspond exactly to the fields described in 69b64955a9SDan Williams * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and 70b64955a9SDan Williams * @payload_out are written to, and read from the Command Payload Registers 71b64955a9SDan Williams * defined in CXL 2.0 8.2.8.4.8. 72b64955a9SDan Williams */ 73b64955a9SDan Williams struct cxl_mbox_cmd { 74b64955a9SDan Williams u16 opcode; 75b64955a9SDan Williams void *payload_in; 76b64955a9SDan Williams void *payload_out; 77b64955a9SDan Williams size_t size_in; 78b64955a9SDan Williams size_t size_out; 79b64955a9SDan Williams u16 return_code; 80b64955a9SDan Williams #define CXL_MBOX_SUCCESS 0 81b64955a9SDan Williams }; 82b64955a9SDan Williams 83b64955a9SDan Williams /* 84b64955a9SDan Williams * CXL 2.0 - Memory capacity multiplier 85b64955a9SDan Williams * See Section 8.2.9.5 86b64955a9SDan Williams * 87b64955a9SDan Williams * Volatile, Persistent, and Partition capacities are specified to be in 88b64955a9SDan Williams * multiples of 256MB - define a multiplier to convert to/from bytes. 89b64955a9SDan Williams */ 90b64955a9SDan Williams #define CXL_CAPACITY_MULTIPLIER SZ_256M 91b64955a9SDan Williams 92b64955a9SDan Williams /** 93*560f7855SBen Widawsky * struct cxl_endpoint_dvsec_info - Cached DVSEC info 94*560f7855SBen Widawsky * @mem_enabled: cached value of mem_enabled in the DVSEC, PCIE_DEVICE 95*560f7855SBen Widawsky * @ranges: Number of active HDM ranges this device uses. 96*560f7855SBen Widawsky * @dvsec_range: cached attributes of the ranges in the DVSEC, PCIE_DEVICE 97*560f7855SBen Widawsky */ 98*560f7855SBen Widawsky struct cxl_endpoint_dvsec_info { 99*560f7855SBen Widawsky bool mem_enabled; 100*560f7855SBen Widawsky int ranges; 101*560f7855SBen Widawsky struct range dvsec_range[2]; 102*560f7855SBen Widawsky }; 103*560f7855SBen Widawsky 104*560f7855SBen Widawsky /** 1055e2411aeSIra Weiny * struct cxl_dev_state - The driver device state 1065e2411aeSIra Weiny * 1075e2411aeSIra Weiny * cxl_dev_state represents the CXL driver/device state. It provides an 1085e2411aeSIra Weiny * interface to mailbox commands as well as some cached data about the device. 1095e2411aeSIra Weiny * Currently only memory devices are represented. 1105e2411aeSIra Weiny * 1115e2411aeSIra Weiny * @dev: The device associated with this CXL state 1125161a55cSBen Widawsky * @regs: Parsed register blocks 11306e279e5SBen Widawsky * @cxl_dvsec: Offset to the PCIe device DVSEC 1145161a55cSBen Widawsky * @payload_size: Size of space for payload 1155161a55cSBen Widawsky * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register) 1165161a55cSBen Widawsky * @lsa_size: Size of Label Storage Area 1175161a55cSBen Widawsky * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device) 1185161a55cSBen Widawsky * @mbox_mutex: Mutex to synchronize mailbox access. 1195161a55cSBen Widawsky * @firmware_version: Firmware version for the memory device. 1205161a55cSBen Widawsky * @enabled_cmds: Hardware commands found enabled in CEL. 12112f3856aSDan Williams * @exclusive_cmds: Commands that are kernel-internal only 12213e7749dSDan Williams * @pmem_range: Active Persistent memory capacity configuration 12313e7749dSDan Williams * @ram_range: Active Volatile memory capacity configuration 12413e7749dSDan Williams * @total_bytes: sum of all possible capacities 12513e7749dSDan Williams * @volatile_only_bytes: hard volatile capacity 12613e7749dSDan Williams * @persistent_only_bytes: hard persistent capacity 12713e7749dSDan Williams * @partition_align_bytes: alignment size for partition-able capacity 12813e7749dSDan Williams * @active_volatile_bytes: sum of hard + soft volatile 12913e7749dSDan Williams * @active_persistent_bytes: sum of hard + soft persistent 13013e7749dSDan Williams * @next_volatile_bytes: volatile capacity change pending device reset 13113e7749dSDan Williams * @next_persistent_bytes: persistent capacity change pending device reset 1324112a08dSBen Widawsky * @component_reg_phys: register base of component registers 133*560f7855SBen Widawsky * @info: Cached DVSEC information about the device. 134b64955a9SDan Williams * @mbox_send: @dev specific transport for transmitting mailbox commands 13513e7749dSDan Williams * 13613e7749dSDan Williams * See section 8.2.9.5.2 Capacity Configuration and Label Storage for 13713e7749dSDan Williams * details on capacity parameters. 1385161a55cSBen Widawsky */ 1395e2411aeSIra Weiny struct cxl_dev_state { 14099e222a5SDan Williams struct device *dev; 1415161a55cSBen Widawsky 1425161a55cSBen Widawsky struct cxl_regs regs; 14306e279e5SBen Widawsky int cxl_dvsec; 1445161a55cSBen Widawsky 1455161a55cSBen Widawsky size_t payload_size; 1465161a55cSBen Widawsky size_t lsa_size; 1475161a55cSBen Widawsky struct mutex mbox_mutex; /* Protects device mailbox and firmware */ 1485161a55cSBen Widawsky char firmware_version[0x10]; 149ff56ab9eSDan Williams DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX); 15012f3856aSDan Williams DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX); 1515161a55cSBen Widawsky 1525161a55cSBen Widawsky struct range pmem_range; 1535161a55cSBen Widawsky struct range ram_range; 1540b9159d0SIra Weiny u64 total_bytes; 1550b9159d0SIra Weiny u64 volatile_only_bytes; 1560b9159d0SIra Weiny u64 persistent_only_bytes; 1570b9159d0SIra Weiny u64 partition_align_bytes; 158f847502aSIra Weiny 159f847502aSIra Weiny u64 active_volatile_bytes; 160f847502aSIra Weiny u64 active_persistent_bytes; 161f847502aSIra Weiny u64 next_volatile_bytes; 162f847502aSIra Weiny u64 next_persistent_bytes; 163b64955a9SDan Williams 1644112a08dSBen Widawsky resource_size_t component_reg_phys; 165*560f7855SBen Widawsky struct cxl_endpoint_dvsec_info info; 1664112a08dSBen Widawsky 1675e2411aeSIra Weiny int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd); 1685161a55cSBen Widawsky }; 1694faf31b4SDan Williams 1704faf31b4SDan Williams enum cxl_opcode { 1714faf31b4SDan Williams CXL_MBOX_OP_INVALID = 0x0000, 1724faf31b4SDan Williams CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID, 1734faf31b4SDan Williams CXL_MBOX_OP_GET_FW_INFO = 0x0200, 1744faf31b4SDan Williams CXL_MBOX_OP_ACTIVATE_FW = 0x0202, 1754faf31b4SDan Williams CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400, 1764faf31b4SDan Williams CXL_MBOX_OP_GET_LOG = 0x0401, 1774faf31b4SDan Williams CXL_MBOX_OP_IDENTIFY = 0x4000, 1784faf31b4SDan Williams CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100, 1794faf31b4SDan Williams CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101, 1804faf31b4SDan Williams CXL_MBOX_OP_GET_LSA = 0x4102, 1814faf31b4SDan Williams CXL_MBOX_OP_SET_LSA = 0x4103, 1824faf31b4SDan Williams CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200, 1834faf31b4SDan Williams CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201, 1844faf31b4SDan Williams CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202, 1854faf31b4SDan Williams CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203, 1864faf31b4SDan Williams CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204, 1874faf31b4SDan Williams CXL_MBOX_OP_GET_POISON = 0x4300, 1884faf31b4SDan Williams CXL_MBOX_OP_INJECT_POISON = 0x4301, 1894faf31b4SDan Williams CXL_MBOX_OP_CLEAR_POISON = 0x4302, 1904faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303, 1914faf31b4SDan Williams CXL_MBOX_OP_SCAN_MEDIA = 0x4304, 1924faf31b4SDan Williams CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305, 1934faf31b4SDan Williams CXL_MBOX_OP_MAX = 0x10000 1944faf31b4SDan Williams }; 1954faf31b4SDan Williams 19649be6dd8SDan Williams #define DEFINE_CXL_CEL_UUID \ 19749be6dd8SDan Williams UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \ 19849be6dd8SDan Williams 0x3b, 0x3f, 0x17) 19949be6dd8SDan Williams 20049be6dd8SDan Williams #define DEFINE_CXL_VENDOR_DEBUG_UUID \ 20149be6dd8SDan Williams UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \ 20249be6dd8SDan Williams 0x40, 0x3d, 0x86) 20349be6dd8SDan Williams 20449be6dd8SDan Williams struct cxl_mbox_get_supported_logs { 20549be6dd8SDan Williams __le16 entries; 20649be6dd8SDan Williams u8 rsvd[6]; 20749be6dd8SDan Williams struct cxl_gsl_entry { 20849be6dd8SDan Williams uuid_t uuid; 20949be6dd8SDan Williams __le32 size; 21049be6dd8SDan Williams } __packed entry[]; 21149be6dd8SDan Williams } __packed; 21249be6dd8SDan Williams 21349be6dd8SDan Williams struct cxl_cel_entry { 21449be6dd8SDan Williams __le16 opcode; 21549be6dd8SDan Williams __le16 effect; 21649be6dd8SDan Williams } __packed; 21749be6dd8SDan Williams 21849be6dd8SDan Williams struct cxl_mbox_get_log { 21949be6dd8SDan Williams uuid_t uuid; 22049be6dd8SDan Williams __le32 offset; 22149be6dd8SDan Williams __le32 length; 22249be6dd8SDan Williams } __packed; 22349be6dd8SDan Williams 22449be6dd8SDan Williams /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 22549be6dd8SDan Williams struct cxl_mbox_identify { 22649be6dd8SDan Williams char fw_revision[0x10]; 22749be6dd8SDan Williams __le64 total_capacity; 22849be6dd8SDan Williams __le64 volatile_capacity; 22949be6dd8SDan Williams __le64 persistent_capacity; 23049be6dd8SDan Williams __le64 partition_align; 23149be6dd8SDan Williams __le16 info_event_log_size; 23249be6dd8SDan Williams __le16 warning_event_log_size; 23349be6dd8SDan Williams __le16 failure_event_log_size; 23449be6dd8SDan Williams __le16 fatal_event_log_size; 23549be6dd8SDan Williams __le32 lsa_size; 23649be6dd8SDan Williams u8 poison_list_max_mer[3]; 23749be6dd8SDan Williams __le16 inject_poison_limit; 23849be6dd8SDan Williams u8 poison_caps; 23949be6dd8SDan Williams u8 qos_telemetry_caps; 24049be6dd8SDan Williams } __packed; 24149be6dd8SDan Williams 24249be6dd8SDan Williams struct cxl_mbox_get_lsa { 24349be6dd8SDan Williams u32 offset; 24449be6dd8SDan Williams u32 length; 24549be6dd8SDan Williams } __packed; 24649be6dd8SDan Williams 24749be6dd8SDan Williams struct cxl_mbox_set_lsa { 24849be6dd8SDan Williams u32 offset; 24949be6dd8SDan Williams u32 reserved; 25049be6dd8SDan Williams u8 data[]; 25149be6dd8SDan Williams } __packed; 25249be6dd8SDan Williams 2534faf31b4SDan Williams /** 2544faf31b4SDan Williams * struct cxl_mem_command - Driver representation of a memory device command 2554faf31b4SDan Williams * @info: Command information as it exists for the UAPI 2564faf31b4SDan Williams * @opcode: The actual bits used for the mailbox protocol 2574faf31b4SDan Williams * @flags: Set of flags effecting driver behavior. 2584faf31b4SDan Williams * 2594faf31b4SDan Williams * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag 2604faf31b4SDan Williams * will be enabled by the driver regardless of what hardware may have 2614faf31b4SDan Williams * advertised. 2624faf31b4SDan Williams * 2634faf31b4SDan Williams * The cxl_mem_command is the driver's internal representation of commands that 2644faf31b4SDan Williams * are supported by the driver. Some of these commands may not be supported by 2654faf31b4SDan Williams * the hardware. The driver will use @info to validate the fields passed in by 2664faf31b4SDan Williams * the user then submit the @opcode to the hardware. 2674faf31b4SDan Williams * 2684faf31b4SDan Williams * See struct cxl_command_info. 2694faf31b4SDan Williams */ 2704faf31b4SDan Williams struct cxl_mem_command { 2714faf31b4SDan Williams struct cxl_command_info info; 2724faf31b4SDan Williams enum cxl_opcode opcode; 2734faf31b4SDan Williams u32 flags; 2744faf31b4SDan Williams #define CXL_CMD_FLAG_NONE 0 2754faf31b4SDan Williams #define CXL_CMD_FLAG_FORCE_ENABLE BIT(0) 2764faf31b4SDan Williams }; 2774faf31b4SDan Williams 2785e2411aeSIra Weiny int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in, 2794faf31b4SDan Williams size_t in_size, void *out, size_t out_size); 2805e2411aeSIra Weiny int cxl_dev_state_identify(struct cxl_dev_state *cxlds); 2815e2411aeSIra Weiny int cxl_enumerate_cmds(struct cxl_dev_state *cxlds); 2825e2411aeSIra Weiny int cxl_mem_create_range_info(struct cxl_dev_state *cxlds); 2835e2411aeSIra Weiny struct cxl_dev_state *cxl_dev_state_create(struct device *dev); 2845e2411aeSIra Weiny void set_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds); 2855e2411aeSIra Weiny void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds); 286d17d0540SDan Williams 287d17d0540SDan Williams struct cxl_hdm { 288d17d0540SDan Williams struct cxl_component_regs regs; 289d17d0540SDan Williams unsigned int decoder_count; 290d17d0540SDan Williams unsigned int target_count; 291d17d0540SDan Williams unsigned int interleave_mask; 292d17d0540SDan Williams struct cxl_port *port; 293d17d0540SDan Williams }; 2945161a55cSBen Widawsky #endif /* __CXL_MEM_H__ */ 295