1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
3 #include <linux/io-64-nonatomic-hi-lo.h>
4 #include <linux/device.h>
5 #include <linux/delay.h>
13 * Compute Express Link Host Managed Device Memory, starting with the
14 * CXL 2.0 specification, is managed by an array of HDM Decoder register
15 * instances per CXL port and per CXL endpoint. Define common helpers
16 * for enumerating these registers and capabilities.
19 static int add_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
24 rc = cxl_decoder_add_locked(cxld, target_map);
26 put_device(&cxld->dev);
27 dev_err(&port->dev, "Failed to add decoder\n");
31 rc = cxl_decoder_autoremove(&port->dev, cxld);
35 dev_dbg(&cxld->dev, "Added to port %s\n", dev_name(&port->dev));
41 * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability Structure)
42 * single ported host-bridges need not publish a decoder capability when a
43 * passthrough decode can be assumed, i.e. all transactions that the uport sees
44 * are claimed and passed to the single dport. Disable the range until the first
45 * CXL region is enumerated / activated.
47 int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
49 struct cxl_decoder *cxld;
50 struct cxl_dport *dport;
51 int single_port_map[1];
53 cxld = cxl_switch_decoder_alloc(port, 1);
57 device_lock_assert(&port->dev);
59 dport = list_first_entry(&port->dports, typeof(*dport), list);
60 single_port_map[0] = dport->port_id;
62 return add_hdm_decoder(port, cxld, single_port_map);
64 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_passthrough_decoder, CXL);
66 static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm)
70 hdm_cap = readl(cxlhdm->regs.hdm_decoder + CXL_HDM_DECODER_CAP_OFFSET);
71 cxlhdm->decoder_count = cxl_hdm_decoder_count(hdm_cap);
72 cxlhdm->target_count =
73 FIELD_GET(CXL_HDM_DECODER_TARGET_COUNT_MASK, hdm_cap);
74 if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_11_8, hdm_cap))
75 cxlhdm->interleave_mask |= GENMASK(11, 8);
76 if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
77 cxlhdm->interleave_mask |= GENMASK(14, 12);
80 static void __iomem *map_hdm_decoder_regs(struct cxl_port *port,
83 struct cxl_component_reg_map map;
85 cxl_probe_component_regs(&port->dev, crb, &map);
86 if (!map.hdm_decoder.valid) {
87 dev_err(&port->dev, "HDM decoder registers invalid\n");
88 return IOMEM_ERR_PTR(-ENXIO);
91 return crb + map.hdm_decoder.offset;
95 * devm_cxl_setup_hdm - map HDM decoder component registers
96 * @port: cxl_port to map
98 struct cxl_hdm *devm_cxl_setup_hdm(struct cxl_port *port)
100 struct device *dev = &port->dev;
101 void __iomem *crb, *hdm;
102 struct cxl_hdm *cxlhdm;
104 cxlhdm = devm_kzalloc(dev, sizeof(*cxlhdm), GFP_KERNEL);
106 return ERR_PTR(-ENOMEM);
109 crb = devm_cxl_iomap_block(dev, port->component_reg_phys,
110 CXL_COMPONENT_REG_BLOCK_SIZE);
112 dev_err(dev, "No component registers mapped\n");
113 return ERR_PTR(-ENXIO);
116 hdm = map_hdm_decoder_regs(port, crb);
118 return ERR_CAST(hdm);
119 cxlhdm->regs.hdm_decoder = hdm;
121 parse_hdm_decoder_caps(cxlhdm);
122 if (cxlhdm->decoder_count == 0) {
123 dev_err(dev, "Spec violation. Caps invalid\n");
124 return ERR_PTR(-ENXIO);
129 EXPORT_SYMBOL_NS_GPL(devm_cxl_setup_hdm, CXL);
131 static int to_interleave_granularity(u32 ctrl)
133 int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl);
138 static int to_interleave_ways(u32 ctrl)
140 int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl);
146 return 3 << (val - 8);
152 static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
153 int *target_map, void __iomem *hdm, int which)
160 unsigned char target_id[8];
163 ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(which));
164 base = ioread64_hi_lo(hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(which));
165 size = ioread64_hi_lo(hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(which));
167 if (!(ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED))
169 if (base == U64_MAX || size == U64_MAX) {
170 dev_warn(&port->dev, "decoder%d.%d: Invalid resource range\n",
175 cxld->decoder_range = (struct range) {
177 .end = base + size - 1,
180 /* switch decoders are always enabled if committed */
181 if (ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED) {
182 cxld->flags |= CXL_DECODER_F_ENABLE;
183 if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
184 cxld->flags |= CXL_DECODER_F_LOCK;
186 cxld->interleave_ways = to_interleave_ways(ctrl);
187 if (!cxld->interleave_ways) {
189 "decoder%d.%d: Invalid interleave ways (ctrl: %#x)\n",
190 port->id, cxld->id, ctrl);
193 cxld->interleave_granularity = to_interleave_granularity(ctrl);
195 if (FIELD_GET(CXL_HDM_DECODER0_CTRL_TYPE, ctrl))
196 cxld->target_type = CXL_DECODER_EXPANDER;
198 cxld->target_type = CXL_DECODER_ACCELERATOR;
200 if (is_endpoint_decoder(&cxld->dev))
204 ioread64_hi_lo(hdm + CXL_HDM_DECODER0_TL_LOW(which));
205 for (i = 0; i < cxld->interleave_ways; i++)
206 target_map[i] = target_list.target_id[i];
212 * devm_cxl_enumerate_decoders - add decoder objects per HDM register set
213 * @cxlhdm: Structure to populate with HDM capabilities
215 int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm)
217 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
218 struct cxl_port *port = cxlhdm->port;
219 int i, committed, failed;
223 * Since the register resource was recently claimed via request_region()
224 * be careful about trusting the "not-committed" status until the commit
225 * timeout has elapsed. The commit timeout is 10ms (CXL 2.0
226 * 8.2.5.12.20), but double it to be tolerant of any clock skew between
229 for (i = 0, committed = 0; i < cxlhdm->decoder_count; i++) {
230 ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(i));
231 if (ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED)
235 /* ensure that future checks of committed can be trusted */
236 if (committed != cxlhdm->decoder_count)
239 for (i = 0, failed = 0; i < cxlhdm->decoder_count; i++) {
240 int target_map[CXL_DECODER_MAX_INTERLEAVE] = { 0 };
241 int rc, target_count = cxlhdm->target_count;
242 struct cxl_decoder *cxld;
244 if (is_cxl_endpoint(port))
245 cxld = cxl_endpoint_decoder_alloc(port);
247 cxld = cxl_switch_decoder_alloc(port, target_count);
250 "Failed to allocate the decoder\n");
251 return PTR_ERR(cxld);
254 rc = init_hdm_decoder(port, cxld, target_map,
255 cxlhdm->regs.hdm_decoder, i);
257 put_device(&cxld->dev);
261 rc = add_hdm_decoder(port, cxld, target_map);
264 "Failed to add decoder to port\n");
269 if (failed == cxlhdm->decoder_count) {
270 dev_err(&port->dev, "No valid decoders found\n");
276 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_decoders, CXL);