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 2022-12-23 12:48:48 +11:00 committed by Daniel Kiper
parent b73a44b281
commit e37dbba665
6 changed files with 33 additions and 45 deletions

View 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

View 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;

View 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;

View 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;
}

View 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

View 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);