blob: 7ebb4517b5d65a66ceab03a3347b13e4750129d7 [file] [log] [blame]
Roman Kiryanovdaecd142018-11-14 14:56:27 -08001/*
2 * Copyright (C) 2018 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H
16#define ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H
17
18#include <inttypes.h>
David Reveman74e99bb2019-02-15 18:47:25 -050019#include <stddef.h>
Roman Kiryanovdaecd142018-11-14 14:56:27 -080020
21class GoldfishAddressSpaceBlock;
22
Lingfeng Yangc08fbfb2018-12-05 21:05:17 -080023#ifdef HOST_BUILD
Lingfeng Yanga963ea02019-03-21 21:27:04 -070024class GoldfishAddressSpaceBlockProvider {
25public:
26 GoldfishAddressSpaceBlockProvider();
27 ~GoldfishAddressSpaceBlockProvider();
28
29 uint64_t allocPhys(size_t size);
30 void freePhys(uint64_t phys);
31
32private:
33 void* mAlloc;
34
35 friend class GoldfishAddressSpaceBlock;
36};
Roman Kiryanovdaecd142018-11-14 14:56:27 -080037#else
38class GoldfishAddressSpaceBlockProvider {
39public:
40 GoldfishAddressSpaceBlockProvider();
41 ~GoldfishAddressSpaceBlockProvider();
42
43private:
44 GoldfishAddressSpaceBlockProvider(const GoldfishAddressSpaceBlockProvider &rhs);
45 GoldfishAddressSpaceBlockProvider &operator=(const GoldfishAddressSpaceBlockProvider &rhs);
46
47 bool is_opened();
David Reveman397f5682019-04-08 11:30:05 -040048#ifdef __Fuchsia__
49 uint32_t m_channel;
50#else
Roman Kiryanovdaecd142018-11-14 14:56:27 -080051 int m_fd;
David Reveman397f5682019-04-08 11:30:05 -040052#endif
Roman Kiryanovdaecd142018-11-14 14:56:27 -080053
54 friend class GoldfishAddressSpaceBlock;
55};
56#endif
57
58class GoldfishAddressSpaceBlock {
59public:
60 GoldfishAddressSpaceBlock();
61 ~GoldfishAddressSpaceBlock();
62
63 bool allocate(GoldfishAddressSpaceBlockProvider *provider, size_t size);
64 uint64_t physAddr() const;
65 uint64_t hostAddr() const;
66 void *mmap(uint64_t opaque);
67 void *guestPtr() const;
68 void replace(GoldfishAddressSpaceBlock *x);
69
70private:
71 void destroy();
72 GoldfishAddressSpaceBlock &operator=(const GoldfishAddressSpaceBlock &);
73
Lingfeng Yangc08fbfb2018-12-05 21:05:17 -080074#ifdef HOST_BUILD
Lingfeng Yanga963ea02019-03-21 21:27:04 -070075 bool m_alloced;
Roman Kiryanovdaecd142018-11-14 14:56:27 -080076 void *m_guest_ptr;
Lingfeng Yanga963ea02019-03-21 21:27:04 -070077 uint64_t m_phys_addr;
78 GoldfishAddressSpaceBlockProvider* m_provider;
Roman Kiryanovdaecd142018-11-14 14:56:27 -080079#else
David Reveman5b7c5842019-02-20 01:06:48 -050080#ifdef __Fuchsia__
81 uint32_t m_vmo;
David Reveman397f5682019-04-08 11:30:05 -040082 uint32_t m_channel;
83#else
84 int m_fd;
David Reveman5b7c5842019-02-20 01:06:48 -050085#endif
Roman Kiryanovdaecd142018-11-14 14:56:27 -080086 void *m_mmaped_ptr;
87 uint64_t m_phys_addr;
88 uint64_t m_host_addr;
89 uint64_t m_offset;
90 size_t m_size;
Roman Kiryanovdaecd142018-11-14 14:56:27 -080091#endif
92};
93
94#endif