opal-lpc.c 9.9 KB

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