Skip to content

Commit 448a60e

Browse files
yehs1davejiang
authored andcommitted
cxl/core/regs: Refactor out functions to count regblocks of given type
cxl_find_regblock_instance() counts the number of instances of a register block as a side effect of searching through all available register blocks. cxl_count_regblock() throws away that work and recounts all the register blocks by asking cxl_find_regblock_instance() to redo work it has already done until it finally returns an error, that is needlessly wasteful. Let cxl_count_regblock() leverage the counting that cxl_find_regblock_instance() already does by passing in a sentinel value (CXL_INSTANCES_COUNT) that triggers the count to be returned. [ davej: Updated to more concise commit log supplied by djbw ] [ davej: Fix up checkpatch formatting warnings ] Signed-off-by: Huaisheng Ye <huaisheng.ye@intel.com> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Link: https://patch.msgid.link/20250115152600.26482-2-huaisheng.ye@intel.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 6cdbd84 commit 448a60e

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

drivers/cxl/core/regs.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,17 @@ static bool cxl_decode_regblock(struct pci_dev *pdev, u32 reg_lo, u32 reg_hi,
289289
return true;
290290
}
291291

292-
/**
293-
* cxl_find_regblock_instance() - Locate a register block by type / index
294-
* @pdev: The CXL PCI device to enumerate.
295-
* @type: Register Block Indicator id
296-
* @map: Enumeration output, clobbered on error
297-
* @index: Index into which particular instance of a regblock wanted in the
298-
* order found in register locator DVSEC.
299-
*
300-
* Return: 0 if register block enumerated, negative error code otherwise
292+
/*
293+
* __cxl_find_regblock_instance() - Locate a register block or count instances by type / index
294+
* Use CXL_INSTANCES_COUNT for @index if counting instances.
301295
*
302-
* A CXL DVSEC may point to one or more register blocks, search for them
303-
* by @type and @index.
296+
* __cxl_find_regblock_instance() may return:
297+
* 0 - if register block enumerated.
298+
* >= 0 - if counting instances.
299+
* < 0 - error code otherwise.
304300
*/
305-
int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
306-
struct cxl_register_map *map, int index)
301+
static int __cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
302+
struct cxl_register_map *map, int index)
307303
{
308304
u32 regloc_size, regblocks;
309305
int instance = 0;
@@ -342,8 +338,30 @@ int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
342338
}
343339

344340
map->resource = CXL_RESOURCE_NONE;
341+
if (index == CXL_INSTANCES_COUNT)
342+
return instance;
343+
345344
return -ENODEV;
346345
}
346+
347+
/**
348+
* cxl_find_regblock_instance() - Locate a register block by type / index
349+
* @pdev: The CXL PCI device to enumerate.
350+
* @type: Register Block Indicator id
351+
* @map: Enumeration output, clobbered on error
352+
* @index: Index into which particular instance of a regblock wanted in the
353+
* order found in register locator DVSEC.
354+
*
355+
* Return: 0 if register block enumerated, negative error code otherwise
356+
*
357+
* A CXL DVSEC may point to one or more register blocks, search for them
358+
* by @type and @index.
359+
*/
360+
int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
361+
struct cxl_register_map *map, unsigned int index)
362+
{
363+
return __cxl_find_regblock_instance(pdev, type, map, index);
364+
}
347365
EXPORT_SYMBOL_NS_GPL(cxl_find_regblock_instance, "CXL");
348366

349367
/**
@@ -360,7 +378,7 @@ EXPORT_SYMBOL_NS_GPL(cxl_find_regblock_instance, "CXL");
360378
int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
361379
struct cxl_register_map *map)
362380
{
363-
return cxl_find_regblock_instance(pdev, type, map, 0);
381+
return __cxl_find_regblock_instance(pdev, type, map, 0);
364382
}
365383
EXPORT_SYMBOL_NS_GPL(cxl_find_regblock, "CXL");
366384

@@ -371,19 +389,13 @@ EXPORT_SYMBOL_NS_GPL(cxl_find_regblock, "CXL");
371389
*
372390
* Some regblocks may be repeated. Count how many instances.
373391
*
374-
* Return: count of matching regblocks.
392+
* Return: non-negative count of matching regblocks, negative error code otherwise.
375393
*/
376394
int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type)
377395
{
378396
struct cxl_register_map map;
379-
int rc, count = 0;
380397

381-
while (1) {
382-
rc = cxl_find_regblock_instance(pdev, type, &map, count);
383-
if (rc)
384-
return count;
385-
count++;
386-
}
398+
return __cxl_find_regblock_instance(pdev, type, &map, CXL_INSTANCES_COUNT);
387399
}
388400
EXPORT_SYMBOL_NS_GPL(cxl_count_regblock, "CXL");
389401

drivers/cxl/cxl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,11 @@ int cxl_map_device_regs(const struct cxl_register_map *map,
302302
struct cxl_device_regs *regs);
303303
int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
304304

305+
#define CXL_INSTANCES_COUNT -1
305306
enum cxl_regloc_type;
306307
int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
307308
int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
308-
struct cxl_register_map *map, int index);
309+
struct cxl_register_map *map, unsigned int index);
309310
int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
310311
struct cxl_register_map *map);
311312
int cxl_setup_regs(struct cxl_register_map *map);

drivers/cxl/pci.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,8 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
907907
struct cxl_dev_state *cxlds;
908908
struct cxl_register_map map;
909909
struct cxl_memdev *cxlmd;
910-
int i, rc, pmu_count;
910+
int rc, pmu_count;
911+
unsigned int i;
911912
bool irq_avail;
912913

913914
/*
@@ -1009,6 +1010,9 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
10091010
return rc;
10101011

10111012
pmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_PMU);
1013+
if (pmu_count < 0)
1014+
return pmu_count;
1015+
10121016
for (i = 0; i < pmu_count; i++) {
10131017
struct cxl_pmu_regs pmu_regs;
10141018

0 commit comments

Comments
 (0)