opal.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2016 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include "ops.h"
  10. #include "stdio.h"
  11. #include "io.h"
  12. #include <libfdt.h>
  13. #include "../include/asm/opal-api.h"
  14. #ifdef CONFIG_PPC64_BOOT_WRAPPER
  15. /* Global OPAL struct used by opal-call.S */
  16. struct opal {
  17. u64 base;
  18. u64 entry;
  19. } opal;
  20. static u32 opal_con_id;
  21. /* see opal-wrappers.S */
  22. int64_t opal_console_write(int64_t term_number, u64 *length, const u8 *buffer);
  23. int64_t opal_console_read(int64_t term_number, uint64_t *length, u8 *buffer);
  24. int64_t opal_console_write_buffer_space(uint64_t term_number, uint64_t *length);
  25. int64_t opal_console_flush(uint64_t term_number);
  26. int64_t opal_poll_events(uint64_t *outstanding_event_mask);
  27. void opal_kentry(unsigned long fdt_addr, void *vmlinux_addr);
  28. static int opal_con_open(void)
  29. {
  30. /*
  31. * When OPAL loads the boot kernel it stashes the OPAL base and entry
  32. * address in r8 and r9 so the kernel can use the OPAL console
  33. * before unflattening the devicetree. While executing the wrapper will
  34. * probably trash r8 and r9 so this kentry hook restores them before
  35. * entering the decompressed kernel.
  36. */
  37. platform_ops.kentry = opal_kentry;
  38. return 0;
  39. }
  40. static void opal_con_putc(unsigned char c)
  41. {
  42. int64_t rc;
  43. uint64_t olen, len;
  44. do {
  45. rc = opal_console_write_buffer_space(opal_con_id, &olen);
  46. len = be64_to_cpu(olen);
  47. if (rc)
  48. return;
  49. opal_poll_events(NULL);
  50. } while (len < 1);
  51. olen = cpu_to_be64(1);
  52. opal_console_write(opal_con_id, &olen, &c);
  53. }
  54. static void opal_con_close(void)
  55. {
  56. opal_console_flush(opal_con_id);
  57. }
  58. static void opal_init(void)
  59. {
  60. void *opal_node;
  61. opal_node = finddevice("/ibm,opal");
  62. if (!opal_node)
  63. return;
  64. if (getprop(opal_node, "opal-base-address", &opal.base, sizeof(u64)) < 0)
  65. return;
  66. opal.base = be64_to_cpu(opal.base);
  67. if (getprop(opal_node, "opal-entry-address", &opal.entry, sizeof(u64)) < 0)
  68. return;
  69. opal.entry = be64_to_cpu(opal.entry);
  70. }
  71. int opal_console_init(void *devp, struct serial_console_data *scdp)
  72. {
  73. opal_init();
  74. if (devp) {
  75. int n = getprop(devp, "reg", &opal_con_id, sizeof(u32));
  76. if (n != sizeof(u32))
  77. return -1;
  78. opal_con_id = be32_to_cpu(opal_con_id);
  79. } else
  80. opal_con_id = 0;
  81. scdp->open = opal_con_open;
  82. scdp->putc = opal_con_putc;
  83. scdp->close = opal_con_close;
  84. return 0;
  85. }
  86. #else
  87. int opal_console_init(void *devp, struct serial_console_data *scdp)
  88. {
  89. return -1;
  90. }
  91. #endif /* __powerpc64__ */