Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * RAM Oops/Panic logger |
| 3 | * |
| 4 | * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com> |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 5 | * Copyright (C) 2011 Kees Cook <keescook@chromium.org> |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Marco Stornelli | 0169256 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 25 | #include <linux/kernel.h> |
James Bottomley | 83c1b31 | 2011-07-29 17:11:32 +0400 | [diff] [blame] | 26 | #include <linux/err.h> |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 28 | #include <linux/pstore.h> |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 29 | #include <linux/time.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/ioport.h> |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 33 | #include <linux/slab.h> |
Anton Vorontsov | 1894a25 | 2012-05-16 05:43:08 -0700 | [diff] [blame] | 34 | #include <linux/pstore_ram.h> |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 35 | |
| 36 | #define RAMOOPS_KERNMSG_HDR "====" |
Sergiu Iordache | 3e5c4fa | 2011-07-26 16:08:59 -0700 | [diff] [blame] | 37 | #define MIN_MEM_SIZE 4096UL |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 38 | |
Sergiu Iordache | 3e5c4fa | 2011-07-26 16:08:59 -0700 | [diff] [blame] | 39 | static ulong record_size = MIN_MEM_SIZE; |
| 40 | module_param(record_size, ulong, 0400); |
| 41 | MODULE_PARM_DESC(record_size, |
| 42 | "size of each dump done on oops/panic"); |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 43 | |
| 44 | static ulong mem_address; |
| 45 | module_param(mem_address, ulong, 0400); |
| 46 | MODULE_PARM_DESC(mem_address, |
| 47 | "start of reserved RAM used to store oops/panic logs"); |
| 48 | |
| 49 | static ulong mem_size; |
| 50 | module_param(mem_size, ulong, 0400); |
| 51 | MODULE_PARM_DESC(mem_size, |
| 52 | "size of reserved RAM used to store oops/panic logs"); |
| 53 | |
| 54 | static int dump_oops = 1; |
| 55 | module_param(dump_oops, int, 0600); |
| 56 | MODULE_PARM_DESC(dump_oops, |
| 57 | "set to 1 to dump oopses, 0 to only dump panics (default 1)"); |
| 58 | |
Anton Vorontsov | 39eb7e97 | 2012-05-17 00:15:34 -0700 | [diff] [blame] | 59 | static int ramoops_ecc; |
| 60 | module_param_named(ecc, ramoops_ecc, int, 0600); |
| 61 | MODULE_PARM_DESC(ramoops_ecc, |
| 62 | "set to 1 to enable ECC support"); |
| 63 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 64 | struct ramoops_context { |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 65 | struct persistent_ram_zone **przs; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 66 | phys_addr_t phys_addr; |
| 67 | unsigned long size; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 68 | size_t record_size; |
Sergiu Iordache | 6b4d2a2 | 2011-07-26 16:08:58 -0700 | [diff] [blame] | 69 | int dump_oops; |
Anton Vorontsov | 39eb7e97 | 2012-05-17 00:15:34 -0700 | [diff] [blame] | 70 | bool ecc; |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 71 | unsigned int max_dump_cnt; |
| 72 | unsigned int dump_write_cnt; |
| 73 | unsigned int dump_read_cnt; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 74 | struct pstore_info pstore; |
| 75 | }; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 76 | |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 77 | static struct platform_device *dummy; |
| 78 | static struct ramoops_platform_data *dummy_data; |
| 79 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 80 | static int ramoops_pstore_open(struct pstore_info *psi) |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 81 | { |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 82 | struct ramoops_context *cxt = psi->data; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 83 | |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 84 | cxt->dump_read_cnt = 0; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Anton Vorontsov | 755d66b | 2012-05-26 06:20:22 -0700 | [diff] [blame^] | 88 | static struct persistent_ram_zone * |
| 89 | ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max, |
| 90 | u64 *id, |
| 91 | enum pstore_type_id *typep, enum pstore_type_id type, |
| 92 | bool update) |
| 93 | { |
| 94 | struct persistent_ram_zone *prz; |
| 95 | int i = (*c)++; |
| 96 | |
| 97 | if (i >= max) |
| 98 | return NULL; |
| 99 | |
| 100 | prz = przs[i]; |
| 101 | |
| 102 | if (update) { |
| 103 | /* Update old/shadowed buffer. */ |
| 104 | persistent_ram_save_old(prz); |
| 105 | if (!persistent_ram_old_size(prz)) |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | *typep = type; |
| 110 | *id = i; |
| 111 | |
| 112 | return prz; |
| 113 | } |
| 114 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 115 | static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type, |
| 116 | struct timespec *time, |
| 117 | char **buf, |
| 118 | struct pstore_info *psi) |
| 119 | { |
| 120 | ssize_t size; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 121 | struct ramoops_context *cxt = psi->data; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 122 | struct persistent_ram_zone *prz; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 123 | |
Anton Vorontsov | 755d66b | 2012-05-26 06:20:22 -0700 | [diff] [blame^] | 124 | prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt, |
| 125 | cxt->max_dump_cnt, id, type, |
| 126 | PSTORE_TYPE_DMESG, 1); |
| 127 | if (!prz) |
| 128 | return 0; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 129 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 130 | /* TODO(kees): Bogus time for the moment. */ |
| 131 | time->tv_sec = 0; |
| 132 | time->tv_nsec = 0; |
| 133 | |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 134 | size = persistent_ram_old_size(prz); |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 135 | *buf = kmalloc(size, GFP_KERNEL); |
| 136 | if (*buf == NULL) |
| 137 | return -ENOMEM; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 138 | memcpy(*buf, persistent_ram_old(prz), size); |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 139 | |
| 140 | return size; |
| 141 | } |
| 142 | |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 143 | static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz) |
| 144 | { |
| 145 | char *hdr; |
| 146 | struct timeval timestamp; |
| 147 | size_t len; |
| 148 | |
| 149 | do_gettimeofday(×tamp); |
| 150 | hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n", |
| 151 | (long)timestamp.tv_sec, (long)timestamp.tv_usec); |
| 152 | WARN_ON_ONCE(!hdr); |
| 153 | len = hdr ? strlen(hdr) : 0; |
| 154 | persistent_ram_write(prz, hdr, len); |
| 155 | kfree(hdr); |
| 156 | |
| 157 | return len; |
| 158 | } |
| 159 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 160 | static int ramoops_pstore_write(enum pstore_type_id type, |
| 161 | enum kmsg_dump_reason reason, |
| 162 | u64 *id, |
| 163 | unsigned int part, |
| 164 | size_t size, struct pstore_info *psi) |
| 165 | { |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 166 | struct ramoops_context *cxt = psi->data; |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 167 | struct persistent_ram_zone *prz = cxt->przs[cxt->dump_write_cnt]; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 168 | size_t hlen; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 169 | |
| 170 | /* Currently ramoops is designed to only store dmesg dumps. */ |
| 171 | if (type != PSTORE_TYPE_DMESG) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | /* Out of the various dmesg dump types, ramoops is currently designed |
| 175 | * to only store crash logs, rather than storing general kernel logs. |
| 176 | */ |
Seiji Aguchi | fc2d557 | 2011-01-12 16:59:29 -0800 | [diff] [blame] | 177 | if (reason != KMSG_DUMP_OOPS && |
WANG Cong | a3dd332 | 2012-01-12 17:20:11 -0800 | [diff] [blame] | 178 | reason != KMSG_DUMP_PANIC) |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 179 | return -EINVAL; |
Seiji Aguchi | fc2d557 | 2011-01-12 16:59:29 -0800 | [diff] [blame] | 180 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 181 | /* Skip Oopes when configured to do so. */ |
Sergiu Iordache | 6b4d2a2 | 2011-07-26 16:08:58 -0700 | [diff] [blame] | 182 | if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops) |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 183 | return -EINVAL; |
| 184 | |
| 185 | /* Explicitly only take the first part of any new crash. |
| 186 | * If our buffer is larger than kmsg_bytes, this can never happen, |
| 187 | * and if our buffer is smaller than kmsg_bytes, we don't want the |
| 188 | * report split across multiple records. |
| 189 | */ |
| 190 | if (part != 1) |
| 191 | return -ENOSPC; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 192 | |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 193 | hlen = ramoops_write_kmsg_hdr(prz); |
| 194 | if (size + hlen > prz->buffer_size) |
| 195 | size = prz->buffer_size - hlen; |
| 196 | persistent_ram_write(prz, cxt->pstore.buf, size); |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 197 | |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 198 | cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 199 | |
| 200 | return 0; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 203 | static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, |
| 204 | struct pstore_info *psi) |
| 205 | { |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 206 | struct ramoops_context *cxt = psi->data; |
| 207 | |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 208 | if (id >= cxt->max_dump_cnt) |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 209 | return -EINVAL; |
| 210 | |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 211 | persistent_ram_free_old(cxt->przs[id]); |
Anton Vorontsov | 93cce04 | 2012-05-26 06:07:52 -0700 | [diff] [blame] | 212 | persistent_ram_zap(cxt->przs[id]); |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static struct ramoops_context oops_cxt = { |
| 218 | .pstore = { |
| 219 | .owner = THIS_MODULE, |
| 220 | .name = "ramoops", |
| 221 | .open = ramoops_pstore_open, |
| 222 | .read = ramoops_pstore_read, |
| 223 | .write = ramoops_pstore_write, |
| 224 | .erase = ramoops_pstore_erase, |
| 225 | }, |
| 226 | }; |
| 227 | |
Anton Vorontsov | f4c5d24 | 2012-05-26 06:20:21 -0700 | [diff] [blame] | 228 | static void ramoops_free_przs(struct ramoops_context *cxt) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | if (!cxt->przs) |
| 233 | return; |
| 234 | |
| 235 | for (i = 0; cxt->przs[i]; i++) |
| 236 | persistent_ram_free(cxt->przs[i]); |
| 237 | kfree(cxt->przs); |
| 238 | } |
| 239 | |
| 240 | static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt, |
| 241 | phys_addr_t *paddr, size_t dump_mem_sz) |
| 242 | { |
| 243 | int err = -ENOMEM; |
| 244 | int i; |
| 245 | |
| 246 | if (!cxt->record_size) |
| 247 | return 0; |
| 248 | |
| 249 | cxt->max_dump_cnt = dump_mem_sz / cxt->record_size; |
| 250 | if (!cxt->max_dump_cnt) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt, |
| 254 | GFP_KERNEL); |
| 255 | if (!cxt->przs) { |
| 256 | dev_err(dev, "failed to initialize a prz array for dumps\n"); |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | |
| 260 | for (i = 0; i < cxt->max_dump_cnt; i++) { |
| 261 | size_t sz = cxt->record_size; |
| 262 | |
| 263 | cxt->przs[i] = persistent_ram_new(*paddr, sz, cxt->ecc); |
| 264 | if (IS_ERR(cxt->przs[i])) { |
| 265 | err = PTR_ERR(cxt->przs[i]); |
| 266 | dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n", |
| 267 | sz, (unsigned long long)*paddr, err); |
| 268 | goto fail_prz; |
| 269 | } |
| 270 | *paddr += sz; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | fail_prz: |
| 275 | ramoops_free_przs(cxt); |
| 276 | return err; |
| 277 | } |
| 278 | |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 279 | static int __init ramoops_probe(struct platform_device *pdev) |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 280 | { |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 281 | struct device *dev = &pdev->dev; |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 282 | struct ramoops_platform_data *pdata = pdev->dev.platform_data; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 283 | struct ramoops_context *cxt = &oops_cxt; |
Anton Vorontsov | f4c5d24 | 2012-05-26 06:20:21 -0700 | [diff] [blame] | 284 | size_t dump_mem_sz; |
| 285 | phys_addr_t paddr; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 286 | int err = -EINVAL; |
| 287 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 288 | /* Only a single ramoops area allowed at a time, so fail extra |
| 289 | * probes. |
| 290 | */ |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 291 | if (cxt->max_dump_cnt) |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 292 | goto fail_out; |
| 293 | |
Sergiu Iordache | 3e5c4fa | 2011-07-26 16:08:59 -0700 | [diff] [blame] | 294 | if (!pdata->mem_size || !pdata->record_size) { |
| 295 | pr_err("The memory size and the record size must be " |
| 296 | "non-zero\n"); |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 297 | goto fail_out; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Marco Stornelli | fdb5950 | 2012-01-12 17:20:58 -0800 | [diff] [blame] | 300 | pdata->mem_size = rounddown_pow_of_two(pdata->mem_size); |
| 301 | pdata->record_size = rounddown_pow_of_two(pdata->record_size); |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 302 | |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 303 | cxt->dump_read_cnt = 0; |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 304 | cxt->size = pdata->mem_size; |
| 305 | cxt->phys_addr = pdata->mem_address; |
Sergiu Iordache | 3e5c4fa | 2011-07-26 16:08:59 -0700 | [diff] [blame] | 306 | cxt->record_size = pdata->record_size; |
Sergiu Iordache | 6b4d2a2 | 2011-07-26 16:08:58 -0700 | [diff] [blame] | 307 | cxt->dump_oops = pdata->dump_oops; |
Anton Vorontsov | 39eb7e97 | 2012-05-17 00:15:34 -0700 | [diff] [blame] | 308 | cxt->ecc = pdata->ecc; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 309 | |
Anton Vorontsov | f4c5d24 | 2012-05-26 06:20:21 -0700 | [diff] [blame] | 310 | paddr = cxt->phys_addr; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 311 | |
Anton Vorontsov | f4c5d24 | 2012-05-26 06:20:21 -0700 | [diff] [blame] | 312 | dump_mem_sz = cxt->size; |
| 313 | err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz); |
| 314 | if (err) { |
| 315 | pr_err("memory size too small, minimum is %lu\n", |
| 316 | cxt->record_size); |
| 317 | goto fail_count; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 320 | cxt->pstore.data = cxt; |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 321 | cxt->pstore.bufsize = cxt->przs[0]->buffer_size; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 322 | cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL); |
| 323 | spin_lock_init(&cxt->pstore.buf_lock); |
| 324 | if (!cxt->pstore.buf) { |
| 325 | pr_err("cannot allocate pstore buffer\n"); |
| 326 | goto fail_clear; |
| 327 | } |
| 328 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 329 | err = pstore_register(&cxt->pstore); |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 330 | if (err) { |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 331 | pr_err("registering with pstore failed\n"); |
Anton Vorontsov | 896fc1f | 2012-05-17 00:15:18 -0700 | [diff] [blame] | 332 | goto fail_buf; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Kees Cook | c755201 | 2012-01-12 17:20:59 -0800 | [diff] [blame] | 335 | /* |
| 336 | * Update the module parameter variables as well so they are visible |
| 337 | * through /sys/module/ramoops/parameters/ |
| 338 | */ |
| 339 | mem_size = pdata->mem_size; |
| 340 | mem_address = pdata->mem_address; |
| 341 | record_size = pdata->record_size; |
| 342 | dump_oops = pdata->dump_oops; |
| 343 | |
Anton Vorontsov | 39eb7e97 | 2012-05-17 00:15:34 -0700 | [diff] [blame] | 344 | pr_info("attached 0x%lx@0x%llx (%ux0x%zx), ecc: %s\n", |
Randy Dunlap | d109a67 | 2012-05-03 15:45:03 +1000 | [diff] [blame] | 345 | cxt->size, (unsigned long long)cxt->phys_addr, |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 346 | cxt->max_dump_cnt, cxt->record_size, |
Anton Vorontsov | 39eb7e97 | 2012-05-17 00:15:34 -0700 | [diff] [blame] | 347 | ramoops_ecc ? "on" : "off"); |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 348 | |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 351 | fail_buf: |
| 352 | kfree(cxt->pstore.buf); |
| 353 | fail_clear: |
| 354 | cxt->pstore.bufsize = 0; |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 355 | cxt->max_dump_cnt = 0; |
Anton Vorontsov | f4c5d24 | 2012-05-26 06:20:21 -0700 | [diff] [blame] | 356 | fail_count: |
| 357 | ramoops_free_przs(cxt); |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 358 | fail_out: |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 359 | return err; |
| 360 | } |
| 361 | |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 362 | static int __exit ramoops_remove(struct platform_device *pdev) |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 363 | { |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 364 | #if 0 |
| 365 | /* TODO(kees): We cannot unload ramoops since pstore doesn't support |
| 366 | * unregistering yet. |
| 367 | */ |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 368 | struct ramoops_context *cxt = &oops_cxt; |
| 369 | |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 370 | iounmap(cxt->virt_addr); |
| 371 | release_mem_region(cxt->phys_addr, cxt->size); |
Anton Vorontsov | cac2eb7 | 2012-05-26 06:20:20 -0700 | [diff] [blame] | 372 | cxt->max_dump_cnt = 0; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 373 | |
| 374 | /* TODO(kees): When pstore supports unregistering, call it here. */ |
| 375 | kfree(cxt->pstore.buf); |
| 376 | cxt->pstore.bufsize = 0; |
| 377 | |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 378 | return 0; |
Kees Cook | 9ba80d9 | 2012-05-03 15:45:02 +1000 | [diff] [blame] | 379 | #endif |
| 380 | return -EBUSY; |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 383 | static struct platform_driver ramoops_driver = { |
| 384 | .remove = __exit_p(ramoops_remove), |
| 385 | .driver = { |
| 386 | .name = "ramoops", |
| 387 | .owner = THIS_MODULE, |
| 388 | }, |
| 389 | }; |
| 390 | |
| 391 | static int __init ramoops_init(void) |
| 392 | { |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 393 | int ret; |
| 394 | ret = platform_driver_probe(&ramoops_driver, ramoops_probe); |
| 395 | if (ret == -ENODEV) { |
| 396 | /* |
| 397 | * If we didn't find a platform device, we use module parameters |
| 398 | * building platform data on the fly. |
| 399 | */ |
Marco Stornelli | 0169256 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 400 | pr_info("platform device not found, using module parameters\n"); |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 401 | dummy_data = kzalloc(sizeof(struct ramoops_platform_data), |
| 402 | GFP_KERNEL); |
| 403 | if (!dummy_data) |
| 404 | return -ENOMEM; |
| 405 | dummy_data->mem_size = mem_size; |
| 406 | dummy_data->mem_address = mem_address; |
Sergiu Iordache | 3e5c4fa | 2011-07-26 16:08:59 -0700 | [diff] [blame] | 407 | dummy_data->record_size = record_size; |
Sergiu Iordache | 6b4d2a2 | 2011-07-26 16:08:58 -0700 | [diff] [blame] | 408 | dummy_data->dump_oops = dump_oops; |
Anton Vorontsov | 39eb7e97 | 2012-05-17 00:15:34 -0700 | [diff] [blame] | 409 | dummy_data->ecc = ramoops_ecc; |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 410 | dummy = platform_create_bundle(&ramoops_driver, ramoops_probe, |
| 411 | NULL, 0, dummy_data, |
| 412 | sizeof(struct ramoops_platform_data)); |
| 413 | |
| 414 | if (IS_ERR(dummy)) |
| 415 | ret = PTR_ERR(dummy); |
| 416 | else |
| 417 | ret = 0; |
| 418 | } |
| 419 | |
| 420 | return ret; |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void __exit ramoops_exit(void) |
| 424 | { |
| 425 | platform_driver_unregister(&ramoops_driver); |
Marco Stornelli | 13aefd7 | 2011-07-26 16:08:57 -0700 | [diff] [blame] | 426 | kfree(dummy_data); |
Kyungmin Park | c3b92ce | 2010-10-27 15:34:52 -0700 | [diff] [blame] | 427 | } |
Marco Stornelli | 56d611a | 2010-05-26 14:43:54 -0700 | [diff] [blame] | 428 | |
| 429 | module_init(ramoops_init); |
| 430 | module_exit(ramoops_exit); |
| 431 | |
| 432 | MODULE_LICENSE("GPL"); |
| 433 | MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>"); |
| 434 | MODULE_DESCRIPTION("RAM Oops/Panic logger/driver"); |