Skip to content

Commit dabbabd

Browse files
Refactor createDrmContext function
merge createDrmContext, createDrmContextExt and appendDrmContextFlag Related-To: NEO-6591 Signed-off-by: Mateusz Jablonski <[email protected]>
1 parent 2d901f9 commit dabbabd

File tree

6 files changed

+60
-63
lines changed

6 files changed

+60
-63
lines changed

shared/source/os_interface/linux/drm_neo.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,36 @@ void Drm::setUnrecoverableContext(uint32_t drmContextId) {
270270
uint32_t Drm::createDrmContext(uint32_t drmVmId, bool isDirectSubmissionRequested, bool isCooperativeContextRequested) {
271271
drm_i915_gem_context_create_ext gcc = {};
272272

273-
this->appendDrmContextFlags(gcc, isDirectSubmissionRequested);
273+
if (DebugManager.flags.DirectSubmissionDrmContext.get() != -1) {
274+
isDirectSubmissionRequested = DebugManager.flags.DirectSubmissionDrmContext.get();
275+
}
276+
if (isDirectSubmissionRequested) {
277+
gcc.flags |= ioctlHelper->getDirectSubmissionFlag();
278+
}
274279

275-
auto retVal = this->createDrmContextExt(gcc, drmVmId, isCooperativeContextRequested);
276-
UNRECOVERABLE_IF(retVal != 0);
280+
drm_i915_gem_context_create_ext_setparam extSetparam = {};
277281

282+
if (drmVmId > 0) {
283+
extSetparam.base.name = I915_CONTEXT_CREATE_EXT_SETPARAM;
284+
extSetparam.param.param = I915_CONTEXT_PARAM_VM;
285+
extSetparam.param.value = drmVmId;
286+
gcc.extensions = reinterpret_cast<uint64_t>(&extSetparam);
287+
gcc.flags |= I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS;
288+
}
289+
290+
if (DebugManager.flags.CreateContextWithAccessCounters.get() != -1) {
291+
return ioctlHelper->createContextWithAccessCounters(this, gcc);
292+
}
293+
294+
if (DebugManager.flags.ForceRunAloneContext.get() != -1) {
295+
isCooperativeContextRequested = DebugManager.flags.ForceRunAloneContext.get();
296+
}
297+
if (isCooperativeContextRequested) {
298+
return ioctlHelper->createCooperativeContext(this, gcc);
299+
}
300+
auto ioctlResult = ioctl(DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &gcc);
301+
302+
UNRECOVERABLE_IF(ioctlResult != 0);
278303
return gcc.ctx_id;
279304
}
280305

@@ -835,15 +860,6 @@ bool Drm::querySystemInfo() {
835860
return true;
836861
}
837862

838-
void Drm::appendDrmContextFlags(drm_i915_gem_context_create_ext &gcc, bool isDirectSubmissionRequested) {
839-
if (DebugManager.flags.DirectSubmissionDrmContext.get() != -1) {
840-
isDirectSubmissionRequested = DebugManager.flags.DirectSubmissionDrmContext.get();
841-
}
842-
if (isDirectSubmissionRequested) {
843-
gcc.flags |= ioctlHelper->getDirectSubmissionFlag();
844-
}
845-
}
846-
847863
std::vector<uint8_t> Drm::getMemoryRegions() {
848864
return this->query(ioctlHelper->getMemRegionsIoctlVal(), DrmQueryItemFlags::empty);
849865
}
@@ -1154,30 +1170,6 @@ void Drm::waitForBind(uint32_t vmHandleId) {
11541170
waitUserFence(0u, castToUint64(&this->pagingFence[vmHandleId]), this->fenceVal[vmHandleId], ValueWidth::U64, -1, ioctlHelper->getWaitUserFenceSoftFlag());
11551171
}
11561172

1157-
uint32_t Drm::createDrmContextExt(drm_i915_gem_context_create_ext &gcc, uint32_t drmVmId, bool isCooperativeContextRequested) {
1158-
drm_i915_gem_context_create_ext_setparam extSetparam = {};
1159-
1160-
if (drmVmId > 0) {
1161-
extSetparam.base.name = I915_CONTEXT_CREATE_EXT_SETPARAM;
1162-
extSetparam.param.param = I915_CONTEXT_PARAM_VM;
1163-
extSetparam.param.value = drmVmId;
1164-
gcc.extensions = reinterpret_cast<uint64_t>(&extSetparam);
1165-
gcc.flags |= I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS;
1166-
}
1167-
1168-
if (DebugManager.flags.CreateContextWithAccessCounters.get() != -1) {
1169-
return ioctlHelper->createContextWithAccessCounters(this, gcc);
1170-
}
1171-
1172-
if (DebugManager.flags.ForceRunAloneContext.get() != -1) {
1173-
isCooperativeContextRequested = DebugManager.flags.ForceRunAloneContext.get();
1174-
}
1175-
if (isCooperativeContextRequested) {
1176-
return ioctlHelper->createCooperativeContext(this, gcc);
1177-
}
1178-
return ioctl(DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &gcc);
1179-
}
1180-
11811173
bool Drm::isVmBindAvailable() {
11821174
std::call_once(checkBindOnce, [this]() {
11831175
int ret = ioctlHelper->isVmBindAvailable(this);

shared/source/os_interface/linux/drm_neo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class Drm : public DriverModel {
114114
int createDrmVirtualMemory(uint32_t &drmVmId);
115115
void destroyDrmVirtualMemory(uint32_t drmVmId);
116116
MOCKABLE_VIRTUAL uint32_t createDrmContext(uint32_t drmVmId, bool isDirectSubmissionRequested, bool isCooperativeContextRequested);
117-
void appendDrmContextFlags(drm_i915_gem_context_create_ext &gcc, bool isDirectSubmissionRequested);
118117
void destroyDrmContext(uint32_t drmContextId);
119118
int queryVmId(uint32_t drmContextId, uint32_t &vmId);
120119
void setLowPriorityContextParam(uint32_t drmContextId);
@@ -253,7 +252,6 @@ class Drm : public DriverModel {
253252
protected:
254253
Drm(std::unique_ptr<HwDeviceIdDrm> &&hwDeviceIdIn, RootDeviceEnvironment &rootDeviceEnvironment);
255254

256-
uint32_t createDrmContextExt(drm_i915_gem_context_create_ext &gcc, uint32_t drmVmId, bool isCooperativeContextRequested);
257255
int getQueueSliceCount(drm_i915_gem_context_param_sseu *sseu);
258256
bool translateTopologyInfo(const drm_i915_query_topology_info *queryTopologyInfo, QueryTopologyData &data, TopologyMapping &mapping);
259257
std::string generateUUID();

shared/source/os_interface/linux/ioctl_helper_prelim.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ uint32_t gemCreateContextExt(Drm *drm, drm_i915_gem_context_create_ext &gcc, drm
403403
extSetparam.base.next_extension = gcc.extensions;
404404
gcc.extensions = reinterpret_cast<uint64_t>(&extSetparam);
405405

406-
return IoctlHelper::ioctl(drm, DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &gcc);
406+
auto ioctlResult = IoctlHelper::ioctl(drm, DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &gcc);
407+
UNRECOVERABLE_IF(ioctlResult != 0);
408+
return gcc.ctx_id;
407409
}
408410

409411
uint32_t gemCreateContextAcc(Drm *drm, drm_i915_gem_context_create_ext &gcc, uint16_t trigger, uint8_t granularity) {

shared/test/common/libult/linux/drm_mock.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class DrmMock : public Drm {
3030
using Drm::classHandles;
3131
using Drm::completionFenceSupported;
3232
using Drm::contextDebugSupported;
33-
using Drm::createDrmContextExt;
3433
using Drm::engineInfo;
3534
using Drm::fenceVal;
3635
using Drm::generateElfUUID;

shared/test/unit_test/os_interface/linux/drm_with_prelim_tests.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -252,25 +252,30 @@ TEST_F(IoctlHelperPrelimFixture, givenDrmAllocationWhenSetMemAdviseWithDevicePre
252252
EXPECT_EQ(2u, drm->ioctlCallsCount);
253253
}
254254

255-
TEST_F(IoctlHelperPrelimFixture, givenPrelimsWhenAppendDrmContextFlagsThenCorrectFlagsSet) {
255+
TEST_F(IoctlHelperPrelimFixture, givenVariousDirectSubmissionFlagSettingWhenCreateDrmContextIsCalledThenCorrectFlagsArePassedToIoctl) {
256256
DebugManagerStateRestore stateRestore;
257-
DebugManager.flags.DirectSubmissionDrmContext.set(-1);
258-
257+
uint32_t vmId = 0u;
258+
constexpr bool isCooperativeContextRequested = false;
259+
bool isDirectSubmissionRequested{};
259260
uint32_t ioctlVal = (1u << 31);
260261

261-
drm_i915_gem_context_create_ext ctx{};
262-
drm->appendDrmContextFlags(ctx, true);
263-
EXPECT_EQ(ioctlVal, ctx.flags);
262+
DebugManager.flags.DirectSubmissionDrmContext.set(-1);
263+
drm->receivedContextCreateFlags = 0;
264+
isDirectSubmissionRequested = true;
265+
drm->createDrmContext(vmId, isDirectSubmissionRequested, isCooperativeContextRequested);
266+
EXPECT_EQ(ioctlVal, drm->receivedContextCreateFlags);
264267

265-
ctx.flags = 0u;
266268
DebugManager.flags.DirectSubmissionDrmContext.set(0);
267-
268-
drm->appendDrmContextFlags(ctx, true);
269-
EXPECT_EQ(0u, ctx.flags);
269+
drm->receivedContextCreateFlags = 0;
270+
isDirectSubmissionRequested = true;
271+
drm->createDrmContext(vmId, isDirectSubmissionRequested, isCooperativeContextRequested);
272+
EXPECT_EQ(0u, drm->receivedContextCreateFlags);
270273

271274
DebugManager.flags.DirectSubmissionDrmContext.set(1);
272-
drm->appendDrmContextFlags(ctx, false);
273-
EXPECT_EQ(ioctlVal, ctx.flags);
275+
drm->receivedContextCreateFlags = 0;
276+
isDirectSubmissionRequested = false;
277+
drm->createDrmContext(vmId, isDirectSubmissionRequested, isCooperativeContextRequested);
278+
EXPECT_EQ(ioctlVal, drm->receivedContextCreateFlags);
274279
}
275280

276281
TEST_F(IoctlHelperPrelimFixture, givenPrelimsWhenQueryDistancesThenCorrectDistanceSet) {
@@ -376,7 +381,7 @@ TEST_F(IoctlHelperPrelimFixture, givenIoctlFailureWhenCreateContextWithAccessCou
376381

377382
auto ioctlHelper = drm->getIoctlHelper();
378383
drm_i915_gem_context_create_ext gcc{};
379-
EXPECT_EQ(static_cast<uint32_t>(EINVAL), ioctlHelper->createContextWithAccessCounters(drm.get(), gcc));
384+
EXPECT_THROW(ioctlHelper->createContextWithAccessCounters(drm.get(), gcc), std::runtime_error);
380385
EXPECT_EQ(1u, drm->ioctlCallsCount);
381386
}
382387

@@ -394,7 +399,7 @@ TEST_F(IoctlHelperPrelimFixture, givenIoctlFailureWhenCreateCooperativeContexIsC
394399

395400
auto ioctlHelper = drm->getIoctlHelper();
396401
drm_i915_gem_context_create_ext gcc{};
397-
EXPECT_EQ(static_cast<uint32_t>(EINVAL), ioctlHelper->createCooperativeContext(drm.get(), gcc));
402+
EXPECT_THROW(ioctlHelper->createCooperativeContext(drm.get(), gcc), std::runtime_error);
398403
EXPECT_EQ(1u, drm->ioctlCallsCount);
399404
}
400405

@@ -407,21 +412,20 @@ TEST_F(IoctlHelperPrelimFixture, givenIoctlSuccessWhenCreateCooperativeContexIsC
407412
EXPECT_EQ(1u, drm->ioctlCallsCount);
408413
}
409414

410-
TEST_F(IoctlHelperPrelimFixture, whenCreateDrmContextExtIsCalledThenIoctlIsCalledOnlyOnce) {
415+
TEST_F(IoctlHelperPrelimFixture, whenCreateDrmContextIsCalledThenIoctlIsCalledOnlyOnce) {
411416
drm->ioctlRetVal = 0u;
412417

413418
DebugManagerStateRestore stateRestore;
419+
constexpr bool isCooperativeContextRequested = true;
420+
constexpr bool isDirectSubmissionRequested = false;
414421

415422
for (auto &cooperativeContextRequested : {-1, 0, 1}) {
416423
DebugManager.flags.ForceRunAloneContext.set(cooperativeContextRequested);
417424
for (auto &accessCountersRequested : {-1, 0, 1}) {
418425
DebugManager.flags.CreateContextWithAccessCounters.set(accessCountersRequested);
419426
for (auto vmId = 0u; vmId < 3; vmId++) {
420427
drm->ioctlCallsCount = 0u;
421-
422-
drm_i915_gem_context_create_ext gcc{};
423-
424-
drm->createDrmContextExt(gcc, vmId, true);
428+
drm->createDrmContext(vmId, isDirectSubmissionRequested, isCooperativeContextRequested);
425429

426430
EXPECT_EQ(1u, drm->ioctlCallsCount);
427431
}
@@ -451,4 +455,4 @@ TEST_F(IoctlHelperPrelimFixture, givenProgramDebuggingAndContextDebugSupportedWh
451455

452456
EXPECT_NE(static_cast<uint32_t>(-1), drm->passedContextDebugId);
453457
EXPECT_TRUE(drm->capturedCooperativeContextRequest);
454-
}
458+
}

shared/test/unit_test/os_interface/linux/ioctl_helper_tests_upstream.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,11 @@ TEST(IoctlHelperTestsUpstream, givenUpstreamWhenDirectSubmissionEnabledThenNoFla
178178
executionEnvironment->prepareRootDeviceEnvironments(1);
179179
auto drm = std::make_unique<DrmTipMock>(*executionEnvironment->rootDeviceEnvironments[0]);
180180

181-
drm_i915_gem_context_create_ext ctx{};
182-
drm->appendDrmContextFlags(ctx, false);
183-
EXPECT_EQ(0u, ctx.flags);
181+
uint32_t vmId = 0u;
182+
constexpr bool isCooperativeContextRequested = false;
183+
constexpr bool isDirectSubmissionRequested = true;
184+
drm->createDrmContext(vmId, isDirectSubmissionRequested, isCooperativeContextRequested);
185+
EXPECT_EQ(0u, drm->receivedContextCreateFlags);
184186
}
185187

186188
TEST(IoctlHelperTestsUpstream, givenUpstreamWhenGetMemRegionsIoctlValThenCorrectValueReturned) {

0 commit comments

Comments
 (0)