All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wolfgang Wallner <wolfgang.wallner@br-automation.com>
To: u-boot@lists.denx.de
Subject: [PATCH 13/22] x86: mp: Allow running functions on multiple CPUs
Date: Wed, 10 Jun 2020 15:27:35 +0200	[thread overview]
Message-ID: <OF648DD14E.F52080ED-ONC1258583.0045FF65-C1258583.0049F01A@br-automation.com> (raw)
In-Reply-To: <20200521202309.13.I15c8366d7a11d1eeea57e5ac4d0471a95725ce9e@changeid>

Hi Simon,

-----"Simon Glass" <sjg@chromium.org> schrieb: -----
> Betreff: [PATCH 13/22] x86: mp: Allow running functions on multiple CPUs
> 
> Add a way to run a function on a selection of CPUs. This supports either
> a single CPU, all CPUs, just the main CPU or just the 'APs', in Intel
> terminology.
> 
> It works by writing into a mailbox and then waiting for the CPUs to notice
> it, take action and indicate they are done.
> 
> When SMP is not yet enabled, this just calls the function on the main CPU.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
>  arch/x86/cpu/mp_init.c    | 82 ++++++++++++++++++++++++++++++++++++---
>  arch/x86/include/asm/mp.h | 30 ++++++++++++++
>  2 files changed, 106 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/cpu/mp_init.c b/arch/x86/cpu/mp_init.c
> index 139e9749e74..9f97dc7a9ba 100644
> --- a/arch/x86/cpu/mp_init.c
> +++ b/arch/x86/cpu/mp_init.c
> @@ -50,12 +50,7 @@ struct cpu_map {
>  };
>  
>  struct mp_callback {
> -	/**
> -	 * func() - Function to call on the AP
> -	 *
> -	 * @arg: Argument to pass
> -	 */
> -	void (*func)(void *arg);
> +	mp_run_func func;
>  	void *arg;
>  	int logical_cpu_number;
>  };
> @@ -483,6 +478,50 @@ static void store_callback(struct mp_callback **slot, struct mp_callback *val)
>  	);
>  }

Could you please add a function description for run_ap_work()?

> +static int run_ap_work(struct mp_callback *callback, struct udevice *bsp,
> +		       int num_cpus, uint expire_ms)
> +{
> +	int cur_cpu = bsp->req_seq;
> +	int num_aps = num_cpus - 1; /* number of non-BSPs to get this message */
> +	int cpus_accepted;
> +	ulong start;
> +	int i;
> +
> +	/* Signal to all the APs to run the func. */
> +	for (i = 0; i < num_cpus; i++) {
> +		if (cur_cpu != i)
> +			store_callback(&ap_callbacks[i], callback);
> +	}
> +	mfence();
> +
> +	/* Wait for all the APs to signal back that call has been accepted. */
> +	start = get_timer(0);
> +
> +	do {
> +		mdelay(1);
> +		cpus_accepted = 0;
> +
> +		for (i = 0; i < num_cpus; i++) {
> +			if (cur_cpu == i)
> +				continue;
> +			if (!read_callback(&ap_callbacks[i]))
> +				cpus_accepted++;
> +		}
> +
> +		if (expire_ms && get_timer(start) >= expire_ms) {
> +			log(UCLASS_CPU, LOGL_CRIT,
> +			    "AP call expired; %d/%d CPUs accepted\n",
> +			    cpus_accepted, num_aps);
> +			return -ETIMEDOUT;
> +		}
> +	} while (cpus_accepted != num_aps);
> +
> +	/* Make sure we can see any data written by the APs */
> +	mfence();
> +
> +	return 0;
> +}
> +
>  /**
>   * ap_wait_for_instruction() - Wait for and process requests from the main CPU
>   *
> @@ -539,6 +578,37 @@ static struct mp_flight_record mp_steps[] = {
>  	MP_FR_BLOCK_APS(ap_wait_for_instruction, NULL, NULL, NULL),
>  };
>  
> +int mp_run_on_cpus(int cpu_select, mp_run_func func, void *arg)
> +{
> +	struct mp_callback lcb = {
> +		.func = func,
> +		.arg = arg,
> +		.logical_cpu_number = cpu_select,
> +	};
> +	struct udevice *dev;
> +	int num_cpus;
> +	int ret;
> +
> +	if (!(gd->flags & GD_FLG_SMP_INIT))
> +		return -ENXIO;
> +
> +	ret = get_bsp(&dev, &num_cpus);
> +	if (ret < 0)
> +		return log_msg_ret("bsp", ret);
> +	if (cpu_select == MP_SELECT_ALL || cpu_select == MP_SELECT_BSP ||
> +	    cpu_select == ret) {
> +		/* Run on BSP first */
> +		func(arg);
> +	}
> +
> +	/* Allow up to 1 second for all APs to finish */
> +	ret = run_ap_work(&lcb, dev, num_cpus, 1000 /* ms */);
> +	if (ret)
> +		return log_msg_ret("aps", ret);
> +
> +	return 0;
> +}
> +
>  int mp_init(void)
>  {
>  	int num_aps, num_cpus;
> diff --git a/arch/x86/include/asm/mp.h b/arch/x86/include/asm/mp.h
> index 41b1575f4be..0272b3c0b6a 100644
> --- a/arch/x86/include/asm/mp.h
> +++ b/arch/x86/include/asm/mp.h
> @@ -86,4 +86,34 @@ int mp_init(void);
>  /* Set up additional CPUs */
>  int x86_mp_init(void);
>  
> +/**
> + * mp_run_func() - Function to call on the AP
> + *
> + * @arg: Argument to pass
> + */
> +typedef void (*mp_run_func)(void *arg);
> +
> +#if defined(CONFIG_SMP) && !CONFIG_IS_ENABLED(X86_64)
> +/**
> + * mp_run_on_cpus() - Run a function on one or all CPUs
> + *
> + * This does not return until all CPUs have completed the work
> + *
> + * @cpu_select: CPU to run on, or MP_SELECT_ALL for all, or MP_SELECT_BSP for
> + *	BSP
> + * @func: Function to run
> + * @arg: Argument to pass to the function
> + * @return 0 on success, -ve on error
> + */
> +int mp_run_on_cpus(int cpu_select, mp_run_func func, void *arg);
> +#else
> +static inline int mp_run_on_cpus(int cpu_select, mp_run_func func, void *arg)
> +{
> +	/* There is only one CPU, so just call the function here */
> +	func(arg);
> +
> +	return 0;
> +}
> +#endif
> +
>  #endif /* _X86_MP_H_ */
> -- 
> 2.27.0.rc0.183.gde8f92d652-goog

regards, Wolfgang

  reply	other threads:[~2020-06-10 13:27 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-22  2:23 [PATCH 00/22] x86: Enhance MTRR functionality to support multiple CPUs Simon Glass
2020-05-22  2:23 ` [PATCH 01/22] x86: mp_init: Switch to livetree Simon Glass
2020-06-10 13:25   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 02/22] x86: Move MP code into mp_init Simon Glass
2020-06-10 13:25   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 03/22] x86: mp_init: Avoid declarations in header files Simon Glass
2020-06-10 13:25   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 04/22] x86: mp_init: Switch parameter names in start_aps() Simon Glass
2020-06-10 13:25   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 05/22] x86: mp_init: Drop the num_cpus static variable Simon Glass
2020-06-10 13:25   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 06/22] x86: mtrr: Fix 'ensable' typo Simon Glass
2020-06-10 13:26   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 07/22] x86: mp_init: Set up the CPU numbers at the start Simon Glass
2020-06-10 13:26   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 08/22] x86: mp_init: Adjust bsp_init() to return more information Simon Glass
2020-06-10 13:26   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 09/22] x86: cpu: Remove unnecessary #ifdefs Simon Glass
2020-06-10 13:26   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 10/22] x86: mp: Support APs waiting for instructions Simon Glass
2020-06-10 13:27   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 11/22] global_data: Add a generic global_data flag for SMP state Simon Glass
2020-06-10 13:27   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 12/22] x86: Set the SMP flag when MP init is complete Simon Glass
2020-06-10 13:27   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 13/22] x86: mp: Allow running functions on multiple CPUs Simon Glass
2020-06-10 13:27   ` Wolfgang Wallner [this message]
2020-05-22  2:23 ` [PATCH 14/22] x86: mp: Park CPUs before running the OS Simon Glass
2020-06-10 13:27   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 15/22] x86: mp: Add iterators for CPUs Simon Glass
2020-06-10 13:27   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 16/22] x86: mtrr: Use MP calls to list the MTRRs Simon Glass
2020-06-10 13:28   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 17/22] x86: mtrr: Update MTRRs on all CPUs Simon Glass
2020-06-10 13:28   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 18/22] x86: mtrr: Add support for writing to MTRRs on any CPU Simon Glass
2020-06-10 13:28   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 19/22] x86: mtrr: Update the command to use the new mtrr calls Simon Glass
2020-06-10 13:28   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 20/22] x86: mtrr: Restructure so command execution is in one place Simon Glass
2020-06-10 13:28   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 21/22] x86: mtrr: Update 'mtrr' to allow setting MTRRs on any CPU Simon Glass
2020-06-10 13:29   ` Wolfgang Wallner
2020-05-22  2:23 ` [PATCH 22/22] x86: mtrr: Enhance 'mtrr' command to list " Simon Glass
2020-06-10 13:29   ` Wolfgang Wallner
2020-05-22 10:54 ` [PATCH 00/22] x86: Enhance MTRR functionality to support multiple CPUs Andy Shevchenko
2020-05-22 13:42   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=OF648DD14E.F52080ED-ONC1258583.0045FF65-C1258583.0049F01A@br-automation.com \
    --to=wolfgang.wallner@br-automation.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.