pci_machdep.h revision 1.1 1 /* $NetBSD: pci_machdep.h,v 1.1 2025/01/01 17:53:08 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1996 Carnegie-Mellon University.
34 * All rights reserved.
35 *
36 * Author: Chris G. Demetriou
37 *
38 * Permission to use, copy, modify and distribute this software and
39 * its documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie the
56 * rights to redistribute these changes.
57 */
58
59 #ifndef _RISCV_PCI_MACHDEP_H_
60 #define _RISCV_PCI_MACHDEP_H_
61
62 /*
63 * Machine-specific definitions for PCI autoconfiguration.
64 */
65
66 #define __HAVE_PCI_GET_SEGMENT
67
68 #ifdef _LP64
69 #define _PCI_HAVE_DMA64
70 #endif
71
72 #include <sys/errno.h>
73
74 /*
75 * Types provided to machine-independent PCI code
76 */
77 typedef struct riscv_pci_chipset *pci_chipset_tag_t;
78 typedef u_long pcitag_t;
79 typedef uint64_t pci_intr_handle_t;
80
81 /*
82 * pci_intr_handle_t fields
83 */
84 #define RISCV_PCI_INTR_MSI_VEC __BITS(42, 32)
85 #define RISCV_PCI_INTR_MPSAFE __BIT(31)
86 #define RISCV_PCI_INTR_MSIX __BIT(30)
87 #define RISCV_PCI_INTR_MSI __BIT(29)
88 #define RISCV_PCI_INTR_FRAME __BITS(23, 16)
89 #define RISCV_PCI_INTR_IRQ __BITS(15, 0)
90
91 #ifdef __HAVE_PCI_MSI_MSIX
92 /*
93 * PCI MSI/MSI-X support
94 */
95 typedef enum {
96 PCI_INTR_TYPE_INTX = 0,
97 PCI_INTR_TYPE_MSI,
98 PCI_INTR_TYPE_MSIX,
99 PCI_INTR_TYPE_SIZE,
100 } pci_intr_type_t;
101 #endif /* __HAVE_PCI_MSI_MSIX */
102
103 /*
104 * Forward declarations.
105 */
106 struct pci_attach_args;
107
108 /*
109 * riscv-specific PCI structure and type definitions.
110 * NOT TO BE USED DIRECTLY BY MACHINE INDEPENDENT CODE.
111 */
112 struct riscv_pci_chipset {
113 void *pc_conf_v;
114 void (*pc_attach_hook)(device_t, device_t,
115 struct pcibus_attach_args *);
116 int (*pc_bus_maxdevs)(void *, int);
117 pcitag_t (*pc_make_tag)(void *, int, int, int);
118 void (*pc_decompose_tag)(void *, pcitag_t, int *,
119 int *, int *);
120 u_int (*pc_get_segment)(void *);
121 #if 0
122 // XXXNH devid?
123 uint32_t (*pc_get_devid)(void *, uint32_t);
124 #endif
125 uint32_t (*pc_get_frameid)(void *, uint32_t);
126 pcireg_t (*pc_conf_read)(void *, pcitag_t, int);
127 void (*pc_conf_write)(void *, pcitag_t, int, pcireg_t);
128
129 void *pc_intr_v;
130 int (*pc_intr_map)(const struct pci_attach_args *,
131 pci_intr_handle_t *);
132 const char *(*pc_intr_string)(void *, pci_intr_handle_t,
133 char *, size_t);
134 const struct evcnt *(*pc_intr_evcnt)(void *, pci_intr_handle_t);
135 int (*pc_intr_setattr)(void *, pci_intr_handle_t *,
136 int, uint64_t);
137 void *(*pc_intr_establish)(void *, pci_intr_handle_t,
138 int, int (*)(void *), void *, const char *);
139 void (*pc_intr_disestablish)(void *, void *);
140
141 #ifdef __HAVE_PCI_CONF_HOOK
142 int (*pc_conf_hook)(void *, int, int, int, pcireg_t);
143 #endif
144 void (*pc_conf_interrupt)(void *, int, int, int, int, int *);
145
146 #ifdef __HAVE_PCI_MSI_MSIX
147 void *pc_msi_v;
148 pci_intr_type_t (*pc_intr_type)(void *, pci_intr_handle_t);
149 int (*pc_intr_alloc)(const struct pci_attach_args *,
150 pci_intr_handle_t **, int *, pci_intr_type_t);
151 void (*pc_intr_release)(void *, pci_intr_handle_t *, int);
152 int (*pc_intx_alloc)(const struct pci_attach_args *,
153 pci_intr_handle_t **);
154 int (*pc_msi_alloc)(const struct pci_attach_args *,
155 pci_intr_handle_t **, int *);
156 int (*pc_msi_alloc_exact)(const struct pci_attach_args *,
157 pci_intr_handle_t **, int);
158 int (*pc_msix_alloc)(const struct pci_attach_args *,
159 pci_intr_handle_t **, int *);
160 int (*pc_msix_alloc_exact)(const struct pci_attach_args *,
161 pci_intr_handle_t **, int);
162 int (*pc_msix_alloc_map)(const struct pci_attach_args *,
163 pci_intr_handle_t **, u_int *, int);
164 #endif
165
166 uint32_t pc_cfg_cmd;
167 };
168
169 /*
170 * Functions provided to machine-independent PCI code.
171 */
172 //XXXNH static inlines..
173 #define pci_attach_hook(p, s, pba) \
174 (*(pba)->pba_pc->pc_attach_hook)((p), (s), (pba))
175 #define pci_bus_maxdevs(c, b) \
176 (*(c)->pc_bus_maxdevs)((c)->pc_conf_v, (b))
177 #define pci_make_tag(c, b, d, f) \
178 (*(c)->pc_make_tag)((c)->pc_conf_v, (b), (d), (f))
179 #define pci_decompose_tag(c, t, bp, dp, fp) \
180 (*(c)->pc_decompose_tag)((c)->pc_conf_v, (t), (bp), (dp), (fp))
181 #define pci_get_segment(c) \
182 ((c)->pc_get_segment ? (*(c)->pc_get_segment)((c)->pc_conf_v) : 0)
183 #define pci_get_devid(c, d) \
184 ((c)->pc_get_devid ? (*(c)->pc_get_devid)((c)->pc_conf_v, (d)) : (d))
185 #define pci_get_frameid(c, d) \
186 ((c)->pc_get_frameid ? (*(c)->pc_get_frameid)((c)->pc_conf_v, (d)) : 0)
187 #define pci_conf_read(c, t, r) \
188 (*(c)->pc_conf_read)((c)->pc_conf_v, (t), (r))
189 #define pci_conf_write(c, t, r, v) \
190 (*(c)->pc_conf_write)((c)->pc_conf_v, (t), (r), (v))
191 #define pci_intr_map(pa, ihp) \
192 (*(pa)->pa_pc->pc_intr_map)((pa), (ihp))
193 #define pci_intr_string(c, ih, buf, len) \
194 (*(c)->pc_intr_string)((c)->pc_intr_v, (ih), (buf), (len))
195 #define pci_intr_evcnt(c, ih) \
196 (*(c)->pc_intr_evcnt)((c)->pc_intr_v, (ih))
197 #define pci_intr_establish(c, ih, l, h, a) \
198 (*(c)->pc_intr_establish)((c)->pc_intr_v, (ih), (l), (h), (a), NULL)
199 #define pci_intr_disestablish(c, iv) \
200 (*(c)->pc_intr_disestablish)((c)->pc_intr_v, (iv))
201 #ifdef __HAVE_PCI_CONF_HOOK
202 #define pci_conf_hook(c, b, d, f, id) \
203 (*(c)->pc_conf_hook)((c)->pc_conf_v, (b), (d), (f), (id))
204 #endif
205 #define pci_conf_interrupt(c, b, d, i, s, p) \
206 (*(c)->pc_conf_interrupt)((c)->pc_conf_v, (b), (d), (i), (s), (p))
207
208 static inline int
209 pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ihp,
210 int attr, uint64_t data)
211 {
212 if (!pc->pc_intr_setattr)
213 return ENODEV;
214 return pc->pc_intr_setattr(pc, ihp, attr, data);
215 }
216
217 static inline void *
218 pci_intr_establish_xname(pci_chipset_tag_t pc, pci_intr_handle_t ih,
219 int level, int (*fn)(void *), void *arg, const char *xname)
220 {
221 return pc->pc_intr_establish(pc->pc_intr_v, ih, level, fn, arg, xname);
222 }
223
224 #ifdef __HAVE_PCI_MSI_MSIX
225 pci_intr_type_t
226 pci_intr_type(pci_chipset_tag_t, pci_intr_handle_t);
227 int pci_intr_alloc(const struct pci_attach_args *, pci_intr_handle_t **,
228 int *, pci_intr_type_t);
229 void pci_intr_release(pci_chipset_tag_t, pci_intr_handle_t *, int);
230 int pci_intx_alloc(const struct pci_attach_args *, pci_intr_handle_t **);
231 int pci_msi_alloc(const struct pci_attach_args *, pci_intr_handle_t **,
232 int *);
233 int pci_msi_alloc_exact(const struct pci_attach_args *,
234 pci_intr_handle_t **, int);
235 int pci_msix_alloc(const struct pci_attach_args *, pci_intr_handle_t **,
236 int *);
237 int pci_msix_alloc_exact(const struct pci_attach_args *,
238 pci_intr_handle_t **, int);
239 int pci_msix_alloc_map(const struct pci_attach_args *, pci_intr_handle_t **,
240 u_int *, int);
241 #endif /* __HAVE_PCI_MSI_MSIX */
242
243 #endif /* _RISCV_PCI_MACHDEP_H_ */
244