blob: e4bb2e14cc1d96c132908f401834eb5e4019bb6f [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010
Andrew Scull18c78fc2018-08-20 12:57:41 +010011#include "hf/api.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010012#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010013#include "hf/cpu.h"
J-Alves4ef6e842021-03-18 12:47:01 +000014#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010015#include "hf/ffa.h"
Andrew Scull3c257452019-11-26 13:32:50 +000016#include "hf/layout.h"
17#include "hf/plat/iommu.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010018#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010019
Andrew Scull19503262018-09-20 14:48:39 +010020#include "vmapi/hf/call.h"
21
22static struct vm vms[MAX_VMS];
Olivier Deprez96a2a262020-06-11 17:21:38 +020023static struct vm other_world;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010024static ffa_vm_count_t vm_count;
J-Alvesb37fd082020-10-22 12:29:21 +010025static struct vm *first_boot_vm;
Andrew Scull19503262018-09-20 14:48:39 +010026
J-Alvesfe23ebe2021-10-13 16:07:07 +010027/**
28 * Counters on the status of notifications in the system. It helps to improve
29 * the information retrieved by the receiver scheduler.
30 */
31static struct {
32 /** Counts notifications pending. */
33 uint32_t pending_count;
34 /**
35 * Counts notifications pending, that have been retrieved by the
36 * receiver scheduler.
37 */
38 uint32_t info_get_retrieved_count;
39 struct spinlock lock;
40} all_notifications_state;
41
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080042static bool vm_init_mm(struct vm *vm, struct mpool *ppool)
43{
44 if (vm->el0_partition) {
45 return mm_ptable_init(&vm->ptable, vm->id, MM_FLAG_STAGE1,
46 ppool);
47 }
48 return mm_vm_init(&vm->ptable, vm->id, ppool);
49}
50
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010051struct vm *vm_init(ffa_vm_id_t id, ffa_vcpu_count_t vcpu_count,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080052 struct mpool *ppool, bool el0_partition)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010053{
Wedson Almeida Filho87009642018-07-02 10:20:07 +010054 uint32_t i;
Andrew Scull19503262018-09-20 14:48:39 +010055 struct vm *vm;
56
Olivier Deprez96a2a262020-06-11 17:21:38 +020057 if (id == HF_OTHER_WORLD_ID) {
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080058 CHECK(el0_partition == false);
Olivier Deprez96a2a262020-06-11 17:21:38 +020059 vm = &other_world;
Andrew Walbran9daa57e2019-09-27 13:33:20 +010060 } else {
61 uint16_t vm_index = id - HF_VM_ID_OFFSET;
Andrew Scull19503262018-09-20 14:48:39 +010062
Andrew Walbran9daa57e2019-09-27 13:33:20 +010063 CHECK(id >= HF_VM_ID_OFFSET);
64 CHECK(vm_index < ARRAY_SIZE(vms));
65 vm = &vms[vm_index];
66 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010067
Andrew Scull2b5fbad2019-04-05 13:55:56 +010068 memset_s(vm, sizeof(*vm), 0, sizeof(*vm));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000070 list_init(&vm->mailbox.waiter_list);
71 list_init(&vm->mailbox.ready_list);
72 sl_init(&vm->lock);
73
Andrew Walbran9daa57e2019-09-27 13:33:20 +010074 vm->id = id;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010075 vm->vcpu_count = vcpu_count;
Andrew Sculld6ee1102019-04-05 22:12:42 +010076 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scull9726c252019-01-23 13:44:19 +000077 atomic_init(&vm->aborting, false);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -080078 vm->el0_partition = el0_partition;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010079
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -080080 if (!vm_init_mm(vm, ppool)) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +010081 return NULL;
Wedson Almeida Filho03306112018-11-26 00:08:03 +000082 }
83
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000084 /* Initialise waiter entries. */
85 for (i = 0; i < MAX_VMS; i++) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000086 vm->wait_entries[i].waiting_vm = vm;
87 list_init(&vm->wait_entries[i].wait_links);
88 list_init(&vm->wait_entries[i].ready_links);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000089 }
90
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000091 /* Do basic initialization of vCPUs. */
Andrew Scull7364a8e2018-07-19 15:39:29 +010092 for (i = 0; i < vcpu_count; i++) {
Andrew Walbrane1310df2019-04-29 17:28:28 +010093 vcpu_init(vm_get_vcpu(vm, i), vm);
Andrew Scull7364a8e2018-07-19 15:39:29 +010094 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010095
J-Alves4ef6e842021-03-18 12:47:01 +000096 /* Basic initialization of the notifications structure. */
97 vm_notifications_init_bindings(&vm->notifications.from_sp);
98 vm_notifications_init_bindings(&vm->notifications.from_vm);
99
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100100 return vm;
101}
102
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100103bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool,
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800104 struct vm **new_vm, bool el0_partition)
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100105{
106 if (vm_count >= MAX_VMS) {
107 return false;
108 }
109
110 /* Generate IDs based on an offset, as low IDs e.g., 0, are reserved */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800111 *new_vm = vm_init(vm_count + HF_VM_ID_OFFSET, vcpu_count, ppool,
112 el0_partition);
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100113 if (*new_vm == NULL) {
114 return false;
115 }
Andrew Scull19503262018-09-20 14:48:39 +0100116 ++vm_count;
Andrew Scull19503262018-09-20 14:48:39 +0100117
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000118 return true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100119}
120
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100121ffa_vm_count_t vm_get_count(void)
Andrew Scull19503262018-09-20 14:48:39 +0100122{
123 return vm_count;
124}
125
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100126/**
127 * Returns a pointer to the VM with the corresponding id.
128 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100129struct vm *vm_find(ffa_vm_id_t id)
Andrew Scull19503262018-09-20 14:48:39 +0100130{
David Brazdilbc501192019-09-27 13:20:56 +0100131 uint16_t index;
Fuad Tabba494376e2019-08-05 12:35:10 +0100132
Olivier Deprez96a2a262020-06-11 17:21:38 +0200133 if (id == HF_OTHER_WORLD_ID) {
134 if (other_world.id == HF_OTHER_WORLD_ID) {
135 return &other_world;
136 }
Andrew Scull19503262018-09-20 14:48:39 +0100137 return NULL;
138 }
139
Olivier Deprez96a2a262020-06-11 17:21:38 +0200140 /* Check that this is not a reserved ID. */
141 if (id < HF_VM_ID_OFFSET) {
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100142 return NULL;
143 }
144
David Brazdilbc501192019-09-27 13:20:56 +0100145 index = id - HF_VM_ID_OFFSET;
146
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100147 return vm_find_index(index);
148}
149
150/**
J-Alves46ee0682021-07-26 15:17:53 +0100151 * Returns a locked instance of the VM with the corresponding id.
152 */
153struct vm_locked vm_find_locked(ffa_vm_id_t id)
154{
155 struct vm *vm = vm_find(id);
156
157 if (vm != NULL) {
158 return vm_lock(vm);
159 }
160
161 return (struct vm_locked){.vm = NULL};
162}
163
164/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100165 * Returns a pointer to the VM at the specified index.
166 */
167struct vm *vm_find_index(uint16_t index)
168{
David Brazdilbc501192019-09-27 13:20:56 +0100169 /* Ensure the VM is initialized. */
170 if (index >= vm_count) {
171 return NULL;
172 }
173
174 return &vms[index];
Andrew Scull19503262018-09-20 14:48:39 +0100175}
176
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000177/**
Fuad Tabbaed294af2019-12-20 10:43:01 +0000178 * Locks the given VM and updates `locked` to hold the newly locked VM.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000179 */
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100180struct vm_locked vm_lock(struct vm *vm)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000181{
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100182 struct vm_locked locked = {
183 .vm = vm,
184 };
185
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000186 sl_lock(&vm->lock);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100187
188 return locked;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000189}
190
191/**
Jose Marinho75509b42019-04-09 09:34:59 +0100192 * Locks two VMs ensuring that the locking order is according to the locks'
193 * addresses.
194 */
195struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2)
196{
197 struct two_vm_locked dual_lock;
198
199 sl_lock_both(&vm1->lock, &vm2->lock);
200 dual_lock.vm1.vm = vm1;
201 dual_lock.vm2.vm = vm2;
202
203 return dual_lock;
204}
205
206/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000207 * Unlocks a VM previously locked with vm_lock, and updates `locked` to reflect
208 * the fact that the VM is no longer locked.
209 */
210void vm_unlock(struct vm_locked *locked)
211{
212 sl_unlock(&locked->vm->lock);
213 locked->vm = NULL;
214}
Andrew Walbrane1310df2019-04-29 17:28:28 +0100215
216/**
217 * Get the vCPU with the given index from the given VM.
218 * This assumes the index is valid, i.e. less than vm->vcpu_count.
219 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100220struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index)
Andrew Walbrane1310df2019-04-29 17:28:28 +0100221{
Andrew Scull877ae4b2019-07-02 12:52:33 +0100222 CHECK(vcpu_index < vm->vcpu_count);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100223 return &vm->vcpus[vcpu_index];
224}
Andrew Scull3c257452019-11-26 13:32:50 +0000225
226/**
Andrew Walbranaad8f982019-12-04 10:56:39 +0000227 * Gets `vm`'s wait entry for waiting on the `for_vm`.
228 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100229struct wait_entry *vm_get_wait_entry(struct vm *vm, ffa_vm_id_t for_vm)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000230{
231 uint16_t index;
232
233 CHECK(for_vm >= HF_VM_ID_OFFSET);
234 index = for_vm - HF_VM_ID_OFFSET;
235 CHECK(index < MAX_VMS);
236
237 return &vm->wait_entries[index];
238}
239
240/**
241 * Gets the ID of the VM which the given VM's wait entry is for.
242 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100243ffa_vm_id_t vm_id_for_wait_entry(struct vm *vm, struct wait_entry *entry)
Andrew Walbranaad8f982019-12-04 10:56:39 +0000244{
245 uint16_t index = entry - vm->wait_entries;
246
247 return index + HF_VM_ID_OFFSET;
248}
249
250/**
Andrew Walbran45633dd2020-10-07 17:59:54 +0100251 * Return whether the given VM ID represents an entity in the current world:
252 * i.e. the hypervisor or a normal world VM when running in the normal world, or
253 * the SPM or an SP when running in the secure world.
254 */
255bool vm_id_is_current_world(ffa_vm_id_t vm_id)
256{
257 return (vm_id & HF_VM_ID_WORLD_MASK) !=
258 (HF_OTHER_WORLD_ID & HF_VM_ID_WORLD_MASK);
259}
260
261/**
Andrew Scull3c257452019-11-26 13:32:50 +0000262 * Map a range of addresses to the VM in both the MMU and the IOMMU.
263 *
264 * mm_vm_defrag should always be called after a series of page table updates,
265 * whether they succeed or fail. This is because on failure extra page table
266 * entries may have been allocated and then not used, while on success it may be
267 * possible to compact the page table by merging several entries into a block.
268 *
269 * Returns true on success, or false if the update failed and no changes were
270 * made.
271 *
272 */
273bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
274 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
275{
276 if (!vm_identity_prepare(vm_locked, begin, end, mode, ppool)) {
277 return false;
278 }
279
280 vm_identity_commit(vm_locked, begin, end, mode, ppool, ipa);
281
282 return true;
283}
284
285/**
286 * Prepares the given VM for the given address mapping such that it will be able
287 * to commit the change without failure.
288 *
289 * In particular, multiple calls to this function will result in the
290 * corresponding calls to commit the changes to succeed.
291 *
292 * Returns true on success, or false if the update failed and no changes were
293 * made.
294 */
295bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
296 uint32_t mode, struct mpool *ppool)
297{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800298 if (vm_locked.vm->el0_partition) {
299 return mm_identity_prepare(&vm_locked.vm->ptable, begin, end,
300 mode, ppool);
301 }
Andrew Scull3c257452019-11-26 13:32:50 +0000302 return mm_vm_identity_prepare(&vm_locked.vm->ptable, begin, end, mode,
303 ppool);
304}
305
306/**
307 * Commits the given address mapping to the VM assuming the operation cannot
308 * fail. `vm_identity_prepare` must used correctly before this to ensure
309 * this condition.
310 */
311void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
312 uint32_t mode, struct mpool *ppool, ipaddr_t *ipa)
313{
Raghu Krishnamurthyec1b4912021-02-10 19:09:06 -0800314 if (vm_locked.vm->el0_partition) {
315 mm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
316 ppool);
317 if (ipa != NULL) {
318 /*
319 * EL0 partitions are modeled as lightweight VM's, to
320 * promote code reuse. The below statement returns the
321 * mapped PA as an IPA, however, for an EL0 partition,
322 * this is really a VA.
323 */
324 *ipa = ipa_from_pa(begin);
325 }
326 } else {
327 mm_vm_identity_commit(&vm_locked.vm->ptable, begin, end, mode,
328 ppool, ipa);
329 }
Andrew Scull3c257452019-11-26 13:32:50 +0000330 plat_iommu_identity_map(vm_locked, begin, end, mode);
331}
332
333/**
334 * Unmap a range of addresses from the VM.
335 *
336 * Returns true on success, or false if the update failed and no changes were
337 * made.
338 */
339bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end,
340 struct mpool *ppool)
341{
342 uint32_t mode = MM_MODE_UNMAPPED_MASK;
343
344 return vm_identity_map(vm_locked, begin, end, mode, ppool, NULL);
345}
346
347/**
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700348 * Defrag page tables for an EL0 partition or for a VM.
349 */
350void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool)
351{
352 if (vm_locked.vm->el0_partition) {
353 mm_stage1_defrag(&vm_locked.vm->ptable, ppool);
354 } else {
355 mm_vm_defrag(&vm_locked.vm->ptable, ppool);
356 }
357}
358
359/**
Andrew Scull3c257452019-11-26 13:32:50 +0000360 * Unmaps the hypervisor pages from the given page table.
361 */
362bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool)
363{
364 /* TODO: If we add pages dynamically, they must be included here too. */
365 return vm_unmap(vm_locked, layout_text_begin(), layout_text_end(),
366 ppool) &&
367 vm_unmap(vm_locked, layout_rodata_begin(), layout_rodata_end(),
368 ppool) &&
369 vm_unmap(vm_locked, layout_data_begin(), layout_data_end(),
370 ppool);
371}
J-Alvesb37fd082020-10-22 12:29:21 +0100372
373/**
374 * Gets the first partition to boot, according to Boot Protocol from FFA spec.
375 */
376struct vm *vm_get_first_boot(void)
377{
378 return first_boot_vm;
379}
380
381/**
382 * Insert in boot list, sorted by `boot_order` parameter in the vm structure
383 * and rooted in `first_boot_vm`.
384 */
385void vm_update_boot(struct vm *vm)
386{
387 struct vm *current = NULL;
388 struct vm *previous = NULL;
389
390 if (first_boot_vm == NULL) {
391 first_boot_vm = vm;
392 return;
393 }
394
395 current = first_boot_vm;
396
397 while (current != NULL && current->boot_order >= vm->boot_order) {
398 previous = current;
399 current = current->next_boot;
400 }
401
402 if (previous != NULL) {
403 previous->next_boot = vm;
404 } else {
405 first_boot_vm = vm;
406 }
407
408 vm->next_boot = current;
409}
J-Alves4ef6e842021-03-18 12:47:01 +0000410
Raghu Krishnamurthyea195fa2021-02-12 23:29:00 -0800411/**
412 * Gets the mode of the given range of ipa or va if they are mapped with the
413 * same mode.
414 *
415 * Returns true if the range is mapped with the same mode and false otherwise.
416 * The wrapper calls the appropriate mm function depending on if the partition
417 * is a vm or a el0 partition.
418 */
419bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end,
420 uint32_t *mode)
421{
422 if (vm_locked.vm->el0_partition) {
423 return mm_get_mode(&vm_locked.vm->ptable,
424 va_from_pa(pa_from_ipa(begin)),
425 va_from_pa(pa_from_ipa(end)), mode);
426 }
427 return mm_vm_get_mode(&vm_locked.vm->ptable, begin, end, mode);
428}
J-Alvesa0f317d2021-06-09 13:31:59 +0100429
430/*
431 * Initializes the notifications structure.
432 */
433void vm_notifications_init_bindings(struct notifications *notifications)
434{
435 for (uint32_t i = 0U; i < MAX_FFA_NOTIFICATIONS; i++) {
436 notifications->bindings_sender_id[i] = HF_INVALID_VM_ID;
437 }
438}
439
440/**
441 * Checks if there are pending notifications.
442 */
443bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm,
444 ffa_notifications_bitmap_t notifications)
445{
446 struct notifications *to_check;
447
448 CHECK(vm_locked.vm != NULL);
449
450 to_check = from_vm ? &vm_locked.vm->notifications.from_vm
451 : &vm_locked.vm->notifications.from_sp;
452
453 /* Check if there are pending per vcpu notifications */
454 for (uint32_t i = 0U; i < MAX_CPUS; i++) {
455 if ((to_check->per_vcpu[i].pending & notifications) != 0U) {
456 return true;
457 }
458 }
459
460 /* Check if there are global pending notifications */
461 return (to_check->global.pending & notifications) != 0U;
462}
J-Alvesc003a7a2021-03-18 13:06:53 +0000463
464bool vm_are_notifications_enabled(struct vm_locked vm_locked)
465{
466 return vm_locked.vm->notifications.enabled == true;
467}
468
469static bool vm_is_notification_bit_set(ffa_notifications_bitmap_t notifications,
470 uint32_t i)
471{
472 return (notifications & FFA_NOTIFICATION_MASK(i)) != 0U;
473}
474
475static struct notifications *vm_get_notifications(struct vm_locked vm_locked,
476 bool is_from_vm)
477{
478 return is_from_vm ? &vm_locked.vm->notifications.from_vm
479 : &vm_locked.vm->notifications.from_sp;
480}
481
J-Alvesfe23ebe2021-10-13 16:07:07 +0100482static void vm_notifications_global_state_count_update(
483 ffa_notifications_bitmap_t bitmap, uint32_t *counter, int inc)
484{
485 /*
486 * Helper to increment counters from global notifications
487 * state. Count update by increments or decrements of 1 or -1,
488 * respectively.
489 */
490 CHECK(inc == 1 || inc == -1);
491
492 sl_lock(&all_notifications_state.lock);
493
494 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
495 if (vm_is_notification_bit_set(bitmap, i)) {
496 CHECK((inc > 0 && *counter < UINT32_MAX) ||
497 (inc < 0 && *counter > 0));
498 *counter += inc;
499 }
500 }
501
502 sl_unlock(&all_notifications_state.lock);
503}
504
505/**
506 * Helper function to increment the pending notifications based on a bitmap
507 * passed as argument.
508 * Function to be used at setting notifications for a given VM.
509 */
510static void vm_notifications_pending_count_add(
511 ffa_notifications_bitmap_t to_add)
512{
513 vm_notifications_global_state_count_update(
514 to_add, &all_notifications_state.pending_count, 1);
515}
516
517/**
518 * Helper function to decrement the pending notifications count.
519 * Function to be used when getting the receiver's pending notifications.
520 */
521static void vm_notifications_pending_count_sub(
522 ffa_notifications_bitmap_t to_sub)
523{
524 vm_notifications_global_state_count_update(
525 to_sub, &all_notifications_state.pending_count, -1);
526}
527
528/**
529 * Helper function to count the notifications whose information has been
530 * retrieved by the scheduler of the system, and are still pending.
531 */
532static void vm_notifications_info_get_retrieved_count_add(
533 ffa_notifications_bitmap_t to_add)
534{
535 vm_notifications_global_state_count_update(
536 to_add, &all_notifications_state.info_get_retrieved_count, 1);
537}
538
539/**
540 * Helper function to subtract the notifications that the receiver is getting
541 * and whose information has been retrieved by the receiver scheduler.
542 */
543static void vm_notifications_info_get_retrieved_count_sub(
544 ffa_notifications_bitmap_t to_sub)
545{
546 vm_notifications_global_state_count_update(
547 to_sub, &all_notifications_state.info_get_retrieved_count, -1);
548}
549
550/**
551 * Helper function to determine if there are notifications pending whose info
552 * hasn't been retrieved by the receiver scheduler.
553 */
554bool vm_notifications_pending_not_retrieved_by_scheduler(void)
555{
556 bool ret;
557
558 sl_lock(&all_notifications_state.lock);
559 ret = all_notifications_state.pending_count >
560 all_notifications_state.info_get_retrieved_count;
561 sl_unlock(&all_notifications_state.lock);
562
563 return ret;
564}
565
566bool vm_is_notifications_pending_count_zero(void)
567{
568 bool ret;
569
570 sl_lock(&all_notifications_state.lock);
571 ret = all_notifications_state.pending_count == 0;
572 sl_unlock(&all_notifications_state.lock);
573
574 return ret;
575}
576
J-Alvesc003a7a2021-03-18 13:06:53 +0000577/**
578 * Checks that all provided notifications are bound to the specified sender, and
579 * are per VCPU or global, as specified.
580 */
581bool vm_notifications_validate_binding(struct vm_locked vm_locked,
582 bool is_from_vm, ffa_vm_id_t sender_id,
583 ffa_notifications_bitmap_t notifications,
584 bool is_per_vcpu)
585{
586 return vm_notifications_validate_bound_sender(
587 vm_locked, is_from_vm, sender_id, notifications) &&
588 vm_notifications_validate_per_vcpu(vm_locked, is_from_vm,
589 is_per_vcpu, notifications);
590}
591
592/**
593 * Update binds information in notification structure for the specified
594 * notifications.
595 */
596void vm_notifications_update_bindings(struct vm_locked vm_locked,
597 bool is_from_vm, ffa_vm_id_t sender_id,
598 ffa_notifications_bitmap_t notifications,
599 bool is_per_vcpu)
600{
601 CHECK(vm_locked.vm != NULL);
602 struct notifications *to_update =
603 vm_get_notifications(vm_locked, is_from_vm);
604
605 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
606 if (vm_is_notification_bit_set(notifications, i)) {
607 to_update->bindings_sender_id[i] = sender_id;
608 }
609 }
610
611 /*
612 * Set notifications if they are per VCPU, else clear them as they are
613 * global.
614 */
615 if (is_per_vcpu) {
616 to_update->bindings_per_vcpu |= notifications;
617 } else {
618 to_update->bindings_per_vcpu &= ~notifications;
619 }
620}
621
622bool vm_notifications_validate_bound_sender(
623 struct vm_locked vm_locked, bool is_from_vm, ffa_vm_id_t sender_id,
624 ffa_notifications_bitmap_t notifications)
625{
626 CHECK(vm_locked.vm != NULL);
627 struct notifications *to_check =
628 vm_get_notifications(vm_locked, is_from_vm);
629
630 for (uint32_t i = 0; i < MAX_FFA_NOTIFICATIONS; i++) {
631 if (vm_is_notification_bit_set(notifications, i) &&
632 to_check->bindings_sender_id[i] != sender_id) {
633 return false;
634 }
635 }
636
637 return true;
638}
639
640bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked,
641 bool is_from_vm, bool is_per_vcpu,
642 ffa_notifications_bitmap_t notif)
643{
644 CHECK(vm_locked.vm != NULL);
645 struct notifications *to_check =
646 vm_get_notifications(vm_locked, is_from_vm);
647
648 return is_per_vcpu ? (~to_check->bindings_per_vcpu & notif) == 0U
649 : (to_check->bindings_per_vcpu & notif) == 0U;
650}
J-Alvesaa79c012021-07-09 14:29:45 +0100651
652void vm_notifications_set(struct vm_locked vm_locked, bool is_from_vm,
653 ffa_notifications_bitmap_t notifications,
654 ffa_vcpu_index_t vcpu_id, bool is_per_vcpu)
655{
656 CHECK(vm_locked.vm != NULL);
657 struct notifications *to_set =
658 vm_get_notifications(vm_locked, is_from_vm);
659 CHECK(vcpu_id < MAX_CPUS);
660
661 if (is_per_vcpu) {
662 to_set->per_vcpu[vcpu_id].pending |= notifications;
663 } else {
664 to_set->global.pending |= notifications;
665 }
J-Alvesfe23ebe2021-10-13 16:07:07 +0100666
667 /* Update count of notifications pending. */
668 vm_notifications_pending_count_add(notifications);
J-Alvesaa79c012021-07-09 14:29:45 +0100669}
670
671/**
672 * Get Global notifications and per CPU only of the current VCPU.
673 */
674ffa_notifications_bitmap_t vm_notifications_get_pending_and_clear(
675 struct vm_locked vm_locked, bool is_from_vm,
676 ffa_vcpu_index_t cur_vcpu_id)
677{
678 ffa_notifications_bitmap_t to_ret = 0;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100679 ffa_notifications_bitmap_t pending_and_info_get_retrieved;
J-Alvesaa79c012021-07-09 14:29:45 +0100680
681 CHECK(vm_locked.vm != NULL);
682 struct notifications *to_get =
683 vm_get_notifications(vm_locked, is_from_vm);
684 CHECK(cur_vcpu_id < MAX_CPUS);
685
686 to_ret |= to_get->global.pending;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100687
688 /* Update count of currently pending notifications in the system. */
689 vm_notifications_pending_count_sub(to_get->global.pending);
690
691 /*
692 * If notifications receiver is getting have been retrieved by the
693 * receiver scheduler, decrement those from respective count.
694 */
695 pending_and_info_get_retrieved =
696 to_get->global.pending & to_get->global.info_get_retrieved;
697
698 if (pending_and_info_get_retrieved != 0) {
699 vm_notifications_info_get_retrieved_count_sub(
700 pending_and_info_get_retrieved);
701 }
702
J-Alvesaa79c012021-07-09 14:29:45 +0100703 to_get->global.pending = 0U;
704 to_get->global.info_get_retrieved = 0U;
705
706 to_ret |= to_get->per_vcpu[cur_vcpu_id].pending;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100707
708 /*
709 * Update counts of notifications, this time for per-vCPU notifications.
710 */
711 vm_notifications_pending_count_sub(
712 to_get->per_vcpu[cur_vcpu_id].pending);
713
714 pending_and_info_get_retrieved =
715 to_get->per_vcpu[cur_vcpu_id].pending &
716 to_get->per_vcpu[cur_vcpu_id].info_get_retrieved;
717
718 if (pending_and_info_get_retrieved != 0) {
719 vm_notifications_info_get_retrieved_count_sub(
720 pending_and_info_get_retrieved);
721 }
722
J-Alvesaa79c012021-07-09 14:29:45 +0100723 to_get->per_vcpu[cur_vcpu_id].pending = 0U;
724 to_get->per_vcpu[cur_vcpu_id].info_get_retrieved = 0U;
725
726 return to_ret;
727}
J-Alvesc8e8a222021-06-08 17:33:52 +0100728
729/**
730 * Get pending notification's information to return to the receiver scheduler.
731 */
732void vm_notifications_info_get_pending(
733 struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids,
734 uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count,
735 const uint32_t ids_max_count,
736 enum notifications_info_get_state *info_get_state)
737{
738 ffa_notifications_bitmap_t pending_not_retrieved;
739
740 CHECK(vm_locked.vm != NULL);
741 struct notifications *notifications =
742 vm_get_notifications(vm_locked, is_from_vm);
743
744 if (*info_get_state == FULL) {
745 return;
746 }
747
748 CHECK(*ids_count <= ids_max_count);
749 CHECK(*lists_count <= ids_max_count);
750
751 pending_not_retrieved = notifications->global.pending &
752 ~notifications->global.info_get_retrieved;
753
754 if (pending_not_retrieved != 0U && *info_get_state == INIT) {
755 /*
756 * If action is to INIT, means that no list has been
757 * created for the given VM ID, which also means that global
758 * notifications are not represented in the list yet.
759 */
760 if (*ids_count == ids_max_count) {
761 *info_get_state = FULL;
762 return;
763 }
764
765 *info_get_state = INSERTING;
766
767 (*lists_count)++;
768 ids[*ids_count] = vm_locked.vm->id;
769 ++(*ids_count);
770 }
771
772 notifications->global.info_get_retrieved |= pending_not_retrieved;
773
J-Alvesfe23ebe2021-10-13 16:07:07 +0100774 vm_notifications_info_get_retrieved_count_add(pending_not_retrieved);
775
J-Alvesc8e8a222021-06-08 17:33:52 +0100776 for (ffa_vcpu_count_t i = 0; i < vm_locked.vm->vcpu_count; i++) {
777 /*
778 * Include VCPU ID of per-VCPU notifications.
779 */
780 pending_not_retrieved =
781 notifications->per_vcpu[i].pending &
782 ~notifications->per_vcpu[i].info_get_retrieved;
783
784 if (pending_not_retrieved == 0U) {
785 continue;
786 }
787
788 switch (*info_get_state) {
789 case INIT:
790 case STARTING_NEW:
791 /*
792 * At this iteration two ids need to be added: the VM ID
793 * and VCPU ID. If there is not space, change state and
794 * terminate function.
795 */
796 if (ids_max_count - *ids_count < 2) {
797 *info_get_state = FULL;
798 return;
799 }
800
801 ids[*ids_count] = vm_locked.vm->id;
802 ++(*ids_count);
803
804 /* Insert VCPU ID */
805 ids[*ids_count] = i;
806 ++(*ids_count);
807
808 ++lists_sizes[*lists_count];
809 ++(*lists_count);
810
811 *info_get_state = INSERTING;
812 break;
813 case INSERTING:
814 if (*ids_count == ids_max_count) {
815 *info_get_state = FULL;
816 return;
817 }
818
819 /* Insert VCPU ID */
820 ids[*ids_count] = i;
821 (*ids_count)++;
822
823 /* Increment respective list size */
824 ++lists_sizes[*lists_count - 1];
825
826 if (lists_sizes[*lists_count - 1] == 3) {
827 *info_get_state = STARTING_NEW;
828 }
829 break;
830 default:
831 panic("Notification info get action error!!\n");
832 }
833
834 notifications->per_vcpu[i].info_get_retrieved |=
835 pending_not_retrieved;
J-Alvesfe23ebe2021-10-13 16:07:07 +0100836
837 vm_notifications_info_get_retrieved_count_add(
838 pending_not_retrieved);
J-Alvesc8e8a222021-06-08 17:33:52 +0100839 }
840}
841
842/**
843 * Gets all info from VM's pending notifications.
844 * Returns true if the list is full, and there is more pending.
845 */
846bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids,
847 uint32_t *ids_count, uint32_t *lists_sizes,
848 uint32_t *lists_count,
849 const uint32_t ids_max_count)
850{
851 enum notifications_info_get_state current_state = INIT;
852
853 /* Get info of pending notifications from SPs */
854 vm_notifications_info_get_pending(vm_locked, false, ids, ids_count,
855 lists_sizes, lists_count,
856 ids_max_count, &current_state);
857
858 /* Get info of pending notifications from VMs */
859 vm_notifications_info_get_pending(vm_locked, true, ids, ids_count,
860 lists_sizes, lists_count,
861 ids_max_count, &current_state);
862
863 /*
864 * State transitions to FULL when trying to insert a new ID in the
865 * list and there is not more space. This means there are notifications
866 * pending, whose info is not retrieved.
867 */
868 return current_state == FULL;
869}