Skip to content

Commit 638d744

Browse files
Add gem mmap offset query with retry
Related-To: NEO-6276 Signed-off-by: Milczarek, Slawomir <[email protected]>
1 parent c04f8e5 commit 638d744

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

opencl/test/unit_test/os_interface/linux/device_command_stream_fixture_dg1.h

Lines changed: 6 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
*
@@ -45,6 +45,8 @@ class DrmMockCustomDg1 : public DrmMockCustom {
4545
__u64 mmapOffsetOffset = 0;
4646
__u64 mmapOffsetFlags = 0;
4747

48+
bool failOnMmapOffset = false;
49+
4850
int ioctlExtra(unsigned long request, void *arg) override {
4951
switch (request) {
5052
case DRM_IOCTL_I915_GEM_CREATE_EXT: {
@@ -61,6 +63,9 @@ class DrmMockCustomDg1 : public DrmMockCustom {
6163
mmapOffsetOffset = mmapOffsetParams->offset;
6264
mmapOffsetFlags = mmapOffsetParams->flags;
6365
ioctlDg1_cnt.gemMmapOffset++;
66+
if (failOnMmapOffset == true) {
67+
return -1;
68+
}
6469
} break;
6570
default: {
6671
std::cout << std::hex << DRM_IOCTL_I915_GEM_WAIT << std::endl;

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,48 @@ class DrmMemoryManagerLocalMemoryWithCustomMockTest : public ::testing::Test {
170170
std::unique_ptr<TestedDrmMemoryManager> memoryManager;
171171
};
172172

173+
extern bool retrieveMmapOffsetForBufferObject(Drm &drm, BufferObject &bo, uint64_t flags, uint64_t &offset);
174+
175+
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectSucceedsThenReturnTrueAndCorrectOffset) {
176+
BufferObject bo(mock, 1, 1024, 0);
177+
mock->offset = 21;
178+
179+
uint64_t offset = 0;
180+
auto ret = retrieveMmapOffsetForBufferObject(*mock, bo, 0, offset);
181+
182+
EXPECT_TRUE(ret);
183+
EXPECT_EQ(21u, offset);
184+
}
185+
186+
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectFailsThenReturnFalse) {
187+
BufferObject bo(mock, 1, 1024, 0);
188+
mock->mmapOffsetRetVal = -1;
189+
190+
uint64_t offset = 0;
191+
auto ret = retrieveMmapOffsetForBufferObject(*mock, bo, 0, offset);
192+
193+
EXPECT_FALSE(ret);
194+
}
195+
196+
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectIsCalledThenApplyCorrectFlags) {
197+
BufferObject bo(mock, 1, 1024, 0);
198+
199+
uint64_t offset = 0;
200+
auto ret = retrieveMmapOffsetForBufferObject(*mock, bo, 0, offset);
201+
202+
EXPECT_TRUE(ret);
203+
EXPECT_EQ(4u, mock->mmapOffsetFlagsReceived);
204+
205+
mock->mmapOffsetRetVal = -1;
206+
207+
for (uint64_t flags : {I915_MMAP_OFFSET_WC, I915_MMAP_OFFSET_WB}) {
208+
ret = retrieveMmapOffsetForBufferObject(*mock, bo, flags, offset);
209+
210+
EXPECT_FALSE(ret);
211+
EXPECT_EQ(flags, mock->mmapOffsetFlagsReceived);
212+
}
213+
}
214+
173215
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmMemoryManagerWhenCreateBufferObjectInMemoryRegionIsCalledThenBufferObjectWithAGivenGpuAddressAndSizeIsCreatedAndAllocatedInASpecifiedMemoryRegion) {
174216
DebugManagerStateRestore restorer;
175217
DebugManager.flags.EnableLocalMemory.set(1);
@@ -1433,16 +1475,16 @@ TEST_F(DrmMemoryManagerTestDg1, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAll
14331475
EXPECT_EQ(static_cast<uint32_t>(drmAllocation->getBO()->peekHandle()), mockDg1->mmapOffsetHandle);
14341476
EXPECT_EQ(0u, mockDg1->mmapOffsetPad);
14351477
EXPECT_EQ(0u, mockDg1->mmapOffsetOffset);
1436-
EXPECT_EQ((uint64_t)I915_MMAP_OFFSET_WC, mockDg1->mmapOffsetFlags);
1478+
EXPECT_EQ(4u, mockDg1->mmapOffsetFlags);
14371479

14381480
memoryManager->unlockResource(allocation);
14391481
EXPECT_EQ(nullptr, drmAllocation->getBO()->peekLockedAddress());
14401482

14411483
memoryManager->freeGraphicsMemory(allocation);
14421484
}
14431485

1444-
TEST_F(DrmMemoryManagerTestDg1, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAllocationInLocalMemoryButFailsOnIoctlMmapOffsetThenReturnNullPtr) {
1445-
mockDg1->ioctlDg1_expected.gemMmapOffset = 1;
1486+
TEST_F(DrmMemoryManagerTestDg1, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAllocationInLocalMemoryButFailsOnMmapThenReturnNullPtr) {
1487+
mockDg1->ioctlDg1_expected.gemMmapOffset = 2;
14461488
this->ioctlResExt = {mockDg1->ioctl_cnt.total, -1};
14471489
mockDg1->ioctl_res_ext = &ioctlResExt;
14481490

@@ -1457,6 +1499,22 @@ TEST_F(DrmMemoryManagerTestDg1, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAll
14571499
mockDg1->ioctl_res_ext = &mockDg1->NONE;
14581500
}
14591501

1502+
TEST_F(DrmMemoryManagerTestDg1, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAllocationInLocalMemoryButFailsOnIoctlMmapFunctionOffsetThenReturnNullPtr) {
1503+
mockDg1->ioctlDg1_expected.gemMmapOffset = 2;
1504+
mockDg1->returnIoctlExtraErrorValue = true;
1505+
mockDg1->failOnMmapOffset = true;
1506+
1507+
BufferObject bo(mockDg1, 1, 0, 0);
1508+
DrmAllocation drmAllocation(0, GraphicsAllocation::AllocationType::UNKNOWN, &bo, nullptr, 0u, 0u, MemoryPool::LocalMemory);
1509+
EXPECT_NE(nullptr, drmAllocation.getBO());
1510+
1511+
auto ptr = memoryManager->lockResource(&drmAllocation);
1512+
EXPECT_EQ(nullptr, ptr);
1513+
1514+
memoryManager->unlockResource(&drmAllocation);
1515+
mockDg1->ioctl_res_ext = &mockDg1->NONE;
1516+
}
1517+
14601518
TEST_F(DrmMemoryManagerTestDg1, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAllocationInLocalMemoryButBufferObjectIsNullThenReturnNullPtr) {
14611519
DrmAllocation drmAllocation(0, GraphicsAllocation::AllocationType::UNKNOWN, nullptr, nullptr, 0u, 0u, MemoryPool::LocalMemory);
14621520

opencl/test/unit_test/os_interface/linux/drm_mock_dg1.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class DrmMockDg1 : public DrmMock {
3131
int gemCreateExtRetVal = 0;
3232

3333
//DRM_IOCTL_I915_GEM_MMAP_OFFSET
34+
__u64 mmapOffsetFlagsReceived = 0;
3435
__u64 offset = 0;
3536
int mmapOffsetRetVal = 0;
3637

@@ -79,6 +80,7 @@ class DrmMockDg1 : public DrmMock {
7980

8081
} else if (request == DRM_IOCTL_I915_GEM_MMAP_OFFSET) {
8182
auto mmap_arg = static_cast<drm_i915_gem_mmap_offset *>(arg);
83+
mmapOffsetFlagsReceived = mmap_arg->flags;
8284
mmap_arg->offset = offset;
8385
return mmapOffsetRetVal;
8486
}

shared/source/os_interface/linux/drm_memory_manager_local_memory_dg1.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
namespace NEO {
2020

2121
bool retrieveMmapOffsetForBufferObject(Drm &drm, BufferObject &bo, uint64_t flags, uint64_t &offset) {
22+
constexpr uint64_t mmapOffsetFixed = 4;
23+
2224
drm_i915_gem_mmap_offset mmapOffset = {};
2325
mmapOffset.handle = bo.peekHandle();
24-
mmapOffset.flags = flags;
26+
mmapOffset.flags = mmapOffsetFixed;
2527

2628
auto ret = drm.ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &mmapOffset);
2729
if (ret != 0) {
28-
int err = drm.getErrno();
29-
PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET) failed with %d. errno=%d(%s)\n", ret, err, strerror(err));
30-
DEBUG_BREAK_IF(ret != 0);
31-
return false;
30+
mmapOffset.flags = flags;
31+
ret = drm.ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &mmapOffset);
32+
if (ret != 0) {
33+
int err = drm.getErrno();
34+
PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET) failed with %d. errno=%d(%s)\n", ret, err, strerror(err));
35+
DEBUG_BREAK_IF(ret != 0);
36+
return false;
37+
}
3238
}
3339

3440
offset = mmapOffset.offset;

0 commit comments

Comments
 (0)