From: Robert Richter <rrichter@amd.com>
To: Dan Williams <dan.j.williams@intel.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Jonathan Cameron <jonathan.cameron@huawei.com>,
"Dave Jiang" <dave.jiang@intel.com>,
Alison Schofield <alison.schofield@intel.com>,
Vishal Verma <vishal.l.verma@intel.com>,
Ira Weiny <ira.weiny@intel.com>
Cc: <linux-kernel@vger.kernel.org>, <linux-cxl@vger.kernel.org>,
"Robert Richter" <rrichter@amd.com>
Subject: [PATCH 2/5] cxl/hdm: Implement address translation for HDM decoding
Date: Fri, 28 Jun 2024 00:46:11 +0200 [thread overview]
Message-ID: <20240627224615.854162-3-rrichter@amd.com> (raw)
In-Reply-To: <20240627224615.854162-1-rrichter@amd.com>
Default expectation of Linux is that HPA == SPA, which means that
hardware addresses in the decoders are the same as the kernel sees
them. However, there are platforms where this is not the case and an
address translation between decoder's (HPA) and the system's physical
addresses (SPA) is needed.
The CXL driver stores all internal hardware address ranges as SPA.
When accessing the HDM decoder's registers, hardware addresses must be
translated back and forth. This is needed for the base addresses in
the CXL Range Registers of the PCIe DVSEC for CXL Devices
(CXL_DVSEC_RANGE_BASE*) or the CXL HDM Decoder Capability Structure
(CXL_HDM_DECODER0_BASE*).
To handle address translation the kernel needs to keep track of the
system's base HPA the decoder bases on. The base can be different
between memory domains, each port may have its own domain. Thus, the
HPA base cannot be shared between CXL ports and its decoders, instead
the base HPA must be stored per port. Each port has its own struct
cxl_hdm to handle the sets of decoders and targets, that struct can
also be used for storing the base.
Add @base_hpa to struct cxl_hdm. Also Introduce helper functions for
the translation and use them to convert the HDM decoder base addresses
to or from an SPA.
While at this, rename len to size for the common base/size naming used
with ranges.
Link: https://lore.kernel.org/all/65c6b8c9a42e4_d2d4294f1@dwillia2-xfh.jf.intel.com.notmuch/
Suggested-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Robert Richter <rrichter@amd.com>
---
drivers/cxl/core/hdm.c | 69 ++++++++++++++++++++++++++++++++++--------
drivers/cxl/cxlmem.h | 1 +
2 files changed, 57 insertions(+), 13 deletions(-)
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 605da9a55d89..50078013f4e3 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -125,6 +125,17 @@ static bool should_emulate_decoders(struct cxl_endpoint_dvsec_info *info)
return true;
}
+static void setup_base_hpa(struct cxl_hdm *cxlhdm)
+{
+ /*
+ * Address translation is not needed on platforms with HPA ==
+ * SPA. HDM decoder addresses all base on system addresses,
+ * there is no offset and the base is zero (cxlhdm->base_hpa
+ * == 0). Nothing to do here as it is already pre-initialized
+ * zero.
+ */
+}
+
/**
* devm_cxl_setup_hdm - map HDM decoder component registers
* @port: cxl_port to map
@@ -144,6 +155,8 @@ struct cxl_hdm *devm_cxl_setup_hdm(struct cxl_port *port,
cxlhdm->port = port;
dev_set_drvdata(dev, cxlhdm);
+ setup_base_hpa(cxlhdm);
+
/* Memory devices can configure device HDM using DVSEC range regs. */
if (reg_map->resource == CXL_RESOURCE_NONE) {
if (!info || !info->mem_enabled) {
@@ -611,6 +624,23 @@ static int cxld_await_commit(void __iomem *hdm, int id)
return -ETIMEDOUT;
}
+/*
+ * Default expectation is that decoder base addresses match
+ * HPA resource values (that is cxlhdm->base_hpa == 0).
+ */
+
+static inline resource_size_t cxl_xlat_to_hpa(resource_size_t base,
+ struct cxl_hdm *cxlhdm)
+{
+ return cxlhdm->base_hpa + base;
+}
+
+static inline resource_size_t cxl_xlat_to_base(resource_size_t hpa,
+ struct cxl_hdm *cxlhdm)
+{
+ return hpa - cxlhdm->base_hpa;
+}
+
static int cxl_decoder_commit(struct cxl_decoder *cxld)
{
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
@@ -655,7 +685,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
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;
+ base = cxl_xlat_to_base(cxld->hpa_range.start, cxlhdm);
size = range_len(&cxld->hpa_range);
writel(upper_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
@@ -746,22 +776,27 @@ static int cxl_setup_hdm_decoder_from_dvsec(
struct cxl_port *port, struct cxl_decoder *cxld, u64 *dpa_base,
int which, struct cxl_endpoint_dvsec_info *info)
{
+ struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
struct cxl_endpoint_decoder *cxled;
- u64 len;
+ u64 base, size;
int rc;
if (!is_cxl_endpoint(port))
return -EOPNOTSUPP;
cxled = to_cxl_endpoint_decoder(&cxld->dev);
- len = range_len(&info->dvsec_range[which]);
- if (!len)
+ size = range_len(&info->dvsec_range[which]);
+ if (!size)
return -ENOENT;
+ base = cxl_xlat_to_hpa(info->dvsec_range[which].start, cxlhdm);
cxld->target_type = CXL_DECODER_HOSTONLYMEM;
cxld->commit = NULL;
cxld->reset = NULL;
- cxld->hpa_range = info->dvsec_range[which];
+ cxld->hpa_range = (struct range) {
+ .start = base,
+ .end = base + size -1,
+ };
/*
* Set the emulated decoder as locked pending additional support to
@@ -770,14 +805,14 @@ static int cxl_setup_hdm_decoder_from_dvsec(
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);
+ rc = devm_cxl_dpa_reserve(cxled, *dpa_base, size, 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);
+ port->id, cxld->id, *dpa_base, *dpa_base + size - 1, rc);
return rc;
}
- *dpa_base += len;
+ *dpa_base += size;
cxled->state = CXL_DECODER_STATE_AUTO;
return 0;
@@ -787,6 +822,7 @@ static int 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_hdm *cxlhdm = dev_get_drvdata(&port->dev);
struct cxl_endpoint_decoder *cxled = NULL;
u64 size, base, skip, dpa_size, lo, hi;
bool committed;
@@ -823,6 +859,9 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
if (info)
cxled = to_cxl_endpoint_decoder(&cxld->dev);
+
+ base = cxl_xlat_to_hpa(base, cxlhdm);
+
cxld->hpa_range = (struct range) {
.start = base,
.end = base + size - 1,
@@ -1107,16 +1146,20 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
}
for (i = 0, allowed = 0; info->mem_enabled && i < info->ranges; i++) {
- struct device *cxld_dev;
-
- cxld_dev = device_find_child(&root->dev, &info->dvsec_range[i],
- dvsec_range_allowed);
+ u64 base = cxl_xlat_to_hpa(info->dvsec_range[i].start, cxlhdm);
+ u64 size = range_len(&info->dvsec_range[i]);
+ struct range hpa_range = {
+ .start = base,
+ .end = base + size -1,
+ };
+ struct device *cxld_dev __free(put_device) =
+ cxld_dev = device_find_child(&root->dev, &hpa_range,
+ dvsec_range_allowed);
if (!cxld_dev) {
dev_dbg(dev, "DVSEC Range%d denied by platform\n", i);
continue;
}
dev_dbg(dev, "DVSEC Range%d allowed by platform\n", i);
- put_device(cxld_dev);
allowed++;
}
diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
index 61d9f4e00921..849ea97385c9 100644
--- a/drivers/cxl/cxlmem.h
+++ b/drivers/cxl/cxlmem.h
@@ -856,6 +856,7 @@ struct cxl_hdm {
unsigned int decoder_count;
unsigned int target_count;
unsigned int interleave_mask;
+ u64 base_hpa;
struct cxl_port *port;
};
--
2.39.2
next prev parent reply other threads:[~2024-06-27 22:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-27 22:46 [PATCH 0/5] cxl: Address translation for HDM decoding Robert Richter
2024-06-27 22:46 ` [PATCH 1/5] cxl/hdm: Moving HDM specific code to core/hdm.c Robert Richter
2024-06-27 22:46 ` Robert Richter [this message]
2024-06-27 22:46 ` [PATCH 3/5] cxl/acpi: Add platform flag for HPA address translation Robert Richter
2024-06-27 22:46 ` [PATCH 4/5] cxl/hdm: Setup HPA base for address translation using the HPA window in CFMWS Robert Richter
2024-06-27 22:46 ` [PATCH 5/5] cxl/acpi: Enable address translation for Zen4 platforms Robert Richter
2024-06-29 8:22 ` kernel test robot
2024-06-29 11:29 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240627224615.854162-3-rrichter@amd.com \
--to=rrichter@amd.com \
--cc=alison.schofield@intel.com \
--cc=dan.j.williams@intel.com \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=ira.weiny@intel.com \
--cc=jonathan.cameron@huawei.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vishal.l.verma@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox