|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "core/command_container/cmdcontainer.h" |
| 9 | + |
| 10 | +#include "core/command_stream/linear_stream.h" |
| 11 | +#include "core/helpers/debug_helpers.h" |
| 12 | +#include "core/helpers/heap_helper.h" |
| 13 | +#include "runtime/command_stream/command_stream_receiver.h" |
| 14 | +#include "runtime/device/device.h" |
| 15 | +#include "runtime/indirect_heap/indirect_heap.h" |
| 16 | +#include "runtime/memory_manager/memory_manager.h" |
| 17 | + |
| 18 | +#include <cassert> |
| 19 | + |
| 20 | +namespace NEO { |
| 21 | + |
| 22 | +CommandContainer::~CommandContainer() { |
| 23 | + if (!device) { |
| 24 | + DEBUG_BREAK_IF(device); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + auto memoryManager = device->getMemoryManager(); |
| 29 | + |
| 30 | + memoryManager->freeGraphicsMemory(cmdBufferAllocation); |
| 31 | + |
| 32 | + for (auto allocationIndirectHeap : allocationIndirectHeaps) { |
| 33 | + heapHelper->storeHeapAllocation(allocationIndirectHeap); |
| 34 | + } |
| 35 | + |
| 36 | + residencyContainer.clear(); |
| 37 | + deallocationContainer.clear(); |
| 38 | +} |
| 39 | + |
| 40 | +bool CommandContainer::initialize(Device *device) { |
| 41 | + if (!device) { |
| 42 | + DEBUG_BREAK_IF(device); |
| 43 | + return false; |
| 44 | + } |
| 45 | + this->device = device; |
| 46 | + |
| 47 | + heapHelper = std::unique_ptr<HeapHelper>(new HeapHelper(device->getMemoryManager(), device->getDefaultEngine().commandStreamReceiver->getInternalAllocationStorage(), device->getNumAvailableDevices() > 1u)); |
| 48 | + |
| 49 | + size_t alignedSize = alignUp<size_t>(totalCmdBufferSize, MemoryConstants::pageSize64k); |
| 50 | + NEO::AllocationProperties properties{device->getRootDeviceIndex(), true /* allocateMemory*/, alignedSize, |
| 51 | + GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, |
| 52 | + (device->getNumAvailableDevices() > 1u) /* multiOsContextCapable */, |
| 53 | + false, |
| 54 | + NEO::AllocationProperties::noDeviceSpecified}; |
| 55 | + |
| 56 | + cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties); |
| 57 | + |
| 58 | + UNRECOVERABLE_IF(!cmdBufferAllocation); |
| 59 | + |
| 60 | + commandStream = std::unique_ptr<LinearStream>(new LinearStream(cmdBufferAllocation->getUnderlyingBuffer(), |
| 61 | + defaultListCmdBufferSize)); |
| 62 | + commandStream->replaceGraphicsAllocation(cmdBufferAllocation); |
| 63 | + |
| 64 | + addToResidencyContainer(cmdBufferAllocation); |
| 65 | + size_t heapSize = 65536u; |
| 66 | + |
| 67 | + for (auto &allocationIndirectHeap : allocationIndirectHeaps) { |
| 68 | + allocationIndirectHeap = heapHelper->getHeapAllocation(heapSize, alignedSize, device->getRootDeviceIndex()); |
| 69 | + UNRECOVERABLE_IF(!allocationIndirectHeap); |
| 70 | + residencyContainer.push_back(allocationIndirectHeap); |
| 71 | + } |
| 72 | + |
| 73 | + uint32_t index = 0; |
| 74 | + for (auto &indirectHeap : indirectHeaps) { |
| 75 | + auto alloc = allocationIndirectHeaps[index++]; |
| 76 | + indirectHeap = std::make_unique<IndirectHeap>(alloc); |
| 77 | + } |
| 78 | + |
| 79 | + instructionHeapBaseAddress = device->getMemoryManager()->getInternalHeapBaseAddress(0); |
| 80 | + |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +void CommandContainer::addToResidencyContainer(NEO::GraphicsAllocation *alloc) { |
| 85 | + if (alloc == nullptr) { |
| 86 | + DEBUG_BREAK_IF(true); |
| 87 | + return; |
| 88 | + } |
| 89 | + auto end = this->residencyContainer.end(); |
| 90 | + bool isUnique = (end == std::find(this->residencyContainer.begin(), end, alloc)); |
| 91 | + if (isUnique == false) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + this->residencyContainer.push_back(alloc); |
| 96 | +} |
| 97 | + |
| 98 | +void CommandContainer::reset() { |
| 99 | + dirtyHeaps = std::numeric_limits<uint32_t>::max(); |
| 100 | + slmSize = std::numeric_limits<uint32_t>::max(); |
| 101 | + getResidencyContainer().clear(); |
| 102 | + getDeallocationContainer().clear(); |
| 103 | + |
| 104 | + commandStream->replaceBuffer(this->getCommandStream()->getCpuBase(), |
| 105 | + defaultListCmdBufferSize); |
| 106 | + addToResidencyContainer(commandStream->getGraphicsAllocation()); |
| 107 | + |
| 108 | + for (auto &indirectHeap : indirectHeaps) { |
| 109 | + indirectHeap->replaceBuffer(indirectHeap->getCpuBase(), |
| 110 | + indirectHeap->getMaxAvailableSpace()); |
| 111 | + addToResidencyContainer(indirectHeap->getGraphicsAllocation()); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +} // namespace NEO |
0 commit comments