blob: c4f450ad434d239d0f435dbc5993b729917648c1 [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
36 * @cxlm: pointer to the parent device driver data
37 * @id: id number of this memdev instance.
38 */
39struct cxl_memdev {
40 struct device dev;
41 struct cdev cdev;
42 struct cxl_mem *cxlm;
43 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
Dan Williams4faf31b2021-09-08 22:12:32 -070051struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm);
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/**
Dan Williams5f50d6b2021-05-13 22:21:49 -070093 * struct cxl_mem - A CXL memory device
Dan Williams99e222a2021-09-08 22:12:09 -070094 * @dev: The device associated with this CXL device.
Dan Williams8ac75dd2021-05-13 22:21:54 -070095 * @cxlmd: Logical memory device chardev / interface
96 * @regs: Parsed register blocks
Dan Williams5f50d6b2021-05-13 22:21:49 -070097 * @payload_size: Size of space for payload
98 * (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
Vishal Verma199cf8c2021-05-20 13:47:45 -060099 * @lsa_size: Size of Label Storage Area
100 * (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
Dan Williams5f50d6b2021-05-13 22:21:49 -0700101 * @mbox_mutex: Mutex to synchronize mailbox access.
102 * @firmware_version: Firmware version for the memory device.
103 * @enabled_cmds: Hardware commands found enabled in CEL.
Dan Williams12f38562021-09-14 12:03:04 -0700104 * @exclusive_cmds: Commands that are kernel-internal only
Dan Williams13e77492021-09-13 15:24:32 -0700105 * @pmem_range: Active Persistent memory capacity configuration
106 * @ram_range: Active Volatile memory capacity configuration
107 * @total_bytes: sum of all possible capacities
108 * @volatile_only_bytes: hard volatile capacity
109 * @persistent_only_bytes: hard persistent capacity
110 * @partition_align_bytes: alignment size for partition-able capacity
111 * @active_volatile_bytes: sum of hard + soft volatile
112 * @active_persistent_bytes: sum of hard + soft persistent
113 * @next_volatile_bytes: volatile capacity change pending device reset
114 * @next_persistent_bytes: persistent capacity change pending device reset
Dan Williamsb64955a2021-09-08 22:12:21 -0700115 * @mbox_send: @dev specific transport for transmitting mailbox commands
Dan Williams13e77492021-09-13 15:24:32 -0700116 *
117 * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
118 * details on capacity parameters.
Dan Williams5f50d6b2021-05-13 22:21:49 -0700119 */
120struct cxl_mem {
Dan Williams99e222a2021-09-08 22:12:09 -0700121 struct device *dev;
Dan Williams5f50d6b2021-05-13 22:21:49 -0700122 struct cxl_memdev *cxlmd;
123
Dan Williams8ac75dd2021-05-13 22:21:54 -0700124 struct cxl_regs regs;
Dan Williams5f50d6b2021-05-13 22:21:49 -0700125
126 size_t payload_size;
Vishal Verma199cf8c2021-05-20 13:47:45 -0600127 size_t lsa_size;
Dan Williams5f50d6b2021-05-13 22:21:49 -0700128 struct mutex mbox_mutex; /* Protects device mailbox and firmware */
129 char firmware_version[0x10];
Dan Williamsff56ab92021-09-08 22:12:44 -0700130 DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
Dan Williams12f38562021-09-14 12:03:04 -0700131 DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
Dan Williams5f50d6b2021-05-13 22:21:49 -0700132
133 struct range pmem_range;
134 struct range ram_range;
Ira Weiny0b9159d2021-06-17 15:16:18 -0700135 u64 total_bytes;
136 u64 volatile_only_bytes;
137 u64 persistent_only_bytes;
138 u64 partition_align_bytes;
Ira Weinyf8475022021-08-10 11:57:59 -0700139
140 u64 active_volatile_bytes;
141 u64 active_persistent_bytes;
142 u64 next_volatile_bytes;
143 u64 next_persistent_bytes;
Dan Williamsb64955a2021-09-08 22:12:21 -0700144
145 int (*mbox_send)(struct cxl_mem *cxlm, struct cxl_mbox_cmd *cmd);
Dan Williams5f50d6b2021-05-13 22:21:49 -0700146};
Dan Williams4faf31b2021-09-08 22:12:32 -0700147
148enum cxl_opcode {
149 CXL_MBOX_OP_INVALID = 0x0000,
150 CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID,
151 CXL_MBOX_OP_GET_FW_INFO = 0x0200,
152 CXL_MBOX_OP_ACTIVATE_FW = 0x0202,
153 CXL_MBOX_OP_GET_SUPPORTED_LOGS = 0x0400,
154 CXL_MBOX_OP_GET_LOG = 0x0401,
155 CXL_MBOX_OP_IDENTIFY = 0x4000,
156 CXL_MBOX_OP_GET_PARTITION_INFO = 0x4100,
157 CXL_MBOX_OP_SET_PARTITION_INFO = 0x4101,
158 CXL_MBOX_OP_GET_LSA = 0x4102,
159 CXL_MBOX_OP_SET_LSA = 0x4103,
160 CXL_MBOX_OP_GET_HEALTH_INFO = 0x4200,
161 CXL_MBOX_OP_GET_ALERT_CONFIG = 0x4201,
162 CXL_MBOX_OP_SET_ALERT_CONFIG = 0x4202,
163 CXL_MBOX_OP_GET_SHUTDOWN_STATE = 0x4203,
164 CXL_MBOX_OP_SET_SHUTDOWN_STATE = 0x4204,
165 CXL_MBOX_OP_GET_POISON = 0x4300,
166 CXL_MBOX_OP_INJECT_POISON = 0x4301,
167 CXL_MBOX_OP_CLEAR_POISON = 0x4302,
168 CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS = 0x4303,
169 CXL_MBOX_OP_SCAN_MEDIA = 0x4304,
170 CXL_MBOX_OP_GET_SCAN_MEDIA = 0x4305,
171 CXL_MBOX_OP_MAX = 0x10000
172};
173
Dan Williams49be6dd2021-09-08 22:13:15 -0700174#define DEFINE_CXL_CEL_UUID \
175 UUID_INIT(0xda9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79, 0x96, 0xb1, 0x62, \
176 0x3b, 0x3f, 0x17)
177
178#define DEFINE_CXL_VENDOR_DEBUG_UUID \
179 UUID_INIT(0xe1819d9, 0x11a9, 0x400c, 0x81, 0x1f, 0xd6, 0x07, 0x19, \
180 0x40, 0x3d, 0x86)
181
182struct cxl_mbox_get_supported_logs {
183 __le16 entries;
184 u8 rsvd[6];
185 struct cxl_gsl_entry {
186 uuid_t uuid;
187 __le32 size;
188 } __packed entry[];
189} __packed;
190
191struct cxl_cel_entry {
192 __le16 opcode;
193 __le16 effect;
194} __packed;
195
196struct cxl_mbox_get_log {
197 uuid_t uuid;
198 __le32 offset;
199 __le32 length;
200} __packed;
201
202/* See CXL 2.0 Table 175 Identify Memory Device Output Payload */
203struct cxl_mbox_identify {
204 char fw_revision[0x10];
205 __le64 total_capacity;
206 __le64 volatile_capacity;
207 __le64 persistent_capacity;
208 __le64 partition_align;
209 __le16 info_event_log_size;
210 __le16 warning_event_log_size;
211 __le16 failure_event_log_size;
212 __le16 fatal_event_log_size;
213 __le32 lsa_size;
214 u8 poison_list_max_mer[3];
215 __le16 inject_poison_limit;
216 u8 poison_caps;
217 u8 qos_telemetry_caps;
218} __packed;
219
220struct cxl_mbox_get_lsa {
221 u32 offset;
222 u32 length;
223} __packed;
224
225struct cxl_mbox_set_lsa {
226 u32 offset;
227 u32 reserved;
228 u8 data[];
229} __packed;
230
Dan Williams4faf31b2021-09-08 22:12:32 -0700231/**
232 * struct cxl_mem_command - Driver representation of a memory device command
233 * @info: Command information as it exists for the UAPI
234 * @opcode: The actual bits used for the mailbox protocol
235 * @flags: Set of flags effecting driver behavior.
236 *
237 * * %CXL_CMD_FLAG_FORCE_ENABLE: In cases of error, commands with this flag
238 * will be enabled by the driver regardless of what hardware may have
239 * advertised.
240 *
241 * The cxl_mem_command is the driver's internal representation of commands that
242 * are supported by the driver. Some of these commands may not be supported by
243 * the hardware. The driver will use @info to validate the fields passed in by
244 * the user then submit the @opcode to the hardware.
245 *
246 * See struct cxl_command_info.
247 */
248struct cxl_mem_command {
249 struct cxl_command_info info;
250 enum cxl_opcode opcode;
251 u32 flags;
252#define CXL_CMD_FLAG_NONE 0
253#define CXL_CMD_FLAG_FORCE_ENABLE BIT(0)
254};
255
256int cxl_mem_mbox_send_cmd(struct cxl_mem *cxlm, u16 opcode, void *in,
257 size_t in_size, void *out, size_t out_size);
258int cxl_mem_identify(struct cxl_mem *cxlm);
259int cxl_mem_enumerate_cmds(struct cxl_mem *cxlm);
260int cxl_mem_create_range_info(struct cxl_mem *cxlm);
261struct cxl_mem *cxl_mem_create(struct device *dev);
Dan Williams12f38562021-09-14 12:03:04 -0700262void set_exclusive_cxl_commands(struct cxl_mem *cxlm, unsigned long *cmds);
263void clear_exclusive_cxl_commands(struct cxl_mem *cxlm, unsigned long *cmds);
Dan Williams5f50d6b2021-05-13 22:21:49 -0700264#endif /* __CXL_MEM_H__ */