b353b319ed512707930f3001cfde0b79a329afc1
[platform/upstream/libpciaccess.git] / src / pci_tools.h
1 /*
2  * Copyright © 2007 Sun Microsystems, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 #ifndef _SYS_PCI_TOOLS_H
24 #define _SYS_PCI_TOOLS_H
25
26 #pragma ident   "@(#)pci_tools.h        1.4     05/09/28 SMI"
27
28 #include <sys/modctl.h>
29
30 #ifdef  __cplusplus
31 extern "C" {
32 #endif
33
34 /*
35  * Versioning. Have different versions for userland program and drivers, so
36  * they can all stay in sync with each other.
37  */
38 #define PCITOOL_USER_VERSION    1
39 #define PCITOOL_DRVR_VERSION    1
40
41 /* File suffixes for nexus pcitool nodes. */
42 #define PCI_MINOR_REG   "reg"
43 #define PCI_MINOR_INTR  "intr"
44
45 /*
46  * Ioctls for PCI tools.
47  */
48 #define PCITOOL_IOC             (('P' << 24) | ('C' << 16) | ('T' << 8))
49
50 /* Read/write a device on a PCI bus, in physical space. */
51 #define PCITOOL_DEVICE_GET_REG  (PCITOOL_IOC | 1)
52 #define PCITOOL_DEVICE_SET_REG  (PCITOOL_IOC | 2)
53
54 /* Read/write the PCI nexus bridge, in physical space. */
55 #define PCITOOL_NEXUS_GET_REG   (PCITOOL_IOC | 3)
56 #define PCITOOL_NEXUS_SET_REG   (PCITOOL_IOC | 4)
57
58 /* Get/set interrupt-CPU mapping for PCI devices. */
59 #define PCITOOL_DEVICE_GET_INTR (PCITOOL_IOC | 5)
60 #define PCITOOL_DEVICE_SET_INTR (PCITOOL_IOC | 6)
61
62 /* Return the number of supported interrupts on a PCI bus. */
63 #define PCITOOL_DEVICE_NUM_INTR (PCITOOL_IOC | 7)
64
65
66 /*
67  * This file contains data structures for the pci tool.
68  */
69 #define PCITOOL_CONFIG  0
70 #define PCITOOL_BAR0    1
71 #define PCITOOL_BAR1    2
72 #define PCITOOL_BAR2    3
73 #define PCITOOL_BAR3    4
74 #define PCITOOL_BAR4    5
75 #define PCITOOL_BAR5    6
76 #define PCITOOL_ROM     7
77
78 /*
79  * Pass this through barnum to signal to use a base addr instead.
80  * This is for platforms which do not have a way to automatically map
81  * a selected bank to a base addr.
82  */
83 #define PCITOOL_BASE    0xFF
84
85 /*
86  * BAR corresponding to space desired.
87  */
88 typedef enum {
89     config = PCITOOL_CONFIG,
90     bar0 = PCITOOL_BAR0,
91     bar1 = PCITOOL_BAR1,
92     bar2 = PCITOOL_BAR2,
93     bar3 = PCITOOL_BAR3,
94     bar4 = PCITOOL_BAR4,
95     bar5 = PCITOOL_BAR5,
96     rom = PCITOOL_ROM
97 } pcitool_bars_t;
98
99
100 /*
101  * PCITOOL error numbers.
102  */
103
104 typedef enum {
105         PCITOOL_SUCCESS = 0x0,
106         PCITOOL_INVALID_CPUID,
107         PCITOOL_INVALID_INO,
108         PCITOOL_PENDING_INTRTIMEOUT,
109         PCITOOL_REGPROP_NOTWELLFORMED,
110         PCITOOL_INVALID_ADDRESS,
111         PCITOOL_NOT_ALIGNED,
112         PCITOOL_OUT_OF_RANGE,
113         PCITOOL_END_OF_RANGE,
114         PCITOOL_ROM_DISABLED,
115         PCITOOL_ROM_WRITE,
116         PCITOOL_IO_ERROR,
117         PCITOOL_INVALID_SIZE
118 } pcitool_errno_t;
119
120
121 /*
122  * PCITOOL_DEVICE_SET_INTR ioctl data structure to re-assign the interrupts.
123  */
124 typedef struct pcitool_intr_set {
125         uint16_t user_version;  /* Userland program version - to krnl */
126         uint16_t drvr_version;  /* Driver version - from kernel */
127         uint32_t ino;           /* interrupt to set - to kernel */
128         uint32_t cpu_id;        /* to: cpu to set / from: old cpu returned */
129         pcitool_errno_t status; /* from kernel */
130 } pcitool_intr_set_t;
131
132
133 /*
134  * PCITOOL_DEVICE_GET_INTR ioctl data structure to dump out the
135  * ino mapping information.
136  */
137
138 typedef struct pcitool_intr_dev {
139         uint32_t        dev_inst;       /* device instance - from kernel */
140         char            driver_name[MAXMODCONFNAME];    /* from kernel */
141         char            path[MAXPATHLEN]; /* device path - from kernel */
142 } pcitool_intr_dev_t;
143
144
145 typedef struct pcitool_intr_get {
146         uint16_t user_version;          /* Userland program version - to krnl */
147         uint16_t drvr_version;          /* Driver version - from kernel */
148         uint32_t        ino;            /* interrupt number - to kernel */
149         uint8_t         num_devs_ret;   /* room for this # of devs to be */
150                                         /* returned - to kernel */
151                                         /* # devs returned - from kernel */
152         uint8_t         num_devs;       /* # devs on this ino - from kernel */
153                                         /* intrs enabled for devs if > 0 */
154         uint8_t         ctlr;           /* controller number - from kernel */
155         uint32_t        cpu_id;         /* cpu of interrupt - from kernel */
156         pcitool_errno_t status;         /* returned status - from kernel */
157         pcitool_intr_dev_t      dev[1]; /* start of variable device list */
158                                         /* from kernel */
159 } pcitool_intr_get_t;
160
161 /*
162  * Get the size needed to return the number of devices wanted.
163  * Can't say num_devs - 1 as num_devs may be unsigned.
164  */
165 #define PCITOOL_IGET_SIZE(num_devs) \
166         (sizeof (pcitool_intr_get_t) - \
167         sizeof (pcitool_intr_dev_t) + \
168         (num_devs * sizeof (pcitool_intr_dev_t)))
169
170 /*
171  * Size and endian fields for acc_attr bitmask.
172  */
173 #define PCITOOL_ACC_ATTR_SIZE_MASK      0x3
174 #define PCITOOL_ACC_ATTR_SIZE_1         0x0
175 #define PCITOOL_ACC_ATTR_SIZE_2         0x1
176 #define PCITOOL_ACC_ATTR_SIZE_4         0x2
177 #define PCITOOL_ACC_ATTR_SIZE_8         0x3
178 #define PCITOOL_ACC_ATTR_SIZE(x)        (1 << (x & PCITOOL_ACC_ATTR_SIZE_MASK))
179
180 #define PCITOOL_ACC_ATTR_ENDN_MASK      0x100
181 #define PCITOOL_ACC_ATTR_ENDN_LTL       0x0
182 #define PCITOOL_ACC_ATTR_ENDN_BIG       0x100
183 #define PCITOOL_ACC_IS_BIG_ENDIAN(x)    (x & PCITOOL_ACC_ATTR_ENDN_BIG)
184
185 /*
186  * Data structure to read and write to pci device registers.
187  * This is the argument to the following ioctls:
188  *      PCITOOL_DEVICE_SET/GET_REG
189  *      PCITOOL_NEXUS_SET/GET_REG
190  */
191 typedef struct pcitool_reg {
192         uint16_t        user_version;   /* Userland program version - to krnl */
193         uint16_t        drvr_version;   /* Driver version - from kernel */
194         uint8_t         bus_no;         /* pci bus - to kernel */
195         uint8_t         dev_no;         /* pci dev - to kernel */
196         uint8_t         func_no;        /* pci function - to kernel */
197         uint8_t         barnum;         /* bank (DEVCTL_NEXUS_SET/GET_REG) or */
198                                         /*   BAR from pcitools_bar_t */
199                                         /*   (DEVCTL_DEVICE_SET/GET_REG) */
200                                         /*   to kernel */
201         uint64_t        offset;         /* to kernel */
202         uint32_t        acc_attr;       /* access attributes - to kernel */
203         uint32_t        padding1;       /* 8-byte align next uint64_t for X86 */
204         uint64_t        data;           /* to/from kernel, 64-bit alignment */
205         uint32_t        status;         /* from kernel */
206         uint32_t        padding2;       /* 8-byte align next uint64_t for X86 */
207         uint64_t        phys_addr;      /* from kernel, 64-bit alignment */
208 } pcitool_reg_t;
209
210
211 #ifdef  __cplusplus
212 }
213 #endif
214
215 #endif  /* _SYS_PCI_TOOLS_H */