|
21 | 21 | #include "shared/source/helpers/string.h"
|
22 | 22 | #include "shared/source/helpers/surface_format_info.h"
|
23 | 23 | #include "shared/source/memory_manager/host_ptr_manager.h"
|
| 24 | +#include "shared/source/memory_manager/memory_banks.h" |
| 25 | +#include "shared/source/memory_manager/memory_pool.h" |
24 | 26 | #include "shared/source/memory_manager/residency.h"
|
25 | 27 | #include "shared/source/os_interface/linux/allocator_helper.h"
|
26 | 28 | #include "shared/source/os_interface/linux/drm_memory_operations_handler.h"
|
@@ -504,6 +506,12 @@ GraphicsAllocation *DrmMemoryManager::allocateMemoryByKMD(const AllocationData &
|
504 | 506 | allocation->setDefaultGmm(gmm.release());
|
505 | 507 |
|
506 | 508 | allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuRange), bufferSize);
|
| 509 | + |
| 510 | + if (GraphicsAllocation::isCpuAccessRequired(allocationData.type)) { |
| 511 | + auto cpuAddress = lockResource(allocation); |
| 512 | + allocation->setCpuPtrAndGpuAddress(cpuAddress, gpuRange); |
| 513 | + } |
| 514 | + |
507 | 515 | bo.release();
|
508 | 516 | return allocation;
|
509 | 517 | }
|
@@ -1616,4 +1624,160 @@ void *DrmMemoryManager::lockResourceInLocalMemoryImpl(BufferObject *bo) {
|
1616 | 1624 | return bo->peekLockedAddress();
|
1617 | 1625 | }
|
1618 | 1626 |
|
| 1627 | +void createMemoryRegionsForSharedAllocation(const HardwareInfo &hwInfo, MemoryInfo &memoryInfo, const AllocationData &allocationData, MemRegionsVec &memRegions) { |
| 1628 | + auto memoryBanks = allocationData.storageInfo.memoryBanks; |
| 1629 | + |
| 1630 | + if (allocationData.usmInitialPlacement == GraphicsAllocation::UsmInitialPlacement::CPU) { |
| 1631 | + //System memory region |
| 1632 | + auto regionClassAndInstance = memoryInfo.getMemoryRegionClassAndInstance(0u, hwInfo); |
| 1633 | + memRegions.push_back(regionClassAndInstance); |
| 1634 | + } |
| 1635 | + |
| 1636 | + //All local memory regions |
| 1637 | + size_t currentBank = 0; |
| 1638 | + size_t i = 0; |
| 1639 | + |
| 1640 | + while (i < memoryBanks.count()) { |
| 1641 | + if (memoryBanks.test(currentBank)) { |
| 1642 | + auto regionClassAndInstance = memoryInfo.getMemoryRegionClassAndInstance(1u << currentBank, hwInfo); |
| 1643 | + memRegions.push_back(regionClassAndInstance); |
| 1644 | + i++; |
| 1645 | + } |
| 1646 | + currentBank++; |
| 1647 | + } |
| 1648 | + |
| 1649 | + if (allocationData.usmInitialPlacement == GraphicsAllocation::UsmInitialPlacement::GPU) { |
| 1650 | + //System memory region |
| 1651 | + auto regionClassAndInstance = memoryInfo.getMemoryRegionClassAndInstance(0u, hwInfo); |
| 1652 | + memRegions.push_back(regionClassAndInstance); |
| 1653 | + } |
| 1654 | +} |
| 1655 | + |
| 1656 | +GraphicsAllocation *DrmMemoryManager::createSharedUnifiedMemoryAllocation(const AllocationData &allocationData) { |
| 1657 | + auto &drm = this->getDrm(allocationData.rootDeviceIndex); |
| 1658 | + |
| 1659 | + const auto vmAdviseAttribute = drm.getIoctlHelper()->getVmAdviseAtomicAttribute(); |
| 1660 | + if (vmAdviseAttribute == 0) { |
| 1661 | + return nullptr; |
| 1662 | + } |
| 1663 | + |
| 1664 | + auto memoryInfo = drm.getMemoryInfo(); |
| 1665 | + if (!memoryInfo) { |
| 1666 | + return nullptr; |
| 1667 | + } |
| 1668 | + |
| 1669 | + auto size = allocationData.size; |
| 1670 | + auto alignment = allocationData.alignment; |
| 1671 | + |
| 1672 | + auto pHwInfo = drm.getRootDeviceEnvironment().getHardwareInfo(); |
| 1673 | + |
| 1674 | + MemRegionsVec memRegions; |
| 1675 | + createMemoryRegionsForSharedAllocation(*pHwInfo, *memoryInfo, allocationData, memRegions); |
| 1676 | + |
| 1677 | + uint32_t handle = 0; |
| 1678 | + auto ret = memoryInfo->createGemExt(&drm, memRegions, size, handle); |
| 1679 | + |
| 1680 | + if (ret) { |
| 1681 | + return nullptr; |
| 1682 | + } |
| 1683 | + |
| 1684 | + std::unique_ptr<BufferObject, BufferObject::Deleter> bo(new BufferObject(&drm, handle, size, maxOsContextCount)); |
| 1685 | + |
| 1686 | + if (!drm.getIoctlHelper()->setVmBoAdvise(&drm, bo->peekHandle(), vmAdviseAttribute, nullptr)) { |
| 1687 | + return nullptr; |
| 1688 | + } |
| 1689 | + |
| 1690 | + uint64_t offset = 0; |
| 1691 | + if (!retrieveMmapOffsetForBufferObject(allocationData.rootDeviceIndex, *bo, I915_MMAP_OFFSET_WB, offset)) { |
| 1692 | + return nullptr; |
| 1693 | + } |
| 1694 | + |
| 1695 | + auto totalSizeToAlloc = size + alignment; |
| 1696 | + auto cpuPointer = this->mmapFunction(0, totalSizeToAlloc, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 1697 | + |
| 1698 | + auto cpuBasePointer = cpuPointer; |
| 1699 | + cpuPointer = alignUp(cpuPointer, alignment); |
| 1700 | + |
| 1701 | + this->mmapFunction(cpuPointer, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor(), static_cast<off_t>(offset)); |
| 1702 | + |
| 1703 | + bo->setAddress(reinterpret_cast<uintptr_t>(cpuPointer)); |
| 1704 | + |
| 1705 | + auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->peekAddress(), size, MemoryPool::System4KBPages); |
| 1706 | + allocation->setMmapPtr(cpuBasePointer); |
| 1707 | + allocation->setMmapSize(totalSizeToAlloc); |
| 1708 | + if (!allocation->setCacheRegion(&this->getDrm(allocationData.rootDeviceIndex), static_cast<CacheRegion>(allocationData.cacheRegion))) { |
| 1709 | + this->munmapFunction(cpuPointer, totalSizeToAlloc); |
| 1710 | + return nullptr; |
| 1711 | + } |
| 1712 | + |
| 1713 | + bo.release(); |
| 1714 | + |
| 1715 | + return allocation.release(); |
| 1716 | +} |
| 1717 | + |
| 1718 | +DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool hasMappedPtr) { |
| 1719 | + drm_prime_handle openFd = {0, 0, 0}; |
| 1720 | + openFd.fd = handle; |
| 1721 | + |
| 1722 | + auto ret = this->getDrm(properties.rootDeviceIndex).ioctl(DRM_IOCTL_PRIME_FD_TO_HANDLE, &openFd); |
| 1723 | + if (ret != 0) { |
| 1724 | + int err = this->getDrm(properties.rootDeviceIndex).getErrno(); |
| 1725 | + PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(PRIME_FD_TO_HANDLE) failed with %d. errno=%d(%s)\n", ret, err, strerror(err)); |
| 1726 | + DEBUG_BREAK_IF(ret != 0); |
| 1727 | + return nullptr; |
| 1728 | + } |
| 1729 | + |
| 1730 | + if (hasMappedPtr) { |
| 1731 | + auto bo = new BufferObject(&getDrm(properties.rootDeviceIndex), openFd.handle, properties.size, maxOsContextCount); |
| 1732 | + bo->setAddress(properties.gpuAddress); |
| 1733 | + |
| 1734 | + return new DrmAllocation(properties.rootDeviceIndex, properties.allocationType, bo, reinterpret_cast<void *>(bo->peekAddress()), bo->peekSize(), |
| 1735 | + handle, MemoryPool::SystemCpuInaccessible); |
| 1736 | + } |
| 1737 | + |
| 1738 | + auto boHandle = openFd.handle; |
| 1739 | + auto bo = findAndReferenceSharedBufferObject(boHandle, properties.rootDeviceIndex); |
| 1740 | + |
| 1741 | + void *cpuPointer = nullptr; |
| 1742 | + size_t size = lseekFunction(handle, 0, SEEK_END); |
| 1743 | + |
| 1744 | + if (bo == nullptr) { |
| 1745 | + bo = new BufferObject(&getDrm(properties.rootDeviceIndex), boHandle, size, maxOsContextCount); |
| 1746 | + cpuPointer = this->mmapFunction(0, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 1747 | + bo->setAddress(reinterpret_cast<uintptr_t>(cpuPointer)); |
| 1748 | + |
| 1749 | + uint64_t offset = 0; |
| 1750 | + if (!retrieveMmapOffsetForBufferObject(properties.rootDeviceIndex, *bo, I915_MMAP_OFFSET_WB, offset)) { |
| 1751 | + this->munmapFunction(cpuPointer, size); |
| 1752 | + delete bo; |
| 1753 | + return nullptr; |
| 1754 | + } |
| 1755 | + |
| 1756 | + [[maybe_unused]] auto retPtr = this->mmapFunction(cpuPointer, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(properties.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(offset)); |
| 1757 | + DEBUG_BREAK_IF(retPtr != cpuPointer); |
| 1758 | + |
| 1759 | + AllocationData allocationData = {}; |
| 1760 | + allocationData.rootDeviceIndex = properties.rootDeviceIndex; |
| 1761 | + allocationData.size = size; |
| 1762 | + emitPinningRequest(bo, allocationData); |
| 1763 | + |
| 1764 | + bo->setUnmapSize(size); |
| 1765 | + bo->setRootDeviceIndex(properties.rootDeviceIndex); |
| 1766 | + |
| 1767 | + pushSharedBufferObject(bo); |
| 1768 | + |
| 1769 | + DrmAllocation *drmAllocation = nullptr; |
| 1770 | + drmAllocation = new DrmAllocation(properties.rootDeviceIndex, properties.allocationType, bo, cpuPointer, bo->peekAddress(), bo->peekSize(), MemoryPool::System4KBPages); |
| 1771 | + drmAllocation->setMmapPtr(cpuPointer); |
| 1772 | + drmAllocation->setMmapSize(size); |
| 1773 | + drmAllocation->setReservedAddressRange(reinterpret_cast<void *>(cpuPointer), size); |
| 1774 | + drmAllocation->setCacheRegion(&this->getDrm(properties.rootDeviceIndex), static_cast<CacheRegion>(properties.cacheRegion)); |
| 1775 | + |
| 1776 | + return drmAllocation; |
| 1777 | + } |
| 1778 | + |
| 1779 | + return new DrmAllocation(properties.rootDeviceIndex, properties.allocationType, bo, reinterpret_cast<void *>(bo->peekAddress()), bo->peekSize(), |
| 1780 | + handle, MemoryPool::SystemCpuInaccessible); |
| 1781 | +} |
| 1782 | + |
1619 | 1783 | } // namespace NEO
|
0 commit comments