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