xref: /linux/include/cxl/cxl.h (revision c801107605f39b5a8632fbc835e0da65a6fb6b51)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2020 Intel Corporation. */
3 /* Copyright(c) 2026 Advanced Micro Devices, Inc. */
4 
5 #ifndef __CXL_CXL_H__
6 #define __CXL_CXL_H__
7 
8 #include <linux/node.h>
9 #include <linux/ioport.h>
10 #include <cxl/mailbox.h>
11 
12 /**
13  * enum cxl_devtype - delineate type-2 from a generic type-3 device
14  * @CXL_DEVTYPE_DEVMEM: Vendor specific CXL Type-2 device implementing HDM-D or
15  *			 HDM-DB, no requirement that this device implements a
16  *			 mailbox, or other memory-device-standard manageability
17  *			 flows.
18  * @CXL_DEVTYPE_CLASSMEM: Common class definition of a CXL Type-3 device with
19  *			   HDM-H and class-mandatory memory device registers
20  */
21 enum cxl_devtype {
22 	CXL_DEVTYPE_DEVMEM,
23 	CXL_DEVTYPE_CLASSMEM,
24 };
25 
26 struct device;
27 
28 /*
29  * Using struct_group() allows for per register-block-type helper routines,
30  * without requiring block-type agnostic code to include the prefix.
31  */
32 struct cxl_regs {
33 	/*
34 	 * Common set of CXL Component register block base pointers
35 	 * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
36 	 * @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure
37 	 */
38 	struct_group_tagged(cxl_component_regs, component,
39 		void __iomem *hdm_decoder;
40 		void __iomem *ras;
41 	);
42 	/*
43 	 * Common set of CXL Device register block base pointers
44 	 * @status: CXL 2.0 8.2.8.3 Device Status Registers
45 	 * @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
46 	 * @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
47 	 */
48 	struct_group_tagged(cxl_device_regs, device_regs,
49 		void __iomem *status, *mbox, *memdev;
50 	);
51 
52 	struct_group_tagged(cxl_pmu_regs, pmu_regs,
53 		void __iomem *pmu;
54 	);
55 
56 	/*
57 	 * RCH downstream port specific RAS register
58 	 * @aer: CXL 3.0 8.2.1.1 RCH Downstream Port RCRB
59 	 */
60 	struct_group_tagged(cxl_rch_regs, rch_regs,
61 		void __iomem *dport_aer;
62 	);
63 
64 	/*
65 	 * RCD upstream port specific PCIe cap register
66 	 * @pcie_cap: CXL 3.0 8.2.1.2 RCD Upstream Port RCRB
67 	 */
68 	struct_group_tagged(cxl_rcd_regs, rcd_regs,
69 		void __iomem *rcd_pcie_cap;
70 	);
71 };
72 
73 struct cxl_reg_map {
74 	bool valid;
75 	int id;
76 	unsigned long offset;
77 	unsigned long size;
78 };
79 
80 struct cxl_component_reg_map {
81 	struct cxl_reg_map hdm_decoder;
82 	struct cxl_reg_map ras;
83 };
84 
85 struct cxl_device_reg_map {
86 	struct cxl_reg_map status;
87 	struct cxl_reg_map mbox;
88 	struct cxl_reg_map memdev;
89 };
90 
91 struct cxl_pmu_reg_map {
92 	struct cxl_reg_map pmu;
93 };
94 
95 /**
96  * struct cxl_register_map - DVSEC harvested register block mapping parameters
97  * @host: device for devm operations and logging
98  * @base: virtual base of the register-block-BAR + @block_offset
99  * @resource: physical resource base of the register block
100  * @max_size: maximum mapping size to perform register search
101  * @reg_type: see enum cxl_regloc_type
102  * @component_map: cxl_reg_map for component registers
103  * @device_map: cxl_reg_maps for device registers
104  * @pmu_map: cxl_reg_maps for CXL Performance Monitoring Units
105  */
106 struct cxl_register_map {
107 	struct device *host;
108 	void __iomem *base;
109 	resource_size_t resource;
110 	resource_size_t max_size;
111 	u8 reg_type;
112 	union {
113 		struct cxl_component_reg_map component_map;
114 		struct cxl_device_reg_map device_map;
115 		struct cxl_pmu_reg_map pmu_map;
116 	};
117 };
118 
119 /**
120  * struct cxl_dpa_perf - DPA performance property entry
121  * @dpa_range: range for DPA address
122  * @coord: QoS performance data (i.e. latency, bandwidth)
123  * @cdat_coord: raw QoS performance data from CDAT
124  * @qos_class: QoS Class cookies
125  */
126 struct cxl_dpa_perf {
127 	struct range dpa_range;
128 	struct access_coordinate coord[ACCESS_COORDINATE_MAX];
129 	struct access_coordinate cdat_coord[ACCESS_COORDINATE_MAX];
130 	int qos_class;
131 };
132 
133 /*
134  * Partitions share one flat DPA space in a fixed order:
135  *
136  *   DPA 0                                                     end
137  *   +---------------+---------------+---------------------------+
138  *   |      ram      |     pmem      |       dynamic_ram_1       |
139  *   +---------------+---------------+---------------------------+
140  *     part[0]         part[1]         part[2]
141  *
142  * The static ram and pmem partition order is mandated by CXL r4.0
143  * 8.2.10.9.2.1 "Get Partition Info" (opcode 4100h), Table 8-310: volatile
144  * capacity starts at DPA 0 and persistent capacity starts at the DPA
145  * immediately following it.  A dynamic partition has no such
146  * requirement -- 8.2.10.9.9.1 "Get Dynamic Capacity Configuration"
147  * (opcode 4800h), Table 8-347, only requires the DC Region Base to be
148  * 256MB aligned -- but Linux follows that precedent and requires it to
149  * start at the DPA immediately following static capacity: after pmem,
150  * after ram on a device with no pmem, or at DPA 0 on a device with no
151  * static capacity at all.  cxl_configure_dcd() enforces it.
152  */
153 enum cxl_partition_mode {
154 	CXL_PARTMODE_RAM,
155 	CXL_PARTMODE_PMEM,
156 	CXL_PARTMODE_DYNAMIC_RAM,
157 };
158 
159 /**
160  * struct cxl_dpa_partition - DPA partition descriptor
161  * @res: shortcut to the partition in the DPA resource tree (cxlds->dpa_res)
162  * @perf: performance attributes of the partition from CDAT
163  * @mode: operation mode for the DPA capacity, e.g. ram, pmem, dynamic...
164  * @handle: DSMAS handle that represents this partition
165  * @shareable: Is the partition shareable (from its CDAT DSMAS entry)
166  * @read_only: Is the partition read-only (from its CDAT DSMAS entry)
167  */
168 struct cxl_dpa_partition {
169 	struct resource res;
170 	struct cxl_dpa_perf perf;
171 	enum cxl_partition_mode mode;
172 	u8 handle;
173 	bool shareable;
174 	bool read_only;
175 };
176 
177 #define CXL_NR_PARTITIONS_MAX 3
178 
179 /**
180  * struct cxl_dev_state - The driver device state
181  *
182  * cxl_dev_state represents the CXL driver/device state.  It provides an
183  * interface to mailbox commands as well as some cached data about the device.
184  * Currently only memory devices are represented.
185  *
186  * @dev: The device associated with this CXL state
187  * @cxlmd: The device representing the CXL.mem capabilities of @dev
188  * @reg_map: component and ras register mapping parameters
189  * @regs: Parsed register blocks
190  * @cxl_dvsec: Offset to the PCIe device DVSEC
191  * @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)
192  * @media_ready: Indicate whether the device media is usable
193  * @dpa_res: Overall DPA resource tree for the device
194  * @part: DPA partition array
195  * @nr_partitions: Number of DPA partitions
196  * @serial: PCIe Device Serial Number
197  * @type: Generic Memory Class device or Vendor Specific Memory device
198  * @cxl_mbox: CXL mailbox context
199  * @cxlfs: CXL features context
200  */
201 struct cxl_dev_state {
202 	/* public for Type2 drivers */
203 	struct device *dev;
204 	struct cxl_memdev *cxlmd;
205 
206 	/* private for Type2 drivers */
207 	struct cxl_register_map reg_map;
208 	struct cxl_device_regs regs;
209 	int cxl_dvsec;
210 	bool rcd;
211 	bool media_ready;
212 	struct resource dpa_res;
213 	struct cxl_dpa_partition part[CXL_NR_PARTITIONS_MAX];
214 	unsigned int nr_partitions;
215 	u64 serial;
216 	enum cxl_devtype type;
217 	struct cxl_mailbox cxl_mbox;
218 #ifdef CONFIG_CXL_FEATURES
219 	struct cxl_features_state *cxlfs;
220 #endif
221 };
222 
223 struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
224 						 enum cxl_devtype type,
225 						 u64 serial, u16 dvsec,
226 						 size_t size, bool has_mbox);
227 
228 /**
229  * cxl_dev_state_create - safely create and cast a cxl dev state embedded in a
230  * driver specific struct.
231  *
232  * @parent: device behind the request
233  * @type: CXL device type
234  * @serial: device identification
235  * @dvsec: dvsec capability offset
236  * @drv_struct: driver struct embedding a cxl_dev_state struct
237  * @member: name of the struct cxl_dev_state member in drv_struct
238  * @mbox: true if mailbox supported
239  *
240  * Returns a pointer to the drv_struct allocated and embedding a cxl_dev_state
241  * struct initialized.
242  *
243  * Introduced for Type2 driver support.
244  */
245 #define devm_cxl_dev_state_create(parent, type, serial, dvsec, drv_struct, member, mbox)	\
246 	({										\
247 		static_assert(__same_type(struct cxl_dev_state,				\
248 			      ((drv_struct *)NULL)->member));				\
249 		static_assert(offsetof(drv_struct, member) == 0);			\
250 		(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec,	\
251 						      sizeof(drv_struct), mbox);	\
252 	})
253 
254 struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds,
255 				      struct range *range);
256 
257 int cxl_set_capacity(struct cxl_dev_state *cxlds, u64 capacity);
258 #endif /* __CXL_CXL_H__ */
259