Commit a32320b7 authored by Dan Williams's avatar Dan Williams

cxl/region: Add region autodiscovery

Region autodiscovery is an asynchronous state machine advanced by
cxl_port_probe(). After the decoders on an endpoint port are enumerated
they are scanned for actively enabled instances. Each active decoder is
flagged for auto-assembly CXL_DECODER_F_AUTO and attached to a region.
If a region does not already exist for the address range setting of the
decoder one is created. That creation process may race with other
decoders of the same region being discovered since cxl_port_probe() is
asynchronous. A new 'struct cxl_root_decoder' lock, @range_lock, is
introduced to mitigate that race.

Once all decoders have arrived, "p->nr_targets == p->interleave_ways",
they are sorted by their relative decode position. The sort algorithm
involves finding the point in the cxl_port topology where one leg of the
decode leads to deviceA and the other deviceB. At that point in the
topology the target order in the 'struct cxl_switch_decoder' indicates
the relative position of those endpoint decoders in the region.

>From that point the region goes through the same setup and validation
steps as user-created regions, but instead of programming the decoders
it validates that driver would have written the same values to the
decoders as were already present.
Tested-by: default avatarFan Ni <fan.ni@samsung.com>
Reviewed-by: default avatarVishal Verma <vishal.l.verma@intel.com>
Reviewed-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://lore.kernel.org/r/167601999958.1924368.9366954455835735048.stgit@dwillia2-xfh.jf.intel.comSigned-off-by: default avatarDan Williams <dan.j.williams@intel.com>
parent 32ce3f18
...@@ -676,6 +676,14 @@ static int cxl_decoder_reset(struct cxl_decoder *cxld) ...@@ -676,6 +676,14 @@ static int cxl_decoder_reset(struct cxl_decoder *cxld)
port->commit_end--; port->commit_end--;
cxld->flags &= ~CXL_DECODER_F_ENABLE; cxld->flags &= ~CXL_DECODER_F_ENABLE;
/* Userspace is now responsible for reconfiguring this decoder */
if (is_endpoint_decoder(&cxld->dev)) {
struct cxl_endpoint_decoder *cxled;
cxled = to_cxl_endpoint_decoder(&cxld->dev);
cxled->state = CXL_DECODER_STATE_MANUAL;
}
return 0; return 0;
} }
...@@ -783,6 +791,9 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, ...@@ -783,6 +791,9 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
return rc; return rc;
} }
*dpa_base += dpa_size + skip; *dpa_base += dpa_size + skip;
cxled->state = CXL_DECODER_STATE_AUTO;
return 0; return 0;
} }
......
...@@ -446,6 +446,7 @@ bool is_endpoint_decoder(struct device *dev) ...@@ -446,6 +446,7 @@ bool is_endpoint_decoder(struct device *dev)
{ {
return dev->type == &cxl_decoder_endpoint_type; return dev->type == &cxl_decoder_endpoint_type;
} }
EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, CXL);
bool is_root_decoder(struct device *dev) bool is_root_decoder(struct device *dev)
{ {
...@@ -1628,6 +1629,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port, ...@@ -1628,6 +1629,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
} }
cxlrd->calc_hb = calc_hb; cxlrd->calc_hb = calc_hb;
mutex_init(&cxlrd->range_lock);
cxld = &cxlsd->cxld; cxld = &cxlsd->cxld;
cxld->dev.type = &cxl_decoder_root_type; cxld->dev.type = &cxl_decoder_root_type;
......
This diff is collapsed.
...@@ -261,6 +261,8 @@ resource_size_t cxl_rcrb_to_component(struct device *dev, ...@@ -261,6 +261,8 @@ resource_size_t cxl_rcrb_to_component(struct device *dev,
* cxl_decoder flags that define the type of memory / devices this * cxl_decoder flags that define the type of memory / devices this
* decoder supports as well as configuration lock status See "CXL 2.0 * decoder supports as well as configuration lock status See "CXL 2.0
* 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details. * 8.2.5.12.7 CXL HDM Decoder 0 Control Register" for details.
* Additionally indicate whether decoder settings were autodetected,
* user customized.
*/ */
#define CXL_DECODER_F_RAM BIT(0) #define CXL_DECODER_F_RAM BIT(0)
#define CXL_DECODER_F_PMEM BIT(1) #define CXL_DECODER_F_PMEM BIT(1)
...@@ -334,12 +336,22 @@ static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode) ...@@ -334,12 +336,22 @@ static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
return "mixed"; return "mixed";
} }
/*
* Track whether this decoder is reserved for region autodiscovery, or
* free for userspace provisioning.
*/
enum cxl_decoder_state {
CXL_DECODER_STATE_MANUAL,
CXL_DECODER_STATE_AUTO,
};
/** /**
* struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder * struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder
* @cxld: base cxl_decoder_object * @cxld: base cxl_decoder_object
* @dpa_res: actively claimed DPA span of this decoder * @dpa_res: actively claimed DPA span of this decoder
* @skip: offset into @dpa_res where @cxld.hpa_range maps * @skip: offset into @dpa_res where @cxld.hpa_range maps
* @mode: which memory type / access-mode-partition this decoder targets * @mode: which memory type / access-mode-partition this decoder targets
* @state: autodiscovery state
* @pos: interleave position in @cxld.region * @pos: interleave position in @cxld.region
*/ */
struct cxl_endpoint_decoder { struct cxl_endpoint_decoder {
...@@ -347,6 +359,7 @@ struct cxl_endpoint_decoder { ...@@ -347,6 +359,7 @@ struct cxl_endpoint_decoder {
struct resource *dpa_res; struct resource *dpa_res;
resource_size_t skip; resource_size_t skip;
enum cxl_decoder_mode mode; enum cxl_decoder_mode mode;
enum cxl_decoder_state state;
int pos; int pos;
}; };
...@@ -380,6 +393,7 @@ typedef struct cxl_dport *(*cxl_calc_hb_fn)(struct cxl_root_decoder *cxlrd, ...@@ -380,6 +393,7 @@ typedef struct cxl_dport *(*cxl_calc_hb_fn)(struct cxl_root_decoder *cxlrd,
* @region_id: region id for next region provisioning event * @region_id: region id for next region provisioning event
* @calc_hb: which host bridge covers the n'th position by granularity * @calc_hb: which host bridge covers the n'th position by granularity
* @platform_data: platform specific configuration data * @platform_data: platform specific configuration data
* @range_lock: sync region autodiscovery by address range
* @cxlsd: base cxl switch decoder * @cxlsd: base cxl switch decoder
*/ */
struct cxl_root_decoder { struct cxl_root_decoder {
...@@ -387,6 +401,7 @@ struct cxl_root_decoder { ...@@ -387,6 +401,7 @@ struct cxl_root_decoder {
atomic_t region_id; atomic_t region_id;
cxl_calc_hb_fn calc_hb; cxl_calc_hb_fn calc_hb;
void *platform_data; void *platform_data;
struct mutex range_lock;
struct cxl_switch_decoder cxlsd; struct cxl_switch_decoder cxlsd;
}; };
...@@ -436,6 +451,13 @@ struct cxl_region_params { ...@@ -436,6 +451,13 @@ struct cxl_region_params {
*/ */
#define CXL_REGION_F_INCOHERENT 0 #define CXL_REGION_F_INCOHERENT 0
/*
* Indicate whether this region has been assembled by autodetection or
* userspace assembly. Prevent endpoint decoders outside of automatic
* detection from being added to the region.
*/
#define CXL_REGION_F_AUTO 1
/** /**
* struct cxl_region - CXL region * struct cxl_region - CXL region
* @dev: This region's device * @dev: This region's device
...@@ -699,6 +721,8 @@ struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct device *dev); ...@@ -699,6 +721,8 @@ struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct device *dev);
#ifdef CONFIG_CXL_REGION #ifdef CONFIG_CXL_REGION
bool is_cxl_pmem_region(struct device *dev); bool is_cxl_pmem_region(struct device *dev);
struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev); struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev);
int cxl_add_to_region(struct cxl_port *root,
struct cxl_endpoint_decoder *cxled);
#else #else
static inline bool is_cxl_pmem_region(struct device *dev) static inline bool is_cxl_pmem_region(struct device *dev)
{ {
...@@ -708,6 +732,11 @@ static inline struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev) ...@@ -708,6 +732,11 @@ static inline struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
{ {
return NULL; return NULL;
} }
static inline int cxl_add_to_region(struct cxl_port *root,
struct cxl_endpoint_decoder *cxled)
{
return 0;
}
#endif #endif
/* /*
......
...@@ -30,6 +30,33 @@ static void schedule_detach(void *cxlmd) ...@@ -30,6 +30,33 @@ static void schedule_detach(void *cxlmd)
schedule_cxl_memdev_detach(cxlmd); schedule_cxl_memdev_detach(cxlmd);
} }
static int discover_region(struct device *dev, void *root)
{
struct cxl_endpoint_decoder *cxled;
int rc;
if (!is_endpoint_decoder(dev))
return 0;
cxled = to_cxl_endpoint_decoder(dev);
if ((cxled->cxld.flags & CXL_DECODER_F_ENABLE) == 0)
return 0;
if (cxled->state != CXL_DECODER_STATE_AUTO)
return 0;
/*
* Region enumeration is opportunistic, if this add-event fails,
* continue to the next endpoint decoder.
*/
rc = cxl_add_to_region(root, cxled);
if (rc)
dev_dbg(dev, "failed to add to region: %#llx-%#llx\n",
cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end);
return 0;
}
static int cxl_switch_port_probe(struct cxl_port *port) static int cxl_switch_port_probe(struct cxl_port *port)
{ {
struct cxl_hdm *cxlhdm; struct cxl_hdm *cxlhdm;
...@@ -54,6 +81,7 @@ static int cxl_endpoint_port_probe(struct cxl_port *port) ...@@ -54,6 +81,7 @@ static int cxl_endpoint_port_probe(struct cxl_port *port)
struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport); struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
struct cxl_dev_state *cxlds = cxlmd->cxlds; struct cxl_dev_state *cxlds = cxlmd->cxlds;
struct cxl_hdm *cxlhdm; struct cxl_hdm *cxlhdm;
struct cxl_port *root;
int rc; int rc;
cxlhdm = devm_cxl_setup_hdm(port); cxlhdm = devm_cxl_setup_hdm(port);
...@@ -78,7 +106,24 @@ static int cxl_endpoint_port_probe(struct cxl_port *port) ...@@ -78,7 +106,24 @@ static int cxl_endpoint_port_probe(struct cxl_port *port)
return rc; return rc;
} }
return devm_cxl_enumerate_decoders(cxlhdm); rc = devm_cxl_enumerate_decoders(cxlhdm);
if (rc)
return rc;
/*
* This can't fail in practice as CXL root exit unregisters all
* descendant ports and that in turn synchronizes with cxl_port_probe()
*/
root = find_cxl_root(&cxlmd->dev);
/*
* Now that all endpoint decoders are successfully enumerated, try to
* assemble regions from committed decoders
*/
device_for_each_child(&port->dev, root, discover_region);
put_device(&root->dev);
return 0;
} }
static int cxl_port_probe(struct device *dev) static int cxl_port_probe(struct device *dev)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment