1 // Copyright 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "aemu/base/files/Stream.h"
16 
17 #include "host-common/GoldfishDma.h"
18 #include "host-common/DmaMap.h"
19 
20 #include "host-common/address_space_device.h"
21 #include "host-common/android_pipe_host.h"
22 
android_goldfish_dma_add_buffer(void * pipe,uint64_t guest_paddr,uint64_t sz)23 static void android_goldfish_dma_add_buffer(void* pipe, uint64_t guest_paddr, uint64_t sz) {
24     android::DmaMap::get()->addBuffer(pipe, guest_paddr, sz);
25 }
26 
android_goldfish_dma_remove_buffer(uint64_t guest_paddr)27 static void android_goldfish_dma_remove_buffer(uint64_t guest_paddr) {
28     android::DmaMap::get()->removeBuffer(guest_paddr);
29 }
30 
android_goldfish_dma_get_host_addr(uint64_t guest_paddr)31 static void* android_goldfish_dma_get_host_addr(uint64_t guest_paddr) {
32     void *host_ptr;
33 
34     host_ptr = get_address_space_device_control_ops()->get_host_ptr(guest_paddr);
35     if (host_ptr) {
36         return host_ptr;
37     }
38 
39     return android::DmaMap::get()->getHostAddr(guest_paddr);
40 }
41 
android_goldfish_dma_invalidate_host_mappings()42 static void android_goldfish_dma_invalidate_host_mappings() {
43     android::DmaMap::get()->invalidateHostMappings();
44 }
45 
android_goldfish_dma_unlock(uint64_t guest_paddr)46 static void android_goldfish_dma_unlock(uint64_t guest_paddr) {
47     void* hwpipe = android::DmaMap::get()->getPipeInstance(guest_paddr);
48 
49     if (hwpipe) {
50         /*
51          * DMA regions allocated with AddressSpaceHostMemoryAllocatorContext
52          * don't have hwpipe associated with them.
53          */
54         android_pipe_host_signal_wake(hwpipe, PIPE_WAKE_UNLOCK_DMA);
55     }
56 }
57 
android_goldfish_dma_reset_host_mappings()58 static void android_goldfish_dma_reset_host_mappings() {
59     android::DmaMap::get()->resetHostMappings();
60 }
61 
android_goldfish_dma_save_mappings(android::base::Stream * stream)62 static void android_goldfish_dma_save_mappings(android::base::Stream* stream) {
63     android::DmaMap::get()->save(stream);
64 }
65 
android_goldfish_dma_load_mappings(android::base::Stream * stream)66 static void android_goldfish_dma_load_mappings(android::base::Stream* stream) {
67     android::DmaMap::get()->load(stream);
68 }
69 
70 const GoldfishDmaOps android_goldfish_dma_ops = {
71     .add_buffer = android_goldfish_dma_add_buffer,
72     .remove_buffer = android_goldfish_dma_remove_buffer,
73     .get_host_addr = android_goldfish_dma_get_host_addr,
74     .invalidate_host_mappings = android_goldfish_dma_invalidate_host_mappings,
75     .unlock = android_goldfish_dma_unlock,
76     .reset_host_mappings = android_goldfish_dma_reset_host_mappings,
77     .save_mappings = android_goldfish_dma_save_mappings,
78     .load_mappings = android_goldfish_dma_load_mappings,
79 };
80