1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #ifndef PCIE_CORE_H
10 #define PCIE_CORE_H
11
12 #include "hdf_base.h"
13 #include "hdf_device_desc.h"
14 #include "osal_mutex.h"
15 #include "pcie_if.h"
16 #include "platform_core.h"
17
18 #ifdef __cplusplus
19 #if __cplusplus
20 extern "C" {
21 #endif
22 #endif /* __cplusplus */
23
24 struct PcieCntlr;
25
26 struct PcieCntlrOps {
27 int32_t (*read)(struct PcieCntlr *cntlr, uint32_t mode, uint32_t pos, uint8_t *data, uint32_t len);
28 int32_t (*write)(struct PcieCntlr *cntlr, uint32_t mode, uint32_t pos, uint8_t *data, uint32_t len);
29 int32_t (*dmaMap)(struct PcieCntlr *cntlr, uintptr_t addr, uint32_t len, uint8_t dir);
30 void (*dmaUnmap)(struct PcieCntlr *cntlr, uintptr_t addr, uint32_t len, uint8_t dir);
31 int32_t (*registerIrq)(struct PcieCntlr *cntlr);
32 void (*unregisterIrq)(struct PcieCntlr *cntlr);
33 };
34
35 struct PcieDevCfgInfo {
36 uint16_t busNum;
37 uint32_t vendorId;
38 uint32_t devId;
39 };
40
41 struct PcieCntlr {
42 struct IDeviceIoService service;
43 struct HdfDeviceObject *hdfDevObj;
44 struct PlatformDevice device;
45 OsalSpinlock spin;
46 struct PcieCntlrOps *ops;
47 struct PcieDevCfgInfo devInfo;
48 PcieCallbackFunc cb;
49 PcieCallbackFunc dmaCb;
50 uintptr_t dmaData;
51 uint32_t len;
52 uint8_t dir;
53 void *priv;
54 };
55
PcieCntlrLock(struct PcieCntlr * cntlr)56 static inline void PcieCntlrLock(struct PcieCntlr *cntlr)
57 {
58 if (cntlr != NULL) {
59 (void)OsalSpinLock(&cntlr->spin);
60 }
61 }
62
PcieCntlrUnlock(struct PcieCntlr * cntlr)63 static inline void PcieCntlrUnlock(struct PcieCntlr *cntlr)
64 {
65 if (cntlr != NULL) {
66 (void)OsalSpinUnlock(&cntlr->spin);
67 }
68 }
69
PcieCntlrGetByBusNum(uint16_t num)70 static inline struct PcieCntlr *PcieCntlrGetByBusNum(uint16_t num)
71 {
72 struct PlatformDevice *device = PlatformManagerGetDeviceByNumber(PlatformManagerGet(PLATFORM_MODULE_PCIE), num);
73
74 if (device == NULL) {
75 return NULL;
76 }
77 return CONTAINER_OF(device, struct PcieCntlr, device);
78 }
79
80 int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj);
81 int32_t PcieCntlrAdd(struct PcieCntlr *cntlr);
82 void PcieCntlrRemove(struct PcieCntlr *cntlr);
83 int32_t PcieCntlrCallback(struct PcieCntlr *cntlr);
84 int32_t PcieCntlrDmaCallback(struct PcieCntlr *cntlr);
85
86 int32_t PcieCntlrRead(struct PcieCntlr *cntlr, uint32_t mode, uint32_t pos, uint8_t *data, uint32_t len);
87 int32_t PcieCntlrWrite(struct PcieCntlr *cntlr, uint32_t mode, uint32_t pos, uint8_t *data, uint32_t len);
88 int32_t PcieCntlrRegisterIrq(struct PcieCntlr *cntlr, PcieCallbackFunc cb);
89 void PcieCntlrUnregisterIrq(struct PcieCntlr *cntlr);
90 int32_t PcieCntlrDmaMap(struct PcieCntlr *cntlr, PcieCallbackFunc cb, uintptr_t addr, uint32_t len, uint8_t dir);
91 void PcieCntlrDmaUnmap(struct PcieCntlr *cntlr, uintptr_t addr, uint32_t len, uint8_t dir);
92
93 #ifdef __cplusplus
94 #if __cplusplus
95 }
96 #endif
97 #endif /* __cplusplus */
98
99 #endif /* PCIE_CORE_H */
100