imr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /**
  2. * imr.c -- Intel Isolated Memory Region driver
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <asm-generic/sections.h>
  26. #include <asm/cpu_device_id.h>
  27. #include <asm/imr.h>
  28. #include <asm/iosf_mbi.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/init.h>
  31. #include <linux/mm.h>
  32. #include <linux/types.h>
  33. struct imr_device {
  34. struct dentry *file;
  35. bool init;
  36. struct mutex lock;
  37. int max_imr;
  38. int reg_base;
  39. };
  40. static struct imr_device imr_dev;
  41. /*
  42. * IMR read/write mask control registers.
  43. * See quark-x1000-datasheet.pdf sections 12.7.4.5 and 12.7.4.6 for
  44. * bit definitions.
  45. *
  46. * addr_hi
  47. * 31 Lock bit
  48. * 30:24 Reserved
  49. * 23:2 1 KiB aligned lo address
  50. * 1:0 Reserved
  51. *
  52. * addr_hi
  53. * 31:24 Reserved
  54. * 23:2 1 KiB aligned hi address
  55. * 1:0 Reserved
  56. */
  57. #define IMR_LOCK BIT(31)
  58. struct imr_regs {
  59. u32 addr_lo;
  60. u32 addr_hi;
  61. u32 rmask;
  62. u32 wmask;
  63. };
  64. #define IMR_NUM_REGS (sizeof(struct imr_regs)/sizeof(u32))
  65. #define IMR_SHIFT 8
  66. #define imr_to_phys(x) ((x) << IMR_SHIFT)
  67. #define phys_to_imr(x) ((x) >> IMR_SHIFT)
  68. /**
  69. * imr_is_enabled - true if an IMR is enabled false otherwise.
  70. *
  71. * Determines if an IMR is enabled based on address range and read/write
  72. * mask. An IMR set with an address range set to zero and a read/write
  73. * access mask set to all is considered to be disabled. An IMR in any
  74. * other state - for example set to zero but without read/write access
  75. * all is considered to be enabled. This definition of disabled is how
  76. * firmware switches off an IMR and is maintained in kernel for
  77. * consistency.
  78. *
  79. * @imr: pointer to IMR descriptor.
  80. * @return: true if IMR enabled false if disabled.
  81. */
  82. static inline int imr_is_enabled(struct imr_regs *imr)
  83. {
  84. return !(imr->rmask == IMR_READ_ACCESS_ALL &&
  85. imr->wmask == IMR_WRITE_ACCESS_ALL &&
  86. imr_to_phys(imr->addr_lo) == 0 &&
  87. imr_to_phys(imr->addr_hi) == 0);
  88. }
  89. /**
  90. * imr_read - read an IMR at a given index.
  91. *
  92. * Requires caller to hold imr mutex.
  93. *
  94. * @idev: pointer to imr_device structure.
  95. * @imr_id: IMR entry to read.
  96. * @imr: IMR structure representing address and access masks.
  97. * @return: 0 on success or error code passed from mbi_iosf on failure.
  98. */
  99. static int imr_read(struct imr_device *idev, u32 imr_id, struct imr_regs *imr)
  100. {
  101. u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
  102. int ret;
  103. ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_lo);
  104. if (ret)
  105. return ret;
  106. ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_hi);
  107. if (ret)
  108. return ret;
  109. ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->rmask);
  110. if (ret)
  111. return ret;
  112. return iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->wmask);
  113. }
  114. /**
  115. * imr_write - write an IMR at a given index.
  116. *
  117. * Requires caller to hold imr mutex.
  118. * Note lock bits need to be written independently of address bits.
  119. *
  120. * @idev: pointer to imr_device structure.
  121. * @imr_id: IMR entry to write.
  122. * @imr: IMR structure representing address and access masks.
  123. * @return: 0 on success or error code passed from mbi_iosf on failure.
  124. */
  125. static int imr_write(struct imr_device *idev, u32 imr_id, struct imr_regs *imr)
  126. {
  127. unsigned long flags;
  128. u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
  129. int ret;
  130. local_irq_save(flags);
  131. ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_lo);
  132. if (ret)
  133. goto failed;
  134. ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_hi);
  135. if (ret)
  136. goto failed;
  137. ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->rmask);
  138. if (ret)
  139. goto failed;
  140. ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->wmask);
  141. if (ret)
  142. goto failed;
  143. local_irq_restore(flags);
  144. return 0;
  145. failed:
  146. /*
  147. * If writing to the IOSF failed then we're in an unknown state,
  148. * likely a very bad state. An IMR in an invalid state will almost
  149. * certainly lead to a memory access violation.
  150. */
  151. local_irq_restore(flags);
  152. WARN(ret, "IOSF-MBI write fail range 0x%08x-0x%08x unreliable\n",
  153. imr_to_phys(imr->addr_lo), imr_to_phys(imr->addr_hi) + IMR_MASK);
  154. return ret;
  155. }
  156. /**
  157. * imr_dbgfs_state_show - print state of IMR registers.
  158. *
  159. * @s: pointer to seq_file for output.
  160. * @unused: unused parameter.
  161. * @return: 0 on success or error code passed from mbi_iosf on failure.
  162. */
  163. static int imr_dbgfs_state_show(struct seq_file *s, void *unused)
  164. {
  165. phys_addr_t base;
  166. phys_addr_t end;
  167. int i;
  168. struct imr_device *idev = s->private;
  169. struct imr_regs imr;
  170. size_t size;
  171. int ret = -ENODEV;
  172. mutex_lock(&idev->lock);
  173. for (i = 0; i < idev->max_imr; i++) {
  174. ret = imr_read(idev, i, &imr);
  175. if (ret)
  176. break;
  177. /*
  178. * Remember to add IMR_ALIGN bytes to size to indicate the
  179. * inherent IMR_ALIGN size bytes contained in the masked away
  180. * lower ten bits.
  181. */
  182. if (imr_is_enabled(&imr)) {
  183. base = imr_to_phys(imr.addr_lo);
  184. end = imr_to_phys(imr.addr_hi) + IMR_MASK;
  185. size = end - base + 1;
  186. } else {
  187. base = 0;
  188. end = 0;
  189. size = 0;
  190. }
  191. seq_printf(s, "imr%02i: base=%pa, end=%pa, size=0x%08zx "
  192. "rmask=0x%08x, wmask=0x%08x, %s, %s\n", i,
  193. &base, &end, size, imr.rmask, imr.wmask,
  194. imr_is_enabled(&imr) ? "enabled " : "disabled",
  195. imr.addr_lo & IMR_LOCK ? "locked" : "unlocked");
  196. }
  197. mutex_unlock(&idev->lock);
  198. return ret;
  199. }
  200. /**
  201. * imr_state_open - debugfs open callback.
  202. *
  203. * @inode: pointer to struct inode.
  204. * @file: pointer to struct file.
  205. * @return: result of single open.
  206. */
  207. static int imr_state_open(struct inode *inode, struct file *file)
  208. {
  209. return single_open(file, imr_dbgfs_state_show, inode->i_private);
  210. }
  211. static const struct file_operations imr_state_ops = {
  212. .open = imr_state_open,
  213. .read = seq_read,
  214. .llseek = seq_lseek,
  215. .release = single_release,
  216. };
  217. /**
  218. * imr_debugfs_register - register debugfs hooks.
  219. *
  220. * @idev: pointer to imr_device structure.
  221. * @return: 0 on success - errno on failure.
  222. */
  223. static int imr_debugfs_register(struct imr_device *idev)
  224. {
  225. idev->file = debugfs_create_file("imr_state", S_IFREG | S_IRUGO, NULL,
  226. idev, &imr_state_ops);
  227. return PTR_ERR_OR_ZERO(idev->file);
  228. }
  229. /**
  230. * imr_check_params - check passed address range IMR alignment and non-zero size
  231. *
  232. * @base: base address of intended IMR.
  233. * @size: size of intended IMR.
  234. * @return: zero on valid range -EINVAL on unaligned base/size.
  235. */
  236. static int imr_check_params(phys_addr_t base, size_t size)
  237. {
  238. if ((base & IMR_MASK) || (size & IMR_MASK)) {
  239. pr_err("base %pa size 0x%08zx must align to 1KiB\n",
  240. &base, size);
  241. return -EINVAL;
  242. }
  243. if (size == 0)
  244. return -EINVAL;
  245. return 0;
  246. }
  247. /**
  248. * imr_raw_size - account for the IMR_ALIGN bytes that addr_hi appends.
  249. *
  250. * IMR addr_hi has a built in offset of plus IMR_ALIGN (0x400) bytes from the
  251. * value in the register. We need to subtract IMR_ALIGN bytes from input sizes
  252. * as a result.
  253. *
  254. * @size: input size bytes.
  255. * @return: reduced size.
  256. */
  257. static inline size_t imr_raw_size(size_t size)
  258. {
  259. return size - IMR_ALIGN;
  260. }
  261. /**
  262. * imr_address_overlap - detects an address overlap.
  263. *
  264. * @addr: address to check against an existing IMR.
  265. * @imr: imr being checked.
  266. * @return: true for overlap false for no overlap.
  267. */
  268. static inline int imr_address_overlap(phys_addr_t addr, struct imr_regs *imr)
  269. {
  270. return addr >= imr_to_phys(imr->addr_lo) && addr <= imr_to_phys(imr->addr_hi);
  271. }
  272. /**
  273. * imr_add_range - add an Isolated Memory Region.
  274. *
  275. * @base: physical base address of region aligned to 1KiB.
  276. * @size: physical size of region in bytes must be aligned to 1KiB.
  277. * @read_mask: read access mask.
  278. * @write_mask: write access mask.
  279. * @return: zero on success or negative value indicating error.
  280. */
  281. int imr_add_range(phys_addr_t base, size_t size,
  282. unsigned int rmask, unsigned int wmask)
  283. {
  284. phys_addr_t end;
  285. unsigned int i;
  286. struct imr_device *idev = &imr_dev;
  287. struct imr_regs imr;
  288. size_t raw_size;
  289. int reg;
  290. int ret;
  291. if (WARN_ONCE(idev->init == false, "driver not initialized"))
  292. return -ENODEV;
  293. ret = imr_check_params(base, size);
  294. if (ret)
  295. return ret;
  296. /* Tweak the size value. */
  297. raw_size = imr_raw_size(size);
  298. end = base + raw_size;
  299. /*
  300. * Check for reserved IMR value common to firmware, kernel and grub
  301. * indicating a disabled IMR.
  302. */
  303. imr.addr_lo = phys_to_imr(base);
  304. imr.addr_hi = phys_to_imr(end);
  305. imr.rmask = rmask;
  306. imr.wmask = wmask;
  307. if (!imr_is_enabled(&imr))
  308. return -ENOTSUPP;
  309. mutex_lock(&idev->lock);
  310. /*
  311. * Find a free IMR while checking for an existing overlapping range.
  312. * Note there's no restriction in silicon to prevent IMR overlaps.
  313. * For the sake of simplicity and ease in defining/debugging an IMR
  314. * memory map we exclude IMR overlaps.
  315. */
  316. reg = -1;
  317. for (i = 0; i < idev->max_imr; i++) {
  318. ret = imr_read(idev, i, &imr);
  319. if (ret)
  320. goto failed;
  321. /* Find overlap @ base or end of requested range. */
  322. ret = -EINVAL;
  323. if (imr_is_enabled(&imr)) {
  324. if (imr_address_overlap(base, &imr))
  325. goto failed;
  326. if (imr_address_overlap(end, &imr))
  327. goto failed;
  328. } else {
  329. reg = i;
  330. }
  331. }
  332. /* Error out if we have no free IMR entries. */
  333. if (reg == -1) {
  334. ret = -ENOMEM;
  335. goto failed;
  336. }
  337. pr_debug("add %d phys %pa-%pa size %zx mask 0x%08x wmask 0x%08x\n",
  338. reg, &base, &end, raw_size, rmask, wmask);
  339. /* Enable IMR at specified range and access mask. */
  340. imr.addr_lo = phys_to_imr(base);
  341. imr.addr_hi = phys_to_imr(end);
  342. imr.rmask = rmask;
  343. imr.wmask = wmask;
  344. ret = imr_write(idev, reg, &imr);
  345. if (ret < 0) {
  346. /*
  347. * In the highly unlikely event iosf_mbi_write failed
  348. * attempt to rollback the IMR setup skipping the trapping
  349. * of further IOSF write failures.
  350. */
  351. imr.addr_lo = 0;
  352. imr.addr_hi = 0;
  353. imr.rmask = IMR_READ_ACCESS_ALL;
  354. imr.wmask = IMR_WRITE_ACCESS_ALL;
  355. imr_write(idev, reg, &imr);
  356. }
  357. failed:
  358. mutex_unlock(&idev->lock);
  359. return ret;
  360. }
  361. EXPORT_SYMBOL_GPL(imr_add_range);
  362. /**
  363. * __imr_remove_range - delete an Isolated Memory Region.
  364. *
  365. * This function allows you to delete an IMR by its index specified by reg or
  366. * by address range specified by base and size respectively. If you specify an
  367. * index on its own the base and size parameters are ignored.
  368. * imr_remove_range(0, base, size); delete IMR at index 0 base/size ignored.
  369. * imr_remove_range(-1, base, size); delete IMR from base to base+size.
  370. *
  371. * @reg: imr index to remove.
  372. * @base: physical base address of region aligned to 1 KiB.
  373. * @size: physical size of region in bytes aligned to 1 KiB.
  374. * @return: -EINVAL on invalid range or out or range id
  375. * -ENODEV if reg is valid but no IMR exists or is locked
  376. * 0 on success.
  377. */
  378. static int __imr_remove_range(int reg, phys_addr_t base, size_t size)
  379. {
  380. phys_addr_t end;
  381. bool found = false;
  382. unsigned int i;
  383. struct imr_device *idev = &imr_dev;
  384. struct imr_regs imr;
  385. size_t raw_size;
  386. int ret = 0;
  387. if (WARN_ONCE(idev->init == false, "driver not initialized"))
  388. return -ENODEV;
  389. /*
  390. * Validate address range if deleting by address, else we are
  391. * deleting by index where base and size will be ignored.
  392. */
  393. if (reg == -1) {
  394. ret = imr_check_params(base, size);
  395. if (ret)
  396. return ret;
  397. }
  398. /* Tweak the size value. */
  399. raw_size = imr_raw_size(size);
  400. end = base + raw_size;
  401. mutex_lock(&idev->lock);
  402. if (reg >= 0) {
  403. /* If a specific IMR is given try to use it. */
  404. ret = imr_read(idev, reg, &imr);
  405. if (ret)
  406. goto failed;
  407. if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) {
  408. ret = -ENODEV;
  409. goto failed;
  410. }
  411. found = true;
  412. } else {
  413. /* Search for match based on address range. */
  414. for (i = 0; i < idev->max_imr; i++) {
  415. ret = imr_read(idev, i, &imr);
  416. if (ret)
  417. goto failed;
  418. if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK)
  419. continue;
  420. if ((imr_to_phys(imr.addr_lo) == base) &&
  421. (imr_to_phys(imr.addr_hi) == end)) {
  422. found = true;
  423. reg = i;
  424. break;
  425. }
  426. }
  427. }
  428. if (!found) {
  429. ret = -ENODEV;
  430. goto failed;
  431. }
  432. pr_debug("remove %d phys %pa-%pa size %zx\n", reg, &base, &end, raw_size);
  433. /* Tear down the IMR. */
  434. imr.addr_lo = 0;
  435. imr.addr_hi = 0;
  436. imr.rmask = IMR_READ_ACCESS_ALL;
  437. imr.wmask = IMR_WRITE_ACCESS_ALL;
  438. ret = imr_write(idev, reg, &imr);
  439. failed:
  440. mutex_unlock(&idev->lock);
  441. return ret;
  442. }
  443. /**
  444. * imr_remove_range - delete an Isolated Memory Region by address
  445. *
  446. * This function allows you to delete an IMR by an address range specified
  447. * by base and size respectively.
  448. * imr_remove_range(base, size); delete IMR from base to base+size.
  449. *
  450. * @base: physical base address of region aligned to 1 KiB.
  451. * @size: physical size of region in bytes aligned to 1 KiB.
  452. * @return: -EINVAL on invalid range or out or range id
  453. * -ENODEV if reg is valid but no IMR exists or is locked
  454. * 0 on success.
  455. */
  456. int imr_remove_range(phys_addr_t base, size_t size)
  457. {
  458. return __imr_remove_range(-1, base, size);
  459. }
  460. EXPORT_SYMBOL_GPL(imr_remove_range);
  461. /**
  462. * imr_clear - delete an Isolated Memory Region by index
  463. *
  464. * This function allows you to delete an IMR by an address range specified
  465. * by the index of the IMR. Useful for initial sanitization of the IMR
  466. * address map.
  467. * imr_ge(base, size); delete IMR from base to base+size.
  468. *
  469. * @reg: imr index to remove.
  470. * @return: -EINVAL on invalid range or out or range id
  471. * -ENODEV if reg is valid but no IMR exists or is locked
  472. * 0 on success.
  473. */
  474. static inline int imr_clear(int reg)
  475. {
  476. return __imr_remove_range(reg, 0, 0);
  477. }
  478. /**
  479. * imr_fixup_memmap - Tear down IMRs used during bootup.
  480. *
  481. * BIOS and Grub both setup IMRs around compressed kernel, initrd memory
  482. * that need to be removed before the kernel hands out one of the IMR
  483. * encased addresses to a downstream DMA agent such as the SD or Ethernet.
  484. * IMRs on Galileo are setup to immediately reset the system on violation.
  485. * As a result if you're running a root filesystem from SD - you'll need
  486. * the boot-time IMRs torn down or you'll find seemingly random resets when
  487. * using your filesystem.
  488. *
  489. * @idev: pointer to imr_device structure.
  490. * @return:
  491. */
  492. static void __init imr_fixup_memmap(struct imr_device *idev)
  493. {
  494. phys_addr_t base = virt_to_phys(&_text);
  495. size_t size = virt_to_phys(&__end_rodata) - base;
  496. unsigned long start, end;
  497. int i;
  498. int ret;
  499. /* Tear down all existing unlocked IMRs. */
  500. for (i = 0; i < idev->max_imr; i++)
  501. imr_clear(i);
  502. start = (unsigned long)_text;
  503. end = (unsigned long)__end_rodata - 1;
  504. /*
  505. * Setup an unlocked IMR around the physical extent of the kernel
  506. * from the beginning of the .text secton to the end of the
  507. * .rodata section as one physically contiguous block.
  508. *
  509. * We don't round up @size since it is already PAGE_SIZE aligned.
  510. * See vmlinux.lds.S for details.
  511. */
  512. ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
  513. if (ret < 0) {
  514. pr_err("unable to setup IMR for kernel: %zu KiB (%lx - %lx)\n",
  515. size / 1024, start, end);
  516. } else {
  517. pr_info("protecting kernel .text - .rodata: %zu KiB (%lx - %lx)\n",
  518. size / 1024, start, end);
  519. }
  520. }
  521. static const struct x86_cpu_id imr_ids[] __initconst = {
  522. { X86_VENDOR_INTEL, 5, 9 }, /* Intel Quark SoC X1000. */
  523. {}
  524. };
  525. /**
  526. * imr_init - entry point for IMR driver.
  527. *
  528. * return: -ENODEV for no IMR support 0 if good to go.
  529. */
  530. static int __init imr_init(void)
  531. {
  532. struct imr_device *idev = &imr_dev;
  533. int ret;
  534. if (!x86_match_cpu(imr_ids) || !iosf_mbi_available())
  535. return -ENODEV;
  536. idev->max_imr = QUARK_X1000_IMR_MAX;
  537. idev->reg_base = QUARK_X1000_IMR_REGBASE;
  538. idev->init = true;
  539. mutex_init(&idev->lock);
  540. ret = imr_debugfs_register(idev);
  541. if (ret != 0)
  542. pr_warn("debugfs register failed!\n");
  543. imr_fixup_memmap(idev);
  544. return 0;
  545. }
  546. device_initcall(imr_init);