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_private.h
27 * Functions and datastructures that are private to the pciaccess library.
28 *
29 * \author Ian Romanick <idr@us.ibm.com>
30 */
31
32#ifndef PCIACCESS_PRIVATE_H
33#define PCIACCESS_PRIVATE_H
34
35#if defined(__GNUC__) && (__GNUC__ >= 4)
36# define _pci_hidden      __attribute__((visibility("hidden")))
37#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
38# define _pci_hidden      __hidden
39#else /* not gcc >= 4 and not Sun Studio >= 8 */
40# define _pci_hidden
41#endif /* GNUC >= 4 */
42
43/*
44 * O_CLOEXEC fixes an fd leak case (see 'man 2 open' for details). I don't
45 * know of any OS we support where this isn't available in a sufficiently
46 * new version, so warn unconditionally.
47 */
48#include <fcntl.h>
49
50#ifndef O_CLOEXEC
51#warning O_CLOEXEC not available, please upgrade.
52#define O_CLOEXEC 0
53#endif
54
55
56struct pci_device_mapping;
57
58int pci_fill_capabilities_generic( struct pci_device * dev );
59int pci_device_generic_unmap_range(struct pci_device *dev,
60    struct pci_device_mapping *map);
61
62struct pci_system_methods {
63    void (*destroy)( void );
64    void (*destroy_device)( struct pci_device * dev );
65    int (*read_rom)( struct pci_device * dev, void * buffer );
66    int (*probe)( struct pci_device * dev );
67    int (*map_range)(struct pci_device *dev, struct pci_device_mapping *map);
68    int (*unmap_range)(struct pci_device * dev,
69		       struct pci_device_mapping *map);
70
71    int (*read)(struct pci_device * dev, void * data, pciaddr_t offset,
72		pciaddr_t size, pciaddr_t * bytes_read );
73
74    int (*write)(struct pci_device * dev, const void * data, pciaddr_t offset,
75		pciaddr_t size, pciaddr_t * bytes_written );
76
77    int (*fill_capabilities)( struct pci_device * dev );
78    void (*enable)( struct pci_device *dev );
79    void (*disable)( struct pci_device *dev );
80    int (*boot_vga)( struct pci_device *dev );
81    int (*has_kernel_driver)( struct pci_device *dev );
82    struct pci_io_handle *(*open_device_io)( struct pci_io_handle *handle,
83					     struct pci_device *dev, int bar,
84					     pciaddr_t base, pciaddr_t size );
85    struct pci_io_handle *(*open_legacy_io)( struct pci_io_handle *handle,
86					     struct pci_device *dev,
87					     pciaddr_t base, pciaddr_t size );
88    void (*close_io)( struct pci_device *dev, struct pci_io_handle *handle );
89    uint32_t (*read32)( struct pci_io_handle *handle, uint32_t reg );
90    uint16_t (*read16)( struct pci_io_handle *handle, uint32_t reg );
91    uint8_t  (*read8)( struct pci_io_handle *handle, uint32_t reg );
92    void (*write32)( struct pci_io_handle *handle, uint32_t reg,
93		     uint32_t data );
94    void (*write16)( struct pci_io_handle *handle, uint32_t reg,
95		     uint16_t data );
96    void (*write8)( struct pci_io_handle *handle, uint32_t reg, uint8_t data );
97
98    int (*map_legacy)(struct pci_device *dev, pciaddr_t base, pciaddr_t size,
99		      unsigned map_flags, void **addr);
100    int (*unmap_legacy)(struct pci_device *dev, void *addr, pciaddr_t size);
101};
102
103struct pci_device_mapping {
104    pciaddr_t base;
105    pciaddr_t size;
106    unsigned region;
107    unsigned flags;
108    void *memory;
109};
110
111struct pci_io_handle {
112    pciaddr_t base;
113    pciaddr_t size;
114    void *memory;
115    int fd;
116    int is_legacy;
117};
118
119struct pci_device_private {
120    struct pci_device  base;
121    const char * device_string;
122
123    uint8_t header_type;
124
125    /**
126     * \name PCI Capabilities
127     */
128    /*@{*/
129    const struct pci_agp_info * agp;   /**< AGP capability information. */
130    /*@}*/
131
132    /**
133     * Base address of the device's expansion ROM.
134     */
135    pciaddr_t rom_base;
136
137    /**
138     * \name Bridge information.
139     */
140    /*@{*/
141    union {
142	struct pci_bridge_info * pci;
143	struct pci_pcmcia_bridge_info * pcmcia;
144    } bridge;
145    /*@}*/
146
147    /**
148     * \name Mappings active on this device.
149     */
150    /*@{*/
151    struct pci_device_mapping *mappings;
152    unsigned num_mappings;
153    /*@}*/
154#ifdef __sun
155    int is_primary;
156#endif
157#ifdef __GNU__
158    unsigned long device_port;
159#endif
160};
161
162
163/**
164 * Base type for tracking PCI subsystem information.
165 */
166struct pci_system {
167    /**
168     * Platform dependent implementations of specific API routines.
169     */
170    const struct pci_system_methods * methods;
171
172    /**
173     * Number of known devices in the system.
174     */
175    size_t num_devices;
176
177    /**
178     * Array of known devices.
179     */
180    struct pci_device_private * devices;
181
182#ifdef HAVE_MTRR
183    int mtrr_fd;
184#endif
185    int vgaarb_fd;
186    int vga_count;
187    struct pci_device *vga_target;
188    struct pci_device *vga_default_dev;
189};
190
191extern struct pci_system * pci_sys;
192
193extern int pci_system_linux_sysfs_create( void );
194extern int pci_system_freebsd_create( void );
195extern int pci_system_netbsd_create( void );
196extern int pci_system_openbsd_create( void );
197extern void pci_system_openbsd_init_dev_mem( int );
198extern int pci_system_solx_devfs_create( void );
199extern int pci_system_hurd_create( void );
200extern int pci_system_x86_create( void );
201extern void pci_io_cleanup( void );
202
203#endif /* PCIACCESS_PRIVATE_H */
204