summaryrefslogtreecommitdiff
path: root/src/common_bridge.c
blob: b4b5d7e15fe962cd5b7f605890d4de3e6f34910a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * (C) Copyright IBM Corporation 2006
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file common_bridge.c
 * Support routines used to process PCI header information for bridges.
 *
 * \author Ian Romanick <idr@us.ibm.com>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

#if defined(HAVE_STRING_H)
# include <string.h>
#elif defined(HAVE_STRINGS_H)
# include <strings.h>
#endif

#if defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#elif defined(HAVE_STDINT_H)
# include <stdint.h>
#endif

#include "pciaccess.h"
#include "pciaccess_private.h"

static int
read_bridge_info( struct pci_device_private * priv )
{
    uint8_t  buf[0x40];
    pciaddr_t bytes;
    int err;


    /* Make sure the device has been probed.  If not, header_type won't be
     * set and the rest of this function will fail.
     */
    err = pci_device_probe(& priv->base);
    if (err) {
	return err;
    }

    switch ( priv->header_type & 0x7f ) {
    case 0x00:
	break;

    case 0x01: {
	struct pci_bridge_info *info;

	info = malloc(sizeof(*info));
	if (info != NULL) {
	    pci_device_cfg_read( (struct pci_device *) priv, buf + 0x18, 0x18,
				 0x40 - 0x18, & bytes );

	    info->primary_bus = buf[0x18];
	    info->secondary_bus = buf[0x19];
	    info->subordinate_bus = buf[0x1a];
	    info->secondary_latency_timer = buf[0x1b];

	    info->io_type = buf[0x1c] & 0x0f;
	    info->io_base = (((uint32_t) (buf[0x1c] & 0x0f0)) << 8)
	      + (((uint32_t) buf[0x30]) << 16)
	      + (((uint32_t) buf[0x31]) << 24);

	    info->io_limit = 0x00000fff
	      + (((uint32_t) (buf[0x1d] & 0x0f0)) << 8)
	      + (((uint32_t) buf[0x32]) << 16)
	      + (((uint32_t) buf[0x33]) << 24);

	    info->mem_type = buf[0x20] & 0x0f;
	    info->mem_base = (((uint32_t) (buf[0x20] & 0x0f0)) << 16)
	      + (((uint32_t) buf[0x21]) << 24);

	    info->mem_limit = 0x0000ffff
	      + (((uint32_t) (buf[0x22] & 0x0f0)) << 16)
	      + (((uint32_t) buf[0x23]) << 24);

	    info->prefetch_mem_type = buf[0x24] & 0x0f;
	    info->prefetch_mem_base = (((uint64_t) (buf[0x24] & 0x0f0)) << 16)
	      + (((uint64_t) buf[0x25]) << 24)
	      + (((uint64_t) buf[0x28]) << 32)
	      + (((uint64_t) buf[0x29]) << 40)
	      + (((uint64_t) buf[0x2a]) << 48)
	      + (((uint64_t) buf[0x2b]) << 56);

	    info->prefetch_mem_limit = 0x0000ffff
	      + (((uint64_t) (buf[0x26] & 0x0f0)) << 16)
	      + (((uint64_t) buf[0x27]) << 24)
	      + (((uint64_t) buf[0x2c]) << 32)
	      + (((uint64_t) buf[0x2d]) << 40)
	      + (((uint64_t) buf[0x2e]) << 48)
	      + (((uint64_t) buf[0x2f]) << 56);

	    info->bridge_control = ((uint16_t) buf[0x3e])
	      + (((uint16_t) buf[0x3f]) << 8);

	    info->secondary_status = ((uint16_t) buf[0x1e])
	      + (((uint16_t) buf[0x1f]) << 8);
	}

	priv->bridge.pci = info;
	break;
    }

    case 0x02: {
	struct pci_pcmcia_bridge_info *info;

	info = malloc(sizeof(*info));
	if (info != NULL) {
	    pci_device_cfg_read( (struct pci_device *) priv, buf + 0x16, 0x16,
				 0x40 - 0x16, & bytes );

	    info->primary_bus = buf[0x18];
	    info->card_bus = buf[0x19];
	    info->subordinate_bus = buf[0x1a];
	    info->cardbus_latency_timer = buf[0x1b];

	    info->mem[0].base = (((uint32_t) buf[0x1c]))
	      + (((uint32_t) buf[0x1d]) << 8)
	      + (((uint32_t) buf[0x1e]) << 16)
	      + (((uint32_t) buf[0x1f]) << 24);

	    info->mem[0].limit = (((uint32_t) buf[0x20]))
	      + (((uint32_t) buf[0x21]) << 8)
	      + (((uint32_t) buf[0x22]) << 16)
	      + (((uint32_t) buf[0x23]) << 24);

	    info->mem[1].base = (((uint32_t) buf[0x24]))
	      + (((uint32_t) buf[0x25]) << 8)
	      + (((uint32_t) buf[0x26]) << 16)
	      + (((uint32_t) buf[0x27]) << 24);

	    info->mem[1].limit = (((uint32_t) buf[0x28]))
	      + (((uint32_t) buf[0x29]) << 8)
	      + (((uint32_t) buf[0x2a]) << 16)
	      + (((uint32_t) buf[0x2b]) << 24);

	    info->io[0].base = (((uint32_t) buf[0x2c]))
	      + (((uint32_t) buf[0x2d]) << 8)
	      + (((uint32_t) buf[0x2e]) << 16)
	      + (((uint32_t) buf[0x2f]) << 24);

	    info->io[0].limit = (((uint32_t) buf[0x30]))
	      + (((uint32_t) buf[0x31]) << 8)
	      + (((uint32_t) buf[0x32]) << 16)
	      + (((uint32_t) buf[0x33]) << 24);

	    info->io[1].base = (((uint32_t) buf[0x34]))
	      + (((uint32_t) buf[0x35]) << 8)
	      + (((uint32_t) buf[0x36]) << 16)
	      + (((uint32_t) buf[0x37]) << 24);

	    info->io[1].limit = (((uint32_t) buf[0x38]))
	      + (((uint32_t) buf[0x39]) << 8)
	      + (((uint32_t) buf[0x3a]) << 16)
	      + (((uint32_t) buf[0x3b]) << 24);

	    info->secondary_status = ((uint16_t) buf[0x16])
	      + (((uint16_t) buf[0x17]) << 8);

	    info->bridge_control = ((uint16_t) buf[0x3e])
	      + (((uint16_t) buf[0x3f]) << 8);
	}

	priv->bridge.pcmcia = info;
	break;
    }
    }

    return 0;
}


/**
 * Get the PCI bridge information for a device
 *
 * \returns
 * If \c dev is a PCI-to-PCI bridge, a pointer to a \c pci_bridge_info
 * structure.  Otherwise, \c NULL is returned.
 */
const struct pci_bridge_info *
pci_device_get_bridge_info( struct pci_device * dev )
{
    struct pci_device_private * priv = (struct pci_device_private *) dev;

    if (priv->bridge.pci == NULL) {
	read_bridge_info(priv);
    }

    return (priv->header_type == 1) ? priv->bridge.pci : NULL;
}


/**
 * Get the PCMCIA bridge information for a device
 *
 * \returns
 * If \c dev is a PCI-to-PCMCIA bridge, a pointer to a
 * \c pci_pcmcia_bridge_info structure.  Otherwise, \c NULL is returned.
 */
const struct pci_pcmcia_bridge_info *
pci_device_get_pcmcia_bridge_info( struct pci_device * dev )
{
    struct pci_device_private * priv = (struct pci_device_private *) dev;

    if (priv->bridge.pcmcia == NULL) {
	read_bridge_info(priv);
    }

    return (priv->header_type == 2) ? priv->bridge.pcmcia : NULL;
}


/**
 * Determine the primary, secondary, and subordinate buses for a bridge
 *
 * Determines the IDs of the primary, secondary, and subordinate buses for
 * a specified bridge.  Not all bridges directly store this information
 * (e.g., PCI-to-ISA bridges).  For those bridges, no error is returned, but
 * -1 is stored in the bus IDs that don't make sense.
 *
 * For example, for a PCI-to-ISA bridge, \c primary_bus will be set to the ID
 * of the bus containing the device and both \c secondary_bus and
 * \c subordinate_bus will be set to -1.
 *
 * \return
 * On success, zero is returned.  If \c dev is not a bridge, \c ENODEV is
 * returned.
 *
 * \bug
 * Host bridges are handled the same way as PCI-to-ISA bridges.  This is
 * almost certainly not correct.
 */
int
pci_device_get_bridge_buses(struct pci_device * dev, int *primary_bus,
			    int *secondary_bus, int *subordinate_bus)
{
    struct pci_device_private * priv = (struct pci_device_private *) dev;

    /* If the device isn't a bridge, return an error.
     */

    if (((dev->device_class >> 16) & 0x0ff) != 0x06) {
	return ENODEV;
    }

    switch ((dev->device_class >> 8) & 0x0ff) {
    case 0x00:
	/* What to do for host bridges?  I'm pretty sure this isn't right.
	 */
	*primary_bus = dev->bus;
	*secondary_bus = -1;
	*subordinate_bus = -1;
	break;

    case 0x01:
    case 0x02:
    case 0x03:
	*primary_bus = dev->bus;
	*secondary_bus = -1;
	*subordinate_bus = -1;
	break;

    case 0x04:
    if (priv->bridge.pci == NULL)
        read_bridge_info(priv);
    if ((priv->header_type & 0x7f) == 0x01) {
	*primary_bus = priv->bridge.pci->primary_bus;
	*secondary_bus = priv->bridge.pci->secondary_bus;
	*subordinate_bus = priv->bridge.pci->subordinate_bus;
    } else {
	*primary_bus = dev->bus;
	*secondary_bus = -1;
	*subordinate_bus = -1;
    }
	break;

    case 0x07:
    if (priv->bridge.pcmcia == NULL)
        read_bridge_info(priv);
    if ((priv->header_type & 0x7f) == 0x02) {
	*primary_bus = priv->bridge.pcmcia->primary_bus;
	*secondary_bus = priv->bridge.pcmcia->card_bus;
	*subordinate_bus = priv->bridge.pcmcia->subordinate_bus;
    } else {
	*primary_bus = dev->bus;
	*secondary_bus = -1;
	*subordinate_bus = -1;
    }
	break;
    }

    return 0;
}

#define PCI_CLASS_BRIDGE 0x06
#define PCI_SUBCLASS_BRIDGE_PCI 0x04
#define PCI_CLASS_MASK 0xFF
#define PCI_SUBCLASS_MASK 0xFF

struct pci_device *
pci_device_get_parent_bridge(struct pci_device *dev)
{
    struct pci_id_match bridge_match = {
        PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
        (PCI_CLASS_BRIDGE << 16) | (PCI_SUBCLASS_BRIDGE_PCI << 8),
        (PCI_CLASS_MASK << 16) | (PCI_SUBCLASS_MASK << 8)
    };

    struct pci_device *bridge;
    struct pci_device_iterator *iter;

    if (dev == NULL)
        return NULL;

    iter = pci_id_match_iterator_create(& bridge_match);
    if (iter == NULL)
        return NULL;

    while ((bridge = pci_device_next(iter)) != NULL) {
        if (bridge->domain == dev->domain) {
            const struct pci_bridge_info *info =
                pci_device_get_bridge_info(bridge);

            if (info != NULL) {
                if (info->secondary_bus == dev->bus) {
                    break;
                }
            }
        }
    }

    pci_iterator_destroy(iter);

    return bridge;
}