nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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