blob: c32b2b6d86e04f83e5cd73d999af296f62e78c61 [file] [log] [blame]
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "hf/ffa.h"
18
19#include <stddef.h>
20
21#include "hf/types.h"
22
23#if defined(__linux__) && defined(__KERNEL__)
24#include <linux/kernel.h>
25#include <linux/string.h>
26
27#else
28#include "hf/std.h"
29#endif
30
31/**
32 * Initialises the given `ffa_memory_region` and copies the constituent
33 * information to it. Returns the length in bytes occupied by the data copied to
34 * `memory_region` (attributes, constituents and memory region header size).
35 */
36static uint32_t ffa_memory_region_init_internal(
37 struct ffa_memory_region *memory_region, ffa_vm_id_t sender,
38 ffa_memory_attributes_t attributes, ffa_memory_region_flags_t flags,
39 ffa_memory_handle_t handle, uint32_t tag, ffa_vm_id_t receiver,
40 ffa_memory_access_permissions_t permissions,
41 const struct ffa_memory_region_constituent constituents[],
42 uint32_t constituent_count)
43{
44 struct ffa_composite_memory_region *composite_memory_region;
45 uint32_t index;
46 uint32_t constituents_length =
47 constituent_count *
48 sizeof(struct ffa_memory_region_constituent);
49
50 memory_region->sender = sender;
51 memory_region->attributes = attributes;
52 memory_region->reserved_0 = 0;
53 memory_region->flags = flags;
54 memory_region->handle = handle;
55 memory_region->tag = tag;
56 memory_region->reserved_1 = 0;
57 memory_region->receiver_count = 1;
58 memory_region->receivers[0].receiver_permissions.receiver = receiver;
59 memory_region->receivers[0].receiver_permissions.permissions =
60 permissions;
61 memory_region->receivers[0].receiver_permissions.flags = 0;
62 /*
63 * Note that `sizeof(struct_ffa_memory_region)` and `sizeof(struct
64 * ffa_memory_access)` must both be multiples of 16 (as verified by the
65 * asserts in `ffa_memory.c`, so it is guaranteed that the offset we
66 * calculate here is aligned to a 64-bit boundary and so 64-bit values
67 * can be copied without alignment faults.
68 */
69 memory_region->receivers[0].composite_memory_region_offset =
70 sizeof(struct ffa_memory_region) +
71 memory_region->receiver_count *
72 sizeof(struct ffa_memory_access);
73 memory_region->receivers[0].reserved_0 = 0;
74
75 composite_memory_region =
76 ffa_memory_region_get_composite(memory_region, 0);
77
78 composite_memory_region->page_count = 0;
79 composite_memory_region->constituent_count = constituent_count;
80 composite_memory_region->reserved_0 = 0;
81
82 for (index = 0; index < constituent_count; index++) {
83 composite_memory_region->constituents[index] =
84 constituents[index];
85 composite_memory_region->page_count +=
86 constituents[index].page_count;
87 }
88
89 /*
90 * TODO: Add assert ensuring that the specified message
91 * length is not greater than FFA_MSG_PAYLOAD_MAX.
92 */
93
94 return memory_region->receivers[0].composite_memory_region_offset +
95 sizeof(struct ffa_composite_memory_region) + constituents_length;
96}
97
98/**
99 * Initialises the given `ffa_memory_region` and copies the constituent
100 * information to it. Returns the length in bytes occupied by the data copied to
101 * `memory_region` (attributes, constituents and memory region header size).
102 */
103uint32_t ffa_memory_region_init(
104 struct ffa_memory_region *memory_region, ffa_vm_id_t sender,
105 ffa_vm_id_t receiver,
106 const struct ffa_memory_region_constituent constituents[],
107 uint32_t constituent_count, uint32_t tag,
108 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
109 enum ffa_instruction_access instruction_access,
110 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
111 enum ffa_memory_shareability shareability)
112{
113 ffa_memory_access_permissions_t permissions = 0;
114 ffa_memory_attributes_t attributes = 0;
115
116 /* Set memory region's permissions. */
117 ffa_set_data_access_attr(&permissions, data_access);
118 ffa_set_instruction_access_attr(&permissions, instruction_access);
119
120 /* Set memory region's page attributes. */
121 ffa_set_memory_type_attr(&attributes, type);
122 ffa_set_memory_cacheability_attr(&attributes, cacheability);
123 ffa_set_memory_shareability_attr(&attributes, shareability);
124
125 return ffa_memory_region_init_internal(
126 memory_region, sender, attributes, flags, 0, tag, receiver,
127 permissions, constituents, constituent_count);
128}
129
130uint32_t ffa_memory_retrieve_request_init(
131 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
132 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
133 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
134 enum ffa_instruction_access instruction_access,
135 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
136 enum ffa_memory_shareability shareability)
137{
138 ffa_memory_access_permissions_t permissions = 0;
139 ffa_memory_attributes_t attributes = 0;
140
141 /* Set memory region's permissions. */
142 ffa_set_data_access_attr(&permissions, data_access);
143 ffa_set_instruction_access_attr(&permissions, instruction_access);
144
145 /* Set memory region's page attributes. */
146 ffa_set_memory_type_attr(&attributes, type);
147 ffa_set_memory_cacheability_attr(&attributes, cacheability);
148 ffa_set_memory_shareability_attr(&attributes, shareability);
149
150 memory_region->sender = sender;
151 memory_region->attributes = attributes;
152 memory_region->reserved_0 = 0;
153 memory_region->flags = flags;
154 memory_region->reserved_1 = 0;
155 memory_region->handle = handle;
156 memory_region->tag = tag;
157 memory_region->receiver_count = 1;
158 memory_region->receivers[0].receiver_permissions.receiver = receiver;
159 memory_region->receivers[0].receiver_permissions.permissions =
160 permissions;
161 memory_region->receivers[0].receiver_permissions.flags = 0;
162 /*
163 * Offset 0 in this case means that the hypervisor should allocate the
164 * address ranges. This is the only configuration supported by Hafnium,
165 * as it enforces 1:1 mappings in the stage 2 page tables.
166 */
167 memory_region->receivers[0].composite_memory_region_offset = 0;
168 memory_region->receivers[0].reserved_0 = 0;
169
170 return sizeof(struct ffa_memory_region) +
171 memory_region->receiver_count * sizeof(struct ffa_memory_access);
172}
173
174uint32_t ffa_memory_lender_retrieve_request_init(
175 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
176 ffa_vm_id_t sender)
177{
178 memory_region->sender = sender;
179 memory_region->attributes = 0;
180 memory_region->reserved_0 = 0;
181 memory_region->flags = 0;
182 memory_region->reserved_1 = 0;
183 memory_region->handle = handle;
184 memory_region->tag = 0;
185 memory_region->receiver_count = 0;
186
187 return sizeof(struct ffa_memory_region);
188}
189
190uint32_t ffa_retrieved_memory_region_init(
191 struct ffa_memory_region *response, size_t response_max_size,
192 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
193 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
194 ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions,
195 const struct ffa_memory_region_constituent constituents[],
196 uint32_t constituent_count)
197{
198 /* TODO: Check against response_max_size first. */
199 return ffa_memory_region_init_internal(
200 response, sender, attributes, flags, handle, 0, receiver,
201 permissions, constituents, constituent_count);
202}