Signed-off-by: Tom St Denis <tom.stdenis at amd.com> --- src/app/main.c | 2 +- src/lib/discover.c | 2 +- src/lib/discover_by_did.c | 37 ++++++++++++++++++++++++++++++++++++- src/lib/discover_by_name.c | 8 ++++---- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/app/main.c b/src/app/main.c index 2ecd5a39c903..e5ca9acabdd6 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -169,8 +169,8 @@ int main(int argc, char **argv) if (sscanf(argv[i+1], "0x%lx", &options.forcedid) == 0) { strncpy(options.dev_name, argv[i+1], sizeof(options.dev_name) - 1); options.forcedid = 0; - options.instance = -1; } + options.instance = -1; ++i; } else { printf("--force requires a number/name\n"); diff --git a/src/lib/discover.c b/src/lib/discover.c index 6613eec42d53..4af3733c8af8 100644 --- a/src/lib/discover.c +++ b/src/lib/discover.c @@ -138,7 +138,7 @@ struct umr_asic *umr_discover_asic(struct umr_options *options) } // try to scan via debugfs - if (!options->no_kernel) { + if (options->instance >= 0 && !options->no_kernel) { asic = calloc(1, sizeof *asic); if (asic) { asic->instance = options->instance; diff --git a/src/lib/discover_by_did.c b/src/lib/discover_by_did.c index 4e7c3f43faa5..0649da90ba08 100644 --- a/src/lib/discover_by_did.c +++ b/src/lib/discover_by_did.c @@ -230,6 +230,41 @@ static const struct { { 0x687f, &umr_create_vega10 }, }; +static int find_first_did(long did) +{ + char name[128], device[128]; + FILE *f, *f2; + + int x; + + for (x = 0; x < 16; x++) { + snprintf(name, sizeof(name)-1, "/sys/kernel/debug/dri/%d/name", x); + f = fopen(name, "r"); + if (f) { + unsigned tmp_did; + fscanf(f, "%*s %s", name); + fclose(f); + + // strip off dev= for kernels > 4.7 + if (strstr(name, "dev=")) + memmove(name, name+4, strlen(name)-3); + + snprintf(device, sizeof(device)-1, "/sys/bus/pci/devices/%s/device", name); + f2 = fopen(device, "r"); + if (f2) { + fscanf(f, "0x%04x", &tmp_did); + if (tmp_did == did) { + fclose(f2); + return x; + } + } + fclose(f2); + } + } + return -1; +} + + struct umr_asic *umr_discover_asic_by_did(struct umr_options *options, long did) { unsigned x; @@ -242,7 +277,7 @@ struct umr_asic *umr_discover_asic_by_did(struct umr_options *options, long did) if (asic) { asic->did = did; - asic->instance = options->instance; + asic->instance = find_first_did(did); umr_scan_config(asic); // set all file handles to -1 (so a call to close_asic won't close handle 0) diff --git a/src/lib/discover_by_name.c b/src/lib/discover_by_name.c index c0b49ad69475..f183640567b4 100644 --- a/src/lib/discover_by_name.c +++ b/src/lib/discover_by_name.c @@ -68,10 +68,10 @@ struct umr_asic *umr_discover_asic_by_name(struct umr_options *options, char *na if (options->instance == -1) { // try and discover an instance that works struct umr_options tmp_opt; - memset(&tmp_opt, 0, sizeof(tmp_opt)); - tmp_opt.forcedid = -1; - tmp_opt.quiet = 1; - for (x = 0; x < 10; x++) { + for (x = 0; x < 16; x++) { + memset(&tmp_opt, 0, sizeof(tmp_opt)); + tmp_opt.quiet = 1; + tmp_opt.forcedid = -1; tmp_opt.instance = x; tmp = umr_discover_asic(&tmp_opt); if (tmp) { -- 2.14.3