-Test commit\n-Add pci.[ch]\n
[pintos-anon] / src / devices / pci.c
1 #include <debug.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include "threads/io.h"
5 #include "devices/pci.h"
6
7 static uint32_t
8 pci_read_config (unsigned bus, unsigned dev, unsigned func, unsigned reg) 
9 {
10   ASSERT (bus < 256);
11   ASSERT (dev < 32);
12   ASSERT (func < 8);
13   ASSERT (reg < 64);
14
15   outl (0xcf8,
16         0x80000000 | (bus << 16) | (dev << 11) | (func << 8) | (reg << 2));
17   return inl (0xcfc);
18 }
19
20 static bool
21 scan_device (int bus, int dev, int func) 
22 {
23   uint32_t cfg[16];
24   int dev_id, vendor_id;
25   int line;
26   int reg;
27
28   for (reg = 0; reg < 16; reg++)
29     cfg[reg] = pci_read_config (bus, dev, func, reg);
30   dev_id = cfg[0] >> 16;
31   vendor_id = cfg[0] & 0xffff;
32
33   if (dev_id == 0 || dev_id == 0xffff
34       || vendor_id == 0 || vendor_id == 0xffff)
35     return 0;
36
37   printf ("%04x:%02x:%02x.%x PCI device %04x:%04x\n",
38           0, bus, dev, func, vendor_id, dev_id);
39   for (line = 0; line < 4; line++) 
40     {
41       int byte;
42
43       printf ("%02x:", line * 16);
44       for (byte = 0; byte < 16; byte++)
45         printf (" %02x", ((uint8_t *) cfg)[line * 16 + byte]);
46       printf ("\n");
47     }
48   printf ("\n");
49
50   return cfg[3] & 0x00800000;
51 }
52
53 void
54 pci_scan (void) 
55 {
56   int dev;
57
58   printf ("PCI BUS: Scanning \n");
59   
60   for (dev = 0; dev < 32; dev++) 
61     {
62       if (scan_device (0, dev, 0)) 
63         {
64           int func;
65
66           for (func = 1; func < 8; func++)
67             scan_device (0, dev, func);
68         }
69     }
70
71   printf ("PCI BUS: Done \n");
72 }