1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef __QCOM_APR_H_ 4 #define __QCOM_APR_H_ 5 6 #include <linux/spinlock.h> 7 #include <linux/device.h> 8 #include <dt-bindings/soc/qcom,apr.h> 9 #include <dt-bindings/soc/qcom,gpr.h> 10 11 extern const struct bus_type aprbus; 12 13 #define APR_HDR_LEN(hdr_len) ((hdr_len)/4) 14 15 /* 16 * HEADER field 17 * version:0:3 18 * header_size : 4:7 19 * message_type : 8:9 20 * reserved: 10:15 21 */ 22 #define APR_HDR_FIELD(msg_type, hdr_len, ver)\ 23 (((msg_type & 0x3) << 8) | ((hdr_len & 0xF) << 4) | (ver & 0xF)) 24 25 #define APR_HDR_SIZE sizeof(struct apr_hdr) 26 #define APR_SEQ_CMD_HDR_FIELD APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \ 27 APR_HDR_LEN(APR_HDR_SIZE), \ 28 APR_PKT_VER) 29 /* Version */ 30 #define APR_PKT_VER 0x0 31 32 /* Command and Response Types */ 33 #define APR_MSG_TYPE_EVENT 0x0 34 #define APR_MSG_TYPE_CMD_RSP 0x1 35 #define APR_MSG_TYPE_SEQ_CMD 0x2 36 #define APR_MSG_TYPE_NSEQ_CMD 0x3 37 #define APR_MSG_TYPE_MAX 0x04 38 39 /* APR Basic Response Message */ 40 #define APR_BASIC_RSP_RESULT 0x000110E8 41 #define APR_RSP_ACCEPTED 0x000100BE 42 43 struct aprv2_ibasic_rsp_result_t { 44 uint32_t opcode; 45 uint32_t status; 46 }; 47 48 /* hdr field Ver [0:3], Size [4:7], Message type [8:10] */ 49 #define APR_HDR_FIELD_VER(h) (h & 0x000F) 50 #define APR_HDR_FIELD_SIZE(h) ((h & 0x00F0) >> 4) 51 #define APR_HDR_FIELD_SIZE_BYTES(h) (((h & 0x00F0) >> 4) * 4) 52 #define APR_HDR_FIELD_MT(h) ((h & 0x0300) >> 8) 53 54 struct apr_hdr { 55 uint16_t hdr_field; 56 uint16_t pkt_size; 57 uint8_t src_svc; 58 uint8_t src_domain; 59 uint16_t src_port; 60 uint8_t dest_svc; 61 uint8_t dest_domain; 62 uint16_t dest_port; 63 uint32_t token; 64 uint32_t opcode; 65 } __packed; 66 67 struct apr_pkt { 68 struct apr_hdr hdr; 69 uint8_t payload[]; 70 }; 71 72 struct apr_resp_pkt { 73 struct apr_hdr hdr; 74 void *payload; 75 int payload_size; 76 }; 77 78 struct gpr_hdr { 79 uint32_t version:4; 80 uint32_t hdr_size:4; 81 uint32_t pkt_size:24; 82 uint32_t dest_domain:8; 83 uint32_t src_domain:8; 84 uint32_t reserved:16; 85 uint32_t src_port; 86 uint32_t dest_port; 87 uint32_t token; 88 uint32_t opcode; 89 } __packed; 90 91 struct gpr_pkt { 92 struct gpr_hdr hdr; 93 uint32_t payload[]; 94 }; 95 96 struct gpr_resp_pkt { 97 struct gpr_hdr hdr; 98 void *payload; 99 int payload_size; 100 }; 101 102 #define GPR_HDR_SIZE sizeof(struct gpr_hdr) 103 #define GPR_PKT_VER 0x0 104 #define GPR_PKT_HEADER_WORD_SIZE ((sizeof(struct gpr_pkt) + 3) >> 2) 105 #define GPR_PKT_HEADER_BYTE_SIZE (GPR_PKT_HEADER_WORD_SIZE << 2) 106 107 #define GPR_BASIC_RSP_RESULT 0x02001005 108 109 struct gpr_ibasic_rsp_result_t { 110 uint32_t opcode; 111 uint32_t status; 112 }; 113 114 #define GPR_BASIC_EVT_ACCEPTED 0x02001006 115 116 struct gpr_ibasic_rsp_accepted_t { 117 uint32_t opcode; 118 }; 119 120 /* Bits 0 to 15 -- Minor version, Bits 16 to 31 -- Major version */ 121 #define APR_SVC_MAJOR_VERSION(v) ((v >> 16) & 0xFF) 122 #define APR_SVC_MINOR_VERSION(v) (v & 0xFF) 123 124 typedef int (*gpr_port_cb) (const struct gpr_resp_pkt *d, void *priv, int op); 125 struct packet_router; 126 struct pkt_router_svc { 127 struct device *dev; 128 gpr_port_cb callback; 129 struct packet_router *pr; 130 spinlock_t lock; 131 int id; 132 void *priv; 133 }; 134 135 typedef struct pkt_router_svc gpr_port_t; 136 137 #define APR_NAME_SIZE 32 138 139 struct apr_device { 140 struct device dev; 141 uint16_t svc_id; 142 uint16_t domain_id; 143 uint32_t version; 144 char name[APR_NAME_SIZE]; 145 const char *service_path; 146 struct pkt_router_svc svc; 147 struct list_head node; 148 }; 149 150 typedef struct apr_device gpr_device_t; 151 152 #define to_apr_device(d) container_of(d, struct apr_device, dev) 153 #define svc_to_apr_device(d) container_of(d, struct apr_device, svc) 154 155 struct apr_driver { 156 int (*probe)(struct apr_device *sl); 157 void (*remove)(struct apr_device *sl); 158 int (*callback)(struct apr_device *a, 159 const struct apr_resp_pkt *d); 160 gpr_port_cb gpr_callback; 161 struct device_driver driver; 162 }; 163 164 typedef struct apr_driver gpr_driver_t; 165 #define to_apr_driver(d) container_of_const(d, struct apr_driver, driver) 166 167 /* 168 * use a macro to avoid include chaining to get THIS_MODULE 169 */ 170 #define apr_driver_register(drv) __apr_driver_register(drv, THIS_MODULE) 171 172 int __apr_driver_register(struct apr_driver *drv, struct module *owner); 173 void apr_driver_unregister(struct apr_driver *drv); 174 175 /** 176 * module_apr_driver() - Helper macro for registering a aprbus driver 177 * @__apr_driver: apr_driver struct 178 * 179 * Helper macro for aprbus drivers which do not do anything special in 180 * module init/exit. This eliminates a lot of boilerplate. Each module 181 * may only use this macro once, and calling it replaces module_init() 182 * and module_exit() 183 */ 184 #define module_apr_driver(__apr_driver) \ 185 module_driver(__apr_driver, apr_driver_register, \ 186 apr_driver_unregister) 187 #define module_gpr_driver(__gpr_driver) module_apr_driver(__gpr_driver) 188 189 int apr_send_pkt(struct apr_device *adev, struct apr_pkt *pkt); 190 191 gpr_port_t *gpr_alloc_port(gpr_device_t *gdev, struct device *dev, 192 gpr_port_cb cb, void *priv); 193 void gpr_free_port(gpr_port_t *port); 194 int gpr_send_port_pkt(gpr_port_t *port, const struct gpr_pkt *pkt); 195 int gpr_send_pkt(gpr_device_t *gdev, const struct gpr_pkt *pkt); 196 197 #endif /* __QCOM_APR_H_ */ 198