kfd_chardev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 <uapi/asm-generic/mman-common.h>
  34. #include <asm/processor.h>
  35. #include "kfd_priv.h"
  36. #include "kfd_device_queue_manager.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. bool is_32bit_user_mode;
  90. if (iminor(inode) != 0)
  91. return -ENODEV;
  92. is_32bit_user_mode = is_compat_task();
  93. if (is_32bit_user_mode == true) {
  94. dev_warn(kfd_device,
  95. "Process %d (32-bit) failed to open /dev/kfd\n"
  96. "32-bit processes are not supported by amdkfd\n",
  97. current->pid);
  98. return -EPERM;
  99. }
  100. process = kfd_create_process(current);
  101. if (IS_ERR(process))
  102. return PTR_ERR(process);
  103. dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
  104. process->pasid, process->is_32bit_user_mode);
  105. return 0;
  106. }
  107. static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
  108. void *data)
  109. {
  110. struct kfd_ioctl_get_version_args *args = data;
  111. int err = 0;
  112. args->major_version = KFD_IOCTL_MAJOR_VERSION;
  113. args->minor_version = KFD_IOCTL_MINOR_VERSION;
  114. return err;
  115. }
  116. static int set_queue_properties_from_user(struct queue_properties *q_properties,
  117. struct kfd_ioctl_create_queue_args *args)
  118. {
  119. if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
  120. pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
  121. return -EINVAL;
  122. }
  123. if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
  124. pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
  125. return -EINVAL;
  126. }
  127. if ((args->ring_base_address) &&
  128. (!access_ok(VERIFY_WRITE,
  129. (const void __user *) args->ring_base_address,
  130. sizeof(uint64_t)))) {
  131. pr_err("kfd: can't access ring base address\n");
  132. return -EFAULT;
  133. }
  134. if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
  135. pr_err("kfd: ring size must be a power of 2 or 0\n");
  136. return -EINVAL;
  137. }
  138. if (!access_ok(VERIFY_WRITE,
  139. (const void __user *) args->read_pointer_address,
  140. sizeof(uint32_t))) {
  141. pr_err("kfd: can't access read pointer\n");
  142. return -EFAULT;
  143. }
  144. if (!access_ok(VERIFY_WRITE,
  145. (const void __user *) args->write_pointer_address,
  146. sizeof(uint32_t))) {
  147. pr_err("kfd: can't access write pointer\n");
  148. return -EFAULT;
  149. }
  150. q_properties->is_interop = false;
  151. q_properties->queue_percent = args->queue_percentage;
  152. q_properties->priority = args->queue_priority;
  153. q_properties->queue_address = args->ring_base_address;
  154. q_properties->queue_size = args->ring_size;
  155. q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
  156. q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
  157. if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
  158. args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
  159. q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
  160. else
  161. return -ENOTSUPP;
  162. if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
  163. q_properties->format = KFD_QUEUE_FORMAT_AQL;
  164. else
  165. q_properties->format = KFD_QUEUE_FORMAT_PM4;
  166. pr_debug("Queue Percentage (%d, %d)\n",
  167. q_properties->queue_percent, args->queue_percentage);
  168. pr_debug("Queue Priority (%d, %d)\n",
  169. q_properties->priority, args->queue_priority);
  170. pr_debug("Queue Address (0x%llX, 0x%llX)\n",
  171. q_properties->queue_address, args->ring_base_address);
  172. pr_debug("Queue Size (0x%llX, %u)\n",
  173. q_properties->queue_size, args->ring_size);
  174. pr_debug("Queue r/w Pointers (0x%llX, 0x%llX)\n",
  175. (uint64_t) q_properties->read_ptr,
  176. (uint64_t) q_properties->write_ptr);
  177. pr_debug("Queue Format (%d)\n", q_properties->format);
  178. return 0;
  179. }
  180. static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
  181. void *data)
  182. {
  183. struct kfd_ioctl_create_queue_args *args = data;
  184. struct kfd_dev *dev;
  185. int err = 0;
  186. unsigned int queue_id;
  187. struct kfd_process_device *pdd;
  188. struct queue_properties q_properties;
  189. memset(&q_properties, 0, sizeof(struct queue_properties));
  190. pr_debug("kfd: creating queue ioctl\n");
  191. err = set_queue_properties_from_user(&q_properties, args);
  192. if (err)
  193. return err;
  194. dev = kfd_device_by_id(args->gpu_id);
  195. if (dev == NULL)
  196. return -EINVAL;
  197. mutex_lock(&p->mutex);
  198. pdd = kfd_bind_process_to_device(dev, p);
  199. if (IS_ERR(pdd)) {
  200. err = -ESRCH;
  201. goto err_bind_process;
  202. }
  203. pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
  204. p->pasid,
  205. dev->id);
  206. err = pqm_create_queue(&p->pqm, dev, filep, &q_properties, 0,
  207. KFD_QUEUE_TYPE_COMPUTE, &queue_id);
  208. if (err != 0)
  209. goto err_create_queue;
  210. args->queue_id = queue_id;
  211. /* Return gpu_id as doorbell offset for mmap usage */
  212. args->doorbell_offset = args->gpu_id << PAGE_SHIFT;
  213. mutex_unlock(&p->mutex);
  214. pr_debug("kfd: queue id %d was created successfully\n", args->queue_id);
  215. pr_debug("ring buffer address == 0x%016llX\n",
  216. args->ring_base_address);
  217. pr_debug("read ptr address == 0x%016llX\n",
  218. args->read_pointer_address);
  219. pr_debug("write ptr address == 0x%016llX\n",
  220. args->write_pointer_address);
  221. return 0;
  222. err_create_queue:
  223. err_bind_process:
  224. mutex_unlock(&p->mutex);
  225. return err;
  226. }
  227. static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
  228. void *data)
  229. {
  230. int retval;
  231. struct kfd_ioctl_destroy_queue_args *args = data;
  232. pr_debug("kfd: destroying queue id %d for PASID %d\n",
  233. args->queue_id,
  234. p->pasid);
  235. mutex_lock(&p->mutex);
  236. retval = pqm_destroy_queue(&p->pqm, args->queue_id);
  237. mutex_unlock(&p->mutex);
  238. return retval;
  239. }
  240. static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
  241. void *data)
  242. {
  243. int retval;
  244. struct kfd_ioctl_update_queue_args *args = data;
  245. struct queue_properties properties;
  246. if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
  247. pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
  248. return -EINVAL;
  249. }
  250. if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
  251. pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
  252. return -EINVAL;
  253. }
  254. if ((args->ring_base_address) &&
  255. (!access_ok(VERIFY_WRITE,
  256. (const void __user *) args->ring_base_address,
  257. sizeof(uint64_t)))) {
  258. pr_err("kfd: can't access ring base address\n");
  259. return -EFAULT;
  260. }
  261. if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
  262. pr_err("kfd: ring size must be a power of 2 or 0\n");
  263. return -EINVAL;
  264. }
  265. properties.queue_address = args->ring_base_address;
  266. properties.queue_size = args->ring_size;
  267. properties.queue_percent = args->queue_percentage;
  268. properties.priority = args->queue_priority;
  269. pr_debug("kfd: updating queue id %d for PASID %d\n",
  270. args->queue_id, p->pasid);
  271. mutex_lock(&p->mutex);
  272. retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
  273. mutex_unlock(&p->mutex);
  274. return retval;
  275. }
  276. static int kfd_ioctl_set_memory_policy(struct file *filep,
  277. struct kfd_process *p, void *data)
  278. {
  279. struct kfd_ioctl_set_memory_policy_args *args = data;
  280. struct kfd_dev *dev;
  281. int err = 0;
  282. struct kfd_process_device *pdd;
  283. enum cache_policy default_policy, alternate_policy;
  284. if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
  285. && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  286. return -EINVAL;
  287. }
  288. if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
  289. && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  290. return -EINVAL;
  291. }
  292. dev = kfd_device_by_id(args->gpu_id);
  293. if (dev == NULL)
  294. return -EINVAL;
  295. mutex_lock(&p->mutex);
  296. pdd = kfd_bind_process_to_device(dev, p);
  297. if (IS_ERR(pdd)) {
  298. err = -ESRCH;
  299. goto out;
  300. }
  301. default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  302. ? cache_policy_coherent : cache_policy_noncoherent;
  303. alternate_policy =
  304. (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  305. ? cache_policy_coherent : cache_policy_noncoherent;
  306. if (!dev->dqm->set_cache_memory_policy(dev->dqm,
  307. &pdd->qpd,
  308. default_policy,
  309. alternate_policy,
  310. (void __user *)args->alternate_aperture_base,
  311. args->alternate_aperture_size))
  312. err = -EINVAL;
  313. out:
  314. mutex_unlock(&p->mutex);
  315. return err;
  316. }
  317. static int kfd_ioctl_get_clock_counters(struct file *filep,
  318. struct kfd_process *p, void *data)
  319. {
  320. struct kfd_ioctl_get_clock_counters_args *args = data;
  321. struct kfd_dev *dev;
  322. struct timespec time;
  323. dev = kfd_device_by_id(args->gpu_id);
  324. if (dev == NULL)
  325. return -EINVAL;
  326. /* Reading GPU clock counter from KGD */
  327. args->gpu_clock_counter = kfd2kgd->get_gpu_clock_counter(dev->kgd);
  328. /* No access to rdtsc. Using raw monotonic time */
  329. getrawmonotonic(&time);
  330. args->cpu_clock_counter = (uint64_t)timespec_to_ns(&time);
  331. get_monotonic_boottime(&time);
  332. args->system_clock_counter = (uint64_t)timespec_to_ns(&time);
  333. /* Since the counter is in nano-seconds we use 1GHz frequency */
  334. args->system_clock_freq = 1000000000;
  335. return 0;
  336. }
  337. static int kfd_ioctl_get_process_apertures(struct file *filp,
  338. struct kfd_process *p, void *data)
  339. {
  340. struct kfd_ioctl_get_process_apertures_args *args = data;
  341. struct kfd_process_device_apertures *pAperture;
  342. struct kfd_process_device *pdd;
  343. dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
  344. args->num_of_nodes = 0;
  345. mutex_lock(&p->mutex);
  346. /*if the process-device list isn't empty*/
  347. if (kfd_has_process_device_data(p)) {
  348. /* Run over all pdd of the process */
  349. pdd = kfd_get_first_process_device_data(p);
  350. do {
  351. pAperture =
  352. &args->process_apertures[args->num_of_nodes];
  353. pAperture->gpu_id = pdd->dev->id;
  354. pAperture->lds_base = pdd->lds_base;
  355. pAperture->lds_limit = pdd->lds_limit;
  356. pAperture->gpuvm_base = pdd->gpuvm_base;
  357. pAperture->gpuvm_limit = pdd->gpuvm_limit;
  358. pAperture->scratch_base = pdd->scratch_base;
  359. pAperture->scratch_limit = pdd->scratch_limit;
  360. dev_dbg(kfd_device,
  361. "node id %u\n", args->num_of_nodes);
  362. dev_dbg(kfd_device,
  363. "gpu id %u\n", pdd->dev->id);
  364. dev_dbg(kfd_device,
  365. "lds_base %llX\n", pdd->lds_base);
  366. dev_dbg(kfd_device,
  367. "lds_limit %llX\n", pdd->lds_limit);
  368. dev_dbg(kfd_device,
  369. "gpuvm_base %llX\n", pdd->gpuvm_base);
  370. dev_dbg(kfd_device,
  371. "gpuvm_limit %llX\n", pdd->gpuvm_limit);
  372. dev_dbg(kfd_device,
  373. "scratch_base %llX\n", pdd->scratch_base);
  374. dev_dbg(kfd_device,
  375. "scratch_limit %llX\n", pdd->scratch_limit);
  376. args->num_of_nodes++;
  377. } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
  378. (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS));
  379. }
  380. mutex_unlock(&p->mutex);
  381. return 0;
  382. }
  383. #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
  384. [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0, .name = #ioctl}
  385. /** Ioctl table */
  386. static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
  387. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
  388. kfd_ioctl_get_version, 0),
  389. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
  390. kfd_ioctl_create_queue, 0),
  391. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
  392. kfd_ioctl_destroy_queue, 0),
  393. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
  394. kfd_ioctl_set_memory_policy, 0),
  395. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
  396. kfd_ioctl_get_clock_counters, 0),
  397. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
  398. kfd_ioctl_get_process_apertures, 0),
  399. AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
  400. kfd_ioctl_update_queue, 0),
  401. };
  402. #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
  403. static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  404. {
  405. struct kfd_process *process;
  406. amdkfd_ioctl_t *func;
  407. const struct amdkfd_ioctl_desc *ioctl = NULL;
  408. unsigned int nr = _IOC_NR(cmd);
  409. char stack_kdata[128];
  410. char *kdata = NULL;
  411. unsigned int usize, asize;
  412. int retcode = -EINVAL;
  413. if (nr >= AMDKFD_CORE_IOCTL_COUNT)
  414. goto err_i1;
  415. if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
  416. u32 amdkfd_size;
  417. ioctl = &amdkfd_ioctls[nr];
  418. amdkfd_size = _IOC_SIZE(ioctl->cmd);
  419. usize = asize = _IOC_SIZE(cmd);
  420. if (amdkfd_size > asize)
  421. asize = amdkfd_size;
  422. cmd = ioctl->cmd;
  423. } else
  424. goto err_i1;
  425. dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg);
  426. process = kfd_get_process(current);
  427. if (IS_ERR(process)) {
  428. dev_dbg(kfd_device, "no process\n");
  429. goto err_i1;
  430. }
  431. /* Do not trust userspace, use our own definition */
  432. func = ioctl->func;
  433. if (unlikely(!func)) {
  434. dev_dbg(kfd_device, "no function\n");
  435. retcode = -EINVAL;
  436. goto err_i1;
  437. }
  438. if (cmd & (IOC_IN | IOC_OUT)) {
  439. if (asize <= sizeof(stack_kdata)) {
  440. kdata = stack_kdata;
  441. } else {
  442. kdata = kmalloc(asize, GFP_KERNEL);
  443. if (!kdata) {
  444. retcode = -ENOMEM;
  445. goto err_i1;
  446. }
  447. }
  448. if (asize > usize)
  449. memset(kdata + usize, 0, asize - usize);
  450. }
  451. if (cmd & IOC_IN) {
  452. if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
  453. retcode = -EFAULT;
  454. goto err_i1;
  455. }
  456. } else if (cmd & IOC_OUT) {
  457. memset(kdata, 0, usize);
  458. }
  459. retcode = func(filep, process, kdata);
  460. if (cmd & IOC_OUT)
  461. if (copy_to_user((void __user *)arg, kdata, usize) != 0)
  462. retcode = -EFAULT;
  463. err_i1:
  464. if (!ioctl)
  465. dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
  466. task_pid_nr(current), cmd, nr);
  467. if (kdata != stack_kdata)
  468. kfree(kdata);
  469. if (retcode)
  470. dev_dbg(kfd_device, "ret = %d\n", retcode);
  471. return retcode;
  472. }
  473. static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
  474. {
  475. struct kfd_process *process;
  476. process = kfd_get_process(current);
  477. if (IS_ERR(process))
  478. return PTR_ERR(process);
  479. return kfd_doorbell_mmap(process, vma);
  480. }