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

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