blob: 00259a5f3b3b9ed8e87ab37d5a7c352698912389 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Texas Instruments System Control Interface Protocol Driver
4 *
5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
6 * Nishanth Menon
7 */
8
9#define pr_fmt(fmt) "%s: " fmt, __func__
10
11#include <linux/bitmap.h>
12#include <linux/debugfs.h>
13#include <linux/export.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/mailbox_client.h>
17#include <linux/module.h>
18#include <linux/of_device.h>
19#include <linux/semaphore.h>
20#include <linux/slab.h>
21#include <linux/soc/ti/ti-msgmgr.h>
22#include <linux/soc/ti/ti_sci_protocol.h>
23#include <linux/reboot.h>
24
25#include "ti_sci.h"
26
27/* List of all TI SCI devices active in system */
28static LIST_HEAD(ti_sci_list);
29/* Protection for the entire list */
30static DEFINE_MUTEX(ti_sci_list_mutex);
31
32/**
33 * struct ti_sci_xfer - Structure representing a message flow
34 * @tx_message: Transmit message
35 * @rx_len: Receive message length
36 * @xfer_buf: Preallocated buffer to store receive message
37 * Since we work with request-ACK protocol, we can
38 * reuse the same buffer for the rx path as we
39 * use for the tx path.
40 * @done: completion event
41 */
42struct ti_sci_xfer {
43 struct ti_msgmgr_message tx_message;
44 u8 rx_len;
45 u8 *xfer_buf;
46 struct completion done;
47};
48
49/**
50 * struct ti_sci_xfers_info - Structure to manage transfer information
51 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
52 * Messages.
53 * @xfer_block: Preallocated Message array
54 * @xfer_alloc_table: Bitmap table for allocated messages.
55 * Index of this bitmap table is also used for message
56 * sequence identifier.
57 * @xfer_lock: Protection for message allocation
58 */
59struct ti_sci_xfers_info {
60 struct semaphore sem_xfer_count;
61 struct ti_sci_xfer *xfer_block;
62 unsigned long *xfer_alloc_table;
63 /* protect transfer allocation */
64 spinlock_t xfer_lock;
65};
66
67/**
68 * struct ti_sci_rm_type_map - Structure representing TISCI Resource
69 * management representation of dev_ids.
70 * @dev_id: TISCI device ID
71 * @type: Corresponding id as identified by TISCI RM.
72 *
73 * Note: This is used only as a work around for using RM range apis
74 * for AM654 SoC. For future SoCs dev_id will be used as type
75 * for RM range APIs. In order to maintain ABI backward compatibility
76 * type is not being changed for AM654 SoC.
77 */
78struct ti_sci_rm_type_map {
79 u32 dev_id;
80 u16 type;
81};
82
83/**
84 * struct ti_sci_desc - Description of SoC integration
85 * @default_host_id: Host identifier representing the compute entity
86 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
87 * @max_msgs: Maximum number of messages that can be pending
88 * simultaneously in the system
89 * @max_msg_size: Maximum size of data per message that can be handled.
90 * @rm_type_map: RM resource type mapping structure.
91 */
92struct ti_sci_desc {
93 u8 default_host_id;
94 int max_rx_timeout_ms;
95 int max_msgs;
96 int max_msg_size;
97 struct ti_sci_rm_type_map *rm_type_map;
98};
99
100/**
101 * struct ti_sci_info - Structure representing a TI SCI instance
102 * @dev: Device pointer
103 * @desc: SoC description for this instance
104 * @nb: Reboot Notifier block
105 * @d: Debugfs file entry
106 * @debug_region: Memory region where the debug message are available
107 * @debug_region_size: Debug region size
108 * @debug_buffer: Buffer allocated to copy debug messages.
109 * @handle: Instance of TI SCI handle to send to clients.
110 * @cl: Mailbox Client
111 * @chan_tx: Transmit mailbox channel
112 * @chan_rx: Receive mailbox channel
113 * @minfo: Message info
114 * @node: list head
115 * @host_id: Host ID
116 * @users: Number of users of this instance
117 */
118struct ti_sci_info {
119 struct device *dev;
120 struct notifier_block nb;
121 const struct ti_sci_desc *desc;
122 struct dentry *d;
123 void __iomem *debug_region;
124 char *debug_buffer;
125 size_t debug_region_size;
126 struct ti_sci_handle handle;
127 struct mbox_client cl;
128 struct mbox_chan *chan_tx;
129 struct mbox_chan *chan_rx;
130 struct ti_sci_xfers_info minfo;
131 struct list_head node;
132 u8 host_id;
133 /* protected by ti_sci_list_mutex */
134 int users;
135
136};
137
138#define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
139#define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
140#define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
141
142#ifdef CONFIG_DEBUG_FS
143
144/**
145 * ti_sci_debug_show() - Helper to dump the debug log
146 * @s: sequence file pointer
147 * @unused: unused.
148 *
149 * Return: 0
150 */
151static int ti_sci_debug_show(struct seq_file *s, void *unused)
152{
153 struct ti_sci_info *info = s->private;
154
155 memcpy_fromio(info->debug_buffer, info->debug_region,
156 info->debug_region_size);
157 /*
158 * We don't trust firmware to leave NULL terminated last byte (hence
159 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
160 * specific data format for debug messages, We just present the data
161 * in the buffer as is - we expect the messages to be self explanatory.
162 */
163 seq_puts(s, info->debug_buffer);
164 return 0;
165}
166
167/* Provide the log file operations interface*/
168DEFINE_SHOW_ATTRIBUTE(ti_sci_debug);
169
170/**
171 * ti_sci_debugfs_create() - Create log debug file
172 * @pdev: platform device pointer
173 * @info: Pointer to SCI entity information
174 *
175 * Return: 0 if all went fine, else corresponding error.
176 */
177static int ti_sci_debugfs_create(struct platform_device *pdev,
178 struct ti_sci_info *info)
179{
180 struct device *dev = &pdev->dev;
181 struct resource *res;
182 char debug_name[50];
183
184 /* Debug region is optional */
185 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
186 "debug_messages");
187 info->debug_region = devm_ioremap_resource(dev, res);
188 if (IS_ERR(info->debug_region))
189 return 0;
190 info->debug_region_size = resource_size(res);
191
192 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
193 sizeof(char), GFP_KERNEL);
194 if (!info->debug_buffer)
195 return -ENOMEM;
196 /* Setup NULL termination */
197 info->debug_buffer[info->debug_region_size] = 0;
198
199 snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s",
200 dev_name(dev));
201 info->d = debugfs_create_file(debug_name, 0444, NULL, info,
202 &ti_sci_debug_fops);
203 if (IS_ERR(info->d))
204 return PTR_ERR(info->d);
205
206 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
207 info->debug_region, info->debug_region_size, res);
208 return 0;
209}
210
211#else /* CONFIG_DEBUG_FS */
212static inline int ti_sci_debugfs_create(struct platform_device *dev,
213 struct ti_sci_info *info)
214{
215 return 0;
216}
217
218static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
219 struct ti_sci_info *info)
220{
221}
222#endif /* CONFIG_DEBUG_FS */
223
224/**
225 * ti_sci_dump_header_dbg() - Helper to dump a message header.
226 * @dev: Device pointer corresponding to the SCI entity
227 * @hdr: pointer to header.
228 */
229static inline void ti_sci_dump_header_dbg(struct device *dev,
230 struct ti_sci_msg_hdr *hdr)
231{
232 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
233 hdr->type, hdr->host, hdr->seq, hdr->flags);
234}
235
236/**
237 * ti_sci_rx_callback() - mailbox client callback for receive messages
238 * @cl: client pointer
239 * @m: mailbox message
240 *
241 * Processes one received message to appropriate transfer information and
242 * signals completion of the transfer.
243 *
244 * NOTE: This function will be invoked in IRQ context, hence should be
245 * as optimal as possible.
246 */
247static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
248{
249 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
250 struct device *dev = info->dev;
251 struct ti_sci_xfers_info *minfo = &info->minfo;
252 struct ti_msgmgr_message *mbox_msg = m;
253 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
254 struct ti_sci_xfer *xfer;
255 u8 xfer_id;
256
257 xfer_id = hdr->seq;
258
259 /*
260 * Are we even expecting this?
261 * NOTE: barriers were implicit in locks used for modifying the bitmap
262 */
263 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
264 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
265 return;
266 }
267
268 xfer = &minfo->xfer_block[xfer_id];
269
270 /* Is the message of valid length? */
271 if (mbox_msg->len > info->desc->max_msg_size) {
272 dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
273 mbox_msg->len, info->desc->max_msg_size);
274 ti_sci_dump_header_dbg(dev, hdr);
275 return;
276 }
277 if (mbox_msg->len < xfer->rx_len) {
278 dev_err(dev, "Recv xfer %zu < expected %d length\n",
279 mbox_msg->len, xfer->rx_len);
280 ti_sci_dump_header_dbg(dev, hdr);
281 return;
282 }
283
284 ti_sci_dump_header_dbg(dev, hdr);
285 /* Take a copy to the rx buffer.. */
286 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
287 complete(&xfer->done);
288}
289
290/**
291 * ti_sci_get_one_xfer() - Allocate one message
292 * @info: Pointer to SCI entity information
293 * @msg_type: Message type
294 * @msg_flags: Flag to set for the message
295 * @tx_message_size: transmit message size
296 * @rx_message_size: receive message size
297 *
298 * Helper function which is used by various command functions that are
299 * exposed to clients of this driver for allocating a message traffic event.
300 *
301 * This function can sleep depending on pending requests already in the system
302 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
303 * of internal data structures.
304 *
305 * Return: 0 if all went fine, else corresponding error.
306 */
307static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
308 u16 msg_type, u32 msg_flags,
309 size_t tx_message_size,
310 size_t rx_message_size)
311{
312 struct ti_sci_xfers_info *minfo = &info->minfo;
313 struct ti_sci_xfer *xfer;
314 struct ti_sci_msg_hdr *hdr;
315 unsigned long flags;
316 unsigned long bit_pos;
317 u8 xfer_id;
318 int ret;
319 int timeout;
320
321 /* Ensure we have sane transfer sizes */
322 if (rx_message_size > info->desc->max_msg_size ||
323 tx_message_size > info->desc->max_msg_size ||
324 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
325 return ERR_PTR(-ERANGE);
326
327 /*
328 * Ensure we have only controlled number of pending messages.
329 * Ideally, we might just have to wait a single message, be
330 * conservative and wait 5 times that..
331 */
332 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
333 ret = down_timeout(&minfo->sem_xfer_count, timeout);
334 if (ret < 0)
335 return ERR_PTR(ret);
336
337 /* Keep the locked section as small as possible */
338 spin_lock_irqsave(&minfo->xfer_lock, flags);
339 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
340 info->desc->max_msgs);
341 set_bit(bit_pos, minfo->xfer_alloc_table);
342 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
343
344 /*
345 * We already ensured in probe that we can have max messages that can
346 * fit in hdr.seq - NOTE: this improves access latencies
347 * to predictable O(1) access, BUT, it opens us to risk if
348 * remote misbehaves with corrupted message sequence responses.
349 * If that happens, we are going to be messed up anyways..
350 */
351 xfer_id = (u8)bit_pos;
352
353 xfer = &minfo->xfer_block[xfer_id];
354
355 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
356 xfer->tx_message.len = tx_message_size;
357 xfer->rx_len = (u8)rx_message_size;
358
359 reinit_completion(&xfer->done);
360
361 hdr->seq = xfer_id;
362 hdr->type = msg_type;
363 hdr->host = info->host_id;
364 hdr->flags = msg_flags;
365
366 return xfer;
367}
368
369/**
370 * ti_sci_put_one_xfer() - Release a message
371 * @minfo: transfer info pointer
372 * @xfer: message that was reserved by ti_sci_get_one_xfer
373 *
374 * This holds a spinlock to maintain integrity of internal data structures.
375 */
376static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
377 struct ti_sci_xfer *xfer)
378{
379 unsigned long flags;
380 struct ti_sci_msg_hdr *hdr;
381 u8 xfer_id;
382
383 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
384 xfer_id = hdr->seq;
385
386 /*
387 * Keep the locked section as small as possible
388 * NOTE: we might escape with smp_mb and no lock here..
389 * but just be conservative and symmetric.
390 */
391 spin_lock_irqsave(&minfo->xfer_lock, flags);
392 clear_bit(xfer_id, minfo->xfer_alloc_table);
393 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
394
395 /* Increment the count for the next user to get through */
396 up(&minfo->sem_xfer_count);
397}
398
399/**
400 * ti_sci_do_xfer() - Do one transfer
401 * @info: Pointer to SCI entity information
402 * @xfer: Transfer to initiate and wait for response
403 *
404 * Return: -ETIMEDOUT in case of no response, if transmit error,
405 * return corresponding error, else if all goes well,
406 * return 0.
407 */
408static inline int ti_sci_do_xfer(struct ti_sci_info *info,
409 struct ti_sci_xfer *xfer)
410{
411 int ret;
412 int timeout;
413 struct device *dev = info->dev;
414
415 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
416 if (ret < 0)
417 return ret;
418
419 ret = 0;
420
421 /* And we wait for the response. */
422 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
423 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
424 dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
425 (void *)_RET_IP_);
426 ret = -ETIMEDOUT;
427 }
428 /*
429 * NOTE: we might prefer not to need the mailbox ticker to manage the
430 * transfer queueing since the protocol layer queues things by itself.
431 * Unfortunately, we have to kick the mailbox framework after we have
432 * received our message.
433 */
434 mbox_client_txdone(info->chan_tx, ret);
435
436 return ret;
437}
438
439/**
440 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
441 * @info: Pointer to SCI entity information
442 *
443 * Updates the SCI information in the internal data structure.
444 *
445 * Return: 0 if all went fine, else return appropriate error.
446 */
447static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
448{
449 struct device *dev = info->dev;
450 struct ti_sci_handle *handle = &info->handle;
451 struct ti_sci_version_info *ver = &handle->version;
452 struct ti_sci_msg_resp_version *rev_info;
453 struct ti_sci_xfer *xfer;
454 int ret;
455
456 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
457 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
458 sizeof(struct ti_sci_msg_hdr),
459 sizeof(*rev_info));
460 if (IS_ERR(xfer)) {
461 ret = PTR_ERR(xfer);
462 dev_err(dev, "Message alloc failed(%d)\n", ret);
463 return ret;
464 }
465
466 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
467
468 ret = ti_sci_do_xfer(info, xfer);
469 if (ret) {
470 dev_err(dev, "Mbox send fail %d\n", ret);
471 goto fail;
472 }
473
474 ver->abi_major = rev_info->abi_major;
475 ver->abi_minor = rev_info->abi_minor;
476 ver->firmware_revision = rev_info->firmware_revision;
477 strncpy(ver->firmware_description, rev_info->firmware_description,
478 sizeof(ver->firmware_description));
479
480fail:
481 ti_sci_put_one_xfer(&info->minfo, xfer);
482 return ret;
483}
484
485/**
486 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
487 * @r: pointer to response buffer
488 *
489 * Return: true if the response was an ACK, else returns false.
490 */
491static inline bool ti_sci_is_response_ack(void *r)
492{
493 struct ti_sci_msg_hdr *hdr = r;
494
495 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
496}
497
498/**
499 * ti_sci_set_device_state() - Set device state helper
500 * @handle: pointer to TI SCI handle
501 * @id: Device identifier
502 * @flags: flags to setup for the device
503 * @state: State to move the device to
504 *
505 * Return: 0 if all went well, else returns appropriate error value.
506 */
507static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
508 u32 id, u32 flags, u8 state)
509{
510 struct ti_sci_info *info;
511 struct ti_sci_msg_req_set_device_state *req;
512 struct ti_sci_msg_hdr *resp;
513 struct ti_sci_xfer *xfer;
514 struct device *dev;
515 int ret = 0;
516
517 if (IS_ERR(handle))
518 return PTR_ERR(handle);
519 if (!handle)
520 return -EINVAL;
521
522 info = handle_to_ti_sci_info(handle);
523 dev = info->dev;
524
525 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
526 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
527 sizeof(*req), sizeof(*resp));
528 if (IS_ERR(xfer)) {
529 ret = PTR_ERR(xfer);
530 dev_err(dev, "Message alloc failed(%d)\n", ret);
531 return ret;
532 }
533 req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
534 req->id = id;
535 req->state = state;
536
537 ret = ti_sci_do_xfer(info, xfer);
538 if (ret) {
539 dev_err(dev, "Mbox send fail %d\n", ret);
540 goto fail;
541 }
542
543 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
544
545 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
546
547fail:
548 ti_sci_put_one_xfer(&info->minfo, xfer);
549
550 return ret;
551}
552
553/**
554 * ti_sci_get_device_state() - Get device state helper
555 * @handle: Handle to the device
556 * @id: Device Identifier
557 * @clcnt: Pointer to Context Loss Count
558 * @resets: pointer to resets
559 * @p_state: pointer to p_state
560 * @c_state: pointer to c_state
561 *
562 * Return: 0 if all went fine, else return appropriate error.
563 */
564static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
565 u32 id, u32 *clcnt, u32 *resets,
566 u8 *p_state, u8 *c_state)
567{
568 struct ti_sci_info *info;
569 struct ti_sci_msg_req_get_device_state *req;
570 struct ti_sci_msg_resp_get_device_state *resp;
571 struct ti_sci_xfer *xfer;
572 struct device *dev;
573 int ret = 0;
574
575 if (IS_ERR(handle))
576 return PTR_ERR(handle);
577 if (!handle)
578 return -EINVAL;
579
580 if (!clcnt && !resets && !p_state && !c_state)
581 return -EINVAL;
582
583 info = handle_to_ti_sci_info(handle);
584 dev = info->dev;
585
586 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
587 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
588 sizeof(*req), sizeof(*resp));
589 if (IS_ERR(xfer)) {
590 ret = PTR_ERR(xfer);
591 dev_err(dev, "Message alloc failed(%d)\n", ret);
592 return ret;
593 }
594 req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
595 req->id = id;
596
597 ret = ti_sci_do_xfer(info, xfer);
598 if (ret) {
599 dev_err(dev, "Mbox send fail %d\n", ret);
600 goto fail;
601 }
602
603 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
604 if (!ti_sci_is_response_ack(resp)) {
605 ret = -ENODEV;
606 goto fail;
607 }
608
609 if (clcnt)
610 *clcnt = resp->context_loss_count;
611 if (resets)
612 *resets = resp->resets;
613 if (p_state)
614 *p_state = resp->programmed_state;
615 if (c_state)
616 *c_state = resp->current_state;
617fail:
618 ti_sci_put_one_xfer(&info->minfo, xfer);
619
620 return ret;
621}
622
623/**
624 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
625 * that can be shared with other hosts.
626 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
627 * @id: Device Identifier
628 *
629 * Request for the device - NOTE: the client MUST maintain integrity of
630 * usage count by balancing get_device with put_device. No refcounting is
631 * managed by driver for that purpose.
632 *
633 * Return: 0 if all went fine, else return appropriate error.
634 */
635static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
636{
637 return ti_sci_set_device_state(handle, id, 0,
638 MSG_DEVICE_SW_STATE_ON);
639}
640
641/**
642 * ti_sci_cmd_get_device_exclusive() - command to request for device managed by
643 * TISCI that is exclusively owned by the
644 * requesting host.
645 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
646 * @id: Device Identifier
647 *
648 * Request for the device - NOTE: the client MUST maintain integrity of
649 * usage count by balancing get_device with put_device. No refcounting is
650 * managed by driver for that purpose.
651 *
652 * Return: 0 if all went fine, else return appropriate error.
653 */
654static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
655 u32 id)
656{
657 return ti_sci_set_device_state(handle, id,
658 MSG_FLAG_DEVICE_EXCLUSIVE,
659 MSG_DEVICE_SW_STATE_ON);
660}
661
662/**
663 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
664 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
665 * @id: Device Identifier
666 *
667 * Request for the device - NOTE: the client MUST maintain integrity of
668 * usage count by balancing get_device with put_device. No refcounting is
669 * managed by driver for that purpose.
670 *
671 * Return: 0 if all went fine, else return appropriate error.
672 */
673static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
674{
675 return ti_sci_set_device_state(handle, id, 0,
676 MSG_DEVICE_SW_STATE_RETENTION);
677}
678
679/**
680 * ti_sci_cmd_idle_device_exclusive() - Command to idle a device managed by
681 * TISCI that is exclusively owned by
682 * requesting host.
683 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
684 * @id: Device Identifier
685 *
686 * Request for the device - NOTE: the client MUST maintain integrity of
687 * usage count by balancing get_device with put_device. No refcounting is
688 * managed by driver for that purpose.
689 *
690 * Return: 0 if all went fine, else return appropriate error.
691 */
692static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
693 u32 id)
694{
695 return ti_sci_set_device_state(handle, id,
696 MSG_FLAG_DEVICE_EXCLUSIVE,
697 MSG_DEVICE_SW_STATE_RETENTION);
698}
699
700/**
701 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
702 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
703 * @id: Device Identifier
704 *
705 * Request for the device - NOTE: the client MUST maintain integrity of
706 * usage count by balancing get_device with put_device. No refcounting is
707 * managed by driver for that purpose.
708 *
709 * Return: 0 if all went fine, else return appropriate error.
710 */
711static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
712{
713 return ti_sci_set_device_state(handle, id,
714 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
715}
716
717/**
718 * ti_sci_cmd_dev_is_valid() - Is the device valid
719 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
720 * @id: Device Identifier
721 *
722 * Return: 0 if all went fine and the device ID is valid, else return
723 * appropriate error.
724 */
725static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
726{
727 u8 unused;
728
729 /* check the device state which will also tell us if the ID is valid */
730 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
731}
732
733/**
734 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
735 * @handle: Pointer to TISCI handle
736 * @id: Device Identifier
737 * @count: Pointer to Context Loss counter to populate
738 *
739 * Return: 0 if all went fine, else return appropriate error.
740 */
741static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
742 u32 *count)
743{
744 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
745}
746
747/**
748 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
749 * @handle: Pointer to TISCI handle
750 * @id: Device Identifier
751 * @r_state: true if requested to be idle
752 *
753 * Return: 0 if all went fine, else return appropriate error.
754 */
755static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
756 bool *r_state)
757{
758 int ret;
759 u8 state;
760
761 if (!r_state)
762 return -EINVAL;
763
764 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
765 if (ret)
766 return ret;
767
768 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
769
770 return 0;
771}
772
773/**
774 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
775 * @handle: Pointer to TISCI handle
776 * @id: Device Identifier
777 * @r_state: true if requested to be stopped
778 * @curr_state: true if currently stopped.
779 *
780 * Return: 0 if all went fine, else return appropriate error.
781 */
782static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
783 bool *r_state, bool *curr_state)
784{
785 int ret;
786 u8 p_state, c_state;
787
788 if (!r_state && !curr_state)
789 return -EINVAL;
790
791 ret =
792 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
793 if (ret)
794 return ret;
795
796 if (r_state)
797 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
798 if (curr_state)
799 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
800
801 return 0;
802}
803
804/**
805 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
806 * @handle: Pointer to TISCI handle
807 * @id: Device Identifier
808 * @r_state: true if requested to be ON
809 * @curr_state: true if currently ON and active
810 *
811 * Return: 0 if all went fine, else return appropriate error.
812 */
813static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
814 bool *r_state, bool *curr_state)
815{
816 int ret;
817 u8 p_state, c_state;
818
819 if (!r_state && !curr_state)
820 return -EINVAL;
821
822 ret =
823 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
824 if (ret)
825 return ret;
826
827 if (r_state)
828 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
829 if (curr_state)
830 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
831
832 return 0;
833}
834
835/**
836 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
837 * @handle: Pointer to TISCI handle
838 * @id: Device Identifier
839 * @curr_state: true if currently transitioning.
840 *
841 * Return: 0 if all went fine, else return appropriate error.
842 */
843static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
844 bool *curr_state)
845{
846 int ret;
847 u8 state;
848
849 if (!curr_state)
850 return -EINVAL;
851
852 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
853 if (ret)
854 return ret;
855
856 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
857
858 return 0;
859}
860
861/**
862 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
863 * by TISCI
864 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
865 * @id: Device Identifier
866 * @reset_state: Device specific reset bit field
867 *
868 * Return: 0 if all went fine, else return appropriate error.
869 */
870static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
871 u32 id, u32 reset_state)
872{
873 struct ti_sci_info *info;
874 struct ti_sci_msg_req_set_device_resets *req;
875 struct ti_sci_msg_hdr *resp;
876 struct ti_sci_xfer *xfer;
877 struct device *dev;
878 int ret = 0;
879
880 if (IS_ERR(handle))
881 return PTR_ERR(handle);
882 if (!handle)
883 return -EINVAL;
884
885 info = handle_to_ti_sci_info(handle);
886 dev = info->dev;
887
888 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
889 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
890 sizeof(*req), sizeof(*resp));
891 if (IS_ERR(xfer)) {
892 ret = PTR_ERR(xfer);
893 dev_err(dev, "Message alloc failed(%d)\n", ret);
894 return ret;
895 }
896 req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
897 req->id = id;
898 req->resets = reset_state;
899
900 ret = ti_sci_do_xfer(info, xfer);
901 if (ret) {
902 dev_err(dev, "Mbox send fail %d\n", ret);
903 goto fail;
904 }
905
906 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
907
908 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
909
910fail:
911 ti_sci_put_one_xfer(&info->minfo, xfer);
912
913 return ret;
914}
915
916/**
917 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
918 * by TISCI
919 * @handle: Pointer to TISCI handle
920 * @id: Device Identifier
921 * @reset_state: Pointer to reset state to populate
922 *
923 * Return: 0 if all went fine, else return appropriate error.
924 */
925static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
926 u32 id, u32 *reset_state)
927{
928 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
929 NULL);
930}
931
932/**
933 * ti_sci_set_clock_state() - Set clock state helper
934 * @handle: pointer to TI SCI handle
935 * @dev_id: Device identifier this request is for
936 * @clk_id: Clock identifier for the device for this request.
937 * Each device has it's own set of clock inputs. This indexes
938 * which clock input to modify.
939 * @flags: Header flags as needed
940 * @state: State to request for the clock.
941 *
942 * Return: 0 if all went well, else returns appropriate error value.
943 */
944static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
945 u32 dev_id, u32 clk_id,
946 u32 flags, u8 state)
947{
948 struct ti_sci_info *info;
949 struct ti_sci_msg_req_set_clock_state *req;
950 struct ti_sci_msg_hdr *resp;
951 struct ti_sci_xfer *xfer;
952 struct device *dev;
953 int ret = 0;
954
955 if (IS_ERR(handle))
956 return PTR_ERR(handle);
957 if (!handle)
958 return -EINVAL;
959
960 info = handle_to_ti_sci_info(handle);
961 dev = info->dev;
962
963 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
964 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
965 sizeof(*req), sizeof(*resp));
966 if (IS_ERR(xfer)) {
967 ret = PTR_ERR(xfer);
968 dev_err(dev, "Message alloc failed(%d)\n", ret);
969 return ret;
970 }
971 req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
972 req->dev_id = dev_id;
973 if (clk_id < 255) {
974 req->clk_id = clk_id;
975 } else {
976 req->clk_id = 255;
977 req->clk_id_32 = clk_id;
978 }
979 req->request_state = state;
980
981 ret = ti_sci_do_xfer(info, xfer);
982 if (ret) {
983 dev_err(dev, "Mbox send fail %d\n", ret);
984 goto fail;
985 }
986
987 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
988
989 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
990
991fail:
992 ti_sci_put_one_xfer(&info->minfo, xfer);
993
994 return ret;
995}
996
997/**
998 * ti_sci_cmd_get_clock_state() - Get clock state helper
999 * @handle: pointer to TI SCI handle
1000 * @dev_id: Device identifier this request is for
1001 * @clk_id: Clock identifier for the device for this request.
1002 * Each device has it's own set of clock inputs. This indexes
1003 * which clock input to modify.
1004 * @programmed_state: State requested for clock to move to
1005 * @current_state: State that the clock is currently in
1006 *
1007 * Return: 0 if all went well, else returns appropriate error value.
1008 */
1009static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
1010 u32 dev_id, u32 clk_id,
1011 u8 *programmed_state, u8 *current_state)
1012{
1013 struct ti_sci_info *info;
1014 struct ti_sci_msg_req_get_clock_state *req;
1015 struct ti_sci_msg_resp_get_clock_state *resp;
1016 struct ti_sci_xfer *xfer;
1017 struct device *dev;
1018 int ret = 0;
1019
1020 if (IS_ERR(handle))
1021 return PTR_ERR(handle);
1022 if (!handle)
1023 return -EINVAL;
1024
1025 if (!programmed_state && !current_state)
1026 return -EINVAL;
1027
1028 info = handle_to_ti_sci_info(handle);
1029 dev = info->dev;
1030
1031 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1032 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1033 sizeof(*req), sizeof(*resp));
1034 if (IS_ERR(xfer)) {
1035 ret = PTR_ERR(xfer);
1036 dev_err(dev, "Message alloc failed(%d)\n", ret);
1037 return ret;
1038 }
1039 req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1040 req->dev_id = dev_id;
1041 if (clk_id < 255) {
1042 req->clk_id = clk_id;
1043 } else {
1044 req->clk_id = 255;
1045 req->clk_id_32 = clk_id;
1046 }
1047
1048 ret = ti_sci_do_xfer(info, xfer);
1049 if (ret) {
1050 dev_err(dev, "Mbox send fail %d\n", ret);
1051 goto fail;
1052 }
1053
1054 resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1055
1056 if (!ti_sci_is_response_ack(resp)) {
1057 ret = -ENODEV;
1058 goto fail;
1059 }
1060
1061 if (programmed_state)
1062 *programmed_state = resp->programmed_state;
1063 if (current_state)
1064 *current_state = resp->current_state;
1065
1066fail:
1067 ti_sci_put_one_xfer(&info->minfo, xfer);
1068
1069 return ret;
1070}
1071
1072/**
1073 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1074 * @handle: pointer to TI SCI handle
1075 * @dev_id: Device identifier this request is for
1076 * @clk_id: Clock identifier for the device for this request.
1077 * Each device has it's own set of clock inputs. This indexes
1078 * which clock input to modify.
1079 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1080 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1081 * @enable_input_term: 'true' if input termination is desired, else 'false'
1082 *
1083 * Return: 0 if all went well, else returns appropriate error value.
1084 */
1085static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1086 u32 clk_id, bool needs_ssc,
1087 bool can_change_freq, bool enable_input_term)
1088{
1089 u32 flags = 0;
1090
1091 flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1092 flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1093 flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1094
1095 return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1096 MSG_CLOCK_SW_STATE_REQ);
1097}
1098
1099/**
1100 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1101 * @handle: pointer to TI SCI handle
1102 * @dev_id: Device identifier this request is for
1103 * @clk_id: Clock identifier for the device for this request.
1104 * Each device has it's own set of clock inputs. This indexes
1105 * which clock input to modify.
1106 *
1107 * NOTE: This clock must have been requested by get_clock previously.
1108 *
1109 * Return: 0 if all went well, else returns appropriate error value.
1110 */
1111static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1112 u32 dev_id, u32 clk_id)
1113{
1114 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1115 MSG_CLOCK_SW_STATE_UNREQ);
1116}
1117
1118/**
1119 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1120 * @handle: pointer to TI SCI handle
1121 * @dev_id: Device identifier this request is for
1122 * @clk_id: Clock identifier for the device for this request.
1123 * Each device has it's own set of clock inputs. This indexes
1124 * which clock input to modify.
1125 *
1126 * NOTE: This clock must have been requested by get_clock previously.
1127 *
1128 * Return: 0 if all went well, else returns appropriate error value.
1129 */
1130static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1131 u32 dev_id, u32 clk_id)
1132{
1133 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1134 MSG_CLOCK_SW_STATE_AUTO);
1135}
1136
1137/**
1138 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1139 * @handle: pointer to TI SCI handle
1140 * @dev_id: Device identifier this request is for
1141 * @clk_id: Clock identifier for the device for this request.
1142 * Each device has it's own set of clock inputs. This indexes
1143 * which clock input to modify.
1144 * @req_state: state indicating if the clock is auto managed
1145 *
1146 * Return: 0 if all went well, else returns appropriate error value.
1147 */
1148static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1149 u32 dev_id, u32 clk_id, bool *req_state)
1150{
1151 u8 state = 0;
1152 int ret;
1153
1154 if (!req_state)
1155 return -EINVAL;
1156
1157 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1158 if (ret)
1159 return ret;
1160
1161 *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1162 return 0;
1163}
1164
1165/**
1166 * ti_sci_cmd_clk_is_on() - Is the clock ON
1167 * @handle: pointer to TI SCI handle
1168 * @dev_id: Device identifier this request is for
1169 * @clk_id: Clock identifier for the device for this request.
1170 * Each device has it's own set of clock inputs. This indexes
1171 * which clock input to modify.
1172 * @req_state: state indicating if the clock is managed by us and enabled
1173 * @curr_state: state indicating if the clock is ready for operation
1174 *
1175 * Return: 0 if all went well, else returns appropriate error value.
1176 */
1177static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1178 u32 clk_id, bool *req_state, bool *curr_state)
1179{
1180 u8 c_state = 0, r_state = 0;
1181 int ret;
1182
1183 if (!req_state && !curr_state)
1184 return -EINVAL;
1185
1186 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1187 &r_state, &c_state);
1188 if (ret)
1189 return ret;
1190
1191 if (req_state)
1192 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1193 if (curr_state)
1194 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1195 return 0;
1196}
1197
1198/**
1199 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1200 * @handle: pointer to TI SCI handle
1201 * @dev_id: Device identifier this request is for
1202 * @clk_id: Clock identifier for the device for this request.
1203 * Each device has it's own set of clock inputs. This indexes
1204 * which clock input to modify.
1205 * @req_state: state indicating if the clock is managed by us and disabled
1206 * @curr_state: state indicating if the clock is NOT ready for operation
1207 *
1208 * Return: 0 if all went well, else returns appropriate error value.
1209 */
1210static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1211 u32 clk_id, bool *req_state, bool *curr_state)
1212{
1213 u8 c_state = 0, r_state = 0;
1214 int ret;
1215
1216 if (!req_state && !curr_state)
1217 return -EINVAL;
1218
1219 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1220 &r_state, &c_state);
1221 if (ret)
1222 return ret;
1223
1224 if (req_state)
1225 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1226 if (curr_state)
1227 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1228 return 0;
1229}
1230
1231/**
1232 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1233 * @handle: pointer to TI SCI handle
1234 * @dev_id: Device identifier this request is for
1235 * @clk_id: Clock identifier for the device for this request.
1236 * Each device has it's own set of clock inputs. This indexes
1237 * which clock input to modify.
1238 * @parent_id: Parent clock identifier to set
1239 *
1240 * Return: 0 if all went well, else returns appropriate error value.
1241 */
1242static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1243 u32 dev_id, u32 clk_id, u32 parent_id)
1244{
1245 struct ti_sci_info *info;
1246 struct ti_sci_msg_req_set_clock_parent *req;
1247 struct ti_sci_msg_hdr *resp;
1248 struct ti_sci_xfer *xfer;
1249 struct device *dev;
1250 int ret = 0;
1251
1252 if (IS_ERR(handle))
1253 return PTR_ERR(handle);
1254 if (!handle)
1255 return -EINVAL;
1256
1257 info = handle_to_ti_sci_info(handle);
1258 dev = info->dev;
1259
1260 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1261 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1262 sizeof(*req), sizeof(*resp));
1263 if (IS_ERR(xfer)) {
1264 ret = PTR_ERR(xfer);
1265 dev_err(dev, "Message alloc failed(%d)\n", ret);
1266 return ret;
1267 }
1268 req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1269 req->dev_id = dev_id;
1270 if (clk_id < 255) {
1271 req->clk_id = clk_id;
1272 } else {
1273 req->clk_id = 255;
1274 req->clk_id_32 = clk_id;
1275 }
1276 if (parent_id < 255) {
1277 req->parent_id = parent_id;
1278 } else {
1279 req->parent_id = 255;
1280 req->parent_id_32 = parent_id;
1281 }
1282
1283 ret = ti_sci_do_xfer(info, xfer);
1284 if (ret) {
1285 dev_err(dev, "Mbox send fail %d\n", ret);
1286 goto fail;
1287 }
1288
1289 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1290
1291 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1292
1293fail:
1294 ti_sci_put_one_xfer(&info->minfo, xfer);
1295
1296 return ret;
1297}
1298
1299/**
1300 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1301 * @handle: pointer to TI SCI handle
1302 * @dev_id: Device identifier this request is for
1303 * @clk_id: Clock identifier for the device for this request.
1304 * Each device has it's own set of clock inputs. This indexes
1305 * which clock input to modify.
1306 * @parent_id: Current clock parent
1307 *
1308 * Return: 0 if all went well, else returns appropriate error value.
1309 */
1310static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1311 u32 dev_id, u32 clk_id, u32 *parent_id)
1312{
1313 struct ti_sci_info *info;
1314 struct ti_sci_msg_req_get_clock_parent *req;
1315 struct ti_sci_msg_resp_get_clock_parent *resp;
1316 struct ti_sci_xfer *xfer;
1317 struct device *dev;
1318 int ret = 0;
1319
1320 if (IS_ERR(handle))
1321 return PTR_ERR(handle);
1322 if (!handle || !parent_id)
1323 return -EINVAL;
1324
1325 info = handle_to_ti_sci_info(handle);
1326 dev = info->dev;
1327
1328 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1329 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1330 sizeof(*req), sizeof(*resp));
1331 if (IS_ERR(xfer)) {
1332 ret = PTR_ERR(xfer);
1333 dev_err(dev, "Message alloc failed(%d)\n", ret);
1334 return ret;
1335 }
1336 req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1337 req->dev_id = dev_id;
1338 if (clk_id < 255) {
1339 req->clk_id = clk_id;
1340 } else {
1341 req->clk_id = 255;
1342 req->clk_id_32 = clk_id;
1343 }
1344
1345 ret = ti_sci_do_xfer(info, xfer);
1346 if (ret) {
1347 dev_err(dev, "Mbox send fail %d\n", ret);
1348 goto fail;
1349 }
1350
1351 resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1352
1353 if (!ti_sci_is_response_ack(resp)) {
1354 ret = -ENODEV;
1355 } else {
1356 if (resp->parent_id < 255)
1357 *parent_id = resp->parent_id;
1358 else
1359 *parent_id = resp->parent_id_32;
1360 }
1361
1362fail:
1363 ti_sci_put_one_xfer(&info->minfo, xfer);
1364
1365 return ret;
1366}
1367
1368/**
1369 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1370 * @handle: pointer to TI SCI handle
1371 * @dev_id: Device identifier this request is for
1372 * @clk_id: Clock identifier for the device for this request.
1373 * Each device has it's own set of clock inputs. This indexes
1374 * which clock input to modify.
1375 * @num_parents: Returns he number of parents to the current clock.
1376 *
1377 * Return: 0 if all went well, else returns appropriate error value.
1378 */
1379static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1380 u32 dev_id, u32 clk_id,
1381 u32 *num_parents)
1382{
1383 struct ti_sci_info *info;
1384 struct ti_sci_msg_req_get_clock_num_parents *req;
1385 struct ti_sci_msg_resp_get_clock_num_parents *resp;
1386 struct ti_sci_xfer *xfer;
1387 struct device *dev;
1388 int ret = 0;
1389
1390 if (IS_ERR(handle))
1391 return PTR_ERR(handle);
1392 if (!handle || !num_parents)
1393 return -EINVAL;
1394
1395 info = handle_to_ti_sci_info(handle);
1396 dev = info->dev;
1397
1398 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1399 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1400 sizeof(*req), sizeof(*resp));
1401 if (IS_ERR(xfer)) {
1402 ret = PTR_ERR(xfer);
1403 dev_err(dev, "Message alloc failed(%d)\n", ret);
1404 return ret;
1405 }
1406 req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1407 req->dev_id = dev_id;
1408 if (clk_id < 255) {
1409 req->clk_id = clk_id;
1410 } else {
1411 req->clk_id = 255;
1412 req->clk_id_32 = clk_id;
1413 }
1414
1415 ret = ti_sci_do_xfer(info, xfer);
1416 if (ret) {
1417 dev_err(dev, "Mbox send fail %d\n", ret);
1418 goto fail;
1419 }
1420
1421 resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1422
1423 if (!ti_sci_is_response_ack(resp)) {
1424 ret = -ENODEV;
1425 } else {
1426 if (resp->num_parents < 255)
1427 *num_parents = resp->num_parents;
1428 else
1429 *num_parents = resp->num_parents_32;
1430 }
1431
1432fail:
1433 ti_sci_put_one_xfer(&info->minfo, xfer);
1434
1435 return ret;
1436}
1437
1438/**
1439 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1440 * @handle: pointer to TI SCI handle
1441 * @dev_id: Device identifier this request is for
1442 * @clk_id: Clock identifier for the device for this request.
1443 * Each device has it's own set of clock inputs. This indexes
1444 * which clock input to modify.
1445 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1446 * allowable programmed frequency and does not account for clock
1447 * tolerances and jitter.
1448 * @target_freq: The target clock frequency in Hz. A frequency will be
1449 * processed as close to this target frequency as possible.
1450 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1451 * allowable programmed frequency and does not account for clock
1452 * tolerances and jitter.
1453 * @match_freq: Frequency match in Hz response.
1454 *
1455 * Return: 0 if all went well, else returns appropriate error value.
1456 */
1457static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1458 u32 dev_id, u32 clk_id, u64 min_freq,
1459 u64 target_freq, u64 max_freq,
1460 u64 *match_freq)
1461{
1462 struct ti_sci_info *info;
1463 struct ti_sci_msg_req_query_clock_freq *req;
1464 struct ti_sci_msg_resp_query_clock_freq *resp;
1465 struct ti_sci_xfer *xfer;
1466 struct device *dev;
1467 int ret = 0;
1468
1469 if (IS_ERR(handle))
1470 return PTR_ERR(handle);
1471 if (!handle || !match_freq)
1472 return -EINVAL;
1473
1474 info = handle_to_ti_sci_info(handle);
1475 dev = info->dev;
1476
1477 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1478 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1479 sizeof(*req), sizeof(*resp));
1480 if (IS_ERR(xfer)) {
1481 ret = PTR_ERR(xfer);
1482 dev_err(dev, "Message alloc failed(%d)\n", ret);
1483 return ret;
1484 }
1485 req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1486 req->dev_id = dev_id;
1487 if (clk_id < 255) {
1488 req->clk_id = clk_id;
1489 } else {
1490 req->clk_id = 255;
1491 req->clk_id_32 = clk_id;
1492 }
1493 req->min_freq_hz = min_freq;
1494 req->target_freq_hz = target_freq;
1495 req->max_freq_hz = max_freq;
1496
1497 ret = ti_sci_do_xfer(info, xfer);
1498 if (ret) {
1499 dev_err(dev, "Mbox send fail %d\n", ret);
1500 goto fail;
1501 }
1502
1503 resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1504
1505 if (!ti_sci_is_response_ack(resp))
1506 ret = -ENODEV;
1507 else
1508 *match_freq = resp->freq_hz;
1509
1510fail:
1511 ti_sci_put_one_xfer(&info->minfo, xfer);
1512
1513 return ret;
1514}
1515
1516/**
1517 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1518 * @handle: pointer to TI SCI handle
1519 * @dev_id: Device identifier this request is for
1520 * @clk_id: Clock identifier for the device for this request.
1521 * Each device has it's own set of clock inputs. This indexes
1522 * which clock input to modify.
1523 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1524 * allowable programmed frequency and does not account for clock
1525 * tolerances and jitter.
1526 * @target_freq: The target clock frequency in Hz. A frequency will be
1527 * processed as close to this target frequency as possible.
1528 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1529 * allowable programmed frequency and does not account for clock
1530 * tolerances and jitter.
1531 *
1532 * Return: 0 if all went well, else returns appropriate error value.
1533 */
1534static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1535 u32 dev_id, u32 clk_id, u64 min_freq,
1536 u64 target_freq, u64 max_freq)
1537{
1538 struct ti_sci_info *info;
1539 struct ti_sci_msg_req_set_clock_freq *req;
1540 struct ti_sci_msg_hdr *resp;
1541 struct ti_sci_xfer *xfer;
1542 struct device *dev;
1543 int ret = 0;
1544
1545 if (IS_ERR(handle))
1546 return PTR_ERR(handle);
1547 if (!handle)
1548 return -EINVAL;
1549
1550 info = handle_to_ti_sci_info(handle);
1551 dev = info->dev;
1552
1553 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1554 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1555 sizeof(*req), sizeof(*resp));
1556 if (IS_ERR(xfer)) {
1557 ret = PTR_ERR(xfer);
1558 dev_err(dev, "Message alloc failed(%d)\n", ret);
1559 return ret;
1560 }
1561 req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1562 req->dev_id = dev_id;
1563 if (clk_id < 255) {
1564 req->clk_id = clk_id;
1565 } else {
1566 req->clk_id = 255;
1567 req->clk_id_32 = clk_id;
1568 }
1569 req->min_freq_hz = min_freq;
1570 req->target_freq_hz = target_freq;
1571 req->max_freq_hz = max_freq;
1572
1573 ret = ti_sci_do_xfer(info, xfer);
1574 if (ret) {
1575 dev_err(dev, "Mbox send fail %d\n", ret);
1576 goto fail;
1577 }
1578
1579 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1580
1581 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1582
1583fail:
1584 ti_sci_put_one_xfer(&info->minfo, xfer);
1585
1586 return ret;
1587}
1588
1589/**
1590 * ti_sci_cmd_clk_get_freq() - Get current frequency
1591 * @handle: pointer to TI SCI handle
1592 * @dev_id: Device identifier this request is for
1593 * @clk_id: Clock identifier for the device for this request.
1594 * Each device has it's own set of clock inputs. This indexes
1595 * which clock input to modify.
1596 * @freq: Currently frequency in Hz
1597 *
1598 * Return: 0 if all went well, else returns appropriate error value.
1599 */
1600static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1601 u32 dev_id, u32 clk_id, u64 *freq)
1602{
1603 struct ti_sci_info *info;
1604 struct ti_sci_msg_req_get_clock_freq *req;
1605 struct ti_sci_msg_resp_get_clock_freq *resp;
1606 struct ti_sci_xfer *xfer;
1607 struct device *dev;
1608 int ret = 0;
1609
1610 if (IS_ERR(handle))
1611 return PTR_ERR(handle);
1612 if (!handle || !freq)
1613 return -EINVAL;
1614
1615 info = handle_to_ti_sci_info(handle);
1616 dev = info->dev;
1617
1618 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1619 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1620 sizeof(*req), sizeof(*resp));
1621 if (IS_ERR(xfer)) {
1622 ret = PTR_ERR(xfer);
1623 dev_err(dev, "Message alloc failed(%d)\n", ret);
1624 return ret;
1625 }
1626 req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1627 req->dev_id = dev_id;
1628 if (clk_id < 255) {
1629 req->clk_id = clk_id;
1630 } else {
1631 req->clk_id = 255;
1632 req->clk_id_32 = clk_id;
1633 }
1634
1635 ret = ti_sci_do_xfer(info, xfer);
1636 if (ret) {
1637 dev_err(dev, "Mbox send fail %d\n", ret);
1638 goto fail;
1639 }
1640
1641 resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1642
1643 if (!ti_sci_is_response_ack(resp))
1644 ret = -ENODEV;
1645 else
1646 *freq = resp->freq_hz;
1647
1648fail:
1649 ti_sci_put_one_xfer(&info->minfo, xfer);
1650
1651 return ret;
1652}
1653
1654static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1655{
1656 struct ti_sci_info *info;
1657 struct ti_sci_msg_req_reboot *req;
1658 struct ti_sci_msg_hdr *resp;
1659 struct ti_sci_xfer *xfer;
1660 struct device *dev;
1661 int ret = 0;
1662
1663 if (IS_ERR(handle))
1664 return PTR_ERR(handle);
1665 if (!handle)
1666 return -EINVAL;
1667
1668 info = handle_to_ti_sci_info(handle);
1669 dev = info->dev;
1670
1671 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1672 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1673 sizeof(*req), sizeof(*resp));
1674 if (IS_ERR(xfer)) {
1675 ret = PTR_ERR(xfer);
1676 dev_err(dev, "Message alloc failed(%d)\n", ret);
1677 return ret;
1678 }
1679 req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1680
1681 ret = ti_sci_do_xfer(info, xfer);
1682 if (ret) {
1683 dev_err(dev, "Mbox send fail %d\n", ret);
1684 goto fail;
1685 }
1686
1687 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1688
1689 if (!ti_sci_is_response_ack(resp))
1690 ret = -ENODEV;
1691 else
1692 ret = 0;
1693
1694fail:
1695 ti_sci_put_one_xfer(&info->minfo, xfer);
1696
1697 return ret;
1698}
1699
1700static int ti_sci_get_resource_type(struct ti_sci_info *info, u16 dev_id,
1701 u16 *type)
1702{
1703 struct ti_sci_rm_type_map *rm_type_map = info->desc->rm_type_map;
1704 bool found = false;
1705 int i;
1706
1707 /* If map is not provided then assume dev_id is used as type */
1708 if (!rm_type_map) {
1709 *type = dev_id;
1710 return 0;
1711 }
1712
1713 for (i = 0; rm_type_map[i].dev_id; i++) {
1714 if (rm_type_map[i].dev_id == dev_id) {
1715 *type = rm_type_map[i].type;
1716 found = true;
1717 break;
1718 }
1719 }
1720
1721 if (!found)
1722 return -EINVAL;
1723
1724 return 0;
1725}
1726
1727/**
1728 * ti_sci_get_resource_range - Helper to get a range of resources assigned
1729 * to a host. Resource is uniquely identified by
1730 * type and subtype.
1731 * @handle: Pointer to TISCI handle.
1732 * @dev_id: TISCI device ID.
1733 * @subtype: Resource assignment subtype that is being requested
1734 * from the given device.
1735 * @s_host: Host processor ID to which the resources are allocated
1736 * @range_start: Start index of the resource range
1737 * @range_num: Number of resources in the range
1738 *
1739 * Return: 0 if all went fine, else return appropriate error.
1740 */
1741static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
1742 u32 dev_id, u8 subtype, u8 s_host,
1743 u16 *range_start, u16 *range_num)
1744{
1745 struct ti_sci_msg_resp_get_resource_range *resp;
1746 struct ti_sci_msg_req_get_resource_range *req;
1747 struct ti_sci_xfer *xfer;
1748 struct ti_sci_info *info;
1749 struct device *dev;
1750 u16 type;
1751 int ret = 0;
1752
1753 if (IS_ERR(handle))
1754 return PTR_ERR(handle);
1755 if (!handle)
1756 return -EINVAL;
1757
1758 info = handle_to_ti_sci_info(handle);
1759 dev = info->dev;
1760
1761 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
1762 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1763 sizeof(*req), sizeof(*resp));
1764 if (IS_ERR(xfer)) {
1765 ret = PTR_ERR(xfer);
1766 dev_err(dev, "Message alloc failed(%d)\n", ret);
1767 return ret;
1768 }
1769
1770 ret = ti_sci_get_resource_type(info, dev_id, &type);
1771 if (ret) {
1772 dev_err(dev, "rm type lookup failed for %u\n", dev_id);
1773 goto fail;
1774 }
1775
1776 req = (struct ti_sci_msg_req_get_resource_range *)xfer->xfer_buf;
1777 req->secondary_host = s_host;
1778 req->type = type & MSG_RM_RESOURCE_TYPE_MASK;
1779 req->subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
1780
1781 ret = ti_sci_do_xfer(info, xfer);
1782 if (ret) {
1783 dev_err(dev, "Mbox send fail %d\n", ret);
1784 goto fail;
1785 }
1786
1787 resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->xfer_buf;
1788
1789 if (!ti_sci_is_response_ack(resp)) {
1790 ret = -ENODEV;
1791 } else if (!resp->range_start && !resp->range_num) {
1792 ret = -ENODEV;
1793 } else {
1794 *range_start = resp->range_start;
1795 *range_num = resp->range_num;
1796 };
1797
1798fail:
1799 ti_sci_put_one_xfer(&info->minfo, xfer);
1800
1801 return ret;
1802}
1803
1804/**
1805 * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
1806 * that is same as ti sci interface host.
1807 * @handle: Pointer to TISCI handle.
1808 * @dev_id: TISCI device ID.
1809 * @subtype: Resource assignment subtype that is being requested
1810 * from the given device.
1811 * @range_start: Start index of the resource range
1812 * @range_num: Number of resources in the range
1813 *
1814 * Return: 0 if all went fine, else return appropriate error.
1815 */
1816static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
1817 u32 dev_id, u8 subtype,
1818 u16 *range_start, u16 *range_num)
1819{
1820 return ti_sci_get_resource_range(handle, dev_id, subtype,
1821 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
1822 range_start, range_num);
1823}
1824
1825/**
1826 * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
1827 * assigned to a specified host.
1828 * @handle: Pointer to TISCI handle.
1829 * @dev_id: TISCI device ID.
1830 * @subtype: Resource assignment subtype that is being requested
1831 * from the given device.
1832 * @s_host: Host processor ID to which the resources are allocated
1833 * @range_start: Start index of the resource range
1834 * @range_num: Number of resources in the range
1835 *
1836 * Return: 0 if all went fine, else return appropriate error.
1837 */
1838static
1839int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
1840 u32 dev_id, u8 subtype, u8 s_host,
1841 u16 *range_start, u16 *range_num)
1842{
1843 return ti_sci_get_resource_range(handle, dev_id, subtype, s_host,
1844 range_start, range_num);
1845}
1846
1847/**
1848 * ti_sci_manage_irq() - Helper api to configure/release the irq route between
1849 * the requested source and destination
1850 * @handle: Pointer to TISCI handle.
1851 * @valid_params: Bit fields defining the validity of certain params
1852 * @src_id: Device ID of the IRQ source
1853 * @src_index: IRQ source index within the source device
1854 * @dst_id: Device ID of the IRQ destination
1855 * @dst_host_irq: IRQ number of the destination device
1856 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1857 * @vint: Virtual interrupt to be used within the IA
1858 * @global_event: Global event number to be used for the requesting event
1859 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1860 * @s_host: Secondary host ID to which the irq/event is being
1861 * requested for.
1862 * @type: Request type irq set or release.
1863 *
1864 * Return: 0 if all went fine, else return appropriate error.
1865 */
1866static int ti_sci_manage_irq(const struct ti_sci_handle *handle,
1867 u32 valid_params, u16 src_id, u16 src_index,
1868 u16 dst_id, u16 dst_host_irq, u16 ia_id, u16 vint,
1869 u16 global_event, u8 vint_status_bit, u8 s_host,
1870 u16 type)
1871{
1872 struct ti_sci_msg_req_manage_irq *req;
1873 struct ti_sci_msg_hdr *resp;
1874 struct ti_sci_xfer *xfer;
1875 struct ti_sci_info *info;
1876 struct device *dev;
1877 int ret = 0;
1878
1879 if (IS_ERR(handle))
1880 return PTR_ERR(handle);
1881 if (!handle)
1882 return -EINVAL;
1883
1884 info = handle_to_ti_sci_info(handle);
1885 dev = info->dev;
1886
1887 xfer = ti_sci_get_one_xfer(info, type, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1888 sizeof(*req), sizeof(*resp));
1889 if (IS_ERR(xfer)) {
1890 ret = PTR_ERR(xfer);
1891 dev_err(dev, "Message alloc failed(%d)\n", ret);
1892 return ret;
1893 }
1894 req = (struct ti_sci_msg_req_manage_irq *)xfer->xfer_buf;
1895 req->valid_params = valid_params;
1896 req->src_id = src_id;
1897 req->src_index = src_index;
1898 req->dst_id = dst_id;
1899 req->dst_host_irq = dst_host_irq;
1900 req->ia_id = ia_id;
1901 req->vint = vint;
1902 req->global_event = global_event;
1903 req->vint_status_bit = vint_status_bit;
1904 req->secondary_host = s_host;
1905
1906 ret = ti_sci_do_xfer(info, xfer);
1907 if (ret) {
1908 dev_err(dev, "Mbox send fail %d\n", ret);
1909 goto fail;
1910 }
1911
1912 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1913
1914 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1915
1916fail:
1917 ti_sci_put_one_xfer(&info->minfo, xfer);
1918
1919 return ret;
1920}
1921
1922/**
1923 * ti_sci_set_irq() - Helper api to configure the irq route between the
1924 * requested source and destination
1925 * @handle: Pointer to TISCI handle.
1926 * @valid_params: Bit fields defining the validity of certain params
1927 * @src_id: Device ID of the IRQ source
1928 * @src_index: IRQ source index within the source device
1929 * @dst_id: Device ID of the IRQ destination
1930 * @dst_host_irq: IRQ number of the destination device
1931 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1932 * @vint: Virtual interrupt to be used within the IA
1933 * @global_event: Global event number to be used for the requesting event
1934 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1935 * @s_host: Secondary host ID to which the irq/event is being
1936 * requested for.
1937 *
1938 * Return: 0 if all went fine, else return appropriate error.
1939 */
1940static int ti_sci_set_irq(const struct ti_sci_handle *handle, u32 valid_params,
1941 u16 src_id, u16 src_index, u16 dst_id,
1942 u16 dst_host_irq, u16 ia_id, u16 vint,
1943 u16 global_event, u8 vint_status_bit, u8 s_host)
1944{
1945 pr_debug("%s: IRQ set with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1946 __func__, valid_params, src_id, src_index,
1947 dst_id, dst_host_irq, ia_id, vint, global_event,
1948 vint_status_bit);
1949
1950 return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1951 dst_id, dst_host_irq, ia_id, vint,
1952 global_event, vint_status_bit, s_host,
1953 TI_SCI_MSG_SET_IRQ);
1954}
1955
1956/**
1957 * ti_sci_free_irq() - Helper api to free the irq route between the
1958 * requested source and destination
1959 * @handle: Pointer to TISCI handle.
1960 * @valid_params: Bit fields defining the validity of certain params
1961 * @src_id: Device ID of the IRQ source
1962 * @src_index: IRQ source index within the source device
1963 * @dst_id: Device ID of the IRQ destination
1964 * @dst_host_irq: IRQ number of the destination device
1965 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1966 * @vint: Virtual interrupt to be used within the IA
1967 * @global_event: Global event number to be used for the requesting event
1968 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1969 * @s_host: Secondary host ID to which the irq/event is being
1970 * requested for.
1971 *
1972 * Return: 0 if all went fine, else return appropriate error.
1973 */
1974static int ti_sci_free_irq(const struct ti_sci_handle *handle, u32 valid_params,
1975 u16 src_id, u16 src_index, u16 dst_id,
1976 u16 dst_host_irq, u16 ia_id, u16 vint,
1977 u16 global_event, u8 vint_status_bit, u8 s_host)
1978{
1979 pr_debug("%s: IRQ release with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1980 __func__, valid_params, src_id, src_index,
1981 dst_id, dst_host_irq, ia_id, vint, global_event,
1982 vint_status_bit);
1983
1984 return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1985 dst_id, dst_host_irq, ia_id, vint,
1986 global_event, vint_status_bit, s_host,
1987 TI_SCI_MSG_FREE_IRQ);
1988}
1989
1990/**
1991 * ti_sci_cmd_set_irq() - Configure a host irq route between the requested
1992 * source and destination.
1993 * @handle: Pointer to TISCI handle.
1994 * @src_id: Device ID of the IRQ source
1995 * @src_index: IRQ source index within the source device
1996 * @dst_id: Device ID of the IRQ destination
1997 * @dst_host_irq: IRQ number of the destination device
1998 * @vint_irq: Boolean specifying if this interrupt belongs to
1999 * Interrupt Aggregator.
2000 *
2001 * Return: 0 if all went fine, else return appropriate error.
2002 */
2003static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
2004 u16 src_index, u16 dst_id, u16 dst_host_irq)
2005{
2006 u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2007
2008 return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
2009 dst_host_irq, 0, 0, 0, 0, 0);
2010}
2011
2012/**
2013 * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
2014 * requested source and Interrupt Aggregator.
2015 * @handle: Pointer to TISCI handle.
2016 * @src_id: Device ID of the IRQ source
2017 * @src_index: IRQ source index within the source device
2018 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
2019 * @vint: Virtual interrupt to be used within the IA
2020 * @global_event: Global event number to be used for the requesting event
2021 * @vint_status_bit: Virtual interrupt status bit to be used for the event
2022 *
2023 * Return: 0 if all went fine, else return appropriate error.
2024 */
2025static int ti_sci_cmd_set_event_map(const struct ti_sci_handle *handle,
2026 u16 src_id, u16 src_index, u16 ia_id,
2027 u16 vint, u16 global_event,
2028 u8 vint_status_bit)
2029{
2030 u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
2031 MSG_FLAG_GLB_EVNT_VALID |
2032 MSG_FLAG_VINT_STS_BIT_VALID;
2033
2034 return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
2035 ia_id, vint, global_event, vint_status_bit, 0);
2036}
2037
2038/**
2039 * ti_sci_cmd_free_irq() - Free a host irq route between the between the
2040 * requested source and destination.
2041 * @handle: Pointer to TISCI handle.
2042 * @src_id: Device ID of the IRQ source
2043 * @src_index: IRQ source index within the source device
2044 * @dst_id: Device ID of the IRQ destination
2045 * @dst_host_irq: IRQ number of the destination device
2046 * @vint_irq: Boolean specifying if this interrupt belongs to
2047 * Interrupt Aggregator.
2048 *
2049 * Return: 0 if all went fine, else return appropriate error.
2050 */
2051static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
2052 u16 src_index, u16 dst_id, u16 dst_host_irq)
2053{
2054 u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2055
2056 return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
2057 dst_host_irq, 0, 0, 0, 0, 0);
2058}
2059
2060/**
2061 * ti_sci_cmd_free_event_map() - Free an event map between the requested source
2062 * and Interrupt Aggregator.
2063 * @handle: Pointer to TISCI handle.
2064 * @src_id: Device ID of the IRQ source
2065 * @src_index: IRQ source index within the source device
2066 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
2067 * @vint: Virtual interrupt to be used within the IA
2068 * @global_event: Global event number to be used for the requesting event
2069 * @vint_status_bit: Virtual interrupt status bit to be used for the event
2070 *
2071 * Return: 0 if all went fine, else return appropriate error.
2072 */
2073static int ti_sci_cmd_free_event_map(const struct ti_sci_handle *handle,
2074 u16 src_id, u16 src_index, u16 ia_id,
2075 u16 vint, u16 global_event,
2076 u8 vint_status_bit)
2077{
2078 u32 valid_params = MSG_FLAG_IA_ID_VALID |
2079 MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
2080 MSG_FLAG_VINT_STS_BIT_VALID;
2081
2082 return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
2083 ia_id, vint, global_event, vint_status_bit, 0);
2084}
2085
2086/**
2087 * ti_sci_cmd_ring_config() - configure RA ring
2088 * @handle: Pointer to TI SCI handle.
2089 * @valid_params: Bitfield defining validity of ring configuration
2090 * parameters
2091 * @nav_id: Device ID of Navigator Subsystem from which the ring is
2092 * allocated
2093 * @index: Ring index
2094 * @addr_lo: The ring base address lo 32 bits
2095 * @addr_hi: The ring base address hi 32 bits
2096 * @count: Number of ring elements
2097 * @mode: The mode of the ring
2098 * @size: The ring element size.
2099 * @order_id: Specifies the ring's bus order ID
2100 *
2101 * Return: 0 if all went well, else returns appropriate error value.
2102 *
2103 * See @ti_sci_msg_rm_ring_cfg_req for more info.
2104 */
2105static int ti_sci_cmd_ring_config(const struct ti_sci_handle *handle,
2106 u32 valid_params, u16 nav_id, u16 index,
2107 u32 addr_lo, u32 addr_hi, u32 count,
2108 u8 mode, u8 size, u8 order_id)
2109{
2110 struct ti_sci_msg_rm_ring_cfg_req *req;
2111 struct ti_sci_msg_hdr *resp;
2112 struct ti_sci_xfer *xfer;
2113 struct ti_sci_info *info;
2114 struct device *dev;
2115 int ret = 0;
2116
2117 if (IS_ERR_OR_NULL(handle))
2118 return -EINVAL;
2119
2120 info = handle_to_ti_sci_info(handle);
2121 dev = info->dev;
2122
2123 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
2124 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2125 sizeof(*req), sizeof(*resp));
2126 if (IS_ERR(xfer)) {
2127 ret = PTR_ERR(xfer);
2128 dev_err(dev, "RM_RA:Message config failed(%d)\n", ret);
2129 return ret;
2130 }
2131 req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
2132 req->valid_params = valid_params;
2133 req->nav_id = nav_id;
2134 req->index = index;
2135 req->addr_lo = addr_lo;
2136 req->addr_hi = addr_hi;
2137 req->count = count;
2138 req->mode = mode;
2139 req->size = size;
2140 req->order_id = order_id;
2141
2142 ret = ti_sci_do_xfer(info, xfer);
2143 if (ret) {
2144 dev_err(dev, "RM_RA:Mbox config send fail %d\n", ret);
2145 goto fail;
2146 }
2147
2148 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2149 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2150
2151fail:
2152 ti_sci_put_one_xfer(&info->minfo, xfer);
2153 dev_dbg(dev, "RM_RA:config ring %u ret:%d\n", index, ret);
2154 return ret;
2155}
2156
2157/**
2158 * ti_sci_cmd_ring_get_config() - get RA ring configuration
2159 * @handle: Pointer to TI SCI handle.
2160 * @nav_id: Device ID of Navigator Subsystem from which the ring is
2161 * allocated
2162 * @index: Ring index
2163 * @addr_lo: Returns ring's base address lo 32 bits
2164 * @addr_hi: Returns ring's base address hi 32 bits
2165 * @count: Returns number of ring elements
2166 * @mode: Returns mode of the ring
2167 * @size: Returns ring element size
2168 * @order_id: Returns ring's bus order ID
2169 *
2170 * Return: 0 if all went well, else returns appropriate error value.
2171 *
2172 * See @ti_sci_msg_rm_ring_get_cfg_req for more info.
2173 */
2174static int ti_sci_cmd_ring_get_config(const struct ti_sci_handle *handle,
2175 u32 nav_id, u32 index, u8 *mode,
2176 u32 *addr_lo, u32 *addr_hi,
2177 u32 *count, u8 *size, u8 *order_id)
2178{
2179 struct ti_sci_msg_rm_ring_get_cfg_resp *resp;
2180 struct ti_sci_msg_rm_ring_get_cfg_req *req;
2181 struct ti_sci_xfer *xfer;
2182 struct ti_sci_info *info;
2183 struct device *dev;
2184 int ret = 0;
2185
2186 if (IS_ERR_OR_NULL(handle))
2187 return -EINVAL;
2188
2189 info = handle_to_ti_sci_info(handle);
2190 dev = info->dev;
2191
2192 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_GET_CFG,
2193 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2194 sizeof(*req), sizeof(*resp));
2195 if (IS_ERR(xfer)) {
2196 ret = PTR_ERR(xfer);
2197 dev_err(dev,
2198 "RM_RA:Message get config failed(%d)\n", ret);
2199 return ret;
2200 }
2201 req = (struct ti_sci_msg_rm_ring_get_cfg_req *)xfer->xfer_buf;
2202 req->nav_id = nav_id;
2203 req->index = index;
2204
2205 ret = ti_sci_do_xfer(info, xfer);
2206 if (ret) {
2207 dev_err(dev, "RM_RA:Mbox get config send fail %d\n", ret);
2208 goto fail;
2209 }
2210
2211 resp = (struct ti_sci_msg_rm_ring_get_cfg_resp *)xfer->xfer_buf;
2212
2213 if (!ti_sci_is_response_ack(resp)) {
2214 ret = -ENODEV;
2215 } else {
2216 if (mode)
2217 *mode = resp->mode;
2218 if (addr_lo)
2219 *addr_lo = resp->addr_lo;
2220 if (addr_hi)
2221 *addr_hi = resp->addr_hi;
2222 if (count)
2223 *count = resp->count;
2224 if (size)
2225 *size = resp->size;
2226 if (order_id)
2227 *order_id = resp->order_id;
2228 };
2229
2230fail:
2231 ti_sci_put_one_xfer(&info->minfo, xfer);
2232 dev_dbg(dev, "RM_RA:get config ring %u ret:%d\n", index, ret);
2233 return ret;
2234}
2235
2236/**
2237 * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
2238 * @handle: Pointer to TI SCI handle.
2239 * @nav_id: Device ID of Navigator Subsystem which should be used for
2240 * pairing
2241 * @src_thread: Source PSI-L thread ID
2242 * @dst_thread: Destination PSI-L thread ID
2243 *
2244 * Return: 0 if all went well, else returns appropriate error value.
2245 */
2246static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
2247 u32 nav_id, u32 src_thread, u32 dst_thread)
2248{
2249 struct ti_sci_msg_psil_pair *req;
2250 struct ti_sci_msg_hdr *resp;
2251 struct ti_sci_xfer *xfer;
2252 struct ti_sci_info *info;
2253 struct device *dev;
2254 int ret = 0;
2255
2256 if (IS_ERR(handle))
2257 return PTR_ERR(handle);
2258 if (!handle)
2259 return -EINVAL;
2260
2261 info = handle_to_ti_sci_info(handle);
2262 dev = info->dev;
2263
2264 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
2265 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2266 sizeof(*req), sizeof(*resp));
2267 if (IS_ERR(xfer)) {
2268 ret = PTR_ERR(xfer);
2269 dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2270 return ret;
2271 }
2272 req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
2273 req->nav_id = nav_id;
2274 req->src_thread = src_thread;
2275 req->dst_thread = dst_thread;
2276
2277 ret = ti_sci_do_xfer(info, xfer);
2278 if (ret) {
2279 dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2280 goto fail;
2281 }
2282
2283 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2284 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2285
2286fail:
2287 ti_sci_put_one_xfer(&info->minfo, xfer);
2288
2289 return ret;
2290}
2291
2292/**
2293 * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
2294 * @handle: Pointer to TI SCI handle.
2295 * @nav_id: Device ID of Navigator Subsystem which should be used for
2296 * unpairing
2297 * @src_thread: Source PSI-L thread ID
2298 * @dst_thread: Destination PSI-L thread ID
2299 *
2300 * Return: 0 if all went well, else returns appropriate error value.
2301 */
2302static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
2303 u32 nav_id, u32 src_thread, u32 dst_thread)
2304{
2305 struct ti_sci_msg_psil_unpair *req;
2306 struct ti_sci_msg_hdr *resp;
2307 struct ti_sci_xfer *xfer;
2308 struct ti_sci_info *info;
2309 struct device *dev;
2310 int ret = 0;
2311
2312 if (IS_ERR(handle))
2313 return PTR_ERR(handle);
2314 if (!handle)
2315 return -EINVAL;
2316
2317 info = handle_to_ti_sci_info(handle);
2318 dev = info->dev;
2319
2320 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
2321 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2322 sizeof(*req), sizeof(*resp));
2323 if (IS_ERR(xfer)) {
2324 ret = PTR_ERR(xfer);
2325 dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2326 return ret;
2327 }
2328 req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
2329 req->nav_id = nav_id;
2330 req->src_thread = src_thread;
2331 req->dst_thread = dst_thread;
2332
2333 ret = ti_sci_do_xfer(info, xfer);
2334 if (ret) {
2335 dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2336 goto fail;
2337 }
2338
2339 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2340 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2341
2342fail:
2343 ti_sci_put_one_xfer(&info->minfo, xfer);
2344
2345 return ret;
2346}
2347
2348/**
2349 * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
2350 * @handle: Pointer to TI SCI handle.
2351 * @params: Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
2352 * structure
2353 *
2354 * Return: 0 if all went well, else returns appropriate error value.
2355 *
2356 * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
2357 * more info.
2358 */
2359static int ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle *handle,
2360 const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
2361{
2362 struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
2363 struct ti_sci_msg_hdr *resp;
2364 struct ti_sci_xfer *xfer;
2365 struct ti_sci_info *info;
2366 struct device *dev;
2367 int ret = 0;
2368
2369 if (IS_ERR_OR_NULL(handle))
2370 return -EINVAL;
2371
2372 info = handle_to_ti_sci_info(handle);
2373 dev = info->dev;
2374
2375 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
2376 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2377 sizeof(*req), sizeof(*resp));
2378 if (IS_ERR(xfer)) {
2379 ret = PTR_ERR(xfer);
2380 dev_err(dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
2381 return ret;
2382 }
2383 req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
2384 req->valid_params = params->valid_params;
2385 req->nav_id = params->nav_id;
2386 req->index = params->index;
2387 req->tx_pause_on_err = params->tx_pause_on_err;
2388 req->tx_filt_einfo = params->tx_filt_einfo;
2389 req->tx_filt_pswords = params->tx_filt_pswords;
2390 req->tx_atype = params->tx_atype;
2391 req->tx_chan_type = params->tx_chan_type;
2392 req->tx_supr_tdpkt = params->tx_supr_tdpkt;
2393 req->tx_fetch_size = params->tx_fetch_size;
2394 req->tx_credit_count = params->tx_credit_count;
2395 req->txcq_qnum = params->txcq_qnum;
2396 req->tx_priority = params->tx_priority;
2397 req->tx_qos = params->tx_qos;
2398 req->tx_orderid = params->tx_orderid;
2399 req->fdepth = params->fdepth;
2400 req->tx_sched_priority = params->tx_sched_priority;
2401 req->tx_burst_size = params->tx_burst_size;
2402
2403 ret = ti_sci_do_xfer(info, xfer);
2404 if (ret) {
2405 dev_err(dev, "Mbox send TX_CH_CFG fail %d\n", ret);
2406 goto fail;
2407 }
2408
2409 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2410 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2411
2412fail:
2413 ti_sci_put_one_xfer(&info->minfo, xfer);
2414 dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
2415 return ret;
2416}
2417
2418/**
2419 * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
2420 * @handle: Pointer to TI SCI handle.
2421 * @params: Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
2422 * structure
2423 *
2424 * Return: 0 if all went well, else returns appropriate error value.
2425 *
2426 * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
2427 * more info.
2428 */
2429static int ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle *handle,
2430 const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
2431{
2432 struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
2433 struct ti_sci_msg_hdr *resp;
2434 struct ti_sci_xfer *xfer;
2435 struct ti_sci_info *info;
2436 struct device *dev;
2437 int ret = 0;
2438
2439 if (IS_ERR_OR_NULL(handle))
2440 return -EINVAL;
2441
2442 info = handle_to_ti_sci_info(handle);
2443 dev = info->dev;
2444
2445 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
2446 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2447 sizeof(*req), sizeof(*resp));
2448 if (IS_ERR(xfer)) {
2449 ret = PTR_ERR(xfer);
2450 dev_err(dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
2451 return ret;
2452 }
2453 req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
2454 req->valid_params = params->valid_params;
2455 req->nav_id = params->nav_id;
2456 req->index = params->index;
2457 req->rx_fetch_size = params->rx_fetch_size;
2458 req->rxcq_qnum = params->rxcq_qnum;
2459 req->rx_priority = params->rx_priority;
2460 req->rx_qos = params->rx_qos;
2461 req->rx_orderid = params->rx_orderid;
2462 req->rx_sched_priority = params->rx_sched_priority;
2463 req->flowid_start = params->flowid_start;
2464 req->flowid_cnt = params->flowid_cnt;
2465 req->rx_pause_on_err = params->rx_pause_on_err;
2466 req->rx_atype = params->rx_atype;
2467 req->rx_chan_type = params->rx_chan_type;
2468 req->rx_ignore_short = params->rx_ignore_short;
2469 req->rx_ignore_long = params->rx_ignore_long;
2470 req->rx_burst_size = params->rx_burst_size;
2471
2472 ret = ti_sci_do_xfer(info, xfer);
2473 if (ret) {
2474 dev_err(dev, "Mbox send RX_CH_CFG fail %d\n", ret);
2475 goto fail;
2476 }
2477
2478 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2479 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2480
2481fail:
2482 ti_sci_put_one_xfer(&info->minfo, xfer);
2483 dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
2484 return ret;
2485}
2486
2487/**
2488 * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
2489 * @handle: Pointer to TI SCI handle.
2490 * @params: Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
2491 * structure
2492 *
2493 * Return: 0 if all went well, else returns appropriate error value.
2494 *
2495 * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
2496 * more info.
2497 */
2498static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
2499 const struct ti_sci_msg_rm_udmap_flow_cfg *params)
2500{
2501 struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
2502 struct ti_sci_msg_hdr *resp;
2503 struct ti_sci_xfer *xfer;
2504 struct ti_sci_info *info;
2505 struct device *dev;
2506 int ret = 0;
2507
2508 if (IS_ERR_OR_NULL(handle))
2509 return -EINVAL;
2510
2511 info = handle_to_ti_sci_info(handle);
2512 dev = info->dev;
2513
2514 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
2515 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2516 sizeof(*req), sizeof(*resp));
2517 if (IS_ERR(xfer)) {
2518 ret = PTR_ERR(xfer);
2519 dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
2520 return ret;
2521 }
2522 req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
2523 req->valid_params = params->valid_params;
2524 req->nav_id = params->nav_id;
2525 req->flow_index = params->flow_index;
2526 req->rx_einfo_present = params->rx_einfo_present;
2527 req->rx_psinfo_present = params->rx_psinfo_present;
2528 req->rx_error_handling = params->rx_error_handling;
2529 req->rx_desc_type = params->rx_desc_type;
2530 req->rx_sop_offset = params->rx_sop_offset;
2531 req->rx_dest_qnum = params->rx_dest_qnum;
2532 req->rx_src_tag_hi = params->rx_src_tag_hi;
2533 req->rx_src_tag_lo = params->rx_src_tag_lo;
2534 req->rx_dest_tag_hi = params->rx_dest_tag_hi;
2535 req->rx_dest_tag_lo = params->rx_dest_tag_lo;
2536 req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
2537 req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
2538 req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
2539 req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
2540 req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
2541 req->rx_fdq1_qnum = params->rx_fdq1_qnum;
2542 req->rx_fdq2_qnum = params->rx_fdq2_qnum;
2543 req->rx_fdq3_qnum = params->rx_fdq3_qnum;
2544 req->rx_ps_location = params->rx_ps_location;
2545
2546 ret = ti_sci_do_xfer(info, xfer);
2547 if (ret) {
2548 dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
2549 goto fail;
2550 }
2551
2552 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2553 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2554
2555fail:
2556 ti_sci_put_one_xfer(&info->minfo, xfer);
2557 dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
2558 return ret;
2559}
2560
2561/**
2562 * ti_sci_cmd_proc_request() - Command to request a physical processor control
2563 * @handle: Pointer to TI SCI handle
2564 * @proc_id: Processor ID this request is for
2565 *
2566 * Return: 0 if all went well, else returns appropriate error value.
2567 */
2568static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
2569 u8 proc_id)
2570{
2571 struct ti_sci_msg_req_proc_request *req;
2572 struct ti_sci_msg_hdr *resp;
2573 struct ti_sci_info *info;
2574 struct ti_sci_xfer *xfer;
2575 struct device *dev;
2576 int ret = 0;
2577
2578 if (!handle)
2579 return -EINVAL;
2580 if (IS_ERR(handle))
2581 return PTR_ERR(handle);
2582
2583 info = handle_to_ti_sci_info(handle);
2584 dev = info->dev;
2585
2586 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
2587 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2588 sizeof(*req), sizeof(*resp));
2589 if (IS_ERR(xfer)) {
2590 ret = PTR_ERR(xfer);
2591 dev_err(dev, "Message alloc failed(%d)\n", ret);
2592 return ret;
2593 }
2594 req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
2595 req->processor_id = proc_id;
2596
2597 ret = ti_sci_do_xfer(info, xfer);
2598 if (ret) {
2599 dev_err(dev, "Mbox send fail %d\n", ret);
2600 goto fail;
2601 }
2602
2603 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2604
2605 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2606
2607fail:
2608 ti_sci_put_one_xfer(&info->minfo, xfer);
2609
2610 return ret;
2611}
2612
2613/**
2614 * ti_sci_cmd_proc_release() - Command to release a physical processor control
2615 * @handle: Pointer to TI SCI handle
2616 * @proc_id: Processor ID this request is for
2617 *
2618 * Return: 0 if all went well, else returns appropriate error value.
2619 */
2620static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
2621 u8 proc_id)
2622{
2623 struct ti_sci_msg_req_proc_release *req;
2624 struct ti_sci_msg_hdr *resp;
2625 struct ti_sci_info *info;
2626 struct ti_sci_xfer *xfer;
2627 struct device *dev;
2628 int ret = 0;
2629
2630 if (!handle)
2631 return -EINVAL;
2632 if (IS_ERR(handle))
2633 return PTR_ERR(handle);
2634
2635 info = handle_to_ti_sci_info(handle);
2636 dev = info->dev;
2637
2638 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
2639 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2640 sizeof(*req), sizeof(*resp));
2641 if (IS_ERR(xfer)) {
2642 ret = PTR_ERR(xfer);
2643 dev_err(dev, "Message alloc failed(%d)\n", ret);
2644 return ret;
2645 }
2646 req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
2647 req->processor_id = proc_id;
2648
2649 ret = ti_sci_do_xfer(info, xfer);
2650 if (ret) {
2651 dev_err(dev, "Mbox send fail %d\n", ret);
2652 goto fail;
2653 }
2654
2655 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2656
2657 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2658
2659fail:
2660 ti_sci_put_one_xfer(&info->minfo, xfer);
2661
2662 return ret;
2663}
2664
2665/**
2666 * ti_sci_cmd_proc_handover() - Command to handover a physical processor
2667 * control to a host in the processor's access
2668 * control list.
2669 * @handle: Pointer to TI SCI handle
2670 * @proc_id: Processor ID this request is for
2671 * @host_id: Host ID to get the control of the processor
2672 *
2673 * Return: 0 if all went well, else returns appropriate error value.
2674 */
2675static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
2676 u8 proc_id, u8 host_id)
2677{
2678 struct ti_sci_msg_req_proc_handover *req;
2679 struct ti_sci_msg_hdr *resp;
2680 struct ti_sci_info *info;
2681 struct ti_sci_xfer *xfer;
2682 struct device *dev;
2683 int ret = 0;
2684
2685 if (!handle)
2686 return -EINVAL;
2687 if (IS_ERR(handle))
2688 return PTR_ERR(handle);
2689
2690 info = handle_to_ti_sci_info(handle);
2691 dev = info->dev;
2692
2693 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
2694 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2695 sizeof(*req), sizeof(*resp));
2696 if (IS_ERR(xfer)) {
2697 ret = PTR_ERR(xfer);
2698 dev_err(dev, "Message alloc failed(%d)\n", ret);
2699 return ret;
2700 }
2701 req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
2702 req->processor_id = proc_id;
2703 req->host_id = host_id;
2704
2705 ret = ti_sci_do_xfer(info, xfer);
2706 if (ret) {
2707 dev_err(dev, "Mbox send fail %d\n", ret);
2708 goto fail;
2709 }
2710
2711 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2712
2713 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2714
2715fail:
2716 ti_sci_put_one_xfer(&info->minfo, xfer);
2717
2718 return ret;
2719}
2720
2721/**
2722 * ti_sci_cmd_proc_set_config() - Command to set the processor boot
2723 * configuration flags
2724 * @handle: Pointer to TI SCI handle
2725 * @proc_id: Processor ID this request is for
2726 * @config_flags_set: Configuration flags to be set
2727 * @config_flags_clear: Configuration flags to be cleared.
2728 *
2729 * Return: 0 if all went well, else returns appropriate error value.
2730 */
2731static int ti_sci_cmd_proc_set_config(const struct ti_sci_handle *handle,
2732 u8 proc_id, u64 bootvector,
2733 u32 config_flags_set,
2734 u32 config_flags_clear)
2735{
2736 struct ti_sci_msg_req_set_config *req;
2737 struct ti_sci_msg_hdr *resp;
2738 struct ti_sci_info *info;
2739 struct ti_sci_xfer *xfer;
2740 struct device *dev;
2741 int ret = 0;
2742
2743 if (!handle)
2744 return -EINVAL;
2745 if (IS_ERR(handle))
2746 return PTR_ERR(handle);
2747
2748 info = handle_to_ti_sci_info(handle);
2749 dev = info->dev;
2750
2751 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
2752 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2753 sizeof(*req), sizeof(*resp));
2754 if (IS_ERR(xfer)) {
2755 ret = PTR_ERR(xfer);
2756 dev_err(dev, "Message alloc failed(%d)\n", ret);
2757 return ret;
2758 }
2759 req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
2760 req->processor_id = proc_id;
2761 req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
2762 req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
2763 TI_SCI_ADDR_HIGH_SHIFT;
2764 req->config_flags_set = config_flags_set;
2765 req->config_flags_clear = config_flags_clear;
2766
2767 ret = ti_sci_do_xfer(info, xfer);
2768 if (ret) {
2769 dev_err(dev, "Mbox send fail %d\n", ret);
2770 goto fail;
2771 }
2772
2773 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2774
2775 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2776
2777fail:
2778 ti_sci_put_one_xfer(&info->minfo, xfer);
2779
2780 return ret;
2781}
2782
2783/**
2784 * ti_sci_cmd_proc_set_control() - Command to set the processor boot
2785 * control flags
2786 * @handle: Pointer to TI SCI handle
2787 * @proc_id: Processor ID this request is for
2788 * @control_flags_set: Control flags to be set
2789 * @control_flags_clear: Control flags to be cleared
2790 *
2791 * Return: 0 if all went well, else returns appropriate error value.
2792 */
2793static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
2794 u8 proc_id, u32 control_flags_set,
2795 u32 control_flags_clear)
2796{
2797 struct ti_sci_msg_req_set_ctrl *req;
2798 struct ti_sci_msg_hdr *resp;
2799 struct ti_sci_info *info;
2800 struct ti_sci_xfer *xfer;
2801 struct device *dev;
2802 int ret = 0;
2803
2804 if (!handle)
2805 return -EINVAL;
2806 if (IS_ERR(handle))
2807 return PTR_ERR(handle);
2808
2809 info = handle_to_ti_sci_info(handle);
2810 dev = info->dev;
2811
2812 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
2813 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2814 sizeof(*req), sizeof(*resp));
2815 if (IS_ERR(xfer)) {
2816 ret = PTR_ERR(xfer);
2817 dev_err(dev, "Message alloc failed(%d)\n", ret);
2818 return ret;
2819 }
2820 req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
2821 req->processor_id = proc_id;
2822 req->control_flags_set = control_flags_set;
2823 req->control_flags_clear = control_flags_clear;
2824
2825 ret = ti_sci_do_xfer(info, xfer);
2826 if (ret) {
2827 dev_err(dev, "Mbox send fail %d\n", ret);
2828 goto fail;
2829 }
2830
2831 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2832
2833 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2834
2835fail:
2836 ti_sci_put_one_xfer(&info->minfo, xfer);
2837
2838 return ret;
2839}
2840
2841/**
2842 * ti_sci_cmd_get_boot_status() - Command to get the processor boot status
2843 * @handle: Pointer to TI SCI handle
2844 * @proc_id: Processor ID this request is for
2845 *
2846 * Return: 0 if all went well, else returns appropriate error value.
2847 */
2848static int ti_sci_cmd_proc_get_status(const struct ti_sci_handle *handle,
2849 u8 proc_id, u64 *bv, u32 *cfg_flags,
2850 u32 *ctrl_flags, u32 *sts_flags)
2851{
2852 struct ti_sci_msg_resp_get_status *resp;
2853 struct ti_sci_msg_req_get_status *req;
2854 struct ti_sci_info *info;
2855 struct ti_sci_xfer *xfer;
2856 struct device *dev;
2857 int ret = 0;
2858
2859 if (!handle)
2860 return -EINVAL;
2861 if (IS_ERR(handle))
2862 return PTR_ERR(handle);
2863
2864 info = handle_to_ti_sci_info(handle);
2865 dev = info->dev;
2866
2867 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
2868 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2869 sizeof(*req), sizeof(*resp));
2870 if (IS_ERR(xfer)) {
2871 ret = PTR_ERR(xfer);
2872 dev_err(dev, "Message alloc failed(%d)\n", ret);
2873 return ret;
2874 }
2875 req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
2876 req->processor_id = proc_id;
2877
2878 ret = ti_sci_do_xfer(info, xfer);
2879 if (ret) {
2880 dev_err(dev, "Mbox send fail %d\n", ret);
2881 goto fail;
2882 }
2883
2884 resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
2885
2886 if (!ti_sci_is_response_ack(resp)) {
2887 ret = -ENODEV;
2888 } else {
2889 *bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
2890 (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
2891 TI_SCI_ADDR_HIGH_MASK);
2892 *cfg_flags = resp->config_flags;
2893 *ctrl_flags = resp->control_flags;
2894 *sts_flags = resp->status_flags;
2895 }
2896
2897fail:
2898 ti_sci_put_one_xfer(&info->minfo, xfer);
2899
2900 return ret;
2901}
2902
2903/*
2904 * ti_sci_setup_ops() - Setup the operations structures
2905 * @info: pointer to TISCI pointer
2906 */
2907static void ti_sci_setup_ops(struct ti_sci_info *info)
2908{
2909 struct ti_sci_ops *ops = &info->handle.ops;
2910 struct ti_sci_core_ops *core_ops = &ops->core_ops;
2911 struct ti_sci_dev_ops *dops = &ops->dev_ops;
2912 struct ti_sci_clk_ops *cops = &ops->clk_ops;
2913 struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
2914 struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
2915 struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
2916 struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
2917 struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
2918 struct ti_sci_proc_ops *pops = &ops->proc_ops;
2919
2920 core_ops->reboot_device = ti_sci_cmd_core_reboot;
2921
2922 dops->get_device = ti_sci_cmd_get_device;
2923 dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
2924 dops->idle_device = ti_sci_cmd_idle_device;
2925 dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
2926 dops->put_device = ti_sci_cmd_put_device;
2927
2928 dops->is_valid = ti_sci_cmd_dev_is_valid;
2929 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
2930 dops->is_idle = ti_sci_cmd_dev_is_idle;
2931 dops->is_stop = ti_sci_cmd_dev_is_stop;
2932 dops->is_on = ti_sci_cmd_dev_is_on;
2933 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
2934 dops->set_device_resets = ti_sci_cmd_set_device_resets;
2935 dops->get_device_resets = ti_sci_cmd_get_device_resets;
2936
2937 cops->get_clock = ti_sci_cmd_get_clock;
2938 cops->idle_clock = ti_sci_cmd_idle_clock;
2939 cops->put_clock = ti_sci_cmd_put_clock;
2940 cops->is_auto = ti_sci_cmd_clk_is_auto;
2941 cops->is_on = ti_sci_cmd_clk_is_on;
2942 cops->is_off = ti_sci_cmd_clk_is_off;
2943
2944 cops->set_parent = ti_sci_cmd_clk_set_parent;
2945 cops->get_parent = ti_sci_cmd_clk_get_parent;
2946 cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
2947
2948 cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
2949 cops->set_freq = ti_sci_cmd_clk_set_freq;
2950 cops->get_freq = ti_sci_cmd_clk_get_freq;
2951
2952 rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
2953 rm_core_ops->get_range_from_shost =
2954 ti_sci_cmd_get_resource_range_from_shost;
2955
2956 iops->set_irq = ti_sci_cmd_set_irq;
2957 iops->set_event_map = ti_sci_cmd_set_event_map;
2958 iops->free_irq = ti_sci_cmd_free_irq;
2959 iops->free_event_map = ti_sci_cmd_free_event_map;
2960
2961 rops->config = ti_sci_cmd_ring_config;
2962 rops->get_config = ti_sci_cmd_ring_get_config;
2963
2964 psilops->pair = ti_sci_cmd_rm_psil_pair;
2965 psilops->unpair = ti_sci_cmd_rm_psil_unpair;
2966
2967 udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
2968 udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
2969 udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
2970
2971 pops->request = ti_sci_cmd_proc_request;
2972 pops->release = ti_sci_cmd_proc_release;
2973 pops->handover = ti_sci_cmd_proc_handover;
2974 pops->set_config = ti_sci_cmd_proc_set_config;
2975 pops->set_control = ti_sci_cmd_proc_set_control;
2976 pops->get_status = ti_sci_cmd_proc_get_status;
2977}
2978
2979/**
2980 * ti_sci_get_handle() - Get the TI SCI handle for a device
2981 * @dev: Pointer to device for which we want SCI handle
2982 *
2983 * NOTE: The function does not track individual clients of the framework
2984 * and is expected to be maintained by caller of TI SCI protocol library.
2985 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2986 * Return: pointer to handle if successful, else:
2987 * -EPROBE_DEFER if the instance is not ready
2988 * -ENODEV if the required node handler is missing
2989 * -EINVAL if invalid conditions are encountered.
2990 */
2991const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
2992{
2993 struct device_node *ti_sci_np;
2994 struct list_head *p;
2995 struct ti_sci_handle *handle = NULL;
2996 struct ti_sci_info *info;
2997
2998 if (!dev) {
2999 pr_err("I need a device pointer\n");
3000 return ERR_PTR(-EINVAL);
3001 }
3002 ti_sci_np = of_get_parent(dev->of_node);
3003 if (!ti_sci_np) {
3004 dev_err(dev, "No OF information\n");
3005 return ERR_PTR(-EINVAL);
3006 }
3007
3008 mutex_lock(&ti_sci_list_mutex);
3009 list_for_each(p, &ti_sci_list) {
3010 info = list_entry(p, struct ti_sci_info, node);
3011 if (ti_sci_np == info->dev->of_node) {
3012 handle = &info->handle;
3013 info->users++;
3014 break;
3015 }
3016 }
3017 mutex_unlock(&ti_sci_list_mutex);
3018 of_node_put(ti_sci_np);
3019
3020 if (!handle)
3021 return ERR_PTR(-EPROBE_DEFER);
3022
3023 return handle;
3024}
3025EXPORT_SYMBOL_GPL(ti_sci_get_handle);
3026
3027/**
3028 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
3029 * @handle: Handle acquired by ti_sci_get_handle
3030 *
3031 * NOTE: The function does not track individual clients of the framework
3032 * and is expected to be maintained by caller of TI SCI protocol library.
3033 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
3034 *
3035 * Return: 0 is successfully released
3036 * if an error pointer was passed, it returns the error value back,
3037 * if null was passed, it returns -EINVAL;
3038 */
3039int ti_sci_put_handle(const struct ti_sci_handle *handle)
3040{
3041 struct ti_sci_info *info;
3042
3043 if (IS_ERR(handle))
3044 return PTR_ERR(handle);
3045 if (!handle)
3046 return -EINVAL;
3047
3048 info = handle_to_ti_sci_info(handle);
3049 mutex_lock(&ti_sci_list_mutex);
3050 if (!WARN_ON(!info->users))
3051 info->users--;
3052 mutex_unlock(&ti_sci_list_mutex);
3053
3054 return 0;
3055}
3056EXPORT_SYMBOL_GPL(ti_sci_put_handle);
3057
3058static void devm_ti_sci_release(struct device *dev, void *res)
3059{
3060 const struct ti_sci_handle **ptr = res;
3061 const struct ti_sci_handle *handle = *ptr;
3062 int ret;
3063
3064 ret = ti_sci_put_handle(handle);
3065 if (ret)
3066 dev_err(dev, "failed to put handle %d\n", ret);
3067}
3068
3069/**
3070 * devm_ti_sci_get_handle() - Managed get handle
3071 * @dev: device for which we want SCI handle for.
3072 *
3073 * NOTE: This releases the handle once the device resources are
3074 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3075 * The function does not track individual clients of the framework
3076 * and is expected to be maintained by caller of TI SCI protocol library.
3077 *
3078 * Return: 0 if all went fine, else corresponding error.
3079 */
3080const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
3081{
3082 const struct ti_sci_handle **ptr;
3083 const struct ti_sci_handle *handle;
3084
3085 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3086 if (!ptr)
3087 return ERR_PTR(-ENOMEM);
3088 handle = ti_sci_get_handle(dev);
3089
3090 if (!IS_ERR(handle)) {
3091 *ptr = handle;
3092 devres_add(dev, ptr);
3093 } else {
3094 devres_free(ptr);
3095 }
3096
3097 return handle;
3098}
3099EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
3100
3101/**
3102 * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
3103 * @np: device node
3104 * @property: property name containing phandle on TISCI node
3105 *
3106 * NOTE: The function does not track individual clients of the framework
3107 * and is expected to be maintained by caller of TI SCI protocol library.
3108 * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
3109 * Return: pointer to handle if successful, else:
3110 * -EPROBE_DEFER if the instance is not ready
3111 * -ENODEV if the required node handler is missing
3112 * -EINVAL if invalid conditions are encountered.
3113 */
3114const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
3115 const char *property)
3116{
3117 struct ti_sci_handle *handle = NULL;
3118 struct device_node *ti_sci_np;
3119 struct ti_sci_info *info;
3120 struct list_head *p;
3121
3122 if (!np) {
3123 pr_err("I need a device pointer\n");
3124 return ERR_PTR(-EINVAL);
3125 }
3126
3127 ti_sci_np = of_parse_phandle(np, property, 0);
3128 if (!ti_sci_np)
3129 return ERR_PTR(-ENODEV);
3130
3131 mutex_lock(&ti_sci_list_mutex);
3132 list_for_each(p, &ti_sci_list) {
3133 info = list_entry(p, struct ti_sci_info, node);
3134 if (ti_sci_np == info->dev->of_node) {
3135 handle = &info->handle;
3136 info->users++;
3137 break;
3138 }
3139 }
3140 mutex_unlock(&ti_sci_list_mutex);
3141 of_node_put(ti_sci_np);
3142
3143 if (!handle)
3144 return ERR_PTR(-EPROBE_DEFER);
3145
3146 return handle;
3147}
3148EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
3149
3150/**
3151 * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
3152 * @dev: Device pointer requesting TISCI handle
3153 * @property: property name containing phandle on TISCI node
3154 *
3155 * NOTE: This releases the handle once the device resources are
3156 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3157 * The function does not track individual clients of the framework
3158 * and is expected to be maintained by caller of TI SCI protocol library.
3159 *
3160 * Return: 0 if all went fine, else corresponding error.
3161 */
3162const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
3163 const char *property)
3164{
3165 const struct ti_sci_handle *handle;
3166 const struct ti_sci_handle **ptr;
3167
3168 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3169 if (!ptr)
3170 return ERR_PTR(-ENOMEM);
3171 handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
3172
3173 if (!IS_ERR(handle)) {
3174 *ptr = handle;
3175 devres_add(dev, ptr);
3176 } else {
3177 devres_free(ptr);
3178 }
3179
3180 return handle;
3181}
3182EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
3183
3184/**
3185 * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3186 * @res: Pointer to the TISCI resource
3187 *
3188 * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3189 */
3190u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3191{
3192 unsigned long flags;
3193 u16 set, free_bit;
3194
3195 raw_spin_lock_irqsave(&res->lock, flags);
3196 for (set = 0; set < res->sets; set++) {
3197 free_bit = find_first_zero_bit(res->desc[set].res_map,
3198 res->desc[set].num);
3199 if (free_bit != res->desc[set].num) {
3200 set_bit(free_bit, res->desc[set].res_map);
3201 raw_spin_unlock_irqrestore(&res->lock, flags);
3202 return res->desc[set].start + free_bit;
3203 }
3204 }
3205 raw_spin_unlock_irqrestore(&res->lock, flags);
3206
3207 return TI_SCI_RESOURCE_NULL;
3208}
3209EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
3210
3211/**
3212 * ti_sci_release_resource() - Release a resource from TISCI resource.
3213 * @res: Pointer to the TISCI resource
3214 * @id: Resource id to be released.
3215 */
3216void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3217{
3218 unsigned long flags;
3219 u16 set;
3220
3221 raw_spin_lock_irqsave(&res->lock, flags);
3222 for (set = 0; set < res->sets; set++) {
3223 if (res->desc[set].start <= id &&
3224 (res->desc[set].num + res->desc[set].start) > id)
3225 clear_bit(id - res->desc[set].start,
3226 res->desc[set].res_map);
3227 }
3228 raw_spin_unlock_irqrestore(&res->lock, flags);
3229}
3230EXPORT_SYMBOL_GPL(ti_sci_release_resource);
3231
3232/**
3233 * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
3234 * @res: Pointer to the TISCI resource
3235 *
3236 * Return: Total number of available resources.
3237 */
3238u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
3239{
3240 u32 set, count = 0;
3241
3242 for (set = 0; set < res->sets; set++)
3243 count += res->desc[set].num;
3244
3245 return count;
3246}
3247EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
3248
3249/**
3250 * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
3251 * @handle: TISCI handle
3252 * @dev: Device pointer to which the resource is assigned
3253 * @dev_id: TISCI device id to which the resource is assigned
3254 * @of_prop: property name by which the resource are represented
3255 *
3256 * Return: Pointer to ti_sci_resource if all went well else appropriate
3257 * error pointer.
3258 */
3259struct ti_sci_resource *
3260devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
3261 struct device *dev, u32 dev_id, char *of_prop)
3262{
3263 struct ti_sci_resource *res;
3264 bool valid_set = false;
3265 u32 resource_subtype;
3266 int i, ret;
3267
3268 res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3269 if (!res)
3270 return ERR_PTR(-ENOMEM);
3271
3272 ret = of_property_count_elems_of_size(dev_of_node(dev), of_prop,
3273 sizeof(u32));
3274 if (ret < 0) {
3275 dev_err(dev, "%s resource type ids not available\n", of_prop);
3276 return ERR_PTR(ret);
3277 }
3278 res->sets = ret;
3279
3280 res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3281 GFP_KERNEL);
3282 if (!res->desc)
3283 return ERR_PTR(-ENOMEM);
3284
3285 for (i = 0; i < res->sets; i++) {
3286 ret = of_property_read_u32_index(dev_of_node(dev), of_prop, i,
3287 &resource_subtype);
3288 if (ret)
3289 return ERR_PTR(-EINVAL);
3290
3291 ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
3292 resource_subtype,
3293 &res->desc[i].start,
3294 &res->desc[i].num);
3295 if (ret) {
3296 dev_dbg(dev, "dev = %d subtype %d not allocated for this host\n",
3297 dev_id, resource_subtype);
3298 res->desc[i].start = 0;
3299 res->desc[i].num = 0;
3300 continue;
3301 }
3302
3303 dev_dbg(dev, "dev = %d, subtype = %d, start = %d, num = %d\n",
3304 dev_id, resource_subtype, res->desc[i].start,
3305 res->desc[i].num);
3306
3307 valid_set = true;
3308 res->desc[i].res_map =
3309 devm_kzalloc(dev, BITS_TO_LONGS(res->desc[i].num) *
3310 sizeof(*res->desc[i].res_map), GFP_KERNEL);
3311 if (!res->desc[i].res_map)
3312 return ERR_PTR(-ENOMEM);
3313 }
3314 raw_spin_lock_init(&res->lock);
3315
3316 if (valid_set)
3317 return res;
3318
3319 return ERR_PTR(-EINVAL);
3320}
3321
3322static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
3323 void *cmd)
3324{
3325 struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
3326 const struct ti_sci_handle *handle = &info->handle;
3327
3328 ti_sci_cmd_core_reboot(handle);
3329
3330 /* call fail OR pass, we should not be here in the first place */
3331 return NOTIFY_BAD;
3332}
3333
3334/* Description for K2G */
3335static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
3336 .default_host_id = 2,
3337 /* Conservative duration */
3338 .max_rx_timeout_ms = 1000,
3339 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3340 .max_msgs = 20,
3341 .max_msg_size = 64,
3342 .rm_type_map = NULL,
3343};
3344
3345static struct ti_sci_rm_type_map ti_sci_am654_rm_type_map[] = {
3346 {.dev_id = 56, .type = 0x00b}, /* GIC_IRQ */
3347 {.dev_id = 179, .type = 0x000}, /* MAIN_NAV_UDMASS_IA0 */
3348 {.dev_id = 187, .type = 0x009}, /* MAIN_NAV_RA */
3349 {.dev_id = 188, .type = 0x006}, /* MAIN_NAV_UDMAP */
3350 {.dev_id = 194, .type = 0x007}, /* MCU_NAV_UDMAP */
3351 {.dev_id = 195, .type = 0x00a}, /* MCU_NAV_RA */
3352 {.dev_id = 0, .type = 0x000}, /* end of table */
3353};
3354
3355/* Description for AM654 */
3356static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
3357 .default_host_id = 12,
3358 /* Conservative duration */
3359 .max_rx_timeout_ms = 10000,
3360 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3361 .max_msgs = 20,
3362 .max_msg_size = 60,
3363 .rm_type_map = ti_sci_am654_rm_type_map,
3364};
3365
3366static const struct of_device_id ti_sci_of_match[] = {
3367 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
3368 {.compatible = "ti,am654-sci", .data = &ti_sci_pmmc_am654_desc},
3369 { /* Sentinel */ },
3370};
3371MODULE_DEVICE_TABLE(of, ti_sci_of_match);
3372
3373static int ti_sci_probe(struct platform_device *pdev)
3374{
3375 struct device *dev = &pdev->dev;
3376 const struct of_device_id *of_id;
3377 const struct ti_sci_desc *desc;
3378 struct ti_sci_xfer *xfer;
3379 struct ti_sci_info *info = NULL;
3380 struct ti_sci_xfers_info *minfo;
3381 struct mbox_client *cl;
3382 int ret = -EINVAL;
3383 int i;
3384 int reboot = 0;
3385 u32 h_id;
3386
3387 of_id = of_match_device(ti_sci_of_match, dev);
3388 if (!of_id) {
3389 dev_err(dev, "OF data missing\n");
3390 return -EINVAL;
3391 }
3392 desc = of_id->data;
3393
3394 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3395 if (!info)
3396 return -ENOMEM;
3397
3398 info->dev = dev;
3399 info->desc = desc;
3400 ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
3401 /* if the property is not present in DT, use a default from desc */
3402 if (ret < 0) {
3403 info->host_id = info->desc->default_host_id;
3404 } else {
3405 if (!h_id) {
3406 dev_warn(dev, "Host ID 0 is reserved for firmware\n");
3407 info->host_id = info->desc->default_host_id;
3408 } else {
3409 info->host_id = h_id;
3410 }
3411 }
3412
3413 reboot = of_property_read_bool(dev->of_node,
3414 "ti,system-reboot-controller");
3415 INIT_LIST_HEAD(&info->node);
3416 minfo = &info->minfo;
3417
3418 /*
3419 * Pre-allocate messages
3420 * NEVER allocate more than what we can indicate in hdr.seq
3421 * if we have data description bug, force a fix..
3422 */
3423 if (WARN_ON(desc->max_msgs >=
3424 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
3425 return -EINVAL;
3426
3427 minfo->xfer_block = devm_kcalloc(dev,
3428 desc->max_msgs,
3429 sizeof(*minfo->xfer_block),
3430 GFP_KERNEL);
3431 if (!minfo->xfer_block)
3432 return -ENOMEM;
3433
3434 minfo->xfer_alloc_table = devm_kcalloc(dev,
3435 BITS_TO_LONGS(desc->max_msgs),
3436 sizeof(unsigned long),
3437 GFP_KERNEL);
3438 if (!minfo->xfer_alloc_table)
3439 return -ENOMEM;
3440 bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
3441
3442 /* Pre-initialize the buffer pointer to pre-allocated buffers */
3443 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
3444 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
3445 GFP_KERNEL);
3446 if (!xfer->xfer_buf)
3447 return -ENOMEM;
3448
3449 xfer->tx_message.buf = xfer->xfer_buf;
3450 init_completion(&xfer->done);
3451 }
3452
3453 ret = ti_sci_debugfs_create(pdev, info);
3454 if (ret)
3455 dev_warn(dev, "Failed to create debug file\n");
3456
3457 platform_set_drvdata(pdev, info);
3458
3459 cl = &info->cl;
3460 cl->dev = dev;
3461 cl->tx_block = false;
3462 cl->rx_callback = ti_sci_rx_callback;
3463 cl->knows_txdone = true;
3464
3465 spin_lock_init(&minfo->xfer_lock);
3466 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
3467
3468 info->chan_rx = mbox_request_channel_byname(cl, "rx");
3469 if (IS_ERR(info->chan_rx)) {
3470 ret = PTR_ERR(info->chan_rx);
3471 goto out;
3472 }
3473
3474 info->chan_tx = mbox_request_channel_byname(cl, "tx");
3475 if (IS_ERR(info->chan_tx)) {
3476 ret = PTR_ERR(info->chan_tx);
3477 goto out;
3478 }
3479 ret = ti_sci_cmd_get_revision(info);
3480 if (ret) {
3481 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
3482 goto out;
3483 }
3484
3485 ti_sci_setup_ops(info);
3486
3487 if (reboot) {
3488 info->nb.notifier_call = tisci_reboot_handler;
3489 info->nb.priority = 128;
3490
3491 ret = register_restart_handler(&info->nb);
3492 if (ret) {
3493 dev_err(dev, "reboot registration fail(%d)\n", ret);
3494 return ret;
3495 }
3496 }
3497
3498 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
3499 info->handle.version.abi_major, info->handle.version.abi_minor,
3500 info->handle.version.firmware_revision,
3501 info->handle.version.firmware_description);
3502
3503 mutex_lock(&ti_sci_list_mutex);
3504 list_add_tail(&info->node, &ti_sci_list);
3505 mutex_unlock(&ti_sci_list_mutex);
3506
3507 return of_platform_populate(dev->of_node, NULL, NULL, dev);
3508out:
3509 if (!IS_ERR(info->chan_tx))
3510 mbox_free_channel(info->chan_tx);
3511 if (!IS_ERR(info->chan_rx))
3512 mbox_free_channel(info->chan_rx);
3513 debugfs_remove(info->d);
3514 return ret;
3515}
3516
3517static struct platform_driver ti_sci_driver = {
3518 .probe = ti_sci_probe,
3519 .driver = {
3520 .name = "ti-sci",
3521 .of_match_table = of_match_ptr(ti_sci_of_match),
3522 .suppress_bind_attrs = true,
3523 },
3524};
3525module_platform_driver(ti_sci_driver);
3526
3527MODULE_LICENSE("GPL v2");
3528MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
3529MODULE_AUTHOR("Nishanth Menon");
3530MODULE_ALIAS("platform:ti-sci");