1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright(c) 2020 Intel Corporation. */
7 #include <linux/libnvdimm.h>
8 #include <linux/bitfield.h>
9 #include <linux/notifier.h>
10 #include <linux/bitops.h>
11 #include <linux/log2.h>
12 #include <linux/node.h>
15 extern const struct nvdimm_security_ops *cxl_security_ops;
20 * The CXL core objects like ports, decoders, and regions are shared
21 * between the subsystem drivers cxl_acpi, cxl_pci, and core drivers
22 * (port-driver, region-driver, nvdimm object-drivers... etc).
25 /* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
26 #define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K
28 /* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
29 #define CXL_CM_OFFSET 0x1000
30 #define CXL_CM_CAP_HDR_OFFSET 0x0
31 #define CXL_CM_CAP_HDR_ID_MASK GENMASK(15, 0)
32 #define CM_CAP_HDR_CAP_ID 1
33 #define CXL_CM_CAP_HDR_VERSION_MASK GENMASK(19, 16)
34 #define CM_CAP_HDR_CAP_VERSION 1
35 #define CXL_CM_CAP_HDR_CACHE_MEM_VERSION_MASK GENMASK(23, 20)
36 #define CM_CAP_HDR_CACHE_MEM_VERSION 1
37 #define CXL_CM_CAP_HDR_ARRAY_SIZE_MASK GENMASK(31, 24)
38 #define CXL_CM_CAP_PTR_MASK GENMASK(31, 20)
40 #define CXL_CM_CAP_CAP_ID_RAS 0x2
41 #define CXL_CM_CAP_CAP_ID_HDM 0x5
42 #define CXL_CM_CAP_CAP_HDM_VERSION 1
44 /* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */
45 #define CXL_HDM_DECODER_CAP_OFFSET 0x0
46 #define CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
47 #define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
48 #define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
49 #define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
50 #define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)
51 #define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)
52 #define CXL_HDM_DECODER_CTRL_OFFSET 0x4
53 #define CXL_HDM_DECODER_ENABLE BIT(1)
54 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
55 #define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)
56 #define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
57 #define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
58 #define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
59 #define CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
60 #define CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
61 #define CXL_HDM_DECODER0_CTRL_LOCK BIT(8)
62 #define CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
63 #define CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
64 #define CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11)
65 #define CXL_HDM_DECODER0_CTRL_HOSTONLY BIT(12)
66 #define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
67 #define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
68 #define CXL_HDM_DECODER0_SKIP_LOW(i) CXL_HDM_DECODER0_TL_LOW(i)
69 #define CXL_HDM_DECODER0_SKIP_HIGH(i) CXL_HDM_DECODER0_TL_HIGH(i)
71 /* HDM decoder control register constants CXL 3.0 8.2.5.19.7 */
72 #define CXL_DECODER_MIN_GRANULARITY 256
73 #define CXL_DECODER_MAX_ENCODED_IG 6
75 static inline int cxl_hdm_decoder_count(u32 cap_hdr)
77 int val = FIELD_GET(CXL_HDM_DECODER_COUNT_MASK, cap_hdr);
79 return val ? val * 2 : 1;
82 /* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */
83 static inline int eig_to_granularity(u16 eig, unsigned int *granularity)
85 if (eig > CXL_DECODER_MAX_ENCODED_IG)
87 *granularity = CXL_DECODER_MIN_GRANULARITY << eig;
91 /* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */
92 static inline int eiw_to_ways(u8 eiw, unsigned int *ways)
99 *ways = 3 << (eiw - 8);
108 static inline int granularity_to_eig(int granularity, u16 *eig)
110 if (granularity > SZ_16K || granularity < CXL_DECODER_MIN_GRANULARITY ||
111 !is_power_of_2(granularity))
113 *eig = ilog2(granularity) - 8;
117 static inline int ways_to_eiw(unsigned int ways, u8 *eiw)
121 if (is_power_of_2(ways)) {
128 if (!is_power_of_2(ways))
130 *eiw = ilog2(ways) + 8;
134 /* RAS Registers CXL 2.0 8.2.5.9 CXL RAS Capability Structure */
135 #define CXL_RAS_UNCORRECTABLE_STATUS_OFFSET 0x0
136 #define CXL_RAS_UNCORRECTABLE_STATUS_MASK (GENMASK(16, 14) | GENMASK(11, 0))
137 #define CXL_RAS_UNCORRECTABLE_MASK_OFFSET 0x4
138 #define CXL_RAS_UNCORRECTABLE_MASK_MASK (GENMASK(16, 14) | GENMASK(11, 0))
139 #define CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK BIT(8)
140 #define CXL_RAS_UNCORRECTABLE_SEVERITY_OFFSET 0x8
141 #define CXL_RAS_UNCORRECTABLE_SEVERITY_MASK (GENMASK(16, 14) | GENMASK(11, 0))
142 #define CXL_RAS_CORRECTABLE_STATUS_OFFSET 0xC
143 #define CXL_RAS_CORRECTABLE_STATUS_MASK GENMASK(6, 0)
144 #define CXL_RAS_CORRECTABLE_MASK_OFFSET 0x10
145 #define CXL_RAS_CORRECTABLE_MASK_MASK GENMASK(6, 0)
146 #define CXL_RAS_CAP_CONTROL_OFFSET 0x14
147 #define CXL_RAS_CAP_CONTROL_FE_MASK GENMASK(5, 0)
148 #define CXL_RAS_HEADER_LOG_OFFSET 0x18
149 #define CXL_RAS_CAPABILITY_LENGTH 0x58
150 #define CXL_HEADERLOG_SIZE SZ_512
151 #define CXL_HEADERLOG_SIZE_U32 SZ_512 / sizeof(u32)
153 /* CXL 2.0 8.2.8.1 Device Capabilities Array Register */
154 #define CXLDEV_CAP_ARRAY_OFFSET 0x0
155 #define CXLDEV_CAP_ARRAY_CAP_ID 0
156 #define CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0)
157 #define CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32)
158 /* CXL 2.0 8.2.8.2 CXL Device Capability Header Register */
159 #define CXLDEV_CAP_HDR_CAP_ID_MASK GENMASK(15, 0)
160 /* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */
161 #define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1
162 #define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2
163 #define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3
164 #define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000
166 /* CXL 3.0 8.2.8.3.1 Event Status Register */
167 #define CXLDEV_DEV_EVENT_STATUS_OFFSET 0x00
168 #define CXLDEV_EVENT_STATUS_INFO BIT(0)
169 #define CXLDEV_EVENT_STATUS_WARN BIT(1)
170 #define CXLDEV_EVENT_STATUS_FAIL BIT(2)
171 #define CXLDEV_EVENT_STATUS_FATAL BIT(3)
173 #define CXLDEV_EVENT_STATUS_ALL (CXLDEV_EVENT_STATUS_INFO | \
174 CXLDEV_EVENT_STATUS_WARN | \
175 CXLDEV_EVENT_STATUS_FAIL | \
176 CXLDEV_EVENT_STATUS_FATAL)
178 /* CXL rev 3.0 section 8.2.9.2.4; Table 8-52 */
179 #define CXLDEV_EVENT_INT_MODE_MASK GENMASK(1, 0)
180 #define CXLDEV_EVENT_INT_MSGNUM_MASK GENMASK(7, 4)
182 /* CXL 2.0 8.2.8.4 Mailbox Registers */
183 #define CXLDEV_MBOX_CAPS_OFFSET 0x00
184 #define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0)
185 #define CXLDEV_MBOX_CAP_BG_CMD_IRQ BIT(6)
186 #define CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK GENMASK(10, 7)
187 #define CXLDEV_MBOX_CTRL_OFFSET 0x04
188 #define CXLDEV_MBOX_CTRL_DOORBELL BIT(0)
189 #define CXLDEV_MBOX_CTRL_BG_CMD_IRQ BIT(2)
190 #define CXLDEV_MBOX_CMD_OFFSET 0x08
191 #define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
192 #define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)
193 #define CXLDEV_MBOX_STATUS_OFFSET 0x10
194 #define CXLDEV_MBOX_STATUS_BG_CMD BIT(0)
195 #define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)
196 #define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18
197 #define CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
198 #define CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK GENMASK_ULL(22, 16)
199 #define CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK GENMASK_ULL(47, 32)
200 #define CXLDEV_MBOX_BG_CMD_COMMAND_VENDOR_MASK GENMASK_ULL(63, 48)
201 #define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20
204 * Using struct_group() allows for per register-block-type helper routines,
205 * without requiring block-type agnostic code to include the prefix.
209 * Common set of CXL Component register block base pointers
210 * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
211 * @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure
213 struct_group_tagged(cxl_component_regs, component,
214 void __iomem *hdm_decoder;
218 * Common set of CXL Device register block base pointers
219 * @status: CXL 2.0 8.2.8.3 Device Status Registers
220 * @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
221 * @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
223 struct_group_tagged(cxl_device_regs, device_regs,
224 void __iomem *status, *mbox, *memdev;
227 struct_group_tagged(cxl_pmu_regs, pmu_regs,
232 * RCH downstream port specific RAS register
233 * @aer: CXL 3.0 8.2.1.1 RCH Downstream Port RCRB
235 struct_group_tagged(cxl_rch_regs, rch_regs,
236 void __iomem *dport_aer;
243 unsigned long offset;
247 struct cxl_component_reg_map {
248 struct cxl_reg_map hdm_decoder;
249 struct cxl_reg_map ras;
252 struct cxl_device_reg_map {
253 struct cxl_reg_map status;
254 struct cxl_reg_map mbox;
255 struct cxl_reg_map memdev;
258 struct cxl_pmu_reg_map {
259 struct cxl_reg_map pmu;
263 * struct cxl_register_map - DVSEC harvested register block mapping parameters
264 * @host: device for devm operations and logging
265 * @base: virtual base of the register-block-BAR + @block_offset
266 * @resource: physical resource base of the register block
267 * @max_size: maximum mapping size to perform register search
268 * @reg_type: see enum cxl_regloc_type
269 * @component_map: cxl_reg_map for component registers
270 * @device_map: cxl_reg_maps for device registers
271 * @pmu_map: cxl_reg_maps for CXL Performance Monitoring Units
273 struct cxl_register_map {
276 resource_size_t resource;
277 resource_size_t max_size;
280 struct cxl_component_reg_map component_map;
281 struct cxl_device_reg_map device_map;
282 struct cxl_pmu_reg_map pmu_map;
286 void cxl_probe_component_regs(struct device *dev, void __iomem *base,
287 struct cxl_component_reg_map *map);
288 void cxl_probe_device_regs(struct device *dev, void __iomem *base,
289 struct cxl_device_reg_map *map);
290 int cxl_map_component_regs(const struct cxl_register_map *map,
291 struct cxl_component_regs *regs,
292 unsigned long map_mask);
293 int cxl_map_device_regs(const struct cxl_register_map *map,
294 struct cxl_device_regs *regs);
295 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
297 enum cxl_regloc_type;
298 int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
299 int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
300 struct cxl_register_map *map, int index);
301 int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
302 struct cxl_register_map *map);
303 int cxl_setup_regs(struct cxl_register_map *map);
305 resource_size_t cxl_rcd_component_reg_phys(struct device *dev,
306 struct cxl_dport *dport);
308 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
309 #define CXL_TARGET_STRLEN 20
312 * cxl_decoder flags that define the type of memory / devices this
313 * decoder supports as well as configuration lock status See "CXL 2.0
314 * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details.
315 * Additionally indicate whether decoder settings were autodetected,
318 #define CXL_DECODER_F_RAM BIT(0)
319 #define CXL_DECODER_F_PMEM BIT(1)
320 #define CXL_DECODER_F_TYPE2 BIT(2)
321 #define CXL_DECODER_F_TYPE3 BIT(3)
322 #define CXL_DECODER_F_LOCK BIT(4)
323 #define CXL_DECODER_F_ENABLE BIT(5)
324 #define CXL_DECODER_F_MASK GENMASK(5, 0)
326 enum cxl_decoder_type {
327 CXL_DECODER_DEVMEM = 2,
328 CXL_DECODER_HOSTONLYMEM = 3,
332 * Current specification goes up to 8, double that seems a reasonable
333 * software max for the foreseeable future
335 #define CXL_DECODER_MAX_INTERLEAVE 16
337 #define CXL_QOS_CLASS_INVALID -1
340 * struct cxl_decoder - Common CXL HDM Decoder Attributes
341 * @dev: this decoder's device
342 * @id: kernel device name id
343 * @hpa_range: Host physical address range mapped by this decoder
344 * @interleave_ways: number of cxl_dports in this decode
345 * @interleave_granularity: data stride per dport
346 * @target_type: accelerator vs expander (type2 vs type3) selector
347 * @region: currently assigned region for this decoder
348 * @flags: memory type capabilities and locking
349 * @commit: device/decoder-type specific callback to commit settings to hw
350 * @reset: device/decoder-type specific callback to reset hw settings
355 struct range hpa_range;
357 int interleave_granularity;
358 enum cxl_decoder_type target_type;
359 struct cxl_region *region;
361 int (*commit)(struct cxl_decoder *cxld);
362 int (*reset)(struct cxl_decoder *cxld);
366 * CXL_DECODER_DEAD prevents endpoints from being reattached to regions
367 * while cxld_unregister() is running
369 enum cxl_decoder_mode {
377 static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
379 static const char * const names[] = {
380 [CXL_DECODER_NONE] = "none",
381 [CXL_DECODER_RAM] = "ram",
382 [CXL_DECODER_PMEM] = "pmem",
383 [CXL_DECODER_MIXED] = "mixed",
386 if (mode >= CXL_DECODER_NONE && mode <= CXL_DECODER_MIXED)
392 * Track whether this decoder is reserved for region autodiscovery, or
393 * free for userspace provisioning.
395 enum cxl_decoder_state {
396 CXL_DECODER_STATE_MANUAL,
397 CXL_DECODER_STATE_AUTO,
401 * struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder
402 * @cxld: base cxl_decoder_object
403 * @dpa_res: actively claimed DPA span of this decoder
404 * @skip: offset into @dpa_res where @cxld.hpa_range maps
405 * @mode: which memory type / access-mode-partition this decoder targets
406 * @state: autodiscovery state
407 * @pos: interleave position in @cxld.region
409 struct cxl_endpoint_decoder {
410 struct cxl_decoder cxld;
411 struct resource *dpa_res;
412 resource_size_t skip;
413 enum cxl_decoder_mode mode;
414 enum cxl_decoder_state state;
419 * struct cxl_switch_decoder - Switch specific CXL HDM Decoder
420 * @cxld: base cxl_decoder object
421 * @nr_targets: number of elements in @target
422 * @target: active ordered target list in current decoder configuration
424 * The 'switch' decoder type represents the decoder instances of cxl_port's that
425 * route from the root of a CXL memory decode topology to the endpoints. They
426 * come in two flavors, root-level decoders, statically defined by platform
427 * firmware, and mid-level decoders, where interleave-granularity,
428 * interleave-width, and the target list are mutable.
430 struct cxl_switch_decoder {
431 struct cxl_decoder cxld;
433 struct cxl_dport *target[];
436 struct cxl_root_decoder;
437 typedef struct cxl_dport *(*cxl_calc_hb_fn)(struct cxl_root_decoder *cxlrd,
441 * struct cxl_root_decoder - Static platform CXL address decoder
442 * @res: host / parent resource for region allocations
443 * @region_id: region id for next region provisioning event
444 * @calc_hb: which host bridge covers the n'th position by granularity
445 * @platform_data: platform specific configuration data
446 * @range_lock: sync region autodiscovery by address range
447 * @qos_class: QoS performance class cookie
448 * @cxlsd: base cxl switch decoder
450 struct cxl_root_decoder {
451 struct resource *res;
453 cxl_calc_hb_fn calc_hb;
455 struct mutex range_lock;
457 struct cxl_switch_decoder cxlsd;
461 * enum cxl_config_state - State machine for region configuration
462 * @CXL_CONFIG_IDLE: Any sysfs attribute can be written freely
463 * @CXL_CONFIG_INTERLEAVE_ACTIVE: region size has been set, no more
464 * changes to interleave_ways or interleave_granularity
465 * @CXL_CONFIG_ACTIVE: All targets have been added the region is now
467 * @CXL_CONFIG_RESET_PENDING: see commit_store()
468 * @CXL_CONFIG_COMMIT: Soft-config has been committed to hardware
470 enum cxl_config_state {
472 CXL_CONFIG_INTERLEAVE_ACTIVE,
474 CXL_CONFIG_RESET_PENDING,
479 * struct cxl_region_params - region settings
480 * @state: allow the driver to lockdown further parameter changes
481 * @uuid: unique id for persistent regions
482 * @interleave_ways: number of endpoints in the region
483 * @interleave_granularity: capacity each endpoint contributes to a stripe
484 * @res: allocated iomem capacity for this region
485 * @targets: active ordered targets in current decoder configuration
486 * @nr_targets: number of targets
488 * State transitions are protected by the cxl_region_rwsem
490 struct cxl_region_params {
491 enum cxl_config_state state;
494 int interleave_granularity;
495 struct resource *res;
496 struct cxl_endpoint_decoder *targets[CXL_DECODER_MAX_INTERLEAVE];
501 * Indicate whether this region has been assembled by autodetection or
502 * userspace assembly. Prevent endpoint decoders outside of automatic
503 * detection from being added to the region.
505 #define CXL_REGION_F_AUTO 0
508 * Require that a committed region successfully complete a teardown once
509 * any of its associated decoders have been torn down. This maintains
510 * the commit state for the region since there are committed decoders,
511 * but blocks cxl_region_probe().
513 #define CXL_REGION_F_NEEDS_RESET 1
516 * struct cxl_region - CXL region
517 * @dev: This region's device
518 * @id: This region's id. Id is globally unique across all regions
519 * @mode: Endpoint decoder allocation / access mode
520 * @type: Endpoint decoder target type
521 * @cxl_nvb: nvdimm bridge for coordinating @cxlr_pmem setup / shutdown
522 * @cxlr_pmem: (for pmem regions) cached copy of the nvdimm bridge
523 * @flags: Region state flags
524 * @params: active + config params for the region
525 * @coord: QoS access coordinates for the region
526 * @memory_notifier: notifier for setting the access coordinates to node
531 enum cxl_decoder_mode mode;
532 enum cxl_decoder_type type;
533 struct cxl_nvdimm_bridge *cxl_nvb;
534 struct cxl_pmem_region *cxlr_pmem;
536 struct cxl_region_params params;
537 struct access_coordinate coord[ACCESS_COORDINATE_MAX];
538 struct notifier_block memory_notifier;
541 struct cxl_nvdimm_bridge {
544 struct cxl_port *port;
545 struct nvdimm_bus *nvdimm_bus;
546 struct nvdimm_bus_descriptor nd_desc;
549 #define CXL_DEV_ID_LEN 19
553 struct cxl_memdev *cxlmd;
554 u8 dev_id[CXL_DEV_ID_LEN]; /* for nvdimm, string of 'serial' */
557 struct cxl_pmem_region_mapping {
558 struct cxl_memdev *cxlmd;
559 struct cxl_nvdimm *cxl_nvd;
565 struct cxl_pmem_region {
567 struct cxl_region *cxlr;
568 struct nd_region *nd_region;
569 struct range hpa_range;
571 struct cxl_pmem_region_mapping mapping[];
574 struct cxl_dax_region {
576 struct cxl_region *cxlr;
577 struct range hpa_range;
581 * struct cxl_port - logical collection of upstream port devices and
582 * downstream port devices to construct a CXL memory
584 * @dev: this port's device
585 * @uport_dev: PCI or platform device implementing the upstream port capability
586 * @host_bridge: Shortcut to the platform attach point for this port
587 * @id: id for port device-name
588 * @dports: cxl_dport instances referenced by decoders
589 * @endpoints: cxl_ep instances, endpoints that are a descendant of this port
590 * @regions: cxl_region_ref instances, regions mapped by this port
591 * @parent_dport: dport that points to this port in the parent
592 * @decoder_ida: allocator for decoder ids
593 * @reg_map: component and ras register mapping parameters
594 * @nr_dports: number of entries in @dports
595 * @hdm_end: track last allocated HDM decoder instance for allocation ordering
596 * @commit_end: cursor to track highest committed decoder for commit ordering
597 * @dead: last ep has been removed, force port re-creation
598 * @depth: How deep this port is relative to the root. depth 0 is the root.
599 * @cdat: Cached CDAT data
600 * @cdat_available: Should a CDAT attribute be available in sysfs
601 * @pci_latency: Upstream latency in picoseconds
605 struct device *uport_dev;
606 struct device *host_bridge;
608 struct xarray dports;
609 struct xarray endpoints;
610 struct xarray regions;
611 struct cxl_dport *parent_dport;
612 struct ida decoder_ida;
613 struct cxl_register_map reg_map;
628 * struct cxl_root - logical collection of root cxl_port items
630 * @port: cxl_port member
631 * @ops: cxl root operations
634 struct cxl_port port;
635 const struct cxl_root_ops *ops;
638 static inline struct cxl_root *
639 to_cxl_root(const struct cxl_port *port)
641 return container_of(port, struct cxl_root, port);
644 struct cxl_root_ops {
645 int (*qos_class)(struct cxl_root *cxl_root,
646 struct access_coordinate *coord, int entries,
650 static inline struct cxl_dport *
651 cxl_find_dport_by_dev(struct cxl_port *port, const struct device *dport_dev)
653 return xa_load(&port->dports, (unsigned long)dport_dev);
656 struct cxl_rcrb_info {
657 resource_size_t base;
662 * struct cxl_dport - CXL downstream port
663 * @dport_dev: PCI bridge or firmware device representing the downstream link
664 * @reg_map: component and ras register mapping parameters
665 * @port_id: unique hardware identifier for dport in decoder target list
666 * @rcrb: Data about the Root Complex Register Block layout
667 * @rch: Indicate whether this dport was enumerated in RCH or VH mode
668 * @port: reference to cxl_port that contains this downstream port
669 * @regs: Dport parsed register blocks
670 * @coord: access coordinates (bandwidth and latency performance attributes)
671 * @link_latency: calculated PCIe downstream latency
674 struct device *dport_dev;
675 struct cxl_register_map reg_map;
677 struct cxl_rcrb_info rcrb;
679 struct cxl_port *port;
680 struct cxl_regs regs;
681 struct access_coordinate coord[ACCESS_COORDINATE_MAX];
686 * struct cxl_ep - track an endpoint's interest in a port
687 * @ep: device that hosts a generic CXL endpoint (expander or accelerator)
688 * @dport: which dport routes to this endpoint on @port
689 * @next: cxl switch port across the link attached to @dport NULL if
690 * attached to an endpoint
694 struct cxl_dport *dport;
695 struct cxl_port *next;
699 * struct cxl_region_ref - track a region's interest in a port
700 * @port: point in topology to install this reference
701 * @decoder: decoder assigned for @region in @port
702 * @region: region for this reference
703 * @endpoints: cxl_ep references for region members beneath @port
704 * @nr_targets_set: track how many targets have been programmed during setup
705 * @nr_eps: number of endpoints beneath @port
706 * @nr_targets: number of distinct targets needed to reach @nr_eps
708 struct cxl_region_ref {
709 struct cxl_port *port;
710 struct cxl_decoder *decoder;
711 struct cxl_region *region;
712 struct xarray endpoints;
719 * The platform firmware device hosting the root is also the top of the
720 * CXL port topology. All other CXL ports have another CXL port as their
721 * parent and their ->uport_dev / host device is out-of-line of the port
724 static inline bool is_cxl_root(struct cxl_port *port)
726 return port->uport_dev == port->dev.parent;
729 int cxl_num_decoders_committed(struct cxl_port *port);
730 bool is_cxl_port(const struct device *dev);
731 struct cxl_port *to_cxl_port(const struct device *dev);
733 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
734 struct pci_bus *bus);
735 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port);
736 struct cxl_port *devm_cxl_add_port(struct device *host,
737 struct device *uport_dev,
738 resource_size_t component_reg_phys,
739 struct cxl_dport *parent_dport);
740 struct cxl_root *devm_cxl_add_root(struct device *host,
741 const struct cxl_root_ops *ops);
742 struct cxl_root *find_cxl_root(struct cxl_port *port);
743 void put_cxl_root(struct cxl_root *cxl_root);
744 DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_cxl_root(_T))
746 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
747 void cxl_bus_rescan(void);
748 void cxl_bus_drain(void);
749 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev,
750 struct cxl_dport **dport);
751 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
752 struct cxl_dport **dport);
753 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd);
755 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
756 struct device *dport, int port_id,
757 resource_size_t component_reg_phys);
758 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
759 struct device *dport_dev, int port_id,
760 resource_size_t rcrb);
762 #ifdef CONFIG_PCIEAER_CXL
763 void cxl_setup_parent_dport(struct device *host, struct cxl_dport *dport);
765 static inline void cxl_setup_parent_dport(struct device *host,
766 struct cxl_dport *dport) { }
769 struct cxl_decoder *to_cxl_decoder(struct device *dev);
770 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev);
771 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev);
772 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev);
773 bool is_root_decoder(struct device *dev);
774 bool is_switch_decoder(struct device *dev);
775 bool is_endpoint_decoder(struct device *dev);
776 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
777 unsigned int nr_targets,
778 cxl_calc_hb_fn calc_hb);
779 struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos);
780 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
781 unsigned int nr_targets);
782 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
783 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port);
784 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
785 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
786 static inline int cxl_root_decoder_autoremove(struct device *host,
787 struct cxl_root_decoder *cxlrd)
789 return cxl_decoder_autoremove(host, &cxlrd->cxlsd.cxld);
791 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint);
794 * struct cxl_endpoint_dvsec_info - Cached DVSEC info
795 * @mem_enabled: cached value of mem_enabled in the DVSEC at init time
796 * @ranges: Number of active HDM ranges this device uses.
797 * @port: endpoint port associated with this info instance
798 * @dvsec_range: cached attributes of the ranges in the DVSEC, PCIE_DEVICE
800 struct cxl_endpoint_dvsec_info {
803 struct cxl_port *port;
804 struct range dvsec_range[2];
808 struct cxl_hdm *devm_cxl_setup_hdm(struct cxl_port *port,
809 struct cxl_endpoint_dvsec_info *info);
810 int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
811 struct cxl_endpoint_dvsec_info *info);
812 int devm_cxl_add_passthrough_decoder(struct cxl_port *port);
813 int cxl_dvsec_rr_decode(struct device *dev, int dvsec,
814 struct cxl_endpoint_dvsec_info *info);
816 bool is_cxl_region(struct device *dev);
818 extern struct bus_type cxl_bus_type;
822 int (*probe)(struct device *dev);
823 void (*remove)(struct device *dev);
824 struct device_driver drv;
828 static inline struct cxl_driver *to_cxl_drv(struct device_driver *drv)
830 return container_of(drv, struct cxl_driver, drv);
833 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
834 const char *modname);
835 #define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME)
836 void cxl_driver_unregister(struct cxl_driver *cxl_drv);
838 #define module_cxl_driver(__cxl_driver) \
839 module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister)
841 #define CXL_DEVICE_NVDIMM_BRIDGE 1
842 #define CXL_DEVICE_NVDIMM 2
843 #define CXL_DEVICE_PORT 3
844 #define CXL_DEVICE_ROOT 4
845 #define CXL_DEVICE_MEMORY_EXPANDER 5
846 #define CXL_DEVICE_REGION 6
847 #define CXL_DEVICE_PMEM_REGION 7
848 #define CXL_DEVICE_DAX_REGION 8
849 #define CXL_DEVICE_PMU 9
851 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
852 #define CXL_MODALIAS_FMT "cxl:t%d"
854 struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev);
855 struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host,
856 struct cxl_port *port);
857 struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev);
858 bool is_cxl_nvdimm(struct device *dev);
859 bool is_cxl_nvdimm_bridge(struct device *dev);
860 int devm_cxl_add_nvdimm(struct cxl_port *parent_port, struct cxl_memdev *cxlmd);
861 struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_port *port);
863 #ifdef CONFIG_CXL_REGION
864 bool is_cxl_pmem_region(struct device *dev);
865 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev);
866 int cxl_add_to_region(struct cxl_port *root,
867 struct cxl_endpoint_decoder *cxled);
868 struct cxl_dax_region *to_cxl_dax_region(struct device *dev);
870 static inline bool is_cxl_pmem_region(struct device *dev)
874 static inline struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
878 static inline int cxl_add_to_region(struct cxl_port *root,
879 struct cxl_endpoint_decoder *cxled)
883 static inline struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
889 void cxl_endpoint_parse_cdat(struct cxl_port *port);
890 void cxl_switch_parse_cdat(struct cxl_port *port);
892 int cxl_endpoint_get_perf_coordinates(struct cxl_port *port,
893 struct access_coordinate *coord);
894 void cxl_region_perf_data_calculate(struct cxl_region *cxlr,
895 struct cxl_endpoint_decoder *cxled);
897 void cxl_memdev_update_perf(struct cxl_memdev *cxlmd);
899 void cxl_coordinates_combine(struct access_coordinate *out,
900 struct access_coordinate *c1,
901 struct access_coordinate *c2);
903 bool cxl_endpoint_decoder_reset_detected(struct cxl_port *port);
906 * Unit test builds overrides this to __weak, find the 'strong' version
907 * of these symbols in tools/testing/cxl/.
910 #define __mock static
913 #endif /* __CXL_H__ */