1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <ddk/debug.h>
8#include <fbl/limits.h>
9#include <fbl/ref_ptr.h>
10#include <fbl/unique_ptr.h>
11#include <lib/fzl/vmo-mapper.h>
12#include <lib/zx/bti.h>
13#include <lib/zx/pmt.h>
14#include <lib/zx/vmo.h>
15#include <zircon/compiler.h>
16
17#include <zircon/types.h>
18
19class PinnedBuffer : public fbl::RefCounted<PinnedBuffer> {
20
21public:
22    static fbl::RefPtr<PinnedBuffer> Create(size_t size, const zx::bti& bti,
23                                            uint32_t cache_policy);
24    zx_status_t LookupPhys(zx_off_t offset, zx_paddr_t* out);
25
26    void* GetBaseAddress() { return vmo_mapper_.start(); }
27    size_t GetSize() { return vmo_mapper_.size(); }
28    zx_status_t UnPin();
29
30private:
31    PinnedBuffer() = default;
32    //Note - not using zx:bti since this bti may be used for multiple
33
34    fzl::VmoMapper vmo_mapper_;
35    zx::vmo vmo_;
36    zx::pmt pmt_;
37
38    fbl::unique_ptr<zx_paddr_t[]> paddrs_;
39};
40