int main(void) { drmDevicePtr *devices; drmDevicePtr device; int fd, ret, max_devices; printf("--- Checking the number of DRM device available ---\n"); max_devices = drmGetDevices2(0, NULL, 0); if (max_devices <= 0) { printf("drmGetDevices2() has not found any devices (errno=%d)\n", -max_devices); return 77; } printf("--- Devices reported %d ---\n", max_devices); devices = calloc(max_devices, sizeof(drmDevicePtr)); if (devices == NULL) { printf("Failed to allocate memory for the drmDevicePtr array\n"); return -1; } printf("--- Retrieving devices information (PCI device revision is ignored) ---\n"); ret = drmGetDevices2(0, devices, max_devices); if (ret < 0) { printf("drmGetDevices2() returned an error %d\n", ret); free(devices); return -1; } for (int i = 0; i < ret; i++) { print_device_info(devices[i], i, false); for (int j = 0; j < DRM_NODE_MAX; j++) { if (devices[i]->available_nodes & 1 << j) { printf("--- Opening device node %s ---\n", devices[i]->nodes[j]); fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC, 0); if (fd < 0) { printf("Failed - %s (%d)\n", strerror(errno), errno); continue; } printf("--- Retrieving device info, for node %s ---\n", devices[i]->nodes[j]); if (drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, &device) == 0) { print_device_info(device, i, true); drmFreeDevice(&device); } close(fd); } } } drmFreeDevices(devices, ret); free(devices); return 0; }
/* Open AMD devices. * Return the number of AMD device openned. */ static int amdgpu_open_devices(int open_render_node) { drmDevicePtr devices[MAX_CARDS_SUPPORTED]; int i; int drm_node; int amd_index = 0; int drm_count; int fd; drmVersionPtr version; drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED); if (drm_count < 0) { fprintf(stderr, "drmGetDevices2() returned an error %d\n", drm_count); return 0; } for (i = 0; i < drm_count; i++) { /* If this is not PCI device, skip*/ if (devices[i]->bustype != DRM_BUS_PCI) continue; /* If this is not AMD GPU vender ID, skip*/ if (devices[i]->deviceinfo.pci->vendor_id != 0x1002) continue; if (open_render_node) drm_node = DRM_NODE_RENDER; else drm_node = DRM_NODE_PRIMARY; fd = -1; if (devices[i]->available_nodes & 1 << drm_node) fd = open( devices[i]->nodes[drm_node], O_RDWR | O_CLOEXEC); /* This node is not available. */ if (fd < 0) continue; version = drmGetVersion(fd); if (!version) { fprintf(stderr, "Warning: Cannot get version for %s." "Error is %s\n", devices[i]->nodes[drm_node], strerror(errno)); close(fd); continue; } if (strcmp(version->name, "amdgpu")) { /* This is not AMDGPU driver, skip.*/ drmFreeVersion(version); close(fd); continue; } drmFreeVersion(version); drm_amdgpu[amd_index] = fd; amd_index++; } drmFreeDevices(devices, drm_count); return amd_index; }