/*-1* Copyright (C) 2019 Justin Hibbits2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR13* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES14* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.15* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,16* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,17* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;18* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,19* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR20* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF21* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.22*/2324#include <sys/param.h>25#include <sys/eventhandler.h>26#include <sys/malloc.h>27#include <sys/proc.h>28#include <sys/vmem.h>2930#include <vm/vm.h>31#include <vm/pmap.h>32#include "opal.h"3334#include <machine/cpufunc.h>3536/*37* Manage asynchronous tokens for the OPAL abstraction layer.38*39* Only a finite number of in-flight tokens are supported by OPAL, so we must be40* careful managing this. The basic design uses the vmem subsystem as a general41* purpose allocator, with wrappers to manage expected behaviors and42* requirements.43*/44static vmem_t *async_token_pool;4546static void opal_handle_async_completion(void *, struct opal_msg *);4748struct async_completion {49volatile uint64_t completed;50struct opal_msg msg;51};5253struct async_completion *completions;5455/* Setup the token pool. */56int57opal_init_async_tokens(int count)58{59/* Only allow one initialization */60if (async_token_pool != NULL)61return (EINVAL);6263async_token_pool = vmem_create("OPAL Async", 0, count, 1, 1,64M_WAITOK | M_FIRSTFIT);65completions = malloc(count * sizeof(struct async_completion),66M_DEVBUF, M_WAITOK | M_ZERO);6768EVENTHANDLER_REGISTER(OPAL_ASYNC_COMP, opal_handle_async_completion,69NULL, EVENTHANDLER_PRI_ANY);7071return (0);72}7374int75opal_alloc_async_token(void)76{77vmem_addr_t token;7879vmem_alloc(async_token_pool, 1, M_FIRSTFIT | M_WAITOK, &token);80completions[token].completed = false;8182return (token);83}8485void86opal_free_async_token(int token)87{8889vmem_free(async_token_pool, token, 1);90}9192/*93* Wait for the operation watched by the token to complete. Return the result94* of the operation, error if it returns early.95*/96int97opal_wait_completion(void *buf, uint64_t size, int token)98{99int err;100101do {102err = opal_call(OPAL_CHECK_ASYNC_COMPLETION,103vtophys(buf), size, token);104if (err == OPAL_BUSY) {105if (completions[token].completed) {106atomic_thread_fence_acq();107memcpy(buf, &completions[token].msg, size);108return (OPAL_SUCCESS);109}110}111DELAY(100);112} while (err == OPAL_BUSY);113114return (err);115}116117static void opal_handle_async_completion(void *arg, struct opal_msg *msg)118{119int token;120121token = msg->params[0];122memcpy(&completions[token].msg, msg, sizeof(*msg));123atomic_thread_fence_rel();124completions[token].completed = true;125}126127128