[PATCH i-g-t v8 1/5] lib/igt_device_scan: Add support for the device filter

Zbigniew Kempczyński zbigniew.kempczynski at intel.com
Fri Apr 18 07:56:46 UTC 2025


On Thu, Apr 17, 2025 at 07:29:37PM +0530, Soham Purkait wrote:
> Add support for the device filter based on
> driver string, device type (integrated or discrete)
> and card number.
> 
> v5 : Add device filter to filter out
>      matching devices.           (Zbigniew)
> 
> v6 : Move device filter with Separate
>      commit.                     (Zbigniew)
> 
> v7 : Fix interpretation of card numbering
>      and add 'all' option for all the cards.
>                                  (Zbigniew)
> 
> v8 : Fix for card filter output. (Zbigniew)
> 
> Signed-off-by: Soham Purkait <soham.purkait at intel.com>

This filter still doesn't behave as I expect (lsgpu -d device:
doesn't open render node). List of device filter examples and
their results are:

1. device:card=all
   collects all drm devices

2. device:driver=xe,card=all
   collects all drm devices for xe driver

3. device:driver=xe
   picks first (implicit card=0) device of xe driver

4. device:driver=xe,card=1
   picks second (explicit card=0) device of xe driver

device=type stays as it is.

With your igt_device_card_match_all() we may extend lsgpu
to handle multiple cards (currently it is limited to open
first one).

In the review of 5/5 (gputop) I put 'device:' filter for
populate_devices(), but it should be 'device:card=all'.

--
Zbigniew

> ---
>  lib/igt_device_scan.c | 71 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 71 insertions(+)
> 
> diff --git a/lib/igt_device_scan.c b/lib/igt_device_scan.c
> index 3f26a1737..3dbe06812 100644
> --- a/lib/igt_device_scan.c
> +++ b/lib/igt_device_scan.c
> @@ -1709,6 +1709,71 @@ static struct igt_list_head *filter_sriov(const struct filter_class *fcls,
>  	return &igt_devs.filtered;
>  }
>  
> +/*
> + * Find appropriate gpu device through matching driver, device type and
> + * card filter arguments.
> + */
> +static struct igt_list_head *filter_device(const struct filter_class *fcls,
> +					   const struct filter *filter)
> +{
> +	struct igt_device *dev;
> +	bool allcards = false;
> +	int card = 0;
> +	(void)fcls;
> +
> +	DBG("filter device\n");
> +	if (filter->data.card) {
> +		char crdop[5] = {0};
> +
> +		if (sscanf(filter->data.card, "%d", &card) == 1) {
> +			if (card < 0)
> +				return &igt_devs.filtered;
> +		} else {
> +			card = 0;
> +			if (sscanf(filter->data.card, "%4s", crdop) == 1) {
> +				if (!strcmp(crdop, "all"))
> +					allcards = true;
> +				else
> +					return &igt_devs.filtered;
> +			} else {
> +				return &igt_devs.filtered;
> +			}
> +		}
> +	} else {
> +		card = 0;
> +	}
> +
> +	igt_list_for_each_entry(dev, &igt_devs.all, link) {
> +		/* Skip if 'driver' doesn't match */
> +		if (filter->data.driver && !strequal(filter->data.driver, dev->driver))
> +			continue;
> +
> +		/* Skip if 'device' doesn't match */
> +		if (filter->data.device && !is_device_matched(dev, filter->data.device))
> +			continue;
> +
> +		/* We get n-th card */
> +		if (!allcards && !card) {
> +			struct igt_device *dup = duplicate_device(dev);
> +
> +			igt_list_add_tail(&dup->link, &igt_devs.filtered);
> +			break;
> +		} else if (!allcards) {
> +			card--;
> +		}
> +		/* Include all the cards */
> +		else if (allcards) {
> +			struct igt_device *dup = duplicate_device(dev);
> +
> +			igt_list_add(&dup->link, &igt_devs.filtered);
> +		}
> +	}
> +
> +	DBG("Filter device filtered size: %d\n", igt_list_length(&igt_devs.filtered));
> +
> +	return &igt_devs.filtered;
> +}
> +
>  static bool sys_path_valid(const struct filter_class *fcls,
>  			   const struct filter *filter)
>  {
> @@ -1750,6 +1815,12 @@ static struct filter_class filter_definition_list[] = {
>  		.help = "sriov:[vendor=%04x/name][,device=%04x][,card=%d][,pf=%d][,vf=%d]",
>  		.detail = "find pf or vf\n",
>  	},
> +	{
> +		.name = "device",
> +		.filter_function = filter_device,
> +		.help = "device:[driver=name][,device=type][,card=%d|all]",
> +		.detail = "find device by driver name, device type and card number\n",
> +	},
>  	{
>  		.name = NULL,
>  	},
> -- 
> 2.34.1
> 


More information about the igt-dev mailing list