1 /* 2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. 3 * 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: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 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. 21 * 22 */ 23 #include "qmgr.h" 24 25 static void 26 msg_queue_open(struct nvkm_msgqueue_queue *queue) 27 { 28 mutex_lock(&queue->mutex); 29 queue->position = nvkm_falcon_rd32(queue->qmgr->falcon, queue->tail_reg); 30 } 31 32 static void 33 msg_queue_close(struct nvkm_msgqueue_queue *queue, bool commit) 34 { 35 struct nvkm_falcon *falcon = queue->qmgr->falcon; 36 37 if (commit) 38 nvkm_falcon_wr32(falcon, queue->tail_reg, queue->position); 39 40 mutex_unlock(&queue->mutex); 41 } 42 43 static bool 44 msg_queue_empty(struct nvkm_msgqueue_queue *queue) 45 { 46 u32 head = nvkm_falcon_rd32(queue->qmgr->falcon, queue->head_reg); 47 u32 tail = nvkm_falcon_rd32(queue->qmgr->falcon, queue->tail_reg); 48 return head == tail; 49 } 50 51 static int 52 msg_queue_pop(struct nvkm_msgqueue_queue *queue, void *data, u32 size) 53 { 54 struct nvkm_falcon *falcon = queue->qmgr->falcon; 55 u32 head, tail, available; 56 57 head = nvkm_falcon_rd32(falcon, queue->head_reg); 58 /* has the buffer looped? */ 59 if (head < queue->position) 60 queue->position = queue->offset; 61 62 tail = queue->position; 63 64 available = head - tail; 65 if (size > available) { 66 FLCNQ_ERR(queue, "requested %d bytes, but only %d available", 67 size, available); 68 return -EINVAL; 69 } 70 71 nvkm_falcon_read_dmem(falcon, tail, size, 0, data); 72 queue->position += ALIGN(size, QUEUE_ALIGNMENT); 73 return 0; 74 } 75 76 static int 77 msg_queue_read(struct nvkm_msgqueue_queue *queue, struct nv_falcon_msg *hdr) 78 { 79 int ret = 0; 80 81 msg_queue_open(queue); 82 83 if (msg_queue_empty(queue)) 84 goto close; 85 86 ret = msg_queue_pop(queue, hdr, HDR_SIZE); 87 if (ret) { 88 FLCNQ_ERR(queue, "failed to read message header"); 89 goto close; 90 } 91 92 if (hdr->size > MSG_BUF_SIZE) { 93 FLCNQ_ERR(queue, "message too big, %d bytes", hdr->size); 94 ret = -ENOSPC; 95 goto close; 96 } 97 98 if (hdr->size > HDR_SIZE) { 99 u32 read_size = hdr->size - HDR_SIZE; 100 101 ret = msg_queue_pop(queue, (hdr + 1), read_size); 102 if (ret) { 103 FLCNQ_ERR(queue, "failed to read message data"); 104 goto close; 105 } 106 } 107 108 ret = 1; 109 close: 110 msg_queue_close(queue, (ret >= 0)); 111 return ret; 112 } 113 114 static int 115 msgqueue_msg_handle(struct nvkm_falcon_msgq *msgq, struct nv_falcon_msg *hdr) 116 { 117 struct nvkm_falcon_qmgr_seq *seq; 118 119 seq = &msgq->qmgr->seq.id[hdr->seq_id]; 120 if (seq->state != SEQ_STATE_USED && seq->state != SEQ_STATE_CANCELLED) { 121 FLCNQ_ERR(msgq, "message for unknown sequence %08x", seq->id); 122 return -EINVAL; 123 } 124 125 if (seq->state == SEQ_STATE_USED) { 126 if (seq->callback) 127 seq->result = seq->callback(seq->priv, hdr); 128 } 129 130 if (seq->async) { 131 nvkm_falcon_qmgr_seq_release(msgq->qmgr, seq); 132 return 0; 133 } 134 135 complete_all(&seq->done); 136 return 0; 137 } 138 139 int 140 nvkm_falcon_msgq_recv_initmsg(struct nvkm_falcon_msgq *msgq, 141 void *data, u32 size) 142 { 143 struct nvkm_falcon *falcon = msgq->qmgr->falcon; 144 struct nv_falcon_msg *hdr = data; 145 int ret; 146 147 msgq->head_reg = falcon->func->msgq.head; 148 msgq->tail_reg = falcon->func->msgq.tail; 149 msgq->offset = nvkm_falcon_rd32(falcon, falcon->func->msgq.tail); 150 151 msg_queue_open(msgq); 152 ret = msg_queue_pop(msgq, data, size); 153 if (ret == 0 && hdr->size != size) { 154 FLCN_ERR(falcon, "unexpected init message size %d vs %d", 155 hdr->size, size); 156 ret = -EINVAL; 157 } 158 msg_queue_close(msgq, ret == 0); 159 return ret; 160 } 161 162 void 163 nvkm_msgqueue_process_msgs(struct nvkm_msgqueue *priv, 164 struct nvkm_msgqueue_queue *queue) 165 { 166 /* 167 * We are invoked from a worker thread, so normally we have plenty of 168 * stack space to work with. 169 */ 170 u8 msg_buffer[MSG_BUF_SIZE]; 171 struct nv_falcon_msg *hdr = (void *)msg_buffer; 172 173 while (msg_queue_read(queue, hdr) > 0) 174 msgqueue_msg_handle(queue, hdr); 175 } 176 177 void 178 nvkm_falcon_msgq_init(struct nvkm_falcon_msgq *msgq, 179 u32 index, u32 offset, u32 size) 180 { 181 const struct nvkm_falcon_func *func = msgq->qmgr->falcon->func; 182 183 msgq->head_reg = func->msgq.head + index * func->msgq.stride; 184 msgq->tail_reg = func->msgq.tail + index * func->msgq.stride; 185 msgq->offset = offset; 186 187 FLCNQ_DBG(msgq, "initialised @ index %d offset 0x%08x size 0x%08x", 188 index, msgq->offset, size); 189 } 190 191 void 192 nvkm_falcon_msgq_del(struct nvkm_falcon_msgq **pmsgq) 193 { 194 struct nvkm_falcon_msgq *msgq = *pmsgq; 195 if (msgq) { 196 kfree(*pmsgq); 197 *pmsgq = NULL; 198 } 199 } 200 201 int 202 nvkm_falcon_msgq_new(struct nvkm_falcon_qmgr *qmgr, const char *name, 203 struct nvkm_falcon_msgq **pmsgq) 204 { 205 struct nvkm_falcon_msgq *msgq = *pmsgq; 206 207 if (!(msgq = *pmsgq = kzalloc(sizeof(*msgq), GFP_KERNEL))) 208 return -ENOMEM; 209 210 msgq->qmgr = qmgr; 211 msgq->name = name; 212 mutex_init(&msgq->mutex); 213 return 0; 214 } 215