[PATCH <i-g-t> v4 1/4] lib/igt_device_scan: Enable finding all IGT devices for a specific driver

Zbigniew Kempczyński zbigniew.kempczynski at intel.com
Thu Mar 13 11:51:23 UTC 2025


On Thu, Mar 13, 2025 at 11:51:51AM +0530, Soham Purkait wrote:
>     Add function to find all the available GPU
> cards by driver name and card type.
> Add driver field to igt_device_card structure 
> for storing driver names.
> 
> v2 : fix for refactoring GPUTOP into a
>      vendor-agnostic tool (Lucas)
> 
> v3 : Separate commit for lib (Kamil)
> 
> v4 : Refactor to use composition strategy
>      for driver and device type filtering
>      Refactor code to improve memory
>      allocation and error handling (Lucas)
> 
> Signed-off-by: Soham Purkait <soham.purkait at intel.com>
> ---
>  lib/igt_device_scan.c | 58 +++++++++++++++++++++++++++++++++++++++++++
>  lib/igt_device_scan.h |  3 +++
>  2 files changed, 61 insertions(+)
> 
> diff --git a/lib/igt_device_scan.c b/lib/igt_device_scan.c
> index 711bedc5c..4d243126e 100644
> --- a/lib/igt_device_scan.c
> +++ b/lib/igt_device_scan.c
> @@ -774,6 +774,10 @@ __copy_dev_to_card(struct igt_device *dev, struct igt_device_card *card)
>  		safe_strncpy(card->render, dev->drm_render,
>  			     sizeof(card->render));
>  
> +	if (dev->driver != NULL)
> +		safe_strncpy(card->driver, dev->driver,
> +			     sizeof(card->driver));
> +
>  	if (dev->pci_slot_name != NULL)
>  		safe_strncpy(card->pci_slot_name, dev->pci_slot_name,
>  			     sizeof(card->pci_slot_name));
> @@ -820,6 +824,60 @@ static bool __find_first_intel_card_by_driver_name(struct igt_device_card *card,
>  	return false;
>  }
>  
> +/**
> + * Iterate over all igt_devices array and find all discrete/integrated cards.
> + * @card: double pointer to igt_device_card structure, containing
> + * an array of igt_device_card structures upon successful return.
> + * @card_type: flag to indicate whether to find discrete, integrated, or
> + * both types of cards. Use 0 for integrated, 1 for discrete, and 2 for both.
> + * @drv_name: name of the driver to match.
> + *
> + * Returns the number of cards found, or -1 on error.
> + */
> +int find_all_intel_card_by_driver_name(struct igt_device_card **card,
> +				       uint8_t card_type, const char *drv_name)
> +{
> +	int count = 0;
> +	struct igt_device *dev;
> +	int is_integrated;
> +	struct igt_device_card *tmp;
> +	struct igt_device_card *crd = NULL;
> +
> +	igt_assert(drv_name);
> +	*card = NULL;
> +
> +	igt_list_for_each_entry(dev, &igt_devs.all, link) {
> +		if (!is_pci_subsystem(dev) || strcmp(dev->driver, drv_name))
> +			continue;
> +
> +		is_integrated = !strncmp(dev->pci_slot_name, INTEGRATED_I915_GPU_PCI_ID,
> +						PCI_SLOT_NAME_SIZE);
> +
> +		if ((card_type == 1 && !is_integrated) ||
> +		    (card_type == 0 && is_integrated) ||
> +		    card_type == 2) {

This if doesn't look well, you should use enum for better readability.
Anyway, haven't you thought to provide new filter instead of function
which returns collection of matching devices? Lets say:

	device:driver=xe,card=0
	device:driver=xe,card=...

--
Zbigniew

> +			tmp = realloc(crd, sizeof(struct igt_device_card) * (count + 1));
> +			if (!tmp) {
> +				free(crd);
> +				return -1;
> +			}
> +
> +			crd = tmp;
> +			__copy_dev_to_card(dev, &crd[count]);
> +			count++;
> +		}
> +	}
> +
> +	if (!count) {
> +		if (!crd)
> +			free(crd);
> +		return 0;
> +	}
> +
> +	*card = crd;
> +	return count;
> +}
> +
>  bool igt_device_find_first_i915_discrete_card(struct igt_device_card *card)
>  {
>  	igt_assert(card);
> diff --git a/lib/igt_device_scan.h b/lib/igt_device_scan.h
> index 92741fe3c..4fbb11193 100644
> --- a/lib/igt_device_scan.h
> +++ b/lib/igt_device_scan.h
> @@ -59,6 +59,7 @@ struct igt_device_card {
>  	char subsystem[NAME_MAX];
>  	char card[NAME_MAX];
>  	char render[NAME_MAX];
> +	char driver[NAME_MAX];
>  	char pci_slot_name[PCI_SLOT_NAME_SIZE+1];
>  	uint16_t pci_vendor, pci_device;
>  };
> @@ -92,6 +93,8 @@ bool igt_device_find_first_i915_discrete_card(struct igt_device_card *card);
>  bool igt_device_find_integrated_card(struct igt_device_card *card);
>  bool igt_device_find_first_xe_discrete_card(struct igt_device_card *card);
>  bool igt_device_find_xe_integrated_card(struct igt_device_card *card);
> +int find_all_intel_card_by_driver_name(struct igt_device_card **card,
> +				       uint8_t want_discrete, const char *drv_name);
>  char *igt_device_get_pretty_name(struct igt_device_card *card, bool numeric);
>  int igt_open_card(struct igt_device_card *card);
>  int igt_open_render(struct igt_device_card *card);
> -- 
> 2.34.1
> 


More information about the igt-dev mailing list