Blame SOURCES/0200-ieee1275-request-memory-with-ibm-client-architecture.patch

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