]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
term/serial: Avoid double lookup of serial ports
authorBenjamin Herrenschmidt <benh@kernel.crashing.org>
Fri, 23 Dec 2022 01:48:48 +0000 (12:48 +1100)
committerDaniel Kiper <daniel.kiper@oracle.com>
Thu, 19 Jan 2023 16:39:04 +0000 (17:39 +0100)
The various functions to add a port used to return port->name, and
the callers would immediately iterate all registered ports to "find"
the one just created by comparing that return value with ... port->name.

This is a waste of cycles and code. Instead, have those functions
return "port" directly.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/term/arc/serial.c
grub-core/term/ieee1275/serial.c
grub-core/term/ns8250-spcr.c
grub-core/term/ns8250.c
grub-core/term/serial.c
include/grub/serial.h

index 87d1ce8218db807045bdd64fb06c5af9d5746416..651f814ee6b824d7d0abd4e4c8ff8d810f81bd0e 100644 (file)
@@ -100,7 +100,7 @@ struct grub_serial_driver grub_arcserial_driver =
     .put = serial_hw_put
   };
 
-const char *
+struct grub_serial_port *
 grub_arcserial_add_port (const char *path)
 {
   struct grub_serial_port *port;
@@ -120,7 +120,7 @@ grub_arcserial_add_port (const char *path)
 
   grub_serial_register (port);
 
-  return port->name;
+  return port;
 }
 
 static int
index 7df2bee6f39189a1b4109497323dada34c77384d..b4aa9d5c589815a13229d9a415b7d269a464a00d 100644 (file)
@@ -217,7 +217,7 @@ dev_iterate (struct grub_ieee1275_devalias *alias)
   return 0;
 }
 
-static const char *
+static struct grub_serial_port *
 add_port (struct ofserial_hash_ent *ent)
 {
   struct grub_serial_port *port;
@@ -245,10 +245,10 @@ add_port (struct ofserial_hash_ent *ent)
 
   grub_serial_register (port);
 
-  return port->name;
+  return port;
 }
 
-const char *
+const struct grub_serial_port *
 grub_ofserial_add_port (const char *path)
 {
   struct ofserial_hash_ent *ent;
index 1adc6063bfc39a76c9ff2eec890b65d9f27d507a..d101bffb512dbf05011eeed55cae6217f4b928a9 100644 (file)
@@ -24,7 +24,7 @@
 #include <grub/dl.h>
 #include <grub/acpi.h>
 
-char *
+struct grub_serial_port *
 grub_ns8250_spcr_init (void)
 {
   struct grub_acpi_spcr *spcr;
index 172bca9c420871f1b736bd4285025f660b44a5ab..62838670a0eb722cd88cb3a90b40db6cd1e4c839 100644 (file)
@@ -347,7 +347,7 @@ grub_ns8250_hw_get_port (const unsigned int unit)
     return 0;
 }
 
-char *
+struct grub_serial_port *
 grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config)
 {
   struct grub_serial_port *p;
@@ -361,7 +361,7 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
        /* Give the opportunity for SPCR to configure a default com port. */
        if (config != NULL)
          grub_serial_port_configure (&com_ports[i], config);
-       return com_names[i];
+       return &com_ports[i];
       }
 
   grub_outb (0x5a, port + UART_SR);
@@ -391,10 +391,10 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
     grub_serial_config_defaults (p);
   grub_serial_register (p);
 
-  return p->name;
+  return p;
 }
 
-char *
+struct grub_serial_port *
 grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
                              struct grub_serial_config *config)
 {
@@ -406,7 +406,7 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
       {
         if (config != NULL)
           grub_serial_port_configure (&com_ports[i], config);
-        return com_names[i];
+        return &com_ports[i];
       }
 
   p = grub_malloc (sizeof (*p));
@@ -428,5 +428,5 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
     grub_serial_config_defaults (p);
   grub_serial_register (p);
 
-  return p->name;
+  return p;
 }
index 6e9483cb871033ba7d82a9e9c3c7de5751337caf..bf2825795b32806b58352631177b0d47bb0407ef 100644 (file)
@@ -155,14 +155,10 @@ grub_serial_find (const char *name)
   if (!port && grub_strncmp (name, "port", sizeof ("port") - 1) == 0
       && grub_isxdigit (name [sizeof ("port") - 1]))
     {
-      name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
+      port = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
                                                        0, 16), NULL);
-      if (name == NULL)
+      if (port == NULL)
         return NULL;
-
-      FOR_SERIAL_PORTS (port)
-        if (grub_strcmp (port->name, name) == 0)
-           break;
     }
   if (!port && grub_strncmp (name, "mmio,", sizeof ("mmio,") - 1) == 0
       && grub_isxdigit (name [sizeof ("mmio,") - 1]))
@@ -219,27 +215,22 @@ grub_serial_find (const char *name)
           break;
         }
 
-      name = grub_serial_ns8250_add_mmio (addr, acc_size, NULL);
-      if (name == NULL)
+      port = grub_serial_ns8250_add_mmio (addr, acc_size, NULL);
+      if (port == NULL)
         return NULL;
-
-      FOR_SERIAL_PORTS (port)
-        if (grub_strcmp (port->name, name) == 0) {
-          break;
-        }
     }
 
 #if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)
   if (!port && grub_strcmp (name, "auto") == 0)
     {
       /* Look for an SPCR if any. If not, default to com0. */
-      name = grub_ns8250_spcr_init ();
-      if (name == NULL)
-        name = "com0";
-
-      FOR_SERIAL_PORTS (port)
-        if (grub_strcmp (port->name, name) == 0)
-          break;
+      port = grub_ns8250_spcr_init ();
+      if (port == NULL)
+        {
+          FOR_SERIAL_PORTS (port)
+            if (grub_strcmp (port->name, "com0") == 0)
+              break;
+       }
     }
 #endif
 #endif
@@ -247,13 +238,9 @@ grub_serial_find (const char *name)
 #ifdef GRUB_MACHINE_IEEE1275
   if (!port && grub_strncmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) == 0)
     {
-      name = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]);
-      if (!name)
-       return NULL;
-
-      FOR_SERIAL_PORTS (port)
-       if (grub_strcmp (port->name, name) == 0)
-         break;
+      port = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]);
+      if (port == NULL)
+        return NULL;
     }
 #endif
 
index 36be4b7ee76aa8f9fedcd3a7f636ebd3e1428bc6..c527f4d92f494b720ddc8e6a8283340e64871705 100644 (file)
@@ -190,10 +190,12 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
-char *grub_ns8250_spcr_init (void);
-char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
-char *grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
-                                   struct grub_serial_config *config);
+struct grub_serial_port *grub_ns8250_spcr_init (void);
+struct grub_serial_port *grub_serial_ns8250_add_port (grub_port_t port,
+                                                     struct grub_serial_config *config);
+struct grub_serial_port *grub_serial_ns8250_add_mmio (grub_addr_t addr,
+                                                     unsigned int acc_size,
+                                                     struct grub_serial_config *config);
 #endif
 #ifdef GRUB_MACHINE_IEEE1275
 void grub_ofserial_init (void);
@@ -205,8 +207,7 @@ grub_efiserial_init (void);
 #ifdef GRUB_MACHINE_ARC
 void
 grub_arcserial_init (void);
-const char *
-grub_arcserial_add_port (const char *path);
+struct grub_serial_port *grub_arcserial_add_port (const char *path);
 #endif
 
 struct grub_serial_port *grub_serial_find (const char *name);