blob: 4e8674c3cdaf8e022cb22fd847cb7b55e5edbd62 [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#include "peripheral-loader.h"
Stephen Boyd10667772012-11-28 16:45:35 -080041#include "ramdump.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
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;
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 Boyd2db158c2012-07-26 21:47:17 -0700301 * Ensure the entry address lies within the image limits and if the image is
302 * relocatable ensure it lies within a relocatable segment.
Stephen Boyd3030c252012-08-08 17:24:05 -0700303 */
304static int pil_init_entry_addr(struct pil_priv *priv, const struct pil_mdt *mdt)
305{
306 struct pil_seg *seg;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700307 phys_addr_t entry = mdt->hdr.e_entry;
308 bool image_relocated = priv->region;
Stephen Boyd3030c252012-08-08 17:24:05 -0700309
Stephen Boyd2db158c2012-07-26 21:47:17 -0700310 if (image_relocated)
311 entry = pil_reloc(priv, entry);
312 priv->entry_addr = entry;
Stephen Boyd3030c252012-08-08 17:24:05 -0700313
314 if (priv->desc->flags & PIL_SKIP_ENTRY_CHECK)
315 return 0;
316
317 list_for_each_entry(seg, &priv->segs, list) {
Stephen Boyd2db158c2012-07-26 21:47:17 -0700318 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 Boyd3030c252012-08-08 17:24:05 -0700324 }
Stephen Boyd2db158c2012-07-26 21:47:17 -0700325 pil_err(priv->desc, "entry address %08zx not within range\n", entry);
Stephen Boyd3030c252012-08-08 17:24:05 -0700326 pil_dump_segs(priv);
327 return -EADDRNOTAVAIL;
328}
329
Stephen Boyd2db158c2012-07-26 21:47:17 -0700330static 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 Boyd6385e312012-12-12 11:17:04 -0800336 size_t size = max_addr - min_addr;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700337
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 Boyd379e7332012-08-08 18:04:21 -0700372static int pil_setup_region(struct pil_priv *priv, const struct pil_mdt *mdt)
373{
374 const struct elf32_phdr *phdr;
Stephen Boyd2db158c2012-07-26 21:47:17 -0700375 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 Boyd379e7332012-08-08 18:04:21 -0700379
Stephen Boyd2db158c2012-07-26 21:47:17 -0700380 min_addr_n = min_addr_r = (phys_addr_t)ULLONG_MAX;
381 max_addr_n = max_addr_r = 0;
Stephen Boyd379e7332012-08-08 18:04:21 -0700382
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 Boyd2db158c2012-07-26 21:47:17 -0700389 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 Boyd379e7332012-08-08 18:04:21 -0700407 }
408
Stephen Boyd6385e312012-12-12 11:17:04 -0800409 /*
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 Boyd2db158c2012-07-26 21:47:17 -0700416 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 Boyd379e7332012-08-08 18:04:21 -0700423
Stephen Boydb455f322012-11-27 19:00:01 -0800424 writeq(priv->region_start, &priv->info->start);
425 writel_relaxed(priv->region_end - priv->region_start,
426 &priv->info->size);
427
Stephen Boyd2db158c2012-07-26 21:47:17 -0700428 return ret;
Stephen Boyd379e7332012-08-08 18:04:21 -0700429}
430
Stephen Boydedff6cf2012-07-11 19:39:27 -0700431static 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
439static int pil_init_mmap(struct pil_desc *desc, const struct pil_mdt *mdt)
440{
Stephen Boyd3030c252012-08-08 17:24:05 -0700441 struct pil_priv *priv = desc->priv;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700442 const struct elf32_phdr *phdr;
443 struct pil_seg *seg;
Stephen Boyd379e7332012-08-08 18:04:21 -0700444 int i, ret;
445
446 ret = pil_setup_region(priv, mdt);
447 if (ret)
448 return ret;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700449
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 Boyd3030c252012-08-08 17:24:05 -0700463 return pil_init_entry_addr(priv, mdt);
Stephen Boydedff6cf2012-07-11 19:39:27 -0700464}
465
466static void pil_release_mmap(struct pil_desc *desc)
467{
468 struct pil_priv *priv = desc->priv;
469 struct pil_seg *p, *tmp;
470
Stephen Boydb455f322012-11-27 19:00:01 -0800471 writeq(0, &priv->info->start);
472 writel_relaxed(0, &priv->info->size);
473
Stephen Boyd2db158c2012-07-26 21:47:17 -0700474 if (priv->region)
475 ion_free(ion, priv->region);
Stephen Boyd3e29f102012-11-28 18:55:25 -0800476 priv->region = NULL;
Stephen Boydedff6cf2012-07-11 19:39:27 -0700477 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
485static 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 Boyd3f4da322011-08-30 01:03:23 -0700494 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
Stephen Boyd163f1c32012-06-29 13:20:20 -0700495 desc->name, num);
496 ret = request_firmware(&fw, fw_name, desc->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700498 pil_err(desc, "Failed to locate blob %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 return ret;
500 }
501
Stephen Boydedff6cf2012-07-11 19:39:27 -0700502 if (fw->size != seg->filesz) {
503 pil_err(desc, "Blob size %u doesn't match %lu\n",
504 fw->size, seg->filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 ret = -EPERM;
506 goto release_fw;
507 }
508 }
509
510 /* Load the segment into memory */
Stephen Boydedff6cf2012-07-11 19:39:27 -0700511 count = seg->filesz;
512 paddr = seg->paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 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 Boyd163f1c32012-06-29 13:20:20 -0700521 pil_err(desc, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 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 Boydedff6cf2012-07-11 19:39:27 -0700534 count = seg->sz - seg->filesz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 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 Boyd163f1c32012-06-29 13:20:20 -0700542 pil_err(desc, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 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 Boyd163f1c32012-06-29 13:20:20 -0700553 if (desc->ops->verify_blob) {
Stephen Boydedff6cf2012-07-11 19:39:27 -0700554 ret = desc->ops->verify_blob(desc, seg->paddr, seg->sz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800555 if (ret)
Stephen Boyd163f1c32012-06-29 13:20:20 -0700556 pil_err(desc, "Blob%u failed verification\n", num);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800557 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
559release_fw:
560 release_firmware(fw);
561 return ret;
562}
563
Stephen Boyd163f1c32012-06-29 13:20:20 -0700564/* Synchronize request_firmware() with suspend */
Stephen Boyd80bde032012-03-16 00:14:42 -0700565static DECLARE_RWSEM(pil_pm_rwsem);
566
Stephen Boyd163f1c32012-06-29 13:20:20 -0700567/**
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 */
573int pil_boot(struct pil_desc *desc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574{
Stephen Boydedff6cf2012-07-11 19:39:27 -0700575 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 char fw_name[30];
Stephen Boydedff6cf2012-07-11 19:39:27 -0700577 const struct pil_mdt *mdt;
578 const struct elf32_hdr *ehdr;
579 struct pil_seg *seg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 const struct firmware *fw;
Stephen Boyd379e7332012-08-08 18:04:21 -0700581 struct pil_priv *priv = desc->priv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
Stephen Boydedff6cf2012-07-11 19:39:27 -0700583 /* Reinitialize for new image */
584 pil_release_mmap(desc);
585
Stephen Boyd80bde032012-03-16 00:14:42 -0700586 down_read(&pil_pm_rwsem);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700587 snprintf(fw_name, sizeof(fw_name), "%s.mdt", desc->name);
588 ret = request_firmware(&fw, fw_name, desc->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700590 pil_err(desc, "Failed to locate %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 goto out;
592 }
593
594 if (fw->size < sizeof(*ehdr)) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700595 pil_err(desc, "Not big enough to be an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596 ret = -EIO;
597 goto release_fw;
598 }
599
Stephen Boydedff6cf2012-07-11 19:39:27 -0700600 mdt = (const struct pil_mdt *)fw->data;
601 ehdr = &mdt->hdr;
602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700604 pil_err(desc, "Not an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 ret = -EIO;
606 goto release_fw;
607 }
608
609 if (ehdr->e_phnum == 0) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700610 pil_err(desc, "No loadable segments\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 ret = -EIO;
612 goto release_fw;
613 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700614 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
615 sizeof(struct elf32_hdr) > fw->size) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700616 pil_err(desc, "Program headers not within mdt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 ret = -EIO;
618 goto release_fw;
619 }
620
Stephen Boydedff6cf2012-07-11 19:39:27 -0700621 ret = pil_init_mmap(desc, mdt);
622 if (ret)
623 goto release_fw;
624
Stephen Boyd3030c252012-08-08 17:24:05 -0700625 if (desc->ops->init_image)
626 ret = desc->ops->init_image(desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700628 pil_err(desc, "Invalid firmware metadata\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629 goto release_fw;
630 }
631
Stephen Boyd379e7332012-08-08 18:04:21 -0700632 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 Boydedff6cf2012-07-11 19:39:27 -0700640 list_for_each_entry(seg, &desc->priv->segs, list) {
641 ret = pil_load_seg(desc, seg);
642 if (ret)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 goto release_fw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 }
645
Stephen Boyd163f1c32012-06-29 13:20:20 -0700646 ret = pil_proxy_vote(desc);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700647 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700648 pil_err(desc, "Failed to proxy vote\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700649 goto release_fw;
650 }
651
Stephen Boyd163f1c32012-06-29 13:20:20 -0700652 ret = desc->ops->auth_and_reset(desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 if (ret) {
Stephen Boyd163f1c32012-06-29 13:20:20 -0700654 pil_err(desc, "Failed to bring out of reset\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700655 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 }
Stephen Boyd163f1c32012-06-29 13:20:20 -0700657 pil_info(desc, "Brought out of reset\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700658err_boot:
Seemanta Dutta78f43192013-03-04 18:29:43 -0800659 pil_proxy_unvote(desc, ret);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660release_fw:
661 release_firmware(fw);
662out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700663 up_read(&pil_pm_rwsem);
Stephen Boydedff6cf2012-07-11 19:39:27 -0700664 if (ret)
665 pil_release_mmap(desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 return ret;
667}
Stephen Boyd163f1c32012-06-29 13:20:20 -0700668EXPORT_SYMBOL(pil_boot);
669
Stephen Boyd163f1c32012-06-29 13:20:20 -0700670/**
671 * pil_shutdown() - Shutdown a peripheral
672 * @desc: descriptor from pil_desc_init()
673 */
674void pil_shutdown(struct pil_desc *desc)
Stephen Boyd36974ec2012-03-22 01:30:59 -0700675{
Stephen Boyd163f1c32012-06-29 13:20:20 -0700676 struct pil_priv *priv = desc->priv;
Matt Wagantall19b48592013-02-27 10:18:41 -0800677 if (desc->ops->shutdown)
678 desc->ops->shutdown(desc);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700679 if (proxy_timeout_ms == 0 && desc->ops->proxy_unvote)
680 desc->ops->proxy_unvote(desc);
Matt Wagantall0aafa352012-05-14 16:40:41 -0700681 else
Stephen Boyd163f1c32012-06-29 13:20:20 -0700682 flush_delayed_work(&priv->proxy);
683}
684EXPORT_SYMBOL(pil_shutdown);
Matt Wagantall0aafa352012-05-14 16:40:41 -0700685
Stephen Boydb455f322012-11-27 19:00:01 -0800686static DEFINE_IDA(pil_ida);
687
Stephen Boyd163f1c32012-06-29 13:20:20 -0700688/**
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 */
697int pil_desc_init(struct pil_desc *desc)
698{
699 struct pil_priv *priv;
Seemanta Duttad21a7972013-03-05 12:16:17 -0800700 int ret;
Stephen Boydb455f322012-11-27 19:00:01 -0800701 void __iomem *addr;
Stephen Boyd06ce3962013-01-02 15:03:14 -0800702 char buf[sizeof(priv->info->name)];
Stephen Boyd163f1c32012-06-29 13:20:20 -0700703
704 /* Ignore users who don't make any sense */
Seemanta Duttad21a7972013-03-05 12:16:17 -0800705 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 Boyd163f1c32012-06-29 13:20:20 -0700710 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 Duttad21a7972013-03-05 12:16:17 -0800720 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 Boydb455f322012-11-27 19:00:01 -0800725 priv->info = (struct pil_image_info __iomem *)addr;
726
Stephen Boyd06ce3962013-01-02 15:03:14 -0800727 strncpy(buf, desc->name, sizeof(buf));
728 __iowrite32_copy(priv->info->name, buf, sizeof(buf) / 4);
Stephen Boydb455f322012-11-27 19:00:01 -0800729
Seemanta Duttad21a7972013-03-05 12:16:17 -0800730 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 Boyd163f1c32012-06-29 13:20:20 -0700743 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 Boydedff6cf2012-07-11 19:39:27 -0700746 INIT_LIST_HEAD(&priv->segs);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700747
748 return 0;
Seemanta Duttad21a7972013-03-05 12:16:17 -0800749err:
750 kfree(priv);
751 return ret;
Stephen Boyd163f1c32012-06-29 13:20:20 -0700752}
753EXPORT_SYMBOL(pil_desc_init);
754
755/**
756 * pil_desc_release() - Release a pil descriptor
757 * @desc: descriptor to free
758 */
759void pil_desc_release(struct pil_desc *desc)
760{
761 struct pil_priv *priv = desc->priv;
762
763 if (priv) {
Stephen Boydb455f322012-11-27 19:00:01 -0800764 ida_simple_remove(&pil_ida, priv->id);
Stephen Boyd163f1c32012-06-29 13:20:20 -0700765 flush_delayed_work(&priv->proxy);
766 wake_lock_destroy(&priv->wlock);
767 }
768 desc->priv = NULL;
769 kfree(priv);
770}
771EXPORT_SYMBOL(pil_desc_release);
772
Stephen Boyd80bde032012-03-16 00:14:42 -0700773static 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
786static struct notifier_block pil_pm_notifier = {
787 .notifier_call = pil_pm_notify,
788};
789
Stephen Boyd6d67d252011-09-27 11:50:05 -0700790static int __init msm_pil_init(void)
791{
Stephen Boyd2db158c2012-07-26 21:47:17 -0700792 ion = msm_ion_client_create(UINT_MAX, "pil");
793 if (IS_ERR(ion)) /* Can't support relocatable images */
794 ion = NULL;
Stephen Boyd04148122012-06-29 18:18:12 -0700795 return register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700796}
Stephen Boyd2db158c2012-07-26 21:47:17 -0700797device_initcall(msm_pil_init);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700798
799static void __exit msm_pil_exit(void)
800{
Stephen Boyd80bde032012-03-16 00:14:42 -0700801 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd2db158c2012-07-26 21:47:17 -0700802 if (ion)
803 ion_client_destroy(ion);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700804}
805module_exit(msm_pil_exit);
806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807MODULE_LICENSE("GPL v2");
808MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");