1 /* 2 * Copyright 2018 Google 3 * SPDX-License-Identifier: MIT 4 */ 5 6 #ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H 7 #define ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H 8 9 #include <inttypes.h> 10 #include <stddef.h> 11 12 #ifdef __Fuchsia__ 13 #include <fidl/fuchsia.hardware.goldfish/cpp/wire.h> 14 #endif 15 16 #include "address_space.h" 17 18 class GoldfishAddressSpaceBlock; 19 class GoldfishAddressSpaceHostMemoryAllocator; 20 21 using GoldfishAddressSpaceSubdeviceType = AddressSpaceSubdeviceType; 22 23 class GoldfishAddressSpaceBlockProvider { 24 public: 25 GoldfishAddressSpaceBlockProvider(GoldfishAddressSpaceSubdeviceType subdevice); 26 ~GoldfishAddressSpaceBlockProvider(); 27 28 private: 29 GoldfishAddressSpaceBlockProvider(const GoldfishAddressSpaceBlockProvider &rhs); 30 GoldfishAddressSpaceBlockProvider &operator=(const GoldfishAddressSpaceBlockProvider &rhs); 31 32 bool is_opened() const; 33 void close(); 34 address_space_handle_t release(); 35 static void closeHandle(address_space_handle_t handle); 36 37 #ifdef __Fuchsia__ 38 ::fidl::WireSyncClient<fuchsia_hardware_goldfish::AddressSpaceDevice> 39 m_device; 40 ::fidl::WireSyncClient<fuchsia_hardware_goldfish::AddressSpaceChildDriver> 41 m_child_driver; 42 #else // __Fuchsia__ 43 address_space_handle_t m_handle; 44 #endif // !__Fuchsia__ 45 46 friend class GoldfishAddressSpaceBlock; 47 friend class GoldfishAddressSpaceHostMemoryAllocator; 48 }; 49 50 class GoldfishAddressSpaceBlock { 51 public: 52 GoldfishAddressSpaceBlock(); 53 ~GoldfishAddressSpaceBlock(); 54 55 bool allocate(GoldfishAddressSpaceBlockProvider *provider, size_t size); 56 bool claimShared(GoldfishAddressSpaceBlockProvider *provider, uint64_t offset, uint64_t size); 57 uint64_t physAddr() const; 58 uint64_t hostAddr() const; offset()59 uint64_t offset() const { return m_offset; } size()60 size_t size() const { return m_size; } 61 void *mmap(uint64_t opaque); 62 void *guestPtr() const; 63 void replace(GoldfishAddressSpaceBlock *other); 64 void release(); 65 static int memoryMap(void *addr, size_t len, address_space_handle_t fd, uint64_t off, void** dst); 66 static void memoryUnmap(void *ptr, size_t size); 67 68 private: 69 void destroy(); 70 GoldfishAddressSpaceBlock &operator=(const GoldfishAddressSpaceBlock &); 71 72 #ifdef __Fuchsia__ 73 ::fidl::WireSyncClient<fuchsia_hardware_goldfish::AddressSpaceChildDriver>* 74 m_driver; 75 uint32_t m_vmo; 76 #else // __Fuchsia__ 77 address_space_handle_t m_handle; 78 #endif // !__Fuchsia__ 79 80 void *m_mmaped_ptr; 81 uint64_t m_phys_addr; 82 uint64_t m_host_addr; 83 uint64_t m_offset; 84 uint64_t m_size; 85 bool m_is_shared_mapping; 86 }; 87 88 class GoldfishAddressSpaceHostMemoryAllocator { 89 public: 90 GoldfishAddressSpaceHostMemoryAllocator(bool useSharedSlots); 91 92 long hostMalloc(GoldfishAddressSpaceBlock *block, size_t size); 93 void hostFree(GoldfishAddressSpaceBlock *block); 94 95 bool is_opened() const; release()96 address_space_handle_t release() { return m_provider.release(); } closeHandle(address_space_handle_t handle)97 static void closeHandle(address_space_handle_t handle) { GoldfishAddressSpaceBlockProvider::closeHandle(handle); } 98 99 private: 100 GoldfishAddressSpaceBlockProvider m_provider; 101 bool m_useSharedSlots; 102 }; 103 104 // Convenience functions that run address space driver api without wrapping in 105 // a class. Useful for when a client wants to manage the driver handle directly 106 // (e.g., mmaping() more than one region associated with a single handle will 107 // require different lifetime expectations versus GoldfishAddressSpaceBlock). 108 109 address_space_handle_t goldfish_address_space_open(); 110 void goldfish_address_space_close(address_space_handle_t); 111 112 bool goldfish_address_space_allocate( 113 address_space_handle_t, size_t size, uint64_t* phys_addr, uint64_t* offset); 114 bool goldfish_address_space_free( 115 address_space_handle_t, uint64_t offset); 116 117 bool goldfish_address_space_claim_shared( 118 address_space_handle_t, uint64_t offset, uint64_t size); 119 bool goldfish_address_space_unclaim_shared( 120 address_space_handle_t, uint64_t offset); 121 122 // pgoff is the offset into the page to return in the result 123 void* goldfish_address_space_map( 124 address_space_handle_t, uint64_t offset, uint64_t size, uint64_t pgoff = 0); 125 void goldfish_address_space_unmap(void* ptr, uint64_t size); 126 127 bool goldfish_address_space_set_subdevice_type(address_space_handle_t, GoldfishAddressSpaceSubdeviceType type, address_space_handle_t*); 128 bool goldfish_address_space_ping(address_space_handle_t, struct address_space_ping*); 129 130 #endif // #ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H 131