kfd_chardev.c 17 KB

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