1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. */
3 #include <linux/io-64-nonatomic-lo-hi.h>
4 #include <linux/device.h>
5 #include <linux/slab.h>
15 * CXL device capabilities are enumerated by PCI DVSEC (Designated
16 * Vendor-specific) and / or descriptors provided by platform firmware.
17 * They can be defined as a set like the device and component registers
18 * mandated by CXL Section 8.1.12.2 Memory Device PCIe Capabilities and
19 * Extended Capabilities, or they can be individual capabilities
20 * appended to bridged and endpoint devices.
22 * Provide common infrastructure for enumerating and mapping these
23 * discrete capabilities.
27 * cxl_probe_component_regs() - Detect CXL Component register blocks
28 * @dev: Host device of the @base mapping
29 * @base: Mapping containing the HDM Decoder Capability Header
30 * @map: Map object describing the register block information found
32 * See CXL 2.0 8.2.4 Component Register Layout and Definition
33 * See CXL 2.0 8.2.5.5 CXL Device Register Interface
35 * Probe for component register information and return it in map object.
37 void cxl_probe_component_regs(struct device *dev, void __iomem *base,
38 struct cxl_component_reg_map *map)
43 *map = (struct cxl_component_reg_map) { 0 };
46 * CXL.cache and CXL.mem registers are at offset 0x1000 as defined in
47 * CXL 2.0 8.2.4 Table 141.
49 base += CXL_CM_OFFSET;
51 cap_array = readl(base + CXL_CM_CAP_HDR_OFFSET);
53 if (FIELD_GET(CXL_CM_CAP_HDR_ID_MASK, cap_array) != CM_CAP_HDR_CAP_ID) {
55 "Couldn't locate the CXL.cache and CXL.mem capability array header.\n");
59 /* It's assumed that future versions will be backward compatible */
60 cap_count = FIELD_GET(CXL_CM_CAP_HDR_ARRAY_SIZE_MASK, cap_array);
62 for (cap = 1; cap <= cap_count; cap++) {
63 void __iomem *register_block;
64 struct cxl_reg_map *rmap;
68 hdr = readl(base + cap * 0x4);
70 cap_id = FIELD_GET(CXL_CM_CAP_HDR_ID_MASK, hdr);
71 offset = FIELD_GET(CXL_CM_CAP_PTR_MASK, hdr);
72 register_block = base + offset;
73 hdr = readl(register_block);
77 case CXL_CM_CAP_CAP_ID_HDM: {
80 dev_dbg(dev, "found HDM decoder capability (0x%x)\n",
83 decoder_cnt = cxl_hdm_decoder_count(hdr);
84 length = 0x20 * decoder_cnt + 0x10;
85 rmap = &map->hdm_decoder;
88 case CXL_CM_CAP_CAP_ID_RAS:
89 dev_dbg(dev, "found RAS capability (0x%x)\n",
91 length = CXL_RAS_CAPABILITY_LENGTH;
95 dev_dbg(dev, "Unknown CM cap ID: %d (0x%x)\n", cap_id,
104 rmap->offset = CXL_CM_OFFSET + offset;
108 EXPORT_SYMBOL_NS_GPL(cxl_probe_component_regs, CXL);
111 * cxl_probe_device_regs() - Detect CXL Device register blocks
112 * @dev: Host device of the @base mapping
113 * @base: Mapping of CXL 2.0 8.2.8 CXL Device Register Interface
114 * @map: Map object describing the register block information found
116 * Probe for device register information and return it in map object.
118 void cxl_probe_device_regs(struct device *dev, void __iomem *base,
119 struct cxl_device_reg_map *map)
124 *map = (struct cxl_device_reg_map){ 0 };
126 cap_array = readq(base + CXLDEV_CAP_ARRAY_OFFSET);
127 if (FIELD_GET(CXLDEV_CAP_ARRAY_ID_MASK, cap_array) !=
128 CXLDEV_CAP_ARRAY_CAP_ID)
131 cap_count = FIELD_GET(CXLDEV_CAP_ARRAY_COUNT_MASK, cap_array);
133 for (cap = 1; cap <= cap_count; cap++) {
134 struct cxl_reg_map *rmap;
138 cap_id = FIELD_GET(CXLDEV_CAP_HDR_CAP_ID_MASK,
139 readl(base + cap * 0x10));
140 offset = readl(base + cap * 0x10 + 0x4);
141 length = readl(base + cap * 0x10 + 0x8);
145 case CXLDEV_CAP_CAP_ID_DEVICE_STATUS:
146 dev_dbg(dev, "found Status capability (0x%x)\n", offset);
149 case CXLDEV_CAP_CAP_ID_PRIMARY_MAILBOX:
150 dev_dbg(dev, "found Mailbox capability (0x%x)\n", offset);
153 case CXLDEV_CAP_CAP_ID_SECONDARY_MAILBOX:
154 dev_dbg(dev, "found Secondary Mailbox capability (0x%x)\n", offset);
156 case CXLDEV_CAP_CAP_ID_MEMDEV:
157 dev_dbg(dev, "found Memory Device capability (0x%x)\n", offset);
161 if (cap_id >= 0x8000)
162 dev_dbg(dev, "Vendor cap ID: %#x offset: %#x\n", cap_id, offset);
164 dev_dbg(dev, "Unknown cap ID: %#x offset: %#x\n", cap_id, offset);
172 rmap->offset = offset;
176 EXPORT_SYMBOL_NS_GPL(cxl_probe_device_regs, CXL);
178 void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
179 resource_size_t length)
181 void __iomem *ret_val;
182 struct resource *res;
184 if (WARN_ON_ONCE(addr == CXL_RESOURCE_NONE))
187 res = devm_request_mem_region(dev, addr, length, dev_name(dev));
189 resource_size_t end = addr + length - 1;
191 dev_err(dev, "Failed to request region %pa-%pa\n", &addr, &end);
195 ret_val = devm_ioremap(dev, addr, length);
197 dev_err(dev, "Failed to map region %pr\n", res);
202 int cxl_map_component_regs(struct device *dev, struct cxl_component_regs *regs,
203 struct cxl_register_map *map, unsigned long map_mask)
206 struct cxl_reg_map *rmap;
209 { &map->component_map.hdm_decoder, ®s->hdm_decoder },
210 { &map->component_map.ras, ®s->ras },
214 for (i = 0; i < ARRAY_SIZE(mapinfo); i++) {
215 struct mapinfo *mi = &mapinfo[i];
216 resource_size_t phys_addr;
217 resource_size_t length;
219 if (!mi->rmap->valid)
221 if (!test_bit(mi->rmap->id, &map_mask))
223 phys_addr = map->resource + mi->rmap->offset;
224 length = mi->rmap->size;
225 *(mi->addr) = devm_cxl_iomap_block(dev, phys_addr, length);
232 EXPORT_SYMBOL_NS_GPL(cxl_map_component_regs, CXL);
234 int cxl_map_device_regs(struct device *dev,
235 struct cxl_device_regs *regs,
236 struct cxl_register_map *map)
238 resource_size_t phys_addr = map->resource;
240 struct cxl_reg_map *rmap;
243 { &map->device_map.status, ®s->status, },
244 { &map->device_map.mbox, ®s->mbox, },
245 { &map->device_map.memdev, ®s->memdev, },
249 for (i = 0; i < ARRAY_SIZE(mapinfo); i++) {
250 struct mapinfo *mi = &mapinfo[i];
251 resource_size_t length;
252 resource_size_t addr;
254 if (!mi->rmap->valid)
257 addr = phys_addr + mi->rmap->offset;
258 length = mi->rmap->size;
259 *(mi->addr) = devm_cxl_iomap_block(dev, addr, length);
266 EXPORT_SYMBOL_NS_GPL(cxl_map_device_regs, CXL);
268 static bool cxl_decode_regblock(struct pci_dev *pdev, u32 reg_lo, u32 reg_hi,
269 struct cxl_register_map *map)
271 int bar = FIELD_GET(CXL_DVSEC_REG_LOCATOR_BIR_MASK, reg_lo);
272 u64 offset = ((u64)reg_hi << 32) |
273 (reg_lo & CXL_DVSEC_REG_LOCATOR_BLOCK_OFF_LOW_MASK);
275 if (offset > pci_resource_len(pdev, bar)) {
277 "BAR%d: %pr: too small (offset: %pa, type: %d)\n", bar,
278 &pdev->resource[bar], &offset, map->reg_type);
282 map->reg_type = FIELD_GET(CXL_DVSEC_REG_LOCATOR_BLOCK_ID_MASK, reg_lo);
283 map->resource = pci_resource_start(pdev, bar) + offset;
284 map->max_size = pci_resource_len(pdev, bar) - offset;
289 * cxl_find_regblock() - Locate register blocks by type
290 * @pdev: The CXL PCI device to enumerate.
291 * @type: Register Block Indicator id
292 * @map: Enumeration output, clobbered on error
294 * Return: 0 if register block enumerated, negative error code otherwise
296 * A CXL DVSEC may point to one or more register blocks, search for them
299 int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
300 struct cxl_register_map *map)
302 u32 regloc_size, regblocks;
305 map->resource = CXL_RESOURCE_NONE;
306 regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
307 CXL_DVSEC_REG_LOCATOR);
311 pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, ®loc_size);
312 regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
314 regloc += CXL_DVSEC_REG_LOCATOR_BLOCK1_OFFSET;
315 regblocks = (regloc_size - CXL_DVSEC_REG_LOCATOR_BLOCK1_OFFSET) / 8;
317 for (i = 0; i < regblocks; i++, regloc += 8) {
320 pci_read_config_dword(pdev, regloc, ®_lo);
321 pci_read_config_dword(pdev, regloc + 4, ®_hi);
323 if (!cxl_decode_regblock(pdev, reg_lo, reg_hi, map))
326 if (map->reg_type == type)
330 map->resource = CXL_RESOURCE_NONE;
333 EXPORT_SYMBOL_NS_GPL(cxl_find_regblock, CXL);
335 resource_size_t cxl_rcrb_to_component(struct device *dev,
336 resource_size_t rcrb,
339 resource_size_t component_reg_phys;
345 if (which == CXL_RCRB_UPSTREAM)
349 * RCRB's BAR[0..1] point to component block containing CXL
350 * subsystem component registers. MEMBAR extraction follows
351 * the PCI Base spec here, esp. 64 bit extraction and memory
352 * ranges alignment (6.0, 7.5.1.2.1).
354 if (!request_mem_region(rcrb, SZ_4K, "CXL RCRB"))
355 return CXL_RESOURCE_NONE;
356 addr = ioremap(rcrb, SZ_4K);
358 dev_err(dev, "Failed to map region %pr\n", addr);
359 release_mem_region(rcrb, SZ_4K);
360 return CXL_RESOURCE_NONE;
363 id = readl(addr + PCI_VENDOR_ID);
364 cmd = readw(addr + PCI_COMMAND);
365 bar0 = readl(addr + PCI_BASE_ADDRESS_0);
366 bar1 = readl(addr + PCI_BASE_ADDRESS_1);
368 release_mem_region(rcrb, SZ_4K);
371 * Sanity check, see CXL 3.0 Figure 9-8 CXL Device that Does Not
372 * Remap Upstream Port and Component Registers
375 if (which == CXL_RCRB_DOWNSTREAM)
376 dev_err(dev, "Failed to access Downstream Port RCRB\n");
377 return CXL_RESOURCE_NONE;
379 if (!(cmd & PCI_COMMAND_MEMORY))
380 return CXL_RESOURCE_NONE;
381 /* The RCRB is a Memory Window, and the MEM_TYPE_1M bit is obsolete */
382 if (bar0 & (PCI_BASE_ADDRESS_MEM_TYPE_1M | PCI_BASE_ADDRESS_SPACE_IO))
383 return CXL_RESOURCE_NONE;
385 component_reg_phys = bar0 & PCI_BASE_ADDRESS_MEM_MASK;
386 if (bar0 & PCI_BASE_ADDRESS_MEM_TYPE_64)
387 component_reg_phys |= ((u64)bar1) << 32;
389 if (!component_reg_phys)
390 return CXL_RESOURCE_NONE;
392 /* MEMBAR is block size (64k) aligned. */
393 if (!IS_ALIGNED(component_reg_phys, CXL_COMPONENT_REG_BLOCK_SIZE))
394 return CXL_RESOURCE_NONE;
396 return component_reg_phys;
398 EXPORT_SYMBOL_NS_GPL(cxl_rcrb_to_component, CXL);