cxl/region: Allocate HPA capacity to regions
[linux-2.6-block.git] / drivers / cxl / cxl.h
CommitLineData
8adaf747
BW
1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright(c) 2020 Intel Corporation. */
3
4#ifndef __CXL_H__
5#define __CXL_H__
6
8fdcb170 7#include <linux/libnvdimm.h>
8adaf747
BW
8#include <linux/bitfield.h>
9#include <linux/bitops.h>
80d10a6c 10#include <linux/log2.h>
8adaf747
BW
11#include <linux/io.h>
12
4812be97
DW
13/**
14 * DOC: cxl objects
15 *
16 * The CXL core objects like ports, decoders, and regions are shared
17 * between the subsystem drivers cxl_acpi, cxl_pci, and core drivers
18 * (port-driver, region-driver, nvdimm object-drivers... etc).
19 */
20
d17d0540
DW
21/* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
22#define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K
23
08422378
BW
24/* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
25#define CXL_CM_OFFSET 0x1000
26#define CXL_CM_CAP_HDR_OFFSET 0x0
27#define CXL_CM_CAP_HDR_ID_MASK GENMASK(15, 0)
28#define CM_CAP_HDR_CAP_ID 1
29#define CXL_CM_CAP_HDR_VERSION_MASK GENMASK(19, 16)
30#define CM_CAP_HDR_CAP_VERSION 1
31#define CXL_CM_CAP_HDR_CACHE_MEM_VERSION_MASK GENMASK(23, 20)
32#define CM_CAP_HDR_CACHE_MEM_VERSION 1
33#define CXL_CM_CAP_HDR_ARRAY_SIZE_MASK GENMASK(31, 24)
34#define CXL_CM_CAP_PTR_MASK GENMASK(31, 20)
35
36#define CXL_CM_CAP_CAP_ID_HDM 0x5
37#define CXL_CM_CAP_CAP_HDM_VERSION 1
38
39/* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */
40#define CXL_HDM_DECODER_CAP_OFFSET 0x0
41#define CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
42#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
d17d0540
DW
43#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
44#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
45#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
46#define CXL_HDM_DECODER_ENABLE BIT(1)
47#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
48#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)
49#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
50#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
51#define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
52#define CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
53#define CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
54#define CXL_HDM_DECODER0_CTRL_LOCK BIT(8)
55#define CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
56#define CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
57#define CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
58#define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
59#define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
9c57cde0
DW
60#define CXL_HDM_DECODER0_SKIP_LOW(i) CXL_HDM_DECODER0_TL_LOW(i)
61#define CXL_HDM_DECODER0_SKIP_HIGH(i) CXL_HDM_DECODER0_TL_HIGH(i)
08422378 62
6423035f
BW
63static inline int cxl_hdm_decoder_count(u32 cap_hdr)
64{
65 int val = FIELD_GET(CXL_HDM_DECODER_COUNT_MASK, cap_hdr);
66
67 return val ? val * 2 : 1;
68}
69
419af595
DW
70/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */
71static inline int cxl_to_granularity(u16 ig, unsigned int *val)
72{
73 if (ig > 6)
74 return -EINVAL;
75 *val = 256 << ig;
76 return 0;
77}
78
79/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */
80static inline int cxl_to_ways(u8 eniw, unsigned int *val)
81{
82 switch (eniw) {
83 case 0 ... 4:
84 *val = 1 << eniw;
85 break;
86 case 8 ... 10:
87 *val = 3 << (eniw - 8);
88 break;
89 default:
90 return -EINVAL;
91 }
92
93 return 0;
94}
95
80d10a6c
BW
96static inline int granularity_to_cxl(int g, u16 *ig)
97{
98 if (g > SZ_16K || g < 256 || !is_power_of_2(g))
99 return -EINVAL;
100 *ig = ilog2(g) - 8;
101 return 0;
102}
103
104static inline int ways_to_cxl(int ways, u8 *iw)
105{
106 if (ways > 16)
107 return -EINVAL;
108 if (is_power_of_2(ways)) {
109 *iw = ilog2(ways);
110 return 0;
111 }
112 if (ways % 3)
113 return -EINVAL;
114 ways /= 3;
115 if (!is_power_of_2(ways))
116 return -EINVAL;
117 *iw = ilog2(ways) + 8;
118 return 0;
119}
120
8adaf747
BW
121/* CXL 2.0 8.2.8.1 Device Capabilities Array Register */
122#define CXLDEV_CAP_ARRAY_OFFSET 0x0
123#define CXLDEV_CAP_ARRAY_CAP_ID 0
124#define CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0)
125#define CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32)
126/* CXL 2.0 8.2.8.2 CXL Device Capability Header Register */
127#define CXLDEV_CAP_HDR_CAP_ID_MASK GENMASK(15, 0)
128/* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */
129#define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1
130#define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2
131#define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3
132#define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000
133
134/* CXL 2.0 8.2.8.4 Mailbox Registers */
135#define CXLDEV_MBOX_CAPS_OFFSET 0x00
136#define CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0)
137#define CXLDEV_MBOX_CTRL_OFFSET 0x04
138#define CXLDEV_MBOX_CTRL_DOORBELL BIT(0)
139#define CXLDEV_MBOX_CMD_OFFSET 0x08
140#define CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
141#define CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)
142#define CXLDEV_MBOX_STATUS_OFFSET 0x10
143#define CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)
144#define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18
145#define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20
146
8ac75dd6 147/*
301e68dd
KC
148 * Using struct_group() allows for per register-block-type helper routines,
149 * without requiring block-type agnostic code to include the prefix.
8ac75dd6
DW
150 */
151struct cxl_regs {
301e68dd
KC
152 /*
153 * Common set of CXL Component register block base pointers
154 * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
155 */
156 struct_group_tagged(cxl_component_regs, component,
157 void __iomem *hdm_decoder;
158 );
159 /*
160 * Common set of CXL Device register block base pointers
161 * @status: CXL 2.0 8.2.8.3 Device Status Registers
162 * @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
163 * @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
164 */
165 struct_group_tagged(cxl_device_regs, device_regs,
166 void __iomem *status, *mbox, *memdev;
167 );
8ac75dd6
DW
168};
169
30af9729
IW
170struct cxl_reg_map {
171 bool valid;
172 unsigned long offset;
173 unsigned long size;
174};
175
08422378
BW
176struct cxl_component_reg_map {
177 struct cxl_reg_map hdm_decoder;
178};
179
30af9729
IW
180struct cxl_device_reg_map {
181 struct cxl_reg_map status;
182 struct cxl_reg_map mbox;
183 struct cxl_reg_map memdev;
184};
185
a261e9a1
DW
186/**
187 * struct cxl_register_map - DVSEC harvested register block mapping parameters
188 * @base: virtual base of the register-block-BAR + @block_offset
189 * @block_offset: offset to start of register block in @barno
190 * @reg_type: see enum cxl_regloc_type
191 * @barno: PCI BAR number containing the register block
192 * @component_map: cxl_reg_map for component registers
193 * @device_map: cxl_reg_maps for device registers
194 */
30af9729 195struct cxl_register_map {
a261e9a1 196 void __iomem *base;
30af9729
IW
197 u64 block_offset;
198 u8 reg_type;
199 u8 barno;
200 union {
08422378 201 struct cxl_component_reg_map component_map;
30af9729
IW
202 struct cxl_device_reg_map device_map;
203 };
204};
205
08422378
BW
206void cxl_probe_component_regs(struct device *dev, void __iomem *base,
207 struct cxl_component_reg_map *map);
30af9729
IW
208void cxl_probe_device_regs(struct device *dev, void __iomem *base,
209 struct cxl_device_reg_map *map);
08422378
BW
210int cxl_map_component_regs(struct pci_dev *pdev,
211 struct cxl_component_regs *regs,
212 struct cxl_register_map *map);
30af9729
IW
213int cxl_map_device_regs(struct pci_dev *pdev,
214 struct cxl_device_regs *regs,
215 struct cxl_register_map *map);
399d34eb 216
303ebc1b
BW
217enum cxl_regloc_type;
218int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
219 struct cxl_register_map *map);
54cdbf84
BW
220void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
221 resource_size_t length);
303ebc1b 222
4812be97 223#define CXL_RESOURCE_NONE ((resource_size_t) -1)
7d4b5ca2 224#define CXL_TARGET_STRLEN 20
4812be97 225
40ba17af
DW
226/*
227 * cxl_decoder flags that define the type of memory / devices this
228 * decoder supports as well as configuration lock status See "CXL 2.0
229 * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details.
230 */
231#define CXL_DECODER_F_RAM BIT(0)
232#define CXL_DECODER_F_PMEM BIT(1)
233#define CXL_DECODER_F_TYPE2 BIT(2)
234#define CXL_DECODER_F_TYPE3 BIT(3)
235#define CXL_DECODER_F_LOCK BIT(4)
d17d0540
DW
236#define CXL_DECODER_F_ENABLE BIT(5)
237#define CXL_DECODER_F_MASK GENMASK(5, 0)
40ba17af
DW
238
239enum cxl_decoder_type {
240 CXL_DECODER_ACCELERATOR = 2,
241 CXL_DECODER_EXPANDER = 3,
242};
243
a5c25802
DW
244/*
245 * Current specification goes up to 8, double that seems a reasonable
246 * software max for the foreseeable future
247 */
248#define CXL_DECODER_MAX_INTERLEAVE 16
249
40ba17af 250/**
e636479e 251 * struct cxl_decoder - Common CXL HDM Decoder Attributes
40ba17af
DW
252 * @dev: this decoder's device
253 * @id: kernel device name id
e8b7ea58 254 * @hpa_range: Host physical address range mapped by this decoder
40ba17af
DW
255 * @interleave_ways: number of cxl_dports in this decode
256 * @interleave_granularity: data stride per dport
257 * @target_type: accelerator vs expander (type2 vs type3) selector
258 * @flags: memory type capabilities and locking
40ba17af
DW
259 */
260struct cxl_decoder {
261 struct device dev;
262 int id;
e50fe01e 263 struct range hpa_range;
40ba17af
DW
264 int interleave_ways;
265 int interleave_granularity;
266 enum cxl_decoder_type target_type;
267 unsigned long flags;
e636479e
DW
268};
269
2c866903
DW
270enum cxl_decoder_mode {
271 CXL_DECODER_NONE,
272 CXL_DECODER_RAM,
273 CXL_DECODER_PMEM,
274 CXL_DECODER_MIXED,
275};
276
3bf65915
DW
277/**
278 * struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder
279 * @cxld: base cxl_decoder_object
280 * @dpa_res: actively claimed DPA span of this decoder
281 * @skip: offset into @dpa_res where @cxld.hpa_range maps
2c866903 282 * @mode: which memory type / access-mode-partition this decoder targets
3bf65915
DW
283 */
284struct cxl_endpoint_decoder {
285 struct cxl_decoder cxld;
286 struct resource *dpa_res;
287 resource_size_t skip;
2c866903 288 enum cxl_decoder_mode mode;
3bf65915
DW
289};
290
e636479e
DW
291/**
292 * struct cxl_switch_decoder - Switch specific CXL HDM Decoder
293 * @cxld: base cxl_decoder object
294 * @target_lock: coordinate coherent reads of the target list
295 * @nr_targets: number of elements in @target
296 * @target: active ordered target list in current decoder configuration
297 *
298 * The 'switch' decoder type represents the decoder instances of cxl_port's that
299 * route from the root of a CXL memory decode topology to the endpoints. They
300 * come in two flavors, root-level decoders, statically defined by platform
301 * firmware, and mid-level decoders, where interleave-granularity,
302 * interleave-width, and the target list are mutable.
303 */
304struct cxl_switch_decoder {
305 struct cxl_decoder cxld;
86c8ea0f 306 seqlock_t target_lock;
be185c29 307 int nr_targets;
40ba17af
DW
308 struct cxl_dport *target[];
309};
310
8fdcb170 311
0f157c7f
DW
312/**
313 * struct cxl_root_decoder - Static platform CXL address decoder
314 * @res: host / parent resource for region allocations
779dd20c 315 * @region_id: region id for next region provisioning event
0f157c7f
DW
316 * @cxlsd: base cxl switch decoder
317 */
318struct cxl_root_decoder {
319 struct resource *res;
779dd20c 320 atomic_t region_id;
0f157c7f
DW
321 struct cxl_switch_decoder cxlsd;
322};
323
dd5ba0eb
BW
324/*
325 * enum cxl_config_state - State machine for region configuration
326 * @CXL_CONFIG_IDLE: Any sysfs attribute can be written freely
80d10a6c
BW
327 * @CXL_CONFIG_INTERLEAVE_ACTIVE: region size has been set, no more
328 * changes to interleave_ways or interleave_granularity
dd5ba0eb
BW
329 * @CXL_CONFIG_ACTIVE: All targets have been added the region is now
330 * active
331 */
332enum cxl_config_state {
333 CXL_CONFIG_IDLE,
80d10a6c 334 CXL_CONFIG_INTERLEAVE_ACTIVE,
dd5ba0eb
BW
335 CXL_CONFIG_ACTIVE,
336};
337
338/**
339 * struct cxl_region_params - region settings
340 * @state: allow the driver to lockdown further parameter changes
341 * @uuid: unique id for persistent regions
80d10a6c
BW
342 * @interleave_ways: number of endpoints in the region
343 * @interleave_granularity: capacity each endpoint contributes to a stripe
23a22cd1 344 * @res: allocated iomem capacity for this region
dd5ba0eb
BW
345 *
346 * State transitions are protected by the cxl_region_rwsem
347 */
348struct cxl_region_params {
349 enum cxl_config_state state;
350 uuid_t uuid;
80d10a6c
BW
351 int interleave_ways;
352 int interleave_granularity;
23a22cd1 353 struct resource *res;
dd5ba0eb
BW
354};
355
779dd20c
BW
356/**
357 * struct cxl_region - CXL region
358 * @dev: This region's device
359 * @id: This region's id. Id is globally unique across all regions
360 * @mode: Endpoint decoder allocation / access mode
361 * @type: Endpoint decoder target type
dd5ba0eb 362 * @params: active + config params for the region
779dd20c
BW
363 */
364struct cxl_region {
365 struct device dev;
366 int id;
367 enum cxl_decoder_mode mode;
368 enum cxl_decoder_type type;
dd5ba0eb 369 struct cxl_region_params params;
779dd20c
BW
370};
371
53989fad
DW
372/**
373 * enum cxl_nvdimm_brige_state - state machine for managing bus rescans
374 * @CXL_NVB_NEW: Set at bridge create and after cxl_pmem_wq is destroyed
375 * @CXL_NVB_DEAD: Set at brige unregistration to preclude async probing
376 * @CXL_NVB_ONLINE: Target state after successful ->probe()
377 * @CXL_NVB_OFFLINE: Target state after ->remove() or failed ->probe()
378 */
8fdcb170
DW
379enum cxl_nvdimm_brige_state {
380 CXL_NVB_NEW,
381 CXL_NVB_DEAD,
382 CXL_NVB_ONLINE,
383 CXL_NVB_OFFLINE,
384};
385
386struct cxl_nvdimm_bridge {
2e52b625 387 int id;
8fdcb170
DW
388 struct device dev;
389 struct cxl_port *port;
390 struct nvdimm_bus *nvdimm_bus;
391 struct nvdimm_bus_descriptor nd_desc;
392 struct work_struct state_work;
393 enum cxl_nvdimm_brige_state state;
394};
395
21083f51
DW
396struct cxl_nvdimm {
397 struct device dev;
398 struct cxl_memdev *cxlmd;
21083f51
DW
399};
400
4812be97
DW
401/**
402 * struct cxl_port - logical collection of upstream port devices and
403 * downstream port devices to construct a CXL memory
404 * decode hierarchy.
405 * @dev: this port's device
406 * @uport: PCI or platform device implementing the upstream port capability
ee800010 407 * @host_bridge: Shortcut to the platform attach point for this port
4812be97 408 * @id: id for port device-name
7d4b5ca2 409 * @dports: cxl_dport instances referenced by decoders
2703c16c 410 * @endpoints: cxl_ep instances, endpoints that are a descendant of this port
1b58b4ca 411 * @parent_dport: dport that points to this port in the parent
40ba17af 412 * @decoder_ida: allocator for decoder ids
0c33b393 413 * @hdm_end: track last allocated HDM decoder instance for allocation ordering
4812be97 414 * @component_reg_phys: component register capability base address (optional)
2703c16c 415 * @dead: last ep has been removed, force port re-creation
53fa1bff 416 * @depth: How deep this port is relative to the root. depth 0 is the root.
c9700604
IW
417 * @cdat: Cached CDAT data
418 * @cdat_available: Should a CDAT attribute be available in sysfs
4812be97
DW
419 */
420struct cxl_port {
421 struct device dev;
422 struct device *uport;
ee800010 423 struct device *host_bridge;
4812be97 424 int id;
39178585 425 struct xarray dports;
256d0e9e 426 struct xarray endpoints;
1b58b4ca 427 struct cxl_dport *parent_dport;
40ba17af 428 struct ida decoder_ida;
0c33b393 429 int hdm_end;
4812be97 430 resource_size_t component_reg_phys;
2703c16c 431 bool dead;
53fa1bff 432 unsigned int depth;
c9700604
IW
433 struct cxl_cdat {
434 void *table;
435 size_t length;
436 } cdat;
437 bool cdat_available;
4812be97
DW
438};
439
39178585
DW
440static inline struct cxl_dport *
441cxl_find_dport_by_dev(struct cxl_port *port, const struct device *dport_dev)
442{
443 return xa_load(&port->dports, (unsigned long)dport_dev);
444}
445
7d4b5ca2
DW
446/**
447 * struct cxl_dport - CXL downstream port
448 * @dport: PCI bridge or firmware device representing the downstream link
449 * @port_id: unique hardware identifier for dport in decoder target list
450 * @component_reg_phys: downstream port component registers
451 * @port: reference to cxl_port that contains this downstream port
7d4b5ca2
DW
452 */
453struct cxl_dport {
454 struct device *dport;
455 int port_id;
456 resource_size_t component_reg_phys;
457 struct cxl_port *port;
7d4b5ca2
DW
458};
459
2703c16c
DW
460/**
461 * struct cxl_ep - track an endpoint's interest in a port
462 * @ep: device that hosts a generic CXL endpoint (expander or accelerator)
de516b40 463 * @dport: which dport routes to this endpoint on @port
7f8faf96
DW
464 * @next: cxl switch port across the link attached to @dport NULL if
465 * attached to an endpoint
2703c16c
DW
466 */
467struct cxl_ep {
468 struct device *ep;
de516b40 469 struct cxl_dport *dport;
7f8faf96 470 struct cxl_port *next;
2703c16c
DW
471};
472
d54c1bbe
BW
473/*
474 * The platform firmware device hosting the root is also the top of the
475 * CXL port topology. All other CXL ports have another CXL port as their
476 * parent and their ->uport / host device is out-of-line of the port
477 * ancestry.
478 */
479static inline bool is_cxl_root(struct cxl_port *port)
480{
481 return port->uport == port->dev.parent;
482}
483
3c5b9039 484bool is_cxl_port(struct device *dev);
4812be97 485struct cxl_port *to_cxl_port(struct device *dev);
98d2d3a2 486struct pci_bus;
5ff7316f
DW
487int devm_cxl_register_pci_bus(struct device *host, struct device *uport,
488 struct pci_bus *bus);
489struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port);
4812be97
DW
490struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
491 resource_size_t component_reg_phys,
1b58b4ca 492 struct cxl_dport *parent_dport);
7f8faf96
DW
493int devm_cxl_add_endpoint(struct cxl_memdev *cxlmd,
494 struct cxl_dport *parent_dport);
a46cfc0f 495struct cxl_port *find_cxl_root(struct device *dev);
2703c16c 496int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
8dd2bc0f 497int cxl_bus_rescan(void);
1b58b4ca
DW
498struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
499 struct cxl_dport **dport);
8dd2bc0f 500bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd);
2703c16c 501
664bf115 502struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
98d2d3a2
DW
503 struct device *dport, int port_id,
504 resource_size_t component_reg_phys);
2703c16c 505
40ba17af 506struct cxl_decoder *to_cxl_decoder(struct device *dev);
0f157c7f 507struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev);
3bf65915 508struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev);
8fdcb170 509bool is_root_decoder(struct device *dev);
8ae3cebc 510bool is_endpoint_decoder(struct device *dev);
0f157c7f
DW
511struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
512 unsigned int nr_targets);
e636479e
DW
513struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
514 unsigned int nr_targets);
48667f67 515int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
3bf65915 516struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port);
d17d0540 517int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
48667f67 518int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
8dd2bc0f
BW
519int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint);
520
d17d0540 521struct cxl_hdm;
664bf115
DW
522struct cxl_hdm *devm_cxl_setup_hdm(struct cxl_port *port);
523int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm);
524int devm_cxl_add_passthrough_decoder(struct cxl_port *port);
40ba17af 525
779dd20c
BW
526bool is_cxl_region(struct device *dev);
527
b39cb105 528extern struct bus_type cxl_bus_type;
6af7139c
DW
529
530struct cxl_driver {
531 const char *name;
532 int (*probe)(struct device *dev);
533 void (*remove)(struct device *dev);
534 struct device_driver drv;
535 int id;
536};
537
538static inline struct cxl_driver *to_cxl_drv(struct device_driver *drv)
539{
540 return container_of(drv, struct cxl_driver, drv);
541}
542
543int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
544 const char *modname);
545#define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME)
546void cxl_driver_unregister(struct cxl_driver *cxl_drv);
547
c57cae78
BW
548#define module_cxl_driver(__cxl_driver) \
549 module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister)
550
21083f51
DW
551#define CXL_DEVICE_NVDIMM_BRIDGE 1
552#define CXL_DEVICE_NVDIMM 2
54cdbf84
BW
553#define CXL_DEVICE_PORT 3
554#define CXL_DEVICE_ROOT 4
8dd2bc0f 555#define CXL_DEVICE_MEMORY_EXPANDER 5
8fdcb170 556
6af7139c
DW
557#define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
558#define CXL_MODALIAS_FMT "cxl:t%d"
559
8fdcb170
DW
560struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev);
561struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host,
562 struct cxl_port *port);
21083f51
DW
563struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev);
564bool is_cxl_nvdimm(struct device *dev);
53989fad 565bool is_cxl_nvdimm_bridge(struct device *dev);
21083f51 566int devm_cxl_add_nvdimm(struct device *host, struct cxl_memdev *cxlmd);
7d3eb23c 567struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_nvdimm *cxl_nvd);
67dcdd4d
DW
568
569/*
570 * Unit test builds overrides this to __weak, find the 'strong' version
571 * of these symbols in tools/testing/cxl/.
572 */
573#ifndef __mock
574#define __mock static
575#endif
3c5b9039 576
8adaf747 577#endif /* __CXL_H__ */