dawr.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DAWR infrastructure
  4. *
  5. * Copyright 2019, Michael Neuling, IBM Corporation.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/export.h>
  9. #include <linux/fs.h>
  10. #include <linux/debugfs.h>
  11. #include <asm/machdep.h>
  12. #include <asm/hvcall.h>
  13. #include <asm/firmware.h>
  14. bool dawr_force_enable;
  15. EXPORT_SYMBOL_GPL(dawr_force_enable);
  16. int set_dawr(int nr, struct arch_hw_breakpoint *brk)
  17. {
  18. unsigned long dawr, dawrx, mrd;
  19. dawr = brk->address;
  20. dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE))
  21. << (63 - 58);
  22. dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) << (63 - 59);
  23. dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) >> 3;
  24. /*
  25. * DAWR length is stored in field MDR bits 48:53. Matches range in
  26. * doublewords (64 bits) biased by -1 eg. 0b000000=1DW and
  27. * 0b111111=64DW.
  28. * brk->hw_len is in bytes.
  29. * This aligns up to double word size, shifts and does the bias.
  30. */
  31. mrd = ((brk->hw_len + 7) >> 3) - 1;
  32. dawrx |= (mrd & 0x3f) << (63 - 53);
  33. if (ppc_md.set_dawr)
  34. return ppc_md.set_dawr(nr, dawr, dawrx);
  35. if (nr == 0) {
  36. mtspr(SPRN_DAWR0, dawr);
  37. mtspr(SPRN_DAWRX0, dawrx);
  38. } else {
  39. mtspr(SPRN_DAWR1, dawr);
  40. mtspr(SPRN_DAWRX1, dawrx);
  41. }
  42. return 0;
  43. }
  44. static void disable_dawrs_cb(void *info)
  45. {
  46. struct arch_hw_breakpoint null_brk = {0};
  47. int i;
  48. for (i = 0; i < nr_wp_slots(); i++)
  49. set_dawr(i, &null_brk);
  50. }
  51. static ssize_t dawr_write_file_bool(struct file *file,
  52. const char __user *user_buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. struct arch_hw_breakpoint null_brk = {0};
  56. size_t rc;
  57. /* Send error to user if they hypervisor won't allow us to write DAWR */
  58. if (!dawr_force_enable &&
  59. firmware_has_feature(FW_FEATURE_LPAR) &&
  60. set_dawr(0, &null_brk) != H_SUCCESS)
  61. return -ENODEV;
  62. rc = debugfs_write_file_bool(file, user_buf, count, ppos);
  63. if (rc)
  64. return rc;
  65. /* If we are clearing, make sure all CPUs have the DAWR cleared */
  66. if (!dawr_force_enable)
  67. smp_call_function(disable_dawrs_cb, NULL, 0);
  68. return rc;
  69. }
  70. static const struct file_operations dawr_enable_fops = {
  71. .read = debugfs_read_file_bool,
  72. .write = dawr_write_file_bool,
  73. .open = simple_open,
  74. .llseek = default_llseek,
  75. };
  76. static int __init dawr_force_setup(void)
  77. {
  78. if (cpu_has_feature(CPU_FTR_DAWR)) {
  79. /* Don't setup sysfs file for user control on P8 */
  80. dawr_force_enable = true;
  81. return 0;
  82. }
  83. if (PVR_VER(mfspr(SPRN_PVR)) == PVR_POWER9) {
  84. /* Turn DAWR off by default, but allow admin to turn it on */
  85. debugfs_create_file_unsafe("dawr_enable_dangerous", 0600,
  86. arch_debugfs_dir,
  87. &dawr_force_enable,
  88. &dawr_enable_fops);
  89. }
  90. return 0;
  91. }
  92. arch_initcall(dawr_force_setup);