blob: 60c01eb47dd6334dda7e838ed2270ffd0dc3791f [file] [log] [blame]
Bryan O'Donoghue28a375d2015-01-30 16:29:38 +00001/**
2 * imr.c
3 *
4 * Copyright(c) 2013 Intel Corporation.
5 * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie>
6 *
7 * IMR registers define an isolated region of memory that can
8 * be masked to prohibit certain system agents from accessing memory.
9 * When a device behind a masked port performs an access - snooped or
10 * not, an IMR may optionally prevent that transaction from changing
11 * the state of memory or from getting correct data in response to the
12 * operation.
13 *
14 * Write data will be dropped and reads will return 0xFFFFFFFF, the
15 * system will reset and system BIOS will print out an error message to
16 * inform the user that an IMR has been violated.
17 *
18 * This code is based on the Linux MTRR code and reference code from
19 * Intel's Quark BSP EFI, Linux and grub code.
20 *
21 * See quark-x1000-datasheet.pdf for register definitions.
22 * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/quark-x1000-datasheet.pdf
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27#include <asm-generic/sections.h>
28#include <asm/cpu_device_id.h>
29#include <asm/imr.h>
30#include <asm/iosf_mbi.h>
31#include <linux/debugfs.h>
32#include <linux/init.h>
33#include <linux/mm.h>
34#include <linux/module.h>
35#include <linux/types.h>
36
37struct imr_device {
38 struct dentry *file;
39 bool init;
40 struct mutex lock;
41 int max_imr;
42 int reg_base;
43};
44
45static struct imr_device imr_dev;
46
47/*
48 * IMR read/write mask control registers.
49 * See quark-x1000-datasheet.pdf sections 12.7.4.5 and 12.7.4.6 for
50 * bit definitions.
51 *
52 * addr_hi
53 * 31 Lock bit
54 * 30:24 Reserved
55 * 23:2 1 KiB aligned lo address
56 * 1:0 Reserved
57 *
58 * addr_hi
59 * 31:24 Reserved
60 * 23:2 1 KiB aligned hi address
61 * 1:0 Reserved
62 */
63#define IMR_LOCK BIT(31)
64
65struct imr_regs {
66 u32 addr_lo;
67 u32 addr_hi;
68 u32 rmask;
69 u32 wmask;
70};
71
72#define IMR_NUM_REGS (sizeof(struct imr_regs)/sizeof(u32))
73#define IMR_SHIFT 8
74#define imr_to_phys(x) ((x) << IMR_SHIFT)
75#define phys_to_imr(x) ((x) >> IMR_SHIFT)
76
77/**
78 * imr_is_enabled - true if an IMR is enabled false otherwise.
79 *
80 * Determines if an IMR is enabled based on address range and read/write
81 * mask. An IMR set with an address range set to zero and a read/write
82 * access mask set to all is considered to be disabled. An IMR in any
83 * other state - for example set to zero but without read/write access
84 * all is considered to be enabled. This definition of disabled is how
85 * firmware switches off an IMR and is maintained in kernel for
86 * consistency.
87 *
88 * @imr: pointer to IMR descriptor.
89 * @return: true if IMR enabled false if disabled.
90 */
91static inline int imr_is_enabled(struct imr_regs *imr)
92{
93 return !(imr->rmask == IMR_READ_ACCESS_ALL &&
94 imr->wmask == IMR_WRITE_ACCESS_ALL &&
95 imr_to_phys(imr->addr_lo) == 0 &&
96 imr_to_phys(imr->addr_hi) == 0);
97}
98
99/**
100 * imr_read - read an IMR at a given index.
101 *
102 * Requires caller to hold imr mutex.
103 *
104 * @idev: pointer to imr_device structure.
105 * @imr_id: IMR entry to read.
106 * @imr: IMR structure representing address and access masks.
107 * @return: 0 on success or error code passed from mbi_iosf on failure.
108 */
109static int imr_read(struct imr_device *idev, u32 imr_id, struct imr_regs *imr)
110{
111 u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
112 int ret;
113
114 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, QRK_MBI_MM_READ,
115 reg++, &imr->addr_lo);
116 if (ret)
117 return ret;
118
119 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, QRK_MBI_MM_READ,
120 reg++, &imr->addr_hi);
121 if (ret)
122 return ret;
123
124 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, QRK_MBI_MM_READ,
125 reg++, &imr->rmask);
126 if (ret)
127 return ret;
128
129 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, QRK_MBI_MM_READ,
130 reg++, &imr->wmask);
131 if (ret)
132 return ret;
133
134 return 0;
135}
136
137/**
138 * imr_write - write an IMR at a given index.
139 *
140 * Requires caller to hold imr mutex.
141 * Note lock bits need to be written independently of address bits.
142 *
143 * @idev: pointer to imr_device structure.
144 * @imr_id: IMR entry to write.
145 * @imr: IMR structure representing address and access masks.
146 * @lock: indicates if the IMR lock bit should be applied.
147 * @return: 0 on success or error code passed from mbi_iosf on failure.
148 */
149static int imr_write(struct imr_device *idev, u32 imr_id,
150 struct imr_regs *imr, bool lock)
151{
152 unsigned long flags;
153 u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
154 int ret;
155
156 local_irq_save(flags);
157
158 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, QRK_MBI_MM_WRITE, reg++,
159 imr->addr_lo);
160 if (ret)
161 goto failed;
162
163 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, QRK_MBI_MM_WRITE,
164 reg++, imr->addr_hi);
165 if (ret)
166 goto failed;
167
168 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, QRK_MBI_MM_WRITE,
169 reg++, imr->rmask);
170 if (ret)
171 goto failed;
172
173 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, QRK_MBI_MM_WRITE,
174 reg++, imr->wmask);
175 if (ret)
176 goto failed;
177
178 /* Lock bit must be set separately to addr_lo address bits. */
179 if (lock) {
180 imr->addr_lo |= IMR_LOCK;
181 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, QRK_MBI_MM_WRITE,
182 reg - IMR_NUM_REGS, imr->addr_lo);
183 if (ret)
184 goto failed;
185 }
186
187 local_irq_restore(flags);
188 return 0;
189failed:
190 /*
191 * If writing to the IOSF failed then we're in an unknown state,
192 * likely a very bad state. An IMR in an invalid state will almost
193 * certainly lead to a memory access violation.
194 */
195 local_irq_restore(flags);
196 WARN(ret, "IOSF-MBI write fail range 0x%08x-0x%08x unreliable\n",
197 imr_to_phys(imr->addr_lo), imr_to_phys(imr->addr_hi) + IMR_MASK);
198
199 return ret;
200}
201
202/**
203 * imr_dbgfs_state_show - print state of IMR registers.
204 *
205 * @s: pointer to seq_file for output.
206 * @unused: unused parameter.
207 * @return: 0 on success or error code passed from mbi_iosf on failure.
208 */
209static int imr_dbgfs_state_show(struct seq_file *s, void *unused)
210{
211 phys_addr_t base;
212 phys_addr_t end;
213 int i;
214 struct imr_device *idev = s->private;
215 struct imr_regs imr;
216 size_t size;
217 int ret = -ENODEV;
218
219 mutex_lock(&idev->lock);
220
221 for (i = 0; i < idev->max_imr; i++) {
222
223 ret = imr_read(idev, i, &imr);
224 if (ret)
225 break;
226
227 /*
228 * Remember to add IMR_ALIGN bytes to size to indicate the
229 * inherent IMR_ALIGN size bytes contained in the masked away
230 * lower ten bits.
231 */
232 if (imr_is_enabled(&imr)) {
233 base = imr_to_phys(imr.addr_lo);
234 end = imr_to_phys(imr.addr_hi) + IMR_MASK;
235 } else {
236 base = 0;
237 end = 0;
238 }
239 size = end - base;
240 seq_printf(s, "imr%02i: base=%pa, end=%pa, size=0x%08zx "
241 "rmask=0x%08x, wmask=0x%08x, %s, %s\n", i,
242 &base, &end, size, imr.rmask, imr.wmask,
243 imr_is_enabled(&imr) ? "enabled " : "disabled",
244 imr.addr_lo & IMR_LOCK ? "locked" : "unlocked");
245 }
246
247 mutex_unlock(&idev->lock);
248 return ret;
249}
250
251/**
252 * imr_state_open - debugfs open callback.
253 *
254 * @inode: pointer to struct inode.
255 * @file: pointer to struct file.
256 * @return: result of single open.
257 */
258static int imr_state_open(struct inode *inode, struct file *file)
259{
260 return single_open(file, imr_dbgfs_state_show, inode->i_private);
261}
262
263static const struct file_operations imr_state_ops = {
264 .open = imr_state_open,
265 .read = seq_read,
266 .llseek = seq_lseek,
267 .release = single_release,
268};
269
270/**
271 * imr_debugfs_register - register debugfs hooks.
272 *
273 * @idev: pointer to imr_device structure.
274 * @return: 0 on success - errno on failure.
275 */
276static int imr_debugfs_register(struct imr_device *idev)
277{
278 idev->file = debugfs_create_file("imr_state", S_IFREG | S_IRUGO, NULL,
279 idev, &imr_state_ops);
Fengguang Wu32d39162015-02-19 16:14:32 +0800280 return PTR_ERR_OR_ZERO(idev->file);
Bryan O'Donoghue28a375d2015-01-30 16:29:38 +0000281}
282
283/**
284 * imr_debugfs_unregister - unregister debugfs hooks.
285 *
286 * @idev: pointer to imr_device structure.
287 * @return:
288 */
289static void imr_debugfs_unregister(struct imr_device *idev)
290{
291 debugfs_remove(idev->file);
292}
293
294/**
295 * imr_check_params - check passed address range IMR alignment and non-zero size
296 *
297 * @base: base address of intended IMR.
298 * @size: size of intended IMR.
299 * @return: zero on valid range -EINVAL on unaligned base/size.
300 */
301static int imr_check_params(phys_addr_t base, size_t size)
302{
303 if ((base & IMR_MASK) || (size & IMR_MASK)) {
304 pr_err("base %pa size 0x%08zx must align to 1KiB\n",
305 &base, size);
306 return -EINVAL;
307 }
308 if (size == 0)
309 return -EINVAL;
310
311 return 0;
312}
313
314/**
315 * imr_raw_size - account for the IMR_ALIGN bytes that addr_hi appends.
316 *
317 * IMR addr_hi has a built in offset of plus IMR_ALIGN (0x400) bytes from the
318 * value in the register. We need to subtract IMR_ALIGN bytes from input sizes
319 * as a result.
320 *
321 * @size: input size bytes.
322 * @return: reduced size.
323 */
324static inline size_t imr_raw_size(size_t size)
325{
326 return size - IMR_ALIGN;
327}
328
329/**
330 * imr_address_overlap - detects an address overlap.
331 *
332 * @addr: address to check against an existing IMR.
333 * @imr: imr being checked.
334 * @return: true for overlap false for no overlap.
335 */
336static inline int imr_address_overlap(phys_addr_t addr, struct imr_regs *imr)
337{
338 return addr >= imr_to_phys(imr->addr_lo) && addr <= imr_to_phys(imr->addr_hi);
339}
340
341/**
342 * imr_add_range - add an Isolated Memory Region.
343 *
344 * @base: physical base address of region aligned to 1KiB.
345 * @size: physical size of region in bytes must be aligned to 1KiB.
346 * @read_mask: read access mask.
347 * @write_mask: write access mask.
348 * @lock: indicates whether or not to permanently lock this region.
349 * @return: zero on success or negative value indicating error.
350 */
351int imr_add_range(phys_addr_t base, size_t size,
352 unsigned int rmask, unsigned int wmask, bool lock)
353{
354 phys_addr_t end;
355 unsigned int i;
356 struct imr_device *idev = &imr_dev;
357 struct imr_regs imr;
358 size_t raw_size;
359 int reg;
360 int ret;
361
362 if (WARN_ONCE(idev->init == false, "driver not initialized"))
363 return -ENODEV;
364
365 ret = imr_check_params(base, size);
366 if (ret)
367 return ret;
368
369 /* Tweak the size value. */
370 raw_size = imr_raw_size(size);
371 end = base + raw_size;
372
373 /*
374 * Check for reserved IMR value common to firmware, kernel and grub
375 * indicating a disabled IMR.
376 */
377 imr.addr_lo = phys_to_imr(base);
378 imr.addr_hi = phys_to_imr(end);
379 imr.rmask = rmask;
380 imr.wmask = wmask;
381 if (!imr_is_enabled(&imr))
382 return -ENOTSUPP;
383
384 mutex_lock(&idev->lock);
385
386 /*
387 * Find a free IMR while checking for an existing overlapping range.
388 * Note there's no restriction in silicon to prevent IMR overlaps.
389 * For the sake of simplicity and ease in defining/debugging an IMR
390 * memory map we exclude IMR overlaps.
391 */
392 reg = -1;
393 for (i = 0; i < idev->max_imr; i++) {
394 ret = imr_read(idev, i, &imr);
395 if (ret)
396 goto failed;
397
398 /* Find overlap @ base or end of requested range. */
399 ret = -EINVAL;
400 if (imr_is_enabled(&imr)) {
401 if (imr_address_overlap(base, &imr))
402 goto failed;
403 if (imr_address_overlap(end, &imr))
404 goto failed;
405 } else {
406 reg = i;
407 }
408 }
409
410 /* Error out if we have no free IMR entries. */
411 if (reg == -1) {
412 ret = -ENOMEM;
413 goto failed;
414 }
415
416 pr_debug("add %d phys %pa-%pa size %zx mask 0x%08x wmask 0x%08x\n",
417 reg, &base, &end, raw_size, rmask, wmask);
418
419 /* Enable IMR at specified range and access mask. */
420 imr.addr_lo = phys_to_imr(base);
421 imr.addr_hi = phys_to_imr(end);
422 imr.rmask = rmask;
423 imr.wmask = wmask;
424
425 ret = imr_write(idev, reg, &imr, lock);
426 if (ret < 0) {
427 /*
428 * In the highly unlikely event iosf_mbi_write failed
429 * attempt to rollback the IMR setup skipping the trapping
430 * of further IOSF write failures.
431 */
432 imr.addr_lo = 0;
433 imr.addr_hi = 0;
434 imr.rmask = IMR_READ_ACCESS_ALL;
435 imr.wmask = IMR_WRITE_ACCESS_ALL;
436 imr_write(idev, reg, &imr, false);
437 }
438failed:
439 mutex_unlock(&idev->lock);
440 return ret;
441}
442EXPORT_SYMBOL_GPL(imr_add_range);
443
444/**
445 * __imr_remove_range - delete an Isolated Memory Region.
446 *
447 * This function allows you to delete an IMR by its index specified by reg or
448 * by address range specified by base and size respectively. If you specify an
449 * index on its own the base and size parameters are ignored.
450 * imr_remove_range(0, base, size); delete IMR at index 0 base/size ignored.
451 * imr_remove_range(-1, base, size); delete IMR from base to base+size.
452 *
453 * @reg: imr index to remove.
454 * @base: physical base address of region aligned to 1 KiB.
455 * @size: physical size of region in bytes aligned to 1 KiB.
456 * @return: -EINVAL on invalid range or out or range id
457 * -ENODEV if reg is valid but no IMR exists or is locked
458 * 0 on success.
459 */
460static int __imr_remove_range(int reg, phys_addr_t base, size_t size)
461{
462 phys_addr_t end;
463 bool found = false;
464 unsigned int i;
465 struct imr_device *idev = &imr_dev;
466 struct imr_regs imr;
467 size_t raw_size;
468 int ret = 0;
469
470 if (WARN_ONCE(idev->init == false, "driver not initialized"))
471 return -ENODEV;
472
473 /*
474 * Validate address range if deleting by address, else we are
475 * deleting by index where base and size will be ignored.
476 */
477 if (reg == -1) {
478 ret = imr_check_params(base, size);
479 if (ret)
480 return ret;
481 }
482
483 /* Tweak the size value. */
484 raw_size = imr_raw_size(size);
485 end = base + raw_size;
486
487 mutex_lock(&idev->lock);
488
489 if (reg >= 0) {
490 /* If a specific IMR is given try to use it. */
491 ret = imr_read(idev, reg, &imr);
492 if (ret)
493 goto failed;
494
495 if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) {
496 ret = -ENODEV;
497 goto failed;
498 }
499 found = true;
500 } else {
501 /* Search for match based on address range. */
502 for (i = 0; i < idev->max_imr; i++) {
503 ret = imr_read(idev, i, &imr);
504 if (ret)
505 goto failed;
506
507 if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK)
508 continue;
509
510 if ((imr_to_phys(imr.addr_lo) == base) &&
511 (imr_to_phys(imr.addr_hi) == end)) {
512 found = true;
513 reg = i;
514 break;
515 }
516 }
517 }
518
519 if (!found) {
520 ret = -ENODEV;
521 goto failed;
522 }
523
524 pr_debug("remove %d phys %pa-%pa size %zx\n", reg, &base, &end, raw_size);
525
526 /* Tear down the IMR. */
527 imr.addr_lo = 0;
528 imr.addr_hi = 0;
529 imr.rmask = IMR_READ_ACCESS_ALL;
530 imr.wmask = IMR_WRITE_ACCESS_ALL;
531
532 ret = imr_write(idev, reg, &imr, false);
533
534failed:
535 mutex_unlock(&idev->lock);
536 return ret;
537}
538
539/**
540 * imr_remove_range - delete an Isolated Memory Region by address
541 *
542 * This function allows you to delete an IMR by an address range specified
543 * by base and size respectively.
544 * imr_remove_range(base, size); delete IMR from base to base+size.
545 *
546 * @base: physical base address of region aligned to 1 KiB.
547 * @size: physical size of region in bytes aligned to 1 KiB.
548 * @return: -EINVAL on invalid range or out or range id
549 * -ENODEV if reg is valid but no IMR exists or is locked
550 * 0 on success.
551 */
552int imr_remove_range(phys_addr_t base, size_t size)
553{
554 return __imr_remove_range(-1, base, size);
555}
556EXPORT_SYMBOL_GPL(imr_remove_range);
557
558/**
559 * imr_clear - delete an Isolated Memory Region by index
560 *
561 * This function allows you to delete an IMR by an address range specified
562 * by the index of the IMR. Useful for initial sanitization of the IMR
563 * address map.
564 * imr_ge(base, size); delete IMR from base to base+size.
565 *
566 * @reg: imr index to remove.
567 * @return: -EINVAL on invalid range or out or range id
568 * -ENODEV if reg is valid but no IMR exists or is locked
569 * 0 on success.
570 */
571static inline int imr_clear(int reg)
572{
573 return __imr_remove_range(reg, 0, 0);
574}
575
576/**
577 * imr_fixup_memmap - Tear down IMRs used during bootup.
578 *
579 * BIOS and Grub both setup IMRs around compressed kernel, initrd memory
580 * that need to be removed before the kernel hands out one of the IMR
581 * encased addresses to a downstream DMA agent such as the SD or Ethernet.
582 * IMRs on Galileo are setup to immediately reset the system on violation.
583 * As a result if you're running a root filesystem from SD - you'll need
584 * the boot-time IMRs torn down or you'll find seemingly random resets when
585 * using your filesystem.
586 *
587 * @idev: pointer to imr_device structure.
588 * @return:
589 */
590static void __init imr_fixup_memmap(struct imr_device *idev)
591{
592 phys_addr_t base = virt_to_phys(&_text);
593 size_t size = virt_to_phys(&__end_rodata) - base;
594 int i;
595 int ret;
596
597 /* Tear down all existing unlocked IMRs. */
598 for (i = 0; i < idev->max_imr; i++)
599 imr_clear(i);
600
601 /*
602 * Setup a locked IMR around the physical extent of the kernel
603 * from the beginning of the .text secton to the end of the
604 * .rodata section as one physically contiguous block.
605 */
606 ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, true);
607 if (ret < 0) {
608 pr_err("unable to setup IMR for kernel: (%p - %p)\n",
609 &_text, &__end_rodata);
610 } else {
611 pr_info("protecting kernel .text - .rodata: %zu KiB (%p - %p)\n",
612 size / 1024, &_text, &__end_rodata);
613 }
614
615}
616
617static const struct x86_cpu_id imr_ids[] __initconst = {
618 { X86_VENDOR_INTEL, 5, 9 }, /* Intel Quark SoC X1000. */
619 {}
620};
621MODULE_DEVICE_TABLE(x86cpu, imr_ids);
622
623/**
624 * imr_init - entry point for IMR driver.
625 *
626 * return: -ENODEV for no IMR support 0 if good to go.
627 */
628static int __init imr_init(void)
629{
630 struct imr_device *idev = &imr_dev;
631 int ret;
632
633 if (!x86_match_cpu(imr_ids) || !iosf_mbi_available())
634 return -ENODEV;
635
636 idev->max_imr = QUARK_X1000_IMR_MAX;
637 idev->reg_base = QUARK_X1000_IMR_REGBASE;
638 idev->init = true;
639
640 mutex_init(&idev->lock);
641 ret = imr_debugfs_register(idev);
642 if (ret != 0)
643 pr_warn("debugfs register failed!\n");
644 imr_fixup_memmap(idev);
645 return 0;
646}
647
648/**
649 * imr_exit - exit point for IMR code.
650 *
651 * Deregisters debugfs, leave IMR state as-is.
652 *
653 * return:
654 */
655static void __exit imr_exit(void)
656{
657 imr_debugfs_unregister(&imr_dev);
658}
659
660module_init(imr_init);
661module_exit(imr_exit);
662
663MODULE_AUTHOR("Bryan O'Donoghue <pure.logic@nexus-software.ie>");
664MODULE_DESCRIPTION("Intel Isolated Memory Region driver");
665MODULE_LICENSE("Dual BSD/GPL");