blob: 6a8f8b1c4344c87f30500082ff81b89d793fe514 [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 *
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
Andrew Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
Andrew Scull9726c252019-01-23 13:44:19 +000019#include <stdatomic.h>
20
Andrew Walbran1f32e722019-06-07 17:57:26 +010021#include "hf/arch/types.h"
22
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/cpu.h"
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000024#include "hf/list.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/mm.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000026#include "hf/mpool.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000027#include "hf/spci.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010028
Andrew Scullaa039b32018-10-04 15:02:26 +010029enum mailbox_state {
Andrew Walbranc3910f72018-11-27 14:24:36 +000030 /** There is no message in the mailbox. */
Andrew Sculld6ee1102019-04-05 22:12:42 +010031 MAILBOX_STATE_EMPTY,
Andrew Scullaa039b32018-10-04 15:02:26 +010032
Andrew Walbranc3910f72018-11-27 14:24:36 +000033 /** There is a message in the mailbox that is waiting for a reader. */
Andrew Sculld6ee1102019-04-05 22:12:42 +010034 MAILBOX_STATE_RECEIVED,
Andrew Scullaa039b32018-10-04 15:02:26 +010035
Andrew Walbranc3910f72018-11-27 14:24:36 +000036 /** There is a message in the mailbox that has been read. */
Andrew Sculld6ee1102019-04-05 22:12:42 +010037 MAILBOX_STATE_READ,
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010038};
39
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000040struct wait_entry {
41 /** The VM that is waiting for a mailbox to become writable. */
42 struct vm *waiting_vm;
43
44 /**
45 * Links used to add entry to a VM's waiter_list. This is protected by
46 * the notifying VM's lock.
47 */
48 struct list_entry wait_links;
49
50 /**
51 * Links used to add entry to a VM's ready_list. This is protected by
52 * the waiting VM's lock.
53 */
54 struct list_entry ready_links;
55};
56
Andrew Scullaa039b32018-10-04 15:02:26 +010057struct mailbox {
58 enum mailbox_state state;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000059 struct spci_message *recv;
60 const struct spci_message *send;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000061
62 /**
63 * List of wait_entry structs representing VMs that want to be notified
64 * when the mailbox becomes writable. Once the mailbox does become
65 * writable, the entry is removed from this list and added to the
66 * waiting VM's ready_list.
67 */
68 struct list_entry waiter_list;
69
70 /**
71 * List of wait_entry structs representing VMs whose mailboxes became
72 * writable since the owner of the mailbox registers for notification.
73 */
74 struct list_entry ready_list;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010075};
76
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010077struct vm {
Andrew Walbran95534922019-06-19 11:32:54 +010078 spci_vm_id_t id;
Andrew Walbran0d7a0682018-12-06 16:48:47 +000079 /** See api.c for the partial ordering on locks. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010080 struct spinlock lock;
Wedson Almeida Filho87009642018-07-02 10:20:07 +010081 uint32_t vcpu_count;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010082 struct vcpu vcpus[MAX_CPUS];
Andrew Scull89a75242018-08-06 17:04:55 +010083 struct mm_ptable ptable;
Andrew Scullaa039b32018-10-04 15:02:26 +010084 struct mailbox mailbox;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000085
86 /** Wait entries to be used when waiting on other VM mailboxes. */
Wedson Almeida Filhob790f652019-01-22 23:41:56 +000087 struct wait_entry wait_entries[MAX_VMS];
Andrew Scull9726c252019-01-23 13:44:19 +000088
89 atomic_bool aborting;
Andrew Walbran1f32e722019-06-07 17:57:26 +010090
91 /** Arch-specific VM information. */
92 struct arch_vm arch;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000093};
94
95/** Encapsulates a VM whose lock is held. */
96struct vm_locked {
97 struct vm *vm;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010098};
99
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000100bool vm_init(uint32_t vcpu_count, struct mpool *ppool, struct vm **new_vm);
Andrew Scull19503262018-09-20 14:48:39 +0100101uint32_t vm_get_count(void);
Andrew Walbran42347a92019-05-09 13:59:03 +0100102struct vm *vm_find(spci_vm_id_t id);
Andrew Walbran7e932bd2019-04-29 16:47:06 +0100103struct vm_locked vm_lock(struct vm *vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000104void vm_unlock(struct vm_locked *locked);
Andrew Walbrane1310df2019-04-29 17:28:28 +0100105struct vcpu *vm_get_vcpu(struct vm *vm, uint32_t vcpu_index);