term/serial: Avoid double lookup of serial ports

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>
This commit is contained in:
Benjamin Herrenschmidt authored and Daniel Kiper committed 2023-01-19 17:39:04 +01:00
1 parent b73a44b281
commit e37dbba665
6 files changed
+33 -45

No files matched your search

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