2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2018-01-08 22:58:02 +08:00
|
|
|
* Copyright (c) 2018, Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "runtime/memory_manager/memory_manager.h"
|
|
|
|
#include "runtime/device/device.h"
|
2018-07-03 16:00:12 +08:00
|
|
|
#include "runtime/execution_environment/execution_environment.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/helpers/hw_info.h"
|
|
|
|
#include "runtime/memory_manager/os_agnostic_memory_manager.h"
|
|
|
|
#include "unit_tests/libult/ult_command_stream_receiver.h"
|
2018-02-09 05:52:58 +08:00
|
|
|
#include "unit_tests/mocks/mock_memory_manager.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
namespace OCLRT {
|
|
|
|
class OSTime;
|
|
|
|
class MemoryManager;
|
|
|
|
class MockMemoryManager;
|
|
|
|
|
|
|
|
class MockDevice : public Device {
|
|
|
|
public:
|
2018-01-08 22:58:02 +08:00
|
|
|
using Device::commandStreamReceiver;
|
2018-04-23 20:26:03 +08:00
|
|
|
using Device::createDeviceImpl;
|
2018-01-26 23:53:18 +08:00
|
|
|
using Device::initializeCaps;
|
2018-04-23 20:26:03 +08:00
|
|
|
using Device::sourceLevelDebugger;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void setOSTime(OSTime *osTime);
|
|
|
|
void setDriverInfo(DriverInfo *driverInfo);
|
|
|
|
bool hasDriverInfo();
|
|
|
|
|
|
|
|
bool getCpuTime(uint64_t *timeStamp) { return true; };
|
|
|
|
void *peekSlmWindowStartAddress() const {
|
|
|
|
return this->slmWindowStartAddress;
|
|
|
|
}
|
2018-06-29 15:12:40 +08:00
|
|
|
MockDevice(const HardwareInfo &hwInfo);
|
2018-07-10 23:14:20 +08:00
|
|
|
MockDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
DeviceInfo *getDeviceInfoToModify() {
|
|
|
|
return &this->deviceInfo;
|
|
|
|
}
|
|
|
|
|
2018-04-10 19:49:26 +08:00
|
|
|
void initializeCaps() override {
|
2017-12-21 07:45:38 +08:00
|
|
|
Device::initializeCaps();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPreemptionMode(PreemptionMode mode) {
|
|
|
|
preemptionMode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WhitelistedRegisters &getWhitelistedRegisters() override {
|
|
|
|
if (forceWhitelistedRegs) {
|
|
|
|
return mockWhitelistedRegs;
|
|
|
|
}
|
|
|
|
return Device::getWhitelistedRegisters();
|
|
|
|
}
|
|
|
|
|
|
|
|
const WorkaroundTable *getWaTable() const override { return &mockWaTable; }
|
|
|
|
|
|
|
|
void setForceWhitelistedRegs(bool force, WhitelistedRegisters *mockRegs = nullptr) {
|
|
|
|
forceWhitelistedRegs = force;
|
|
|
|
if (mockRegs) {
|
|
|
|
mockWhitelistedRegs = *mockRegs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void injectMemoryManager(MockMemoryManager *);
|
|
|
|
|
|
|
|
void setPerfCounters(PerformanceCounters *perfCounters) {
|
|
|
|
performanceCounters = std::unique_ptr<PerformanceCounters>(perfCounters);
|
|
|
|
}
|
|
|
|
void setMemoryManager(MemoryManager *memoryManager);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
UltCommandStreamReceiver<T> &getUltCommandStreamReceiver() {
|
|
|
|
return reinterpret_cast<UltCommandStreamReceiver<T> &>(getCommandStreamReceiver());
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetCommandStreamReceiver(CommandStreamReceiver *newCsr);
|
|
|
|
|
|
|
|
GraphicsAllocation *getTagAllocation() { return tagAllocation; }
|
|
|
|
|
2018-04-10 19:49:26 +08:00
|
|
|
void setSourceLevelDebuggerActive(bool active) {
|
|
|
|
this->deviceInfo.sourceLevelDebuggerActive = active;
|
|
|
|
}
|
2018-04-23 22:08:57 +08:00
|
|
|
template <typename T>
|
|
|
|
static T *createWithMemoryManager(const HardwareInfo *pHwInfo,
|
|
|
|
MemoryManager *memManager) {
|
|
|
|
pHwInfo = getDeviceInitHwInfo(pHwInfo);
|
2018-07-10 23:14:20 +08:00
|
|
|
T *device = new T(*pHwInfo, new ExecutionEnvironment);
|
2018-04-23 22:08:57 +08:00
|
|
|
if (memManager) {
|
|
|
|
device->setMemoryManager(memManager);
|
|
|
|
}
|
2018-06-29 15:12:40 +08:00
|
|
|
if (false == createDeviceImpl(pHwInfo, *device)) {
|
2018-04-23 22:08:57 +08:00
|
|
|
delete device;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return device;
|
|
|
|
}
|
2018-07-03 16:00:12 +08:00
|
|
|
template <typename T>
|
|
|
|
static T *createWithNewExecutionEnvironment(const HardwareInfo *pHwInfo) {
|
|
|
|
return Device::create<T>(pHwInfo, new ExecutionEnvironment);
|
|
|
|
}
|
2018-04-10 19:49:26 +08:00
|
|
|
|
2018-05-11 19:30:07 +08:00
|
|
|
void allocatePreemptionAllocationIfNotPresent() {
|
|
|
|
if (this->preemptionAllocation == nullptr) {
|
|
|
|
if (preemptionMode == PreemptionMode::MidThread || isSourceLevelDebuggerActive()) {
|
|
|
|
size_t requiredSize = hwInfo.capabilityTable.requiredPreemptionSurfaceSize;
|
|
|
|
size_t alignment = 256 * MemoryConstants::kiloByte;
|
|
|
|
bool uncacheable = getWaTable()->waCSRUncachable;
|
|
|
|
this->preemptionAllocation = memoryManager->allocateGraphicsMemory(requiredSize, alignment, false, uncacheable);
|
|
|
|
|
|
|
|
commandStreamReceiver->setPreemptionCsrAllocation(preemptionAllocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
private:
|
|
|
|
bool forceWhitelistedRegs = false;
|
|
|
|
WhitelistedRegisters mockWhitelistedRegs = {0};
|
|
|
|
WorkaroundTable mockWaTable = {};
|
|
|
|
};
|
|
|
|
|
2018-02-09 05:52:58 +08:00
|
|
|
class FailMemoryManager : public MockMemoryManager {
|
2017-12-21 07:45:38 +08:00
|
|
|
public:
|
|
|
|
FailMemoryManager();
|
|
|
|
FailMemoryManager(int32_t fail);
|
|
|
|
virtual ~FailMemoryManager() override {
|
|
|
|
if (agnostic) {
|
|
|
|
for (auto alloc : allocations) {
|
|
|
|
agnostic->freeGraphicsMemory(alloc);
|
|
|
|
}
|
|
|
|
delete agnostic;
|
|
|
|
}
|
|
|
|
};
|
2018-01-05 00:48:46 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) override {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (fail <= 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
fail--;
|
2018-01-05 00:48:46 +08:00
|
|
|
GraphicsAllocation *alloc = agnostic->allocateGraphicsMemory(size, alignment, forcePin, uncacheable);
|
2017-12-21 07:45:38 +08:00
|
|
|
allocations.push_back(alloc);
|
|
|
|
return alloc;
|
|
|
|
};
|
|
|
|
GraphicsAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin) override {
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr) override {
|
|
|
|
return nullptr;
|
|
|
|
};
|
2018-07-09 20:12:32 +08:00
|
|
|
GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override {
|
2017-12-21 07:45:38 +08:00
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness, bool reuseBO) override {
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle) override {
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override{};
|
|
|
|
void *lockResource(GraphicsAllocation *gfxAllocation) override { return nullptr; };
|
|
|
|
void unlockResource(GraphicsAllocation *gfxAllocation) override{};
|
|
|
|
|
2018-02-28 19:09:48 +08:00
|
|
|
MemoryManager::AllocationStatus populateOsHandles(OsHandleStorage &handleStorage) override {
|
|
|
|
return AllocationStatus::Error;
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
|
|
|
void cleanOsHandles(OsHandleStorage &handleStorage) override{};
|
|
|
|
|
|
|
|
uint64_t getSystemSharedMemory() override {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t getMaxApplicationAddress() override {
|
|
|
|
return MemoryConstants::max32BitAppAddress;
|
|
|
|
};
|
|
|
|
|
|
|
|
GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) override {
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, Gmm *gmm) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
int32_t fail;
|
|
|
|
OsAgnosticMemoryManager *agnostic;
|
|
|
|
std::vector<GraphicsAllocation *> allocations;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FailDevice : public Device {
|
|
|
|
public:
|
2018-07-10 23:14:20 +08:00
|
|
|
FailDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment)
|
|
|
|
: Device(hwInfo, executionEnvironment) {
|
2017-12-21 07:45:38 +08:00
|
|
|
memoryManager = new FailMemoryManager;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FailDeviceAfterOne : public Device {
|
|
|
|
public:
|
2018-07-10 23:14:20 +08:00
|
|
|
FailDeviceAfterOne(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment)
|
|
|
|
: Device(hwInfo, executionEnvironment) {
|
2017-12-21 07:45:38 +08:00
|
|
|
memoryManager = new FailMemoryManager(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-26 23:53:18 +08:00
|
|
|
class MockAlignedMallocManagerDevice : public MockDevice {
|
|
|
|
public:
|
2018-07-10 23:14:20 +08:00
|
|
|
MockAlignedMallocManagerDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment);
|
2018-01-26 23:53:18 +08:00
|
|
|
};
|
|
|
|
|
2018-05-22 19:22:56 +08:00
|
|
|
template <typename T = SourceLevelDebugger>
|
|
|
|
class MockDeviceWithSourceLevelDebugger : public MockDevice {
|
|
|
|
public:
|
2018-07-10 23:14:20 +08:00
|
|
|
MockDeviceWithSourceLevelDebugger(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment) : MockDevice(hwInfo, executionEnvironment) {
|
2018-05-22 19:22:56 +08:00
|
|
|
T *sourceLevelDebuggerCreated = new T(nullptr);
|
|
|
|
sourceLevelDebugger.reset(sourceLevelDebuggerCreated);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
} // namespace OCLRT
|