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> |
Seemanta Dutta | 4e2d49c | 2013-04-05 16:28:11 -0700 | [diff] [blame] | 39 | #include <mach/ramdump.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | |
| 41 | #include "peripheral-loader.h" |
| 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; |
Tianyi Gou | 74a9399 | 2013-04-10 19:58:21 -0700 | [diff] [blame^] | 293 | phys_addr_t seg_h_paddr; |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 294 | |
| 295 | list_for_each_entry(seg, &priv->segs, list) { |
Tianyi Gou | 74a9399 | 2013-04-10 19:58:21 -0700 | [diff] [blame^] | 296 | seg_h_paddr = seg->paddr + seg->sz; |
| 297 | pil_info(priv->desc, "%d: %pa %pa\n", seg->num, |
| 298 | &seg->paddr, &seg_h_paddr); |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /* |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 303 | * Ensure the entry address lies within the image limits and if the image is |
| 304 | * relocatable ensure it lies within a relocatable segment. |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 305 | */ |
| 306 | static int pil_init_entry_addr(struct pil_priv *priv, const struct pil_mdt *mdt) |
| 307 | { |
| 308 | struct pil_seg *seg; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 309 | phys_addr_t entry = mdt->hdr.e_entry; |
| 310 | bool image_relocated = priv->region; |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 311 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 312 | if (image_relocated) |
| 313 | entry = pil_reloc(priv, entry); |
| 314 | priv->entry_addr = entry; |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 315 | |
| 316 | if (priv->desc->flags & PIL_SKIP_ENTRY_CHECK) |
| 317 | return 0; |
| 318 | |
| 319 | list_for_each_entry(seg, &priv->segs, list) { |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 320 | if (entry >= seg->paddr && entry < seg->paddr + seg->sz) { |
| 321 | if (!image_relocated) |
| 322 | return 0; |
| 323 | else if (seg->relocated) |
| 324 | return 0; |
| 325 | } |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 326 | } |
Tianyi Gou | 74a9399 | 2013-04-10 19:58:21 -0700 | [diff] [blame^] | 327 | pil_err(priv->desc, "entry address %pa not within range\n", &entry); |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 328 | pil_dump_segs(priv); |
| 329 | return -EADDRNOTAVAIL; |
| 330 | } |
| 331 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 332 | static int pil_alloc_region(struct pil_priv *priv, phys_addr_t min_addr, |
| 333 | phys_addr_t max_addr, size_t align) |
| 334 | { |
| 335 | struct ion_handle *region; |
| 336 | int ret; |
| 337 | unsigned int mask; |
Stephen Boyd | 6385e31 | 2012-12-12 11:17:04 -0800 | [diff] [blame] | 338 | size_t size = max_addr - min_addr; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 339 | |
Stephen Boyd | 87c7bfa | 2013-03-20 18:19:13 -0700 | [diff] [blame] | 340 | /* Don't reallocate due to fragmentation concerns, just sanity check */ |
| 341 | if (priv->region) { |
| 342 | if (WARN(priv->region_end - priv->region_start < size, |
| 343 | "Can't reuse PIL memory, too small\n")) |
| 344 | return -ENOMEM; |
| 345 | return 0; |
| 346 | } |
| 347 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 348 | if (!ion) { |
| 349 | WARN_ON_ONCE("No ION client, can't support relocation\n"); |
| 350 | return -ENOMEM; |
| 351 | } |
| 352 | |
| 353 | /* Force alignment due to linker scripts not getting it right */ |
| 354 | if (align > SZ_1M) { |
| 355 | mask = ION_HEAP(ION_PIL2_HEAP_ID); |
| 356 | align = SZ_4M; |
| 357 | } else { |
| 358 | mask = ION_HEAP(ION_PIL1_HEAP_ID); |
| 359 | align = SZ_1M; |
| 360 | } |
| 361 | |
| 362 | region = ion_alloc(ion, size, align, mask, 0); |
| 363 | if (IS_ERR(region)) { |
| 364 | pil_err(priv->desc, "Failed to allocate relocatable region\n"); |
| 365 | return PTR_ERR(region); |
| 366 | } |
| 367 | |
| 368 | ret = ion_phys(ion, region, (ion_phys_addr_t *)&priv->region_start, |
| 369 | &size); |
| 370 | if (ret) { |
| 371 | ion_free(ion, region); |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | priv->region = region; |
| 376 | priv->region_end = priv->region_start + size; |
| 377 | priv->base_addr = min_addr; |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 382 | static int pil_setup_region(struct pil_priv *priv, const struct pil_mdt *mdt) |
| 383 | { |
| 384 | const struct elf32_phdr *phdr; |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 385 | phys_addr_t min_addr_r, min_addr_n, max_addr_r, max_addr_n, start, end; |
| 386 | size_t align = 0; |
| 387 | int i, ret = 0; |
| 388 | bool relocatable = false; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 389 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 390 | min_addr_n = min_addr_r = (phys_addr_t)ULLONG_MAX; |
| 391 | max_addr_n = max_addr_r = 0; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 392 | |
| 393 | /* Find the image limits */ |
| 394 | for (i = 0; i < mdt->hdr.e_phnum; i++) { |
| 395 | phdr = &mdt->phdr[i]; |
| 396 | if (!segment_is_loadable(phdr)) |
| 397 | continue; |
| 398 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 399 | start = phdr->p_paddr; |
| 400 | end = start + phdr->p_memsz; |
| 401 | |
| 402 | if (segment_is_relocatable(phdr)) { |
| 403 | min_addr_r = min(min_addr_r, start); |
| 404 | max_addr_r = max(max_addr_r, end); |
| 405 | /* |
| 406 | * Lowest relocatable segment dictates alignment of |
| 407 | * relocatable region |
| 408 | */ |
| 409 | if (min_addr_r == start) |
| 410 | align = phdr->p_align; |
| 411 | relocatable = true; |
| 412 | } else { |
| 413 | min_addr_n = min(min_addr_n, start); |
| 414 | max_addr_n = max(max_addr_n, end); |
| 415 | } |
| 416 | |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Stephen Boyd | 6385e31 | 2012-12-12 11:17:04 -0800 | [diff] [blame] | 419 | /* |
| 420 | * Align the max address to the next 4K boundary to satisfy iommus and |
| 421 | * XPUs that operate on 4K chunks. |
| 422 | */ |
| 423 | max_addr_n = ALIGN(max_addr_n, SZ_4K); |
| 424 | max_addr_r = ALIGN(max_addr_r, SZ_4K); |
| 425 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 426 | if (relocatable) { |
| 427 | ret = pil_alloc_region(priv, min_addr_r, max_addr_r, align); |
| 428 | } else { |
| 429 | priv->region_start = min_addr_n; |
| 430 | priv->region_end = max_addr_n; |
| 431 | priv->base_addr = min_addr_n; |
| 432 | } |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 433 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 434 | writeq(priv->region_start, &priv->info->start); |
| 435 | writel_relaxed(priv->region_end - priv->region_start, |
| 436 | &priv->info->size); |
| 437 | |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 438 | return ret; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 441 | static int pil_cmp_seg(void *priv, struct list_head *a, struct list_head *b) |
| 442 | { |
| 443 | struct pil_seg *seg_a = list_entry(a, struct pil_seg, list); |
| 444 | struct pil_seg *seg_b = list_entry(b, struct pil_seg, list); |
| 445 | |
| 446 | return seg_a->paddr - seg_b->paddr; |
| 447 | } |
| 448 | |
| 449 | static int pil_init_mmap(struct pil_desc *desc, const struct pil_mdt *mdt) |
| 450 | { |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 451 | struct pil_priv *priv = desc->priv; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 452 | const struct elf32_phdr *phdr; |
| 453 | struct pil_seg *seg; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 454 | int i, ret; |
| 455 | |
| 456 | ret = pil_setup_region(priv, mdt); |
| 457 | if (ret) |
| 458 | return ret; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 459 | |
| 460 | for (i = 0; i < mdt->hdr.e_phnum; i++) { |
| 461 | phdr = &mdt->phdr[i]; |
| 462 | if (!segment_is_loadable(phdr)) |
| 463 | continue; |
| 464 | |
| 465 | seg = pil_init_seg(desc, phdr, i); |
| 466 | if (IS_ERR(seg)) |
| 467 | return PTR_ERR(seg); |
| 468 | |
| 469 | list_add_tail(&seg->list, &priv->segs); |
| 470 | } |
| 471 | list_sort(NULL, &priv->segs, pil_cmp_seg); |
| 472 | |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 473 | return pil_init_entry_addr(priv, mdt); |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static void pil_release_mmap(struct pil_desc *desc) |
| 477 | { |
| 478 | struct pil_priv *priv = desc->priv; |
| 479 | struct pil_seg *p, *tmp; |
| 480 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 481 | writeq(0, &priv->info->start); |
| 482 | writel_relaxed(0, &priv->info->size); |
| 483 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 484 | list_for_each_entry_safe(p, tmp, &priv->segs, list) { |
| 485 | list_del(&p->list); |
| 486 | kfree(p); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | #define IOMAP_SIZE SZ_4M |
| 491 | |
| 492 | static int pil_load_seg(struct pil_desc *desc, struct pil_seg *seg) |
| 493 | { |
Tianyi Gou | 74a9399 | 2013-04-10 19:58:21 -0700 | [diff] [blame^] | 494 | int ret = 0, count; |
| 495 | phys_addr_t paddr; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 496 | char fw_name[30]; |
| 497 | const struct firmware *fw = NULL; |
| 498 | const u8 *data; |
| 499 | int num = seg->num; |
| 500 | |
| 501 | if (seg->filesz) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 502 | snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 503 | desc->name, num); |
| 504 | ret = request_firmware(&fw, fw_name, desc->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 506 | pil_err(desc, "Failed to locate blob %s\n", fw_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 507 | return ret; |
| 508 | } |
| 509 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 510 | if (fw->size != seg->filesz) { |
| 511 | pil_err(desc, "Blob size %u doesn't match %lu\n", |
| 512 | fw->size, seg->filesz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 513 | ret = -EPERM; |
| 514 | goto release_fw; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /* Load the segment into memory */ |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 519 | count = seg->filesz; |
| 520 | paddr = seg->paddr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 521 | data = fw ? fw->data : NULL; |
| 522 | while (count > 0) { |
| 523 | int size; |
| 524 | u8 __iomem *buf; |
| 525 | |
| 526 | size = min_t(size_t, IOMAP_SIZE, count); |
| 527 | buf = ioremap(paddr, size); |
| 528 | if (!buf) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 529 | pil_err(desc, "Failed to map memory\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 530 | ret = -ENOMEM; |
| 531 | goto release_fw; |
| 532 | } |
| 533 | memcpy(buf, data, size); |
| 534 | iounmap(buf); |
| 535 | |
| 536 | count -= size; |
| 537 | paddr += size; |
| 538 | data += size; |
| 539 | } |
| 540 | |
| 541 | /* Zero out trailing memory */ |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 542 | count = seg->sz - seg->filesz; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 543 | while (count > 0) { |
| 544 | int size; |
| 545 | u8 __iomem *buf; |
| 546 | |
| 547 | size = min_t(size_t, IOMAP_SIZE, count); |
| 548 | buf = ioremap(paddr, size); |
| 549 | if (!buf) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 550 | pil_err(desc, "Failed to map memory\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | ret = -ENOMEM; |
| 552 | goto release_fw; |
| 553 | } |
| 554 | memset(buf, 0, size); |
| 555 | iounmap(buf); |
| 556 | |
| 557 | count -= size; |
| 558 | paddr += size; |
| 559 | } |
| 560 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 561 | if (desc->ops->verify_blob) { |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 562 | ret = desc->ops->verify_blob(desc, seg->paddr, seg->sz); |
Stephen Boyd | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 563 | if (ret) |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 564 | pil_err(desc, "Blob%u failed verification\n", num); |
Stephen Boyd | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 565 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 566 | |
| 567 | release_fw: |
| 568 | release_firmware(fw); |
| 569 | return ret; |
| 570 | } |
| 571 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 572 | /* Synchronize request_firmware() with suspend */ |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 573 | static DECLARE_RWSEM(pil_pm_rwsem); |
| 574 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 575 | /** |
| 576 | * pil_boot() - Load a peripheral image into memory and boot it |
| 577 | * @desc: descriptor from pil_desc_init() |
| 578 | * |
| 579 | * Returns 0 on success or -ERROR on failure. |
| 580 | */ |
| 581 | int pil_boot(struct pil_desc *desc) |
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 | int ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 584 | char fw_name[30]; |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 585 | const struct pil_mdt *mdt; |
| 586 | const struct elf32_hdr *ehdr; |
| 587 | struct pil_seg *seg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 588 | const struct firmware *fw; |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 589 | struct pil_priv *priv = desc->priv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 590 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 591 | /* Reinitialize for new image */ |
| 592 | pil_release_mmap(desc); |
| 593 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 594 | down_read(&pil_pm_rwsem); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 595 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", desc->name); |
| 596 | ret = request_firmware(&fw, fw_name, desc->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 597 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 598 | pil_err(desc, "Failed to locate %s\n", fw_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 599 | goto out; |
| 600 | } |
| 601 | |
| 602 | if (fw->size < sizeof(*ehdr)) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 603 | pil_err(desc, "Not big enough to be an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 604 | ret = -EIO; |
| 605 | goto release_fw; |
| 606 | } |
| 607 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 608 | mdt = (const struct pil_mdt *)fw->data; |
| 609 | ehdr = &mdt->hdr; |
| 610 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 612 | pil_err(desc, "Not an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 613 | ret = -EIO; |
| 614 | goto release_fw; |
| 615 | } |
| 616 | |
| 617 | if (ehdr->e_phnum == 0) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 618 | pil_err(desc, "No loadable segments\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 619 | ret = -EIO; |
| 620 | goto release_fw; |
| 621 | } |
Stephen Boyd | 96a9f90 | 2011-07-18 18:43:00 -0700 | [diff] [blame] | 622 | if (sizeof(struct elf32_phdr) * ehdr->e_phnum + |
| 623 | sizeof(struct elf32_hdr) > fw->size) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 624 | pil_err(desc, "Program headers not within mdt\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | ret = -EIO; |
| 626 | goto release_fw; |
| 627 | } |
| 628 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 629 | ret = pil_init_mmap(desc, mdt); |
| 630 | if (ret) |
| 631 | goto release_fw; |
| 632 | |
Stephen Boyd | 3030c25 | 2012-08-08 17:24:05 -0700 | [diff] [blame] | 633 | if (desc->ops->init_image) |
| 634 | ret = desc->ops->init_image(desc, fw->data, fw->size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 635 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 636 | pil_err(desc, "Invalid firmware metadata\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | goto release_fw; |
| 638 | } |
| 639 | |
Stephen Boyd | 379e733 | 2012-08-08 18:04:21 -0700 | [diff] [blame] | 640 | if (desc->ops->mem_setup) |
| 641 | ret = desc->ops->mem_setup(desc, priv->region_start, |
| 642 | priv->region_end - priv->region_start); |
| 643 | if (ret) { |
| 644 | pil_err(desc, "Memory setup error\n"); |
| 645 | goto release_fw; |
| 646 | } |
| 647 | |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 648 | list_for_each_entry(seg, &desc->priv->segs, list) { |
| 649 | ret = pil_load_seg(desc, seg); |
| 650 | if (ret) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 651 | goto release_fw; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 654 | ret = pil_proxy_vote(desc); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 655 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 656 | pil_err(desc, "Failed to proxy vote\n"); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 657 | goto release_fw; |
| 658 | } |
| 659 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 660 | ret = desc->ops->auth_and_reset(desc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 661 | if (ret) { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 662 | pil_err(desc, "Failed to bring out of reset\n"); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 663 | goto err_boot; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 664 | } |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 665 | pil_info(desc, "Brought out of reset\n"); |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 666 | err_boot: |
Seemanta Dutta | 78f4319 | 2013-03-04 18:29:43 -0800 | [diff] [blame] | 667 | pil_proxy_unvote(desc, ret); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 668 | release_fw: |
| 669 | release_firmware(fw); |
| 670 | out: |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 671 | up_read(&pil_pm_rwsem); |
Stephen Boyd | 87c7bfa | 2013-03-20 18:19:13 -0700 | [diff] [blame] | 672 | if (ret) { |
| 673 | if (priv->region) { |
| 674 | ion_free(ion, priv->region); |
| 675 | priv->region = NULL; |
| 676 | } |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 677 | pil_release_mmap(desc); |
Stephen Boyd | 87c7bfa | 2013-03-20 18:19:13 -0700 | [diff] [blame] | 678 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 679 | return ret; |
| 680 | } |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 681 | EXPORT_SYMBOL(pil_boot); |
| 682 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 683 | /** |
| 684 | * pil_shutdown() - Shutdown a peripheral |
| 685 | * @desc: descriptor from pil_desc_init() |
| 686 | */ |
| 687 | void pil_shutdown(struct pil_desc *desc) |
Stephen Boyd | 36974ec | 2012-03-22 01:30:59 -0700 | [diff] [blame] | 688 | { |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 689 | struct pil_priv *priv = desc->priv; |
Matt Wagantall | 19b4859 | 2013-02-27 10:18:41 -0800 | [diff] [blame] | 690 | if (desc->ops->shutdown) |
| 691 | desc->ops->shutdown(desc); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 692 | if (proxy_timeout_ms == 0 && desc->ops->proxy_unvote) |
| 693 | desc->ops->proxy_unvote(desc); |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 694 | else |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 695 | flush_delayed_work(&priv->proxy); |
| 696 | } |
| 697 | EXPORT_SYMBOL(pil_shutdown); |
Matt Wagantall | 0aafa35 | 2012-05-14 16:40:41 -0700 | [diff] [blame] | 698 | |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 699 | static DEFINE_IDA(pil_ida); |
| 700 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 701 | /** |
| 702 | * pil_desc_init() - Initialize a pil descriptor |
| 703 | * @desc: descriptor to intialize |
| 704 | * |
| 705 | * Initialize a pil descriptor for use by other pil functions. This function |
| 706 | * must be called before calling pil_boot() or pil_shutdown(). |
| 707 | * |
| 708 | * Returns 0 for success and -ERROR on failure. |
| 709 | */ |
| 710 | int pil_desc_init(struct pil_desc *desc) |
| 711 | { |
| 712 | struct pil_priv *priv; |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 713 | int ret; |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 714 | void __iomem *addr; |
Stephen Boyd | 06ce396 | 2013-01-02 15:03:14 -0800 | [diff] [blame] | 715 | char buf[sizeof(priv->info->name)]; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 716 | |
| 717 | /* Ignore users who don't make any sense */ |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 718 | WARN(desc->ops->proxy_unvote && desc->proxy_unvote_irq == 0 |
| 719 | && !desc->proxy_timeout, |
| 720 | "Invalid proxy unvote callback or a proxy timeout of 0" |
| 721 | " was specified or no proxy unvote IRQ was specified.\n"); |
| 722 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 723 | if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote, |
| 724 | "Invalid proxy voting. Ignoring\n")) |
| 725 | ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL; |
| 726 | |
| 727 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 728 | if (!priv) |
| 729 | return -ENOMEM; |
| 730 | desc->priv = priv; |
| 731 | priv->desc = desc; |
| 732 | |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 733 | priv->id = ret = ida_simple_get(&pil_ida, 0, 10, GFP_KERNEL); |
| 734 | if (priv->id < 0) |
| 735 | goto err; |
| 736 | |
| 737 | addr = PIL_IMAGE_INFO_BASE + sizeof(struct pil_image_info) * priv->id; |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 738 | priv->info = (struct pil_image_info __iomem *)addr; |
| 739 | |
Stephen Boyd | 06ce396 | 2013-01-02 15:03:14 -0800 | [diff] [blame] | 740 | strncpy(buf, desc->name, sizeof(buf)); |
| 741 | __iowrite32_copy(priv->info->name, buf, sizeof(buf) / 4); |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 742 | |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 743 | if (desc->proxy_unvote_irq > 0) { |
| 744 | ret = request_irq(desc->proxy_unvote_irq, |
| 745 | proxy_unvote_intr_handler, |
| 746 | IRQF_TRIGGER_RISING|IRQF_SHARED, |
| 747 | desc->name, desc); |
| 748 | if (ret < 0) { |
| 749 | dev_err(desc->dev, |
| 750 | "Unable to request proxy unvote IRQ: %d\n", |
| 751 | ret); |
| 752 | goto err; |
| 753 | } |
| 754 | } |
| 755 | |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 756 | snprintf(priv->wname, sizeof(priv->wname), "pil-%s", desc->name); |
| 757 | wake_lock_init(&priv->wlock, WAKE_LOCK_SUSPEND, priv->wname); |
| 758 | INIT_DELAYED_WORK(&priv->proxy, pil_proxy_work); |
Stephen Boyd | edff6cf | 2012-07-11 19:39:27 -0700 | [diff] [blame] | 759 | INIT_LIST_HEAD(&priv->segs); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 760 | |
| 761 | return 0; |
Seemanta Dutta | d21a797 | 2013-03-05 12:16:17 -0800 | [diff] [blame] | 762 | err: |
| 763 | kfree(priv); |
| 764 | return ret; |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 765 | } |
| 766 | EXPORT_SYMBOL(pil_desc_init); |
| 767 | |
| 768 | /** |
| 769 | * pil_desc_release() - Release a pil descriptor |
| 770 | * @desc: descriptor to free |
| 771 | */ |
| 772 | void pil_desc_release(struct pil_desc *desc) |
| 773 | { |
| 774 | struct pil_priv *priv = desc->priv; |
| 775 | |
| 776 | if (priv) { |
Stephen Boyd | b455f32 | 2012-11-27 19:00:01 -0800 | [diff] [blame] | 777 | ida_simple_remove(&pil_ida, priv->id); |
Stephen Boyd | 163f1c3 | 2012-06-29 13:20:20 -0700 | [diff] [blame] | 778 | flush_delayed_work(&priv->proxy); |
| 779 | wake_lock_destroy(&priv->wlock); |
| 780 | } |
| 781 | desc->priv = NULL; |
| 782 | kfree(priv); |
| 783 | } |
| 784 | EXPORT_SYMBOL(pil_desc_release); |
| 785 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 786 | static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p) |
| 787 | { |
| 788 | switch (event) { |
| 789 | case PM_SUSPEND_PREPARE: |
| 790 | down_write(&pil_pm_rwsem); |
| 791 | break; |
| 792 | case PM_POST_SUSPEND: |
| 793 | up_write(&pil_pm_rwsem); |
| 794 | break; |
| 795 | } |
| 796 | return NOTIFY_DONE; |
| 797 | } |
| 798 | |
| 799 | static struct notifier_block pil_pm_notifier = { |
| 800 | .notifier_call = pil_pm_notify, |
| 801 | }; |
| 802 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 803 | static int __init msm_pil_init(void) |
| 804 | { |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 805 | ion = msm_ion_client_create(UINT_MAX, "pil"); |
| 806 | if (IS_ERR(ion)) /* Can't support relocatable images */ |
| 807 | ion = NULL; |
Stephen Boyd | 0414812 | 2012-06-29 18:18:12 -0700 | [diff] [blame] | 808 | return register_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 809 | } |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 810 | device_initcall(msm_pil_init); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 811 | |
| 812 | static void __exit msm_pil_exit(void) |
| 813 | { |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 814 | unregister_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 2db158c | 2012-07-26 21:47:17 -0700 | [diff] [blame] | 815 | if (ion) |
| 816 | ion_client_destroy(ion); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 817 | } |
| 818 | module_exit(msm_pil_exit); |
| 819 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 820 | MODULE_LICENSE("GPL v2"); |
| 821 | MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset"); |