pciaccess.h 14.7 KB
Newer Older
Ian Romanick's avatar
Ian Romanick committed
1
2
/*
 * (C) Copyright IBM Corporation 2006
Adam Jackson's avatar
Adam Jackson committed
3
 * Copyright 2009 Red Hat, Inc.
Ian Romanick's avatar
Ian Romanick committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * 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.
 */
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
/*
 * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti
 *
 * 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 the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, 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 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
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS 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.
 *
 */
Ian Romanick's avatar
Ian Romanick committed
50
51
52

/**
 * \file pciaccess.h
Alan Coopersmith's avatar
Alan Coopersmith committed
53
 *
Ian Romanick's avatar
Ian Romanick committed
54
55
56
 * \author Ian Romanick <idr@us.ibm.com>
 */

Ian Romanick's avatar
Ian Romanick committed
57
58
59
#ifndef PCIACCESS_H
#define PCIACCESS_H

Ian Romanick's avatar
Ian Romanick committed
60
61
#include <inttypes.h>

62
#if (__GNUC__ >= 3) || (__SUNPRO_C >= 0x5130)
63
64
#define __deprecated __attribute__((deprecated))
#else
Alan Coopersmith's avatar
Alan Coopersmith committed
65
#define __deprecated
66
67
#endif

Ian Romanick's avatar
Ian Romanick committed
68
69
70
71
72
typedef uint64_t pciaddr_t;

struct pci_device;
struct pci_device_iterator;
struct pci_id_match;
Ian Romanick's avatar
Ian Romanick committed
73
struct pci_slot_match;
Ian Romanick's avatar
Ian Romanick committed
74

75
76
77
78
#ifdef __cplusplus
extern "C" {
#endif

79
80
int pci_device_has_kernel_driver(struct pci_device *dev);

81
82
int pci_device_is_boot_vga(struct pci_device *dev);

Ian Romanick's avatar
Ian Romanick committed
83
int pci_device_read_rom(struct pci_device *dev, void *buffer);
Ian Romanick's avatar
Ian Romanick committed
84

85
86
int  __deprecated pci_device_map_region(struct pci_device *dev,
    unsigned region, int write_enable);
Ian Romanick's avatar
Ian Romanick committed
87

88
89
int __deprecated pci_device_unmap_region(struct pci_device *dev,
    unsigned region);
Ian Romanick's avatar
Ian Romanick committed
90

91
92
int pci_device_map_range(struct pci_device *dev, pciaddr_t base,
    pciaddr_t size, unsigned map_flags, void **addr);
93

94
int pci_device_unmap_range(struct pci_device *dev, void *memory,
95
96
    pciaddr_t size);

97
98
99
100
101
102
int __deprecated pci_device_map_memory_range(struct pci_device *dev,
    pciaddr_t base, pciaddr_t size, int write_enable, void **addr);

int __deprecated pci_device_unmap_memory_range(struct pci_device *dev,
    void *memory, pciaddr_t size);

Ian Romanick's avatar
Ian Romanick committed
103
int pci_device_probe(struct pci_device *dev);
Ian Romanick's avatar
Ian Romanick committed
104

Ian Romanick's avatar
Ian Romanick committed
105
const struct pci_agp_info *pci_device_get_agp_info(struct pci_device *dev);
Ian Romanick's avatar
Ian Romanick committed
106

Ian Romanick's avatar
Ian Romanick committed
107
108
const struct pci_bridge_info *pci_device_get_bridge_info(
    struct pci_device *dev);
109

Ian Romanick's avatar
Ian Romanick committed
110
111
const struct pci_pcmcia_bridge_info *pci_device_get_pcmcia_bridge_info(
    struct pci_device *dev);
112

Ian Romanick's avatar
Ian Romanick committed
113
int pci_device_get_bridge_buses(struct pci_device *dev, int *primary_bus,
114
115
    int *secondary_bus, int *subordinate_bus);

Ian Romanick's avatar
Ian Romanick committed
116
int pci_system_init(void);
Ian Romanick's avatar
Ian Romanick committed
117

118
119
void pci_system_init_dev_mem(int fd);

Ian Romanick's avatar
Ian Romanick committed
120
void pci_system_cleanup(void);
Ian Romanick's avatar
Ian Romanick committed
121

Ian Romanick's avatar
Ian Romanick committed
122
123
struct pci_device_iterator *pci_slot_match_iterator_create(
    const struct pci_slot_match *match);
Ian Romanick's avatar
Ian Romanick committed
124

Ian Romanick's avatar
Ian Romanick committed
125
126
struct pci_device_iterator *pci_id_match_iterator_create(
    const struct pci_id_match *match);
Ian Romanick's avatar
Ian Romanick committed
127

Ian Romanick's avatar
Ian Romanick committed
128
void pci_iterator_destroy(struct pci_device_iterator *iter);
Ian Romanick's avatar
Ian Romanick committed
129

Ian Romanick's avatar
Ian Romanick committed
130
struct pci_device *pci_device_next(struct pci_device_iterator *iter);
Ian Romanick's avatar
Ian Romanick committed
131

Ian Romanick's avatar
Ian Romanick committed
132
133
struct pci_device *pci_device_find_by_slot(uint32_t domain, uint32_t bus,
    uint32_t dev, uint32_t func);
Ian Romanick's avatar
Ian Romanick committed
134

135
136
struct pci_device *pci_device_get_parent_bridge(struct pci_device *dev);

Ian Romanick's avatar
Ian Romanick committed
137
138
139
140
141
142
143
void pci_get_strings(const struct pci_id_match *m,
    const char **device_name, const char **vendor_name,
    const char **subdevice_name, const char **subvendor_name);
const char *pci_device_get_device_name(const struct pci_device *dev);
const char *pci_device_get_subdevice_name(const struct pci_device *dev);
const char *pci_device_get_vendor_name(const struct pci_device *dev);
const char *pci_device_get_subvendor_name(const struct pci_device *dev);
Ian Romanick's avatar
Ian Romanick committed
144

145
146
void pci_device_enable(struct pci_device *dev);

Ian Romanick's avatar
Ian Romanick committed
147
148
149
150
151
152
153
154
int pci_device_cfg_read    (struct pci_device *dev, void *data,
    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read);
int pci_device_cfg_read_u8 (struct pci_device *dev, uint8_t  *data,
    pciaddr_t offset);
int pci_device_cfg_read_u16(struct pci_device *dev, uint16_t *data,
    pciaddr_t offset);
int pci_device_cfg_read_u32(struct pci_device *dev, uint32_t *data,
    pciaddr_t offset);
Ian Romanick's avatar
Ian Romanick committed
155

Ian Romanick's avatar
Ian Romanick committed
156
157
int pci_device_cfg_write    (struct pci_device *dev, const void *data,
    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written);
158
159
160
161
162
163
int pci_device_cfg_write_u8 (struct pci_device *dev, uint8_t  data,
    pciaddr_t offset);
int pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data,
    pciaddr_t offset);
int pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data,
    pciaddr_t offset);
Ian Romanick's avatar
Ian Romanick committed
164
165
int pci_device_cfg_write_bits(struct pci_device *dev, uint32_t mask,
    uint32_t data, pciaddr_t offset);
Ian Romanick's avatar
Ian Romanick committed
166

167
168
169
170
#ifdef __cplusplus
}
#endif

171
172
173
174
175
176
177
178
179
180
/**
 * \name Mapping flags passed to \c pci_device_map_range
 */
/*@{*/
#define PCI_DEV_MAP_FLAG_WRITABLE       (1U<<0)
#define PCI_DEV_MAP_FLAG_WRITE_COMBINE  (1U<<1)
#define PCI_DEV_MAP_FLAG_CACHABLE       (1U<<2)
/*@}*/


181
#define PCI_MATCH_ANY  (~0U)
Ian Romanick's avatar
Ian Romanick committed
182

Ian Romanick's avatar
Ian Romanick committed
183
184
185
186
187
188
189
190
/**
 * Compare two PCI ID values (either vendor or device).  This is used
 * internally to compare the fields of \c pci_id_match to the fields of
 * \c pci_device.
 */
#define PCI_ID_COMPARE(a, b) \
    (((a) == PCI_MATCH_ANY) || ((a) == (b)))

Ian Romanick's avatar
Ian Romanick committed
191
192
193
194
195
/**
 */
struct pci_id_match {
    /**
     * \name Device / vendor matching controls
Alan Coopersmith's avatar
Alan Coopersmith committed
196
     *
Ian Romanick's avatar
Ian Romanick committed
197
198
199
200
201
202
203
204
205
206
207
     * Control the search based on the device, vendor, subdevice, or subvendor
     * IDs.  Setting any of these fields to \c PCI_MATCH_ANY will cause the
     * field to not be used in the comparison.
     */
    /*@{*/
    uint32_t    vendor_id;
    uint32_t    device_id;
    uint32_t    subvendor_id;
    uint32_t    subdevice_id;
    /*@}*/

Ian Romanick's avatar
Ian Romanick committed
208

Ian Romanick's avatar
Ian Romanick committed
209
210
    /**
     * \name Device class matching controls
Alan Coopersmith's avatar
Alan Coopersmith committed
211
     *
Ian Romanick's avatar
Ian Romanick committed
212
213
214
215
216
     */
    /*@{*/
    uint32_t    device_class;
    uint32_t    device_class_mask;
    /*@}*/
Ian Romanick's avatar
Ian Romanick committed
217

Ian Romanick's avatar
Ian Romanick committed
218
219
220
221
    intptr_t    match_data;
};


Ian Romanick's avatar
Ian Romanick committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 */
struct pci_slot_match {
    /**
     * \name Device slot matching controls
     *
     * Control the search based on the domain, bus, slot, and function of
     * the device.  Setting any of these fields to \c PCI_MATCH_ANY will cause
     * the field to not be used in the comparison.
     */
    /*@{*/
    uint32_t    domain;
    uint32_t    bus;
    uint32_t    dev;
    uint32_t    func;
    /*@}*/

    intptr_t    match_data;
};

Ian Romanick's avatar
Ian Romanick committed
242
243
244
245
246
247
/**
 * BAR descriptor for a PCI device.
 */
struct pci_mem_region {
    /**
     * When the region is mapped, this is the pointer to the memory.
248
249
250
251
252
     *
     * This field is \b only set when the deprecated \c pci_device_map_region
     * interface is used.  Use \c pci_device_map_range instead.
     *
     * \deprecated
Ian Romanick's avatar
Ian Romanick committed
253
     */
Ian Romanick's avatar
Ian Romanick committed
254
    void *memory;
Ian Romanick's avatar
Ian Romanick committed
255

256
257
258
259
260
261
262
263

    /**
     * Base physical address of the region within its bus / domain.
     *
     * \warning
     * This address is really only useful to other devices in the same
     * domain.  It's probably \b not the address applications will ever
     * use.
Alan Coopersmith's avatar
Alan Coopersmith committed
264
     *
265
266
267
     * \warning
     * Most (all?) platform back-ends leave this field unset.
     */
Ian Romanick's avatar
Ian Romanick committed
268
    pciaddr_t bus_addr;
269
270
271
272


    /**
     * Base physical address of the region from the CPU's point of view.
Alan Coopersmith's avatar
Alan Coopersmith committed
273
     *
274
275
276
     * This address is typically passed to \c pci_device_map_range to create
     * a mapping of the region to the CPU's virtual address space.
     */
Ian Romanick's avatar
Ian Romanick committed
277
278
279
280
281
282
283
    pciaddr_t base_addr;


    /**
     * Size, in bytes, of the region.
     */
    pciaddr_t size;
Ian Romanick's avatar
Ian Romanick committed
284

Ian Romanick's avatar
Ian Romanick committed
285
286
287
288
289

    /**
     * Is the region I/O ports or memory?
     */
    unsigned is_IO:1;
Ian Romanick's avatar
Ian Romanick committed
290

Ian Romanick's avatar
Ian Romanick committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    /**
     * Is the memory region prefetchable?
     *
     * \note
     * This can only be set if \c is_IO is not set.
     */
    unsigned is_prefetchable:1;


    /**
     * Is the memory at a 64-bit address?
     *
     * \note
     * This can only be set if \c is_IO is not set.
     */
    unsigned is_64:1;
};


/**
 * PCI device.
Ian Romanick's avatar
Ian Romanick committed
312
 *
Ian Romanick's avatar
Ian Romanick committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
 * Contains all of the information about a particular PCI device.
 */
struct pci_device {
    /**
     * \name Device bus identification.
     *
     * Complete bus identification, including domain, of the device.  On
     * platforms that do not support PCI domains (e.g., 32-bit x86 hardware),
     * the domain will always be zero.
     */
    /*@{*/
    uint16_t    domain;
    uint8_t     bus;
    uint8_t     dev;
    uint8_t     func;
    /*@}*/


    /**
     * \name Vendor / device ID
Ian Romanick's avatar
Ian Romanick committed
333
     *
Ian Romanick's avatar
Ian Romanick committed
334
335
336
337
338
339
340
341
342
343
     * The vendor ID, device ID, and sub-IDs for the device.
     */
    /*@{*/
    uint16_t    vendor_id;
    uint16_t    device_id;
    uint16_t    subvendor_id;
    uint16_t    subdevice_id;
    /*@}*/

    /**
Ian Romanick's avatar
Ian Romanick committed
344
345
346
     * Device's class, subclass, and programming interface packed into a
     * single 32-bit value.  The class is at bits [23:16], subclass is at
     * bits [15:8], and programming interface is at [7:0].
Ian Romanick's avatar
Ian Romanick committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
     */
    uint32_t    device_class;


    /**
     * Device revision number, as read from the configuration header.
     */
    uint8_t     revision;


    /**
     * BAR descriptors for the device.
     */
    struct pci_mem_region regions[6];


    /**
     * Size, in bytes, of the device's expansion ROM.
     */
    pciaddr_t   rom_size;


    /**
     * IRQ associated with the device.  If there is no IRQ, this value will
     * be -1.
     */
    int irq;


    /**
     * Storage for user data.  Users of the library can store arbitrary
     * data in this pointer.  The library will not use it for any purpose.
     * It is the user's responsability to free this memory before destroying
     * the \c pci_device structure.
     */
Ian Romanick's avatar
Ian Romanick committed
382
    intptr_t user_data;
Tiago Vignatti's avatar
Tiago Vignatti committed
383
384

    /**
Tiago Vignatti's avatar
Tiago Vignatti committed
385
386
      * Used by the VGA arbiter. Type of resource decoded by the device and
      * the file descriptor (/dev/vga_arbiter). */
Tiago Vignatti's avatar
Tiago Vignatti committed
387
    int vgaarb_rsrc;
Ian Romanick's avatar
Ian Romanick committed
388
389
390
391
392
};


/**
 * Description of the AGP capability of the device.
Ian Romanick's avatar
Ian Romanick committed
393
 *
Ian Romanick's avatar
Ian Romanick committed
394
395
396
397
398
399
400
401
402
403
 * \sa pci_device_get_agp_info
 */
struct pci_agp_info {
    /**
     * Offset of the AGP registers in the devices configuration register
     * space.  This is generally used so that the offset of the AGP command
     * register can be determined.
     */
    unsigned    config_offset;

Ian Romanick's avatar
Ian Romanick committed
404

Ian Romanick's avatar
Ian Romanick committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    /**
     * \name AGP major / minor version.
     */
    /*@{*/
    uint8_t	major_version;
    uint8_t     minor_version;
    /*@}*/

    /**
     * Logical OR of the supported AGP rates.  For example, a value of 0x07
     * means that the device can support 1x, 2x, and 4x.  A value of 0x0c
     * means that the device can support 8x and 4x.
     */
    uint8_t    rates;

420
421
422
423
424
425
426
    unsigned int    fast_writes:1;  /**< Are fast-writes supported? */
    unsigned int    addr64:1;
    unsigned int    htrans:1;
    unsigned int    gart64:1;
    unsigned int    coherent:1;
    unsigned int    sideband:1;     /**< Is side-band addressing supported? */
    unsigned int    isochronus:1;
Ian Romanick's avatar
Ian Romanick committed
427
428
429
430
431

    uint8_t    async_req_size;
    uint8_t    calibration_cycle_timing;
    uint8_t    max_requests;
};
Ian Romanick's avatar
Ian Romanick committed
432

433
434
/**
 * Description of a PCI-to-PCI bridge device.
Ian Romanick's avatar
Ian Romanick committed
435
 *
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
 * \sa pci_device_get_bridge_info
 */
struct pci_bridge_info {
    uint8_t    primary_bus;
    uint8_t    secondary_bus;
    uint8_t    subordinate_bus;
    uint8_t    secondary_latency_timer;

    uint8_t     io_type;
    uint8_t     mem_type;
    uint8_t     prefetch_mem_type;

    uint16_t    secondary_status;
    uint16_t    bridge_control;

    uint32_t    io_base;
    uint32_t    io_limit;

    uint32_t    mem_base;
    uint32_t    mem_limit;

    uint64_t    prefetch_mem_base;
    uint64_t    prefetch_mem_limit;
};

/**
 * Description of a PCI-to-PCMCIA bridge device.
Ian Romanick's avatar
Ian Romanick committed
463
 *
464
465
466
467
468
469
470
 * \sa pci_device_get_pcmcia_bridge_info
 */
struct pci_pcmcia_bridge_info {
    uint8_t    primary_bus;
    uint8_t    card_bus;
    uint8_t    subordinate_bus;
    uint8_t    cardbus_latency_timer;
Alan Coopersmith's avatar
Alan Coopersmith committed
471

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    uint16_t    secondary_status;
    uint16_t    bridge_control;

    struct {
	uint32_t    base;
	uint32_t    limit;
    } io[2];

    struct {
	uint32_t    base;
	uint32_t    limit;
    } mem[2];

};

487
488
489
490
491

/**
 * VGA Arbiter definitions, functions and related.
 */

Tiago Vignatti's avatar
Tiago Vignatti committed
492
493
494
495
496
497
498
/* Legacy VGA regions */
#define VGA_ARB_RSRC_NONE       0x00
#define VGA_ARB_RSRC_LEGACY_IO  0x01
#define VGA_ARB_RSRC_LEGACY_MEM 0x02
/* Non-legacy access */
#define VGA_ARB_RSRC_NORMAL_IO  0x04
#define VGA_ARB_RSRC_NORMAL_MEM 0x08
499

500
501
int  pci_device_vgaarb_init         (void);
void pci_device_vgaarb_fini         (void);
Tiago Vignatti's avatar
Tiago Vignatti committed
502
int  pci_device_vgaarb_set_target   (struct pci_device *dev);
503
504
505
506
507
/* use the targetted device */
int  pci_device_vgaarb_decodes      (int new_vga_rsrc);
int  pci_device_vgaarb_lock         (void);
int  pci_device_vgaarb_trylock      (void);
int  pci_device_vgaarb_unlock       (void);
508
509
/* return the current device count + resource decodes for the device */
int pci_device_vgaarb_get_info	    (struct pci_device *dev, int *vga_count, int *rsrc_decodes);
510

Adam Jackson's avatar
Adam Jackson committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * I/O space access.
 */

struct pci_io_handle;

struct pci_io_handle *pci_device_open_io(struct pci_device *dev, pciaddr_t base,
					 pciaddr_t size);
struct pci_io_handle *pci_legacy_open_io(struct pci_device *dev, pciaddr_t base,
					 pciaddr_t size);
void pci_device_close_io(struct pci_device *dev, struct pci_io_handle *handle);
uint32_t pci_io_read32(struct pci_io_handle *handle, uint32_t reg);
uint16_t pci_io_read16(struct pci_io_handle *handle, uint32_t reg);
uint8_t pci_io_read8(struct pci_io_handle *handle, uint32_t reg);
void pci_io_write32(struct pci_io_handle *handle, uint32_t reg, uint32_t data);
void pci_io_write16(struct pci_io_handle *handle, uint32_t reg, uint16_t data);
void pci_io_write8(struct pci_io_handle *handle, uint32_t reg, uint8_t data);

Adam Jackson's avatar
Adam Jackson committed
529
530
531
532
533
534
535
536
/*
 * Legacy memory access
 */

int pci_device_map_legacy(struct pci_device *dev, pciaddr_t base,
			  pciaddr_t size, unsigned map_flags, void **addr);
int pci_device_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size);

Ian Romanick's avatar
Ian Romanick committed
537
#endif /* PCIACCESS_H */