18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * SN Platform GRU Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * 		MMUOPS callbacks  + TLB flushing
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This file handles emu notifier callbacks from the core kernel. The callbacks
88c2ecf20Sopenharmony_ci * are used to update the TLB in the GRU as a result of changes in the
98c2ecf20Sopenharmony_ci * state of a process address space. This file also handles TLB invalidates
108c2ecf20Sopenharmony_ci * from the GRU driver.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *  Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/list.h>
178c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
188c2ecf20Sopenharmony_ci#include <linux/mm.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/device.h>
218c2ecf20Sopenharmony_ci#include <linux/hugetlb.h>
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/timex.h>
248c2ecf20Sopenharmony_ci#include <linux/srcu.h>
258c2ecf20Sopenharmony_ci#include <asm/processor.h>
268c2ecf20Sopenharmony_ci#include "gru.h"
278c2ecf20Sopenharmony_ci#include "grutables.h"
288c2ecf20Sopenharmony_ci#include <asm/uv/uv_hub.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define gru_random()	get_cycles()
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* ---------------------------------- TLB Invalidation functions --------
338c2ecf20Sopenharmony_ci * get_tgh_handle
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Find a TGH to use for issuing a TLB invalidate. For GRUs that are on the
368c2ecf20Sopenharmony_ci * local blade, use a fixed TGH that is a function of the blade-local cpu
378c2ecf20Sopenharmony_ci * number. Normally, this TGH is private to the cpu & no contention occurs for
388c2ecf20Sopenharmony_ci * the TGH. For offblade GRUs, select a random TGH in the range above the
398c2ecf20Sopenharmony_ci * private TGHs. A spinlock is required to access this TGH & the lock must be
408c2ecf20Sopenharmony_ci * released when the invalidate is completes. This sucks, but it is the best we
418c2ecf20Sopenharmony_ci * can do.
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * Note that the spinlock is IN the TGH handle so locking does not involve
448c2ecf20Sopenharmony_ci * additional cache lines.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic inline int get_off_blade_tgh(struct gru_state *gru)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	int n;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	n = GRU_NUM_TGH - gru->gs_tgh_first_remote;
528c2ecf20Sopenharmony_ci	n = gru_random() % n;
538c2ecf20Sopenharmony_ci	n += gru->gs_tgh_first_remote;
548c2ecf20Sopenharmony_ci	return n;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic inline int get_on_blade_tgh(struct gru_state *gru)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	return uv_blade_processor_id() >> gru->gs_tgh_local_shift;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic struct gru_tlb_global_handle *get_lock_tgh_handle(struct gru_state
638c2ecf20Sopenharmony_ci							 *gru)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct gru_tlb_global_handle *tgh;
668c2ecf20Sopenharmony_ci	int n;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	preempt_disable();
698c2ecf20Sopenharmony_ci	if (uv_numa_blade_id() == gru->gs_blade_id)
708c2ecf20Sopenharmony_ci		n = get_on_blade_tgh(gru);
718c2ecf20Sopenharmony_ci	else
728c2ecf20Sopenharmony_ci		n = get_off_blade_tgh(gru);
738c2ecf20Sopenharmony_ci	tgh = get_tgh_by_index(gru, n);
748c2ecf20Sopenharmony_ci	lock_tgh_handle(tgh);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return tgh;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void get_unlock_tgh_handle(struct gru_tlb_global_handle *tgh)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	unlock_tgh_handle(tgh);
828c2ecf20Sopenharmony_ci	preempt_enable();
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * gru_flush_tlb_range
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * General purpose TLB invalidation function. This function scans every GRU in
898c2ecf20Sopenharmony_ci * the ENTIRE system (partition) looking for GRUs where the specified MM has
908c2ecf20Sopenharmony_ci * been accessed by the GRU. For each GRU found, the TLB must be invalidated OR
918c2ecf20Sopenharmony_ci * the ASID invalidated. Invalidating an ASID causes a new ASID to be assigned
928c2ecf20Sopenharmony_ci * on the next fault. This effectively flushes the ENTIRE TLB for the MM at the
938c2ecf20Sopenharmony_ci * cost of (possibly) a large number of future TLBmisses.
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci * The current algorithm is optimized based on the following (somewhat true)
968c2ecf20Sopenharmony_ci * assumptions:
978c2ecf20Sopenharmony_ci * 	- GRU contexts are not loaded into a GRU unless a reference is made to
988c2ecf20Sopenharmony_ci * 	  the data segment or control block (this is true, not an assumption).
998c2ecf20Sopenharmony_ci * 	  If a DS/CB is referenced, the user will also issue instructions that
1008c2ecf20Sopenharmony_ci * 	  cause TLBmisses. It is not necessary to optimize for the case where
1018c2ecf20Sopenharmony_ci * 	  contexts are loaded but no instructions cause TLB misses. (I know
1028c2ecf20Sopenharmony_ci * 	  this will happen but I'm not optimizing for it).
1038c2ecf20Sopenharmony_ci * 	- GRU instructions to invalidate TLB entries are SLOOOOWWW - normally
1048c2ecf20Sopenharmony_ci * 	  a few usec but in unusual cases, it could be longer. Avoid if
1058c2ecf20Sopenharmony_ci * 	  possible.
1068c2ecf20Sopenharmony_ci * 	- intrablade process migration between cpus is not frequent but is
1078c2ecf20Sopenharmony_ci * 	  common.
1088c2ecf20Sopenharmony_ci * 	- a GRU context is not typically migrated to a different GRU on the
1098c2ecf20Sopenharmony_ci * 	  blade because of intrablade migration
1108c2ecf20Sopenharmony_ci *	- interblade migration is rare. Processes migrate their GRU context to
1118c2ecf20Sopenharmony_ci *	  the new blade.
1128c2ecf20Sopenharmony_ci *	- if interblade migration occurs, migration back to the original blade
1138c2ecf20Sopenharmony_ci *	  is very very rare (ie., no optimization for this case)
1148c2ecf20Sopenharmony_ci *	- most GRU instruction operate on a subset of the user REGIONS. Code
1158c2ecf20Sopenharmony_ci *	  & shared library regions are not likely targets of GRU instructions.
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * To help improve the efficiency of TLB invalidation, the GMS data
1188c2ecf20Sopenharmony_ci * structure is maintained for EACH address space (MM struct). The GMS is
1198c2ecf20Sopenharmony_ci * also the structure that contains the pointer to the mmu callout
1208c2ecf20Sopenharmony_ci * functions. This structure is linked to the mm_struct for the address space
1218c2ecf20Sopenharmony_ci * using the mmu "register" function. The mmu interfaces are used to
1228c2ecf20Sopenharmony_ci * provide the callbacks for TLB invalidation. The GMS contains:
1238c2ecf20Sopenharmony_ci *
1248c2ecf20Sopenharmony_ci * 	- asid[maxgrus] array. ASIDs are assigned to a GRU when a context is
1258c2ecf20Sopenharmony_ci * 	  loaded into the GRU.
1268c2ecf20Sopenharmony_ci * 	- asidmap[maxgrus]. bitmap to make it easier to find non-zero asids in
1278c2ecf20Sopenharmony_ci * 	  the above array
1288c2ecf20Sopenharmony_ci *	- ctxbitmap[maxgrus]. Indicates the contexts that are currently active
1298c2ecf20Sopenharmony_ci *	  in the GRU for the address space. This bitmap must be passed to the
1308c2ecf20Sopenharmony_ci *	  GRU to do an invalidate.
1318c2ecf20Sopenharmony_ci *
1328c2ecf20Sopenharmony_ci * The current algorithm for invalidating TLBs is:
1338c2ecf20Sopenharmony_ci * 	- scan the asidmap for GRUs where the context has been loaded, ie,
1348c2ecf20Sopenharmony_ci * 	  asid is non-zero.
1358c2ecf20Sopenharmony_ci * 	- for each gru found:
1368c2ecf20Sopenharmony_ci * 		- if the ctxtmap is non-zero, there are active contexts in the
1378c2ecf20Sopenharmony_ci * 		  GRU. TLB invalidate instructions must be issued to the GRU.
1388c2ecf20Sopenharmony_ci *		- if the ctxtmap is zero, no context is active. Set the ASID to
1398c2ecf20Sopenharmony_ci *		  zero to force a full TLB invalidation. This is fast but will
1408c2ecf20Sopenharmony_ci *		  cause a lot of TLB misses if the context is reloaded onto the
1418c2ecf20Sopenharmony_ci *		  GRU
1428c2ecf20Sopenharmony_ci *
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_civoid gru_flush_tlb_range(struct gru_mm_struct *gms, unsigned long start,
1468c2ecf20Sopenharmony_ci			 unsigned long len)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct gru_state *gru;
1498c2ecf20Sopenharmony_ci	struct gru_mm_tracker *asids;
1508c2ecf20Sopenharmony_ci	struct gru_tlb_global_handle *tgh;
1518c2ecf20Sopenharmony_ci	unsigned long num;
1528c2ecf20Sopenharmony_ci	int grupagesize, pagesize, pageshift, gid, asid;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* ZZZ TODO - handle huge pages */
1558c2ecf20Sopenharmony_ci	pageshift = PAGE_SHIFT;
1568c2ecf20Sopenharmony_ci	pagesize = (1UL << pageshift);
1578c2ecf20Sopenharmony_ci	grupagesize = GRU_PAGESIZE(pageshift);
1588c2ecf20Sopenharmony_ci	num = min(((len + pagesize - 1) >> pageshift), GRUMAXINVAL);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	STAT(flush_tlb);
1618c2ecf20Sopenharmony_ci	gru_dbg(grudev, "gms %p, start 0x%lx, len 0x%lx, asidmap 0x%lx\n", gms,
1628c2ecf20Sopenharmony_ci		start, len, gms->ms_asidmap[0]);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	spin_lock(&gms->ms_asid_lock);
1658c2ecf20Sopenharmony_ci	for_each_gru_in_bitmap(gid, gms->ms_asidmap) {
1668c2ecf20Sopenharmony_ci		STAT(flush_tlb_gru);
1678c2ecf20Sopenharmony_ci		gru = GID_TO_GRU(gid);
1688c2ecf20Sopenharmony_ci		asids = gms->ms_asids + gid;
1698c2ecf20Sopenharmony_ci		asid = asids->mt_asid;
1708c2ecf20Sopenharmony_ci		if (asids->mt_ctxbitmap && asid) {
1718c2ecf20Sopenharmony_ci			STAT(flush_tlb_gru_tgh);
1728c2ecf20Sopenharmony_ci			asid = GRUASID(asid, start);
1738c2ecf20Sopenharmony_ci			gru_dbg(grudev,
1748c2ecf20Sopenharmony_ci	"  FLUSH gruid %d, asid 0x%x, vaddr 0x%lx, vamask 0x%x, num %ld, cbmap 0x%x\n",
1758c2ecf20Sopenharmony_ci			      gid, asid, start, grupagesize, num, asids->mt_ctxbitmap);
1768c2ecf20Sopenharmony_ci			tgh = get_lock_tgh_handle(gru);
1778c2ecf20Sopenharmony_ci			tgh_invalidate(tgh, start, ~0, asid, grupagesize, 0,
1788c2ecf20Sopenharmony_ci				       num - 1, asids->mt_ctxbitmap);
1798c2ecf20Sopenharmony_ci			get_unlock_tgh_handle(tgh);
1808c2ecf20Sopenharmony_ci		} else {
1818c2ecf20Sopenharmony_ci			STAT(flush_tlb_gru_zero_asid);
1828c2ecf20Sopenharmony_ci			asids->mt_asid = 0;
1838c2ecf20Sopenharmony_ci			__clear_bit(gru->gs_gid, gms->ms_asidmap);
1848c2ecf20Sopenharmony_ci			gru_dbg(grudev,
1858c2ecf20Sopenharmony_ci	"  CLEARASID gruid %d, asid 0x%x, cbtmap 0x%x, asidmap 0x%lx\n",
1868c2ecf20Sopenharmony_ci				gid, asid, asids->mt_ctxbitmap,
1878c2ecf20Sopenharmony_ci				gms->ms_asidmap[0]);
1888c2ecf20Sopenharmony_ci		}
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci	spin_unlock(&gms->ms_asid_lock);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/*
1948c2ecf20Sopenharmony_ci * Flush the entire TLB on a chiplet.
1958c2ecf20Sopenharmony_ci */
1968c2ecf20Sopenharmony_civoid gru_flush_all_tlb(struct gru_state *gru)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct gru_tlb_global_handle *tgh;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	gru_dbg(grudev, "gid %d\n", gru->gs_gid);
2018c2ecf20Sopenharmony_ci	tgh = get_lock_tgh_handle(gru);
2028c2ecf20Sopenharmony_ci	tgh_invalidate(tgh, 0, ~0, 0, 1, 1, GRUMAXINVAL - 1, 0xffff);
2038c2ecf20Sopenharmony_ci	get_unlock_tgh_handle(tgh);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/*
2078c2ecf20Sopenharmony_ci * MMUOPS notifier callout functions
2088c2ecf20Sopenharmony_ci */
2098c2ecf20Sopenharmony_cistatic int gru_invalidate_range_start(struct mmu_notifier *mn,
2108c2ecf20Sopenharmony_ci			const struct mmu_notifier_range *range)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,
2138c2ecf20Sopenharmony_ci						 ms_notifier);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	STAT(mmu_invalidate_range);
2168c2ecf20Sopenharmony_ci	atomic_inc(&gms->ms_range_active);
2178c2ecf20Sopenharmony_ci	gru_dbg(grudev, "gms %p, start 0x%lx, end 0x%lx, act %d\n", gms,
2188c2ecf20Sopenharmony_ci		range->start, range->end, atomic_read(&gms->ms_range_active));
2198c2ecf20Sopenharmony_ci	gru_flush_tlb_range(gms, range->start, range->end - range->start);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return 0;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic void gru_invalidate_range_end(struct mmu_notifier *mn,
2258c2ecf20Sopenharmony_ci			const struct mmu_notifier_range *range)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,
2288c2ecf20Sopenharmony_ci						 ms_notifier);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* ..._and_test() provides needed barrier */
2318c2ecf20Sopenharmony_ci	(void)atomic_dec_and_test(&gms->ms_range_active);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	wake_up_all(&gms->ms_wait_queue);
2348c2ecf20Sopenharmony_ci	gru_dbg(grudev, "gms %p, start 0x%lx, end 0x%lx\n",
2358c2ecf20Sopenharmony_ci		gms, range->start, range->end);
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic struct mmu_notifier *gru_alloc_notifier(struct mm_struct *mm)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct gru_mm_struct *gms;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	gms = kzalloc(sizeof(*gms), GFP_KERNEL);
2438c2ecf20Sopenharmony_ci	if (!gms)
2448c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2458c2ecf20Sopenharmony_ci	STAT(gms_alloc);
2468c2ecf20Sopenharmony_ci	spin_lock_init(&gms->ms_asid_lock);
2478c2ecf20Sopenharmony_ci	init_waitqueue_head(&gms->ms_wait_queue);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return &gms->ms_notifier;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic void gru_free_notifier(struct mmu_notifier *mn)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	kfree(container_of(mn, struct gru_mm_struct, ms_notifier));
2558c2ecf20Sopenharmony_ci	STAT(gms_free);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic const struct mmu_notifier_ops gru_mmuops = {
2598c2ecf20Sopenharmony_ci	.invalidate_range_start	= gru_invalidate_range_start,
2608c2ecf20Sopenharmony_ci	.invalidate_range_end	= gru_invalidate_range_end,
2618c2ecf20Sopenharmony_ci	.alloc_notifier		= gru_alloc_notifier,
2628c2ecf20Sopenharmony_ci	.free_notifier		= gru_free_notifier,
2638c2ecf20Sopenharmony_ci};
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistruct gru_mm_struct *gru_register_mmu_notifier(void)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	struct mmu_notifier *mn;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	mn = mmu_notifier_get_locked(&gru_mmuops, current->mm);
2708c2ecf20Sopenharmony_ci	if (IS_ERR(mn))
2718c2ecf20Sopenharmony_ci		return ERR_CAST(mn);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return container_of(mn, struct gru_mm_struct, ms_notifier);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_civoid gru_drop_mmu_notifier(struct gru_mm_struct *gms)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	mmu_notifier_put(&gms->ms_notifier);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/*
2828c2ecf20Sopenharmony_ci * Setup TGH parameters. There are:
2838c2ecf20Sopenharmony_ci * 	- 24 TGH handles per GRU chiplet
2848c2ecf20Sopenharmony_ci * 	- a portion (MAX_LOCAL_TGH) of the handles are reserved for
2858c2ecf20Sopenharmony_ci * 	  use by blade-local cpus
2868c2ecf20Sopenharmony_ci * 	- the rest are used by off-blade cpus. This usage is
2878c2ecf20Sopenharmony_ci * 	  less frequent than blade-local usage.
2888c2ecf20Sopenharmony_ci *
2898c2ecf20Sopenharmony_ci * For now, use 16 handles for local flushes, 8 for remote flushes. If the blade
2908c2ecf20Sopenharmony_ci * has less tan or equal to 16 cpus, each cpu has a unique handle that it can
2918c2ecf20Sopenharmony_ci * use.
2928c2ecf20Sopenharmony_ci */
2938c2ecf20Sopenharmony_ci#define MAX_LOCAL_TGH	16
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_civoid gru_tgh_flush_init(struct gru_state *gru)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	int cpus, shift = 0, n;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	cpus = uv_blade_nr_possible_cpus(gru->gs_blade_id);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	/* n = cpus rounded up to next power of 2 */
3028c2ecf20Sopenharmony_ci	if (cpus) {
3038c2ecf20Sopenharmony_ci		n = 1 << fls(cpus - 1);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		/*
3068c2ecf20Sopenharmony_ci		 * shift count for converting local cpu# to TGH index
3078c2ecf20Sopenharmony_ci		 *      0 if cpus <= MAX_LOCAL_TGH,
3088c2ecf20Sopenharmony_ci		 *      1 if cpus <= 2*MAX_LOCAL_TGH,
3098c2ecf20Sopenharmony_ci		 *      etc
3108c2ecf20Sopenharmony_ci		 */
3118c2ecf20Sopenharmony_ci		shift = max(0, fls(n - 1) - fls(MAX_LOCAL_TGH - 1));
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci	gru->gs_tgh_local_shift = shift;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* first starting TGH index to use for remote purges */
3168c2ecf20Sopenharmony_ci	gru->gs_tgh_first_remote = (cpus + (1 << shift) - 1) >> shift;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci}
319