nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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