From patchwork Thu Jun 1 15:48:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 9759953 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5218860390 for ; Thu, 1 Jun 2017 15:48:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 47C3726256 for ; Thu, 1 Jun 2017 15:48:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3CBAC284FC; Thu, 1 Jun 2017 15:48:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD006284F9 for ; Thu, 1 Jun 2017 15:48:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752442AbdFAPs1 (ORCPT ); Thu, 1 Jun 2017 11:48:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43620 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752404AbdFAPs0 (ORCPT ); Thu, 1 Jun 2017 11:48:26 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F2AEC059735 for ; Thu, 1 Jun 2017 15:48:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1F2AEC059735 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=drjones@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1F2AEC059735 Received: from kamzik.brq.redhat.com (kamzik.brq.redhat.com [10.34.1.143]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE81F87E32; Thu, 1 Jun 2017 15:48:24 +0000 (UTC) From: Andrew Jones To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, rkrcmar@redhat.com Subject: [PATCH kvm-unit-tests 2/7] lib/x86/smp: introduce on_cpus Date: Thu, 1 Jun 2017 17:48:14 +0200 Message-Id: <20170601154819.18831-3-drjones@redhat.com> In-Reply-To: <20170601154819.18831-1-drjones@redhat.com> References: <20170601154819.18831-1-drjones@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 01 Jun 2017 15:48:26 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Andrew Jones --- lib/x86/smp.c | 20 ++++++++++++++++++++ lib/x86/smp.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/lib/x86/smp.c b/lib/x86/smp.c index 4bdbeaeb8b68..b16c98c02ad2 100644 --- a/lib/x86/smp.c +++ b/lib/x86/smp.c @@ -1,5 +1,6 @@ #include +#include "atomic.h" #include "smp.h" #include "apic.h" #include "fwcfg.h" @@ -15,6 +16,7 @@ static void *volatile ipi_data; static volatile int ipi_done; static volatile bool ipi_wait; static int _cpu_count; +static atomic_t active_cpus; static __attribute__((used)) void ipi() { @@ -27,6 +29,7 @@ static __attribute__((used)) void ipi() apic_write(APIC_EOI, 0); } function(data); + atomic_dec(&active_cpus); if (wait) { ipi_done = 1; apic_write(APIC_EOI, 0); @@ -68,6 +71,7 @@ static void __on_cpu(int cpu, void (*function)(void *data), void *data, if (cpu == smp_id()) function(data); else { + atomic_inc(&active_cpus); ipi_done = 0; ipi_function = function; ipi_data = data; @@ -91,6 +95,21 @@ void on_cpu_async(int cpu, void (*function)(void *data), void *data) __on_cpu(cpu, function, data, 0); } +void on_cpus(void (*func)(void)) +{ + int cpu; + + for (cpu = cpu_count() - 1; cpu >= 0; --cpu) + on_cpu_async(cpu, (ipi_function_type)func, NULL); + + while (cpus_active()) + ; +} + +int cpus_active(void) +{ + return atomic_read(&active_cpus) > 1; +} void smp_init(void) { @@ -106,4 +125,5 @@ void smp_init(void) for (i = 1; i < cpu_count(); ++i) on_cpu(i, setup_smp_id, 0); + atomic_inc(&active_cpus); } diff --git a/lib/x86/smp.h b/lib/x86/smp.h index afabac8495f1..5b8deb4a185e 100644 --- a/lib/x86/smp.h +++ b/lib/x86/smp.h @@ -6,7 +6,9 @@ void smp_init(void); int cpu_count(void); int smp_id(void); +int cpus_active(void); void on_cpu(int cpu, void (*function)(void *data), void *data); void on_cpu_async(int cpu, void (*function)(void *data), void *data); +void on_cpus(void (*func)(void)); #endif