gpu: nvgpu: Add device for_each macro

Add a macro to iterate over a device list; it is just a wrapper to
the nvgpu_list_for_each() macro. It lets code iterate over the
list of detected devices without being aware of the underlying
instance IDs.

This also removes the need to do a separate nvgpu_device_get()
and subsequent NULL checking. This will reduce overhead for
unit testing!

Change-Id: If41dbee30a743d29ab62ce930a819160265b9351
Signed-off-by: Alex Waterman <alexw@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2404914
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
This commit is contained in:
Alex Waterman
2020-08-27 14:32:19 -05:00
parent b269aae9f2
commit 2b48aa5b0c
3 changed files with 32 additions and 18 deletions

View File

@@ -32,14 +32,6 @@
nvgpu_log(g, gpu_dbg_device, fmt, ##args); \
} while (0)
static inline struct nvgpu_device *
nvgpu_device_from_dev_list_node(struct nvgpu_list_node *node)
{
return (struct nvgpu_device *)
((uintptr_t)node - offsetof(struct nvgpu_device,
dev_list_node));
};
static inline const char *nvgpu_device_type_to_str(const struct nvgpu_device *dev)
{
const char *str = "Unknown";

View File

@@ -779,9 +779,9 @@ static int nvgpu_engine_init_one_dev(struct nvgpu_fifo *f,
int nvgpu_engine_init_info(struct nvgpu_fifo *f)
{
u32 i;
int err;
struct gk20a *g = f->g;
const struct nvgpu_device *dev;
f->num_engines = 0;
@@ -789,15 +789,7 @@ int nvgpu_engine_init_info(struct nvgpu_fifo *f)
nvgpu_log(g, gpu_dbg_device, " GFX devices: %u",
nvgpu_device_count(g, NVGPU_DEVTYPE_GRAPHICS));
for (i = 0U; i < nvgpu_device_count(g, NVGPU_DEVTYPE_GRAPHICS); i++) {
const struct nvgpu_device *dev =
nvgpu_device_get(g, NVGPU_DEVTYPE_GRAPHICS, i);
if (dev == NULL) {
nvgpu_err(g, "Failed to get graphics engine %d", i);
return -EINVAL;
}
nvgpu_device_for_each(g, dev, NVGPU_DEVTYPE_GRAPHICS) {
err = nvgpu_engine_init_one_dev(f, dev);
if (err != 0) {
return err;

View File

@@ -174,6 +174,36 @@ struct nvgpu_device_list {
u32 dev_counts[NVGPU_MAX_DEVTYPE];
};
/*
* Internal function: this shouldn't be necessary to directly call.
* But in any event it converts a list_node pointer to the parent
* nvgpu_device pointer.
*/
static inline struct nvgpu_device *
nvgpu_device_from_dev_list_node(struct nvgpu_list_node *node)
{
return (struct nvgpu_device *)
((uintptr_t)node - offsetof(struct nvgpu_device,
dev_list_node));
};
/**
* @brief Iterate over each device of the specified type.
*
* @param g [in] The GPU.
* @param dev [in] Device pointer to visit each device with.
* @param dev_type [in] Device type to iterate over.
*
* Visit each device of the specified type; _do_not_ modify this device
* list. It is immutable. Although it's' not type checked the dev pointer
* should be a const struct device *.
*/
#define nvgpu_device_for_each(g, dev, dev_type) \
nvgpu_list_for_each_entry(dev, \
&g->devs->devlist_heads[dev_type], \
nvgpu_device, \
dev_list_node)
/**
* @brief Initialize the SW device list from the HW device list.
*