qualcomm/qbox
Loading...
Searching...
No Matches
ipcc.h
1/*
2 * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#pragma once
8
9#include <ports/initiator-signal-socket.h>
10#include <ports/target-signal-socket.h>
11#include <systemc>
12#include <cci_configuration>
13#include <tlm>
14#include <tlm_utils/simple_target_socket.h>
15#include <module_factory_registery.h>
16
17#include <scp/report.h>
18#include <tlm_sockets_buswidth.h>
19
20#include <string>
21
22/* registers as extracted
23
24IPC_PROTOCOLp_CLIENTc_VERSION, 0x400000, Read, Yes, 0x10200
25IPC_PROTOCOLp_CLIENTc_ID, 0x400004, Read, Yes, 0x0
26IPC_PROTOCOLp_CLIENTc_CONFIG, 0x400008, Read/Write, Yes, 0x0
27IPC_PROTOCOLp_CLIENTc_SEND, 0x40000C, Write, Yes, 0x0
28IPC_PROTOCOLp_CLIENTc_RECV_ID, 0x400010, Read, Yes, 0xFFFFFFFF
29IPC_PROTOCOLp_CLIENTc_RECV_SIGNAL_ENABLE, 0x400014, Write, Yes, 0x0
30IPC_PROTOCOLp_CLIENTc_RECV_SIGNAL_DISABLE, 0x400018, Write, Yes, 0x0
31IPC_PROTOCOLp_CLIENTc_RECV_SIGNAL_CLEAR, 0x40001C, Write, Yes, 0x0
32IPC_PROTOCOLp_CLIENTc_GLOBAL_SIGNAL_STATUS_0, 0x400020, Read, Yes, 0x0
33IPC_PROTOCOLp_CLIENTc_GLOBAL_SIGNAL_STATUS_1, 0x400024, Read, Yes, 0x0
34IPC_PROTOCOLp_CLIENTc_GLOBAL_SIGNAL_CLEAR_0, 0x400028, Write, Yes, 0x0
35IPC_PROTOCOLp_CLIENTc_GLOBAL_SIGNAL_CLEAR_1, 0x40002C, Write, Yes, 0x0
36IPC_PROTOCOLp_CLIENTc_GLOBAL_SIGNAL_MASK_0, 0x400030, Read/Write, Yes, 0x0
37IPC_PROTOCOLp_CLIENTc_GLOBAL_SIGNAL_MASK_1, 0x400034, Read/Write, Yes, 0x0
38IPC_PROTOCOLp_CLIENTc_CLIENT_CLEAR, 0x400038, Write, Yes, 0x0
39IPC_PROTOCOLp_CLIENTc_GLOBAL_QUAL_SIGNAL_STATUS_0, 0x40003C, Read, Yes, 0x0
40IPC_PROTOCOLp_CLIENTc_GLOBAL_QUAL_SIGNAL_STATUS_1, 0x400040, Read, Yes, 0x0
41IPC_PROTOCOLp_CLIENTc_SEND16, 0x400044, Write, Yes, 0x0
42IPC_PROTOCOLp_CLIENTc_RECV_CLIENT_PRIORITY_n, 0x400100, Read/Write, Yes, 0x0
43IPC_PROTOCOLp_CLIENTc_RECV_CLIENT_ENABLE_0_n, 0x400200, Write, Yes, 0x0
44IPC_PROTOCOLp_CLIENTc_RECV_CLIENT_ENABLE_1_n, 0x400300, Write, Yes, 0x0
45IPC_PROTOCOLp_CLIENTc_RECV_CLIENT_DISABLE_0_n, 0x400400, Write, Yes, 0x0
46IPC_PROTOCOLp_CLIENTc_RECV_CLIENT_DISABLE_1_n, 0x400500, Write, Yes, 0x0
47IPC_PROTOCOLp_CLIENTc_CLIENT_SIGNAL_STATUS_0_n, 0x400600, Read, Yes, 0x0
48IPC_PROTOCOLp_CLIENTc_CLIENT_SIGNAL_STATUS_1_n, 0x400700, Read, Yes, 0x0
49IPC_PROTOCOLp_CLIENTc_CLIENT_ENABLE_STATUS_0_n, 0x400800, Read, Yes, 0x0
50IPC_PROTOCOLp_CLIENTc_CLIENT_ENABLE_STATUS_1_n, 0x400900, Read, Yes, 0x0
51
52*/
53
60
61class ipcc : public sc_core::sc_module
62{
63 SCP_LOGGER();
64
65private:
66 enum IPCC_Regs {
67 VERSION,
68 ID,
69 CONFIG,
70 SEND,
71 RECV_ID,
72 RECV_SIGNAL_ENABLE,
73 RECV_SIGNAL_DISABLE,
74 RECV_SIGNAL_CLEAR,
75 GLOBAL_SIGNAL_STATUS_0,
76 GLOBAL_SIGNAL_STATUS_1,
77 GLOBAL_SIGNAL_CLEAR_0,
78 GLOBAL_SIGNAL_CLEAR_1,
79 GLOBAL_SIGNAL_MASK_0,
80 GLOBAL_SIGNAL_MASK_1,
81 CLIENT_CLEAR,
82 GLOBAL_QUAL_SIGNAL_STATUS_0,
83 GLOBAL_QUAL_SIGNAL_STATUS_1,
84 SEND16,
85 RECV_ID_PHYS = 0x48 / 4,
86 RECV_CLIENT_PRIORITY_n = 0x100 / 4,
87 RECV_CLIENT_ENABLE_0_n = 0x200 / 4,
88 RECV_CLIENT_ENABLE_1_n = 0x300 / 4,
89 RECV_CLIENT_DISABLE_0_n = 0x400 / 4,
90 RECV_CLIENT_DISABLE_1_n = 0x500 / 4,
91 // I think these means "Is a signal coming from client N active on this client"
92 CLIENT_SIGNAL_STATUS_0_n = 0x600 / 4,
93 CLIENT_SIGNAL_STATUS_1_n = 0x700 / 4,
94 CLIENT_ENABLE_STATUS_0_n = 0x800 / 4,
95 CLIENT_ENABLE_STATUS_1_n = 0x900 / 4,
96 MAX_CLIENT_SIZE = 64
97 };
98 enum Protocol { MPROC = 0, COMPUTE_L0 = 1, COMPUTE_L1 = 2, PCIE_MSI = 3 };
99 static constexpr unsigned int NUM_PROTOCOLS = 4;
100 static_assert(NUM_PROTOCOLS <= 4, "IPCC supports at most four protocols");
101
102 static constexpr int protocol_physical_client_count(unsigned int protocol)
103 {
104 return protocol == PCIE_MSI ? 60 : MAX_CLIENT_SIZE;
105 }
106
107 class IPC_client
108 {
109 public:
110 uint32_t regs[0x1000 / 4];
111 // These could be parameters, but then each IPC_client needs a unique name and would need to
112 // be constructed during elaboration
113 uint32_t p_client_size;
114 uint32_t p_version;
115 bool status(int client, int signal)
116 {
117 assert(client < p_client_size);
118 assert(signal < p_client_size);
119 if (signal < 32)
120 return (regs[CLIENT_SIGNAL_STATUS_0_n + client] & (1 << signal)) != 0;
121 else
122 return (regs[CLIENT_SIGNAL_STATUS_1_n + client] & (1 << (signal - 32))) != 0;
123 }
124 bool enable(int client, int signal)
125 {
126 assert(client < p_client_size);
127 assert(signal < p_client_size);
128 if (signal < 32)
129 return (regs[CLIENT_ENABLE_STATUS_0_n + client] & (1 << signal)) != 0;
130 else
131 return (regs[CLIENT_ENABLE_STATUS_1_n + client] & (1 << (signal - 32))) != 0;
132 }
133
134 void set_status(int client, int signal)
135 {
136 assert(client < p_client_size);
137 assert(signal < p_client_size);
138 if (signal < 32)
139 regs[CLIENT_SIGNAL_STATUS_0_n + client] |= (1 << signal);
140 else
141 regs[CLIENT_SIGNAL_STATUS_1_n + client] |= (1 << (signal - 32));
142 }
143
144 void clear_status(int client, int signal)
145 {
146 assert(client < p_client_size);
147 assert(signal < p_client_size);
148 if (signal < 32)
149 regs[CLIENT_SIGNAL_STATUS_0_n + client] &= ~(1 << signal);
150 else
151 regs[CLIENT_SIGNAL_STATUS_1_n + client] &= ~(1 << (signal - 32));
152 }
153
154 void set_enable(int client, int signal)
155 {
156 assert(client < p_client_size);
157 assert(signal < p_client_size);
158 if (signal < 32)
159 regs[CLIENT_ENABLE_STATUS_0_n + client] |= (1 << signal);
160 else
161 regs[CLIENT_ENABLE_STATUS_1_n + client] |= (1 << (signal - 32));
162 }
163
164 void clear_enable(int client, int signal)
165 {
166 assert(client < p_client_size);
167 assert(signal < p_client_size);
168 if (signal < 32)
169 regs[CLIENT_ENABLE_STATUS_0_n + client] &= ~(1 << signal);
170 else
171 regs[CLIENT_ENABLE_STATUS_1_n + client] &= ~(1 << (signal - 32));
172 }
173
174 void reset()
175 {
176 memset(regs, 0, sizeof(regs));
177 regs[VERSION] = p_version;
178 regs[RECV_ID] = 0xFFFFFFFF;
179 }
180 IPC_client(): p_client_size(MAX_CLIENT_SIZE), p_version(0x10200) { reset(); };
181 };
182 IPC_client client[NUM_PROTOCOLS][MAX_CLIENT_SIZE];
183 sc_core::sc_event update_irq_ev;
184
185 int logical_of_physical[MAX_CLIENT_SIZE];
186 int physical_of_logical[MAX_CLIENT_SIZE];
187
188 int physical_client(unsigned int protocol, int logical_client) const
189 {
190 for (int client = 0; client < protocol_physical_client_count(protocol); client++) {
191 if (logical_of_physical[client] == logical_client) return client;
192 }
193 return -1;
194 }
195
196protected:
197 cci::cci_param<unsigned int> p_num_irq;
198 cci::cci_param<unsigned int> p_hw_version;
199 cci::cci_param<std::string> p_logical_ids;
200
201 template <class Port>
202 static void safe_write(Port& p, bool v)
203 {
204 if (p.size() > 0) p->write(v);
205 }
206
207 uint32_t read(IPC_client& src, uint64_t addr)
208 {
209 uint32_t ret = src.regs[addr / 4];
210 // Side effects
211 switch (addr / 4) {
212 case RECV_ID:
213 case RECV_ID_PHYS: {
214 uint32_t logical_id = src.regs[RECV_ID];
215 if ((src.regs[CONFIG] & 0x1) && (logical_id != 0xffffffff)) {
216 src.clear_status(logical_id >> 16, logical_id & 0xffff);
217 src.regs[RECV_ID] = 0xffffffff;
218 src.regs[RECV_ID_PHYS] = 0xffffffff;
219 }
220 break;
221 }
222 }
223
224 return ret;
225 }
226 void write(IPC_client& src, uint64_t addr, uint32_t data)
227 {
228 src.regs[addr / 4] = data;
229 // side effects
230 switch (addr / 4) {
231 case SEND: {
232 int s = data & 0xffff;
233 int src_protocol = src.regs[ID] & 0x3f;
234 int src_logical = (src.regs[ID] >> 16) & 0x3f;
235 if (data & 0x80000000) {
236 SCP_INFO(()) << "IPCC_SEND broadcast src_logical=" << std::dec << src_logical
237 << " protocol=" << src_protocol << " signal=" << s << " raw=0x" << std::hex << data
238 << std::dec;
239 for (int c = 0; c < src.p_client_size; c++) {
240 send(src, c, s);
241 }
242 } else {
243 int target_logical = (data >> 16) & 0xffff;
244 int c = physical_client(src_protocol, target_logical & 0x3f);
245 SCP_INFO(()) << "IPCC_SEND src_logical=" << std::dec << src_logical << " protocol=" << src_protocol
246 << " target_logical=" << target_logical << " target_phys=" << c << " signal=" << s
247 << " raw=0x" << std::hex << data << std::dec;
248 if ((src_logical == 6 || src_logical == 18) && target_logical == 8 && s == 2) {
249 SCP_WARN(()) << "IPCC_CDSP_READY_KICK src_logical=" << std::dec << src_logical
250 << " target_logical=" << target_logical << " signal=" << s << " raw=0x" << std::hex
251 << data << std::dec;
252 }
253 send(src, c, s);
254 }
255 break;
256 }
257 case RECV_SIGNAL_ENABLE: {
258 int c = (data >> 16) & 0xffff; // Source client
259 int s = (data & 0xffff); // Signal
260 int p = src.regs[ID] & 0x3f;
261 int dst_c = (src.regs[ID] >> 16) & 0x3f;
262 SCP_DEBUG(()) << "IPCC_LOG ENABLE: DstClient " << dst_c << " (Proto " << p << ") enabled Signal " << s
263 << " from SrcClient " << c;
264 SCP_INFO(()) << "IPCC_RECV_ENABLE dst_logical=" << std::dec << dst_c << " src_logical=" << c
265 << " signal=" << s << " raw=0x" << std::hex << data << std::dec;
266 src.set_enable(c, s);
267 break;
268 }
269 case RECV_SIGNAL_DISABLE: {
270 int c = (data >> 16) & 0xffff;
271 int s = (data & 0xffff);
272 src.clear_enable(c, s);
273 break;
274 }
275 case RECV_SIGNAL_CLEAR: {
276 int c = (data >> 16) & 0xffff;
277 int s = (data & 0xffff);
278 int dst_c = (src.regs[ID] >> 16) & 0x3f;
279 SCP_INFO(()) << "IPCC_RECV_CLEAR dst_logical=" << std::dec << dst_c << " src_logical=" << c
280 << " signal=" << s << " raw=0x" << std::hex << data << std::dec;
281 src.clear_status(c, s);
282 break;
283 }
284 case CLIENT_CLEAR: {
285 uint32_t id = src.regs[ID];
286 if (data & 0x1) {
287 memset(src.regs, 0, sizeof(src.regs));
288 src.regs[VERSION] = p_hw_version.get_value();
289 src.regs[RECV_ID] = 0xFFFFFFFF;
290 src.regs[ID] = id;
291 }
292 break;
293 }
294 }
295 }
296 void send(IPC_client& src, int c, int s)
297 {
298 int p = src.regs[ID] & 0x3f;
299 int sc = (src.regs[ID] >> 16) & 0x3f;
300 if (p >= NUM_PROTOCOLS) {
301 SCP_WARN(()) << "Ignoring send from unsupported protocol " << p;
302 return;
303 }
304 if (c < 0 || c >= protocol_physical_client_count(p)) {
305 SCP_WARN(()) << "Ignoring send to unsupported client " << c << " on protocol " << p;
306 return;
307 }
308 IPC_client& dst = client[p][c];
309 SCP_DEBUG(()) << "IPCC_LOG STATUS SET: DstClient " << c << " (Proto " << p << ") received Signal " << s
310 << " from SrcClient " << sc;
311 dst.set_status(sc, s);
312 }
313 void update_irq()
314 {
315 int client_size = client[0][0].p_client_size;
316 for (int c = 0; c < client_size; c++) {
317 int allirqcount = 0;
318 for (unsigned int p = 0; p < NUM_PROTOCOLS; p++) {
319 if (c >= protocol_physical_client_count(p)) continue;
320 int irqcount = 0;
321 int prio = 0;
322 int sig = 0;
323 int cli = 0;
324
325 for (int sc = 0; sc < client_size; sc++) {
326 for (int s = 0; s < client_size; s++) {
327 if (client[p][c].status(sc, s)) {
328 // there is an active signal s from p/sc to p/c
329 if ((client[p][c].regs[CONFIG] & 0x80000000) == 0) {
330 if (client[p][c].enable(sc, s)) {
331 int pr = client[p][c].regs[RECV_CLIENT_PRIORITY_n + sc];
332 if (irqcount == 0 || pr > prio) {
333 prio = pr;
334 sig = s;
335 cli = sc;
336 }
337 irqcount++;
338 }
339 }
340 }
341 }
342 }
343 allirqcount += irqcount;
344 if (irqcount) {
345 int phys_cli = (cli < MAX_CLIENT_SIZE && physical_of_logical[cli] >= 0) ? physical_of_logical[cli]
346 : cli;
347 client[p][c].regs[RECV_ID] = (cli << 16) + sig;
348 client[p][c].regs[RECV_ID_PHYS] = (phys_cli << 16) + sig;
349 } else {
350 client[p][c].regs[RECV_ID] = 0xffffffff;
351 client[p][c].regs[RECV_ID_PHYS] = 0xffffffff;
352 }
353 switch (p) {
354 case MPROC:
355 safe_write(irq_mproc[c], irqcount != 0);
356 break;
357 case COMPUTE_L0:
358 safe_write(irq_compute_l0[c], irqcount != 0);
359 break;
360 case COMPUTE_L1:
361 safe_write(irq_compute_l1[c], irqcount != 0);
362 break;
363 case PCIE_MSI:
364 safe_write(irq_pcie_msi[c], irqcount != 0);
365 break;
366 default:
367 SCP_FATAL(()) << "Unsupported IPCC protocol " << p;
368 }
369 }
370 if (allirqcount >= 1) {
371 if (!irq_status[c]) {
372 SCP_INFO(()) << "SEND IRQ " << c;
373 irq_status[c] = true;
374 safe_write(irq[c], true);
375 }
376 } else {
377 if (irq_status[c]) {
378 SCP_INFO(()) << "CLEAR IRQ " << c;
379 irq_status[c] = false;
380 safe_write(irq[c], false);
381 }
382 }
383 }
384 }
385 void b_transport(tlm::tlm_generic_payload& txn, sc_core::sc_time& delay)
386 {
387 unsigned int len = txn.get_data_length();
388 unsigned char* ptr = txn.get_data_ptr();
389 sc_dt::uint64 addr = txn.get_address();
390 if (len > sizeof(client)) {
391 txn.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
392 return;
393 }
394 assert(len == 4);
395
396 int c = (addr / 0x1000) & 0x3f;
397 uint64_t p = addr / 0x40000;
398 if (p >= NUM_PROTOCOLS || addr >= 0x100000ULL || (p == PCIE_MSI && c >= 60)) {
399 SCP_WARN(()) << "Ignoring access to unsupported protocol " << p;
400 txn.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
401 return;
402 }
403 switch (txn.get_command()) {
404 case tlm::TLM_READ_COMMAND: {
405 uint32_t data = read(client[p][c], addr & 0xfff);
406 memcpy(ptr, &data, len);
407 SCP_INFO(()) << "b_transport read " << std::hex << addr << " " << data;
408 break;
409 }
410 case tlm::TLM_WRITE_COMMAND: {
411 uint32_t data;
412 memcpy(&data, ptr, len);
413 SCP_INFO(()) << "b_transport write " << std::hex << addr << " " << data;
414 write(client[p][c], addr & 0xfff, data);
415 break;
416 }
417 default:
418 SCP_ERR(()) << "TLM command not supported";
419 break;
420 }
421
422 txn.set_response_status(tlm::TLM_OK_RESPONSE);
423
424 update_irq_ev.notify(sc_core::SC_ZERO_TIME);
425 }
426
427public:
428 tlm_utils::simple_target_socket<ipcc, DEFAULT_TLM_BUSWIDTH> socket;
429 // InitiatorSignalSocket<bool> irq[MAX_CLIENT_SIZE];
430 sc_core::sc_vector<InitiatorSignalSocket<bool>> irq;
431 bool irq_status[MAX_CLIENT_SIZE] = { false };
432 sc_core::sc_vector<InitiatorSignalSocket<bool>> irq_mproc;
433 sc_core::sc_vector<InitiatorSignalSocket<bool>> irq_compute_l0;
434 sc_core::sc_vector<InitiatorSignalSocket<bool>> irq_compute_l1;
435 sc_core::sc_vector<InitiatorSignalSocket<bool>> irq_pcie_msi;
437
438 ipcc(sc_core::sc_module_name name)
439 : p_num_irq("num_irq", MAX_CLIENT_SIZE, "Number of irq")
440 , p_hw_version("hw_version", 0x10200, "IPCC HW VERSION register value")
441 , p_logical_ids("logical_ids", "", "Comma-separated logical IDs per physical slot (empty=identity)")
442 , socket("target_socket")
443 , irq("irq", p_num_irq)
444 , irq_mproc("irq_mproc", p_num_irq)
445 , irq_compute_l0("irq_compute_l0", p_num_irq)
446 , irq_compute_l1("irq_compute_l1", p_num_irq)
447 , irq_pcie_msi("irq_pcie_msi", p_num_irq)
448 , reset("reset")
449 {
450 SCP_TRACE(())("Init");
451 for (int i = 0; i < MAX_CLIENT_SIZE; i++) {
452 logical_of_physical[i] = i;
453 physical_of_logical[i] = -1;
454 }
455 {
456 std::string s = p_logical_ids.get_value();
457 int i = 0;
458 while (!s.empty() && i < MAX_CLIENT_SIZE) {
459 size_t pos = s.find(',');
460 int lid = std::stoi(pos == std::string::npos ? s : s.substr(0, pos));
461 if (lid >= 0 && lid < MAX_CLIENT_SIZE) {
462 logical_of_physical[i] = lid;
463 }
464 if (pos == std::string::npos) break;
465 s = s.substr(pos + 1);
466 i++;
467 }
468 }
469 for (int i = 0; i < MAX_CLIENT_SIZE; i++) {
470 int lid = logical_of_physical[i];
471 if (lid >= 0 && lid < MAX_CLIENT_SIZE && physical_of_logical[lid] < 0) {
472 physical_of_logical[lid] = i;
473 }
474 }
475
476 int client_size = client[0][0].p_client_size;
477 uint32_t hw_ver = p_hw_version.get_value();
478 for (unsigned int p = 0; p < NUM_PROTOCOLS; p++) {
479 for (int c = 0; c < protocol_physical_client_count(p); c++) {
480 client[p][c].p_version = hw_ver;
481 client[p][c].regs[VERSION] = hw_ver;
482 client[p][c].regs[ID] = (logical_of_physical[c] << 16) + p;
483 }
484 }
485 socket.register_b_transport(this, &ipcc::b_transport);
486
487 SC_METHOD(update_irq);
488 sensitive << update_irq_ev;
489
490 reset.register_value_changed_cb([&](bool value) {
491 if (value) {
492 SCP_WARN(())("Reset");
493 int client_size = client[0][0].p_client_size;
494 for (unsigned int p = 0; p < NUM_PROTOCOLS; p++) {
495 for (int c = 0; c < protocol_physical_client_count(p); c++) {
496 client[p][c].reset();
497 client[p][c].regs[ID] = (logical_of_physical[c] << 16) + p;
498 }
499 }
500 update_irq();
501 }
502 });
503 }
504
505 ipcc() = delete;
506 ipcc(const ipcc&) = delete;
507
508 ~ipcc() {}
509};
510
511extern "C" void module_register();
Definition target-signal-socket.h:163