kfd_chardev.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/export.h>
  24. #include <linux/err.h>
  25. #include <linux/fs.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/compat.h>
  30. #include <uapi/linux/kfd_ioctl.h>
  31. #include <linux/time.h>
  32. #include <linux/mm.h>
  33. #include <linux/uaccess.h>
  34. #include <uapi/asm-generic/mman-common.h>
  35. #include <asm/processor.h>
  36. #include "kfd_priv.h"
  37. static long kfd_ioctl(struct file *, unsigned int, unsigned long);
  38. static int kfd_open(struct inode *, struct file *);
  39. static int kfd_mmap(struct file *, struct vm_area_struct *);
  40. static const char kfd_dev_name[] = "kfd";
  41. static const struct file_operations kfd_fops = {
  42. .owner = THIS_MODULE,
  43. .unlocked_ioctl = kfd_ioctl,
  44. .compat_ioctl = kfd_ioctl,
  45. .open = kfd_open,
  46. .mmap = kfd_mmap,
  47. };
  48. static int kfd_char_dev_major = -1;
  49. static struct class *kfd_class;
  50. struct device *kfd_device;
  51. int kfd_chardev_init(void)
  52. {
  53. int err = 0;
  54. kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
  55. err = kfd_char_dev_major;
  56. if (err < 0)
  57. goto err_register_chrdev;
  58. kfd_class = class_create(THIS_MODULE, kfd_dev_name);
  59. err = PTR_ERR(kfd_class);
  60. if (IS_ERR(kfd_class))
  61. goto err_class_create;
  62. kfd_device = device_create(kfd_class, NULL,
  63. MKDEV(kfd_char_dev_major, 0),
  64. NULL, kfd_dev_name);
  65. err = PTR_ERR(kfd_device);
  66. if (IS_ERR(kfd_device))
  67. goto err_device_create;
  68. return 0;
  69. err_device_create:
  70. class_destroy(kfd_class);
  71. err_class_create:
  72. unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
  73. err_register_chrdev:
  74. return err;
  75. }
  76. void kfd_chardev_exit(void)
  77. {
  78. device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0));
  79. class_destroy(kfd_class);
  80. unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
  81. }
  82. struct device *kfd_chardev(void)
  83. {
  84. return kfd_device;
  85. }
  86. static int kfd_open(struct inode *inode, struct file *filep)
  87. {
  88. struct kfd_process *process;
  89. if (iminor(inode) != 0)
  90. return -ENODEV;
  91. process = kfd_create_process(current);
  92. if (IS_ERR(process))
  93. return PTR_ERR(process);
  94. process->is_32bit_user_mode = is_compat_task();
  95. dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
  96. process->pasid, process->is_32bit_user_mode);
  97. kfd_init_apertures(process);
  98. return 0;
  99. }
  100. static long kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
  101. void __user *arg)
  102. {
  103. return -ENODEV;
  104. }
  105. static long kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
  106. void __user *arg)
  107. {
  108. return -ENODEV;
  109. }
  110. static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
  111. void __user *arg)
  112. {
  113. return -ENODEV;
  114. }
  115. static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
  116. void __user *arg)
  117. {
  118. return -ENODEV;
  119. }
  120. static long kfd_ioctl_set_memory_policy(struct file *filep,
  121. struct kfd_process *p, void __user *arg)
  122. {
  123. return -ENODEV;
  124. }
  125. static long kfd_ioctl_get_clock_counters(struct file *filep,
  126. struct kfd_process *p, void __user *arg)
  127. {
  128. return -ENODEV;
  129. }
  130. static int kfd_ioctl_get_process_apertures(struct file *filp,
  131. struct kfd_process *p, void __user *arg)
  132. {
  133. return -ENODEV;
  134. }
  135. static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  136. {
  137. struct kfd_process *process;
  138. long err = -EINVAL;
  139. dev_dbg(kfd_device,
  140. "ioctl cmd 0x%x (#%d), arg 0x%lx\n",
  141. cmd, _IOC_NR(cmd), arg);
  142. process = kfd_get_process(current);
  143. if (IS_ERR(process))
  144. return PTR_ERR(process);
  145. switch (cmd) {
  146. case KFD_IOC_GET_VERSION:
  147. err = kfd_ioctl_get_version(filep, process, (void __user *)arg);
  148. break;
  149. case KFD_IOC_CREATE_QUEUE:
  150. err = kfd_ioctl_create_queue(filep, process,
  151. (void __user *)arg);
  152. break;
  153. case KFD_IOC_DESTROY_QUEUE:
  154. err = kfd_ioctl_destroy_queue(filep, process,
  155. (void __user *)arg);
  156. break;
  157. case KFD_IOC_SET_MEMORY_POLICY:
  158. err = kfd_ioctl_set_memory_policy(filep, process,
  159. (void __user *)arg);
  160. break;
  161. case KFD_IOC_GET_CLOCK_COUNTERS:
  162. err = kfd_ioctl_get_clock_counters(filep, process,
  163. (void __user *)arg);
  164. break;
  165. case KFD_IOC_GET_PROCESS_APERTURES:
  166. err = kfd_ioctl_get_process_apertures(filep, process,
  167. (void __user *)arg);
  168. break;
  169. case KFD_IOC_UPDATE_QUEUE:
  170. err = kfd_ioctl_update_queue(filep, process,
  171. (void __user *)arg);
  172. break;
  173. default:
  174. dev_err(kfd_device,
  175. "unknown ioctl cmd 0x%x, arg 0x%lx)\n",
  176. cmd, arg);
  177. err = -EINVAL;
  178. break;
  179. }
  180. if (err < 0)
  181. dev_err(kfd_device,
  182. "ioctl error %ld for ioctl cmd 0x%x (#%d)\n",
  183. err, cmd, _IOC_NR(cmd));
  184. return err;
  185. }
  186. static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
  187. {
  188. struct kfd_process *process;
  189. process = kfd_get_process(current);
  190. if (IS_ERR(process))
  191. return PTR_ERR(process);
  192. return kfd_doorbell_mmap(process, vma);
  193. }