/* SPDX-License-Identifier: GPL-2.0 */1/* Copyright(c) 2020 Intel Corporation. */2/* Copyright(c) 2026 Advanced Micro Devices, Inc. */34#ifndef __CXL_CXL_H__5#define __CXL_CXL_H__67#include <linux/node.h>8#include <linux/ioport.h>9#include <cxl/mailbox.h>1011/**12* enum cxl_devtype - delineate type-2 from a generic type-3 device13* @CXL_DEVTYPE_DEVMEM: Vendor specific CXL Type-2 device implementing HDM-D or14* HDM-DB, no requirement that this device implements a15* mailbox, or other memory-device-standard manageability16* flows.17* @CXL_DEVTYPE_CLASSMEM: Common class definition of a CXL Type-3 device with18* HDM-H and class-mandatory memory device registers19*/20enum cxl_devtype {21CXL_DEVTYPE_DEVMEM,22CXL_DEVTYPE_CLASSMEM,23};2425struct device;2627/*28* Using struct_group() allows for per register-block-type helper routines,29* without requiring block-type agnostic code to include the prefix.30*/31struct cxl_regs {32/*33* Common set of CXL Component register block base pointers34* @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure35* @ras: CXL 2.0 8.2.5.9 CXL RAS Capability Structure36*/37struct_group_tagged(cxl_component_regs, component,38void __iomem *hdm_decoder;39void __iomem *ras;40);41/*42* Common set of CXL Device register block base pointers43* @status: CXL 2.0 8.2.8.3 Device Status Registers44* @mbox: CXL 2.0 8.2.8.4 Mailbox Registers45* @memdev: CXL 2.0 8.2.8.5 Memory Device Registers46*/47struct_group_tagged(cxl_device_regs, device_regs,48void __iomem *status, *mbox, *memdev;49);5051struct_group_tagged(cxl_pmu_regs, pmu_regs,52void __iomem *pmu;53);5455/*56* RCH downstream port specific RAS register57* @aer: CXL 3.0 8.2.1.1 RCH Downstream Port RCRB58*/59struct_group_tagged(cxl_rch_regs, rch_regs,60void __iomem *dport_aer;61);6263/*64* RCD upstream port specific PCIe cap register65* @pcie_cap: CXL 3.0 8.2.1.2 RCD Upstream Port RCRB66*/67struct_group_tagged(cxl_rcd_regs, rcd_regs,68void __iomem *rcd_pcie_cap;69);70};7172struct cxl_reg_map {73bool valid;74int id;75unsigned long offset;76unsigned long size;77};7879struct cxl_component_reg_map {80struct cxl_reg_map hdm_decoder;81struct cxl_reg_map ras;82};8384struct cxl_device_reg_map {85struct cxl_reg_map status;86struct cxl_reg_map mbox;87struct cxl_reg_map memdev;88};8990struct cxl_pmu_reg_map {91struct cxl_reg_map pmu;92};9394/**95* struct cxl_register_map - DVSEC harvested register block mapping parameters96* @host: device for devm operations and logging97* @base: virtual base of the register-block-BAR + @block_offset98* @resource: physical resource base of the register block99* @max_size: maximum mapping size to perform register search100* @reg_type: see enum cxl_regloc_type101* @component_map: cxl_reg_map for component registers102* @device_map: cxl_reg_maps for device registers103* @pmu_map: cxl_reg_maps for CXL Performance Monitoring Units104*/105struct cxl_register_map {106struct device *host;107void __iomem *base;108resource_size_t resource;109resource_size_t max_size;110u8 reg_type;111union {112struct cxl_component_reg_map component_map;113struct cxl_device_reg_map device_map;114struct cxl_pmu_reg_map pmu_map;115};116};117118/**119* struct cxl_dpa_perf - DPA performance property entry120* @dpa_range: range for DPA address121* @coord: QoS performance data (i.e. latency, bandwidth)122* @cdat_coord: raw QoS performance data from CDAT123* @qos_class: QoS Class cookies124*/125struct cxl_dpa_perf {126struct range dpa_range;127struct access_coordinate coord[ACCESS_COORDINATE_MAX];128struct access_coordinate cdat_coord[ACCESS_COORDINATE_MAX];129int qos_class;130};131132enum cxl_partition_mode {133CXL_PARTMODE_RAM,134CXL_PARTMODE_PMEM,135};136137/**138* struct cxl_dpa_partition - DPA partition descriptor139* @res: shortcut to the partition in the DPA resource tree (cxlds->dpa_res)140* @perf: performance attributes of the partition from CDAT141* @mode: operation mode for the DPA capacity, e.g. ram, pmem, dynamic...142*/143struct cxl_dpa_partition {144struct resource res;145struct cxl_dpa_perf perf;146enum cxl_partition_mode mode;147};148149#define CXL_NR_PARTITIONS_MAX 2150151/**152* struct cxl_dev_state - The driver device state153*154* cxl_dev_state represents the CXL driver/device state. It provides an155* interface to mailbox commands as well as some cached data about the device.156* Currently only memory devices are represented.157*158* @dev: The device associated with this CXL state159* @cxlmd: The device representing the CXL.mem capabilities of @dev160* @reg_map: component and ras register mapping parameters161* @regs: Parsed register blocks162* @cxl_dvsec: Offset to the PCIe device DVSEC163* @rcd: operating in RCD mode (CXL 3.0 9.11.8 CXL Devices Attached to an RCH)164* @media_ready: Indicate whether the device media is usable165* @dpa_res: Overall DPA resource tree for the device166* @part: DPA partition array167* @nr_partitions: Number of DPA partitions168* @serial: PCIe Device Serial Number169* @type: Generic Memory Class device or Vendor Specific Memory device170* @cxl_mbox: CXL mailbox context171* @cxlfs: CXL features context172*/173struct cxl_dev_state {174/* public for Type2 drivers */175struct device *dev;176struct cxl_memdev *cxlmd;177178/* private for Type2 drivers */179struct cxl_register_map reg_map;180struct cxl_device_regs regs;181int cxl_dvsec;182bool rcd;183bool media_ready;184struct resource dpa_res;185struct cxl_dpa_partition part[CXL_NR_PARTITIONS_MAX];186unsigned int nr_partitions;187u64 serial;188enum cxl_devtype type;189struct cxl_mailbox cxl_mbox;190#ifdef CONFIG_CXL_FEATURES191struct cxl_features_state *cxlfs;192#endif193};194195struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,196enum cxl_devtype type,197u64 serial, u16 dvsec,198size_t size, bool has_mbox);199200/**201* cxl_dev_state_create - safely create and cast a cxl dev state embedded in a202* driver specific struct.203*204* @parent: device behind the request205* @type: CXL device type206* @serial: device identification207* @dvsec: dvsec capability offset208* @drv_struct: driver struct embedding a cxl_dev_state struct209* @member: name of the struct cxl_dev_state member in drv_struct210* @mbox: true if mailbox supported211*212* Returns a pointer to the drv_struct allocated and embedding a cxl_dev_state213* struct initialized.214*215* Introduced for Type2 driver support.216*/217#define devm_cxl_dev_state_create(parent, type, serial, dvsec, drv_struct, member, mbox) \218({ \219static_assert(__same_type(struct cxl_dev_state, \220((drv_struct *)NULL)->member)); \221static_assert(offsetof(drv_struct, member) == 0); \222(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec, \223sizeof(drv_struct), mbox); \224})225#endif /* __CXL_CXL_H__ */226227228