1 /*
2 * Copyright (c) 2021 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 "platform_core.h"
16
17 #ifdef __cplusplus
18 #if __cplusplus
19 extern "C" {
20 #endif
21 #endif /* __cplusplus */
22
23 struct PcieCntlr;
24
25 struct PcieCntlrOps {
26 int32_t (*read)(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len);
27 int32_t (*write)(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len);
28 };
29
30 struct PcieDevCfgInfo {
31 uint32_t busNum;
32 uint32_t vendorId;
33 uint32_t devId;
34 };
35
36 struct PcieCntlr {
37 struct IDeviceIoService service;
38 struct HdfDeviceObject *hdfDevObj;
39 struct PlatformDevice device;
40 struct OsalMutex mutex;
41 struct PcieCntlrOps *ops;
42 struct PcieDevCfgInfo devInfo;
43 void *priv;
44 };
45
PcieCntlrLock(struct PcieCntlr * cntlr)46 static inline void PcieCntlrLock(struct PcieCntlr *cntlr)
47 {
48 if (cntlr != NULL) {
49 (void)OsalMutexLock(&cntlr->mutex);
50 }
51 }
52
PcieCntlrUnlock(struct PcieCntlr * cntlr)53 static inline void PcieCntlrUnlock(struct PcieCntlr *cntlr)
54 {
55 if (cntlr != NULL) {
56 (void)OsalMutexUnlock(&cntlr->mutex);
57 }
58 }
59
PcieCntlrGetByBusNum(uint16_t num)60 static inline struct PcieCntlr *PcieCntlrGetByBusNum(uint16_t num)
61 {
62 struct PlatformDevice *device = PlatformManagerGetDeviceByNumber(PlatformManagerGet(PLATFORM_MODULE_PCIE), num);
63
64 if (device == NULL) {
65 return NULL;
66 }
67 return CONTAINER_OF(device, struct PcieCntlr, device);
68 }
69
70 int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj);
71 int32_t PcieCntlrAdd(struct PcieCntlr *cntlr);
72 void PcieCntlrRemove(struct PcieCntlr *cntlr);
73
74 int32_t PcieCntlrRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len);
75 int32_t PcieCntlrWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len);
76
77 #ifdef __cplusplus
78 #if __cplusplus
79 }
80 #endif
81 #endif /* __cplusplus */
82
83 #endif /* PCIE_CORE_H */
84