adi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Privileged ADI driver for sparc64
  4. *
  5. * Author: Tom Hromatka <tom.hromatka@oracle.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/module.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/uaccess.h>
  13. #include <asm/asi.h>
  14. #define MAX_BUF_SZ PAGE_SIZE
  15. static int adi_open(struct inode *inode, struct file *file)
  16. {
  17. file->f_mode |= FMODE_UNSIGNED_OFFSET;
  18. return 0;
  19. }
  20. static int read_mcd_tag(unsigned long addr)
  21. {
  22. long err;
  23. int ver;
  24. __asm__ __volatile__(
  25. "1: ldxa [%[addr]] %[asi], %[ver]\n"
  26. " mov 0, %[err]\n"
  27. "2:\n"
  28. " .section .fixup,#alloc,#execinstr\n"
  29. " .align 4\n"
  30. "3: sethi %%hi(2b), %%g1\n"
  31. " jmpl %%g1 + %%lo(2b), %%g0\n"
  32. " mov %[invalid], %[err]\n"
  33. " .previous\n"
  34. " .section __ex_table, \"a\"\n"
  35. " .align 4\n"
  36. " .word 1b, 3b\n"
  37. " .previous\n"
  38. : [ver] "=r" (ver), [err] "=r" (err)
  39. : [addr] "r" (addr), [invalid] "i" (EFAULT),
  40. [asi] "i" (ASI_MCD_REAL)
  41. : "memory", "g1"
  42. );
  43. if (err)
  44. return -EFAULT;
  45. else
  46. return ver;
  47. }
  48. static ssize_t adi_read(struct file *file, char __user *buf,
  49. size_t count, loff_t *offp)
  50. {
  51. size_t ver_buf_sz, bytes_read = 0;
  52. int ver_buf_idx = 0;
  53. loff_t offset;
  54. u8 *ver_buf;
  55. ssize_t ret;
  56. ver_buf_sz = min_t(size_t, count, MAX_BUF_SZ);
  57. ver_buf = kmalloc(ver_buf_sz, GFP_KERNEL);
  58. if (!ver_buf)
  59. return -ENOMEM;
  60. offset = (*offp) * adi_blksize();
  61. while (bytes_read < count) {
  62. ret = read_mcd_tag(offset);
  63. if (ret < 0)
  64. goto out;
  65. ver_buf[ver_buf_idx] = (u8)ret;
  66. ver_buf_idx++;
  67. offset += adi_blksize();
  68. if (ver_buf_idx >= ver_buf_sz) {
  69. if (copy_to_user(buf + bytes_read, ver_buf,
  70. ver_buf_sz)) {
  71. ret = -EFAULT;
  72. goto out;
  73. }
  74. bytes_read += ver_buf_sz;
  75. ver_buf_idx = 0;
  76. ver_buf_sz = min(count - bytes_read,
  77. (size_t)MAX_BUF_SZ);
  78. }
  79. }
  80. (*offp) += bytes_read;
  81. ret = bytes_read;
  82. out:
  83. kfree(ver_buf);
  84. return ret;
  85. }
  86. static int set_mcd_tag(unsigned long addr, u8 ver)
  87. {
  88. long err;
  89. __asm__ __volatile__(
  90. "1: stxa %[ver], [%[addr]] %[asi]\n"
  91. " mov 0, %[err]\n"
  92. "2:\n"
  93. " .section .fixup,#alloc,#execinstr\n"
  94. " .align 4\n"
  95. "3: sethi %%hi(2b), %%g1\n"
  96. " jmpl %%g1 + %%lo(2b), %%g0\n"
  97. " mov %[invalid], %[err]\n"
  98. " .previous\n"
  99. " .section __ex_table, \"a\"\n"
  100. " .align 4\n"
  101. " .word 1b, 3b\n"
  102. " .previous\n"
  103. : [err] "=r" (err)
  104. : [ver] "r" (ver), [addr] "r" (addr),
  105. [invalid] "i" (EFAULT), [asi] "i" (ASI_MCD_REAL)
  106. : "memory", "g1"
  107. );
  108. if (err)
  109. return -EFAULT;
  110. else
  111. return ver;
  112. }
  113. static ssize_t adi_write(struct file *file, const char __user *buf,
  114. size_t count, loff_t *offp)
  115. {
  116. size_t ver_buf_sz, bytes_written = 0;
  117. loff_t offset;
  118. u8 *ver_buf;
  119. ssize_t ret;
  120. int i;
  121. if (count <= 0)
  122. return -EINVAL;
  123. ver_buf_sz = min_t(size_t, count, MAX_BUF_SZ);
  124. ver_buf = kmalloc(ver_buf_sz, GFP_KERNEL);
  125. if (!ver_buf)
  126. return -ENOMEM;
  127. offset = (*offp) * adi_blksize();
  128. do {
  129. if (copy_from_user(ver_buf, &buf[bytes_written],
  130. ver_buf_sz)) {
  131. ret = -EFAULT;
  132. goto out;
  133. }
  134. for (i = 0; i < ver_buf_sz; i++) {
  135. ret = set_mcd_tag(offset, ver_buf[i]);
  136. if (ret < 0)
  137. goto out;
  138. offset += adi_blksize();
  139. }
  140. bytes_written += ver_buf_sz;
  141. ver_buf_sz = min(count - bytes_written, (size_t)MAX_BUF_SZ);
  142. } while (bytes_written < count);
  143. (*offp) += bytes_written;
  144. ret = bytes_written;
  145. out:
  146. __asm__ __volatile__("membar #Sync");
  147. kfree(ver_buf);
  148. return ret;
  149. }
  150. static loff_t adi_llseek(struct file *file, loff_t offset, int whence)
  151. {
  152. loff_t ret = -EINVAL;
  153. switch (whence) {
  154. case SEEK_END:
  155. case SEEK_DATA:
  156. case SEEK_HOLE:
  157. /* unsupported */
  158. return -EINVAL;
  159. case SEEK_CUR:
  160. if (offset == 0)
  161. return file->f_pos;
  162. offset += file->f_pos;
  163. break;
  164. case SEEK_SET:
  165. break;
  166. }
  167. if (offset != file->f_pos) {
  168. file->f_pos = offset;
  169. file->f_version = 0;
  170. ret = offset;
  171. }
  172. return ret;
  173. }
  174. static const struct file_operations adi_fops = {
  175. .owner = THIS_MODULE,
  176. .llseek = adi_llseek,
  177. .open = adi_open,
  178. .read = adi_read,
  179. .write = adi_write,
  180. };
  181. static struct miscdevice adi_miscdev = {
  182. .minor = MISC_DYNAMIC_MINOR,
  183. .name = KBUILD_MODNAME,
  184. .fops = &adi_fops,
  185. };
  186. static int __init adi_init(void)
  187. {
  188. if (!adi_capable())
  189. return -EPERM;
  190. return misc_register(&adi_miscdev);
  191. }
  192. static void __exit adi_exit(void)
  193. {
  194. misc_deregister(&adi_miscdev);
  195. }
  196. module_init(adi_init);
  197. module_exit(adi_exit);
  198. MODULE_AUTHOR("Tom Hromatka <tom.hromatka@oracle.com>");
  199. MODULE_DESCRIPTION("Privileged interface to ADI");
  200. MODULE_VERSION("1.0");
  201. MODULE_LICENSE("GPL v2");