Skip to content

Commit edf066a

Browse files
Return proper clDevice for given media adapter
Signed-off-by: Kamil Diedrich <[email protected]>
1 parent 64d772d commit edf066a

File tree

10 files changed

+328
-41
lines changed

10 files changed

+328
-41
lines changed

opencl/source/sharings/va/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (C) 2017-2020 Intel Corporation
2+
# Copyright (C) 2017-2021 Intel Corporation
33
#
44
# SPDX-License-Identifier: MIT
55
#
@@ -8,6 +8,7 @@ set(RUNTIME_SRCS_SHARINGS_VA
88
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
99
${CMAKE_CURRENT_SOURCE_DIR}/${BRANCH_DIR_SUFFIX}/va_device.cpp
1010
${CMAKE_CURRENT_SOURCE_DIR}/va_device.h
11+
${CMAKE_CURRENT_SOURCE_DIR}/va_device_shared.cpp
1112
${CMAKE_CURRENT_SOURCE_DIR}/${BRANCH_DIR_SUFFIX}/va_extension.cpp
1213
${CMAKE_CURRENT_SOURCE_DIR}/cl_va_api.cpp
1314
${CMAKE_CURRENT_SOURCE_DIR}/cl_va_api.h

opencl/source/sharings/va/cl_va_api.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ clGetDeviceIDsFromVA_APIMediaAdapterINTEL(cl_platform_id platform, cl_va_api_dev
7575
VADevice vaDevice{};
7676
cl_device_id device = vaDevice.getDeviceFromVA(pPlatform, mediaAdapter);
7777
GetInfoHelper::set(devices, device);
78-
GetInfoHelper::set(numDevices, 1u);
78+
if (device == nullptr) {
79+
GetInfoHelper::set(numDevices, 0u);
80+
status = CL_DEVICE_NOT_FOUND;
81+
} else {
82+
GetInfoHelper::set(numDevices, 1u);
83+
}
7984
}
8085
return status;
8186
}

opencl/source/sharings/va/va_device.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -16,7 +16,7 @@ VADevice::VADevice() {
1616
}
1717

1818
ClDevice *VADevice::getDeviceFromVA(Platform *pPlatform, VADisplay vaDisplay) {
19-
return pPlatform->getClDevice(0);
19+
return getRootDeviceFromVaDisplay(pPlatform, vaDisplay);
2020
}
2121

2222
VADevice::~VADevice() {

opencl/source/sharings/va/va_device.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -20,6 +20,7 @@ class VADevice {
2020
VADevice();
2121
virtual ~VADevice();
2222

23+
ClDevice *getRootDeviceFromVaDisplay(Platform *pPlatform, VADisplay vaDisplay);
2324
ClDevice *getDeviceFromVA(Platform *pPlatform, VADisplay vaDisplay);
2425

2526
static std::function<void *(const char *, int)> fdlopen;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "shared/source/device/device.h"
9+
#include "shared/source/os_interface/linux/drm_neo.h"
10+
#include "shared/source/os_interface/linux/os_interface.h"
11+
#include "shared/source/os_interface/linux/sys_calls.h"
12+
13+
#include "opencl/source/cl_device/cl_device.h"
14+
#include "opencl/source/platform/platform.h"
15+
#include "opencl/source/sharings/va/va_device.h"
16+
17+
#include <sys/stat.h>
18+
#include <system_error>
19+
#include <unistd.h>
20+
#include <va/va_backend.h>
21+
22+
namespace NEO {
23+
ClDevice *VADevice::getRootDeviceFromVaDisplay(Platform *pPlatform, VADisplay vaDisplay) {
24+
VADisplayContextP pDisplayContext_test = reinterpret_cast<VADisplayContextP>(vaDisplay);
25+
UNRECOVERABLE_IF(pDisplayContext_test->vadpy_magic != 0x56414430);
26+
VADriverContextP pDriverContext_test = pDisplayContext_test->pDriverContext;
27+
int deviceFd = *(int *)pDriverContext_test->drm_state;
28+
29+
UNRECOVERABLE_IF(deviceFd < 0);
30+
31+
char path[256] = {0};
32+
size_t pathlen = 256;
33+
34+
if (SysCalls::getDevicePath(deviceFd, path, pathlen)) {
35+
return nullptr;
36+
}
37+
38+
if (SysCalls::access(path, F_OK)) {
39+
return nullptr;
40+
}
41+
42+
int readLinkSize = 0;
43+
char devicePath[256] = {0};
44+
readLinkSize = SysCalls::readlink(path, devicePath, pathlen);
45+
46+
if (readLinkSize == -1) {
47+
return nullptr;
48+
}
49+
50+
std::string_view prefixView = "../../devices/pci0000:00/0000:";
51+
std::string_view devicePathView(devicePath, static_cast<size_t>(readLinkSize));
52+
devicePathView.remove_prefix(prefixView.size());
53+
devicePathView = devicePathView.substr(0, 7);
54+
55+
for (size_t i = 0; i < pPlatform->getNumDevices(); ++i) {
56+
auto device = pPlatform->getClDevice(i);
57+
NEO::Device *neoDevice = &device->getDevice();
58+
59+
auto *drm = neoDevice->getRootDeviceEnvironment().osInterface->get()->getDrm();
60+
auto pciPath = drm->getPciPath();
61+
if (devicePathView == pciPath) {
62+
return device;
63+
}
64+
}
65+
return nullptr;
66+
}
67+
} // namespace NEO

opencl/test/unit_test/os_interface/linux/sys_calls_linux_ult.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
66
*/
77

8+
#include "shared/source/helpers/string.h"
89
#include "shared/source/os_interface/linux/sys_calls.h"
910

1011
#include "drm/i915_drm.h"
1112

1213
#include <cstdint>
1314
#include <cstdio>
1415
#include <cstring>
16+
#include <iostream>
17+
#include <stdio.h>
18+
#include <string.h>
1519
#include <sys/ioctl.h>
20+
#include <system_error>
1621

1722
namespace NEO {
1823
namespace SysCalls {
1924
uint32_t closeFuncCalled = 0u;
2025
int closeFuncArgPassed = 0;
2126
constexpr int fakeFileDescriptor = 123;
2227
uint32_t vmId = 0;
28+
bool makeFakeDevicePath = false;
29+
bool allowFakeDevicePath = false;
2330

2431
int close(int fileDescriptor) {
2532
closeFuncCalled++;
@@ -55,5 +62,35 @@ int ioctl(int fileDescriptor, unsigned long int request, void *arg) {
5562
}
5663
return 0;
5764
}
65+
66+
int access(const char *pathName, int mode) {
67+
if (allowFakeDevicePath || strcmp(pathName, "/sys/dev/char/226:128") == 0) {
68+
return 0;
69+
}
70+
return -1;
71+
}
72+
73+
int readlink(const char *path, char *buf, size_t bufsize) {
74+
if (strcmp(path, "/sys/dev/char/226:128") != 0) {
75+
return -1;
76+
}
77+
78+
constexpr size_t sizeofPath = sizeof("../../devices/pci0000:00/0000:00:02.0/drm/renderD128");
79+
80+
strcpy_s(buf, sizeofPath, "../../devices/pci0000:00/0000:00:02.0/drm/renderD128");
81+
return sizeofPath;
82+
}
83+
84+
int getDevicePath(int deviceFd, char *buf, size_t &bufSize) {
85+
if (deviceFd <= 0) {
86+
return -1;
87+
}
88+
constexpr size_t sizeofPath = sizeof("/sys/dev/char/226:128");
89+
90+
makeFakeDevicePath ? strcpy_s(buf, sizeofPath, "/sys/dev/char/xyzwerq") : strcpy_s(buf, sizeofPath, "/sys/dev/char/226:128");
91+
bufSize = sizeofPath;
92+
93+
return 0;
94+
}
5895
} // namespace SysCalls
5996
} // namespace NEO
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
66
*/
7-
8-
#include "opencl/source/cl_device/cl_device.h"
9-
#include "opencl/source/platform/platform.h"
10-
#include "opencl/source/sharings/va/va_device.h"
11-
#include "opencl/test/unit_test/fixtures/platform_fixture.h"
12-
#include "test.h"
13-
14-
using namespace NEO;
15-
16-
using VaDeviceTests = Test<PlatformFixture>;
17-
18-
TEST_F(VaDeviceTests, givenVADeviceWhenGetDeviceFromVAIsCalledThenRootDeviceIsReturned) {
19-
VADisplay vaDisplay = nullptr;
20-
21-
VADevice vaDevice{};
22-
ClDevice *device = vaDevice.getDeviceFromVA(pPlatform, vaDisplay);
23-
24-
EXPECT_EQ(pPlatform->getClDevice(0), device);
25-
}

0 commit comments

Comments
 (0)