mirror of
				https://github.com/riscv-software-src/opensbi
				synced 2025-11-04 05:50:22 +00:00 
			
		
		
		
	A platform can have discontinuous and/or sparse HART ids so we cannot always assume a set of HARTs with continuous HART ids. This patch adds support for discontinuous and sparse HART ids by introducing HART index to HART id table. This table has platform hart_count entries and it maps HART index to HART id. The HART index to HART id table has only two restrictions: 1. HART index < sbi_platform hart_count 2. HART id < SBI_HARTMASK_MAX_BITS Example1: Let's say we have a platform with 2 HART ids 11 and 22, for such a a platform: hart_count = 2 hart_index2id[0] = 11 hart_index2id[1] = 22 Example2: Let's say we have a platform with 5 HARTs ids 0, 1, 2, 3, and 4 but out of these HART with id 0 is not usable so for such a platform: hart_count = 5 hart_index2id[0] = -1U hart_index2id[1] = 1 hart_index2id[2] = 2 hart_index2id[3] = 3 hart_index2id[4] = 4 OR hart_count = 4 hart_index2id[0] = 1 hart_index2id[1] = 2 hart_index2id[2] = 3 hart_index2id[3] = 4 With HART index to HART id table in place, the hart_disabled() callback is now redundant so we remove it as well. Signed-off-by: Anup Patel <anup.patel@wdc.com> Reviewed-by: Bin Meng <bmeng.cn@gmail.com> Reviewed-by: Atish Patra <atish.patra@wdc.com>
		
			
				
	
	
		
			97 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
 /*
 | 
						|
 * SPDX-License-Identifier: BSD-2-Clause
 | 
						|
 *
 | 
						|
 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
 | 
						|
 *
 | 
						|
 * Authors:
 | 
						|
 *   Anup Patel <anup.patel@wdc.com>
 | 
						|
 */
 | 
						|
 | 
						|
#include <sbi/riscv_locks.h>
 | 
						|
#include <sbi/sbi_hart.h>
 | 
						|
#include <sbi/sbi_hartmask.h>
 | 
						|
#include <sbi/sbi_platform.h>
 | 
						|
#include <sbi/sbi_scratch.h>
 | 
						|
#include <sbi/sbi_string.h>
 | 
						|
 | 
						|
struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
 | 
						|
 | 
						|
static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 | 
						|
static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 | 
						|
 | 
						|
typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
 | 
						|
 | 
						|
int sbi_scratch_init(struct sbi_scratch *scratch)
 | 
						|
{
 | 
						|
	u32 i;
 | 
						|
	const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 | 
						|
 | 
						|
	for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
 | 
						|
		if (sbi_platform_hart_invalid(plat, i))
 | 
						|
			continue;
 | 
						|
		hartid_to_scratch_table[i] =
 | 
						|
			((hartid2scratch)scratch->hartid_to_scratch)(i,
 | 
						|
					sbi_platform_hart_index(plat, i));
 | 
						|
	}
 | 
						|
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
unsigned long sbi_scratch_alloc_offset(unsigned long size, const char *owner)
 | 
						|
{
 | 
						|
	u32 i;
 | 
						|
	void *ptr;
 | 
						|
	unsigned long ret = 0;
 | 
						|
	struct sbi_scratch *rscratch;
 | 
						|
 | 
						|
	/*
 | 
						|
	 * We have a simple brain-dead allocator which never expects
 | 
						|
	 * anything to be free-ed hence it keeps incrementing the
 | 
						|
	 * next allocation offset until it runs-out of space.
 | 
						|
	 *
 | 
						|
	 * In future, we will have more sophisticated allocator which
 | 
						|
	 * will allow us to re-claim free-ed space.
 | 
						|
	 */
 | 
						|
 | 
						|
	if (!size)
 | 
						|
		return 0;
 | 
						|
 | 
						|
	if (size & (__SIZEOF_POINTER__ - 1))
 | 
						|
		size = (size & ~(__SIZEOF_POINTER__ - 1)) + __SIZEOF_POINTER__;
 | 
						|
 | 
						|
	spin_lock(&extra_lock);
 | 
						|
 | 
						|
	if (SBI_SCRATCH_SIZE < (extra_offset + size))
 | 
						|
		goto done;
 | 
						|
 | 
						|
	ret = extra_offset;
 | 
						|
	extra_offset += size;
 | 
						|
 | 
						|
done:
 | 
						|
	spin_unlock(&extra_lock);
 | 
						|
 | 
						|
	if (ret) {
 | 
						|
		for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
 | 
						|
			rscratch = sbi_hartid_to_scratch(i);
 | 
						|
			if (!rscratch)
 | 
						|
				continue;
 | 
						|
			ptr = sbi_scratch_offset_ptr(rscratch, ret);
 | 
						|
			sbi_memset(ptr, 0, size);
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	return ret;
 | 
						|
}
 | 
						|
 | 
						|
void sbi_scratch_free_offset(unsigned long offset)
 | 
						|
{
 | 
						|
	if ((offset < SBI_SCRATCH_EXTRA_SPACE_OFFSET) ||
 | 
						|
	    (SBI_SCRATCH_SIZE <= offset))
 | 
						|
		return;
 | 
						|
 | 
						|
	/*
 | 
						|
	 * We don't actually free-up because it's a simple
 | 
						|
	 * brain-dead allocator.
 | 
						|
	 */
 | 
						|
}
 |