pciaccess.h revision 4f5e7dd7
1/*
2 * (C) Copyright IBM Corporation 2006
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * \file pciaccess.h
27 *
28 * \author Ian Romanick <idr@us.ibm.com>
29 */
30
31#ifndef PCIACCESS_H
32#define PCIACCESS_H
33
34#include <inttypes.h>
35
36#if __GNUC__ >= 3
37#define __deprecated __attribute__((deprecated))
38#else
39#define __deprecated
40#endif
41
42typedef uint64_t pciaddr_t;
43
44struct pci_device;
45struct pci_device_iterator;
46struct pci_id_match;
47struct pci_slot_match;
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53int pci_device_read_rom(struct pci_device *dev, void *buffer);
54
55int  __deprecated pci_device_map_region(struct pci_device *dev,
56    unsigned region, int write_enable);
57
58int __deprecated pci_device_unmap_region(struct pci_device *dev,
59    unsigned region);
60
61int pci_device_map_range(struct pci_device *dev, pciaddr_t base,
62    pciaddr_t size, unsigned map_flags, void **addr);
63
64int pci_device_unmap_range(struct pci_device *dev, void *memory,
65    pciaddr_t size);
66
67int __deprecated pci_device_map_memory_range(struct pci_device *dev,
68    pciaddr_t base, pciaddr_t size, int write_enable, void **addr);
69
70int __deprecated pci_device_unmap_memory_range(struct pci_device *dev,
71    void *memory, pciaddr_t size);
72
73int pci_device_probe(struct pci_device *dev);
74
75const struct pci_agp_info *pci_device_get_agp_info(struct pci_device *dev);
76
77const struct pci_bridge_info *pci_device_get_bridge_info(
78    struct pci_device *dev);
79
80const struct pci_pcmcia_bridge_info *pci_device_get_pcmcia_bridge_info(
81    struct pci_device *dev);
82
83int pci_device_get_bridge_buses(struct pci_device *dev, int *primary_bus,
84    int *secondary_bus, int *subordinate_bus);
85
86int pci_system_init(void);
87
88void pci_system_init_dev_mem(int fd);
89
90void pci_system_cleanup(void);
91
92struct pci_device_iterator *pci_slot_match_iterator_create(
93    const struct pci_slot_match *match);
94
95struct pci_device_iterator *pci_id_match_iterator_create(
96    const struct pci_id_match *match);
97
98void pci_iterator_destroy(struct pci_device_iterator *iter);
99
100struct pci_device *pci_device_next(struct pci_device_iterator *iter);
101
102struct pci_device *pci_device_find_by_slot(uint32_t domain, uint32_t bus,
103    uint32_t dev, uint32_t func);
104
105void pci_get_strings(const struct pci_id_match *m,
106    const char **device_name, const char **vendor_name,
107    const char **subdevice_name, const char **subvendor_name);
108const char *pci_device_get_device_name(const struct pci_device *dev);
109const char *pci_device_get_subdevice_name(const struct pci_device *dev);
110const char *pci_device_get_vendor_name(const struct pci_device *dev);
111const char *pci_device_get_subvendor_name(const struct pci_device *dev);
112
113void pci_device_enable(struct pci_device *dev);
114
115int pci_device_cfg_read    (struct pci_device *dev, void *data,
116    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read);
117int pci_device_cfg_read_u8 (struct pci_device *dev, uint8_t  *data,
118    pciaddr_t offset);
119int pci_device_cfg_read_u16(struct pci_device *dev, uint16_t *data,
120    pciaddr_t offset);
121int pci_device_cfg_read_u32(struct pci_device *dev, uint32_t *data,
122    pciaddr_t offset);
123
124int pci_device_cfg_write    (struct pci_device *dev, const void *data,
125    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written);
126int pci_device_cfg_write_u8 (struct pci_device *dev, uint8_t  data,
127    pciaddr_t offset);
128int pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data,
129    pciaddr_t offset);
130int pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data,
131    pciaddr_t offset);
132int pci_device_cfg_write_bits(struct pci_device *dev, uint32_t mask,
133    uint32_t data, pciaddr_t offset);
134
135#ifdef __cplusplus
136}
137#endif
138
139/**
140 * \name Mapping flags passed to \c pci_device_map_range
141 */
142/*@{*/
143#define PCI_DEV_MAP_FLAG_WRITABLE       (1U<<0)
144#define PCI_DEV_MAP_FLAG_WRITE_COMBINE  (1U<<1)
145#define PCI_DEV_MAP_FLAG_CACHABLE       (1U<<2)
146/*@}*/
147
148
149#define PCI_MATCH_ANY  (~0)
150
151/**
152 * Compare two PCI ID values (either vendor or device).  This is used
153 * internally to compare the fields of \c pci_id_match to the fields of
154 * \c pci_device.
155 */
156#define PCI_ID_COMPARE(a, b) \
157    (((a) == PCI_MATCH_ANY) || ((a) == (b)))
158
159/**
160 */
161struct pci_id_match {
162    /**
163     * \name Device / vendor matching controls
164     *
165     * Control the search based on the device, vendor, subdevice, or subvendor
166     * IDs.  Setting any of these fields to \c PCI_MATCH_ANY will cause the
167     * field to not be used in the comparison.
168     */
169    /*@{*/
170    uint32_t    vendor_id;
171    uint32_t    device_id;
172    uint32_t    subvendor_id;
173    uint32_t    subdevice_id;
174    /*@}*/
175
176
177    /**
178     * \name Device class matching controls
179     *
180     */
181    /*@{*/
182    uint32_t    device_class;
183    uint32_t    device_class_mask;
184    /*@}*/
185
186    intptr_t    match_data;
187};
188
189
190/**
191 */
192struct pci_slot_match {
193    /**
194     * \name Device slot matching controls
195     *
196     * Control the search based on the domain, bus, slot, and function of
197     * the device.  Setting any of these fields to \c PCI_MATCH_ANY will cause
198     * the field to not be used in the comparison.
199     */
200    /*@{*/
201    uint32_t    domain;
202    uint32_t    bus;
203    uint32_t    dev;
204    uint32_t    func;
205    /*@}*/
206
207    intptr_t    match_data;
208};
209
210/**
211 * BAR descriptor for a PCI device.
212 */
213struct pci_mem_region {
214    /**
215     * When the region is mapped, this is the pointer to the memory.
216     *
217     * This field is \b only set when the deprecated \c pci_device_map_region
218     * interface is used.  Use \c pci_device_map_range instead.
219     *
220     * \deprecated
221     */
222    void *memory;
223
224
225    /**
226     * Base physical address of the region within its bus / domain.
227     *
228     * \warning
229     * This address is really only useful to other devices in the same
230     * domain.  It's probably \b not the address applications will ever
231     * use.
232     *
233     * \warning
234     * Most (all?) platform back-ends leave this field unset.
235     */
236    pciaddr_t bus_addr;
237
238
239    /**
240     * Base physical address of the region from the CPU's point of view.
241     *
242     * This address is typically passed to \c pci_device_map_range to create
243     * a mapping of the region to the CPU's virtual address space.
244     */
245    pciaddr_t base_addr;
246
247
248    /**
249     * Size, in bytes, of the region.
250     */
251    pciaddr_t size;
252
253
254    /**
255     * Is the region I/O ports or memory?
256     */
257    unsigned is_IO:1;
258
259    /**
260     * Is the memory region prefetchable?
261     *
262     * \note
263     * This can only be set if \c is_IO is not set.
264     */
265    unsigned is_prefetchable:1;
266
267
268    /**
269     * Is the memory at a 64-bit address?
270     *
271     * \note
272     * This can only be set if \c is_IO is not set.
273     */
274    unsigned is_64:1;
275};
276
277
278/**
279 * PCI device.
280 *
281 * Contains all of the information about a particular PCI device.
282 */
283struct pci_device {
284    /**
285     * \name Device bus identification.
286     *
287     * Complete bus identification, including domain, of the device.  On
288     * platforms that do not support PCI domains (e.g., 32-bit x86 hardware),
289     * the domain will always be zero.
290     */
291    /*@{*/
292    uint16_t    domain;
293    uint8_t     bus;
294    uint8_t     dev;
295    uint8_t     func;
296    /*@}*/
297
298
299    /**
300     * \name Vendor / device ID
301     *
302     * The vendor ID, device ID, and sub-IDs for the device.
303     */
304    /*@{*/
305    uint16_t    vendor_id;
306    uint16_t    device_id;
307    uint16_t    subvendor_id;
308    uint16_t    subdevice_id;
309    /*@}*/
310
311    /**
312     * Device's class, subclass, and programming interface packed into a
313     * single 32-bit value.  The class is at bits [23:16], subclass is at
314     * bits [15:8], and programming interface is at [7:0].
315     */
316    uint32_t    device_class;
317
318
319    /**
320     * Device revision number, as read from the configuration header.
321     */
322    uint8_t     revision;
323
324
325    /**
326     * BAR descriptors for the device.
327     */
328    struct pci_mem_region regions[6];
329
330
331    /**
332     * Size, in bytes, of the device's expansion ROM.
333     */
334    pciaddr_t   rom_size;
335
336
337    /**
338     * IRQ associated with the device.  If there is no IRQ, this value will
339     * be -1.
340     */
341    int irq;
342
343
344    /**
345     * Storage for user data.  Users of the library can store arbitrary
346     * data in this pointer.  The library will not use it for any purpose.
347     * It is the user's responsability to free this memory before destroying
348     * the \c pci_device structure.
349     */
350    intptr_t user_data;
351};
352
353
354/**
355 * Description of the AGP capability of the device.
356 *
357 * \sa pci_device_get_agp_info
358 */
359struct pci_agp_info {
360    /**
361     * Offset of the AGP registers in the devices configuration register
362     * space.  This is generally used so that the offset of the AGP command
363     * register can be determined.
364     */
365    unsigned    config_offset;
366
367
368    /**
369     * \name AGP major / minor version.
370     */
371    /*@{*/
372    uint8_t	major_version;
373    uint8_t     minor_version;
374    /*@}*/
375
376    /**
377     * Logical OR of the supported AGP rates.  For example, a value of 0x07
378     * means that the device can support 1x, 2x, and 4x.  A value of 0x0c
379     * means that the device can support 8x and 4x.
380     */
381    uint8_t    rates;
382
383    unsigned int    fast_writes:1;  /**< Are fast-writes supported? */
384    unsigned int    addr64:1;
385    unsigned int    htrans:1;
386    unsigned int    gart64:1;
387    unsigned int    coherent:1;
388    unsigned int    sideband:1;     /**< Is side-band addressing supported? */
389    unsigned int    isochronus:1;
390
391    uint8_t    async_req_size;
392    uint8_t    calibration_cycle_timing;
393    uint8_t    max_requests;
394};
395
396/**
397 * Description of a PCI-to-PCI bridge device.
398 *
399 * \sa pci_device_get_bridge_info
400 */
401struct pci_bridge_info {
402    uint8_t    primary_bus;
403    uint8_t    secondary_bus;
404    uint8_t    subordinate_bus;
405    uint8_t    secondary_latency_timer;
406
407    uint8_t     io_type;
408    uint8_t     mem_type;
409    uint8_t     prefetch_mem_type;
410
411    uint16_t    secondary_status;
412    uint16_t    bridge_control;
413
414    uint32_t    io_base;
415    uint32_t    io_limit;
416
417    uint32_t    mem_base;
418    uint32_t    mem_limit;
419
420    uint64_t    prefetch_mem_base;
421    uint64_t    prefetch_mem_limit;
422};
423
424/**
425 * Description of a PCI-to-PCMCIA bridge device.
426 *
427 * \sa pci_device_get_pcmcia_bridge_info
428 */
429struct pci_pcmcia_bridge_info {
430    uint8_t    primary_bus;
431    uint8_t    card_bus;
432    uint8_t    subordinate_bus;
433    uint8_t    cardbus_latency_timer;
434
435    uint16_t    secondary_status;
436    uint16_t    bridge_control;
437
438    struct {
439	uint32_t    base;
440	uint32_t    limit;
441    } io[2];
442
443    struct {
444	uint32_t    base;
445	uint32_t    limit;
446    } mem[2];
447
448};
449
450#endif /* PCIACCESS_H */
451