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 | { |
Stephen Boyd | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 105 | int ret = 0, count, paddr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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 | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 176 | if (pil->desc->ops->verify_blob) { |
| 177 | ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr, |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 178 | phdr->p_memsz); |
Stephen Boyd | b0f1f80 | 2012-02-03 11:28:08 -0800 | [diff] [blame] | 179 | if (ret) |
| 180 | dev_err(&pil->dev, "Blob%u failed verification\n", num); |
| 181 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | |
| 183 | release_fw: |
| 184 | release_firmware(fw); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | #define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24)) |
| 189 | |
| 190 | static int segment_is_loadable(const struct elf32_phdr *p) |
| 191 | { |
| 192 | return (p->p_type & PT_LOAD) && !segment_is_hash(p->p_flags); |
| 193 | } |
| 194 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 195 | /* Sychronize request_firmware() with suspend */ |
| 196 | static DECLARE_RWSEM(pil_pm_rwsem); |
| 197 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | static int load_image(struct pil_device *pil) |
| 199 | { |
| 200 | int i, ret; |
| 201 | char fw_name[30]; |
| 202 | struct elf32_hdr *ehdr; |
| 203 | const struct elf32_phdr *phdr; |
| 204 | const struct firmware *fw; |
| 205 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 206 | down_read(&pil_pm_rwsem); |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 207 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 208 | ret = request_firmware(&fw, fw_name, &pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 210 | dev_err(&pil->dev, "Failed to locate %s\n", fw_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | if (fw->size < sizeof(*ehdr)) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 215 | 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] | 216 | ret = -EIO; |
| 217 | goto release_fw; |
| 218 | } |
| 219 | |
| 220 | ehdr = (struct elf32_hdr *)fw->data; |
| 221 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 222 | dev_err(&pil->dev, "Not an elf header\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | ret = -EIO; |
| 224 | goto release_fw; |
| 225 | } |
| 226 | |
| 227 | if (ehdr->e_phnum == 0) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 228 | dev_err(&pil->dev, "No loadable segments\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | ret = -EIO; |
| 230 | goto release_fw; |
| 231 | } |
Stephen Boyd | 96a9f90 | 2011-07-18 18:43:00 -0700 | [diff] [blame] | 232 | if (sizeof(struct elf32_phdr) * ehdr->e_phnum + |
| 233 | sizeof(struct elf32_hdr) > fw->size) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 234 | dev_err(&pil->dev, "Program headers not within mdt\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | ret = -EIO; |
| 236 | goto release_fw; |
| 237 | } |
| 238 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 239 | ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 241 | dev_err(&pil->dev, "Invalid firmware metadata\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 242 | goto release_fw; |
| 243 | } |
| 244 | |
Stephen Boyd | c9753e1 | 2011-07-13 17:58:48 -0700 | [diff] [blame] | 245 | phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 246 | for (i = 0; i < ehdr->e_phnum; i++, phdr++) { |
| 247 | if (!segment_is_loadable(phdr)) |
| 248 | continue; |
| 249 | |
| 250 | ret = load_segment(phdr, i, pil); |
| 251 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 252 | dev_err(&pil->dev, "Failed to load segment %d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 253 | i); |
| 254 | goto release_fw; |
| 255 | } |
| 256 | } |
| 257 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 258 | ret = pil->desc->ops->auth_and_reset(pil->desc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | if (ret) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 260 | dev_err(&pil->dev, "Failed to bring out of reset\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 261 | goto release_fw; |
| 262 | } |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 263 | 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] | 264 | |
| 265 | release_fw: |
| 266 | release_firmware(fw); |
| 267 | out: |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 268 | up_read(&pil_pm_rwsem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | return ret; |
| 270 | } |
| 271 | |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 272 | static void pil_set_state(struct pil_device *pil, enum pil_state state) |
| 273 | { |
| 274 | if (pil->state != state) { |
| 275 | pil->state = state; |
| 276 | sysfs_notify(&pil->dev.kobj, NULL, "state"); |
| 277 | } |
| 278 | } |
| 279 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | /** |
| 281 | * pil_get() - Load a peripheral into memory and take it out of reset |
| 282 | * @name: pointer to a string containing the name of the peripheral to load |
| 283 | * |
| 284 | * This function returns a pointer if it succeeds. If an error occurs an |
| 285 | * ERR_PTR is returned. |
| 286 | * |
| 287 | * If PIL is not enabled in the kernel, the value %NULL will be returned. |
| 288 | */ |
| 289 | void *pil_get(const char *name) |
| 290 | { |
| 291 | int ret; |
| 292 | struct pil_device *pil; |
| 293 | struct pil_device *pil_d; |
| 294 | void *retval; |
| 295 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 296 | if (!name) |
| 297 | return NULL; |
| 298 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | pil = retval = find_peripheral(name); |
| 300 | if (!pil) |
| 301 | return ERR_PTR(-ENODEV); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 302 | if (!try_module_get(pil->owner)) { |
| 303 | put_device(&pil->dev); |
| 304 | return ERR_PTR(-ENODEV); |
| 305 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 306 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 307 | pil_d = pil_get(pil->desc->depends_on); |
| 308 | if (IS_ERR(pil_d)) { |
| 309 | retval = pil_d; |
| 310 | goto err_depends; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | mutex_lock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 314 | if (!pil->count++) { |
| 315 | ret = load_image(pil); |
| 316 | if (ret) { |
| 317 | retval = ERR_PTR(ret); |
| 318 | goto err_load; |
| 319 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 320 | } |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 321 | pil_set_state(pil, PIL_ONLINE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 322 | mutex_unlock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 323 | out: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 324 | return retval; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 325 | err_load: |
| 326 | mutex_unlock(&pil->lock); |
| 327 | pil_put(pil_d); |
| 328 | err_depends: |
| 329 | put_device(&pil->dev); |
| 330 | module_put(pil->owner); |
| 331 | goto out; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 332 | } |
| 333 | EXPORT_SYMBOL(pil_get); |
| 334 | |
| 335 | /** |
| 336 | * pil_put() - Inform PIL the peripheral no longer needs to be active |
| 337 | * @peripheral_handle: pointer from a previous call to pil_get() |
| 338 | * |
| 339 | * This doesn't imply that a peripheral is shutdown or in reset since another |
| 340 | * driver could be using the peripheral. |
| 341 | */ |
| 342 | void pil_put(void *peripheral_handle) |
| 343 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 344 | struct pil_device *pil_d, *pil = peripheral_handle; |
| 345 | |
| 346 | if (IS_ERR_OR_NULL(pil)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 348 | |
| 349 | mutex_lock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 350 | if (WARN(!pil->count, "%s: Reference count mismatch\n", __func__)) |
| 351 | goto err_out; |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 352 | if (!--pil->count) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 353 | pil->desc->ops->shutdown(pil->desc); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 354 | pil_set_state(pil, PIL_OFFLINE); |
| 355 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 356 | mutex_unlock(&pil->lock); |
| 357 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 358 | pil_d = find_peripheral(pil->desc->depends_on); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 359 | module_put(pil->owner); |
| 360 | if (pil_d) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 361 | pil_put(pil_d); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 362 | put_device(&pil_d->dev); |
| 363 | } |
| 364 | put_device(&pil->dev); |
| 365 | return; |
| 366 | err_out: |
| 367 | mutex_unlock(&pil->lock); |
| 368 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 369 | } |
| 370 | EXPORT_SYMBOL(pil_put); |
| 371 | |
| 372 | void pil_force_shutdown(const char *name) |
| 373 | { |
| 374 | struct pil_device *pil; |
| 375 | |
| 376 | pil = find_peripheral(name); |
Stephen Boyd | 01c02c1 | 2012-03-14 10:12:26 -0700 | [diff] [blame] | 377 | if (!pil) { |
| 378 | pr_err("%s: Couldn't find %s\n", __func__, name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 379 | return; |
Stephen Boyd | 01c02c1 | 2012-03-14 10:12:26 -0700 | [diff] [blame] | 380 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 381 | |
| 382 | mutex_lock(&pil->lock); |
| 383 | if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__)) |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 384 | pil->desc->ops->shutdown(pil->desc); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 385 | pil_set_state(pil, PIL_OFFLINE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 386 | mutex_unlock(&pil->lock); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 387 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 388 | put_device(&pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 389 | } |
| 390 | EXPORT_SYMBOL(pil_force_shutdown); |
| 391 | |
| 392 | int pil_force_boot(const char *name) |
| 393 | { |
| 394 | int ret = -EINVAL; |
| 395 | struct pil_device *pil; |
| 396 | |
| 397 | pil = find_peripheral(name); |
Stephen Boyd | 01c02c1 | 2012-03-14 10:12:26 -0700 | [diff] [blame] | 398 | if (!pil) { |
| 399 | pr_err("%s: Couldn't find %s\n", __func__, name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 400 | return -EINVAL; |
Stephen Boyd | 01c02c1 | 2012-03-14 10:12:26 -0700 | [diff] [blame] | 401 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | |
| 403 | mutex_lock(&pil->lock); |
| 404 | if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__)) |
| 405 | ret = load_image(pil); |
Stephen Boyd | 20ad810 | 2011-10-09 21:28:01 -0700 | [diff] [blame] | 406 | if (!ret) |
| 407 | pil_set_state(pil, PIL_ONLINE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | mutex_unlock(&pil->lock); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 409 | put_device(&pil->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | EXPORT_SYMBOL(pil_force_boot); |
| 414 | |
| 415 | #ifdef CONFIG_DEBUG_FS |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 416 | static int msm_pil_debugfs_open(struct inode *inode, struct file *filp) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 417 | { |
| 418 | filp->private_data = inode->i_private; |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf, |
| 423 | size_t cnt, loff_t *ppos) |
| 424 | { |
| 425 | int r; |
| 426 | char buf[40]; |
| 427 | struct pil_device *pil = filp->private_data; |
| 428 | |
| 429 | mutex_lock(&pil->lock); |
| 430 | r = snprintf(buf, sizeof(buf), "%d\n", pil->count); |
| 431 | mutex_unlock(&pil->lock); |
| 432 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 433 | } |
| 434 | |
| 435 | static ssize_t msm_pil_debugfs_write(struct file *filp, |
| 436 | const char __user *ubuf, size_t cnt, loff_t *ppos) |
| 437 | { |
| 438 | struct pil_device *pil = filp->private_data; |
| 439 | char buf[4]; |
| 440 | |
| 441 | if (cnt > sizeof(buf)) |
| 442 | return -EINVAL; |
| 443 | |
| 444 | if (copy_from_user(&buf, ubuf, cnt)) |
| 445 | return -EFAULT; |
| 446 | |
| 447 | if (!strncmp(buf, "get", 3)) { |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 448 | if (IS_ERR(pil_get(pil->desc->name))) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 449 | return -EIO; |
| 450 | } else if (!strncmp(buf, "put", 3)) |
| 451 | pil_put(pil); |
| 452 | else |
| 453 | return -EINVAL; |
| 454 | |
| 455 | return cnt; |
| 456 | } |
| 457 | |
| 458 | static const struct file_operations msm_pil_debugfs_fops = { |
| 459 | .open = msm_pil_debugfs_open, |
| 460 | .read = msm_pil_debugfs_read, |
| 461 | .write = msm_pil_debugfs_write, |
| 462 | }; |
| 463 | |
| 464 | static struct dentry *pil_base_dir; |
| 465 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 466 | static int __init msm_pil_debugfs_init(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 467 | { |
| 468 | pil_base_dir = debugfs_create_dir("pil", NULL); |
| 469 | if (!pil_base_dir) { |
| 470 | pil_base_dir = NULL; |
| 471 | return -ENOMEM; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 476 | |
| 477 | static void __exit msm_pil_debugfs_exit(void) |
| 478 | { |
| 479 | debugfs_remove_recursive(pil_base_dir); |
| 480 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 481 | |
| 482 | static int msm_pil_debugfs_add(struct pil_device *pil) |
| 483 | { |
| 484 | if (!pil_base_dir) |
| 485 | return -ENOMEM; |
| 486 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 487 | pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR, |
| 488 | pil_base_dir, pil, &msm_pil_debugfs_fops); |
| 489 | return !pil->dentry ? -ENOMEM : 0; |
| 490 | } |
| 491 | |
| 492 | static void msm_pil_debugfs_remove(struct pil_device *pil) |
| 493 | { |
| 494 | debugfs_remove(pil->dentry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 495 | } |
| 496 | #else |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 497 | static int __init msm_pil_debugfs_init(void) { return 0; }; |
| 498 | static void __exit msm_pil_debugfs_exit(void) { return 0; }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 499 | static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; } |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 500 | static void msm_pil_debugfs_remove(struct pil_device *pil) { } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 501 | #endif |
| 502 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 503 | static int __msm_pil_shutdown(struct device *dev, void *data) |
| 504 | { |
| 505 | struct pil_device *pil = to_pil_device(dev); |
| 506 | pil->desc->ops->shutdown(pil->desc); |
| 507 | return 0; |
| 508 | } |
| 509 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 510 | static int msm_pil_shutdown_at_boot(void) |
| 511 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 512 | 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] | 513 | } |
| 514 | late_initcall(msm_pil_shutdown_at_boot); |
| 515 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 516 | static void pil_device_release(struct device *dev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 518 | struct pil_device *pil = to_pil_device(dev); |
| 519 | mutex_destroy(&pil->lock); |
| 520 | kfree(pil); |
| 521 | } |
| 522 | |
| 523 | struct pil_device *msm_pil_register(struct pil_desc *desc) |
| 524 | { |
| 525 | int err; |
| 526 | static atomic_t pil_count = ATOMIC_INIT(-1); |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 527 | struct pil_device *pil = kzalloc(sizeof(*pil), GFP_KERNEL); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 528 | |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 529 | if (!pil) |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 530 | return ERR_PTR(-ENOMEM); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 531 | |
| 532 | mutex_init(&pil->lock); |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 533 | pil->desc = desc; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 534 | pil->owner = desc->owner; |
| 535 | pil->dev.parent = desc->dev; |
| 536 | pil->dev.bus = &pil_bus_type; |
| 537 | pil->dev.release = pil_device_release; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 538 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 539 | dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count)); |
| 540 | err = device_register(&pil->dev); |
| 541 | if (err) { |
| 542 | put_device(&pil->dev); |
| 543 | mutex_destroy(&pil->lock); |
| 544 | kfree(pil); |
| 545 | return ERR_PTR(err); |
| 546 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 547 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 548 | err = msm_pil_debugfs_add(pil); |
| 549 | if (err) { |
| 550 | device_unregister(&pil->dev); |
| 551 | return ERR_PTR(err); |
| 552 | } |
| 553 | |
| 554 | return pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | } |
Stephen Boyd | 3f4da32 | 2011-08-30 01:03:23 -0700 | [diff] [blame] | 556 | EXPORT_SYMBOL(msm_pil_register); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 557 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 558 | void msm_pil_unregister(struct pil_device *pil) |
| 559 | { |
| 560 | if (IS_ERR_OR_NULL(pil)) |
| 561 | return; |
| 562 | |
| 563 | if (get_device(&pil->dev)) { |
| 564 | mutex_lock(&pil->lock); |
| 565 | WARN_ON(pil->count); |
| 566 | msm_pil_debugfs_remove(pil); |
| 567 | device_unregister(&pil->dev); |
| 568 | mutex_unlock(&pil->lock); |
| 569 | put_device(&pil->dev); |
| 570 | } |
| 571 | } |
| 572 | EXPORT_SYMBOL(msm_pil_unregister); |
| 573 | |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 574 | static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p) |
| 575 | { |
| 576 | switch (event) { |
| 577 | case PM_SUSPEND_PREPARE: |
| 578 | down_write(&pil_pm_rwsem); |
| 579 | break; |
| 580 | case PM_POST_SUSPEND: |
| 581 | up_write(&pil_pm_rwsem); |
| 582 | break; |
| 583 | } |
| 584 | return NOTIFY_DONE; |
| 585 | } |
| 586 | |
| 587 | static struct notifier_block pil_pm_notifier = { |
| 588 | .notifier_call = pil_pm_notify, |
| 589 | }; |
| 590 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 591 | static int __init msm_pil_init(void) |
| 592 | { |
| 593 | int ret = msm_pil_debugfs_init(); |
| 594 | if (ret) |
| 595 | return ret; |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 596 | register_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 597 | return bus_register(&pil_bus_type); |
| 598 | } |
| 599 | subsys_initcall(msm_pil_init); |
| 600 | |
| 601 | static void __exit msm_pil_exit(void) |
| 602 | { |
| 603 | bus_unregister(&pil_bus_type); |
Stephen Boyd | 80bde03 | 2012-03-16 00:14:42 -0700 | [diff] [blame] | 604 | unregister_pm_notifier(&pil_pm_notifier); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 605 | msm_pil_debugfs_exit(); |
| 606 | } |
| 607 | module_exit(msm_pil_exit); |
| 608 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 609 | MODULE_LICENSE("GPL v2"); |
| 610 | MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset"); |