Stephen Boyd | 06ce396 | 2013-01-02 15:03:14 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -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> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/firmware.h> |
| 16 | #include <linux/io.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 17 | #include <linux/elf.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/memblock.h> |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 21 | #include <linux/suspend.h> |
| 22 | #include <linux/rwsem.h> |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 23 | #include <linux/sysfs.h> |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/wakelock.h> |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 27 | #include <linux/err.h> |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 28 | #include <linux/msm_ion.h> |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 29 | #include <linux/list.h> |
| 30 | #include <linux/list_sort.h> |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 31 | #include <linux/idr.h> |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 32 | #include <linux/interrupt.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/uaccess.h> |
| 35 | #include <asm/setup.h> |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 36 | #include <asm-generic/io-64-nonatomic-lo-hi.h> |
| 37 | |
| 38 | #include <mach/msm_iomap.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | |
| 40 | #include "peripheral-loader.h" |
Stephen Boyd | 1066777 | 2012-11-28 16:45:35 -0800 | [diff] [blame] | 41 | #include "ramdump.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 43 | #define pil_err(desc, fmt, ...) \ |
| 44 | dev_err(desc->dev, "%s: " fmt, desc->name, ##__VA_ARGS__) |
| 45 | #define pil_info(desc, fmt, ...) \ |
| 46 | dev_info(desc->dev, "%s: " fmt, desc->name, ##__VA_ARGS__) |
| 47 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 48 | #define PIL_IMAGE_INFO_BASE (MSM_IMEM_BASE + 0x94c) |
| 49 | |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 50 | /** |
| 51 | * proxy_timeout - Override for proxy vote timeouts |
| 52 | * -1: Use driver-specified timeout |
| 53 | * 0: Hold proxy votes until shutdown |
| 54 | * >0: Specify a custom timeout in ms |
| 55 | */ |
| 56 | static int proxy_timeout_ms = -1; |
| 57 | module_param(proxy_timeout_ms, int, S_IRUGO | S_IWUSR); |
| 58 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 59 | /** |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 60 | * struct pil_mdt - Representation of <name>.mdt file in memory |
| 61 | * @hdr: ELF32 header |
| 62 | * @phdr: ELF32 program headers |
| 63 | */ |
| 64 | struct pil_mdt { |
| 65 | struct elf32_hdr hdr; |
| 66 | struct elf32_phdr phdr[]; |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * struct pil_seg - memory map representing one segment |
| 71 | * @next: points to next seg mentor NULL if last segment |
| 72 | * @paddr: start address of segment |
| 73 | * @sz: size of segment |
| 74 | * @filesz: size of segment on disk |
| 75 | * @num: segment number |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 76 | * @relocated: true if segment is relocated, false otherwise |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 77 | * |
| 78 | * Loosely based on an elf program header. Contains all necessary information |
| 79 | * to load and initialize a segment of the image in memory. |
| 80 | */ |
| 81 | struct pil_seg { |
| 82 | phys_addr_t paddr; |
| 83 | unsigned long sz; |
| 84 | unsigned long filesz; |
| 85 | int num; |
| 86 | struct list_head list; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 87 | bool relocated; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /** |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 91 | * struct pil_image_info - information in IMEM about image and where it is loaded |
| 92 | * @name: name of image (may or may not be NULL terminated) |
| 93 | * @start: indicates physical address where image starts (little endian) |
| 94 | * @size: size of image (little endian) |
| 95 | */ |
| 96 | struct pil_image_info { |
| 97 | char name[8]; |
| 98 | __le64 start; |
| 99 | __le32 size; |
| 100 | } __attribute__((__packed__)); |
| 101 | |
| 102 | /** |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 103 | * struct pil_priv - Private state for a pil_desc |
| 104 | * @proxy: work item used to run the proxy unvoting routine |
| 105 | * @wlock: wakelock to prevent suspend during pil_boot |
| 106 | * @wname: name of @wlock |
| 107 | * @desc: pointer to pil_desc this is private data for |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 108 | * @seg: list of segments sorted by physical address |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 109 | * @entry_addr: physical address where processor starts booting at |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 110 | * @base_addr: smallest start address among all segments that are relocatable |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 111 | * @region_start: address where relocatable region starts or lowest address |
| 112 | * for non-relocatable images |
| 113 | * @region_end: address where relocatable region ends or highest address for |
| 114 | * non-relocatable images |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 115 | * @region: region allocated for relocatable images |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 116 | * |
| 117 | * This struct contains data for a pil_desc that should not be exposed outside |
| 118 | * of this file. This structure points to the descriptor and the descriptor |
| 119 | * points to this structure so that PIL drivers can't access the private |
| 120 | * data of a descriptor but this file can access both. |
| 121 | */ |
| 122 | struct pil_priv { |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 123 | struct delayed_work proxy; |
| 124 | struct wake_lock wlock; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 125 | char wname[32]; |
| 126 | struct pil_desc *desc; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 127 | struct list_head segs; |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 128 | phys_addr_t entry_addr; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 129 | phys_addr_t base_addr; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 130 | phys_addr_t region_start; |
| 131 | phys_addr_t region_end; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 132 | struct ion_handle *region; |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 133 | struct pil_image_info __iomem *info; |
| 134 | int id; |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Stephen Boyd | 1066777 | 2012-11-28 16:45:35 -0800 | [diff] [blame] | 137 | /** |
| 138 | * pil_do_ramdump() - Ramdump an image |
| 139 | * @desc: descriptor from pil_desc_init() |
| 140 | * @ramdump_dev: ramdump device returned from create_ramdump_device() |
| 141 | * |
| 142 | * Calls the ramdump API with a list of segments generated from the addresses |
| 143 | * that the descriptor corresponds to. |
| 144 | */ |
| 145 | int pil_do_ramdump(struct pil_desc *desc, void *ramdump_dev) |
| 146 | { |
| 147 | struct pil_priv *priv = desc->priv; |
| 148 | struct pil_seg *seg; |
| 149 | int count = 0, ret; |
| 150 | struct ramdump_segment *ramdump_segs, *s; |
| 151 | |
| 152 | list_for_each_entry(seg, &priv->segs, list) |
| 153 | count++; |
| 154 | |
| 155 | ramdump_segs = kmalloc_array(count, sizeof(*ramdump_segs), GFP_KERNEL); |
| 156 | if (!ramdump_segs) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | s = ramdump_segs; |
| 160 | list_for_each_entry(seg, &priv->segs, list) { |
| 161 | s->address = seg->paddr; |
| 162 | s->size = seg->sz; |
| 163 | s++; |
| 164 | } |
| 165 | |
| 166 | ret = do_elf_ramdump(ramdump_dev, ramdump_segs, count); |
| 167 | kfree(ramdump_segs); |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | EXPORT_SYMBOL(pil_do_ramdump); |
| 172 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 173 | static struct ion_client *ion; |
| 174 | |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 175 | /** |
| 176 | * pil_get_entry_addr() - Retrieve the entry address of a peripheral image |
| 177 | * @desc: descriptor from pil_desc_init() |
| 178 | * |
| 179 | * Returns the physical address where the image boots at or 0 if unknown. |
| 180 | */ |
| 181 | phys_addr_t pil_get_entry_addr(struct pil_desc *desc) |
| 182 | { |
| 183 | return desc->priv ? desc->priv->entry_addr : 0; |
| 184 | } |
| 185 | EXPORT_SYMBOL(pil_get_entry_addr); |
| 186 | |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 187 | static void pil_proxy_work(struct work_struct *work) |
| 188 | { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 189 | struct delayed_work *delayed = to_delayed_work(work); |
| 190 | struct pil_priv *priv = container_of(delayed, struct pil_priv, proxy); |
| 191 | struct pil_desc *desc = priv->desc; |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 192 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 193 | desc->ops->proxy_unvote(desc); |
| 194 | wake_unlock(&priv->wlock); |
| 195 | module_put(desc->owner); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 198 | static int pil_proxy_vote(struct pil_desc *desc) |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 199 | { |
Stephen Boyd | 0a56314 | 2012-07-02 13:33:31 -0700 | [diff] [blame] | 200 | int ret = 0; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 201 | struct pil_priv *priv = desc->priv; |
Stephen Boyd | 0a56314 | 2012-07-02 13:33:31 -0700 | [diff] [blame] | 202 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 203 | if (desc->ops->proxy_vote) { |
| 204 | wake_lock(&priv->wlock); |
| 205 | ret = desc->ops->proxy_vote(desc); |
Stephen Boyd | 0a56314 | 2012-07-02 13:33:31 -0700 | [diff] [blame] | 206 | if (ret) |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 207 | wake_unlock(&priv->wlock); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 208 | } |
Stephen Boyd | 0a56314 | 2012-07-02 13:33:31 -0700 | [diff] [blame] | 209 | return ret; |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 212 | static void pil_proxy_unvote(struct pil_desc *desc, int immediate) |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 213 | { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 214 | struct pil_priv *priv = desc->priv; |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 215 | unsigned long timeout; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 216 | |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 217 | if (proxy_timeout_ms == 0 && !immediate) |
| 218 | return; |
| 219 | else if (proxy_timeout_ms > 0) |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 220 | timeout = proxy_timeout_ms; |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 221 | else |
| 222 | timeout = desc->proxy_timeout; |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 223 | |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 224 | if (desc->ops->proxy_unvote) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 225 | if (WARN_ON(!try_module_get(desc->owner))) |
| 226 | return; |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 227 | |
| 228 | if (immediate) |
| 229 | timeout = 0; |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 230 | |
| 231 | if (!desc->proxy_unvote_irq || immediate) |
| 232 | schedule_delayed_work(&priv->proxy, |
| 233 | msecs_to_jiffies(timeout)); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 234 | } |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 237 | static irqreturn_t proxy_unvote_intr_handler(int irq, void *dev_id) |
| 238 | { |
| 239 | struct pil_desc *desc = dev_id; |
| 240 | |
| 241 | schedule_delayed_work(&desc->priv->proxy, 0); |
| 242 | return IRQ_HANDLED; |
| 243 | } |
| 244 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 245 | static bool segment_is_relocatable(const struct elf32_phdr *p) |
| 246 | { |
| 247 | return !!(p->p_flags & BIT(27)); |
| 248 | } |
| 249 | |
| 250 | static phys_addr_t pil_reloc(const struct pil_priv *priv, phys_addr_t addr) |
| 251 | { |
| 252 | return addr - priv->base_addr + priv->region_start; |
| 253 | } |
| 254 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 255 | static struct pil_seg *pil_init_seg(const struct pil_desc *desc, |
| 256 | const struct elf32_phdr *phdr, int num) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | { |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 258 | bool reloc = segment_is_relocatable(phdr); |
| 259 | const struct pil_priv *priv = desc->priv; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 260 | struct pil_seg *seg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 261 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 262 | if (!reloc && memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 263 | pil_err(desc, "kernel memory would be overwritten [%#08lx, %#08lx)\n", |
Stephen Boyd | f79af53 | 2012-05-14 18:57:26 -0700 | [diff] [blame] | 264 | (unsigned long)phdr->p_paddr, |
| 265 | (unsigned long)(phdr->p_paddr + phdr->p_memsz)); |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 266 | return ERR_PTR(-EPERM); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 269 | seg = kmalloc(sizeof(*seg), GFP_KERNEL); |
| 270 | if (!seg) |
| 271 | return ERR_PTR(-ENOMEM); |
| 272 | seg->num = num; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 273 | seg->paddr = reloc ? pil_reloc(priv, phdr->p_paddr) : phdr->p_paddr; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 274 | seg->filesz = phdr->p_filesz; |
| 275 | seg->sz = phdr->p_memsz; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 276 | seg->relocated = reloc; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 277 | INIT_LIST_HEAD(&seg->list); |
| 278 | |
| 279 | return seg; |
| 280 | } |
| 281 | |
| 282 | #define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24)) |
| 283 | |
| 284 | static int segment_is_loadable(const struct elf32_phdr *p) |
| 285 | { |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 286 | return (p->p_type == PT_LOAD) && !segment_is_hash(p->p_flags) && |
| 287 | p->p_memsz; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 290 | static void pil_dump_segs(const struct pil_priv *priv) |
| 291 | { |
| 292 | struct pil_seg *seg; |
| 293 | |
| 294 | list_for_each_entry(seg, &priv->segs, list) { |
| 295 | pil_info(priv->desc, "%d: %#08zx %#08lx\n", seg->num, |
| 296 | seg->paddr, seg->paddr + seg->sz); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 301 | * Ensure the entry address lies within the image limits and if the image is |
| 302 | * relocatable ensure it lies within a relocatable segment. |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 303 | */ |
| 304 | static int pil_init_entry_addr(struct pil_priv *priv, const struct pil_mdt *mdt) |
| 305 | { |
| 306 | struct pil_seg *seg; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 307 | phys_addr_t entry = mdt->hdr.e_entry; |
| 308 | bool image_relocated = priv->region; |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 309 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 310 | if (image_relocated) |
| 311 | entry = pil_reloc(priv, entry); |
| 312 | priv->entry_addr = entry; |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 313 | |
| 314 | if (priv->desc->flags & PIL_SKIP_ENTRY_CHECK) |
| 315 | return 0; |
| 316 | |
| 317 | list_for_each_entry(seg, &priv->segs, list) { |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 318 | if (entry >= seg->paddr && entry < seg->paddr + seg->sz) { |
| 319 | if (!image_relocated) |
| 320 | return 0; |
| 321 | else if (seg->relocated) |
| 322 | return 0; |
| 323 | } |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 324 | } |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 325 | pil_err(priv->desc, "entry address %08zx not within range\n", entry); |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 326 | pil_dump_segs(priv); |
| 327 | return -EADDRNOTAVAIL; |
| 328 | } |
| 329 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 330 | static int pil_alloc_region(struct pil_priv *priv, phys_addr_t min_addr, |
| 331 | phys_addr_t max_addr, size_t align) |
| 332 | { |
| 333 | struct ion_handle *region; |
| 334 | int ret; |
| 335 | unsigned int mask; |
Stephen Boyd | 6385e31 | 2012-12-12 11:17:04 -0800 | [diff] [blame] | 336 | size_t size = max_addr - min_addr; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 337 | |
| 338 | if (!ion) { |
| 339 | WARN_ON_ONCE("No ION client, can't support relocation\n"); |
| 340 | return -ENOMEM; |
| 341 | } |
| 342 | |
| 343 | /* Force alignment due to linker scripts not getting it right */ |
| 344 | if (align > SZ_1M) { |
| 345 | mask = ION_HEAP(ION_PIL2_HEAP_ID); |
| 346 | align = SZ_4M; |
| 347 | } else { |
| 348 | mask = ION_HEAP(ION_PIL1_HEAP_ID); |
| 349 | align = SZ_1M; |
| 350 | } |
| 351 | |
| 352 | region = ion_alloc(ion, size, align, mask, 0); |
| 353 | if (IS_ERR(region)) { |
| 354 | pil_err(priv->desc, "Failed to allocate relocatable region\n"); |
| 355 | return PTR_ERR(region); |
| 356 | } |
| 357 | |
| 358 | ret = ion_phys(ion, region, (ion_phys_addr_t *)&priv->region_start, |
| 359 | &size); |
| 360 | if (ret) { |
| 361 | ion_free(ion, region); |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | priv->region = region; |
| 366 | priv->region_end = priv->region_start + size; |
| 367 | priv->base_addr = min_addr; |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 372 | static int pil_setup_region(struct pil_priv *priv, const struct pil_mdt *mdt) |
| 373 | { |
| 374 | const struct elf32_phdr *phdr; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 375 | phys_addr_t min_addr_r, min_addr_n, max_addr_r, max_addr_n, start, end; |
| 376 | size_t align = 0; |
| 377 | int i, ret = 0; |
| 378 | bool relocatable = false; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 379 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 380 | min_addr_n = min_addr_r = (phys_addr_t)ULLONG_MAX; |
| 381 | max_addr_n = max_addr_r = 0; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 382 | |
| 383 | /* Find the image limits */ |
| 384 | for (i = 0; i < mdt->hdr.e_phnum; i++) { |
| 385 | phdr = &mdt->phdr[i]; |
| 386 | if (!segment_is_loadable(phdr)) |
| 387 | continue; |
| 388 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 389 | start = phdr->p_paddr; |
| 390 | end = start + phdr->p_memsz; |
| 391 | |
| 392 | if (segment_is_relocatable(phdr)) { |
| 393 | min_addr_r = min(min_addr_r, start); |
| 394 | max_addr_r = max(max_addr_r, end); |
| 395 | /* |
| 396 | * Lowest relocatable segment dictates alignment of |
| 397 | * relocatable region |
| 398 | */ |
| 399 | if (min_addr_r == start) |
| 400 | align = phdr->p_align; |
| 401 | relocatable = true; |
| 402 | } else { |
| 403 | min_addr_n = min(min_addr_n, start); |
| 404 | max_addr_n = max(max_addr_n, end); |
| 405 | } |
| 406 | |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Stephen Boyd | 6385e31 | 2012-12-12 11:17:04 -0800 | [diff] [blame] | 409 | /* |
| 410 | * Align the max address to the next 4K boundary to satisfy iommus and |
| 411 | * XPUs that operate on 4K chunks. |
| 412 | */ |
| 413 | max_addr_n = ALIGN(max_addr_n, SZ_4K); |
| 414 | max_addr_r = ALIGN(max_addr_r, SZ_4K); |
| 415 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 416 | if (relocatable) { |
| 417 | ret = pil_alloc_region(priv, min_addr_r, max_addr_r, align); |
| 418 | } else { |
| 419 | priv->region_start = min_addr_n; |
| 420 | priv->region_end = max_addr_n; |
| 421 | priv->base_addr = min_addr_n; |
| 422 | } |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 423 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 424 | writeq(priv->region_start, &priv->info->start); |
| 425 | writel_relaxed(priv->region_end - priv->region_start, |
| 426 | &priv->info->size); |
| 427 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 428 | return ret; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 431 | static int pil_cmp_seg(void *priv, struct list_head *a, struct list_head *b) |
| 432 | { |
| 433 | struct pil_seg *seg_a = list_entry(a, struct pil_seg, list); |
| 434 | struct pil_seg *seg_b = list_entry(b, struct pil_seg, list); |
| 435 | |
| 436 | return seg_a->paddr - seg_b->paddr; |
| 437 | } |
| 438 | |
| 439 | static int pil_init_mmap(struct pil_desc *desc, const struct pil_mdt *mdt) |
| 440 | { |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 441 | struct pil_priv *priv = desc->priv; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 442 | const struct elf32_phdr *phdr; |
| 443 | struct pil_seg *seg; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 444 | int i, ret; |
| 445 | |
| 446 | ret = pil_setup_region(priv, mdt); |
| 447 | if (ret) |
| 448 | return ret; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 449 | |
| 450 | for (i = 0; i < mdt->hdr.e_phnum; i++) { |
| 451 | phdr = &mdt->phdr[i]; |
| 452 | if (!segment_is_loadable(phdr)) |
| 453 | continue; |
| 454 | |
| 455 | seg = pil_init_seg(desc, phdr, i); |
| 456 | if (IS_ERR(seg)) |
| 457 | return PTR_ERR(seg); |
| 458 | |
| 459 | list_add_tail(&seg->list, &priv->segs); |
| 460 | } |
| 461 | list_sort(NULL, &priv->segs, pil_cmp_seg); |
| 462 | |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 463 | return pil_init_entry_addr(priv, mdt); |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void pil_release_mmap(struct pil_desc *desc) |
| 467 | { |
| 468 | struct pil_priv *priv = desc->priv; |
| 469 | struct pil_seg *p, *tmp; |
| 470 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 471 | writeq(0, &priv->info->start); |
| 472 | writel_relaxed(0, &priv->info->size); |
| 473 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 474 | if (priv->region) |
| 475 | ion_free(ion, priv->region); |
Stephen Boyd | 3e29f10 | 2012-11-28 18:55:25 -0800 | [diff] [blame] | 476 | priv->region = NULL; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 477 | list_for_each_entry_safe(p, tmp, &priv->segs, list) { |
| 478 | list_del(&p->list); |
| 479 | kfree(p); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | #define IOMAP_SIZE SZ_4M |
| 484 | |
| 485 | static int pil_load_seg(struct pil_desc *desc, struct pil_seg *seg) |
| 486 | { |
| 487 | int ret = 0, count, paddr; |
| 488 | char fw_name[30]; |
| 489 | const struct firmware *fw = NULL; |
| 490 | const u8 *data; |
| 491 | int num = seg->num; |
| 492 | |
| 493 | if (seg->filesz) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 494 | snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 495 | desc->name, num); |
| 496 | ret = request_firmware(&fw, fw_name, desc->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 497 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 498 | pil_err(desc, "Failed to locate blob %s\n", fw_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 499 | return ret; |
| 500 | } |
| 501 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 502 | if (fw->size != seg->filesz) { |
| 503 | pil_err(desc, "Blob size %u doesn't match %lu\n", |
| 504 | fw->size, seg->filesz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | ret = -EPERM; |
| 506 | goto release_fw; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* Load the segment into memory */ |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 511 | count = seg->filesz; |
| 512 | paddr = seg->paddr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 513 | data = fw ? fw->data : NULL; |
| 514 | while (count > 0) { |
| 515 | int size; |
| 516 | u8 __iomem *buf; |
| 517 | |
| 518 | size = min_t(size_t, IOMAP_SIZE, count); |
| 519 | buf = ioremap(paddr, size); |
| 520 | if (!buf) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 521 | pil_err(desc, "Failed to map memory\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 522 | ret = -ENOMEM; |
| 523 | goto release_fw; |
| 524 | } |
| 525 | memcpy(buf, data, size); |
| 526 | iounmap(buf); |
| 527 | |
| 528 | count -= size; |
| 529 | paddr += size; |
| 530 | data += size; |
| 531 | } |
| 532 | |
| 533 | /* Zero out trailing memory */ |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 534 | count = seg->sz - seg->filesz; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 535 | while (count > 0) { |
| 536 | int size; |
| 537 | u8 __iomem *buf; |
| 538 | |
| 539 | size = min_t(size_t, IOMAP_SIZE, count); |
| 540 | buf = ioremap(paddr, size); |
| 541 | if (!buf) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 542 | pil_err(desc, "Failed to map memory\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 543 | ret = -ENOMEM; |
| 544 | goto release_fw; |
| 545 | } |
| 546 | memset(buf, 0, size); |
| 547 | iounmap(buf); |
| 548 | |
| 549 | count -= size; |
| 550 | paddr += size; |
| 551 | } |
| 552 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 553 | if (desc->ops->verify_blob) { |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 554 | ret = desc->ops->verify_blob(desc, seg->paddr, seg->sz); |
Stephen Boyd | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 555 | if (ret) |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 556 | pil_err(desc, "Blob%u failed verification\n", num); |
Stephen Boyd | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 557 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 558 | |
| 559 | release_fw: |
| 560 | release_firmware(fw); |
| 561 | return ret; |
| 562 | } |
| 563 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 564 | /* Synchronize request_firmware() with suspend */ |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 565 | static DECLARE_RWSEM(pil_pm_rwsem); |
| 566 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 567 | /** |
| 568 | * pil_boot() - Load a peripheral image into memory and boot it |
| 569 | * @desc: descriptor from pil_desc_init() |
| 570 | * |
| 571 | * Returns 0 on success or -ERROR on failure. |
| 572 | */ |
| 573 | int pil_boot(struct pil_desc *desc) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | { |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 575 | int ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | char fw_name[30]; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 577 | const struct pil_mdt *mdt; |
| 578 | const struct elf32_hdr *ehdr; |
| 579 | struct pil_seg *seg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | const struct firmware *fw; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 581 | struct pil_priv *priv = desc->priv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 582 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 583 | /* Reinitialize for new image */ |
| 584 | pil_release_mmap(desc); |
| 585 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 586 | down_read(&pil_pm_rwsem); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 587 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", desc->name); |
| 588 | ret = request_firmware(&fw, fw_name, desc->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 589 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 590 | pil_err(desc, "Failed to locate %s\n", fw_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 591 | goto out; |
| 592 | } |
| 593 | |
| 594 | if (fw->size < sizeof(*ehdr)) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 595 | pil_err(desc, "Not big enough to be an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 596 | ret = -EIO; |
| 597 | goto release_fw; |
| 598 | } |
| 599 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 600 | mdt = (const struct pil_mdt *)fw->data; |
| 601 | ehdr = &mdt->hdr; |
| 602 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 603 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 604 | pil_err(desc, "Not an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 605 | ret = -EIO; |
| 606 | goto release_fw; |
| 607 | } |
| 608 | |
| 609 | if (ehdr->e_phnum == 0) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 610 | pil_err(desc, "No loadable segments\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | ret = -EIO; |
| 612 | goto release_fw; |
| 613 | } |
Stephen Boyd | 96a9f90 | 2011-07-18 18:43:00 -0700 | [diff] [blame] | 614 | if (sizeof(struct elf32_phdr) * ehdr->e_phnum + |
| 615 | sizeof(struct elf32_hdr) > fw->size) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 616 | pil_err(desc, "Program headers not within mdt\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 617 | ret = -EIO; |
| 618 | goto release_fw; |
| 619 | } |
| 620 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 621 | ret = pil_init_mmap(desc, mdt); |
| 622 | if (ret) |
| 623 | goto release_fw; |
| 624 | |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 625 | if (desc->ops->init_image) |
| 626 | ret = desc->ops->init_image(desc, fw->data, fw->size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 627 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 628 | pil_err(desc, "Invalid firmware metadata\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 629 | goto release_fw; |
| 630 | } |
| 631 | |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 632 | if (desc->ops->mem_setup) |
| 633 | ret = desc->ops->mem_setup(desc, priv->region_start, |
| 634 | priv->region_end - priv->region_start); |
| 635 | if (ret) { |
| 636 | pil_err(desc, "Memory setup error\n"); |
| 637 | goto release_fw; |
| 638 | } |
| 639 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 640 | list_for_each_entry(seg, &desc->priv->segs, list) { |
| 641 | ret = pil_load_seg(desc, seg); |
| 642 | if (ret) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 643 | goto release_fw; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 646 | ret = pil_proxy_vote(desc); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 647 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 648 | pil_err(desc, "Failed to proxy vote\n"); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 649 | goto release_fw; |
| 650 | } |
| 651 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 652 | ret = desc->ops->auth_and_reset(desc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 653 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 654 | pil_err(desc, "Failed to bring out of reset\n"); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 655 | goto err_boot; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 656 | } |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 657 | pil_info(desc, "Brought out of reset\n"); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 658 | err_boot: |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 659 | pil_proxy_unvote(desc, ret); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 660 | release_fw: |
| 661 | release_firmware(fw); |
| 662 | out: |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 663 | up_read(&pil_pm_rwsem); |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 664 | if (ret) |
| 665 | pil_release_mmap(desc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 666 | return ret; |
| 667 | } |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 668 | EXPORT_SYMBOL(pil_boot); |
| 669 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 670 | /** |
| 671 | * pil_shutdown() - Shutdown a peripheral |
| 672 | * @desc: descriptor from pil_desc_init() |
| 673 | */ |
| 674 | void pil_shutdown(struct pil_desc *desc) |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 675 | { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 676 | struct pil_priv *priv = desc->priv; |
Matt Wagantall | 19b4859 | 2013-02-27 10:18:41 -0800 | [diff] [blame] | 677 | if (desc->ops->shutdown) |
| 678 | desc->ops->shutdown(desc); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 679 | if (proxy_timeout_ms == 0 && desc->ops->proxy_unvote) |
| 680 | desc->ops->proxy_unvote(desc); |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 681 | else |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 682 | flush_delayed_work(&priv->proxy); |
| 683 | } |
| 684 | EXPORT_SYMBOL(pil_shutdown); |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 685 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 686 | static DEFINE_IDA(pil_ida); |
| 687 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 688 | /** |
| 689 | * pil_desc_init() - Initialize a pil descriptor |
| 690 | * @desc: descriptor to intialize |
| 691 | * |
| 692 | * Initialize a pil descriptor for use by other pil functions. This function |
| 693 | * must be called before calling pil_boot() or pil_shutdown(). |
| 694 | * |
| 695 | * Returns 0 for success and -ERROR on failure. |
| 696 | */ |
| 697 | int pil_desc_init(struct pil_desc *desc) |
| 698 | { |
| 699 | struct pil_priv *priv; |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 700 | int ret; |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 701 | void __iomem *addr; |
Stephen Boyd | 06ce396 | 2013-01-02 15:03:14 -0800 | [diff] [blame] | 702 | char buf[sizeof(priv->info->name)]; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 703 | |
| 704 | /* Ignore users who don't make any sense */ |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 705 | WARN(desc->ops->proxy_unvote && desc->proxy_unvote_irq == 0 |
| 706 | && !desc->proxy_timeout, |
| 707 | "Invalid proxy unvote callback or a proxy timeout of 0" |
| 708 | " was specified or no proxy unvote IRQ was specified.\n"); |
| 709 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 710 | if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote, |
| 711 | "Invalid proxy voting. Ignoring\n")) |
| 712 | ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL; |
| 713 | |
| 714 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 715 | if (!priv) |
| 716 | return -ENOMEM; |
| 717 | desc->priv = priv; |
| 718 | priv->desc = desc; |
| 719 | |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 720 | priv->id = ret = ida_simple_get(&pil_ida, 0, 10, GFP_KERNEL); |
| 721 | if (priv->id < 0) |
| 722 | goto err; |
| 723 | |
| 724 | addr = PIL_IMAGE_INFO_BASE + sizeof(struct pil_image_info) * priv->id; |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 725 | priv->info = (struct pil_image_info __iomem *)addr; |
| 726 | |
Stephen Boyd | 06ce396 | 2013-01-02 15:03:14 -0800 | [diff] [blame] | 727 | strncpy(buf, desc->name, sizeof(buf)); |
| 728 | __iowrite32_copy(priv->info->name, buf, sizeof(buf) / 4); |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 729 | |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 730 | if (desc->proxy_unvote_irq > 0) { |
| 731 | ret = request_irq(desc->proxy_unvote_irq, |
| 732 | proxy_unvote_intr_handler, |
| 733 | IRQF_TRIGGER_RISING|IRQF_SHARED, |
| 734 | desc->name, desc); |
| 735 | if (ret < 0) { |
| 736 | dev_err(desc->dev, |
| 737 | "Unable to request proxy unvote IRQ: %d\n", |
| 738 | ret); |
| 739 | goto err; |
| 740 | } |
| 741 | } |
| 742 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 743 | snprintf(priv->wname, sizeof(priv->wname), "pil-%s", desc->name); |
| 744 | wake_lock_init(&priv->wlock, WAKE_LOCK_SUSPEND, priv->wname); |
| 745 | INIT_DELAYED_WORK(&priv->proxy, pil_proxy_work); |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 746 | INIT_LIST_HEAD(&priv->segs); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 747 | |
| 748 | return 0; |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 749 | err: |
| 750 | kfree(priv); |
| 751 | return ret; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 752 | } |
| 753 | EXPORT_SYMBOL(pil_desc_init); |
| 754 | |
| 755 | /** |
| 756 | * pil_desc_release() - Release a pil descriptor |
| 757 | * @desc: descriptor to free |
| 758 | */ |
| 759 | void pil_desc_release(struct pil_desc *desc) |
| 760 | { |
| 761 | struct pil_priv *priv = desc->priv; |
| 762 | |
| 763 | if (priv) { |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 764 | ida_simple_remove(&pil_ida, priv->id); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 765 | flush_delayed_work(&priv->proxy); |
| 766 | wake_lock_destroy(&priv->wlock); |
| 767 | } |
| 768 | desc->priv = NULL; |
| 769 | kfree(priv); |
| 770 | } |
| 771 | EXPORT_SYMBOL(pil_desc_release); |
| 772 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 773 | static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p) |
| 774 | { |
| 775 | switch (event) { |
| 776 | case PM_SUSPEND_PREPARE: |
| 777 | down_write(&pil_pm_rwsem); |
| 778 | break; |
| 779 | case PM_POST_SUSPEND: |
| 780 | up_write(&pil_pm_rwsem); |
| 781 | break; |
| 782 | } |
| 783 | return NOTIFY_DONE; |
| 784 | } |
| 785 | |
| 786 | static struct notifier_block pil_pm_notifier = { |
| 787 | .notifier_call = pil_pm_notify, |
| 788 | }; |
| 789 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 790 | static int __init msm_pil_init(void) |
| 791 | { |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 792 | ion = msm_ion_client_create(UINT_MAX, "pil"); |
| 793 | if (IS_ERR(ion)) /* Can't support relocatable images */ |
| 794 | ion = NULL; |
Stephen Boyd | 0414812 | 2012-06-29 18:18:12 -0700 | [diff] [blame] | 795 | return register_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 796 | } |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 797 | device_initcall(msm_pil_init); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 798 | |
| 799 | static void __exit msm_pil_exit(void) |
| 800 | { |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 801 | unregister_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 802 | if (ion) |
| 803 | ion_client_destroy(ion); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 804 | } |
| 805 | module_exit(msm_pil_exit); |
| 806 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 807 | MODULE_LICENSE("GPL v2"); |
| 808 | MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset"); |