b988ea288f530d37e255c1463ceb91e817c223fa
[linux-2.6-microblaze.git] / drivers / cxl / cxl.h
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/bitfield.h>
8 #include <linux/bitops.h>
9 #include <linux/io.h>
10
11 /**
12  * DOC: cxl objects
13  *
14  * The CXL core objects like ports, decoders, and regions are shared
15  * between the subsystem drivers cxl_acpi, cxl_pci, and core drivers
16  * (port-driver, region-driver, nvdimm object-drivers... etc).
17  */
18
19 /* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
20 #define CXL_CM_OFFSET 0x1000
21 #define CXL_CM_CAP_HDR_OFFSET 0x0
22 #define   CXL_CM_CAP_HDR_ID_MASK GENMASK(15, 0)
23 #define     CM_CAP_HDR_CAP_ID 1
24 #define   CXL_CM_CAP_HDR_VERSION_MASK GENMASK(19, 16)
25 #define     CM_CAP_HDR_CAP_VERSION 1
26 #define   CXL_CM_CAP_HDR_CACHE_MEM_VERSION_MASK GENMASK(23, 20)
27 #define     CM_CAP_HDR_CACHE_MEM_VERSION 1
28 #define   CXL_CM_CAP_HDR_ARRAY_SIZE_MASK GENMASK(31, 24)
29 #define CXL_CM_CAP_PTR_MASK GENMASK(31, 20)
30
31 #define   CXL_CM_CAP_CAP_ID_HDM 0x5
32 #define   CXL_CM_CAP_CAP_HDM_VERSION 1
33
34 /* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */
35 #define CXL_HDM_DECODER_CAP_OFFSET 0x0
36 #define   CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
37 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
38 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET 0x10
39 #define CXL_HDM_DECODER0_BASE_HIGH_OFFSET 0x14
40 #define CXL_HDM_DECODER0_SIZE_LOW_OFFSET 0x18
41 #define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET 0x1c
42 #define CXL_HDM_DECODER0_CTRL_OFFSET 0x20
43
44 /* CXL 2.0 8.2.8.1 Device Capabilities Array Register */
45 #define CXLDEV_CAP_ARRAY_OFFSET 0x0
46 #define   CXLDEV_CAP_ARRAY_CAP_ID 0
47 #define   CXLDEV_CAP_ARRAY_ID_MASK GENMASK_ULL(15, 0)
48 #define   CXLDEV_CAP_ARRAY_COUNT_MASK GENMASK_ULL(47, 32)
49 /* CXL 2.0 8.2.8.2 CXL Device Capability Header Register */
50 #define CXLDEV_CAP_HDR_CAP_ID_MASK GENMASK(15, 0)
51 /* CXL 2.0 8.2.8.2.1 CXL Device Capabilities */
52 #define CXLDEV_CAP_CAP_ID_DEVICE_STATUS 0x1
53 #define CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX 0x2
54 #define CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX 0x3
55 #define CXLDEV_CAP_CAP_ID_MEMDEV 0x4000
56
57 /* CXL 2.0 8.2.8.4 Mailbox Registers */
58 #define CXLDEV_MBOX_CAPS_OFFSET 0x00
59 #define   CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK GENMASK(4, 0)
60 #define CXLDEV_MBOX_CTRL_OFFSET 0x04
61 #define   CXLDEV_MBOX_CTRL_DOORBELL BIT(0)
62 #define CXLDEV_MBOX_CMD_OFFSET 0x08
63 #define   CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK GENMASK_ULL(15, 0)
64 #define   CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK GENMASK_ULL(36, 16)
65 #define CXLDEV_MBOX_STATUS_OFFSET 0x10
66 #define   CXLDEV_MBOX_STATUS_RET_CODE_MASK GENMASK_ULL(47, 32)
67 #define CXLDEV_MBOX_BG_CMD_STATUS_OFFSET 0x18
68 #define CXLDEV_MBOX_PAYLOAD_OFFSET 0x20
69
70 #define CXL_COMPONENT_REGS() \
71         void __iomem *hdm_decoder
72
73 #define CXL_DEVICE_REGS() \
74         void __iomem *status; \
75         void __iomem *mbox; \
76         void __iomem *memdev
77
78 /* See note for 'struct cxl_regs' for the rationale of this organization */
79 /*
80  * CXL_COMPONENT_REGS - Common set of CXL Component register block base pointers
81  * @hdm_decoder: CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure
82  */
83 struct cxl_component_regs {
84         CXL_COMPONENT_REGS();
85 };
86
87 /* See note for 'struct cxl_regs' for the rationale of this organization */
88 /*
89  * CXL_DEVICE_REGS - Common set of CXL Device register block base pointers
90  * @status: CXL 2.0 8.2.8.3 Device Status Registers
91  * @mbox: CXL 2.0 8.2.8.4 Mailbox Registers
92  * @memdev: CXL 2.0 8.2.8.5 Memory Device Registers
93  */
94 struct cxl_device_regs {
95         CXL_DEVICE_REGS();
96 };
97
98 /*
99  * Note, the anonymous union organization allows for per
100  * register-block-type helper routines, without requiring block-type
101  * agnostic code to include the prefix.
102  */
103 struct cxl_regs {
104         union {
105                 struct {
106                         CXL_COMPONENT_REGS();
107                 };
108                 struct cxl_component_regs component;
109         };
110         union {
111                 struct {
112                         CXL_DEVICE_REGS();
113                 };
114                 struct cxl_device_regs device_regs;
115         };
116 };
117
118 struct cxl_reg_map {
119         bool valid;
120         unsigned long offset;
121         unsigned long size;
122 };
123
124 struct cxl_component_reg_map {
125         struct cxl_reg_map hdm_decoder;
126 };
127
128 struct cxl_device_reg_map {
129         struct cxl_reg_map status;
130         struct cxl_reg_map mbox;
131         struct cxl_reg_map memdev;
132 };
133
134 struct cxl_register_map {
135         struct list_head list;
136         u64 block_offset;
137         u8 reg_type;
138         u8 barno;
139         union {
140                 struct cxl_component_reg_map component_map;
141                 struct cxl_device_reg_map device_map;
142         };
143 };
144
145 void cxl_probe_component_regs(struct device *dev, void __iomem *base,
146                               struct cxl_component_reg_map *map);
147 void cxl_probe_device_regs(struct device *dev, void __iomem *base,
148                            struct cxl_device_reg_map *map);
149 int cxl_map_component_regs(struct pci_dev *pdev,
150                            struct cxl_component_regs *regs,
151                            struct cxl_register_map *map);
152 int cxl_map_device_regs(struct pci_dev *pdev,
153                         struct cxl_device_regs *regs,
154                         struct cxl_register_map *map);
155
156 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
157 #define CXL_TARGET_STRLEN 20
158
159 /*
160  * cxl_decoder flags that define the type of memory / devices this
161  * decoder supports as well as configuration lock status See "CXL 2.0
162  * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details.
163  */
164 #define CXL_DECODER_F_RAM   BIT(0)
165 #define CXL_DECODER_F_PMEM  BIT(1)
166 #define CXL_DECODER_F_TYPE2 BIT(2)
167 #define CXL_DECODER_F_TYPE3 BIT(3)
168 #define CXL_DECODER_F_LOCK  BIT(4)
169 #define CXL_DECODER_F_MASK  GENMASK(4, 0)
170
171 enum cxl_decoder_type {
172        CXL_DECODER_ACCELERATOR = 2,
173        CXL_DECODER_EXPANDER = 3,
174 };
175
176 /**
177  * struct cxl_decoder - CXL address range decode configuration
178  * @dev: this decoder's device
179  * @id: kernel device name id
180  * @range: address range considered by this decoder
181  * @interleave_ways: number of cxl_dports in this decode
182  * @interleave_granularity: data stride per dport
183  * @target_type: accelerator vs expander (type2 vs type3) selector
184  * @flags: memory type capabilities and locking
185  * @target: active ordered target list in current decoder configuration
186  */
187 struct cxl_decoder {
188         struct device dev;
189         int id;
190         struct range range;
191         int interleave_ways;
192         int interleave_granularity;
193         enum cxl_decoder_type target_type;
194         unsigned long flags;
195         struct cxl_dport *target[];
196 };
197
198 /**
199  * struct cxl_port - logical collection of upstream port devices and
200  *                   downstream port devices to construct a CXL memory
201  *                   decode hierarchy.
202  * @dev: this port's device
203  * @uport: PCI or platform device implementing the upstream port capability
204  * @id: id for port device-name
205  * @dports: cxl_dport instances referenced by decoders
206  * @decoder_ida: allocator for decoder ids
207  * @component_reg_phys: component register capability base address (optional)
208  */
209 struct cxl_port {
210         struct device dev;
211         struct device *uport;
212         int id;
213         struct list_head dports;
214         struct ida decoder_ida;
215         resource_size_t component_reg_phys;
216 };
217
218 /**
219  * struct cxl_dport - CXL downstream port
220  * @dport: PCI bridge or firmware device representing the downstream link
221  * @port_id: unique hardware identifier for dport in decoder target list
222  * @component_reg_phys: downstream port component registers
223  * @port: reference to cxl_port that contains this downstream port
224  * @list: node for a cxl_port's list of cxl_dport instances
225  */
226 struct cxl_dport {
227         struct device *dport;
228         int port_id;
229         resource_size_t component_reg_phys;
230         struct cxl_port *port;
231         struct list_head list;
232 };
233
234 struct cxl_port *to_cxl_port(struct device *dev);
235 struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
236                                    resource_size_t component_reg_phys,
237                                    struct cxl_port *parent_port);
238
239 int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
240                   resource_size_t component_reg_phys);
241
242 struct cxl_decoder *to_cxl_decoder(struct device *dev);
243 struct cxl_decoder *
244 devm_cxl_add_decoder(struct device *host, struct cxl_port *port, int nr_targets,
245                      resource_size_t base, resource_size_t len,
246                      int interleave_ways, int interleave_granularity,
247                      enum cxl_decoder_type type, unsigned long flags);
248
249 /*
250  * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability Structure)
251  * single ported host-bridges need not publish a decoder capability when a
252  * passthrough decode can be assumed, i.e. all transactions that the uport sees
253  * are claimed and passed to the single dport. Default the range a 0-base
254  * 0-length until the first CXL region is activated.
255  */
256 static inline struct cxl_decoder *
257 devm_cxl_add_passthrough_decoder(struct device *host, struct cxl_port *port)
258 {
259         return devm_cxl_add_decoder(host, port, 1, 0, 0, 1, PAGE_SIZE,
260                                     CXL_DECODER_EXPANDER, 0);
261 }
262
263 extern struct bus_type cxl_bus_type;
264 #endif /* __CXL_H__ */