xref: /openbsd/sys/dev/pci/drm/apple/afk.h (revision 5dd0baa8)
1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /*
3  * AFK (Apple Firmware Kit) EPIC (EndPoint Interface Client) support
4  */
5 /* Copyright 2022 Sven Peter <sven@svenpeter.dev> */
6 
7 #ifndef _DRM_APPLE_DCP_AFK_H
8 #define _DRM_APPLE_DCP_AFK_H
9 
10 #include <linux/completion.h>
11 #include <linux/types.h>
12 
13 #include "dcp.h"
14 
15 #define AFK_MAX_CHANNEL 16
16 #define MAX_PENDING_CMDS 16
17 
18 struct apple_epic_service_ops;
19 struct apple_dcp_afkep;
20 
21 struct epic_cmd_info {
22 	u16 tag;
23 
24 	void *rxbuf;
25 	void *txbuf;
26 	dma_addr_t rxbuf_dma;
27 	dma_addr_t txbuf_dma;
28 	size_t rxlen;
29 	size_t txlen;
30 
31 	u32 retcode;
32 	bool done;
33 	bool free_on_ack;
34 	struct completion *completion;
35 };
36 
37 struct apple_epic_service {
38 	const struct apple_epic_service_ops *ops;
39 	struct apple_dcp_afkep *ep;
40 
41 	struct epic_cmd_info cmds[MAX_PENDING_CMDS];
42 	DECLARE_BITMAP(cmd_map, MAX_PENDING_CMDS);
43 	u8 cmd_tag;
44 	spinlock_t lock;
45 
46 	u32 channel;
47 	bool enabled;
48 
49 	void *cookie;
50 };
51 
52 enum epic_subtype;
53 
54 struct apple_epic_service_ops {
55 	const char name[32];
56 
57 	void (*init)(struct apple_epic_service *service, const char *name,
58 			      const char *class, s64 unit);
59 	int (*call)(struct apple_epic_service *service, u32 idx,
60 		    const void *data, size_t data_size, void *reply,
61 		    size_t reply_size);
62 	int (*report)(struct apple_epic_service *service, enum epic_subtype type,
63 		      const void *data, size_t data_size);
64 	void (*teardown)(struct apple_epic_service *service);
65 };
66 
67 struct afk_ringbuffer_header {
68 	__le32 bufsz;
69 	u32 unk;
70 	u32 _pad1[14];
71 	__le32 rptr;
72 	u32 _pad2[15];
73 	__le32 wptr;
74 	u32 _pad3[15];
75 };
76 
77 struct afk_qe {
78 #define QE_MAGIC 0x20504f49 // ' POI'
79 	__le32 magic;
80 	__le32 size;
81 	__le32 channel;
82 	__le32 type;
83 	u8 data[];
84 };
85 
86 struct epic_hdr {
87 	u8 version;
88 	__le16 seq;
89 	u8 _pad;
90 	__le32 unk;
91 	__le64 timestamp;
92 } __attribute__((packed));
93 
94 struct epic_sub_hdr {
95 	__le32 length;
96 	u8 version;
97 	u8 category;
98 	__le16 type;
99 	__le64 timestamp;
100 	__le16 tag;
101 	__le16 unk;
102 	__le32 inline_len;
103 } __attribute__((packed));
104 
105 struct epic_cmd {
106 	__le32 retcode;
107 	__le64 rxbuf;
108 	__le64 txbuf;
109 	__le32 rxlen;
110 	__le32 txlen;
111 	u8 rxcookie;
112 	u8 txcookie;
113 } __attribute__((packed));
114 
115 struct epic_service_call {
116 	u8 _pad0[2];
117 	__le16 group;
118 	__le32 command;
119 	__le32 data_len;
120 #define EPIC_SERVICE_CALL_MAGIC 0x69706378
121 	__le32 magic;
122 	u8 _pad1[48];
123 } __attribute__((packed));
124 static_assert(sizeof(struct epic_service_call) == 64);
125 
126 enum epic_type {
127 	EPIC_TYPE_NOTIFY = 0,
128 	EPIC_TYPE_COMMAND = 3,
129 	EPIC_TYPE_REPLY = 4,
130 	EPIC_TYPE_NOTIFY_ACK = 8,
131 };
132 
133 enum epic_category {
134 	EPIC_CAT_REPORT = 0x00,
135 	EPIC_CAT_NOTIFY = 0x10,
136 	EPIC_CAT_REPLY = 0x20,
137 	EPIC_CAT_COMMAND = 0x30,
138 };
139 
140 enum epic_subtype {
141 	EPIC_SUBTYPE_ANNOUNCE = 0x30,
142 	EPIC_SUBTYPE_TEARDOWN = 0x32,
143 	EPIC_SUBTYPE_STD_SERVICE = 0xc0,
144 };
145 
146 struct afk_ringbuffer {
147 	bool ready;
148 	struct afk_ringbuffer_header *hdr;
149 	u32 rptr;
150 	void *buf;
151 	size_t bufsz;
152 };
153 
154 struct apple_dcp_afkep {
155 	struct apple_dcp *dcp;
156 
157 	u32 endpoint;
158 	struct workqueue_struct *wq;
159 
160 	struct completion started;
161 	struct completion stopped;
162 
163 	void *bfr;
164 	u16 bfr_tag;
165 	size_t bfr_size;
166 	dma_addr_t bfr_dma;
167 
168 	struct afk_ringbuffer txbfr;
169 	struct afk_ringbuffer rxbfr;
170 
171 	spinlock_t lock;
172 	u16 qe_seq;
173 
174 	const struct apple_epic_service_ops *ops;
175 	struct apple_epic_service services[AFK_MAX_CHANNEL];
176 	u32 num_channels;
177 };
178 
179 struct apple_dcp_afkep *afk_init(struct apple_dcp *dcp, u32 endpoint,
180 				 const struct apple_epic_service_ops *ops);
181 int afk_start(struct apple_dcp_afkep *ep);
182 int afk_receive_message(struct apple_dcp_afkep *ep, u64 message);
183 int afk_send_epic(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
184 		  enum epic_type etype, enum epic_category ecat, u8 stype,
185 		  const void *payload, size_t payload_len);
186 int afk_send_command(struct apple_epic_service *service, u8 type,
187 		     const void *payload, size_t payload_len, void *output,
188 		     size_t output_len, u32 *retcode);
189 int afk_service_call(struct apple_epic_service *service, u16 group, u32 command,
190 		     const void *data, size_t data_len, size_t data_pad,
191 		     void *output, size_t output_len, size_t output_pad);
192 #endif
193