2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef __NVKM_CORE_FALCON_MSGQUEUE_H
25 #define __NVKM_CORE_FALCON_MSGQUEUE_H
27 #include <core/msgqueue.h>
30 * The struct nvkm_msgqueue (named so for lack of better candidate) manages
31 * a firmware (typically, NVIDIA signed firmware) running under a given falcon.
33 * Such firmwares expect to receive commands (through one or several command
34 * queues) and will reply to such command by sending messages (using one
37 * Each firmware can support one or several units - ACR for managing secure
38 * falcons, PMU for power management, etc. A unit can be seen as a class to
39 * which command can be sent.
41 * One usage example would be to send a command to the SEC falcon to ask it to
42 * reset a secure falcon. The SEC falcon will receive the command, process it,
43 * and send a message to signal success or failure. Only when the corresponding
44 * message is received can the requester assume the request has been processed.
46 * Since we expect many variations between the firmwares NVIDIA will release
47 * across GPU generations, this library is built in a very modular way. Message
48 * formats and queues details (such as number of usage) are left to
49 * specializations of struct nvkm_msgqueue, while the functions in msgqueue.c
50 * take care of posting commands and processing messages in a fashion that is
55 enum msgqueue_msg_priority
{
56 MSGQUEUE_MSG_PRIORITY_HIGH
,
57 MSGQUEUE_MSG_PRIORITY_LOW
,
61 * struct nvkm_msgqueue_hdr - header for all commands/messages
62 * @unit_id: id of firmware using receiving the command/sending the message
63 * @size: total size of command/message
64 * @ctrl_flags: type of command/message
65 * @seq_id: used to match a message from its corresponding command
67 struct nvkm_msgqueue_hdr
{
75 * struct nvkm_msgqueue_msg - base message.
77 * This is just a header and a message (or command) type. Useful when
78 * building command-specific structures.
80 struct nvkm_msgqueue_msg
{
81 struct nvkm_msgqueue_hdr hdr
;
87 (*nvkm_msgqueue_callback
)(struct nvkm_msgqueue
*, struct nvkm_msgqueue_hdr
*);
90 * struct nvkm_msgqueue_init_func - msgqueue functions related to initialization
92 * @gen_cmdline: build the commandline into a pre-allocated buffer
93 * @init_callback: called to process the init message
95 struct nvkm_msgqueue_init_func
{
96 void (*gen_cmdline
)(struct nvkm_msgqueue
*, void *);
97 int (*init_callback
)(struct nvkm_msgqueue
*, struct nvkm_msgqueue_hdr
*);
101 * struct nvkm_msgqueue_acr_func - msgqueue functions related to ACR
103 * @boot_falcon: build and send the command to reset a given falcon
104 * @boot_multiple_falcons: build and send the command to reset several falcons
106 struct nvkm_msgqueue_acr_func
{
107 int (*boot_falcon
)(struct nvkm_msgqueue
*, enum nvkm_secboot_falcon
);
108 int (*boot_multiple_falcons
)(struct nvkm_msgqueue
*, unsigned long);
111 struct nvkm_msgqueue_func
{
112 const struct nvkm_msgqueue_init_func
*init_func
;
113 const struct nvkm_msgqueue_acr_func
*acr_func
;
114 void (*dtor
)(struct nvkm_msgqueue
*);
115 struct nvkm_msgqueue_queue
*(*cmd_queue
)(struct nvkm_msgqueue
*,
116 enum msgqueue_msg_priority
);
117 void (*recv
)(struct nvkm_msgqueue
*queue
);
121 * struct nvkm_msgqueue_queue - information about a command or message queue
123 * The number of queues is firmware-dependent. All queues must have their
124 * information filled by the init message handler.
126 * @mutex_lock: to be acquired when the queue is being used
127 * @index: physical queue index
128 * @offset: DMEM offset where this queue begins
129 * @size: size allocated to this queue in DMEM (in bytes)
130 * @position: current write position
131 * @head_reg: address of the HEAD register for this queue
132 * @tail_reg: address of the TAIL register for this queue
134 struct nvkm_msgqueue_queue
{
146 * struct nvkm_msgqueue_seq - keep track of ongoing commands
148 * Every time a command is sent, a sequence is assigned to it so the
149 * corresponding message can be matched. Upon receiving the message, a callback
150 * can be called and/or a completion signaled.
153 * @state: current state
154 * @callback: callback to call upon receiving matching message
155 * @completion: completion to signal after callback is called
157 struct nvkm_msgqueue_seq
{
165 nvkm_msgqueue_callback callback
;
166 struct completion
*completion
;
170 * We can have an arbitrary number of sequences, but realistically we will
171 * probably not use that much simultaneously.
173 #define NVKM_MSGQUEUE_NUM_SEQUENCES 16
176 * struct nvkm_msgqueue - manage a command/message based FW on a falcon
178 * @falcon: falcon to be managed
179 * @func: implementation of the firmware to use
180 * @init_msg_received: whether the init message has already been received
181 * @init_done: whether all init is complete and commands can be processed
182 * @seq_lock: protects seq and seq_tbl
183 * @seq: sequences to match commands and messages
184 * @seq_tbl: bitmap of sequences currently in use
186 struct nvkm_msgqueue
{
187 struct nvkm_falcon
*falcon
;
188 const struct nvkm_msgqueue_func
*func
;
190 bool init_msg_received
;
191 struct completion init_done
;
193 struct mutex seq_lock
;
194 struct nvkm_msgqueue_seq seq
[NVKM_MSGQUEUE_NUM_SEQUENCES
];
195 unsigned long seq_tbl
[BITS_TO_LONGS(NVKM_MSGQUEUE_NUM_SEQUENCES
)];
198 void nvkm_msgqueue_ctor(const struct nvkm_msgqueue_func
*, struct nvkm_falcon
*,
199 struct nvkm_msgqueue
*);
200 int nvkm_msgqueue_post(struct nvkm_msgqueue
*, enum msgqueue_msg_priority
,
201 struct nvkm_msgqueue_hdr
*, nvkm_msgqueue_callback
,
202 struct completion
*, bool);
203 void nvkm_msgqueue_process_msgs(struct nvkm_msgqueue
*,
204 struct nvkm_msgqueue_queue
*);
206 int msgqueue_0137c63d_new(struct nvkm_falcon
*, const struct nvkm_secboot
*,
207 struct nvkm_msgqueue
**);
208 int msgqueue_0137bca5_new(struct nvkm_falcon
*, const struct nvkm_secboot
*,
209 struct nvkm_msgqueue
**);
210 int msgqueue_0148cdec_new(struct nvkm_falcon
*, const struct nvkm_secboot
*,
211 struct nvkm_msgqueue
**);