opal.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * PowerNV OPAL high level interfaces
  3. *
  4. * Copyright 2011 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. #undef DEBUG
  12. #include <linux/types.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/interrupt.h>
  16. #include <asm/opal.h>
  17. #include <asm/firmware.h>
  18. #include "powernv.h"
  19. struct opal {
  20. u64 base;
  21. u64 entry;
  22. } opal;
  23. static struct device_node *opal_node;
  24. static DEFINE_SPINLOCK(opal_write_lock);
  25. int __init early_init_dt_scan_opal(unsigned long node,
  26. const char *uname, int depth, void *data)
  27. {
  28. const void *basep, *entryp;
  29. unsigned long basesz, entrysz;
  30. if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
  31. return 0;
  32. basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
  33. entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
  34. if (!basep || !entryp)
  35. return 1;
  36. opal.base = of_read_number(basep, basesz/4);
  37. opal.entry = of_read_number(entryp, entrysz/4);
  38. pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
  39. opal.base, basep, basesz);
  40. pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
  41. opal.entry, entryp, entrysz);
  42. powerpc_firmware_features |= FW_FEATURE_OPAL;
  43. if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
  44. powerpc_firmware_features |= FW_FEATURE_OPALv2;
  45. printk("OPAL V2 detected !\n");
  46. } else {
  47. printk("OPAL V1 detected !\n");
  48. }
  49. return 1;
  50. }
  51. int opal_get_chars(uint32_t vtermno, char *buf, int count)
  52. {
  53. s64 len, rc;
  54. u64 evt;
  55. if (!opal.entry)
  56. return -ENODEV;
  57. opal_poll_events(&evt);
  58. if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0)
  59. return 0;
  60. len = count;
  61. rc = opal_console_read(vtermno, &len, buf);
  62. if (rc == OPAL_SUCCESS)
  63. return len;
  64. return 0;
  65. }
  66. int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
  67. {
  68. int written = 0;
  69. s64 len, rc;
  70. unsigned long flags;
  71. u64 evt;
  72. if (!opal.entry)
  73. return -ENODEV;
  74. /* We want put_chars to be atomic to avoid mangling of hvsi
  75. * packets. To do that, we first test for room and return
  76. * -EAGAIN if there isn't enough.
  77. *
  78. * Unfortunately, opal_console_write_buffer_space() doesn't
  79. * appear to work on opal v1, so we just assume there is
  80. * enough room and be done with it
  81. */
  82. spin_lock_irqsave(&opal_write_lock, flags);
  83. if (firmware_has_feature(FW_FEATURE_OPALv2)) {
  84. rc = opal_console_write_buffer_space(vtermno, &len);
  85. if (rc || len < total_len) {
  86. spin_unlock_irqrestore(&opal_write_lock, flags);
  87. /* Closed -> drop characters */
  88. if (rc)
  89. return total_len;
  90. opal_poll_events(&evt);
  91. return -EAGAIN;
  92. }
  93. }
  94. /* We still try to handle partial completions, though they
  95. * should no longer happen.
  96. */
  97. rc = OPAL_BUSY;
  98. while(total_len > 0 && (rc == OPAL_BUSY ||
  99. rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
  100. len = total_len;
  101. rc = opal_console_write(vtermno, &len, data);
  102. if (rc == OPAL_SUCCESS) {
  103. total_len -= len;
  104. data += len;
  105. written += len;
  106. }
  107. /* This is a bit nasty but we need that for the console to
  108. * flush when there aren't any interrupts. We will clean
  109. * things a bit later to limit that to synchronous path
  110. * such as the kernel console and xmon/udbg
  111. */
  112. do
  113. opal_poll_events(&evt);
  114. while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
  115. }
  116. spin_unlock_irqrestore(&opal_write_lock, flags);
  117. return written;
  118. }
  119. static irqreturn_t opal_interrupt(int irq, void *data)
  120. {
  121. uint64_t events;
  122. opal_handle_interrupt(virq_to_hw(irq), &events);
  123. /* XXX TODO: Do something with the events */
  124. return IRQ_HANDLED;
  125. }
  126. static int __init opal_init(void)
  127. {
  128. struct device_node *np, *consoles;
  129. const u32 *irqs;
  130. int rc, i, irqlen;
  131. opal_node = of_find_node_by_path("/ibm,opal");
  132. if (!opal_node) {
  133. pr_warn("opal: Node not found\n");
  134. return -ENODEV;
  135. }
  136. if (firmware_has_feature(FW_FEATURE_OPALv2))
  137. consoles = of_find_node_by_path("/ibm,opal/consoles");
  138. else
  139. consoles = of_node_get(opal_node);
  140. /* Register serial ports */
  141. for_each_child_of_node(consoles, np) {
  142. if (strcmp(np->name, "serial"))
  143. continue;
  144. of_platform_device_create(np, NULL, NULL);
  145. }
  146. of_node_put(consoles);
  147. /* Find all OPAL interrupts and request them */
  148. irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
  149. pr_debug("opal: Found %d interrupts reserved for OPAL\n",
  150. irqs ? (irqlen / 4) : 0);
  151. for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
  152. unsigned int hwirq = be32_to_cpup(irqs);
  153. unsigned int irq = irq_create_mapping(NULL, hwirq);
  154. if (irq == NO_IRQ) {
  155. pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
  156. continue;
  157. }
  158. rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
  159. if (rc)
  160. pr_warning("opal: Error %d requesting irq %d"
  161. " (0x%x)\n", rc, irq, hwirq);
  162. }
  163. return 0;
  164. }
  165. subsys_initcall(opal_init);