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