851216d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
851216d
From: Daniel Axtens <dja@axtens.net>
851216d
Date: Mon, 6 Feb 2023 10:03:20 -0500
851216d
Subject: [PATCH] ieee1275: request memory with ibm,
851216d
 client-architecture-support
851216d
851216d
On PowerVM, the first time we boot a Linux partition, we may only get
851216d
256MB of real memory area, even if the partition has more memory.
851216d
851216d
This isn't enough to reliably verify a kernel. Fortunately, the Power
851216d
Architecture Platform Reference (PAPR) defines a method we can call to ask
851216d
for more memory: the broad and powerful ibm,client-architecture-support
851216d
(CAS) method.
851216d
851216d
CAS can do an enormous amount of things on a PAPR platform: as well as
851216d
asking for memory, you can set the supported processor level, the interrupt
851216d
controller, hash vs radix mmu, and so on.
851216d
851216d
If:
851216d
851216d
 - we are running under what we think is PowerVM (compatible property of /
851216d
   begins with "IBM"), and
851216d
851216d
 - the full amount of RMA is less than 512MB (as determined by the reg
851216d
   property of /memory)
851216d
851216d
then call CAS as follows: (refer to the Linux on Power Architecture
851216d
Reference, LoPAR, which is public, at B.5.2.3):
851216d
851216d
 - Use the "any" PVR value and supply 2 option vectors.
851216d
851216d
 - Set option vector 1 (PowerPC Server Processor Architecture Level)
851216d
   to "ignore".
851216d
851216d
 - Set option vector 2 with default or Linux-like options, including a
851216d
   min-rma-size of 512MB.
851216d
851216d
 - Set option vector 3 to request Floating Point, VMX and Decimal Floating
851216d
   point, but don't abort the boot if we can't get them.
851216d
851216d
 - Set option vector 4 to request a minimum VP percentage to 1%, which is
851216d
   what Linux requests, and is below the default of 10%. Without this,
851216d
   some systems with very large or very small configurations fail to boot.
851216d
851216d
This will cause a CAS reboot and the partition will restart with 512MB
851216d
of RMA. Importantly, grub will notice the 512MB and not call CAS again.
851216d
851216d
Notes about the choices of parameters:
851216d
851216d
 - A partition can be configured with only 256MB of memory, which would
851216d
   mean this request couldn't be satisfied, but PFW refuses to load with
851216d
   only 256MB of memory, so it's a bit moot. SLOF will run fine with 256MB,
851216d
   but we will never call CAS under qemu/SLOF because /compatible won't
851216d
   begin with "IBM".)
851216d
851216d
 - unspecified CAS vectors take on default values. Some of these values
851216d
   might restrict the ability of certain hardware configurations to boot.
851216d
   This is why we need to specify the VP percentage in vector 4, which is
851216d
   in turn why we need to specify vector 3.
851216d
851216d
Finally, we should have enough memory to verify a kernel, and we will
851216d
reach Linux. One of the first things Linux does while still running under
851216d
OpenFirmware is to call CAS with a much fuller set of options (including
851216d
asking for 512MB of memory). Linux includes a much more restrictive set of
851216d
PVR values and processor support levels, and this CAS invocation will likely
851216d
induce another reboot. On this reboot grub will again notice the higher RMA,
851216d
and not call CAS. We will get to Linux again, Linux will call CAS again, but
851216d
because the values are now set for Linux this will not induce another CAS
851216d
reboot and we will finally boot all the way to userspace.
851216d
851216d
On all subsequent boots, everything will be configured with 512MB of RMA,
851216d
so there will be no further CAS reboots from grub. (phyp is super sticky
851216d
with the RMA size - it persists even on cold boots. So if you've ever booted
851216d
Linux in a partition, you'll probably never have grub call CAS. It'll only
851216d
ever fire the first time a partition loads grub, or if you deliberately lower
851216d
the amount of memory your partition has below 512MB.)
851216d
851216d
Signed-off-by: Daniel Axtens <dja@axtens.net>
851216d
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
851216d
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
851216d
(cherry picked from commit d5571590b7de61887efac1c298901455697ba307)
851216d
---
851216d
 grub-core/kern/ieee1275/cmain.c  |   5 ++
851216d
 grub-core/kern/ieee1275/init.c   | 167 ++++++++++++++++++++++++++++++++++++++-
851216d
 include/grub/ieee1275/ieee1275.h |  12 ++-
851216d
 3 files changed, 182 insertions(+), 2 deletions(-)
851216d
851216d
diff --git a/grub-core/kern/ieee1275/cmain.c b/grub-core/kern/ieee1275/cmain.c
851216d
index 04df9d2c66..dce7b84922 100644
851216d
--- a/grub-core/kern/ieee1275/cmain.c
851216d
+++ b/grub-core/kern/ieee1275/cmain.c
851216d
@@ -127,6 +127,11 @@ grub_ieee1275_find_options (void)
851216d
 	      break;
851216d
 	    }
851216d
 	}
851216d
+
851216d
+#if defined(__powerpc__)
851216d
+      if (grub_strncmp (tmp, "IBM,", 4) == 0)
851216d
+	grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
851216d
+#endif
851216d
     }
851216d
 
851216d
   if (is_smartfirmware)
851216d
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
851216d
index 6581c2c996..8ae405bc79 100644
851216d
--- a/grub-core/kern/ieee1275/init.c
851216d
+++ b/grub-core/kern/ieee1275/init.c
851216d
@@ -202,11 +202,176 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
851216d
   return 0;
851216d
 }
851216d
 
851216d
-static void 
851216d
+/*
851216d
+ * How much memory does OF believe it has? (regardless of whether
851216d
+ * it's accessible or not)
851216d
+ */
851216d
+static grub_err_t
851216d
+grub_ieee1275_total_mem (grub_uint64_t *total)
851216d
+{
851216d
+  grub_ieee1275_phandle_t root;
851216d
+  grub_ieee1275_phandle_t memory;
851216d
+  grub_uint32_t reg[4];
851216d
+  grub_ssize_t reg_size;
851216d
+  grub_uint32_t address_cells = 1;
851216d
+  grub_uint32_t size_cells = 1;
851216d
+  grub_uint64_t size;
851216d
+
851216d
+  /* If we fail to get to the end, report 0. */
851216d
+  *total = 0;
851216d
+
851216d
+  /* Determine the format of each entry in `reg'.  */
851216d
+  if (grub_ieee1275_finddevice ("/", &root))
851216d
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "couldn't find / node");
851216d
+  if (grub_ieee1275_get_integer_property (root, "#address-cells", &address_cells,
851216d
+					  sizeof (address_cells), 0))
851216d
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "couldn't examine #address-cells");
851216d
+  if (grub_ieee1275_get_integer_property (root, "#size-cells", &size_cells,
851216d
+					  sizeof (size_cells), 0))
851216d
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "couldn't examine #size-cells");
851216d
+
851216d
+  if (size_cells > address_cells)
851216d
+    address_cells = size_cells;
851216d
+
851216d
+  /* Load `/memory/reg'.  */
851216d
+  if (grub_ieee1275_finddevice ("/memory", &memory))
851216d
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "couldn't find /memory node");
851216d
+  if (grub_ieee1275_get_integer_property (memory, "reg", reg,
851216d
+					  sizeof (reg), &reg_size))
851216d
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "couldn't examine /memory/reg property");
851216d
+  if (reg_size < 0 || (grub_size_t) reg_size > sizeof (reg))
851216d
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "/memory response buffer exceeded");
851216d
+
851216d
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS))
851216d
+    {
851216d
+      address_cells = 1;
851216d
+      size_cells = 1;
851216d
+    }
851216d
+
851216d
+  /* Decode only the size */
851216d
+  size = reg[address_cells];
851216d
+  if (size_cells == 2)
851216d
+    size = (size << 32) | reg[address_cells + 1];
851216d
+
851216d
+  *total = size;
851216d
+
851216d
+  return grub_errno;
851216d
+}
851216d
+
851216d
+#if defined(__powerpc__)
851216d
+
851216d
+/* See PAPR or arch/powerpc/kernel/prom_init.c */
851216d
+struct option_vector2
851216d
+{
851216d
+  grub_uint8_t byte1;
851216d
+  grub_uint16_t reserved;
851216d
+  grub_uint32_t real_base;
851216d
+  grub_uint32_t real_size;
851216d
+  grub_uint32_t virt_base;
851216d
+  grub_uint32_t virt_size;
851216d
+  grub_uint32_t load_base;
851216d
+  grub_uint32_t min_rma;
851216d
+  grub_uint32_t min_load;
851216d
+  grub_uint8_t min_rma_percent;
851216d
+  grub_uint8_t max_pft_size;
851216d
+} GRUB_PACKED;
851216d
+
851216d
+struct pvr_entry
851216d
+{
851216d
+  grub_uint32_t mask;
851216d
+  grub_uint32_t entry;
851216d
+};
851216d
+
851216d
+struct cas_vector
851216d
+{
851216d
+  struct
851216d
+  {
851216d
+    struct pvr_entry terminal;
851216d
+  } pvr_list;
851216d
+  grub_uint8_t num_vecs;
851216d
+  grub_uint8_t vec1_size;
851216d
+  grub_uint8_t vec1;
851216d
+  grub_uint8_t vec2_size;
851216d
+  struct option_vector2 vec2;
851216d
+  grub_uint8_t vec3_size;
851216d
+  grub_uint16_t vec3;
851216d
+  grub_uint8_t vec4_size;
851216d
+  grub_uint16_t vec4;
851216d
+} GRUB_PACKED;
851216d
+
851216d
+/*
851216d
+ * Call ibm,client-architecture-support to try to get more RMA.
851216d
+ * We ask for 512MB which should be enough to verify a distro kernel.
851216d
+ * We ignore most errors: if we don't succeed we'll proceed with whatever
851216d
+ * memory we have.
851216d
+ */
851216d
+static void
851216d
+grub_ieee1275_ibm_cas (void)
851216d
+{
851216d
+  int rc;
851216d
+  grub_ieee1275_ihandle_t root;
851216d
+  struct cas_args
851216d
+  {
851216d
+    struct grub_ieee1275_common_hdr common;
851216d
+    grub_ieee1275_cell_t method;
851216d
+    grub_ieee1275_ihandle_t ihandle;
851216d
+    grub_ieee1275_cell_t cas_addr;
851216d
+    grub_ieee1275_cell_t result;
851216d
+  } args;
851216d
+  struct cas_vector vector =
851216d
+  {
851216d
+    .pvr_list = { { 0x00000000, 0xffffffff } }, /* any processor */
851216d
+    .num_vecs = 4 - 1,
851216d
+    .vec1_size = 0,
851216d
+    .vec1 = 0x80, /* ignore */
851216d
+    .vec2_size = 1 + sizeof (struct option_vector2) - 2,
851216d
+    .vec2 = {
851216d
+      0, 0, -1, -1, -1, -1, -1, 512, -1, 0, 48
851216d
+    },
851216d
+    .vec3_size = 2 - 1,
851216d
+    .vec3 = 0x00e0, /* ask for FP + VMX + DFP but don't halt if unsatisfied */
851216d
+    .vec4_size = 2 - 1,
851216d
+    .vec4 = 0x0001, /* set required minimum capacity % to the lowest value */
851216d
+  };
851216d
+
851216d
+  INIT_IEEE1275_COMMON (&args.common, "call-method", 3, 2);
851216d
+  args.method = (grub_ieee1275_cell_t) "ibm,client-architecture-support";
851216d
+  rc = grub_ieee1275_open ("/", &root);
851216d
+  if (rc)
851216d
+    {
851216d
+      grub_error (GRUB_ERR_IO, "could not open root when trying to call CAS");
851216d
+      return;
851216d
+    }
851216d
+  args.ihandle = root;
851216d
+  args.cas_addr = (grub_ieee1275_cell_t) &vector;
851216d
+
851216d
+  grub_printf ("Calling ibm,client-architecture-support from grub...");
851216d
+  IEEE1275_CALL_ENTRY_FN (&args);
851216d
+  grub_printf ("done\n");
851216d
+
851216d
+  grub_ieee1275_close (root);
851216d
+}
851216d
+
851216d
+#endif /* __powerpc__ */
851216d
+
851216d
+static void
851216d
 grub_claim_heap (void)
851216d
 {
851216d
   unsigned long total = 0;
851216d
 
851216d
+#if defined(__powerpc__)
851216d
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY))
851216d
+    {
851216d
+      grub_uint64_t rma_size;
851216d
+      grub_err_t err;
851216d
+
851216d
+      err = grub_ieee1275_total_mem (&rma_size);
851216d
+      /* if we have an error, don't call CAS, just hope for the best */
851216d
+      if (err == GRUB_ERR_NONE && rma_size < (512 * 1024 * 1024))
851216d
+	grub_ieee1275_ibm_cas ();
851216d
+    }
851216d
+#endif
851216d
+
851216d
   grub_machine_mmap_iterate (heap_init, &total);
851216d
 }
851216d
 #endif
851216d
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
851216d
index 6a1d3e5d70..560c968460 100644
851216d
--- a/include/grub/ieee1275/ieee1275.h
851216d
+++ b/include/grub/ieee1275/ieee1275.h
851216d
@@ -138,7 +138,17 @@ enum grub_ieee1275_flag
851216d
 
851216d
   GRUB_IEEE1275_FLAG_RAW_DEVNAMES,
851216d
   
851216d
-  GRUB_IEEE1275_FLAG_DISABLE_VIDEO_SUPPORT
851216d
+  GRUB_IEEE1275_FLAG_DISABLE_VIDEO_SUPPORT,
851216d
+
851216d
+#if defined(__powerpc__)
851216d
+  /*
851216d
+   * On PFW, the first time we boot a Linux partition, we may only get 256MB of
851216d
+   * real memory area, even if the partition has more memory. Set this flag if
851216d
+   * we think we're running under PFW. Then, if this flag is set, and the RMA is
851216d
+   * only 256MB in size, try asking for more with CAS.
851216d
+   */
851216d
+  GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
851216d
+#endif
851216d
 };
851216d
 
851216d
 extern int EXPORT_FUNC(grub_ieee1275_test_flag) (enum grub_ieee1275_flag flag);