blob: 8d96d009ad90708523a9f3360675289ee55fb202 [file] [log] [blame]
Dan Williams5f50d6b2021-05-13 22:21:49 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright(c) 2020-2021 Intel Corporation. */
3#ifndef __CXL_MEM_H__
4#define __CXL_MEM_H__
Dan Williams4faf31b2021-09-08 22:12:32 -07005#include <uapi/linux/cxl_mem.h>
Dan Williams21083f52021-06-15 16:36:31 -07006#include <linux/cdev.h>
7#include "cxl.h"
Dan Williams5f50d6b2021-05-13 22:21:49 -07008
9/* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
10#define CXLMDEV_STATUS_OFFSET 0x0
11#define CXLMDEV_DEV_FATAL BIT(0)
12#define CXLMDEV_FW_HALT BIT(1)
13#define CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
14#define CXLMDEV_MS_NOT_READY 0
15#define CXLMDEV_MS_READY 1
16#define CXLMDEV_MS_ERROR 2
17#define CXLMDEV_MS_DISABLED 3
18#define CXLMDEV_READY(status) \
19 (FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) == \
20 CXLMDEV_MS_READY)
21#define CXLMDEV_MBOX_IF_READY BIT(4)
22#define CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
23#define CXLMDEV_RESET_NEEDED_NOT 0
24#define CXLMDEV_RESET_NEEDED_COLD 1
25#define CXLMDEV_RESET_NEEDED_WARM 2
26#define CXLMDEV_RESET_NEEDED_HOT 3
27#define CXLMDEV_RESET_NEEDED_CXL 4
28#define CXLMDEV_RESET_NEEDED(status) \
29 (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \
30 CXLMDEV_RESET_NEEDED_NOT)
31
Dan Williams5f50d6b2021-05-13 22:21:49 -070032/**
33 * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
34 * @dev: driver core device object
35 * @cdev: char dev core object for ioctl operations
Ira Weiny5e2411a2021-11-02 13:29:01 -070036 * @cxlds: The device state backing this device
Dan Williams5f50d6b2021-05-13 22:21:49 -070037 * @id: id number of this memdev instance.
38 */
39struct cxl_memdev {
40 struct device dev;
41 struct cdev cdev;
Ira Weiny5e2411a2021-11-02 13:29:01 -070042 struct cxl_dev_state *cxlds;
Dan Williams5f50d6b2021-05-13 22:21:49 -070043 int id;
44};
45
Ben Widawsky3d135db2021-08-02 10:30:05 -070046static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
47{
48 return container_of(dev, struct cxl_memdev, dev);
49}
50
Ira Weiny5e2411a2021-11-02 13:29:01 -070051struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds);
Ben Widawsky3d135db2021-08-02 10:30:05 -070052
Dan Williams5f50d6b2021-05-13 22:21:49 -070053/**
Dan Williamsb64955a2021-09-08 22:12:21 -070054 * struct cxl_mbox_cmd - A command to be submitted to hardware.
55 * @opcode: (input) The command set and command submitted to hardware.
56 * @payload_in: (input) Pointer to the input payload.
57 * @payload_out: (output) Pointer to the output payload. Must be allocated by
58 * the caller.
59 * @size_in: (input) Number of bytes to load from @payload_in.
60 * @size_out: (input) Max number of bytes loaded into @payload_out.
61 * (output) Number of bytes generated by the device. For fixed size
62 * outputs commands this is always expected to be deterministic. For
63 * variable sized output commands, it tells the exact number of bytes
64 * written.
65 * @return_code: (output) Error code returned from hardware.
66 *
67 * This is the primary mechanism used to send commands to the hardware.
68 * All the fields except @payload_* correspond exactly to the fields described in
69 * Command Register section of the CXL 2.0 8.2.8.4.5. @payload_in and
70 * @payload_out are written to, and read from the Command Payload Registers
71 * defined in CXL 2.0 8.2.8.4.8.
72 */
73struct cxl_mbox_cmd {
74 u16 opcode;
75 void *payload_in;
76 void *payload_out;
77 size_t size_in;
78 size_t size_out;
79 u16 return_code;
80#define CXL_MBOX_SUCCESS 0
81};
82
83/*
84 * CXL 2.0 - Memory capacity multiplier
85 * See Section 8.2.9.5
86 *
87 * Volatile, Persistent, and Partition capacities are specified to be in
88 * multiples of 256MB - define a multiplier to convert to/from bytes.
89 */
90#define CXL_CAPACITY_MULTIPLIER SZ_256M
91
92/**
Ira Weiny5e2411a2021-11-02 13:29:01 -070093 * struct cxl_dev_state - The driver device state
94 *
95 * cxl_dev_state represents the CXL driver/device state. It provides an
96 * interface to mailbox commands as well as some cached data about the device.
97 * Currently only memory devices are represented.
98 *
99 * @dev: The device associated with this CXL state
Dan Williams8ac75dd2021-05-13 22:21:54 -0700100 * @regs: Parsed register blocks
Dan Williams5f50d6b2021-05-13 22:21:49 -0700101 * @payload_size: Size of space for payload
102 * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
Vishal Verma199cf8c2021-05-20 13:47:45 -0600103 * @lsa_size: Size of Label Storage Area
104 * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
Dan Williams5f50d6b2021-05-13 22:21:49 -0700105 * @mbox_mutex: Mutex to synchronize mailbox access.
106 * @firmware_version: Firmware version for the memory device.
107 * @enabled_cmds: Hardware commands found enabled in CEL.
Dan Williams12f38562021-09-14 12:03:04 -0700108 * @exclusive_cmds: Commands that are kernel-internal only
Dan Williams13e77492021-09-13 15:24:32 -0700109 * @pmem_range: Active Persistent memory capacity configuration
110 * @ram_range: Active Volatile memory capacity configuration
111 * @total_bytes: sum of all possible capacities
112 * @volatile_only_bytes: hard volatile capacity
113 * @persistent_only_bytes: hard persistent capacity
114 * @partition_align_bytes: alignment size for partition-able capacity
115 * @active_volatile_bytes: sum of hard + soft volatile
116 * @active_persistent_bytes: sum of hard + soft persistent
117 * @next_volatile_bytes: volatile capacity change pending device reset
118 * @next_persistent_bytes: persistent capacity change pending device reset
Dan Williamsb64955a2021-09-08 22:12:21 -0700119 * @mbox_send: @dev specific transport for transmitting mailbox commands
Dan Williams13e77492021-09-13 15:24:32 -0700120 *
121 * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
122 * details on capacity parameters.
Dan Williams5f50d6b2021-05-13 22:21:49 -0700123 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700124struct cxl_dev_state {
Dan Williams99e222a2021-09-08 22:12:09 -0700125 struct device *dev;
Dan Williams5f50d6b2021-05-13 22:21:49 -0700126
Dan Williams8ac75dd2021-05-13 22:21:54 -0700127 struct cxl_regs regs;
Dan Williams5f50d6b2021-05-13 22:21:49 -0700128
129 size_t payload_size;
Vishal Verma199cf8c2021-05-20 13:47:45 -0600130 size_t lsa_size;
Dan Williams5f50d6b2021-05-13 22:21:49 -0700131 struct mutex mbox_mutex; /* Protects device mailbox and firmware */
132 char firmware_version[0x10];
Dan Williamsff56ab92021-09-08 22:12:44 -0700133 DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
Dan Williams12f38562021-09-14 12:03:04 -0700134 DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
Dan Williams5f50d6b2021-05-13 22:21:49 -0700135
136 struct range pmem_range;
137 struct range ram_range;
Ira Weiny0b9159d2021-06-17 15:16:18 -0700138 u64 total_bytes;
139 u64 volatile_only_bytes;
140 u64 persistent_only_bytes;
141 u64 partition_align_bytes;
Ira Weinyf8475022021-08-10 11:57:59 -0700142
143 u64 active_volatile_bytes;
144 u64 active_persistent_bytes;
145 u64 next_volatile_bytes;
146 u64 next_persistent_bytes;
Dan Williamsb64955a2021-09-08 22:12:21 -0700147
Ira Weiny5e2411a2021-11-02 13:29:01 -0700148 int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
Dan Williams5f50d6b2021-05-13 22:21:49 -0700149};
Dan Williams4faf31b2021-09-08 22:12:32 -0700150
151enum cxl_opcode {
152 CXL_MBOX_OP_INVALID = 0x0000,
153 CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID,
154 CXL_MBOX_OP_GET_FW_INFO = 0x0200,
155 CXL_MBOX_OP_ACTIVATE_FW = 0x0202,
156 CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400,
157 CXL_MBOX_OP_GET_LOG = 0x0401,
158 CXL_MBOX_OP_IDENTIFY = 0x4000,
159 CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100,
160 CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101,
161 CXL_MBOX_OP_GET_LSA = 0x4102,
162 CXL_MBOX_OP_SET_LSA = 0x4103,
163 CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200,
164 CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201,
165 CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202,
166 CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203,
167 CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204,
168 CXL_MBOX_OP_GET_POISON = 0x4300,
169 CXL_MBOX_OP_INJECT_POISON = 0x4301,
170 CXL_MBOX_OP_CLEAR_POISON = 0x4302,
171 CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303,
172 CXL_MBOX_OP_SCAN_MEDIA = 0x4304,
173 CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305,
174 CXL_MBOX_OP_MAX = 0x10000
175};
176
Dan Williams49be6dd2021-09-08 22:13:15 -0700177#define DEFINE_CXL_CEL_UUID \
178 UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \
179 0x3b, 0x3f, 0x17)
180
181#define DEFINE_CXL_VENDOR_DEBUG_UUID \
182 UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \
183 0x40, 0x3d, 0x86)
184
185struct cxl_mbox_get_supported_logs {
186 __le16 entries;
187 u8 rsvd[6];
188 struct cxl_gsl_entry {
189 uuid_t uuid;
190 __le32 size;
191 } __packed entry[];
192} __packed;
193
194struct cxl_cel_entry {
195 __le16 opcode;
196 __le16 effect;
197} __packed;
198
199struct cxl_mbox_get_log {
200 uuid_t uuid;
201 __le32 offset;
202 __le32 length;
203} __packed;
204
205/* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
206struct cxl_mbox_identify {
207 char fw_revision[0x10];
208 __le64 total_capacity;
209 __le64 volatile_capacity;
210 __le64 persistent_capacity;
211 __le64 partition_align;
212 __le16 info_event_log_size;
213 __le16 warning_event_log_size;
214 __le16 failure_event_log_size;
215 __le16 fatal_event_log_size;
216 __le32 lsa_size;
217 u8 poison_list_max_mer[3];
218 __le16 inject_poison_limit;
219 u8 poison_caps;
220 u8 qos_telemetry_caps;
221} __packed;
222
223struct cxl_mbox_get_lsa {
224 u32 offset;
225 u32 length;
226} __packed;
227
228struct cxl_mbox_set_lsa {
229 u32 offset;
230 u32 reserved;
231 u8 data[];
232} __packed;
233
Dan Williams4faf31b2021-09-08 22:12:32 -0700234/**
235 * struct cxl_mem_command - Driver representation of a memory device command
236 * @info: Command information as it exists for the UAPI
237 * @opcode: The actual bits used for the mailbox protocol
238 * @flags: Set of flags effecting driver behavior.
239 *
240 * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
241 * will be enabled by the driver regardless of what hardware may have
242 * advertised.
243 *
244 * The cxl_mem_command is the driver's internal representation of commands that
245 * are supported by the driver. Some of these commands may not be supported by
246 * the hardware. The driver will use @info to validate the fields passed in by
247 * the user then submit the @opcode to the hardware.
248 *
249 * See struct cxl_command_info.
250 */
251struct cxl_mem_command {
252 struct cxl_command_info info;
253 enum cxl_opcode opcode;
254 u32 flags;
255#define CXL_CMD_FLAG_NONE 0
256#define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
257};
258
Ira Weiny5e2411a2021-11-02 13:29:01 -0700259int cxl_mbox_send_cmd(struct cxl_dev_state *cxlds, u16 opcode, void *in,
260 size_t in_size, void *out, size_t out_size);
261int cxl_dev_state_identify(struct cxl_dev_state *cxlds);
262int cxl_enumerate_cmds(struct cxl_dev_state *cxlds);
263int cxl_mem_create_range_info(struct cxl_dev_state *cxlds);
264struct cxl_dev_state *cxl_dev_state_create(struct device *dev);
265void set_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds);
266void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds);
Dan Williams5f50d6b2021-05-13 22:21:49 -0700267#endif /* __CXL_MEM_H__ */