|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * Copyright (c) 2019 by Contributors |
| 22 | + * \file virtual_memory.cc |
| 23 | + * \brief Thread-safe virtal memory manager |
| 24 | + */ |
| 25 | + |
| 26 | +#include "virtual_memory.h" |
| 27 | + |
| 28 | +#include <dmlc/logging.h> |
| 29 | +#include <vta/driver.h> |
| 30 | +#include <cstdint> |
| 31 | +#include <cstdlib> |
| 32 | +#include <cstring> |
| 33 | +#include <list> |
| 34 | +#include <utility> |
| 35 | +#include <iterator> |
| 36 | +#include <unordered_map> |
| 37 | +#include <map> |
| 38 | +#include <mutex> |
| 39 | + |
| 40 | +namespace vta { |
| 41 | +namespace vmem { |
| 42 | + |
| 43 | +/*! |
| 44 | + * \brief Get virtual address given physical address. |
| 45 | + * \param phy_addr The simulator phyiscal address. |
| 46 | + * \return The true virtual address; |
| 47 | + */ |
| 48 | +void* VirtualMemoryManager::GetAddr(uint64_t phy_addr) { |
| 49 | + CHECK_NE(phy_addr, 0) |
| 50 | + << "trying to get address that is nullptr"; |
| 51 | + std::lock_guard<std::mutex> lock(mutex_); |
| 52 | + uint64_t loc = (phy_addr >> kPageBits) - 1; |
| 53 | + CHECK_LT(loc, ptable_.size()) |
| 54 | + << "phy_addr=" << phy_addr; |
| 55 | + Page* p = ptable_[loc]; |
| 56 | + CHECK(p != nullptr); |
| 57 | + size_t offset = (loc - p->ptable_begin) << kPageBits; |
| 58 | + offset += phy_addr & (kPageSize - 1); |
| 59 | + return reinterpret_cast<char*>(p->data) + offset; |
| 60 | +} |
| 61 | + |
| 62 | +/*! |
| 63 | + * \brief Get physical address |
| 64 | + * \param buf The virtual address. |
| 65 | + * \return The true physical address; |
| 66 | + */ |
| 67 | +vta_phy_addr_t VirtualMemoryManager::GetPhyAddr(void* buf) { |
| 68 | + std::lock_guard<std::mutex> lock(mutex_); |
| 69 | + auto it = pmap_.find(buf); |
| 70 | + CHECK(it != pmap_.end()); |
| 71 | + Page* p = it->second.get(); |
| 72 | + return (p->ptable_begin + 1) << kPageBits; |
| 73 | +} |
| 74 | + |
| 75 | +/*! |
| 76 | + * \brief Allocate memory from manager |
| 77 | + * \param size The size of memory |
| 78 | + * \return The virtual address |
| 79 | + */ |
| 80 | +void* VirtualMemoryManager::Alloc(size_t size) { |
| 81 | + std::lock_guard<std::mutex> lock(mutex_); |
| 82 | + size_t npage = (size + kPageSize - 1) / kPageSize; |
| 83 | + auto it = free_map_.lower_bound(npage); |
| 84 | + if (it != free_map_.end()) { |
| 85 | + Page* p = it->second; |
| 86 | + free_map_.erase(it); |
| 87 | + return p->data; |
| 88 | + } |
| 89 | + size_t start = ptable_.size(); |
| 90 | + std::unique_ptr<Page> p(new Page(start, npage)); |
| 91 | + // insert page entry |
| 92 | + ptable_.resize(start + npage, p.get()); |
| 93 | + void* data = p->data; |
| 94 | + pmap_[data] = std::move(p); |
| 95 | + return data; |
| 96 | +} |
| 97 | + |
| 98 | +/*! |
| 99 | + * \brief Free the memory. |
| 100 | + * \param size The size of memory |
| 101 | + * \return The virtual address |
| 102 | + */ |
| 103 | +void VirtualMemoryManager::Free(void* data) { |
| 104 | + std::lock_guard<std::mutex> lock(mutex_); |
| 105 | + if (pmap_.size() == 0) return; |
| 106 | + auto it = pmap_.find(data); |
| 107 | + CHECK(it != pmap_.end()); |
| 108 | + Page* p = it->second.get(); |
| 109 | + free_map_.insert(std::make_pair(p->num_pages, p)); |
| 110 | +} |
| 111 | + |
| 112 | +/*! |
| 113 | + * \brief Copy from the host memory to device memory (virtual). |
| 114 | + * \param dst The device memory address (virtual) |
| 115 | + * \param src The host memory address |
| 116 | + * \param size The size of memory |
| 117 | + */ |
| 118 | +void VirtualMemoryManager::MemCopyFromHost(void* dst, const void * src, size_t size) { |
| 119 | + void * addr = this->GetAddr(reinterpret_cast<uint64_t>(dst)); |
| 120 | + memcpy(addr, src, size); |
| 121 | +} |
| 122 | + |
| 123 | +/*! |
| 124 | + * \brief Copy from the device memory (virtual) to host memory. |
| 125 | + * \param dst The host memory address |
| 126 | + * \param src The device memory address (virtual) |
| 127 | + * \param size The size of memory |
| 128 | + */ |
| 129 | +void VirtualMemoryManager::MemCopyToHost(void* dst, const void * src, size_t size) { |
| 130 | + void * addr = this->GetAddr(reinterpret_cast<uint64_t>(src)); |
| 131 | + memcpy(dst, addr, size); |
| 132 | +} |
| 133 | + |
| 134 | +VirtualMemoryManager* VirtualMemoryManager::Global() { |
| 135 | + static VirtualMemoryManager inst; |
| 136 | + return &inst; |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace vmem |
| 140 | +} // namespace vta |
0 commit comments