1dac618a6SJustin Hibbits /*-
2dac618a6SJustin Hibbits * Copyright (C) 2019 Justin Hibbits
3dac618a6SJustin Hibbits *
4dac618a6SJustin Hibbits * Redistribution and use in source and binary forms, with or without
5dac618a6SJustin Hibbits * modification, are permitted provided that the following conditions
6dac618a6SJustin Hibbits * are met:
7dac618a6SJustin Hibbits * 1. Redistributions of source code must retain the above copyright
8dac618a6SJustin Hibbits * notice, this list of conditions and the following disclaimer.
9dac618a6SJustin Hibbits * 2. Redistributions in binary form must reproduce the above copyright
10dac618a6SJustin Hibbits * notice, this list of conditions and the following disclaimer in the
11dac618a6SJustin Hibbits * documentation and/or other materials provided with the distribution.
12dac618a6SJustin Hibbits *
13dac618a6SJustin Hibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14dac618a6SJustin Hibbits * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15dac618a6SJustin Hibbits * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16dac618a6SJustin Hibbits * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17dac618a6SJustin Hibbits * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18dac618a6SJustin Hibbits * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19dac618a6SJustin Hibbits * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20dac618a6SJustin Hibbits * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21dac618a6SJustin Hibbits * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22dac618a6SJustin Hibbits * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23dac618a6SJustin Hibbits */
24dac618a6SJustin Hibbits
25dac618a6SJustin Hibbits #include <sys/param.h>
26*e2e050c8SConrad Meyer #include <sys/eventhandler.h>
27dac618a6SJustin Hibbits #include <sys/malloc.h>
28dac618a6SJustin Hibbits #include <sys/proc.h>
29dac618a6SJustin Hibbits #include <sys/vmem.h>
30dac618a6SJustin Hibbits
31dac618a6SJustin Hibbits #include <vm/vm.h>
32dac618a6SJustin Hibbits #include <vm/pmap.h>
33dac618a6SJustin Hibbits #include "opal.h"
34dac618a6SJustin Hibbits
35fbf77379SJustin Hibbits #include <machine/cpufunc.h>
36fbf77379SJustin Hibbits
37dac618a6SJustin Hibbits /*
38dac618a6SJustin Hibbits * Manage asynchronous tokens for the OPAL abstraction layer.
39dac618a6SJustin Hibbits *
40dac618a6SJustin Hibbits * Only a finite number of in-flight tokens are supported by OPAL, so we must be
41dac618a6SJustin Hibbits * careful managing this. The basic design uses the vmem subsystem as a general
42dac618a6SJustin Hibbits * purpose allocator, with wrappers to manage expected behaviors and
43dac618a6SJustin Hibbits * requirements.
44dac618a6SJustin Hibbits */
45dac618a6SJustin Hibbits static vmem_t *async_token_pool;
46dac618a6SJustin Hibbits
47fbf77379SJustin Hibbits static void opal_handle_async_completion(void *, struct opal_msg *);
48fbf77379SJustin Hibbits
49fbf77379SJustin Hibbits struct async_completion {
50947079ebSJustin Hibbits volatile uint64_t completed;
51947079ebSJustin Hibbits struct opal_msg msg;
52fbf77379SJustin Hibbits };
53fbf77379SJustin Hibbits
54fbf77379SJustin Hibbits struct async_completion *completions;
55fbf77379SJustin Hibbits
56dac618a6SJustin Hibbits /* Setup the token pool. */
57dac618a6SJustin Hibbits int
opal_init_async_tokens(int count)58dac618a6SJustin Hibbits opal_init_async_tokens(int count)
59dac618a6SJustin Hibbits {
60dac618a6SJustin Hibbits /* Only allow one initialization */
61dac618a6SJustin Hibbits if (async_token_pool != NULL)
62dac618a6SJustin Hibbits return (EINVAL);
63dac618a6SJustin Hibbits
64dac618a6SJustin Hibbits async_token_pool = vmem_create("OPAL Async", 0, count, 1, 1,
65dac618a6SJustin Hibbits M_WAITOK | M_FIRSTFIT);
66fbf77379SJustin Hibbits completions = malloc(count * sizeof(struct async_completion),
67fbf77379SJustin Hibbits M_DEVBUF, M_WAITOK | M_ZERO);
68fbf77379SJustin Hibbits
69fbf77379SJustin Hibbits EVENTHANDLER_REGISTER(OPAL_ASYNC_COMP, opal_handle_async_completion,
70fbf77379SJustin Hibbits NULL, EVENTHANDLER_PRI_ANY);
71dac618a6SJustin Hibbits
72dac618a6SJustin Hibbits return (0);
73dac618a6SJustin Hibbits }
74dac618a6SJustin Hibbits
75dac618a6SJustin Hibbits int
opal_alloc_async_token(void)76dac618a6SJustin Hibbits opal_alloc_async_token(void)
77dac618a6SJustin Hibbits {
78dac618a6SJustin Hibbits vmem_addr_t token;
79dac618a6SJustin Hibbits
80dac618a6SJustin Hibbits vmem_alloc(async_token_pool, 1, M_FIRSTFIT | M_WAITOK, &token);
81fbf77379SJustin Hibbits completions[token].completed = false;
82dac618a6SJustin Hibbits
83dac618a6SJustin Hibbits return (token);
84dac618a6SJustin Hibbits }
85dac618a6SJustin Hibbits
86dac618a6SJustin Hibbits void
opal_free_async_token(int token)87dac618a6SJustin Hibbits opal_free_async_token(int token)
88dac618a6SJustin Hibbits {
89dac618a6SJustin Hibbits
90dac618a6SJustin Hibbits vmem_free(async_token_pool, token, 1);
91dac618a6SJustin Hibbits }
92dac618a6SJustin Hibbits
93dac618a6SJustin Hibbits /*
94dac618a6SJustin Hibbits * Wait for the operation watched by the token to complete. Return the result
95dac618a6SJustin Hibbits * of the operation, error if it returns early.
96dac618a6SJustin Hibbits */
97dac618a6SJustin Hibbits int
opal_wait_completion(void * buf,uint64_t size,int token)98947079ebSJustin Hibbits opal_wait_completion(void *buf, uint64_t size, int token)
99dac618a6SJustin Hibbits {
100dac618a6SJustin Hibbits int err;
101dac618a6SJustin Hibbits
102dac618a6SJustin Hibbits do {
103dac618a6SJustin Hibbits err = opal_call(OPAL_CHECK_ASYNC_COMPLETION,
104dac618a6SJustin Hibbits vtophys(buf), size, token);
105947079ebSJustin Hibbits if (err == OPAL_BUSY) {
106947079ebSJustin Hibbits if (completions[token].completed) {
107947079ebSJustin Hibbits atomic_thread_fence_acq();
108947079ebSJustin Hibbits memcpy(buf, &completions[token].msg, size);
109947079ebSJustin Hibbits return (OPAL_SUCCESS);
110947079ebSJustin Hibbits }
111947079ebSJustin Hibbits }
112947079ebSJustin Hibbits DELAY(100);
113dac618a6SJustin Hibbits } while (err == OPAL_BUSY);
114dac618a6SJustin Hibbits
115dac618a6SJustin Hibbits return (err);
116dac618a6SJustin Hibbits }
117fbf77379SJustin Hibbits
opal_handle_async_completion(void * arg,struct opal_msg * msg)118fbf77379SJustin Hibbits static void opal_handle_async_completion(void *arg, struct opal_msg *msg)
119fbf77379SJustin Hibbits {
120fbf77379SJustin Hibbits int token;
121fbf77379SJustin Hibbits
122fbf77379SJustin Hibbits token = msg->params[0];
123947079ebSJustin Hibbits memcpy(&completions[token].msg, msg, sizeof(*msg));
124947079ebSJustin Hibbits atomic_thread_fence_rel();
125fbf77379SJustin Hibbits completions[token].completed = true;
126fbf77379SJustin Hibbits }
127