cxl.h 19.3 KB
Newer Older
B
Ben Widawsky 已提交
1 2 3 4 5 6
/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright(c) 2020 Intel Corporation. */

#ifndef __CXL_H__
#define __CXL_H__

7
#include <linux/libnvdimm.h>
B
Ben Widawsky 已提交
8 9
#include <linux/bitfield.h>
#include <linux/bitops.h>
10
#include <linux/log2.h>
B
Ben Widawsky 已提交
11 12
#include <linux/io.h>

13 14 15 16 17 18 19 20
/**
 * DOC: cxl objects
 *
 * The CXL core objects like ports, decoders, and regions are shared
 * between the subsystem drivers cxl_acpi, cxl_pci, and core drivers
 * (port-driver, region-driver, nvdimm object-drivers... etc).
 */

21 22 23
/* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
#define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
#define CXL_CM_OFFSET 0x1000
#define CXL_CM_CAP_HDR_OFFSET 0x0
#define   CXL_CM_CAP_HDR_ID_MASK GENMASK(15, 0)
#define     CM_CAP_HDR_CAP_ID 1
#define   CXL_CM_CAP_HDR_VERSION_MASK GENMASK(19, 16)
#define     CM_CAP_HDR_CAP_VERSION 1
#define   CXL_CM_CAP_HDR_CACHE_MEM_VERSION_MASK GENMASK(23, 20)
#define     CM_CAP_HDR_CACHE_MEM_VERSION 1
#define   CXL_CM_CAP_HDR_ARRAY_SIZE_MASK GENMASK(31, 24)
#define CXL_CM_CAP_PTR_MASK GENMASK(31, 20)

#define   CXL_CM_CAP_CAP_ID_HDM 0x5
#define   CXL_CM_CAP_CAP_HDM_VERSION 1

/* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */
#define CXL_HDM_DECODER_CAP_OFFSET 0x0
#define   CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
#define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
#define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
#define   CXL_HDM_DECODER_ENABLE BIT(1)
#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)
#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
#define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
#define   CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
#define   CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
#define   CXL_HDM_DECODER0_CTRL_LOCK BIT(8)
#define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
#define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
#define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
#define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
#define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
D
Dan Williams 已提交
60 61
#define CXL_HDM_DECODER0_SKIP_LOW(i) CXL_HDM_DECODER0_TL_LOW(i)
#define CXL_HDM_DECODER0_SKIP_HIGH(i) CXL_HDM_DECODER0_TL_HIGH(i)
62

63 64 65 66 67 68 69
static inline int cxl_hdm_decoder_count(u32 cap_hdr)
{
	int val = FIELD_GET(CXL_HDM_DECODER_COUNT_MASK, cap_hdr);

	return val ? val * 2 : 1;
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */
static inline int cxl_to_granularity(u16 ig, unsigned int *val)
{
	if (ig > 6)
		return -EINVAL;
	*val = 256 << ig;
	return 0;
}

/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */
static inline int cxl_to_ways(u8 eniw, unsigned int *val)
{
	switch (eniw) {
	case 0 ... 4:
		*val = 1 << eniw;
		break;
	case 8 ... 10:
		*val = 3 << (eniw - 8);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static inline int granularity_to_cxl(int g, u16 *ig)
{
	if (g > SZ_16K || g < 256 || !is_power_of_2(g))
		return -EINVAL;
	*ig = ilog2(g) - 8;
	return 0;
}

static inline int ways_to_cxl(int ways, u8 *iw)
{
	if (ways > 16)
		return -EINVAL;
	if (is_power_of_2(ways)) {
		*iw = ilog2(ways);
		return 0;
	}
	if (ways % 3)
		return -EINVAL;
	ways /= 3;
	if (!is_power_of_2(ways))
		return -EINVAL;
	*iw = ilog2(ways) + 8;
	return 0;
}

B
Ben Widawsky 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */
#define CXLDEV_CAP_ARRAY_OFFSET 0x0
#define   CXLDEV_CAP_ARRAY_CAP_ID 0
#define   CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0)
#define   CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32)
/* CXL 2.0 8.2.8.2 CXL Device Capability Header Register */
#define CXLDEV_CAP_HDR_CAP_ID_MASK GENMASK(15, 0)
/* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */
#define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1
#define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2
#define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3
#define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000

/* CXL 2.0 8.2.8.4 Mailbox Registers */
#define CXLDEV_MBOX_CAPS_OFFSET 0x00
#define   CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0)
#define CXLDEV_MBOX_CTRL_OFFSET 0x04
#define   CXLDEV_MBOX_CTRL_DOORBELL BIT(0)
#define CXLDEV_MBOX_CMD_OFFSET 0x08
#define   CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
#define   CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)
#define CXLDEV_MBOX_STATUS_OFFSET 0x10
#define   CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)
#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18
#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20

147
/*
148 149
 * Using struct_group() allows for per register-block-type helper routines,
 * without requiring block-type agnostic code to include the prefix.
150 151
 */
struct cxl_regs {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	/*
	 * Common set of CXL Component register block base pointers
	 * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
	 */
	struct_group_tagged(cxl_component_regs, component,
		void __iomem *hdm_decoder;
	);
	/*
	 * Common set of CXL Device register block base pointers
	 * @status: CXL 2.0 8.2.8.3 Device Status Registers
	 * @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
	 * @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
	 */
	struct_group_tagged(cxl_device_regs, device_regs,
		void __iomem *status, *mbox, *memdev;
	);
168 169
};

170 171 172 173 174 175
struct cxl_reg_map {
	bool valid;
	unsigned long offset;
	unsigned long size;
};

176 177 178 179
struct cxl_component_reg_map {
	struct cxl_reg_map hdm_decoder;
};

180 181 182 183 184 185
struct cxl_device_reg_map {
	struct cxl_reg_map status;
	struct cxl_reg_map mbox;
	struct cxl_reg_map memdev;
};

186 187 188 189 190 191 192 193 194
/**
 * struct cxl_register_map - DVSEC harvested register block mapping parameters
 * @base: virtual base of the register-block-BAR + @block_offset
 * @block_offset: offset to start of register block in @barno
 * @reg_type: see enum cxl_regloc_type
 * @barno: PCI BAR number containing the register block
 * @component_map: cxl_reg_map for component registers
 * @device_map: cxl_reg_maps for device registers
 */
195
struct cxl_register_map {
196
	void __iomem *base;
197 198 199 200
	u64 block_offset;
	u8 reg_type;
	u8 barno;
	union {
201
		struct cxl_component_reg_map component_map;
202 203 204 205
		struct cxl_device_reg_map device_map;
	};
};

206 207
void cxl_probe_component_regs(struct device *dev, void __iomem *base,
			      struct cxl_component_reg_map *map);
208 209
void cxl_probe_device_regs(struct device *dev, void __iomem *base,
			   struct cxl_device_reg_map *map);
210 211 212
int cxl_map_component_regs(struct pci_dev *pdev,
			   struct cxl_component_regs *regs,
			   struct cxl_register_map *map);
213 214 215
int cxl_map_device_regs(struct pci_dev *pdev,
			struct cxl_device_regs *regs,
			struct cxl_register_map *map);
216

217 218 219
enum cxl_regloc_type;
int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
		      struct cxl_register_map *map);
220 221
void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
				   resource_size_t length);
222

223
#define CXL_RESOURCE_NONE ((resource_size_t) -1)
224
#define CXL_TARGET_STRLEN 20
225

226 227 228 229 230 231 232 233 234 235
/*
 * cxl_decoder flags that define the type of memory / devices this
 * decoder supports as well as configuration lock status See "CXL 2.0
 * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details.
 */
#define CXL_DECODER_F_RAM   BIT(0)
#define CXL_DECODER_F_PMEM  BIT(1)
#define CXL_DECODER_F_TYPE2 BIT(2)
#define CXL_DECODER_F_TYPE3 BIT(3)
#define CXL_DECODER_F_LOCK  BIT(4)
236 237
#define CXL_DECODER_F_ENABLE    BIT(5)
#define CXL_DECODER_F_MASK  GENMASK(5, 0)
238 239 240 241 242 243

enum cxl_decoder_type {
       CXL_DECODER_ACCELERATOR = 2,
       CXL_DECODER_EXPANDER = 3,
};

244 245 246 247 248 249
/*
 * Current specification goes up to 8, double that seems a reasonable
 * software max for the foreseeable future
 */
#define CXL_DECODER_MAX_INTERLEAVE 16

250
/**
251
 * struct cxl_decoder - Common CXL HDM Decoder Attributes
252 253
 * @dev: this decoder's device
 * @id: kernel device name id
254
 * @hpa_range: Host physical address range mapped by this decoder
255 256 257
 * @interleave_ways: number of cxl_dports in this decode
 * @interleave_granularity: data stride per dport
 * @target_type: accelerator vs expander (type2 vs type3) selector
258
 * @region: currently assigned region for this decoder
259 260 261 262 263
 * @flags: memory type capabilities and locking
 */
struct cxl_decoder {
	struct device dev;
	int id;
264
	struct range hpa_range;
265 266 267
	int interleave_ways;
	int interleave_granularity;
	enum cxl_decoder_type target_type;
268
	struct cxl_region *region;
269
	unsigned long flags;
270 271
};

272 273 274 275
/*
 * CXL_DECODER_DEAD prevents endpoints from being reattached to regions
 * while cxld_unregister() is running
 */
276 277 278 279 280
enum cxl_decoder_mode {
	CXL_DECODER_NONE,
	CXL_DECODER_RAM,
	CXL_DECODER_PMEM,
	CXL_DECODER_MIXED,
281
	CXL_DECODER_DEAD,
282 283
};

284 285 286 287 288
/**
 * struct cxl_endpoint_decoder - Endpoint  / SPA to DPA decoder
 * @cxld: base cxl_decoder_object
 * @dpa_res: actively claimed DPA span of this decoder
 * @skip: offset into @dpa_res where @cxld.hpa_range maps
289
 * @mode: which memory type / access-mode-partition this decoder targets
290
 * @pos: interleave position in @cxld.region
291 292 293 294 295
 */
struct cxl_endpoint_decoder {
	struct cxl_decoder cxld;
	struct resource *dpa_res;
	resource_size_t skip;
296
	enum cxl_decoder_mode mode;
297
	int pos;
298 299
};

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
/**
 * struct cxl_switch_decoder - Switch specific CXL HDM Decoder
 * @cxld: base cxl_decoder object
 * @target_lock: coordinate coherent reads of the target list
 * @nr_targets: number of elements in @target
 * @target: active ordered target list in current decoder configuration
 *
 * The 'switch' decoder type represents the decoder instances of cxl_port's that
 * route from the root of a CXL memory decode topology to the endpoints. They
 * come in two flavors, root-level decoders, statically defined by platform
 * firmware, and mid-level decoders, where interleave-granularity,
 * interleave-width, and the target list are mutable.
 */
struct cxl_switch_decoder {
	struct cxl_decoder cxld;
315
	seqlock_t target_lock;
316
	int nr_targets;
317 318 319
	struct cxl_dport *target[];
};

320

321 322 323
/**
 * struct cxl_root_decoder - Static platform CXL address decoder
 * @res: host / parent resource for region allocations
324
 * @region_id: region id for next region provisioning event
325
 * @calc_hb: which host bridge covers the n'th position by granularity
326 327 328 329
 * @cxlsd: base cxl switch decoder
 */
struct cxl_root_decoder {
	struct resource *res;
330
	atomic_t region_id;
331
	struct cxl_dport *(*calc_hb)(struct cxl_root_decoder *cxlrd, int pos);
332 333 334
	struct cxl_switch_decoder cxlsd;
};

335 336 337
/*
 * enum cxl_config_state - State machine for region configuration
 * @CXL_CONFIG_IDLE: Any sysfs attribute can be written freely
338 339
 * @CXL_CONFIG_INTERLEAVE_ACTIVE: region size has been set, no more
 * changes to interleave_ways or interleave_granularity
340 341 342 343 344
 * @CXL_CONFIG_ACTIVE: All targets have been added the region is now
 * active
 */
enum cxl_config_state {
	CXL_CONFIG_IDLE,
345
	CXL_CONFIG_INTERLEAVE_ACTIVE,
346 347 348 349 350 351 352
	CXL_CONFIG_ACTIVE,
};

/**
 * struct cxl_region_params - region settings
 * @state: allow the driver to lockdown further parameter changes
 * @uuid: unique id for persistent regions
353 354
 * @interleave_ways: number of endpoints in the region
 * @interleave_granularity: capacity each endpoint contributes to a stripe
355
 * @res: allocated iomem capacity for this region
356 357 358 359 360 361
 *
 * State transitions are protected by the cxl_region_rwsem
 */
struct cxl_region_params {
	enum cxl_config_state state;
	uuid_t uuid;
362 363
	int interleave_ways;
	int interleave_granularity;
364
	struct resource *res;
365 366
	struct cxl_endpoint_decoder *targets[CXL_DECODER_MAX_INTERLEAVE];
	int nr_targets;
367 368
};

369 370 371 372 373 374
/**
 * struct cxl_region - CXL region
 * @dev: This region's device
 * @id: This region's id. Id is globally unique across all regions
 * @mode: Endpoint decoder allocation / access mode
 * @type: Endpoint decoder target type
375
 * @params: active + config params for the region
376 377 378 379 380 381
 */
struct cxl_region {
	struct device dev;
	int id;
	enum cxl_decoder_mode mode;
	enum cxl_decoder_type type;
382
	struct cxl_region_params params;
383 384
};

385 386 387 388 389 390 391
/**
 * enum cxl_nvdimm_brige_state - state machine for managing bus rescans
 * @CXL_NVB_NEW: Set at bridge create and after cxl_pmem_wq is destroyed
 * @CXL_NVB_DEAD: Set at brige unregistration to preclude async probing
 * @CXL_NVB_ONLINE: Target state after successful ->probe()
 * @CXL_NVB_OFFLINE: Target state after ->remove() or failed ->probe()
 */
392 393 394 395 396 397 398 399
enum cxl_nvdimm_brige_state {
	CXL_NVB_NEW,
	CXL_NVB_DEAD,
	CXL_NVB_ONLINE,
	CXL_NVB_OFFLINE,
};

struct cxl_nvdimm_bridge {
400
	int id;
401 402 403 404 405 406 407 408
	struct device dev;
	struct cxl_port *port;
	struct nvdimm_bus *nvdimm_bus;
	struct nvdimm_bus_descriptor nd_desc;
	struct work_struct state_work;
	enum cxl_nvdimm_brige_state state;
};

409 410 411 412 413
struct cxl_nvdimm {
	struct device dev;
	struct cxl_memdev *cxlmd;
};

414 415 416 417 418 419
/**
 * struct cxl_port - logical collection of upstream port devices and
 *		     downstream port devices to construct a CXL memory
 *		     decode hierarchy.
 * @dev: this port's device
 * @uport: PCI or platform device implementing the upstream port capability
420
 * @host_bridge: Shortcut to the platform attach point for this port
421
 * @id: id for port device-name
422
 * @dports: cxl_dport instances referenced by decoders
423
 * @endpoints: cxl_ep instances, endpoints that are a descendant of this port
424
 * @regions: cxl_region_ref instances, regions mapped by this port
425
 * @parent_dport: dport that points to this port in the parent
426
 * @decoder_ida: allocator for decoder ids
427
 * @hdm_end: track last allocated HDM decoder instance for allocation ordering
428
 * @component_reg_phys: component register capability base address (optional)
429
 * @dead: last ep has been removed, force port re-creation
B
Ben Widawsky 已提交
430
 * @depth: How deep this port is relative to the root. depth 0 is the root.
I
Ira Weiny 已提交
431 432
 * @cdat: Cached CDAT data
 * @cdat_available: Should a CDAT attribute be available in sysfs
433 434 435 436
 */
struct cxl_port {
	struct device dev;
	struct device *uport;
437
	struct device *host_bridge;
438
	int id;
439
	struct xarray dports;
440
	struct xarray endpoints;
441
	struct xarray regions;
442
	struct cxl_dport *parent_dport;
443
	struct ida decoder_ida;
444
	int hdm_end;
445
	resource_size_t component_reg_phys;
446
	bool dead;
B
Ben Widawsky 已提交
447
	unsigned int depth;
I
Ira Weiny 已提交
448 449 450 451 452
	struct cxl_cdat {
		void *table;
		size_t length;
	} cdat;
	bool cdat_available;
453 454
};

455 456 457 458 459 460
static inline struct cxl_dport *
cxl_find_dport_by_dev(struct cxl_port *port, const struct device *dport_dev)
{
	return xa_load(&port->dports, (unsigned long)dport_dev);
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474
/**
 * struct cxl_dport - CXL downstream port
 * @dport: PCI bridge or firmware device representing the downstream link
 * @port_id: unique hardware identifier for dport in decoder target list
 * @component_reg_phys: downstream port component registers
 * @port: reference to cxl_port that contains this downstream port
 */
struct cxl_dport {
	struct device *dport;
	int port_id;
	resource_size_t component_reg_phys;
	struct cxl_port *port;
};

475 476 477
/**
 * struct cxl_ep - track an endpoint's interest in a port
 * @ep: device that hosts a generic CXL endpoint (expander or accelerator)
478
 * @dport: which dport routes to this endpoint on @port
479 480
 * @next: cxl switch port across the link attached to @dport NULL if
 *	  attached to an endpoint
481 482 483
 */
struct cxl_ep {
	struct device *ep;
484
	struct cxl_dport *dport;
485
	struct cxl_port *next;
486 487
};

488 489 490 491 492 493
/**
 * struct cxl_region_ref - track a region's interest in a port
 * @port: point in topology to install this reference
 * @decoder: decoder assigned for @region in @port
 * @region: region for this reference
 * @endpoints: cxl_ep references for region members beneath @port
D
Dan Williams 已提交
494
 * @nr_targets_set: track how many targets have been programmed during setup
495 496 497 498 499 500 501 502
 * @nr_eps: number of endpoints beneath @port
 * @nr_targets: number of distinct targets needed to reach @nr_eps
 */
struct cxl_region_ref {
	struct cxl_port *port;
	struct cxl_decoder *decoder;
	struct cxl_region *region;
	struct xarray endpoints;
D
Dan Williams 已提交
503
	int nr_targets_set;
504 505 506 507
	int nr_eps;
	int nr_targets;
};

508 509 510 511 512 513 514 515 516 517 518
/*
 * The platform firmware device hosting the root is also the top of the
 * CXL port topology. All other CXL ports have another CXL port as their
 * parent and their ->uport / host device is out-of-line of the port
 * ancestry.
 */
static inline bool is_cxl_root(struct cxl_port *port)
{
	return port->uport == port->dev.parent;
}

D
Dan Williams 已提交
519
bool is_cxl_port(struct device *dev);
520
struct cxl_port *to_cxl_port(struct device *dev);
521
struct pci_bus;
522 523 524
int devm_cxl_register_pci_bus(struct device *host, struct device *uport,
			      struct pci_bus *bus);
struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port);
525 526
struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
				   resource_size_t component_reg_phys,
527
				   struct cxl_dport *parent_dport);
528 529
int devm_cxl_add_endpoint(struct cxl_memdev *cxlmd,
			  struct cxl_dport *parent_dport);
530
struct cxl_port *find_cxl_root(struct device *dev);
531
int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
B
Ben Widawsky 已提交
532
int cxl_bus_rescan(void);
533 534
struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
				   struct cxl_dport **dport);
B
Ben Widawsky 已提交
535
bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd);
536

537
struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
538 539
				     struct device *dport, int port_id,
				     resource_size_t component_reg_phys);
540

541
struct cxl_decoder *to_cxl_decoder(struct device *dev);
542
struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev);
543
struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev);
544
bool is_root_decoder(struct device *dev);
B
Ben Widawsky 已提交
545
bool is_endpoint_decoder(struct device *dev);
546 547
struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
						unsigned int nr_targets);
548 549
struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
						    unsigned int nr_targets);
550
int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
551
struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port);
552
int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
553
int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
B
Ben Widawsky 已提交
554 555
int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint);

556
struct cxl_hdm;
557 558 559
struct cxl_hdm *devm_cxl_setup_hdm(struct cxl_port *port);
int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm);
int devm_cxl_add_passthrough_decoder(struct cxl_port *port);
560

561 562
bool is_cxl_region(struct device *dev);

563
extern struct bus_type cxl_bus_type;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

struct cxl_driver {
	const char *name;
	int (*probe)(struct device *dev);
	void (*remove)(struct device *dev);
	struct device_driver drv;
	int id;
};

static inline struct cxl_driver *to_cxl_drv(struct device_driver *drv)
{
	return container_of(drv, struct cxl_driver, drv);
}

int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
			  const char *modname);
#define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME)
void cxl_driver_unregister(struct cxl_driver *cxl_drv);

B
Ben Widawsky 已提交
583 584 585
#define module_cxl_driver(__cxl_driver) \
	module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister)

586 587
#define CXL_DEVICE_NVDIMM_BRIDGE	1
#define CXL_DEVICE_NVDIMM		2
588 589
#define CXL_DEVICE_PORT			3
#define CXL_DEVICE_ROOT			4
B
Ben Widawsky 已提交
590
#define CXL_DEVICE_MEMORY_EXPANDER	5
591

592 593 594
#define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
#define CXL_MODALIAS_FMT "cxl:t%d"

595 596 597
struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev);
struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host,
						     struct cxl_port *port);
598 599
struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev);
bool is_cxl_nvdimm(struct device *dev);
600
bool is_cxl_nvdimm_bridge(struct device *dev);
601
int devm_cxl_add_nvdimm(struct device *host, struct cxl_memdev *cxlmd);
602
struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_nvdimm *cxl_nvd);
603 604 605 606 607 608 609 610

/*
 * Unit test builds overrides this to __weak, find the 'strong' version
 * of these symbols in tools/testing/cxl/.
 */
#ifndef __mock
#define __mock static
#endif
D
Dan Williams 已提交
611

B
Ben Widawsky 已提交
612
#endif /* __CXL_H__ */