kfd_chardev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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) {
  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. args->major_version = KFD_IOCTL_MAJOR_VERSION;
  113. args->minor_version = KFD_IOCTL_MINOR_VERSION;
  114. return 0;
  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 = (KFD_MMAP_DOORBELL_MASK | args->gpu_id);
  240. args->doorbell_offset <<= PAGE_SHIFT;
  241. mutex_unlock(&p->mutex);
  242. pr_debug("kfd: queue id %d was created successfully\n", args->queue_id);
  243. pr_debug("ring buffer address == 0x%016llX\n",
  244. args->ring_base_address);
  245. pr_debug("read ptr address == 0x%016llX\n",
  246. args->read_pointer_address);
  247. pr_debug("write ptr address == 0x%016llX\n",
  248. args->write_pointer_address);
  249. return 0;
  250. err_create_queue:
  251. err_bind_process:
  252. mutex_unlock(&p->mutex);
  253. return err;
  254. }
  255. static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
  256. void *data)
  257. {
  258. int retval;
  259. struct kfd_ioctl_destroy_queue_args *args = data;
  260. pr_debug("kfd: destroying queue id %d for PASID %d\n",
  261. args->queue_id,
  262. p->pasid);
  263. mutex_lock(&p->mutex);
  264. retval = pqm_destroy_queue(&p->pqm, args->queue_id);
  265. mutex_unlock(&p->mutex);
  266. return retval;
  267. }
  268. static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
  269. void *data)
  270. {
  271. int retval;
  272. struct kfd_ioctl_update_queue_args *args = data;
  273. struct queue_properties properties;
  274. if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
  275. pr_err("kfd: queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
  276. return -EINVAL;
  277. }
  278. if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
  279. pr_err("kfd: queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
  280. return -EINVAL;
  281. }
  282. if ((args->ring_base_address) &&
  283. (!access_ok(VERIFY_WRITE,
  284. (const void __user *) args->ring_base_address,
  285. sizeof(uint64_t)))) {
  286. pr_err("kfd: can't access ring base address\n");
  287. return -EFAULT;
  288. }
  289. if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
  290. pr_err("kfd: ring size must be a power of 2 or 0\n");
  291. return -EINVAL;
  292. }
  293. properties.queue_address = args->ring_base_address;
  294. properties.queue_size = args->ring_size;
  295. properties.queue_percent = args->queue_percentage;
  296. properties.priority = args->queue_priority;
  297. pr_debug("kfd: updating queue id %d for PASID %d\n",
  298. args->queue_id, p->pasid);
  299. mutex_lock(&p->mutex);
  300. retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
  301. mutex_unlock(&p->mutex);
  302. return retval;
  303. }
  304. static int kfd_ioctl_set_memory_policy(struct file *filep,
  305. struct kfd_process *p, void *data)
  306. {
  307. struct kfd_ioctl_set_memory_policy_args *args = data;
  308. struct kfd_dev *dev;
  309. int err = 0;
  310. struct kfd_process_device *pdd;
  311. enum cache_policy default_policy, alternate_policy;
  312. if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
  313. && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  314. return -EINVAL;
  315. }
  316. if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
  317. && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  318. return -EINVAL;
  319. }
  320. dev = kfd_device_by_id(args->gpu_id);
  321. if (dev == NULL)
  322. return -EINVAL;
  323. mutex_lock(&p->mutex);
  324. pdd = kfd_bind_process_to_device(dev, p);
  325. if (IS_ERR(pdd)) {
  326. err = -ESRCH;
  327. goto out;
  328. }
  329. default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  330. ? cache_policy_coherent : cache_policy_noncoherent;
  331. alternate_policy =
  332. (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  333. ? cache_policy_coherent : cache_policy_noncoherent;
  334. if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
  335. &pdd->qpd,
  336. default_policy,
  337. alternate_policy,
  338. (void __user *)args->alternate_aperture_base,
  339. args->alternate_aperture_size))
  340. err = -EINVAL;
  341. out:
  342. mutex_unlock(&p->mutex);
  343. return err;
  344. }
  345. static int kfd_ioctl_dbg_register(struct file *filep,
  346. struct kfd_process *p, void *data)
  347. {
  348. struct kfd_ioctl_dbg_register_args *args = data;
  349. struct kfd_dev *dev;
  350. struct kfd_dbgmgr *dbgmgr_ptr;
  351. struct kfd_process_device *pdd;
  352. bool create_ok;
  353. long status = 0;
  354. dev = kfd_device_by_id(args->gpu_id);
  355. if (dev == NULL)
  356. return -EINVAL;
  357. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  358. pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
  359. return -EINVAL;
  360. }
  361. mutex_lock(kfd_get_dbgmgr_mutex());
  362. mutex_lock(&p->mutex);
  363. /*
  364. * make sure that we have pdd, if this the first queue created for
  365. * this process
  366. */
  367. pdd = kfd_bind_process_to_device(dev, p);
  368. if (IS_ERR(pdd)) {
  369. mutex_unlock(&p->mutex);
  370. mutex_unlock(kfd_get_dbgmgr_mutex());
  371. return PTR_ERR(pdd);
  372. }
  373. if (dev->dbgmgr == NULL) {
  374. /* In case of a legal call, we have no dbgmgr yet */
  375. create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev);
  376. if (create_ok) {
  377. status = kfd_dbgmgr_register(dbgmgr_ptr, p);
  378. if (status != 0)
  379. kfd_dbgmgr_destroy(dbgmgr_ptr);
  380. else
  381. dev->dbgmgr = dbgmgr_ptr;
  382. }
  383. } else {
  384. pr_debug("debugger already registered\n");
  385. status = -EINVAL;
  386. }
  387. mutex_unlock(&p->mutex);
  388. mutex_unlock(kfd_get_dbgmgr_mutex());
  389. return status;
  390. }
  391. static int kfd_ioctl_dbg_unregister(struct file *filep,
  392. struct kfd_process *p, void *data)
  393. {
  394. struct kfd_ioctl_dbg_unregister_args *args = data;
  395. struct kfd_dev *dev;
  396. long status;
  397. dev = kfd_device_by_id(args->gpu_id);
  398. if (dev == NULL)
  399. return -EINVAL;
  400. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  401. pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n");
  402. return -EINVAL;
  403. }
  404. mutex_lock(kfd_get_dbgmgr_mutex());
  405. status = kfd_dbgmgr_unregister(dev->dbgmgr, p);
  406. if (status == 0) {
  407. kfd_dbgmgr_destroy(dev->dbgmgr);
  408. dev->dbgmgr = NULL;
  409. }
  410. mutex_unlock(kfd_get_dbgmgr_mutex());
  411. return status;
  412. }
  413. /*
  414. * Parse and generate variable size data structure for address watch.
  415. * Total size of the buffer and # watch points is limited in order
  416. * to prevent kernel abuse. (no bearing to the much smaller HW limitation
  417. * which is enforced by dbgdev module)
  418. * please also note that the watch address itself are not "copied from user",
  419. * since it be set into the HW in user mode values.
  420. *
  421. */
  422. static int kfd_ioctl_dbg_address_watch(struct file *filep,
  423. struct kfd_process *p, void *data)
  424. {
  425. struct kfd_ioctl_dbg_address_watch_args *args = data;
  426. struct kfd_dev *dev;
  427. struct dbg_address_watch_info aw_info;
  428. unsigned char *args_buff;
  429. long status;
  430. void __user *cmd_from_user;
  431. uint64_t watch_mask_value = 0;
  432. unsigned int args_idx = 0;
  433. memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info));
  434. dev = kfd_device_by_id(args->gpu_id);
  435. if (dev == NULL)
  436. return -EINVAL;
  437. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  438. pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
  439. return -EINVAL;
  440. }
  441. cmd_from_user = (void __user *) args->content_ptr;
  442. /* Validate arguments */
  443. if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) ||
  444. (args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) ||
  445. (cmd_from_user == NULL))
  446. return -EINVAL;
  447. /* this is the actual buffer to work with */
  448. args_buff = memdup_user(cmd_from_user,
  449. args->buf_size_in_bytes - sizeof(*args));
  450. if (IS_ERR(args_buff))
  451. return PTR_ERR(args_buff);
  452. aw_info.process = p;
  453. aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx]));
  454. args_idx += sizeof(aw_info.num_watch_points);
  455. aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx];
  456. args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points;
  457. /*
  458. * set watch address base pointer to point on the array base
  459. * within args_buff
  460. */
  461. aw_info.watch_address = (uint64_t *) &args_buff[args_idx];
  462. /* skip over the addresses buffer */
  463. args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points;
  464. if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) {
  465. kfree(args_buff);
  466. return -EINVAL;
  467. }
  468. watch_mask_value = (uint64_t) args_buff[args_idx];
  469. if (watch_mask_value > 0) {
  470. /*
  471. * There is an array of masks.
  472. * set watch mask base pointer to point on the array base
  473. * within args_buff
  474. */
  475. aw_info.watch_mask = (uint64_t *) &args_buff[args_idx];
  476. /* skip over the masks buffer */
  477. args_idx += sizeof(aw_info.watch_mask) *
  478. aw_info.num_watch_points;
  479. } else {
  480. /* just the NULL mask, set to NULL and skip over it */
  481. aw_info.watch_mask = NULL;
  482. args_idx += sizeof(aw_info.watch_mask);
  483. }
  484. if (args_idx >= args->buf_size_in_bytes - sizeof(args)) {
  485. kfree(args_buff);
  486. return -EINVAL;
  487. }
  488. /* Currently HSA Event is not supported for DBG */
  489. aw_info.watch_event = NULL;
  490. mutex_lock(kfd_get_dbgmgr_mutex());
  491. status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info);
  492. mutex_unlock(kfd_get_dbgmgr_mutex());
  493. kfree(args_buff);
  494. return status;
  495. }
  496. /* Parse and generate fixed size data structure for wave control */
  497. static int kfd_ioctl_dbg_wave_control(struct file *filep,
  498. struct kfd_process *p, void *data)
  499. {
  500. struct kfd_ioctl_dbg_wave_control_args *args = data;
  501. struct kfd_dev *dev;
  502. struct dbg_wave_control_info wac_info;
  503. unsigned char *args_buff;
  504. uint32_t computed_buff_size;
  505. long status;
  506. void __user *cmd_from_user;
  507. unsigned int args_idx = 0;
  508. memset((void *) &wac_info, 0, sizeof(struct dbg_wave_control_info));
  509. /* we use compact form, independent of the packing attribute value */
  510. computed_buff_size = sizeof(*args) +
  511. sizeof(wac_info.mode) +
  512. sizeof(wac_info.operand) +
  513. sizeof(wac_info.dbgWave_msg.DbgWaveMsg) +
  514. sizeof(wac_info.dbgWave_msg.MemoryVA) +
  515. sizeof(wac_info.trapId);
  516. dev = kfd_device_by_id(args->gpu_id);
  517. if (dev == NULL)
  518. return -EINVAL;
  519. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  520. pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
  521. return -EINVAL;
  522. }
  523. /* input size must match the computed "compact" size */
  524. if (args->buf_size_in_bytes != computed_buff_size) {
  525. pr_debug("size mismatch, computed : actual %u : %u\n",
  526. args->buf_size_in_bytes, computed_buff_size);
  527. return -EINVAL;
  528. }
  529. cmd_from_user = (void __user *) args->content_ptr;
  530. if (cmd_from_user == NULL)
  531. return -EINVAL;
  532. /* copy the entire buffer from user */
  533. args_buff = memdup_user(cmd_from_user,
  534. args->buf_size_in_bytes - sizeof(*args));
  535. if (IS_ERR(args_buff))
  536. return PTR_ERR(args_buff);
  537. /* move ptr to the start of the "pay-load" area */
  538. wac_info.process = p;
  539. wac_info.operand = *((enum HSA_DBG_WAVEOP *)(&args_buff[args_idx]));
  540. args_idx += sizeof(wac_info.operand);
  541. wac_info.mode = *((enum HSA_DBG_WAVEMODE *)(&args_buff[args_idx]));
  542. args_idx += sizeof(wac_info.mode);
  543. wac_info.trapId = *((uint32_t *)(&args_buff[args_idx]));
  544. args_idx += sizeof(wac_info.trapId);
  545. wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value =
  546. *((uint32_t *)(&args_buff[args_idx]));
  547. wac_info.dbgWave_msg.MemoryVA = NULL;
  548. mutex_lock(kfd_get_dbgmgr_mutex());
  549. pr_debug("Calling dbg manager process %p, operand %u, mode %u, trapId %u, message %u\n",
  550. wac_info.process, wac_info.operand,
  551. wac_info.mode, wac_info.trapId,
  552. wac_info.dbgWave_msg.DbgWaveMsg.WaveMsgInfoGen2.Value);
  553. status = kfd_dbgmgr_wave_control(dev->dbgmgr, &wac_info);
  554. pr_debug("Returned status of dbg manager is %ld\n", status);
  555. mutex_unlock(kfd_get_dbgmgr_mutex());
  556. kfree(args_buff);
  557. return status;
  558. }
  559. static int kfd_ioctl_get_clock_counters(struct file *filep,
  560. struct kfd_process *p, void *data)
  561. {
  562. struct kfd_ioctl_get_clock_counters_args *args = data;
  563. struct kfd_dev *dev;
  564. struct timespec64 time;
  565. dev = kfd_device_by_id(args->gpu_id);
  566. if (dev == NULL)
  567. return -EINVAL;
  568. /* Reading GPU clock counter from KGD */
  569. args->gpu_clock_counter =
  570. dev->kfd2kgd->get_gpu_clock_counter(dev->kgd);
  571. /* No access to rdtsc. Using raw monotonic time */
  572. getrawmonotonic64(&time);
  573. args->cpu_clock_counter = (uint64_t)timespec64_to_ns(&time);
  574. get_monotonic_boottime64(&time);
  575. args->system_clock_counter = (uint64_t)timespec64_to_ns(&time);
  576. /* Since the counter is in nano-seconds we use 1GHz frequency */
  577. args->system_clock_freq = 1000000000;
  578. return 0;
  579. }
  580. static int kfd_ioctl_get_process_apertures(struct file *filp,
  581. struct kfd_process *p, void *data)
  582. {
  583. struct kfd_ioctl_get_process_apertures_args *args = data;
  584. struct kfd_process_device_apertures *pAperture;
  585. struct kfd_process_device *pdd;
  586. dev_dbg(kfd_device, "get apertures for PASID %d", p->pasid);
  587. args->num_of_nodes = 0;
  588. mutex_lock(&p->mutex);
  589. /*if the process-device list isn't empty*/
  590. if (kfd_has_process_device_data(p)) {
  591. /* Run over all pdd of the process */
  592. pdd = kfd_get_first_process_device_data(p);
  593. do {
  594. pAperture =
  595. &args->process_apertures[args->num_of_nodes];
  596. pAperture->gpu_id = pdd->dev->id;
  597. pAperture->lds_base = pdd->lds_base;
  598. pAperture->lds_limit = pdd->lds_limit;
  599. pAperture->gpuvm_base = pdd->gpuvm_base;
  600. pAperture->gpuvm_limit = pdd->gpuvm_limit;
  601. pAperture->scratch_base = pdd->scratch_base;
  602. pAperture->scratch_limit = pdd->scratch_limit;
  603. dev_dbg(kfd_device,
  604. "node id %u\n", args->num_of_nodes);
  605. dev_dbg(kfd_device,
  606. "gpu id %u\n", pdd->dev->id);
  607. dev_dbg(kfd_device,
  608. "lds_base %llX\n", pdd->lds_base);
  609. dev_dbg(kfd_device,
  610. "lds_limit %llX\n", pdd->lds_limit);
  611. dev_dbg(kfd_device,
  612. "gpuvm_base %llX\n", pdd->gpuvm_base);
  613. dev_dbg(kfd_device,
  614. "gpuvm_limit %llX\n", pdd->gpuvm_limit);
  615. dev_dbg(kfd_device,
  616. "scratch_base %llX\n", pdd->scratch_base);
  617. dev_dbg(kfd_device,
  618. "scratch_limit %llX\n", pdd->scratch_limit);
  619. args->num_of_nodes++;
  620. } while ((pdd = kfd_get_next_process_device_data(p, pdd)) != NULL &&
  621. (args->num_of_nodes < NUM_OF_SUPPORTED_GPUS));
  622. }
  623. mutex_unlock(&p->mutex);
  624. return 0;
  625. }
  626. static int kfd_ioctl_create_event(struct file *filp, struct kfd_process *p,
  627. void *data)
  628. {
  629. struct kfd_ioctl_create_event_args *args = data;
  630. int err;
  631. err = kfd_event_create(filp, p, args->event_type,
  632. args->auto_reset != 0, args->node_id,
  633. &args->event_id, &args->event_trigger_data,
  634. &args->event_page_offset,
  635. &args->event_slot_index);
  636. return err;
  637. }
  638. static int kfd_ioctl_destroy_event(struct file *filp, struct kfd_process *p,
  639. void *data)
  640. {
  641. struct kfd_ioctl_destroy_event_args *args = data;
  642. return kfd_event_destroy(p, args->event_id);
  643. }
  644. static int kfd_ioctl_set_event(struct file *filp, struct kfd_process *p,
  645. void *data)
  646. {
  647. struct kfd_ioctl_set_event_args *args = data;
  648. return kfd_set_event(p, args->event_id);
  649. }
  650. static int kfd_ioctl_reset_event(struct file *filp, struct kfd_process *p,
  651. void *data)
  652. {
  653. struct kfd_ioctl_reset_event_args *args = data;
  654. return kfd_reset_event(p, args->event_id);
  655. }
  656. static int kfd_ioctl_wait_events(struct file *filp, struct kfd_process *p,
  657. void *data)
  658. {
  659. struct kfd_ioctl_wait_events_args *args = data;
  660. enum kfd_event_wait_result wait_result;
  661. int err;
  662. err = kfd_wait_on_events(p, args->num_events,
  663. (void __user *)args->events_ptr,
  664. (args->wait_for_all != 0),
  665. args->timeout, &wait_result);
  666. args->wait_result = wait_result;
  667. return err;
  668. }
  669. #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
  670. [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0, .name = #ioctl}
  671. /** Ioctl table */
  672. static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
  673. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
  674. kfd_ioctl_get_version, 0),
  675. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
  676. kfd_ioctl_create_queue, 0),
  677. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
  678. kfd_ioctl_destroy_queue, 0),
  679. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
  680. kfd_ioctl_set_memory_policy, 0),
  681. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
  682. kfd_ioctl_get_clock_counters, 0),
  683. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
  684. kfd_ioctl_get_process_apertures, 0),
  685. AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
  686. kfd_ioctl_update_queue, 0),
  687. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
  688. kfd_ioctl_create_event, 0),
  689. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
  690. kfd_ioctl_destroy_event, 0),
  691. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
  692. kfd_ioctl_set_event, 0),
  693. AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
  694. kfd_ioctl_reset_event, 0),
  695. AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
  696. kfd_ioctl_wait_events, 0),
  697. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER,
  698. kfd_ioctl_dbg_register, 0),
  699. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER,
  700. kfd_ioctl_dbg_unregister, 0),
  701. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH,
  702. kfd_ioctl_dbg_address_watch, 0),
  703. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL,
  704. kfd_ioctl_dbg_wave_control, 0),
  705. };
  706. #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
  707. static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  708. {
  709. struct kfd_process *process;
  710. amdkfd_ioctl_t *func;
  711. const struct amdkfd_ioctl_desc *ioctl = NULL;
  712. unsigned int nr = _IOC_NR(cmd);
  713. char stack_kdata[128];
  714. char *kdata = NULL;
  715. unsigned int usize, asize;
  716. int retcode = -EINVAL;
  717. if (nr >= AMDKFD_CORE_IOCTL_COUNT)
  718. goto err_i1;
  719. if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
  720. u32 amdkfd_size;
  721. ioctl = &amdkfd_ioctls[nr];
  722. amdkfd_size = _IOC_SIZE(ioctl->cmd);
  723. usize = asize = _IOC_SIZE(cmd);
  724. if (amdkfd_size > asize)
  725. asize = amdkfd_size;
  726. cmd = ioctl->cmd;
  727. } else
  728. goto err_i1;
  729. dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg);
  730. process = kfd_get_process(current);
  731. if (IS_ERR(process)) {
  732. dev_dbg(kfd_device, "no process\n");
  733. goto err_i1;
  734. }
  735. /* Do not trust userspace, use our own definition */
  736. func = ioctl->func;
  737. if (unlikely(!func)) {
  738. dev_dbg(kfd_device, "no function\n");
  739. retcode = -EINVAL;
  740. goto err_i1;
  741. }
  742. if (cmd & (IOC_IN | IOC_OUT)) {
  743. if (asize <= sizeof(stack_kdata)) {
  744. kdata = stack_kdata;
  745. } else {
  746. kdata = kmalloc(asize, GFP_KERNEL);
  747. if (!kdata) {
  748. retcode = -ENOMEM;
  749. goto err_i1;
  750. }
  751. }
  752. if (asize > usize)
  753. memset(kdata + usize, 0, asize - usize);
  754. }
  755. if (cmd & IOC_IN) {
  756. if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
  757. retcode = -EFAULT;
  758. goto err_i1;
  759. }
  760. } else if (cmd & IOC_OUT) {
  761. memset(kdata, 0, usize);
  762. }
  763. retcode = func(filep, process, kdata);
  764. if (cmd & IOC_OUT)
  765. if (copy_to_user((void __user *)arg, kdata, usize) != 0)
  766. retcode = -EFAULT;
  767. err_i1:
  768. if (!ioctl)
  769. dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
  770. task_pid_nr(current), cmd, nr);
  771. if (kdata != stack_kdata)
  772. kfree(kdata);
  773. if (retcode)
  774. dev_dbg(kfd_device, "ret = %d\n", retcode);
  775. return retcode;
  776. }
  777. static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
  778. {
  779. struct kfd_process *process;
  780. process = kfd_get_process(current);
  781. if (IS_ERR(process))
  782. return PTR_ERR(process);
  783. if ((vma->vm_pgoff & KFD_MMAP_DOORBELL_MASK) ==
  784. KFD_MMAP_DOORBELL_MASK) {
  785. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_DOORBELL_MASK;
  786. return kfd_doorbell_mmap(process, vma);
  787. } else if ((vma->vm_pgoff & KFD_MMAP_EVENTS_MASK) ==
  788. KFD_MMAP_EVENTS_MASK) {
  789. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_EVENTS_MASK;
  790. return kfd_event_mmap(process, vma);
  791. }
  792. return -EFAULT;
  793. }