Commit 9466ee98 authored by Robert Richter's avatar Robert Richter Committed by Dave Jiang
Browse files

cxl/region: Factor out code to find the root decoder



In function cxl_add_to_region() there is code to determine the root
decoder associated to an endpoint decoder. Factor out that code for
later reuse. This has the benefit of reducing cxl_add_to_region()'s
function complexity.

The reference count must be decremented after using the root decoder.
cxl_find_root_decoder() is paired with the put_cxl_root_decoder
cleanup helper that can be used for this.

[dj: Fixed up "obj __free(...) = NULL" pattern]

Signed-off-by: default avatarRobert Richter <rrichter@amd.com>
Reviewed-by: default avatarGregory Price <gourry@gourry.net>
Reviewed-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: default avatarDave Jiang <dave.jiang@intel.com>
Reviewed-by: default avatarDan Williams <dan.j.williams@intel.com>
Reviewed-by: default avatarAlison Schofield <alison.schofield@intel.com>
Reviewed-by: default avatar"Fabio M. De Francesco" <fabio.m.de.francesco@linux.intel.com>
Tested-by: default avatarGregory Price <gourry@gourry.net>
Acked-by: default avatarDan Williams <dan.j.williams@intel.com>
Link: https://patch.msgid.link/20250509150700.2817697-10-rrichter@amd.com


Signed-off-by: default avatarDave Jiang <dave.jiang@intel.com>
parent 74bf125a
Loading
Loading
Loading
Loading
+29 −19
Original line number Diff line number Diff line
@@ -3216,6 +3216,29 @@ static int match_root_decoder_by_range(struct device *dev,
	return range_contains(r1, r2);
}

static struct cxl_root_decoder *
cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
{
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_port *port = cxled_to_port(cxled);
	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
	struct cxl_decoder *cxld = &cxled->cxld;
	struct range *hpa = &cxld->hpa_range;
	struct device *cxlrd_dev;

	cxlrd_dev = device_find_child(&cxl_root->port.dev, hpa,
				      match_root_decoder_by_range);
	if (!cxlrd_dev) {
		dev_err(cxlmd->dev.parent,
			"%s:%s no CXL window for range %#llx:%#llx\n",
			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
			cxld->hpa_range.start, cxld->hpa_range.end);
		return NULL;
	}

	return to_cxl_root_decoder(cxlrd_dev);
}

static int match_region_by_range(struct device *dev, const void *data)
{
	struct cxl_region_params *p;
@@ -3384,29 +3407,17 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,

int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
{
	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
	struct cxl_port *port = cxled_to_port(cxled);
	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
	struct range *hpa = &cxled->cxld.hpa_range;
	struct cxl_decoder *cxld = &cxled->cxld;
	struct device *cxlrd_dev, *region_dev;
	struct cxl_root_decoder *cxlrd;
	struct device *region_dev;
	struct cxl_region_params *p;
	struct cxl_region *cxlr;
	bool attach = false;
	int rc;

	cxlrd_dev = device_find_child(&cxl_root->port.dev, &cxld->hpa_range,
				      match_root_decoder_by_range);
	if (!cxlrd_dev) {
		dev_err(cxlmd->dev.parent,
			"%s:%s no CXL window for range %#llx:%#llx\n",
			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
			cxld->hpa_range.start, cxld->hpa_range.end);
	struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) =
		cxl_find_root_decoder(cxled);
	if (!cxlrd)
		return -ENXIO;
	}

	cxlrd = to_cxl_root_decoder(cxlrd_dev);

	/*
	 * Ensure that if multiple threads race to construct_region() for @hpa
@@ -3424,7 +3435,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)

	rc = PTR_ERR_OR_ZERO(cxlr);
	if (rc)
		goto out;
		return rc;

	attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);

@@ -3445,8 +3456,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
	}

	put_device(region_dev);
out:
	put_device(cxlrd_dev);

	return rc;
}
EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
+1 −0
Original line number Diff line number Diff line
@@ -740,6 +740,7 @@ struct cxl_root *find_cxl_root(struct cxl_port *port);

DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_device(&_T->port.dev))
DEFINE_FREE(put_cxl_port, struct cxl_port *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
DEFINE_FREE(put_cxl_root_decoder, struct cxl_root_decoder *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev))

int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
void cxl_bus_rescan(void);