Subversion Repositories Kolibri OS

Rev

Rev 2187 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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