ibm_rtl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * IBM Real-Time Linux driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2010
  19. *
  20. * Author: Keith Mannthey <kmannth@us.ibm.com>
  21. * Vernon Mauery <vernux@us.ibm.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/dmi.h>
  30. #include <linux/mutex.h>
  31. #include <asm/bios_ebda.h>
  32. static bool force;
  33. module_param(force, bool, 0);
  34. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  35. static bool debug;
  36. module_param(debug, bool, 0644);
  37. MODULE_PARM_DESC(debug, "Show debug output");
  38. MODULE_LICENSE("GPL");
  39. MODULE_AUTHOR("Keith Mannthey <kmmanth@us.ibm.com>");
  40. MODULE_AUTHOR("Vernon Mauery <vernux@us.ibm.com>");
  41. #define RTL_ADDR_TYPE_IO 1
  42. #define RTL_ADDR_TYPE_MMIO 2
  43. #define RTL_CMD_ENTER_PRTM 1
  44. #define RTL_CMD_EXIT_PRTM 2
  45. /* The RTL table as presented by the EBDA: */
  46. struct ibm_rtl_table {
  47. char signature[5]; /* signature should be "_RTL_" */
  48. u8 version;
  49. u8 rt_status;
  50. u8 command;
  51. u8 command_status;
  52. u8 cmd_address_type;
  53. u8 cmd_granularity;
  54. u8 cmd_offset;
  55. u16 reserve1;
  56. u32 cmd_port_address; /* platform dependent address */
  57. u32 cmd_port_value; /* platform dependent value */
  58. } __attribute__((packed));
  59. /* to locate "_RTL_" signature do a masked 5-byte integer compare */
  60. #define RTL_SIGNATURE 0x0000005f4c54525fULL
  61. #define RTL_MASK 0x000000ffffffffffULL
  62. #define RTL_DEBUG(A, ...) do { \
  63. if (debug) \
  64. pr_info("ibm-rtl: " A, ##__VA_ARGS__ ); \
  65. } while (0)
  66. static DEFINE_MUTEX(rtl_lock);
  67. static struct ibm_rtl_table __iomem *rtl_table;
  68. static void __iomem *ebda_map;
  69. static void __iomem *rtl_cmd_addr;
  70. static u8 rtl_cmd_type;
  71. static u8 rtl_cmd_width;
  72. static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
  73. {
  74. if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
  75. return ioremap(addr, len);
  76. return ioport_map(addr, len);
  77. }
  78. static void rtl_port_unmap(void __iomem *addr)
  79. {
  80. if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
  81. iounmap(addr);
  82. else
  83. ioport_unmap(addr);
  84. }
  85. static int ibm_rtl_write(u8 value)
  86. {
  87. int ret = 0, count = 0;
  88. static u32 cmd_port_val;
  89. RTL_DEBUG("%s(%d)\n", __FUNCTION__, value);
  90. value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
  91. mutex_lock(&rtl_lock);
  92. if (ioread8(&rtl_table->rt_status) != value) {
  93. iowrite8(value, &rtl_table->command);
  94. switch (rtl_cmd_width) {
  95. case 8:
  96. cmd_port_val = ioread8(&rtl_table->cmd_port_value);
  97. RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
  98. iowrite8((u8)cmd_port_val, rtl_cmd_addr);
  99. break;
  100. case 16:
  101. cmd_port_val = ioread16(&rtl_table->cmd_port_value);
  102. RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
  103. iowrite16((u16)cmd_port_val, rtl_cmd_addr);
  104. break;
  105. case 32:
  106. cmd_port_val = ioread32(&rtl_table->cmd_port_value);
  107. RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
  108. iowrite32(cmd_port_val, rtl_cmd_addr);
  109. break;
  110. }
  111. while (ioread8(&rtl_table->command)) {
  112. msleep(10);
  113. if (count++ > 500) {
  114. pr_err("ibm-rtl: Hardware not responding to "
  115. "mode switch request\n");
  116. ret = -EIO;
  117. break;
  118. }
  119. }
  120. if (ioread8(&rtl_table->command_status)) {
  121. RTL_DEBUG("command_status reports failed command\n");
  122. ret = -EIO;
  123. }
  124. }
  125. mutex_unlock(&rtl_lock);
  126. return ret;
  127. }
  128. static ssize_t rtl_show_version(struct sysdev_class * dev,
  129. struct sysdev_class_attribute *attr,
  130. char *buf)
  131. {
  132. return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
  133. }
  134. static ssize_t rtl_show_state(struct sysdev_class *dev,
  135. struct sysdev_class_attribute *attr,
  136. char *buf)
  137. {
  138. return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
  139. }
  140. static ssize_t rtl_set_state(struct sysdev_class *dev,
  141. struct sysdev_class_attribute *attr,
  142. const char *buf,
  143. size_t count)
  144. {
  145. ssize_t ret;
  146. if (count < 1 || count > 2)
  147. return -EINVAL;
  148. switch (buf[0]) {
  149. case '0':
  150. ret = ibm_rtl_write(0);
  151. break;
  152. case '1':
  153. ret = ibm_rtl_write(1);
  154. break;
  155. default:
  156. ret = -EINVAL;
  157. }
  158. if (ret >= 0)
  159. ret = count;
  160. return ret;
  161. }
  162. static struct sysdev_class class_rtl = {
  163. .name = "ibm_rtl",
  164. };
  165. static SYSDEV_CLASS_ATTR(version, S_IRUGO, rtl_show_version, NULL);
  166. static SYSDEV_CLASS_ATTR(state, 0600, rtl_show_state, rtl_set_state);
  167. static struct sysdev_class_attribute *rtl_attributes[] = {
  168. &attr_version,
  169. &attr_state,
  170. NULL
  171. };
  172. static int rtl_setup_sysfs(void) {
  173. int ret, i;
  174. ret = sysdev_class_register(&class_rtl);
  175. if (!ret) {
  176. for (i = 0; rtl_attributes[i]; i ++)
  177. sysdev_class_create_file(&class_rtl, rtl_attributes[i]);
  178. }
  179. return ret;
  180. }
  181. static void rtl_teardown_sysfs(void) {
  182. int i;
  183. for (i = 0; rtl_attributes[i]; i ++)
  184. sysdev_class_remove_file(&class_rtl, rtl_attributes[i]);
  185. sysdev_class_unregister(&class_rtl);
  186. }
  187. static int dmi_check_cb(const struct dmi_system_id *id)
  188. {
  189. RTL_DEBUG("found IBM server '%s'\n", id->ident);
  190. return 0;
  191. }
  192. #define ibm_dmi_entry(NAME, TYPE) \
  193. { \
  194. .ident = NAME, \
  195. .matches = { \
  196. DMI_MATCH(DMI_SYS_VENDOR, "IBM"), \
  197. DMI_MATCH(DMI_PRODUCT_NAME, TYPE), \
  198. }, \
  199. .callback = dmi_check_cb \
  200. }
  201. static struct dmi_system_id __initdata ibm_rtl_dmi_table[] = {
  202. ibm_dmi_entry("BladeCenter LS21", "7971"),
  203. ibm_dmi_entry("BladeCenter LS22", "7901"),
  204. ibm_dmi_entry("BladeCenter HS21 XM", "7995"),
  205. ibm_dmi_entry("BladeCenter HS22", "7870"),
  206. ibm_dmi_entry("BladeCenter HS22V", "7871"),
  207. ibm_dmi_entry("System x3550 M2", "7946"),
  208. ibm_dmi_entry("System x3650 M2", "7947"),
  209. ibm_dmi_entry("System x3550 M3", "7944"),
  210. ibm_dmi_entry("System x3650 M3", "7945"),
  211. { }
  212. };
  213. static int __init ibm_rtl_init(void) {
  214. unsigned long ebda_addr, ebda_size;
  215. unsigned int ebda_kb;
  216. int ret = -ENODEV, i;
  217. if (force)
  218. pr_warning("ibm-rtl: module loaded by force\n");
  219. /* first ensure that we are running on IBM HW */
  220. else if (!dmi_check_system(ibm_rtl_dmi_table))
  221. return -ENODEV;
  222. /* Get the address for the Extended BIOS Data Area */
  223. ebda_addr = get_bios_ebda();
  224. if (!ebda_addr) {
  225. RTL_DEBUG("no BIOS EBDA found\n");
  226. return -ENODEV;
  227. }
  228. ebda_map = ioremap(ebda_addr, 4);
  229. if (!ebda_map)
  230. return -ENOMEM;
  231. /* First word in the EDBA is the Size in KB */
  232. ebda_kb = ioread16(ebda_map);
  233. RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
  234. if (ebda_kb == 0)
  235. goto out;
  236. iounmap(ebda_map);
  237. ebda_size = ebda_kb*1024;
  238. /* Remap the whole table */
  239. ebda_map = ioremap(ebda_addr, ebda_size);
  240. if (!ebda_map)
  241. return -ENOMEM;
  242. /* search for the _RTL_ signature at the start of the table */
  243. for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
  244. struct ibm_rtl_table __iomem * tmp;
  245. tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
  246. if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
  247. phys_addr_t addr;
  248. unsigned int plen;
  249. RTL_DEBUG("found RTL_SIGNATURE at %#llx\n", (u64)tmp);
  250. rtl_table = tmp;
  251. /* The address, value, width and offset are platform
  252. * dependent and found in the ibm_rtl_table */
  253. rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
  254. rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
  255. RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
  256. rtl_cmd_width, rtl_cmd_type);
  257. addr = ioread32(&rtl_table->cmd_port_address);
  258. RTL_DEBUG("addr = %#llx\n", addr);
  259. plen = rtl_cmd_width/sizeof(char);
  260. rtl_cmd_addr = rtl_port_map(addr, plen);
  261. RTL_DEBUG("rtl_cmd_addr = %#llx\n", (u64)rtl_cmd_addr);
  262. if (!rtl_cmd_addr) {
  263. ret = -ENOMEM;
  264. break;
  265. }
  266. ret = rtl_setup_sysfs();
  267. break;
  268. }
  269. }
  270. out:
  271. if (ret) {
  272. iounmap(ebda_map);
  273. rtl_port_unmap(rtl_cmd_addr);
  274. }
  275. return ret;
  276. }
  277. static void __exit ibm_rtl_exit(void)
  278. {
  279. if (rtl_table) {
  280. RTL_DEBUG("cleaning up");
  281. /* do not leave the machine in SMI-free mode */
  282. ibm_rtl_write(0);
  283. /* unmap, unlink and remove all traces */
  284. rtl_teardown_sysfs();
  285. iounmap(ebda_map);
  286. rtl_port_unmap(rtl_cmd_addr);
  287. }
  288. }
  289. module_init(ibm_rtl_init);
  290. module_exit(ibm_rtl_exit);