1// Copyright 2017 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/protocol/display-controller.h>
8#include <fbl/unique_ptr.h>
9#include <fbl/vector.h>
10#include <hwreg/mmio.h>
11#include <region-alloc/region-alloc.h>
12#include <lib/zx/bti.h>
13#include <lib/zx/vmo.h>
14
15namespace i915 {
16
17class Controller;
18class Gtt;
19
20class GttRegion {
21public:
22    explicit GttRegion(Gtt* gtt);
23    ~GttRegion();
24
25    void SetRotation(uint32_t rotation, const image_t& image);
26
27    zx_status_t PopulateRegion(zx_handle_t vmo, uint64_t page_offset,
28                               uint64_t length, bool writable = false);
29    void ClearRegion(bool close_vmo);
30
31    uint64_t base() const { return region_->base; }
32    uint64_t size() const { return region_->size; }
33private:
34    fbl::unique_ptr<const RegionAllocator::Region> region_;
35    Gtt* gtt_;
36
37    fbl::Vector<zx::pmt> pmts_;
38    uint32_t mapped_end_ = 0;
39    // The region's current vmo. The region does not own the vmo handle; it
40    // is up to the owner of the region to determine when the vmo should be
41    // closed.
42    zx_handle_t vmo_ = ZX_HANDLE_INVALID;
43
44    bool is_rotated_;
45
46    friend class Gtt;
47};
48
49class Gtt {
50public:
51    Gtt();
52    ~Gtt();
53    zx_status_t Init(Controller* controller);
54    zx_status_t AllocRegion(uint32_t length,
55                            uint32_t align_pow2, fbl::unique_ptr<GttRegion>* region_out);
56    void SetupForMexec(uintptr_t stolen_fb, uint32_t length);
57
58    uint64_t size() const { return gfx_mem_size_; }
59private:
60    Controller* controller_;
61
62    uint64_t gfx_mem_size_;
63    RegionAllocator region_allocator_;
64    zx::vmo scratch_buffer_;
65    zx::bti bti_;
66    zx::pmt scratch_buffer_pmt_;
67    zx_paddr_t scratch_buffer_paddr_ = 0;
68    uint64_t min_contiguity_;
69
70    friend class GttRegion;
71
72    DISALLOW_COPY_ASSIGN_AND_MOVE(Gtt);
73};
74
75} // namespace i915
76