Skip to content

Commit 604804e

Browse files
committed
mpfs_opensbi: Re-organize SBI areas so that RW areas follow each other
Change the ordering of the SBI areas so that: - The first area is the executable area (.text) - The second area is the heap (RW) - The last area is the scratch registers (RW) This makes it easier to encode PMP areas for OpenSBI.
1 parent bc9dd58 commit 604804e

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

arch/risc-v/src/mpfs/mpfs_opensbi.c

+39-16
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ extern const uint8_t __mpfs_nuttx_start[];
105105
extern const uint8_t __mpfs_nuttx_end[];
106106
extern const uint8_t _ssbi_zerodev[];
107107
extern const uint8_t _esbi_zerodev[];
108+
extern const uint8_t _sbi_heap_start[];
109+
extern const uint8_t _sbi_heap_size[];
108110

109111
/****************************************************************************
110112
* Private Function Prototypes
@@ -470,27 +472,41 @@ static void mpfs_opensbi_scratch_setup(uint32_t hartid)
470472
(unsigned long)mpfs_hart_to_scratch;
471473
g_scratches[hartid].scratch.platform_addr = (unsigned long)&platform;
472474

473-
/* Our FW area in l2lim section. OpenSBI needs to be aware of it in order
474-
* to protect the area. However, we set the PMP values already and lock
475-
* them so that OpenSBI has no chance override then.
475+
/* Our FW area, heap and the scratch area are in l2lim.
476+
*
477+
* The memory is organized as follows:
478+
*
479+
* ----------------------------------------------------------------------
480+
* | | | |
481+
* | SBI .text (0000h) | SBI heap (8000h) | SBI scratch (8000h + 4000h) |
482+
* | | | |
483+
* ----------------------------------------------------------------------
484+
*
485+
* The reason for this organization is that the RX and RW areas can be set
486+
* to the physical memory protection unit (PMP) as two contiguous areas.
476487
*/
477488

478489
g_scratches[hartid].scratch.fw_start = (unsigned long)_ssbi_zerodev;
479490
g_scratches[hartid].scratch.fw_size = (unsigned long)_esbi_zerodev -
480491
(unsigned long)_ssbi_zerodev;
481492

482-
g_scratches[hartid].scratch.fw_rw_offset =
483-
(unsigned long)g_scratches[hartid].scratch.fw_size;
493+
/* RW area starts from the heap */
484494

485-
/* fw_rw_offset needs to be an aligned address */
495+
g_scratches[hartid].scratch.fw_heap_offset =
496+
(unsigned long)_sbi_heap_start -
497+
(unsigned long)g_scratches[hartid].scratch.fw_start;
486498

487-
g_scratches[hartid].scratch.fw_rw_offset += 1024 * 2;
488-
g_scratches[hartid].scratch.fw_rw_offset &= 0xffffff800;
489-
g_scratches[hartid].scratch.fw_size =
490-
g_scratches[hartid].scratch.fw_rw_offset;
499+
g_scratches[hartid].scratch.fw_heap_size =
500+
(unsigned long)_sbi_heap_size;
491501

492-
g_scratches[hartid].scratch.fw_heap_offset =
493-
(unsigned long)g_scratches[hartid].scratch.fw_size;
502+
g_scratches[hartid].scratch.fw_rw_offset =
503+
g_scratches[hartid].scratch.fw_heap_offset;
504+
505+
/* Because sbi_heap_init does not work otherwise */
506+
507+
g_scratches[hartid].scratch.fw_size =
508+
g_scratches[hartid].scratch.fw_heap_offset +
509+
g_scratches[hartid].scratch.fw_heap_size;
494510

495511
/* Heap minimum is 16k. Otherwise sbi_heap.c fails:
496512
* hpctrl.hksize = hpctrl.size / HEAP_HOUSEKEEPING_FACTOR;
@@ -500,10 +516,17 @@ static void mpfs_opensbi_scratch_setup(uint32_t hartid)
500516
* hpctrl.hksize gets to be zero making the OpenSBI crash.
501517
*/
502518

503-
g_scratches[hartid].scratch.fw_heap_size = 1024 * 16;
504-
g_scratches[hartid].scratch.fw_size =
505-
g_scratches[hartid].scratch.fw_heap_offset +
506-
g_scratches[hartid].scratch.fw_heap_size;
519+
if (g_scratches[hartid].scratch.fw_heap_size < 0x4000)
520+
{
521+
sbi_panic(__func__);
522+
}
523+
524+
/* fw_rw_offset needs to be an aligned address */
525+
526+
if (g_scratches[hartid].scratch.fw_rw_offset & 0x7FF)
527+
{
528+
sbi_panic(__func__);
529+
}
507530
}
508531

509532
/****************************************************************************

0 commit comments

Comments
 (0)