| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Amlogic Secure Monitor driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2016 Endless Mobile, Inc. |
|---|
| 5 | 6 | * Author: Carlo Caione <carlo@endlessm.com> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This program is free software; you can redistribute it and/or |
|---|
| 8 | | - * modify it under the terms of the GNU General Public License |
|---|
| 9 | | - * version 2 as published by the Free Software Foundation. |
|---|
| 10 | | - * |
|---|
| 11 | | - * You should have received a copy of the GNU General Public License |
|---|
| 12 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 13 | 7 | */ |
|---|
| 14 | 8 | |
|---|
| 15 | 9 | #define pr_fmt(fmt) "meson-sm: " fmt |
|---|
| .. | .. |
|---|
| 24 | 18 | #include <linux/printk.h> |
|---|
| 25 | 19 | #include <linux/types.h> |
|---|
| 26 | 20 | #include <linux/sizes.h> |
|---|
| 21 | + #include <linux/slab.h> |
|---|
| 27 | 22 | |
|---|
| 28 | 23 | #include <linux/firmware/meson/meson_sm.h> |
|---|
| 29 | 24 | |
|---|
| .. | .. |
|---|
| 40 | 35 | struct meson_sm_cmd cmd[]; |
|---|
| 41 | 36 | }; |
|---|
| 42 | 37 | |
|---|
| 43 | | -struct meson_sm_chip gxbb_chip = { |
|---|
| 38 | +static const struct meson_sm_chip gxbb_chip = { |
|---|
| 44 | 39 | .shmem_size = SZ_4K, |
|---|
| 45 | 40 | .cmd_shmem_in_base = 0x82000020, |
|---|
| 46 | 41 | .cmd_shmem_out_base = 0x82000021, |
|---|
| .. | .. |
|---|
| 48 | 43 | CMD(SM_EFUSE_READ, 0x82000030), |
|---|
| 49 | 44 | CMD(SM_EFUSE_WRITE, 0x82000031), |
|---|
| 50 | 45 | CMD(SM_EFUSE_USER_MAX, 0x82000033), |
|---|
| 46 | + CMD(SM_GET_CHIP_ID, 0x82000044), |
|---|
| 47 | + CMD(SM_A1_PWRC_SET, 0x82000093), |
|---|
| 48 | + CMD(SM_A1_PWRC_GET, 0x82000095), |
|---|
| 51 | 49 | { /* sentinel */ }, |
|---|
| 52 | 50 | }, |
|---|
| 53 | 51 | }; |
|---|
| .. | .. |
|---|
| 57 | 55 | void __iomem *sm_shmem_in_base; |
|---|
| 58 | 56 | void __iomem *sm_shmem_out_base; |
|---|
| 59 | 57 | }; |
|---|
| 60 | | - |
|---|
| 61 | | -static struct meson_sm_firmware fw; |
|---|
| 62 | 58 | |
|---|
| 63 | 59 | static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip, |
|---|
| 64 | 60 | unsigned int cmd_index) |
|---|
| .. | .. |
|---|
| 94 | 90 | /** |
|---|
| 95 | 91 | * meson_sm_call - generic SMC32 call to the secure-monitor |
|---|
| 96 | 92 | * |
|---|
| 93 | + * @fw: Pointer to secure-monitor firmware |
|---|
| 97 | 94 | * @cmd_index: Index of the SMC32 function ID |
|---|
| 98 | 95 | * @ret: Returned value |
|---|
| 99 | 96 | * @arg0: SMC32 Argument 0 |
|---|
| .. | .. |
|---|
| 104 | 101 | * |
|---|
| 105 | 102 | * Return: 0 on success, a negative value on error |
|---|
| 106 | 103 | */ |
|---|
| 107 | | -int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0, |
|---|
| 108 | | - u32 arg1, u32 arg2, u32 arg3, u32 arg4) |
|---|
| 104 | +int meson_sm_call(struct meson_sm_firmware *fw, unsigned int cmd_index, |
|---|
| 105 | + u32 *ret, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4) |
|---|
| 109 | 106 | { |
|---|
| 110 | 107 | u32 cmd, lret; |
|---|
| 111 | 108 | |
|---|
| 112 | | - if (!fw.chip) |
|---|
| 109 | + if (!fw->chip) |
|---|
| 113 | 110 | return -ENOENT; |
|---|
| 114 | 111 | |
|---|
| 115 | | - cmd = meson_sm_get_cmd(fw.chip, cmd_index); |
|---|
| 112 | + cmd = meson_sm_get_cmd(fw->chip, cmd_index); |
|---|
| 116 | 113 | if (!cmd) |
|---|
| 117 | 114 | return -EINVAL; |
|---|
| 118 | 115 | |
|---|
| .. | .. |
|---|
| 128 | 125 | /** |
|---|
| 129 | 126 | * meson_sm_call_read - retrieve data from secure-monitor |
|---|
| 130 | 127 | * |
|---|
| 128 | + * @fw: Pointer to secure-monitor firmware |
|---|
| 131 | 129 | * @buffer: Buffer to store the retrieved data |
|---|
| 132 | 130 | * @bsize: Size of the buffer |
|---|
| 133 | 131 | * @cmd_index: Index of the SMC32 function ID |
|---|
| .. | .. |
|---|
| 141 | 139 | * When 0 is returned there is no guarantee about the amount of |
|---|
| 142 | 140 | * data read and bsize bytes are copied in buffer. |
|---|
| 143 | 141 | */ |
|---|
| 144 | | -int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index, |
|---|
| 145 | | - u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4) |
|---|
| 142 | +int meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer, |
|---|
| 143 | + unsigned int bsize, unsigned int cmd_index, u32 arg0, |
|---|
| 144 | + u32 arg1, u32 arg2, u32 arg3, u32 arg4) |
|---|
| 146 | 145 | { |
|---|
| 147 | 146 | u32 size; |
|---|
| 148 | 147 | int ret; |
|---|
| 149 | 148 | |
|---|
| 150 | | - if (!fw.chip) |
|---|
| 149 | + if (!fw->chip) |
|---|
| 151 | 150 | return -ENOENT; |
|---|
| 152 | 151 | |
|---|
| 153 | | - if (!fw.chip->cmd_shmem_out_base) |
|---|
| 152 | + if (!fw->chip->cmd_shmem_out_base) |
|---|
| 154 | 153 | return -EINVAL; |
|---|
| 155 | 154 | |
|---|
| 156 | | - if (bsize > fw.chip->shmem_size) |
|---|
| 155 | + if (bsize > fw->chip->shmem_size) |
|---|
| 157 | 156 | return -EINVAL; |
|---|
| 158 | 157 | |
|---|
| 159 | | - if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0) |
|---|
| 158 | + if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0) |
|---|
| 160 | 159 | return -EINVAL; |
|---|
| 161 | 160 | |
|---|
| 162 | 161 | if (size > bsize) |
|---|
| .. | .. |
|---|
| 168 | 167 | size = bsize; |
|---|
| 169 | 168 | |
|---|
| 170 | 169 | if (buffer) |
|---|
| 171 | | - memcpy(buffer, fw.sm_shmem_out_base, size); |
|---|
| 170 | + memcpy(buffer, fw->sm_shmem_out_base, size); |
|---|
| 172 | 171 | |
|---|
| 173 | 172 | return ret; |
|---|
| 174 | 173 | } |
|---|
| .. | .. |
|---|
| 177 | 176 | /** |
|---|
| 178 | 177 | * meson_sm_call_write - send data to secure-monitor |
|---|
| 179 | 178 | * |
|---|
| 179 | + * @fw: Pointer to secure-monitor firmware |
|---|
| 180 | 180 | * @buffer: Buffer containing data to send |
|---|
| 181 | 181 | * @size: Size of the data to send |
|---|
| 182 | 182 | * @cmd_index: Index of the SMC32 function ID |
|---|
| .. | .. |
|---|
| 188 | 188 | * |
|---|
| 189 | 189 | * Return: size of sent data on success, a negative value on error |
|---|
| 190 | 190 | */ |
|---|
| 191 | | -int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index, |
|---|
| 192 | | - u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4) |
|---|
| 191 | +int meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer, |
|---|
| 192 | + unsigned int size, unsigned int cmd_index, u32 arg0, |
|---|
| 193 | + u32 arg1, u32 arg2, u32 arg3, u32 arg4) |
|---|
| 193 | 194 | { |
|---|
| 194 | 195 | u32 written; |
|---|
| 195 | 196 | |
|---|
| 196 | | - if (!fw.chip) |
|---|
| 197 | + if (!fw->chip) |
|---|
| 197 | 198 | return -ENOENT; |
|---|
| 198 | 199 | |
|---|
| 199 | | - if (size > fw.chip->shmem_size) |
|---|
| 200 | + if (size > fw->chip->shmem_size) |
|---|
| 200 | 201 | return -EINVAL; |
|---|
| 201 | 202 | |
|---|
| 202 | | - if (!fw.chip->cmd_shmem_in_base) |
|---|
| 203 | + if (!fw->chip->cmd_shmem_in_base) |
|---|
| 203 | 204 | return -EINVAL; |
|---|
| 204 | 205 | |
|---|
| 205 | | - memcpy(fw.sm_shmem_in_base, buffer, size); |
|---|
| 206 | + memcpy(fw->sm_shmem_in_base, buffer, size); |
|---|
| 206 | 207 | |
|---|
| 207 | | - if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0) |
|---|
| 208 | + if (meson_sm_call(fw, cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0) |
|---|
| 208 | 209 | return -EINVAL; |
|---|
| 209 | 210 | |
|---|
| 210 | 211 | if (!written) |
|---|
| .. | .. |
|---|
| 214 | 215 | } |
|---|
| 215 | 216 | EXPORT_SYMBOL(meson_sm_call_write); |
|---|
| 216 | 217 | |
|---|
| 218 | +/** |
|---|
| 219 | + * meson_sm_get - get pointer to meson_sm_firmware structure. |
|---|
| 220 | + * |
|---|
| 221 | + * @sm_node: Pointer to the secure-monitor Device Tree node. |
|---|
| 222 | + * |
|---|
| 223 | + * Return: NULL is the secure-monitor device is not ready. |
|---|
| 224 | + */ |
|---|
| 225 | +struct meson_sm_firmware *meson_sm_get(struct device_node *sm_node) |
|---|
| 226 | +{ |
|---|
| 227 | + struct platform_device *pdev = of_find_device_by_node(sm_node); |
|---|
| 228 | + |
|---|
| 229 | + if (!pdev) |
|---|
| 230 | + return NULL; |
|---|
| 231 | + |
|---|
| 232 | + return platform_get_drvdata(pdev); |
|---|
| 233 | +} |
|---|
| 234 | +EXPORT_SYMBOL_GPL(meson_sm_get); |
|---|
| 235 | + |
|---|
| 236 | +#define SM_CHIP_ID_LENGTH 119 |
|---|
| 237 | +#define SM_CHIP_ID_OFFSET 4 |
|---|
| 238 | +#define SM_CHIP_ID_SIZE 12 |
|---|
| 239 | + |
|---|
| 240 | +static ssize_t serial_show(struct device *dev, struct device_attribute *attr, |
|---|
| 241 | + char *buf) |
|---|
| 242 | +{ |
|---|
| 243 | + struct platform_device *pdev = to_platform_device(dev); |
|---|
| 244 | + struct meson_sm_firmware *fw; |
|---|
| 245 | + uint8_t *id_buf; |
|---|
| 246 | + int ret; |
|---|
| 247 | + |
|---|
| 248 | + fw = platform_get_drvdata(pdev); |
|---|
| 249 | + |
|---|
| 250 | + id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL); |
|---|
| 251 | + if (!id_buf) |
|---|
| 252 | + return -ENOMEM; |
|---|
| 253 | + |
|---|
| 254 | + ret = meson_sm_call_read(fw, id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID, |
|---|
| 255 | + 0, 0, 0, 0, 0); |
|---|
| 256 | + if (ret < 0) { |
|---|
| 257 | + kfree(id_buf); |
|---|
| 258 | + return ret; |
|---|
| 259 | + } |
|---|
| 260 | + |
|---|
| 261 | + ret = sprintf(buf, "%12phN\n", &id_buf[SM_CHIP_ID_OFFSET]); |
|---|
| 262 | + |
|---|
| 263 | + kfree(id_buf); |
|---|
| 264 | + |
|---|
| 265 | + return ret; |
|---|
| 266 | +} |
|---|
| 267 | + |
|---|
| 268 | +static DEVICE_ATTR_RO(serial); |
|---|
| 269 | + |
|---|
| 270 | +static struct attribute *meson_sm_sysfs_attributes[] = { |
|---|
| 271 | + &dev_attr_serial.attr, |
|---|
| 272 | + NULL, |
|---|
| 273 | +}; |
|---|
| 274 | + |
|---|
| 275 | +static const struct attribute_group meson_sm_sysfs_attr_group = { |
|---|
| 276 | + .attrs = meson_sm_sysfs_attributes, |
|---|
| 277 | +}; |
|---|
| 278 | + |
|---|
| 217 | 279 | static const struct of_device_id meson_sm_ids[] = { |
|---|
| 218 | 280 | { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip }, |
|---|
| 219 | 281 | { /* sentinel */ }, |
|---|
| .. | .. |
|---|
| 221 | 283 | |
|---|
| 222 | 284 | static int __init meson_sm_probe(struct platform_device *pdev) |
|---|
| 223 | 285 | { |
|---|
| 286 | + struct device *dev = &pdev->dev; |
|---|
| 224 | 287 | const struct meson_sm_chip *chip; |
|---|
| 288 | + struct meson_sm_firmware *fw; |
|---|
| 225 | 289 | |
|---|
| 226 | | - chip = of_match_device(meson_sm_ids, &pdev->dev)->data; |
|---|
| 290 | + fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL); |
|---|
| 291 | + if (!fw) |
|---|
| 292 | + return -ENOMEM; |
|---|
| 293 | + |
|---|
| 294 | + chip = of_match_device(meson_sm_ids, dev)->data; |
|---|
| 227 | 295 | |
|---|
| 228 | 296 | if (chip->cmd_shmem_in_base) { |
|---|
| 229 | | - fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base, |
|---|
| 230 | | - chip->shmem_size); |
|---|
| 231 | | - if (WARN_ON(!fw.sm_shmem_in_base)) |
|---|
| 297 | + fw->sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base, |
|---|
| 298 | + chip->shmem_size); |
|---|
| 299 | + if (WARN_ON(!fw->sm_shmem_in_base)) |
|---|
| 232 | 300 | goto out; |
|---|
| 233 | 301 | } |
|---|
| 234 | 302 | |
|---|
| 235 | 303 | if (chip->cmd_shmem_out_base) { |
|---|
| 236 | | - fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base, |
|---|
| 237 | | - chip->shmem_size); |
|---|
| 238 | | - if (WARN_ON(!fw.sm_shmem_out_base)) |
|---|
| 304 | + fw->sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base, |
|---|
| 305 | + chip->shmem_size); |
|---|
| 306 | + if (WARN_ON(!fw->sm_shmem_out_base)) |
|---|
| 239 | 307 | goto out_in_base; |
|---|
| 240 | 308 | } |
|---|
| 241 | 309 | |
|---|
| 242 | | - fw.chip = chip; |
|---|
| 310 | + fw->chip = chip; |
|---|
| 311 | + |
|---|
| 312 | + platform_set_drvdata(pdev, fw); |
|---|
| 313 | + |
|---|
| 243 | 314 | pr_info("secure-monitor enabled\n"); |
|---|
| 315 | + |
|---|
| 316 | + if (sysfs_create_group(&pdev->dev.kobj, &meson_sm_sysfs_attr_group)) |
|---|
| 317 | + goto out_in_base; |
|---|
| 244 | 318 | |
|---|
| 245 | 319 | return 0; |
|---|
| 246 | 320 | |
|---|
| 247 | 321 | out_in_base: |
|---|
| 248 | | - iounmap(fw.sm_shmem_in_base); |
|---|
| 322 | + iounmap(fw->sm_shmem_in_base); |
|---|
| 249 | 323 | out: |
|---|
| 250 | 324 | return -EINVAL; |
|---|
| 251 | 325 | } |
|---|
| .. | .. |
|---|
| 257 | 331 | }, |
|---|
| 258 | 332 | }; |
|---|
| 259 | 333 | module_platform_driver_probe(meson_sm_driver, meson_sm_probe); |
|---|
| 334 | +MODULE_LICENSE("GPL v2"); |
|---|