Skip to contentSkip to conversation

earlyprintk: Clean up pciserial

PATCH Applied Peter Zijlstra 4 participants 7 messages Aug 25, 2022 – Aug 29, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 earlyprintk: Clean up pciserial

While working on a GRUB patch to support PCI-serial, a number of cleanups were suggested that apply to the code I took inspiration from.

---
 arch/x86/kernel/early_printk.c | 14 +++++++-------
 include/linux/pci_ids.h        |  3 +++
 2 files changed, 10 insertions(+), 7 deletions(-)
arch/x86/kernel/early_printk.c +7 -7
index 68b38925a74f..44f937015e1e 100644
@@ -264,11 +264,11 @@ static __init void early_pci_serial_init(char *s)
 	bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
 
 	/*
-	 * Verify it is a UART type device
+	 * Verify it is a 16550-UART type device
 	 */
 	if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
 	     (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
-	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
+	    (((classcode >> 8) & 0xff) != PCI_SERIAL_16550_COMPATIBLE)) {
 		if (!force)
 			return;
 	}
@@ -276,22 +276,22 @@ static __init void early_pci_serial_init(char *s)
 	/*
 	 * Determine if it is IO or memory mapped
 	 */
-	if (bar0 & 0x01) {
+	if ((bar0 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
 		/* it is IO mapped */
 		serial_in = io_serial_in;
 		serial_out = io_serial_out;
-		early_serial_base = bar0&0xfffffffc;
+		early_serial_base = bar0 & PCI_BASE_ADDRESS_IO_MASK;
 		write_pci_config(bus, slot, func, PCI_COMMAND,
-						cmdreg|PCI_COMMAND_IO);
+				 cmdreg|PCI_COMMAND_IO);
 	} else {
 		/* It is memory mapped - assume 32-bit alignment */
 		serial_in = mem32_serial_in;
 		serial_out = mem32_serial_out;
 		/* WARNING! assuming the address is always in the first 4G */
 		early_serial_base =
-			(unsigned long)early_ioremap(bar0 & 0xfffffff0, 0x10);
+			(unsigned long)early_ioremap(bar0 & PCI_BASE_ADDRESS_MEM_MASK, 0x10);
 		write_pci_config(bus, slot, func, PCI_COMMAND,
-						cmdreg|PCI_COMMAND_MEMORY);
+				 cmdreg|PCI_COMMAND_MEMORY);
 	}
 
 	/*
include/linux/pci_ids.h +3 -0
index 6feade66efdb..1d2c7df8cd41 100644
@@ -75,6 +75,9 @@
 #define PCI_CLASS_COMMUNICATION_MODEM	0x0703
 #define PCI_CLASS_COMMUNICATION_OTHER	0x0780
 
+/* I/F for SERIAL/MODEM */
+#define PCI_SERIAL_16550_COMPATIBLE	0x02
+
 #define PCI_BASE_CLASS_SYSTEM		0x08
 #define PCI_CLASS_SYSTEM_PIC		0x0800
 #define PCI_CLASS_SYSTEM_PIC_IOAPIC	0x080010
Acked-by on v1 1/1
#2 lore raw
In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra

While working on a GRUB patch to support PCI-serial, a number of cleanups were suggested that apply to the code I took inspiration from.

Quoted 56 lines
---
 arch/x86/kernel/early_printk.c | 14 +++++++-------
 include/linux/pci_ids.h        |  3 +++
 2 files changed, 10 insertions(+), 7 deletions(-)
arch/x86/kernel/early_printk.c +7 -7
index 68b38925a74f..44f937015e1e 100644
@@ -264,11 +264,11 @@ static __init void early_pci_serial_init(char *s)
 	bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
 
 	/*
-	 * Verify it is a UART type device
+	 * Verify it is a 16550-UART type device
 	 */
 	if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
 	     (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
-	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
+	    (((classcode >> 8) & 0xff) != PCI_SERIAL_16550_COMPATIBLE)) {
 		if (!force)
 			return;
 	}
@@ -276,22 +276,22 @@ static __init void early_pci_serial_init(char *s)
 	/*
 	 * Determine if it is IO or memory mapped
 	 */
-	if (bar0 & 0x01) {
+	if ((bar0 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
 		/* it is IO mapped */
 		serial_in = io_serial_in;
 		serial_out = io_serial_out;
-		early_serial_base = bar0&0xfffffffc;
+		early_serial_base = bar0 & PCI_BASE_ADDRESS_IO_MASK;
 		write_pci_config(bus, slot, func, PCI_COMMAND,
-						cmdreg|PCI_COMMAND_IO);
+				 cmdreg|PCI_COMMAND_IO);
 	} else {
 		/* It is memory mapped - assume 32-bit alignment */
 		serial_in = mem32_serial_in;
 		serial_out = mem32_serial_out;
 		/* WARNING! assuming the address is always in the first 4G */
 		early_serial_base =
-			(unsigned long)early_ioremap(bar0 & 0xfffffff0, 0x10);
+			(unsigned long)early_ioremap(bar0 & PCI_BASE_ADDRESS_MEM_MASK, 0x10);
 		write_pci_config(bus, slot, func, PCI_COMMAND,
-						cmdreg|PCI_COMMAND_MEMORY);
+				 cmdreg|PCI_COMMAND_MEMORY);
 	}
 
 	/*
include/linux/pci_ids.h +0 -0
index 6feade66efdb..1d2c7df8cd41 100644

include/linux/pci_ids.h line 78

Quoted 4 lines
include/linux/pci_ids.h +1 -0
@@ -75,6 +75,9 @@
 #define PCI_CLASS_COMMUNICATION_MODEM	0x0703
 #define PCI_CLASS_COMMUNICATION_OTHER	0x0780
 
+/* I/F for SERIAL/MODEM */

I/F? Grep says it's fairly common, but doesn't seem completely obvious. I guess it means "interface"?

Quoted 5 lines
include/linux/pci_ids.h +2 -0
+#define PCI_SERIAL_16550_COMPATIBLE	0x02
+
 #define PCI_BASE_CLASS_SYSTEM		0x08
 #define PCI_CLASS_SYSTEM_PIC		0x0800
 #define PCI_CLASS_SYSTEM_PIC_IOAPIC	0x080010
#3 lore raw
Quoted 10 lines · Bjorn Helgaas
Quoted 10 lines
arch/x86/kernel/early_printk.c +0 -1
-	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
include/linux/pci_ids.h +0 -0
index 6feade66efdb..1d2c7df8cd41 100644
@@ -75,6 +75,9 @@
 #define PCI_CLASS_COMMUNICATION_MODEM	0x0703
 #define PCI_CLASS_COMMUNICATION_OTHER	0x0780
 
Quoted 4 lines

include/linux/pci_ids.h line 78

Quoted 1 line
include/linux/pci_ids.h +1 -0
+/* I/F for SERIAL/MODEM */

I/F? Grep says it's fairly common, but doesn't seem completely obvious. I guess it means "interface"?

It does, I carried the nomenclature from the above line; happy to change it if you want.

https://wiki.osdev.org/PCI#Class_Codes

Calls it 'Prog IF'.

Quoted 5 lines
Quoted 5 lines
include/linux/pci_ids.h +2 -0
+#define PCI_SERIAL_16550_COMPATIBLE	0x02
+
 #define PCI_BASE_CLASS_SYSTEM		0x08
 #define PCI_CLASS_SYSTEM_PIC		0x0800
 #define PCI_CLASS_SYSTEM_PIC_IOAPIC	0x080010
#4 lore raw
Quoted 17 lines · Peter Zijlstra
Quoted 10 lines · Bjorn Helgaas
Quoted 10 lines
arch/x86/kernel/early_printk.c +0 -1
-	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
include/linux/pci_ids.h +0 -0
index 6feade66efdb..1d2c7df8cd41 100644
@@ -75,6 +75,9 @@
 #define PCI_CLASS_COMMUNICATION_MODEM	0x0703
 #define PCI_CLASS_COMMUNICATION_OTHER	0x0780
 
Quoted 4 lines

include/linux/pci_ids.h line 78

Quoted 1 line
include/linux/pci_ids.h +1 -0
+/* I/F for SERIAL/MODEM */

I/F? Grep says it's fairly common, but doesn't seem completely obvious. I guess it means "interface"?

Quoted 6 lines

It does, I carried the nomenclature from the above line; happy to change it if you want.

https://wiki.osdev.org/PCI#Class_Codes

Calls it 'Prog IF'.

Personally, I would spell it out just to make it less arcane. I wouldn't know where to find the definition, and the PCI specs don't seem to use the abbreviation. But no big deal either way.

Bjorn

#5 lore raw
Quoted 21 lines · Bjorn Helgaas
Quoted 20 lines · Peter Zijlstra
Quoted 10 lines · Bjorn Helgaas
Quoted 10 lines
arch/x86/kernel/early_printk.c +0 -1
-	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
include/linux/pci_ids.h +0 -0
index 6feade66efdb..1d2c7df8cd41 100644
@@ -75,6 +75,9 @@
 #define PCI_CLASS_COMMUNICATION_MODEM	0x0703
 #define PCI_CLASS_COMMUNICATION_OTHER	0x0780
 
Quoted 4 lines

include/linux/pci_ids.h line 78

Quoted 1 line
include/linux/pci_ids.h +1 -0
+/* I/F for SERIAL/MODEM */

I/F? Grep says it's fairly common, but doesn't seem completely obvious. I guess it means "interface"?

It does, I carried the nomenclature from the above line; happy to change it if you want.

Quoted 7 lines
Quoted 3 lines

https://wiki.osdev.org/PCI#Class_Codes

Calls it 'Prog IF'.

Personally, I would spell it out just to make it less arcane. I wouldn't know where to find the definition, and the PCI specs don't seem to use the abbreviation. But no big deal either way.

Ok,

/* Interface for SERIAL/MODEM */

it is.

merge bot Applied
#6 lore raw

[tip: x86/core] x86/earlyprintk: Clean up pciserial

In reply to Peter Zijlstra
The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     bc12b70f7d216b36bd87701349374a13e486f8eb
Gitweb:        https://git.kernel.org/tip/bc12b70f7d216b36bd87701349374a13e486f8eb
Author:        Peter Zijlstra <[email protected]>
AuthorDate:    Thu, 25 Aug 2022 13:36:40 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Mon, 29 Aug 2022 12:19:25 +02:00

x86/earlyprintk: Clean up pciserial

While working on a GRUB patch to support PCI-serial, a number of cleanups were suggested that apply to the code I took inspiration from.

---
 arch/x86/kernel/early_printk.c | 14 +++++++-------
 include/linux/pci_ids.h        |  3 +++
 2 files changed, 10 insertions(+), 7 deletions(-)
arch/x86/kernel/early_printk.c +7 -7
index 68b3892..44f9370 100644
@@ -264,11 +264,11 @@ static __init void early_pci_serial_init(char *s)
 	bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
 
 	/*
-	 * Verify it is a UART type device
+	 * Verify it is a 16550-UART type device
 	 */
 	if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
 	     (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
-	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
+	    (((classcode >> 8) & 0xff) != PCI_SERIAL_16550_COMPATIBLE)) {
 		if (!force)
 			return;
 	}
@@ -276,22 +276,22 @@ static __init void early_pci_serial_init(char *s)
 	/*
 	 * Determine if it is IO or memory mapped
 	 */
-	if (bar0 & 0x01) {
+	if ((bar0 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
 		/* it is IO mapped */
 		serial_in = io_serial_in;
 		serial_out = io_serial_out;
-		early_serial_base = bar0&0xfffffffc;
+		early_serial_base = bar0 & PCI_BASE_ADDRESS_IO_MASK;
 		write_pci_config(bus, slot, func, PCI_COMMAND,
-						cmdreg|PCI_COMMAND_IO);
+				 cmdreg|PCI_COMMAND_IO);
 	} else {
 		/* It is memory mapped - assume 32-bit alignment */
 		serial_in = mem32_serial_in;
 		serial_out = mem32_serial_out;
 		/* WARNING! assuming the address is always in the first 4G */
 		early_serial_base =
-			(unsigned long)early_ioremap(bar0 & 0xfffffff0, 0x10);
+			(unsigned long)early_ioremap(bar0 & PCI_BASE_ADDRESS_MEM_MASK, 0x10);
 		write_pci_config(bus, slot, func, PCI_COMMAND,
-						cmdreg|PCI_COMMAND_MEMORY);
+				 cmdreg|PCI_COMMAND_MEMORY);
 	}
 
 	/*
include/linux/pci_ids.h +3 -0
index 6feade6..41b3fff 100644
@@ -75,6 +75,9 @@
 #define PCI_CLASS_COMMUNICATION_MODEM	0x0703
 #define PCI_CLASS_COMMUNICATION_OTHER	0x0780
 
+/* Interface for SERIAL/MODEM */
+#define PCI_SERIAL_16550_COMPATIBLE	0x02
+
 #define PCI_BASE_CLASS_SYSTEM		0x08
 #define PCI_CLASS_SYSTEM_PIC		0x0800
 #define PCI_CLASS_SYSTEM_PIC_IOAPIC	0x080010