blob: 0318da208e629c871450dcf5a67baf7b36cca340 [file] [log] [blame]
Lokesh Vutla32cd2512018-08-27 15:57:32 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments System Control Interface Protocol Driver
4 * Based on drivers/firmware/ti_sci.c from Linux.
5 *
6 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
7 * Lokesh Vutla <lokeshvutla@ti.com>
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Lokesh Vutla32cd2512018-08-27 15:57:32 +053014#include <mailbox.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Lokesh Vutla32cd2512018-08-27 15:57:32 +053016#include <dm/device.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <dm/devres.h>
Simon Glasscd93d622020-05-10 11:40:13 -060019#include <linux/bitops.h>
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +053020#include <linux/compat.h>
Lokesh Vutla32cd2512018-08-27 15:57:32 +053021#include <linux/err.h>
22#include <linux/soc/ti/k3-sec-proxy.h>
23#include <linux/soc/ti/ti_sci_protocol.h>
24
25#include "ti_sci.h"
Vignesh Raghavendra0e811582021-06-07 19:47:48 +053026#include "ti_sci_static_data.h"
Lokesh Vutla32cd2512018-08-27 15:57:32 +053027
28/* List of all TI SCI devices active in system */
29static LIST_HEAD(ti_sci_list);
30
31/**
32 * struct ti_sci_xfer - Structure representing a message flow
33 * @tx_message: Transmit message
34 * @rx_len: Receive message length
35 */
36struct ti_sci_xfer {
37 struct k3_sec_proxy_msg tx_message;
38 u8 rx_len;
39};
40
41/**
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +053042 * struct ti_sci_rm_type_map - Structure representing TISCI Resource
43 * management representation of dev_ids.
44 * @dev_id: TISCI device ID
45 * @type: Corresponding id as identified by TISCI RM.
46 *
47 * Note: This is used only as a work around for using RM range apis
48 * for AM654 SoC. For future SoCs dev_id will be used as type
49 * for RM range APIs. In order to maintain ABI backward compatibility
50 * type is not being changed for AM654 SoC.
51 */
52struct ti_sci_rm_type_map {
53 u32 dev_id;
54 u16 type;
55};
56
57/**
Lokesh Vutla32cd2512018-08-27 15:57:32 +053058 * struct ti_sci_desc - Description of SoC integration
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +053059 * @default_host_id: Host identifier representing the compute entity
60 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
61 * @max_msgs: Maximum number of messages that can be pending
62 * simultaneously in the system
63 * @max_msg_size: Maximum size of data per message that can be handled.
Lokesh Vutla32cd2512018-08-27 15:57:32 +053064 */
65struct ti_sci_desc {
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +053066 u8 default_host_id;
67 int max_rx_timeout_ms;
68 int max_msgs;
Lokesh Vutla32cd2512018-08-27 15:57:32 +053069 int max_msg_size;
70};
71
72/**
73 * struct ti_sci_info - Structure representing a TI SCI instance
74 * @dev: Device pointer
75 * @desc: SoC description for this instance
76 * @handle: Instance of TI SCI handle to send to clients.
77 * @chan_tx: Transmit mailbox channel
78 * @chan_rx: Receive mailbox channel
79 * @xfer: xfer info
80 * @list: list head
81 * @is_secure: Determines if the communication is through secure threads.
82 * @host_id: Host identifier representing the compute entity
83 * @seq: Seq id used for verification for tx and rx message.
84 */
85struct ti_sci_info {
86 struct udevice *dev;
87 const struct ti_sci_desc *desc;
88 struct ti_sci_handle handle;
89 struct mbox_chan chan_tx;
90 struct mbox_chan chan_rx;
91 struct mbox_chan chan_notify;
92 struct ti_sci_xfer xfer;
93 struct list_head list;
Lokesh Vutla9566b772019-06-07 19:24:41 +053094 struct list_head dev_list;
Lokesh Vutla32cd2512018-08-27 15:57:32 +053095 bool is_secure;
96 u8 host_id;
97 u8 seq;
98};
99
Lokesh Vutla9566b772019-06-07 19:24:41 +0530100struct ti_sci_exclusive_dev {
101 u32 id;
102 u32 count;
103 struct list_head list;
104};
105
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530106#define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
107
108/**
109 * ti_sci_setup_one_xfer() - Setup one message type
110 * @info: Pointer to SCI entity information
111 * @msg_type: Message type
112 * @msg_flags: Flag to set for the message
113 * @buf: Buffer to be send to mailbox channel
114 * @tx_message_size: transmit message size
Andreas Dannenberg410adcc2019-06-07 19:24:40 +0530115 * @rx_message_size: receive message size. may be set to zero for send-only
116 * transactions.
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530117 *
118 * Helper function which is used by various command functions that are
119 * exposed to clients of this driver for allocating a message traffic event.
120 *
121 * Return: Corresponding ti_sci_xfer pointer if all went fine,
122 * else appropriate error pointer.
123 */
124static struct ti_sci_xfer *ti_sci_setup_one_xfer(struct ti_sci_info *info,
125 u16 msg_type, u32 msg_flags,
126 u32 *buf,
127 size_t tx_message_size,
128 size_t rx_message_size)
129{
130 struct ti_sci_xfer *xfer = &info->xfer;
131 struct ti_sci_msg_hdr *hdr;
132
133 /* Ensure we have sane transfer sizes */
134 if (rx_message_size > info->desc->max_msg_size ||
135 tx_message_size > info->desc->max_msg_size ||
Andreas Dannenberg410adcc2019-06-07 19:24:40 +0530136 (rx_message_size > 0 && rx_message_size < sizeof(*hdr)) ||
137 tx_message_size < sizeof(*hdr))
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530138 return ERR_PTR(-ERANGE);
139
140 info->seq = ~info->seq;
141 xfer->tx_message.buf = buf;
142 xfer->tx_message.len = tx_message_size;
143 xfer->rx_len = (u8)rx_message_size;
144
145 hdr = (struct ti_sci_msg_hdr *)buf;
146 hdr->seq = info->seq;
147 hdr->type = msg_type;
148 hdr->host = info->host_id;
149 hdr->flags = msg_flags;
150
151 return xfer;
152}
153
154/**
155 * ti_sci_get_response() - Receive response from mailbox channel
156 * @info: Pointer to SCI entity information
157 * @xfer: Transfer to initiate and wait for response
158 * @chan: Channel to receive the response
159 *
160 * Return: -ETIMEDOUT in case of no response, if transmit error,
161 * return corresponding error, else if all goes well,
162 * return 0.
163 */
164static inline int ti_sci_get_response(struct ti_sci_info *info,
165 struct ti_sci_xfer *xfer,
166 struct mbox_chan *chan)
167{
168 struct k3_sec_proxy_msg *msg = &xfer->tx_message;
169 struct ti_sci_secure_msg_hdr *secure_hdr;
170 struct ti_sci_msg_hdr *hdr;
171 int ret;
172
173 /* Receive the response */
Andreas Dannenberg32aebcf2019-04-24 14:20:08 -0500174 ret = mbox_recv(chan, msg, info->desc->max_rx_timeout_ms * 1000);
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530175 if (ret) {
176 dev_err(info->dev, "%s: Message receive failed. ret = %d\n",
177 __func__, ret);
178 return ret;
179 }
180
181 /* ToDo: Verify checksum */
182 if (info->is_secure) {
183 secure_hdr = (struct ti_sci_secure_msg_hdr *)msg->buf;
184 msg->buf = (u32 *)((void *)msg->buf + sizeof(*secure_hdr));
185 }
186
187 /* msg is updated by mailbox driver */
188 hdr = (struct ti_sci_msg_hdr *)msg->buf;
189
190 /* Sanity check for message response */
191 if (hdr->seq != info->seq) {
192 dev_dbg(info->dev, "%s: Message for %d is not expected\n",
193 __func__, hdr->seq);
194 return ret;
195 }
196
197 if (msg->len > info->desc->max_msg_size) {
198 dev_err(info->dev, "%s: Unable to handle %zu xfer (max %d)\n",
199 __func__, msg->len, info->desc->max_msg_size);
200 return -EINVAL;
201 }
202
203 if (msg->len < xfer->rx_len) {
204 dev_err(info->dev, "%s: Recv xfer %zu < expected %d length\n",
205 __func__, msg->len, xfer->rx_len);
206 }
207
208 return ret;
209}
210
211/**
212 * ti_sci_do_xfer() - Do one transfer
213 * @info: Pointer to SCI entity information
214 * @xfer: Transfer to initiate and wait for response
215 *
216 * Return: 0 if all went fine, else return appropriate error.
217 */
218static inline int ti_sci_do_xfer(struct ti_sci_info *info,
219 struct ti_sci_xfer *xfer)
220{
221 struct k3_sec_proxy_msg *msg = &xfer->tx_message;
222 u8 secure_buf[info->desc->max_msg_size];
223 struct ti_sci_secure_msg_hdr secure_hdr;
224 int ret;
225
226 if (info->is_secure) {
227 /* ToDo: get checksum of the entire message */
228 secure_hdr.checksum = 0;
229 secure_hdr.reserved = 0;
230 memcpy(&secure_buf[sizeof(secure_hdr)], xfer->tx_message.buf,
231 xfer->tx_message.len);
232
233 xfer->tx_message.buf = (u32 *)secure_buf;
234 xfer->tx_message.len += sizeof(secure_hdr);
Andreas Dannenberg410adcc2019-06-07 19:24:40 +0530235
236 if (xfer->rx_len)
237 xfer->rx_len += sizeof(secure_hdr);
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530238 }
239
240 /* Send the message */
241 ret = mbox_send(&info->chan_tx, msg);
242 if (ret) {
243 dev_err(info->dev, "%s: Message sending failed. ret = %d\n",
244 __func__, ret);
245 return ret;
246 }
247
Andreas Dannenberg410adcc2019-06-07 19:24:40 +0530248 /* Get response if requested */
249 if (xfer->rx_len)
250 ret = ti_sci_get_response(info, xfer, &info->chan_rx);
251
252 return ret;
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530253}
254
255/**
256 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
257 * @handle: pointer to TI SCI handle
258 *
259 * Updates the SCI information in the internal data structure.
260 *
261 * Return: 0 if all went fine, else return appropriate error.
262 */
263static int ti_sci_cmd_get_revision(struct ti_sci_handle *handle)
264{
265 struct ti_sci_msg_resp_version *rev_info;
266 struct ti_sci_version_info *ver;
267 struct ti_sci_msg_hdr hdr;
268 struct ti_sci_info *info;
269 struct ti_sci_xfer *xfer;
270 int ret;
271
272 if (IS_ERR(handle))
273 return PTR_ERR(handle);
274 if (!handle)
275 return -EINVAL;
276
277 info = handle_to_ti_sci_info(handle);
278
Andrew F. Davisefbfd442019-04-29 09:04:11 -0400279 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_VERSION,
280 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
Lokesh Vutla32cd2512018-08-27 15:57:32 +0530281 (u32 *)&hdr, sizeof(struct ti_sci_msg_hdr),
282 sizeof(*rev_info));
283 if (IS_ERR(xfer)) {
284 ret = PTR_ERR(xfer);
285 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
286 return ret;
287 }
288
289 ret = ti_sci_do_xfer(info, xfer);
290 if (ret) {
291 dev_err(info->dev, "Mbox communication fail %d\n", ret);
292 return ret;
293 }
294
295 rev_info = (struct ti_sci_msg_resp_version *)xfer->tx_message.buf;
296
297 ver = &handle->version;
298 ver->abi_major = rev_info->abi_major;
299 ver->abi_minor = rev_info->abi_minor;
300 ver->firmware_revision = rev_info->firmware_revision;
301 strncpy(ver->firmware_description, rev_info->firmware_description,
302 sizeof(ver->firmware_description));
303
304 return 0;
305}
306
307/**
308 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
309 * @r: pointer to response buffer
310 *
311 * Return: true if the response was an ACK, else returns false.
312 */
313static inline bool ti_sci_is_response_ack(void *r)
314{
315 struct ti_sci_msg_hdr *hdr = r;
316
317 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
318}
319
320/**
Andreas Dannenbergdcfc52a2018-08-27 15:57:33 +0530321 * cmd_set_board_config_using_msg() - Common command to send board configuration
322 * message
323 * @handle: pointer to TI SCI handle
324 * @msg_type: One of the TISCI message types to set board configuration
325 * @addr: Address where the board config structure is located
326 * @size: Size of the board config structure
327 *
328 * Return: 0 if all went well, else returns appropriate error value.
329 */
330static int cmd_set_board_config_using_msg(const struct ti_sci_handle *handle,
331 u16 msg_type, u64 addr, u32 size)
332{
333 struct ti_sci_msg_board_config req;
334 struct ti_sci_msg_hdr *resp;
335 struct ti_sci_info *info;
336 struct ti_sci_xfer *xfer;
337 int ret = 0;
338
339 if (IS_ERR(handle))
340 return PTR_ERR(handle);
341 if (!handle)
342 return -EINVAL;
343
344 info = handle_to_ti_sci_info(handle);
345
346 xfer = ti_sci_setup_one_xfer(info, msg_type,
347 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
348 (u32 *)&req, sizeof(req), sizeof(*resp));
349 if (IS_ERR(xfer)) {
350 ret = PTR_ERR(xfer);
351 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
352 return ret;
353 }
354 req.boardcfgp_high = (addr >> 32) & 0xffffffff;
355 req.boardcfgp_low = addr & 0xffffffff;
356 req.boardcfg_size = size;
357
358 ret = ti_sci_do_xfer(info, xfer);
359 if (ret) {
360 dev_err(info->dev, "Mbox send fail %d\n", ret);
361 return ret;
362 }
363
364 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
365
366 if (!ti_sci_is_response_ack(resp))
367 return -ENODEV;
368
369 return ret;
370}
371
372/**
373 * ti_sci_cmd_set_board_config() - Command to send board configuration message
374 * @handle: pointer to TI SCI handle
375 * @addr: Address where the board config structure is located
376 * @size: Size of the board config structure
377 *
378 * Return: 0 if all went well, else returns appropriate error value.
379 */
380static int ti_sci_cmd_set_board_config(const struct ti_sci_handle *handle,
381 u64 addr, u32 size)
382{
383 return cmd_set_board_config_using_msg(handle,
384 TI_SCI_MSG_BOARD_CONFIG,
385 addr, size);
386}
387
388/**
389 * ti_sci_cmd_set_board_config_rm() - Command to send board resource
390 * management configuration
391 * @handle: pointer to TI SCI handle
392 * @addr: Address where the board RM config structure is located
393 * @size: Size of the RM config structure
394 *
395 * Return: 0 if all went well, else returns appropriate error value.
396 */
397static
398int ti_sci_cmd_set_board_config_rm(const struct ti_sci_handle *handle,
399 u64 addr, u32 size)
400{
401 return cmd_set_board_config_using_msg(handle,
402 TI_SCI_MSG_BOARD_CONFIG_RM,
403 addr, size);
404}
405
406/**
407 * ti_sci_cmd_set_board_config_security() - Command to send board security
408 * configuration message
409 * @handle: pointer to TI SCI handle
410 * @addr: Address where the board security config structure is located
411 * @size: Size of the security config structure
412 *
413 * Return: 0 if all went well, else returns appropriate error value.
414 */
415static
416int ti_sci_cmd_set_board_config_security(const struct ti_sci_handle *handle,
417 u64 addr, u32 size)
418{
419 return cmd_set_board_config_using_msg(handle,
420 TI_SCI_MSG_BOARD_CONFIG_SECURITY,
421 addr, size);
422}
423
424/**
425 * ti_sci_cmd_set_board_config_pm() - Command to send board power and clock
426 * configuration message
427 * @handle: pointer to TI SCI handle
428 * @addr: Address where the board PM config structure is located
429 * @size: Size of the PM config structure
430 *
431 * Return: 0 if all went well, else returns appropriate error value.
432 */
433static int ti_sci_cmd_set_board_config_pm(const struct ti_sci_handle *handle,
434 u64 addr, u32 size)
435{
436 return cmd_set_board_config_using_msg(handle,
437 TI_SCI_MSG_BOARD_CONFIG_PM,
438 addr, size);
439}
440
Lokesh Vutla9566b772019-06-07 19:24:41 +0530441static struct ti_sci_exclusive_dev
442*ti_sci_get_exclusive_dev(struct list_head *dev_list, u32 id)
443{
444 struct ti_sci_exclusive_dev *dev;
445
446 list_for_each_entry(dev, dev_list, list)
447 if (dev->id == id)
448 return dev;
449
450 return NULL;
451}
452
453static void ti_sci_add_exclusive_dev(struct ti_sci_info *info, u32 id)
454{
455 struct ti_sci_exclusive_dev *dev;
456
457 dev = ti_sci_get_exclusive_dev(&info->dev_list, id);
458 if (dev) {
459 dev->count++;
460 return;
461 }
462
463 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
464 dev->id = id;
465 dev->count = 1;
466 INIT_LIST_HEAD(&dev->list);
467 list_add_tail(&dev->list, &info->dev_list);
468}
469
470static void ti_sci_delete_exclusive_dev(struct ti_sci_info *info, u32 id)
471{
472 struct ti_sci_exclusive_dev *dev;
473
474 dev = ti_sci_get_exclusive_dev(&info->dev_list, id);
475 if (!dev)
476 return;
477
478 if (dev->count > 0)
479 dev->count--;
480}
481
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530482/**
483 * ti_sci_set_device_state() - Set device state helper
484 * @handle: pointer to TI SCI handle
485 * @id: Device identifier
486 * @flags: flags to setup for the device
487 * @state: State to move the device to
488 *
489 * Return: 0 if all went well, else returns appropriate error value.
490 */
491static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
492 u32 id, u32 flags, u8 state)
493{
494 struct ti_sci_msg_req_set_device_state req;
495 struct ti_sci_msg_hdr *resp;
496 struct ti_sci_info *info;
497 struct ti_sci_xfer *xfer;
498 int ret = 0;
499
500 if (IS_ERR(handle))
501 return PTR_ERR(handle);
502 if (!handle)
503 return -EINVAL;
504
505 info = handle_to_ti_sci_info(handle);
506
507 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
508 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
509 (u32 *)&req, sizeof(req), sizeof(*resp));
510 if (IS_ERR(xfer)) {
511 ret = PTR_ERR(xfer);
512 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
513 return ret;
514 }
515 req.id = id;
516 req.state = state;
517
518 ret = ti_sci_do_xfer(info, xfer);
519 if (ret) {
520 dev_err(info->dev, "Mbox send fail %d\n", ret);
521 return ret;
522 }
523
524 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
525
526 if (!ti_sci_is_response_ack(resp))
527 return -ENODEV;
528
Lokesh Vutla9566b772019-06-07 19:24:41 +0530529 if (state == MSG_DEVICE_SW_STATE_AUTO_OFF)
530 ti_sci_delete_exclusive_dev(info, id);
531 else if (flags & MSG_FLAG_DEVICE_EXCLUSIVE)
532 ti_sci_add_exclusive_dev(info, id);
533
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530534 return ret;
535}
536
537/**
Andreas Dannenberg410adcc2019-06-07 19:24:40 +0530538 * ti_sci_set_device_state_no_wait() - Set device state helper without
539 * requesting or waiting for a response.
540 * @handle: pointer to TI SCI handle
541 * @id: Device identifier
542 * @flags: flags to setup for the device
543 * @state: State to move the device to
544 *
545 * Return: 0 if all went well, else returns appropriate error value.
546 */
547static int ti_sci_set_device_state_no_wait(const struct ti_sci_handle *handle,
548 u32 id, u32 flags, u8 state)
549{
550 struct ti_sci_msg_req_set_device_state req;
551 struct ti_sci_info *info;
552 struct ti_sci_xfer *xfer;
553 int ret = 0;
554
555 if (IS_ERR(handle))
556 return PTR_ERR(handle);
557 if (!handle)
558 return -EINVAL;
559
560 info = handle_to_ti_sci_info(handle);
561
562 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
563 flags | TI_SCI_FLAG_REQ_GENERIC_NORESPONSE,
564 (u32 *)&req, sizeof(req), 0);
565 if (IS_ERR(xfer)) {
566 ret = PTR_ERR(xfer);
567 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
568 return ret;
569 }
570 req.id = id;
571 req.state = state;
572
573 ret = ti_sci_do_xfer(info, xfer);
574 if (ret)
575 dev_err(info->dev, "Mbox send fail %d\n", ret);
576
577 return ret;
578}
579
580/**
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530581 * ti_sci_get_device_state() - Get device state helper
582 * @handle: Handle to the device
583 * @id: Device Identifier
584 * @clcnt: Pointer to Context Loss Count
585 * @resets: pointer to resets
586 * @p_state: pointer to p_state
587 * @c_state: pointer to c_state
588 *
589 * Return: 0 if all went fine, else return appropriate error.
590 */
591static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
592 u32 id, u32 *clcnt, u32 *resets,
593 u8 *p_state, u8 *c_state)
594{
595 struct ti_sci_msg_resp_get_device_state *resp;
596 struct ti_sci_msg_req_get_device_state req;
597 struct ti_sci_info *info;
598 struct ti_sci_xfer *xfer;
599 int ret = 0;
600
601 if (IS_ERR(handle))
602 return PTR_ERR(handle);
603 if (!handle)
604 return -EINVAL;
605
606 if (!clcnt && !resets && !p_state && !c_state)
607 return -EINVAL;
608
609 info = handle_to_ti_sci_info(handle);
610
Andrew F. Davisefbfd442019-04-29 09:04:11 -0400611 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
612 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530613 (u32 *)&req, sizeof(req), sizeof(*resp));
614 if (IS_ERR(xfer)) {
615 ret = PTR_ERR(xfer);
616 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
617 return ret;
618 }
619 req.id = id;
620
621 ret = ti_sci_do_xfer(info, xfer);
622 if (ret) {
Sean Andersone5792302020-09-15 10:44:38 -0400623 dev_err(info->dev, "Mbox send fail %d\n", ret);
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530624 return ret;
625 }
626
627 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->tx_message.buf;
628 if (!ti_sci_is_response_ack(resp))
629 return -ENODEV;
630
631 if (clcnt)
632 *clcnt = resp->context_loss_count;
633 if (resets)
634 *resets = resp->resets;
635 if (p_state)
636 *p_state = resp->programmed_state;
637 if (c_state)
638 *c_state = resp->current_state;
639
640 return ret;
641}
642
643/**
644 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
645 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
646 * @id: Device Identifier
647 *
648 * Request for the device - NOTE: the client MUST maintain integrity of
649 * usage count by balancing get_device with put_device. No refcounting is
650 * managed by driver for that purpose.
651 *
652 * NOTE: The request is for exclusive access for the processor.
653 *
654 * Return: 0 if all went fine, else return appropriate error.
655 */
656static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
657{
Lokesh Vutlaae0b8a22019-06-07 19:24:39 +0530658 return ti_sci_set_device_state(handle, id, 0,
659 MSG_DEVICE_SW_STATE_ON);
660}
661
662static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
663 u32 id)
664{
665 return ti_sci_set_device_state(handle, id, MSG_FLAG_DEVICE_EXCLUSIVE,
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530666 MSG_DEVICE_SW_STATE_ON);
667}
668
669/**
670 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
671 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
672 * @id: Device Identifier
673 *
674 * Request for the device - NOTE: the client MUST maintain integrity of
675 * usage count by balancing get_device with put_device. No refcounting is
676 * managed by driver for that purpose.
677 *
678 * Return: 0 if all went fine, else return appropriate error.
679 */
680static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
681{
682 return ti_sci_set_device_state(handle, id,
Lokesh Vutlaae0b8a22019-06-07 19:24:39 +0530683 0,
684 MSG_DEVICE_SW_STATE_RETENTION);
685}
686
687static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
688 u32 id)
689{
690 return ti_sci_set_device_state(handle, id, MSG_FLAG_DEVICE_EXCLUSIVE,
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530691 MSG_DEVICE_SW_STATE_RETENTION);
692}
693
694/**
695 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
696 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
697 * @id: Device Identifier
698 *
699 * Request for the device - NOTE: the client MUST maintain integrity of
700 * usage count by balancing get_device with put_device. No refcounting is
701 * managed by driver for that purpose.
702 *
703 * Return: 0 if all went fine, else return appropriate error.
704 */
705static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
706{
Lokesh Vutlaae0b8a22019-06-07 19:24:39 +0530707 return ti_sci_set_device_state(handle, id, 0,
708 MSG_DEVICE_SW_STATE_AUTO_OFF);
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530709}
710
Lokesh Vutla9566b772019-06-07 19:24:41 +0530711static
712int ti_sci_cmd_release_exclusive_devices(const struct ti_sci_handle *handle)
713{
714 struct ti_sci_exclusive_dev *dev, *tmp;
715 struct ti_sci_info *info;
716 int i, cnt;
717
718 info = handle_to_ti_sci_info(handle);
719
720 list_for_each_entry_safe(dev, tmp, &info->dev_list, list) {
721 cnt = dev->count;
722 debug("%s: id = %d, cnt = %d\n", __func__, dev->id, cnt);
723 for (i = 0; i < cnt; i++)
724 ti_sci_cmd_put_device(handle, dev->id);
725 }
726
727 return 0;
728}
729
Andreas Dannenberg7bc33042018-08-27 15:57:34 +0530730/**
731 * ti_sci_cmd_dev_is_valid() - Is the device valid
732 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
733 * @id: Device Identifier
734 *
735 * Return: 0 if all went fine and the device ID is valid, else return
736 * appropriate error.
737 */
738static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
739{
740 u8 unused;
741
742 /* check the device state which will also tell us if the ID is valid */
743 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
744}
745
746/**
747 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
748 * @handle: Pointer to TISCI handle
749 * @id: Device Identifier
750 * @count: Pointer to Context Loss counter to populate
751 *
752 * Return: 0 if all went fine, else return appropriate error.
753 */
754static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
755 u32 *count)
756{
757 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
758}
759
760/**
761 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
762 * @handle: Pointer to TISCI handle
763 * @id: Device Identifier
764 * @r_state: true if requested to be idle
765 *
766 * Return: 0 if all went fine, else return appropriate error.
767 */
768static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
769 bool *r_state)
770{
771 int ret;
772 u8 state;
773
774 if (!r_state)
775 return -EINVAL;
776
777 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
778 if (ret)
779 return ret;
780
781 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
782
783 return 0;
784}
785
786/**
787 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
788 * @handle: Pointer to TISCI handle
789 * @id: Device Identifier
790 * @r_state: true if requested to be stopped
791 * @curr_state: true if currently stopped.
792 *
793 * Return: 0 if all went fine, else return appropriate error.
794 */
795static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
796 bool *r_state, bool *curr_state)
797{
798 int ret;
799 u8 p_state, c_state;
800
801 if (!r_state && !curr_state)
802 return -EINVAL;
803
804 ret =
805 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
806 if (ret)
807 return ret;
808
809 if (r_state)
810 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
811 if (curr_state)
812 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
813
814 return 0;
815}
816
817/**
818 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
819 * @handle: Pointer to TISCI handle
820 * @id: Device Identifier
821 * @r_state: true if requested to be ON
822 * @curr_state: true if currently ON and active
823 *
824 * Return: 0 if all went fine, else return appropriate error.
825 */
826static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
827 bool *r_state, bool *curr_state)
828{
829 int ret;
830 u8 p_state, c_state;
831
832 if (!r_state && !curr_state)
833 return -EINVAL;
834
835 ret =
836 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
837 if (ret)
838 return ret;
839
840 if (r_state)
841 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
842 if (curr_state)
843 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
844
845 return 0;
846}
847
848/**
849 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
850 * @handle: Pointer to TISCI handle
851 * @id: Device Identifier
852 * @curr_state: true if currently transitioning.
853 *
854 * Return: 0 if all went fine, else return appropriate error.
855 */
856static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
857 bool *curr_state)
858{
859 int ret;
860 u8 state;
861
862 if (!curr_state)
863 return -EINVAL;
864
865 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
866 if (ret)
867 return ret;
868
869 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
870
871 return 0;
872}
873
874/**
875 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
876 * by TISCI
877 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
878 * @id: Device Identifier
879 * @reset_state: Device specific reset bit field
880 *
881 * Return: 0 if all went fine, else return appropriate error.
882 */
883static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
884 u32 id, u32 reset_state)
885{
886 struct ti_sci_msg_req_set_device_resets req;
887 struct ti_sci_msg_hdr *resp;
888 struct ti_sci_info *info;
889 struct ti_sci_xfer *xfer;
890 int ret = 0;
891
892 if (IS_ERR(handle))
893 return PTR_ERR(handle);
894 if (!handle)
895 return -EINVAL;
896
897 info = handle_to_ti_sci_info(handle);
898
899 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
900 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
901 (u32 *)&req, sizeof(req), sizeof(*resp));
902 if (IS_ERR(xfer)) {
903 ret = PTR_ERR(xfer);
904 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
905 return ret;
906 }
907 req.id = id;
908 req.resets = reset_state;
909
910 ret = ti_sci_do_xfer(info, xfer);
911 if (ret) {
912 dev_err(info->dev, "Mbox send fail %d\n", ret);
913 return ret;
914 }
915
916 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
917
918 if (!ti_sci_is_response_ack(resp))
919 return -ENODEV;
920
921 return ret;
922}
923
924/**
925 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
926 * by TISCI
927 * @handle: Pointer to TISCI handle
928 * @id: Device Identifier
929 * @reset_state: Pointer to reset state to populate
930 *
931 * Return: 0 if all went fine, else return appropriate error.
932 */
933static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
934 u32 id, u32 *reset_state)
935{
936 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
937 NULL);
938}
939
Lokesh Vutla9b871812018-08-27 15:57:35 +0530940/**
941 * ti_sci_set_clock_state() - Set clock state helper
942 * @handle: pointer to TI SCI handle
943 * @dev_id: Device identifier this request is for
944 * @clk_id: Clock identifier for the device for this request.
945 * Each device has it's own set of clock inputs. This indexes
946 * which clock input to modify.
947 * @flags: Header flags as needed
948 * @state: State to request for the clock.
949 *
950 * Return: 0 if all went well, else returns appropriate error value.
951 */
952static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
953 u32 dev_id, u8 clk_id,
954 u32 flags, u8 state)
955{
956 struct ti_sci_msg_req_set_clock_state req;
957 struct ti_sci_msg_hdr *resp;
958 struct ti_sci_info *info;
959 struct ti_sci_xfer *xfer;
960 int ret = 0;
961
962 if (IS_ERR(handle))
963 return PTR_ERR(handle);
964 if (!handle)
965 return -EINVAL;
966
967 info = handle_to_ti_sci_info(handle);
968
969 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
970 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
971 (u32 *)&req, sizeof(req), sizeof(*resp));
972 if (IS_ERR(xfer)) {
973 ret = PTR_ERR(xfer);
974 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
975 return ret;
976 }
977 req.dev_id = dev_id;
978 req.clk_id = clk_id;
979 req.request_state = state;
980
981 ret = ti_sci_do_xfer(info, xfer);
982 if (ret) {
983 dev_err(info->dev, "Mbox send fail %d\n", ret);
984 return ret;
985 }
986
987 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
988
989 if (!ti_sci_is_response_ack(resp))
990 return -ENODEV;
991
992 return ret;
993}
994
995/**
996 * ti_sci_cmd_get_clock_state() - Get clock state helper
997 * @handle: pointer to TI SCI handle
998 * @dev_id: Device identifier this request is for
999 * @clk_id: Clock identifier for the device for this request.
1000 * Each device has it's own set of clock inputs. This indexes
1001 * which clock input to modify.
1002 * @programmed_state: State requested for clock to move to
1003 * @current_state: State that the clock is currently in
1004 *
1005 * Return: 0 if all went well, else returns appropriate error value.
1006 */
1007static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
1008 u32 dev_id, u8 clk_id,
1009 u8 *programmed_state, u8 *current_state)
1010{
1011 struct ti_sci_msg_resp_get_clock_state *resp;
1012 struct ti_sci_msg_req_get_clock_state req;
1013 struct ti_sci_info *info;
1014 struct ti_sci_xfer *xfer;
1015 int ret = 0;
1016
1017 if (IS_ERR(handle))
1018 return PTR_ERR(handle);
1019 if (!handle)
1020 return -EINVAL;
1021
1022 if (!programmed_state && !current_state)
1023 return -EINVAL;
1024
1025 info = handle_to_ti_sci_info(handle);
1026
1027 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1028 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1029 (u32 *)&req, sizeof(req), sizeof(*resp));
1030 if (IS_ERR(xfer)) {
1031 ret = PTR_ERR(xfer);
1032 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1033 return ret;
1034 }
1035 req.dev_id = dev_id;
1036 req.clk_id = clk_id;
1037
1038 ret = ti_sci_do_xfer(info, xfer);
1039 if (ret) {
1040 dev_err(info->dev, "Mbox send fail %d\n", ret);
1041 return ret;
1042 }
1043
1044 resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->tx_message.buf;
1045
1046 if (!ti_sci_is_response_ack(resp))
1047 return -ENODEV;
1048
1049 if (programmed_state)
1050 *programmed_state = resp->programmed_state;
1051 if (current_state)
1052 *current_state = resp->current_state;
1053
1054 return ret;
1055}
1056
1057/**
1058 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1059 * @handle: pointer to TI SCI handle
1060 * @dev_id: Device identifier this request is for
1061 * @clk_id: Clock identifier for the device for this request.
1062 * Each device has it's own set of clock inputs. This indexes
1063 * which clock input to modify.
1064 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1065 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1066 * @enable_input_term: 'true' if input termination is desired, else 'false'
1067 *
1068 * Return: 0 if all went well, else returns appropriate error value.
1069 */
1070static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1071 u8 clk_id, bool needs_ssc, bool can_change_freq,
1072 bool enable_input_term)
1073{
1074 u32 flags = 0;
1075
1076 flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1077 flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1078 flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1079
1080 return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1081 MSG_CLOCK_SW_STATE_REQ);
1082}
1083
1084/**
1085 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1086 * @handle: pointer to TI SCI handle
1087 * @dev_id: Device identifier this request is for
1088 * @clk_id: Clock identifier for the device for this request.
1089 * Each device has it's own set of clock inputs. This indexes
1090 * which clock input to modify.
1091 *
1092 * NOTE: This clock must have been requested by get_clock previously.
1093 *
1094 * Return: 0 if all went well, else returns appropriate error value.
1095 */
1096static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1097 u32 dev_id, u8 clk_id)
1098{
1099 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1100 MSG_CLOCK_SW_STATE_UNREQ);
1101}
1102
1103/**
1104 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1105 * @handle: pointer to TI SCI handle
1106 * @dev_id: Device identifier this request is for
1107 * @clk_id: Clock identifier for the device for this request.
1108 * Each device has it's own set of clock inputs. This indexes
1109 * which clock input to modify.
1110 *
1111 * NOTE: This clock must have been requested by get_clock previously.
1112 *
1113 * Return: 0 if all went well, else returns appropriate error value.
1114 */
1115static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1116 u32 dev_id, u8 clk_id)
1117{
1118 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1119 MSG_CLOCK_SW_STATE_AUTO);
1120}
1121
1122/**
1123 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1124 * @handle: pointer to TI SCI handle
1125 * @dev_id: Device identifier this request is for
1126 * @clk_id: Clock identifier for the device for this request.
1127 * Each device has it's own set of clock inputs. This indexes
1128 * which clock input to modify.
1129 * @req_state: state indicating if the clock is auto managed
1130 *
1131 * Return: 0 if all went well, else returns appropriate error value.
1132 */
1133static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1134 u32 dev_id, u8 clk_id, bool *req_state)
1135{
1136 u8 state = 0;
1137 int ret;
1138
1139 if (!req_state)
1140 return -EINVAL;
1141
1142 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1143 if (ret)
1144 return ret;
1145
1146 *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1147 return 0;
1148}
1149
1150/**
1151 * ti_sci_cmd_clk_is_on() - Is the clock ON
1152 * @handle: pointer to TI SCI handle
1153 * @dev_id: Device identifier this request is for
1154 * @clk_id: Clock identifier for the device for this request.
1155 * Each device has it's own set of clock inputs. This indexes
1156 * which clock input to modify.
1157 * @req_state: state indicating if the clock is managed by us and enabled
1158 * @curr_state: state indicating if the clock is ready for operation
1159 *
1160 * Return: 0 if all went well, else returns appropriate error value.
1161 */
1162static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1163 u8 clk_id, bool *req_state, bool *curr_state)
1164{
1165 u8 c_state = 0, r_state = 0;
1166 int ret;
1167
1168 if (!req_state && !curr_state)
1169 return -EINVAL;
1170
1171 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1172 &r_state, &c_state);
1173 if (ret)
1174 return ret;
1175
1176 if (req_state)
1177 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1178 if (curr_state)
1179 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1180 return 0;
1181}
1182
1183/**
1184 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1185 * @handle: pointer to TI SCI handle
1186 * @dev_id: Device identifier this request is for
1187 * @clk_id: Clock identifier for the device for this request.
1188 * Each device has it's own set of clock inputs. This indexes
1189 * which clock input to modify.
1190 * @req_state: state indicating if the clock is managed by us and disabled
1191 * @curr_state: state indicating if the clock is NOT ready for operation
1192 *
1193 * Return: 0 if all went well, else returns appropriate error value.
1194 */
1195static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1196 u8 clk_id, bool *req_state, bool *curr_state)
1197{
1198 u8 c_state = 0, r_state = 0;
1199 int ret;
1200
1201 if (!req_state && !curr_state)
1202 return -EINVAL;
1203
1204 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1205 &r_state, &c_state);
1206 if (ret)
1207 return ret;
1208
1209 if (req_state)
1210 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1211 if (curr_state)
1212 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1213 return 0;
1214}
1215
1216/**
1217 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1218 * @handle: pointer to TI SCI handle
1219 * @dev_id: Device identifier this request is for
1220 * @clk_id: Clock identifier for the device for this request.
1221 * Each device has it's own set of clock inputs. This indexes
1222 * which clock input to modify.
1223 * @parent_id: Parent clock identifier to set
1224 *
1225 * Return: 0 if all went well, else returns appropriate error value.
1226 */
1227static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1228 u32 dev_id, u8 clk_id, u8 parent_id)
1229{
1230 struct ti_sci_msg_req_set_clock_parent req;
1231 struct ti_sci_msg_hdr *resp;
1232 struct ti_sci_info *info;
1233 struct ti_sci_xfer *xfer;
1234 int ret = 0;
1235
1236 if (IS_ERR(handle))
1237 return PTR_ERR(handle);
1238 if (!handle)
1239 return -EINVAL;
1240
1241 info = handle_to_ti_sci_info(handle);
1242
1243 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1244 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1245 (u32 *)&req, sizeof(req), sizeof(*resp));
1246 if (IS_ERR(xfer)) {
1247 ret = PTR_ERR(xfer);
1248 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1249 return ret;
1250 }
1251 req.dev_id = dev_id;
1252 req.clk_id = clk_id;
1253 req.parent_id = parent_id;
1254
1255 ret = ti_sci_do_xfer(info, xfer);
1256 if (ret) {
1257 dev_err(info->dev, "Mbox send fail %d\n", ret);
1258 return ret;
1259 }
1260
1261 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1262
1263 if (!ti_sci_is_response_ack(resp))
1264 return -ENODEV;
1265
1266 return ret;
1267}
1268
1269/**
1270 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1271 * @handle: pointer to TI SCI handle
1272 * @dev_id: Device identifier this request is for
1273 * @clk_id: Clock identifier for the device for this request.
1274 * Each device has it's own set of clock inputs. This indexes
1275 * which clock input to modify.
1276 * @parent_id: Current clock parent
1277 *
1278 * Return: 0 if all went well, else returns appropriate error value.
1279 */
1280static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1281 u32 dev_id, u8 clk_id, u8 *parent_id)
1282{
1283 struct ti_sci_msg_resp_get_clock_parent *resp;
1284 struct ti_sci_msg_req_get_clock_parent req;
1285 struct ti_sci_info *info;
1286 struct ti_sci_xfer *xfer;
1287 int ret = 0;
1288
1289 if (IS_ERR(handle))
1290 return PTR_ERR(handle);
1291 if (!handle || !parent_id)
1292 return -EINVAL;
1293
1294 info = handle_to_ti_sci_info(handle);
1295
1296 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1297 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1298 (u32 *)&req, sizeof(req), sizeof(*resp));
1299 if (IS_ERR(xfer)) {
1300 ret = PTR_ERR(xfer);
1301 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1302 return ret;
1303 }
1304 req.dev_id = dev_id;
1305 req.clk_id = clk_id;
1306
1307 ret = ti_sci_do_xfer(info, xfer);
1308 if (ret) {
1309 dev_err(info->dev, "Mbox send fail %d\n", ret);
1310 return ret;
1311 }
1312
1313 resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->tx_message.buf;
1314
1315 if (!ti_sci_is_response_ack(resp))
1316 ret = -ENODEV;
1317 else
1318 *parent_id = resp->parent_id;
1319
1320 return ret;
1321}
1322
1323/**
1324 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1325 * @handle: pointer to TI SCI handle
1326 * @dev_id: Device identifier this request is for
1327 * @clk_id: Clock identifier for the device for this request.
1328 * Each device has it's own set of clock inputs. This indexes
1329 * which clock input to modify.
1330 * @num_parents: Returns he number of parents to the current clock.
1331 *
1332 * Return: 0 if all went well, else returns appropriate error value.
1333 */
1334static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1335 u32 dev_id, u8 clk_id,
1336 u8 *num_parents)
1337{
1338 struct ti_sci_msg_resp_get_clock_num_parents *resp;
1339 struct ti_sci_msg_req_get_clock_num_parents req;
1340 struct ti_sci_info *info;
1341 struct ti_sci_xfer *xfer;
1342 int ret = 0;
1343
1344 if (IS_ERR(handle))
1345 return PTR_ERR(handle);
1346 if (!handle || !num_parents)
1347 return -EINVAL;
1348
1349 info = handle_to_ti_sci_info(handle);
1350
1351 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1352 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1353 (u32 *)&req, sizeof(req), sizeof(*resp));
1354 if (IS_ERR(xfer)) {
1355 ret = PTR_ERR(xfer);
1356 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1357 return ret;
1358 }
1359 req.dev_id = dev_id;
1360 req.clk_id = clk_id;
1361
1362 ret = ti_sci_do_xfer(info, xfer);
1363 if (ret) {
1364 dev_err(info->dev, "Mbox send fail %d\n", ret);
1365 return ret;
1366 }
1367
1368 resp = (struct ti_sci_msg_resp_get_clock_num_parents *)
1369 xfer->tx_message.buf;
1370
1371 if (!ti_sci_is_response_ack(resp))
1372 ret = -ENODEV;
1373 else
1374 *num_parents = resp->num_parents;
1375
1376 return ret;
1377}
1378
1379/**
1380 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1381 * @handle: pointer to TI SCI handle
1382 * @dev_id: Device identifier this request is for
1383 * @clk_id: Clock identifier for the device for this request.
1384 * Each device has it's own set of clock inputs. This indexes
1385 * which clock input to modify.
1386 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1387 * allowable programmed frequency and does not account for clock
1388 * tolerances and jitter.
1389 * @target_freq: The target clock frequency in Hz. A frequency will be
1390 * processed as close to this target frequency as possible.
1391 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1392 * allowable programmed frequency and does not account for clock
1393 * tolerances and jitter.
1394 * @match_freq: Frequency match in Hz response.
1395 *
1396 * Return: 0 if all went well, else returns appropriate error value.
1397 */
1398static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1399 u32 dev_id, u8 clk_id, u64 min_freq,
1400 u64 target_freq, u64 max_freq,
1401 u64 *match_freq)
1402{
1403 struct ti_sci_msg_resp_query_clock_freq *resp;
1404 struct ti_sci_msg_req_query_clock_freq req;
1405 struct ti_sci_info *info;
1406 struct ti_sci_xfer *xfer;
1407 int ret = 0;
1408
1409 if (IS_ERR(handle))
1410 return PTR_ERR(handle);
1411 if (!handle || !match_freq)
1412 return -EINVAL;
1413
1414 info = handle_to_ti_sci_info(handle);
1415
1416 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1417 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1418 (u32 *)&req, sizeof(req), sizeof(*resp));
1419 if (IS_ERR(xfer)) {
1420 ret = PTR_ERR(xfer);
1421 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1422 return ret;
1423 }
1424 req.dev_id = dev_id;
1425 req.clk_id = clk_id;
1426 req.min_freq_hz = min_freq;
1427 req.target_freq_hz = target_freq;
1428 req.max_freq_hz = max_freq;
1429
1430 ret = ti_sci_do_xfer(info, xfer);
1431 if (ret) {
1432 dev_err(info->dev, "Mbox send fail %d\n", ret);
1433 return ret;
1434 }
1435
1436 resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->tx_message.buf;
1437
1438 if (!ti_sci_is_response_ack(resp))
1439 ret = -ENODEV;
1440 else
1441 *match_freq = resp->freq_hz;
1442
1443 return ret;
1444}
1445
1446/**
1447 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1448 * @handle: pointer to TI SCI handle
1449 * @dev_id: Device identifier this request is for
1450 * @clk_id: Clock identifier for the device for this request.
1451 * Each device has it's own set of clock inputs. This indexes
1452 * which clock input to modify.
1453 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1454 * allowable programmed frequency and does not account for clock
1455 * tolerances and jitter.
1456 * @target_freq: The target clock frequency in Hz. A frequency will be
1457 * processed as close to this target frequency as possible.
1458 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1459 * allowable programmed frequency and does not account for clock
1460 * tolerances and jitter.
1461 *
1462 * Return: 0 if all went well, else returns appropriate error value.
1463 */
1464static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1465 u32 dev_id, u8 clk_id, u64 min_freq,
1466 u64 target_freq, u64 max_freq)
1467{
1468 struct ti_sci_msg_req_set_clock_freq req;
1469 struct ti_sci_msg_hdr *resp;
1470 struct ti_sci_info *info;
1471 struct ti_sci_xfer *xfer;
1472 int ret = 0;
1473
1474 if (IS_ERR(handle))
1475 return PTR_ERR(handle);
1476 if (!handle)
1477 return -EINVAL;
1478
1479 info = handle_to_ti_sci_info(handle);
1480
1481 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1482 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1483 (u32 *)&req, sizeof(req), sizeof(*resp));
1484 if (IS_ERR(xfer)) {
1485 ret = PTR_ERR(xfer);
1486 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1487 return ret;
1488 }
1489 req.dev_id = dev_id;
1490 req.clk_id = clk_id;
1491 req.min_freq_hz = min_freq;
1492 req.target_freq_hz = target_freq;
1493 req.max_freq_hz = max_freq;
1494
1495 ret = ti_sci_do_xfer(info, xfer);
1496 if (ret) {
1497 dev_err(info->dev, "Mbox send fail %d\n", ret);
1498 return ret;
1499 }
1500
1501 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1502
1503 if (!ti_sci_is_response_ack(resp))
1504 return -ENODEV;
1505
1506 return ret;
1507}
1508
1509/**
1510 * ti_sci_cmd_clk_get_freq() - Get current frequency
1511 * @handle: pointer to TI SCI handle
1512 * @dev_id: Device identifier this request is for
1513 * @clk_id: Clock identifier for the device for this request.
1514 * Each device has it's own set of clock inputs. This indexes
1515 * which clock input to modify.
1516 * @freq: Currently frequency in Hz
1517 *
1518 * Return: 0 if all went well, else returns appropriate error value.
1519 */
1520static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1521 u32 dev_id, u8 clk_id, u64 *freq)
1522{
1523 struct ti_sci_msg_resp_get_clock_freq *resp;
1524 struct ti_sci_msg_req_get_clock_freq req;
1525 struct ti_sci_info *info;
1526 struct ti_sci_xfer *xfer;
1527 int ret = 0;
1528
1529 if (IS_ERR(handle))
1530 return PTR_ERR(handle);
1531 if (!handle || !freq)
1532 return -EINVAL;
1533
1534 info = handle_to_ti_sci_info(handle);
1535
1536 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1537 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1538 (u32 *)&req, sizeof(req), sizeof(*resp));
1539 if (IS_ERR(xfer)) {
1540 ret = PTR_ERR(xfer);
1541 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1542 return ret;
1543 }
1544 req.dev_id = dev_id;
1545 req.clk_id = clk_id;
1546
1547 ret = ti_sci_do_xfer(info, xfer);
1548 if (ret) {
1549 dev_err(info->dev, "Mbox send fail %d\n", ret);
1550 return ret;
1551 }
1552
1553 resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->tx_message.buf;
1554
1555 if (!ti_sci_is_response_ack(resp))
1556 ret = -ENODEV;
1557 else
1558 *freq = resp->freq_hz;
1559
1560 return ret;
1561}
1562
Andreas Dannenbergf369b0f2018-08-27 15:57:36 +05301563/**
1564 * ti_sci_cmd_core_reboot() - Command to request system reset
1565 * @handle: pointer to TI SCI handle
1566 *
1567 * Return: 0 if all went well, else returns appropriate error value.
1568 */
1569static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1570{
1571 struct ti_sci_msg_req_reboot req;
1572 struct ti_sci_msg_hdr *resp;
1573 struct ti_sci_info *info;
1574 struct ti_sci_xfer *xfer;
1575 int ret = 0;
1576
1577 if (IS_ERR(handle))
1578 return PTR_ERR(handle);
1579 if (!handle)
1580 return -EINVAL;
1581
1582 info = handle_to_ti_sci_info(handle);
1583
1584 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1585 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1586 (u32 *)&req, sizeof(req), sizeof(*resp));
1587 if (IS_ERR(xfer)) {
1588 ret = PTR_ERR(xfer);
1589 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1590 return ret;
1591 }
Dave Gerlachbeed3052021-05-13 20:10:55 -05001592 req.domain = 0;
Andreas Dannenbergf369b0f2018-08-27 15:57:36 +05301593
1594 ret = ti_sci_do_xfer(info, xfer);
1595 if (ret) {
Sean Andersone5792302020-09-15 10:44:38 -04001596 dev_err(info->dev, "Mbox send fail %d\n", ret);
Andreas Dannenbergf369b0f2018-08-27 15:57:36 +05301597 return ret;
1598 }
1599
1600 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1601
1602 if (!ti_sci_is_response_ack(resp))
1603 return -ENODEV;
1604
1605 return ret;
1606}
1607
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301608/**
1609 * ti_sci_get_resource_range - Helper to get a range of resources assigned
1610 * to a host. Resource is uniquely identified by
1611 * type and subtype.
1612 * @handle: Pointer to TISCI handle.
1613 * @dev_id: TISCI device ID.
1614 * @subtype: Resource assignment subtype that is being requested
1615 * from the given device.
1616 * @s_host: Host processor ID to which the resources are allocated
1617 * @range_start: Start index of the resource range
1618 * @range_num: Number of resources in the range
1619 *
1620 * Return: 0 if all went fine, else return appropriate error.
1621 */
1622static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
1623 u32 dev_id, u8 subtype, u8 s_host,
1624 u16 *range_start, u16 *range_num)
1625{
1626 struct ti_sci_msg_resp_get_resource_range *resp;
1627 struct ti_sci_msg_req_get_resource_range req;
1628 struct ti_sci_xfer *xfer;
1629 struct ti_sci_info *info;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301630 int ret = 0;
1631
1632 if (IS_ERR(handle))
1633 return PTR_ERR(handle);
1634 if (!handle)
1635 return -EINVAL;
1636
1637 info = handle_to_ti_sci_info(handle);
1638
1639 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
1640 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1641 (u32 *)&req, sizeof(req), sizeof(*resp));
1642 if (IS_ERR(xfer)) {
1643 ret = PTR_ERR(xfer);
Sean Andersone5792302020-09-15 10:44:38 -04001644 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301645 return ret;
1646 }
1647
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301648 req.secondary_host = s_host;
Lokesh Vutla4986b152020-08-17 11:00:48 +05301649 req.type = dev_id & MSG_RM_RESOURCE_TYPE_MASK;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301650 req.subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
1651
1652 ret = ti_sci_do_xfer(info, xfer);
1653 if (ret) {
Sean Andersone5792302020-09-15 10:44:38 -04001654 dev_err(info->dev, "Mbox send fail %d\n", ret);
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301655 goto fail;
1656 }
1657
1658 resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->tx_message.buf;
1659 if (!ti_sci_is_response_ack(resp)) {
1660 ret = -ENODEV;
1661 } else if (!resp->range_start && !resp->range_num) {
1662 ret = -ENODEV;
1663 } else {
1664 *range_start = resp->range_start;
1665 *range_num = resp->range_num;
1666 };
1667
1668fail:
1669 return ret;
1670}
1671
Vignesh Raghavendra0e811582021-06-07 19:47:48 +05301672static int __maybe_unused
1673ti_sci_get_resource_range_static(u32 dev_id, u8 subtype, u16 *range_start,
1674 u16 *range_num)
1675{
1676 struct ti_sci_resource_static_data *data;
1677 int i = 0;
1678
1679 while (1) {
1680 data = &rm_static_data[i];
1681
1682 if (!data->dev_id)
1683 return -EINVAL;
1684
1685 if (data->dev_id != dev_id || data->subtype != subtype) {
1686 i++;
1687 continue;
1688 }
1689
1690 *range_start = data->range_start;
1691 *range_num = data->range_num;
1692
1693 return 0;
1694 }
1695
1696 return -EINVAL;
1697}
1698
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301699/**
1700 * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
1701 * that is same as ti sci interface host.
1702 * @handle: Pointer to TISCI handle.
1703 * @dev_id: TISCI device ID.
1704 * @subtype: Resource assignment subtype that is being requested
1705 * from the given device.
1706 * @range_start: Start index of the resource range
1707 * @range_num: Number of resources in the range
1708 *
1709 * Return: 0 if all went fine, else return appropriate error.
1710 */
1711static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
1712 u32 dev_id, u8 subtype,
1713 u16 *range_start, u16 *range_num)
1714{
Vignesh Raghavendra0e811582021-06-07 19:47:48 +05301715 if (CONFIG_IS_ENABLED(TI_K3_RAW_RM))
1716 return ti_sci_get_resource_range_static(dev_id, subtype,
1717 range_start,
1718 range_num);
1719
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301720 return ti_sci_get_resource_range(handle, dev_id, subtype,
1721 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
1722 range_start, range_num);
1723}
1724
1725/**
1726 * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
1727 * assigned to a specified host.
1728 * @handle: Pointer to TISCI handle.
1729 * @dev_id: TISCI device ID.
1730 * @subtype: Resource assignment subtype that is being requested
1731 * from the given device.
1732 * @s_host: Host processor ID to which the resources are allocated
1733 * @range_start: Start index of the resource range
1734 * @range_num: Number of resources in the range
1735 *
1736 * Return: 0 if all went fine, else return appropriate error.
1737 */
1738static
1739int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
1740 u32 dev_id, u8 subtype, u8 s_host,
1741 u16 *range_start, u16 *range_num)
1742{
Vignesh Raghavendra0e811582021-06-07 19:47:48 +05301743 if (CONFIG_IS_ENABLED(TI_K3_RAW_RM))
1744 return -EINVAL;
1745
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05301746 return ti_sci_get_resource_range(handle, dev_id, subtype, s_host,
1747 range_start, range_num);
1748}
1749
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05301750/**
Lokesh Vutla826eb742019-03-08 11:47:32 +05301751 * ti_sci_cmd_query_msmc() - Command to query currently available msmc memory
1752 * @handle: pointer to TI SCI handle
1753 * @msms_start: MSMC start as returned by tisci
1754 * @msmc_end: MSMC end as returned by tisci
1755 *
1756 * Return: 0 if all went well, else returns appropriate error value.
1757 */
1758static int ti_sci_cmd_query_msmc(const struct ti_sci_handle *handle,
1759 u64 *msmc_start, u64 *msmc_end)
1760{
1761 struct ti_sci_msg_resp_query_msmc *resp;
1762 struct ti_sci_msg_hdr req;
1763 struct ti_sci_info *info;
1764 struct ti_sci_xfer *xfer;
1765 int ret = 0;
1766
1767 if (IS_ERR(handle))
1768 return PTR_ERR(handle);
1769 if (!handle)
1770 return -EINVAL;
1771
1772 info = handle_to_ti_sci_info(handle);
1773
1774 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_QUERY_MSMC,
1775 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1776 (u32 *)&req, sizeof(req), sizeof(*resp));
1777 if (IS_ERR(xfer)) {
1778 ret = PTR_ERR(xfer);
1779 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1780 return ret;
1781 }
1782
1783 ret = ti_sci_do_xfer(info, xfer);
1784 if (ret) {
Sean Andersone5792302020-09-15 10:44:38 -04001785 dev_err(info->dev, "Mbox send fail %d\n", ret);
Lokesh Vutla826eb742019-03-08 11:47:32 +05301786 return ret;
1787 }
1788
1789 resp = (struct ti_sci_msg_resp_query_msmc *)xfer->tx_message.buf;
1790
1791 if (!ti_sci_is_response_ack(resp))
1792 return -ENODEV;
1793
1794 *msmc_start = ((u64)resp->msmc_start_high << TISCI_ADDR_HIGH_SHIFT) |
1795 resp->msmc_start_low;
1796 *msmc_end = ((u64)resp->msmc_end_high << TISCI_ADDR_HIGH_SHIFT) |
1797 resp->msmc_end_low;
1798
1799 return ret;
1800}
1801
1802/**
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05301803 * ti_sci_cmd_proc_request() - Command to request a physical processor control
1804 * @handle: Pointer to TI SCI handle
1805 * @proc_id: Processor ID this request is for
1806 *
1807 * Return: 0 if all went well, else returns appropriate error value.
1808 */
1809static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
1810 u8 proc_id)
1811{
1812 struct ti_sci_msg_req_proc_request req;
1813 struct ti_sci_msg_hdr *resp;
1814 struct ti_sci_info *info;
1815 struct ti_sci_xfer *xfer;
1816 int ret = 0;
1817
1818 if (IS_ERR(handle))
1819 return PTR_ERR(handle);
1820 if (!handle)
1821 return -EINVAL;
1822
1823 info = handle_to_ti_sci_info(handle);
1824
1825 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_REQUEST,
1826 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1827 (u32 *)&req, sizeof(req), sizeof(*resp));
1828 if (IS_ERR(xfer)) {
1829 ret = PTR_ERR(xfer);
1830 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1831 return ret;
1832 }
1833 req.processor_id = proc_id;
1834
1835 ret = ti_sci_do_xfer(info, xfer);
1836 if (ret) {
1837 dev_err(info->dev, "Mbox send fail %d\n", ret);
1838 return ret;
1839 }
1840
1841 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1842
1843 if (!ti_sci_is_response_ack(resp))
1844 ret = -ENODEV;
1845
1846 return ret;
1847}
1848
1849/**
1850 * ti_sci_cmd_proc_release() - Command to release a physical processor control
1851 * @handle: Pointer to TI SCI handle
1852 * @proc_id: Processor ID this request is for
1853 *
1854 * Return: 0 if all went well, else returns appropriate error value.
1855 */
1856static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
1857 u8 proc_id)
1858{
1859 struct ti_sci_msg_req_proc_release req;
1860 struct ti_sci_msg_hdr *resp;
1861 struct ti_sci_info *info;
1862 struct ti_sci_xfer *xfer;
1863 int ret = 0;
1864
1865 if (IS_ERR(handle))
1866 return PTR_ERR(handle);
1867 if (!handle)
1868 return -EINVAL;
1869
1870 info = handle_to_ti_sci_info(handle);
1871
1872 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_RELEASE,
1873 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1874 (u32 *)&req, sizeof(req), sizeof(*resp));
1875 if (IS_ERR(xfer)) {
1876 ret = PTR_ERR(xfer);
1877 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1878 return ret;
1879 }
1880 req.processor_id = proc_id;
1881
1882 ret = ti_sci_do_xfer(info, xfer);
1883 if (ret) {
1884 dev_err(info->dev, "Mbox send fail %d\n", ret);
1885 return ret;
1886 }
1887
1888 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1889
1890 if (!ti_sci_is_response_ack(resp))
1891 ret = -ENODEV;
1892
1893 return ret;
1894}
1895
1896/**
1897 * ti_sci_cmd_proc_handover() - Command to handover a physical processor
1898 * control to a host in the processor's access
1899 * control list.
1900 * @handle: Pointer to TI SCI handle
1901 * @proc_id: Processor ID this request is for
1902 * @host_id: Host ID to get the control of the processor
1903 *
1904 * Return: 0 if all went well, else returns appropriate error value.
1905 */
1906static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
1907 u8 proc_id, u8 host_id)
1908{
1909 struct ti_sci_msg_req_proc_handover req;
1910 struct ti_sci_msg_hdr *resp;
1911 struct ti_sci_info *info;
1912 struct ti_sci_xfer *xfer;
1913 int ret = 0;
1914
1915 if (IS_ERR(handle))
1916 return PTR_ERR(handle);
1917 if (!handle)
1918 return -EINVAL;
1919
1920 info = handle_to_ti_sci_info(handle);
1921
1922 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_HANDOVER,
1923 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1924 (u32 *)&req, sizeof(req), sizeof(*resp));
1925 if (IS_ERR(xfer)) {
1926 ret = PTR_ERR(xfer);
1927 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1928 return ret;
1929 }
1930 req.processor_id = proc_id;
1931 req.host_id = host_id;
1932
1933 ret = ti_sci_do_xfer(info, xfer);
1934 if (ret) {
1935 dev_err(info->dev, "Mbox send fail %d\n", ret);
1936 return ret;
1937 }
1938
1939 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1940
1941 if (!ti_sci_is_response_ack(resp))
1942 ret = -ENODEV;
1943
1944 return ret;
1945}
1946
1947/**
1948 * ti_sci_cmd_set_proc_boot_cfg() - Command to set the processor boot
1949 * configuration flags
1950 * @handle: Pointer to TI SCI handle
1951 * @proc_id: Processor ID this request is for
1952 * @config_flags_set: Configuration flags to be set
1953 * @config_flags_clear: Configuration flags to be cleared.
1954 *
1955 * Return: 0 if all went well, else returns appropriate error value.
1956 */
1957static int ti_sci_cmd_set_proc_boot_cfg(const struct ti_sci_handle *handle,
1958 u8 proc_id, u64 bootvector,
1959 u32 config_flags_set,
1960 u32 config_flags_clear)
1961{
1962 struct ti_sci_msg_req_set_proc_boot_config req;
1963 struct ti_sci_msg_hdr *resp;
1964 struct ti_sci_info *info;
1965 struct ti_sci_xfer *xfer;
1966 int ret = 0;
1967
1968 if (IS_ERR(handle))
1969 return PTR_ERR(handle);
1970 if (!handle)
1971 return -EINVAL;
1972
1973 info = handle_to_ti_sci_info(handle);
1974
1975 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_SET_PROC_BOOT_CONFIG,
1976 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1977 (u32 *)&req, sizeof(req), sizeof(*resp));
1978 if (IS_ERR(xfer)) {
1979 ret = PTR_ERR(xfer);
1980 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
1981 return ret;
1982 }
1983 req.processor_id = proc_id;
1984 req.bootvector_low = bootvector & TISCI_ADDR_LOW_MASK;
1985 req.bootvector_high = (bootvector & TISCI_ADDR_HIGH_MASK) >>
1986 TISCI_ADDR_HIGH_SHIFT;
1987 req.config_flags_set = config_flags_set;
1988 req.config_flags_clear = config_flags_clear;
1989
1990 ret = ti_sci_do_xfer(info, xfer);
1991 if (ret) {
1992 dev_err(info->dev, "Mbox send fail %d\n", ret);
1993 return ret;
1994 }
1995
1996 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
1997
1998 if (!ti_sci_is_response_ack(resp))
1999 ret = -ENODEV;
2000
2001 return ret;
2002}
2003
2004/**
2005 * ti_sci_cmd_set_proc_boot_ctrl() - Command to set the processor boot
2006 * control flags
2007 * @handle: Pointer to TI SCI handle
2008 * @proc_id: Processor ID this request is for
2009 * @control_flags_set: Control flags to be set
2010 * @control_flags_clear: Control flags to be cleared
2011 *
2012 * Return: 0 if all went well, else returns appropriate error value.
2013 */
2014static int ti_sci_cmd_set_proc_boot_ctrl(const struct ti_sci_handle *handle,
2015 u8 proc_id, u32 control_flags_set,
2016 u32 control_flags_clear)
2017{
2018 struct ti_sci_msg_req_set_proc_boot_ctrl req;
2019 struct ti_sci_msg_hdr *resp;
2020 struct ti_sci_info *info;
2021 struct ti_sci_xfer *xfer;
2022 int ret = 0;
2023
2024 if (IS_ERR(handle))
2025 return PTR_ERR(handle);
2026 if (!handle)
2027 return -EINVAL;
2028
2029 info = handle_to_ti_sci_info(handle);
2030
2031 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_SET_PROC_BOOT_CTRL,
2032 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2033 (u32 *)&req, sizeof(req), sizeof(*resp));
2034 if (IS_ERR(xfer)) {
2035 ret = PTR_ERR(xfer);
2036 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2037 return ret;
2038 }
2039 req.processor_id = proc_id;
2040 req.control_flags_set = control_flags_set;
2041 req.control_flags_clear = control_flags_clear;
2042
2043 ret = ti_sci_do_xfer(info, xfer);
2044 if (ret) {
2045 dev_err(info->dev, "Mbox send fail %d\n", ret);
2046 return ret;
2047 }
2048
2049 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2050
2051 if (!ti_sci_is_response_ack(resp))
2052 ret = -ENODEV;
2053
2054 return ret;
2055}
2056
2057/**
2058 * ti_sci_cmd_proc_auth_boot_image() - Command to authenticate and load the
2059 * image and then set the processor configuration flags.
2060 * @handle: Pointer to TI SCI handle
Andrew F. Davisff6043a2019-04-12 12:54:44 -04002061 * @image_addr: Memory address at which payload image and certificate is
2062 * located in memory, this is updated if the image data is
2063 * moved during authentication.
2064 * @image_size: This is updated with the final size of the image after
2065 * authentication.
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302066 *
2067 * Return: 0 if all went well, else returns appropriate error value.
2068 */
2069static int ti_sci_cmd_proc_auth_boot_image(const struct ti_sci_handle *handle,
Andrew F. Davisff6043a2019-04-12 12:54:44 -04002070 u64 *image_addr, u32 *image_size)
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302071{
2072 struct ti_sci_msg_req_proc_auth_boot_image req;
Andrew F. Davisff6043a2019-04-12 12:54:44 -04002073 struct ti_sci_msg_resp_proc_auth_boot_image *resp;
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302074 struct ti_sci_info *info;
2075 struct ti_sci_xfer *xfer;
2076 int ret = 0;
2077
2078 if (IS_ERR(handle))
2079 return PTR_ERR(handle);
2080 if (!handle)
2081 return -EINVAL;
2082
2083 info = handle_to_ti_sci_info(handle);
2084
2085 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_AUTH_BOOT_IMIAGE,
2086 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2087 (u32 *)&req, sizeof(req), sizeof(*resp));
2088 if (IS_ERR(xfer)) {
2089 ret = PTR_ERR(xfer);
2090 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2091 return ret;
2092 }
Andrew F. Davisff6043a2019-04-12 12:54:44 -04002093 req.cert_addr_low = *image_addr & TISCI_ADDR_LOW_MASK;
2094 req.cert_addr_high = (*image_addr & TISCI_ADDR_HIGH_MASK) >>
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302095 TISCI_ADDR_HIGH_SHIFT;
2096
2097 ret = ti_sci_do_xfer(info, xfer);
2098 if (ret) {
2099 dev_err(info->dev, "Mbox send fail %d\n", ret);
2100 return ret;
2101 }
2102
Andrew F. Davisff6043a2019-04-12 12:54:44 -04002103 resp = (struct ti_sci_msg_resp_proc_auth_boot_image *)xfer->tx_message.buf;
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302104
2105 if (!ti_sci_is_response_ack(resp))
Andrew F. Davisff6043a2019-04-12 12:54:44 -04002106 return -ENODEV;
2107
2108 *image_addr = (resp->image_addr_low & TISCI_ADDR_LOW_MASK) |
2109 (((u64)resp->image_addr_high <<
2110 TISCI_ADDR_HIGH_SHIFT) & TISCI_ADDR_HIGH_MASK);
2111 *image_size = resp->image_size;
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302112
2113 return ret;
2114}
2115
2116/**
2117 * ti_sci_cmd_get_proc_boot_status() - Command to get the processor boot status
2118 * @handle: Pointer to TI SCI handle
2119 * @proc_id: Processor ID this request is for
2120 *
2121 * Return: 0 if all went well, else returns appropriate error value.
2122 */
2123static int ti_sci_cmd_get_proc_boot_status(const struct ti_sci_handle *handle,
2124 u8 proc_id, u64 *bv, u32 *cfg_flags,
2125 u32 *ctrl_flags, u32 *sts_flags)
2126{
2127 struct ti_sci_msg_resp_get_proc_boot_status *resp;
2128 struct ti_sci_msg_req_get_proc_boot_status req;
2129 struct ti_sci_info *info;
2130 struct ti_sci_xfer *xfer;
2131 int ret = 0;
2132
2133 if (IS_ERR(handle))
2134 return PTR_ERR(handle);
2135 if (!handle)
2136 return -EINVAL;
2137
2138 info = handle_to_ti_sci_info(handle);
2139
2140 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_GET_PROC_BOOT_STATUS,
2141 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2142 (u32 *)&req, sizeof(req), sizeof(*resp));
2143 if (IS_ERR(xfer)) {
2144 ret = PTR_ERR(xfer);
2145 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2146 return ret;
2147 }
2148 req.processor_id = proc_id;
2149
2150 ret = ti_sci_do_xfer(info, xfer);
2151 if (ret) {
2152 dev_err(info->dev, "Mbox send fail %d\n", ret);
2153 return ret;
2154 }
2155
2156 resp = (struct ti_sci_msg_resp_get_proc_boot_status *)
2157 xfer->tx_message.buf;
2158
2159 if (!ti_sci_is_response_ack(resp))
2160 return -ENODEV;
2161 *bv = (resp->bootvector_low & TISCI_ADDR_LOW_MASK) |
2162 (((u64)resp->bootvector_high <<
2163 TISCI_ADDR_HIGH_SHIFT) & TISCI_ADDR_HIGH_MASK);
2164 *cfg_flags = resp->config_flags;
2165 *ctrl_flags = resp->control_flags;
2166 *sts_flags = resp->status_flags;
2167
2168 return ret;
2169}
2170
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302171/**
Andreas Dannenberg410adcc2019-06-07 19:24:40 +05302172 * ti_sci_proc_wait_boot_status_no_wait() - Helper function to wait for a
2173 * processor boot status without requesting or
2174 * waiting for a response.
2175 * @proc_id: Processor ID this request is for
2176 * @num_wait_iterations: Total number of iterations we will check before
2177 * we will timeout and give up
2178 * @num_match_iterations: How many iterations should we have continued
2179 * status to account for status bits glitching.
2180 * This is to make sure that match occurs for
2181 * consecutive checks. This implies that the
2182 * worst case should consider that the stable
2183 * time should at the worst be num_wait_iterations
2184 * num_match_iterations to prevent timeout.
2185 * @delay_per_iteration_us: Specifies how long to wait (in micro seconds)
2186 * between each status checks. This is the minimum
2187 * duration, and overhead of register reads and
2188 * checks are on top of this and can vary based on
2189 * varied conditions.
2190 * @delay_before_iterations_us: Specifies how long to wait (in micro seconds)
2191 * before the very first check in the first
2192 * iteration of status check loop. This is the
2193 * minimum duration, and overhead of register
2194 * reads and checks are.
2195 * @status_flags_1_set_all_wait:If non-zero, Specifies that all bits of the
2196 * status matching this field requested MUST be 1.
2197 * @status_flags_1_set_any_wait:If non-zero, Specifies that at least one of the
2198 * bits matching this field requested MUST be 1.
2199 * @status_flags_1_clr_all_wait:If non-zero, Specifies that all bits of the
2200 * status matching this field requested MUST be 0.
2201 * @status_flags_1_clr_any_wait:If non-zero, Specifies that at least one of the
2202 * bits matching this field requested MUST be 0.
2203 *
2204 * Return: 0 if all goes well, else appropriate error message
2205 */
2206static int
2207ti_sci_proc_wait_boot_status_no_wait(const struct ti_sci_handle *handle,
2208 u8 proc_id,
2209 u8 num_wait_iterations,
2210 u8 num_match_iterations,
2211 u8 delay_per_iteration_us,
2212 u8 delay_before_iterations_us,
2213 u32 status_flags_1_set_all_wait,
2214 u32 status_flags_1_set_any_wait,
2215 u32 status_flags_1_clr_all_wait,
2216 u32 status_flags_1_clr_any_wait)
2217{
2218 struct ti_sci_msg_req_wait_proc_boot_status req;
2219 struct ti_sci_info *info;
2220 struct ti_sci_xfer *xfer;
2221 int ret = 0;
2222
2223 if (IS_ERR(handle))
2224 return PTR_ERR(handle);
2225 if (!handle)
2226 return -EINVAL;
2227
2228 info = handle_to_ti_sci_info(handle);
2229
2230 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_WAIT_PROC_BOOT_STATUS,
2231 TI_SCI_FLAG_REQ_GENERIC_NORESPONSE,
2232 (u32 *)&req, sizeof(req), 0);
2233 if (IS_ERR(xfer)) {
2234 ret = PTR_ERR(xfer);
2235 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2236 return ret;
2237 }
2238 req.processor_id = proc_id;
2239 req.num_wait_iterations = num_wait_iterations;
2240 req.num_match_iterations = num_match_iterations;
2241 req.delay_per_iteration_us = delay_per_iteration_us;
2242 req.delay_before_iterations_us = delay_before_iterations_us;
2243 req.status_flags_1_set_all_wait = status_flags_1_set_all_wait;
2244 req.status_flags_1_set_any_wait = status_flags_1_set_any_wait;
2245 req.status_flags_1_clr_all_wait = status_flags_1_clr_all_wait;
2246 req.status_flags_1_clr_any_wait = status_flags_1_clr_any_wait;
2247
2248 ret = ti_sci_do_xfer(info, xfer);
2249 if (ret)
2250 dev_err(info->dev, "Mbox send fail %d\n", ret);
2251
2252 return ret;
2253}
2254
2255/**
2256 * ti_sci_cmd_proc_shutdown_no_wait() - Command to shutdown a core without
2257 * requesting or waiting for a response. Note that this API call
2258 * should be followed by placing the respective processor into
2259 * either WFE or WFI mode.
2260 * @handle: Pointer to TI SCI handle
2261 * @proc_id: Processor ID this request is for
2262 *
2263 * Return: 0 if all went well, else returns appropriate error value.
2264 */
2265static int ti_sci_cmd_proc_shutdown_no_wait(const struct ti_sci_handle *handle,
2266 u8 proc_id)
2267{
2268 int ret;
Sean Andersone5792302020-09-15 10:44:38 -04002269 struct ti_sci_info *info;
2270
2271 if (IS_ERR(handle))
2272 return PTR_ERR(handle);
2273 if (!handle)
2274 return -EINVAL;
2275
2276 info = handle_to_ti_sci_info(handle);
Andreas Dannenberg410adcc2019-06-07 19:24:40 +05302277
2278 /*
2279 * Send the core boot status wait message waiting for either WFE or
2280 * WFI without requesting or waiting for a TISCI response with the
2281 * maximum wait time to give us the best chance to get to the WFE/WFI
2282 * command that should follow the invocation of this API before the
2283 * DMSC-internal processing of this command times out. Note that
2284 * waiting for the R5 WFE/WFI flags will also work on an ARMV8 type
2285 * core as the related flag bit positions are the same.
2286 */
2287 ret = ti_sci_proc_wait_boot_status_no_wait(handle, proc_id,
2288 U8_MAX, 100, U8_MAX, U8_MAX,
2289 0, PROC_BOOT_STATUS_FLAG_R5_WFE | PROC_BOOT_STATUS_FLAG_R5_WFI,
2290 0, 0);
2291 if (ret) {
2292 dev_err(info->dev, "Sending core %u wait message fail %d\n",
2293 proc_id, ret);
2294 return ret;
2295 }
2296
2297 /*
2298 * Release a processor managed by TISCI without requesting or waiting
2299 * for a response.
2300 */
2301 ret = ti_sci_set_device_state_no_wait(handle, proc_id, 0,
2302 MSG_DEVICE_SW_STATE_AUTO_OFF);
2303 if (ret)
2304 dev_err(info->dev, "Sending core %u shutdown message fail %d\n",
2305 proc_id, ret);
2306
2307 return ret;
2308}
2309
2310/**
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302311 * ti_sci_cmd_ring_config() - configure RA ring
2312 * @handle: pointer to TI SCI handle
2313 * @valid_params: Bitfield defining validity of ring configuration parameters.
2314 * @nav_id: Device ID of Navigator Subsystem from which the ring is allocated
2315 * @index: Ring index.
2316 * @addr_lo: The ring base address lo 32 bits
2317 * @addr_hi: The ring base address hi 32 bits
2318 * @count: Number of ring elements.
2319 * @mode: The mode of the ring
2320 * @size: The ring element size.
2321 * @order_id: Specifies the ring's bus order ID.
2322 *
2323 * Return: 0 if all went well, else returns appropriate error value.
2324 *
2325 * See @ti_sci_msg_rm_ring_cfg_req for more info.
2326 */
2327static int ti_sci_cmd_ring_config(const struct ti_sci_handle *handle,
2328 u32 valid_params, u16 nav_id, u16 index,
2329 u32 addr_lo, u32 addr_hi, u32 count,
2330 u8 mode, u8 size, u8 order_id)
2331{
2332 struct ti_sci_msg_rm_ring_cfg_resp *resp;
2333 struct ti_sci_msg_rm_ring_cfg_req req;
2334 struct ti_sci_xfer *xfer;
2335 struct ti_sci_info *info;
2336 int ret = 0;
2337
2338 if (IS_ERR(handle))
2339 return PTR_ERR(handle);
2340 if (!handle)
2341 return -EINVAL;
2342
2343 info = handle_to_ti_sci_info(handle);
2344
2345 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
2346 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2347 (u32 *)&req, sizeof(req), sizeof(*resp));
2348 if (IS_ERR(xfer)) {
2349 ret = PTR_ERR(xfer);
2350 dev_err(info->dev, "RM_RA:Message config failed(%d)\n", ret);
2351 return ret;
2352 }
2353 req.valid_params = valid_params;
2354 req.nav_id = nav_id;
2355 req.index = index;
2356 req.addr_lo = addr_lo;
2357 req.addr_hi = addr_hi;
2358 req.count = count;
2359 req.mode = mode;
2360 req.size = size;
2361 req.order_id = order_id;
2362
2363 ret = ti_sci_do_xfer(info, xfer);
2364 if (ret) {
2365 dev_err(info->dev, "RM_RA:Mbox config send fail %d\n", ret);
2366 goto fail;
2367 }
2368
2369 resp = (struct ti_sci_msg_rm_ring_cfg_resp *)xfer->tx_message.buf;
2370
2371 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2372
2373fail:
2374 dev_dbg(info->dev, "RM_RA:config ring %u ret:%d\n", index, ret);
2375 return ret;
2376}
2377
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302378static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
2379 u32 nav_id, u32 src_thread, u32 dst_thread)
2380{
2381 struct ti_sci_msg_hdr *resp;
2382 struct ti_sci_msg_psil_pair req;
2383 struct ti_sci_xfer *xfer;
2384 struct ti_sci_info *info;
2385 int ret = 0;
2386
2387 if (IS_ERR(handle))
2388 return PTR_ERR(handle);
2389 if (!handle)
2390 return -EINVAL;
2391
2392 info = handle_to_ti_sci_info(handle);
2393
2394 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
2395 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2396 (u32 *)&req, sizeof(req), sizeof(*resp));
2397 if (IS_ERR(xfer)) {
2398 ret = PTR_ERR(xfer);
2399 dev_err(info->dev, "RM_PSIL:Message alloc failed(%d)\n", ret);
2400 return ret;
2401 }
2402 req.nav_id = nav_id;
2403 req.src_thread = src_thread;
2404 req.dst_thread = dst_thread;
2405
2406 ret = ti_sci_do_xfer(info, xfer);
2407 if (ret) {
2408 dev_err(info->dev, "RM_PSIL:Mbox send fail %d\n", ret);
2409 goto fail;
2410 }
2411
2412 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2413 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2414
2415fail:
2416 dev_dbg(info->dev, "RM_PSIL: nav: %u link pair %u->%u ret:%u\n",
2417 nav_id, src_thread, dst_thread, ret);
2418 return ret;
2419}
2420
2421static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
2422 u32 nav_id, u32 src_thread, u32 dst_thread)
2423{
2424 struct ti_sci_msg_hdr *resp;
2425 struct ti_sci_msg_psil_unpair req;
2426 struct ti_sci_xfer *xfer;
2427 struct ti_sci_info *info;
2428 int ret = 0;
2429
2430 if (IS_ERR(handle))
2431 return PTR_ERR(handle);
2432 if (!handle)
2433 return -EINVAL;
2434
2435 info = handle_to_ti_sci_info(handle);
2436
2437 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
2438 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2439 (u32 *)&req, sizeof(req), sizeof(*resp));
2440 if (IS_ERR(xfer)) {
2441 ret = PTR_ERR(xfer);
2442 dev_err(info->dev, "RM_PSIL:Message alloc failed(%d)\n", ret);
2443 return ret;
2444 }
2445 req.nav_id = nav_id;
2446 req.src_thread = src_thread;
2447 req.dst_thread = dst_thread;
2448
2449 ret = ti_sci_do_xfer(info, xfer);
2450 if (ret) {
2451 dev_err(info->dev, "RM_PSIL:Mbox send fail %d\n", ret);
2452 goto fail;
2453 }
2454
2455 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2456 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2457
2458fail:
2459 dev_dbg(info->dev, "RM_PSIL: link unpair %u->%u ret:%u\n",
2460 src_thread, dst_thread, ret);
2461 return ret;
2462}
2463
2464static int ti_sci_cmd_rm_udmap_tx_ch_cfg(
2465 const struct ti_sci_handle *handle,
2466 const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
2467{
2468 struct ti_sci_msg_rm_udmap_tx_ch_cfg_resp *resp;
2469 struct ti_sci_msg_rm_udmap_tx_ch_cfg_req req;
2470 struct ti_sci_xfer *xfer;
2471 struct ti_sci_info *info;
2472 int ret = 0;
2473
2474 if (IS_ERR(handle))
2475 return PTR_ERR(handle);
2476 if (!handle)
2477 return -EINVAL;
2478
2479 info = handle_to_ti_sci_info(handle);
2480
2481 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
2482 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2483 (u32 *)&req, sizeof(req), sizeof(*resp));
2484 if (IS_ERR(xfer)) {
2485 ret = PTR_ERR(xfer);
2486 dev_err(info->dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
2487 return ret;
2488 }
2489 req.valid_params = params->valid_params;
2490 req.nav_id = params->nav_id;
2491 req.index = params->index;
2492 req.tx_pause_on_err = params->tx_pause_on_err;
2493 req.tx_filt_einfo = params->tx_filt_einfo;
2494 req.tx_filt_pswords = params->tx_filt_pswords;
2495 req.tx_atype = params->tx_atype;
2496 req.tx_chan_type = params->tx_chan_type;
2497 req.tx_supr_tdpkt = params->tx_supr_tdpkt;
2498 req.tx_fetch_size = params->tx_fetch_size;
2499 req.tx_credit_count = params->tx_credit_count;
2500 req.txcq_qnum = params->txcq_qnum;
2501 req.tx_priority = params->tx_priority;
2502 req.tx_qos = params->tx_qos;
2503 req.tx_orderid = params->tx_orderid;
2504 req.fdepth = params->fdepth;
2505 req.tx_sched_priority = params->tx_sched_priority;
Vignesh Raghavendra91f1e792021-05-10 20:06:02 +05302506 req.tx_burst_size = params->tx_burst_size;
2507 req.tx_tdtype = params->tx_tdtype;
2508 req.extended_ch_type = params->extended_ch_type;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302509
2510 ret = ti_sci_do_xfer(info, xfer);
2511 if (ret) {
2512 dev_err(info->dev, "Mbox send TX_CH_CFG fail %d\n", ret);
2513 goto fail;
2514 }
2515
2516 resp =
2517 (struct ti_sci_msg_rm_udmap_tx_ch_cfg_resp *)xfer->tx_message.buf;
2518 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2519
2520fail:
2521 dev_dbg(info->dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
2522 return ret;
2523}
2524
2525static int ti_sci_cmd_rm_udmap_rx_ch_cfg(
2526 const struct ti_sci_handle *handle,
2527 const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
2528{
2529 struct ti_sci_msg_rm_udmap_rx_ch_cfg_resp *resp;
2530 struct ti_sci_msg_rm_udmap_rx_ch_cfg_req req;
2531 struct ti_sci_xfer *xfer;
2532 struct ti_sci_info *info;
2533 int ret = 0;
2534
2535 if (IS_ERR(handle))
2536 return PTR_ERR(handle);
2537 if (!handle)
2538 return -EINVAL;
2539
2540 info = handle_to_ti_sci_info(handle);
2541
2542 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
2543 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2544 (u32 *)&req, sizeof(req), sizeof(*resp));
2545 if (IS_ERR(xfer)) {
2546 ret = PTR_ERR(xfer);
2547 dev_err(info->dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
2548 return ret;
2549 }
2550
2551 req.valid_params = params->valid_params;
2552 req.nav_id = params->nav_id;
2553 req.index = params->index;
2554 req.rx_fetch_size = params->rx_fetch_size;
2555 req.rxcq_qnum = params->rxcq_qnum;
2556 req.rx_priority = params->rx_priority;
2557 req.rx_qos = params->rx_qos;
2558 req.rx_orderid = params->rx_orderid;
2559 req.rx_sched_priority = params->rx_sched_priority;
2560 req.flowid_start = params->flowid_start;
2561 req.flowid_cnt = params->flowid_cnt;
2562 req.rx_pause_on_err = params->rx_pause_on_err;
2563 req.rx_atype = params->rx_atype;
2564 req.rx_chan_type = params->rx_chan_type;
2565 req.rx_ignore_short = params->rx_ignore_short;
2566 req.rx_ignore_long = params->rx_ignore_long;
2567
2568 ret = ti_sci_do_xfer(info, xfer);
2569 if (ret) {
2570 dev_err(info->dev, "Mbox send RX_CH_CFG fail %d\n", ret);
2571 goto fail;
2572 }
2573
2574 resp =
2575 (struct ti_sci_msg_rm_udmap_rx_ch_cfg_resp *)xfer->tx_message.buf;
2576 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2577
2578fail:
2579 dev_dbg(info->dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
2580 return ret;
2581}
2582
2583static int ti_sci_cmd_rm_udmap_rx_flow_cfg(
2584 const struct ti_sci_handle *handle,
2585 const struct ti_sci_msg_rm_udmap_flow_cfg *params)
2586{
2587 struct ti_sci_msg_rm_udmap_flow_cfg_resp *resp;
2588 struct ti_sci_msg_rm_udmap_flow_cfg_req req;
2589 struct ti_sci_xfer *xfer;
2590 struct ti_sci_info *info;
2591 int ret = 0;
2592
2593 if (IS_ERR(handle))
2594 return PTR_ERR(handle);
2595 if (!handle)
2596 return -EINVAL;
2597
2598 info = handle_to_ti_sci_info(handle);
2599
2600 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
2601 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2602 (u32 *)&req, sizeof(req), sizeof(*resp));
2603 if (IS_ERR(xfer)) {
2604 ret = PTR_ERR(xfer);
Sean Andersone5792302020-09-15 10:44:38 -04002605 dev_err(info->dev, "RX_FL_CFG: Message alloc failed(%d)\n",
2606 ret);
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302607 return ret;
2608 }
2609
2610 req.valid_params = params->valid_params;
2611 req.nav_id = params->nav_id;
2612 req.flow_index = params->flow_index;
2613 req.rx_einfo_present = params->rx_einfo_present;
2614 req.rx_psinfo_present = params->rx_psinfo_present;
2615 req.rx_error_handling = params->rx_error_handling;
2616 req.rx_desc_type = params->rx_desc_type;
2617 req.rx_sop_offset = params->rx_sop_offset;
2618 req.rx_dest_qnum = params->rx_dest_qnum;
2619 req.rx_src_tag_hi = params->rx_src_tag_hi;
2620 req.rx_src_tag_lo = params->rx_src_tag_lo;
2621 req.rx_dest_tag_hi = params->rx_dest_tag_hi;
2622 req.rx_dest_tag_lo = params->rx_dest_tag_lo;
2623 req.rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
2624 req.rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
2625 req.rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
2626 req.rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
2627 req.rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
2628 req.rx_fdq1_qnum = params->rx_fdq1_qnum;
2629 req.rx_fdq2_qnum = params->rx_fdq2_qnum;
2630 req.rx_fdq3_qnum = params->rx_fdq3_qnum;
2631 req.rx_ps_location = params->rx_ps_location;
2632
2633 ret = ti_sci_do_xfer(info, xfer);
2634 if (ret) {
Sean Andersone5792302020-09-15 10:44:38 -04002635 dev_err(info->dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302636 goto fail;
2637 }
2638
2639 resp =
2640 (struct ti_sci_msg_rm_udmap_flow_cfg_resp *)xfer->tx_message.buf;
2641 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2642
2643fail:
2644 dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
2645 return ret;
2646}
2647
Andrew F. Davis32ca8ff2019-04-12 12:54:43 -04002648/**
2649 * ti_sci_cmd_set_fwl_region() - Request for configuring a firewall region
2650 * @handle: pointer to TI SCI handle
2651 * @region: region configuration parameters
2652 *
2653 * Return: 0 if all went well, else returns appropriate error value.
2654 */
2655static int ti_sci_cmd_set_fwl_region(const struct ti_sci_handle *handle,
2656 const struct ti_sci_msg_fwl_region *region)
2657{
2658 struct ti_sci_msg_fwl_set_firewall_region_req req;
2659 struct ti_sci_msg_hdr *resp;
2660 struct ti_sci_info *info;
2661 struct ti_sci_xfer *xfer;
2662 int ret = 0;
2663
2664 if (IS_ERR(handle))
2665 return PTR_ERR(handle);
2666 if (!handle)
2667 return -EINVAL;
2668
2669 info = handle_to_ti_sci_info(handle);
2670
2671 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_FWL_SET,
2672 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2673 (u32 *)&req, sizeof(req), sizeof(*resp));
2674 if (IS_ERR(xfer)) {
2675 ret = PTR_ERR(xfer);
2676 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2677 return ret;
2678 }
2679
2680 req.fwl_id = region->fwl_id;
2681 req.region = region->region;
2682 req.n_permission_regs = region->n_permission_regs;
2683 req.control = region->control;
2684 req.permissions[0] = region->permissions[0];
2685 req.permissions[1] = region->permissions[1];
2686 req.permissions[2] = region->permissions[2];
2687 req.start_address = region->start_address;
2688 req.end_address = region->end_address;
2689
2690 ret = ti_sci_do_xfer(info, xfer);
2691 if (ret) {
2692 dev_err(info->dev, "Mbox send fail %d\n", ret);
2693 return ret;
2694 }
2695
2696 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2697
2698 if (!ti_sci_is_response_ack(resp))
2699 return -ENODEV;
2700
2701 return 0;
2702}
2703
2704/**
2705 * ti_sci_cmd_get_fwl_region() - Request for getting a firewall region
2706 * @handle: pointer to TI SCI handle
2707 * @region: region configuration parameters
2708 *
2709 * Return: 0 if all went well, else returns appropriate error value.
2710 */
2711static int ti_sci_cmd_get_fwl_region(const struct ti_sci_handle *handle,
2712 struct ti_sci_msg_fwl_region *region)
2713{
2714 struct ti_sci_msg_fwl_get_firewall_region_req req;
2715 struct ti_sci_msg_fwl_get_firewall_region_resp *resp;
2716 struct ti_sci_info *info;
2717 struct ti_sci_xfer *xfer;
2718 int ret = 0;
2719
2720 if (IS_ERR(handle))
2721 return PTR_ERR(handle);
2722 if (!handle)
2723 return -EINVAL;
2724
2725 info = handle_to_ti_sci_info(handle);
2726
2727 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_FWL_GET,
2728 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2729 (u32 *)&req, sizeof(req), sizeof(*resp));
2730 if (IS_ERR(xfer)) {
2731 ret = PTR_ERR(xfer);
2732 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2733 return ret;
2734 }
2735
2736 req.fwl_id = region->fwl_id;
2737 req.region = region->region;
2738 req.n_permission_regs = region->n_permission_regs;
2739
2740 ret = ti_sci_do_xfer(info, xfer);
2741 if (ret) {
2742 dev_err(info->dev, "Mbox send fail %d\n", ret);
2743 return ret;
2744 }
2745
2746 resp = (struct ti_sci_msg_fwl_get_firewall_region_resp *)xfer->tx_message.buf;
2747
2748 if (!ti_sci_is_response_ack(resp))
2749 return -ENODEV;
2750
2751 region->fwl_id = resp->fwl_id;
2752 region->region = resp->region;
2753 region->n_permission_regs = resp->n_permission_regs;
2754 region->control = resp->control;
2755 region->permissions[0] = resp->permissions[0];
2756 region->permissions[1] = resp->permissions[1];
2757 region->permissions[2] = resp->permissions[2];
2758 region->start_address = resp->start_address;
2759 region->end_address = resp->end_address;
2760
2761 return 0;
2762}
2763
2764/**
2765 * ti_sci_cmd_change_fwl_owner() - Request for changing a firewall owner
2766 * @handle: pointer to TI SCI handle
2767 * @region: region configuration parameters
2768 *
2769 * Return: 0 if all went well, else returns appropriate error value.
2770 */
2771static int ti_sci_cmd_change_fwl_owner(const struct ti_sci_handle *handle,
2772 struct ti_sci_msg_fwl_owner *owner)
2773{
2774 struct ti_sci_msg_fwl_change_owner_info_req req;
2775 struct ti_sci_msg_fwl_change_owner_info_resp *resp;
2776 struct ti_sci_info *info;
2777 struct ti_sci_xfer *xfer;
2778 int ret = 0;
2779
2780 if (IS_ERR(handle))
2781 return PTR_ERR(handle);
2782 if (!handle)
2783 return -EINVAL;
2784
2785 info = handle_to_ti_sci_info(handle);
2786
Andrew F. Davisefbfd442019-04-29 09:04:11 -04002787 xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_FWL_CHANGE_OWNER,
2788 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
Andrew F. Davis32ca8ff2019-04-12 12:54:43 -04002789 (u32 *)&req, sizeof(req), sizeof(*resp));
2790 if (IS_ERR(xfer)) {
2791 ret = PTR_ERR(xfer);
2792 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
2793 return ret;
2794 }
2795
2796 req.fwl_id = owner->fwl_id;
2797 req.region = owner->region;
2798 req.owner_index = owner->owner_index;
2799
2800 ret = ti_sci_do_xfer(info, xfer);
2801 if (ret) {
2802 dev_err(info->dev, "Mbox send fail %d\n", ret);
2803 return ret;
2804 }
2805
2806 resp = (struct ti_sci_msg_fwl_change_owner_info_resp *)xfer->tx_message.buf;
2807
2808 if (!ti_sci_is_response_ack(resp))
2809 return -ENODEV;
2810
2811 owner->fwl_id = resp->fwl_id;
2812 owner->region = resp->region;
2813 owner->owner_index = resp->owner_index;
2814 owner->owner_privid = resp->owner_privid;
2815 owner->owner_permission_bits = resp->owner_permission_bits;
2816
2817 return ret;
2818}
2819
Andreas Dannenbergdcfc52a2018-08-27 15:57:33 +05302820/*
2821 * ti_sci_setup_ops() - Setup the operations structures
2822 * @info: pointer to TISCI pointer
2823 */
2824static void ti_sci_setup_ops(struct ti_sci_info *info)
2825{
2826 struct ti_sci_ops *ops = &info->handle.ops;
2827 struct ti_sci_board_ops *bops = &ops->board_ops;
Andreas Dannenberg7bc33042018-08-27 15:57:34 +05302828 struct ti_sci_dev_ops *dops = &ops->dev_ops;
Lokesh Vutla9b871812018-08-27 15:57:35 +05302829 struct ti_sci_clk_ops *cops = &ops->clk_ops;
Andreas Dannenbergf369b0f2018-08-27 15:57:36 +05302830 struct ti_sci_core_ops *core_ops = &ops->core_ops;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302831 struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302832 struct ti_sci_proc_ops *pops = &ops->proc_ops;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302833 struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
2834 struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
2835 struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
Andrew F. Davis32ca8ff2019-04-12 12:54:43 -04002836 struct ti_sci_fwl_ops *fwl_ops = &ops->fwl_ops;
Andreas Dannenbergdcfc52a2018-08-27 15:57:33 +05302837
2838 bops->board_config = ti_sci_cmd_set_board_config;
2839 bops->board_config_rm = ti_sci_cmd_set_board_config_rm;
2840 bops->board_config_security = ti_sci_cmd_set_board_config_security;
2841 bops->board_config_pm = ti_sci_cmd_set_board_config_pm;
Andreas Dannenberg7bc33042018-08-27 15:57:34 +05302842
2843 dops->get_device = ti_sci_cmd_get_device;
Lokesh Vutlaae0b8a22019-06-07 19:24:39 +05302844 dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
Andreas Dannenberg7bc33042018-08-27 15:57:34 +05302845 dops->idle_device = ti_sci_cmd_idle_device;
Lokesh Vutlaae0b8a22019-06-07 19:24:39 +05302846 dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
Andreas Dannenberg7bc33042018-08-27 15:57:34 +05302847 dops->put_device = ti_sci_cmd_put_device;
2848 dops->is_valid = ti_sci_cmd_dev_is_valid;
2849 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
2850 dops->is_idle = ti_sci_cmd_dev_is_idle;
2851 dops->is_stop = ti_sci_cmd_dev_is_stop;
2852 dops->is_on = ti_sci_cmd_dev_is_on;
2853 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
2854 dops->set_device_resets = ti_sci_cmd_set_device_resets;
2855 dops->get_device_resets = ti_sci_cmd_get_device_resets;
Lokesh Vutla9566b772019-06-07 19:24:41 +05302856 dops->release_exclusive_devices = ti_sci_cmd_release_exclusive_devices;
Lokesh Vutla9b871812018-08-27 15:57:35 +05302857
2858 cops->get_clock = ti_sci_cmd_get_clock;
2859 cops->idle_clock = ti_sci_cmd_idle_clock;
2860 cops->put_clock = ti_sci_cmd_put_clock;
2861 cops->is_auto = ti_sci_cmd_clk_is_auto;
2862 cops->is_on = ti_sci_cmd_clk_is_on;
2863 cops->is_off = ti_sci_cmd_clk_is_off;
2864
2865 cops->set_parent = ti_sci_cmd_clk_set_parent;
2866 cops->get_parent = ti_sci_cmd_clk_get_parent;
2867 cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
2868
2869 cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
2870 cops->set_freq = ti_sci_cmd_clk_set_freq;
2871 cops->get_freq = ti_sci_cmd_clk_get_freq;
Andreas Dannenbergf369b0f2018-08-27 15:57:36 +05302872
2873 core_ops->reboot_device = ti_sci_cmd_core_reboot;
Lokesh Vutla826eb742019-03-08 11:47:32 +05302874 core_ops->query_msmc = ti_sci_cmd_query_msmc;
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302875
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302876 rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
2877 rm_core_ops->get_range_from_shost =
2878 ti_sci_cmd_get_resource_range_from_shost;
2879
Lokesh Vutlaccbc8b22018-08-27 15:57:37 +05302880 pops->proc_request = ti_sci_cmd_proc_request;
2881 pops->proc_release = ti_sci_cmd_proc_release;
2882 pops->proc_handover = ti_sci_cmd_proc_handover;
2883 pops->set_proc_boot_cfg = ti_sci_cmd_set_proc_boot_cfg;
2884 pops->set_proc_boot_ctrl = ti_sci_cmd_set_proc_boot_ctrl;
2885 pops->proc_auth_boot_image = ti_sci_cmd_proc_auth_boot_image;
2886 pops->get_proc_boot_status = ti_sci_cmd_get_proc_boot_status;
Andreas Dannenberg410adcc2019-06-07 19:24:40 +05302887 pops->proc_shutdown_no_wait = ti_sci_cmd_proc_shutdown_no_wait;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302888
2889 rops->config = ti_sci_cmd_ring_config;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05302890
2891 psilops->pair = ti_sci_cmd_rm_psil_pair;
2892 psilops->unpair = ti_sci_cmd_rm_psil_unpair;
2893
2894 udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
2895 udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
2896 udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
Andrew F. Davis32ca8ff2019-04-12 12:54:43 -04002897
2898 fwl_ops->set_fwl_region = ti_sci_cmd_set_fwl_region;
2899 fwl_ops->get_fwl_region = ti_sci_cmd_get_fwl_region;
2900 fwl_ops->change_fwl_owner = ti_sci_cmd_change_fwl_owner;
Andreas Dannenbergdcfc52a2018-08-27 15:57:33 +05302901}
2902
2903/**
Lokesh Vutla32cd2512018-08-27 15:57:32 +05302904 * ti_sci_get_handle_from_sysfw() - Get the TI SCI handle of the SYSFW
2905 * @dev: Pointer to the SYSFW device
2906 *
2907 * Return: pointer to handle if successful, else EINVAL if invalid conditions
2908 * are encountered.
2909 */
2910const
2911struct ti_sci_handle *ti_sci_get_handle_from_sysfw(struct udevice *sci_dev)
2912{
2913 if (!sci_dev)
2914 return ERR_PTR(-EINVAL);
2915
2916 struct ti_sci_info *info = dev_get_priv(sci_dev);
2917
2918 if (!info)
2919 return ERR_PTR(-EINVAL);
2920
2921 struct ti_sci_handle *handle = &info->handle;
2922
2923 if (!handle)
2924 return ERR_PTR(-EINVAL);
2925
2926 return handle;
2927}
2928
2929/**
2930 * ti_sci_get_handle() - Get the TI SCI handle for a device
2931 * @dev: Pointer to device for which we want SCI handle
2932 *
2933 * Return: pointer to handle if successful, else EINVAL if invalid conditions
2934 * are encountered.
2935 */
2936const struct ti_sci_handle *ti_sci_get_handle(struct udevice *dev)
2937{
2938 if (!dev)
2939 return ERR_PTR(-EINVAL);
2940
2941 struct udevice *sci_dev = dev_get_parent(dev);
2942
2943 return ti_sci_get_handle_from_sysfw(sci_dev);
2944}
2945
2946/**
2947 * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
2948 * @dev: device node
2949 * @propname: property name containing phandle on TISCI node
2950 *
2951 * Return: pointer to handle if successful, else appropriate error value.
2952 */
2953const struct ti_sci_handle *ti_sci_get_by_phandle(struct udevice *dev,
2954 const char *property)
2955{
2956 struct ti_sci_info *entry, *info = NULL;
2957 u32 phandle, err;
2958 ofnode node;
2959
2960 err = ofnode_read_u32(dev_ofnode(dev), property, &phandle);
2961 if (err)
2962 return ERR_PTR(err);
2963
2964 node = ofnode_get_by_phandle(phandle);
2965 if (!ofnode_valid(node))
2966 return ERR_PTR(-EINVAL);
2967
2968 list_for_each_entry(entry, &ti_sci_list, list)
2969 if (ofnode_equal(dev_ofnode(entry->dev), node)) {
2970 info = entry;
2971 break;
2972 }
2973
2974 if (!info)
2975 return ERR_PTR(-ENODEV);
2976
2977 return &info->handle;
2978}
2979
2980/**
2981 * ti_sci_of_to_info() - generate private data from device tree
2982 * @dev: corresponding system controller interface device
2983 * @info: pointer to driver specific private data
2984 *
2985 * Return: 0 if all goes good, else appropriate error message.
2986 */
2987static int ti_sci_of_to_info(struct udevice *dev, struct ti_sci_info *info)
2988{
2989 int ret;
2990
2991 ret = mbox_get_by_name(dev, "tx", &info->chan_tx);
2992 if (ret) {
2993 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
2994 __func__, ret);
2995 return ret;
2996 }
2997
2998 ret = mbox_get_by_name(dev, "rx", &info->chan_rx);
2999 if (ret) {
3000 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
3001 __func__, ret);
3002 return ret;
3003 }
3004
3005 /* Notify channel is optional. Enable only if populated */
3006 ret = mbox_get_by_name(dev, "notify", &info->chan_notify);
3007 if (ret) {
3008 dev_dbg(dev, "%s: Acquiring notify channel failed. ret = %d\n",
3009 __func__, ret);
3010 }
3011
3012 info->host_id = dev_read_u32_default(dev, "ti,host-id",
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303013 info->desc->default_host_id);
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303014
3015 info->is_secure = dev_read_bool(dev, "ti,secure-host");
3016
3017 return 0;
3018}
3019
3020/**
3021 * ti_sci_probe() - Basic probe
3022 * @dev: corresponding system controller interface device
3023 *
3024 * Return: 0 if all goes good, else appropriate error message.
3025 */
3026static int ti_sci_probe(struct udevice *dev)
3027{
3028 struct ti_sci_info *info;
3029 int ret;
3030
3031 debug("%s(dev=%p)\n", __func__, dev);
3032
3033 info = dev_get_priv(dev);
3034 info->desc = (void *)dev_get_driver_data(dev);
3035
3036 ret = ti_sci_of_to_info(dev, info);
3037 if (ret) {
3038 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
3039 return ret;
3040 }
3041
3042 info->dev = dev;
3043 info->seq = 0xA;
3044
3045 list_add_tail(&info->list, &ti_sci_list);
Andreas Dannenbergdcfc52a2018-08-27 15:57:33 +05303046 ti_sci_setup_ops(info);
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303047
3048 ret = ti_sci_cmd_get_revision(&info->handle);
3049
Lokesh Vutla9566b772019-06-07 19:24:41 +05303050 INIT_LIST_HEAD(&info->dev_list);
3051
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303052 return ret;
3053}
3054
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303055/*
3056 * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3057 * @res: Pointer to the TISCI resource
3058 *
3059 * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3060 */
3061u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3062{
3063 u16 set, free_bit;
3064
3065 for (set = 0; set < res->sets; set++) {
3066 free_bit = find_first_zero_bit(res->desc[set].res_map,
3067 res->desc[set].num);
3068 if (free_bit != res->desc[set].num) {
3069 set_bit(free_bit, res->desc[set].res_map);
3070 return res->desc[set].start + free_bit;
3071 }
3072 }
3073
3074 return TI_SCI_RESOURCE_NULL;
3075}
3076
3077/**
3078 * ti_sci_release_resource() - Release a resource from TISCI resource.
3079 * @res: Pointer to the TISCI resource
3080 */
3081void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3082{
3083 u16 set;
3084
3085 for (set = 0; set < res->sets; set++) {
3086 if (res->desc[set].start <= id &&
3087 (res->desc[set].num + res->desc[set].start) > id)
3088 clear_bit(id - res->desc[set].start,
3089 res->desc[set].res_map);
3090 }
3091}
3092
3093/**
3094 * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
3095 * @handle: TISCI handle
3096 * @dev: Device pointer to which the resource is assigned
3097 * @of_prop: property name by which the resource are represented
3098 *
3099 * Note: This function expects of_prop to be in the form of tuples
3100 * <type, subtype>. Allocates and initializes ti_sci_resource structure
3101 * for each of_prop. Client driver can directly call
3102 * ti_sci_(get_free, release)_resource apis for handling the resource.
3103 *
3104 * Return: Pointer to ti_sci_resource if all went well else appropriate
3105 * error pointer.
3106 */
3107struct ti_sci_resource *
3108devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
3109 struct udevice *dev, u32 dev_id, char *of_prop)
3110{
3111 u32 resource_subtype;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303112 struct ti_sci_resource *res;
Vignesh Raghavendrac659a972019-08-05 12:26:44 -05003113 bool valid_set = false;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303114 int sets, i, ret;
3115 u32 *temp;
3116
3117 res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3118 if (!res)
3119 return ERR_PTR(-ENOMEM);
3120
3121 sets = dev_read_size(dev, of_prop);
3122 if (sets < 0) {
3123 dev_err(dev, "%s resource type ids not available\n", of_prop);
3124 return ERR_PTR(sets);
3125 }
3126 temp = malloc(sets);
3127 sets /= sizeof(u32);
3128 res->sets = sets;
3129
3130 res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3131 GFP_KERNEL);
3132 if (!res->desc)
3133 return ERR_PTR(-ENOMEM);
3134
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303135 ret = dev_read_u32_array(dev, of_prop, temp, res->sets);
3136 if (ret)
3137 return ERR_PTR(-EINVAL);
3138
3139 for (i = 0; i < res->sets; i++) {
3140 resource_subtype = temp[i];
3141 ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
3142 resource_subtype,
3143 &res->desc[i].start,
3144 &res->desc[i].num);
3145 if (ret) {
Vignesh Raghavendrac659a972019-08-05 12:26:44 -05003146 dev_dbg(dev, "type %d subtype %d not allocated for host %d\n",
Lokesh Vutla4986b152020-08-17 11:00:48 +05303147 dev_id, resource_subtype,
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303148 handle_to_ti_sci_info(handle)->host_id);
Vignesh Raghavendrac659a972019-08-05 12:26:44 -05003149 res->desc[i].start = 0;
3150 res->desc[i].num = 0;
3151 continue;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303152 }
3153
Vignesh Raghavendrac659a972019-08-05 12:26:44 -05003154 valid_set = true;
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303155 dev_dbg(dev, "res type = %d, subtype = %d, start = %d, num = %d\n",
Lokesh Vutla4986b152020-08-17 11:00:48 +05303156 dev_id, resource_subtype, res->desc[i].start,
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303157 res->desc[i].num);
3158
3159 res->desc[i].res_map =
3160 devm_kzalloc(dev, BITS_TO_LONGS(res->desc[i].num) *
3161 sizeof(*res->desc[i].res_map), GFP_KERNEL);
3162 if (!res->desc[i].res_map)
3163 return ERR_PTR(-ENOMEM);
3164 }
3165
Vignesh Raghavendrac659a972019-08-05 12:26:44 -05003166 if (valid_set)
3167 return res;
3168
3169 return ERR_PTR(-EINVAL);
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303170}
3171
3172/* Description for K2G */
3173static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
3174 .default_host_id = 2,
3175 /* Conservative duration */
3176 .max_rx_timeout_ms = 10000,
3177 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3178 .max_msgs = 20,
3179 .max_msg_size = 64,
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303180};
3181
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303182/* Description for AM654 */
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303183static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
3184 .default_host_id = 12,
3185 /* Conservative duration */
3186 .max_rx_timeout_ms = 10000,
3187 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3188 .max_msgs = 20,
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303189 .max_msg_size = 60,
3190};
3191
3192static const struct udevice_id ti_sci_ids[] = {
3193 {
3194 .compatible = "ti,k2g-sci",
Grygorii Strashkofd6b40b2019-02-05 17:31:21 +05303195 .data = (ulong)&ti_sci_pmmc_k2g_desc
3196 },
3197 {
3198 .compatible = "ti,am654-sci",
3199 .data = (ulong)&ti_sci_pmmc_am654_desc
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303200 },
3201 { /* Sentinel */ },
3202};
3203
3204U_BOOT_DRIVER(ti_sci) = {
3205 .name = "ti_sci",
3206 .id = UCLASS_FIRMWARE,
3207 .of_match = ti_sci_ids,
3208 .probe = ti_sci_probe,
Simon Glass41575d82020-12-03 16:55:17 -07003209 .priv_auto = sizeof(struct ti_sci_info),
Lokesh Vutla32cd2512018-08-27 15:57:32 +05303210};