Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved. |
| 4 | * Copyright (C) 2019-2020 Linaro Limited. |
| 5 | */ |
| 6 | |
Patrick Delaunay | 5dd84d4 | 2021-02-24 11:19:44 +0100 | [diff] [blame^] | 7 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 8 | |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <cpu_func.h> |
| 11 | #include <dm.h> |
Sean Anderson | 31786fe | 2020-10-04 21:39:44 -0400 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | #include <scmi_agent.h> |
| 15 | #include <asm/cache.h> |
| 16 | #include <asm/system.h> |
| 17 | #include <dm/ofnode.h> |
| 18 | #include <linux/compat.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/ioport.h> |
| 21 | |
| 22 | #include "smt.h" |
| 23 | |
| 24 | /** |
| 25 | * Get shared memory configuration defined by the referred DT phandle |
| 26 | * Return with a errno compliant value. |
| 27 | */ |
| 28 | int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt) |
| 29 | { |
| 30 | int ret; |
| 31 | struct ofnode_phandle_args args; |
| 32 | struct resource resource; |
| 33 | fdt32_t faddr; |
| 34 | phys_addr_t paddr; |
| 35 | |
| 36 | ret = dev_read_phandle_with_args(dev, "shmem", NULL, 0, 0, &args); |
| 37 | if (ret) |
| 38 | return ret; |
| 39 | |
| 40 | ret = ofnode_read_resource(args.node, 0, &resource); |
| 41 | if (ret) |
| 42 | return ret; |
| 43 | |
| 44 | faddr = cpu_to_fdt32(resource.start); |
| 45 | paddr = ofnode_translate_address(args.node, &faddr); |
| 46 | |
| 47 | smt->size = resource_size(&resource); |
| 48 | if (smt->size < sizeof(struct scmi_smt_header)) { |
| 49 | dev_err(dev, "Shared memory buffer too small\n"); |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | |
| 53 | smt->buf = devm_ioremap(dev, paddr, smt->size); |
| 54 | if (!smt->buf) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | #ifdef CONFIG_ARM |
| 58 | if (dcache_status()) |
| 59 | mmu_set_region_dcache_behaviour((uintptr_t)smt->buf, |
| 60 | smt->size, DCACHE_OFF); |
| 61 | #endif |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Write SCMI message @msg into a SMT shared buffer @smt. |
| 68 | * Return 0 on success and with a negative errno in case of error. |
| 69 | */ |
| 70 | int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt, |
| 71 | struct scmi_msg *msg) |
| 72 | { |
| 73 | struct scmi_smt_header *hdr = (void *)smt->buf; |
| 74 | |
| 75 | if ((!msg->in_msg && msg->in_msg_sz) || |
| 76 | (!msg->out_msg && msg->out_msg_sz)) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) { |
| 80 | dev_dbg(dev, "Channel busy\n"); |
| 81 | return -EBUSY; |
| 82 | } |
| 83 | |
| 84 | if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) || |
| 85 | smt->size < (sizeof(*hdr) + msg->out_msg_sz)) { |
| 86 | dev_dbg(dev, "Buffer too small\n"); |
| 87 | return -ETOOSMALL; |
| 88 | } |
| 89 | |
| 90 | /* Load message in shared memory */ |
| 91 | hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE; |
| 92 | hdr->length = msg->in_msg_sz + sizeof(hdr->msg_header); |
| 93 | hdr->msg_header = SMT_HEADER_TOKEN(0) | |
| 94 | SMT_HEADER_MESSAGE_TYPE(0) | |
| 95 | SMT_HEADER_PROTOCOL_ID(msg->protocol_id) | |
| 96 | SMT_HEADER_MESSAGE_ID(msg->message_id); |
| 97 | |
| 98 | memcpy_toio(hdr->msg_payload, msg->in_msg, msg->in_msg_sz); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Read SCMI message from a SMT shared buffer @smt and copy it into @msg. |
| 105 | * Return 0 on success and with a negative errno in case of error. |
| 106 | */ |
| 107 | int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt, |
| 108 | struct scmi_msg *msg) |
| 109 | { |
| 110 | struct scmi_smt_header *hdr = (void *)smt->buf; |
| 111 | |
| 112 | if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) { |
| 113 | dev_err(dev, "Channel unexpectedly busy\n"); |
| 114 | return -EBUSY; |
| 115 | } |
| 116 | |
| 117 | if (hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR) { |
| 118 | dev_err(dev, "Channel error reported, reset channel\n"); |
| 119 | return -ECOMM; |
| 120 | } |
| 121 | |
| 122 | if (hdr->length > msg->out_msg_sz + sizeof(hdr->msg_header)) { |
| 123 | dev_err(dev, "Buffer to small\n"); |
| 124 | return -ETOOSMALL; |
| 125 | } |
| 126 | |
| 127 | /* Get the data */ |
| 128 | msg->out_msg_sz = hdr->length - sizeof(hdr->msg_header); |
| 129 | memcpy_fromio(msg->out_msg, hdr->msg_payload, msg->out_msg_sz); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Clear SMT flags in shared buffer to allow further message exchange |
| 136 | */ |
| 137 | void scmi_clear_smt_channel(struct scmi_smt *smt) |
| 138 | { |
| 139 | struct scmi_smt_header *hdr = (void *)smt->buf; |
| 140 | |
| 141 | hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR; |
| 142 | } |