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