opal-msglog.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ssize_t opal_msglog_read(struct file *file, struct kobject *kobj,
  32. struct bin_attribute *bin_attr, char *to,
  33. loff_t pos, size_t count)
  34. {
  35. struct memcons *mc = bin_attr->private;
  36. const char *conbuf;
  37. ssize_t ret;
  38. size_t first_read = 0;
  39. uint32_t out_pos, avail;
  40. if (!mc)
  41. return -ENODEV;
  42. out_pos = be32_to_cpu(ACCESS_ONCE(mc->out_pos));
  43. /* Now we've read out_pos, put a barrier in before reading the new
  44. * data it points to in conbuf. */
  45. smp_rmb();
  46. conbuf = phys_to_virt(be64_to_cpu(mc->obuf_phys));
  47. /* When the buffer has wrapped, read from the out_pos marker to the end
  48. * of the buffer, and then read the remaining data as in the un-wrapped
  49. * case. */
  50. if (out_pos & MEMCONS_OUT_POS_WRAP) {
  51. out_pos &= MEMCONS_OUT_POS_MASK;
  52. avail = be32_to_cpu(mc->obuf_size) - out_pos;
  53. ret = memory_read_from_buffer(to, count, &pos,
  54. conbuf + out_pos, avail);
  55. if (ret < 0)
  56. goto out;
  57. first_read = ret;
  58. to += first_read;
  59. count -= first_read;
  60. pos -= avail;
  61. if (count <= 0)
  62. goto out;
  63. }
  64. /* Sanity check. The firmware should not do this to us. */
  65. if (out_pos > be32_to_cpu(mc->obuf_size)) {
  66. pr_err("OPAL: memory console corruption. Aborting read.\n");
  67. return -EINVAL;
  68. }
  69. ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos);
  70. if (ret < 0)
  71. goto out;
  72. ret += first_read;
  73. out:
  74. return ret;
  75. }
  76. static struct bin_attribute opal_msglog_attr = {
  77. .attr = {.name = "msglog", .mode = 0400},
  78. .read = opal_msglog_read
  79. };
  80. void __init opal_msglog_init(void)
  81. {
  82. u64 mcaddr;
  83. struct memcons *mc;
  84. if (of_property_read_u64(opal_node, "ibm,opal-memcons", &mcaddr)) {
  85. pr_warn("OPAL: Property ibm,opal-memcons not found, no message log\n");
  86. return;
  87. }
  88. mc = phys_to_virt(mcaddr);
  89. if (!mc) {
  90. pr_warn("OPAL: memory console address is invalid\n");
  91. return;
  92. }
  93. if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) {
  94. pr_warn("OPAL: memory console version is invalid\n");
  95. return;
  96. }
  97. opal_msglog_attr.private = mc;
  98. if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0)
  99. pr_warn("OPAL: sysfs file creation failed\n");
  100. }