ddafa09
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
ddafa09
From: Daniel Axtens <dja@axtens.net>
ddafa09
Date: Fri, 16 Apr 2021 11:48:46 +1000
ddafa09
Subject: [PATCH] ieee1275: request memory with ibm,client-architecture-support
ddafa09
ddafa09
On PowerVM, the first time we boot a Linux partition, we may only get
ddafa09
256MB of real memory area, even if the partition has more memory.
ddafa09
ddafa09
This isn't really enough. Fortunately, the Power Architecture Platform
ddafa09
Reference (PAPR) defines a method we can call to ask for more memory.
ddafa09
This is part of the broad and powerful ibm,client-architecture-support
ddafa09
(CAS) method.
ddafa09
ddafa09
CAS can do an enormous amount of things on a PAPR platform: as well as
ddafa09
asking for memory, you can set the supported processor level, the interrupt
ddafa09
controller, hash vs radix mmu, and so on. We want to touch as little of
ddafa09
this as possible because we don't want to step on the toes of the future OS.
ddafa09
ddafa09
If:
ddafa09
ddafa09
 - we are running under what we think is PowerVM (compatible property of /
ddafa09
   begins with "IBM"), and
ddafa09
ddafa09
 - the full amount of RMA is less than 512MB (as determined by the reg
ddafa09
   property of /memory)
ddafa09
ddafa09
then call CAS as follows: (refer to the Linux on Power Architecture
ddafa09
Reference, LoPAR, which is public, at B.5.2.3):
ddafa09
ddafa09
 - Use the "any" PVR value and supply 2 option vectors.
ddafa09
ddafa09
 - Set option vector 1 (PowerPC Server Processor Architecture Level)
ddafa09
   to "ignore".
ddafa09
ddafa09
 - Set option vector 2 with default or Linux-like options, including a
ddafa09
   min-rma-size of 512MB.
ddafa09
ddafa09
This will cause a CAS reboot and the partition will restart with 512MB
ddafa09
of RMA. Grub will notice the 512MB and not call CAS again.
ddafa09
ddafa09
(A partition can be configured with only 256MB of memory, which would
ddafa09
mean this request couldn't be satisfied, but PFW refuses to load with
ddafa09
only 256MB of memory, so it's a bit moot. SLOF will run fine with 256MB,
ddafa09
but we will never call CAS under qemu/SLOF because /compatible won't
ddafa09
begin with "IBM".)
ddafa09
ddafa09
One of the first things Linux does while still running under OpenFirmware
ddafa09
is to call CAS with a much fuller set of options (including asking for
ddafa09
512MB of memory). This includes a much more restrictive set of PVR values
ddafa09
and processor support levels, and this will induce another reboot. On this
ddafa09
reboot grub will again notice the higher RMA, and not call CAS. We will get
ddafa09
to Linux, Linux will call CAS but because the values are now set for Linux
ddafa09
this will not induce another CAS reboot and we will finally boot.
ddafa09
ddafa09
On all subsequent boots, everything will be configured with 512MB of RMA
ddafa09
and all the settings Linux likes, so there will be no further CAS reboots.
ddafa09
ddafa09
(phyp is super sticky with the RMA size - it persists even on cold boots.
ddafa09
So if you've ever booted Linux in a partition, you'll probably never have
ddafa09
grub call CAS. It'll only ever fire the first time a partition loads grub,
ddafa09
or if you deliberately lower the amount of memory your partition has below
ddafa09
512MB.)
ddafa09
ddafa09
Signed-off-by: Daniel Axtens <dja@axtens.net>
ddafa09
---
ddafa09
 grub-core/kern/ieee1275/cmain.c  |   3 +
ddafa09
 grub-core/kern/ieee1275/init.c   | 144 ++++++++++++++++++++++++++++++++++++++-
ddafa09
 include/grub/ieee1275/ieee1275.h |   8 ++-
ddafa09
 3 files changed, 152 insertions(+), 3 deletions(-)
ddafa09
ddafa09
diff --git a/grub-core/kern/ieee1275/cmain.c b/grub-core/kern/ieee1275/cmain.c
ddafa09
index 04df9d2c667..6435628ec57 100644
ddafa09
--- a/grub-core/kern/ieee1275/cmain.c
ddafa09
+++ b/grub-core/kern/ieee1275/cmain.c
ddafa09
@@ -127,6 +127,9 @@ grub_ieee1275_find_options (void)
ddafa09
 	      break;
ddafa09
 	    }
ddafa09
 	}
ddafa09
+
ddafa09
+      if (grub_strncmp (tmp, "IBM,", 4) == 0)
ddafa09
+	grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
ddafa09
     }
ddafa09
 
ddafa09
   if (is_smartfirmware)
ddafa09
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
ddafa09
index c61d91a0285..9704715c837 100644
ddafa09
--- a/grub-core/kern/ieee1275/init.c
ddafa09
+++ b/grub-core/kern/ieee1275/init.c
ddafa09
@@ -242,6 +242,135 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
ddafa09
   return 0;
ddafa09
 }
ddafa09
 
ddafa09
+/* How much memory does OF believe it has? (regardless of whether
ddafa09
+   it's accessible or not) */
ddafa09
+static grub_err_t
ddafa09
+grub_ieee1275_total_mem (grub_uint64_t *total)
ddafa09
+{
ddafa09
+  grub_ieee1275_phandle_t root;
ddafa09
+  grub_ieee1275_phandle_t memory;
ddafa09
+  grub_uint32_t reg[4];
ddafa09
+  grub_ssize_t reg_size;
ddafa09
+  grub_uint32_t address_cells = 1;
ddafa09
+  grub_uint32_t size_cells = 1;
ddafa09
+  grub_uint64_t size;
ddafa09
+
ddafa09
+  /* If we fail to get to the end, report 0. */
ddafa09
+  *total = 0;
ddafa09
+
ddafa09
+  /* Determine the format of each entry in `reg'.  */
ddafa09
+  grub_ieee1275_finddevice ("/", &root);
ddafa09
+  grub_ieee1275_get_integer_property (root, "#address-cells", &address_cells,
ddafa09
+				      sizeof address_cells, 0);
ddafa09
+  grub_ieee1275_get_integer_property (root, "#size-cells", &size_cells,
ddafa09
+				      sizeof size_cells, 0);
ddafa09
+
ddafa09
+  if (size_cells > address_cells)
ddafa09
+    address_cells = size_cells;
ddafa09
+
ddafa09
+  /* Load `/memory/reg'.  */
ddafa09
+  if (grub_ieee1275_finddevice ("/memory", &memory))
ddafa09
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
ddafa09
+		       "couldn't find /memory node");
ddafa09
+  if (grub_ieee1275_get_integer_property (memory, "reg", reg,
ddafa09
+					  sizeof reg, &reg_size))
ddafa09
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
ddafa09
+		       "couldn't examine /memory/reg property");
ddafa09
+  if (reg_size < 0 || (grub_size_t) reg_size > sizeof (reg))
ddafa09
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
ddafa09
+                       "/memory response buffer exceeded");
ddafa09
+
ddafa09
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS))
ddafa09
+    {
ddafa09
+      address_cells = 1;
ddafa09
+      size_cells = 1;
ddafa09
+    }
ddafa09
+
ddafa09
+  /* Decode only the size */
ddafa09
+  size = reg[address_cells];
ddafa09
+  if (size_cells == 2)
ddafa09
+    size = (size << 32) | reg[address_cells + 1];
ddafa09
+
ddafa09
+  *total = size;
ddafa09
+
ddafa09
+  return grub_errno;
ddafa09
+}
ddafa09
+
ddafa09
+/* Based on linux - arch/powerpc/kernel/prom_init.c */
ddafa09
+struct option_vector2 {
ddafa09
+	grub_uint8_t byte1;
ddafa09
+	grub_uint16_t reserved;
ddafa09
+	grub_uint32_t real_base;
ddafa09
+	grub_uint32_t real_size;
ddafa09
+	grub_uint32_t virt_base;
ddafa09
+	grub_uint32_t virt_size;
ddafa09
+	grub_uint32_t load_base;
ddafa09
+	grub_uint32_t min_rma;
ddafa09
+	grub_uint32_t min_load;
ddafa09
+	grub_uint8_t min_rma_percent;
ddafa09
+	grub_uint8_t max_pft_size;
ddafa09
+} __attribute__((packed));
ddafa09
+
ddafa09
+struct pvr_entry {
ddafa09
+	  grub_uint32_t mask;
ddafa09
+	  grub_uint32_t entry;
ddafa09
+};
ddafa09
+
ddafa09
+struct cas_vector {
ddafa09
+    struct {
ddafa09
+      struct pvr_entry terminal;
ddafa09
+    } pvr_list;
ddafa09
+    grub_uint8_t num_vecs;
ddafa09
+    grub_uint8_t vec1_size;
ddafa09
+    grub_uint8_t vec1;
ddafa09
+    grub_uint8_t vec2_size;
ddafa09
+    struct option_vector2 vec2;
ddafa09
+} __attribute__((packed));
ddafa09
+
ddafa09
+/* Call ibm,client-architecture-support to try to get more RMA.
ddafa09
+   We ask for 512MB which should be enough to verify a distro kernel.
ddafa09
+   We ignore most errors: if we don't succeed we'll proceed with whatever
ddafa09
+   memory we have. */
ddafa09
+static void
ddafa09
+grub_ieee1275_ibm_cas (void)
ddafa09
+{
ddafa09
+  int rc;
ddafa09
+  grub_ieee1275_ihandle_t root;
ddafa09
+  struct cas_args {
ddafa09
+    struct grub_ieee1275_common_hdr common;
ddafa09
+    grub_ieee1275_cell_t method;
ddafa09
+    grub_ieee1275_ihandle_t ihandle;
ddafa09
+    grub_ieee1275_cell_t cas_addr;
ddafa09
+    grub_ieee1275_cell_t result;
ddafa09
+  } args;
ddafa09
+  struct cas_vector vector = {
ddafa09
+    .pvr_list = { { 0x00000000, 0xffffffff } }, /* any processor */
ddafa09
+    .num_vecs = 2 - 1,
ddafa09
+    .vec1_size = 0,
ddafa09
+    .vec1 = 0x80, /* ignore */
ddafa09
+    .vec2_size = 1 + sizeof(struct option_vector2) - 2,
ddafa09
+    .vec2 = {
ddafa09
+      0, 0, -1, -1, -1, -1, -1, 512, -1, 0, 48
ddafa09
+    },
ddafa09
+  };
ddafa09
+
ddafa09
+  INIT_IEEE1275_COMMON (&args.common, "call-method", 3, 2);
ddafa09
+  args.method = (grub_ieee1275_cell_t)"ibm,client-architecture-support";
ddafa09
+  rc = grub_ieee1275_open("/", &root);
ddafa09
+  if (rc) {
ddafa09
+	  grub_error (GRUB_ERR_IO, "could not open root when trying to call CAS");
ddafa09
+	  return;
ddafa09
+  }
ddafa09
+  args.ihandle = root;
ddafa09
+  args.cas_addr = (grub_ieee1275_cell_t)&vector;
ddafa09
+
ddafa09
+  grub_printf("Calling ibm,client-architecture-support...");
ddafa09
+  IEEE1275_CALL_ENTRY_FN (&args);
ddafa09
+  grub_printf("done\n");
ddafa09
+
ddafa09
+  grub_ieee1275_close(root);
ddafa09
+}
ddafa09
+
ddafa09
 static void 
ddafa09
 grub_claim_heap (void)
ddafa09
 {
ddafa09
@@ -249,11 +378,22 @@ grub_claim_heap (void)
ddafa09
 
ddafa09
   if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_FORCE_CLAIM))
ddafa09
     {
ddafa09
-      heap_init (GRUB_IEEE1275_STATIC_HEAP_START, GRUB_IEEE1275_STATIC_HEAP_LEN,
ddafa09
-		 1, &total);
ddafa09
+      heap_init (GRUB_IEEE1275_STATIC_HEAP_START,
ddafa09
+		 GRUB_IEEE1275_STATIC_HEAP_LEN, 1, &total);
ddafa09
       return;
ddafa09
     }
ddafa09
 
ddafa09
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY))
ddafa09
+    {
ddafa09
+      grub_uint64_t rma_size;
ddafa09
+      grub_err_t err;
ddafa09
+
ddafa09
+      err = grub_ieee1275_total_mem (&rma_size);
ddafa09
+      /* if we have an error, don't call CAS, just hope for the best */
ddafa09
+      if (!err && rma_size < (512 * 1024 * 1024))
ddafa09
+	grub_ieee1275_ibm_cas();
ddafa09
+    }
ddafa09
+
ddafa09
   grub_machine_mmap_iterate (heap_size, &total);
ddafa09
 
ddafa09
   total = total / 4;
ddafa09
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
ddafa09
index b5a1d49bbc3..e0a6c2ce1e6 100644
ddafa09
--- a/include/grub/ieee1275/ieee1275.h
ddafa09
+++ b/include/grub/ieee1275/ieee1275.h
ddafa09
@@ -149,7 +149,13 @@ enum grub_ieee1275_flag
ddafa09
 
ddafa09
   GRUB_IEEE1275_FLAG_RAW_DEVNAMES,
ddafa09
   
ddafa09
-  GRUB_IEEE1275_FLAG_DISABLE_VIDEO_SUPPORT
ddafa09
+  GRUB_IEEE1275_FLAG_DISABLE_VIDEO_SUPPORT,
ddafa09
+
ddafa09
+  /* On PFW, the first time we boot a Linux partition, we may only get 256MB
ddafa09
+     of real memory area, even if the partition has more memory. Set this flag
ddafa09
+     if we think we're running under PFW. Then, if this flag is set, and the
ddafa09
+     RMA is only 256MB in size, try asking for more with CAS. */
ddafa09
+  GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
ddafa09
 };
ddafa09
 
ddafa09
 extern int EXPORT_FUNC(grub_ieee1275_test_flag) (enum grub_ieee1275_flag flag);