blob: 475e8a14f251d5a2698bcae0fe9f114bf0ecdb67 [file] [log] [blame]
Stephen Boyd06ce3962013-01-02 15:03:14 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/firmware.h>
16#include <linux/io.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070017#include <linux/elf.h>
18#include <linux/mutex.h>
19#include <linux/memblock.h>
Stephen Boyd3f4da322011-08-30 01:03:23 -070020#include <linux/slab.h>
Stephen Boyd80bde032012-03-16 00:14:42 -070021#include <linux/suspend.h>
22#include <linux/rwsem.h>
Stephen Boyd20ad8102011-10-09 21:28:01 -070023#include <linux/sysfs.h>
Stephen Boyd36974ec2012-03-22 01:30:59 -070024#include <linux/workqueue.h>
25#include <linux/jiffies.h>
26#include <linux/wakelock.h>
Stephen Boydedff6cf2012-07-11 19:39:27 -070027#include <linux/err.h>
Stephen Boyd2db158c2012-07-26 21:47:17 -070028#include <linux/msm_ion.h>
Stephen Boydedff6cf2012-07-11 19:39:27 -070029#include <linux/list.h>
30#include <linux/list_sort.h>
Stephen Boydb455f322012-11-27 19:00:01 -080031#include <linux/idr.h>
Seemanta Duttad21a7972013-03-05 12:16:17 -080032#include <linux/interrupt.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#include <asm/uaccess.h>
35#include <asm/setup.h>
Stephen Boydb455f322012-11-27 19:00:01 -080036#include <asm-generic/io-64-nonatomic-lo-hi.h>
37
38#include <mach/msm_iomap.h>
Seemanta Dutta4e2d49c2013-04-05 16:28:11 -070039#include <mach/ramdump.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
41#include "peripheral-loader.h"
42
Stephen Boyd163f1c32012-06-29 13:20:20 -070043#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 Boydb455f322012-11-27 19:00:01 -080048#define PIL_IMAGE_INFO_BASE (MSM_IMEM_BASE + 0x94c)
49
Matt Wagantall0aafa352012-05-14 16:40:41 -070050/**
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 */
56static int proxy_timeout_ms = -1;
57module_param(proxy_timeout_ms, int, S_IRUGO | S_IWUSR);
58
Stephen Boyd163f1c32012-06-29 13:20:20 -070059/**
Stephen Boydedff6cf2012-07-11 19:39:27 -070060 * struct pil_mdt - Representation of <name>.mdt file in memory
61 * @hdr: ELF32 header
62 * @phdr: ELF32 program headers
63 */
64struct 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 Boyd2db158c2012-07-26 21:47:17 -070076 * @relocated: true if segment is relocated, false otherwise
Stephen Boydedff6cf2012-07-11 19:39:27 -070077 *
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 */
81struct pil_seg {
82 phys_addr_t paddr;
83 unsigned long sz;
84 unsigned long filesz;
85 int num;
86 struct list_head list;
Stephen Boyd2db158c2012-07-26 21:47:17 -070087 bool relocated;
Stephen Boydedff6cf2012-07-11 19:39:27 -070088};
89
90/**
Stephen Boydb455f322012-11-27 19:00:01 -080091 * 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 */
96struct pil_image_info {
97 char name[8];
98 __le64 start;
99 __le32 size;
100} __attribute__((__packed__));
101
102/**
Stephen Boyd163f1c32012-06-29 13:20:20 -0700103 * 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 Boydedff6cf2012-07-11 19:39:27 -0700108 * @seg: list of segments sorted by physical address
Stephen Boyd3030c252012-08-08 17:24:05 -0700109 * @entry_addr: physical address where processor starts booting at
Stephen Boyd2db158c2012-07-26 21:47:17 -0700110 * @base_addr: smallest start address among all segments that are relocatable
Stephen Boyd379e7332012-08-08 18:04:21 -0700111 * @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 Boyd2db158c2012-07-26 21:47:17 -0700115 * @region: region allocated for relocatable images
Stephen Boyd163f1c32012-06-29 13:20:20 -0700116 *
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 */
122struct pil_priv {
Stephen Boyd36974ec2012-03-22 01:30:59 -0700123 struct delayed_work proxy;
124 struct wake_lock wlock;
Stephen Boyd163f1c32012-06-29 13:20:20 -0700125 char wname[32];
126 struct pil_desc *desc;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700127 struct list_head segs;
Stephen Boyd3030c252012-08-08 17:24:05 -0700128 phys_addr_t entry_addr;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700129 phys_addr_t base_addr;
Stephen Boyd379e7332012-08-08 18:04:21 -0700130 phys_addr_t region_start;
131 phys_addr_t region_end;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700132 struct ion_handle *region;
Stephen Boydb455f322012-11-27 19:00:01 -0800133 struct pil_image_info __iomem *info;
134 int id;
Stephen Boyd3f4da322011-08-30 01:03:23 -0700135};
136
Stephen Boyd10667772012-11-28 16:45:35 -0800137/**
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 */
145int 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}
171EXPORT_SYMBOL(pil_do_ramdump);
172
Stephen Boyd2db158c2012-07-26 21:47:17 -0700173static struct ion_client *ion;
174
Stephen Boyd3030c252012-08-08 17:24:05 -0700175/**
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 */
181phys_addr_t pil_get_entry_addr(struct pil_desc *desc)
182{
183 return desc->priv ? desc->priv->entry_addr : 0;
184}
185EXPORT_SYMBOL(pil_get_entry_addr);
186
Stephen Boyd36974ec2012-03-22 01:30:59 -0700187static void pil_proxy_work(struct work_struct *work)
188{
Stephen Boyd163f1c32012-06-29 13:20:20 -0700189 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 Boyd36974ec2012-03-22 01:30:59 -0700192
Stephen Boyd163f1c32012-06-29 13:20:20 -0700193 desc->ops->proxy_unvote(desc);
194 wake_unlock(&priv->wlock);
195 module_put(desc->owner);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700196}
197
Stephen Boyd163f1c32012-06-29 13:20:20 -0700198static int pil_proxy_vote(struct pil_desc *desc)
Stephen Boyd36974ec2012-03-22 01:30:59 -0700199{
Stephen Boyd0a563142012-07-02 13:33:31 -0700200 int ret = 0;
Stephen Boyd163f1c32012-06-29 13:20:20 -0700201 struct pil_priv *priv = desc->priv;
Stephen Boyd0a563142012-07-02 13:33:31 -0700202
Stephen Boyd163f1c32012-06-29 13:20:20 -0700203 if (desc->ops->proxy_vote) {
204 wake_lock(&priv->wlock);
205 ret = desc->ops->proxy_vote(desc);
Stephen Boyd0a563142012-07-02 13:33:31 -0700206 if (ret)
Stephen Boyd163f1c32012-06-29 13:20:20 -0700207 wake_unlock(&priv->wlock);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700208 }
Stephen Boyd0a563142012-07-02 13:33:31 -0700209 return ret;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700210}
211
Seemanta Dutta78f43192013-03-04 18:29:43 -0800212static void pil_proxy_unvote(struct pil_desc *desc, int immediate)
Stephen Boyd36974ec2012-03-22 01:30:59 -0700213{
Stephen Boyd163f1c32012-06-29 13:20:20 -0700214 struct pil_priv *priv = desc->priv;
Seemanta Dutta78f43192013-03-04 18:29:43 -0800215 unsigned long timeout;
Stephen Boyd163f1c32012-06-29 13:20:20 -0700216
Seemanta Dutta78f43192013-03-04 18:29:43 -0800217 if (proxy_timeout_ms == 0 && !immediate)
218 return;
219 else if (proxy_timeout_ms > 0)
Matt Wagantall0aafa352012-05-14 16:40:41 -0700220 timeout = proxy_timeout_ms;
Seemanta Dutta78f43192013-03-04 18:29:43 -0800221 else
222 timeout = desc->proxy_timeout;
Matt Wagantall0aafa352012-05-14 16:40:41 -0700223
Seemanta Dutta78f43192013-03-04 18:29:43 -0800224 if (desc->ops->proxy_unvote) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700225 if (WARN_ON(!try_module_get(desc->owner)))
226 return;
Seemanta Dutta78f43192013-03-04 18:29:43 -0800227
228 if (immediate)
229 timeout = 0;
Seemanta Duttad21a7972013-03-05 12:16:17 -0800230
231 if (!desc->proxy_unvote_irq || immediate)
232 schedule_delayed_work(&priv->proxy,
233 msecs_to_jiffies(timeout));
Stephen Boyd163f1c32012-06-29 13:20:20 -0700234 }
Stephen Boyd36974ec2012-03-22 01:30:59 -0700235}
236
Seemanta Duttad21a7972013-03-05 12:16:17 -0800237static 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 Boyd2db158c2012-07-26 21:47:17 -0700245static bool segment_is_relocatable(const struct elf32_phdr *p)
246{
247 return !!(p->p_flags & BIT(27));
248}
249
250static 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 Boydedff6cf2012-07-11 19:39:27 -0700255static struct pil_seg *pil_init_seg(const struct pil_desc *desc,
256 const struct elf32_phdr *phdr, int num)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257{
Stephen Boyd2db158c2012-07-26 21:47:17 -0700258 bool reloc = segment_is_relocatable(phdr);
259 const struct pil_priv *priv = desc->priv;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700260 struct pil_seg *seg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261
Stephen Boyd2db158c2012-07-26 21:47:17 -0700262 if (!reloc && memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700263 pil_err(desc, "kernel memory would be overwritten [%#08lx, %#08lx)\n",
Stephen Boydf79af532012-05-14 18:57:26 -0700264 (unsigned long)phdr->p_paddr,
265 (unsigned long)(phdr->p_paddr + phdr->p_memsz));
Stephen Boydedff6cf2012-07-11 19:39:27 -0700266 return ERR_PTR(-EPERM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 }
268
Stephen Boydedff6cf2012-07-11 19:39:27 -0700269 seg = kmalloc(sizeof(*seg), GFP_KERNEL);
270 if (!seg)
271 return ERR_PTR(-ENOMEM);
272 seg->num = num;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700273 seg->paddr = reloc ? pil_reloc(priv, phdr->p_paddr) : phdr->p_paddr;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700274 seg->filesz = phdr->p_filesz;
275 seg->sz = phdr->p_memsz;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700276 seg->relocated = reloc;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700277 INIT_LIST_HEAD(&seg->list);
278
279 return seg;
280}
281
282#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
283
284static int segment_is_loadable(const struct elf32_phdr *p)
285{
Stephen Boyd2db158c2012-07-26 21:47:17 -0700286 return (p->p_type == PT_LOAD) && !segment_is_hash(p->p_flags) &&
287 p->p_memsz;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700288}
289
Stephen Boyd3030c252012-08-08 17:24:05 -0700290static void pil_dump_segs(const struct pil_priv *priv)
291{
292 struct pil_seg *seg;
Tianyi Gou74a93992013-04-10 19:58:21 -0700293 phys_addr_t seg_h_paddr;
Stephen Boyd3030c252012-08-08 17:24:05 -0700294
295 list_for_each_entry(seg, &priv->segs, list) {
Tianyi Gou74a93992013-04-10 19:58:21 -0700296 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 Boyd3030c252012-08-08 17:24:05 -0700299 }
300}
301
302/*
Stephen Boyd2db158c2012-07-26 21:47:17 -0700303 * Ensure the entry address lies within the image limits and if the image is
304 * relocatable ensure it lies within a relocatable segment.
Stephen Boyd3030c252012-08-08 17:24:05 -0700305 */
306static int pil_init_entry_addr(struct pil_priv *priv, const struct pil_mdt *mdt)
307{
308 struct pil_seg *seg;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700309 phys_addr_t entry = mdt->hdr.e_entry;
310 bool image_relocated = priv->region;
Stephen Boyd3030c252012-08-08 17:24:05 -0700311
Stephen Boyd2db158c2012-07-26 21:47:17 -0700312 if (image_relocated)
313 entry = pil_reloc(priv, entry);
314 priv->entry_addr = entry;
Stephen Boyd3030c252012-08-08 17:24:05 -0700315
316 if (priv->desc->flags & PIL_SKIP_ENTRY_CHECK)
317 return 0;
318
319 list_for_each_entry(seg, &priv->segs, list) {
Stephen Boyd2db158c2012-07-26 21:47:17 -0700320 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 Boyd3030c252012-08-08 17:24:05 -0700326 }
Tianyi Gou74a93992013-04-10 19:58:21 -0700327 pil_err(priv->desc, "entry address %pa not within range\n", &entry);
Stephen Boyd3030c252012-08-08 17:24:05 -0700328 pil_dump_segs(priv);
329 return -EADDRNOTAVAIL;
330}
331
Stephen Boyd2db158c2012-07-26 21:47:17 -0700332static 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 Boyd6385e312012-12-12 11:17:04 -0800338 size_t size = max_addr - min_addr;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700339
Stephen Boyd87c7bfa2013-03-20 18:19:13 -0700340 /* 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 Boyd2db158c2012-07-26 21:47:17 -0700348 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 Boyd379e7332012-08-08 18:04:21 -0700382static int pil_setup_region(struct pil_priv *priv, const struct pil_mdt *mdt)
383{
384 const struct elf32_phdr *phdr;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700385 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 Boyd379e7332012-08-08 18:04:21 -0700389
Stephen Boyd2db158c2012-07-26 21:47:17 -0700390 min_addr_n = min_addr_r = (phys_addr_t)ULLONG_MAX;
391 max_addr_n = max_addr_r = 0;
Stephen Boyd379e7332012-08-08 18:04:21 -0700392
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 Boyd2db158c2012-07-26 21:47:17 -0700399 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 Boyd379e7332012-08-08 18:04:21 -0700417 }
418
Stephen Boyd6385e312012-12-12 11:17:04 -0800419 /*
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 Boyd2db158c2012-07-26 21:47:17 -0700426 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 Boyd379e7332012-08-08 18:04:21 -0700433
Stephen Boydb455f322012-11-27 19:00:01 -0800434 writeq(priv->region_start, &priv->info->start);
435 writel_relaxed(priv->region_end - priv->region_start,
436 &priv->info->size);
437
Stephen Boyd2db158c2012-07-26 21:47:17 -0700438 return ret;
Stephen Boyd379e7332012-08-08 18:04:21 -0700439}
440
Stephen Boydedff6cf2012-07-11 19:39:27 -0700441static 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
449static int pil_init_mmap(struct pil_desc *desc, const struct pil_mdt *mdt)
450{
Stephen Boyd3030c252012-08-08 17:24:05 -0700451 struct pil_priv *priv = desc->priv;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700452 const struct elf32_phdr *phdr;
453 struct pil_seg *seg;
Stephen Boyd379e7332012-08-08 18:04:21 -0700454 int i, ret;
455
456 ret = pil_setup_region(priv, mdt);
457 if (ret)
458 return ret;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700459
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 Boyd3030c252012-08-08 17:24:05 -0700473 return pil_init_entry_addr(priv, mdt);
Stephen Boydedff6cf2012-07-11 19:39:27 -0700474}
475
476static void pil_release_mmap(struct pil_desc *desc)
477{
478 struct pil_priv *priv = desc->priv;
479 struct pil_seg *p, *tmp;
480
Stephen Boydb455f322012-11-27 19:00:01 -0800481 writeq(0, &priv->info->start);
482 writel_relaxed(0, &priv->info->size);
483
Stephen Boydedff6cf2012-07-11 19:39:27 -0700484 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
492static int pil_load_seg(struct pil_desc *desc, struct pil_seg *seg)
493{
Tianyi Gou74a93992013-04-10 19:58:21 -0700494 int ret = 0, count;
495 phys_addr_t paddr;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700496 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 Boyd3f4da322011-08-30 01:03:23 -0700502 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
Stephen Boyd163f1c32012-06-29 13:20:20 -0700503 desc->name, num);
504 ret = request_firmware(&fw, fw_name, desc->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700506 pil_err(desc, "Failed to locate blob %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 return ret;
508 }
509
Stephen Boydedff6cf2012-07-11 19:39:27 -0700510 if (fw->size != seg->filesz) {
511 pil_err(desc, "Blob size %u doesn't match %lu\n",
512 fw->size, seg->filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 ret = -EPERM;
514 goto release_fw;
515 }
516 }
517
518 /* Load the segment into memory */
Stephen Boydedff6cf2012-07-11 19:39:27 -0700519 count = seg->filesz;
520 paddr = seg->paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521 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 Boyd163f1c32012-06-29 13:20:20 -0700529 pil_err(desc, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 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 Boydedff6cf2012-07-11 19:39:27 -0700542 count = seg->sz - seg->filesz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 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 Boyd163f1c32012-06-29 13:20:20 -0700550 pil_err(desc, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 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 Boyd163f1c32012-06-29 13:20:20 -0700561 if (desc->ops->verify_blob) {
Stephen Boydedff6cf2012-07-11 19:39:27 -0700562 ret = desc->ops->verify_blob(desc, seg->paddr, seg->sz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800563 if (ret)
Stephen Boyd163f1c32012-06-29 13:20:20 -0700564 pil_err(desc, "Blob%u failed verification\n", num);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800565 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566
567release_fw:
568 release_firmware(fw);
569 return ret;
570}
571
Stephen Boyd163f1c32012-06-29 13:20:20 -0700572/* Synchronize request_firmware() with suspend */
Stephen Boyd80bde032012-03-16 00:14:42 -0700573static DECLARE_RWSEM(pil_pm_rwsem);
574
Stephen Boyd163f1c32012-06-29 13:20:20 -0700575/**
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 */
581int pil_boot(struct pil_desc *desc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582{
Stephen Boydedff6cf2012-07-11 19:39:27 -0700583 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 char fw_name[30];
Stephen Boydedff6cf2012-07-11 19:39:27 -0700585 const struct pil_mdt *mdt;
586 const struct elf32_hdr *ehdr;
587 struct pil_seg *seg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 const struct firmware *fw;
Stephen Boyd379e7332012-08-08 18:04:21 -0700589 struct pil_priv *priv = desc->priv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
Stephen Boydedff6cf2012-07-11 19:39:27 -0700591 /* Reinitialize for new image */
592 pil_release_mmap(desc);
593
Stephen Boyd80bde032012-03-16 00:14:42 -0700594 down_read(&pil_pm_rwsem);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700595 snprintf(fw_name, sizeof(fw_name), "%s.mdt", desc->name);
596 ret = request_firmware(&fw, fw_name, desc->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700598 pil_err(desc, "Failed to locate %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 goto out;
600 }
601
602 if (fw->size < sizeof(*ehdr)) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700603 pil_err(desc, "Not big enough to be an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 ret = -EIO;
605 goto release_fw;
606 }
607
Stephen Boydedff6cf2012-07-11 19:39:27 -0700608 mdt = (const struct pil_mdt *)fw->data;
609 ehdr = &mdt->hdr;
610
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700612 pil_err(desc, "Not an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613 ret = -EIO;
614 goto release_fw;
615 }
616
617 if (ehdr->e_phnum == 0) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700618 pil_err(desc, "No loadable segments\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 ret = -EIO;
620 goto release_fw;
621 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700622 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
623 sizeof(struct elf32_hdr) > fw->size) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700624 pil_err(desc, "Program headers not within mdt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 ret = -EIO;
626 goto release_fw;
627 }
628
Stephen Boydedff6cf2012-07-11 19:39:27 -0700629 ret = pil_init_mmap(desc, mdt);
630 if (ret)
631 goto release_fw;
632
Stephen Boyd3030c252012-08-08 17:24:05 -0700633 if (desc->ops->init_image)
634 ret = desc->ops->init_image(desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700636 pil_err(desc, "Invalid firmware metadata\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637 goto release_fw;
638 }
639
Stephen Boyd379e7332012-08-08 18:04:21 -0700640 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 Boydedff6cf2012-07-11 19:39:27 -0700648 list_for_each_entry(seg, &desc->priv->segs, list) {
649 ret = pil_load_seg(desc, seg);
650 if (ret)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 goto release_fw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 }
653
Stephen Boyd163f1c32012-06-29 13:20:20 -0700654 ret = pil_proxy_vote(desc);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700655 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700656 pil_err(desc, "Failed to proxy vote\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700657 goto release_fw;
658 }
659
Stephen Boyd163f1c32012-06-29 13:20:20 -0700660 ret = desc->ops->auth_and_reset(desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700662 pil_err(desc, "Failed to bring out of reset\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700663 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 }
Stephen Boyd163f1c32012-06-29 13:20:20 -0700665 pil_info(desc, "Brought out of reset\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700666err_boot:
Seemanta Dutta78f43192013-03-04 18:29:43 -0800667 pil_proxy_unvote(desc, ret);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668release_fw:
669 release_firmware(fw);
670out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700671 up_read(&pil_pm_rwsem);
Stephen Boyd87c7bfa2013-03-20 18:19:13 -0700672 if (ret) {
673 if (priv->region) {
674 ion_free(ion, priv->region);
675 priv->region = NULL;
676 }
Stephen Boydedff6cf2012-07-11 19:39:27 -0700677 pil_release_mmap(desc);
Stephen Boyd87c7bfa2013-03-20 18:19:13 -0700678 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 return ret;
680}
Stephen Boyd163f1c32012-06-29 13:20:20 -0700681EXPORT_SYMBOL(pil_boot);
682
Stephen Boyd163f1c32012-06-29 13:20:20 -0700683/**
684 * pil_shutdown() - Shutdown a peripheral
685 * @desc: descriptor from pil_desc_init()
686 */
687void pil_shutdown(struct pil_desc *desc)
Stephen Boyd36974ec2012-03-22 01:30:59 -0700688{
Stephen Boyd163f1c32012-06-29 13:20:20 -0700689 struct pil_priv *priv = desc->priv;
Matt Wagantall19b48592013-02-27 10:18:41 -0800690 if (desc->ops->shutdown)
691 desc->ops->shutdown(desc);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700692 if (proxy_timeout_ms == 0 && desc->ops->proxy_unvote)
693 desc->ops->proxy_unvote(desc);
Matt Wagantall0aafa352012-05-14 16:40:41 -0700694 else
Stephen Boyd163f1c32012-06-29 13:20:20 -0700695 flush_delayed_work(&priv->proxy);
696}
697EXPORT_SYMBOL(pil_shutdown);
Matt Wagantall0aafa352012-05-14 16:40:41 -0700698
Stephen Boydb455f322012-11-27 19:00:01 -0800699static DEFINE_IDA(pil_ida);
700
Stephen Boyd163f1c32012-06-29 13:20:20 -0700701/**
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 */
710int pil_desc_init(struct pil_desc *desc)
711{
712 struct pil_priv *priv;
Seemanta Duttad21a7972013-03-05 12:16:17 -0800713 int ret;
Stephen Boydb455f322012-11-27 19:00:01 -0800714 void __iomem *addr;
Stephen Boyd06ce3962013-01-02 15:03:14 -0800715 char buf[sizeof(priv->info->name)];
Stephen Boyd163f1c32012-06-29 13:20:20 -0700716
717 /* Ignore users who don't make any sense */
Seemanta Duttad21a7972013-03-05 12:16:17 -0800718 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 Boyd163f1c32012-06-29 13:20:20 -0700723 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 Duttad21a7972013-03-05 12:16:17 -0800733 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 Boydb455f322012-11-27 19:00:01 -0800738 priv->info = (struct pil_image_info __iomem *)addr;
739
Stephen Boyd06ce3962013-01-02 15:03:14 -0800740 strncpy(buf, desc->name, sizeof(buf));
741 __iowrite32_copy(priv->info->name, buf, sizeof(buf) / 4);
Stephen Boydb455f322012-11-27 19:00:01 -0800742
Seemanta Duttad21a7972013-03-05 12:16:17 -0800743 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 Boyd163f1c32012-06-29 13:20:20 -0700756 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 Boydedff6cf2012-07-11 19:39:27 -0700759 INIT_LIST_HEAD(&priv->segs);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700760
761 return 0;
Seemanta Duttad21a7972013-03-05 12:16:17 -0800762err:
763 kfree(priv);
764 return ret;
Stephen Boyd163f1c32012-06-29 13:20:20 -0700765}
766EXPORT_SYMBOL(pil_desc_init);
767
768/**
769 * pil_desc_release() - Release a pil descriptor
770 * @desc: descriptor to free
771 */
772void pil_desc_release(struct pil_desc *desc)
773{
774 struct pil_priv *priv = desc->priv;
775
776 if (priv) {
Stephen Boydb455f322012-11-27 19:00:01 -0800777 ida_simple_remove(&pil_ida, priv->id);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700778 flush_delayed_work(&priv->proxy);
779 wake_lock_destroy(&priv->wlock);
780 }
781 desc->priv = NULL;
782 kfree(priv);
783}
784EXPORT_SYMBOL(pil_desc_release);
785
Stephen Boyd80bde032012-03-16 00:14:42 -0700786static 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
799static struct notifier_block pil_pm_notifier = {
800 .notifier_call = pil_pm_notify,
801};
802
Stephen Boyd6d67d252011-09-27 11:50:05 -0700803static int __init msm_pil_init(void)
804{
Stephen Boyd2db158c2012-07-26 21:47:17 -0700805 ion = msm_ion_client_create(UINT_MAX, "pil");
806 if (IS_ERR(ion)) /* Can't support relocatable images */
807 ion = NULL;
Stephen Boyd04148122012-06-29 18:18:12 -0700808 return register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700809}
Stephen Boyd2db158c2012-07-26 21:47:17 -0700810device_initcall(msm_pil_init);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700811
812static void __exit msm_pil_exit(void)
813{
Stephen Boyd80bde032012-03-16 00:14:42 -0700814 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd2db158c2012-07-26 21:47:17 -0700815 if (ion)
816 ion_client_destroy(ion);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700817}
818module_exit(msm_pil_exit);
819
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820MODULE_LICENSE("GPL v2");
821MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");