// SPDX-License-Identifier: GPL-2.0-only /* Copyright(c) 2022 Intel Corporation. All rights reserved. */ #include <linux/seq_file.h> #include <linux/device.h> #include <linux/delay.h>
#include"cxlmem.h" #include"core.h"
/** * DOC: cxl core hdm * * Compute Express Link Host Managed Device Memory, starting with the * CXL 2.0 specification, is managed by an array of HDM Decoder register * instances per CXL port and per CXL endpoint. Define common helpers * for enumerating these registers and capabilities.
*/
staticint add_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, int *target_map)
{ int rc;
rc = cxl_decoder_add_locked(cxld, target_map); if (rc) {
put_device(&cxld->dev);
dev_err(&port->dev, "Failed to add decoder\n"); return rc;
}
rc = cxl_decoder_autoremove(&port->dev, cxld); if (rc) return rc;
dev_dbg(port->uport_dev, "%s added to %s\n",
dev_name(&cxld->dev), dev_name(&port->dev));
return 0;
}
/* * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability Structure) * single ported host-bridges need not publish a decoder capability when a * passthrough decode can be assumed, i.e. all transactions that the uport sees * are claimed and passed to the single dport. Disable the range until the first * CXL region is enumerated / activated.
*/ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
{ struct cxl_switch_decoder *cxlsd; struct cxl_dport *dport = NULL; int single_port_map[1]; unsignedlong index; struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
/* * Capability checks are moot for passthrough decoders, support * any and all possibilities.
*/
cxlhdm->interleave_mask = ~0U;
cxlhdm->iw_cap_mask = ~0UL;
cxlsd = cxl_switch_decoder_alloc(port, 1); if (IS_ERR(cxlsd)) return PTR_ERR(cxlsd);
/* * If HDM decoders are present and the driver is in control of * Mem_Enable skip DVSEC based emulation
*/ if (!info->mem_enabled) returnfalse;
/* * If any decoders are committed already, there should not be any * emulated DVSEC decoders.
*/ for (i = 0; i < cxlhdm->decoder_count; i++) {
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(i));
dev_dbg(&info->port->dev, "decoder%d.%d: committed: %ld base: %#x_%.8x size: %#x_%.8x\n",
info->port->id, i,
FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl),
readl(hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i)),
readl(hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(i)),
readl(hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i)),
readl(hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i))); if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl)) returnfalse;
}
/* Memory devices can configure device HDM using DVSEC range regs. */ if (reg_map->resource == CXL_RESOURCE_NONE) { if (!info || !info->mem_enabled) {
dev_err(dev, "No component registers mapped\n"); return ERR_PTR(-ENXIO);
}
/* * Now that the hdm capability is parsed, decide if range * register emulation is needed and fixup cxlhdm accordingly.
*/ if (should_emulate_decoders(info)) {
dev_dbg(dev, "Fallback map %d range register%s\n", info->ranges,
info->ranges > 1 ? "s" : "");
cxlhdm->decoder_count = info->ranges;
}
/* * Must be called from context that will not race port device * unregistration, like decoder sysfs attribute methods
*/ staticvoid devm_cxl_dpa_release(struct cxl_endpoint_decoder *cxled)
{ struct cxl_port *port = cxled_to_port(cxled);
/** * request_skip() - Track DPA 'skip' in @cxlds->dpa_res resource tree * @cxlds: CXL.mem device context that parents @cxled * @cxled: Endpoint decoder establishing new allocation that skips lower DPA * @skip_base: DPA < start of new DPA allocation (DPAnew) * @skip_len: @skip_base + @skip_len == DPAnew * * DPA 'skip' arises from out-of-sequence DPA allocation events relative * to free capacity across multiple partitions. It is a wasteful event * as usable DPA gets thrown away, but if a deployment has, for example, * a dual RAM+PMEM device, wants to use PMEM, and has unallocated RAM * DPA, the free RAM DPA must be sacrificed to start allocating PMEM. * See third "Implementation Note" in CXL 3.1 8.2.4.19.13 "Decoder * Protection" for more details. * * A 'skip' always covers the last allocated DPA in a previous partition * to the start of the current partition to allocate. Allocations never * start in the middle of a partition, and allocations are always * de-allocated in reverse order (see cxl_dpa_free(), or natural devm * unwind order from forced in-order allocation). * * If @cxlds->nr_partitions was guaranteed to be <= 2 then the 'skip' * would always be contained to a single partition. Given * @cxlds->nr_partitions may be > 2 it results in cases where the 'skip' * might span "tail capacity of partition[0], all of partition[1], ..., * all of partition[N-1]" to support allocating from partition[N]. That * in turn interacts with the partition 'struct resource' boundaries * within @cxlds->dpa_res whereby 'skip' requests need to be divided by * partition. I.e. this is a quirk of using a 'struct resource' tree to * detect range conflicts while also tracking partition boundaries in * @cxlds->dpa_res.
*/ staticint request_skip(struct cxl_dev_state *cxlds, struct cxl_endpoint_decoder *cxled, const resource_size_t skip_base, const resource_size_t skip_len)
{
resource_size_t skipped = __adjust_skip(cxlds, skip_base, skip_len,
dev_name(&cxled->cxld.dev));
if (skipped == skip_len) return 0;
dev_dbg(cxlds->dev, "%s: failed to reserve skipped space (%pa %pa %pa)\n",
dev_name(&cxled->cxld.dev), &skip_base, &skip_len, &skipped);
if (port->hdm_end + 1 != cxled->cxld.id) { /* * Assumes alloc and commit order is always in hardware instance * order per expectations from 8.2.5.12.20 Committing Decoder * Programming that enforce decoder[m] committed before * decoder[m+1] commit start.
*/
dev_dbg(dev, "decoder%d.%d: expected decoder%d.%d\n", port->id,
cxled->cxld.id, port->id, port->hdm_end + 1); return -EBUSY;
}
if (skipped) {
rc = request_skip(cxlds, cxled, base - skipped, skipped); if (rc) return rc;
}
res = __request_region(&cxlds->dpa_res, base, len,
dev_name(&cxled->cxld.dev), 0); if (!res) {
dev_dbg(dev, "decoder%d.%d: failed to reserve allocation\n",
port->id, cxled->cxld.id); if (skipped)
release_skip(cxlds, base - skipped, skipped); return -EBUSY;
}
cxled->dpa_res = res;
cxled->skip = skipped;
/* * When allocating new capacity, ->part is already set, when * discovering decoder settings at initial enumeration, ->part * is not set.
*/ if (cxled->part < 0) for (int i = 0; cxlds->nr_partitions; i++) if (resource_contains(&cxlds->part[i].res, res)) {
cxled->part = i; break;
}
if (cxled->part < 0)
dev_warn(dev, "decoder%d.%d: %pr does not map any partition\n",
port->id, cxled->cxld.id, res);
staticconstchar *cxl_mode_name(enum cxl_partition_mode mode)
{ switch (mode) { case CXL_PARTMODE_RAM: return"ram"; case CXL_PARTMODE_PMEM: return"pmem"; default: return"";
};
}
/* if this fails the caller must destroy @cxlds, there is no recovery */ int cxl_dpa_setup(struct cxl_dev_state *cxlds, conststruct cxl_dpa_info *info)
{ struct device *dev = cxlds->dev;
part = cxled->part; if (part < 0) {
dev_dbg(dev, "partition not set\n"); return -EBUSY;
}
res = &cxlds->part[part].res; for (p = res->child, last = NULL; p; p = p->sibling)
last = p; if (last)
start = last->end + 1; else
start = res->start;
/* * To allocate at partition N, a skip needs to be calculated for all * unallocated space at lower partitions indices. * * If a partition has any allocations, the search can end because a * previous cxl_dpa_alloc() invocation is assumed to have accounted for * all previous partitions.
*/
skip_start = CXL_RESOURCE_NONE; for (int i = part; i; i--) {
prev = &cxlds->part[i - 1].res; for (p = prev->child, last = NULL; p; p = p->sibling)
last = p; if (last) {
skip_start = last->end + 1; break;
}
skip_start = prev->start;
}
*tgt = FIELD_PREP(GENMASK(7, 0), t[0]->port_id); if (ways > 1)
*tgt |= FIELD_PREP(GENMASK(15, 8), t[1]->port_id); if (ways > 2)
*tgt |= FIELD_PREP(GENMASK(23, 16), t[2]->port_id); if (ways > 3)
*tgt |= FIELD_PREP(GENMASK(31, 24), t[3]->port_id); if (ways > 4)
*tgt |= FIELD_PREP(GENMASK_ULL(39, 32), t[4]->port_id); if (ways > 5)
*tgt |= FIELD_PREP(GENMASK_ULL(47, 40), t[5]->port_id); if (ways > 6)
*tgt |= FIELD_PREP(GENMASK_ULL(55, 48), t[6]->port_id); if (ways > 7)
*tgt |= FIELD_PREP(GENMASK_ULL(63, 56), t[7]->port_id);
}
/* * Per CXL 2.0 8.2.5.12.20 Committing Decoder Programming, hardware must set * committed or error within 10ms, but just be generous with 20ms to account for * clock skew and other marginal behavior
*/ #define COMMIT_TIMEOUT_MS 20 staticint cxld_await_commit(void __iomem *hdm, int id)
{
u32 ctrl; int i;
for (i = 0; i < COMMIT_TIMEOUT_MS; i++) {
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id)); if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMIT_ERROR, ctrl)) {
ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id)); return -EIO;
} if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl)) return 0;
fsleep(1000);
}
return -ETIMEDOUT;
}
staticvoid setup_hw_decoder(struct cxl_decoder *cxld, void __iomem *hdm)
{ int id = cxld->id;
u64 base, size;
u32 ctrl;
/* common decoder settings */
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
cxld_set_interleave(cxld, &ctrl);
cxld_set_type(cxld, &ctrl);
base = cxld->hpa_range.start;
size = range_len(&cxld->hpa_range);
if (cxl_num_decoders_committed(port) != id) {
dev_dbg(&port->dev, "%s: out of order commit, expected decoder%d.%d\n",
dev_name(&cxld->dev), port->id,
cxl_num_decoders_committed(port)); return -EBUSY;
}
/* * For endpoint decoders hosted on CXL memory devices that * support the sanitize operation, make sure sanitize is not in-flight.
*/ if (is_endpoint_decoder(&cxld->dev)) { struct cxl_endpoint_decoder *cxled =
to_cxl_endpoint_decoder(&cxld->dev); struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); struct cxl_memdev_state *mds =
to_cxl_memdev_state(cxlmd->cxlds);
if (mds && mds->security.sanitize_active) {
dev_dbg(&cxlmd->dev, "attempted to commit %s during sanitize\n",
dev_name(&cxld->dev)); return -EBUSY;
}
}
/* * Once the highest committed decoder is disabled, free any other * decoders that were pinned allocated by out-of-order release.
*/
port->commit_end--;
dev_dbg(&port->dev, "reap: %s commit_end: %d\n", dev_name(&cxld->dev),
port->commit_end);
device_for_each_child_reverse_from(&port->dev, &cxld->dev, NULL,
commit_reap);
}
EXPORT_SYMBOL_NS_GPL(cxl_port_commit_reap, "CXL");
if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0) return;
if (port->commit_end == id)
cxl_port_commit_reap(cxld); else
dev_dbg(&port->dev, "%s: out of order reset, expected decoder%d.%d\n",
dev_name(&cxld->dev), port->id, port->commit_end);
/* * Set the emulated decoder as locked pending additional support to * change the range registers at run time.
*/
cxld->flags |= CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
port->commit_end = cxld->id;
rc = devm_cxl_dpa_reserve(cxled, *dpa_base, len, 0); if (rc) {
dev_err(&port->dev, "decoder%d.%d: Failed to reserve DPA range %#llx - %#llx\n (%d)",
port->id, cxld->id, *dpa_base, *dpa_base + len - 1, rc); return rc;
}
*dpa_base += len;
cxled->state = CXL_DECODER_STATE_AUTO;
return 0;
}
staticint init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, int *target_map, void __iomem *hdm, int which,
u64 *dpa_base, struct cxl_endpoint_dvsec_info *info)
{ struct cxl_endpoint_decoder *cxled = NULL;
u64 size, base, skip, dpa_size, lo, hi; bool committed;
u32 remainder; int i, rc;
u32 ctrl; union {
u64 value; unsignedchar target_id[8];
} target_list;
if (should_emulate_decoders(info)) return cxl_setup_hdm_decoder_from_dvsec(port, cxld, dpa_base,
which, info);
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(which));
lo = readl(hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(which));
hi = readl(hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(which));
base = (hi << 32) + lo;
lo = readl(hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(which));
hi = readl(hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(which));
size = (hi << 32) + lo;
committed = !!(ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED);
cxld->commit = cxl_decoder_commit;
cxld->reset = cxl_decoder_reset;
/* decoders are enabled if committed */ if (committed) {
cxld->flags |= CXL_DECODER_F_ENABLE; if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
cxld->flags |= CXL_DECODER_F_LOCK; if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
cxld->target_type = CXL_DECODER_HOSTONLYMEM; else
cxld->target_type = CXL_DECODER_DEVMEM;
guard(rwsem_write)(&cxl_rwsem.region); if (cxld->id != cxl_num_decoders_committed(port)) {
dev_warn(&port->dev, "decoder%d.%d: Committed out of order\n",
port->id, cxld->id); return -ENXIO;
}
if (size == 0) {
dev_warn(&port->dev, "decoder%d.%d: Committed with zero size\n",
port->id, cxld->id); return -ENXIO;
}
port->commit_end = cxld->id;
} else { if (cxled) { struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); struct cxl_dev_state *cxlds = cxlmd->cxlds;
/* * Default by devtype until a device arrives that needs * more precision.
*/ if (cxlds->type == CXL_DEVTYPE_CLASSMEM)
cxld->target_type = CXL_DECODER_HOSTONLYMEM; else
cxld->target_type = CXL_DECODER_DEVMEM;
} else { /* To be overridden by region type at commit time */
cxld->target_type = CXL_DECODER_HOSTONLYMEM;
}
/* * Since the register resource was recently claimed via request_region() * be careful about trusting the "not-committed" status until the commit * timeout has elapsed. The commit timeout is 10ms (CXL 2.0 * 8.2.5.12.20), but double it to be tolerant of any clock skew between * host and target.
*/ for (i = 0, committed = 0; i < cxlhdm->decoder_count; i++) {
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(i)); if (ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED)
committed++;
}
/* ensure that future checks of committed can be trusted */ if (committed != cxlhdm->decoder_count)
msleep(20);
}
/** * devm_cxl_enumerate_decoders - add decoder objects per HDM register set * @cxlhdm: Structure to populate with HDM capabilities * @info: cached DVSEC range register info
*/ int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, struct cxl_endpoint_dvsec_info *info)
{ void __iomem *hdm = cxlhdm->regs.hdm_decoder; struct cxl_port *port = cxlhdm->port; int i;
u64 dpa_base = 0;
cxl_settle_decoders(cxlhdm);
for (i = 0; i < cxlhdm->decoder_count; i++) { int target_map[CXL_DECODER_MAX_INTERLEAVE] = { 0 }; int rc, target_count = cxlhdm->target_count; struct cxl_decoder *cxld;
if (is_cxl_endpoint(port)) { struct cxl_endpoint_decoder *cxled;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.