1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* Copyright(c) 2020 Intel Corporation. */ 3 4 #ifndef __CXL_H__ 5 #define __CXL_H__ 6 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> 13 #include <linux/io.h> 14 #include <linux/range.h> 15 #include <cxl/cxl.h> 16 17 extern const struct nvdimm_security_ops *cxl_security_ops; 18 19 /** 20 * DOC: cxl objects 21 * 22 * The CXL core objects like ports, decoders, and regions are shared 23 * between the subsystem drivers cxl_acpi, cxl_pci, and core drivers 24 * (port-driver, region-driver, nvdimm object-drivers... etc). 25 */ 26 27 /* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */ 28 #define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K 29 30 /* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/ 31 #define CXL_CM_OFFSET 0x1000 32 #define CXL_CM_CAP_HDR_OFFSET 0x0 33 #define CXL_CM_CAP_HDR_ID_MASK GENMASK(15, 0) 34 #define CM_CAP_HDR_CAP_ID 1 35 #define CXL_CM_CAP_HDR_VERSION_MASK GENMASK(19, 16) 36 #define CM_CAP_HDR_CAP_VERSION 1 37 #define CXL_CM_CAP_HDR_CACHE_MEM_VERSION_MASK GENMASK(23, 20) 38 #define CM_CAP_HDR_CACHE_MEM_VERSION 1 39 #define CXL_CM_CAP_HDR_ARRAY_SIZE_MASK GENMASK(31, 24) 40 #define CXL_CM_CAP_PTR_MASK GENMASK(31, 20) 41 42 #define CXL_CM_CAP_CAP_ID_RAS 0x2 43 #define CXL_CM_CAP_CAP_ID_HDM 0x5 44 #define CXL_CM_CAP_CAP_HDM_VERSION 1 45 46 /* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */ 47 #define CXL_HDM_DECODER_CAP_OFFSET 0x0 48 #define CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0) 49 #define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4) 50 #define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8) 51 #define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9) 52 #define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11) 53 #define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12) 54 #define CXL_HDM_DECODER_CTRL_OFFSET 0x4 55 #define CXL_HDM_DECODER_ENABLE BIT(1) 56 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10) 57 #define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14) 58 #define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18) 59 #define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c) 60 #define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20) 61 #define CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0) 62 #define CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4) 63 #define CXL_HDM_DECODER0_CTRL_LOCK BIT(8) 64 #define CXL_HDM_DECODER0_CTRL_COMMIT BIT(9) 65 #define CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10) 66 #define CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11) 67 #define CXL_HDM_DECODER0_CTRL_HOSTONLY BIT(12) 68 #define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24) 69 #define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28) 70 /* Target list capacity, i.e. max interleave ways for a non-endpoint decoder */ 71 #define CXL_HDM_DECODER0_TL_TARGETS 8 72 #define CXL_HDM_DECODER0_SKIP_LOW(i) CXL_HDM_DECODER0_TL_LOW(i) 73 #define CXL_HDM_DECODER0_SKIP_HIGH(i) CXL_HDM_DECODER0_TL_HIGH(i) 74 75 /* HDM decoder control register constants CXL 3.0 8.2.5.19.7 */ 76 #define CXL_DECODER_MIN_GRANULARITY 256 77 #define CXL_DECODER_MAX_ENCODED_IG 6 78 79 static inline int cxl_hdm_decoder_count(u32 cap_hdr) 80 { 81 int val = FIELD_GET(CXL_HDM_DECODER_COUNT_MASK, cap_hdr); 82 83 switch (val) { 84 case 0: 85 return 1; 86 case 1 ... 8: 87 return val * 2; 88 case 9 ... 12: 89 return (val - 4) * 4; 90 default: 91 return -ENXIO; 92 } 93 } 94 95 /* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */ 96 static inline int eig_to_granularity(u16 eig, int *granularity) 97 { 98 if (eig > CXL_DECODER_MAX_ENCODED_IG) 99 return -EINVAL; 100 *granularity = CXL_DECODER_MIN_GRANULARITY << eig; 101 return 0; 102 } 103 104 /* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */ 105 static inline int eiw_to_ways(u8 eiw, int *ways) 106 { 107 switch (eiw) { 108 case 0 ... 4: 109 *ways = 1 << eiw; 110 break; 111 case 8 ... 10: 112 *ways = 3 << (eiw - 8); 113 break; 114 default: 115 return -EINVAL; 116 } 117 118 return 0; 119 } 120 121 static inline int granularity_to_eig(int granularity, u16 *eig) 122 { 123 *eig = 0; 124 if (granularity > SZ_16K || granularity < CXL_DECODER_MIN_GRANULARITY || 125 !is_power_of_2(granularity)) 126 return -EINVAL; 127 *eig = ilog2(granularity) - 8; 128 return 0; 129 } 130 131 static inline int ways_to_eiw(unsigned int ways, u8 *eiw) 132 { 133 *eiw = 0; 134 if (ways > 16) 135 return -EINVAL; 136 if (is_power_of_2(ways)) { 137 *eiw = ilog2(ways); 138 return 0; 139 } 140 if (ways % 3) 141 return -EINVAL; 142 ways /= 3; 143 if (!is_power_of_2(ways)) 144 return -EINVAL; 145 *eiw = ilog2(ways) + 8; 146 return 0; 147 } 148 149 /* RAS Registers CXL 2.0 8.2.5.9 CXL RAS Capability Structure */ 150 #define CXL_RAS_UNCORRECTABLE_STATUS_OFFSET 0x0 151 #define CXL_RAS_UNCORRECTABLE_STATUS_MASK (GENMASK(16, 14) | GENMASK(11, 0)) 152 #define CXL_RAS_UNCORRECTABLE_MASK_OFFSET 0x4 153 #define CXL_RAS_UNCORRECTABLE_MASK_MASK (GENMASK(16, 14) | GENMASK(11, 0)) 154 #define CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK BIT(8) 155 #define CXL_RAS_UNCORRECTABLE_SEVERITY_OFFSET 0x8 156 #define CXL_RAS_UNCORRECTABLE_SEVERITY_MASK (GENMASK(16, 14) | GENMASK(11, 0)) 157 #define CXL_RAS_CORRECTABLE_STATUS_OFFSET 0xC 158 #define CXL_RAS_CORRECTABLE_STATUS_MASK GENMASK(6, 0) 159 #define CXL_RAS_CORRECTABLE_MASK_OFFSET 0x10 160 #define CXL_RAS_CORRECTABLE_MASK_MASK GENMASK(6, 0) 161 #define CXL_RAS_CAP_CONTROL_OFFSET 0x14 162 #define CXL_RAS_CAP_CONTROL_FE_MASK GENMASK(5, 0) 163 #define CXL_RAS_HEADER_LOG_OFFSET 0x18 164 #define CXL_RAS_CAPABILITY_LENGTH 0x58 165 #define CXL_HEADERLOG_SIZE SZ_64 166 #define CXL_HEADERLOG_SIZE_U32 (CXL_HEADERLOG_SIZE / sizeof(u32)) 167 168 /* 169 * The RAS UCE trace event header array was originally sized at SZ_512/sizeof(u32) 170 * = 128 u32s due to a bug. Userspace tools (rasdaemon) have grown a dependency 171 * on that 512-byte layout. Keep the trace array at 128 u32s to preserve the 172 * ABI; only CXL_HEADERLOG_SIZE_U32 (16) dwords are valid hardware data, the 173 * remainder are zero-filled. 174 */ 175 #define CXL_HEADERLOG_TRACE_SIZE SZ_512 176 #define CXL_HEADERLOG_TRACE_SIZE_U32 (CXL_HEADERLOG_TRACE_SIZE / sizeof(u32)) 177 178 /* CXL 2.0 8.2.8.1 Device Capabilities Array Register */ 179 #define CXLDEV_CAP_ARRAY_OFFSET 0x0 180 #define CXLDEV_CAP_ARRAY_CAP_ID 0 181 #define CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0) 182 #define CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32) 183 /* CXL 2.0 8.2.8.2 CXL Device Capability Header Register */ 184 #define CXLDEV_CAP_HDR_CAP_ID_MASK GENMASK(15, 0) 185 /* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */ 186 #define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1 187 #define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2 188 #define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3 189 #define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000 190 191 /* CXL 3.0 8.2.8.3.1 Event Status Register */ 192 #define CXLDEV_DEV_EVENT_STATUS_OFFSET 0x00 193 #define CXLDEV_EVENT_STATUS_INFO BIT(0) 194 #define CXLDEV_EVENT_STATUS_WARN BIT(1) 195 #define CXLDEV_EVENT_STATUS_FAIL BIT(2) 196 #define CXLDEV_EVENT_STATUS_FATAL BIT(3) 197 #define CXLDEV_EVENT_STATUS_DCD BIT(4) 198 /* 199 * Logs whose ownership follows native_cxl_error; the DCD log is always 200 * OS owned. 201 */ 202 #define CXLDEV_EVENT_STATUS_STANDARD_LOGS (CXLDEV_EVENT_STATUS_INFO | \ 203 CXLDEV_EVENT_STATUS_WARN | \ 204 CXLDEV_EVENT_STATUS_FAIL | \ 205 CXLDEV_EVENT_STATUS_FATAL) 206 207 /* CXL rev 3.0 section 8.2.9.2.4; Table 8-52 */ 208 #define CXLDEV_EVENT_INT_MODE_MASK GENMASK(1, 0) 209 #define CXLDEV_EVENT_INT_MSGNUM_MASK GENMASK(7, 4) 210 211 /* CXL 2.0 8.2.8.4 Mailbox Registers */ 212 #define CXLDEV_MBOX_CAPS_OFFSET 0x00 213 #define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0) 214 #define CXLDEV_MBOX_CAP_BG_CMD_IRQ BIT(6) 215 #define CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK GENMASK(10, 7) 216 #define CXLDEV_MBOX_CTRL_OFFSET 0x04 217 #define CXLDEV_MBOX_CTRL_DOORBELL BIT(0) 218 #define CXLDEV_MBOX_CTRL_BG_CMD_IRQ BIT(2) 219 #define CXLDEV_MBOX_CMD_OFFSET 0x08 220 #define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0) 221 #define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16) 222 #define CXLDEV_MBOX_STATUS_OFFSET 0x10 223 #define CXLDEV_MBOX_STATUS_BG_CMD BIT(0) 224 #define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32) 225 #define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18 226 #define CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0) 227 #define CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK GENMASK_ULL(22, 16) 228 #define CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK GENMASK_ULL(47, 32) 229 #define CXLDEV_MBOX_BG_CMD_COMMAND_VENDOR_MASK GENMASK_ULL(63, 48) 230 #define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20 231 232 void cxl_probe_component_regs(struct device *dev, void __iomem *base, 233 struct cxl_component_reg_map *map); 234 void cxl_probe_device_regs(struct device *dev, void __iomem *base, 235 struct cxl_device_reg_map *map); 236 int cxl_map_component_regs(const struct cxl_register_map *map, 237 struct cxl_component_regs *regs, 238 unsigned long map_mask); 239 int cxl_map_device_regs(const struct cxl_register_map *map, 240 struct cxl_device_regs *regs); 241 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs); 242 243 #define CXL_INSTANCES_COUNT -1 244 enum cxl_regloc_type; 245 int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type); 246 int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type, 247 struct cxl_register_map *map, unsigned int index); 248 int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type, 249 struct cxl_register_map *map); 250 int cxl_setup_regs(struct cxl_register_map *map); 251 struct cxl_dport; 252 int cxl_dport_map_rcd_linkcap(struct pci_dev *pdev, struct cxl_dport *dport); 253 254 #define CXL_RESOURCE_NONE ((resource_size_t) -1) 255 #define CXL_TARGET_STRLEN 20 256 257 /* 258 * cxl_decoder flags that define the type of memory / devices this 259 * decoder supports as well as configuration lock status See "CXL 2.0 260 * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details. 261 * Additionally indicate whether decoder settings were autodetected, 262 * user customized. 263 */ 264 #define CXL_DECODER_F_RAM BIT(0) 265 #define CXL_DECODER_F_PMEM BIT(1) 266 #define CXL_DECODER_F_TYPE2 BIT(2) 267 #define CXL_DECODER_F_TYPE3 BIT(3) 268 #define CXL_DECODER_F_LOCK BIT(4) 269 #define CXL_DECODER_F_ENABLE BIT(5) 270 #define CXL_DECODER_F_NORMALIZED_ADDRESSING BIT(6) 271 #define CXL_DECODER_F_RESET_MASK (CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK) 272 273 enum cxl_decoder_type { 274 CXL_DECODER_DEVMEM = 2, 275 CXL_DECODER_HOSTONLYMEM = 3, 276 }; 277 278 /* 279 * Current specification goes up to 8, double that seems a reasonable 280 * software max for the foreseeable future 281 */ 282 #define CXL_DECODER_MAX_INTERLEAVE 16 283 284 #define CXL_QOS_CLASS_INVALID -1 285 286 /** 287 * struct cxl_decoder - Common CXL HDM Decoder Attributes 288 * @dev: this decoder's device 289 * @id: kernel device name id 290 * @hpa_range: Host physical address range mapped by this decoder 291 * @interleave_ways: number of cxl_dports in this decode 292 * @interleave_granularity: data stride per dport 293 * @target_type: accelerator vs expander (type2 vs type3) selector 294 * @region: currently assigned region for this decoder 295 * @flags: memory type capabilities and locking 296 * @target_map: cached copy of hardware port-id list, available at init 297 * before all @dport objects have been instantiated. While 298 * dport id is 8bit, CFMWS interleave targets are 32bits. 299 * @commit: device/decoder-type specific callback to commit settings to hw 300 * @reset: device/decoder-type specific callback to reset hw settings 301 */ 302 struct cxl_decoder { 303 struct device dev; 304 int id; 305 struct range hpa_range; 306 int interleave_ways; 307 int interleave_granularity; 308 enum cxl_decoder_type target_type; 309 struct cxl_region *region; 310 unsigned long flags; 311 u32 target_map[CXL_DECODER_MAX_INTERLEAVE]; 312 int (*commit)(struct cxl_decoder *cxld); 313 void (*reset)(struct cxl_decoder *cxld); 314 }; 315 316 /* 317 * Track whether this decoder is free for userspace provisioning, reserved for 318 * region autodiscovery, whether it is started connecting (awaiting other 319 * peers), or has completed auto assembly. 320 */ 321 enum cxl_decoder_state { 322 CXL_DECODER_STATE_MANUAL, 323 CXL_DECODER_STATE_AUTO, 324 CXL_DECODER_STATE_AUTO_STAGED, 325 }; 326 327 /** 328 * struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder 329 * @cxld: base cxl_decoder_object 330 * @dpa_res: actively claimed DPA span of this decoder 331 * @skip: offset into @dpa_res where @cxld.hpa_range maps 332 * @state: autodiscovery state 333 * @part: partition index this decoder maps 334 * @pos: interleave position in @cxld.region 335 */ 336 struct cxl_endpoint_decoder { 337 struct cxl_decoder cxld; 338 struct resource *dpa_res; 339 resource_size_t skip; 340 enum cxl_decoder_state state; 341 int part; 342 int pos; 343 }; 344 345 /* 346 * The common case is decoders with no reservation, but also handle 347 * decoders with a zero-sized reservation that firmware may install for 348 * security lockdown purposes. 349 */ 350 static inline bool cxled_empty(struct cxl_endpoint_decoder *cxled) 351 { 352 return !cxled->dpa_res || !resource_size(cxled->dpa_res); 353 } 354 355 /** 356 * struct cxl_switch_decoder - Switch specific CXL HDM Decoder 357 * @cxld: base cxl_decoder object 358 * @nr_targets: number of elements in @target 359 * @target: active ordered target list in current decoder configuration 360 * 361 * The 'switch' decoder type represents the decoder instances of cxl_port's that 362 * route from the root of a CXL memory decode topology to the endpoints. They 363 * come in two flavors, root-level decoders, statically defined by platform 364 * firmware, and mid-level decoders, where interleave-granularity, 365 * interleave-width, and the target list are mutable. 366 */ 367 struct cxl_switch_decoder { 368 struct cxl_decoder cxld; 369 int nr_targets; 370 struct cxl_dport *target[]; 371 }; 372 373 struct cxl_root_decoder; 374 /** 375 * struct cxl_rd_ops - CXL root decoder callback operations 376 * @hpa_to_spa: Convert host physical address to system physical address 377 * @spa_to_hpa: Convert system physical address to host physical address 378 */ 379 struct cxl_rd_ops { 380 u64 (*hpa_to_spa)(struct cxl_root_decoder *cxlrd, u64 hpa); 381 u64 (*spa_to_hpa)(struct cxl_root_decoder *cxlrd, u64 spa); 382 }; 383 384 /** 385 * struct cxl_root_decoder - Static platform CXL address decoder 386 * @res: host / parent resource for region allocations 387 * @cache_size: extended linear cache size if exists, otherwise zero. 388 * @region_id: region id for next region provisioning event 389 * @platform_data: platform specific configuration data 390 * @regions_lock: sync region discovery, construction, and deletion 391 * @regions: regions to remove at root decoder destruct time 392 * @dead: root decoder dead to region creation 393 * @qos_class: QoS performance class cookie 394 * @ops: CXL root decoder operations 395 * @cxlsd: base cxl switch decoder 396 */ 397 struct cxl_root_decoder { 398 struct resource *res; 399 resource_size_t cache_size; 400 atomic_t region_id; 401 void *platform_data; 402 struct mutex regions_lock; 403 struct xarray regions; 404 bool dead; 405 int qos_class; 406 struct cxl_rd_ops ops; 407 struct cxl_switch_decoder cxlsd; 408 }; 409 410 /* 411 * enum cxl_config_state - State machine for region configuration 412 * @CXL_CONFIG_IDLE: Any sysfs attribute can be written freely 413 * @CXL_CONFIG_INTERLEAVE_ACTIVE: region size has been set, no more 414 * changes to interleave_ways or interleave_granularity 415 * @CXL_CONFIG_ACTIVE: All targets have been added the region is now 416 * active 417 * @CXL_CONFIG_RESET_PENDING: see commit_store() 418 * @CXL_CONFIG_COMMIT: Soft-config has been committed to hardware 419 */ 420 enum cxl_config_state { 421 CXL_CONFIG_IDLE, 422 CXL_CONFIG_INTERLEAVE_ACTIVE, 423 CXL_CONFIG_ACTIVE, 424 CXL_CONFIG_RESET_PENDING, 425 CXL_CONFIG_COMMIT, 426 }; 427 428 /** 429 * struct cxl_region_params - region settings 430 * @state: allow the driver to lockdown further parameter changes 431 * @uuid: unique id for persistent regions 432 * @interleave_ways: number of endpoints in the region 433 * @interleave_granularity: capacity each endpoint contributes to a stripe 434 * @res: allocated iomem capacity for this region 435 * @targets: active ordered targets in current decoder configuration 436 * @nr_targets: number of targets 437 * @cache_size: extended linear cache size if exists, otherwise zero. 438 * 439 * State transitions are protected by cxl_rwsem.region 440 */ 441 struct cxl_region_params { 442 enum cxl_config_state state; 443 uuid_t uuid; 444 int interleave_ways; 445 int interleave_granularity; 446 struct resource *res; 447 struct cxl_endpoint_decoder *targets[CXL_DECODER_MAX_INTERLEAVE]; 448 int nr_targets; 449 resource_size_t cache_size; 450 }; 451 452 /* 453 * Indicate whether this region has been assembled by autodetection or 454 * userspace assembly. Prevent endpoint decoders outside of automatic 455 * detection from being added to the region. 456 */ 457 #define CXL_REGION_F_AUTO 0 458 459 /* 460 * Require that a committed region successfully complete a teardown once 461 * any of its associated decoders have been torn down. This maintains 462 * the commit state for the region since there are committed decoders, 463 * but blocks cxl_region_probe(). 464 */ 465 #define CXL_REGION_F_NEEDS_RESET 1 466 467 /* 468 * Indicate whether this region is locked due to 1 or more decoders that have 469 * been locked. The approach of all or nothing is taken with regard to the 470 * locked attribute. CXL_REGION_F_NEEDS_RESET should not be set if this flag is 471 * set. 472 */ 473 #define CXL_REGION_F_LOCK 2 474 475 /* 476 * Indicate Normalized Addressing. Use it to disable SPA conversion if 477 * HPA != SPA and an address translation callback handler does not 478 * exist. Flag is needed by AMD Zen5 platforms. 479 */ 480 #define CXL_REGION_F_NORMALIZED_ADDRESSING 3 481 482 /** 483 * struct cxl_region - CXL region 484 * @dev: This region's device 485 * @id: This region's id. Id is globally unique across all regions 486 * @cxlrd: Region's root decoder 487 * @hpa_range: Address range occupied by the region 488 * @mode: Operational mode of the mapped capacity 489 * @type: Endpoint decoder target type 490 * @cxl_nvb: nvdimm bridge for coordinating @cxlr_pmem setup / shutdown 491 * @cxlr_pmem: (for pmem regions) cached copy of the nvdimm bridge 492 * @flags: Region state flags 493 * @params: active + config params for the region 494 * @coord: QoS access coordinates for the region 495 * @node_notifier: notifier for setting the access coordinates to node 496 * @adist_notifier: notifier for calculating the abstract distance of node 497 * @mce_notifier: notifier for MCE 498 */ 499 struct cxl_region { 500 struct device dev; 501 int id; 502 struct cxl_root_decoder *cxlrd; 503 struct range hpa_range; 504 enum cxl_partition_mode mode; 505 enum cxl_decoder_type type; 506 struct cxl_nvdimm_bridge *cxl_nvb; 507 struct cxl_pmem_region *cxlr_pmem; 508 unsigned long flags; 509 struct cxl_region_params params; 510 struct access_coordinate coord[ACCESS_COORDINATE_MAX]; 511 struct notifier_block node_notifier; 512 struct notifier_block adist_notifier; 513 struct notifier_block mce_notifier; 514 }; 515 516 struct cxl_nvdimm_bridge { 517 int id; 518 struct device dev; 519 struct cxl_port *port; 520 struct nvdimm_bus *nvdimm_bus; 521 struct nvdimm_bus_descriptor nd_desc; 522 }; 523 524 /* Holds a u64 serial as a decimal string: up to 20 digits + NUL */ 525 #define CXL_DEV_ID_LEN 21 526 527 enum { 528 CXL_NVD_F_INVALIDATED = 0, 529 }; 530 531 struct cxl_nvdimm { 532 struct device dev; 533 struct cxl_memdev *cxlmd; 534 u8 dev_id[CXL_DEV_ID_LEN]; /* for nvdimm, string of 'serial' */ 535 u64 dirty_shutdowns; 536 unsigned long flags; 537 }; 538 539 struct 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 547 struct cxl_pmem_region { 548 struct device dev; 549 struct cxl_region *cxlr; 550 struct nd_region *nd_region; 551 struct range hpa_range; 552 int nr_mappings; 553 struct cxl_pmem_region_mapping mapping[]; 554 }; 555 556 struct cxl_dax_region { 557 struct device dev; 558 struct cxl_region *cxlr; 559 struct range hpa_range; 560 }; 561 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 567 * @uport_dev: PCI or platform device implementing the upstream port capability 568 * @host_bridge: Shortcut to the platform attach point for this port 569 * @id: id for port device-name 570 * @dports: cxl_dport instances referenced by decoders 571 * @endpoints: cxl_ep instances, endpoints that are a descendant of this port 572 * @regions: cxl_region_ref instances, regions mapped by this port 573 * @parent_dport: dport that points to this port in the parent 574 * @decoder_ida: allocator for decoder ids 575 * @reg_map: component and ras register mapping parameters 576 * @regs: mapped component registers 577 * @nr_dports: number of entries in @dports 578 * @hdm_end: track last allocated HDM decoder instance for allocation ordering 579 * @commit_end: cursor to track highest committed decoder for commit ordering 580 * @dead: last ep has been removed, force port re-creation 581 * @depth: How deep this port is relative to the root. depth 0 is the root. 582 * @cdat: Cached CDAT data 583 * @cdat_available: Should a CDAT attribute be available in sysfs 584 * @pci_latency: Upstream latency in picoseconds 585 * @component_reg_phys: Physical address of component register 586 */ 587 struct cxl_port { 588 struct device dev; 589 struct device *uport_dev; 590 struct device *host_bridge; 591 int id; 592 struct xarray dports; 593 struct xarray endpoints; 594 struct xarray regions; 595 struct cxl_dport *parent_dport; 596 struct ida decoder_ida; 597 struct cxl_register_map reg_map; 598 struct cxl_component_regs regs; 599 int nr_dports; 600 int hdm_end; 601 int commit_end; 602 bool dead; 603 unsigned int depth; 604 struct cxl_cdat { 605 void *table; 606 size_t length; 607 } cdat; 608 bool cdat_available; 609 long pci_latency; 610 resource_size_t component_reg_phys; 611 }; 612 613 struct cxl_root; 614 615 struct cxl_root_ops { 616 int (*qos_class)(struct cxl_root *cxl_root, 617 struct access_coordinate *coord, int entries, 618 int *qos_class); 619 int (*translation_setup_root)(struct cxl_root *cxl_root, void *data); 620 }; 621 622 /** 623 * struct cxl_root - logical collection of root cxl_port items 624 * 625 * @port: cxl_port member 626 * @ops: cxl root operations 627 */ 628 struct cxl_root { 629 struct cxl_port port; 630 struct cxl_root_ops ops; 631 }; 632 633 static inline struct cxl_root * 634 to_cxl_root(const struct cxl_port *port) 635 { 636 return container_of(port, struct cxl_root, port); 637 } 638 639 static inline struct cxl_dport * 640 cxl_find_dport_by_dev(struct cxl_port *port, const struct device *dport_dev) 641 { 642 return xa_load(&port->dports, (unsigned long)dport_dev); 643 } 644 645 struct cxl_rcrb_info { 646 resource_size_t base; 647 u16 aer_cap; 648 }; 649 650 /** 651 * struct cxl_dport - CXL downstream port 652 * @dport_dev: PCI bridge or firmware device representing the downstream link 653 * @reg_map: component and ras register mapping parameters 654 * @port_id: unique hardware identifier for dport in decoder target list 655 * @rcrb: Data about the Root Complex Register Block layout 656 * @rch: Indicate whether this dport was enumerated in RCH or VH mode 657 * @port: reference to cxl_port that contains this downstream port 658 * @regs: Dport parsed register blocks 659 * @coord: access coordinates (bandwidth and latency performance attributes) 660 * @link_latency: calculated PCIe downstream latency 661 * @gpf_dvsec: Cached GPF port DVSEC 662 */ 663 struct cxl_dport { 664 struct device *dport_dev; 665 struct cxl_register_map reg_map; 666 int port_id; 667 struct cxl_rcrb_info rcrb; 668 bool rch; 669 struct cxl_port *port; 670 struct cxl_regs regs; 671 struct access_coordinate coord[ACCESS_COORDINATE_MAX]; 672 long link_latency; 673 int gpf_dvsec; 674 }; 675 676 /** 677 * struct cxl_ep - track an endpoint's interest in a port 678 * @ep: device that hosts a generic CXL endpoint (expander or accelerator) 679 * @dport: which dport routes to this endpoint on @port 680 * @next: cxl switch port across the link attached to @dport NULL if 681 * attached to an endpoint 682 */ 683 struct cxl_ep { 684 struct device *ep; 685 struct cxl_dport *dport; 686 struct cxl_port *next; 687 }; 688 689 /** 690 * struct cxl_region_ref - track a region's interest in a port 691 * @port: point in topology to install this reference 692 * @decoder: decoder assigned for @region in @port 693 * @region: region for this reference 694 * @endpoints: cxl_ep references for region members beneath @port 695 * @nr_targets_set: track how many targets have been programmed during setup 696 * @nr_eps: number of endpoints beneath @port 697 * @nr_targets: number of distinct targets needed to reach @nr_eps 698 */ 699 struct cxl_region_ref { 700 struct cxl_port *port; 701 struct cxl_decoder *decoder; 702 struct cxl_region *region; 703 struct xarray endpoints; 704 int nr_targets_set; 705 int nr_eps; 706 int nr_targets; 707 }; 708 709 /* 710 * The platform firmware device hosting the root is also the top of the 711 * CXL port topology. All other CXL ports have another CXL port as their 712 * parent and their ->uport_dev / host device is out-of-line of the port 713 * ancestry. 714 */ 715 static inline bool is_cxl_root(struct cxl_port *port) 716 { 717 return port->uport_dev == port->dev.parent; 718 } 719 720 /* 721 * Return the owner device of the devm allocations of @port's dports. They 722 * are freed when that device unbinds, so hold device_lock() on the returned 723 * device across any lookup and use of a 'struct cxl_dport'. 724 */ 725 static inline struct device *cxl_port_dport_host(struct cxl_port *port) 726 { 727 if (is_cxl_root(port)) 728 return port->uport_dev; 729 return &port->dev; 730 } 731 732 /* Address translation functions exported to cxl_translate test module only */ 733 int cxl_validate_translation_params(u8 eiw, u16 eig, int pos); 734 u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig); 735 u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig); 736 int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig); 737 struct cxl_cxims_data { 738 int nr_maps; 739 u64 xormaps[] __counted_by(nr_maps); 740 }; 741 742 #if IS_ENABLED(CONFIG_CXL_ACPI) 743 u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw); 744 #else 745 static inline u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw) 746 { 747 return ULLONG_MAX; 748 } 749 #endif 750 751 int cxl_num_decoders_committed(struct cxl_port *port); 752 bool is_cxl_port(const struct device *dev); 753 struct cxl_port *to_cxl_port(const struct device *dev); 754 struct cxl_port *parent_port_of(struct cxl_port *port); 755 void cxl_port_commit_reap(struct cxl_decoder *cxld); 756 struct pci_bus; 757 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev, 758 struct pci_bus *bus); 759 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port); 760 struct cxl_port *devm_cxl_add_port(struct device *host, 761 struct device *uport_dev, 762 resource_size_t component_reg_phys, 763 struct cxl_dport *parent_dport); 764 struct cxl_root *devm_cxl_add_root(struct device *host); 765 int devm_cxl_add_endpoint(struct device *host, struct cxl_memdev *cxlmd, 766 struct cxl_dport *parent_dport); 767 struct cxl_root *find_cxl_root(struct cxl_port *port); 768 769 DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_device(&_T->port.dev)) 770 DEFINE_FREE(put_cxl_port, struct cxl_port *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev)) 771 DEFINE_FREE(put_cxl_root_decoder, struct cxl_root_decoder *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev)) 772 DEFINE_FREE(put_cxl_region, struct cxl_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev)) 773 DEFINE_FREE(put_cxl_dax_region, struct cxl_dax_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev)) 774 775 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd); 776 void cxl_bus_rescan(void); 777 void cxl_bus_drain(void); 778 struct cxl_dport *cxl_pci_find_dport(struct pci_dev *pdev, 779 struct cxl_port *port); 780 struct cxl_dport *cxl_mem_find_dport(struct cxl_memdev *cxlmd, 781 struct cxl_port *port); 782 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev); 783 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd); 784 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd); 785 786 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port, 787 struct device *dport, int port_id, 788 resource_size_t component_reg_phys); 789 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port, 790 struct device *dport_dev, int port_id, 791 resource_size_t rcrb); 792 793 #ifdef CONFIG_CXL_ATL 794 void cxl_setup_prm_address_translation(struct cxl_root *cxl_root); 795 #else 796 static inline 797 void cxl_setup_prm_address_translation(struct cxl_root *cxl_root) {} 798 #endif 799 800 struct cxl_decoder *to_cxl_decoder(struct device *dev); 801 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev); 802 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev); 803 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev); 804 bool is_root_decoder(struct device *dev); 805 bool is_switch_decoder(struct device *dev); 806 bool is_endpoint_decoder(struct device *dev); 807 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port, 808 unsigned int nr_targets); 809 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port, 810 unsigned int nr_targets); 811 int cxl_decoder_add(struct cxl_decoder *cxld); 812 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port); 813 int cxl_decoder_add_locked(struct cxl_decoder *cxld); 814 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld); 815 static inline int cxl_root_decoder_autoremove(struct device *host, 816 struct cxl_root_decoder *cxlrd) 817 { 818 return cxl_decoder_autoremove(host, &cxlrd->cxlsd.cxld); 819 } 820 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint); 821 822 /** 823 * struct cxl_endpoint_dvsec_info - Cached DVSEC info 824 * @mem_enabled: cached value of mem_enabled in the DVSEC at init time 825 * @ranges: Number of active HDM ranges this device uses. 826 * @port: endpoint port associated with this info instance 827 * @dvsec_range: cached attributes of the ranges in the DVSEC, PCIE_DEVICE 828 */ 829 struct cxl_endpoint_dvsec_info { 830 bool mem_enabled; 831 int ranges; 832 struct cxl_port *port; 833 struct range dvsec_range[2]; 834 }; 835 836 int devm_cxl_switch_port_decoders_setup(struct cxl_port *port); 837 int devm_cxl_endpoint_decoders_setup(struct cxl_port *port); 838 void cxl_port_update_decoder_targets(struct cxl_port *port, 839 struct cxl_dport *dport); 840 int cxl_port_setup_regs(struct cxl_port *port, 841 resource_size_t component_reg_phys); 842 843 struct cxl_dev_state; 844 int cxl_dvsec_rr_decode(struct cxl_dev_state *cxlds, 845 struct cxl_endpoint_dvsec_info *info); 846 847 bool is_cxl_region(struct device *dev); 848 849 extern const struct bus_type cxl_bus_type; 850 851 /* 852 * Note, add_dport() is expressly for the cxl_port driver. TODO: investigate a 853 * type-safe driver model where probe()/remove() take the type of object implied 854 * by @id and the add_dport() op only defined for the CXL_DEVICE_PORT driver 855 * template. 856 */ 857 struct cxl_driver { 858 const char *name; 859 int (*probe)(struct device *dev); 860 void (*remove)(struct device *dev); 861 struct cxl_dport *(*add_dport)(struct cxl_port *port, 862 struct device *dport_dev); 863 struct device_driver drv; 864 int id; 865 }; 866 867 #define to_cxl_drv(__drv) container_of_const(__drv, struct cxl_driver, drv) 868 869 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner, 870 const char *modname); 871 #define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME) 872 void cxl_driver_unregister(struct cxl_driver *cxl_drv); 873 874 #define module_cxl_driver(__cxl_driver) \ 875 module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister) 876 877 #define CXL_DEVICE_NVDIMM_BRIDGE 1 878 #define CXL_DEVICE_NVDIMM 2 879 #define CXL_DEVICE_PORT 3 880 #define CXL_DEVICE_ROOT 4 881 #define CXL_DEVICE_MEMORY_EXPANDER 5 882 #define CXL_DEVICE_REGION 6 883 #define CXL_DEVICE_PMEM_REGION 7 884 #define CXL_DEVICE_DAX_REGION 8 885 #define CXL_DEVICE_PMU 9 886 887 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*") 888 #define CXL_MODALIAS_FMT "cxl:t%d" 889 890 struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev); 891 struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host, 892 struct cxl_port *port); 893 struct cxl_nvdimm_bridge *__devm_cxl_add_nvdimm_bridge(struct device *host, 894 struct cxl_port *port); 895 struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev); 896 bool is_cxl_nvdimm(struct device *dev); 897 int devm_cxl_add_nvdimm(struct device *host, struct cxl_port *port, 898 struct cxl_memdev *cxlmd); 899 struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_port *port); 900 901 #ifdef CONFIG_CXL_REGION 902 bool is_cxl_pmem_region(struct device *dev); 903 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev); 904 int cxl_add_to_region(struct cxl_endpoint_decoder *cxled); 905 struct cxl_dax_region *to_cxl_dax_region(struct device *dev); 906 bool cxl_region_contains_resource(const struct resource *res); 907 #else 908 static inline bool is_cxl_pmem_region(struct device *dev) 909 { 910 return false; 911 } 912 static inline struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev) 913 { 914 return NULL; 915 } 916 static inline int cxl_add_to_region(struct cxl_endpoint_decoder *cxled) 917 { 918 return 0; 919 } 920 static inline struct cxl_dax_region *to_cxl_dax_region(struct device *dev) 921 { 922 return NULL; 923 } 924 static inline bool cxl_region_contains_resource(const struct resource *res) 925 { 926 return false; 927 } 928 #endif 929 930 void cxl_endpoint_parse_cdat(struct cxl_port *port); 931 void cxl_switch_parse_cdat(struct cxl_dport *dport); 932 933 int cxl_endpoint_get_perf_coordinates(struct cxl_port *port, 934 struct access_coordinate *coord); 935 void cxl_region_perf_data_calculate(struct cxl_region *cxlr, 936 struct cxl_endpoint_decoder *cxled); 937 void cxl_region_shared_upstream_bandwidth_update(struct cxl_region *cxlr); 938 939 void cxl_memdev_update_perf(struct cxl_memdev *cxlmd); 940 941 void cxl_coordinates_combine(struct access_coordinate *out, 942 struct access_coordinate *c1, 943 struct access_coordinate *c2); 944 945 bool cxl_endpoint_decoder_reset_detected(struct cxl_port *port); 946 struct cxl_dport *devm_cxl_add_dport_by_dev(struct cxl_port *port, 947 struct device *dport_dev); 948 949 /* 950 * Unit test builds overrides this to __weak, find the 'strong' version 951 * of these symbols in tools/testing/cxl/. 952 */ 953 #ifndef __mock 954 #define __mock static 955 #endif 956 957 u16 cxl_gpf_get_dvsec(struct device *dev); 958 959 #endif /* __CXL_H__ */ 960