Subversion Repositories HelenOS

Rev

Rev 4418 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4399 trochtova 1
#include <futex.h>
2
#include <assert.h>
3
 
4
#include "pci.h"
5
#include "pci_bus.h"
6
#include "pci_regs.h"
7
#include "pci_conf.h"
8
 
9
LIST_INITIALIZE(devices_list);
10
LIST_INITIALIZE(buses_list);
11
LIST_INITIALIZE(drivers_list);
12
 
13
static atomic_t pci_bus_futex = FUTEX_INITIALIZER;
14
 
15
static int pci_match(pci_drv_t *drv, pci_dev_t *dev);
16
static int pci_pass_dev(pci_drv_t *drv, pci_dev_t *dev);
17
static void pci_lookup_driver(pci_dev_t *dev);
18
static void pci_lookup_devices(pci_drv_t *drv);
19
 
20
 
21
void pci_bus_scan(pci_bus_t *bus)
22
{
23
    pci_dev_t *dev = pci_alloc_dev();
24
    pci_bus_t *child_bus = NULL;
4418 trochtova 25
    int dnum, fnum, bus_num;
4399 trochtova 26
    bool multi;
27
    uint8_t header_type;
28
 
4438 trochtova 29
    for (dnum = 0; dnum < 32; dnum++) {
4399 trochtova 30
        multi = true;
31
        for (fnum = 0; multi && fnum < 8; fnum++) {
32
            pci_init_dev(dev, bus, dnum, fnum);
33
            dev->vendor_id = pci_conf_read_16(dev, PCI_VENDOR_ID);
34
            dev->device_id = pci_conf_read_16(dev, PCI_DEVICE_ID);
4438 trochtova 35
            if (dev->vendor_id == 0xFFFF) { // device is not present, go on scanning the bus
36
                if (fnum == 0) {
37
                    break;
38
                } else {
39
                    continue;  
40
                }
4399 trochtova 41
            }
42
            header_type = pci_conf_read_8(dev, PCI_HEADER_TYPE);
43
            if (fnum == 0) {
44
                 multi = header_type >> 7;  // is the device multifunction?
45
            }
46
            header_type = header_type & 0x7F; // clear the multifunction bit
47
 
4438 trochtova 48
            printf("PCI: adding new device %d : %d : %d", dev->bus->num, dnum, fnum);
49
            printf(" - vendor = 0x%04X, device = 0x%04X.\n", dev->vendor_id, dev->device_id);
50
            pci_device_register(dev);          
4399 trochtova 51
 
52
            if (header_type == PCI_HEADER_TYPE_BRIDGE || header_type == PCI_HEADER_TYPE_CARDBUS ) {
4418 trochtova 53
                bus_num = pci_conf_read_8(dev, PCI_BRIDGE_SEC_BUS_NUM);
4438 trochtova 54
                printf("PCI: device is pci-to-pci bridge, secondary bus number = %d.\n", bus_num);
55
                if(bus_num > bus->num) {                   
4399 trochtova 56
                    child_bus = pci_alloc_bus();
4418 trochtova 57
                    pci_init_bus(child_bus, bus, bus_num);
4399 trochtova 58
                    pci_bus_register(child_bus);
59
                    pci_bus_scan(child_bus);   
4418 trochtova 60
                }                  
4399 trochtova 61
            }
4438 trochtova 62
 
63
            dev = pci_alloc_dev();  // alloc new aux. dev. structure
4399 trochtova 64
        }
65
    }
66
 
67
    if (dev->vendor_id == 0xFFFF) {
68
        pci_free_dev(dev);  // free the auxiliary device structure
69
    }  
70
}
71
 
72
/*
73
 * Usage: pci_bus_futex must be down.
74
 */
75
static int pci_pass_dev(pci_drv_t *drv, pci_dev_t *dev)
76
{
77
    assert(dev->driver == NULL);
78
 
79
    if (drv->ops->add_device(dev)) {
80
        dev->driver = drv;
81
        return 1;
82
    }
83
 
84
    return 0;
85
}
86
 
87
/*
88
 * Usage: pci_bus_futex must be down.
89
 */
90
static void pci_lookup_driver(pci_dev_t *dev)
91
{
92
    link_t *item = drivers_list.next;
93
    pci_drv_t *drv = NULL;
94
 
95
    while (item != &drivers_list) {
96
        drv = list_get_instance(item, pci_drv_t, link);
97
        if (pci_match(drv, dev) && pci_pass_dev(drv, dev)) {
98
            break;
99
        }
100
        item = item->next;
101
    }
102
}
103
 
104
/*
105
 * Usage: pci_bus_futex must be down.
106
 */
107
static void pci_lookup_devices(pci_drv_t *drv)
108
{
109
    link_t *item = devices_list.next;
110
    pci_dev_t *dev = NULL;
111
 
112
    while (item != &devices_list) {
113
        dev = list_get_instance(item, pci_dev_t, link);
114
        if (dev->driver == NULL && pci_match(drv, dev)) {
115
            pci_pass_dev(drv, dev);
116
        }
117
        item = item->next;
118
    }
119
}
120
 
121
static int pci_match(pci_drv_t *drv, pci_dev_t *dev)
122
{
123
    return drv->vendor_id == dev->vendor_id && drv->device_id == dev->vendor_id;
124
}
125
 
126
void pci_bus_register(pci_bus_t *bus)
127
{
128
    futex_down(&pci_bus_futex);
129
 
130
    // add the device to the list of pci devices
131
    list_append(&(bus->link), &buses_list);
132
 
133
    futex_up(&pci_bus_futex);
134
}
135
 
136
void pci_device_register(pci_dev_t *dev)
137
{
138
    futex_down(&pci_bus_futex);
139
 
140
    // add the device to the list of pci devices
141
    list_append(&(dev->link), &devices_list);
142
 
143
    // try to find suitable driver for the device and pass the device to it
144
    pci_lookup_driver(dev);
145
 
146
    futex_up(&pci_bus_futex);  
147
}
148
 
149
void pci_driver_register(pci_drv_t *drv)
150
{
151
    futex_down(&pci_bus_futex);
152
    list_append(&(drv->link), &drivers_list);
153
 
154
    // try to find compatible devices
155
    pci_lookup_devices(drv);
156
 
157
    futex_up(&pci_bus_futex);
158
}
159