imr.c 16 KB

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