pci.h revision 1.7.2.3 1 1.7.2.3 martin /* $NetBSD: pci.h,v 1.7.2.3 2014/11/11 09:06:32 martin 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.7.2.3 martin #if defined(i386) || defined(amd64)
36 1.7.2.3 martin #include "acpica.h"
37 1.7.2.3 martin #else /* !(i386 || amd64) */
38 1.7.2.3 martin #define NACPICA 0
39 1.7.2.3 martin #endif /* i386 || amd64 */
40 1.7.2.3 martin
41 1.2 riastrad #include <sys/types.h>
42 1.4 riastrad #include <sys/param.h>
43 1.2 riastrad #include <sys/bus.h>
44 1.3 riastrad #include <sys/cdefs.h>
45 1.2 riastrad #include <sys/kmem.h>
46 1.2 riastrad #include <sys/systm.h>
47 1.2 riastrad
48 1.4 riastrad #include <machine/limits.h>
49 1.4 riastrad
50 1.2 riastrad #include <dev/pci/pcidevs.h>
51 1.2 riastrad #include <dev/pci/pcireg.h>
52 1.2 riastrad #include <dev/pci/pcivar.h>
53 1.4 riastrad #include <dev/pci/agpvar.h>
54 1.2 riastrad
55 1.7.2.3 martin #include <dev/acpi/acpivar.h>
56 1.7.2.3 martin #include <dev/acpi/acpi_pci.h>
57 1.7.2.3 martin
58 1.7 riastrad #include <linux/dma-mapping.h>
59 1.2 riastrad #include <linux/ioport.h>
60 1.2 riastrad
61 1.7.2.3 martin struct pci_bus {
62 1.7.2.3 martin u_int number;
63 1.7.2.3 martin };
64 1.2 riastrad
65 1.2 riastrad struct pci_device_id {
66 1.2 riastrad uint32_t vendor;
67 1.2 riastrad uint32_t device;
68 1.2 riastrad uint32_t subvendor;
69 1.2 riastrad uint32_t subdevice;
70 1.2 riastrad uint32_t class;
71 1.2 riastrad uint32_t class_mask;
72 1.2 riastrad unsigned long driver_data;
73 1.2 riastrad };
74 1.2 riastrad
75 1.2 riastrad #define PCI_ANY_ID ((pcireg_t)-1)
76 1.2 riastrad
77 1.2 riastrad #define PCI_BASE_CLASS_DISPLAY PCI_CLASS_DISPLAY
78 1.2 riastrad
79 1.2 riastrad #define PCI_CLASS_BRIDGE_ISA \
80 1.2 riastrad ((PCI_CLASS_BRIDGE << 8) | PCI_SUBCLASS_BRIDGE_ISA)
81 1.2 riastrad CTASSERT(PCI_CLASS_BRIDGE_ISA == 0x0601);
82 1.2 riastrad
83 1.5 riastrad /* XXX This is getting silly... */
84 1.5 riastrad #define PCI_VENDOR_ID_ASUSTEK PCI_VENDOR_ASUSTEK
85 1.5 riastrad #define PCI_VENDOR_ID_ATI PCI_VENDOR_ATI
86 1.5 riastrad #define PCI_VENDOR_ID_DELL PCI_VENDOR_DELL
87 1.5 riastrad #define PCI_VENDOR_ID_IBM PCI_VENDOR_IBM
88 1.5 riastrad #define PCI_VENDOR_ID_HP PCI_VENDOR_HP
89 1.2 riastrad #define PCI_VENDOR_ID_INTEL PCI_VENDOR_INTEL
90 1.7 riastrad #define PCI_VENDOR_ID_NVIDIA PCI_VENDOR_NVIDIA
91 1.5 riastrad #define PCI_VENDOR_ID_SONY PCI_VENDOR_SONY
92 1.5 riastrad #define PCI_VENDOR_ID_VIA PCI_VENDOR_VIATECH
93 1.5 riastrad
94 1.5 riastrad #define PCI_DEVICE_ID_ATI_RADEON_QY PCI_PRODUCT_ATI_RADEON_RV100_QY
95 1.2 riastrad
96 1.2 riastrad #define PCI_DEVFN(DEV, FN) \
97 1.2 riastrad (__SHIFTIN((DEV), __BITS(3, 7)) | __SHIFTIN((FN), __BITS(0, 2)))
98 1.2 riastrad #define PCI_SLOT(DEVFN) __SHIFTOUT((DEVFN), __BITS(3, 7))
99 1.2 riastrad #define PCI_FUNC(DEVFN) __SHIFTOUT((DEVFN), __BITS(0, 2))
100 1.2 riastrad
101 1.4 riastrad #define PCI_NUM_RESOURCES ((PCI_MAPREG_END - PCI_MAPREG_START) / 4)
102 1.5 riastrad #define DEVICE_COUNT_RESOURCE PCI_NUM_RESOURCES
103 1.4 riastrad
104 1.2 riastrad #define PCI_CAP_ID_AGP PCI_CAP_AGP
105 1.2 riastrad
106 1.4 riastrad typedef int pci_power_t;
107 1.4 riastrad
108 1.4 riastrad #define PCI_D0 0
109 1.4 riastrad #define PCI_D1 1
110 1.4 riastrad #define PCI_D2 2
111 1.4 riastrad #define PCI_D3hot 3
112 1.4 riastrad #define PCI_D3cold 4
113 1.4 riastrad
114 1.4 riastrad #define __pci_iomem
115 1.4 riastrad
116 1.2 riastrad struct pci_dev {
117 1.2 riastrad struct pci_attach_args pd_pa;
118 1.2 riastrad int pd_kludges; /* Gotta lose 'em... */
119 1.2 riastrad #define NBPCI_KLUDGE_GET_MUMBLE 0x01
120 1.2 riastrad #define NBPCI_KLUDGE_MAP_ROM 0x02
121 1.2 riastrad bus_space_tag_t pd_rom_bst;
122 1.2 riastrad bus_space_handle_t pd_rom_bsh;
123 1.2 riastrad bus_size_t pd_rom_size;
124 1.2 riastrad void *pd_rom_vaddr;
125 1.2 riastrad device_t pd_dev;
126 1.4 riastrad struct {
127 1.4 riastrad pcireg_t type;
128 1.4 riastrad bus_addr_t addr;
129 1.4 riastrad bus_size_t size;
130 1.4 riastrad int flags;
131 1.4 riastrad bus_space_tag_t bst;
132 1.4 riastrad bus_space_handle_t bsh;
133 1.4 riastrad void __pci_iomem *kva;
134 1.4 riastrad } pd_resources[PCI_NUM_RESOURCES];
135 1.5 riastrad struct pci_conf_state *pd_saved_state;
136 1.7.2.3 martin struct acpi_devnode *pd_ad;
137 1.2 riastrad struct device dev; /* XXX Don't believe me! */
138 1.2 riastrad struct pci_bus *bus;
139 1.2 riastrad uint32_t devfn;
140 1.2 riastrad uint16_t vendor;
141 1.2 riastrad uint16_t device;
142 1.2 riastrad uint16_t subsystem_vendor;
143 1.2 riastrad uint16_t subsystem_device;
144 1.2 riastrad uint8_t revision;
145 1.2 riastrad uint32_t class;
146 1.5 riastrad bool msi_enabled;
147 1.2 riastrad };
148 1.2 riastrad
149 1.2 riastrad static inline device_t
150 1.2 riastrad pci_dev_dev(struct pci_dev *pdev)
151 1.2 riastrad {
152 1.2 riastrad return pdev->pd_dev;
153 1.2 riastrad }
154 1.2 riastrad
155 1.2 riastrad static inline void
156 1.2 riastrad linux_pci_dev_init(struct pci_dev *pdev, device_t dev,
157 1.2 riastrad const struct pci_attach_args *pa, int kludges)
158 1.2 riastrad {
159 1.2 riastrad const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag,
160 1.2 riastrad PCI_SUBSYS_ID_REG);
161 1.4 riastrad unsigned i;
162 1.2 riastrad
163 1.2 riastrad pdev->pd_pa = *pa;
164 1.2 riastrad pdev->pd_kludges = kludges;
165 1.2 riastrad pdev->pd_rom_vaddr = NULL;
166 1.2 riastrad pdev->pd_dev = dev;
167 1.7.2.3 martin #if (NACPICA > 0)
168 1.7.2.3 martin pdev->pd_ad = acpi_pcidev_find(0 /*XXX segment*/, pa->pa_bus,
169 1.7.2.3 martin pa->pa_device, pa->pa_function);
170 1.7.2.3 martin #else
171 1.7.2.3 martin pdev->pd_ad = NULL;
172 1.7.2.3 martin #endif
173 1.7.2.3 martin pdev->bus = kmem_zalloc(sizeof(struct pci_bus), KM_NOSLEEP);
174 1.7.2.3 martin pdev->bus->number = pa->pa_bus;
175 1.2 riastrad pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
176 1.2 riastrad pdev->vendor = PCI_VENDOR(pa->pa_id);
177 1.2 riastrad pdev->device = PCI_PRODUCT(pa->pa_id);
178 1.2 riastrad pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id);
179 1.2 riastrad pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id);
180 1.2 riastrad pdev->revision = PCI_REVISION(pa->pa_class);
181 1.2 riastrad pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */
182 1.4 riastrad
183 1.4 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
184 1.4 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) {
185 1.4 riastrad const int reg = PCI_BAR(i);
186 1.4 riastrad
187 1.4 riastrad pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc,
188 1.4 riastrad pa->pa_tag, reg);
189 1.4 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
190 1.4 riastrad pdev->pd_resources[i].type,
191 1.4 riastrad &pdev->pd_resources[i].addr,
192 1.4 riastrad &pdev->pd_resources[i].size,
193 1.4 riastrad &pdev->pd_resources[i].flags)) {
194 1.4 riastrad pdev->pd_resources[i].addr = 0;
195 1.4 riastrad pdev->pd_resources[i].size = 0;
196 1.4 riastrad pdev->pd_resources[i].flags = 0;
197 1.4 riastrad }
198 1.4 riastrad pdev->pd_resources[i].kva = NULL;
199 1.4 riastrad }
200 1.2 riastrad }
201 1.2 riastrad
202 1.2 riastrad static inline int
203 1.2 riastrad pci_find_capability(struct pci_dev *pdev, int cap)
204 1.2 riastrad {
205 1.2 riastrad return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
206 1.2 riastrad NULL, NULL);
207 1.2 riastrad }
208 1.2 riastrad
209 1.4 riastrad static inline int
210 1.2 riastrad pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
211 1.2 riastrad {
212 1.2 riastrad KASSERT(!ISSET(reg, 3));
213 1.2 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
214 1.4 riastrad return 0;
215 1.2 riastrad }
216 1.2 riastrad
217 1.4 riastrad static inline int
218 1.2 riastrad pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
219 1.2 riastrad {
220 1.2 riastrad KASSERT(!ISSET(reg, 1));
221 1.2 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
222 1.7.2.1 martin (reg &~ 2)) >> (8 * (reg & 2));
223 1.4 riastrad return 0;
224 1.2 riastrad }
225 1.2 riastrad
226 1.4 riastrad static inline int
227 1.2 riastrad pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep)
228 1.2 riastrad {
229 1.2 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
230 1.7.2.1 martin (reg &~ 3)) >> (8 * (reg & 3));
231 1.4 riastrad return 0;
232 1.2 riastrad }
233 1.2 riastrad
234 1.4 riastrad static inline int
235 1.2 riastrad pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
236 1.2 riastrad {
237 1.2 riastrad KASSERT(!ISSET(reg, 3));
238 1.2 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
239 1.4 riastrad return 0;
240 1.2 riastrad }
241 1.2 riastrad
242 1.2 riastrad static inline void
243 1.2 riastrad pci_rmw_config(struct pci_dev *pdev, int reg, unsigned int bytes,
244 1.2 riastrad uint32_t value)
245 1.2 riastrad {
246 1.2 riastrad const uint32_t mask = ~((~0UL) << (8 * bytes));
247 1.2 riastrad const int reg32 = (reg &~ 3);
248 1.2 riastrad const unsigned int shift = (8 * (reg & 3));
249 1.2 riastrad uint32_t value32;
250 1.2 riastrad
251 1.2 riastrad KASSERT(bytes <= 4);
252 1.2 riastrad KASSERT(!ISSET(value, ~mask));
253 1.2 riastrad pci_read_config_dword(pdev, reg32, &value32);
254 1.2 riastrad value32 &=~ (mask << shift);
255 1.2 riastrad value32 |= (value << shift);
256 1.2 riastrad pci_write_config_dword(pdev, reg32, value32);
257 1.2 riastrad }
258 1.2 riastrad
259 1.4 riastrad static inline int
260 1.2 riastrad pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value)
261 1.2 riastrad {
262 1.2 riastrad KASSERT(!ISSET(reg, 1));
263 1.2 riastrad pci_rmw_config(pdev, reg, 2, value);
264 1.4 riastrad return 0;
265 1.2 riastrad }
266 1.2 riastrad
267 1.4 riastrad static inline int
268 1.2 riastrad pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value)
269 1.2 riastrad {
270 1.2 riastrad pci_rmw_config(pdev, reg, 1, value);
271 1.4 riastrad return 0;
272 1.2 riastrad }
273 1.2 riastrad
274 1.2 riastrad /*
275 1.2 riastrad * XXX pci msi
276 1.2 riastrad */
277 1.5 riastrad static inline int
278 1.2 riastrad pci_enable_msi(struct pci_dev *pdev)
279 1.2 riastrad {
280 1.5 riastrad return -ENOSYS;
281 1.2 riastrad }
282 1.2 riastrad
283 1.2 riastrad static inline void
284 1.5 riastrad pci_disable_msi(struct pci_dev *pdev __unused)
285 1.2 riastrad {
286 1.2 riastrad KASSERT(pdev->msi_enabled);
287 1.2 riastrad }
288 1.2 riastrad
289 1.2 riastrad static inline void
290 1.2 riastrad pci_set_master(struct pci_dev *pdev)
291 1.2 riastrad {
292 1.2 riastrad pcireg_t csr;
293 1.2 riastrad
294 1.2 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
295 1.2 riastrad PCI_COMMAND_STATUS_REG);
296 1.2 riastrad csr |= PCI_COMMAND_MASTER_ENABLE;
297 1.2 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
298 1.2 riastrad PCI_COMMAND_STATUS_REG, csr);
299 1.2 riastrad }
300 1.2 riastrad
301 1.5 riastrad static inline void
302 1.5 riastrad pci_clear_master(struct pci_dev *pdev)
303 1.5 riastrad {
304 1.5 riastrad pcireg_t csr;
305 1.5 riastrad
306 1.5 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
307 1.5 riastrad PCI_COMMAND_STATUS_REG);
308 1.5 riastrad csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE;
309 1.5 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
310 1.5 riastrad PCI_COMMAND_STATUS_REG, csr);
311 1.5 riastrad }
312 1.5 riastrad
313 1.2 riastrad #define PCIBIOS_MIN_MEM 0 /* XXX bogus x86 kludge bollocks */
314 1.2 riastrad
315 1.2 riastrad static inline bus_addr_t
316 1.2 riastrad pcibios_align_resource(void *p, const struct resource *resource,
317 1.2 riastrad bus_addr_t addr, bus_size_t size)
318 1.2 riastrad {
319 1.2 riastrad panic("pcibios_align_resource has accessed unaligned neurons!");
320 1.2 riastrad }
321 1.2 riastrad
322 1.2 riastrad static inline int
323 1.2 riastrad pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
324 1.2 riastrad bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
325 1.2 riastrad bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
326 1.2 riastrad bus_size_t) __unused,
327 1.2 riastrad struct pci_dev *pdev)
328 1.2 riastrad {
329 1.2 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa;
330 1.2 riastrad bus_space_tag_t bst;
331 1.2 riastrad int error;
332 1.2 riastrad
333 1.2 riastrad switch (resource->flags) {
334 1.2 riastrad case IORESOURCE_MEM:
335 1.2 riastrad bst = pa->pa_memt;
336 1.2 riastrad break;
337 1.2 riastrad
338 1.2 riastrad case IORESOURCE_IO:
339 1.2 riastrad bst = pa->pa_iot;
340 1.2 riastrad break;
341 1.2 riastrad
342 1.2 riastrad default:
343 1.2 riastrad panic("I don't know what kind of resource you want!");
344 1.2 riastrad }
345 1.2 riastrad
346 1.2 riastrad resource->r_bst = bst;
347 1.3 riastrad error = bus_space_alloc(bst, start, __type_max(bus_addr_t),
348 1.2 riastrad size, align, 0, 0, &resource->start, &resource->r_bsh);
349 1.2 riastrad if (error)
350 1.2 riastrad return error;
351 1.2 riastrad
352 1.2 riastrad resource->size = size;
353 1.2 riastrad return 0;
354 1.2 riastrad }
355 1.2 riastrad
356 1.2 riastrad /*
357 1.2 riastrad * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are
358 1.2 riastrad * defined only for their single purposes in i915drm, in
359 1.2 riastrad * i915_get_bridge_dev and intel_detect_pch. We can't define them more
360 1.2 riastrad * generally without adapting pci_find_device (and pci_enumerate_bus
361 1.2 riastrad * internally) to pass a cookie through.
362 1.2 riastrad */
363 1.2 riastrad
364 1.2 riastrad static inline int /* XXX inline? */
365 1.2 riastrad pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa)
366 1.2 riastrad {
367 1.2 riastrad
368 1.2 riastrad if (pa->pa_bus != 0)
369 1.2 riastrad return 0;
370 1.2 riastrad if (pa->pa_device != 0)
371 1.2 riastrad return 0;
372 1.2 riastrad if (pa->pa_function != 0)
373 1.2 riastrad return 0;
374 1.2 riastrad
375 1.2 riastrad return 1;
376 1.2 riastrad }
377 1.2 riastrad
378 1.2 riastrad static inline struct pci_dev *
379 1.2 riastrad pci_get_bus_and_slot(int bus, int slot)
380 1.2 riastrad {
381 1.2 riastrad struct pci_attach_args pa;
382 1.2 riastrad
383 1.2 riastrad KASSERT(bus == 0);
384 1.2 riastrad KASSERT(slot == PCI_DEVFN(0, 0));
385 1.2 riastrad
386 1.2 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0))
387 1.2 riastrad return NULL;
388 1.2 riastrad
389 1.2 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
390 1.2 riastrad linux_pci_dev_init(pdev, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
391 1.2 riastrad
392 1.2 riastrad return pdev;
393 1.2 riastrad }
394 1.2 riastrad
395 1.2 riastrad static inline int /* XXX inline? */
396 1.2 riastrad pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa)
397 1.2 riastrad {
398 1.2 riastrad
399 1.2 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
400 1.2 riastrad return 0;
401 1.2 riastrad if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
402 1.2 riastrad return 0;
403 1.2 riastrad
404 1.2 riastrad return 1;
405 1.2 riastrad }
406 1.2 riastrad
407 1.4 riastrad static inline void
408 1.4 riastrad pci_dev_put(struct pci_dev *pdev)
409 1.4 riastrad {
410 1.4 riastrad
411 1.4 riastrad if (pdev == NULL)
412 1.4 riastrad return;
413 1.4 riastrad
414 1.4 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE));
415 1.4 riastrad kmem_free(pdev, sizeof(*pdev));
416 1.4 riastrad }
417 1.4 riastrad
418 1.2 riastrad static inline struct pci_dev *
419 1.4 riastrad pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from)
420 1.2 riastrad {
421 1.2 riastrad struct pci_attach_args pa;
422 1.2 riastrad
423 1.2 riastrad KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8));
424 1.4 riastrad
425 1.4 riastrad if (from != NULL) {
426 1.4 riastrad pci_dev_put(from);
427 1.4 riastrad return NULL;
428 1.4 riastrad }
429 1.2 riastrad
430 1.2 riastrad if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge))
431 1.2 riastrad return NULL;
432 1.2 riastrad
433 1.2 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
434 1.2 riastrad linux_pci_dev_init(pdev, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
435 1.2 riastrad
436 1.2 riastrad return pdev;
437 1.2 riastrad }
438 1.2 riastrad
439 1.2 riastrad #define __pci_rom_iomem
440 1.2 riastrad
441 1.2 riastrad static inline void
442 1.2 riastrad pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused)
443 1.2 riastrad {
444 1.2 riastrad
445 1.2 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
446 1.2 riastrad KASSERT(vaddr == pdev->pd_rom_vaddr);
447 1.2 riastrad bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size);
448 1.2 riastrad pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM;
449 1.2 riastrad pdev->pd_rom_vaddr = NULL;
450 1.2 riastrad }
451 1.2 riastrad
452 1.7.2.2 martin /* XXX Whattakludge! Should move this in sys/arch/. */
453 1.7.2.2 martin static int
454 1.7.2.2 martin pci_map_rom_md(struct pci_dev *pdev)
455 1.7.2.2 martin {
456 1.7.2.2 martin #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
457 1.7.2.2 martin const bus_addr_t rom_base = 0xc0000;
458 1.7.2.2 martin const bus_size_t rom_size = 0x20000;
459 1.7.2.2 martin bus_space_handle_t rom_bsh;
460 1.7.2.2 martin int error;
461 1.7.2.2 martin
462 1.7.2.2 martin if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY)
463 1.7.2.2 martin return ENXIO;
464 1.7.2.2 martin if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
465 1.7.2.2 martin return ENXIO;
466 1.7.2.2 martin /* XXX Check whether this is the primary VGA card? */
467 1.7.2.2 martin error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size,
468 1.7.2.2 martin (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh);
469 1.7.2.2 martin if (error)
470 1.7.2.2 martin return ENXIO;
471 1.7.2.2 martin
472 1.7.2.2 martin pdev->pd_rom_bst = pdev->pd_pa.pa_memt;
473 1.7.2.2 martin pdev->pd_rom_bsh = rom_bsh;
474 1.7.2.2 martin pdev->pd_rom_size = rom_size;
475 1.7.2.2 martin
476 1.7.2.2 martin return 0;
477 1.7.2.2 martin #else
478 1.7.2.2 martin return ENXIO;
479 1.7.2.2 martin #endif
480 1.7.2.2 martin }
481 1.7.2.2 martin
482 1.2 riastrad static inline void __pci_rom_iomem *
483 1.2 riastrad pci_map_rom(struct pci_dev *pdev, size_t *sizep)
484 1.2 riastrad {
485 1.2 riastrad bus_space_handle_t bsh;
486 1.2 riastrad bus_size_t size;
487 1.2 riastrad
488 1.2 riastrad KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
489 1.2 riastrad
490 1.2 riastrad if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
491 1.2 riastrad (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR),
492 1.2 riastrad &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size)
493 1.7.2.2 martin != 0 &&
494 1.7.2.2 martin pci_map_rom_md(pdev) != 0)
495 1.2 riastrad return NULL;
496 1.2 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
497 1.2 riastrad
498 1.2 riastrad /* XXX This type is obviously wrong in general... */
499 1.2 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
500 1.7.2.2 martin pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, &bsh, &size)) {
501 1.2 riastrad pci_unmap_rom(pdev, NULL);
502 1.2 riastrad return NULL;
503 1.2 riastrad }
504 1.2 riastrad
505 1.2 riastrad KASSERT(size <= SIZE_T_MAX);
506 1.2 riastrad *sizep = size;
507 1.2 riastrad pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, bsh);
508 1.2 riastrad return pdev->pd_rom_vaddr;
509 1.2 riastrad }
510 1.2 riastrad
511 1.4 riastrad static inline bus_addr_t
512 1.4 riastrad pci_resource_start(struct pci_dev *pdev, unsigned i)
513 1.4 riastrad {
514 1.4 riastrad
515 1.4 riastrad KASSERT(i < PCI_NUM_RESOURCES);
516 1.4 riastrad return pdev->pd_resources[i].addr;
517 1.4 riastrad }
518 1.4 riastrad
519 1.4 riastrad static inline bus_size_t
520 1.4 riastrad pci_resource_len(struct pci_dev *pdev, unsigned i)
521 1.4 riastrad {
522 1.4 riastrad
523 1.4 riastrad KASSERT(i < PCI_NUM_RESOURCES);
524 1.4 riastrad return pdev->pd_resources[i].size;
525 1.4 riastrad }
526 1.4 riastrad
527 1.4 riastrad static inline bus_addr_t
528 1.4 riastrad pci_resource_end(struct pci_dev *pdev, unsigned i)
529 1.4 riastrad {
530 1.4 riastrad
531 1.4 riastrad return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1);
532 1.4 riastrad }
533 1.4 riastrad
534 1.4 riastrad static inline int
535 1.4 riastrad pci_resource_flags(struct pci_dev *pdev, unsigned i)
536 1.4 riastrad {
537 1.4 riastrad
538 1.4 riastrad KASSERT(i < PCI_NUM_RESOURCES);
539 1.4 riastrad return pdev->pd_resources[i].flags;
540 1.4 riastrad }
541 1.4 riastrad
542 1.4 riastrad static inline void __pci_iomem *
543 1.4 riastrad pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size)
544 1.4 riastrad {
545 1.4 riastrad int error;
546 1.4 riastrad
547 1.4 riastrad KASSERT(i < PCI_NUM_RESOURCES);
548 1.4 riastrad KASSERT(pdev->pd_resources[i].kva == NULL);
549 1.4 riastrad
550 1.4 riastrad if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM)
551 1.4 riastrad return NULL;
552 1.4 riastrad if (pdev->pd_resources[i].size < size)
553 1.4 riastrad return NULL;
554 1.4 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr,
555 1.4 riastrad size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags,
556 1.4 riastrad &pdev->pd_resources[i].bsh);
557 1.4 riastrad if (error) {
558 1.4 riastrad /* Horrible hack: try asking the fake AGP device. */
559 1.4 riastrad if (!agp_i810_borrow(pdev->pd_resources[i].addr, size,
560 1.4 riastrad &pdev->pd_resources[i].bsh))
561 1.4 riastrad return NULL;
562 1.4 riastrad }
563 1.4 riastrad pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt;
564 1.4 riastrad pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst,
565 1.4 riastrad pdev->pd_resources[i].bsh);
566 1.4 riastrad
567 1.4 riastrad return pdev->pd_resources[i].kva;
568 1.4 riastrad }
569 1.4 riastrad
570 1.4 riastrad static inline void
571 1.4 riastrad pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva)
572 1.4 riastrad {
573 1.4 riastrad unsigned i;
574 1.4 riastrad
575 1.4 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
576 1.4 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) {
577 1.4 riastrad if (pdev->pd_resources[i].kva == kva)
578 1.4 riastrad break;
579 1.4 riastrad }
580 1.4 riastrad KASSERT(i < PCI_NUM_RESOURCES);
581 1.4 riastrad
582 1.4 riastrad pdev->pd_resources[i].kva = NULL;
583 1.4 riastrad bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh,
584 1.4 riastrad pdev->pd_resources[i].size);
585 1.4 riastrad }
586 1.4 riastrad
587 1.5 riastrad static inline void
588 1.5 riastrad pci_save_state(struct pci_dev *pdev)
589 1.5 riastrad {
590 1.5 riastrad
591 1.5 riastrad KASSERT(pdev->pd_saved_state == NULL);
592 1.5 riastrad pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state),
593 1.5 riastrad KM_SLEEP);
594 1.5 riastrad pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
595 1.5 riastrad pdev->pd_saved_state);
596 1.5 riastrad }
597 1.5 riastrad
598 1.5 riastrad static inline void
599 1.5 riastrad pci_restore_state(struct pci_dev *pdev)
600 1.5 riastrad {
601 1.5 riastrad
602 1.5 riastrad KASSERT(pdev->pd_saved_state != NULL);
603 1.5 riastrad pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
604 1.5 riastrad pdev->pd_saved_state);
605 1.5 riastrad kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state));
606 1.5 riastrad pdev->pd_saved_state = NULL;
607 1.5 riastrad }
608 1.5 riastrad
609 1.5 riastrad static inline bool
610 1.5 riastrad pci_is_pcie(struct pci_dev *pdev)
611 1.5 riastrad {
612 1.5 riastrad
613 1.5 riastrad return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0);
614 1.5 riastrad }
615 1.5 riastrad
616 1.7 riastrad static inline bool
617 1.7 riastrad pci_dma_supported(struct pci_dev *pdev, uintmax_t mask)
618 1.7 riastrad {
619 1.7 riastrad
620 1.7 riastrad /* XXX Cop-out. */
621 1.7 riastrad if (mask > DMA_BIT_MASK(32))
622 1.7 riastrad return pci_dma64_available(&pdev->pd_pa);
623 1.7 riastrad else
624 1.7 riastrad return true;
625 1.7 riastrad }
626 1.7 riastrad
627 1.2 riastrad #endif /* _LINUX_PCI_H_ */
628