Skip to contentSkip to conversation

PCI support

PATCH Marco Gerards 3 participants 16 messages Jan 28, 2008 – Feb 2, 2008

lore
Share & tools
lore
#0 lore raw

Hi,

Here is a patch that allows you to iterate over PCI devices. The file pci.c adds a function to iterate over the devices and a function to generate an address to query the PCI device. Furthermore, there is a function to actually access the hardware, which is machine specific. I only implemented this for i386-pc. I assume it can be shared with linuxbios, i386-efi and i386-ieee1275. When you want to use this, just include <grub/pci.h>.

When you want to detect if your device is supported, just use grub_pci_iterate. It gives you every PCI device. For example, the class has to be read to determine if a device is an ATA device. This will be added to the ATA driver.

The lspci that is included shows the PCIID and the class of the device. It's not capable of reading pci.ids yet. I have some code for this, but it is just too damn slow... But I am sure this will do for now
:-)

I wanted to wait with this patch until the ATA driver is adapted. But I won't have time for that this week. So I can better get this committed and people working on this ;-). There is some code for ATA, but it was not tested yet. The main problem here is qemu.

Does someone know if it is possible, at all, to have ATA devices showing up as non-legacy PCI devices in Qemu? IDE interfaces are placed (and locked) in Legacy mode, meaning default IRQs and IO ports are used. This means just our old ISA code will be used. To support non-legacy modes, it would be nice if I could do some testing. Does someone have suggestions?

--
Marco

2009-01-28 Marco Gerards <[email protected]>

* bus/pci.c: New file.

* include/grub/pci.h: Likewise.

* include/grub/i386/pc/pci.h: Likewise.

* commands/lspci.c: Likewise.

* conf/i386-pc.rmk (pkglib_MODULES): Add pci.mod' and lspci.mod'.
(pci_mod_SOURCES): New variable. (pci_mod_CFLAGS): Likewise. (pci_mod_LDFLAGS): Likewise. (lspci_mod_SOURCES): Likewise. (lspci_mod_CFLAGS): Likewise. (lspci_mod_LDFLAGS): Likewise.

Index: bus/pci.c
===================================================================
RCS file: bus/pci.c

pci.c new file +55 -0
diff -N bus/pci.c
@@ -0,0 +1,55 @@
+/* pci.c - Generic PCI interfaces.  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2008  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/pci.h>
+
+grub_pci_address_t
+grub_pci_make_address (int bus, int device, int function, int reg)
+{
+  return (1 << 31) | (bus << 16) | (device << 11) | (function << 8) | (reg << 2);
+}
+
+void
+grub_pci_iterate (grub_pci_iteratefunc_t hook)
+{
+  int bus;
+  int dev;
+  int func;
+  grub_pci_address_t addr;
+  grub_pci_id_t id;
+
+  for (bus = 0; bus < 256; bus++)
+    {
+      for (dev = 0; dev < 32; dev++)
+	{
+	  for (func = 0; func < 3; func++)
+	    {
+	      addr = grub_pci_make_address (bus, dev, func, 0);
+	      id = grub_pci_read (addr);
+
+	      /* Check if there is a device present.  */
+	      if (id >> 16 == 0xFFFF)
+		continue;
+
+	      hook (bus, dev, func, id);
+	    }
+	}
+    }
+}

Index: commands/lspci.c
===================================================================
RCS file: commands/lspci.c

lspci.c new file +163 -0

163 lines not on this page · raw message

Index: conf/i386-pc.rmk
===================================================================
RCS file: /sources/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.100

i386-pc.rmk +11 -1
diff -u -p -u -p -r1.100 i386-pc.rmk
@@ -136,7 +136,7 @@ pkglib_MODULES = biosdisk.mod _chain.mod
 	_multiboot.mod chain.mod multiboot.mod reboot.mod halt.mod	\
 	vbe.mod vbetest.mod vbeinfo.mod video.mod gfxterm.mod \
 	videotest.mod play.mod bitmap.mod tga.mod cpuid.mod serial.mod	\
-	ata.mod vga.mod
+	ata.mod vga.mod pci.mod lspci.mod
 
 # For biosdisk.mod.
 biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
@@ -264,4 +264,14 @@ vga_mod_SOURCES = term/i386/pc/vga.c
 vga_mod_CFLAGS = $(COMMON_CFLAGS)
 vga_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For pci.mod
+pci_mod_SOURCES = bus/pci.c
+pci_mod_CFLAGS = $(COMMON_CFLAGS)
+pci_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+# For lspci.mod
+lspci_mod_SOURCES = commands/lspci.c
+lspci_mod_CFLAGS = $(COMMON_CFLAGS)
+lspci_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 include $(srcdir)/conf/common.mk

Index: include/grub/pci.h
===================================================================
RCS file: include/grub/pci.h

grub/pci.h new file +37 -0
diff -N include/grub/pci.h
@@ -0,0 +1,37 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2008  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef	GRUB_PCI_H
+#define	GRUB_PCI_H	1
+
+#include <grub/types.h>
+#include <grub/symbol.h>
+
+typedef grub_uint32_t grub_pci_id_t;
+typedef int (*grub_pci_iteratefunc_t) (int bus, int device, int func,
+				       grub_pci_id_t pciid);
+typedef grub_uint32_t grub_pci_address_t;
+
+grub_pci_address_t EXPORT_FUNC(grub_pci_make_address) (int bus, int device,
+						       int function, int reg);
+
+void EXPORT_FUNC(grub_pci_iterate) (grub_pci_iteratefunc_t hook);
+
+#include <grub/machine/pci.h>
+
+#endif /* GRUB_PCI_H */

Index: include/grub/i386/pc/pci.h
===================================================================
RCS file: include/grub/i386/pc/pci.h

grub/i386/pc/pci.h new file +35 -0
diff -N include/grub/i386/pc/pci.h
@@ -0,0 +1,35 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2008  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef	GRUB_CPU_PCI_H
+#define	GRUB_CPU_PCI_H	1
+
+#include <grub/types.h>
+#include <grub/i386/io.h>
+
+#define GRUB_PCI_ADDR_REG	0xcf8
+#define GRUB_PCI_DATA_REG	0xcfc
+
+static inline grub_uint32_t
+grub_pci_read (grub_pci_address_t addr)
+{
+  grub_outl (addr, GRUB_PCI_ADDR_REG);
+  return grub_inl (GRUB_PCI_DATA_REG);
+}
+
+#endif /* GRUB_CPU_PCI_H */
#1 lore raw
Quoted 6 lines · Marco Gerards

I wanted to wait with this patch until the ATA driver is adapted. But I won't have time for that this week. So I can better get this committed and people working on this ;-). There is some code for ATA, but it was not tested yet. The main problem here is qemu.

Quoted 6 lines

Does someone know if it is possible, at all, to have ATA devices showing up as non-legacy PCI devices in Qemu? IDE interfaces are placed (and locked) in Legacy mode, meaning default IRQs and IO ports are used. This means just our old ISA code will be used. To support non-legacy modes, it would be nice if I could do some testing. Does someone have suggestions?

Try:

hw/pc.c 2008-01-28 18:25:00.000000000 +0100 renamed from hw/pc.c~ 2007-06-06 18:26:13.000000000 +0200 +2 -0
@@ -676,6 +676,8 @@
     qemu_irq *cpu_irq;
     qemu_irq *i8259;
 
+    pci_enabled = 1;
+
     linux_boot = (kernel_filename != NULL);
 
     /* init CPUs */
Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#2 lore raw
Quoted 19 lines · Robert Millan
Quoted 6 lines · Marco Gerards

I wanted to wait with this patch until the ATA driver is adapted. But I won't have time for that this week. So I can better get this committed and people working on this ;-). There is some code for ATA, but it was not tested yet. The main problem here is qemu.

Quoted 6 lines

Does someone know if it is possible, at all, to have ATA devices showing up as non-legacy PCI devices in Qemu? IDE interfaces are placed (and locked) in Legacy mode, meaning default IRQs and IO ports are used. This means just our old ISA code will be used. To support non-legacy modes, it would be nice if I could do some testing. Does someone have suggestions?

Try:

hw/pc.c 2008-01-28 18:25:00.000000000 +0100 line 683

Quoted 8 lines
hw/pc.c 2008-01-28 18:25:00.000000000 +0100 renamed from hw/pc.c~ 2007-06-06 18:26:13.000000000 +0200 +2 -0
@@ -676,6 +676,8 @@
     qemu_irq *cpu_irq;
     qemu_irq *i8259;
 
+    pci_enabled = 1;
+
     linux_boot = (kernel_filename != NULL);
 
     /* init CPUs */

The problem isn't that PCI isn't enabled. The problem is that IDE devices are in legacy mode...

So you do see the IDE interface using lspci. One bit can be used to check if the device is in legacy mode or not. If it indicates legacy mode, you have to use some fixed ports that are already present in ata.c. Otherwise, you can query the port ranges from the PCI device. Qemu only supports the latter mode, as it seems.

--
Marco

#3 lore raw
Quoted 15 lines · Marco Gerards

hw/pc.c 2008-01-28 18:25:00.000000000 +0100 line 683

Quoted 11 lines
hw/pc.c 2008-01-28 18:25:00.000000000 +0100 renamed from hw/pc.c~ 2007-06-06 18:26:13.000000000 +0200 +2 -0
@@ -676,6 +676,8 @@
     qemu_irq *cpu_irq;
     qemu_irq *i8259;
 
+    pci_enabled = 1;
+
     linux_boot = (kernel_filename != NULL);
 
     /* init CPUs */

The problem isn't that PCI isn't enabled. The problem is that IDE devices are in legacy mode...

Quoted 5 lines

So you do see the IDE interface using lspci. One bit can be used to check if the device is in legacy mode or not. If it indicates legacy mode, you have to use some fixed ports that are already present in ata.c. Otherwise, you can query the port ranges from the PCI device. Qemu only supports the latter mode, as it seems.

You're right. Even when PCI is enabled, you still get the same, although it doesn't look like it at first glance:

    if (pci_enabled) {
        pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1, i8259);
    } else {
        for(i = 0; i < 2; i++) {
            isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
                         bs_table[2 * i], bs_table[2 * i + 1]);
        }
    }

I think what you want is to change the hardcoded port numbers in hw/ide.c:pci_piix3_ide_init():

    ide_init_ioport(&d->ide_if[0], 0x1f0, 0x3f6);
    ide_init_ioport(&d->ide_if[2], 0x170, 0x376);

could that be it?

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#4 lore raw
Quoted 34 lines · Robert Millan
Quoted 15 lines · Marco Gerards

hw/pc.c 2008-01-28 18:25:00.000000000 +0100 line 683

Quoted 11 lines
hw/pc.c 2008-01-28 18:25:00.000000000 +0100 renamed from hw/pc.c~ 2007-06-06 18:26:13.000000000 +0200 +2 -0
@@ -676,6 +676,8 @@
     qemu_irq *cpu_irq;
     qemu_irq *i8259;
 
+    pci_enabled = 1;
+
     linux_boot = (kernel_filename != NULL);
 
     /* init CPUs */

The problem isn't that PCI isn't enabled. The problem is that IDE devices are in legacy mode...

Quoted 5 lines

So you do see the IDE interface using lspci. One bit can be used to check if the device is in legacy mode or not. If it indicates legacy mode, you have to use some fixed ports that are already present in ata.c. Otherwise, you can query the port ranges from the PCI device. Qemu only supports the latter mode, as it seems.

You're right. Even when PCI is enabled, you still get the same, although it doesn't look like it at first glance:

    if (pci_enabled) {
        pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1, i8259);
    } else {
        for(i = 0; i < 2; i++) {
            isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
                         bs_table[2 * i], bs_table[2 * i + 1]);
        }
    }
Quoted 7 lines

I think what you want is to change the hardcoded port numbers in hw/ide.c:pci_piix3_ide_init():

    ide_init_ioport(&d->ide_if[0], 0x1f0, 0x3f6);
    ide_init_ioport(&d->ide_if[2], 0x170, 0x376);

could that be it?

Perhaps... But it is still in legacy mode I guess... ;-)

--
Marco

#5 lore raw
In reply to Marco Gerards
Quoted 10 lines · Marco Gerards

2009-01-28 Marco Gerards <[email protected]>

* bus/pci.c: New file.

* include/grub/pci.h: Likewise.

* include/grub/i386/pc/pci.h: Likewise.

* commands/lspci.c: Likewise.

Quoted 8 lines

* conf/i386-pc.rmk (pkglib_MODULES): Add pci.mod' and lspci.mod'.
(pci_mod_SOURCES): New variable. (pci_mod_CFLAGS): Likewise. (pci_mod_LDFLAGS): Likewise. (lspci_mod_SOURCES): Likewise. (lspci_mod_CFLAGS): Likewise. (lspci_mod_LDFLAGS): Likewise.

No objections? Did everyone have a (quick) look at the interfaces?

If I hear nothing, I will commit this patch Friday.

--
Marco

#6 lore raw
Quoted 21 lines · Marco Gerards
Quoted 10 lines · Marco Gerards

2009-01-28 Marco Gerards <[email protected]>

* bus/pci.c: New file.

* include/grub/pci.h: Likewise.

* include/grub/i386/pc/pci.h: Likewise.

* commands/lspci.c: Likewise.

Quoted 8 lines

* conf/i386-pc.rmk (pkglib_MODULES): Add pci.mod' and lspci.mod'.
(pci_mod_SOURCES): New variable. (pci_mod_CFLAGS): Likewise. (pci_mod_LDFLAGS): Likewise. (lspci_mod_SOURCES): Likewise. (lspci_mod_CFLAGS): Likewise. (lspci_mod_LDFLAGS): Likewise.

Quoted 1 line

No objections? Did everyone have a (quick) look at the interfaces?

You put grub_pci_read() in an arch-specific file; is that function really arch-specific, or only its IO addresses are?

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#14 lore raw
In reply to Marco Gerards
Quoted 3 lines · Marco Gerards

No objections? Did everyone have a (quick) look at the interfaces?

If I hear nothing, I will commit this patch Friday.

You didn't check the return value from a hook. Besides that, okay for me.

Thanks,
Okuji

#15 lore raw
Quoted 6 lines · Yoshinori K. Okuji
Quoted 3 lines · Marco Gerards

No objections? Did everyone have a (quick) look at the interfaces?

If I hear nothing, I will commit this patch Friday.

You didn't check the return value from a hook. Besides that, okay for me.

Right, I should do that. Before I commit :-)

--
Marco

#7 lore raw
In reply to Robert Millan
Quoted 22 lines · Robert Millan
Quoted 21 lines · Marco Gerards
Quoted 18 lines · Marco Gerards

2009-01-28 Marco Gerards <[email protected]>

* bus/pci.c: New file.

* include/grub/pci.h: Likewise.

* include/grub/i386/pc/pci.h: Likewise.

* commands/lspci.c: Likewise.

* conf/i386-pc.rmk (pkglib_MODULES): Add pci.mod' and lspci.mod'.
(pci_mod_SOURCES): New variable. (pci_mod_CFLAGS): Likewise. (pci_mod_LDFLAGS): Likewise. (lspci_mod_SOURCES): Likewise. (lspci_mod_CFLAGS): Likewise. (lspci_mod_LDFLAGS): Likewise.

Quoted 4 lines
Quoted 1 line

No objections? Did everyone have a (quick) look at the interfaces?

You put grub_pci_read() in an arch-specific file; is that function really arch-specific, or only its IO addresses are?

IO addresses are Intel only, AFAIK. Almost all architectures have mmapped IO. It's really arch specific.

--
Marco

#8 lore raw
Quoted 24 lines · Marco Gerards
Quoted 22 lines · Robert Millan
Quoted 21 lines · Marco Gerards
Quoted 18 lines · Marco Gerards

2009-01-28 Marco Gerards <[email protected]>

* bus/pci.c: New file.

* include/grub/pci.h: Likewise.

* include/grub/i386/pc/pci.h: Likewise.

* commands/lspci.c: Likewise.

* conf/i386-pc.rmk (pkglib_MODULES): Add pci.mod' and lspci.mod'.
(pci_mod_SOURCES): New variable. (pci_mod_CFLAGS): Likewise. (pci_mod_LDFLAGS): Likewise. (lspci_mod_SOURCES): Likewise. (lspci_mod_CFLAGS): Likewise. (lspci_mod_LDFLAGS): Likewise.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

No objections? Did everyone have a (quick) look at the interfaces?

You put grub_pci_read() in an arch-specific file; is that function really arch-specific, or only its IO addresses are?

IO addresses are Intel only, AFAIK. Almost all architectures have mmapped IO. It's really arch specific.

Are you sure that makes grub_inl / grub_outl arch-specific ? They can't be implemented as wrappers for direct memory access?

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#9 lore raw
Quoted 27 lines · Robert Millan
Quoted 26 lines · Marco Gerards
Quoted 24 lines · Robert Millan
Quoted 22 lines · Marco Gerards
Quoted 10 lines · Marco Gerards

2009-01-28 Marco Gerards <[email protected]>

* bus/pci.c: New file.

* include/grub/pci.h: Likewise.

* include/grub/i386/pc/pci.h: Likewise.

* commands/lspci.c: Likewise.

Quoted 8 lines

* conf/i386-pc.rmk (pkglib_MODULES): Add pci.mod' and lspci.mod'.
(pci_mod_SOURCES): New variable. (pci_mod_CFLAGS): Likewise. (pci_mod_LDFLAGS): Likewise. (lspci_mod_SOURCES): Likewise. (lspci_mod_CFLAGS): Likewise. (lspci_mod_LDFLAGS): Likewise.

No objections? Did everyone have a (quick) look at the interfaces?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

You put grub_pci_read() in an arch-specific file; is that function really arch-specific, or only its IO addresses are?

IO addresses are Intel only, AFAIK. Almost all architectures have mmapped IO. It's really arch specific.

Are you sure that makes grub_inl / grub_outl arch-specific ? They can't be implemented as wrappers for direct memory access?

No, AFAIK that is not possible. However, PCI gives you the mmapped IO addresses we can use, for example for the ATA driver.

--
Marco

#10 lore raw
Quoted 8 lines · Marco Gerards
Quoted 5 lines
Quoted 2 lines

IO addresses are Intel only, AFAIK. Almost all architectures have mmapped IO. It's really arch specific.

Are you sure that makes grub_inl / grub_outl arch-specific ? They can't be implemented as wrappers for direct memory access?

No, AFAIK that is not possible. However, PCI gives you the mmapped IO addresses we can use, for example for the ATA driver.

Why not?

What's wrong with:

grub_uint32_t
grub_inl (grub_uint32_t *addr)
{
return *addr;
}

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#11 lore raw
Quoted 12 lines · Robert Millan
Quoted 8 lines · Marco Gerards
Quoted 5 lines
Quoted 2 lines

IO addresses are Intel only, AFAIK. Almost all architectures have mmapped IO. It's really arch specific.

Are you sure that makes grub_inl / grub_outl arch-specific ? They can't be implemented as wrappers for direct memory access?

No, AFAIK that is not possible. However, PCI gives you the mmapped IO addresses we can use, for example for the ATA driver.

Why not?

Quoted 7 lines

What's wrong with:

grub_uint32_t
grub_inl (grub_uint32_t *addr)
{
return *addr;
}

Nothing, except the missing volatile I guess. This just isn't an IO port. So you can't use IO port 0x60 to access the keyboard or so. But sure, we need an abstraction.

--
Marco

#12 lore raw
Quoted 10 lines · Marco Gerards
Quoted 6 lines

grub_uint32_t
grub_inl (grub_uint32_t *addr)
{
return *addr;
}

Nothing, except the missing volatile I guess. This just isn't an IO port. So you can't use IO port 0x60 to access the keyboard or so. But sure, we need an abstraction.

AT keyboard is too arch-specific, but ATA and PCI aren't. Anyway, I suppose it's no big deal as we can always move the code later.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#13 lore raw
Quoted 8 lines · Robert Millan
Quoted 7 lines · Marco Gerards
Quoted 6 lines

grub_uint32_t
grub_inl (grub_uint32_t *addr)
{
return *addr;
}

Quoted 6 lines
Quoted 3 lines

Nothing, except the missing volatile I guess. This just isn't an IO port. So you can't use IO port 0x60 to access the keyboard or so. But sure, we need an abstraction.

AT keyboard is too arch-specific, but ATA and PCI aren't. Anyway, I suppose it's no big deal as we can always move the code later.

Right. Committed.

--
Marco