cxl/port: Remove Component Register base address from struct cxl_port
[linux-2.6-block.git] / drivers / cxl / cxl.h
CommitLineData
8adaf747
BW
1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright(c) 2020 Intel Corporation. */
3
4#ifndef __CXL_H__
5#define __CXL_H__
6
8fdcb170 7#include <linux/libnvdimm.h>
8adaf747
BW
8#include <linux/bitfield.h>
9#include <linux/bitops.h>
80d10a6c 10#include <linux/log2.h>
8adaf747
BW
11#include <linux/io.h>
12
4812be97
DW
13/**
14 * DOC: cxl objects
15 *
16 * The CXL core objects like ports, decoders, and regions are shared
17 * between the subsystem drivers cxl_acpi, cxl_pci, and core drivers
18 * (port-driver, region-driver, nvdimm object-drivers... etc).
19 */
20
d17d0540
DW
21/* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
22#define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K
23
08422378
BW
24/* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
25#define CXL_CM_OFFSET 0x1000
26#define CXL_CM_CAP_HDR_OFFSET 0x0
27#define CXL_CM_CAP_HDR_ID_MASK GENMASK(15, 0)
28#define CM_CAP_HDR_CAP_ID 1
29#define CXL_CM_CAP_HDR_VERSION_MASK GENMASK(19, 16)
30#define CM_CAP_HDR_CAP_VERSION 1
31#define CXL_CM_CAP_HDR_CACHE_MEM_VERSION_MASK GENMASK(23, 20)
32#define CM_CAP_HDR_CACHE_MEM_VERSION 1
33#define CXL_CM_CAP_HDR_ARRAY_SIZE_MASK GENMASK(31, 24)
34#define CXL_CM_CAP_PTR_MASK GENMASK(31, 20)
35
bd09626b 36#define CXL_CM_CAP_CAP_ID_RAS 0x2
08422378
BW
37#define CXL_CM_CAP_CAP_ID_HDM 0x5
38#define CXL_CM_CAP_CAP_HDM_VERSION 1
39
40/* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */
41#define CXL_HDM_DECODER_CAP_OFFSET 0x0
42#define CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
43#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
d17d0540
DW
44#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
45#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
46#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
47#define CXL_HDM_DECODER_ENABLE BIT(1)
48#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
49#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)
50#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
51#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
52#define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
53#define CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
54#define CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
55#define CXL_HDM_DECODER0_CTRL_LOCK BIT(8)
56#define CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
57#define CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
176baefb 58#define CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11)
cecbb5da 59#define CXL_HDM_DECODER0_CTRL_HOSTONLY BIT(12)
d17d0540
DW
60#define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
61#define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
9c57cde0
DW
62#define CXL_HDM_DECODER0_SKIP_LOW(i) CXL_HDM_DECODER0_TL_LOW(i)
63#define CXL_HDM_DECODER0_SKIP_HIGH(i) CXL_HDM_DECODER0_TL_HIGH(i)
08422378 64
3b39fd6c
AM
65/* HDM decoder control register constants CXL 3.0 8.2.5.19.7 */
66#define CXL_DECODER_MIN_GRANULARITY 256
67#define CXL_DECODER_MAX_ENCODED_IG 6
68
6423035f
BW
69static inline int cxl_hdm_decoder_count(u32 cap_hdr)
70{
71 int val = FIELD_GET(CXL_HDM_DECODER_COUNT_MASK, cap_hdr);
72
73 return val ? val * 2 : 1;
74}
75
419af595 76/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */
83351ddb 77static inline int eig_to_granularity(u16 eig, unsigned int *granularity)
419af595 78{
83351ddb 79 if (eig > CXL_DECODER_MAX_ENCODED_IG)
419af595 80 return -EINVAL;
83351ddb 81 *granularity = CXL_DECODER_MIN_GRANULARITY << eig;
419af595
DW
82 return 0;
83}
84
85/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */
c99b2e8c 86static inline int eiw_to_ways(u8 eiw, unsigned int *ways)
419af595 87{
c99b2e8c 88 switch (eiw) {
419af595 89 case 0 ... 4:
c99b2e8c 90 *ways = 1 << eiw;
419af595
DW
91 break;
92 case 8 ... 10:
c99b2e8c 93 *ways = 3 << (eiw - 8);
419af595
DW
94 break;
95 default:
96 return -EINVAL;
97 }
98
99 return 0;
100}
101
83351ddb 102static inline int granularity_to_eig(int granularity, u16 *eig)
80d10a6c 103{
83351ddb
DJ
104 if (granularity > SZ_16K || granularity < CXL_DECODER_MIN_GRANULARITY ||
105 !is_power_of_2(granularity))
80d10a6c 106 return -EINVAL;
83351ddb 107 *eig = ilog2(granularity) - 8;
80d10a6c
BW
108 return 0;
109}
110
c99b2e8c 111static inline int ways_to_eiw(unsigned int ways, u8 *eiw)
80d10a6c
BW
112{
113 if (ways > 16)
114 return -EINVAL;
115 if (is_power_of_2(ways)) {
c99b2e8c 116 *eiw = ilog2(ways);
80d10a6c
BW
117 return 0;
118 }
119 if (ways % 3)
120 return -EINVAL;
121 ways /= 3;
122 if (!is_power_of_2(ways))
123 return -EINVAL;
c99b2e8c 124 *eiw = ilog2(ways) + 8;
80d10a6c
BW
125 return 0;
126}
127
bd09626b
DW
128/* RAS Registers CXL 2.0 8.2.5.9 CXL RAS Capability Structure */
129#define CXL_RAS_UNCORRECTABLE_STATUS_OFFSET 0x0
130#define CXL_RAS_UNCORRECTABLE_STATUS_MASK (GENMASK(16, 14) | GENMASK(11, 0))
131#define CXL_RAS_UNCORRECTABLE_MASK_OFFSET 0x4
132#define CXL_RAS_UNCORRECTABLE_MASK_MASK (GENMASK(16, 14) | GENMASK(11, 0))
248529ed 133#define CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK BIT(8)
bd09626b
DW
134#define CXL_RAS_UNCORRECTABLE_SEVERITY_OFFSET 0x8
135#define CXL_RAS_UNCORRECTABLE_SEVERITY_MASK (GENMASK(16, 14) | GENMASK(11, 0))
136#define CXL_RAS_CORRECTABLE_STATUS_OFFSET 0xC
137#define CXL_RAS_CORRECTABLE_STATUS_MASK GENMASK(6, 0)
138#define CXL_RAS_CORRECTABLE_MASK_OFFSET 0x10
139#define CXL_RAS_CORRECTABLE_MASK_MASK GENMASK(6, 0)
140#define CXL_RAS_CAP_CONTROL_OFFSET 0x14
2905cb52 141#define CXL_RAS_CAP_CONTROL_FE_MASK GENMASK(5, 0)
bd09626b
DW
142#define CXL_RAS_HEADER_LOG_OFFSET 0x18
143#define CXL_RAS_CAPABILITY_LENGTH 0x58
4a20bc3e
DW
144#define CXL_HEADERLOG_SIZE SZ_512
145#define CXL_HEADERLOG_SIZE_U32 SZ_512 / sizeof(u32)
bd09626b 146
8adaf747
BW
147/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */
148#define CXLDEV_CAP_ARRAY_OFFSET 0x0
149#define CXLDEV_CAP_ARRAY_CAP_ID 0
150#define CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0)
151#define CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32)
152/* CXL 2.0 8.2.8.2 CXL Device Capability Header Register */
153#define CXLDEV_CAP_HDR_CAP_ID_MASK GENMASK(15, 0)
154/* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */
155#define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1
156#define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2
157#define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3
158#define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000
159
6ebe28f9
IW
160/* CXL 3.0 8.2.8.3.1 Event Status Register */
161#define CXLDEV_DEV_EVENT_STATUS_OFFSET 0x00
162#define CXLDEV_EVENT_STATUS_INFO BIT(0)
163#define CXLDEV_EVENT_STATUS_WARN BIT(1)
164#define CXLDEV_EVENT_STATUS_FAIL BIT(2)
165#define CXLDEV_EVENT_STATUS_FATAL BIT(3)
166
167#define CXLDEV_EVENT_STATUS_ALL (CXLDEV_EVENT_STATUS_INFO | \
168 CXLDEV_EVENT_STATUS_WARN | \
169 CXLDEV_EVENT_STATUS_FAIL | \
170 CXLDEV_EVENT_STATUS_FATAL)
171
a49aa814
DB
172/* CXL rev 3.0 section 8.2.9.2.4; Table 8-52 */
173#define CXLDEV_EVENT_INT_MODE_MASK GENMASK(1, 0)
174#define CXLDEV_EVENT_INT_MSGNUM_MASK GENMASK(7, 4)
175
8adaf747
BW
176/* CXL 2.0 8.2.8.4 Mailbox Registers */
177#define CXLDEV_MBOX_CAPS_OFFSET 0x00
178#define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0)
ccadf131
DB
179#define CXLDEV_MBOX_CAP_BG_CMD_IRQ BIT(6)
180#define CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK GENMASK(10, 7)
8adaf747
BW
181#define CXLDEV_MBOX_CTRL_OFFSET 0x04
182#define CXLDEV_MBOX_CTRL_DOORBELL BIT(0)
ccadf131 183#define CXLDEV_MBOX_CTRL_BG_CMD_IRQ BIT(2)
8adaf747
BW
184#define CXLDEV_MBOX_CMD_OFFSET 0x08
185#define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
186#define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)
187#define CXLDEV_MBOX_STATUS_OFFSET 0x10
ccadf131 188#define CXLDEV_MBOX_STATUS_BG_CMD BIT(0)
8adaf747
BW
189#define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)
190#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18
ccadf131
DB
191#define CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
192#define CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK GENMASK_ULL(22, 16)
193#define CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK GENMASK_ULL(47, 32)
194#define CXLDEV_MBOX_BG_CMD_COMMAND_VENDOR_MASK GENMASK_ULL(63, 48)
8adaf747
BW
195#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20
196
8ac75dd6 197/*
301e68dd
KC
198 * Using struct_group() allows for per register-block-type helper routines,
199 * without requiring block-type agnostic code to include the prefix.
8ac75dd6
DW
200 */
201struct cxl_regs {
301e68dd
KC
202 /*
203 * Common set of CXL Component register block base pointers
204 * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
bd09626b 205 * @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure
301e68dd
KC
206 */
207 struct_group_tagged(cxl_component_regs, component,
208 void __iomem *hdm_decoder;
bd09626b 209 void __iomem *ras;
301e68dd
KC
210 );
211 /*
212 * Common set of CXL Device register block base pointers
213 * @status: CXL 2.0 8.2.8.3 Device Status Registers
214 * @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
215 * @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
216 */
217 struct_group_tagged(cxl_device_regs, device_regs,
218 void __iomem *status, *mbox, *memdev;
219 );
1ad3f701
JC
220
221 struct_group_tagged(cxl_pmu_regs, pmu_regs,
222 void __iomem *pmu;
223 );
8ac75dd6
DW
224};
225
30af9729
IW
226struct cxl_reg_map {
227 bool valid;
a1554e9c 228 int id;
30af9729
IW
229 unsigned long offset;
230 unsigned long size;
231};
232
08422378
BW
233struct cxl_component_reg_map {
234 struct cxl_reg_map hdm_decoder;
bd09626b 235 struct cxl_reg_map ras;
08422378
BW
236};
237
30af9729
IW
238struct cxl_device_reg_map {
239 struct cxl_reg_map status;
240 struct cxl_reg_map mbox;
241 struct cxl_reg_map memdev;
242};
243
1ad3f701
JC
244struct cxl_pmu_reg_map {
245 struct cxl_reg_map pmu;
246};
247
a261e9a1
DW
248/**
249 * struct cxl_register_map - DVSEC harvested register block mapping parameters
dd22581f 250 * @host: device for devm operations and logging
a261e9a1 251 * @base: virtual base of the register-block-BAR + @block_offset
6c7f4f1e
DW
252 * @resource: physical resource base of the register block
253 * @max_size: maximum mapping size to perform register search
a261e9a1 254 * @reg_type: see enum cxl_regloc_type
a261e9a1
DW
255 * @component_map: cxl_reg_map for component registers
256 * @device_map: cxl_reg_maps for device registers
1ad3f701 257 * @pmu_map: cxl_reg_maps for CXL Performance Monitoring Units
a261e9a1 258 */
30af9729 259struct cxl_register_map {
dd22581f 260 struct device *host;
a261e9a1 261 void __iomem *base;
6c7f4f1e
DW
262 resource_size_t resource;
263 resource_size_t max_size;
30af9729 264 u8 reg_type;
30af9729 265 union {
08422378 266 struct cxl_component_reg_map component_map;
30af9729 267 struct cxl_device_reg_map device_map;
1ad3f701 268 struct cxl_pmu_reg_map pmu_map;
30af9729
IW
269 };
270};
271
08422378
BW
272void cxl_probe_component_regs(struct device *dev, void __iomem *base,
273 struct cxl_component_reg_map *map);
30af9729
IW
274void cxl_probe_device_regs(struct device *dev, void __iomem *base,
275 struct cxl_device_reg_map *map);
0c0df631 276int cxl_map_component_regs(const struct cxl_register_map *map,
57340804 277 struct cxl_component_regs *regs,
a1554e9c 278 unsigned long map_mask);
0c0df631 279int cxl_map_device_regs(const struct cxl_register_map *map,
57340804 280 struct cxl_device_regs *regs);
1ad3f701
JC
281int cxl_map_pmu_regs(struct pci_dev *pdev, struct cxl_pmu_regs *regs,
282 struct cxl_register_map *map);
399d34eb 283
303ebc1b 284enum cxl_regloc_type;
d717d7f3
JC
285int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
286int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
287 struct cxl_register_map *map, int index);
303ebc1b
BW
288int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
289 struct cxl_register_map *map);
d076bb8c 290int cxl_setup_regs(struct cxl_register_map *map);
eb4663b0
RR
291struct cxl_dport;
292resource_size_t cxl_rcd_component_reg_phys(struct device *dev,
293 struct cxl_dport *dport);
d5b1a271 294
4812be97 295#define CXL_RESOURCE_NONE ((resource_size_t) -1)
7d4b5ca2 296#define CXL_TARGET_STRLEN 20
4812be97 297
40ba17af
DW
298/*
299 * cxl_decoder flags that define the type of memory / devices this
300 * decoder supports as well as configuration lock status See "CXL 2.0
301 * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details.
a32320b7
DW
302 * Additionally indicate whether decoder settings were autodetected,
303 * user customized.
40ba17af
DW
304 */
305#define CXL_DECODER_F_RAM BIT(0)
306#define CXL_DECODER_F_PMEM BIT(1)
307#define CXL_DECODER_F_TYPE2 BIT(2)
308#define CXL_DECODER_F_TYPE3 BIT(3)
309#define CXL_DECODER_F_LOCK BIT(4)
d17d0540
DW
310#define CXL_DECODER_F_ENABLE BIT(5)
311#define CXL_DECODER_F_MASK GENMASK(5, 0)
40ba17af
DW
312
313enum cxl_decoder_type {
5aa39a91
DW
314 CXL_DECODER_DEVMEM = 2,
315 CXL_DECODER_HOSTONLYMEM = 3,
40ba17af
DW
316};
317
a5c25802
DW
318/*
319 * Current specification goes up to 8, double that seems a reasonable
320 * software max for the foreseeable future
321 */
322#define CXL_DECODER_MAX_INTERLEAVE 16
323
e7748305 324
40ba17af 325/**
e636479e 326 * struct cxl_decoder - Common CXL HDM Decoder Attributes
40ba17af
DW
327 * @dev: this decoder's device
328 * @id: kernel device name id
e8b7ea58 329 * @hpa_range: Host physical address range mapped by this decoder
40ba17af
DW
330 * @interleave_ways: number of cxl_dports in this decode
331 * @interleave_granularity: data stride per dport
332 * @target_type: accelerator vs expander (type2 vs type3) selector
b9686e8c 333 * @region: currently assigned region for this decoder
40ba17af 334 * @flags: memory type capabilities and locking
176baefb
DW
335 * @commit: device/decoder-type specific callback to commit settings to hw
336 * @reset: device/decoder-type specific callback to reset hw settings
337*/
40ba17af
DW
338struct cxl_decoder {
339 struct device dev;
340 int id;
e50fe01e 341 struct range hpa_range;
40ba17af
DW
342 int interleave_ways;
343 int interleave_granularity;
344 enum cxl_decoder_type target_type;
b9686e8c 345 struct cxl_region *region;
40ba17af 346 unsigned long flags;
176baefb
DW
347 int (*commit)(struct cxl_decoder *cxld);
348 int (*reset)(struct cxl_decoder *cxld);
e636479e
DW
349};
350
b9686e8c
DW
351/*
352 * CXL_DECODER_DEAD prevents endpoints from being reattached to regions
353 * while cxld_unregister() is running
354 */
2c866903
DW
355enum cxl_decoder_mode {
356 CXL_DECODER_NONE,
357 CXL_DECODER_RAM,
358 CXL_DECODER_PMEM,
359 CXL_DECODER_MIXED,
b9686e8c 360 CXL_DECODER_DEAD,
2c866903
DW
361};
362
7d505f98
DW
363static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
364{
365 static const char * const names[] = {
366 [CXL_DECODER_NONE] = "none",
367 [CXL_DECODER_RAM] = "ram",
368 [CXL_DECODER_PMEM] = "pmem",
369 [CXL_DECODER_MIXED] = "mixed",
370 };
371
372 if (mode >= CXL_DECODER_NONE && mode <= CXL_DECODER_MIXED)
373 return names[mode];
374 return "mixed";
375}
376
a32320b7
DW
377/*
378 * Track whether this decoder is reserved for region autodiscovery, or
379 * free for userspace provisioning.
380 */
381enum cxl_decoder_state {
382 CXL_DECODER_STATE_MANUAL,
383 CXL_DECODER_STATE_AUTO,
384};
385
3bf65915
DW
386/**
387 * struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder
388 * @cxld: base cxl_decoder_object
389 * @dpa_res: actively claimed DPA span of this decoder
390 * @skip: offset into @dpa_res where @cxld.hpa_range maps
2c866903 391 * @mode: which memory type / access-mode-partition this decoder targets
a32320b7 392 * @state: autodiscovery state
b9686e8c 393 * @pos: interleave position in @cxld.region
3bf65915
DW
394 */
395struct cxl_endpoint_decoder {
396 struct cxl_decoder cxld;
397 struct resource *dpa_res;
398 resource_size_t skip;
2c866903 399 enum cxl_decoder_mode mode;
a32320b7 400 enum cxl_decoder_state state;
b9686e8c 401 int pos;
3bf65915
DW
402};
403
e636479e
DW
404/**
405 * struct cxl_switch_decoder - Switch specific CXL HDM Decoder
406 * @cxld: base cxl_decoder object
407 * @target_lock: coordinate coherent reads of the target list
408 * @nr_targets: number of elements in @target
409 * @target: active ordered target list in current decoder configuration
410 *
411 * The 'switch' decoder type represents the decoder instances of cxl_port's that
412 * route from the root of a CXL memory decode topology to the endpoints. They
413 * come in two flavors, root-level decoders, statically defined by platform
414 * firmware, and mid-level decoders, where interleave-granularity,
415 * interleave-width, and the target list are mutable.
416 */
417struct cxl_switch_decoder {
418 struct cxl_decoder cxld;
86c8ea0f 419 seqlock_t target_lock;
be185c29 420 int nr_targets;
40ba17af
DW
421 struct cxl_dport *target[];
422};
423
f9db85bf
AS
424struct cxl_root_decoder;
425typedef struct cxl_dport *(*cxl_calc_hb_fn)(struct cxl_root_decoder *cxlrd,
426 int pos);
8fdcb170 427
0f157c7f
DW
428/**
429 * struct cxl_root_decoder - Static platform CXL address decoder
430 * @res: host / parent resource for region allocations
779dd20c 431 * @region_id: region id for next region provisioning event
6aa41144 432 * @calc_hb: which host bridge covers the n'th position by granularity
f9db85bf 433 * @platform_data: platform specific configuration data
a32320b7 434 * @range_lock: sync region autodiscovery by address range
0f157c7f
DW
435 * @cxlsd: base cxl switch decoder
436 */
437struct cxl_root_decoder {
438 struct resource *res;
779dd20c 439 atomic_t region_id;
f9db85bf
AS
440 cxl_calc_hb_fn calc_hb;
441 void *platform_data;
a32320b7 442 struct mutex range_lock;
0f157c7f
DW
443 struct cxl_switch_decoder cxlsd;
444};
445
dd5ba0eb
BW
446/*
447 * enum cxl_config_state - State machine for region configuration
448 * @CXL_CONFIG_IDLE: Any sysfs attribute can be written freely
80d10a6c
BW
449 * @CXL_CONFIG_INTERLEAVE_ACTIVE: region size has been set, no more
450 * changes to interleave_ways or interleave_granularity
dd5ba0eb
BW
451 * @CXL_CONFIG_ACTIVE: All targets have been added the region is now
452 * active
176baefb
DW
453 * @CXL_CONFIG_RESET_PENDING: see commit_store()
454 * @CXL_CONFIG_COMMIT: Soft-config has been committed to hardware
dd5ba0eb
BW
455 */
456enum cxl_config_state {
457 CXL_CONFIG_IDLE,
80d10a6c 458 CXL_CONFIG_INTERLEAVE_ACTIVE,
dd5ba0eb 459 CXL_CONFIG_ACTIVE,
176baefb
DW
460 CXL_CONFIG_RESET_PENDING,
461 CXL_CONFIG_COMMIT,
dd5ba0eb
BW
462};
463
464/**
465 * struct cxl_region_params - region settings
466 * @state: allow the driver to lockdown further parameter changes
467 * @uuid: unique id for persistent regions
80d10a6c
BW
468 * @interleave_ways: number of endpoints in the region
469 * @interleave_granularity: capacity each endpoint contributes to a stripe
23a22cd1 470 * @res: allocated iomem capacity for this region
038e6eb8
BS
471 * @targets: active ordered targets in current decoder configuration
472 * @nr_targets: number of targets
dd5ba0eb
BW
473 *
474 * State transitions are protected by the cxl_region_rwsem
475 */
476struct cxl_region_params {
477 enum cxl_config_state state;
478 uuid_t uuid;
80d10a6c
BW
479 int interleave_ways;
480 int interleave_granularity;
23a22cd1 481 struct resource *res;
b9686e8c
DW
482 struct cxl_endpoint_decoder *targets[CXL_DECODER_MAX_INTERLEAVE];
483 int nr_targets;
dd5ba0eb
BW
484};
485
a32320b7
DW
486/*
487 * Indicate whether this region has been assembled by autodetection or
488 * userspace assembly. Prevent endpoint decoders outside of automatic
489 * detection from being added to the region.
490 */
d1257d09 491#define CXL_REGION_F_AUTO 0
a32320b7 492
2ab47045
DW
493/*
494 * Require that a committed region successfully complete a teardown once
495 * any of its associated decoders have been torn down. This maintains
496 * the commit state for the region since there are committed decoders,
497 * but blocks cxl_region_probe().
498 */
499#define CXL_REGION_F_NEEDS_RESET 1
a32320b7 500
779dd20c
BW
501/**
502 * struct cxl_region - CXL region
503 * @dev: This region's device
504 * @id: This region's id. Id is globally unique across all regions
505 * @mode: Endpoint decoder allocation / access mode
506 * @type: Endpoint decoder target type
f17b558d
DW
507 * @cxl_nvb: nvdimm bridge for coordinating @cxlr_pmem setup / shutdown
508 * @cxlr_pmem: (for pmem regions) cached copy of the nvdimm bridge
d18bc74a 509 * @flags: Region state flags
dd5ba0eb 510 * @params: active + config params for the region
779dd20c
BW
511 */
512struct cxl_region {
513 struct device dev;
514 int id;
515 enum cxl_decoder_mode mode;
516 enum cxl_decoder_type type;
f17b558d
DW
517 struct cxl_nvdimm_bridge *cxl_nvb;
518 struct cxl_pmem_region *cxlr_pmem;
d18bc74a 519 unsigned long flags;
dd5ba0eb 520 struct cxl_region_params params;
779dd20c
BW
521};
522
8fdcb170 523struct cxl_nvdimm_bridge {
2e52b625 524 int id;
8fdcb170
DW
525 struct device dev;
526 struct cxl_port *port;
527 struct nvdimm_bus *nvdimm_bus;
528 struct nvdimm_bus_descriptor nd_desc;
8fdcb170
DW
529};
530
b5807c80
DJ
531#define CXL_DEV_ID_LEN 19
532
21083f51
DW
533struct cxl_nvdimm {
534 struct device dev;
535 struct cxl_memdev *cxlmd;
b5807c80 536 u8 dev_id[CXL_DEV_ID_LEN]; /* for nvdimm, string of 'serial' */
04ad63f0
DW
537};
538
539struct cxl_pmem_region_mapping {
540 struct cxl_memdev *cxlmd;
541 struct cxl_nvdimm *cxl_nvd;
542 u64 start;
543 u64 size;
544 int position;
545};
546
547struct cxl_pmem_region {
548 struct device dev;
549 struct cxl_region *cxlr;
550 struct nd_region *nd_region;
04ad63f0
DW
551 struct range hpa_range;
552 int nr_mappings;
553 struct cxl_pmem_region_mapping mapping[];
21083f51
DW
554};
555
09d09e04
DW
556struct cxl_dax_region {
557 struct device dev;
558 struct cxl_region *cxlr;
559 struct range hpa_range;
560};
561
4812be97
DW
562/**
563 * struct cxl_port - logical collection of upstream port devices and
564 * downstream port devices to construct a CXL memory
565 * decode hierarchy.
566 * @dev: this port's device
7481653d 567 * @uport_dev: PCI or platform device implementing the upstream port capability
ee800010 568 * @host_bridge: Shortcut to the platform attach point for this port
4812be97 569 * @id: id for port device-name
7d4b5ca2 570 * @dports: cxl_dport instances referenced by decoders
2703c16c 571 * @endpoints: cxl_ep instances, endpoints that are a descendant of this port
384e624b 572 * @regions: cxl_region_ref instances, regions mapped by this port
1b58b4ca 573 * @parent_dport: dport that points to this port in the parent
40ba17af 574 * @decoder_ida: allocator for decoder ids
d8add492 575 * @reg_map: component and ras register mapping parameters
e4f6dfa9 576 * @nr_dports: number of entries in @dports
0c33b393 577 * @hdm_end: track last allocated HDM decoder instance for allocation ordering
176baefb 578 * @commit_end: cursor to track highest committed decoder for commit ordering
2703c16c 579 * @dead: last ep has been removed, force port re-creation
53fa1bff 580 * @depth: How deep this port is relative to the root. depth 0 is the root.
c9700604
IW
581 * @cdat: Cached CDAT data
582 * @cdat_available: Should a CDAT attribute be available in sysfs
4812be97
DW
583 */
584struct cxl_port {
585 struct device dev;
7481653d 586 struct device *uport_dev;
ee800010 587 struct device *host_bridge;
4812be97 588 int id;
39178585 589 struct xarray dports;
256d0e9e 590 struct xarray endpoints;
384e624b 591 struct xarray regions;
1b58b4ca 592 struct cxl_dport *parent_dport;
40ba17af 593 struct ida decoder_ida;
d8add492 594 struct cxl_register_map reg_map;
e4f6dfa9 595 int nr_dports;
0c33b393 596 int hdm_end;
176baefb 597 int commit_end;
2703c16c 598 bool dead;
53fa1bff 599 unsigned int depth;
c9700604
IW
600 struct cxl_cdat {
601 void *table;
602 size_t length;
603 } cdat;
604 bool cdat_available;
4812be97
DW
605};
606
39178585
DW
607static inline struct cxl_dport *
608cxl_find_dport_by_dev(struct cxl_port *port, const struct device *dport_dev)
609{
610 return xa_load(&port->dports, (unsigned long)dport_dev);
611}
612
06193378
DW
613struct cxl_rcrb_info {
614 resource_size_t base;
615 u16 aer_cap;
616};
617
7d4b5ca2
DW
618/**
619 * struct cxl_dport - CXL downstream port
227db574 620 * @dport_dev: PCI bridge or firmware device representing the downstream link
d8add492 621 * @reg_map: component and ras register mapping parameters
7d4b5ca2 622 * @port_id: unique hardware identifier for dport in decoder target list
06193378 623 * @rcrb: Data about the Root Complex Register Block layout
d5b1a271 624 * @rch: Indicate whether this dport was enumerated in RCH or VH mode
7d4b5ca2 625 * @port: reference to cxl_port that contains this downstream port
7d4b5ca2
DW
626 */
627struct cxl_dport {
227db574 628 struct device *dport_dev;
d8add492 629 struct cxl_register_map reg_map;
7d4b5ca2 630 int port_id;
06193378 631 struct cxl_rcrb_info rcrb;
d5b1a271 632 bool rch;
7d4b5ca2 633 struct cxl_port *port;
7d4b5ca2
DW
634};
635
2703c16c
DW
636/**
637 * struct cxl_ep - track an endpoint's interest in a port
638 * @ep: device that hosts a generic CXL endpoint (expander or accelerator)
de516b40 639 * @dport: which dport routes to this endpoint on @port
7f8faf96
DW
640 * @next: cxl switch port across the link attached to @dport NULL if
641 * attached to an endpoint
2703c16c
DW
642 */
643struct cxl_ep {
644 struct device *ep;
de516b40 645 struct cxl_dport *dport;
7f8faf96 646 struct cxl_port *next;
2703c16c
DW
647};
648
384e624b
DW
649/**
650 * struct cxl_region_ref - track a region's interest in a port
651 * @port: point in topology to install this reference
652 * @decoder: decoder assigned for @region in @port
653 * @region: region for this reference
654 * @endpoints: cxl_ep references for region members beneath @port
27b3f8d1 655 * @nr_targets_set: track how many targets have been programmed during setup
384e624b
DW
656 * @nr_eps: number of endpoints beneath @port
657 * @nr_targets: number of distinct targets needed to reach @nr_eps
658 */
659struct cxl_region_ref {
660 struct cxl_port *port;
661 struct cxl_decoder *decoder;
662 struct cxl_region *region;
663 struct xarray endpoints;
27b3f8d1 664 int nr_targets_set;
384e624b
DW
665 int nr_eps;
666 int nr_targets;
667};
668
d54c1bbe
BW
669/*
670 * The platform firmware device hosting the root is also the top of the
671 * CXL port topology. All other CXL ports have another CXL port as their
7481653d 672 * parent and their ->uport_dev / host device is out-of-line of the port
d54c1bbe
BW
673 * ancestry.
674 */
675static inline bool is_cxl_root(struct cxl_port *port)
676{
7481653d 677 return port->uport_dev == port->dev.parent;
d54c1bbe
BW
678}
679
2a81ada3
GKH
680bool is_cxl_port(const struct device *dev);
681struct cxl_port *to_cxl_port(const struct device *dev);
98d2d3a2 682struct pci_bus;
7481653d 683int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
5ff7316f
DW
684 struct pci_bus *bus);
685struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port);
7481653d
DW
686struct cxl_port *devm_cxl_add_port(struct device *host,
687 struct device *uport_dev,
4812be97 688 resource_size_t component_reg_phys,
1b58b4ca 689 struct cxl_dport *parent_dport);
d35b495d 690struct cxl_port *find_cxl_root(struct cxl_port *port);
2703c16c 691int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
4029c32f
DW
692void cxl_bus_rescan(void);
693void cxl_bus_drain(void);
733b57f2
RR
694struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev,
695 struct cxl_dport **dport);
1b58b4ca
DW
696struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
697 struct cxl_dport **dport);
8dd2bc0f 698bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd);
2703c16c 699
664bf115 700struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
98d2d3a2
DW
701 struct device *dport, int port_id,
702 resource_size_t component_reg_phys);
d5b1a271
RR
703struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
704 struct device *dport_dev, int port_id,
d5b1a271 705 resource_size_t rcrb);
2703c16c 706
40ba17af 707struct cxl_decoder *to_cxl_decoder(struct device *dev);
0f157c7f 708struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev);
3d8f7cca 709struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev);
3bf65915 710struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev);
8fdcb170 711bool is_root_decoder(struct device *dev);
3d8f7cca 712bool is_switch_decoder(struct device *dev);
8ae3cebc 713bool is_endpoint_decoder(struct device *dev);
0f157c7f 714struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
f9db85bf
AS
715 unsigned int nr_targets,
716 cxl_calc_hb_fn calc_hb);
717struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos);
e636479e
DW
718struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
719 unsigned int nr_targets);
48667f67 720int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
3bf65915 721struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port);
d17d0540 722int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
48667f67 723int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
8dd2bc0f
BW
724int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint);
725
59c3368b
DJ
726/**
727 * struct cxl_endpoint_dvsec_info - Cached DVSEC info
b70c2cf9 728 * @mem_enabled: cached value of mem_enabled in the DVSEC at init time
59c3368b 729 * @ranges: Number of active HDM ranges this device uses.
b70c2cf9 730 * @port: endpoint port associated with this info instance
59c3368b
DJ
731 * @dvsec_range: cached attributes of the ranges in the DVSEC, PCIE_DEVICE
732 */
733struct cxl_endpoint_dvsec_info {
734 bool mem_enabled;
735 int ranges;
b70c2cf9 736 struct cxl_port *port;
59c3368b
DJ
737 struct range dvsec_range[2];
738};
739
d17d0540 740struct cxl_hdm;
4474ce56
DJ
741struct cxl_hdm *devm_cxl_setup_hdm(struct cxl_port *port,
742 struct cxl_endpoint_dvsec_info *info);
b777e9be
DJ
743int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
744 struct cxl_endpoint_dvsec_info *info);
664bf115 745int devm_cxl_add_passthrough_decoder(struct cxl_port *port);
59c3368b
DJ
746int cxl_dvsec_rr_decode(struct device *dev, int dvsec,
747 struct cxl_endpoint_dvsec_info *info);
40ba17af 748
779dd20c
BW
749bool is_cxl_region(struct device *dev);
750
b39cb105 751extern struct bus_type cxl_bus_type;
6af7139c
DW
752
753struct cxl_driver {
754 const char *name;
755 int (*probe)(struct device *dev);
756 void (*remove)(struct device *dev);
757 struct device_driver drv;
758 int id;
759};
760
761static inline struct cxl_driver *to_cxl_drv(struct device_driver *drv)
762{
763 return container_of(drv, struct cxl_driver, drv);
764}
765
766int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
767 const char *modname);
768#define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME)
769void cxl_driver_unregister(struct cxl_driver *cxl_drv);
770
c57cae78
BW
771#define module_cxl_driver(__cxl_driver) \
772 module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister)
773
21083f51
DW
774#define CXL_DEVICE_NVDIMM_BRIDGE 1
775#define CXL_DEVICE_NVDIMM 2
54cdbf84
BW
776#define CXL_DEVICE_PORT 3
777#define CXL_DEVICE_ROOT 4
8dd2bc0f 778#define CXL_DEVICE_MEMORY_EXPANDER 5
8d48817d 779#define CXL_DEVICE_REGION 6
04ad63f0 780#define CXL_DEVICE_PMEM_REGION 7
09d09e04 781#define CXL_DEVICE_DAX_REGION 8
1ad3f701 782#define CXL_DEVICE_PMU 9
8fdcb170 783
6af7139c
DW
784#define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
785#define CXL_MODALIAS_FMT "cxl:t%d"
786
8fdcb170
DW
787struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev);
788struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host,
789 struct cxl_port *port);
21083f51
DW
790struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev);
791bool is_cxl_nvdimm(struct device *dev);
53989fad 792bool is_cxl_nvdimm_bridge(struct device *dev);
f17b558d 793int devm_cxl_add_nvdimm(struct cxl_memdev *cxlmd);
d35b495d 794struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_memdev *cxlmd);
04ad63f0
DW
795
796#ifdef CONFIG_CXL_REGION
797bool is_cxl_pmem_region(struct device *dev);
798struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev);
a32320b7
DW
799int cxl_add_to_region(struct cxl_port *root,
800 struct cxl_endpoint_decoder *cxled);
09d09e04 801struct cxl_dax_region *to_cxl_dax_region(struct device *dev);
04ad63f0
DW
802#else
803static inline bool is_cxl_pmem_region(struct device *dev)
804{
805 return false;
806}
807static inline struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
808{
809 return NULL;
810}
a32320b7
DW
811static inline int cxl_add_to_region(struct cxl_port *root,
812 struct cxl_endpoint_decoder *cxled)
813{
814 return 0;
815}
09d09e04
DW
816static inline struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
817{
818 return NULL;
819}
04ad63f0 820#endif
67dcdd4d
DW
821
822/*
823 * Unit test builds overrides this to __weak, find the 'strong' version
824 * of these symbols in tools/testing/cxl/.
825 */
826#ifndef __mock
827#define __mock static
828#endif
3c5b9039 829
8adaf747 830#endif /* __CXL_H__ */