| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: pci.h,v 1.1.2.13 2013/07/24 03:24:03 riastradh Exp $ */ | | 1 | /* $NetBSD: pci.h,v 1.1.2.14 2013/07/24 03:32:19 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Taylor R. Campbell. | | 8 | * by Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -64,54 +64,61 @@ struct pci_device_id { | | | @@ -64,54 +64,61 @@ struct pci_device_id { |
64 | CTASSERT(PCI_CLASS_BRIDGE_ISA == 0x0601); | | 64 | CTASSERT(PCI_CLASS_BRIDGE_ISA == 0x0601); |
65 | | | 65 | |
66 | #define PCI_VENDOR_ID_INTEL PCI_VENDOR_INTEL | | 66 | #define PCI_VENDOR_ID_INTEL PCI_VENDOR_INTEL |
67 | | | 67 | |
68 | #define PCI_DEVFN(DEV, FN) \ | | 68 | #define PCI_DEVFN(DEV, FN) \ |
69 | (__SHIFTIN((DEV), __BITS(3, 7)) | __SHIFTIN((FN), __BITS(0, 2))) | | 69 | (__SHIFTIN((DEV), __BITS(3, 7)) | __SHIFTIN((FN), __BITS(0, 2))) |
70 | #define PCI_SLOT(DEVFN) __SHIFTOUT((DEVFN), __BITS(3, 7)) | | 70 | #define PCI_SLOT(DEVFN) __SHIFTOUT((DEVFN), __BITS(3, 7)) |
71 | #define PCI_FUNC(DEVFN) __SHIFTOUT((DEVFN), __BITS(0, 2)) | | 71 | #define PCI_FUNC(DEVFN) __SHIFTOUT((DEVFN), __BITS(0, 2)) |
72 | | | 72 | |
73 | #define PCI_CAP_ID_AGP PCI_CAP_AGP | | 73 | #define PCI_CAP_ID_AGP PCI_CAP_AGP |
74 | | | 74 | |
75 | struct pci_dev { | | 75 | struct pci_dev { |
76 | struct pci_attach_args pd_pa; | | 76 | struct pci_attach_args pd_pa; |
77 | bool pd_kludged; /* XXX pci_kludgey_find_dev */ | | 77 | int pd_kludges; /* Gotta lose 'em... */ |
| | | 78 | #define NBPCI_KLUDGE_GET_MUMBLE 0x01 |
| | | 79 | #define NBPCI_KLUDGE_MAP_ROM 0x02 |
| | | 80 | bus_space_tag_t pd_rom_bst; |
| | | 81 | bus_space_handle_t pd_rom_bsh; |
| | | 82 | bus_size_t pd_rom_size; |
| | | 83 | void *pd_rom_vaddr; |
78 | device_t pd_dev; | | 84 | device_t pd_dev; |
79 | struct pci_bus *bus; | | 85 | struct pci_bus *bus; |
80 | uint32_t devfn; | | 86 | uint32_t devfn; |
81 | uint16_t vendor; | | 87 | uint16_t vendor; |
82 | uint16_t device; | | 88 | uint16_t device; |
83 | uint16_t subsystem_vendor; | | 89 | uint16_t subsystem_vendor; |
84 | uint16_t subsystem_device; | | 90 | uint16_t subsystem_device; |
85 | uint8_t revision; | | 91 | uint8_t revision; |
86 | uint32_t class; | | 92 | uint32_t class; |
87 | bool msi_enabled; | | 93 | bool msi_enabled; |
88 | }; | | 94 | }; |
89 | | | 95 | |
90 | static inline device_t | | 96 | static inline device_t |
91 | pci_dev_dev(struct pci_dev *pdev) | | 97 | pci_dev_dev(struct pci_dev *pdev) |
92 | { | | 98 | { |
93 | return pdev->pd_dev; | | 99 | return pdev->pd_dev; |
94 | } | | 100 | } |
95 | | | 101 | |
96 | static inline void | | 102 | static inline void |
97 | linux_pci_dev_init(struct pci_dev *pdev, device_t dev, | | 103 | linux_pci_dev_init(struct pci_dev *pdev, device_t dev, |
98 | const struct pci_attach_args *pa, bool kludged) | | 104 | const struct pci_attach_args *pa, int kludges) |
99 | { | | 105 | { |
100 | const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag, | | 106 | const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag, |
101 | PCI_SUBSYS_ID_REG); | | 107 | PCI_SUBSYS_ID_REG); |
102 | | | 108 | |
103 | pdev->pd_pa = *pa; | | 109 | pdev->pd_pa = *pa; |
104 | pdev->pd_kludged = kludged; | | 110 | pdev->pd_kludges = kludges; |
| | | 111 | pdev->pd_rom_vaddr = NULL; |
105 | pdev->pd_dev = dev; | | 112 | pdev->pd_dev = dev; |
106 | pdev->bus = NULL; /* XXX struct pci_dev::bus */ | | 113 | pdev->bus = NULL; /* XXX struct pci_dev::bus */ |
107 | pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); | | 114 | pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); |
108 | pdev->vendor = PCI_VENDOR(pa->pa_id); | | 115 | pdev->vendor = PCI_VENDOR(pa->pa_id); |
109 | pdev->device = PCI_PRODUCT(pa->pa_id); | | 116 | pdev->device = PCI_PRODUCT(pa->pa_id); |
110 | pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id); | | 117 | pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id); |
111 | pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id); | | 118 | pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id); |
112 | pdev->revision = PCI_REVISION(pa->pa_class); | | 119 | pdev->revision = PCI_REVISION(pa->pa_class); |
113 | pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */ | | 120 | pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */ |
114 | pdev->msi_enabled = false; | | 121 | pdev->msi_enabled = false; |
115 | } | | 122 | } |
116 | | | 123 | |
117 | static inline int | | 124 | static inline int |
| @@ -276,27 +283,27 @@ pci_kludgey_match_bus0_dev0_func0(const | | | @@ -276,27 +283,27 @@ pci_kludgey_match_bus0_dev0_func0(const |
276 | | | 283 | |
277 | static inline struct pci_dev * | | 284 | static inline struct pci_dev * |
278 | pci_get_bus_and_slot(int bus, int slot) | | 285 | pci_get_bus_and_slot(int bus, int slot) |
279 | { | | 286 | { |
280 | struct pci_attach_args pa; | | 287 | struct pci_attach_args pa; |
281 | | | 288 | |
282 | KASSERT(bus == 0); | | 289 | KASSERT(bus == 0); |
283 | KASSERT(slot == PCI_DEVFN(0, 0)); | | 290 | KASSERT(slot == PCI_DEVFN(0, 0)); |
284 | | | 291 | |
285 | if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0)) | | 292 | if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0)) |
286 | return NULL; | | 293 | return NULL; |
287 | | | 294 | |
288 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); | | 295 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); |
289 | linux_pci_dev_init(pdev, NULL, &pa, true); | | 296 | linux_pci_dev_init(pdev, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); |
290 | | | 297 | |
291 | return pdev; | | 298 | return pdev; |
292 | } | | 299 | } |
293 | | | 300 | |
294 | static inline int /* XXX inline? */ | | 301 | static inline int /* XXX inline? */ |
295 | pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa) | | 302 | pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa) |
296 | { | | 303 | { |
297 | | | 304 | |
298 | if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE) | | 305 | if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE) |
299 | return 0; | | 306 | return 0; |
300 | if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA) | | 307 | if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA) |
301 | return 0; | | 308 | return 0; |
302 | | | 309 | |
| @@ -306,27 +313,71 @@ pci_kludgey_match_isa_bridge(const struc | | | @@ -306,27 +313,71 @@ pci_kludgey_match_isa_bridge(const struc |
306 | static inline struct pci_dev * | | 313 | static inline struct pci_dev * |
307 | pci_get_class(uint32_t class_subclass_shifted __unused, | | 314 | pci_get_class(uint32_t class_subclass_shifted __unused, |
308 | struct pci_dev *from __unused) | | 315 | struct pci_dev *from __unused) |
309 | { | | 316 | { |
310 | struct pci_attach_args pa; | | 317 | struct pci_attach_args pa; |
311 | | | 318 | |
312 | KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8)); | | 319 | KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8)); |
313 | KASSERT(from == NULL); | | 320 | KASSERT(from == NULL); |
314 | | | 321 | |
315 | if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge)) | | 322 | if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge)) |
316 | return NULL; | | 323 | return NULL; |
317 | | | 324 | |
318 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); | | 325 | struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); |
319 | linux_pci_dev_init(pdev, NULL, &pa, true); | | 326 | linux_pci_dev_init(pdev, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); |
320 | | | 327 | |
321 | return pdev; | | 328 | return pdev; |
322 | } | | 329 | } |
323 | | | 330 | |
324 | static inline void | | 331 | static inline void |
325 | pci_dev_put(struct pci_dev *pdev) | | 332 | pci_dev_put(struct pci_dev *pdev) |
326 | { | | 333 | { |
327 | | | 334 | |
328 | KASSERT(pdev->pd_kludged); | | 335 | KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE)); |
329 | kmem_free(pdev, sizeof(*pdev)); | | 336 | kmem_free(pdev, sizeof(*pdev)); |
330 | } | | 337 | } |
331 | | | 338 | |
| | | 339 | #define __pci_rom_iomem |
| | | 340 | |
| | | 341 | static inline void |
| | | 342 | pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused) |
| | | 343 | { |
| | | 344 | |
| | | 345 | KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); |
| | | 346 | KASSERT(vaddr == pdev->pd_rom_vaddr); |
| | | 347 | bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size); |
| | | 348 | pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM; |
| | | 349 | pdev->pd_rom_vaddr = NULL; |
| | | 350 | } |
| | | 351 | |
| | | 352 | static inline void __pci_rom_iomem * |
| | | 353 | pci_map_rom(struct pci_dev *pdev, size_t *sizep) |
| | | 354 | { |
| | | 355 | bus_space_handle_t bsh; |
| | | 356 | bus_size_t size; |
| | | 357 | |
| | | 358 | KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); |
| | | 359 | |
| | | 360 | if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM, |
| | | 361 | (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR), |
| | | 362 | &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size) |
| | | 363 | != 0) { |
| | | 364 | aprint_error_dev(pdev->pd_dev, "unable to map ROM\n"); |
| | | 365 | return NULL; |
| | | 366 | } |
| | | 367 | pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; |
| | | 368 | |
| | | 369 | /* XXX This type is obviously wrong in general... */ |
| | | 370 | if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, |
| | | 371 | PCI_ROM_CODE_TYPE_X86, &bsh, &size)) { |
| | | 372 | aprint_error_dev(pdev->pd_dev, "unable to find ROM\n"); |
| | | 373 | pci_unmap_rom(pdev, NULL); |
| | | 374 | return NULL; |
| | | 375 | } |
| | | 376 | |
| | | 377 | KASSERT(size <= SIZE_T_MAX); |
| | | 378 | *sizep = size; |
| | | 379 | pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, bsh); |
| | | 380 | return pdev->pd_rom_vaddr; |
| | | 381 | } |
| | | 382 | |
332 | #endif /* _LINUX_PCI_H_ */ | | 383 | #endif /* _LINUX_PCI_H_ */ |