1 /*
2  * Copyright (C) 2019-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #pragma once
9 #include "shared/source/command_stream/linear_stream.h"
10 #include "shared/source/helpers/aligned_memory.h"
11 #include "shared/source/helpers/basic_math.h"
12 #include "shared/source/helpers/constants.h"
13 #include "shared/source/helpers/ptr_math.h"
14 #include "shared/source/memory_manager/graphics_allocation.h"
15 
16 namespace NEO {
17 class GraphicsAllocation;
18 
19 using HeapContainer = std::vector<GraphicsAllocation *>;
20 
21 constexpr size_t defaultHeapSize = 64 * KB;
22 
23 class IndirectHeap : public LinearStream {
24     typedef LinearStream BaseClass;
25 
26   public:
27     enum Type {
28         DYNAMIC_STATE = 0,
29         INDIRECT_OBJECT,
30         SURFACE_STATE,
31         NUM_TYPES
32     };
33 
IndirectHeap(void * graphicsAllocation,size_t bufferSize)34     IndirectHeap(void *graphicsAllocation, size_t bufferSize) : BaseClass(graphicsAllocation, bufferSize){};
IndirectHeap(GraphicsAllocation * graphicsAllocation)35     IndirectHeap(GraphicsAllocation *graphicsAllocation) : BaseClass(graphicsAllocation) {}
IndirectHeap(GraphicsAllocation * graphicsAllocation,bool canBeUtilizedAs4GbHeap)36     IndirectHeap(GraphicsAllocation *graphicsAllocation, bool canBeUtilizedAs4GbHeap)
37         : BaseClass(graphicsAllocation), canBeUtilizedAs4GbHeap(canBeUtilizedAs4GbHeap) {}
38 
39     // Disallow copy'ing
40     IndirectHeap(const IndirectHeap &) = delete;
41     IndirectHeap &operator=(const IndirectHeap &) = delete;
42 
43     void align(size_t alignment);
44     uint64_t getHeapGpuStartOffset() const;
45     uint64_t getHeapGpuBase() const;
46     uint32_t getHeapSizeInPages() const;
47 
48   protected:
49     bool canBeUtilizedAs4GbHeap = false;
50 };
51 
align(size_t alignment)52 inline void IndirectHeap::align(size_t alignment) {
53     auto address = alignUp(ptrOffset(buffer, sizeUsed), alignment);
54     sizeUsed = ptrDiff(address, buffer);
55 }
56 
getHeapSizeInPages()57 inline uint32_t IndirectHeap::getHeapSizeInPages() const {
58     if (this->canBeUtilizedAs4GbHeap) {
59         return MemoryConstants::sizeOf4GBinPageEntities;
60     } else {
61         return (static_cast<uint32_t>(getMaxAvailableSpace()) + MemoryConstants::pageMask) / MemoryConstants::pageSize;
62     }
63 }
64 
getHeapGpuStartOffset()65 inline uint64_t IndirectHeap::getHeapGpuStartOffset() const {
66     if (this->canBeUtilizedAs4GbHeap) {
67         return this->graphicsAllocation->getGpuAddressToPatch();
68     } else {
69         return 0llu;
70     }
71 }
72 
getHeapGpuBase()73 inline uint64_t IndirectHeap::getHeapGpuBase() const {
74     if (this->canBeUtilizedAs4GbHeap) {
75         return this->graphicsAllocation->getGpuBaseAddress();
76     } else {
77         return this->graphicsAllocation->getGpuAddress();
78     }
79 }
80 } // namespace NEO
81