pci.h revision 1.3 1 1.3 riastrad /* $NetBSD: pci.h,v 1.3 2014/04/03 19:18:29 riastradh Exp $ */
2 1.2 riastrad
3 1.2 riastrad /*-
4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2 riastrad * All rights reserved.
6 1.2 riastrad *
7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 riastrad * by Taylor R. Campbell.
9 1.2 riastrad *
10 1.2 riastrad * Redistribution and use in source and binary forms, with or without
11 1.2 riastrad * modification, are permitted provided that the following conditions
12 1.2 riastrad * are met:
13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.2 riastrad * notice, this list of conditions and the following disclaimer.
15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.2 riastrad * documentation and/or other materials provided with the distribution.
18 1.2 riastrad *
19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 riastrad */
31 1.2 riastrad
32 1.2 riastrad #ifndef _LINUX_PCI_H_
33 1.2 riastrad #define _LINUX_PCI_H_
34 1.2 riastrad
35 1.2 riastrad #include <sys/types.h>
36 1.2 riastrad #include <sys/bus.h>
37 1.3 riastrad #include <sys/cdefs.h>
38 1.2 riastrad #include <sys/kmem.h>
39 1.2 riastrad #include <sys/systm.h>
40 1.2 riastrad
41 1.2 riastrad #include <dev/pci/pcidevs.h>
42 1.2 riastrad #include <dev/pci/pcireg.h>
43 1.2 riastrad #include <dev/pci/pcivar.h>
44 1.2 riastrad
45 1.2 riastrad #include <linux/ioport.h>
46 1.2 riastrad
47 1.2 riastrad struct pci_bus;
48 1.2 riastrad
49 1.2 riastrad struct pci_device_id {
50 1.2 riastrad uint32_t vendor;
51 1.2 riastrad uint32_t device;
52 1.2 riastrad uint32_t subvendor;
53 1.2 riastrad uint32_t subdevice;
54 1.2 riastrad uint32_t class;
55 1.2 riastrad uint32_t class_mask;
56 1.2 riastrad unsigned long driver_data;
57 1.2 riastrad };
58 1.2 riastrad
59 1.2 riastrad #define PCI_ANY_ID ((pcireg_t)-1)
60 1.2 riastrad
61 1.2 riastrad #define PCI_BASE_CLASS_DISPLAY PCI_CLASS_DISPLAY
62 1.2 riastrad
63 1.2 riastrad #define PCI_CLASS_BRIDGE_ISA \
64 1.2 riastrad ((PCI_CLASS_BRIDGE << 8) | PCI_SUBCLASS_BRIDGE_ISA)
65 1.2 riastrad CTASSERT(PCI_CLASS_BRIDGE_ISA == 0x0601);
66 1.2 riastrad
67 1.2 riastrad #define PCI_VENDOR_ID_INTEL PCI_VENDOR_INTEL
68 1.2 riastrad
69 1.2 riastrad #define PCI_DEVFN(DEV, FN) \
70 1.2 riastrad (__SHIFTIN((DEV), __BITS(3, 7)) | __SHIFTIN((FN), __BITS(0, 2)))
71 1.2 riastrad #define PCI_SLOT(DEVFN) __SHIFTOUT((DEVFN), __BITS(3, 7))
72 1.2 riastrad #define PCI_FUNC(DEVFN) __SHIFTOUT((DEVFN), __BITS(0, 2))
73 1.2 riastrad
74 1.2 riastrad #define PCI_CAP_ID_AGP PCI_CAP_AGP
75 1.2 riastrad
76 1.2 riastrad struct pci_dev {
77 1.2 riastrad struct pci_attach_args pd_pa;
78 1.2 riastrad int pd_kludges; /* Gotta lose 'em... */
79 1.2 riastrad #define NBPCI_KLUDGE_GET_MUMBLE 0x01
80 1.2 riastrad #define NBPCI_KLUDGE_MAP_ROM 0x02
81 1.2 riastrad bus_space_tag_t pd_rom_bst;
82 1.2 riastrad bus_space_handle_t pd_rom_bsh;
83 1.2 riastrad bus_size_t pd_rom_size;
84 1.2 riastrad void *pd_rom_vaddr;
85 1.2 riastrad device_t pd_dev;
86 1.2 riastrad struct device dev; /* XXX Don't believe me! */
87 1.2 riastrad struct pci_bus *bus;
88 1.2 riastrad uint32_t devfn;
89 1.2 riastrad uint16_t vendor;
90 1.2 riastrad uint16_t device;
91 1.2 riastrad uint16_t subsystem_vendor;
92 1.2 riastrad uint16_t subsystem_device;
93 1.2 riastrad uint8_t revision;
94 1.2 riastrad uint32_t class;
95 1.2 riastrad bool msi_enabled;
96 1.2 riastrad };
97 1.2 riastrad
98 1.2 riastrad static inline device_t
99 1.2 riastrad pci_dev_dev(struct pci_dev *pdev)
100 1.2 riastrad {
101 1.2 riastrad return pdev->pd_dev;
102 1.2 riastrad }
103 1.2 riastrad
104 1.2 riastrad static inline void
105 1.2 riastrad linux_pci_dev_init(struct pci_dev *pdev, device_t dev,
106 1.2 riastrad const struct pci_attach_args *pa, int kludges)
107 1.2 riastrad {
108 1.2 riastrad const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag,
109 1.2 riastrad PCI_SUBSYS_ID_REG);
110 1.2 riastrad
111 1.2 riastrad pdev->pd_pa = *pa;
112 1.2 riastrad pdev->pd_kludges = kludges;
113 1.2 riastrad pdev->pd_rom_vaddr = NULL;
114 1.2 riastrad pdev->pd_dev = dev;
115 1.2 riastrad pdev->bus = NULL; /* XXX struct pci_dev::bus */
116 1.2 riastrad pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
117 1.2 riastrad pdev->vendor = PCI_VENDOR(pa->pa_id);
118 1.2 riastrad pdev->device = PCI_PRODUCT(pa->pa_id);
119 1.2 riastrad pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id);
120 1.2 riastrad pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id);
121 1.2 riastrad pdev->revision = PCI_REVISION(pa->pa_class);
122 1.2 riastrad pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */
123 1.2 riastrad pdev->msi_enabled = false;
124 1.2 riastrad }
125 1.2 riastrad
126 1.2 riastrad static inline int
127 1.2 riastrad pci_find_capability(struct pci_dev *pdev, int cap)
128 1.2 riastrad {
129 1.2 riastrad return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
130 1.2 riastrad NULL, NULL);
131 1.2 riastrad }
132 1.2 riastrad
133 1.2 riastrad static inline void
134 1.2 riastrad pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
135 1.2 riastrad {
136 1.2 riastrad KASSERT(!ISSET(reg, 3));
137 1.2 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
138 1.2 riastrad }
139 1.2 riastrad
140 1.2 riastrad static inline void
141 1.2 riastrad pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
142 1.2 riastrad {
143 1.2 riastrad KASSERT(!ISSET(reg, 1));
144 1.2 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
145 1.2 riastrad (reg &~ 3)) >> (8 * (reg & 3));
146 1.2 riastrad }
147 1.2 riastrad
148 1.2 riastrad static inline void
149 1.2 riastrad pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep)
150 1.2 riastrad {
151 1.2 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
152 1.2 riastrad (reg &~ 1)) >> (8 * (reg & 1));
153 1.2 riastrad }
154 1.2 riastrad
155 1.2 riastrad static inline void
156 1.2 riastrad pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
157 1.2 riastrad {
158 1.2 riastrad KASSERT(!ISSET(reg, 3));
159 1.2 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
160 1.2 riastrad }
161 1.2 riastrad
162 1.2 riastrad static inline void
163 1.2 riastrad pci_rmw_config(struct pci_dev *pdev, int reg, unsigned int bytes,
164 1.2 riastrad uint32_t value)
165 1.2 riastrad {
166 1.2 riastrad const uint32_t mask = ~((~0UL) << (8 * bytes));
167 1.2 riastrad const int reg32 = (reg &~ 3);
168 1.2 riastrad const unsigned int shift = (8 * (reg & 3));
169 1.2 riastrad uint32_t value32;
170 1.2 riastrad
171 1.2 riastrad KASSERT(bytes <= 4);
172 1.2 riastrad KASSERT(!ISSET(value, ~mask));
173 1.2 riastrad pci_read_config_dword(pdev, reg32, &value32);
174 1.2 riastrad value32 &=~ (mask << shift);
175 1.2 riastrad value32 |= (value << shift);
176 1.2 riastrad pci_write_config_dword(pdev, reg32, value32);
177 1.2 riastrad }
178 1.2 riastrad
179 1.2 riastrad static inline void
180 1.2 riastrad pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value)
181 1.2 riastrad {
182 1.2 riastrad KASSERT(!ISSET(reg, 1));
183 1.2 riastrad pci_rmw_config(pdev, reg, 2, value);
184 1.2 riastrad }
185 1.2 riastrad
186 1.2 riastrad static inline void
187 1.2 riastrad pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value)
188 1.2 riastrad {
189 1.2 riastrad pci_rmw_config(pdev, reg, 1, value);
190 1.2 riastrad }
191 1.2 riastrad
192 1.2 riastrad /*
193 1.2 riastrad * XXX pci msi
194 1.2 riastrad */
195 1.2 riastrad static inline void
196 1.2 riastrad pci_enable_msi(struct pci_dev *pdev)
197 1.2 riastrad {
198 1.2 riastrad KASSERT(!pdev->msi_enabled);
199 1.2 riastrad pdev->msi_enabled = true;
200 1.2 riastrad }
201 1.2 riastrad
202 1.2 riastrad static inline void
203 1.2 riastrad pci_disable_msi(struct pci_dev *pdev)
204 1.2 riastrad {
205 1.2 riastrad KASSERT(pdev->msi_enabled);
206 1.2 riastrad pdev->msi_enabled = false;
207 1.2 riastrad }
208 1.2 riastrad
209 1.2 riastrad static inline void
210 1.2 riastrad pci_set_master(struct pci_dev *pdev)
211 1.2 riastrad {
212 1.2 riastrad pcireg_t csr;
213 1.2 riastrad
214 1.2 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
215 1.2 riastrad PCI_COMMAND_STATUS_REG);
216 1.2 riastrad csr |= PCI_COMMAND_MASTER_ENABLE;
217 1.2 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
218 1.2 riastrad PCI_COMMAND_STATUS_REG, csr);
219 1.2 riastrad }
220 1.2 riastrad
221 1.2 riastrad #define PCIBIOS_MIN_MEM 0 /* XXX bogus x86 kludge bollocks */
222 1.2 riastrad
223 1.2 riastrad static inline bus_addr_t
224 1.2 riastrad pcibios_align_resource(void *p, const struct resource *resource,
225 1.2 riastrad bus_addr_t addr, bus_size_t size)
226 1.2 riastrad {
227 1.2 riastrad panic("pcibios_align_resource has accessed unaligned neurons!");
228 1.2 riastrad }
229 1.2 riastrad
230 1.2 riastrad static inline int
231 1.2 riastrad pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
232 1.2 riastrad bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
233 1.2 riastrad bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
234 1.2 riastrad bus_size_t) __unused,
235 1.2 riastrad struct pci_dev *pdev)
236 1.2 riastrad {
237 1.2 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
238 1.2 riastrad bus_space_tag_t bst;
239 1.2 riastrad int error;
240 1.2 riastrad
241 1.2 riastrad switch (resource->flags) {
242 1.2 riastrad case IORESOURCE_MEM:
243 1.2 riastrad bst = pa->pa_memt;
244 1.2 riastrad break;
245 1.2 riastrad
246 1.2 riastrad case IORESOURCE_IO:
247 1.2 riastrad bst = pa->pa_iot;
248 1.2 riastrad break;
249 1.2 riastrad
250 1.2 riastrad default:
251 1.2 riastrad panic("I don't know what kind of resource you want!");
252 1.2 riastrad }
253 1.2 riastrad
254 1.2 riastrad resource->r_bst = bst;
255 1.3 riastrad error = bus_space_alloc(bst, start, __type_max(bus_addr_t),
256 1.2 riastrad size, align, 0, 0, &resource->start, &resource->r_bsh);
257 1.2 riastrad if (error)
258 1.2 riastrad return error;
259 1.2 riastrad
260 1.2 riastrad resource->size = size;
261 1.2 riastrad return 0;
262 1.2 riastrad }
263 1.2 riastrad
264 1.2 riastrad /*
265 1.2 riastrad * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are
266 1.2 riastrad * defined only for their single purposes in i915drm, in
267 1.2 riastrad * i915_get_bridge_dev and intel_detect_pch. We can't define them more
268 1.2 riastrad * generally without adapting pci_find_device (and pci_enumerate_bus
269 1.2 riastrad * internally) to pass a cookie through.
270 1.2 riastrad */
271 1.2 riastrad
272 1.2 riastrad static inline int /* XXX inline? */
273 1.2 riastrad pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa)
274 1.2 riastrad {
275 1.2 riastrad
276 1.2 riastrad if (pa->pa_bus != 0)
277 1.2 riastrad return 0;
278 1.2 riastrad if (pa->pa_device != 0)
279 1.2 riastrad return 0;
280 1.2 riastrad if (pa->pa_function != 0)
281 1.2 riastrad return 0;
282 1.2 riastrad
283 1.2 riastrad return 1;
284 1.2 riastrad }
285 1.2 riastrad
286 1.2 riastrad static inline struct pci_dev *
287 1.2 riastrad pci_get_bus_and_slot(int bus, int slot)
288 1.2 riastrad {
289 1.2 riastrad struct pci_attach_args pa;
290 1.2 riastrad
291 1.2 riastrad KASSERT(bus == 0);
292 1.2 riastrad KASSERT(slot == PCI_DEVFN(0, 0));
293 1.2 riastrad
294 1.2 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0))
295 1.2 riastrad return NULL;
296 1.2 riastrad
297 1.2 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
298 1.2 riastrad linux_pci_dev_init(pdev, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
299 1.2 riastrad
300 1.2 riastrad return pdev;
301 1.2 riastrad }
302 1.2 riastrad
303 1.2 riastrad static inline int /* XXX inline? */
304 1.2 riastrad pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa)
305 1.2 riastrad {
306 1.2 riastrad
307 1.2 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
308 1.2 riastrad return 0;
309 1.2 riastrad if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
310 1.2 riastrad return 0;
311 1.2 riastrad
312 1.2 riastrad return 1;
313 1.2 riastrad }
314 1.2 riastrad
315 1.2 riastrad static inline struct pci_dev *
316 1.2 riastrad pci_get_class(uint32_t class_subclass_shifted __unused,
317 1.2 riastrad struct pci_dev *from __unused)
318 1.2 riastrad {
319 1.2 riastrad struct pci_attach_args pa;
320 1.2 riastrad
321 1.2 riastrad KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8));
322 1.2 riastrad KASSERT(from == NULL);
323 1.2 riastrad
324 1.2 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge))
325 1.2 riastrad return NULL;
326 1.2 riastrad
327 1.2 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
328 1.2 riastrad linux_pci_dev_init(pdev, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
329 1.2 riastrad
330 1.2 riastrad return pdev;
331 1.2 riastrad }
332 1.2 riastrad
333 1.2 riastrad static inline void
334 1.2 riastrad pci_dev_put(struct pci_dev *pdev)
335 1.2 riastrad {
336 1.2 riastrad
337 1.2 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE));
338 1.2 riastrad kmem_free(pdev, sizeof(*pdev));
339 1.2 riastrad }
340 1.2 riastrad
341 1.2 riastrad #define __pci_rom_iomem
342 1.2 riastrad
343 1.2 riastrad static inline void
344 1.2 riastrad pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused)
345 1.2 riastrad {
346 1.2 riastrad
347 1.2 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
348 1.2 riastrad KASSERT(vaddr == pdev->pd_rom_vaddr);
349 1.2 riastrad bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size);
350 1.2 riastrad pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM;
351 1.2 riastrad pdev->pd_rom_vaddr = NULL;
352 1.2 riastrad }
353 1.2 riastrad
354 1.2 riastrad static inline void __pci_rom_iomem *
355 1.2 riastrad pci_map_rom(struct pci_dev *pdev, size_t *sizep)
356 1.2 riastrad {
357 1.2 riastrad bus_space_handle_t bsh;
358 1.2 riastrad bus_size_t size;
359 1.2 riastrad
360 1.2 riastrad KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
361 1.2 riastrad
362 1.2 riastrad if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
363 1.2 riastrad (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR),
364 1.2 riastrad &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size)
365 1.2 riastrad != 0) {
366 1.2 riastrad aprint_error_dev(pdev->pd_dev, "unable to map ROM\n");
367 1.2 riastrad return NULL;
368 1.2 riastrad }
369 1.2 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
370 1.2 riastrad
371 1.2 riastrad /* XXX This type is obviously wrong in general... */
372 1.2 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
373 1.2 riastrad PCI_ROM_CODE_TYPE_X86, &bsh, &size)) {
374 1.2 riastrad aprint_error_dev(pdev->pd_dev, "unable to find ROM\n");
375 1.2 riastrad pci_unmap_rom(pdev, NULL);
376 1.2 riastrad return NULL;
377 1.2 riastrad }
378 1.2 riastrad
379 1.2 riastrad KASSERT(size <= SIZE_T_MAX);
380 1.2 riastrad *sizep = size;
381 1.2 riastrad pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, bsh);
382 1.2 riastrad return pdev->pd_rom_vaddr;
383 1.2 riastrad }
384 1.2 riastrad
385 1.2 riastrad #endif /* _LINUX_PCI_H_ */
386