blob: bb3bcdbf389d2e6af1c558388a68a709257d184a [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>
Andrew Scull55704232018-08-10 17:19:54 +010036
Fuad Tabba3e669bc2019-08-08 16:43:55 +010037#include "uapi/hf/socket.h"
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000038
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000039#define HYPERVISOR_TIMER_NAME "el2_timer"
40
Andrew Scull82257c42018-10-01 10:37:48 +010041#define CONFIG_HAFNIUM_MAX_VMS 16
42#define CONFIG_HAFNIUM_MAX_VCPUS 32
43
Olivier Deprezf441ed02020-08-11 15:50:23 +020044#define HF_VM_ID_BASE 0
Fuad Tabba5da4b6b2019-08-05 13:56:20 +010045#define FIRST_SECONDARY_VM_ID (HF_VM_ID_OFFSET + 1)
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000046
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010047struct hf_vcpu {
Andrew Scullb722f952018-09-27 15:39:10 +010048 struct hf_vm *vm;
Andrew Walbran196ed0e2020-04-30 11:32:29 +010049 ffa_vcpu_index_t vcpu_index;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010050 struct task_struct *task;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +000051 atomic_t abort_sleep;
Andrew Scull71f57362019-02-05 16:11:35 +000052 atomic_t waiting_for_message;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010053 struct hrtimer timer;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010054};
55
56struct hf_vm {
Andrew Walbran196ed0e2020-04-30 11:32:29 +010057 ffa_vm_id_t id;
58 ffa_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010059 struct hf_vcpu *vcpu;
60};
61
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000062struct hf_sock {
63 /* This needs to be the first field. */
64 struct sock sk;
65
66 /*
67 * The following fields are immutable after the socket transitions to
68 * SS_CONNECTED state.
69 */
70 uint64_t local_port;
71 uint64_t remote_port;
72 struct hf_vm *peer_vm;
73};
74
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000075static struct proto hf_sock_proto = {
76 .name = "hafnium",
77 .owner = THIS_MODULE,
78 .obj_size = sizeof(struct hf_sock),
79};
80
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010081static struct hf_vm *hf_vms;
Andrew Walbran196ed0e2020-04-30 11:32:29 +010082static ffa_vm_count_t hf_vm_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000083static struct page *hf_send_page;
84static struct page *hf_recv_page;
85static atomic64_t hf_next_port = ATOMIC64_INIT(0);
86static DEFINE_SPINLOCK(hf_send_lock);
87static DEFINE_HASHTABLE(hf_local_port_hash, 7);
88static DEFINE_SPINLOCK(hf_local_port_hash_lock);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000089static int hf_irq;
Andrew Walbran8d55e502019-02-05 11:42:08 +000090static enum cpuhp_state hf_cpuhp_state;
Andrew Walbran196ed0e2020-04-30 11:32:29 +010091static ffa_vm_id_t current_vm_id;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010092
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010093/**
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000094 * Retrieves a VM from its ID, returning NULL if the VM doesn't exist.
95 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +010096static struct hf_vm *hf_vm_from_id(ffa_vm_id_t vm_id)
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000097{
98 if (vm_id < FIRST_SECONDARY_VM_ID ||
99 vm_id >= FIRST_SECONDARY_VM_ID + hf_vm_count)
100 return NULL;
101
102 return &hf_vms[vm_id - FIRST_SECONDARY_VM_ID];
103}
104
105/**
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000106 * Wakes up the kernel thread responsible for running the given vcpu.
107 *
108 * Returns 0 if the thread was already running, 1 otherwise.
109 */
110static int hf_vcpu_wake_up(struct hf_vcpu *vcpu)
111{
112 /* Set a flag indicating that the thread should not go to sleep. */
113 atomic_set(&vcpu->abort_sleep, 1);
114
115 /* Set the thread to running state. */
116 return wake_up_process(vcpu->task);
117}
118
119/**
120 * Puts the current thread to sleep. The current thread must be responsible for
121 * running the given vcpu.
122 *
123 * Going to sleep will fail if hf_vcpu_wake_up() or kthread_stop() was called on
124 * this vcpu/thread since the last time it [re]started running.
125 */
126static void hf_vcpu_sleep(struct hf_vcpu *vcpu)
127{
128 int abort;
129
130 set_current_state(TASK_INTERRUPTIBLE);
131
132 /* Check the sleep-abort flag after making thread interruptible. */
133 abort = atomic_read(&vcpu->abort_sleep);
134 if (!abort && !kthread_should_stop())
135 schedule();
136
137 /* Set state back to running on the way out. */
138 set_current_state(TASK_RUNNING);
139}
140
141/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100142 * Wakes up the thread associated with the vcpu that owns the given timer. This
143 * is called when the timer the thread is waiting on expires.
144 */
145static enum hrtimer_restart hf_vcpu_timer_expired(struct hrtimer *timer)
146{
147 struct hf_vcpu *vcpu = container_of(timer, struct hf_vcpu, timer);
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000148 /* TODO: Inject interrupt. */
149 hf_vcpu_wake_up(vcpu);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100150 return HRTIMER_NORESTART;
151}
152
153/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000154 * This function is called when Hafnium requests that the primary VM wake up a
155 * vCPU that belongs to a secondary VM.
156 *
157 * 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 +0000158 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100159static void hf_handle_wake_up_request(ffa_vm_id_t vm_id,
160 ffa_vcpu_index_t vcpu)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000161{
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000162 struct hf_vm *vm = hf_vm_from_id(vm_id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000163
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000164 if (!vm) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000165 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
166 return;
167 }
168
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000169 if (vcpu >= vm->vcpu_count) {
Andrew Scull71f57362019-02-05 16:11:35 +0000170 pr_warn("Request to wake up non-existent vCPU: %u.%u\n",
171 vm_id, vcpu);
172 return;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000173 }
174
175 if (hf_vcpu_wake_up(&vm->vcpu[vcpu]) == 0) {
176 /*
177 * The task was already running (presumably on a different
178 * physical CPU); interrupt it. This gives Hafnium a chance to
179 * inject any new interrupts.
180 */
181 kick_process(vm->vcpu[vcpu].task);
182 }
183}
184
185/**
Andrew Scull71f57362019-02-05 16:11:35 +0000186 * Injects an interrupt into a vCPU of the VM and ensures the vCPU will run to
187 * handle the interrupt.
188 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100189static void hf_interrupt_vm(ffa_vm_id_t vm_id, uint64_t int_id)
Andrew Scull71f57362019-02-05 16:11:35 +0000190{
191 struct hf_vm *vm = hf_vm_from_id(vm_id);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100192 ffa_vcpu_index_t vcpu;
Andrew Scull71f57362019-02-05 16:11:35 +0000193 int64_t ret;
194
195 if (!vm) {
196 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
197 return;
198 }
199
200 /*
201 * TODO: For now we're picking the first vcpu to interrupt, but
202 * we want to be smarter.
203 */
204 vcpu = 0;
205 ret = hf_interrupt_inject(vm_id, vcpu, int_id);
206
207 if (ret == -1) {
208 pr_warn("Failed to inject interrupt %lld to vCPU %d of VM %d",
209 int_id, vcpu, vm_id);
210 return;
211 }
212
213 if (ret != 1) {
214 /* We don't need to wake up the vcpu. */
215 return;
216 }
217
218 hf_handle_wake_up_request(vm_id, vcpu);
219}
220
221/**
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000222 * Notify all waiters on the given VM.
223 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100224static void hf_notify_waiters(ffa_vm_id_t vm_id)
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000225{
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100226 ffa_vm_id_t waiter_vm_id;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000227
Andrew Scull71f57362019-02-05 16:11:35 +0000228 while ((waiter_vm_id = hf_mailbox_waiter_get(vm_id)) != -1) {
229 if (waiter_vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000230 /*
231 * TODO: Use this information when implementing per-vm
232 * queues.
233 */
234 } else {
Andrew Scull71f57362019-02-05 16:11:35 +0000235 hf_interrupt_vm(waiter_vm_id,
236 HF_MAILBOX_WRITABLE_INTID);
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000237 }
238 }
239}
240
241/**
Andrew Scull71f57362019-02-05 16:11:35 +0000242 * Delivers a message to a VM.
243 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100244static void hf_deliver_message(ffa_vm_id_t vm_id)
Andrew Scull71f57362019-02-05 16:11:35 +0000245{
246 struct hf_vm *vm = hf_vm_from_id(vm_id);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100247 ffa_vcpu_index_t i;
Andrew Scull71f57362019-02-05 16:11:35 +0000248
249 if (!vm) {
250 pr_warn("Tried to deliver message to non-existent VM id: %u\n",
251 vm_id);
252 return;
253 }
254
255 /* Try to wake a vCPU that is waiting for a message. */
256 for (i = 0; i < vm->vcpu_count; i++) {
257 if (atomic_read(&vm->vcpu[i].waiting_for_message)) {
258 hf_handle_wake_up_request(vm->id,
259 vm->vcpu[i].vcpu_index);
260 return;
261 }
262 }
263
264 /* None were waiting for a message so interrupt one. */
265 hf_interrupt_vm(vm->id, HF_MAILBOX_READABLE_INTID);
266}
267
268/**
Andrew Sculldf6478f2019-02-19 17:52:08 +0000269 * Handles a message delivered to this VM by validating that it's well-formed
270 * and then queueing it for delivery to the appropriate socket.
271 */
Andrew Walbranb331fa92019-10-03 16:48:07 +0100272static void hf_handle_message(struct hf_vm *sender, size_t len,
Andrew Walbrancafe0172019-10-07 14:14:05 +0100273 const void *message)
Andrew Sculldf6478f2019-02-19 17:52:08 +0000274{
275 struct hf_sock *hsock;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100276 const struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)message;
Andrew Sculldf6478f2019-02-19 17:52:08 +0000277 struct sk_buff *skb;
278 int err;
279
280 /* Ignore messages that are too small to hold a header. */
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000281 if (len < sizeof(struct hf_msg_hdr)) {
282 pr_err("Message received without header of length %d\n", len);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100283 ffa_rx_release();
Andrew Sculldf6478f2019-02-19 17:52:08 +0000284 return;
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000285 }
Andrew Sculldf6478f2019-02-19 17:52:08 +0000286
287 len -= sizeof(struct hf_msg_hdr);
288
289 /* Go through the colliding sockets. */
290 rcu_read_lock();
291 hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node,
292 hdr->dst_port) {
293 if (hsock->peer_vm == sender &&
294 hsock->remote_port == hdr->src_port) {
295 sock_hold(&hsock->sk);
296 break;
297 }
298 }
299 rcu_read_unlock();
300
301 /* Nothing to do if we couldn't find the target. */
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000302 if (!hsock) {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100303 ffa_rx_release();
Andrew Sculldf6478f2019-02-19 17:52:08 +0000304 return;
Marc Bonnici39fdefd2019-11-08 15:05:07 +0000305 }
Andrew Sculldf6478f2019-02-19 17:52:08 +0000306
307 /*
308 * TODO: From this point on, there are two failure paths: when we
309 * create the skb below, and when we enqueue it to the socket. What
310 * should we do if they fail? Ideally we would have some form of flow
311 * control to prevent message loss, but how to do it efficiently?
312 *
313 * One option is to have a pre-allocated message that indicates to the
314 * sender that a message was dropped. This way we guarantee that the
315 * sender will be aware of loss and should back-off.
316 */
317 /* Create the skb. */
318 skb = alloc_skb(len, GFP_KERNEL);
319 if (!skb)
320 goto exit;
321
322 memcpy(skb_put(skb, len), hdr + 1, len);
323
324 /*
325 * Add the skb to the receive queue of the target socket. On success it
326 * calls sk->sk_data_ready, which is currently set to sock_def_readable,
327 * which wakes up any waiters.
328 */
329 err = sock_queue_rcv_skb(&hsock->sk, skb);
330 if (err)
331 kfree_skb(skb);
332
333exit:
334 sock_put(&hsock->sk);
Andrew Scull71f57362019-02-05 16:11:35 +0000335
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100336 if (ffa_rx_release().func == FFA_RX_RELEASE_32)
Andrew Scull71f57362019-02-05 16:11:35 +0000337 hf_notify_waiters(HF_PRIMARY_VM_ID);
Andrew Sculldf6478f2019-02-19 17:52:08 +0000338}
339
340/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100341 * This is the main loop of each vcpu.
342 */
343static int hf_vcpu_thread(void *data)
344{
345 struct hf_vcpu *vcpu = data;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100346 struct ffa_value ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100347
348 hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
349 vcpu->timer.function = &hf_vcpu_timer_expired;
350
351 while (!kthread_should_stop()) {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100352 ffa_vcpu_index_t i;
Andrew Scull01f83de2019-01-23 13:41:47 +0000353
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000354 /*
355 * We're about to run the vcpu, so we can reset the abort-sleep
356 * flag.
357 */
358 atomic_set(&vcpu->abort_sleep, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100359
Andrew Scullbb7ae412018-09-28 21:07:15 +0100360 /* Call into Hafnium to run vcpu. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100361 ret = ffa_run(vcpu->vm->id, vcpu->vcpu_index);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100362
Andrew Walbran39bf7892019-11-01 14:14:47 +0000363 switch (ret.func) {
Andrew Sculle05702e2019-01-08 14:46:46 +0000364 /* Preempted. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100365 case FFA_INTERRUPT_32:
Andrew Sculle05702e2019-01-08 14:46:46 +0000366 if (need_resched())
367 schedule();
368 break;
369
370 /* Yield. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100371 case FFA_YIELD_32:
Andrew Sculle05702e2019-01-08 14:46:46 +0000372 if (!kthread_should_stop())
373 schedule();
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100374 break;
375
Andrew Scull01778112019-01-14 15:37:53 +0000376 /* WFI. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100377 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
378 if (ret.arg2 != FFA_SLEEP_INDEFINITE) {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000379 hrtimer_start(&vcpu->timer, ret.arg2,
Andrew Scull71f57362019-02-05 16:11:35 +0000380 HRTIMER_MODE_REL);
381 }
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000382 hf_vcpu_sleep(vcpu);
Andrew Scull71f57362019-02-05 16:11:35 +0000383 hrtimer_cancel(&vcpu->timer);
384 break;
385
386 /* Waiting for a message. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100387 case FFA_MSG_WAIT_32:
Andrew Scull71f57362019-02-05 16:11:35 +0000388 atomic_set(&vcpu->waiting_for_message, 1);
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100389 if (ret.arg2 != FFA_SLEEP_INDEFINITE) {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000390 hrtimer_start(&vcpu->timer, ret.arg2,
Andrew Scull71f57362019-02-05 16:11:35 +0000391 HRTIMER_MODE_REL);
392 }
393 hf_vcpu_sleep(vcpu);
394 hrtimer_cancel(&vcpu->timer);
395 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100396 break;
397
Andrew Scullb3a61b52018-09-17 14:30:34 +0100398 /* Wake up another vcpu. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100399 case HF_FFA_RUN_WAKE_UP:
400 hf_handle_wake_up_request(ffa_vm_id(ret),
401 ffa_vcpu_index(ret));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100402 break;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100403
Andrew Scullb3a61b52018-09-17 14:30:34 +0100404 /* Response available. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100405 case FFA_MSG_SEND_32:
406 if (ffa_msg_send_receiver(ret) == HF_PRIMARY_VM_ID) {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000407 hf_handle_message(vcpu->vm,
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100408 ffa_msg_send_size(ret),
Andrew Scull94704232019-04-01 12:36:37 +0100409 page_address(hf_recv_page));
Andrew Scull71f57362019-02-05 16:11:35 +0000410 } else {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100411 hf_deliver_message(ffa_msg_send_receiver(ret));
Andrew Scull71f57362019-02-05 16:11:35 +0000412 }
Andrew Sculldc8cab52018-10-10 18:29:39 +0100413 break;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000414
415 /* Notify all waiters. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100416 case FFA_RX_RELEASE_32:
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000417 hf_notify_waiters(vcpu->vm->id);
418 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000419
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100420 case FFA_ERROR_32:
421 pr_warn("FF-A error %d running VM %d vCPU %d", ret.arg2,
Andrew Walbran18f08a62019-11-13 11:57:52 +0000422 vcpu->vm->id, vcpu->vcpu_index);
Andrew Walbran39bf7892019-11-01 14:14:47 +0000423 switch (ret.arg2) {
Andrew Walbran9abce272019-11-27 18:41:05 +0000424 /* Abort was triggered. */
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100425 case FFA_ABORTED:
Andrew Walbran39bf7892019-11-01 14:14:47 +0000426 for (i = 0; i < vcpu->vm->vcpu_count; i++) {
427 if (i == vcpu->vcpu_index)
428 continue;
429 hf_handle_wake_up_request(vcpu->vm->id,
430 i);
431 }
432 hf_vcpu_sleep(vcpu);
433 break;
Andrew Walbran9abce272019-11-27 18:41:05 +0000434 default:
435 /* Treat as a yield and try again later. */
436 if (!kthread_should_stop())
437 schedule();
438 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000439 }
Andrew Scull01f83de2019-01-23 13:41:47 +0000440 break;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100441 }
442 }
443
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100444 return 0;
445}
446
447/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000448 * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It
449 * relies on the fact that the first field of hf_sock is a sock.
450 */
451static struct hf_sock *hsock_from_sk(struct sock *sk)
452{
453 return (struct hf_sock *)sk;
454}
455
456/**
457 * This is called when the last reference to the outer socket is released. For
458 * example, if it's a user-space socket, when the last file descriptor pointing
459 * to this socket is closed.
460 *
461 * It begins cleaning up resources, though some can only be cleaned up after all
462 * references to the underlying socket are released, which is handled by
463 * hf_sock_destruct().
464 */
465static int hf_sock_release(struct socket *sock)
466{
467 struct sock *sk = sock->sk;
468 struct hf_sock *hsock = hsock_from_sk(sk);
469 unsigned long flags;
470
471 if (!sk)
472 return 0;
473
474 /* Shutdown for both send and receive. */
475 lock_sock(sk);
476 sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN;
477 sk->sk_state_change(sk);
478 release_sock(sk);
479
480 /* Remove from the hash table, so lookups from now on won't find it. */
481 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
482 hash_del_rcu(&hsock->sk.sk_node);
483 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
484
485 /*
486 * TODO: When we implement a tx queue, we need to clear it here so that
487 * sk_wmem_alloc will not prevent sk from being freed (sk_free).
488 */
489
490 /*
491 * Wait for in-flight lookups to finish. We need to do this here because
Wedson Almeida Filho89d0e472019-01-03 19:18:39 +0000492 * in-flight lookups rely on the reference to the socket we're about to
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000493 * release.
494 */
495 synchronize_rcu();
496 sock_put(sk);
497 sock->sk = NULL;
498
499 return 0;
500}
501
502/**
503 * This is called when there are no more references to the socket. It frees all
504 * resources that haven't been freed during release.
505 */
506static void hf_sock_destruct(struct sock *sk)
507{
508 /*
509 * Clear the receive queue now that the handler cannot add any more
510 * skbs to it.
511 */
512 skb_queue_purge(&sk->sk_receive_queue);
513}
514
515/**
516 * Connects the Hafnium socket to the provided VM and port. After the socket is
517 * connected, it can be used to exchange datagrams with the specified peer.
518 */
Andrew Scull01778112019-01-14 15:37:53 +0000519static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, int len,
520 int connect_flags)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000521{
522 struct sock *sk = sock->sk;
523 struct hf_sock *hsock = hsock_from_sk(sk);
524 struct hf_vm *vm;
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100525 struct hf_sockaddr *addr;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000526 int err;
527 unsigned long flags;
528
529 /* Basic address validation. */
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100530 if (len < sizeof(struct hf_sockaddr) || saddr->sa_family != AF_HF)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000531 return -EINVAL;
532
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100533 addr = (struct hf_sockaddr *)saddr;
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000534 vm = hf_vm_from_id(addr->vm_id);
535 if (!vm)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000536 return -ENETUNREACH;
537
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000538 /*
539 * TODO: Once we implement access control in Hafnium, check that the
540 * caller is allowed to contact the specified VM. Return -ECONNREFUSED
541 * if access is denied.
542 */
543
544 /* Take lock to make sure state doesn't change as we connect. */
545 lock_sock(sk);
546
547 /* Only unconnected sockets are allowed to become connected. */
548 if (sock->state != SS_UNCONNECTED) {
549 err = -EISCONN;
550 goto exit;
551 }
552
553 hsock->local_port = atomic64_inc_return(&hf_next_port);
554 hsock->remote_port = addr->port;
555 hsock->peer_vm = vm;
556
557 sock->state = SS_CONNECTED;
558
559 /* Add socket to hash table now that it's fully initialised. */
560 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
561 hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port);
562 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
563
564 err = 0;
565exit:
566 release_sock(sk);
567 return err;
568}
569
570/**
571 * Sends the given skb to the appropriate VM by calling Hafnium. It will also
572 * trigger the wake up of a recipient VM.
573 *
574 * Takes ownership of the skb on success.
575 */
576static int hf_send_skb(struct sk_buff *skb)
577{
578 unsigned long flags;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100579 struct ffa_value ret;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000580 struct hf_sock *hsock = hsock_from_sk(skb->sk);
581 struct hf_vm *vm = hsock->peer_vm;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100582 void *message = page_address(hf_send_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000583
584 /*
585 * Call Hafnium under the send lock so that we serialize the use of the
586 * global send buffer.
587 */
588 spin_lock_irqsave(&hf_send_lock, flags);
Andrew Walbrancafe0172019-10-07 14:14:05 +0100589 memcpy(message, skb->data, skb->len);
Jose Marinho1cc6c752019-03-11 16:28:03 +0000590
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100591 ret = ffa_msg_send(current_vm_id, vm->id, skb->len, 0);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000592 spin_unlock_irqrestore(&hf_send_lock, flags);
593
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100594 if (ret.func == FFA_ERROR_32) {
Andrew Walbranb040b302019-10-10 13:50:06 +0100595 switch (ret.arg2) {
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100596 case FFA_INVALID_PARAMETERS:
Andrew Walbrancafe0172019-10-07 14:14:05 +0100597 return -ENXIO;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100598 case FFA_NOT_SUPPORTED:
Andrew Walbrancafe0172019-10-07 14:14:05 +0100599 return -EIO;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100600 case FFA_DENIED:
601 case FFA_BUSY:
Andrew Walbrancafe0172019-10-07 14:14:05 +0100602 default:
603 return -EAGAIN;
604 }
605 }
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000606
Andrew Scull71f57362019-02-05 16:11:35 +0000607 /* Ensure the VM will run to pick up the message. */
608 hf_deliver_message(vm->id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000609
610 kfree_skb(skb);
611
612 return 0;
613}
614
615/**
616 * Determines if the given socket is in the connected state. It acquires and
617 * releases the socket lock.
618 */
619static bool hf_sock_is_connected(struct socket *sock)
620{
621 bool ret;
622
623 lock_sock(sock->sk);
624 ret = sock->state == SS_CONNECTED;
625 release_sock(sock->sk);
626
627 return ret;
628}
629
630/**
631 * Sends a message to the VM & port the socket is connected to. All variants
632 * of write/send/sendto/sendmsg eventually call this function.
633 */
634static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
635{
636 struct sock *sk = sock->sk;
637 struct sk_buff *skb;
638 int err;
639 struct hf_msg_hdr *hdr;
640 struct hf_sock *hsock = hsock_from_sk(sk);
Andrew Walbrancafe0172019-10-07 14:14:05 +0100641 size_t payload_max_len = HF_MAILBOX_SIZE - sizeof(struct hf_msg_hdr);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000642
643 /* Check length. */
Andrew Scull614ed7f2019-04-01 12:12:38 +0100644 if (len > payload_max_len)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000645 return -EMSGSIZE;
646
647 /* We don't allow the destination address to be specified. */
648 if (m->msg_namelen > 0)
649 return -EISCONN;
650
651 /* We don't support out of band messages. */
652 if (m->msg_flags & MSG_OOB)
653 return -EOPNOTSUPP;
654
655 /*
656 * Ensure that the socket is connected. We don't need to hold the socket
657 * lock (acquired and released by hf_sock_is_connected) for the
658 * remainder of the function because the fields we care about are
659 * immutable once the state is SS_CONNECTED.
660 */
661 if (!hf_sock_is_connected(sock))
662 return -ENOTCONN;
663
664 /*
665 * Allocate an skb for this write. If there isn't enough room in the
666 * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block
667 * (if it's a blocking call). On success, it increments sk_wmem_alloc
668 * and sets up the skb such that sk_wmem_alloc gets decremented when
669 * the skb is freed (sock_wfree gets called).
670 */
671 skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr),
672 m->msg_flags & MSG_DONTWAIT, &err);
673 if (!skb)
674 return err;
675
676 /* Reserve room for the header and initialise it. */
677 skb_reserve(skb, sizeof(struct hf_msg_hdr));
678 hdr = skb_push(skb, sizeof(struct hf_msg_hdr));
679 hdr->src_port = hsock->local_port;
680 hdr->dst_port = hsock->remote_port;
681
682 /* Allocate area for the contents, then copy into skb. */
683 if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) {
684 err = -EFAULT;
685 goto err_cleanup;
686 }
687
688 /*
689 * TODO: We currently do this inline, but when we have support for
690 * readiness notification from Hafnium, we must add this to a per-VM tx
691 * queue that can make progress when the VM becomes writable. This will
692 * fix send buffering and poll readiness notification.
693 */
694 err = hf_send_skb(skb);
695 if (err)
696 goto err_cleanup;
697
698 return 0;
699
700err_cleanup:
701 kfree_skb(skb);
702 return err;
703}
704
705/**
706 * Receives a message originated from the VM & port the socket is connected to.
707 * All variants of read/recv/recvfrom/recvmsg eventually call this function.
708 */
709static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
710 int flags)
711{
712 struct sock *sk = sock->sk;
713 struct sk_buff *skb;
714 int err;
715 size_t copy_len;
716
717 if (!hf_sock_is_connected(sock))
718 return -ENOTCONN;
719
720 /* Grab the next skb from the receive queue. */
721 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
722 if (!skb)
723 return err;
724
725 /* Make sure we don't copy more than what fits in the output buffer. */
726 copy_len = skb->len;
727 if (copy_len > len) {
728 copy_len = len;
729 m->msg_flags |= MSG_TRUNC;
730 }
731
732 /* Make sure we don't overflow the return value type. */
733 if (copy_len > INT_MAX) {
734 copy_len = INT_MAX;
735 m->msg_flags |= MSG_TRUNC;
736 }
737
738 /* Copy skb to output iterator, then free it. */
739 err = skb_copy_datagram_msg(skb, 0, m, copy_len);
740 skb_free_datagram(sk, skb);
741 if (err)
742 return err;
743
744 return copy_len;
745}
746
747/**
748 * This function is called when a Hafnium socket is created. It initialises all
749 * state such that the caller will be able to connect the socket and then send
750 * and receive messages through it.
751 */
752static int hf_sock_create(struct net *net, struct socket *sock, int protocol,
Andrew Scull01778112019-01-14 15:37:53 +0000753 int kern)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000754{
755 static const struct proto_ops ops = {
756 .family = PF_HF,
757 .owner = THIS_MODULE,
758 .release = hf_sock_release,
759 .bind = sock_no_bind,
760 .connect = hf_sock_connect,
761 .socketpair = sock_no_socketpair,
762 .accept = sock_no_accept,
763 .ioctl = sock_no_ioctl,
764 .listen = sock_no_listen,
765 .shutdown = sock_no_shutdown,
766 .setsockopt = sock_no_setsockopt,
767 .getsockopt = sock_no_getsockopt,
768 .sendmsg = hf_sock_sendmsg,
769 .recvmsg = hf_sock_recvmsg,
770 .mmap = sock_no_mmap,
771 .sendpage = sock_no_sendpage,
772 .poll = datagram_poll,
773 };
774 struct sock *sk;
775
776 if (sock->type != SOCK_DGRAM)
777 return -ESOCKTNOSUPPORT;
778
779 if (protocol != 0)
780 return -EPROTONOSUPPORT;
781
782 /*
783 * For now we only allow callers with sys admin capability to create
784 * Hafnium sockets.
785 */
786 if (!capable(CAP_SYS_ADMIN))
787 return -EPERM;
788
789 /* Allocate and initialise socket. */
790 sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern);
791 if (!sk)
792 return -ENOMEM;
793
794 sock_init_data(sock, sk);
795
796 sk->sk_destruct = hf_sock_destruct;
797 sock->ops = &ops;
798 sock->state = SS_UNCONNECTED;
799
800 return 0;
801}
802
803/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100804 * Frees all resources, including threads, associated with the Hafnium driver.
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100805 */
Andrew Scull82257c42018-10-01 10:37:48 +0100806static void hf_free_resources(void)
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100807{
Fuad Tabba5da4b6b2019-08-05 13:56:20 +0100808 uint16_t i;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100809 ffa_vcpu_index_t j;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100810
811 /*
812 * First stop all worker threads. We need to do this before freeing
813 * resources because workers may reference each other, so it is only
814 * safe to free resources after they have all stopped.
815 */
Andrew Scull82257c42018-10-01 10:37:48 +0100816 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100817 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000818
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100819 for (j = 0; j < vm->vcpu_count; j++)
820 kthread_stop(vm->vcpu[j].task);
821 }
822
823 /* Free resources. */
Andrew Scull82257c42018-10-01 10:37:48 +0100824 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100825 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000826
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100827 for (j = 0; j < vm->vcpu_count; j++)
828 put_task_struct(vm->vcpu[j].task);
829 kfree(vm->vcpu);
830 }
831
832 kfree(hf_vms);
Andrew Walbran81cf04c2020-08-06 16:00:43 +0100833
834 ffa_rx_release();
835 if (hf_send_page) {
836 __free_page(hf_send_page);
837 hf_send_page = NULL;
838 }
839 if (hf_recv_page) {
840 __free_page(hf_recv_page);
841 hf_recv_page = NULL;
842 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100843}
844
Andrew Scullbb7ae412018-09-28 21:07:15 +0100845/**
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000846 * Handles the hypervisor timer interrupt.
847 */
848static irqreturn_t hf_nop_irq_handler(int irq, void *dev)
849{
850 /*
851 * No need to do anything, the interrupt only exists to return to the
852 * primary vCPU so that the virtual timer will be restored and fire as
853 * normal.
854 */
855 return IRQ_HANDLED;
856}
857
858/**
859 * Enables the hypervisor timer interrupt on a CPU, when it starts or after the
860 * driver is first loaded.
861 */
862static int hf_starting_cpu(unsigned int cpu)
863{
864 if (hf_irq != 0) {
865 /* Enable the interrupt, and set it to be edge-triggered. */
866 enable_percpu_irq(hf_irq, IRQ_TYPE_EDGE_RISING);
867 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000868
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000869 return 0;
870}
871
872/**
873 * Disables the hypervisor timer interrupt on a CPU when it is powered down.
874 */
875static int hf_dying_cpu(unsigned int cpu)
876{
877 if (hf_irq != 0) {
878 /* Disable the interrupt while the CPU is asleep. */
879 disable_percpu_irq(hf_irq);
880 }
881
882 return 0;
883}
884
885/**
886 * Registers for the hypervisor timer interrupt.
887 */
888static int hf_int_driver_probe(struct platform_device *pdev)
889{
890 int irq;
891 int ret;
892
893 /*
894 * Register a handler for the hyperviser timer IRQ, as it is needed for
895 * Hafnium to emulate the virtual timer for Linux while a secondary vCPU
896 * is running.
897 */
898 irq = platform_get_irq(pdev, ARCH_TIMER_HYP_PPI);
899 if (irq < 0) {
900 pr_err("Error getting hypervisor timer IRQ: %d\n", irq);
901 return irq;
902 }
903 hf_irq = irq;
904
905 ret = request_percpu_irq(irq, hf_nop_irq_handler, HYPERVISOR_TIMER_NAME,
906 pdev);
907 if (ret != 0) {
908 pr_err("Error registering hypervisor timer IRQ %d: %d\n",
909 irq, ret);
910 return ret;
911 }
912 pr_info("Hafnium registered for IRQ %d\n", irq);
913 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
914 "hafnium/hypervisor_timer:starting",
915 hf_starting_cpu, hf_dying_cpu);
916 if (ret < 0) {
917 pr_err("Error enabling timer on all CPUs: %d\n", ret);
Andrew Walbran8d55e502019-02-05 11:42:08 +0000918 free_percpu_irq(irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000919 return ret;
920 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000921 hf_cpuhp_state = ret;
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000922
923 return 0;
924}
925
926/**
927 * Unregisters for the hypervisor timer interrupt.
928 */
929static int hf_int_driver_remove(struct platform_device *pdev)
930{
Andrew Walbran8d55e502019-02-05 11:42:08 +0000931 /*
932 * This will cause hf_dying_cpu to be called on each CPU, which will
933 * disable the IRQs.
934 */
935 cpuhp_remove_state(hf_cpuhp_state);
936 free_percpu_irq(hf_irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000937
938 return 0;
939}
940
941static const struct of_device_id hf_int_driver_id[] = {
942 {.compatible = "arm,armv7-timer"},
943 {.compatible = "arm,armv8-timer"},
944 {}
945};
946
947static struct platform_driver hf_int_driver = {
948 .driver = {
949 .name = HYPERVISOR_TIMER_NAME,
950 .owner = THIS_MODULE,
951 .of_match_table = of_match_ptr(hf_int_driver_id),
952 },
953 .probe = hf_int_driver_probe,
954 .remove = hf_int_driver_remove,
955};
956
957/**
Andrew Walbran6b796192020-08-06 16:01:59 +0100958 * Print the error code of the given FF-A value if it is an error, or the
959 * function ID otherwise.
960 */
961static void print_ffa_error(struct ffa_value ffa_ret)
962{
963 if (ffa_ret.func == FFA_ERROR_32)
964 pr_err("FF-A error code %d\n", ffa_ret.arg2);
965 else
966 pr_err("Unexpected FF-A function %#x\n", ffa_ret.func);
967}
968
969/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100970 * Initializes the Hafnium driver by creating a thread for each vCPU of each
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100971 * virtual machine.
972 */
973static int __init hf_init(void)
974{
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000975 static const struct net_proto_family proto_family = {
976 .family = PF_HF,
977 .create = hf_sock_create,
978 .owner = THIS_MODULE,
979 };
Andrew Scullbb7ae412018-09-28 21:07:15 +0100980 int64_t ret;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100981 struct ffa_value ffa_ret;
982 ffa_vm_id_t i;
983 ffa_vcpu_index_t j;
Andrew Walbran6b796192020-08-06 16:01:59 +0100984 struct ffa_uuid null_uuid;
Andrew Walbran196ed0e2020-04-30 11:32:29 +0100985 ffa_vm_count_t secondary_vm_count;
Andrew Walbran6b796192020-08-06 16:01:59 +0100986 const struct ffa_partition_info *partition_info;
Andrew Scull82257c42018-10-01 10:37:48 +0100987 uint32_t total_vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100988
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100989 /* Allocate a page for send and receive buffers. */
990 hf_send_page = alloc_page(GFP_KERNEL);
991 if (!hf_send_page) {
992 pr_err("Unable to allocate send buffer\n");
993 return -ENOMEM;
994 }
995
996 hf_recv_page = alloc_page(GFP_KERNEL);
997 if (!hf_recv_page) {
998 __free_page(hf_send_page);
Andrew Walbran81cf04c2020-08-06 16:00:43 +0100999 hf_send_page = NULL;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +01001000 pr_err("Unable to allocate receive buffer\n");
1001 return -ENOMEM;
1002 }
1003
1004 /*
1005 * Configure both addresses. Once configured, we cannot free these pages
1006 * because the hypervisor will use them, even if the module is
1007 * unloaded.
1008 */
Andrew Walbran196ed0e2020-04-30 11:32:29 +01001009 ffa_ret = ffa_rxtx_map(page_to_phys(hf_send_page),
Andrew Walbran2c6e7512019-11-05 14:02:29 +00001010 page_to_phys(hf_recv_page));
Andrew Walbran196ed0e2020-04-30 11:32:29 +01001011 if (ffa_ret.func != FFA_SUCCESS_32) {
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001012 pr_err("Unable to configure VM mailbox.\n");
Andrew Walbran6b796192020-08-06 16:01:59 +01001013 print_ffa_error(ffa_ret);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001014 ret = -EIO;
1015 goto fail_with_cleanup;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +01001016 }
1017
Andrew Walbran6b796192020-08-06 16:01:59 +01001018 /* Get information about secondary VMs. */
1019 ffa_uuid_init(0, 0, 0, 0, &null_uuid);
1020 ffa_ret = ffa_partition_info_get(&null_uuid);
1021 if (ffa_ret.func != FFA_SUCCESS_32) {
1022 pr_err("Unable to get VM information.\n");
1023 print_ffa_error(ffa_ret);
1024 ret = -EIO;
1025 goto fail_with_cleanup;
1026 }
1027 secondary_vm_count = ffa_ret.arg2 - 1;
1028 partition_info = page_address(hf_recv_page);
Andrew Scull82257c42018-10-01 10:37:48 +01001029
1030 /* Confirm the maximum number of VMs looks sane. */
1031 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS < 1);
1032 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS > U16_MAX);
1033
1034 /* Validate the number of VMs. There must at least be the primary. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001035 if (secondary_vm_count > CONFIG_HAFNIUM_MAX_VMS - 1) {
Fuad Tabba8523ccd2019-07-31 15:37:29 +01001036 pr_err("Number of VMs is out of range: %d\n",
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001037 secondary_vm_count);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001038 ret = -EDQUOT;
1039 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001040 }
1041
Andrew Scullb722f952018-09-27 15:39:10 +01001042 /* Only track the secondary VMs. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001043 hf_vms = kmalloc_array(secondary_vm_count, sizeof(struct hf_vm),
1044 GFP_KERNEL);
Andrew Walbran81cf04c2020-08-06 16:00:43 +01001045 if (!hf_vms) {
1046 ret = -ENOMEM;
1047 goto fail_with_cleanup;
1048 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001049
Jose Marinho1cc6c752019-03-11 16:28:03 +00001050 /* Cache the VM id for later usage. */
1051 current_vm_id = hf_vm_get_id();
1052
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001053 /* Initialize each VM. */
Andrew Scull82257c42018-10-01 10:37:48 +01001054 total_vcpu_count = 0;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001055 for (i = 0; i < secondary_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001056 struct hf_vm *vm = &hf_vms[i];
Andrew Walbran196ed0e2020-04-30 11:32:29 +01001057 ffa_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001058
Andrew Walbran6b796192020-08-06 16:01:59 +01001059 /* Adjust the index as only the secondaries are tracked. */
1060 vm->id = partition_info[i + 1].vm_id;
1061 vcpu_count = partition_info[i + 1].vcpu_count;
Andrew Scull82257c42018-10-01 10:37:48 +01001062
1063 /* Avoid overflowing the vcpu count. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001064 if (vcpu_count > (U32_MAX - total_vcpu_count)) {
Andrew Scull82257c42018-10-01 10:37:48 +01001065 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1066 ret = -EDQUOT;
1067 goto fail_with_cleanup;
1068 }
1069
1070 /* Confirm the maximum number of VCPUs looks sane. */
1071 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1);
1072 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX);
1073
1074 /* Enforce the limit on vcpus. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001075 total_vcpu_count += vcpu_count;
Andrew Scull82257c42018-10-01 10:37:48 +01001076 if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) {
1077 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1078 ret = -EDQUOT;
1079 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001080 }
1081
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001082 vm->vcpu_count = vcpu_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001083 vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu),
1084 GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001085 if (!vm->vcpu) {
Andrew Scull82257c42018-10-01 10:37:48 +01001086 ret = -ENOMEM;
1087 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001088 }
1089
Andrew Scull82257c42018-10-01 10:37:48 +01001090 /* Update the number of initialized VMs. */
1091 hf_vm_count = i + 1;
1092
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001093 /* Create a kernel thread for each vcpu. */
1094 for (j = 0; j < vm->vcpu_count; j++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001095 struct hf_vcpu *vcpu = &vm->vcpu[j];
Andrew Scull01778112019-01-14 15:37:53 +00001096
1097 vcpu->task =
1098 kthread_create(hf_vcpu_thread, vcpu,
1099 "vcpu_thread_%u_%u", vm->id, j);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001100 if (IS_ERR(vcpu->task)) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001101 pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n",
1102 vm->id, j, PTR_ERR(vcpu->task));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001103 vm->vcpu_count = j;
Andrew Scull82257c42018-10-01 10:37:48 +01001104 ret = PTR_ERR(vcpu->task);
1105 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001106 }
1107
1108 get_task_struct(vcpu->task);
Andrew Scullb722f952018-09-27 15:39:10 +01001109 vcpu->vm = vm;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001110 vcpu->vcpu_index = j;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +00001111 atomic_set(&vcpu->abort_sleep, 0);
Andrew Scullece5ef42019-05-08 15:07:25 +01001112 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001113 }
1114 }
1115
Andrew Walbran6b796192020-08-06 16:01:59 +01001116 ffa_ret = ffa_rx_release();
1117 if (ffa_ret.func != FFA_SUCCESS_32) {
1118 pr_err("Unable to release RX buffer.\n");
1119 print_ffa_error(ffa_ret);
1120 ret = -EIO;
1121 goto fail_with_cleanup;
1122 }
1123
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001124 /* Register protocol and socket family. */
1125 ret = proto_register(&hf_sock_proto, 0);
1126 if (ret) {
1127 pr_err("Unable to register protocol: %lld\n", ret);
1128 goto fail_with_cleanup;
1129 }
1130
1131 ret = sock_register(&proto_family);
1132 if (ret) {
1133 pr_err("Unable to register Hafnium's socket family: %lld\n",
1134 ret);
1135 goto fail_unregister_proto;
1136 }
1137
1138 /*
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001139 * Register as a driver for the timer device, so we can register a
1140 * handler for the hyperviser timer IRQ.
1141 */
1142 ret = platform_driver_register(&hf_int_driver);
1143 if (ret != 0) {
1144 pr_err("Error registering timer driver %lld\n", ret);
1145 goto fail_unregister_socket;
1146 }
1147
1148 /*
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001149 * Start running threads now that all is initialized.
1150 *
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001151 * Any failures from this point on must also unregister the driver with
1152 * platform_driver_unregister().
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001153 */
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001154 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001155 struct hf_vm *vm = &hf_vms[i];
Andrew Scull01778112019-01-14 15:37:53 +00001156
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001157 for (j = 0; j < vm->vcpu_count; j++)
1158 wake_up_process(vm->vcpu[j].task);
1159 }
1160
1161 /* Dump vm/vcpu count info. */
Andrew Scullbb7ae412018-09-28 21:07:15 +01001162 pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001163 for (i = 0; i < hf_vm_count; i++) {
1164 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001165
Andrew Scullbb7ae412018-09-28 21:07:15 +01001166 pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001167 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001168
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001169 return 0;
Andrew Scull82257c42018-10-01 10:37:48 +01001170
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001171fail_unregister_socket:
1172 sock_unregister(PF_HF);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001173fail_unregister_proto:
1174 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001175fail_with_cleanup:
1176 hf_free_resources();
1177 return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001178}
1179
1180/**
Andrew Scullbb7ae412018-09-28 21:07:15 +01001181 * Frees up all resources used by the Hafnium driver in preparation for
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001182 * unloading it.
1183 */
1184static void __exit hf_exit(void)
1185{
Andrew Scullbb7ae412018-09-28 21:07:15 +01001186 pr_info("Preparing to unload Hafnium\n");
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001187 sock_unregister(PF_HF);
1188 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001189 hf_free_resources();
Andrew Walbran8d55e502019-02-05 11:42:08 +00001190 platform_driver_unregister(&hf_int_driver);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001191 pr_info("Hafnium ready to unload\n");
1192}
1193
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001194MODULE_LICENSE("GPL v2");
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001195
1196module_init(hf_init);
1197module_exit(hf_exit);