Matt Wagantall | 39f90cb | 2012-02-08 14:09:11 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. 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> |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 15 | #include <linux/device.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 16 | #include <linux/firmware.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/debugfs.h> |
| 19 | #include <linux/elf.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/memblock.h> |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 23 | #include <linux/atomic.h> |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 24 | #include <linux/suspend.h> |
| 25 | #include <linux/rwsem.h> |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 26 | #include <linux/sysfs.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/setup.h> |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 30 | #include <mach/peripheral-loader.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | |
| 32 | #include "peripheral-loader.h" |
| 33 | |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 34 | enum pil_state { |
| 35 | PIL_OFFLINE, |
| 36 | PIL_ONLINE, |
| 37 | }; |
| 38 | |
| 39 | static const char *pil_states[] = { |
| 40 | [PIL_OFFLINE] = "OFFLINE", |
| 41 | [PIL_ONLINE] = "ONLINE", |
| 42 | }; |
| 43 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 44 | struct pil_device { |
| 45 | struct pil_desc *desc; |
| 46 | int count; |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 47 | enum pil_state state; |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 48 | struct mutex lock; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 49 | struct device dev; |
| 50 | struct module *owner; |
| 51 | #ifdef CONFIG_DEBUG_FS |
| 52 | struct dentry *dentry; |
| 53 | #endif |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 56 | #define to_pil_device(d) container_of(d, struct pil_device, dev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 58 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 59 | char *buf) |
| 60 | { |
| 61 | return snprintf(buf, PAGE_SIZE, "%s\n", to_pil_device(dev)->desc->name); |
| 62 | } |
| 63 | |
| 64 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 65 | char *buf) |
| 66 | { |
| 67 | enum pil_state state = to_pil_device(dev)->state; |
| 68 | return snprintf(buf, PAGE_SIZE, "%s\n", pil_states[state]); |
| 69 | } |
| 70 | |
| 71 | static struct device_attribute pil_attrs[] = { |
| 72 | __ATTR_RO(name), |
| 73 | __ATTR_RO(state), |
| 74 | { }, |
| 75 | }; |
| 76 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 77 | struct bus_type pil_bus_type = { |
| 78 | .name = "pil", |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 79 | .dev_attrs = pil_attrs, |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static int __find_peripheral(struct device *dev, void *data) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 83 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 84 | struct pil_device *pdev = to_pil_device(dev); |
| 85 | return !strncmp(pdev->desc->name, data, INT_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static struct pil_device *find_peripheral(const char *str) |
| 89 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 90 | struct device *dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | |
| 92 | if (!str) |
| 93 | return NULL; |
| 94 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 95 | dev = bus_find_device(&pil_bus_type, NULL, (void *)str, |
| 96 | __find_peripheral); |
| 97 | return dev ? to_pil_device(dev) : NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | #define IOMAP_SIZE SZ_4M |
| 101 | |
| 102 | static int load_segment(const struct elf32_phdr *phdr, unsigned num, |
| 103 | struct pil_device *pil) |
| 104 | { |
| 105 | int ret, count, paddr; |
| 106 | char fw_name[30]; |
| 107 | const struct firmware *fw = NULL; |
| 108 | const u8 *data; |
| 109 | |
| 110 | if (memblock_is_region_memory(phdr->p_paddr, phdr->p_memsz)) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 111 | dev_err(&pil->dev, "Kernel memory would be overwritten"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | return -EPERM; |
| 113 | } |
| 114 | |
| 115 | if (phdr->p_filesz) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 116 | snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", |
| 117 | pil->desc->name, num); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 118 | ret = request_firmware(&fw, fw_name, &pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 119 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 120 | dev_err(&pil->dev, "Failed to locate blob %s\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 121 | fw_name); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | if (fw->size != phdr->p_filesz) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 126 | dev_err(&pil->dev, "Blob size %u doesn't match %u\n", |
| 127 | fw->size, phdr->p_filesz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | ret = -EPERM; |
| 129 | goto release_fw; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Load the segment into memory */ |
| 134 | count = phdr->p_filesz; |
| 135 | paddr = phdr->p_paddr; |
| 136 | data = fw ? fw->data : NULL; |
| 137 | while (count > 0) { |
| 138 | int size; |
| 139 | u8 __iomem *buf; |
| 140 | |
| 141 | size = min_t(size_t, IOMAP_SIZE, count); |
| 142 | buf = ioremap(paddr, size); |
| 143 | if (!buf) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 144 | dev_err(&pil->dev, "Failed to map memory\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | ret = -ENOMEM; |
| 146 | goto release_fw; |
| 147 | } |
| 148 | memcpy(buf, data, size); |
| 149 | iounmap(buf); |
| 150 | |
| 151 | count -= size; |
| 152 | paddr += size; |
| 153 | data += size; |
| 154 | } |
| 155 | |
| 156 | /* Zero out trailing memory */ |
| 157 | count = phdr->p_memsz - phdr->p_filesz; |
| 158 | while (count > 0) { |
| 159 | int size; |
| 160 | u8 __iomem *buf; |
| 161 | |
| 162 | size = min_t(size_t, IOMAP_SIZE, count); |
| 163 | buf = ioremap(paddr, size); |
| 164 | if (!buf) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 165 | dev_err(&pil->dev, "Failed to map memory\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | ret = -ENOMEM; |
| 167 | goto release_fw; |
| 168 | } |
| 169 | memset(buf, 0, size); |
| 170 | iounmap(buf); |
| 171 | |
| 172 | count -= size; |
| 173 | paddr += size; |
| 174 | } |
| 175 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 176 | ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr, |
| 177 | phdr->p_memsz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 178 | if (ret) |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 179 | dev_err(&pil->dev, "Blob %u failed verification\n", num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 180 | |
| 181 | release_fw: |
| 182 | release_firmware(fw); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | #define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24)) |
| 187 | |
| 188 | static int segment_is_loadable(const struct elf32_phdr *p) |
| 189 | { |
| 190 | return (p->p_type & PT_LOAD) && !segment_is_hash(p->p_flags); |
| 191 | } |
| 192 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 193 | /* Sychronize request_firmware() with suspend */ |
| 194 | static DECLARE_RWSEM(pil_pm_rwsem); |
| 195 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | static int load_image(struct pil_device *pil) |
| 197 | { |
| 198 | int i, ret; |
| 199 | char fw_name[30]; |
| 200 | struct elf32_hdr *ehdr; |
| 201 | const struct elf32_phdr *phdr; |
| 202 | const struct firmware *fw; |
| 203 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 204 | down_read(&pil_pm_rwsem); |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 205 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 206 | ret = request_firmware(&fw, fw_name, &pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 207 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 208 | dev_err(&pil->dev, "Failed to locate %s\n", fw_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | goto out; |
| 210 | } |
| 211 | |
| 212 | if (fw->size < sizeof(*ehdr)) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 213 | dev_err(&pil->dev, "Not big enough to be an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 214 | ret = -EIO; |
| 215 | goto release_fw; |
| 216 | } |
| 217 | |
| 218 | ehdr = (struct elf32_hdr *)fw->data; |
| 219 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 220 | dev_err(&pil->dev, "Not an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | ret = -EIO; |
| 222 | goto release_fw; |
| 223 | } |
| 224 | |
| 225 | if (ehdr->e_phnum == 0) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 226 | dev_err(&pil->dev, "No loadable segments\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 227 | ret = -EIO; |
| 228 | goto release_fw; |
| 229 | } |
Stephen Boyd | 96a9f90 | 2011-07-18 18:43:00 -0700 | [diff] [blame] | 230 | if (sizeof(struct elf32_phdr) * ehdr->e_phnum + |
| 231 | sizeof(struct elf32_hdr) > fw->size) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 232 | dev_err(&pil->dev, "Program headers not within mdt\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 233 | ret = -EIO; |
| 234 | goto release_fw; |
| 235 | } |
| 236 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 237 | ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 238 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 239 | dev_err(&pil->dev, "Invalid firmware metadata\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | goto release_fw; |
| 241 | } |
| 242 | |
Stephen Boyd | c9753e1 | 2011-07-13 17:58:48 -0700 | [diff] [blame] | 243 | phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 244 | for (i = 0; i < ehdr->e_phnum; i++, phdr++) { |
| 245 | if (!segment_is_loadable(phdr)) |
| 246 | continue; |
| 247 | |
| 248 | ret = load_segment(phdr, i, pil); |
| 249 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 250 | dev_err(&pil->dev, "Failed to load segment %d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 251 | i); |
| 252 | goto release_fw; |
| 253 | } |
| 254 | } |
| 255 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 256 | ret = pil->desc->ops->auth_and_reset(pil->desc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 258 | dev_err(&pil->dev, "Failed to bring out of reset\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | goto release_fw; |
| 260 | } |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 261 | dev_info(&pil->dev, "brought %s out of reset\n", pil->desc->name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 262 | |
| 263 | release_fw: |
| 264 | release_firmware(fw); |
| 265 | out: |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 266 | up_read(&pil_pm_rwsem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 267 | return ret; |
| 268 | } |
| 269 | |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 270 | static void pil_set_state(struct pil_device *pil, enum pil_state state) |
| 271 | { |
| 272 | if (pil->state != state) { |
| 273 | pil->state = state; |
| 274 | sysfs_notify(&pil->dev.kobj, NULL, "state"); |
| 275 | } |
| 276 | } |
| 277 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | /** |
| 279 | * pil_get() - Load a peripheral into memory and take it out of reset |
| 280 | * @name: pointer to a string containing the name of the peripheral to load |
| 281 | * |
| 282 | * This function returns a pointer if it succeeds. If an error occurs an |
| 283 | * ERR_PTR is returned. |
| 284 | * |
| 285 | * If PIL is not enabled in the kernel, the value %NULL will be returned. |
| 286 | */ |
| 287 | void *pil_get(const char *name) |
| 288 | { |
| 289 | int ret; |
| 290 | struct pil_device *pil; |
| 291 | struct pil_device *pil_d; |
| 292 | void *retval; |
| 293 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 294 | if (!name) |
| 295 | return NULL; |
| 296 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 297 | pil = retval = find_peripheral(name); |
| 298 | if (!pil) |
| 299 | return ERR_PTR(-ENODEV); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 300 | if (!try_module_get(pil->owner)) { |
| 301 | put_device(&pil->dev); |
| 302 | return ERR_PTR(-ENODEV); |
| 303 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 305 | pil_d = pil_get(pil->desc->depends_on); |
| 306 | if (IS_ERR(pil_d)) { |
| 307 | retval = pil_d; |
| 308 | goto err_depends; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | mutex_lock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 312 | if (!pil->count++) { |
| 313 | ret = load_image(pil); |
| 314 | if (ret) { |
| 315 | retval = ERR_PTR(ret); |
| 316 | goto err_load; |
| 317 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 318 | } |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 319 | pil_set_state(pil, PIL_ONLINE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 320 | mutex_unlock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 321 | out: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 322 | return retval; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 323 | err_load: |
| 324 | mutex_unlock(&pil->lock); |
| 325 | pil_put(pil_d); |
| 326 | err_depends: |
| 327 | put_device(&pil->dev); |
| 328 | module_put(pil->owner); |
| 329 | goto out; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 330 | } |
| 331 | EXPORT_SYMBOL(pil_get); |
| 332 | |
| 333 | /** |
| 334 | * pil_put() - Inform PIL the peripheral no longer needs to be active |
| 335 | * @peripheral_handle: pointer from a previous call to pil_get() |
| 336 | * |
| 337 | * This doesn't imply that a peripheral is shutdown or in reset since another |
| 338 | * driver could be using the peripheral. |
| 339 | */ |
| 340 | void pil_put(void *peripheral_handle) |
| 341 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 342 | struct pil_device *pil_d, *pil = peripheral_handle; |
| 343 | |
| 344 | if (IS_ERR_OR_NULL(pil)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 345 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 346 | |
| 347 | mutex_lock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 348 | if (WARN(!pil->count, "%s: Reference count mismatch\n", __func__)) |
| 349 | goto err_out; |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 350 | if (!--pil->count) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 351 | pil->desc->ops->shutdown(pil->desc); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 352 | pil_set_state(pil, PIL_OFFLINE); |
| 353 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 354 | mutex_unlock(&pil->lock); |
| 355 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 356 | pil_d = find_peripheral(pil->desc->depends_on); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 357 | module_put(pil->owner); |
| 358 | if (pil_d) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 359 | pil_put(pil_d); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 360 | put_device(&pil_d->dev); |
| 361 | } |
| 362 | put_device(&pil->dev); |
| 363 | return; |
| 364 | err_out: |
| 365 | mutex_unlock(&pil->lock); |
| 366 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 367 | } |
| 368 | EXPORT_SYMBOL(pil_put); |
| 369 | |
| 370 | void pil_force_shutdown(const char *name) |
| 371 | { |
| 372 | struct pil_device *pil; |
| 373 | |
| 374 | pil = find_peripheral(name); |
| 375 | if (!pil) |
| 376 | return; |
| 377 | |
| 378 | mutex_lock(&pil->lock); |
| 379 | if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__)) |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 380 | pil->desc->ops->shutdown(pil->desc); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 381 | pil_set_state(pil, PIL_OFFLINE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | mutex_unlock(&pil->lock); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 383 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 384 | put_device(&pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL(pil_force_shutdown); |
| 387 | |
| 388 | int pil_force_boot(const char *name) |
| 389 | { |
| 390 | int ret = -EINVAL; |
| 391 | struct pil_device *pil; |
| 392 | |
| 393 | pil = find_peripheral(name); |
| 394 | if (!pil) |
| 395 | return -EINVAL; |
| 396 | |
| 397 | mutex_lock(&pil->lock); |
| 398 | if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__)) |
| 399 | ret = load_image(pil); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame^] | 400 | if (!ret) |
| 401 | pil_set_state(pil, PIL_ONLINE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | mutex_unlock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 403 | put_device(&pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 404 | |
| 405 | return ret; |
| 406 | } |
| 407 | EXPORT_SYMBOL(pil_force_boot); |
| 408 | |
| 409 | #ifdef CONFIG_DEBUG_FS |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 410 | static int msm_pil_debugfs_open(struct inode *inode, struct file *filp) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | { |
| 412 | filp->private_data = inode->i_private; |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf, |
| 417 | size_t cnt, loff_t *ppos) |
| 418 | { |
| 419 | int r; |
| 420 | char buf[40]; |
| 421 | struct pil_device *pil = filp->private_data; |
| 422 | |
| 423 | mutex_lock(&pil->lock); |
| 424 | r = snprintf(buf, sizeof(buf), "%d\n", pil->count); |
| 425 | mutex_unlock(&pil->lock); |
| 426 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 427 | } |
| 428 | |
| 429 | static ssize_t msm_pil_debugfs_write(struct file *filp, |
| 430 | const char __user *ubuf, size_t cnt, loff_t *ppos) |
| 431 | { |
| 432 | struct pil_device *pil = filp->private_data; |
| 433 | char buf[4]; |
| 434 | |
| 435 | if (cnt > sizeof(buf)) |
| 436 | return -EINVAL; |
| 437 | |
| 438 | if (copy_from_user(&buf, ubuf, cnt)) |
| 439 | return -EFAULT; |
| 440 | |
| 441 | if (!strncmp(buf, "get", 3)) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 442 | if (IS_ERR(pil_get(pil->desc->name))) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | return -EIO; |
| 444 | } else if (!strncmp(buf, "put", 3)) |
| 445 | pil_put(pil); |
| 446 | else |
| 447 | return -EINVAL; |
| 448 | |
| 449 | return cnt; |
| 450 | } |
| 451 | |
| 452 | static const struct file_operations msm_pil_debugfs_fops = { |
| 453 | .open = msm_pil_debugfs_open, |
| 454 | .read = msm_pil_debugfs_read, |
| 455 | .write = msm_pil_debugfs_write, |
| 456 | }; |
| 457 | |
| 458 | static struct dentry *pil_base_dir; |
| 459 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 460 | static int __init msm_pil_debugfs_init(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 461 | { |
| 462 | pil_base_dir = debugfs_create_dir("pil", NULL); |
| 463 | if (!pil_base_dir) { |
| 464 | pil_base_dir = NULL; |
| 465 | return -ENOMEM; |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 470 | |
| 471 | static void __exit msm_pil_debugfs_exit(void) |
| 472 | { |
| 473 | debugfs_remove_recursive(pil_base_dir); |
| 474 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 475 | |
| 476 | static int msm_pil_debugfs_add(struct pil_device *pil) |
| 477 | { |
| 478 | if (!pil_base_dir) |
| 479 | return -ENOMEM; |
| 480 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 481 | pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR, |
| 482 | pil_base_dir, pil, &msm_pil_debugfs_fops); |
| 483 | return !pil->dentry ? -ENOMEM : 0; |
| 484 | } |
| 485 | |
| 486 | static void msm_pil_debugfs_remove(struct pil_device *pil) |
| 487 | { |
| 488 | debugfs_remove(pil->dentry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 489 | } |
| 490 | #else |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 491 | static int __init msm_pil_debugfs_init(void) { return 0; }; |
| 492 | static void __exit msm_pil_debugfs_exit(void) { return 0; }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 493 | static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; } |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 494 | static void msm_pil_debugfs_remove(struct pil_device *pil) { } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 495 | #endif |
| 496 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 497 | static int __msm_pil_shutdown(struct device *dev, void *data) |
| 498 | { |
| 499 | struct pil_device *pil = to_pil_device(dev); |
| 500 | pil->desc->ops->shutdown(pil->desc); |
| 501 | return 0; |
| 502 | } |
| 503 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 504 | static int msm_pil_shutdown_at_boot(void) |
| 505 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 506 | return bus_for_each_dev(&pil_bus_type, NULL, NULL, __msm_pil_shutdown); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 507 | } |
| 508 | late_initcall(msm_pil_shutdown_at_boot); |
| 509 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 510 | static void pil_device_release(struct device *dev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 511 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 512 | struct pil_device *pil = to_pil_device(dev); |
| 513 | mutex_destroy(&pil->lock); |
| 514 | kfree(pil); |
| 515 | } |
| 516 | |
| 517 | struct pil_device *msm_pil_register(struct pil_desc *desc) |
| 518 | { |
| 519 | int err; |
| 520 | static atomic_t pil_count = ATOMIC_INIT(-1); |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 521 | struct pil_device *pil = kzalloc(sizeof(*pil), GFP_KERNEL); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 522 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 523 | if (!pil) |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 524 | return ERR_PTR(-ENOMEM); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | |
| 526 | mutex_init(&pil->lock); |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 527 | pil->desc = desc; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 528 | pil->owner = desc->owner; |
| 529 | pil->dev.parent = desc->dev; |
| 530 | pil->dev.bus = &pil_bus_type; |
| 531 | pil->dev.release = pil_device_release; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 532 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 533 | dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count)); |
| 534 | err = device_register(&pil->dev); |
| 535 | if (err) { |
| 536 | put_device(&pil->dev); |
| 537 | mutex_destroy(&pil->lock); |
| 538 | kfree(pil); |
| 539 | return ERR_PTR(err); |
| 540 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 541 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 542 | err = msm_pil_debugfs_add(pil); |
| 543 | if (err) { |
| 544 | device_unregister(&pil->dev); |
| 545 | return ERR_PTR(err); |
| 546 | } |
| 547 | |
| 548 | return pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 549 | } |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 550 | EXPORT_SYMBOL(msm_pil_register); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 552 | void msm_pil_unregister(struct pil_device *pil) |
| 553 | { |
| 554 | if (IS_ERR_OR_NULL(pil)) |
| 555 | return; |
| 556 | |
| 557 | if (get_device(&pil->dev)) { |
| 558 | mutex_lock(&pil->lock); |
| 559 | WARN_ON(pil->count); |
| 560 | msm_pil_debugfs_remove(pil); |
| 561 | device_unregister(&pil->dev); |
| 562 | mutex_unlock(&pil->lock); |
| 563 | put_device(&pil->dev); |
| 564 | } |
| 565 | } |
| 566 | EXPORT_SYMBOL(msm_pil_unregister); |
| 567 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 568 | static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p) |
| 569 | { |
| 570 | switch (event) { |
| 571 | case PM_SUSPEND_PREPARE: |
| 572 | down_write(&pil_pm_rwsem); |
| 573 | break; |
| 574 | case PM_POST_SUSPEND: |
| 575 | up_write(&pil_pm_rwsem); |
| 576 | break; |
| 577 | } |
| 578 | return NOTIFY_DONE; |
| 579 | } |
| 580 | |
| 581 | static struct notifier_block pil_pm_notifier = { |
| 582 | .notifier_call = pil_pm_notify, |
| 583 | }; |
| 584 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 585 | static int __init msm_pil_init(void) |
| 586 | { |
| 587 | int ret = msm_pil_debugfs_init(); |
| 588 | if (ret) |
| 589 | return ret; |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 590 | register_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 591 | return bus_register(&pil_bus_type); |
| 592 | } |
| 593 | subsys_initcall(msm_pil_init); |
| 594 | |
| 595 | static void __exit msm_pil_exit(void) |
| 596 | { |
| 597 | bus_unregister(&pil_bus_type); |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 598 | unregister_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 599 | msm_pil_debugfs_exit(); |
| 600 | } |
| 601 | module_exit(msm_pil_exit); |
| 602 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 603 | MODULE_LICENSE("GPL v2"); |
| 604 | MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset"); |