Skip to contentSkip to conversation

bootstage: TPL and SPL improvements

PATCH Superseded Simon Glass 1 participant 10 messages Oct 14, 2019 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bootstage: TPL and SPL improvements

At present bootstage cannot be fully used on x86 since it violates a few U-Boot rules, mostly accessing pre-relocation memory after relocation. This series corrects this and adds better support for using bootstage in TPL.

Simon Glass (9):
tiny-printf: Reduce size by removing ctype
tiny-printf: Add print_grouped_ull()
bloblist: Reserve an aligned base
bootstage: Store the next ID in the stash
bootstage: Fix counting of entries in stash
bootstage: Avoid conflicts between stash/unstash
bootstage: Correct relocation algorithm
bootstage: Mark the start/end of TPL and SPL separately
bootstage: Allow SPL to obtain bootstage info from TPL

 common/board_f.c    |  2 ++
 common/board_r.c    |  1 -
 common/bootstage.c  | 53 ++++++++++++++++++++++++++++++---------------
 common/spl/spl.c    | 25 +++++++++++++++++----
 include/bootstage.h |  2 ++
 lib/tiny-printf.c   | 13 +++++++++++
 6 files changed, 73 insertions(+), 23 deletions(-)
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#1 lore raw

PATCH 1/9 tiny-printf: Reduce size by removing ctype

The ctype array is brought into the image, adding 256 bytes, when it is unlikely to be needed. The extra code for %p is only present when DEBUG is defined, so let's drop ctype as well unless DEBUG is defined.

---

 lib/tiny-printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/tiny-printf.c +7 -0
index ebef92fc9f6..632b4249142 100644
@@ -289,8 +289,15 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
 				break;
 			case 'p':
 				pointer(info, fmt, va_arg(va, void *));
+				/*
+				 * Skip this because it pulls in _ctype which is
+				 * 256 bytes, and we don't generally implement
+				 * pointer anyway
+				 */
+#ifdef DEBUG
 				while (isalnum(fmt[0]))
 					fmt++;
+#endif
 				break;
 			case '%':
 				out(info, '%');
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#2 lore raw

PATCH 2/9 tiny-printf: Add print_grouped_ull()

In reply to Simon Glass

This function is used in the bootstage report which may be triggered in sPL or TPL. Add a very basic implication of this function so that it builds. There is no attempt to get the formatting right, since this would add too much to code size.

---

 lib/tiny-printf.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/tiny-printf.c +6 -0
index 632b4249142..430c3255bc5 100644
@@ -389,3 +389,9 @@ int snprintf(char *buf, size_t size, const char *fmt, ...)
 
 	return ret;
 }
+
+void print_grouped_ull(unsigned long long int_val, int digits)
+{
+	/* Don't try to ptint the upper 32-bits */
+	printf("%ld ", (ulong)int_val);
+}
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#3 lore raw

PATCH 3/9 bloblist: Reserve an aligned base

In reply to Simon Glass

Make sure that the bloblist starts on an aligned boundary. This protects against one of the earlier allocations causing the alignment to be lost.

---

 common/board_f.c | 1 +
 1 file changed, 1 insertion(+)
common/board_f.c +1 -0
index 591f18f391e..4852a3b0d84 100644
@@ -588,6 +588,7 @@ static int reserve_stacks(void)
 static int reserve_bloblist(void)
 {
 #ifdef CONFIG_BLOBLIST
+	gd->start_addr_sp &= ~0xf;
 	gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
 	gd->new_bloblist = map_sysmem(gd->start_addr_sp, CONFIG_BLOBLIST_SIZE);
 #endif
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#4 lore raw

PATCH 4/9 bootstage: Store the next ID in the stash

In reply to Simon Glass

When stashing bootstage info, store the next ID so that it can be used when the stash is restored. This avoids the ID starting at zero and potentially overwriting existing entries.

---

 common/bootstage.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
common/bootstage.c +7 -4
index 56ef91ad859..257dc5d402f 100644
@@ -41,10 +41,11 @@ enum {
 };
 
 struct bootstage_hdr {
-	uint32_t version;	/* BOOTSTAGE_VERSION */
-	uint32_t count;		/* Number of records */
-	uint32_t size;		/* Total data size (non-zero if valid) */
-	uint32_t magic;		/* Unused */
+	u32 version;		/* BOOTSTAGE_VERSION */
+	u32 count;		/* Number of records */
+	u32 size;		/* Total data size (non-zero if valid) */
+	u32 magic;		/* Magic number */
+	u32 next_id;		/* Next ID to use for bootstage */
 };
 
 int bootstage_relocate(void)
@@ -392,6 +393,7 @@ int bootstage_stash(void *base, int size)
 	hdr->count = count;
 	hdr->size = 0;
 	hdr->magic = BOOTSTAGE_MAGIC;
+	hdr->next_id = data->next_id;
 	ptr += sizeof(*hdr);
 
 	/* Write the records, silently stopping when we run out of space */
@@ -485,6 +487,7 @@ int bootstage_unstash(const void *base, int size)
 
 	/* Mark the records as read */
 	data->rec_count += hdr->count;
+	data->next_id = hdr->next_id;
 	debug("Unstashed %d records\n", hdr->count);
 
 	return 0;
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#5 lore raw

PATCH 5/9 bootstage: Fix counting of entries in stash

In reply to Simon Glass

The current code searches for empty records but these do not exist with bootstage now. This used to be needed when bootstage records were stored in a sparse array.

Drop the unnecessary code and fix a code-style nit at the same time.

---

 common/bootstage.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
common/bootstage.c +2 -10
index 257dc5d402f..fe36bac0474 100644
@@ -373,7 +373,6 @@ int bootstage_stash(void *base, int size)
 	const struct bootstage_record *rec;
 	char buf[20];
 	char *ptr = base, *end = ptr + size;
-	uint32_t count;
 	int i;
 
 	if (hdr + 1 > (struct bootstage_hdr *)end) {
@@ -384,22 +383,15 @@ int bootstage_stash(void *base, int size)
 	/* Write an arbitrary version number */
 	hdr->version = BOOTSTAGE_VERSION;
 
-	/* Count the number of records, and write that value first */
-	for (rec = data->record, i = count = 0; i < data->rec_count;
-	     i++, rec++) {
-		if (rec->id != 0)
-			count++;
-	}
-	hdr->count = count;
+	hdr->count = data->rec_count;
 	hdr->size = 0;
 	hdr->magic = BOOTSTAGE_MAGIC;
 	hdr->next_id = data->next_id;
 	ptr += sizeof(*hdr);
 
 	/* Write the records, silently stopping when we run out of space */
-	for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
+	for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
 		append_data(&ptr, end, rec, sizeof(*rec));
-	}
 
 	/* Write the name strings */
 	for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#6 lore raw

PATCH 6/9 bootstage: Avoid conflicts between stash/unstash

In reply to Simon Glass

At present there is a single shared address for bootstage data in both TPL and SPL. If SPL unstashs TPL bootstage info and then stashes it again to pass it to U-Boot, the new stash overwrites the strings of the old stash.

Fix this by duplicating the strings into the malloc() region. This should be a small cost in terms of space. Fix the header-file order at the same time.

This problem doesn't happen at the next stage (SPL->U-Boot) since U-Boot relocates the boostage data.

---

 common/bootstage.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
common/bootstage.c +4 -1
index fe36bac0474..4557ed4508c 100644
@@ -10,9 +10,10 @@
  */
 
 #include <common.h>
-#include <linux/libfdt.h>
 #include <malloc.h>
+#include <spl.h>
 #include <linux/compiler.h>
+#include <linux/libfdt.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -472,6 +473,8 @@ int bootstage_unstash(const void *base, int size)
 	for (rec = data->record + data->next_id, i = 0; i < hdr->count;
 	     i++, rec++) {
 		rec->name = ptr;
+		if (spl_phase() == PHASE_SPL)
+			rec->name = strdup(ptr);
 
 		/* Assume no data corruption here */
 		ptr += strlen(ptr) + 1;
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#7 lore raw

PATCH 7/9 bootstage: Correct relocation algorithm

In reply to Simon Glass

At present bootstage relocation assumes that it is possible to point back to memory available before relocation, so it does not relocate the strings. However this is not the case on some platforms, such as x86 which uses the cache as RAM and loses access to this when the cache is enabled.

Move the relocation step to before U-Boot relocates, expand the allocated region to include space for the strings and relocate the strings at the same time as the bootstage records.

This ensures that bootstage data can remain accessible from TPL through SPL to U-Boot before/after relocation.

---

 common/board_f.c   |  1 +
 common/board_r.c   |  1 -
 common/bootstage.c | 25 ++++++++++++++++++++++---
 3 files changed, 23 insertions(+), 4 deletions(-)
common/board_f.c +1 -0
index 4852a3b0d84..e3591cbaebd 100644
@@ -696,6 +696,7 @@ static int reloc_bootstage(void)
 		      gd->bootstage, gd->new_bootstage, size);
 		memcpy(gd->new_bootstage, gd->bootstage, size);
 		gd->bootstage = gd->new_bootstage;
+		bootstage_relocate();
 	}
 #endif
 
common/board_r.c +0 -1
index d6fb5047a26..c1ecb06b743 100644
@@ -670,7 +670,6 @@ static init_fnc_t init_sequence_r[] = {
 #ifdef CONFIG_SYS_NONCACHED_MEMORY
 	initr_noncached,
 #endif
-	bootstage_relocate,
 #ifdef CONFIG_OF_LIVE
 	initr_of_live,
 #endif
common/bootstage.c +22 -3
index 4557ed4508c..e8b7bbf81a6 100644
@@ -53,14 +53,23 @@ int bootstage_relocate(void)
 {
 	struct bootstage_data *data = gd->bootstage;
 	int i;
+	char *ptr;
+
+	/* Figure out where to relocate the strings to */
+	ptr = (char *)(data + 1);
 
 	/*
 	 * Duplicate all strings.  They may point to an old location in the
 	 * program .text section that can eventually get trashed.
 	 */
 	debug("Relocating %d records\n", data->rec_count);
-	for (i = 0; i < data->rec_count; i++)
-		data->record[i].name = strdup(data->record[i].name);
+	for (i = 0; i < data->rec_count; i++) {
+		const char *from = data->record[i].name;
+
+		strcpy(ptr, from);
+		data->record[i].name = ptr;
+		ptr += strlen(ptr) + 1;
+	}
 
 	return 0;
 }
@@ -490,7 +499,17 @@ int bootstage_unstash(const void *base, int size)
 
 int bootstage_get_size(void)
 {
-	return sizeof(struct bootstage_data);
+	struct bootstage_data *data = gd->bootstage;
+	struct bootstage_record *rec;
+	int size;
+	int i;
+
+	size = sizeof(struct bootstage_data);
+	for (rec = data->record, i = 0; i < data->rec_count;
+	     i++, rec++)
+		size += strlen(rec->name) + 1;
+
+	return size;
 }
 
 int bootstage_init(bool first)
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#8 lore raw

PATCH 8/9 bootstage: Mark the start/end of TPL and SPL separately

In reply to Simon Glass

At present bootstage in TPL and SPL use the same ID so it is not possible to see the timing of each. Separate out the IDs and use the correct one depending on which phase we are at.

Example output:

Timer summary in microseconds (14 records):

       Mark    Elapsed  Stage
          0          0  reset
    224,787    224,787  TPL
    282,248     57,461  end TPL
    341,067     58,819  SPL
    925,436    584,369  end SPL
    931,710      6,274  board_init_f
  1,035,482    103,772  board_init_r
  1,387,852    352,370  main_loop
  1,387,911         59  id=175

Accumulated time:

                   196  dm_r
                 8,300  dm_spl
                14,139  dm_f
               229,121  fsp-m
               262,992  fsp-s

---

 common/spl/spl.c    | 9 ++++++---
 include/bootstage.h | 2 ++
 2 files changed, 8 insertions(+), 3 deletions(-)
common/spl/spl.c +6 -3
index a9d3e847af5..eabb1fbc138 100644
@@ -402,7 +402,8 @@ static int spl_common_init(bool setup_malloc)
 		      ret);
 		return ret;
 	}
-	bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
+	bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_START_TPL :
+			    BOOTSTAGE_ID_START_SPL, SPL_TPL_NAME);
 #if CONFIG_IS_ENABLED(LOG)
 	ret = log_init();
 	if (ret) {
@@ -418,7 +419,8 @@ static int spl_common_init(bool setup_malloc)
 		}
 	}
 	if (CONFIG_IS_ENABLED(DM)) {
-		bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
+		bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL,
+				spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
 		/* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
 		ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
 		bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
@@ -704,8 +706,9 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
 	debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
 	      gd->malloc_ptr / 1024);
 #endif
+	bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_END_TPL :
+			    BOOTSTAGE_ID_END_SPL, "end " SPL_TPL_NAME);
 #ifdef CONFIG_BOOTSTAGE_STASH
-	bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
 	ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
 			      CONFIG_BOOTSTAGE_STASH_SIZE);
 	if (ret)
include/bootstage.h +2 -0
index 5e7e242b834..d105ae01813 100644
@@ -170,6 +170,8 @@ enum bootstage_id {
 	 * rough boot timing information.
 	 */
 	BOOTSTAGE_ID_AWAKE,
+	BOOTSTAGE_ID_START_TPL,
+	BOOTSTAGE_ID_END_TPL,
 	BOOTSTAGE_ID_START_SPL,
 	BOOTSTAGE_ID_END_SPL,
 	BOOTSTAGE_ID_START_UBOOT_F,
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#9 lore raw

PATCH 9/9 bootstage: Allow SPL to obtain bootstage info from TPL

In reply to Simon Glass

It is possible to enable bootstage in TPL. TPL can stash the info for SPL. But at present this information is then lost because SPL does not read from the stash.

Add support for SPL not being the first phase to enable bootstage.

---

 common/spl/spl.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
common/spl/spl.c +15 -1
index eabb1fbc138..5fac3cbea2b 100644
@@ -18,6 +18,7 @@
 #include <version.h>
 #include <image.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <dm/root.h>
 #include <linux/compiler.h>
 #include <fdt_support.h>
@@ -396,12 +397,25 @@ static int spl_common_init(bool setup_malloc)
 		gd->malloc_ptr = 0;
 	}
 #endif
-	ret = bootstage_init(true);
+	ret = bootstage_init(u_boot_first_phase());
 	if (ret) {
 		debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
 		      ret);
 		return ret;
 	}
+#ifdef CONFIG_BOOTSTAGE_STASH
+	if (!u_boot_first_phase()) {
+		const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
+					       CONFIG_BOOTSTAGE_STASH_SIZE);
+
+		ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
+		if (ret) {
+			debug("%s: Failed to unstash bootstage: ret=%d\n",
+			      __func__, ret);
+			return ret;
+		}
+	}
+#endif /* CONFIG_BOOTSTAGE_STASH */
 	bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_START_TPL :
 			    BOOTSTAGE_ID_START_SPL, SPL_TPL_NAME);
 #if CONFIG_IS_ENABLED(LOG)
Signature · 1 line
2.23.0.700.g56cf767bdb-goog