Kyle Yan | 88156d1 | 2017-01-05 15:12:45 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2017, The Linux Foundation. All rights reserved. |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/firmware.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/elf.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/memblock.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/suspend.h> |
| 22 | #include <linux/rwsem.h> |
| 23 | #include <linux/sysfs.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <linux/list_sort.h> |
| 29 | #include <linux/idr.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/of_gpio.h> |
| 32 | #include <linux/of_address.h> |
| 33 | #include <linux/io.h> |
| 34 | #include <linux/dma-mapping.h> |
| 35 | #include <soc/qcom/ramdump.h> |
| 36 | #include <soc/qcom/subsystem_restart.h> |
| 37 | #include <soc/qcom/secure_buffer.h> |
| 38 | |
| 39 | #include <linux/uaccess.h> |
| 40 | #include <asm/setup.h> |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 41 | #define CREATE_TRACE_POINTS |
| 42 | #include <trace/events/trace_msm_pil_event.h> |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 43 | |
| 44 | #include "peripheral-loader.h" |
| 45 | |
| 46 | #define pil_err(desc, fmt, ...) \ |
| 47 | dev_err(desc->dev, "%s: " fmt, desc->name, ##__VA_ARGS__) |
| 48 | #define pil_info(desc, fmt, ...) \ |
| 49 | dev_info(desc->dev, "%s: " fmt, desc->name, ##__VA_ARGS__) |
| 50 | |
| 51 | #if defined(CONFIG_ARM) |
| 52 | #define pil_memset_io(d, c, count) memset(d, c, count) |
| 53 | #else |
| 54 | #define pil_memset_io(d, c, count) memset_io(d, c, count) |
| 55 | #endif |
| 56 | |
| 57 | #define PIL_NUM_DESC 10 |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 58 | #define NUM_OF_ENCRYPTED_KEY 3 |
Kyle Yan | 0ceeb64 | 2017-09-13 11:21:35 -0700 | [diff] [blame] | 59 | #define MAX_LEN 96 |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 60 | static void __iomem *pil_info_base; |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 61 | static void __iomem *pil_minidump_base; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * proxy_timeout - Override for proxy vote timeouts |
| 65 | * -1: Use driver-specified timeout |
| 66 | * 0: Hold proxy votes until shutdown |
| 67 | * >0: Specify a custom timeout in ms |
| 68 | */ |
| 69 | static int proxy_timeout_ms = -1; |
| 70 | module_param(proxy_timeout_ms, int, 0644); |
| 71 | |
| 72 | static bool disable_timeouts; |
| 73 | /** |
| 74 | * struct pil_mdt - Representation of <name>.mdt file in memory |
| 75 | * @hdr: ELF32 header |
| 76 | * @phdr: ELF32 program headers |
| 77 | */ |
| 78 | struct pil_mdt { |
| 79 | struct elf32_hdr hdr; |
| 80 | struct elf32_phdr phdr[]; |
| 81 | }; |
| 82 | |
| 83 | /** |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 84 | * struct boot_minidump_smem_region - Representation of SMEM TOC |
| 85 | * @region_name: Name of modem segment to be dumped |
| 86 | * @region_base_address: Where segment start from |
| 87 | * @region_size: Size of segment to be dumped |
| 88 | */ |
| 89 | struct boot_minidump_smem_region { |
| 90 | char region_name[16]; |
| 91 | u64 region_base_address; |
| 92 | u64 region_size; |
| 93 | }; |
| 94 | |
| 95 | /** |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 96 | * struct pil_seg - memory map representing one segment |
| 97 | * @next: points to next seg mentor NULL if last segment |
| 98 | * @paddr: physical start address of segment |
| 99 | * @sz: size of segment |
| 100 | * @filesz: size of segment on disk |
| 101 | * @num: segment number |
| 102 | * @relocated: true if segment is relocated, false otherwise |
| 103 | * |
| 104 | * Loosely based on an elf program header. Contains all necessary information |
| 105 | * to load and initialize a segment of the image in memory. |
| 106 | */ |
| 107 | struct pil_seg { |
| 108 | phys_addr_t paddr; |
| 109 | unsigned long sz; |
| 110 | unsigned long filesz; |
| 111 | int num; |
| 112 | struct list_head list; |
| 113 | bool relocated; |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * struct pil_priv - Private state for a pil_desc |
| 118 | * @proxy: work item used to run the proxy unvoting routine |
| 119 | * @ws: wakeup source to prevent suspend during pil_boot |
| 120 | * @wname: name of @ws |
| 121 | * @desc: pointer to pil_desc this is private data for |
| 122 | * @seg: list of segments sorted by physical address |
| 123 | * @entry_addr: physical address where processor starts booting at |
| 124 | * @base_addr: smallest start address among all segments that are relocatable |
| 125 | * @region_start: address where relocatable region starts or lowest address |
| 126 | * for non-relocatable images |
| 127 | * @region_end: address where relocatable region ends or highest address for |
| 128 | * non-relocatable images |
| 129 | * @region: region allocated for relocatable images |
| 130 | * @unvoted_flag: flag to keep track if we have unvoted or not. |
| 131 | * |
| 132 | * This struct contains data for a pil_desc that should not be exposed outside |
| 133 | * of this file. This structure points to the descriptor and the descriptor |
| 134 | * points to this structure so that PIL drivers can't access the private |
| 135 | * data of a descriptor but this file can access both. |
| 136 | */ |
| 137 | struct pil_priv { |
| 138 | struct delayed_work proxy; |
| 139 | struct wakeup_source ws; |
| 140 | char wname[32]; |
| 141 | struct pil_desc *desc; |
| 142 | struct list_head segs; |
| 143 | phys_addr_t entry_addr; |
| 144 | phys_addr_t base_addr; |
| 145 | phys_addr_t region_start; |
| 146 | phys_addr_t region_end; |
| 147 | void *region; |
| 148 | struct pil_image_info __iomem *info; |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 149 | struct md_ssr_ss_info __iomem *minidump; |
| 150 | int minidump_id; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 151 | int id; |
| 152 | int unvoted_flag; |
| 153 | size_t region_size; |
| 154 | }; |
| 155 | |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 156 | static int pil_do_minidump(struct pil_desc *desc, void *ramdump_dev) |
| 157 | { |
| 158 | struct boot_minidump_smem_region __iomem *region_info; |
| 159 | struct ramdump_segment *ramdump_segs, *s; |
| 160 | struct pil_priv *priv = desc->priv; |
| 161 | void __iomem *subsys_smem_base; |
| 162 | void __iomem *offset; |
| 163 | int ss_mdump_seg_cnt; |
| 164 | int ret, i; |
| 165 | |
| 166 | memcpy(&offset, &priv->minidump, sizeof(priv->minidump)); |
| 167 | offset = offset + sizeof(priv->minidump->md_ss_smem_regions_baseptr); |
| 168 | /* There are 3 encryption keys which also need to be dumped */ |
| 169 | ss_mdump_seg_cnt = readb_relaxed(offset) + |
| 170 | NUM_OF_ENCRYPTED_KEY; |
| 171 | |
| 172 | subsys_smem_base = ioremap(__raw_readl(priv->minidump), |
| 173 | ss_mdump_seg_cnt * sizeof(*region_info)); |
| 174 | region_info = |
| 175 | (struct boot_minidump_smem_region __iomem *)subsys_smem_base; |
| 176 | ramdump_segs = kcalloc(ss_mdump_seg_cnt, |
| 177 | sizeof(*ramdump_segs), GFP_KERNEL); |
| 178 | if (!ramdump_segs) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | if (desc->subsys_vmid > 0) |
| 182 | ret = pil_assign_mem_to_linux(desc, priv->region_start, |
| 183 | (priv->region_end - priv->region_start)); |
| 184 | |
| 185 | s = ramdump_segs; |
| 186 | for (i = 0; i < ss_mdump_seg_cnt; i++) { |
| 187 | memcpy(&offset, ®ion_info, sizeof(region_info)); |
| 188 | memcpy(&s->name, ®ion_info, sizeof(region_info)); |
| 189 | offset = offset + sizeof(region_info->region_name); |
| 190 | s->address = __raw_readl(offset); |
| 191 | offset = offset + sizeof(region_info->region_base_address); |
| 192 | s->size = __raw_readl(offset); |
| 193 | s++; |
| 194 | region_info++; |
| 195 | } |
| 196 | ret = do_minidump(ramdump_dev, ramdump_segs, ss_mdump_seg_cnt); |
| 197 | kfree(ramdump_segs); |
| 198 | if (ret) |
| 199 | pil_err(desc, "%s: Ramdump collection failed for subsys %s rc:%d\n", |
| 200 | __func__, desc->name, ret); |
| 201 | writel_relaxed(0, &priv->minidump->md_ss_smem_regions_baseptr); |
| 202 | writeb_relaxed(1, &priv->minidump->md_ss_ssr_cause); |
| 203 | |
| 204 | if (desc->subsys_vmid > 0) |
| 205 | ret = pil_assign_mem_to_subsys(desc, priv->region_start, |
| 206 | (priv->region_end - priv->region_start)); |
| 207 | return ret; |
| 208 | } |
| 209 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 210 | /** |
| 211 | * pil_do_ramdump() - Ramdump an image |
| 212 | * @desc: descriptor from pil_desc_init() |
| 213 | * @ramdump_dev: ramdump device returned from create_ramdump_device() |
| 214 | * |
| 215 | * Calls the ramdump API with a list of segments generated from the addresses |
| 216 | * that the descriptor corresponds to. |
| 217 | */ |
| 218 | int pil_do_ramdump(struct pil_desc *desc, void *ramdump_dev) |
| 219 | { |
| 220 | struct pil_priv *priv = desc->priv; |
| 221 | struct pil_seg *seg; |
| 222 | int count = 0, ret; |
| 223 | struct ramdump_segment *ramdump_segs, *s; |
| 224 | |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 225 | if (priv->minidump && (__raw_readl(priv->minidump) > 0)) |
| 226 | return pil_do_minidump(desc, ramdump_dev); |
| 227 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 228 | list_for_each_entry(seg, &priv->segs, list) |
| 229 | count++; |
| 230 | |
| 231 | ramdump_segs = kcalloc(count, sizeof(*ramdump_segs), GFP_KERNEL); |
| 232 | if (!ramdump_segs) |
| 233 | return -ENOMEM; |
| 234 | |
| 235 | if (desc->subsys_vmid > 0) |
| 236 | ret = pil_assign_mem_to_linux(desc, priv->region_start, |
| 237 | (priv->region_end - priv->region_start)); |
| 238 | |
| 239 | s = ramdump_segs; |
| 240 | list_for_each_entry(seg, &priv->segs, list) { |
| 241 | s->address = seg->paddr; |
| 242 | s->size = seg->sz; |
| 243 | s++; |
| 244 | } |
| 245 | |
| 246 | ret = do_elf_ramdump(ramdump_dev, ramdump_segs, count); |
| 247 | kfree(ramdump_segs); |
| 248 | |
Puja Gupta | 41ff924 | 2017-02-02 13:41:04 -0800 | [diff] [blame] | 249 | if (ret) |
| 250 | pil_err(desc, "%s: Ramdump collection failed for subsys %s rc:%d\n", |
| 251 | __func__, desc->name, ret); |
| 252 | |
| 253 | if (desc->subsys_vmid > 0) |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 254 | ret = pil_assign_mem_to_subsys(desc, priv->region_start, |
| 255 | (priv->region_end - priv->region_start)); |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | EXPORT_SYMBOL(pil_do_ramdump); |
| 260 | |
| 261 | int pil_assign_mem_to_subsys(struct pil_desc *desc, phys_addr_t addr, |
| 262 | size_t size) |
| 263 | { |
| 264 | int ret; |
| 265 | int srcVM[1] = {VMID_HLOS}; |
| 266 | int destVM[1] = {desc->subsys_vmid}; |
| 267 | int destVMperm[1] = {PERM_READ | PERM_WRITE}; |
| 268 | |
| 269 | ret = hyp_assign_phys(addr, size, srcVM, 1, destVM, destVMperm, 1); |
| 270 | if (ret) |
| 271 | pil_err(desc, "%s: failed for %pa address of size %zx - subsys VMid %d rc:%d\n", |
| 272 | __func__, &addr, size, desc->subsys_vmid, ret); |
| 273 | return ret; |
| 274 | } |
| 275 | EXPORT_SYMBOL(pil_assign_mem_to_subsys); |
| 276 | |
| 277 | int pil_assign_mem_to_linux(struct pil_desc *desc, phys_addr_t addr, |
| 278 | size_t size) |
| 279 | { |
| 280 | int ret; |
| 281 | int srcVM[1] = {desc->subsys_vmid}; |
| 282 | int destVM[1] = {VMID_HLOS}; |
| 283 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 284 | |
| 285 | ret = hyp_assign_phys(addr, size, srcVM, 1, destVM, destVMperm, 1); |
| 286 | if (ret) |
| 287 | panic("%s: failed for %pa address of size %zx - subsys VMid %d rc:%d\n", |
| 288 | __func__, &addr, size, desc->subsys_vmid, ret); |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | EXPORT_SYMBOL(pil_assign_mem_to_linux); |
| 293 | |
| 294 | int pil_assign_mem_to_subsys_and_linux(struct pil_desc *desc, |
| 295 | phys_addr_t addr, size_t size) |
| 296 | { |
| 297 | int ret; |
| 298 | int srcVM[1] = {VMID_HLOS}; |
| 299 | int destVM[2] = {VMID_HLOS, desc->subsys_vmid}; |
| 300 | int destVMperm[2] = {PERM_READ | PERM_WRITE, PERM_READ | PERM_WRITE}; |
| 301 | |
| 302 | ret = hyp_assign_phys(addr, size, srcVM, 1, destVM, destVMperm, 2); |
| 303 | if (ret) |
| 304 | pil_err(desc, "%s: failed for %pa address of size %zx - subsys VMid %d rc:%d\n", |
| 305 | __func__, &addr, size, desc->subsys_vmid, ret); |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | EXPORT_SYMBOL(pil_assign_mem_to_subsys_and_linux); |
| 310 | |
| 311 | int pil_reclaim_mem(struct pil_desc *desc, phys_addr_t addr, size_t size, |
| 312 | int VMid) |
| 313 | { |
| 314 | int ret; |
| 315 | int srcVM[2] = {VMID_HLOS, desc->subsys_vmid}; |
| 316 | int destVM[1] = {VMid}; |
| 317 | int destVMperm[1] = {PERM_READ | PERM_WRITE}; |
| 318 | |
| 319 | if (VMid == VMID_HLOS) |
| 320 | destVMperm[0] = PERM_READ | PERM_WRITE | PERM_EXEC; |
| 321 | |
| 322 | ret = hyp_assign_phys(addr, size, srcVM, 2, destVM, destVMperm, 1); |
| 323 | if (ret) |
| 324 | panic("%s: failed for %pa address of size %zx - subsys VMid %d. Fatal error.\n", |
| 325 | __func__, &addr, size, desc->subsys_vmid); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | EXPORT_SYMBOL(pil_reclaim_mem); |
| 330 | |
| 331 | /** |
| 332 | * pil_get_entry_addr() - Retrieve the entry address of a peripheral image |
| 333 | * @desc: descriptor from pil_desc_init() |
| 334 | * |
| 335 | * Returns the physical address where the image boots at or 0 if unknown. |
| 336 | */ |
| 337 | phys_addr_t pil_get_entry_addr(struct pil_desc *desc) |
| 338 | { |
| 339 | return desc->priv ? desc->priv->entry_addr : 0; |
| 340 | } |
| 341 | EXPORT_SYMBOL(pil_get_entry_addr); |
| 342 | |
| 343 | static void __pil_proxy_unvote(struct pil_priv *priv) |
| 344 | { |
| 345 | struct pil_desc *desc = priv->desc; |
| 346 | |
| 347 | desc->ops->proxy_unvote(desc); |
| 348 | notify_proxy_unvote(desc->dev); |
| 349 | __pm_relax(&priv->ws); |
| 350 | module_put(desc->owner); |
| 351 | |
| 352 | } |
| 353 | |
| 354 | static void pil_proxy_unvote_work(struct work_struct *work) |
| 355 | { |
| 356 | struct delayed_work *delayed = to_delayed_work(work); |
| 357 | struct pil_priv *priv = container_of(delayed, struct pil_priv, proxy); |
| 358 | |
| 359 | __pil_proxy_unvote(priv); |
| 360 | } |
| 361 | |
| 362 | static int pil_proxy_vote(struct pil_desc *desc) |
| 363 | { |
| 364 | int ret = 0; |
| 365 | struct pil_priv *priv = desc->priv; |
| 366 | |
| 367 | if (desc->ops->proxy_vote) { |
| 368 | __pm_stay_awake(&priv->ws); |
| 369 | ret = desc->ops->proxy_vote(desc); |
| 370 | if (ret) |
| 371 | __pm_relax(&priv->ws); |
| 372 | } |
| 373 | |
| 374 | if (desc->proxy_unvote_irq) |
| 375 | enable_irq(desc->proxy_unvote_irq); |
| 376 | notify_proxy_vote(desc->dev); |
| 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | static void pil_proxy_unvote(struct pil_desc *desc, int immediate) |
| 382 | { |
| 383 | struct pil_priv *priv = desc->priv; |
| 384 | unsigned long timeout; |
| 385 | |
| 386 | if (proxy_timeout_ms == 0 && !immediate) |
| 387 | return; |
| 388 | else if (proxy_timeout_ms > 0) |
| 389 | timeout = proxy_timeout_ms; |
| 390 | else |
| 391 | timeout = desc->proxy_timeout; |
| 392 | |
| 393 | if (desc->ops->proxy_unvote) { |
| 394 | if (WARN_ON(!try_module_get(desc->owner))) |
| 395 | return; |
| 396 | |
| 397 | if (immediate) |
| 398 | timeout = 0; |
| 399 | |
| 400 | if (!desc->proxy_unvote_irq || immediate) |
| 401 | schedule_delayed_work(&priv->proxy, |
| 402 | msecs_to_jiffies(timeout)); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static irqreturn_t proxy_unvote_intr_handler(int irq, void *dev_id) |
| 407 | { |
| 408 | struct pil_desc *desc = dev_id; |
| 409 | struct pil_priv *priv = desc->priv; |
| 410 | |
| 411 | pil_info(desc, "Power/Clock ready interrupt received\n"); |
| 412 | if (!desc->priv->unvoted_flag) { |
| 413 | desc->priv->unvoted_flag = 1; |
| 414 | __pil_proxy_unvote(priv); |
| 415 | } |
| 416 | |
| 417 | return IRQ_HANDLED; |
| 418 | } |
| 419 | |
| 420 | static bool segment_is_relocatable(const struct elf32_phdr *p) |
| 421 | { |
| 422 | return !!(p->p_flags & BIT(27)); |
| 423 | } |
| 424 | |
| 425 | static phys_addr_t pil_reloc(const struct pil_priv *priv, phys_addr_t addr) |
| 426 | { |
| 427 | return addr - priv->base_addr + priv->region_start; |
| 428 | } |
| 429 | |
| 430 | static struct pil_seg *pil_init_seg(const struct pil_desc *desc, |
| 431 | const struct elf32_phdr *phdr, int num) |
| 432 | { |
| 433 | bool reloc = segment_is_relocatable(phdr); |
| 434 | const struct pil_priv *priv = desc->priv; |
| 435 | struct pil_seg *seg; |
| 436 | |
| 437 | if (!reloc && memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) { |
| 438 | pil_err(desc, "Segment not relocatable,kernel memory would be overwritten[%#08lx, %#08lx)\n", |
| 439 | (unsigned long)phdr->p_paddr, |
| 440 | (unsigned long)(phdr->p_paddr + phdr->p_memsz)); |
| 441 | return ERR_PTR(-EPERM); |
| 442 | } |
| 443 | |
| 444 | if (phdr->p_filesz > phdr->p_memsz) { |
| 445 | pil_err(desc, "Segment %d: file size (%u) is greater than mem size (%u).\n", |
| 446 | num, phdr->p_filesz, phdr->p_memsz); |
| 447 | return ERR_PTR(-EINVAL); |
| 448 | } |
| 449 | |
| 450 | seg = kmalloc(sizeof(*seg), GFP_KERNEL); |
| 451 | if (!seg) |
| 452 | return ERR_PTR(-ENOMEM); |
| 453 | seg->num = num; |
| 454 | seg->paddr = reloc ? pil_reloc(priv, phdr->p_paddr) : phdr->p_paddr; |
| 455 | seg->filesz = phdr->p_filesz; |
| 456 | seg->sz = phdr->p_memsz; |
| 457 | seg->relocated = reloc; |
| 458 | INIT_LIST_HEAD(&seg->list); |
| 459 | |
| 460 | return seg; |
| 461 | } |
| 462 | |
| 463 | #define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24)) |
| 464 | |
| 465 | static int segment_is_loadable(const struct elf32_phdr *p) |
| 466 | { |
| 467 | return (p->p_type == PT_LOAD) && !segment_is_hash(p->p_flags) && |
| 468 | p->p_memsz; |
| 469 | } |
| 470 | |
| 471 | static void pil_dump_segs(const struct pil_priv *priv) |
| 472 | { |
| 473 | struct pil_seg *seg; |
| 474 | phys_addr_t seg_h_paddr; |
| 475 | |
| 476 | list_for_each_entry(seg, &priv->segs, list) { |
| 477 | seg_h_paddr = seg->paddr + seg->sz; |
| 478 | pil_info(priv->desc, "%d: %pa %pa\n", seg->num, |
| 479 | &seg->paddr, &seg_h_paddr); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Ensure the entry address lies within the image limits and if the image is |
| 485 | * relocatable ensure it lies within a relocatable segment. |
| 486 | */ |
| 487 | static int pil_init_entry_addr(struct pil_priv *priv, const struct pil_mdt *mdt) |
| 488 | { |
| 489 | struct pil_seg *seg; |
| 490 | phys_addr_t entry = mdt->hdr.e_entry; |
| 491 | bool image_relocated = priv->region; |
| 492 | |
| 493 | if (image_relocated) |
| 494 | entry = pil_reloc(priv, entry); |
| 495 | priv->entry_addr = entry; |
| 496 | |
| 497 | if (priv->desc->flags & PIL_SKIP_ENTRY_CHECK) |
| 498 | return 0; |
| 499 | |
| 500 | list_for_each_entry(seg, &priv->segs, list) { |
| 501 | if (entry >= seg->paddr && entry < seg->paddr + seg->sz) { |
| 502 | if (!image_relocated) |
| 503 | return 0; |
| 504 | else if (seg->relocated) |
| 505 | return 0; |
| 506 | } |
| 507 | } |
| 508 | pil_err(priv->desc, "entry address %pa not within range\n", &entry); |
| 509 | pil_dump_segs(priv); |
| 510 | return -EADDRNOTAVAIL; |
| 511 | } |
| 512 | |
| 513 | static int pil_alloc_region(struct pil_priv *priv, phys_addr_t min_addr, |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 514 | phys_addr_t max_addr, size_t align, |
| 515 | size_t mdt_size) |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 516 | { |
| 517 | void *region; |
| 518 | size_t size = max_addr - min_addr; |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 519 | size_t aligned_size = max(size, mdt_size); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 520 | |
| 521 | /* Don't reallocate due to fragmentation concerns, just sanity check */ |
| 522 | if (priv->region) { |
| 523 | if (WARN(priv->region_end - priv->region_start < size, |
| 524 | "Can't reuse PIL memory, too small\n")) |
| 525 | return -ENOMEM; |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | if (align > SZ_4M) |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 530 | aligned_size = ALIGN(aligned_size, SZ_4M); |
Runmin Wang | 3f19a28 | 2017-12-01 15:26:07 -0800 | [diff] [blame] | 531 | else if (align > SZ_1M) |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 532 | aligned_size = ALIGN(aligned_size, SZ_1M); |
Runmin Wang | 3f19a28 | 2017-12-01 15:26:07 -0800 | [diff] [blame] | 533 | else |
| 534 | aligned_size = ALIGN(aligned_size, SZ_4K); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 535 | |
| 536 | priv->desc->attrs = 0; |
| 537 | priv->desc->attrs |= DMA_ATTR_SKIP_ZEROING | DMA_ATTR_NO_KERNEL_MAPPING; |
| 538 | |
| 539 | region = dma_alloc_attrs(priv->desc->dev, aligned_size, |
| 540 | &priv->region_start, GFP_KERNEL, |
| 541 | priv->desc->attrs); |
| 542 | |
| 543 | if (region == NULL) { |
| 544 | pil_err(priv->desc, "Failed to allocate relocatable region of size %zx\n", |
| 545 | size); |
Gaurav Kohli | 38b9bad | 2017-03-25 17:36:30 +0530 | [diff] [blame] | 546 | priv->region_start = 0; |
| 547 | priv->region_end = 0; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 548 | return -ENOMEM; |
| 549 | } |
| 550 | |
| 551 | priv->region = region; |
| 552 | priv->region_end = priv->region_start + size; |
| 553 | priv->base_addr = min_addr; |
| 554 | priv->region_size = aligned_size; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 559 | static int pil_setup_region(struct pil_priv *priv, const struct pil_mdt *mdt, |
| 560 | size_t mdt_size) |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 561 | { |
| 562 | const struct elf32_phdr *phdr; |
| 563 | phys_addr_t min_addr_r, min_addr_n, max_addr_r, max_addr_n, start, end; |
| 564 | size_t align = 0; |
| 565 | int i, ret = 0; |
| 566 | bool relocatable = false; |
| 567 | |
| 568 | min_addr_n = min_addr_r = (phys_addr_t)ULLONG_MAX; |
| 569 | max_addr_n = max_addr_r = 0; |
| 570 | |
| 571 | /* Find the image limits */ |
| 572 | for (i = 0; i < mdt->hdr.e_phnum; i++) { |
| 573 | phdr = &mdt->phdr[i]; |
| 574 | if (!segment_is_loadable(phdr)) |
| 575 | continue; |
| 576 | |
| 577 | start = phdr->p_paddr; |
| 578 | end = start + phdr->p_memsz; |
| 579 | |
| 580 | if (segment_is_relocatable(phdr)) { |
| 581 | min_addr_r = min(min_addr_r, start); |
| 582 | max_addr_r = max(max_addr_r, end); |
| 583 | /* |
| 584 | * Lowest relocatable segment dictates alignment of |
| 585 | * relocatable region |
| 586 | */ |
| 587 | if (min_addr_r == start) |
| 588 | align = phdr->p_align; |
| 589 | relocatable = true; |
| 590 | } else { |
| 591 | min_addr_n = min(min_addr_n, start); |
| 592 | max_addr_n = max(max_addr_n, end); |
| 593 | } |
| 594 | |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Align the max address to the next 4K boundary to satisfy iommus and |
| 599 | * XPUs that operate on 4K chunks. |
| 600 | */ |
| 601 | max_addr_n = ALIGN(max_addr_n, SZ_4K); |
| 602 | max_addr_r = ALIGN(max_addr_r, SZ_4K); |
| 603 | |
| 604 | if (relocatable) { |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 605 | ret = pil_alloc_region(priv, min_addr_r, max_addr_r, align, |
| 606 | mdt_size); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 607 | } else { |
| 608 | priv->region_start = min_addr_n; |
| 609 | priv->region_end = max_addr_n; |
| 610 | priv->base_addr = min_addr_n; |
| 611 | } |
| 612 | |
| 613 | if (priv->info) { |
| 614 | __iowrite32_copy(&priv->info->start, &priv->region_start, |
| 615 | sizeof(priv->region_start) / 4); |
| 616 | writel_relaxed(priv->region_end - priv->region_start, |
| 617 | &priv->info->size); |
| 618 | } |
| 619 | |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | static int pil_cmp_seg(void *priv, struct list_head *a, struct list_head *b) |
| 624 | { |
| 625 | int ret = 0; |
| 626 | struct pil_seg *seg_a = list_entry(a, struct pil_seg, list); |
| 627 | struct pil_seg *seg_b = list_entry(b, struct pil_seg, list); |
| 628 | |
| 629 | if (seg_a->paddr < seg_b->paddr) |
| 630 | ret = -1; |
| 631 | else if (seg_a->paddr > seg_b->paddr) |
| 632 | ret = 1; |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 637 | static int pil_init_mmap(struct pil_desc *desc, const struct pil_mdt *mdt, |
| 638 | size_t mdt_size) |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 639 | { |
| 640 | struct pil_priv *priv = desc->priv; |
| 641 | const struct elf32_phdr *phdr; |
| 642 | struct pil_seg *seg; |
| 643 | int i, ret; |
| 644 | |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 645 | ret = pil_setup_region(priv, mdt, mdt_size); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 646 | if (ret) |
| 647 | return ret; |
| 648 | |
| 649 | |
| 650 | pil_info(desc, "loading from %pa to %pa\n", &priv->region_start, |
| 651 | &priv->region_end); |
| 652 | |
| 653 | for (i = 0; i < mdt->hdr.e_phnum; i++) { |
| 654 | phdr = &mdt->phdr[i]; |
| 655 | if (!segment_is_loadable(phdr)) |
| 656 | continue; |
| 657 | |
| 658 | seg = pil_init_seg(desc, phdr, i); |
| 659 | if (IS_ERR(seg)) |
| 660 | return PTR_ERR(seg); |
| 661 | |
| 662 | list_add_tail(&seg->list, &priv->segs); |
| 663 | } |
| 664 | list_sort(NULL, &priv->segs, pil_cmp_seg); |
| 665 | |
| 666 | return pil_init_entry_addr(priv, mdt); |
| 667 | } |
| 668 | |
Puja Gupta | 7c187e8 | 2017-02-06 14:33:19 -0800 | [diff] [blame] | 669 | struct pil_map_fw_info { |
| 670 | void *region; |
| 671 | unsigned long attrs; |
| 672 | phys_addr_t base_addr; |
| 673 | struct device *dev; |
| 674 | }; |
| 675 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 676 | static void pil_release_mmap(struct pil_desc *desc) |
| 677 | { |
| 678 | struct pil_priv *priv = desc->priv; |
| 679 | struct pil_seg *p, *tmp; |
| 680 | u64 zero = 0ULL; |
Gaurav Kohli | ebb4283 | 2017-02-21 12:08:50 +0530 | [diff] [blame] | 681 | |
| 682 | if (priv->info) { |
| 683 | __iowrite32_copy(&priv->info->start, &zero, |
| 684 | sizeof(zero) / 4); |
| 685 | writel_relaxed(0, &priv->info->size); |
| 686 | } |
| 687 | |
| 688 | list_for_each_entry_safe(p, tmp, &priv->segs, list) { |
| 689 | list_del(&p->list); |
| 690 | kfree(p); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | static void pil_clear_segment(struct pil_desc *desc) |
| 695 | { |
| 696 | struct pil_priv *priv = desc->priv; |
Puja Gupta | 7c187e8 | 2017-02-06 14:33:19 -0800 | [diff] [blame] | 697 | u8 __iomem *buf; |
| 698 | |
| 699 | struct pil_map_fw_info map_fw_info = { |
| 700 | .attrs = desc->attrs, |
| 701 | .region = priv->region, |
| 702 | .base_addr = priv->region_start, |
| 703 | .dev = desc->dev, |
| 704 | }; |
| 705 | |
| 706 | void *map_data = desc->map_data ? desc->map_data : &map_fw_info; |
| 707 | |
| 708 | /* Clear memory so that unauthorized ELF code is not left behind */ |
| 709 | buf = desc->map_fw_mem(priv->region_start, (priv->region_end - |
| 710 | priv->region_start), map_data); |
| 711 | pil_memset_io(buf, 0, (priv->region_end - priv->region_start)); |
| 712 | desc->unmap_fw_mem(buf, (priv->region_end - priv->region_start), |
| 713 | map_data); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 714 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | #define IOMAP_SIZE SZ_1M |
| 718 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 719 | static void *map_fw_mem(phys_addr_t paddr, size_t size, void *data) |
| 720 | { |
| 721 | struct pil_map_fw_info *info = data; |
| 722 | |
| 723 | return dma_remap(info->dev, info->region, paddr, size, |
| 724 | info->attrs); |
| 725 | } |
| 726 | |
| 727 | static void unmap_fw_mem(void *vaddr, size_t size, void *data) |
| 728 | { |
| 729 | struct pil_map_fw_info *info = data; |
| 730 | |
| 731 | dma_unremap(info->dev, vaddr, size); |
| 732 | } |
| 733 | |
| 734 | static int pil_load_seg(struct pil_desc *desc, struct pil_seg *seg) |
| 735 | { |
| 736 | int ret = 0, count; |
| 737 | phys_addr_t paddr; |
| 738 | char fw_name[30]; |
| 739 | int num = seg->num; |
| 740 | const struct firmware *fw = NULL; |
Kyle Yan | 88156d1 | 2017-01-05 15:12:45 -0800 | [diff] [blame] | 741 | void __iomem *firmware_buf; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 742 | struct pil_map_fw_info map_fw_info = { |
| 743 | .attrs = desc->attrs, |
| 744 | .region = desc->priv->region, |
| 745 | .base_addr = desc->priv->region_start, |
| 746 | .dev = desc->dev, |
| 747 | }; |
| 748 | void *map_data = desc->map_data ? desc->map_data : &map_fw_info; |
| 749 | |
| 750 | if (seg->filesz) { |
| 751 | snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", |
| 752 | desc->fw_name, num); |
Kyle Yan | 88156d1 | 2017-01-05 15:12:45 -0800 | [diff] [blame] | 753 | firmware_buf = desc->map_fw_mem(seg->paddr, seg->filesz, |
| 754 | map_data); |
| 755 | if (!firmware_buf) { |
| 756 | pil_err(desc, "Failed to map memory for firmware buffer\n"); |
| 757 | return -ENOMEM; |
| 758 | } |
| 759 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 760 | ret = request_firmware_into_buf(&fw, fw_name, desc->dev, |
Kyle Yan | 88156d1 | 2017-01-05 15:12:45 -0800 | [diff] [blame] | 761 | firmware_buf, seg->filesz); |
| 762 | desc->unmap_fw_mem(firmware_buf, seg->filesz, map_data); |
| 763 | |
| 764 | if (ret) { |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 765 | pil_err(desc, "Failed to locate blob %s or blob is too big(rc:%d)\n", |
| 766 | fw_name, ret); |
| 767 | return ret; |
| 768 | } |
| 769 | |
Kyle Yan | 88156d1 | 2017-01-05 15:12:45 -0800 | [diff] [blame] | 770 | if (fw->size != seg->filesz) { |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 771 | pil_err(desc, "Blob size %u doesn't match %lu\n", |
| 772 | ret, seg->filesz); |
| 773 | return -EPERM; |
| 774 | } |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* Zero out trailing memory */ |
| 778 | paddr = seg->paddr + seg->filesz; |
| 779 | count = seg->sz - seg->filesz; |
| 780 | while (count > 0) { |
| 781 | int size; |
| 782 | u8 __iomem *buf; |
| 783 | |
| 784 | size = min_t(size_t, IOMAP_SIZE, count); |
| 785 | buf = desc->map_fw_mem(paddr, size, map_data); |
| 786 | if (!buf) { |
| 787 | pil_err(desc, "Failed to map memory\n"); |
| 788 | return -ENOMEM; |
| 789 | } |
| 790 | pil_memset_io(buf, 0, size); |
| 791 | |
| 792 | desc->unmap_fw_mem(buf, size, map_data); |
| 793 | |
| 794 | count -= size; |
| 795 | paddr += size; |
| 796 | } |
| 797 | |
| 798 | if (desc->ops->verify_blob) { |
| 799 | ret = desc->ops->verify_blob(desc, seg->paddr, seg->sz); |
| 800 | if (ret) |
| 801 | pil_err(desc, "Blob%u failed verification(rc:%d)\n", |
| 802 | num, ret); |
| 803 | } |
| 804 | |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | static int pil_parse_devicetree(struct pil_desc *desc) |
| 809 | { |
| 810 | struct device_node *ofnode = desc->dev->of_node; |
| 811 | int clk_ready = 0; |
| 812 | |
| 813 | if (!ofnode) |
| 814 | return -EINVAL; |
| 815 | |
| 816 | if (of_property_read_u32(ofnode, "qcom,mem-protect-id", |
| 817 | &desc->subsys_vmid)) |
| 818 | pr_debug("Unable to read the addr-protect-id for %s\n", |
| 819 | desc->name); |
| 820 | |
| 821 | if (desc->ops->proxy_unvote && of_find_property(ofnode, |
| 822 | "qcom,gpio-proxy-unvote", |
| 823 | NULL)) { |
| 824 | clk_ready = of_get_named_gpio(ofnode, |
| 825 | "qcom,gpio-proxy-unvote", 0); |
| 826 | |
| 827 | if (clk_ready < 0) { |
| 828 | dev_dbg(desc->dev, |
| 829 | "[%s]: Error getting proxy unvoting gpio\n", |
| 830 | desc->name); |
| 831 | return clk_ready; |
| 832 | } |
| 833 | |
| 834 | clk_ready = gpio_to_irq(clk_ready); |
| 835 | if (clk_ready < 0) { |
| 836 | dev_err(desc->dev, |
| 837 | "[%s]: Error getting proxy unvote IRQ\n", |
| 838 | desc->name); |
| 839 | return clk_ready; |
| 840 | } |
| 841 | } |
| 842 | desc->proxy_unvote_irq = clk_ready; |
| 843 | return 0; |
| 844 | } |
| 845 | |
Kyle Yan | 0ceeb64 | 2017-09-13 11:21:35 -0700 | [diff] [blame] | 846 | static int pil_notify_aop(struct pil_desc *desc, char *status) |
| 847 | { |
| 848 | struct qmp_pkt pkt; |
| 849 | char mbox_msg[MAX_LEN]; |
| 850 | |
| 851 | if (!desc->signal_aop) |
| 852 | return 0; |
| 853 | |
| 854 | snprintf(mbox_msg, MAX_LEN, |
| 855 | "{class: image, res: load_state, name: %s, val: %s}", |
| 856 | desc->name, status); |
| 857 | pkt.size = MAX_LEN; |
| 858 | pkt.data = mbox_msg; |
| 859 | |
| 860 | return mbox_send_message(desc->mbox, &pkt); |
| 861 | } |
| 862 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 863 | /* Synchronize request_firmware() with suspend */ |
| 864 | static DECLARE_RWSEM(pil_pm_rwsem); |
| 865 | |
| 866 | /** |
| 867 | * pil_boot() - Load a peripheral image into memory and boot it |
| 868 | * @desc: descriptor from pil_desc_init() |
| 869 | * |
| 870 | * Returns 0 on success or -ERROR on failure. |
| 871 | */ |
| 872 | int pil_boot(struct pil_desc *desc) |
| 873 | { |
| 874 | int ret; |
| 875 | char fw_name[30]; |
| 876 | const struct pil_mdt *mdt; |
| 877 | const struct elf32_hdr *ehdr; |
| 878 | struct pil_seg *seg; |
| 879 | const struct firmware *fw; |
| 880 | struct pil_priv *priv = desc->priv; |
| 881 | bool mem_protect = false; |
| 882 | bool hyp_assign = false; |
| 883 | |
Kyle Yan | 0ceeb64 | 2017-09-13 11:21:35 -0700 | [diff] [blame] | 884 | ret = pil_notify_aop(desc, "on"); |
| 885 | if (ret < 0) { |
| 886 | pil_err(desc, "Failed to send ON message to AOP rc:%d\n", ret); |
| 887 | return ret; |
| 888 | } |
| 889 | |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 890 | if (desc->shutdown_fail) |
| 891 | pil_err(desc, "Subsystem shutdown failed previously!\n"); |
| 892 | |
| 893 | /* Reinitialize for new image */ |
| 894 | pil_release_mmap(desc); |
| 895 | |
| 896 | down_read(&pil_pm_rwsem); |
| 897 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", desc->fw_name); |
| 898 | ret = request_firmware(&fw, fw_name, desc->dev); |
| 899 | if (ret) { |
| 900 | pil_err(desc, "Failed to locate %s(rc:%d)\n", fw_name, ret); |
| 901 | goto out; |
| 902 | } |
| 903 | |
| 904 | if (fw->size < sizeof(*ehdr)) { |
| 905 | pil_err(desc, "Not big enough to be an elf header\n"); |
| 906 | ret = -EIO; |
| 907 | goto release_fw; |
| 908 | } |
| 909 | |
| 910 | mdt = (const struct pil_mdt *)fw->data; |
| 911 | ehdr = &mdt->hdr; |
| 912 | |
| 913 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
| 914 | pil_err(desc, "Not an elf header\n"); |
| 915 | ret = -EIO; |
| 916 | goto release_fw; |
| 917 | } |
| 918 | |
| 919 | if (ehdr->e_phnum == 0) { |
| 920 | pil_err(desc, "No loadable segments\n"); |
| 921 | ret = -EIO; |
| 922 | goto release_fw; |
| 923 | } |
| 924 | if (sizeof(struct elf32_phdr) * ehdr->e_phnum + |
| 925 | sizeof(struct elf32_hdr) > fw->size) { |
| 926 | pil_err(desc, "Program headers not within mdt\n"); |
| 927 | ret = -EIO; |
| 928 | goto release_fw; |
| 929 | } |
| 930 | |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 931 | ret = pil_init_mmap(desc, mdt, fw->size); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 932 | if (ret) |
| 933 | goto release_fw; |
| 934 | |
| 935 | desc->priv->unvoted_flag = 0; |
| 936 | ret = pil_proxy_vote(desc); |
| 937 | if (ret) { |
| 938 | pil_err(desc, "Failed to proxy vote(rc:%d)\n", ret); |
| 939 | goto release_fw; |
| 940 | } |
| 941 | |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 942 | trace_pil_event("before_init_image", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 943 | if (desc->ops->init_image) |
Kyle Yan | 95180af | 2017-12-01 17:05:59 -0800 | [diff] [blame] | 944 | ret = desc->ops->init_image(desc, fw->data, fw->size, |
| 945 | priv->region_start, priv->region); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 946 | if (ret) { |
| 947 | pil_err(desc, "Initializing image failed(rc:%d)\n", ret); |
| 948 | goto err_boot; |
| 949 | } |
| 950 | |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 951 | trace_pil_event("before_mem_setup", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 952 | if (desc->ops->mem_setup) |
| 953 | ret = desc->ops->mem_setup(desc, priv->region_start, |
| 954 | priv->region_end - priv->region_start); |
| 955 | if (ret) { |
| 956 | pil_err(desc, "Memory setup error(rc:%d)\n", ret); |
| 957 | goto err_deinit_image; |
| 958 | } |
| 959 | |
| 960 | if (desc->subsys_vmid > 0) { |
| 961 | /** |
| 962 | * In case of modem ssr, we need to assign memory back to linux. |
| 963 | * This is not true after cold boot since linux already owns it. |
| 964 | * Also for secure boot devices, modem memory has to be released |
| 965 | * after MBA is booted |
| 966 | */ |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 967 | trace_pil_event("before_assign_mem", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 968 | if (desc->modem_ssr) { |
| 969 | ret = pil_assign_mem_to_linux(desc, priv->region_start, |
| 970 | (priv->region_end - priv->region_start)); |
| 971 | if (ret) |
| 972 | pil_err(desc, "Failed to assign to linux, ret- %d\n", |
| 973 | ret); |
| 974 | } |
| 975 | ret = pil_assign_mem_to_subsys_and_linux(desc, |
| 976 | priv->region_start, |
| 977 | (priv->region_end - priv->region_start)); |
| 978 | if (ret) { |
| 979 | pil_err(desc, "Failed to assign memory, ret - %d\n", |
| 980 | ret); |
| 981 | goto err_deinit_image; |
| 982 | } |
| 983 | hyp_assign = true; |
| 984 | } |
| 985 | |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 986 | trace_pil_event("before_load_seg", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 987 | list_for_each_entry(seg, &desc->priv->segs, list) { |
| 988 | ret = pil_load_seg(desc, seg); |
| 989 | if (ret) |
| 990 | goto err_deinit_image; |
| 991 | } |
| 992 | |
| 993 | if (desc->subsys_vmid > 0) { |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 994 | trace_pil_event("before_reclaim_mem", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 995 | ret = pil_reclaim_mem(desc, priv->region_start, |
| 996 | (priv->region_end - priv->region_start), |
| 997 | desc->subsys_vmid); |
| 998 | if (ret) { |
| 999 | pil_err(desc, "Failed to assign %s memory, ret - %d\n", |
| 1000 | desc->name, ret); |
| 1001 | goto err_deinit_image; |
| 1002 | } |
| 1003 | hyp_assign = false; |
| 1004 | } |
| 1005 | |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 1006 | trace_pil_event("before_auth_reset", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1007 | ret = desc->ops->auth_and_reset(desc); |
| 1008 | if (ret) { |
| 1009 | pil_err(desc, "Failed to bring out of reset(rc:%d)\n", ret); |
| 1010 | goto err_auth_and_reset; |
| 1011 | } |
Gaurav Kohli | 2da4501 | 2017-05-08 15:21:43 +0530 | [diff] [blame] | 1012 | trace_pil_event("reset_done", desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1013 | pil_info(desc, "Brought out of reset\n"); |
| 1014 | desc->modem_ssr = false; |
| 1015 | err_auth_and_reset: |
| 1016 | if (ret && desc->subsys_vmid > 0) { |
| 1017 | pil_assign_mem_to_linux(desc, priv->region_start, |
| 1018 | (priv->region_end - priv->region_start)); |
| 1019 | mem_protect = true; |
| 1020 | } |
| 1021 | err_deinit_image: |
| 1022 | if (ret && desc->ops->deinit_image) |
| 1023 | desc->ops->deinit_image(desc); |
| 1024 | err_boot: |
| 1025 | if (ret && desc->proxy_unvote_irq) |
| 1026 | disable_irq(desc->proxy_unvote_irq); |
| 1027 | pil_proxy_unvote(desc, ret); |
| 1028 | release_fw: |
| 1029 | release_firmware(fw); |
| 1030 | out: |
| 1031 | up_read(&pil_pm_rwsem); |
| 1032 | if (ret) { |
| 1033 | if (priv->region) { |
| 1034 | if (desc->subsys_vmid > 0 && !mem_protect && |
| 1035 | hyp_assign) { |
| 1036 | pil_reclaim_mem(desc, priv->region_start, |
| 1037 | (priv->region_end - |
| 1038 | priv->region_start), |
| 1039 | VMID_HLOS); |
| 1040 | } |
Avaneesh Kumar Dwivedi | b2c7269 | 2017-04-24 21:51:42 +0530 | [diff] [blame] | 1041 | if (desc->clear_fw_region && priv->region_start) |
| 1042 | pil_clear_segment(desc); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1043 | dma_free_attrs(desc->dev, priv->region_size, |
| 1044 | priv->region, priv->region_start, |
| 1045 | desc->attrs); |
| 1046 | priv->region = NULL; |
| 1047 | } |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1048 | pil_release_mmap(desc); |
Kyle Yan | 0ceeb64 | 2017-09-13 11:21:35 -0700 | [diff] [blame] | 1049 | pil_notify_aop(desc, "off"); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1050 | } |
| 1051 | return ret; |
| 1052 | } |
| 1053 | EXPORT_SYMBOL(pil_boot); |
| 1054 | |
| 1055 | /** |
| 1056 | * pil_shutdown() - Shutdown a peripheral |
| 1057 | * @desc: descriptor from pil_desc_init() |
| 1058 | */ |
| 1059 | void pil_shutdown(struct pil_desc *desc) |
| 1060 | { |
Kyle Yan | 0ceeb64 | 2017-09-13 11:21:35 -0700 | [diff] [blame] | 1061 | int ret; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1062 | struct pil_priv *priv = desc->priv; |
| 1063 | |
| 1064 | if (desc->ops->shutdown) { |
| 1065 | if (desc->ops->shutdown(desc)) |
| 1066 | desc->shutdown_fail = true; |
| 1067 | else |
| 1068 | desc->shutdown_fail = false; |
| 1069 | } |
| 1070 | |
| 1071 | if (desc->proxy_unvote_irq) { |
| 1072 | disable_irq(desc->proxy_unvote_irq); |
| 1073 | if (!desc->priv->unvoted_flag) |
| 1074 | pil_proxy_unvote(desc, 1); |
| 1075 | } else if (!proxy_timeout_ms) |
| 1076 | pil_proxy_unvote(desc, 1); |
| 1077 | else |
| 1078 | flush_delayed_work(&priv->proxy); |
Kyle Yan | 0ceeb64 | 2017-09-13 11:21:35 -0700 | [diff] [blame] | 1079 | ret = pil_notify_aop(desc, "off"); |
| 1080 | if (ret < 0) |
| 1081 | pr_warn("pil: failed to send OFF message to AOP rc:%d\n", ret); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1082 | desc->modem_ssr = true; |
| 1083 | } |
| 1084 | EXPORT_SYMBOL(pil_shutdown); |
| 1085 | |
| 1086 | /** |
| 1087 | * pil_free_memory() - Free memory resources associated with a peripheral |
| 1088 | * @desc: descriptor from pil_desc_init() |
| 1089 | */ |
| 1090 | void pil_free_memory(struct pil_desc *desc) |
| 1091 | { |
| 1092 | struct pil_priv *priv = desc->priv; |
| 1093 | |
| 1094 | if (priv->region) { |
| 1095 | if (desc->subsys_vmid > 0) |
| 1096 | pil_assign_mem_to_linux(desc, priv->region_start, |
| 1097 | (priv->region_end - priv->region_start)); |
| 1098 | dma_free_attrs(desc->dev, priv->region_size, |
| 1099 | priv->region, priv->region_start, desc->attrs); |
| 1100 | priv->region = NULL; |
| 1101 | } |
| 1102 | } |
| 1103 | EXPORT_SYMBOL(pil_free_memory); |
| 1104 | |
| 1105 | static DEFINE_IDA(pil_ida); |
| 1106 | |
| 1107 | bool is_timeout_disabled(void) |
| 1108 | { |
| 1109 | return disable_timeouts; |
| 1110 | } |
| 1111 | /** |
| 1112 | * pil_desc_init() - Initialize a pil descriptor |
| 1113 | * @desc: descriptor to initialize |
| 1114 | * |
| 1115 | * Initialize a pil descriptor for use by other pil functions. This function |
| 1116 | * must be called before calling pil_boot() or pil_shutdown(). |
| 1117 | * |
| 1118 | * Returns 0 for success and -ERROR on failure. |
| 1119 | */ |
| 1120 | int pil_desc_init(struct pil_desc *desc) |
| 1121 | { |
| 1122 | struct pil_priv *priv; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1123 | void __iomem *addr; |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 1124 | int ret, ss_imem_offset_mdump; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1125 | char buf[sizeof(priv->info->name)]; |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 1126 | struct device_node *ofnode = desc->dev->of_node; |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote, |
| 1129 | "Invalid proxy voting. Ignoring\n")) |
| 1130 | ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL; |
| 1131 | |
| 1132 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 1133 | if (!priv) |
| 1134 | return -ENOMEM; |
| 1135 | desc->priv = priv; |
| 1136 | priv->desc = desc; |
| 1137 | |
| 1138 | priv->id = ret = ida_simple_get(&pil_ida, 0, PIL_NUM_DESC, GFP_KERNEL); |
| 1139 | if (priv->id < 0) |
| 1140 | goto err; |
| 1141 | |
| 1142 | if (pil_info_base) { |
| 1143 | addr = pil_info_base + sizeof(struct pil_image_info) * priv->id; |
| 1144 | priv->info = (struct pil_image_info __iomem *)addr; |
| 1145 | |
| 1146 | strlcpy(buf, desc->name, sizeof(buf)); |
| 1147 | __iowrite32_copy(priv->info->name, buf, sizeof(buf) / 4); |
| 1148 | } |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 1149 | if (of_property_read_u32(ofnode, "qcom,minidump-id", |
| 1150 | &priv->minidump_id)) |
| 1151 | pr_debug("minidump-id not found for %s\n", desc->name); |
| 1152 | else { |
| 1153 | ss_imem_offset_mdump = |
| 1154 | sizeof(struct md_ssr_ss_info) * priv->minidump_id; |
| 1155 | if (pil_minidump_base) { |
| 1156 | /* Add 0x4 to get start of struct md_ssr_ss_info base |
| 1157 | * from struct md_ssr_toc for any subsystem, |
| 1158 | * struct md_ssr_ss_info is actually the pointer |
| 1159 | * of ToC in smem for any subsystem. |
| 1160 | */ |
| 1161 | addr = pil_minidump_base + ss_imem_offset_mdump + 0x4; |
| 1162 | priv->minidump = (struct md_ssr_ss_info __iomem *)addr; |
| 1163 | } |
| 1164 | } |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1165 | |
| 1166 | ret = pil_parse_devicetree(desc); |
| 1167 | if (ret) |
| 1168 | goto err_parse_dt; |
| 1169 | |
| 1170 | /* Ignore users who don't make any sense */ |
| 1171 | WARN(desc->ops->proxy_unvote && desc->proxy_unvote_irq == 0 |
| 1172 | && !desc->proxy_timeout, |
| 1173 | "Invalid proxy unvote callback or a proxy timeout of 0 was specified or no proxy unvote IRQ was specified.\n"); |
| 1174 | |
| 1175 | if (desc->proxy_unvote_irq) { |
| 1176 | ret = request_threaded_irq(desc->proxy_unvote_irq, |
| 1177 | NULL, |
| 1178 | proxy_unvote_intr_handler, |
| 1179 | IRQF_ONESHOT | IRQF_TRIGGER_RISING, |
| 1180 | desc->name, desc); |
| 1181 | if (ret < 0) { |
| 1182 | dev_err(desc->dev, |
| 1183 | "Unable to request proxy unvote IRQ: %d\n", |
| 1184 | ret); |
| 1185 | goto err; |
| 1186 | } |
| 1187 | disable_irq(desc->proxy_unvote_irq); |
| 1188 | } |
| 1189 | |
| 1190 | snprintf(priv->wname, sizeof(priv->wname), "pil-%s", desc->name); |
| 1191 | wakeup_source_init(&priv->ws, priv->wname); |
| 1192 | INIT_DELAYED_WORK(&priv->proxy, pil_proxy_unvote_work); |
| 1193 | INIT_LIST_HEAD(&priv->segs); |
| 1194 | |
| 1195 | /* Make sure mapping functions are set. */ |
| 1196 | if (!desc->map_fw_mem) |
| 1197 | desc->map_fw_mem = map_fw_mem; |
| 1198 | |
| 1199 | if (!desc->unmap_fw_mem) |
| 1200 | desc->unmap_fw_mem = unmap_fw_mem; |
| 1201 | |
| 1202 | return 0; |
| 1203 | err_parse_dt: |
| 1204 | ida_simple_remove(&pil_ida, priv->id); |
| 1205 | err: |
| 1206 | kfree(priv); |
| 1207 | return ret; |
| 1208 | } |
| 1209 | EXPORT_SYMBOL(pil_desc_init); |
| 1210 | |
| 1211 | /** |
| 1212 | * pil_desc_release() - Release a pil descriptor |
| 1213 | * @desc: descriptor to free |
| 1214 | */ |
| 1215 | void pil_desc_release(struct pil_desc *desc) |
| 1216 | { |
| 1217 | struct pil_priv *priv = desc->priv; |
| 1218 | |
| 1219 | if (priv) { |
| 1220 | ida_simple_remove(&pil_ida, priv->id); |
| 1221 | flush_delayed_work(&priv->proxy); |
| 1222 | wakeup_source_trash(&priv->ws); |
| 1223 | } |
| 1224 | desc->priv = NULL; |
| 1225 | kfree(priv); |
| 1226 | } |
| 1227 | EXPORT_SYMBOL(pil_desc_release); |
| 1228 | |
| 1229 | static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p) |
| 1230 | { |
| 1231 | switch (event) { |
| 1232 | case PM_SUSPEND_PREPARE: |
| 1233 | down_write(&pil_pm_rwsem); |
| 1234 | break; |
| 1235 | case PM_POST_SUSPEND: |
| 1236 | up_write(&pil_pm_rwsem); |
| 1237 | break; |
| 1238 | } |
| 1239 | return NOTIFY_DONE; |
| 1240 | } |
| 1241 | |
| 1242 | static struct notifier_block pil_pm_notifier = { |
| 1243 | .notifier_call = pil_pm_notify, |
| 1244 | }; |
| 1245 | |
| 1246 | static int __init msm_pil_init(void) |
| 1247 | { |
| 1248 | struct device_node *np; |
| 1249 | struct resource res; |
| 1250 | int i; |
| 1251 | |
| 1252 | np = of_find_compatible_node(NULL, NULL, "qcom,msm-imem-pil"); |
| 1253 | if (!np) { |
| 1254 | pr_warn("pil: failed to find qcom,msm-imem-pil node\n"); |
| 1255 | goto out; |
| 1256 | } |
| 1257 | if (of_address_to_resource(np, 0, &res)) { |
| 1258 | pr_warn("pil: address to resource on imem region failed\n"); |
| 1259 | goto out; |
| 1260 | } |
| 1261 | pil_info_base = ioremap(res.start, resource_size(&res)); |
| 1262 | if (!pil_info_base) { |
| 1263 | pr_warn("pil: could not map imem region\n"); |
| 1264 | goto out; |
| 1265 | } |
| 1266 | if (__raw_readl(pil_info_base) == 0x53444247) { |
| 1267 | pr_info("pil: pil-imem set to disable pil timeouts\n"); |
| 1268 | disable_timeouts = true; |
| 1269 | } |
| 1270 | for (i = 0; i < resource_size(&res)/sizeof(u32); i++) |
| 1271 | writel_relaxed(0, pil_info_base + (i * sizeof(u32))); |
| 1272 | |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 1273 | np = of_find_compatible_node(NULL, NULL, "qcom,msm-imem-minidump"); |
| 1274 | if (!np) { |
| 1275 | pr_warn("pil: failed to find qcom,msm-imem-minidump node\n"); |
| 1276 | goto out; |
| 1277 | } else { |
| 1278 | pil_minidump_base = of_iomap(np, 0); |
| 1279 | if (!pil_minidump_base) { |
| 1280 | pr_err("unable to map pil minidump imem offset\n"); |
| 1281 | goto out; |
| 1282 | } |
| 1283 | } |
| 1284 | for (i = 0; i < sizeof(struct md_ssr_toc)/sizeof(u32); i++) |
| 1285 | writel_relaxed(0, pil_minidump_base + (i * sizeof(u32))); |
| 1286 | writel_relaxed(1, pil_minidump_base); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1287 | out: |
| 1288 | return register_pm_notifier(&pil_pm_notifier); |
| 1289 | } |
| 1290 | device_initcall(msm_pil_init); |
| 1291 | |
| 1292 | static void __exit msm_pil_exit(void) |
| 1293 | { |
| 1294 | unregister_pm_notifier(&pil_pm_notifier); |
| 1295 | if (pil_info_base) |
| 1296 | iounmap(pil_info_base); |
Avaneesh Kumar Dwivedi | ec6d139 | 2017-07-06 21:18:03 +0530 | [diff] [blame] | 1297 | if (pil_minidump_base) |
| 1298 | iounmap(pil_minidump_base); |
Kyle Yan | e45fa02 | 2016-08-29 11:40:26 -0700 | [diff] [blame] | 1299 | } |
| 1300 | module_exit(msm_pil_exit); |
| 1301 | |
| 1302 | MODULE_LICENSE("GPL v2"); |
| 1303 | MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset"); |