opal-msglog.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * PowerNV OPAL in-memory console interface
  3. *
  4. * Copyright 2014 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <asm/io.h>
  12. #include <asm/opal.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/of.h>
  15. #include <linux/types.h>
  16. #include <asm/barrier.h>
  17. /* OPAL in-memory console. Defined in OPAL source at core/console.c */
  18. struct memcons {
  19. __be64 magic;
  20. #define MEMCONS_MAGIC 0x6630696567726173L
  21. __be64 obuf_phys;
  22. __be64 ibuf_phys;
  23. __be32 obuf_size;
  24. __be32 ibuf_size;
  25. __be32 out_pos;
  26. #define MEMCONS_OUT_POS_WRAP 0x80000000u
  27. #define MEMCONS_OUT_POS_MASK 0x00ffffffu
  28. __be32 in_prod;
  29. __be32 in_cons;
  30. };
  31. static struct memcons *opal_memcons = NULL;
  32. ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count)
  33. {
  34. const char *conbuf;
  35. ssize_t ret;
  36. size_t first_read = 0;
  37. uint32_t out_pos, avail;
  38. if (!opal_memcons)
  39. return -ENODEV;
  40. out_pos = be32_to_cpu(READ_ONCE(opal_memcons->out_pos));
  41. /* Now we've read out_pos, put a barrier in before reading the new
  42. * data it points to in conbuf. */
  43. smp_rmb();
  44. conbuf = phys_to_virt(be64_to_cpu(opal_memcons->obuf_phys));
  45. /* When the buffer has wrapped, read from the out_pos marker to the end
  46. * of the buffer, and then read the remaining data as in the un-wrapped
  47. * case. */
  48. if (out_pos & MEMCONS_OUT_POS_WRAP) {
  49. out_pos &= MEMCONS_OUT_POS_MASK;
  50. avail = be32_to_cpu(opal_memcons->obuf_size) - out_pos;
  51. ret = memory_read_from_buffer(to, count, &pos,
  52. conbuf + out_pos, avail);
  53. if (ret < 0)
  54. goto out;
  55. first_read = ret;
  56. to += first_read;
  57. count -= first_read;
  58. pos -= avail;
  59. if (count <= 0)
  60. goto out;
  61. }
  62. /* Sanity check. The firmware should not do this to us. */
  63. if (out_pos > be32_to_cpu(opal_memcons->obuf_size)) {
  64. pr_err("OPAL: memory console corruption. Aborting read.\n");
  65. return -EINVAL;
  66. }
  67. ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos);
  68. if (ret < 0)
  69. goto out;
  70. ret += first_read;
  71. out:
  72. return ret;
  73. }
  74. static ssize_t opal_msglog_read(struct file *file, struct kobject *kobj,
  75. struct bin_attribute *bin_attr, char *to,
  76. loff_t pos, size_t count)
  77. {
  78. return opal_msglog_copy(to, pos, count);
  79. }
  80. static struct bin_attribute opal_msglog_attr = {
  81. .attr = {.name = "msglog", .mode = 0444},
  82. .read = opal_msglog_read
  83. };
  84. void __init opal_msglog_init(void)
  85. {
  86. u64 mcaddr;
  87. struct memcons *mc;
  88. if (of_property_read_u64(opal_node, "ibm,opal-memcons", &mcaddr)) {
  89. pr_warn("OPAL: Property ibm,opal-memcons not found, no message log\n");
  90. return;
  91. }
  92. mc = phys_to_virt(mcaddr);
  93. if (!mc) {
  94. pr_warn("OPAL: memory console address is invalid\n");
  95. return;
  96. }
  97. if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) {
  98. pr_warn("OPAL: memory console version is invalid\n");
  99. return;
  100. }
  101. /* Report maximum size */
  102. opal_msglog_attr.size = be32_to_cpu(mc->ibuf_size) +
  103. be32_to_cpu(mc->obuf_size);
  104. opal_memcons = mc;
  105. }
  106. void __init opal_msglog_sysfs_init(void)
  107. {
  108. if (!opal_memcons) {
  109. pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n");
  110. return;
  111. }
  112. if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0)
  113. pr_warn("OPAL: sysfs file creation failed\n");
  114. }