opal-lpc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * PowerNV LPC bus handling.
  3. *
  4. * Copyright 2013 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 <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/bug.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <asm/machdep.h>
  18. #include <asm/firmware.h>
  19. #include <asm/xics.h>
  20. #include <asm/opal.h>
  21. #include <asm/prom.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/debug.h>
  24. static int opal_lpc_chip_id = -1;
  25. static u8 opal_lpc_inb(unsigned long port)
  26. {
  27. int64_t rc;
  28. __be32 data;
  29. if (opal_lpc_chip_id < 0 || port > 0xffff)
  30. return 0xff;
  31. rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 1);
  32. return rc ? 0xff : be32_to_cpu(data);
  33. }
  34. static __le16 __opal_lpc_inw(unsigned long port)
  35. {
  36. int64_t rc;
  37. __be32 data;
  38. if (opal_lpc_chip_id < 0 || port > 0xfffe)
  39. return 0xffff;
  40. if (port & 1)
  41. return (__le16)opal_lpc_inb(port) << 8 | opal_lpc_inb(port + 1);
  42. rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 2);
  43. return rc ? 0xffff : be32_to_cpu(data);
  44. }
  45. static u16 opal_lpc_inw(unsigned long port)
  46. {
  47. return le16_to_cpu(__opal_lpc_inw(port));
  48. }
  49. static __le32 __opal_lpc_inl(unsigned long port)
  50. {
  51. int64_t rc;
  52. __be32 data;
  53. if (opal_lpc_chip_id < 0 || port > 0xfffc)
  54. return 0xffffffff;
  55. if (port & 3)
  56. return (__le32)opal_lpc_inb(port ) << 24 |
  57. (__le32)opal_lpc_inb(port + 1) << 16 |
  58. (__le32)opal_lpc_inb(port + 2) << 8 |
  59. opal_lpc_inb(port + 3);
  60. rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 4);
  61. return rc ? 0xffffffff : be32_to_cpu(data);
  62. }
  63. static u32 opal_lpc_inl(unsigned long port)
  64. {
  65. return le32_to_cpu(__opal_lpc_inl(port));
  66. }
  67. static void opal_lpc_outb(u8 val, unsigned long port)
  68. {
  69. if (opal_lpc_chip_id < 0 || port > 0xffff)
  70. return;
  71. opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 1);
  72. }
  73. static void __opal_lpc_outw(__le16 val, unsigned long port)
  74. {
  75. if (opal_lpc_chip_id < 0 || port > 0xfffe)
  76. return;
  77. if (port & 1) {
  78. opal_lpc_outb(val >> 8, port);
  79. opal_lpc_outb(val , port + 1);
  80. return;
  81. }
  82. opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 2);
  83. }
  84. static void opal_lpc_outw(u16 val, unsigned long port)
  85. {
  86. __opal_lpc_outw(cpu_to_le16(val), port);
  87. }
  88. static void __opal_lpc_outl(__le32 val, unsigned long port)
  89. {
  90. if (opal_lpc_chip_id < 0 || port > 0xfffc)
  91. return;
  92. if (port & 3) {
  93. opal_lpc_outb(val >> 24, port);
  94. opal_lpc_outb(val >> 16, port + 1);
  95. opal_lpc_outb(val >> 8, port + 2);
  96. opal_lpc_outb(val , port + 3);
  97. return;
  98. }
  99. opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 4);
  100. }
  101. static void opal_lpc_outl(u32 val, unsigned long port)
  102. {
  103. __opal_lpc_outl(cpu_to_le32(val), port);
  104. }
  105. static void opal_lpc_insb(unsigned long p, void *b, unsigned long c)
  106. {
  107. u8 *ptr = b;
  108. while(c--)
  109. *(ptr++) = opal_lpc_inb(p);
  110. }
  111. static void opal_lpc_insw(unsigned long p, void *b, unsigned long c)
  112. {
  113. __le16 *ptr = b;
  114. while(c--)
  115. *(ptr++) = __opal_lpc_inw(p);
  116. }
  117. static void opal_lpc_insl(unsigned long p, void *b, unsigned long c)
  118. {
  119. __le32 *ptr = b;
  120. while(c--)
  121. *(ptr++) = __opal_lpc_inl(p);
  122. }
  123. static void opal_lpc_outsb(unsigned long p, const void *b, unsigned long c)
  124. {
  125. const u8 *ptr = b;
  126. while(c--)
  127. opal_lpc_outb(*(ptr++), p);
  128. }
  129. static void opal_lpc_outsw(unsigned long p, const void *b, unsigned long c)
  130. {
  131. const __le16 *ptr = b;
  132. while(c--)
  133. __opal_lpc_outw(*(ptr++), p);
  134. }
  135. static void opal_lpc_outsl(unsigned long p, const void *b, unsigned long c)
  136. {
  137. const __le32 *ptr = b;
  138. while(c--)
  139. __opal_lpc_outl(*(ptr++), p);
  140. }
  141. static const struct ppc_pci_io opal_lpc_io = {
  142. .inb = opal_lpc_inb,
  143. .inw = opal_lpc_inw,
  144. .inl = opal_lpc_inl,
  145. .outb = opal_lpc_outb,
  146. .outw = opal_lpc_outw,
  147. .outl = opal_lpc_outl,
  148. .insb = opal_lpc_insb,
  149. .insw = opal_lpc_insw,
  150. .insl = opal_lpc_insl,
  151. .outsb = opal_lpc_outsb,
  152. .outsw = opal_lpc_outsw,
  153. .outsl = opal_lpc_outsl,
  154. };
  155. #ifdef CONFIG_DEBUG_FS
  156. struct lpc_debugfs_entry {
  157. enum OpalLPCAddressType lpc_type;
  158. };
  159. static ssize_t lpc_debug_read(struct file *filp, char __user *ubuf,
  160. size_t count, loff_t *ppos)
  161. {
  162. struct lpc_debugfs_entry *lpc = filp->private_data;
  163. u32 data, pos, len, todo;
  164. int rc;
  165. if (!access_ok(VERIFY_WRITE, ubuf, count))
  166. return -EFAULT;
  167. todo = count;
  168. while (todo) {
  169. pos = *ppos;
  170. /*
  171. * Select access size based on count and alignment and
  172. * access type. IO and MEM only support byte acceses,
  173. * FW supports all 3.
  174. */
  175. len = 1;
  176. if (lpc->lpc_type == OPAL_LPC_FW) {
  177. if (todo > 3 && (pos & 3) == 0)
  178. len = 4;
  179. else if (todo > 1 && (pos & 1) == 0)
  180. len = 2;
  181. }
  182. rc = opal_lpc_read(opal_lpc_chip_id, lpc->lpc_type, pos,
  183. &data, len);
  184. if (rc)
  185. return -ENXIO;
  186. /*
  187. * Now there is some trickery with the data returned by OPAL
  188. * as it's the desired data right justified in a 32-bit BE
  189. * word.
  190. *
  191. * This is a very bad interface and I'm to blame for it :-(
  192. *
  193. * So we can't just apply a 32-bit swap to what comes from OPAL,
  194. * because user space expects the *bytes* to be in their proper
  195. * respective positions (ie, LPC position).
  196. *
  197. * So what we really want to do here is to shift data right
  198. * appropriately on a LE kernel.
  199. *
  200. * IE. If the LPC transaction has bytes B0, B1, B2 and B3 in that
  201. * order, we have in memory written to by OPAL at the "data"
  202. * pointer:
  203. *
  204. * Bytes: OPAL "data" LE "data"
  205. * 32-bit: B0 B1 B2 B3 B0B1B2B3 B3B2B1B0
  206. * 16-bit: B0 B1 0000B0B1 B1B00000
  207. * 8-bit: B0 000000B0 B0000000
  208. *
  209. * So a BE kernel will have the leftmost of the above in the MSB
  210. * and rightmost in the LSB and can just then "cast" the u32 "data"
  211. * down to the appropriate quantity and write it.
  212. *
  213. * However, an LE kernel can't. It doesn't need to swap because a
  214. * load from data followed by a store to user are going to preserve
  215. * the byte ordering which is the wire byte order which is what the
  216. * user wants, but in order to "crop" to the right size, we need to
  217. * shift right first.
  218. */
  219. switch(len) {
  220. case 4:
  221. rc = __put_user((u32)data, (u32 __user *)ubuf);
  222. break;
  223. case 2:
  224. #ifdef __LITTLE_ENDIAN__
  225. data >>= 16;
  226. #endif
  227. rc = __put_user((u16)data, (u16 __user *)ubuf);
  228. break;
  229. default:
  230. #ifdef __LITTLE_ENDIAN__
  231. data >>= 24;
  232. #endif
  233. rc = __put_user((u8)data, (u8 __user *)ubuf);
  234. break;
  235. }
  236. if (rc)
  237. return -EFAULT;
  238. *ppos += len;
  239. ubuf += len;
  240. todo -= len;
  241. }
  242. return count;
  243. }
  244. static ssize_t lpc_debug_write(struct file *filp, const char __user *ubuf,
  245. size_t count, loff_t *ppos)
  246. {
  247. struct lpc_debugfs_entry *lpc = filp->private_data;
  248. u32 data, pos, len, todo;
  249. int rc;
  250. if (!access_ok(VERIFY_READ, ubuf, count))
  251. return -EFAULT;
  252. todo = count;
  253. while (todo) {
  254. pos = *ppos;
  255. /*
  256. * Select access size based on count and alignment and
  257. * access type. IO and MEM only support byte acceses,
  258. * FW supports all 3.
  259. */
  260. len = 1;
  261. if (lpc->lpc_type == OPAL_LPC_FW) {
  262. if (todo > 3 && (pos & 3) == 0)
  263. len = 4;
  264. else if (todo > 1 && (pos & 1) == 0)
  265. len = 2;
  266. }
  267. /*
  268. * Similarly to the read case, we have some trickery here but
  269. * it's different to handle. We need to pass the value to OPAL in
  270. * a register whose layout depends on the access size. We want
  271. * to reproduce the memory layout of the user, however we aren't
  272. * doing a load from user and a store to another memory location
  273. * which would achieve that. Here we pass the value to OPAL via
  274. * a register which is expected to contain the "BE" interpretation
  275. * of the byte sequence. IE: for a 32-bit access, byte 0 should be
  276. * in the MSB. So here we *do* need to byteswap on LE.
  277. *
  278. * User bytes: LE "data" OPAL "data"
  279. * 32-bit: B0 B1 B2 B3 B3B2B1B0 B0B1B2B3
  280. * 16-bit: B0 B1 0000B1B0 0000B0B1
  281. * 8-bit: B0 000000B0 000000B0
  282. */
  283. switch(len) {
  284. case 4:
  285. rc = __get_user(data, (u32 __user *)ubuf);
  286. data = cpu_to_be32(data);
  287. break;
  288. case 2:
  289. rc = __get_user(data, (u16 __user *)ubuf);
  290. data = cpu_to_be16(data);
  291. break;
  292. default:
  293. rc = __get_user(data, (u8 __user *)ubuf);
  294. break;
  295. }
  296. if (rc)
  297. return -EFAULT;
  298. rc = opal_lpc_write(opal_lpc_chip_id, lpc->lpc_type, pos,
  299. data, len);
  300. if (rc)
  301. return -ENXIO;
  302. *ppos += len;
  303. ubuf += len;
  304. todo -= len;
  305. }
  306. return count;
  307. }
  308. static const struct file_operations lpc_fops = {
  309. .read = lpc_debug_read,
  310. .write = lpc_debug_write,
  311. .open = simple_open,
  312. .llseek = default_llseek,
  313. };
  314. static int opal_lpc_debugfs_create_type(struct dentry *folder,
  315. const char *fname,
  316. enum OpalLPCAddressType type)
  317. {
  318. struct lpc_debugfs_entry *entry;
  319. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  320. if (!entry)
  321. return -ENOMEM;
  322. entry->lpc_type = type;
  323. debugfs_create_file(fname, 0600, folder, entry, &lpc_fops);
  324. return 0;
  325. }
  326. static int opal_lpc_init_debugfs(void)
  327. {
  328. struct dentry *root;
  329. int rc = 0;
  330. if (opal_lpc_chip_id < 0)
  331. return -ENODEV;
  332. root = debugfs_create_dir("lpc", powerpc_debugfs_root);
  333. rc |= opal_lpc_debugfs_create_type(root, "io", OPAL_LPC_IO);
  334. rc |= opal_lpc_debugfs_create_type(root, "mem", OPAL_LPC_MEM);
  335. rc |= opal_lpc_debugfs_create_type(root, "fw", OPAL_LPC_FW);
  336. return rc;
  337. }
  338. machine_device_initcall(powernv, opal_lpc_init_debugfs);
  339. #endif /* CONFIG_DEBUG_FS */
  340. void opal_lpc_init(void)
  341. {
  342. struct device_node *np;
  343. /*
  344. * Look for a Power8 LPC bus tagged as "primary",
  345. * we currently support only one though the OPAL APIs
  346. * support any number.
  347. */
  348. for_each_compatible_node(np, NULL, "ibm,power8-lpc") {
  349. if (!of_device_is_available(np))
  350. continue;
  351. if (!of_get_property(np, "primary", NULL))
  352. continue;
  353. opal_lpc_chip_id = of_get_ibm_chip_id(np);
  354. break;
  355. }
  356. if (opal_lpc_chip_id < 0)
  357. return;
  358. /* Setup special IO ops */
  359. ppc_pci_io = opal_lpc_io;
  360. isa_io_special = true;
  361. pr_info("OPAL: Power8 LPC bus found, chip ID %d\n", opal_lpc_chip_id);
  362. }