kfd_chardev.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 = is_compat_task();
  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 = kmalloc(args->buf_size_in_bytes -
  450. sizeof(*args), GFP_KERNEL);
  451. if (args_buff == NULL)
  452. return -ENOMEM;
  453. status = copy_from_user(args_buff, cmd_from_user,
  454. args->buf_size_in_bytes - sizeof(*args));
  455. if (status != 0) {
  456. pr_debug("Failed to copy address watch user data\n");
  457. kfree(args_buff);
  458. return -EINVAL;
  459. }
  460. aw_info.process = p;
  461. aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx]));
  462. args_idx += sizeof(aw_info.num_watch_points);
  463. aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx];
  464. args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points;
  465. /*
  466. * set watch address base pointer to point on the array base
  467. * within args_buff
  468. */
  469. aw_info.watch_address = (uint64_t *) &args_buff[args_idx];
  470. /* skip over the addresses buffer */
  471. args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points;
  472. if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) {
  473. kfree(args_buff);
  474. return -EINVAL;
  475. }
  476. watch_mask_value = (uint64_t) args_buff[args_idx];
  477. if (watch_mask_value > 0) {
  478. /*
  479. * There is an array of masks.
  480. * set watch mask base pointer to point on the array base
  481. * within args_buff
  482. */
  483. aw_info.watch_mask = (uint64_t *) &args_buff[args_idx];
  484. /* skip over the masks buffer */
  485. args_idx += sizeof(aw_info.watch_mask) *
  486. aw_info.num_watch_points;
  487. } else {
  488. /* just the NULL mask, set to NULL and skip over it */
  489. aw_info.watch_mask = NULL;
  490. args_idx += sizeof(aw_info.watch_mask);
  491. }
  492. if (args_idx >= args->buf_size_in_bytes - sizeof(args)) {
  493. kfree(args_buff);
  494. return -EINVAL;
  495. }
  496. /* Currently HSA Event is not supported for DBG */
  497. aw_info.watch_event = NULL;
  498. mutex_lock(kfd_get_dbgmgr_mutex());
  499. status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info);
  500. mutex_unlock(kfd_get_dbgmgr_mutex());
  501. kfree(args_buff);
  502. return status;
  503. }
  504. /* Parse and generate fixed size data structure for wave control */
  505. static int kfd_ioctl_dbg_wave_control(struct file *filep,
  506. struct kfd_process *p, void *data)
  507. {
  508. struct kfd_ioctl_dbg_wave_control_args *args = data;
  509. struct kfd_dev *dev;
  510. struct dbg_wave_control_info wac_info;
  511. unsigned char *args_buff;
  512. uint32_t computed_buff_size;
  513. long status;
  514. void __user *cmd_from_user;
  515. unsigned int args_idx = 0;
  516. memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info));
  517. /* we use compact form, independent of the packing attribute value */
  518. computed_buff_size = sizeof(*args) +
  519. sizeof(wac_info.mode) +
  520. sizeof(wac_info.operand) +
  521. sizeof(wac_info.dbgWave_msg.DbgWaveMsg) +
  522. sizeof(wac_info.dbgWave_msg.MemoryVA) +
  523. sizeof(wac_info.trapId);
  524. dev = kfd_device_by_id(args->gpu_id);
  525. if (dev == NULL)
  526. return -EINVAL;
  527. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  528. pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
  529. return -EINVAL;
  530. }
  531. /* input size must match the computed "compact" size */
  532. if (args->buf_size_in_bytes != computed_buff_size) {
  533. pr_debug("size mismatch, computed : actual %u : %u\n",
  534. args->buf_size_in_bytes, computed_buff_size);
  535. return -EINVAL;
  536. }
  537. cmd_from_user = (void __user *) args->content_ptr;
  538. if (cmd_from_user == NULL)
  539. return -EINVAL;
  540. /* this is the actual buffer to work with */
  541. args_buff = kmalloc(args->buf_size_in_bytes - sizeof(*args),
  542. GFP_KERNEL);
  543. if (args_buff == NULL)
  544. return -ENOMEM;
  545. /* Now copy the entire buffer from user */
  546. status = copy_from_user(args_buff, cmd_from_user,
  547. args->buf_size_in_bytes - sizeof(*args));
  548. if (status != 0) {
  549. pr_debug("Failed to copy wave control user data\n");
  550. kfree(args_buff);
  551. return -EINVAL;
  552. }
  553. /* move ptr to the start of the "pay-load" area */
  554. wac_info.process = p;
  555. wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx]));
  556. args_idx += sizeof(wac_info.operand);
  557. wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx]));
  558. args_idx += sizeof(wac_info.mode);
  559. wac_info.trapId = *((uint32_t *)(&args_buff[args_idx]));
  560. args_idx += sizeof(wac_info.trapId);
  561. wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value =
  562. *((uint32_t *)(&args_buff[args_idx]));
  563. wac_info.dbgWave_msg.MemoryVA = NULL;
  564. mutex_lock(kfd_get_dbgmgr_mutex());
  565. pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
  566. wac_info.process, wac_info.operand,
  567. wac_info.mode, wac_info.trapId,
  568. wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value);
  569. status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info);
  570. pr_debug("Returned status of dbg manager is %ld\n", status);
  571. mutex_unlock(kfd_get_dbgmgr_mutex());
  572. kfree(args_buff);
  573. return status;
  574. }
  575. static int kfd_ioctl_get_clock_counters(struct file *filep,
  576. struct kfd_process *p, void *data)
  577. {
  578. struct kfd_ioctl_get_clock_counters_args *args = data;
  579. struct kfd_dev *dev;
  580. struct timespec64 time;
  581. dev = kfd_device_by_id(args->gpu_id);
  582. if (dev == NULL)
  583. return -EINVAL;
  584. /* Reading GPU clock counter from KGD */
  585. args->gpu_clock_counter =
  586. dev->kfd2kgd->get_gpu_clock_counter(dev->kgd);
  587. /* No access to rdtsc. Using raw monotonic time */
  588. getrawmonotonic64(&time);
  589. args->cpu_clock_counter = (uint64_t)timespec64_to_ns(&time);
  590. get_monotonic_boottime64(&time);
  591. args->system_clock_counter = (uint64_t)timespec64_to_ns(&time);
  592. /* Since the counter is in nano-seconds we use 1GHz frequency */
  593. args->system_clock_freq = 1000000000;
  594. return 0;
  595. }
  596. static int kfd_ioctl_get_process_apertures(struct file *filp,
  597. struct kfd_process *p, void *data)
  598. {
  599. struct kfd_ioctl_get_process_apertures_args *args = data;
  600. struct kfd_process_device_apertures *pAperture;
  601. struct kfd_process_device *pdd;
  602. dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
  603. args->num_of_nodes = 0;
  604. mutex_lock(&p->mutex);
  605. /*if the process-device list isn't empty*/
  606. if (kfd_has_process_device_data(p)) {
  607. /* Run over all pdd of the process */
  608. pdd = kfd_get_first_process_device_data(p);
  609. do {
  610. pAperture =
  611. &args->process_apertures[args->num_of_nodes];
  612. pAperture->gpu_id = pdd->dev->id;
  613. pAperture->lds_base = pdd->lds_base;
  614. pAperture->lds_limit = pdd->lds_limit;
  615. pAperture->gpuvm_base = pdd->gpuvm_base;
  616. pAperture->gpuvm_limit = pdd->gpuvm_limit;
  617. pAperture->scratch_base = pdd->scratch_base;
  618. pAperture->scratch_limit = pdd->scratch_limit;
  619. dev_dbg(kfd_device,
  620. "node id %u\n", args->num_of_nodes);
  621. dev_dbg(kfd_device,
  622. "gpu id %u\n", pdd->dev->id);
  623. dev_dbg(kfd_device,
  624. "lds_base %llX\n", pdd->lds_base);
  625. dev_dbg(kfd_device,
  626. "lds_limit %llX\n", pdd->lds_limit);
  627. dev_dbg(kfd_device,
  628. "gpuvm_base %llX\n", pdd->gpuvm_base);
  629. dev_dbg(kfd_device,
  630. "gpuvm_limit %llX\n", pdd->gpuvm_limit);
  631. dev_dbg(kfd_device,
  632. "scratch_base %llX\n", pdd->scratch_base);
  633. dev_dbg(kfd_device,
  634. "scratch_limit %llX\n", pdd->scratch_limit);
  635. args->num_of_nodes++;
  636. } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
  637. (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS));
  638. }
  639. mutex_unlock(&p->mutex);
  640. return 0;
  641. }
  642. static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
  643. void *data)
  644. {
  645. struct kfd_ioctl_create_event_args *args = data;
  646. int err;
  647. err = kfd_event_create(filp, p, args->event_type,
  648. args->auto_reset != 0, args->node_id,
  649. &args->event_id, &args->event_trigger_data,
  650. &args->event_page_offset,
  651. &args->event_slot_index);
  652. return err;
  653. }
  654. static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
  655. void *data)
  656. {
  657. struct kfd_ioctl_destroy_event_args *args = data;
  658. return kfd_event_destroy(p, args->event_id);
  659. }
  660. static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
  661. void *data)
  662. {
  663. struct kfd_ioctl_set_event_args *args = data;
  664. return kfd_set_event(p, args->event_id);
  665. }
  666. static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
  667. void *data)
  668. {
  669. struct kfd_ioctl_reset_event_args *args = data;
  670. return kfd_reset_event(p, args->event_id);
  671. }
  672. static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
  673. void *data)
  674. {
  675. struct kfd_ioctl_wait_events_args *args = data;
  676. enum kfd_event_wait_result wait_result;
  677. int err;
  678. err = kfd_wait_on_events(p, args->num_events,
  679. (void __user *)args->events_ptr,
  680. (args->wait_for_all != 0),
  681. args->timeout, &wait_result);
  682. args->wait_result = wait_result;
  683. return err;
  684. }
  685. #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
  686. [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0, .name = #ioctl}
  687. /** Ioctl table */
  688. static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
  689. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
  690. kfd_ioctl_get_version, 0),
  691. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
  692. kfd_ioctl_create_queue, 0),
  693. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
  694. kfd_ioctl_destroy_queue, 0),
  695. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
  696. kfd_ioctl_set_memory_policy, 0),
  697. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
  698. kfd_ioctl_get_clock_counters, 0),
  699. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
  700. kfd_ioctl_get_process_apertures, 0),
  701. AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
  702. kfd_ioctl_update_queue, 0),
  703. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
  704. kfd_ioctl_create_event, 0),
  705. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
  706. kfd_ioctl_destroy_event, 0),
  707. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
  708. kfd_ioctl_set_event, 0),
  709. AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
  710. kfd_ioctl_reset_event, 0),
  711. AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
  712. kfd_ioctl_wait_events, 0),
  713. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER,
  714. kfd_ioctl_dbg_register, 0),
  715. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER,
  716. kfd_ioctl_dbg_unrgesiter, 0),
  717. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH,
  718. kfd_ioctl_dbg_address_watch, 0),
  719. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL,
  720. kfd_ioctl_dbg_wave_control, 0),
  721. };
  722. #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
  723. static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  724. {
  725. struct kfd_process *process;
  726. amdkfd_ioctl_t *func;
  727. const struct amdkfd_ioctl_desc *ioctl = NULL;
  728. unsigned int nr = _IOC_NR(cmd);
  729. char stack_kdata[128];
  730. char *kdata = NULL;
  731. unsigned int usize, asize;
  732. int retcode = -EINVAL;
  733. if (nr >= AMDKFD_CORE_IOCTL_COUNT)
  734. goto err_i1;
  735. if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
  736. u32 amdkfd_size;
  737. ioctl = &amdkfd_ioctls[nr];
  738. amdkfd_size = _IOC_SIZE(ioctl->cmd);
  739. usize = asize = _IOC_SIZE(cmd);
  740. if (amdkfd_size > asize)
  741. asize = amdkfd_size;
  742. cmd = ioctl->cmd;
  743. } else
  744. goto err_i1;
  745. dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg);
  746. process = kfd_get_process(current);
  747. if (IS_ERR(process)) {
  748. dev_dbg(kfd_device, "no process\n");
  749. goto err_i1;
  750. }
  751. /* Do not trust userspace, use our own definition */
  752. func = ioctl->func;
  753. if (unlikely(!func)) {
  754. dev_dbg(kfd_device, "no function\n");
  755. retcode = -EINVAL;
  756. goto err_i1;
  757. }
  758. if (cmd & (IOC_IN | IOC_OUT)) {
  759. if (asize <= sizeof(stack_kdata)) {
  760. kdata = stack_kdata;
  761. } else {
  762. kdata = kmalloc(asize, GFP_KERNEL);
  763. if (!kdata) {
  764. retcode = -ENOMEM;
  765. goto err_i1;
  766. }
  767. }
  768. if (asize > usize)
  769. memset(kdata + usize, 0, asize - usize);
  770. }
  771. if (cmd & IOC_IN) {
  772. if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
  773. retcode = -EFAULT;
  774. goto err_i1;
  775. }
  776. } else if (cmd & IOC_OUT) {
  777. memset(kdata, 0, usize);
  778. }
  779. retcode = func(filep, process, kdata);
  780. if (cmd & IOC_OUT)
  781. if (copy_to_user((void __user *)arg, kdata, usize) != 0)
  782. retcode = -EFAULT;
  783. err_i1:
  784. if (!ioctl)
  785. dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
  786. task_pid_nr(current), cmd, nr);
  787. if (kdata != stack_kdata)
  788. kfree(kdata);
  789. if (retcode)
  790. dev_dbg(kfd_device, "ret = %d\n", retcode);
  791. return retcode;
  792. }
  793. static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
  794. {
  795. struct kfd_process *process;
  796. process = kfd_get_process(current);
  797. if (IS_ERR(process))
  798. return PTR_ERR(process);
  799. if ((vma->vm_pgoff & KFD_MMAP_DOORBELL_MASK) ==
  800. KFD_MMAP_DOORBELL_MASK) {
  801. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_DOORBELL_MASK;
  802. return kfd_doorbell_mmap(process, vma);
  803. } else if ((vma->vm_pgoff & KFD_MMAP_EVENTS_MASK) ==
  804. KFD_MMAP_EVENTS_MASK) {
  805. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_EVENTS_MASK;
  806. return kfd_event_mmap(process, vma);
  807. }
  808. return -EFAULT;
  809. }