pci: Add PCI device infrastructure

Add generic PCI packages:

o HW.PCI containing basic definitions for PCI devices and
  configuration space headers,
o HW.PCI.MMConf that provides access to memory mapped confi-
  guration space of a PCI device, and
o HW.PCI.Dev adding a method to map resources of a PCI device.

Change-Id: I266ea35e273ff49fdb3176d617bb0b4ab1f13f93
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/20258
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/common/Makefile.inc b/common/Makefile.inc
index 8759664..cc88032 100644
--- a/common/Makefile.inc
+++ b/common/Makefile.inc
@@ -3,6 +3,9 @@
 hw-y += hw-mmio_range.ads
 hw-y += hw-mmio_regs.adb
 hw-y += hw-mmio_regs.ads
+hw-y += hw-pci.ads
+hw-y += hw-pci-mmconf.adb
+hw-y += hw-pci-mmconf.ads
 hw-y += hw-port_io.adb
 hw-y += hw-port_io.ads
 hw-y += hw-sub_regs.ads
@@ -16,6 +19,7 @@
 $(hw-config-ads): $(dir)/hw-config.ads.template $(cnf)
 	printf "    GENERATE   $(patsubst /%,%,$(subst $(obj)/,,$@))\n"
 	sed \
+	    -e's/<<DEFAULT_MMCONF_BASE>>/$(CONFIG_HWBASE_DEFAULT_MMCONF)/' \
 	    -e's/<<DYNAMIC_MMIO>>/$(if $(filter y,$(CONFIG_HWBASE_DYNAMIC_MMIO)),True,False)/' \
 	    $< >$@
 hw-gen-y += $(hw-config-ads)
diff --git a/common/hw-config.ads.template b/common/hw-config.ads.template
index 5a25ccb..a55b230 100644
--- a/common/hw-config.ads.template
+++ b/common/hw-config.ads.template
@@ -17,4 +17,6 @@
 
    Dynamic_MMIO : constant Boolean := <<DYNAMIC_MMIO>>;
 
+   Default_MMConf_Base : constant := <<DEFAULT_MMCONF_BASE>>;
+
 end HW.Config;
diff --git a/common/hw-pci-dev.ads b/common/hw-pci-dev.ads
new file mode 100644
index 0000000..b08f9ab
--- /dev/null
+++ b/common/hw-pci-dev.ads
@@ -0,0 +1,50 @@
+--
+-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
+--
+-- This program 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 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program 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.
+--
+
+generic
+   Dev : PCI.Address := (0, 0, 0);
+package HW.PCI.Dev
+with
+   Abstract_State => (Address_State, (PCI_State with External)),
+   Initializes => Address_State
+is
+
+   procedure Read8 (Value : out Word8; Offset : Index);
+   procedure Read16 (Value : out Word16; Offset : Index)
+   with
+      Pre => Offset mod 2 = 0;
+   procedure Read32 (Value : out Word32; Offset : Index)
+   with
+      Pre => Offset mod 4 = 0;
+
+   procedure Write8 (Offset : Index; Value : Word8);
+   procedure Write16 (Offset : Index; Value : Word16)
+   with
+      Pre => Offset mod 2 = 0;
+   procedure Write32 (Offset : Index; Value : Word32)
+   with
+      Pre => Offset mod 4 = 0;
+
+   procedure Map
+     (Addr     :    out Word64;
+      Res      : in     Resource;
+      Length   : in     Natural := 0;
+      Offset   : in     Natural := 0;
+      WC       : in     Boolean := False);
+
+   procedure Resource_Size (Length : out Natural; Res : Resource);
+
+   procedure Initialize (Success : out Boolean; MMConf_Base : Word64 := 0);
+
+end HW.PCI.Dev;
diff --git a/common/hw-pci-mmconf.adb b/common/hw-pci-mmconf.adb
new file mode 100644
index 0000000..180f2d7
--- /dev/null
+++ b/common/hw-pci-mmconf.adb
@@ -0,0 +1,80 @@
+--
+-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
+--
+-- This program 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 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program 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.
+--
+
+with HW.Config;
+
+package body HW.PCI.MMConf
+with
+   Refined_State =>
+     (Address_State  =>
+        (MM8.Base_Address, MM16.Base_Address, MM32.Base_Address),
+      PCI_State      =>
+        (MM8.State, MM16.State, MM32.State))
+is
+
+   Default_Base_Address : constant Word64 :=
+      Calc_Base_Address (Config.Default_MMConf_Base, Dev);
+
+   type Index16 is new Index range 0 .. Index'Last / 2;
+   type Index32 is new Index range 0 .. Index'Last / 4;
+
+   type Array8 is array (Index) of Byte with Atomic_Components;
+   type Array16 is array (Index16) of Word16 with Atomic_Components;
+   type Array32 is array (Index32) of Word32 with Atomic_Components;
+
+   package MM8 is new HW.MMIO_Range
+     (Default_Base_Address, Word8, Index, Array8);
+   package MM16 is new HW.MMIO_Range
+     (Default_Base_Address, Word16, Index16, Array16);
+   package MM32 is new HW.MMIO_Range
+     (Default_Base_Address, Word32, Index32, Array32);
+
+   procedure Read8 (Value : out Word8; Offset : Index) renames MM8.Read;
+
+   procedure Read16 (Value : out Word16; Offset : Index)
+   is
+   begin
+      MM16.Read (Value, Index16 (Offset / 2));
+   end Read16;
+
+   procedure Read32 (Value : out Word32; Offset : Index)
+   is
+   begin
+      MM32.Read (Value, Index32 (Offset / 4));
+   end Read32;
+
+   procedure Write8 (Offset : Index; Value : Word8) renames MM8.Write;
+
+   procedure Write16 (Offset : Index; Value : Word16)
+   is
+   begin
+      MM16.Write (Index16 (Offset / 2), Value);
+   end Write16;
+
+   procedure Write32 (Offset : Index; Value : Word32)
+   is
+   begin
+      MM32.Write (Index32 (Offset / 4), Value);
+   end Write32;
+
+   procedure Set_Base_Address (Base : Word64)
+   is
+      Base_Address : constant Word64 := Calc_Base_Address (Base, Dev);
+   begin
+      MM8.Set_Base_Address (Base_Address);
+      MM16.Set_Base_Address (Base_Address);
+      MM32.Set_Base_Address (Base_Address);
+   end Set_Base_Address;
+
+end HW.PCI.MMConf;
diff --git a/common/hw-pci-mmconf.ads b/common/hw-pci-mmconf.ads
new file mode 100644
index 0000000..03c9f44
--- /dev/null
+++ b/common/hw-pci-mmconf.ads
@@ -0,0 +1,46 @@
+--
+-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
+--
+-- This program 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 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program 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.
+--
+
+with System;
+with HW.MMIO_Range;
+
+pragma Elaborate_All (HW.MMIO_Range);
+
+private generic
+   Dev : Address := (0, 0, 0);
+package HW.PCI.MMConf
+with
+   Abstract_State => (Address_State, (PCI_State with External)),
+   Initializes => Address_State
+is
+
+   procedure Read8 (Value : out Word8; Offset : Index);
+   procedure Read16 (Value : out Word16; Offset : Index)
+   with
+      Pre => Offset mod 2 = 0;
+   procedure Read32 (Value : out Word32; Offset : Index)
+   with
+      Pre => Offset mod 4 = 0;
+
+   procedure Write8 (Offset : Index; Value : Word8);
+   procedure Write16 (Offset : Index; Value : Word16)
+   with
+      Pre => Offset mod 2 = 0;
+   procedure Write32 (Offset : Index; Value : Word32)
+   with
+      Pre => Offset mod 4 = 0;
+
+   procedure Set_Base_Address (Base : Word64);
+
+end HW.PCI.MMConf;
diff --git a/common/hw-pci.ads b/common/hw-pci.ads
new file mode 100644
index 0000000..da4f42f
--- /dev/null
+++ b/common/hw-pci.ads
@@ -0,0 +1,61 @@
+--
+-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
+--
+-- This program 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 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program 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.
+--
+
+package HW.PCI
+is
+
+   type Bus is range 0 .. 255;
+   type Slot is range 0 .. 31;
+   type Func is range 0 ..  7;
+
+   type Address is record
+      Bus   : PCI.Bus;
+      Slot  : PCI.Slot;
+      Func  : PCI.Func;
+   end record;
+
+   type Index is range 0 .. 4095;
+
+   Vendor_Id                     : constant Index := 16#00#;
+   Device_Id                     : constant Index := 16#02#;
+   Command                       : constant Index := 16#04#;
+      Command_Memory             : constant := 16#02#;
+   Header_Type                   : constant Index := 16#0e#;
+      Header_Type_Mask           : constant := 16#7f#;
+      Header_Type_Normal         : constant := 16#00#;
+
+   type Resource is (Res0, Res1, Res2, Res3, Res4, Res5);
+   Base_Address : constant array (Resource) of Index :=
+     (16#10#, 16#14#, 16#18#, 16#1c#, 16#20#, 16#24#);
+      Base_Address_Space_Mask    : constant := 1 * 2 ** 0;
+      Base_Address_Space_IO      : constant := 1 * 2 ** 0;
+      Base_Address_Space_Mem     : constant := 0 * 2 ** 0;
+      Base_Address_Mem_Type_Mask : constant := 3 * 2 ** 1;
+      Base_Address_Mem_Type_32   : constant := 0 * 2 ** 1;
+      Base_Address_Mem_Type_1M   : constant := 1 * 2 ** 1;
+      Base_Address_Mem_Type_64   : constant := 2 * 2 ** 1;
+      Base_Address_Mem_Prefetch  : constant := 1 * 2 ** 3;
+      Base_Address_IO_Mask       : constant := 16#ffff_fffc#;
+      Base_Address_Mem_Mask      : constant := 16#ffff_fff0#;
+
+private
+   use type HW.Word64;
+   function Calc_Base_Address (Base_Addr : Word64; Dev : Address) return Word64
+   is
+     (Base_Addr +
+      Word64 (Dev.Bus) * 32 * 8 * 4096 +
+      Word64 (Dev.Slot) * 8 * 4096 +
+      Word64 (Dev.Func) * 4096);
+
+end HW.PCI;
diff --git a/configs/defconfig b/configs/defconfig
index c0db7c3..c47e560 100644
--- a/configs/defconfig
+++ b/configs/defconfig
@@ -5,3 +5,4 @@
 CONFIG_HWBASE_TIMER_CLOCK_GETTIME	= y
 CONFIG_HWBASE_TIMER_MUTIME		=
 CONFIG_HWBASE_POSIX_FILE		=
+CONFIG_HWBASE_DEFAULT_MMCONF		= 16\#f000_0000\#
diff --git a/configs/posix b/configs/posix
index 183a5b5..f75f6e7 100644
--- a/configs/posix
+++ b/configs/posix
@@ -5,3 +5,4 @@
 CONFIG_HWBASE_TIMER_CLOCK_GETTIME	= y
 CONFIG_HWBASE_TIMER_MUTIME		=
 CONFIG_HWBASE_POSIX_FILE		= y
+CONFIG_HWBASE_DEFAULT_MMCONF		= 0