kfd_chardev.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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/mman.h>
  34. #include <asm/processor.h>
  35. #include "kfd_priv.h"
  36. #include "kfd_device_queue_manager.h"
  37. #include "kfd_dbgmgr.h"
  38. static long kfd_ioctl(struct file *, unsigned int, unsigned long);
  39. static int kfd_open(struct inode *, struct file *);
  40. static int kfd_mmap(struct file *, struct vm_area_struct *);
  41. static const char kfd_dev_name[] = "kfd";
  42. static const struct file_operations kfd_fops = {
  43. .owner = THIS_MODULE,
  44. .unlocked_ioctl = kfd_ioctl,
  45. .compat_ioctl = kfd_ioctl,
  46. .open = kfd_open,
  47. .mmap = kfd_mmap,
  48. };
  49. static int kfd_char_dev_major = -1;
  50. static struct class *kfd_class;
  51. struct device *kfd_device;
  52. int kfd_chardev_init(void)
  53. {
  54. int err = 0;
  55. kfd_char_dev_major = register_chrdev(0, kfd_dev_name, &kfd_fops);
  56. err = kfd_char_dev_major;
  57. if (err < 0)
  58. goto err_register_chrdev;
  59. kfd_class = class_create(THIS_MODULE, kfd_dev_name);
  60. err = PTR_ERR(kfd_class);
  61. if (IS_ERR(kfd_class))
  62. goto err_class_create;
  63. kfd_device = device_create(kfd_class, NULL,
  64. MKDEV(kfd_char_dev_major, 0),
  65. NULL, kfd_dev_name);
  66. err = PTR_ERR(kfd_device);
  67. if (IS_ERR(kfd_device))
  68. goto err_device_create;
  69. return 0;
  70. err_device_create:
  71. class_destroy(kfd_class);
  72. err_class_create:
  73. unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
  74. err_register_chrdev:
  75. return err;
  76. }
  77. void kfd_chardev_exit(void)
  78. {
  79. device_destroy(kfd_class, MKDEV(kfd_char_dev_major, 0));
  80. class_destroy(kfd_class);
  81. unregister_chrdev(kfd_char_dev_major, kfd_dev_name);
  82. }
  83. struct device *kfd_chardev(void)
  84. {
  85. return kfd_device;
  86. }
  87. static int kfd_open(struct inode *inode, struct file *filep)
  88. {
  89. struct kfd_process *process;
  90. bool is_32bit_user_mode;
  91. if (iminor(inode) != 0)
  92. return -ENODEV;
  93. is_32bit_user_mode = in_compat_syscall();
  94. if (is_32bit_user_mode == true) {
  95. dev_warn(kfd_device,
  96. "Process %d (32-bit) failed to open /dev/kfd\n"
  97. "32-bit processes are not supported by amdkfd\n",
  98. current->pid);
  99. return -EPERM;
  100. }
  101. process = kfd_create_process(current);
  102. if (IS_ERR(process))
  103. return PTR_ERR(process);
  104. dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
  105. process->pasid, process->is_32bit_user_mode);
  106. return 0;
  107. }
  108. static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
  109. void *data)
  110. {
  111. struct kfd_ioctl_get_version_args *args = data;
  112. int err = 0;
  113. args->major_version = KFD_IOCTL_MAJOR_VERSION;
  114. args->minor_version = KFD_IOCTL_MINOR_VERSION;
  115. return err;
  116. }
  117. static int set_queue_properties_from_user(struct queue_properties *q_properties,
  118. struct kfd_ioctl_create_queue_args *args)
  119. {
  120. if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
  121. pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
  122. return -EINVAL;
  123. }
  124. if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
  125. pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
  126. return -EINVAL;
  127. }
  128. if ((args->ring_base_address) &&
  129. (!access_ok(VERIFY_WRITE,
  130. (const void __user *) args->ring_base_address,
  131. sizeof(uint64_t)))) {
  132. pr_err("kfd: can't access ring base address\n");
  133. return -EFAULT;
  134. }
  135. if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
  136. pr_err("kfd: ring size must be a power of 2 or 0\n");
  137. return -EINVAL;
  138. }
  139. if (!access_ok(VERIFY_WRITE,
  140. (const void __user *) args->read_pointer_address,
  141. sizeof(uint32_t))) {
  142. pr_err("kfd: can't access read pointer\n");
  143. return -EFAULT;
  144. }
  145. if (!access_ok(VERIFY_WRITE,
  146. (const void __user *) args->write_pointer_address,
  147. sizeof(uint32_t))) {
  148. pr_err("kfd: can't access write pointer\n");
  149. return -EFAULT;
  150. }
  151. if (args->eop_buffer_address &&
  152. !access_ok(VERIFY_WRITE,
  153. (const void __user *) args->eop_buffer_address,
  154. sizeof(uint32_t))) {
  155. pr_debug("kfd: can't access eop buffer");
  156. return -EFAULT;
  157. }
  158. if (args->ctx_save_restore_address &&
  159. !access_ok(VERIFY_WRITE,
  160. (const void __user *) args->ctx_save_restore_address,
  161. sizeof(uint32_t))) {
  162. pr_debug("kfd: can't access ctx save restore buffer");
  163. return -EFAULT;
  164. }
  165. q_properties->is_interop = false;
  166. q_properties->queue_percent = args->queue_percentage;
  167. q_properties->priority = args->queue_priority;
  168. q_properties->queue_address = args->ring_base_address;
  169. q_properties->queue_size = args->ring_size;
  170. q_properties->read_ptr = (uint32_t *) args->read_pointer_address;
  171. q_properties->write_ptr = (uint32_t *) args->write_pointer_address;
  172. q_properties->eop_ring_buffer_address = args->eop_buffer_address;
  173. q_properties->eop_ring_buffer_size = args->eop_buffer_size;
  174. q_properties->ctx_save_restore_area_address =
  175. args->ctx_save_restore_address;
  176. q_properties->ctx_save_restore_area_size = args->ctx_save_restore_size;
  177. if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE ||
  178. args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
  179. q_properties->type = KFD_QUEUE_TYPE_COMPUTE;
  180. else if (args->queue_type == KFD_IOC_QUEUE_TYPE_SDMA)
  181. q_properties->type = KFD_QUEUE_TYPE_SDMA;
  182. else
  183. return -ENOTSUPP;
  184. if (args->queue_type == KFD_IOC_QUEUE_TYPE_COMPUTE_AQL)
  185. q_properties->format = KFD_QUEUE_FORMAT_AQL;
  186. else
  187. q_properties->format = KFD_QUEUE_FORMAT_PM4;
  188. pr_debug("Queue Percentage (%d, %d)\n",
  189. q_properties->queue_percent, args->queue_percentage);
  190. pr_debug("Queue Priority (%d, %d)\n",
  191. q_properties->priority, args->queue_priority);
  192. pr_debug("Queue Address (0x%llX, 0x%llX)\n",
  193. q_properties->queue_address, args->ring_base_address);
  194. pr_debug("Queue Size (0x%llX, %u)\n",
  195. q_properties->queue_size, args->ring_size);
  196. pr_debug("Queue r/w Pointers (0x%llX, 0x%llX)\n",
  197. (uint64_t) q_properties->read_ptr,
  198. (uint64_t) q_properties->write_ptr);
  199. pr_debug("Queue Format (%d)\n", q_properties->format);
  200. pr_debug("Queue EOP (0x%llX)\n", q_properties->eop_ring_buffer_address);
  201. pr_debug("Queue CTX save arex (0x%llX)\n",
  202. q_properties->ctx_save_restore_area_address);
  203. return 0;
  204. }
  205. static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
  206. void *data)
  207. {
  208. struct kfd_ioctl_create_queue_args *args = data;
  209. struct kfd_dev *dev;
  210. int err = 0;
  211. unsigned int queue_id;
  212. struct kfd_process_device *pdd;
  213. struct queue_properties q_properties;
  214. memset(&q_properties, 0, sizeof(struct queue_properties));
  215. pr_debug("kfd: creating queue ioctl\n");
  216. err = set_queue_properties_from_user(&q_properties, args);
  217. if (err)
  218. return err;
  219. pr_debug("kfd: looking for gpu id 0x%x\n", args->gpu_id);
  220. dev = kfd_device_by_id(args->gpu_id);
  221. if (dev == NULL) {
  222. pr_debug("kfd: gpu id 0x%x was not found\n", args->gpu_id);
  223. return -EINVAL;
  224. }
  225. mutex_lock(&p->mutex);
  226. pdd = kfd_bind_process_to_device(dev, p);
  227. if (IS_ERR(pdd)) {
  228. err = -ESRCH;
  229. goto err_bind_process;
  230. }
  231. pr_debug("kfd: creating queue for PASID %d on GPU 0x%x\n",
  232. p->pasid,
  233. dev->id);
  234. err = pqm_create_queue(&p->pqm, dev, filep, &q_properties,
  235. 0, q_properties.type, &queue_id);
  236. if (err != 0)
  237. goto err_create_queue;
  238. args->queue_id = queue_id;
  239. /* Return gpu_id as doorbell offset for mmap usage */
  240. args->doorbell_offset = (KFD_MMAP_DOORBELL_MASK | args->gpu_id);
  241. args->doorbell_offset <<= PAGE_SHIFT;
  242. mutex_unlock(&p->mutex);
  243. pr_debug("kfd: queue id %d was created successfully\n", args->queue_id);
  244. pr_debug("ring buffer address == 0x%016llX\n",
  245. args->ring_base_address);
  246. pr_debug("read ptr address == 0x%016llX\n",
  247. args->read_pointer_address);
  248. pr_debug("write ptr address == 0x%016llX\n",
  249. args->write_pointer_address);
  250. return 0;
  251. err_create_queue:
  252. err_bind_process:
  253. mutex_unlock(&p->mutex);
  254. return err;
  255. }
  256. static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
  257. void *data)
  258. {
  259. int retval;
  260. struct kfd_ioctl_destroy_queue_args *args = data;
  261. pr_debug("kfd: destroying queue id %d for PASID %d\n",
  262. args->queue_id,
  263. p->pasid);
  264. mutex_lock(&p->mutex);
  265. retval = pqm_destroy_queue(&p->pqm, args->queue_id);
  266. mutex_unlock(&p->mutex);
  267. return retval;
  268. }
  269. static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
  270. void *data)
  271. {
  272. int retval;
  273. struct kfd_ioctl_update_queue_args *args = data;
  274. struct queue_properties properties;
  275. if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
  276. pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
  277. return -EINVAL;
  278. }
  279. if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
  280. pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
  281. return -EINVAL;
  282. }
  283. if ((args->ring_base_address) &&
  284. (!access_ok(VERIFY_WRITE,
  285. (const void __user *) args->ring_base_address,
  286. sizeof(uint64_t)))) {
  287. pr_err("kfd: can't access ring base address\n");
  288. return -EFAULT;
  289. }
  290. if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
  291. pr_err("kfd: ring size must be a power of 2 or 0\n");
  292. return -EINVAL;
  293. }
  294. properties.queue_address = args->ring_base_address;
  295. properties.queue_size = args->ring_size;
  296. properties.queue_percent = args->queue_percentage;
  297. properties.priority = args->queue_priority;
  298. pr_debug("kfd: updating queue id %d for PASID %d\n",
  299. args->queue_id, p->pasid);
  300. mutex_lock(&p->mutex);
  301. retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
  302. mutex_unlock(&p->mutex);
  303. return retval;
  304. }
  305. static int kfd_ioctl_set_memory_policy(struct file *filep,
  306. struct kfd_process *p, void *data)
  307. {
  308. struct kfd_ioctl_set_memory_policy_args *args = data;
  309. struct kfd_dev *dev;
  310. int err = 0;
  311. struct kfd_process_device *pdd;
  312. enum cache_policy default_policy, alternate_policy;
  313. if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
  314. && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  315. return -EINVAL;
  316. }
  317. if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
  318. && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  319. return -EINVAL;
  320. }
  321. dev = kfd_device_by_id(args->gpu_id);
  322. if (dev == NULL)
  323. return -EINVAL;
  324. mutex_lock(&p->mutex);
  325. pdd = kfd_bind_process_to_device(dev, p);
  326. if (IS_ERR(pdd)) {
  327. err = -ESRCH;
  328. goto out;
  329. }
  330. default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  331. ? cache_policy_coherent : cache_policy_noncoherent;
  332. alternate_policy =
  333. (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  334. ? cache_policy_coherent : cache_policy_noncoherent;
  335. if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
  336. &pdd->qpd,
  337. default_policy,
  338. alternate_policy,
  339. (void __user *)args->alternate_aperture_base,
  340. args->alternate_aperture_size))
  341. err = -EINVAL;
  342. out:
  343. mutex_unlock(&p->mutex);
  344. return err;
  345. }
  346. static int kfd_ioctl_dbg_register(struct file *filep,
  347. struct kfd_process *p, void *data)
  348. {
  349. struct kfd_ioctl_dbg_register_args *args = data;
  350. struct kfd_dev *dev;
  351. struct kfd_dbgmgr *dbgmgr_ptr;
  352. struct kfd_process_device *pdd;
  353. bool create_ok;
  354. long status = 0;
  355. dev = kfd_device_by_id(args->gpu_id);
  356. if (dev == NULL)
  357. return -EINVAL;
  358. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  359. pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
  360. return -EINVAL;
  361. }
  362. mutex_lock(kfd_get_dbgmgr_mutex());
  363. mutex_lock(&p->mutex);
  364. /*
  365. * make sure that we have pdd, if this the first queue created for
  366. * this process
  367. */
  368. pdd = kfd_bind_process_to_device(dev, p);
  369. if (IS_ERR(pdd)) {
  370. mutex_unlock(&p->mutex);
  371. mutex_unlock(kfd_get_dbgmgr_mutex());
  372. return PTR_ERR(pdd);
  373. }
  374. if (dev->dbgmgr == NULL) {
  375. /* In case of a legal call, we have no dbgmgr yet */
  376. create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev);
  377. if (create_ok) {
  378. status = kfd_dbgmgr_register(dbgmgr_ptr, p);
  379. if (status != 0)
  380. kfd_dbgmgr_destroy(dbgmgr_ptr);
  381. else
  382. dev->dbgmgr = dbgmgr_ptr;
  383. }
  384. } else {
  385. pr_debug("debugger already registered\n");
  386. status = -EINVAL;
  387. }
  388. mutex_unlock(&p->mutex);
  389. mutex_unlock(kfd_get_dbgmgr_mutex());
  390. return status;
  391. }
  392. static int kfd_ioctl_dbg_unrgesiter(struct file *filep,
  393. struct kfd_process *p, void *data)
  394. {
  395. struct kfd_ioctl_dbg_unregister_args *args = data;
  396. struct kfd_dev *dev;
  397. long status;
  398. dev = kfd_device_by_id(args->gpu_id);
  399. if (dev == NULL)
  400. return -EINVAL;
  401. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  402. pr_debug("kfd_ioctl_dbg_unrgesiter not supported on CZ\n");
  403. return -EINVAL;
  404. }
  405. mutex_lock(kfd_get_dbgmgr_mutex());
  406. status = kfd_dbgmgr_unregister(dev->dbgmgr, p);
  407. if (status == 0) {
  408. kfd_dbgmgr_destroy(dev->dbgmgr);
  409. dev->dbgmgr = NULL;
  410. }
  411. mutex_unlock(kfd_get_dbgmgr_mutex());
  412. return status;
  413. }
  414. /*
  415. * Parse and generate variable size data structure for address watch.
  416. * Total size of the buffer and # watch points is limited in order
  417. * to prevent kernel abuse. (no bearing to the much smaller HW limitation
  418. * which is enforced by dbgdev module)
  419. * please also note that the watch address itself are not "copied from user",
  420. * since it be set into the HW in user mode values.
  421. *
  422. */
  423. static int kfd_ioctl_dbg_address_watch(struct file *filep,
  424. struct kfd_process *p, void *data)
  425. {
  426. struct kfd_ioctl_dbg_address_watch_args *args = data;
  427. struct kfd_dev *dev;
  428. struct dbg_address_watch_info aw_info;
  429. unsigned char *args_buff;
  430. long status;
  431. void __user *cmd_from_user;
  432. uint64_t watch_mask_value = 0;
  433. unsigned int args_idx = 0;
  434. memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info));
  435. dev = kfd_device_by_id(args->gpu_id);
  436. if (dev == NULL)
  437. return -EINVAL;
  438. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  439. pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
  440. return -EINVAL;
  441. }
  442. cmd_from_user = (void __user *) args->content_ptr;
  443. /* Validate arguments */
  444. if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) ||
  445. (args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) ||
  446. (cmd_from_user == NULL))
  447. return -EINVAL;
  448. /* this is the actual buffer to work with */
  449. args_buff = memdup_user(cmd_from_user,
  450. args->buf_size_in_bytes - sizeof(*args));
  451. if (IS_ERR(args_buff))
  452. return PTR_ERR(args_buff);
  453. aw_info.process = p;
  454. aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx]));
  455. args_idx += sizeof(aw_info.num_watch_points);
  456. aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx];
  457. args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points;
  458. /*
  459. * set watch address base pointer to point on the array base
  460. * within args_buff
  461. */
  462. aw_info.watch_address = (uint64_t *) &args_buff[args_idx];
  463. /* skip over the addresses buffer */
  464. args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points;
  465. if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) {
  466. kfree(args_buff);
  467. return -EINVAL;
  468. }
  469. watch_mask_value = (uint64_t) args_buff[args_idx];
  470. if (watch_mask_value > 0) {
  471. /*
  472. * There is an array of masks.
  473. * set watch mask base pointer to point on the array base
  474. * within args_buff
  475. */
  476. aw_info.watch_mask = (uint64_t *) &args_buff[args_idx];
  477. /* skip over the masks buffer */
  478. args_idx += sizeof(aw_info.watch_mask) *
  479. aw_info.num_watch_points;
  480. } else {
  481. /* just the NULL mask, set to NULL and skip over it */
  482. aw_info.watch_mask = NULL;
  483. args_idx += sizeof(aw_info.watch_mask);
  484. }
  485. if (args_idx >= args->buf_size_in_bytes - sizeof(args)) {
  486. kfree(args_buff);
  487. return -EINVAL;
  488. }
  489. /* Currently HSA Event is not supported for DBG */
  490. aw_info.watch_event = NULL;
  491. mutex_lock(kfd_get_dbgmgr_mutex());
  492. status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info);
  493. mutex_unlock(kfd_get_dbgmgr_mutex());
  494. kfree(args_buff);
  495. return status;
  496. }
  497. /* Parse and generate fixed size data structure for wave control */
  498. static int kfd_ioctl_dbg_wave_control(struct file *filep,
  499. struct kfd_process *p, void *data)
  500. {
  501. struct kfd_ioctl_dbg_wave_control_args *args = data;
  502. struct kfd_dev *dev;
  503. struct dbg_wave_control_info wac_info;
  504. unsigned char *args_buff;
  505. uint32_t computed_buff_size;
  506. long status;
  507. void __user *cmd_from_user;
  508. unsigned int args_idx = 0;
  509. memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info));
  510. /* we use compact form, independent of the packing attribute value */
  511. computed_buff_size = sizeof(*args) +
  512. sizeof(wac_info.mode) +
  513. sizeof(wac_info.operand) +
  514. sizeof(wac_info.dbgWave_msg.DbgWaveMsg) +
  515. sizeof(wac_info.dbgWave_msg.MemoryVA) +
  516. sizeof(wac_info.trapId);
  517. dev = kfd_device_by_id(args->gpu_id);
  518. if (dev == NULL)
  519. return -EINVAL;
  520. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  521. pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
  522. return -EINVAL;
  523. }
  524. /* input size must match the computed "compact" size */
  525. if (args->buf_size_in_bytes != computed_buff_size) {
  526. pr_debug("size mismatch, computed : actual %u : %u\n",
  527. args->buf_size_in_bytes, computed_buff_size);
  528. return -EINVAL;
  529. }
  530. cmd_from_user = (void __user *) args->content_ptr;
  531. if (cmd_from_user == NULL)
  532. return -EINVAL;
  533. /* copy the entire buffer from user */
  534. args_buff = memdup_user(cmd_from_user,
  535. args->buf_size_in_bytes - sizeof(*args));
  536. if (IS_ERR(args_buff))
  537. return PTR_ERR(args_buff);
  538. /* move ptr to the start of the "pay-load" area */
  539. wac_info.process = p;
  540. wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx]));
  541. args_idx += sizeof(wac_info.operand);
  542. wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx]));
  543. args_idx += sizeof(wac_info.mode);
  544. wac_info.trapId = *((uint32_t *)(&args_buff[args_idx]));
  545. args_idx += sizeof(wac_info.trapId);
  546. wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value =
  547. *((uint32_t *)(&args_buff[args_idx]));
  548. wac_info.dbgWave_msg.MemoryVA = NULL;
  549. mutex_lock(kfd_get_dbgmgr_mutex());
  550. pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
  551. wac_info.process, wac_info.operand,
  552. wac_info.mode, wac_info.trapId,
  553. wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value);
  554. status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info);
  555. pr_debug("Returned status of dbg manager is %ld\n", status);
  556. mutex_unlock(kfd_get_dbgmgr_mutex());
  557. kfree(args_buff);
  558. return status;
  559. }
  560. static int kfd_ioctl_get_clock_counters(struct file *filep,
  561. struct kfd_process *p, void *data)
  562. {
  563. struct kfd_ioctl_get_clock_counters_args *args = data;
  564. struct kfd_dev *dev;
  565. struct timespec64 time;
  566. dev = kfd_device_by_id(args->gpu_id);
  567. if (dev == NULL)
  568. return -EINVAL;
  569. /* Reading GPU clock counter from KGD */
  570. args->gpu_clock_counter =
  571. dev->kfd2kgd->get_gpu_clock_counter(dev->kgd);
  572. /* No access to rdtsc. Using raw monotonic time */
  573. getrawmonotonic64(&time);
  574. args->cpu_clock_counter = (uint64_t)timespec64_to_ns(&time);
  575. get_monotonic_boottime64(&time);
  576. args->system_clock_counter = (uint64_t)timespec64_to_ns(&time);
  577. /* Since the counter is in nano-seconds we use 1GHz frequency */
  578. args->system_clock_freq = 1000000000;
  579. return 0;
  580. }
  581. static int kfd_ioctl_get_process_apertures(struct file *filp,
  582. struct kfd_process *p, void *data)
  583. {
  584. struct kfd_ioctl_get_process_apertures_args *args = data;
  585. struct kfd_process_device_apertures *pAperture;
  586. struct kfd_process_device *pdd;
  587. dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
  588. args->num_of_nodes = 0;
  589. mutex_lock(&p->mutex);
  590. /*if the process-device list isn't empty*/
  591. if (kfd_has_process_device_data(p)) {
  592. /* Run over all pdd of the process */
  593. pdd = kfd_get_first_process_device_data(p);
  594. do {
  595. pAperture =
  596. &args->process_apertures[args->num_of_nodes];
  597. pAperture->gpu_id = pdd->dev->id;
  598. pAperture->lds_base = pdd->lds_base;
  599. pAperture->lds_limit = pdd->lds_limit;
  600. pAperture->gpuvm_base = pdd->gpuvm_base;
  601. pAperture->gpuvm_limit = pdd->gpuvm_limit;
  602. pAperture->scratch_base = pdd->scratch_base;
  603. pAperture->scratch_limit = pdd->scratch_limit;
  604. dev_dbg(kfd_device,
  605. "node id %u\n", args->num_of_nodes);
  606. dev_dbg(kfd_device,
  607. "gpu id %u\n", pdd->dev->id);
  608. dev_dbg(kfd_device,
  609. "lds_base %llX\n", pdd->lds_base);
  610. dev_dbg(kfd_device,
  611. "lds_limit %llX\n", pdd->lds_limit);
  612. dev_dbg(kfd_device,
  613. "gpuvm_base %llX\n", pdd->gpuvm_base);
  614. dev_dbg(kfd_device,
  615. "gpuvm_limit %llX\n", pdd->gpuvm_limit);
  616. dev_dbg(kfd_device,
  617. "scratch_base %llX\n", pdd->scratch_base);
  618. dev_dbg(kfd_device,
  619. "scratch_limit %llX\n", pdd->scratch_limit);
  620. args->num_of_nodes++;
  621. } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
  622. (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS));
  623. }
  624. mutex_unlock(&p->mutex);
  625. return 0;
  626. }
  627. static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
  628. void *data)
  629. {
  630. struct kfd_ioctl_create_event_args *args = data;
  631. int err;
  632. err = kfd_event_create(filp, p, args->event_type,
  633. args->auto_reset != 0, args->node_id,
  634. &args->event_id, &args->event_trigger_data,
  635. &args->event_page_offset,
  636. &args->event_slot_index);
  637. return err;
  638. }
  639. static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
  640. void *data)
  641. {
  642. struct kfd_ioctl_destroy_event_args *args = data;
  643. return kfd_event_destroy(p, args->event_id);
  644. }
  645. static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
  646. void *data)
  647. {
  648. struct kfd_ioctl_set_event_args *args = data;
  649. return kfd_set_event(p, args->event_id);
  650. }
  651. static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
  652. void *data)
  653. {
  654. struct kfd_ioctl_reset_event_args *args = data;
  655. return kfd_reset_event(p, args->event_id);
  656. }
  657. static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
  658. void *data)
  659. {
  660. struct kfd_ioctl_wait_events_args *args = data;
  661. enum kfd_event_wait_result wait_result;
  662. int err;
  663. err = kfd_wait_on_events(p, args->num_events,
  664. (void __user *)args->events_ptr,
  665. (args->wait_for_all != 0),
  666. args->timeout, &wait_result);
  667. args->wait_result = wait_result;
  668. return err;
  669. }
  670. #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
  671. [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0, .name = #ioctl}
  672. /** Ioctl table */
  673. static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
  674. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
  675. kfd_ioctl_get_version, 0),
  676. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
  677. kfd_ioctl_create_queue, 0),
  678. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
  679. kfd_ioctl_destroy_queue, 0),
  680. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
  681. kfd_ioctl_set_memory_policy, 0),
  682. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
  683. kfd_ioctl_get_clock_counters, 0),
  684. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
  685. kfd_ioctl_get_process_apertures, 0),
  686. AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
  687. kfd_ioctl_update_queue, 0),
  688. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
  689. kfd_ioctl_create_event, 0),
  690. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
  691. kfd_ioctl_destroy_event, 0),
  692. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
  693. kfd_ioctl_set_event, 0),
  694. AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
  695. kfd_ioctl_reset_event, 0),
  696. AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
  697. kfd_ioctl_wait_events, 0),
  698. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER,
  699. kfd_ioctl_dbg_register, 0),
  700. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER,
  701. kfd_ioctl_dbg_unrgesiter, 0),
  702. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH,
  703. kfd_ioctl_dbg_address_watch, 0),
  704. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL,
  705. kfd_ioctl_dbg_wave_control, 0),
  706. };
  707. #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
  708. static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  709. {
  710. struct kfd_process *process;
  711. amdkfd_ioctl_t *func;
  712. const struct amdkfd_ioctl_desc *ioctl = NULL;
  713. unsigned int nr = _IOC_NR(cmd);
  714. char stack_kdata[128];
  715. char *kdata = NULL;
  716. unsigned int usize, asize;
  717. int retcode = -EINVAL;
  718. if (nr >= AMDKFD_CORE_IOCTL_COUNT)
  719. goto err_i1;
  720. if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
  721. u32 amdkfd_size;
  722. ioctl = &amdkfd_ioctls[nr];
  723. amdkfd_size = _IOC_SIZE(ioctl->cmd);
  724. usize = asize = _IOC_SIZE(cmd);
  725. if (amdkfd_size > asize)
  726. asize = amdkfd_size;
  727. cmd = ioctl->cmd;
  728. } else
  729. goto err_i1;
  730. dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg);
  731. process = kfd_get_process(current);
  732. if (IS_ERR(process)) {
  733. dev_dbg(kfd_device, "no process\n");
  734. goto err_i1;
  735. }
  736. /* Do not trust userspace, use our own definition */
  737. func = ioctl->func;
  738. if (unlikely(!func)) {
  739. dev_dbg(kfd_device, "no function\n");
  740. retcode = -EINVAL;
  741. goto err_i1;
  742. }
  743. if (cmd & (IOC_IN | IOC_OUT)) {
  744. if (asize <= sizeof(stack_kdata)) {
  745. kdata = stack_kdata;
  746. } else {
  747. kdata = kmalloc(asize, GFP_KERNEL);
  748. if (!kdata) {
  749. retcode = -ENOMEM;
  750. goto err_i1;
  751. }
  752. }
  753. if (asize > usize)
  754. memset(kdata + usize, 0, asize - usize);
  755. }
  756. if (cmd & IOC_IN) {
  757. if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
  758. retcode = -EFAULT;
  759. goto err_i1;
  760. }
  761. } else if (cmd & IOC_OUT) {
  762. memset(kdata, 0, usize);
  763. }
  764. retcode = func(filep, process, kdata);
  765. if (cmd & IOC_OUT)
  766. if (copy_to_user((void __user *)arg, kdata, usize) != 0)
  767. retcode = -EFAULT;
  768. err_i1:
  769. if (!ioctl)
  770. dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
  771. task_pid_nr(current), cmd, nr);
  772. if (kdata != stack_kdata)
  773. kfree(kdata);
  774. if (retcode)
  775. dev_dbg(kfd_device, "ret = %d\n", retcode);
  776. return retcode;
  777. }
  778. static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
  779. {
  780. struct kfd_process *process;
  781. process = kfd_get_process(current);
  782. if (IS_ERR(process))
  783. return PTR_ERR(process);
  784. if ((vma->vm_pgoff & KFD_MMAP_DOORBELL_MASK) ==
  785. KFD_MMAP_DOORBELL_MASK) {
  786. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_DOORBELL_MASK;
  787. return kfd_doorbell_mmap(process, vma);
  788. } else if ((vma->vm_pgoff & KFD_MMAP_EVENTS_MASK) ==
  789. KFD_MMAP_EVENTS_MASK) {
  790. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_EVENTS_MASK;
  791. return kfd_event_mmap(process, vma);
  792. }
  793. return -EFAULT;
  794. }