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 #include "address_space.h" 13 14 class GoldfishAddressSpaceBlock; 15 class GoldfishAddressSpaceHostMemoryAllocator; 16 17 using GoldfishAddressSpaceSubdeviceType = AddressSpaceSubdeviceType; 18 19 class GoldfishAddressSpaceBlockProvider { 20 public: 21 GoldfishAddressSpaceBlockProvider(GoldfishAddressSpaceSubdeviceType subdevice); 22 ~GoldfishAddressSpaceBlockProvider(); 23 24 private: 25 GoldfishAddressSpaceBlockProvider(const GoldfishAddressSpaceBlockProvider &rhs); 26 GoldfishAddressSpaceBlockProvider &operator=(const GoldfishAddressSpaceBlockProvider &rhs); 27 28 bool is_opened() const; 29 void close(); 30 address_space_handle_t release(); 31 static void closeHandle(address_space_handle_t handle); 32 address_space_handle_t m_handle; 33 34 friend class GoldfishAddressSpaceBlock; 35 friend class GoldfishAddressSpaceHostMemoryAllocator; 36 }; 37 38 class GoldfishAddressSpaceBlock { 39 public: 40 GoldfishAddressSpaceBlock(); 41 ~GoldfishAddressSpaceBlock(); 42 43 bool allocate(GoldfishAddressSpaceBlockProvider *provider, size_t size); 44 bool claimShared(GoldfishAddressSpaceBlockProvider *provider, uint64_t offset, uint64_t size); 45 uint64_t physAddr() const; 46 uint64_t hostAddr() const; offset()47 uint64_t offset() const { return m_offset; } size()48 size_t size() const { return m_size; } 49 void *mmap(uint64_t opaque); 50 void *guestPtr() const; 51 void replace(GoldfishAddressSpaceBlock *other); 52 void release(); 53 static int memoryMap(void *addr, size_t len, address_space_handle_t fd, uint64_t off, void** dst); 54 static void memoryUnmap(void *ptr, size_t size); 55 56 private: 57 void destroy(); 58 GoldfishAddressSpaceBlock &operator=(const GoldfishAddressSpaceBlock &); 59 60 address_space_handle_t m_handle; 61 void *m_mmaped_ptr; 62 uint64_t m_phys_addr; 63 uint64_t m_host_addr; 64 uint64_t m_offset; 65 uint64_t m_size; 66 bool m_is_shared_mapping; 67 }; 68 69 class GoldfishAddressSpaceHostMemoryAllocator { 70 public: 71 GoldfishAddressSpaceHostMemoryAllocator(bool useSharedSlots); 72 73 long hostMalloc(GoldfishAddressSpaceBlock *block, size_t size); 74 void hostFree(GoldfishAddressSpaceBlock *block); 75 76 bool is_opened() const; release()77 address_space_handle_t release() { return m_provider.release(); } closeHandle(address_space_handle_t handle)78 static void closeHandle(address_space_handle_t handle) { GoldfishAddressSpaceBlockProvider::closeHandle(handle); } 79 80 private: 81 GoldfishAddressSpaceBlockProvider m_provider; 82 bool m_useSharedSlots; 83 }; 84 85 // Convenience functions that run address space driver api without wrapping in 86 // a class. Useful for when a client wants to manage the driver handle directly 87 // (e.g., mmaping() more than one region associated with a single handle will 88 // require different lifetime expectations versus GoldfishAddressSpaceBlock). 89 90 address_space_handle_t goldfish_address_space_open(); 91 void goldfish_address_space_close(address_space_handle_t); 92 93 bool goldfish_address_space_allocate( 94 address_space_handle_t, size_t size, uint64_t* phys_addr, uint64_t* offset); 95 bool goldfish_address_space_free( 96 address_space_handle_t, uint64_t offset); 97 98 bool goldfish_address_space_claim_shared( 99 address_space_handle_t, uint64_t offset, uint64_t size); 100 bool goldfish_address_space_unclaim_shared( 101 address_space_handle_t, uint64_t offset); 102 103 // pgoff is the offset into the page to return in the result 104 void* goldfish_address_space_map( 105 address_space_handle_t, uint64_t offset, uint64_t size, uint64_t pgoff = 0); 106 void goldfish_address_space_unmap(void* ptr, uint64_t size); 107 108 bool goldfish_address_space_set_subdevice_type(address_space_handle_t, GoldfishAddressSpaceSubdeviceType type, address_space_handle_t*); 109 bool goldfish_address_space_ping(address_space_handle_t, struct address_space_ping*); 110 111 #endif // #ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_ADDRESS_SPACE_H 112