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