/*
 * Copyright (C) 2017-2018 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "runtime/memory_manager/page_table.h"
#include "runtime/memory_manager/page_table.inl"

namespace OCLRT {

uintptr_t PTE::map(uintptr_t vm, size_t size, uint64_t entryBits, uint32_t memoryBank) {
    const size_t shift = 12;
    const uint32_t mask = (1 << bits) - 1;
    size_t indexStart = (vm >> shift) & mask;
    size_t indexEnd = ((vm + size - 1) >> shift) & mask;
    uintptr_t res = -1;
    entryBits &= 0xfff;
    entryBits |= 0x1;

    for (size_t index = indexStart; index <= indexEnd; index++) {
        if (entries[index] == 0x0) {
            uint64_t tmp = allocator->reservePage(memoryBank);
            entries[index] = reinterpret_cast<void *>(tmp | entryBits);
        } else {
            entries[index] = reinterpret_cast<void *>((reinterpret_cast<uintptr_t>(entries[index]) & 0xfffff000u) | entryBits);
        }
        res = std::min(reinterpret_cast<uintptr_t>(entries[index]) & 0xfffff000u, res);
    }
    return (res & ~entryBits) + (vm & (pageSize - 1));
}

void PTE::pageWalk(uintptr_t vm, size_t size, size_t offset, uint64_t entryBits, PageWalker &pageWalker, uint32_t memoryBank) {
    static const uint32_t bits = 9;
    const size_t shift = 12;
    const uint32_t mask = (1 << bits) - 1;
    size_t indexStart = (vm >> shift) & mask;
    size_t indexEnd = ((vm + size - 1) >> shift) & mask;
    uint64_t res = -1;
    uintptr_t rem = vm & (pageSize - 1);
    entryBits &= 0xfff;
    entryBits |= 0x1;

    for (size_t index = indexStart; index <= indexEnd; index++) {
        if (entries[index] == 0x0) {
            uint64_t tmp = allocator->reservePage(memoryBank);
            entries[index] = reinterpret_cast<void *>(tmp | entryBits);
        } else {
            entries[index] = reinterpret_cast<void *>((reinterpret_cast<uintptr_t>(entries[index]) & 0xfffff000u) | entryBits);
        }
        res = reinterpret_cast<uintptr_t>(entries[index]) & 0xfffff000u;

        size_t lSize = std::min(pageSize - rem, size);
        pageWalker((res & ~0x1) + rem, lSize, offset, reinterpret_cast<uintptr_t>(entries[index]) & 0xfffu);

        size -= lSize;
        offset += lSize;
        rem = 0;
    }
}

template class PageTable<class PDP, 3, 9>;
template class PageTable<class PDE, 2, 2>;
} // namespace OCLRT
