blob: d5cfe558b0c7d53223a303c8075783d19e7740fb [file] [log] [blame]
Andrew Scull01778112019-01-14 15:37:53 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Walbran13c3a0b2018-11-30 11:51:53 +00002/*
Andrew Walbran2bc0a322019-03-07 15:48:06 +00003 * Copyright 2018 The Hafnium Authors.
Andrew Walbran13c3a0b2018-11-30 11:51:53 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Andrew Walbran13c3a0b2018-11-30 11:51:53 +000013 */
14
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000015#include <clocksource/arm_arch_timer.h>
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000016#include <linux/atomic.h>
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000017#include <linux/cpuhotplug.h>
18#include <linux/hrtimer.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010019#include <linux/init.h>
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000020#include <linux/interrupt.h>
21#include <linux/irq.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010022#include <linux/kernel.h>
23#include <linux/kthread.h>
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +010024#include <linux/mm.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010025#include <linux/module.h>
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000026#include <linux/net.h>
27#include <linux/of.h>
28#include <linux/platform_device.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010029#include <linux/sched/task.h>
30#include <linux/slab.h>
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000031#include <net/sock.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010032
Andrew Scull55704232018-08-10 17:19:54 +010033#include <hf/call.h>
Andrew Walbran196ed0e2020-04-30 11:32:29 +010034#include <hf/ffa.h>
Fuad Tabba3e669bc2019-08-08 16:43:55 +010035#include <hf/transport.h>
Olivier Deprezdfe722f2021-02-12 10:21:01 +010036#include <hf/vm_ids.h>
Andrew Scull55704232018-08-10 17:19:54 +010037
Fuad Tabba3e669bc2019-08-08 16:43:55 +010038#include "uapi/hf/socket.h"
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000039
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000040#define HYPERVISOR_TIMER_NAME "el2_timer"
41
Andrew Scull82257c42018-10-01 10:37:48 +010042#define CONFIG_HAFNIUM_MAX_VMS 16
43#define CONFIG_HAFNIUM_MAX_VCPUS 32
44
Olivier Deprezf441ed02020-08-11 15:50:23 +020045#define HF_VM_ID_BASE 0
Andrew Walbrana6974312020-10-29 17:00:09 +000046#define PRIMARY_VM_ID HF_VM_ID_OFFSET
Fuad Tabba5da4b6b2019-08-05 13:56:20 +010047#define FIRST_SECONDARY_VM_ID (HF_VM_ID_OFFSET + 1)
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000048
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010049struct hf_vcpu {
Andrew Scullb722f952018-09-27 15:39:10 +010050 struct hf_vm *vm;
Andrew Walbran196ed0e2020-04-30 11:32:29 +010051 ffa_vcpu_index_t vcpu_index;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010052 struct task_struct *task;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +000053 atomic_t abort_sleep;
Andrew Scull71f57362019-02-05 16:11:35 +000054 atomic_t waiting_for_message;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010055 struct hrtimer timer;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010056};
57
58struct hf_vm {
J-Alvesc653e722023-08-02 13:08:54 +010059 ffa_id_t id;
Andrew Walbran196ed0e2020-04-30 11:32:29 +010060 ffa_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010061 struct hf_vcpu *vcpu;
62};
63
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000064struct hf_sock {
65 /* This needs to be the first field. */
66 struct sock sk;
67
68 /*
69 * The following fields are immutable after the socket transitions to
70 * SS_CONNECTED state.
71 */
72 uint64_t local_port;
73 uint64_t remote_port;
74 struct hf_vm *peer_vm;
75};
76
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000077static struct proto hf_sock_proto = {
78 .name = "hafnium",
79 .owner = THIS_MODULE,
80 .obj_size = sizeof(struct hf_sock),
81};
82
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010083static struct hf_vm *hf_vms;
Andrew Walbran196ed0e2020-04-30 11:32:29 +010084static ffa_vm_count_t hf_vm_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000085static struct page *hf_send_page;
86static struct page *hf_recv_page;
87static atomic64_t hf_next_port = ATOMIC64_INIT(0);
88static DEFINE_SPINLOCK(hf_send_lock);
89static DEFINE_HASHTABLE(hf_local_port_hash, 7);
90static DEFINE_SPINLOCK(hf_local_port_hash_lock);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000091static int hf_irq;
Andrew Walbran8d55e502019-02-05 11:42:08 +000092static enum cpuhp_state hf_cpuhp_state;
J-Alvesc653e722023-08-02 13:08:54 +010093static ffa_id_t current_vm_id;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010094
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010095/**
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000096 * Retrieves a VM from its ID, returning NULL if the VM doesn't exist.
97 */
J-Alvesc653e722023-08-02 13:08:54 +010098static struct hf_vm *hf_vm_from_id(ffa_id_t vm_id)
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000099{
100 if (vm_id < FIRST_SECONDARY_VM_ID ||
101 vm_id >= FIRST_SECONDARY_VM_ID + hf_vm_count)
102 return NULL;
103
104 return &hf_vms[vm_id - FIRST_SECONDARY_VM_ID];
105}
106
107/**
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000108 * Wakes up the kernel thread responsible for running the given vcpu.
109 *
110 * Returns 0 if the thread was already running, 1 otherwise.
111 */
112static int hf_vcpu_wake_up(struct hf_vcpu *vcpu)
113{
114 /* Set a flag indicating that the thread should not go to sleep. */
115 atomic_set(&vcpu->abort_sleep, 1);
116
117 /* Set the thread to running state. */
118 return wake_up_process(vcpu->task);
119}
120
121/**
122 * Puts the current thread to sleep. The current thread must be responsible for
123 * running the given vcpu.
124 *
125 * Going to sleep will fail if hf_vcpu_wake_up() or kthread_stop() was called on
126 * this vcpu/thread since the last time it [re]started running.
127 */
128static void hf_vcpu_sleep(struct hf_vcpu *vcpu)
129{
130 int abort;
131
132 set_current_state(TASK_INTERRUPTIBLE);
133
134 /* Check the sleep-abort flag after making thread interruptible. */
135 abort = atomic_read(&vcpu->abort_sleep);
136 if (!abort && !kthread_should_stop())
137 schedule();
138
139 /* Set state back to running on the way out. */
140 set_current_state(TASK_RUNNING);
141}
142
143/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100144 * Wakes up the thread associated with the vcpu that owns the given timer. This
145 * is called when the timer the thread is waiting on expires.
146 */
147static enum hrtimer_restart hf_vcpu_timer_expired(struct hrtimer *timer)
148{
149 struct hf_vcpu *vcpu = container_of(timer, struct hf_vcpu, timer);
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000150 /* TODO: Inject interrupt. */
151 hf_vcpu_wake_up(vcpu);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100152 return HRTIMER_NORESTART;
153}
154
155/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000156 * This function is called when Hafnium requests that the primary VM wake up a
157 * vCPU that belongs to a secondary VM.
158 *
159 * It wakes up the thread if it's sleeping, or kicks it if it's already running.
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000160 */
J-Alvesc653e722023-08-02 13:08:54 +0100161static void hf_handle_wake_up_request(ffa_id_t vm_id,
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100162 ffa_vcpu_index_t vcpu)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000163{
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000164 struct hf_vm *vm = hf_vm_from_id(vm_id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000165
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000166 if (!vm) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000167 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
168 return;
169 }
170
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000171 if (vcpu >= vm->vcpu_count) {
Andrew Scull71f57362019-02-05 16:11:35 +0000172 pr_warn("Request to wake up non-existent vCPU: %u.%u\n",
173 vm_id, vcpu);
174 return;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000175 }
176
177 if (hf_vcpu_wake_up(&vm->vcpu[vcpu]) == 0) {
178 /*
179 * The task was already running (presumably on a different
180 * physical CPU); interrupt it. This gives Hafnium a chance to
181 * inject any new interrupts.
182 */
183 kick_process(vm->vcpu[vcpu].task);
184 }
185}
186
187/**
Andrew Scull71f57362019-02-05 16:11:35 +0000188 * Injects an interrupt into a vCPU of the VM and ensures the vCPU will run to
189 * handle the interrupt.
190 */
J-Alvesc653e722023-08-02 13:08:54 +0100191static void hf_interrupt_vm(ffa_id_t vm_id, uint64_t int_id)
Andrew Scull71f57362019-02-05 16:11:35 +0000192{
193 struct hf_vm *vm = hf_vm_from_id(vm_id);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100194 ffa_vcpu_index_t vcpu;
Andrew Scull71f57362019-02-05 16:11:35 +0000195 int64_t ret;
196
197 if (!vm) {
198 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
199 return;
200 }
201
202 /*
203 * TODO: For now we're picking the first vcpu to interrupt, but
204 * we want to be smarter.
205 */
206 vcpu = 0;
207 ret = hf_interrupt_inject(vm_id, vcpu, int_id);
208
209 if (ret == -1) {
210 pr_warn("Failed to inject interrupt %lld to vCPU %d of VM %d",
211 int_id, vcpu, vm_id);
212 return;
213 }
214
215 if (ret != 1) {
216 /* We don't need to wake up the vcpu. */
217 return;
218 }
219
220 hf_handle_wake_up_request(vm_id, vcpu);
221}
222
223/**
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000224 * Notify all waiters on the given VM.
225 */
J-Alvesc653e722023-08-02 13:08:54 +0100226static void hf_notify_waiters(ffa_id_t vm_id)
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000227{
J-Alvesc653e722023-08-02 13:08:54 +0100228 ffa_id_t waiter_vm_id;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000229
Andrew Scull71f57362019-02-05 16:11:35 +0000230 while ((waiter_vm_id = hf_mailbox_waiter_get(vm_id)) != -1) {
Andrew Walbrana6974312020-10-29 17:00:09 +0000231 if (waiter_vm_id == PRIMARY_VM_ID) {
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000232 /*
233 * TODO: Use this information when implementing per-vm
234 * queues.
235 */
236 } else {
Andrew Scull71f57362019-02-05 16:11:35 +0000237 hf_interrupt_vm(waiter_vm_id,
238 HF_MAILBOX_WRITABLE_INTID);
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000239 }
240 }
241}
242
243/**
Andrew Scull71f57362019-02-05 16:11:35 +0000244 * Delivers a message to a VM.
245 */
J-Alvesc653e722023-08-02 13:08:54 +0100246static void hf_deliver_message(ffa_id_t vm_id)
Andrew Scull71f57362019-02-05 16:11:35 +0000247{
248 struct hf_vm *vm = hf_vm_from_id(vm_id);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100249 ffa_vcpu_index_t i;
Andrew Scull71f57362019-02-05 16:11:35 +0000250
251 if (!vm) {
252 pr_warn("Tried to deliver message to non-existent VM id: %u\n",
253 vm_id);
254 return;
255 }
256
257 /* Try to wake a vCPU that is waiting for a message. */
258 for (i = 0; i < vm->vcpu_count; i++) {
259 if (atomic_read(&vm->vcpu[i].waiting_for_message)) {
260 hf_handle_wake_up_request(vm->id,
261 vm->vcpu[i].vcpu_index);
262 return;
263 }
264 }
265
266 /* None were waiting for a message so interrupt one. */
267 hf_interrupt_vm(vm->id, HF_MAILBOX_READABLE_INTID);
268}
269
270/**
Andrew Sculldf6478f2019-02-19 17:52:08 +0000271 * Handles a message delivered to this VM by validating that it's well-formed
272 * and then queueing it for delivery to the appropriate socket.
273 */
Andrew Walbranb331fa92019-10-03 16:48:07 +0100274static void hf_handle_message(struct hf_vm *sender, size_t len,
Andrew Walbrancafe0172019-10-07 14:14:05 +0100275 const void *message)
Andrew Sculldf6478f2019-02-19 17:52:08 +0000276{
277 struct hf_sock *hsock;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100278 const struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)message;
Andrew Sculldf6478f2019-02-19 17:52:08 +0000279 struct sk_buff *skb;
280 int err;
281
282 /* Ignore messages that are too small to hold a header. */
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000283 if (len < sizeof(struct hf_msg_hdr)) {
284 pr_err("Message received without header of length %d\n", len);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100285 ffa_rx_release();
Andrew Sculldf6478f2019-02-19 17:52:08 +0000286 return;
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000287 }
Andrew Sculldf6478f2019-02-19 17:52:08 +0000288
289 len -= sizeof(struct hf_msg_hdr);
290
291 /* Go through the colliding sockets. */
292 rcu_read_lock();
293 hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node,
294 hdr->dst_port) {
295 if (hsock->peer_vm == sender &&
296 hsock->remote_port == hdr->src_port) {
297 sock_hold(&hsock->sk);
298 break;
299 }
300 }
301 rcu_read_unlock();
302
303 /* Nothing to do if we couldn't find the target. */
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000304 if (!hsock) {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100305 ffa_rx_release();
Andrew Sculldf6478f2019-02-19 17:52:08 +0000306 return;
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000307 }
Andrew Sculldf6478f2019-02-19 17:52:08 +0000308
309 /*
310 * TODO: From this point on, there are two failure paths: when we
311 * create the skb below, and when we enqueue it to the socket. What
312 * should we do if they fail? Ideally we would have some form of flow
313 * control to prevent message loss, but how to do it efficiently?
314 *
315 * One option is to have a pre-allocated message that indicates to the
316 * sender that a message was dropped. This way we guarantee that the
317 * sender will be aware of loss and should back-off.
318 */
319 /* Create the skb. */
320 skb = alloc_skb(len, GFP_KERNEL);
321 if (!skb)
322 goto exit;
323
324 memcpy(skb_put(skb, len), hdr + 1, len);
325
326 /*
327 * Add the skb to the receive queue of the target socket. On success it
328 * calls sk->sk_data_ready, which is currently set to sock_def_readable,
329 * which wakes up any waiters.
330 */
331 err = sock_queue_rcv_skb(&hsock->sk, skb);
332 if (err)
333 kfree_skb(skb);
334
335exit:
336 sock_put(&hsock->sk);
Andrew Scull71f57362019-02-05 16:11:35 +0000337
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100338 if (ffa_rx_release().func == FFA_RX_RELEASE_32)
Andrew Walbrana6974312020-10-29 17:00:09 +0000339 hf_notify_waiters(PRIMARY_VM_ID);
Andrew Sculldf6478f2019-02-19 17:52:08 +0000340}
341
342/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100343 * This is the main loop of each vcpu.
344 */
345static int hf_vcpu_thread(void *data)
346{
347 struct hf_vcpu *vcpu = data;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100348 struct ffa_value ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100349
350 hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
351 vcpu->timer.function = &hf_vcpu_timer_expired;
352
353 while (!kthread_should_stop()) {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100354 ffa_vcpu_index_t i;
Andrew Scull01f83de2019-01-23 13:41:47 +0000355
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000356 /*
357 * We're about to run the vcpu, so we can reset the abort-sleep
358 * flag.
359 */
360 atomic_set(&vcpu->abort_sleep, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100361
Andrew Scullbb7ae412018-09-28 21:07:15 +0100362 /* Call into Hafnium to run vcpu. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100363 ret = ffa_run(vcpu->vm->id, vcpu->vcpu_index);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100364
Andrew Walbran39bf7892019-11-01 14:14:47 +0000365 switch (ret.func) {
Andrew Walbranc886c612020-10-22 16:47:57 +0100366 /* Preempted, or wants to wake up another vCPU. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100367 case FFA_INTERRUPT_32:
Andrew Walbranc886c612020-10-22 16:47:57 +0100368 {
J-Alvesc653e722023-08-02 13:08:54 +0100369 ffa_id_t vm_id = ffa_vm_id(ret);
Andrew Walbranc886c612020-10-22 16:47:57 +0100370 ffa_vcpu_index_t vcpu_index = ffa_vcpu_index(ret);
371
372 if (vm_id >= FIRST_SECONDARY_VM_ID &&
373 vm_id != vcpu->vm->id) {
374 /* Wake up another vCPU. */
375 hf_handle_wake_up_request(vm_id, vcpu_index);
376 }
Andrew Sculle05702e2019-01-08 14:46:46 +0000377 if (need_resched())
378 schedule();
379 break;
Andrew Walbranc886c612020-10-22 16:47:57 +0100380 }
Andrew Sculle05702e2019-01-08 14:46:46 +0000381
382 /* Yield. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100383 case FFA_YIELD_32:
Andrew Sculle05702e2019-01-08 14:46:46 +0000384 if (!kthread_should_stop())
385 schedule();
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100386 break;
387
Andrew Scull01778112019-01-14 15:37:53 +0000388 /* WFI. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100389 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
390 if (ret.arg2 != FFA_SLEEP_INDEFINITE) {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000391 hrtimer_start(&vcpu->timer, ret.arg2,
Andrew Scull71f57362019-02-05 16:11:35 +0000392 HRTIMER_MODE_REL);
393 }
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000394 hf_vcpu_sleep(vcpu);
Andrew Scull71f57362019-02-05 16:11:35 +0000395 hrtimer_cancel(&vcpu->timer);
396 break;
397
398 /* Waiting for a message. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100399 case FFA_MSG_WAIT_32:
Andrew Scull71f57362019-02-05 16:11:35 +0000400 atomic_set(&vcpu->waiting_for_message, 1);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100401 if (ret.arg2 != FFA_SLEEP_INDEFINITE) {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000402 hrtimer_start(&vcpu->timer, ret.arg2,
Andrew Scull71f57362019-02-05 16:11:35 +0000403 HRTIMER_MODE_REL);
404 }
405 hf_vcpu_sleep(vcpu);
406 hrtimer_cancel(&vcpu->timer);
407 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100408 break;
409
Andrew Scullb3a61b52018-09-17 14:30:34 +0100410 /* Response available. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100411 case FFA_MSG_SEND_32:
J-Alvesc26981f2021-03-05 13:25:40 +0000412 if (ffa_receiver(ret) == PRIMARY_VM_ID) {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000413 hf_handle_message(vcpu->vm,
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100414 ffa_msg_send_size(ret),
Andrew Scull94704232019-04-01 12:36:37 +0100415 page_address(hf_recv_page));
Andrew Scull71f57362019-02-05 16:11:35 +0000416 } else {
J-Alvesc26981f2021-03-05 13:25:40 +0000417 hf_deliver_message(ffa_receiver(ret));
Andrew Scull71f57362019-02-05 16:11:35 +0000418 }
Andrew Sculldc8cab52018-10-10 18:29:39 +0100419 break;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000420
421 /* Notify all waiters. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100422 case FFA_RX_RELEASE_32:
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000423 hf_notify_waiters(vcpu->vm->id);
424 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000425
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100426 case FFA_ERROR_32:
427 pr_warn("FF-A error %d running VM %d vCPU %d", ret.arg2,
Andrew Walbran18f08a62019-11-13 11:57:52 +0000428 vcpu->vm->id, vcpu->vcpu_index);
Andrew Walbran39bf7892019-11-01 14:14:47 +0000429 switch (ret.arg2) {
Andrew Walbran9abce272019-11-27 18:41:05 +0000430 /* Abort was triggered. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100431 case FFA_ABORTED:
Andrew Walbran39bf7892019-11-01 14:14:47 +0000432 for (i = 0; i < vcpu->vm->vcpu_count; i++) {
433 if (i == vcpu->vcpu_index)
434 continue;
435 hf_handle_wake_up_request(vcpu->vm->id,
436 i);
437 }
438 hf_vcpu_sleep(vcpu);
439 break;
Andrew Walbran9abce272019-11-27 18:41:05 +0000440 default:
441 /* Treat as a yield and try again later. */
442 if (!kthread_should_stop())
443 schedule();
444 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000445 }
Andrew Scull01f83de2019-01-23 13:41:47 +0000446 break;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100447 }
448 }
449
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100450 return 0;
451}
452
453/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000454 * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It
455 * relies on the fact that the first field of hf_sock is a sock.
456 */
457static struct hf_sock *hsock_from_sk(struct sock *sk)
458{
459 return (struct hf_sock *)sk;
460}
461
462/**
463 * This is called when the last reference to the outer socket is released. For
464 * example, if it's a user-space socket, when the last file descriptor pointing
465 * to this socket is closed.
466 *
467 * It begins cleaning up resources, though some can only be cleaned up after all
468 * references to the underlying socket are released, which is handled by
469 * hf_sock_destruct().
470 */
471static int hf_sock_release(struct socket *sock)
472{
473 struct sock *sk = sock->sk;
474 struct hf_sock *hsock = hsock_from_sk(sk);
475 unsigned long flags;
476
477 if (!sk)
478 return 0;
479
480 /* Shutdown for both send and receive. */
481 lock_sock(sk);
482 sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN;
483 sk->sk_state_change(sk);
484 release_sock(sk);
485
486 /* Remove from the hash table, so lookups from now on won't find it. */
487 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
488 hash_del_rcu(&hsock->sk.sk_node);
489 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
490
491 /*
492 * TODO: When we implement a tx queue, we need to clear it here so that
493 * sk_wmem_alloc will not prevent sk from being freed (sk_free).
494 */
495
496 /*
497 * Wait for in-flight lookups to finish. We need to do this here because
Wedson Almeida Filho89d0e472019-01-03 19:18:39 +0000498 * in-flight lookups rely on the reference to the socket we're about to
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000499 * release.
500 */
501 synchronize_rcu();
502 sock_put(sk);
503 sock->sk = NULL;
504
505 return 0;
506}
507
508/**
509 * This is called when there are no more references to the socket. It frees all
510 * resources that haven't been freed during release.
511 */
512static void hf_sock_destruct(struct sock *sk)
513{
514 /*
515 * Clear the receive queue now that the handler cannot add any more
516 * skbs to it.
517 */
518 skb_queue_purge(&sk->sk_receive_queue);
519}
520
521/**
522 * Connects the Hafnium socket to the provided VM and port. After the socket is
523 * connected, it can be used to exchange datagrams with the specified peer.
524 */
Andrew Scull01778112019-01-14 15:37:53 +0000525static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, int len,
526 int connect_flags)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000527{
528 struct sock *sk = sock->sk;
529 struct hf_sock *hsock = hsock_from_sk(sk);
530 struct hf_vm *vm;
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100531 struct hf_sockaddr *addr;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000532 int err;
533 unsigned long flags;
534
535 /* Basic address validation. */
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100536 if (len < sizeof(struct hf_sockaddr) || saddr->sa_family != AF_HF)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000537 return -EINVAL;
538
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100539 addr = (struct hf_sockaddr *)saddr;
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000540 vm = hf_vm_from_id(addr->vm_id);
541 if (!vm)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000542 return -ENETUNREACH;
543
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000544 /*
545 * TODO: Once we implement access control in Hafnium, check that the
546 * caller is allowed to contact the specified VM. Return -ECONNREFUSED
547 * if access is denied.
548 */
549
550 /* Take lock to make sure state doesn't change as we connect. */
551 lock_sock(sk);
552
553 /* Only unconnected sockets are allowed to become connected. */
554 if (sock->state != SS_UNCONNECTED) {
555 err = -EISCONN;
556 goto exit;
557 }
558
559 hsock->local_port = atomic64_inc_return(&hf_next_port);
560 hsock->remote_port = addr->port;
561 hsock->peer_vm = vm;
562
563 sock->state = SS_CONNECTED;
564
565 /* Add socket to hash table now that it's fully initialised. */
566 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
567 hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port);
568 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
569
570 err = 0;
571exit:
572 release_sock(sk);
573 return err;
574}
575
576/**
577 * Sends the given skb to the appropriate VM by calling Hafnium. It will also
578 * trigger the wake up of a recipient VM.
579 *
580 * Takes ownership of the skb on success.
581 */
582static int hf_send_skb(struct sk_buff *skb)
583{
584 unsigned long flags;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100585 struct ffa_value ret;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000586 struct hf_sock *hsock = hsock_from_sk(skb->sk);
587 struct hf_vm *vm = hsock->peer_vm;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100588 void *message = page_address(hf_send_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000589
590 /*
591 * Call Hafnium under the send lock so that we serialize the use of the
592 * global send buffer.
593 */
594 spin_lock_irqsave(&hf_send_lock, flags);
Andrew Walbrancafe0172019-10-07 14:14:05 +0100595 memcpy(message, skb->data, skb->len);
Jose Marinho1cc6c752019-03-11 16:28:03 +0000596
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100597 ret = ffa_msg_send(current_vm_id, vm->id, skb->len, 0);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000598 spin_unlock_irqrestore(&hf_send_lock, flags);
599
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100600 if (ret.func == FFA_ERROR_32) {
Andrew Walbranb040b302019-10-10 13:50:06 +0100601 switch (ret.arg2) {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100602 case FFA_INVALID_PARAMETERS:
Andrew Walbrancafe0172019-10-07 14:14:05 +0100603 return -ENXIO;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100604 case FFA_NOT_SUPPORTED:
Andrew Walbrancafe0172019-10-07 14:14:05 +0100605 return -EIO;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100606 case FFA_DENIED:
607 case FFA_BUSY:
Andrew Walbrancafe0172019-10-07 14:14:05 +0100608 default:
609 return -EAGAIN;
610 }
611 }
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000612
Andrew Scull71f57362019-02-05 16:11:35 +0000613 /* Ensure the VM will run to pick up the message. */
614 hf_deliver_message(vm->id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000615
616 kfree_skb(skb);
617
618 return 0;
619}
620
621/**
622 * Determines if the given socket is in the connected state. It acquires and
623 * releases the socket lock.
624 */
625static bool hf_sock_is_connected(struct socket *sock)
626{
627 bool ret;
628
629 lock_sock(sock->sk);
630 ret = sock->state == SS_CONNECTED;
631 release_sock(sock->sk);
632
633 return ret;
634}
635
636/**
637 * Sends a message to the VM & port the socket is connected to. All variants
638 * of write/send/sendto/sendmsg eventually call this function.
639 */
640static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
641{
642 struct sock *sk = sock->sk;
643 struct sk_buff *skb;
644 int err;
645 struct hf_msg_hdr *hdr;
646 struct hf_sock *hsock = hsock_from_sk(sk);
Andrew Walbrancafe0172019-10-07 14:14:05 +0100647 size_t payload_max_len = HF_MAILBOX_SIZE - sizeof(struct hf_msg_hdr);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000648
649 /* Check length. */
Andrew Scull614ed7f2019-04-01 12:12:38 +0100650 if (len > payload_max_len)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000651 return -EMSGSIZE;
652
653 /* We don't allow the destination address to be specified. */
654 if (m->msg_namelen > 0)
655 return -EISCONN;
656
657 /* We don't support out of band messages. */
658 if (m->msg_flags & MSG_OOB)
659 return -EOPNOTSUPP;
660
661 /*
662 * Ensure that the socket is connected. We don't need to hold the socket
663 * lock (acquired and released by hf_sock_is_connected) for the
664 * remainder of the function because the fields we care about are
665 * immutable once the state is SS_CONNECTED.
666 */
667 if (!hf_sock_is_connected(sock))
668 return -ENOTCONN;
669
670 /*
671 * Allocate an skb for this write. If there isn't enough room in the
672 * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block
673 * (if it's a blocking call). On success, it increments sk_wmem_alloc
674 * and sets up the skb such that sk_wmem_alloc gets decremented when
675 * the skb is freed (sock_wfree gets called).
676 */
677 skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr),
678 m->msg_flags & MSG_DONTWAIT, &err);
679 if (!skb)
680 return err;
681
682 /* Reserve room for the header and initialise it. */
683 skb_reserve(skb, sizeof(struct hf_msg_hdr));
684 hdr = skb_push(skb, sizeof(struct hf_msg_hdr));
685 hdr->src_port = hsock->local_port;
686 hdr->dst_port = hsock->remote_port;
687
688 /* Allocate area for the contents, then copy into skb. */
689 if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) {
690 err = -EFAULT;
691 goto err_cleanup;
692 }
693
694 /*
695 * TODO: We currently do this inline, but when we have support for
696 * readiness notification from Hafnium, we must add this to a per-VM tx
697 * queue that can make progress when the VM becomes writable. This will
698 * fix send buffering and poll readiness notification.
699 */
700 err = hf_send_skb(skb);
701 if (err)
702 goto err_cleanup;
703
704 return 0;
705
706err_cleanup:
707 kfree_skb(skb);
708 return err;
709}
710
711/**
712 * Receives a message originated from the VM & port the socket is connected to.
713 * All variants of read/recv/recvfrom/recvmsg eventually call this function.
714 */
715static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
716 int flags)
717{
718 struct sock *sk = sock->sk;
719 struct sk_buff *skb;
720 int err;
721 size_t copy_len;
722
723 if (!hf_sock_is_connected(sock))
724 return -ENOTCONN;
725
726 /* Grab the next skb from the receive queue. */
727 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
728 if (!skb)
729 return err;
730
731 /* Make sure we don't copy more than what fits in the output buffer. */
732 copy_len = skb->len;
733 if (copy_len > len) {
734 copy_len = len;
735 m->msg_flags |= MSG_TRUNC;
736 }
737
738 /* Make sure we don't overflow the return value type. */
739 if (copy_len > INT_MAX) {
740 copy_len = INT_MAX;
741 m->msg_flags |= MSG_TRUNC;
742 }
743
744 /* Copy skb to output iterator, then free it. */
745 err = skb_copy_datagram_msg(skb, 0, m, copy_len);
746 skb_free_datagram(sk, skb);
747 if (err)
748 return err;
749
750 return copy_len;
751}
752
753/**
754 * This function is called when a Hafnium socket is created. It initialises all
755 * state such that the caller will be able to connect the socket and then send
756 * and receive messages through it.
757 */
758static int hf_sock_create(struct net *net, struct socket *sock, int protocol,
Andrew Scull01778112019-01-14 15:37:53 +0000759 int kern)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000760{
761 static const struct proto_ops ops = {
762 .family = PF_HF,
763 .owner = THIS_MODULE,
764 .release = hf_sock_release,
765 .bind = sock_no_bind,
766 .connect = hf_sock_connect,
767 .socketpair = sock_no_socketpair,
768 .accept = sock_no_accept,
769 .ioctl = sock_no_ioctl,
770 .listen = sock_no_listen,
771 .shutdown = sock_no_shutdown,
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000772 .sendmsg = hf_sock_sendmsg,
773 .recvmsg = hf_sock_recvmsg,
774 .mmap = sock_no_mmap,
775 .sendpage = sock_no_sendpage,
776 .poll = datagram_poll,
777 };
778 struct sock *sk;
779
780 if (sock->type != SOCK_DGRAM)
781 return -ESOCKTNOSUPPORT;
782
783 if (protocol != 0)
784 return -EPROTONOSUPPORT;
785
786 /*
787 * For now we only allow callers with sys admin capability to create
788 * Hafnium sockets.
789 */
790 if (!capable(CAP_SYS_ADMIN))
791 return -EPERM;
792
793 /* Allocate and initialise socket. */
794 sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern);
795 if (!sk)
796 return -ENOMEM;
797
798 sock_init_data(sock, sk);
799
800 sk->sk_destruct = hf_sock_destruct;
801 sock->ops = &ops;
802 sock->state = SS_UNCONNECTED;
803
804 return 0;
805}
806
807/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100808 * Frees all resources, including threads, associated with the Hafnium driver.
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100809 */
Andrew Scull82257c42018-10-01 10:37:48 +0100810static void hf_free_resources(void)
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100811{
Fuad Tabba5da4b6b2019-08-05 13:56:20 +0100812 uint16_t i;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100813 ffa_vcpu_index_t j;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100814
815 /*
816 * First stop all worker threads. We need to do this before freeing
817 * resources because workers may reference each other, so it is only
818 * safe to free resources after they have all stopped.
819 */
Andrew Scull82257c42018-10-01 10:37:48 +0100820 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100821 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000822
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100823 for (j = 0; j < vm->vcpu_count; j++)
824 kthread_stop(vm->vcpu[j].task);
825 }
826
827 /* Free resources. */
Andrew Scull82257c42018-10-01 10:37:48 +0100828 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100829 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000830
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100831 for (j = 0; j < vm->vcpu_count; j++)
832 put_task_struct(vm->vcpu[j].task);
833 kfree(vm->vcpu);
834 }
835
836 kfree(hf_vms);
Andrew Walbran81cf04c2020-08-06 16:00:43 +0100837
838 ffa_rx_release();
Olivier Deprez77b1c7f2022-07-05 10:29:01 +0200839 ffa_rxtx_unmap();
Andrew Walbran81cf04c2020-08-06 16:00:43 +0100840 if (hf_send_page) {
841 __free_page(hf_send_page);
842 hf_send_page = NULL;
843 }
844 if (hf_recv_page) {
845 __free_page(hf_recv_page);
846 hf_recv_page = NULL;
847 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100848}
849
Andrew Scullbb7ae412018-09-28 21:07:15 +0100850/**
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000851 * Handles the hypervisor timer interrupt.
852 */
853static irqreturn_t hf_nop_irq_handler(int irq, void *dev)
854{
855 /*
856 * No need to do anything, the interrupt only exists to return to the
857 * primary vCPU so that the virtual timer will be restored and fire as
858 * normal.
859 */
860 return IRQ_HANDLED;
861}
862
863/**
864 * Enables the hypervisor timer interrupt on a CPU, when it starts or after the
865 * driver is first loaded.
866 */
867static int hf_starting_cpu(unsigned int cpu)
868{
869 if (hf_irq != 0) {
870 /* Enable the interrupt, and set it to be edge-triggered. */
871 enable_percpu_irq(hf_irq, IRQ_TYPE_EDGE_RISING);
872 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000873
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000874 return 0;
875}
876
877/**
878 * Disables the hypervisor timer interrupt on a CPU when it is powered down.
879 */
880static int hf_dying_cpu(unsigned int cpu)
881{
882 if (hf_irq != 0) {
883 /* Disable the interrupt while the CPU is asleep. */
884 disable_percpu_irq(hf_irq);
885 }
886
887 return 0;
888}
889
890/**
891 * Registers for the hypervisor timer interrupt.
892 */
893static int hf_int_driver_probe(struct platform_device *pdev)
894{
895 int irq;
896 int ret;
897
898 /*
899 * Register a handler for the hyperviser timer IRQ, as it is needed for
900 * Hafnium to emulate the virtual timer for Linux while a secondary vCPU
901 * is running.
902 */
903 irq = platform_get_irq(pdev, ARCH_TIMER_HYP_PPI);
904 if (irq < 0) {
905 pr_err("Error getting hypervisor timer IRQ: %d\n", irq);
906 return irq;
907 }
908 hf_irq = irq;
909
910 ret = request_percpu_irq(irq, hf_nop_irq_handler, HYPERVISOR_TIMER_NAME,
911 pdev);
912 if (ret != 0) {
913 pr_err("Error registering hypervisor timer IRQ %d: %d\n",
914 irq, ret);
915 return ret;
916 }
917 pr_info("Hafnium registered for IRQ %d\n", irq);
918 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
919 "hafnium/hypervisor_timer:starting",
920 hf_starting_cpu, hf_dying_cpu);
921 if (ret < 0) {
922 pr_err("Error enabling timer on all CPUs: %d\n", ret);
Andrew Walbran8d55e502019-02-05 11:42:08 +0000923 free_percpu_irq(irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000924 return ret;
925 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000926 hf_cpuhp_state = ret;
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000927
928 return 0;
929}
930
931/**
932 * Unregisters for the hypervisor timer interrupt.
933 */
934static int hf_int_driver_remove(struct platform_device *pdev)
935{
Andrew Walbran8d55e502019-02-05 11:42:08 +0000936 /*
937 * This will cause hf_dying_cpu to be called on each CPU, which will
938 * disable the IRQs.
939 */
940 cpuhp_remove_state(hf_cpuhp_state);
941 free_percpu_irq(hf_irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000942
943 return 0;
944}
945
946static const struct of_device_id hf_int_driver_id[] = {
947 {.compatible = "arm,armv7-timer"},
948 {.compatible = "arm,armv8-timer"},
949 {}
950};
951
952static struct platform_driver hf_int_driver = {
953 .driver = {
954 .name = HYPERVISOR_TIMER_NAME,
955 .owner = THIS_MODULE,
956 .of_match_table = of_match_ptr(hf_int_driver_id),
957 },
958 .probe = hf_int_driver_probe,
959 .remove = hf_int_driver_remove,
960};
961
962/**
Andrew Walbran6b796192020-08-06 16:01:59 +0100963 * Print the error code of the given FF-A value if it is an error, or the
964 * function ID otherwise.
965 */
966static void print_ffa_error(struct ffa_value ffa_ret)
967{
968 if (ffa_ret.func == FFA_ERROR_32)
969 pr_err("FF-A error code %d\n", ffa_ret.arg2);
970 else
971 pr_err("Unexpected FF-A function %#x\n", ffa_ret.func);
972}
973
974/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100975 * Initializes the Hafnium driver by creating a thread for each vCPU of each
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100976 * virtual machine.
977 */
978static int __init hf_init(void)
979{
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000980 static const struct net_proto_family proto_family = {
981 .family = PF_HF,
982 .create = hf_sock_create,
983 .owner = THIS_MODULE,
984 };
Andrew Scullbb7ae412018-09-28 21:07:15 +0100985 int64_t ret;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100986 struct ffa_value ffa_ret;
J-Alvesc653e722023-08-02 13:08:54 +0100987 ffa_id_t i;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100988 ffa_vcpu_index_t j;
Andrew Walbran6b796192020-08-06 16:01:59 +0100989 struct ffa_uuid null_uuid;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100990 ffa_vm_count_t secondary_vm_count;
Andrew Walbran6b796192020-08-06 16:01:59 +0100991 const struct ffa_partition_info *partition_info;
Andrew Scull82257c42018-10-01 10:37:48 +0100992 uint32_t total_vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100993
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100994 /* Allocate a page for send and receive buffers. */
995 hf_send_page = alloc_page(GFP_KERNEL);
996 if (!hf_send_page) {
997 pr_err("Unable to allocate send buffer\n");
998 return -ENOMEM;
999 }
1000
1001 hf_recv_page = alloc_page(GFP_KERNEL);
1002 if (!hf_recv_page) {
1003 __free_page(hf_send_page);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001004 hf_send_page = NULL;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +01001005 pr_err("Unable to allocate receive buffer\n");
1006 return -ENOMEM;
1007 }
1008
1009 /*
Olivier Deprez77b1c7f2022-07-05 10:29:01 +02001010 * Map RX/TX buffers to hypervisor.
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +01001011 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +01001012 ffa_ret = ffa_rxtx_map(page_to_phys(hf_send_page),
Andrew Walbran2c6e7512019-11-05 14:02:29 +00001013 page_to_phys(hf_recv_page));
Andrew Walbran196ed0e2020-04-30 11:32:29 +01001014 if (ffa_ret.func != FFA_SUCCESS_32) {
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001015 pr_err("Unable to configure VM mailbox.\n");
Andrew Walbran6b796192020-08-06 16:01:59 +01001016 print_ffa_error(ffa_ret);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001017 ret = -EIO;
1018 goto fail_with_cleanup;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +01001019 }
1020
Andrew Walbran6b796192020-08-06 16:01:59 +01001021 /* Get information about secondary VMs. */
1022 ffa_uuid_init(0, 0, 0, 0, &null_uuid);
Daniel Boulby32d76982021-12-13 17:57:57 +00001023 ffa_ret = ffa_partition_info_get(&null_uuid, 0);
Andrew Walbran6b796192020-08-06 16:01:59 +01001024 if (ffa_ret.func != FFA_SUCCESS_32) {
1025 pr_err("Unable to get VM information.\n");
1026 print_ffa_error(ffa_ret);
1027 ret = -EIO;
1028 goto fail_with_cleanup;
1029 }
1030 secondary_vm_count = ffa_ret.arg2 - 1;
1031 partition_info = page_address(hf_recv_page);
Andrew Scull82257c42018-10-01 10:37:48 +01001032
1033 /* Confirm the maximum number of VMs looks sane. */
1034 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS < 1);
1035 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS > U16_MAX);
1036
1037 /* Validate the number of VMs. There must at least be the primary. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001038 if (secondary_vm_count > CONFIG_HAFNIUM_MAX_VMS - 1) {
Fuad Tabba8523ccd2019-07-31 15:37:29 +01001039 pr_err("Number of VMs is out of range: %d\n",
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001040 secondary_vm_count);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001041 ret = -EDQUOT;
1042 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001043 }
1044
Andrew Scullb722f952018-09-27 15:39:10 +01001045 /* Only track the secondary VMs. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001046 hf_vms = kmalloc_array(secondary_vm_count, sizeof(struct hf_vm),
1047 GFP_KERNEL);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001048 if (!hf_vms) {
1049 ret = -ENOMEM;
1050 goto fail_with_cleanup;
1051 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001052
Jose Marinho1cc6c752019-03-11 16:28:03 +00001053 /* Cache the VM id for later usage. */
1054 current_vm_id = hf_vm_get_id();
1055
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001056 /* Initialize each VM. */
Andrew Scull82257c42018-10-01 10:37:48 +01001057 total_vcpu_count = 0;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001058 for (i = 0; i < secondary_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001059 struct hf_vm *vm = &hf_vms[i];
Andrew Walbran196ed0e2020-04-30 11:32:29 +01001060 ffa_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001061
Andrew Walbran6b796192020-08-06 16:01:59 +01001062 /* Adjust the index as only the secondaries are tracked. */
1063 vm->id = partition_info[i + 1].vm_id;
1064 vcpu_count = partition_info[i + 1].vcpu_count;
Andrew Scull82257c42018-10-01 10:37:48 +01001065
1066 /* Avoid overflowing the vcpu count. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001067 if (vcpu_count > (U32_MAX - total_vcpu_count)) {
Andrew Scull82257c42018-10-01 10:37:48 +01001068 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1069 ret = -EDQUOT;
1070 goto fail_with_cleanup;
1071 }
1072
1073 /* Confirm the maximum number of VCPUs looks sane. */
1074 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1);
1075 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX);
1076
1077 /* Enforce the limit on vcpus. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001078 total_vcpu_count += vcpu_count;
Andrew Scull82257c42018-10-01 10:37:48 +01001079 if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) {
1080 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1081 ret = -EDQUOT;
1082 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001083 }
1084
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001085 vm->vcpu_count = vcpu_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001086 vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu),
1087 GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001088 if (!vm->vcpu) {
Andrew Scull82257c42018-10-01 10:37:48 +01001089 ret = -ENOMEM;
1090 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001091 }
1092
Andrew Scull82257c42018-10-01 10:37:48 +01001093 /* Update the number of initialized VMs. */
1094 hf_vm_count = i + 1;
1095
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001096 /* Create a kernel thread for each vcpu. */
1097 for (j = 0; j < vm->vcpu_count; j++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001098 struct hf_vcpu *vcpu = &vm->vcpu[j];
Andrew Scull01778112019-01-14 15:37:53 +00001099
1100 vcpu->task =
1101 kthread_create(hf_vcpu_thread, vcpu,
1102 "vcpu_thread_%u_%u", vm->id, j);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001103 if (IS_ERR(vcpu->task)) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001104 pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n",
1105 vm->id, j, PTR_ERR(vcpu->task));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001106 vm->vcpu_count = j;
Andrew Scull82257c42018-10-01 10:37:48 +01001107 ret = PTR_ERR(vcpu->task);
1108 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001109 }
1110
1111 get_task_struct(vcpu->task);
Andrew Scullb722f952018-09-27 15:39:10 +01001112 vcpu->vm = vm;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001113 vcpu->vcpu_index = j;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +00001114 atomic_set(&vcpu->abort_sleep, 0);
Andrew Scullece5ef42019-05-08 15:07:25 +01001115 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001116 }
1117 }
1118
Andrew Walbran6b796192020-08-06 16:01:59 +01001119 ffa_ret = ffa_rx_release();
1120 if (ffa_ret.func != FFA_SUCCESS_32) {
1121 pr_err("Unable to release RX buffer.\n");
1122 print_ffa_error(ffa_ret);
1123 ret = -EIO;
1124 goto fail_with_cleanup;
1125 }
1126
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001127 /* Register protocol and socket family. */
1128 ret = proto_register(&hf_sock_proto, 0);
1129 if (ret) {
1130 pr_err("Unable to register protocol: %lld\n", ret);
1131 goto fail_with_cleanup;
1132 }
1133
1134 ret = sock_register(&proto_family);
1135 if (ret) {
1136 pr_err("Unable to register Hafnium's socket family: %lld\n",
1137 ret);
1138 goto fail_unregister_proto;
1139 }
1140
1141 /*
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001142 * Register as a driver for the timer device, so we can register a
1143 * handler for the hyperviser timer IRQ.
1144 */
1145 ret = platform_driver_register(&hf_int_driver);
1146 if (ret != 0) {
1147 pr_err("Error registering timer driver %lld\n", ret);
1148 goto fail_unregister_socket;
1149 }
1150
1151 /*
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001152 * Start running threads now that all is initialized.
1153 *
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001154 * Any failures from this point on must also unregister the driver with
1155 * platform_driver_unregister().
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001156 */
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001157 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001158 struct hf_vm *vm = &hf_vms[i];
Andrew Scull01778112019-01-14 15:37:53 +00001159
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001160 for (j = 0; j < vm->vcpu_count; j++)
1161 wake_up_process(vm->vcpu[j].task);
1162 }
1163
1164 /* Dump vm/vcpu count info. */
Andrew Scullbb7ae412018-09-28 21:07:15 +01001165 pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001166 for (i = 0; i < hf_vm_count; i++) {
1167 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001168
Andrew Scullbb7ae412018-09-28 21:07:15 +01001169 pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001170 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001171
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001172 return 0;
Andrew Scull82257c42018-10-01 10:37:48 +01001173
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001174fail_unregister_socket:
1175 sock_unregister(PF_HF);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001176fail_unregister_proto:
1177 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001178fail_with_cleanup:
1179 hf_free_resources();
1180 return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001181}
1182
1183/**
Andrew Scullbb7ae412018-09-28 21:07:15 +01001184 * Frees up all resources used by the Hafnium driver in preparation for
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001185 * unloading it.
1186 */
1187static void __exit hf_exit(void)
1188{
Andrew Scullbb7ae412018-09-28 21:07:15 +01001189 pr_info("Preparing to unload Hafnium\n");
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001190 sock_unregister(PF_HF);
1191 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001192 hf_free_resources();
Andrew Walbran8d55e502019-02-05 11:42:08 +00001193 platform_driver_unregister(&hf_int_driver);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001194 pr_info("Hafnium ready to unload\n");
1195}
1196
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001197MODULE_LICENSE("GPL v2");
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001198
1199module_init(hf_init);
1200module_exit(hf_exit);