xref: /freebsd/sys/powerpc/powernv/opal_async.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 
25 #include <sys/cdefs.h>
26 #include <sys/param.h>
27 #include <sys/eventhandler.h>
28 #include <sys/malloc.h>
29 #include <sys/proc.h>
30 #include <sys/vmem.h>
31 
32 #include <vm/vm.h>
33 #include <vm/pmap.h>
34 #include "opal.h"
35 
36 #include <machine/cpufunc.h>
37 
38 /*
39  * Manage asynchronous tokens for the OPAL abstraction layer.
40  *
41  * Only a finite number of in-flight tokens are supported by OPAL, so we must be
42  * careful managing this.  The basic design uses the vmem subsystem as a general
43  * purpose allocator, with wrappers to manage expected behaviors and
44  * requirements.
45  */
46 static vmem_t *async_token_pool;
47 
48 static void opal_handle_async_completion(void *, struct opal_msg *);
49 
50 struct async_completion {
51 	volatile uint64_t 	completed;
52 	struct opal_msg 	msg;
53 };
54 
55 struct async_completion *completions;
56 
57 /* Setup the token pool. */
58 int
59 opal_init_async_tokens(int count)
60 {
61 	/* Only allow one initialization */
62 	if (async_token_pool != NULL)
63 		return (EINVAL);
64 
65 	async_token_pool = vmem_create("OPAL Async", 0, count, 1, 1,
66 	    M_WAITOK | M_FIRSTFIT);
67 	completions = malloc(count * sizeof(struct async_completion),
68 	    M_DEVBUF, M_WAITOK | M_ZERO);
69 
70 	EVENTHANDLER_REGISTER(OPAL_ASYNC_COMP, opal_handle_async_completion,
71 	    NULL, EVENTHANDLER_PRI_ANY);
72 
73 	return (0);
74 }
75 
76 int
77 opal_alloc_async_token(void)
78 {
79 	vmem_addr_t token;
80 
81 	vmem_alloc(async_token_pool, 1, M_FIRSTFIT | M_WAITOK, &token);
82 	completions[token].completed = false;
83 
84 	return (token);
85 }
86 
87 void
88 opal_free_async_token(int token)
89 {
90 
91 	vmem_free(async_token_pool, token, 1);
92 }
93 
94 /*
95  * Wait for the operation watched by the token to complete.  Return the result
96  * of the operation, error if it returns early.
97  */
98 int
99 opal_wait_completion(void *buf, uint64_t size, int token)
100 {
101 	int err;
102 
103 	do {
104 		err = opal_call(OPAL_CHECK_ASYNC_COMPLETION,
105 		    vtophys(buf), size, token);
106 		if (err == OPAL_BUSY) {
107 			if (completions[token].completed) {
108 				atomic_thread_fence_acq();
109 				memcpy(buf, &completions[token].msg, size);
110 				return (OPAL_SUCCESS);
111 			}
112 		}
113 		DELAY(100);
114 	} while (err == OPAL_BUSY);
115 
116 	return (err);
117 }
118 
119 static void opal_handle_async_completion(void *arg, struct opal_msg *msg)
120 {
121 	int token;
122 
123 	token = msg->params[0];
124 	memcpy(&completions[token].msg, msg, sizeof(*msg));
125 	atomic_thread_fence_rel();
126 	completions[token].completed = true;
127 }
128