Subversion Repositories Kolibri OS

Rev

Rev 1633 | Rev 2187 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1633 Rev 1867
1
#include 
1
#include 
2
#include 
2
#include 
3
#include 
3
#include 
4
#include 
4
#include 
5
#include 
5
#include 
6
 
6
 
7
LIST_HEAD(pci_root_buses);
7
LIST_HEAD(pci_root_buses);
8
 
8
 
9
#define IO_SPACE_LIMIT 0xffff
9
#define IO_SPACE_LIMIT 0xffff
10
 
10
 
11
 
11
 
12
 
12
 
13
 
13
 
14
#define LEGACY_IO_RESOURCE  (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
14
#define LEGACY_IO_RESOURCE  (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
15
 
15
 
16
#define CARDBUS_LATENCY_TIMER   176 /* secondary latency timer */
16
#define CARDBUS_LATENCY_TIMER   176 /* secondary latency timer */
17
#define CARDBUS_RESERVE_BUSNR   3
17
#define CARDBUS_RESERVE_BUSNR   3
18
 
18
 
19
static int pcibios_assign_all_busses(void)
19
static int pcibios_assign_all_busses(void)
20
{
20
{
21
    return 0;
21
    return 0;
22
};
22
};
23
 
23
 
24
/**
24
/**
25
 * pci_ari_enabled - query ARI forwarding status
25
 * pci_ari_enabled - query ARI forwarding status
26
 * @bus: the PCI bus
26
 * @bus: the PCI bus
27
 *
27
 *
28
 * Returns 1 if ARI forwarding is enabled, or 0 if not enabled;
28
 * Returns 1 if ARI forwarding is enabled, or 0 if not enabled;
29
 */
29
 */
30
static inline int pci_ari_enabled(struct pci_bus *bus)
30
static inline int pci_ari_enabled(struct pci_bus *bus)
31
{
31
{
32
    return bus->self && bus->self->ari_enabled;
32
    return bus->self && bus->self->ari_enabled;
33
}
33
}
34
 
34
 
35
/*
35
/*
36
 * Translate the low bits of the PCI base
36
 * Translate the low bits of the PCI base
37
 * to the resource type
37
 * to the resource type
38
 */
38
 */
39
static inline unsigned int pci_calc_resource_flags(unsigned int flags)
39
static inline unsigned int pci_calc_resource_flags(unsigned int flags)
40
{
40
{
41
    if (flags & PCI_BASE_ADDRESS_SPACE_IO)
41
    if (flags & PCI_BASE_ADDRESS_SPACE_IO)
42
        return IORESOURCE_IO;
42
        return IORESOURCE_IO;
43
 
43
 
44
    if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
44
    if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
45
        return IORESOURCE_MEM | IORESOURCE_PREFETCH;
45
        return IORESOURCE_MEM | IORESOURCE_PREFETCH;
46
 
46
 
47
    return IORESOURCE_MEM;
47
    return IORESOURCE_MEM;
48
}
48
}
49
 
49
 
50
static u64 pci_size(u64 base, u64 maxbase, u64 mask)
50
static u64 pci_size(u64 base, u64 maxbase, u64 mask)
51
{
51
{
52
    u64 size = mask & maxbase;  /* Find the significant bits */
52
    u64 size = mask & maxbase;  /* Find the significant bits */
53
    if (!size)
53
    if (!size)
54
        return 0;
54
        return 0;
55
 
55
 
56
    /* Get the lowest of them to find the decode size, and
56
    /* Get the lowest of them to find the decode size, and
57
       from that the extent.  */
57
       from that the extent.  */
58
    size = (size & ~(size-1)) - 1;
58
    size = (size & ~(size-1)) - 1;
59
 
59
 
60
    /* base == maxbase can be valid only if the BAR has
60
    /* base == maxbase can be valid only if the BAR has
61
       already been programmed with all 1s.  */
61
       already been programmed with all 1s.  */
62
    if (base == maxbase && ((base | size) & mask) != mask)
62
    if (base == maxbase && ((base | size) & mask) != mask)
63
        return 0;
63
        return 0;
64
 
64
 
65
    return size;
65
    return size;
66
}
66
}
67
 
67
 
68
static inline enum pci_bar_type decode_bar(struct resource *res, u32 bar)
68
static inline enum pci_bar_type decode_bar(struct resource *res, u32 bar)
69
{
69
{
70
    if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
70
    if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
71
        res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
71
        res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
72
        return pci_bar_io;
72
        return pci_bar_io;
73
    }
73
    }
74
 
74
 
75
    res->flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
75
    res->flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
76
 
76
 
77
    if (res->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
77
    if (res->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
78
        return pci_bar_mem64;
78
        return pci_bar_mem64;
79
    return pci_bar_mem32;
79
    return pci_bar_mem32;
80
}
80
}
81
 
81
 
82
 
82
 
83
 
83
 
84
/**
84
/**
85
 * pci_read_base - read a PCI BAR
85
 * pci_read_base - read a PCI BAR
86
 * @dev: the PCI device
86
 * @dev: the PCI device
87
 * @type: type of the BAR
87
 * @type: type of the BAR
88
 * @res: resource buffer to be filled in
88
 * @res: resource buffer to be filled in
89
 * @pos: BAR position in the config space
89
 * @pos: BAR position in the config space
90
 *
90
 *
91
 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
91
 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
92
 */
92
 */
93
int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
93
int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
94
            struct resource *res, unsigned int pos)
94
            struct resource *res, unsigned int pos)
95
{
95
{
96
    u32 l, sz, mask;
96
    u32 l, sz, mask;
97
    u16 orig_cmd;
97
    u16 orig_cmd;
98
 
98
 
99
    mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
99
    mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
100
 
100
 
101
    if (!dev->mmio_always_on) {
101
    if (!dev->mmio_always_on) {
102
        pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
102
        pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
103
        pci_write_config_word(dev, PCI_COMMAND,
103
        pci_write_config_word(dev, PCI_COMMAND,
104
            orig_cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
104
            orig_cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
105
    }
105
    }
106
 
106
 
107
    res->name = pci_name(dev);
107
    res->name = pci_name(dev);
108
 
108
 
109
    pci_read_config_dword(dev, pos, &l);
109
    pci_read_config_dword(dev, pos, &l);
110
    pci_write_config_dword(dev, pos, l | mask);
110
    pci_write_config_dword(dev, pos, l | mask);
111
    pci_read_config_dword(dev, pos, &sz);
111
    pci_read_config_dword(dev, pos, &sz);
112
    pci_write_config_dword(dev, pos, l);
112
    pci_write_config_dword(dev, pos, l);
113
 
113
 
114
    if (!dev->mmio_always_on)
114
    if (!dev->mmio_always_on)
115
        pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
115
        pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
116
 
116
 
117
    /*
117
    /*
118
     * All bits set in sz means the device isn't working properly.
118
     * All bits set in sz means the device isn't working properly.
119
     * If the BAR isn't implemented, all bits must be 0.  If it's a
119
     * If the BAR isn't implemented, all bits must be 0.  If it's a
120
     * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
120
     * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
121
     * 1 must be clear.
121
     * 1 must be clear.
122
     */
122
     */
123
    if (!sz || sz == 0xffffffff)
123
    if (!sz || sz == 0xffffffff)
124
        goto fail;
124
        goto fail;
125
 
125
 
126
    /*
126
    /*
127
     * I don't know how l can have all bits set.  Copied from old code.
127
     * I don't know how l can have all bits set.  Copied from old code.
128
     * Maybe it fixes a bug on some ancient platform.
128
     * Maybe it fixes a bug on some ancient platform.
129
     */
129
     */
130
    if (l == 0xffffffff)
130
    if (l == 0xffffffff)
131
        l = 0;
131
        l = 0;
132
 
132
 
133
    if (type == pci_bar_unknown) {
133
    if (type == pci_bar_unknown) {
134
        type = decode_bar(res, l);
134
        type = decode_bar(res, l);
135
        res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
135
        res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
136
        if (type == pci_bar_io) {
136
        if (type == pci_bar_io) {
137
            l &= PCI_BASE_ADDRESS_IO_MASK;
137
            l &= PCI_BASE_ADDRESS_IO_MASK;
138
            mask = PCI_BASE_ADDRESS_IO_MASK & IO_SPACE_LIMIT;
138
            mask = PCI_BASE_ADDRESS_IO_MASK & IO_SPACE_LIMIT;
139
        } else {
139
        } else {
140
            l &= PCI_BASE_ADDRESS_MEM_MASK;
140
            l &= PCI_BASE_ADDRESS_MEM_MASK;
141
            mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
141
            mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
142
        }
142
        }
143
    } else {
143
    } else {
144
        res->flags |= (l & IORESOURCE_ROM_ENABLE);
144
        res->flags |= (l & IORESOURCE_ROM_ENABLE);
145
        l &= PCI_ROM_ADDRESS_MASK;
145
        l &= PCI_ROM_ADDRESS_MASK;
146
        mask = (u32)PCI_ROM_ADDRESS_MASK;
146
        mask = (u32)PCI_ROM_ADDRESS_MASK;
147
    }
147
    }
148
 
148
 
149
    if (type == pci_bar_mem64) {
149
    if (type == pci_bar_mem64) {
150
        u64 l64 = l;
150
        u64 l64 = l;
151
        u64 sz64 = sz;
151
        u64 sz64 = sz;
152
        u64 mask64 = mask | (u64)~0 << 32;
152
        u64 mask64 = mask | (u64)~0 << 32;
153
 
153
 
154
        pci_read_config_dword(dev, pos + 4, &l);
154
        pci_read_config_dword(dev, pos + 4, &l);
155
        pci_write_config_dword(dev, pos + 4, ~0);
155
        pci_write_config_dword(dev, pos + 4, ~0);
156
        pci_read_config_dword(dev, pos + 4, &sz);
156
        pci_read_config_dword(dev, pos + 4, &sz);
157
        pci_write_config_dword(dev, pos + 4, l);
157
        pci_write_config_dword(dev, pos + 4, l);
158
 
158
 
159
        l64 |= ((u64)l << 32);
159
        l64 |= ((u64)l << 32);
160
        sz64 |= ((u64)sz << 32);
160
        sz64 |= ((u64)sz << 32);
161
 
161
 
162
        sz64 = pci_size(l64, sz64, mask64);
162
        sz64 = pci_size(l64, sz64, mask64);
163
 
163
 
164
        if (!sz64)
164
        if (!sz64)
165
            goto fail;
165
            goto fail;
166
 
166
 
167
        if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) {
167
        if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) {
168
            dbgprintf("%s reg %x: can't handle 64-bit BAR\n",
168
            dbgprintf("%s reg %x: can't handle 64-bit BAR\n",
169
                __FUNCTION__, pos);
169
                __FUNCTION__, pos);
170
            goto fail;
170
            goto fail;
171
        }
171
        }
172
 
172
 
173
        res->flags |= IORESOURCE_MEM_64;
173
        res->flags |= IORESOURCE_MEM_64;
174
        if ((sizeof(resource_size_t) < 8) && l) {
174
        if ((sizeof(resource_size_t) < 8) && l) {
175
            /* Address above 32-bit boundary; disable the BAR */
175
            /* Address above 32-bit boundary; disable the BAR */
176
            pci_write_config_dword(dev, pos, 0);
176
            pci_write_config_dword(dev, pos, 0);
177
            pci_write_config_dword(dev, pos + 4, 0);
177
            pci_write_config_dword(dev, pos + 4, 0);
178
            res->start = 0;
178
            res->start = 0;
179
            res->end = sz64;
179
            res->end = sz64;
180
        } else {
180
        } else {
181
            res->start = l64;
181
            res->start = l64;
182
            res->end = l64 + sz64;
182
            res->end = l64 + sz64;
183
        dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
183
        dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
184
        }
184
        }
185
    } else {
185
    } else {
186
        sz = pci_size(l, sz, mask);
186
        sz = pci_size(l, sz, mask);
187
 
187
 
188
        if (!sz)
188
        if (!sz)
189
            goto fail;
189
            goto fail;
190
 
190
 
191
        res->start = l;
191
        res->start = l;
192
        res->end = l + sz;
192
        res->end = l + sz;
193
 
193
 
194
        dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
194
        dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
195
    }
195
    }
196
 
196
 
197
 out:
197
 out:
198
    return (type == pci_bar_mem64) ? 1 : 0;
198
    return (type == pci_bar_mem64) ? 1 : 0;
199
 fail:
199
 fail:
200
    res->flags = 0;
200
    res->flags = 0;
201
    goto out;
201
    goto out;
202
}
202
}
203
 
203
 
204
static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
204
static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
205
{
205
{
206
    unsigned int pos, reg;
206
    unsigned int pos, reg;
207
 
207
 
208
    for (pos = 0; pos < howmany; pos++) {
208
    for (pos = 0; pos < howmany; pos++) {
209
        struct resource *res = &dev->resource[pos];
209
        struct resource *res = &dev->resource[pos];
210
        reg = PCI_BASE_ADDRESS_0 + (pos << 2);
210
        reg = PCI_BASE_ADDRESS_0 + (pos << 2);
211
        pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
211
        pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
212
    }
212
    }
213
 
213
 
214
    if (rom) {
214
    if (rom) {
215
        struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
215
        struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
216
        dev->rom_base_reg = rom;
216
        dev->rom_base_reg = rom;
217
        res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
217
        res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
218
                IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
218
                IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
219
                IORESOURCE_SIZEALIGN;
219
                IORESOURCE_SIZEALIGN;
220
        __pci_read_base(dev, pci_bar_mem32, res, rom);
220
        __pci_read_base(dev, pci_bar_mem32, res, rom);
221
    }
221
    }
222
}
222
}
223
 
223
 
224
#if 0
224
#if 0
225
 
225
 
226
void pci_read_bridge_bases(struct pci_bus *child)
226
void pci_read_bridge_bases(struct pci_bus *child)
227
{
227
{
228
    struct pci_dev *dev = child->self;
228
    struct pci_dev *dev = child->self;
229
    struct resource *res;
229
    struct resource *res;
230
    int i;
230
    int i;
231
 
231
 
232
    if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
232
    if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
233
        return;
233
        return;
234
 
234
 
235
    dbgprintf("PCI bridge to [bus %02x-%02x]%s\n",
235
    dbgprintf("PCI bridge to [bus %02x-%02x]%s\n",
236
         child->secondary, child->subordinate,
236
         child->secondary, child->subordinate,
237
         dev->transparent ? " (subtractive decode)" : "");
237
         dev->transparent ? " (subtractive decode)" : "");
238
 
238
 
239
    pci_bus_remove_resources(child);
239
    pci_bus_remove_resources(child);
240
    for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
240
    for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
241
        child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
241
        child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
242
 
242
 
243
    pci_read_bridge_io(child);
243
    pci_read_bridge_io(child);
244
    pci_read_bridge_mmio(child);
244
    pci_read_bridge_mmio(child);
245
    pci_read_bridge_mmio_pref(child);
245
    pci_read_bridge_mmio_pref(child);
246
 
246
 
247
    if (dev->transparent) {
247
    if (dev->transparent) {
248
        pci_bus_for_each_resource(child->parent, res, i) {
248
        pci_bus_for_each_resource(child->parent, res, i) {
249
            if (res) {
249
            if (res) {
250
                pci_bus_add_resource(child, res,
250
                pci_bus_add_resource(child, res,
251
                             PCI_SUBTRACTIVE_DECODE);
251
                             PCI_SUBTRACTIVE_DECODE);
252
                dbgprintf("  bridge window %pR (subtractive decode)\n", res);
252
                dbgprintf("  bridge window %pR (subtractive decode)\n", res);
253
            }
253
            }
254
        }
254
        }
255
    }
255
    }
256
}
256
}
257
 
257
 
258
#endif
258
#endif
259
 
259
 
260
static struct pci_bus * pci_alloc_bus(void)
260
static struct pci_bus * pci_alloc_bus(void)
261
{
261
{
262
    struct pci_bus *b;
262
    struct pci_bus *b;
263
 
263
 
264
    b = kzalloc(sizeof(*b), GFP_KERNEL);
264
    b = kzalloc(sizeof(*b), GFP_KERNEL);
265
    if (b) {
265
    if (b) {
266
        INIT_LIST_HEAD(&b->node);
266
        INIT_LIST_HEAD(&b->node);
267
        INIT_LIST_HEAD(&b->children);
267
        INIT_LIST_HEAD(&b->children);
268
        INIT_LIST_HEAD(&b->devices);
268
        INIT_LIST_HEAD(&b->devices);
269
        INIT_LIST_HEAD(&b->slots);
269
        INIT_LIST_HEAD(&b->slots);
270
        INIT_LIST_HEAD(&b->resources);
270
        INIT_LIST_HEAD(&b->resources);
271
//        b->max_bus_speed = PCI_SPEED_UNKNOWN;
271
//        b->max_bus_speed = PCI_SPEED_UNKNOWN;
272
//        b->cur_bus_speed = PCI_SPEED_UNKNOWN;
272
//        b->cur_bus_speed = PCI_SPEED_UNKNOWN;
273
    }
273
    }
274
    return b;
274
    return b;
275
}
275
}
276
 
276
 
277
 
277
 
278
#if 0
278
#if 0
279
 
279
 
280
static unsigned char pcix_bus_speed[] = {
280
static unsigned char pcix_bus_speed[] = {
281
    PCI_SPEED_UNKNOWN,      /* 0 */
281
    PCI_SPEED_UNKNOWN,      /* 0 */
282
    PCI_SPEED_66MHz_PCIX,       /* 1 */
282
    PCI_SPEED_66MHz_PCIX,       /* 1 */
283
    PCI_SPEED_100MHz_PCIX,      /* 2 */
283
    PCI_SPEED_100MHz_PCIX,      /* 2 */
284
    PCI_SPEED_133MHz_PCIX,      /* 3 */
284
    PCI_SPEED_133MHz_PCIX,      /* 3 */
285
    PCI_SPEED_UNKNOWN,      /* 4 */
285
    PCI_SPEED_UNKNOWN,      /* 4 */
286
    PCI_SPEED_66MHz_PCIX_ECC,   /* 5 */
286
    PCI_SPEED_66MHz_PCIX_ECC,   /* 5 */
287
    PCI_SPEED_100MHz_PCIX_ECC,  /* 6 */
287
    PCI_SPEED_100MHz_PCIX_ECC,  /* 6 */
288
    PCI_SPEED_133MHz_PCIX_ECC,  /* 7 */
288
    PCI_SPEED_133MHz_PCIX_ECC,  /* 7 */
289
    PCI_SPEED_UNKNOWN,      /* 8 */
289
    PCI_SPEED_UNKNOWN,      /* 8 */
290
    PCI_SPEED_66MHz_PCIX_266,   /* 9 */
290
    PCI_SPEED_66MHz_PCIX_266,   /* 9 */
291
    PCI_SPEED_100MHz_PCIX_266,  /* A */
291
    PCI_SPEED_100MHz_PCIX_266,  /* A */
292
    PCI_SPEED_133MHz_PCIX_266,  /* B */
292
    PCI_SPEED_133MHz_PCIX_266,  /* B */
293
    PCI_SPEED_UNKNOWN,      /* C */
293
    PCI_SPEED_UNKNOWN,      /* C */
294
    PCI_SPEED_66MHz_PCIX_533,   /* D */
294
    PCI_SPEED_66MHz_PCIX_533,   /* D */
295
    PCI_SPEED_100MHz_PCIX_533,  /* E */
295
    PCI_SPEED_100MHz_PCIX_533,  /* E */
296
    PCI_SPEED_133MHz_PCIX_533   /* F */
296
    PCI_SPEED_133MHz_PCIX_533   /* F */
297
};
297
};
298
 
298
 
299
static unsigned char pcie_link_speed[] = {
299
static unsigned char pcie_link_speed[] = {
300
    PCI_SPEED_UNKNOWN,      /* 0 */
300
    PCI_SPEED_UNKNOWN,      /* 0 */
301
    PCIE_SPEED_2_5GT,       /* 1 */
301
    PCIE_SPEED_2_5GT,       /* 1 */
302
    PCIE_SPEED_5_0GT,       /* 2 */
302
    PCIE_SPEED_5_0GT,       /* 2 */
303
    PCIE_SPEED_8_0GT,       /* 3 */
303
    PCIE_SPEED_8_0GT,       /* 3 */
304
    PCI_SPEED_UNKNOWN,      /* 4 */
304
    PCI_SPEED_UNKNOWN,      /* 4 */
305
    PCI_SPEED_UNKNOWN,      /* 5 */
305
    PCI_SPEED_UNKNOWN,      /* 5 */
306
    PCI_SPEED_UNKNOWN,      /* 6 */
306
    PCI_SPEED_UNKNOWN,      /* 6 */
307
    PCI_SPEED_UNKNOWN,      /* 7 */
307
    PCI_SPEED_UNKNOWN,      /* 7 */
308
    PCI_SPEED_UNKNOWN,      /* 8 */
308
    PCI_SPEED_UNKNOWN,      /* 8 */
309
    PCI_SPEED_UNKNOWN,      /* 9 */
309
    PCI_SPEED_UNKNOWN,      /* 9 */
310
    PCI_SPEED_UNKNOWN,      /* A */
310
    PCI_SPEED_UNKNOWN,      /* A */
311
    PCI_SPEED_UNKNOWN,      /* B */
311
    PCI_SPEED_UNKNOWN,      /* B */
312
    PCI_SPEED_UNKNOWN,      /* C */
312
    PCI_SPEED_UNKNOWN,      /* C */
313
    PCI_SPEED_UNKNOWN,      /* D */
313
    PCI_SPEED_UNKNOWN,      /* D */
314
    PCI_SPEED_UNKNOWN,      /* E */
314
    PCI_SPEED_UNKNOWN,      /* E */
315
    PCI_SPEED_UNKNOWN       /* F */
315
    PCI_SPEED_UNKNOWN       /* F */
316
};
316
};
317
 
317
 
318
 
318
 
319
static void pci_set_bus_speed(struct pci_bus *bus)
319
static void pci_set_bus_speed(struct pci_bus *bus)
320
{
320
{
321
    struct pci_dev *bridge = bus->self;
321
    struct pci_dev *bridge = bus->self;
322
    int pos;
322
    int pos;
323
 
323
 
324
    pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
324
    pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
325
    if (!pos)
325
    if (!pos)
326
        pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
326
        pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
327
    if (pos) {
327
    if (pos) {
328
        u32 agpstat, agpcmd;
328
        u32 agpstat, agpcmd;
329
 
329
 
330
        pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
330
        pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
331
        bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
331
        bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
332
 
332
 
333
        pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
333
        pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
334
        bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
334
        bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
335
    }
335
    }
336
 
336
 
337
    pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
337
    pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
338
    if (pos) {
338
    if (pos) {
339
        u16 status;
339
        u16 status;
340
        enum pci_bus_speed max;
340
        enum pci_bus_speed max;
341
        pci_read_config_word(bridge, pos + 2, &status);
341
        pci_read_config_word(bridge, pos + 2, &status);
342
 
342
 
343
        if (status & 0x8000) {
343
        if (status & 0x8000) {
344
            max = PCI_SPEED_133MHz_PCIX_533;
344
            max = PCI_SPEED_133MHz_PCIX_533;
345
        } else if (status & 0x4000) {
345
        } else if (status & 0x4000) {
346
            max = PCI_SPEED_133MHz_PCIX_266;
346
            max = PCI_SPEED_133MHz_PCIX_266;
347
        } else if (status & 0x0002) {
347
        } else if (status & 0x0002) {
348
            if (((status >> 12) & 0x3) == 2) {
348
            if (((status >> 12) & 0x3) == 2) {
349
                max = PCI_SPEED_133MHz_PCIX_ECC;
349
                max = PCI_SPEED_133MHz_PCIX_ECC;
350
            } else {
350
            } else {
351
                max = PCI_SPEED_133MHz_PCIX;
351
                max = PCI_SPEED_133MHz_PCIX;
352
            }
352
            }
353
        } else {
353
        } else {
354
            max = PCI_SPEED_66MHz_PCIX;
354
            max = PCI_SPEED_66MHz_PCIX;
355
        }
355
        }
356
 
356
 
357
        bus->max_bus_speed = max;
357
        bus->max_bus_speed = max;
358
        bus->cur_bus_speed = pcix_bus_speed[(status >> 6) & 0xf];
358
        bus->cur_bus_speed = pcix_bus_speed[(status >> 6) & 0xf];
359
 
359
 
360
        return;
360
        return;
361
    }
361
    }
362
 
362
 
363
    pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
363
    pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
364
    if (pos) {
364
    if (pos) {
365
        u32 linkcap;
365
        u32 linkcap;
366
        u16 linksta;
366
        u16 linksta;
367
 
367
 
368
        pci_read_config_dword(bridge, pos + PCI_EXP_LNKCAP, &linkcap);
368
        pci_read_config_dword(bridge, pos + PCI_EXP_LNKCAP, &linkcap);
369
        bus->max_bus_speed = pcie_link_speed[linkcap & 0xf];
369
        bus->max_bus_speed = pcie_link_speed[linkcap & 0xf];
370
 
370
 
371
        pci_read_config_word(bridge, pos + PCI_EXP_LNKSTA, &linksta);
371
        pci_read_config_word(bridge, pos + PCI_EXP_LNKSTA, &linksta);
372
        pcie_update_link_speed(bus, linksta);
372
        pcie_update_link_speed(bus, linksta);
373
    }
373
    }
374
}
374
}
375
 
375
 
376
#endif
376
#endif
377
 
377
 
378
 
378
 
379
static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
379
static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
380
                       struct pci_dev *bridge, int busnr)
380
                       struct pci_dev *bridge, int busnr)
381
{
381
{
382
    struct pci_bus *child;
382
    struct pci_bus *child;
383
    int i;
383
    int i;
384
 
384
 
385
    /*
385
    /*
386
     * Allocate a new bus, and inherit stuff from the parent..
386
     * Allocate a new bus, and inherit stuff from the parent..
387
     */
387
     */
388
    child = pci_alloc_bus();
388
    child = pci_alloc_bus();
389
    if (!child)
389
    if (!child)
390
        return NULL;
390
        return NULL;
391
 
391
 
392
    child->parent = parent;
392
    child->parent = parent;
393
    child->ops = parent->ops;
393
    child->ops = parent->ops;
394
    child->sysdata = parent->sysdata;
394
    child->sysdata = parent->sysdata;
395
    child->bus_flags = parent->bus_flags;
395
    child->bus_flags = parent->bus_flags;
396
 
396
 
397
    /* initialize some portions of the bus device, but don't register it
397
    /* initialize some portions of the bus device, but don't register it
398
     * now as the parent is not properly set up yet.  This device will get
398
     * now as the parent is not properly set up yet.  This device will get
399
     * registered later in pci_bus_add_devices()
399
     * registered later in pci_bus_add_devices()
400
     */
400
     */
401
//    child->dev.class = &pcibus_class;
401
//    child->dev.class = &pcibus_class;
402
//    dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
402
//    dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
403
 
403
 
404
    /*
404
    /*
405
     * Set up the primary, secondary and subordinate
405
     * Set up the primary, secondary and subordinate
406
     * bus numbers.
406
     * bus numbers.
407
     */
407
     */
408
    child->number = child->secondary = busnr;
408
    child->number = child->secondary = busnr;
409
    child->primary = parent->secondary;
409
    child->primary = parent->secondary;
410
    child->subordinate = 0xff;
410
    child->subordinate = 0xff;
411
 
411
 
412
    if (!bridge)
412
    if (!bridge)
413
        return child;
413
        return child;
414
 
414
 
415
    child->self = bridge;
415
    child->self = bridge;
416
//    child->bridge = get_device(&bridge->dev);
416
//    child->bridge = get_device(&bridge->dev);
417
 
417
 
418
//    pci_set_bus_speed(child);
418
//    pci_set_bus_speed(child);
419
 
419
 
420
    /* Set up default resource pointers and names.. */
420
    /* Set up default resource pointers and names.. */
421
    for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
421
    for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
422
        child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
422
        child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
423
        child->resource[i]->name = child->name;
423
        child->resource[i]->name = child->name;
424
    }
424
    }
425
    bridge->subordinate = child;
425
    bridge->subordinate = child;
426
 
426
 
427
    return child;
427
    return child;
428
}
428
}
429
 
429
 
430
struct pci_bus* pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
430
struct pci_bus* pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
431
{
431
{
432
    struct pci_bus *child;
432
    struct pci_bus *child;
433
 
433
 
434
    child = pci_alloc_child_bus(parent, dev, busnr);
434
    child = pci_alloc_child_bus(parent, dev, busnr);
435
    if (child) {
435
    if (child) {
436
//        down_write(&pci_bus_sem);
436
//        down_write(&pci_bus_sem);
437
        list_add_tail(&child->node, &parent->children);
437
        list_add_tail(&child->node, &parent->children);
438
//        up_write(&pci_bus_sem);
438
//        up_write(&pci_bus_sem);
439
    }
439
    }
440
    return child;
440
    return child;
441
}
441
}
442
 
442
 
443
 
443
 
444
static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
444
static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
445
{
445
{
446
    struct pci_bus *parent = child->parent;
446
    struct pci_bus *parent = child->parent;
447
 
447
 
448
    /* Attempts to fix that up are really dangerous unless
448
    /* Attempts to fix that up are really dangerous unless
449
       we're going to re-assign all bus numbers. */
449
       we're going to re-assign all bus numbers. */
450
    if (!pcibios_assign_all_busses())
450
    if (!pcibios_assign_all_busses())
451
        return;
451
        return;
452
 
452
 
453
    while (parent->parent && parent->subordinate < max) {
453
    while (parent->parent && parent->subordinate < max) {
454
        parent->subordinate = max;
454
        parent->subordinate = max;
455
        pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
455
        pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
456
        parent = parent->parent;
456
        parent = parent->parent;
457
    }
457
    }
458
}
458
}
459
 
459
 
460
 
460
 
461
/*
461
/*
462
 * If it's a bridge, configure it and scan the bus behind it.
462
 * If it's a bridge, configure it and scan the bus behind it.
463
 * For CardBus bridges, we don't scan behind as the devices will
463
 * For CardBus bridges, we don't scan behind as the devices will
464
 * be handled by the bridge driver itself.
464
 * be handled by the bridge driver itself.
465
 *
465
 *
466
 * We need to process bridges in two passes -- first we scan those
466
 * We need to process bridges in two passes -- first we scan those
467
 * already configured by the BIOS and after we are done with all of
467
 * already configured by the BIOS and after we are done with all of
468
 * them, we proceed to assigning numbers to the remaining buses in
468
 * them, we proceed to assigning numbers to the remaining buses in
469
 * order to avoid overlaps between old and new bus numbers.
469
 * order to avoid overlaps between old and new bus numbers.
470
 */
470
 */
471
int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
471
int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
472
{
472
{
473
    struct pci_bus *child;
473
    struct pci_bus *child;
474
    int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
474
    int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
475
    u32 buses, i, j = 0;
475
    u32 buses, i, j = 0;
476
    u16 bctl;
476
    u16 bctl;
477
    u8 primary, secondary, subordinate;
477
    u8 primary, secondary, subordinate;
478
    int broken = 0;
478
    int broken = 0;
479
 
479
 
480
    pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
480
    pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
481
    primary = buses & 0xFF;
481
    primary = buses & 0xFF;
482
    secondary = (buses >> 8) & 0xFF;
482
    secondary = (buses >> 8) & 0xFF;
483
    subordinate = (buses >> 16) & 0xFF;
483
    subordinate = (buses >> 16) & 0xFF;
484
 
484
 
485
    dbgprintf("scanning [bus %02x-%02x] behind bridge, pass %d\n",
485
    dbgprintf("scanning [bus %02x-%02x] behind bridge, pass %d\n",
486
        secondary, subordinate, pass);
486
        secondary, subordinate, pass);
487
 
487
 
488
    /* Check if setup is sensible at all */
488
    /* Check if setup is sensible at all */
489
    if (!pass &&
489
    if (!pass &&
490
        (primary != bus->number || secondary <= bus->number)) {
490
        (primary != bus->number || secondary <= bus->number)) {
491
        dbgprintf("bus configuration invalid, reconfiguring\n");
491
        dbgprintf("bus configuration invalid, reconfiguring\n");
492
        broken = 1;
492
        broken = 1;
493
    }
493
    }
494
 
494
 
495
    /* Disable MasterAbortMode during probing to avoid reporting
495
    /* Disable MasterAbortMode during probing to avoid reporting
496
       of bus errors (in some architectures) */
496
       of bus errors (in some architectures) */
497
    pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
497
    pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
498
    pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
498
    pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
499
                  bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
499
                  bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
500
 
500
 
501
    if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
501
    if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
502
        !is_cardbus && !broken) {
502
        !is_cardbus && !broken) {
503
        unsigned int cmax;
503
        unsigned int cmax;
504
        /*
504
        /*
505
         * Bus already configured by firmware, process it in the first
505
         * Bus already configured by firmware, process it in the first
506
         * pass and just note the configuration.
506
         * pass and just note the configuration.
507
         */
507
         */
508
        if (pass)
508
        if (pass)
509
            goto out;
509
            goto out;
510
 
510
 
511
        /*
511
        /*
512
         * If we already got to this bus through a different bridge,
512
         * If we already got to this bus through a different bridge,
513
         * don't re-add it. This can happen with the i450NX chipset.
513
         * don't re-add it. This can happen with the i450NX chipset.
514
         *
514
         *
515
         * However, we continue to descend down the hierarchy and
515
         * However, we continue to descend down the hierarchy and
516
         * scan remaining child buses.
516
         * scan remaining child buses.
517
         */
517
         */
518
        child = pci_find_bus(pci_domain_nr(bus), secondary);
518
        child = pci_find_bus(pci_domain_nr(bus), secondary);
519
        if (!child) {
519
        if (!child) {
520
            child = pci_add_new_bus(bus, dev, secondary);
520
            child = pci_add_new_bus(bus, dev, secondary);
521
            if (!child)
521
            if (!child)
522
                goto out;
522
                goto out;
523
            child->primary = primary;
523
            child->primary = primary;
524
            child->subordinate = subordinate;
524
            child->subordinate = subordinate;
525
            child->bridge_ctl = bctl;
525
            child->bridge_ctl = bctl;
526
        }
526
        }
527
 
527
 
528
        cmax = pci_scan_child_bus(child);
528
        cmax = pci_scan_child_bus(child);
529
        if (cmax > max)
529
        if (cmax > max)
530
            max = cmax;
530
            max = cmax;
531
        if (child->subordinate > max)
531
        if (child->subordinate > max)
532
            max = child->subordinate;
532
            max = child->subordinate;
533
    } else {
533
    } else {
534
        /*
534
        /*
535
         * We need to assign a number to this bus which we always
535
         * We need to assign a number to this bus which we always
536
         * do in the second pass.
536
         * do in the second pass.
537
         */
537
         */
538
        if (!pass) {
538
        if (!pass) {
539
            if (pcibios_assign_all_busses() || broken)
539
            if (pcibios_assign_all_busses() || broken)
540
                /* Temporarily disable forwarding of the
540
                /* Temporarily disable forwarding of the
541
                   configuration cycles on all bridges in
541
                   configuration cycles on all bridges in
542
                   this bus segment to avoid possible
542
                   this bus segment to avoid possible
543
                   conflicts in the second pass between two
543
                   conflicts in the second pass between two
544
                   bridges programmed with overlapping
544
                   bridges programmed with overlapping
545
                   bus ranges. */
545
                   bus ranges. */
546
                pci_write_config_dword(dev, PCI_PRIMARY_BUS,
546
                pci_write_config_dword(dev, PCI_PRIMARY_BUS,
547
                               buses & ~0xffffff);
547
                               buses & ~0xffffff);
548
            goto out;
548
            goto out;
549
        }
549
        }
550
 
550
 
551
        /* Clear errors */
551
        /* Clear errors */
552
        pci_write_config_word(dev, PCI_STATUS, 0xffff);
552
        pci_write_config_word(dev, PCI_STATUS, 0xffff);
553
 
553
 
554
        /* Prevent assigning a bus number that already exists.
554
        /* Prevent assigning a bus number that already exists.
555
         * This can happen when a bridge is hot-plugged */
555
         * This can happen when a bridge is hot-plugged */
556
        if (pci_find_bus(pci_domain_nr(bus), max+1))
556
        if (pci_find_bus(pci_domain_nr(bus), max+1))
557
            goto out;
557
            goto out;
558
        child = pci_add_new_bus(bus, dev, ++max);
558
        child = pci_add_new_bus(bus, dev, ++max);
559
        buses = (buses & 0xff000000)
559
        buses = (buses & 0xff000000)
560
              | ((unsigned int)(child->primary)     <<  0)
560
              | ((unsigned int)(child->primary)     <<  0)
561
              | ((unsigned int)(child->secondary)   <<  8)
561
              | ((unsigned int)(child->secondary)   <<  8)
562
              | ((unsigned int)(child->subordinate) << 16);
562
              | ((unsigned int)(child->subordinate) << 16);
563
 
563
 
564
        /*
564
        /*
565
         * yenta.c forces a secondary latency timer of 176.
565
         * yenta.c forces a secondary latency timer of 176.
566
         * Copy that behaviour here.
566
         * Copy that behaviour here.
567
         */
567
         */
568
        if (is_cardbus) {
568
        if (is_cardbus) {
569
            buses &= ~0xff000000;
569
            buses &= ~0xff000000;
570
            buses |= CARDBUS_LATENCY_TIMER << 24;
570
            buses |= CARDBUS_LATENCY_TIMER << 24;
571
        }
571
        }
572
 
572
 
573
        /*
573
        /*
574
         * We need to blast all three values with a single write.
574
         * We need to blast all three values with a single write.
575
         */
575
         */
576
        pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
576
        pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
577
 
577
 
578
        if (!is_cardbus) {
578
        if (!is_cardbus) {
579
            child->bridge_ctl = bctl;
579
            child->bridge_ctl = bctl;
580
            /*
580
            /*
581
             * Adjust subordinate busnr in parent buses.
581
             * Adjust subordinate busnr in parent buses.
582
             * We do this before scanning for children because
582
             * We do this before scanning for children because
583
             * some devices may not be detected if the bios
583
             * some devices may not be detected if the bios
584
             * was lazy.
584
             * was lazy.
585
             */
585
             */
586
            pci_fixup_parent_subordinate_busnr(child, max);
586
            pci_fixup_parent_subordinate_busnr(child, max);
587
            /* Now we can scan all subordinate buses... */
587
            /* Now we can scan all subordinate buses... */
588
            max = pci_scan_child_bus(child);
588
            max = pci_scan_child_bus(child);
589
            /*
589
            /*
590
             * now fix it up again since we have found
590
             * now fix it up again since we have found
591
             * the real value of max.
591
             * the real value of max.
592
             */
592
             */
593
            pci_fixup_parent_subordinate_busnr(child, max);
593
            pci_fixup_parent_subordinate_busnr(child, max);
594
        } else {
594
        } else {
595
            /*
595
            /*
596
             * For CardBus bridges, we leave 4 bus numbers
596
             * For CardBus bridges, we leave 4 bus numbers
597
             * as cards with a PCI-to-PCI bridge can be
597
             * as cards with a PCI-to-PCI bridge can be
598
             * inserted later.
598
             * inserted later.
599
             */
599
             */
600
            for (i=0; i
600
            for (i=0; i
601
                struct pci_bus *parent = bus;
601
                struct pci_bus *parent = bus;
602
                if (pci_find_bus(pci_domain_nr(bus),
602
                if (pci_find_bus(pci_domain_nr(bus),
603
                            max+i+1))
603
                            max+i+1))
604
                    break;
604
                    break;
605
                while (parent->parent) {
605
                while (parent->parent) {
606
                    if ((!pcibios_assign_all_busses()) &&
606
                    if ((!pcibios_assign_all_busses()) &&
607
                        (parent->subordinate > max) &&
607
                        (parent->subordinate > max) &&
608
                        (parent->subordinate <= max+i)) {
608
                        (parent->subordinate <= max+i)) {
609
                        j = 1;
609
                        j = 1;
610
                    }
610
                    }
611
                    parent = parent->parent;
611
                    parent = parent->parent;
612
                }
612
                }
613
                if (j) {
613
                if (j) {
614
                    /*
614
                    /*
615
                     * Often, there are two cardbus bridges
615
                     * Often, there are two cardbus bridges
616
                     * -- try to leave one valid bus number
616
                     * -- try to leave one valid bus number
617
                     * for each one.
617
                     * for each one.
618
                     */
618
                     */
619
                    i /= 2;
619
                    i /= 2;
620
                    break;
620
                    break;
621
                }
621
                }
622
            }
622
            }
623
            max += i;
623
            max += i;
624
            pci_fixup_parent_subordinate_busnr(child, max);
624
            pci_fixup_parent_subordinate_busnr(child, max);
625
        }
625
        }
626
        /*
626
        /*
627
         * Set the subordinate bus number to its real value.
627
         * Set the subordinate bus number to its real value.
628
         */
628
         */
629
        child->subordinate = max;
629
        child->subordinate = max;
630
        pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
630
        pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
631
    }
631
    }
632
 
632
 
633
    vsprintf(child->name,
633
    vsprintf(child->name,
634
        (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
634
        (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
635
        pci_domain_nr(bus), child->number);
635
        pci_domain_nr(bus), child->number);
636
 
636
 
637
    /* Has only triggered on CardBus, fixup is in yenta_socket */
637
    /* Has only triggered on CardBus, fixup is in yenta_socket */
638
    while (bus->parent) {
638
    while (bus->parent) {
639
        if ((child->subordinate > bus->subordinate) ||
639
        if ((child->subordinate > bus->subordinate) ||
640
            (child->number > bus->subordinate) ||
640
            (child->number > bus->subordinate) ||
641
            (child->number < bus->number) ||
641
            (child->number < bus->number) ||
642
            (child->subordinate < bus->number)) {
642
            (child->subordinate < bus->number)) {
643
            dbgprintf("[bus %02x-%02x] %s "
643
            dbgprintf("[bus %02x-%02x] %s "
644
                "hidden behind%s bridge %s [bus %02x-%02x]\n",
644
                "hidden behind%s bridge %s [bus %02x-%02x]\n",
645
                child->number, child->subordinate,
645
                child->number, child->subordinate,
646
                (bus->number > child->subordinate &&
646
                (bus->number > child->subordinate &&
647
                 bus->subordinate < child->number) ?
647
                 bus->subordinate < child->number) ?
648
                    "wholly" : "partially",
648
                    "wholly" : "partially",
649
                bus->self->transparent ? " transparent" : "",
649
                bus->self->transparent ? " transparent" : "",
650
                "FIX BRIDGE NAME",
650
                "FIX BRIDGE NAME",
651
                bus->number, bus->subordinate);
651
                bus->number, bus->subordinate);
652
        }
652
        }
653
        bus = bus->parent;
653
        bus = bus->parent;
654
    }
654
    }
655
 
655
 
656
out:
656
out:
657
    pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
657
    pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
658
 
658
 
659
    return max;
659
    return max;
660
}
660
}
661
 
661
 
662
void set_pcie_port_type(struct pci_dev *pdev)
662
void set_pcie_port_type(struct pci_dev *pdev)
663
{
663
{
664
    int pos;
664
    int pos;
665
    u16 reg16;
665
    u16 reg16;
666
 
666
 
667
    pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
667
    pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
668
    if (!pos)
668
    if (!pos)
669
        return;
669
        return;
670
    pdev->is_pcie = 1;
670
    pdev->is_pcie = 1;
671
    pdev->pcie_cap = pos;
671
    pdev->pcie_cap = pos;
672
    pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
672
    pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
673
    pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
673
    pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
674
}
674
}
675
 
675
 
676
void set_pcie_hotplug_bridge(struct pci_dev *pdev)
676
void set_pcie_hotplug_bridge(struct pci_dev *pdev)
677
{
677
{
678
    int pos;
678
    int pos;
679
    u16 reg16;
679
    u16 reg16;
680
    u32 reg32;
680
    u32 reg32;
681
 
681
 
682
    pos = pci_pcie_cap(pdev);
682
    pos = pci_pcie_cap(pdev);
683
    if (!pos)
683
    if (!pos)
684
        return;
684
        return;
685
    pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
685
    pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
686
    if (!(reg16 & PCI_EXP_FLAGS_SLOT))
686
    if (!(reg16 & PCI_EXP_FLAGS_SLOT))
687
        return;
687
        return;
688
    pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, ®32);
688
    pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, ®32);
689
    if (reg32 & PCI_EXP_SLTCAP_HPC)
689
    if (reg32 & PCI_EXP_SLTCAP_HPC)
690
        pdev->is_hotplug_bridge = 1;
690
        pdev->is_hotplug_bridge = 1;
691
}
691
}
692
 
692
 
693
/*
693
/*
694
 * Read interrupt line and base address registers.
694
 * Read interrupt line and base address registers.
695
 * The architecture-dependent code can tweak these, of course.
695
 * The architecture-dependent code can tweak these, of course.
696
 */
696
 */
697
static void pci_read_irq(struct pci_dev *dev)
697
static void pci_read_irq(struct pci_dev *dev)
698
{
698
{
699
    unsigned char irq;
699
    unsigned char irq;
700
 
700
 
701
    pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
701
    pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
702
    dev->pin = irq;
702
    dev->pin = irq;
703
    if (irq)
703
    if (irq)
704
        pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
704
        pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
705
    dev->irq = irq;
705
    dev->irq = irq;
706
}
706
}
707
 
707
 
708
 
708
 
709
/**
709
/**
710
 * pci_setup_device - fill in class and map information of a device
710
 * pci_setup_device - fill in class and map information of a device
711
 * @dev: the device structure to fill
711
 * @dev: the device structure to fill
712
 *
712
 *
713
 * Initialize the device structure with information about the device's
713
 * Initialize the device structure with information about the device's
714
 * vendor,class,memory and IO-space addresses,IRQ lines etc.
714
 * vendor,class,memory and IO-space addresses,IRQ lines etc.
715
 * Called at initialisation of the PCI subsystem and by CardBus services.
715
 * Called at initialisation of the PCI subsystem and by CardBus services.
716
 * Returns 0 on success and negative if unknown type of device (not normal,
716
 * Returns 0 on success and negative if unknown type of device (not normal,
717
 * bridge or CardBus).
717
 * bridge or CardBus).
718
 */
718
 */
719
int pci_setup_device(struct pci_dev *dev)
719
int pci_setup_device(struct pci_dev *dev)
720
{
720
{
721
    u32 class;
721
    u32 class;
722
    u8 hdr_type;
722
    u8 hdr_type;
723
    struct pci_slot *slot;
723
    struct pci_slot *slot;
724
    int pos = 0;
724
    int pos = 0;
725
 
725
 
726
    if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
726
    if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
727
        return -EIO;
727
        return -EIO;
728
 
728
 
729
    dev->sysdata = dev->bus->sysdata;
729
    dev->sysdata = dev->bus->sysdata;
730
//    dev->dev.parent = dev->bus->bridge;
730
//    dev->dev.parent = dev->bus->bridge;
731
//    dev->dev.bus = &pci_bus_type;
731
//    dev->dev.bus = &pci_bus_type;
732
    dev->hdr_type = hdr_type & 0x7f;
732
    dev->hdr_type = hdr_type & 0x7f;
733
    dev->multifunction = !!(hdr_type & 0x80);
733
    dev->multifunction = !!(hdr_type & 0x80);
734
    dev->error_state = pci_channel_io_normal;
734
    dev->error_state = pci_channel_io_normal;
735
    set_pcie_port_type(dev);
735
    set_pcie_port_type(dev);
736
 
736
 
737
    list_for_each_entry(slot, &dev->bus->slots, list)
737
    list_for_each_entry(slot, &dev->bus->slots, list)
738
        if (PCI_SLOT(dev->devfn) == slot->number)
738
        if (PCI_SLOT(dev->devfn) == slot->number)
739
            dev->slot = slot;
739
            dev->slot = slot;
740
 
740
 
741
    /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
741
    /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
742
       set this higher, assuming the system even supports it.  */
742
       set this higher, assuming the system even supports it.  */
743
    dev->dma_mask = 0xffffffff;
743
    dev->dma_mask = 0xffffffff;
744
 
744
 
745
//    dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
745
//    dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
746
//             dev->bus->number, PCI_SLOT(dev->devfn),
746
//             dev->bus->number, PCI_SLOT(dev->devfn),
747
//             PCI_FUNC(dev->devfn));
747
//             PCI_FUNC(dev->devfn));
748
 
748
 
749
    pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
749
    pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
750
    dev->revision = class & 0xff;
750
    dev->revision = class & 0xff;
751
    class >>= 8;                    /* upper 3 bytes */
751
    class >>= 8;                    /* upper 3 bytes */
752
    dev->class = class;
752
    dev->class = class;
753
    class >>= 8;
753
    class >>= 8;
754
 
754
 
755
    dbgprintf("found [%04x:%04x] class %06x header type %02x\n",
755
    dbgprintf("found [%04x:%04x] class %06x header type %02x\n",
756
         dev->vendor, dev->device, class, dev->hdr_type);
756
         dev->vendor, dev->device, class, dev->hdr_type);
757
 
757
 
758
    /* need to have dev->class ready */
758
    /* need to have dev->class ready */
759
    dev->cfg_size = pci_cfg_space_size(dev);
759
    dev->cfg_size = pci_cfg_space_size(dev);
760
 
760
 
761
    /* "Unknown power state" */
761
    /* "Unknown power state" */
762
    dev->current_state = PCI_UNKNOWN;
762
    dev->current_state = PCI_UNKNOWN;
763
 
763
 
764
    /* Early fixups, before probing the BARs */
764
    /* Early fixups, before probing the BARs */
765
//    pci_fixup_device(pci_fixup_early, dev);
765
//    pci_fixup_device(pci_fixup_early, dev);
766
    /* device class may be changed after fixup */
766
    /* device class may be changed after fixup */
767
    class = dev->class >> 8;
767
    class = dev->class >> 8;
768
 
768
 
769
    switch (dev->hdr_type) {            /* header type */
769
    switch (dev->hdr_type) {            /* header type */
770
    case PCI_HEADER_TYPE_NORMAL:            /* standard header */
770
    case PCI_HEADER_TYPE_NORMAL:            /* standard header */
771
        if (class == PCI_CLASS_BRIDGE_PCI)
771
        if (class == PCI_CLASS_BRIDGE_PCI)
772
            goto bad;
772
            goto bad;
773
        pci_read_irq(dev);
773
        pci_read_irq(dev);
774
        pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
774
        pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
775
        pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
775
        pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
776
        pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
776
        pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
777
 
777
 
778
        /*
778
        /*
779
         *  Do the ugly legacy mode stuff here rather than broken chip
779
         *  Do the ugly legacy mode stuff here rather than broken chip
780
         *  quirk code. Legacy mode ATA controllers have fixed
780
         *  quirk code. Legacy mode ATA controllers have fixed
781
         *  addresses. These are not always echoed in BAR0-3, and
781
         *  addresses. These are not always echoed in BAR0-3, and
782
         *  BAR0-3 in a few cases contain junk!
782
         *  BAR0-3 in a few cases contain junk!
783
         */
783
         */
784
        if (class == PCI_CLASS_STORAGE_IDE) {
784
        if (class == PCI_CLASS_STORAGE_IDE) {
785
            u8 progif;
785
            u8 progif;
786
            pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
786
            pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
787
            if ((progif & 1) == 0) {
787
            if ((progif & 1) == 0) {
788
                dev->resource[0].start = 0x1F0;
788
                dev->resource[0].start = 0x1F0;
789
                dev->resource[0].end = 0x1F7;
789
                dev->resource[0].end = 0x1F7;
790
                dev->resource[0].flags = LEGACY_IO_RESOURCE;
790
                dev->resource[0].flags = LEGACY_IO_RESOURCE;
791
                dev->resource[1].start = 0x3F6;
791
                dev->resource[1].start = 0x3F6;
792
                dev->resource[1].end = 0x3F6;
792
                dev->resource[1].end = 0x3F6;
793
                dev->resource[1].flags = LEGACY_IO_RESOURCE;
793
                dev->resource[1].flags = LEGACY_IO_RESOURCE;
794
            }
794
            }
795
            if ((progif & 4) == 0) {
795
            if ((progif & 4) == 0) {
796
                dev->resource[2].start = 0x170;
796
                dev->resource[2].start = 0x170;
797
                dev->resource[2].end = 0x177;
797
                dev->resource[2].end = 0x177;
798
                dev->resource[2].flags = LEGACY_IO_RESOURCE;
798
                dev->resource[2].flags = LEGACY_IO_RESOURCE;
799
                dev->resource[3].start = 0x376;
799
                dev->resource[3].start = 0x376;
800
                dev->resource[3].end = 0x376;
800
                dev->resource[3].end = 0x376;
801
                dev->resource[3].flags = LEGACY_IO_RESOURCE;
801
                dev->resource[3].flags = LEGACY_IO_RESOURCE;
802
            }
802
            }
803
        }
803
        }
804
        break;
804
        break;
805
 
805
 
806
    case PCI_HEADER_TYPE_BRIDGE:            /* bridge header */
806
    case PCI_HEADER_TYPE_BRIDGE:            /* bridge header */
807
        if (class != PCI_CLASS_BRIDGE_PCI)
807
        if (class != PCI_CLASS_BRIDGE_PCI)
808
            goto bad;
808
            goto bad;
809
        /* The PCI-to-PCI bridge spec requires that subtractive
809
        /* The PCI-to-PCI bridge spec requires that subtractive
810
           decoding (i.e. transparent) bridge must have programming
810
           decoding (i.e. transparent) bridge must have programming
811
           interface code of 0x01. */
811
           interface code of 0x01. */
812
        pci_read_irq(dev);
812
        pci_read_irq(dev);
813
        dev->transparent = ((dev->class & 0xff) == 1);
813
        dev->transparent = ((dev->class & 0xff) == 1);
814
        pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
814
        pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
815
        set_pcie_hotplug_bridge(dev);
815
        set_pcie_hotplug_bridge(dev);
816
        pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
816
        pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
817
        if (pos) {
817
        if (pos) {
818
            pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
818
            pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
819
            pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
819
            pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
820
        }
820
        }
821
        break;
821
        break;
822
 
822
 
823
    case PCI_HEADER_TYPE_CARDBUS:           /* CardBus bridge header */
823
    case PCI_HEADER_TYPE_CARDBUS:           /* CardBus bridge header */
824
        if (class != PCI_CLASS_BRIDGE_CARDBUS)
824
        if (class != PCI_CLASS_BRIDGE_CARDBUS)
825
            goto bad;
825
            goto bad;
826
        pci_read_irq(dev);
826
        pci_read_irq(dev);
827
        pci_read_bases(dev, 1, 0);
827
        pci_read_bases(dev, 1, 0);
828
        pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
828
        pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
829
        pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
829
        pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
830
        break;
830
        break;
831
 
831
 
832
    default:                    /* unknown header */
832
    default:                    /* unknown header */
833
        dbgprintf("unknown header type %02x, "
833
        dbgprintf("unknown header type %02x, "
834
            "ignoring device\n", dev->hdr_type);
834
            "ignoring device\n", dev->hdr_type);
835
        return -EIO;
835
        return -EIO;
836
 
836
 
837
    bad:
837
    bad:
838
        dbgprintf("ignoring class %02x (doesn't match header "
838
        dbgprintf("ignoring class %02x (doesn't match header "
839
            "type %02x)\n", class, dev->hdr_type);
839
            "type %02x)\n", class, dev->hdr_type);
840
        dev->class = PCI_CLASS_NOT_DEFINED;
840
        dev->class = PCI_CLASS_NOT_DEFINED;
841
    }
841
    }
842
 
842
 
843
    /* We found a fine healthy device, go go go... */
843
    /* We found a fine healthy device, go go go... */
844
    return 0;
844
    return 0;
845
}
845
}
846
 
846
 
847
 
847
 
848
 
848
 
849
struct pci_dev *alloc_pci_dev(void)
849
struct pci_dev *alloc_pci_dev(void)
850
{
850
{
851
    struct pci_dev *dev;
851
    struct pci_dev *dev;
852
 
852
 
853
    dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
853
    dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
854
    if (!dev)
854
    if (!dev)
855
        return NULL;
855
        return NULL;
856
 
856
 
857
    INIT_LIST_HEAD(&dev->bus_list);
857
    INIT_LIST_HEAD(&dev->bus_list);
858
 
858
 
859
    return dev;
859
    return dev;
860
}
860
}
861
 
861
 
862
/*
862
/*
863
 * Read the config data for a PCI device, sanity-check it
863
 * Read the config data for a PCI device, sanity-check it
864
 * and fill in the dev structure...
864
 * and fill in the dev structure...
865
 */
865
 */
866
static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
866
static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
867
{
867
{
868
    struct pci_dev *dev;
868
    struct pci_dev *dev;
869
    u32 l;
869
    u32 l;
870
    int timeout = 10;
870
    int timeout = 10;
871
 
871
 
872
    if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
872
    if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
873
        return NULL;
873
        return NULL;
874
 
874
 
875
    /* some broken boards return 0 or ~0 if a slot is empty: */
875
    /* some broken boards return 0 or ~0 if a slot is empty: */
876
    if (l == 0xffffffff || l == 0x00000000 ||
876
    if (l == 0xffffffff || l == 0x00000000 ||
877
        l == 0x0000ffff || l == 0xffff0000)
877
        l == 0x0000ffff || l == 0xffff0000)
878
        return NULL;
878
        return NULL;
879
 
879
 
880
    /* Configuration request Retry Status */
880
    /* Configuration request Retry Status */
881
    while (l == 0xffff0001) {
881
    while (l == 0xffff0001) {
882
        delay(timeout/10);
882
        delay(timeout/10);
883
        timeout *= 2;
883
        timeout *= 2;
884
        if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
884
        if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
885
            return NULL;
885
            return NULL;
886
        /* Card hasn't responded in 60 seconds?  Must be stuck. */
886
        /* Card hasn't responded in 60 seconds?  Must be stuck. */
887
        if (timeout > 60 * 1000) {
887
        if (timeout > 60 * 1000) {
888
            printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
888
            printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
889
                    "responding\n", pci_domain_nr(bus),
889
                    "responding\n", pci_domain_nr(bus),
890
                    bus->number, PCI_SLOT(devfn),
890
                    bus->number, PCI_SLOT(devfn),
891
                    PCI_FUNC(devfn));
891
                    PCI_FUNC(devfn));
892
            return NULL;
892
            return NULL;
893
        }
893
        }
894
    }
894
    }
895
 
895
 
896
    dev = alloc_pci_dev();
896
    dev = alloc_pci_dev();
897
    if (!dev)
897
    if (!dev)
898
        return NULL;
898
        return NULL;
899
 
899
 
900
    dev->bus = bus;
900
    dev->bus = bus;
901
    dev->devfn = devfn;
901
    dev->busnr = bus->number;
-
 
902
    dev->devfn = devfn;
902
    dev->vendor = l & 0xffff;
903
    dev->vendor = l & 0xffff;
903
    dev->device = (l >> 16) & 0xffff;
904
    dev->device = (l >> 16) & 0xffff;
904
 
905
 
905
    if (pci_setup_device(dev)) {
906
    if (pci_setup_device(dev)) {
906
        kfree(dev);
907
        kfree(dev);
907
        return NULL;
908
        return NULL;
908
    }
909
    }
909
 
910
 
910
    return dev;
911
    return dev;
911
}
912
}
912
 
913
 
913
void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
914
void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
914
{
915
{
915
//    device_initialize(&dev->dev);
916
//    device_initialize(&dev->dev);
916
//    dev->dev.release = pci_release_dev;
917
//    dev->dev.release = pci_release_dev;
917
//    pci_dev_get(dev);
918
//    pci_dev_get(dev);
918
 
919
 
919
//    dev->dev.dma_mask = &dev->dma_mask;
920
//    dev->dev.dma_mask = &dev->dma_mask;
920
//    dev->dev.dma_parms = &dev->dma_parms;
921
//    dev->dev.dma_parms = &dev->dma_parms;
921
//    dev->dev.coherent_dma_mask = 0xffffffffull;
922
//    dev->dev.coherent_dma_mask = 0xffffffffull;
922
 
923
 
923
//    pci_set_dma_max_seg_size(dev, 65536);
924
//    pci_set_dma_max_seg_size(dev, 65536);
924
//    pci_set_dma_seg_boundary(dev, 0xffffffff);
925
//    pci_set_dma_seg_boundary(dev, 0xffffffff);
925
 
926
 
926
    /* Fix up broken headers */
927
    /* Fix up broken headers */
927
//    pci_fixup_device(pci_fixup_header, dev);
928
//    pci_fixup_device(pci_fixup_header, dev);
928
 
929
 
929
    /* Clear the state_saved flag. */
930
    /* Clear the state_saved flag. */
930
    dev->state_saved = false;
931
    dev->state_saved = false;
931
 
932
 
932
    /* Initialize various capabilities */
933
    /* Initialize various capabilities */
933
//    pci_init_capabilities(dev);
934
//    pci_init_capabilities(dev);
934
 
935
 
935
    /*
936
    /*
936
     * Add the device to our list of discovered devices
937
     * Add the device to our list of discovered devices
937
     * and the bus list for fixup functions, etc.
938
     * and the bus list for fixup functions, etc.
938
     */
939
     */
939
//    down_write(&pci_bus_sem);
940
//    down_write(&pci_bus_sem);
940
    list_add_tail(&dev->bus_list, &bus->devices);
941
    list_add_tail(&dev->bus_list, &bus->devices);
941
//    up_write(&pci_bus_sem);
942
//    up_write(&pci_bus_sem);
942
}
943
}
943
 
944
 
944
struct pci_dev * pci_scan_single_device(struct pci_bus *bus, int devfn)
945
struct pci_dev * pci_scan_single_device(struct pci_bus *bus, int devfn)
945
{
946
{
946
    struct pci_dev *dev;
947
    struct pci_dev *dev;
947
 
948
 
948
    dev = pci_get_slot(bus, devfn);
949
    dev = pci_get_slot(bus, devfn);
949
    if (dev) {
950
    if (dev) {
950
//        pci_dev_put(dev);
951
//        pci_dev_put(dev);
951
        return dev;
952
        return dev;
952
    }
953
    }
953
 
954
 
954
    dev = pci_scan_device(bus, devfn);
955
    dev = pci_scan_device(bus, devfn);
955
    if (!dev)
956
    if (!dev)
956
        return NULL;
957
        return NULL;
957
 
958
 
958
    pci_device_add(dev, bus);
959
    pci_device_add(dev, bus);
959
 
960
 
960
    return dev;
961
    return dev;
961
}
962
}
962
 
963
 
963
static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn)
964
static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn)
964
{
965
{
965
    u16 cap;
966
    u16 cap;
966
    unsigned pos, next_fn;
967
    unsigned pos, next_fn;
967
 
968
 
968
    if (!dev)
969
    if (!dev)
969
        return 0;
970
        return 0;
970
 
971
 
971
    pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
972
    pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
972
    if (!pos)
973
    if (!pos)
973
        return 0;
974
        return 0;
974
    pci_read_config_word(dev, pos + 4, &cap);
975
    pci_read_config_word(dev, pos + 4, &cap);
975
    next_fn = cap >> 8;
976
    next_fn = cap >> 8;
976
    if (next_fn <= fn)
977
    if (next_fn <= fn)
977
        return 0;
978
        return 0;
978
    return next_fn;
979
    return next_fn;
979
}
980
}
980
 
981
 
981
static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn)
982
static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn)
982
{
983
{
983
    return (fn + 1) % 8;
984
    return (fn + 1) % 8;
984
}
985
}
985
 
986
 
986
static unsigned no_next_fn(struct pci_dev *dev, unsigned fn)
987
static unsigned no_next_fn(struct pci_dev *dev, unsigned fn)
987
{
988
{
988
    return 0;
989
    return 0;
989
}
990
}
990
 
991
 
991
static int only_one_child(struct pci_bus *bus)
992
static int only_one_child(struct pci_bus *bus)
992
{
993
{
993
    struct pci_dev *parent = bus->self;
994
    struct pci_dev *parent = bus->self;
994
    if (!parent || !pci_is_pcie(parent))
995
    if (!parent || !pci_is_pcie(parent))
995
        return 0;
996
        return 0;
996
    if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
997
    if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
997
        parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
998
        parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
998
        return 1;
999
        return 1;
999
    return 0;
1000
    return 0;
1000
}
1001
}
1001
 
1002
 
1002
/**
1003
/**
1003
 * pci_scan_slot - scan a PCI slot on a bus for devices.
1004
 * pci_scan_slot - scan a PCI slot on a bus for devices.
1004
 * @bus: PCI bus to scan
1005
 * @bus: PCI bus to scan
1005
 * @devfn: slot number to scan (must have zero function.)
1006
 * @devfn: slot number to scan (must have zero function.)
1006
 *
1007
 *
1007
 * Scan a PCI slot on the specified PCI bus for devices, adding
1008
 * Scan a PCI slot on the specified PCI bus for devices, adding
1008
 * discovered devices to the @bus->devices list.  New devices
1009
 * discovered devices to the @bus->devices list.  New devices
1009
 * will not have is_added set.
1010
 * will not have is_added set.
1010
 *
1011
 *
1011
 * Returns the number of new devices found.
1012
 * Returns the number of new devices found.
1012
 */
1013
 */
1013
int pci_scan_slot(struct pci_bus *bus, int devfn)
1014
int pci_scan_slot(struct pci_bus *bus, int devfn)
1014
{
1015
{
1015
    unsigned fn, nr = 0;
1016
    unsigned fn, nr = 0;
1016
    struct pci_dev *dev;
1017
    struct pci_dev *dev;
1017
    unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn;
1018
    unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn;
1018
 
1019
 
1019
    if (only_one_child(bus) && (devfn > 0))
1020
    if (only_one_child(bus) && (devfn > 0))
1020
        return 0; /* Already scanned the entire slot */
1021
        return 0; /* Already scanned the entire slot */
1021
 
1022
 
1022
    dev = pci_scan_single_device(bus, devfn);
1023
    dev = pci_scan_single_device(bus, devfn);
1023
    if (!dev)
1024
    if (!dev)
1024
        return 0;
1025
        return 0;
1025
    if (!dev->is_added)
1026
    if (!dev->is_added)
1026
        nr++;
1027
        nr++;
1027
 
1028
 
1028
    if (pci_ari_enabled(bus))
1029
    if (pci_ari_enabled(bus))
1029
        next_fn = next_ari_fn;
1030
        next_fn = next_ari_fn;
1030
    else if (dev->multifunction)
1031
    else if (dev->multifunction)
1031
        next_fn = next_trad_fn;
1032
        next_fn = next_trad_fn;
1032
 
1033
 
1033
    for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {
1034
    for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {
1034
        dev = pci_scan_single_device(bus, devfn + fn);
1035
        dev = pci_scan_single_device(bus, devfn + fn);
1035
        if (dev) {
1036
        if (dev) {
1036
            if (!dev->is_added)
1037
            if (!dev->is_added)
1037
                nr++;
1038
                nr++;
1038
            dev->multifunction = 1;
1039
            dev->multifunction = 1;
1039
        }
1040
        }
1040
    }
1041
    }
1041
 
1042
 
1042
    /* only one slot has pcie device */
1043
    /* only one slot has pcie device */
1043
//    if (bus->self && nr)
1044
//    if (bus->self && nr)
1044
//        pcie_aspm_init_link_state(bus->self);
1045
//        pcie_aspm_init_link_state(bus->self);
1045
 
1046
 
1046
    return nr;
1047
    return nr;
1047
}
1048
}
1048
 
1049
 
1049
 
1050
 
1050
unsigned int pci_scan_child_bus(struct pci_bus *bus)
1051
unsigned int pci_scan_child_bus(struct pci_bus *bus)
1051
{
1052
{
1052
    unsigned int devfn, pass, max = bus->secondary;
1053
    unsigned int devfn, pass, max = bus->secondary;
1053
    struct pci_dev *dev;
1054
    struct pci_dev *dev;
1054
 
1055
 
1055
    dbgprintf("scanning bus\n");
1056
    dbgprintf("scanning bus\n");
1056
 
1057
 
1057
    /* Go find them, Rover! */
1058
    /* Go find them, Rover! */
1058
    for (devfn = 0; devfn < 0x100; devfn += 8)
1059
    for (devfn = 0; devfn < 0x100; devfn += 8)
1059
        pci_scan_slot(bus, devfn);
1060
        pci_scan_slot(bus, devfn);
1060
 
1061
 
1061
    /* Reserve buses for SR-IOV capability. */
1062
    /* Reserve buses for SR-IOV capability. */
1062
    max += pci_iov_bus_range(bus);
1063
    max += pci_iov_bus_range(bus);
1063
 
1064
 
1064
    /*
1065
    /*
1065
     * After performing arch-dependent fixup of the bus, look behind
1066
     * After performing arch-dependent fixup of the bus, look behind
1066
     * all PCI-to-PCI bridges on this bus.
1067
     * all PCI-to-PCI bridges on this bus.
1067
     */
1068
     */
1068
    if (!bus->is_added) {
1069
    if (!bus->is_added) {
1069
        dbgprintf("fixups for bus\n");
1070
        dbgprintf("fixups for bus\n");
1070
//        pcibios_fixup_bus(bus);
1071
//        pcibios_fixup_bus(bus);
1071
        if (pci_is_root_bus(bus))
1072
        if (pci_is_root_bus(bus))
1072
            bus->is_added = 1;
1073
            bus->is_added = 1;
1073
    }
1074
    }
1074
 
1075
 
1075
    for (pass=0; pass < 2; pass++)
1076
    for (pass=0; pass < 2; pass++)
1076
        list_for_each_entry(dev, &bus->devices, bus_list) {
1077
        list_for_each_entry(dev, &bus->devices, bus_list) {
1077
            if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1078
            if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1078
                dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1079
                dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1079
                max = pci_scan_bridge(bus, dev, max, pass);
1080
                max = pci_scan_bridge(bus, dev, max, pass);
1080
        }
1081
        }
1081
 
1082
 
1082
    /*
1083
    /*
1083
     * We've scanned the bus and so we know all about what's on
1084
     * We've scanned the bus and so we know all about what's on
1084
     * the other side of any bridges that may be on this bus plus
1085
     * the other side of any bridges that may be on this bus plus
1085
     * any devices.
1086
     * any devices.
1086
     *
1087
     *
1087
     * Return how far we've got finding sub-buses.
1088
     * Return how far we've got finding sub-buses.
1088
     */
1089
     */
1089
    dbgprintf("bus scan returning with max=%02x\n", max);
1090
    dbgprintf("bus scan returning with max=%02x\n", max);
1090
    return max;
1091
    return max;
1091
}
1092
}
1092
 
1093
 
1093
/**
1094
/**
1094
 * pci_cfg_space_size - get the configuration space size of the PCI device.
1095
 * pci_cfg_space_size - get the configuration space size of the PCI device.
1095
 * @dev: PCI device
1096
 * @dev: PCI device
1096
 *
1097
 *
1097
 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1098
 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1098
 * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
1099
 * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
1099
 * access it.  Maybe we don't have a way to generate extended config space
1100
 * access it.  Maybe we don't have a way to generate extended config space
1100
 * accesses, or the device is behind a reverse Express bridge.  So we try
1101
 * accesses, or the device is behind a reverse Express bridge.  So we try
1101
 * reading the dword at 0x100 which must either be 0 or a valid extended
1102
 * reading the dword at 0x100 which must either be 0 or a valid extended
1102
 * capability header.
1103
 * capability header.
1103
 */
1104
 */
1104
int pci_cfg_space_size_ext(struct pci_dev *dev)
1105
int pci_cfg_space_size_ext(struct pci_dev *dev)
1105
{
1106
{
1106
    u32 status;
1107
    u32 status;
1107
    int pos = PCI_CFG_SPACE_SIZE;
1108
    int pos = PCI_CFG_SPACE_SIZE;
1108
 
1109
 
1109
    if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1110
    if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
1110
        goto fail;
1111
        goto fail;
1111
    if (status == 0xffffffff)
1112
    if (status == 0xffffffff)
1112
        goto fail;
1113
        goto fail;
1113
 
1114
 
1114
    return PCI_CFG_SPACE_EXP_SIZE;
1115
    return PCI_CFG_SPACE_EXP_SIZE;
1115
 
1116
 
1116
 fail:
1117
 fail:
1117
    return PCI_CFG_SPACE_SIZE;
1118
    return PCI_CFG_SPACE_SIZE;
1118
}
1119
}
1119
 
1120
 
1120
int pci_cfg_space_size(struct pci_dev *dev)
1121
int pci_cfg_space_size(struct pci_dev *dev)
1121
{
1122
{
1122
    int pos;
1123
    int pos;
1123
    u32 status;
1124
    u32 status;
1124
    u16 class;
1125
    u16 class;
1125
 
1126
 
1126
    class = dev->class >> 8;
1127
    class = dev->class >> 8;
1127
    if (class == PCI_CLASS_BRIDGE_HOST)
1128
    if (class == PCI_CLASS_BRIDGE_HOST)
1128
        return pci_cfg_space_size_ext(dev);
1129
        return pci_cfg_space_size_ext(dev);
1129
 
1130
 
1130
    pos = pci_pcie_cap(dev);
1131
    pos = pci_pcie_cap(dev);
1131
    if (!pos) {
1132
    if (!pos) {
1132
        pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1133
        pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1133
        if (!pos)
1134
        if (!pos)
1134
            goto fail;
1135
            goto fail;
1135
 
1136
 
1136
        pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1137
        pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
1137
        if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
1138
        if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
1138
            goto fail;
1139
            goto fail;
1139
    }
1140
    }
1140
 
1141
 
1141
    return pci_cfg_space_size_ext(dev);
1142
    return pci_cfg_space_size_ext(dev);
1142
 
1143
 
1143
 fail:
1144
 fail:
1144
    return PCI_CFG_SPACE_SIZE;
1145
    return PCI_CFG_SPACE_SIZE;
1145
}
1146
}
1146
 
1147
 
1147
 
1148
 
1148
 
1149
 
1149
struct pci_bus * pci_create_bus(int bus, struct pci_ops *ops, void *sysdata)
1150
struct pci_bus * pci_create_bus(int bus, struct pci_ops *ops, void *sysdata)
1150
{
1151
{
1151
    int error;
1152
    int error;
1152
    struct pci_bus *b, *b2;
1153
    struct pci_bus *b, *b2;
1153
 
1154
 
1154
    b = pci_alloc_bus();
1155
    b = pci_alloc_bus();
1155
    if (!b)
1156
    if (!b)
1156
        return NULL;
1157
        return NULL;
1157
 
1158
 
1158
    b->sysdata = sysdata;
1159
    b->sysdata = sysdata;
1159
    b->ops = ops;
1160
    b->ops = ops;
1160
 
1161
 
1161
    b2 = pci_find_bus(pci_domain_nr(b), bus);
1162
    b2 = pci_find_bus(pci_domain_nr(b), bus);
1162
    if (b2) {
1163
    if (b2) {
1163
        /* If we already got to this bus through a different bridge, ignore it */
1164
        /* If we already got to this bus through a different bridge, ignore it */
1164
        dbgprintf("bus already known\n");
1165
        dbgprintf("bus already known\n");
1165
        goto err_out;
1166
        goto err_out;
1166
    }
1167
    }
1167
 
1168
 
1168
//    down_write(&pci_bus_sem);
1169
//    down_write(&pci_bus_sem);
1169
    list_add_tail(&b->node, &pci_root_buses);
1170
    list_add_tail(&b->node, &pci_root_buses);
1170
//    up_write(&pci_bus_sem);
1171
//    up_write(&pci_bus_sem);
1171
 
1172
 
1172
    b->number = b->secondary = bus;
1173
    b->number = b->secondary = bus;
1173
    b->resource[0] = &ioport_resource;
1174
    b->resource[0] = &ioport_resource;
1174
    b->resource[1] = &iomem_resource;
1175
    b->resource[1] = &iomem_resource;
1175
 
1176
 
1176
    return b;
1177
    return b;
1177
 
1178
 
1178
err_out:
1179
err_out:
1179
    kfree(b);
1180
    kfree(b);
1180
    return NULL;
1181
    return NULL;
1181
}
1182
}