acpi/acpigen: factor out acpigen_write_processor_namestring

This functionality is used in multiple places, so factor it out into a
function. Compared to acpigen_write_processor_cnot, the buffer size is
decreased from 40 to 16 bytes, but the format string specified by
CONFIG_ACPI_CPU_STRING results in 9 chars and a NULL byte which will fit
into the buffer without any issue. I've seen the CPU devices being put
into another scope within \_SB, but even in that case that would be 14
chars and a NULL byte whist still fits into the 16 byte buffer. For
acpigen_write_processor and acpigen_write_processor_package this doesn't
change any edge case behavior. In the unrealistic case of the format
string resulting in a longer CPU device string, this would have been a
problem before this patch too.

Also drop the curly braces of the for loop in
acpigen_write_processor_package. This makes the code a bit harder to
read and isn't a very good idea, but with the curly braces in place, the
linter breaks the build :(

Signed-off-by: Felix Held <felix-coreboot@felixheld.de>
Change-Id: I5d8291a2aaae2011cb185d72c7f7864b6e2220ec
Reviewed-on: https://review.coreboot.org/c/coreboot/+/72452
Reviewed-by: Fred Reitberger <reitbergerfred@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Matt DeVillier <matt.devillier@amd.corp-partner.google.com>
Reviewed-by: Elyes Haouas <ehaouas@noos.fr>
diff --git a/src/acpi/acpigen.c b/src/acpi/acpigen.c
index 08915a9..b8f1dfb 100644
--- a/src/acpi/acpigen.c
+++ b/src/acpi/acpigen.c
@@ -388,19 +388,22 @@
 	acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
 }
 
+void acpigen_write_processor_namestring(unsigned int cpu_index)
+{
+	char buffer[16];
+	snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING, cpu_index);
+	acpigen_emit_namestring(buffer);
+}
+
 void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
 {
 /*
 	Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
 	{
 */
-	char pscope[16];
 	acpigen_emit_ext_op(PROCESSOR_OP);
 	acpigen_write_len_f();
-
-	snprintf(pscope, sizeof(pscope),
-		 CONFIG_ACPI_CPU_STRING, (unsigned int)cpuindex);
-	acpigen_emit_namestring(pscope);
+	acpigen_write_processor_namestring(cpuindex);
 	acpigen_emit_byte(cpuindex);
 	acpigen_emit_dword(pblock_addr);
 	acpigen_emit_byte(pblock_len);
@@ -410,14 +413,13 @@
 				     const unsigned int core_count)
 {
 	unsigned int i;
-	char pscope[16];
 
 	acpigen_write_name(name);
 	acpigen_write_package(core_count);
-	for (i = first_core; i < first_core + core_count; ++i) {
-		snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
-		acpigen_emit_namestring(pscope);
-	}
+
+	for (i = first_core; i < first_core + core_count; ++i)
+		acpigen_write_processor_namestring(i);
+
 	acpigen_pop_len();
 }
 
@@ -428,10 +430,8 @@
 
 	acpigen_write_method("\\_SB.CNOT", 1);
 	for (core_id = 0; core_id < number_of_cores; core_id++) {
-		char buffer[DEVICE_PATH_MAX];
-		snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING, core_id);
 		acpigen_emit_byte(NOTIFY_OP);
-		acpigen_emit_namestring(buffer);
+		acpigen_write_processor_namestring(core_id);
 		acpigen_emit_byte(ARG0_OP);
 	}
 	acpigen_pop_len();
diff --git a/src/include/acpi/acpigen.h b/src/include/acpi/acpigen.h
index bb36707..ac0cab2 100644
--- a/src/include/acpi/acpigen.h
+++ b/src/include/acpi/acpigen.h
@@ -449,6 +449,7 @@
 void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value);
 void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values,
 			       size_t nentries);
+void acpigen_write_processor_namestring(unsigned int cpu_index);
 void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len);
 __always_inline void acpigen_write_processor_end(void)
 {