trace: Reduce the number of function sites

Given that the compiler adds two function calls into each function, the
current spacing is overkill. Drop it down to 16 bytes per function, which
is still plenty. This saves some space in the trace buffer.

Also move the calculation into a function, so it is common code. Add a
check for gd->mon_len being unset, which breaks tracing.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/include/trace.h b/include/trace.h
index 2be8d81..763d6d1 100644
--- a/include/trace.h
+++ b/include/trace.h
@@ -17,14 +17,16 @@
 	 *
 	 * The value here assumes a minimum instruction size of 4 bytes,
 	 * or that instructions are 2 bytes but there are at least 2 of
-	 * them in every function.
+	 * them in every function. Given that each function needs a call to
+	 * __cyg_profile_func_enter() and __cyg_profile_func_exit() as well,
+	 * we cannot have functions smaller that 16 bytes.
 	 *
 	 * Increasing this value reduces the number of functions we can
 	 * resolve, but reduces the size of the uintptr_t array used for
 	 * our function list, which is the length of the code divided by
 	 * this value.
 	 */
-	FUNC_SITE_SIZE	= 4,	/* distance between function sites */
+	FUNC_SITE_SIZE	= 16,	/* distance between function sites */
 
 	TRACE_VERSION	= 1,
 };
diff --git a/lib/trace.c b/lib/trace.c
index 2e2c1be..12dae20 100644
--- a/lib/trace.c
+++ b/lib/trace.c
@@ -321,6 +321,17 @@
 	trace_enabled = enabled != 0;
 }
 
+static int get_func_count(void)
+{
+	/* Detect no support for mon_len since this means tracing cannot work */
+	if (IS_ENABLED(CONFIG_SANDBOX) && !gd->mon_len) {
+		puts("Tracing is not supported on this board\n");
+		return -ENOTSUPP;
+	}
+
+	return gd->mon_len / FUNC_SITE_SIZE;
+}
+
 /**
  * trace_init() - initialize the tracing system and enable it
  *
@@ -330,10 +341,12 @@
  */
 int notrace trace_init(void *buff, size_t buff_size)
 {
-	ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
+	int func_count = get_func_count();
 	size_t needed;
 	int was_disabled = !trace_enabled;
 
+	if (func_count < 0)
+		return func_count;
 	trace_save_gd();
 
 	if (!was_disabled) {
@@ -393,10 +406,12 @@
  */
 int notrace trace_early_init(void)
 {
-	ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
+	int func_count = get_func_count();
 	size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
 	size_t needed;
 
+	if (func_count < 0)
+		return func_count;
 	/* We can ignore additional calls to this function */
 	if (trace_enabled)
 		return 0;