blob: 26367f62b478ff8b1dcb084fda57c9c112a95a16 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonsson35de9e62022-03-08 13:25:45 +01002 * Copyright (c) 2020-2022 Arm Limited.
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
21#ifndef ETHOSU_MAILBOX_H
22#define ETHOSU_MAILBOX_H
23
24/****************************************************************************
25 * Includes
26 ****************************************************************************/
Jonny Svärd7c24c772021-01-14 19:53:17 +010027#include "ethosu_core_interface.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020028
29#include <linux/types.h>
30#include <linux/mailbox_client.h>
31#include <linux/workqueue.h>
Davide Grohmann32660f92022-04-27 16:49:07 +020032#include <linux/idr.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020033
34/****************************************************************************
35 * Types
36 ****************************************************************************/
37
38struct device;
39struct ethosu_buffer;
40struct ethosu_device;
41struct ethosu_core_msg;
42struct ethosu_core_queue;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010043struct ethosu_watchdog;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020044struct resource;
45
46typedef void (*ethosu_mailbox_cb)(void *user_arg);
47
48struct ethosu_mailbox {
49 struct device *dev;
50 struct workqueue_struct *wq;
51 struct work_struct work;
52 struct ethosu_core_queue __iomem *in_queue;
53 struct ethosu_core_queue __iomem *out_queue;
54 struct mbox_client client;
55 struct mbox_chan *rx;
56 struct mbox_chan *tx;
57 ethosu_mailbox_cb callback;
58 void *user_arg;
Davide Grohmann32660f92022-04-27 16:49:07 +020059 struct idr msg_idr;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010060 struct ethosu_watchdog *wdog;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010061 unsigned ping_count;
62};
63
64struct ethosu_mailbox_msg {
Davide Grohmann32660f92022-04-27 16:49:07 +020065 int id;
66 void (*fail)(struct ethosu_mailbox_msg *msg);
67 int (*resend)(struct ethosu_mailbox_msg *msg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020068};
69
70/****************************************************************************
71 * Functions
72 ****************************************************************************/
73
74/**
75 * ethosu_mailbox_init() - Initialize mailbox
76 *
77 * Return: 0 on success, else error code.
78 */
79int ethosu_mailbox_init(struct ethosu_mailbox *mbox,
80 struct device *dev,
81 struct resource *in_queue,
82 struct resource *out_queue,
83 ethosu_mailbox_cb callback,
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010084 void *user_arg,
85 struct ethosu_watchdog *wdog);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020086
87/**
88 * ethosu_mailbox_deinit() - Deinitialize mailbox
89 */
90void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox);
91
92/**
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010093 * ethosu_mailbox_wait_prepare() - Prepare to wait on firmware
94 *
95 * This function must only be called when the firmware is in a
96 * stopped state. It invalidates the firmware queues setting
97 * size, read and write positions to illegal values.
98 */
99void ethosu_mailbox_wait_prepare(struct ethosu_mailbox *mbox);
100
101/**
102 * ethosu_mailbox_wait_firmware() - Waiting for firmware to initialize
103 * message queues
104 *
105 * Following a call to ethosu_mailbox_wait_prepare() this function waits for
106 * the firmware to boot up and initialize the firmware queues.
107 *
108 * Return: 0 on success, else error code.
109 */
110int ethosu_mailbox_wait_firmware(struct ethosu_mailbox *mbox);
111
112/**
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200113 * ethosu_mailbox_read() - Read message from mailbox
114 *
115 * Return: 0 message read, else error code.
116 */
117int ethosu_mailbox_read(struct ethosu_mailbox *mbox,
118 struct ethosu_core_msg *header,
119 void *data,
120 size_t length);
121
122/**
Davide Grohmann32660f92022-04-27 16:49:07 +0200123 * ethosu_mailbox_register() - Register the ethosu_mailbox_msg in ethosu_mailbox
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100124 *
125 * Return: 0 on success, else error code.
126 */
Davide Grohmann32660f92022-04-27 16:49:07 +0200127int ethosu_mailbox_register(struct ethosu_mailbox *mbox,
128 struct ethosu_mailbox_msg *msg);
129
130/**
131 * ethosu_mailbox_free_id() - Free the id of the ethosu_mailbox_msg
132 */
133void ethosu_mailbox_deregister(struct ethosu_mailbox *mbox,
134 struct ethosu_mailbox_msg *msg);
135
136/**
137 * ethosu_mailbox_find() - Find mailbox message
138 *
139 * Return: a valid pointer on success, otherwise an error ptr.
140 */
141struct ethosu_mailbox_msg *ethosu_mailbox_find(struct ethosu_mailbox *mbox,
142 int msg_id);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100143
144/**
145 * ethosu_mailbox_fail() - Fail mailbox messages
146 *
147 * Call fail() callback on all messages in pending list.
148 */
149void ethosu_mailbox_fail(struct ethosu_mailbox *mbox);
150
151/**
152 * ethosu_mailbox_resend() - Resend mailbox messages
153 *
154 * Call resend() callback on all messages in pending list.
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100155 */
Davide Grohmann8b1fe552022-04-07 16:58:32 +0200156void ethosu_mailbox_resend(struct ethosu_mailbox *mbox);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100157
158/**
Jonny Svärd7c24c772021-01-14 19:53:17 +0100159 * ethosu_mailbox_reset() - Reset to end of queue
160 */
161void ethosu_mailbox_reset(struct ethosu_mailbox *mbox);
162
163/**
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164 * ethosu_mailbox_ping() - Send ping message
165 *
166 * Return: 0 on success, else error code.
167 */
168int ethosu_mailbox_ping(struct ethosu_mailbox *mbox);
169
170/**
Jonny Svärd7c24c772021-01-14 19:53:17 +0100171 * ethosu_mailbox_pong() - Send pong response
172 *
173 * Return: 0 on success, else error code.
174 */
175int ethosu_mailbox_pong(struct ethosu_mailbox *mbox);
176
177/**
178 * ethosu_mailbox_version_response - Send version request
179 *
180 * Return: 0 on succes, else error code
181 */
182int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox);
183
184/**
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200185 * ethosu_mailbox_capabilities_request() - Send capabilities request
186 *
187 * Return: 0 on success, else error code.
188 */
189int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200190 struct ethosu_mailbox_msg *msg);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200191
192/**
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200193 * ethosu_mailbox_inference() - Send inference
194 *
195 * Return: 0 on success, else error code.
196 */
197int ethosu_mailbox_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200198 struct ethosu_mailbox_msg *msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200199 uint32_t ifm_count,
200 struct ethosu_buffer **ifm,
201 uint32_t ofm_count,
202 struct ethosu_buffer **ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200203 struct ethosu_buffer *network,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100204 uint32_t network_index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200205 uint8_t *pmu_event_config,
206 uint8_t pmu_event_config_count,
207 uint8_t pmu_cycle_counter_enable);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200208
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100209/**
210 * ethosu_mailbox_network_info_request() - Send network info request
211 *
212 * Return: 0 on success, else error code.
213 */
214int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200215 struct ethosu_mailbox_msg *msg,
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100216 struct ethosu_buffer *network,
217 uint32_t network_index);
218
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100219/**
220 * ethosu_mailbox_cancel_inference() - Send inference cancellation
221 *
222 * Return: 0 on success, else error code.
223 */
224int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200225 struct ethosu_mailbox_msg *msg,
226 int inference_handle);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100227
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200228#endif /* ETHOSU_MAILBOX_H */