[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/3] serial: Avoid double lookup of serial ports
From: |
Benjamin Herrenschmidt |
Subject: |
[PATCH 2/3] serial: Avoid double lookup of serial ports |
Date: |
Fri, 23 Dec 2022 12:48:48 +1100 |
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>
---
grub-core/term/arc/serial.c | 4 ++--
grub-core/term/ieee1275/serial.c | 6 ++---
grub-core/term/ns8250-spcr.c | 2 +-
grub-core/term/ns8250.c | 12 +++++-----
grub-core/term/serial.c | 41 +++++++++++---------------------
include/grub/serial.h | 13 +++++-----
6 files changed, 33 insertions(+), 45 deletions(-)
diff --git a/grub-core/term/arc/serial.c b/grub-core/term/arc/serial.c
index 87d1ce821..651f814ee 100644
--- a/grub-core/term/arc/serial.c
+++ b/grub-core/term/arc/serial.c
@@ -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
diff --git a/grub-core/term/ieee1275/serial.c b/grub-core/term/ieee1275/serial.c
index 7df2bee6f..b4aa9d5c5 100644
--- a/grub-core/term/ieee1275/serial.c
+++ b/grub-core/term/ieee1275/serial.c
@@ -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;
diff --git a/grub-core/term/ns8250-spcr.c b/grub-core/term/ns8250-spcr.c
index f4b718833..9fe36733d 100644
--- a/grub-core/term/ns8250-spcr.c
+++ b/grub-core/term/ns8250-spcr.c
@@ -22,7 +22,7 @@
#include <grub/dl.h>
#include <grub/acpi.h>
-char *
+struct grub_serial_port *
grub_ns8250_spcr_init (void)
{
struct grub_acpi_spcr *spcr;
diff --git a/grub-core/term/ns8250.c b/grub-core/term/ns8250.c
index 98f0b3bc3..1eb6a30b6 100644
--- a/grub-core/term/ns8250.c
+++ b/grub-core/term/ns8250.c
@@ -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;
}
diff --git a/grub-core/term/serial.c b/grub-core/term/serial.c
index 88bcd4032..275dd61af 100644
--- a/grub-core/term/serial.c
+++ b/grub-core/term/serial.c
@@ -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,38 +215,29 @@ 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 (!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
#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
diff --git a/include/grub/serial.h b/include/grub/serial.h
index 65ccab4ff..17ee0f08b 100644
--- a/include/grub/serial.h
+++ b/include/grub/serial.h
@@ -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);
--
2.34.1