|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2019 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "runtime/os_interface/windows/os_interface.h" |
| 9 | +#include "runtime/os_interface/windows/wddm_device_command_stream.h" |
| 10 | + |
| 11 | +#include "unit_tests/fixtures/buffer_fixture.h" |
| 12 | +#include "unit_tests/helpers/hw_info_helper.h" |
| 13 | +#include "unit_tests/helpers/execution_environment_helper.h" |
| 14 | +#include "unit_tests/helpers/hw_parse.h" |
| 15 | +#include "unit_tests/mocks/mock_command_queue.h" |
| 16 | +#include "unit_tests/mocks/mock_device.h" |
| 17 | +#include "unit_tests/os_interface/windows/mock_wddm_memory_manager.h" |
| 18 | +#include "test.h" |
| 19 | + |
| 20 | +using namespace OCLRT; |
| 21 | + |
| 22 | +struct EnqueueBufferWindowsTest : public HardwareParse, |
| 23 | + public ::testing::Test { |
| 24 | + EnqueueBufferWindowsTest(void) |
| 25 | + : buffer(nullptr) { |
| 26 | + } |
| 27 | + |
| 28 | + void SetUp() override { |
| 29 | + executionEnvironment = getExecutionEnvironmentImpl(hwInfo); |
| 30 | + } |
| 31 | + |
| 32 | + void TearDown() override { |
| 33 | + buffer.reset(nullptr); |
| 34 | + } |
| 35 | + |
| 36 | + template <typename FamilyType> |
| 37 | + void initializeFixture() { |
| 38 | + auto wddmCsr = new WddmCommandStreamReceiver<FamilyType>(*hwInfo, *executionEnvironment); |
| 39 | + |
| 40 | + executionEnvironment->commandStreamReceivers.resize(1); |
| 41 | + executionEnvironment->commandStreamReceivers[0][0].reset(wddmCsr); |
| 42 | + |
| 43 | + memoryManager = new MockWddmMemoryManager(executionEnvironment->osInterface->get()->getWddm(), *executionEnvironment); |
| 44 | + executionEnvironment->memoryManager.reset(memoryManager); |
| 45 | + |
| 46 | + device = std::unique_ptr<MockDevice>(Device::create<MockDevice>(hwInfo, executionEnvironment, 0)); |
| 47 | + |
| 48 | + context = std::make_unique<MockContext>(device.get()); |
| 49 | + |
| 50 | + const size_t bufferMisalignment = 1; |
| 51 | + const size_t bufferSize = 16; |
| 52 | + bufferMemory = std::make_unique<uint32_t[]>(alignUp(bufferSize + bufferMisalignment, sizeof(uint32_t))); |
| 53 | + cl_int retVal = 0; |
| 54 | + |
| 55 | + buffer.reset(Buffer::create(context.get(), |
| 56 | + CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, |
| 57 | + bufferSize, |
| 58 | + reinterpret_cast<char *>(bufferMemory.get()) + bufferMisalignment, |
| 59 | + retVal)); |
| 60 | + |
| 61 | + EXPECT_EQ(CL_SUCCESS, retVal); |
| 62 | + } |
| 63 | + |
| 64 | + protected: |
| 65 | + HwInfoHelper hwInfoHelper; |
| 66 | + HardwareInfo *hwInfo = nullptr; |
| 67 | + ExecutionEnvironment *executionEnvironment; |
| 68 | + cl_queue_properties properties = {}; |
| 69 | + std::unique_ptr<uint32_t[]> bufferMemory; |
| 70 | + std::unique_ptr<MockDevice> device; |
| 71 | + std::unique_ptr<MockContext> context; |
| 72 | + std::unique_ptr<Buffer> buffer; |
| 73 | + |
| 74 | + MockWddmMemoryManager *memoryManager = nullptr; |
| 75 | +}; |
| 76 | + |
| 77 | +HWTEST_F(EnqueueBufferWindowsTest, givenMisalignedHostPtrWhenEnqueueReadBufferCalledThenStateBaseAddressAddressIsAlignedAndMatchesKernelDispatchInfoParams) { |
| 78 | + initializeFixture<FamilyType>(); |
| 79 | + auto cmdQ = std::make_unique<MockCommandQueueHw<FamilyType>>(context.get(), device.get(), &properties); |
| 80 | + uint32_t memory[2] = {}; |
| 81 | + char *misalignedPtr = reinterpret_cast<char *>(memory) + 1; |
| 82 | + |
| 83 | + buffer->forceDisallowCPUCopy = true; |
| 84 | + auto retVal = cmdQ->enqueueReadBuffer(buffer.get(), CL_TRUE, 0, 4, misalignedPtr, 0, nullptr, nullptr); |
| 85 | + |
| 86 | + EXPECT_EQ(CL_SUCCESS, retVal); |
| 87 | + ASSERT_NE(0, cmdQ->lastEnqueuedKernels.size()); |
| 88 | + Kernel *kernel = cmdQ->lastEnqueuedKernels[0]; |
| 89 | + |
| 90 | + parseCommands<FamilyType>(*cmdQ); |
| 91 | + |
| 92 | + if (hwInfo->capabilityTable.gpuAddressSpace == MemoryConstants::max48BitAddress) { |
| 93 | + const auto &surfaceStateDst = getSurfaceState<FamilyType>(1); |
| 94 | + |
| 95 | + if (kernel->getKernelInfo().kernelArgInfo[1].kernelArgPatchInfoVector[0].size == sizeof(uint64_t)) { |
| 96 | + auto pKernelArg = (uint64_t *)(kernel->getCrossThreadData() + |
| 97 | + kernel->getKernelInfo().kernelArgInfo[1].kernelArgPatchInfoVector[0].crossthreadOffset); |
| 98 | + EXPECT_EQ(reinterpret_cast<uint64_t>(alignDown(misalignedPtr, 4)), *pKernelArg); |
| 99 | + EXPECT_EQ(*pKernelArg, surfaceStateDst.getSurfaceBaseAddress()); |
| 100 | + |
| 101 | + } else if (kernel->getKernelInfo().kernelArgInfo[1].kernelArgPatchInfoVector[0].size == sizeof(uint32_t)) { |
| 102 | + auto pKernelArg = (uint32_t *)(kernel->getCrossThreadData() + |
| 103 | + kernel->getKernelInfo().kernelArgInfo[1].kernelArgPatchInfoVector[0].crossthreadOffset); |
| 104 | + EXPECT_EQ(reinterpret_cast<uint64_t>(alignDown(misalignedPtr, 4)), static_cast<uint64_t>(*pKernelArg)); |
| 105 | + EXPECT_EQ(static_cast<uint64_t>(*pKernelArg), surfaceStateDst.getSurfaceBaseAddress()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (kernel->getKernelInfo().kernelArgInfo[3].kernelArgPatchInfoVector[0].size == sizeof(uint32_t)) { |
| 110 | + auto dstOffset = (uint32_t *)(kernel->getCrossThreadData() + |
| 111 | + kernel->getKernelInfo().kernelArgInfo[3].kernelArgPatchInfoVector[0].crossthreadOffset); |
| 112 | + EXPECT_EQ(ptrDiff(misalignedPtr, alignDown(misalignedPtr, 4)), *dstOffset); |
| 113 | + } else { |
| 114 | + EXPECT_TRUE(false); |
| 115 | + } |
| 116 | +} |
0 commit comments