1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Copyright 2023 Google
3*61046927SAndroid Build Coastguard Worker * SPDX-License-Identifier: MIT
4*61046927SAndroid Build Coastguard Worker */
5*61046927SAndroid Build Coastguard Worker
6*61046927SAndroid Build Coastguard Worker #include "GoldfishAddressSpaceStream.h"
7*61046927SAndroid Build Coastguard Worker
8*61046927SAndroid Build Coastguard Worker #include "goldfish_address_space.h"
9*61046927SAndroid Build Coastguard Worker #include "util/log.h"
10*61046927SAndroid Build Coastguard Worker
createGoldfishAddressSpaceStream(size_t ignored_bufSize)11*61046927SAndroid Build Coastguard Worker AddressSpaceStream* createGoldfishAddressSpaceStream(size_t ignored_bufSize) {
12*61046927SAndroid Build Coastguard Worker // Ignore incoming ignored_bufSize
13*61046927SAndroid Build Coastguard Worker (void)ignored_bufSize;
14*61046927SAndroid Build Coastguard Worker
15*61046927SAndroid Build Coastguard Worker auto handle = goldfish_address_space_open();
16*61046927SAndroid Build Coastguard Worker address_space_handle_t child_device_handle;
17*61046927SAndroid Build Coastguard Worker
18*61046927SAndroid Build Coastguard Worker if (!goldfish_address_space_set_subdevice_type(handle, GoldfishAddressSpaceSubdeviceType::Graphics, &child_device_handle)) {
19*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (initial device create)\n");
20*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(handle);
21*61046927SAndroid Build Coastguard Worker return nullptr;
22*61046927SAndroid Build Coastguard Worker }
23*61046927SAndroid Build Coastguard Worker
24*61046927SAndroid Build Coastguard Worker struct address_space_ping request;
25*61046927SAndroid Build Coastguard Worker request.metadata = ASG_GET_RING;
26*61046927SAndroid Build Coastguard Worker if (!goldfish_address_space_ping(child_device_handle, &request)) {
27*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (get ring)\n");
28*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
29*61046927SAndroid Build Coastguard Worker return nullptr;
30*61046927SAndroid Build Coastguard Worker }
31*61046927SAndroid Build Coastguard Worker
32*61046927SAndroid Build Coastguard Worker uint64_t ringOffset = request.metadata;
33*61046927SAndroid Build Coastguard Worker
34*61046927SAndroid Build Coastguard Worker request.metadata = ASG_GET_BUFFER;
35*61046927SAndroid Build Coastguard Worker if (!goldfish_address_space_ping(child_device_handle, &request)) {
36*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (get buffer)\n");
37*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
38*61046927SAndroid Build Coastguard Worker return nullptr;
39*61046927SAndroid Build Coastguard Worker }
40*61046927SAndroid Build Coastguard Worker
41*61046927SAndroid Build Coastguard Worker uint64_t bufferOffset = request.metadata;
42*61046927SAndroid Build Coastguard Worker uint64_t bufferSize = request.size;
43*61046927SAndroid Build Coastguard Worker
44*61046927SAndroid Build Coastguard Worker if (!goldfish_address_space_claim_shared(
45*61046927SAndroid Build Coastguard Worker child_device_handle, ringOffset, sizeof(asg_ring_storage))) {
46*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (claim ring storage)\n");
47*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
48*61046927SAndroid Build Coastguard Worker return nullptr;
49*61046927SAndroid Build Coastguard Worker }
50*61046927SAndroid Build Coastguard Worker
51*61046927SAndroid Build Coastguard Worker if (!goldfish_address_space_claim_shared(
52*61046927SAndroid Build Coastguard Worker child_device_handle, bufferOffset, bufferSize)) {
53*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (claim buffer storage)\n");
54*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, ringOffset);
55*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
56*61046927SAndroid Build Coastguard Worker return nullptr;
57*61046927SAndroid Build Coastguard Worker }
58*61046927SAndroid Build Coastguard Worker
59*61046927SAndroid Build Coastguard Worker char* ringPtr = (char*)goldfish_address_space_map(
60*61046927SAndroid Build Coastguard Worker child_device_handle, ringOffset, sizeof(struct asg_ring_storage));
61*61046927SAndroid Build Coastguard Worker
62*61046927SAndroid Build Coastguard Worker if (!ringPtr) {
63*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (map ring storage)\n");
64*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, bufferOffset);
65*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, ringOffset);
66*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
67*61046927SAndroid Build Coastguard Worker return nullptr;
68*61046927SAndroid Build Coastguard Worker }
69*61046927SAndroid Build Coastguard Worker
70*61046927SAndroid Build Coastguard Worker char* bufferPtr = (char*)goldfish_address_space_map(
71*61046927SAndroid Build Coastguard Worker child_device_handle, bufferOffset, bufferSize);
72*61046927SAndroid Build Coastguard Worker
73*61046927SAndroid Build Coastguard Worker if (!bufferPtr) {
74*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (map buffer storage)\n");
75*61046927SAndroid Build Coastguard Worker goldfish_address_space_unmap(ringPtr, sizeof(struct asg_ring_storage));
76*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, bufferOffset);
77*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, ringOffset);
78*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
79*61046927SAndroid Build Coastguard Worker return nullptr;
80*61046927SAndroid Build Coastguard Worker }
81*61046927SAndroid Build Coastguard Worker
82*61046927SAndroid Build Coastguard Worker struct asg_context context =
83*61046927SAndroid Build Coastguard Worker asg_context_create(
84*61046927SAndroid Build Coastguard Worker ringPtr, bufferPtr, bufferSize);
85*61046927SAndroid Build Coastguard Worker
86*61046927SAndroid Build Coastguard Worker request.metadata = ASG_SET_VERSION;
87*61046927SAndroid Build Coastguard Worker request.size = 1; // version 1
88*61046927SAndroid Build Coastguard Worker
89*61046927SAndroid Build Coastguard Worker if (!goldfish_address_space_ping(child_device_handle, &request)) {
90*61046927SAndroid Build Coastguard Worker mesa_loge("AddressSpaceStream::create failed (get buffer)\n");
91*61046927SAndroid Build Coastguard Worker goldfish_address_space_unmap(bufferPtr, bufferSize);
92*61046927SAndroid Build Coastguard Worker goldfish_address_space_unmap(ringPtr, sizeof(struct asg_ring_storage));
93*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, bufferOffset);
94*61046927SAndroid Build Coastguard Worker goldfish_address_space_unclaim_shared(child_device_handle, ringOffset);
95*61046927SAndroid Build Coastguard Worker goldfish_address_space_close(child_device_handle);
96*61046927SAndroid Build Coastguard Worker return nullptr;
97*61046927SAndroid Build Coastguard Worker }
98*61046927SAndroid Build Coastguard Worker
99*61046927SAndroid Build Coastguard Worker uint32_t version = request.size;
100*61046927SAndroid Build Coastguard Worker
101*61046927SAndroid Build Coastguard Worker context.ring_config->transfer_mode = 1;
102*61046927SAndroid Build Coastguard Worker context.ring_config->host_consumed_pos = 0;
103*61046927SAndroid Build Coastguard Worker context.ring_config->guest_write_pos = 0;
104*61046927SAndroid Build Coastguard Worker
105*61046927SAndroid Build Coastguard Worker struct address_space_ops ops = {
106*61046927SAndroid Build Coastguard Worker .open = goldfish_address_space_open,
107*61046927SAndroid Build Coastguard Worker .close = goldfish_address_space_close,
108*61046927SAndroid Build Coastguard Worker .claim_shared = goldfish_address_space_claim_shared,
109*61046927SAndroid Build Coastguard Worker .unclaim_shared = goldfish_address_space_unclaim_shared,
110*61046927SAndroid Build Coastguard Worker .map = goldfish_address_space_map,
111*61046927SAndroid Build Coastguard Worker .unmap = goldfish_address_space_unmap,
112*61046927SAndroid Build Coastguard Worker .set_subdevice_type = goldfish_address_space_set_subdevice_type,
113*61046927SAndroid Build Coastguard Worker .ping = goldfish_address_space_ping,
114*61046927SAndroid Build Coastguard Worker };
115*61046927SAndroid Build Coastguard Worker
116*61046927SAndroid Build Coastguard Worker AddressSpaceStream* res = new AddressSpaceStream(child_device_handle, version, context,
117*61046927SAndroid Build Coastguard Worker ringOffset, bufferOffset, ops);
118*61046927SAndroid Build Coastguard Worker
119*61046927SAndroid Build Coastguard Worker return res;
120*61046927SAndroid Build Coastguard Worker }
121