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