kfd_chardev.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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("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("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("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("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("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("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("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("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: %p, %p\n",
  196. q_properties->read_ptr,
  197. 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 area: 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("Creating queue ioctl\n");
  215. err = set_queue_properties_from_user(&q_properties, args);
  216. if (err)
  217. return err;
  218. pr_debug("Looking for gpu id 0x%x\n", args->gpu_id);
  219. dev = kfd_device_by_id(args->gpu_id);
  220. if (!dev) {
  221. pr_debug("Could not find gpu id 0x%x\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("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, &queue_id);
  234. if (err != 0)
  235. goto err_create_queue;
  236. args->queue_id = queue_id;
  237. /* Return gpu_id as doorbell offset for mmap usage */
  238. args->doorbell_offset = (KFD_MMAP_DOORBELL_MASK | args->gpu_id);
  239. args->doorbell_offset <<= PAGE_SHIFT;
  240. mutex_unlock(&p->mutex);
  241. pr_debug("Queue id %d was created successfully\n", args->queue_id);
  242. pr_debug("Ring buffer address == 0x%016llX\n",
  243. args->ring_base_address);
  244. pr_debug("Read ptr address == 0x%016llX\n",
  245. args->read_pointer_address);
  246. pr_debug("Write ptr address == 0x%016llX\n",
  247. args->write_pointer_address);
  248. return 0;
  249. err_create_queue:
  250. err_bind_process:
  251. mutex_unlock(&p->mutex);
  252. return err;
  253. }
  254. static int kfd_ioctl_destroy_queue(struct file *filp, struct kfd_process *p,
  255. void *data)
  256. {
  257. int retval;
  258. struct kfd_ioctl_destroy_queue_args *args = data;
  259. pr_debug("Destroying queue id %d for pasid %d\n",
  260. args->queue_id,
  261. p->pasid);
  262. mutex_lock(&p->mutex);
  263. retval = pqm_destroy_queue(&p->pqm, args->queue_id);
  264. mutex_unlock(&p->mutex);
  265. return retval;
  266. }
  267. static int kfd_ioctl_update_queue(struct file *filp, struct kfd_process *p,
  268. void *data)
  269. {
  270. int retval;
  271. struct kfd_ioctl_update_queue_args *args = data;
  272. struct queue_properties properties;
  273. if (args->queue_percentage > KFD_MAX_QUEUE_PERCENTAGE) {
  274. pr_err("Queue percentage must be between 0 to KFD_MAX_QUEUE_PERCENTAGE\n");
  275. return -EINVAL;
  276. }
  277. if (args->queue_priority > KFD_MAX_QUEUE_PRIORITY) {
  278. pr_err("Queue priority must be between 0 to KFD_MAX_QUEUE_PRIORITY\n");
  279. return -EINVAL;
  280. }
  281. if ((args->ring_base_address) &&
  282. (!access_ok(VERIFY_WRITE,
  283. (const void __user *) args->ring_base_address,
  284. sizeof(uint64_t)))) {
  285. pr_err("Can't access ring base address\n");
  286. return -EFAULT;
  287. }
  288. if (!is_power_of_2(args->ring_size) && (args->ring_size != 0)) {
  289. pr_err("Ring size must be a power of 2 or 0\n");
  290. return -EINVAL;
  291. }
  292. properties.queue_address = args->ring_base_address;
  293. properties.queue_size = args->ring_size;
  294. properties.queue_percent = args->queue_percentage;
  295. properties.priority = args->queue_priority;
  296. pr_debug("Updating queue id %d for pasid %d\n",
  297. args->queue_id, p->pasid);
  298. mutex_lock(&p->mutex);
  299. retval = pqm_update_queue(&p->pqm, args->queue_id, &properties);
  300. mutex_unlock(&p->mutex);
  301. return retval;
  302. }
  303. static int kfd_ioctl_set_memory_policy(struct file *filep,
  304. struct kfd_process *p, void *data)
  305. {
  306. struct kfd_ioctl_set_memory_policy_args *args = data;
  307. struct kfd_dev *dev;
  308. int err = 0;
  309. struct kfd_process_device *pdd;
  310. enum cache_policy default_policy, alternate_policy;
  311. if (args->default_policy != KFD_IOC_CACHE_POLICY_COHERENT
  312. && args->default_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  313. return -EINVAL;
  314. }
  315. if (args->alternate_policy != KFD_IOC_CACHE_POLICY_COHERENT
  316. && args->alternate_policy != KFD_IOC_CACHE_POLICY_NONCOHERENT) {
  317. return -EINVAL;
  318. }
  319. dev = kfd_device_by_id(args->gpu_id);
  320. if (!dev)
  321. return -EINVAL;
  322. mutex_lock(&p->mutex);
  323. pdd = kfd_bind_process_to_device(dev, p);
  324. if (IS_ERR(pdd)) {
  325. err = -ESRCH;
  326. goto out;
  327. }
  328. default_policy = (args->default_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  329. ? cache_policy_coherent : cache_policy_noncoherent;
  330. alternate_policy =
  331. (args->alternate_policy == KFD_IOC_CACHE_POLICY_COHERENT)
  332. ? cache_policy_coherent : cache_policy_noncoherent;
  333. if (!dev->dqm->ops.set_cache_memory_policy(dev->dqm,
  334. &pdd->qpd,
  335. default_policy,
  336. alternate_policy,
  337. (void __user *)args->alternate_aperture_base,
  338. args->alternate_aperture_size))
  339. err = -EINVAL;
  340. out:
  341. mutex_unlock(&p->mutex);
  342. return err;
  343. }
  344. static int kfd_ioctl_dbg_register(struct file *filep,
  345. struct kfd_process *p, void *data)
  346. {
  347. struct kfd_ioctl_dbg_register_args *args = data;
  348. struct kfd_dev *dev;
  349. struct kfd_dbgmgr *dbgmgr_ptr;
  350. struct kfd_process_device *pdd;
  351. bool create_ok;
  352. long status = 0;
  353. dev = kfd_device_by_id(args->gpu_id);
  354. if (!dev)
  355. return -EINVAL;
  356. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  357. pr_debug("kfd_ioctl_dbg_register not supported on CZ\n");
  358. return -EINVAL;
  359. }
  360. mutex_lock(&p->mutex);
  361. mutex_lock(kfd_get_dbgmgr_mutex());
  362. /*
  363. * make sure that we have pdd, if this the first queue created for
  364. * this process
  365. */
  366. pdd = kfd_bind_process_to_device(dev, p);
  367. if (IS_ERR(pdd)) {
  368. status = PTR_ERR(pdd);
  369. goto out;
  370. }
  371. if (!dev->dbgmgr) {
  372. /* In case of a legal call, we have no dbgmgr yet */
  373. create_ok = kfd_dbgmgr_create(&dbgmgr_ptr, dev);
  374. if (create_ok) {
  375. status = kfd_dbgmgr_register(dbgmgr_ptr, p);
  376. if (status != 0)
  377. kfd_dbgmgr_destroy(dbgmgr_ptr);
  378. else
  379. dev->dbgmgr = dbgmgr_ptr;
  380. }
  381. } else {
  382. pr_debug("debugger already registered\n");
  383. status = -EINVAL;
  384. }
  385. out:
  386. mutex_unlock(kfd_get_dbgmgr_mutex());
  387. mutex_unlock(&p->mutex);
  388. return status;
  389. }
  390. static int kfd_ioctl_dbg_unregister(struct file *filep,
  391. struct kfd_process *p, void *data)
  392. {
  393. struct kfd_ioctl_dbg_unregister_args *args = data;
  394. struct kfd_dev *dev;
  395. long status;
  396. dev = kfd_device_by_id(args->gpu_id);
  397. if (!dev)
  398. return -EINVAL;
  399. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  400. pr_debug("kfd_ioctl_dbg_unregister not supported on CZ\n");
  401. return -EINVAL;
  402. }
  403. mutex_lock(kfd_get_dbgmgr_mutex());
  404. status = kfd_dbgmgr_unregister(dev->dbgmgr, p);
  405. if (!status) {
  406. kfd_dbgmgr_destroy(dev->dbgmgr);
  407. dev->dbgmgr = NULL;
  408. }
  409. mutex_unlock(kfd_get_dbgmgr_mutex());
  410. return status;
  411. }
  412. /*
  413. * Parse and generate variable size data structure for address watch.
  414. * Total size of the buffer and # watch points is limited in order
  415. * to prevent kernel abuse. (no bearing to the much smaller HW limitation
  416. * which is enforced by dbgdev module)
  417. * please also note that the watch address itself are not "copied from user",
  418. * since it be set into the HW in user mode values.
  419. *
  420. */
  421. static int kfd_ioctl_dbg_address_watch(struct file *filep,
  422. struct kfd_process *p, void *data)
  423. {
  424. struct kfd_ioctl_dbg_address_watch_args *args = data;
  425. struct kfd_dev *dev;
  426. struct dbg_address_watch_info aw_info;
  427. unsigned char *args_buff;
  428. long status;
  429. void __user *cmd_from_user;
  430. uint64_t watch_mask_value = 0;
  431. unsigned int args_idx = 0;
  432. memset((void *) &aw_info, 0, sizeof(struct dbg_address_watch_info));
  433. dev = kfd_device_by_id(args->gpu_id);
  434. if (!dev)
  435. return -EINVAL;
  436. if (dev->device_info->asic_family == CHIP_CARRIZO) {
  437. pr_debug("kfd_ioctl_dbg_wave_control not supported on CZ\n");
  438. return -EINVAL;
  439. }
  440. cmd_from_user = (void __user *) args->content_ptr;
  441. /* Validate arguments */
  442. if ((args->buf_size_in_bytes > MAX_ALLOWED_AW_BUFF_SIZE) ||
  443. (args->buf_size_in_bytes <= sizeof(*args) + sizeof(int) * 2) ||
  444. (cmd_from_user == NULL))
  445. return -EINVAL;
  446. /* this is the actual buffer to work with */
  447. args_buff = memdup_user(cmd_from_user,
  448. args->buf_size_in_bytes - sizeof(*args));
  449. if (IS_ERR(args_buff))
  450. return PTR_ERR(args_buff);
  451. aw_info.process = p;
  452. aw_info.num_watch_points = *((uint32_t *)(&args_buff[args_idx]));
  453. args_idx += sizeof(aw_info.num_watch_points);
  454. aw_info.watch_mode = (enum HSA_DBG_WATCH_MODE *) &args_buff[args_idx];
  455. args_idx += sizeof(enum HSA_DBG_WATCH_MODE) * aw_info.num_watch_points;
  456. /*
  457. * set watch address base pointer to point on the array base
  458. * within args_buff
  459. */
  460. aw_info.watch_address = (uint64_t *) &args_buff[args_idx];
  461. /* skip over the addresses buffer */
  462. args_idx += sizeof(aw_info.watch_address) * aw_info.num_watch_points;
  463. if (args_idx >= args->buf_size_in_bytes - sizeof(*args)) {
  464. status = -EINVAL;
  465. goto out;
  466. }
  467. watch_mask_value = (uint64_t) args_buff[args_idx];
  468. if (watch_mask_value > 0) {
  469. /*
  470. * There is an array of masks.
  471. * set watch mask base pointer to point on the array base
  472. * within args_buff
  473. */
  474. aw_info.watch_mask = (uint64_t *) &args_buff[args_idx];
  475. /* skip over the masks buffer */
  476. args_idx += sizeof(aw_info.watch_mask) *
  477. aw_info.num_watch_points;
  478. } else {
  479. /* just the NULL mask, set to NULL and skip over it */
  480. aw_info.watch_mask = NULL;
  481. args_idx += sizeof(aw_info.watch_mask);
  482. }
  483. if (args_idx >= args->buf_size_in_bytes - sizeof(args)) {
  484. status = -EINVAL;
  485. goto out;
  486. }
  487. /* Currently HSA Event is not supported for DBG */
  488. aw_info.watch_event = NULL;
  489. mutex_lock(kfd_get_dbgmgr_mutex());
  490. status = kfd_dbgmgr_address_watch(dev->dbgmgr, &aw_info);
  491. mutex_unlock(kfd_get_dbgmgr_mutex());
  492. out:
  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)
  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. pdd = kfd_get_next_process_device_data(p, pdd);
  621. } while (pdd && (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. int err;
  661. err = kfd_wait_on_events(p, args->num_events,
  662. (void __user *)args->events_ptr,
  663. (args->wait_for_all != 0),
  664. args->timeout, &args->wait_result);
  665. return err;
  666. }
  667. static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
  668. struct kfd_process *p, void *data)
  669. {
  670. struct kfd_ioctl_set_scratch_backing_va_args *args = data;
  671. struct kfd_process_device *pdd;
  672. struct kfd_dev *dev;
  673. long err;
  674. dev = kfd_device_by_id(args->gpu_id);
  675. if (!dev)
  676. return -EINVAL;
  677. mutex_lock(&p->mutex);
  678. pdd = kfd_bind_process_to_device(dev, p);
  679. if (IS_ERR(pdd)) {
  680. err = PTR_ERR(pdd);
  681. goto bind_process_to_device_fail;
  682. }
  683. pdd->qpd.sh_hidden_private_base = args->va_addr;
  684. mutex_unlock(&p->mutex);
  685. if (sched_policy == KFD_SCHED_POLICY_NO_HWS && pdd->qpd.vmid != 0)
  686. dev->kfd2kgd->set_scratch_backing_va(
  687. dev->kgd, args->va_addr, pdd->qpd.vmid);
  688. return 0;
  689. bind_process_to_device_fail:
  690. mutex_unlock(&p->mutex);
  691. return err;
  692. }
  693. static int kfd_ioctl_get_tile_config(struct file *filep,
  694. struct kfd_process *p, void *data)
  695. {
  696. struct kfd_ioctl_get_tile_config_args *args = data;
  697. struct kfd_dev *dev;
  698. struct tile_config config;
  699. int err = 0;
  700. dev = kfd_device_by_id(args->gpu_id);
  701. if (!dev)
  702. return -EINVAL;
  703. dev->kfd2kgd->get_tile_config(dev->kgd, &config);
  704. args->gb_addr_config = config.gb_addr_config;
  705. args->num_banks = config.num_banks;
  706. args->num_ranks = config.num_ranks;
  707. if (args->num_tile_configs > config.num_tile_configs)
  708. args->num_tile_configs = config.num_tile_configs;
  709. err = copy_to_user((void __user *)args->tile_config_ptr,
  710. config.tile_config_ptr,
  711. args->num_tile_configs * sizeof(uint32_t));
  712. if (err) {
  713. args->num_tile_configs = 0;
  714. return -EFAULT;
  715. }
  716. if (args->num_macro_tile_configs > config.num_macro_tile_configs)
  717. args->num_macro_tile_configs =
  718. config.num_macro_tile_configs;
  719. err = copy_to_user((void __user *)args->macro_tile_config_ptr,
  720. config.macro_tile_config_ptr,
  721. args->num_macro_tile_configs * sizeof(uint32_t));
  722. if (err) {
  723. args->num_macro_tile_configs = 0;
  724. return -EFAULT;
  725. }
  726. return 0;
  727. }
  728. #define AMDKFD_IOCTL_DEF(ioctl, _func, _flags) \
  729. [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \
  730. .cmd_drv = 0, .name = #ioctl}
  731. /** Ioctl table */
  732. static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {
  733. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_VERSION,
  734. kfd_ioctl_get_version, 0),
  735. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_QUEUE,
  736. kfd_ioctl_create_queue, 0),
  737. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_QUEUE,
  738. kfd_ioctl_destroy_queue, 0),
  739. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_MEMORY_POLICY,
  740. kfd_ioctl_set_memory_policy, 0),
  741. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_CLOCK_COUNTERS,
  742. kfd_ioctl_get_clock_counters, 0),
  743. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_PROCESS_APERTURES,
  744. kfd_ioctl_get_process_apertures, 0),
  745. AMDKFD_IOCTL_DEF(AMDKFD_IOC_UPDATE_QUEUE,
  746. kfd_ioctl_update_queue, 0),
  747. AMDKFD_IOCTL_DEF(AMDKFD_IOC_CREATE_EVENT,
  748. kfd_ioctl_create_event, 0),
  749. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DESTROY_EVENT,
  750. kfd_ioctl_destroy_event, 0),
  751. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_EVENT,
  752. kfd_ioctl_set_event, 0),
  753. AMDKFD_IOCTL_DEF(AMDKFD_IOC_RESET_EVENT,
  754. kfd_ioctl_reset_event, 0),
  755. AMDKFD_IOCTL_DEF(AMDKFD_IOC_WAIT_EVENTS,
  756. kfd_ioctl_wait_events, 0),
  757. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_REGISTER,
  758. kfd_ioctl_dbg_register, 0),
  759. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_UNREGISTER,
  760. kfd_ioctl_dbg_unregister, 0),
  761. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_ADDRESS_WATCH,
  762. kfd_ioctl_dbg_address_watch, 0),
  763. AMDKFD_IOCTL_DEF(AMDKFD_IOC_DBG_WAVE_CONTROL,
  764. kfd_ioctl_dbg_wave_control, 0),
  765. AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_SCRATCH_BACKING_VA,
  766. kfd_ioctl_set_scratch_backing_va, 0),
  767. AMDKFD_IOCTL_DEF(AMDKFD_IOC_GET_TILE_CONFIG,
  768. kfd_ioctl_get_tile_config, 0)
  769. };
  770. #define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
  771. static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  772. {
  773. struct kfd_process *process;
  774. amdkfd_ioctl_t *func;
  775. const struct amdkfd_ioctl_desc *ioctl = NULL;
  776. unsigned int nr = _IOC_NR(cmd);
  777. char stack_kdata[128];
  778. char *kdata = NULL;
  779. unsigned int usize, asize;
  780. int retcode = -EINVAL;
  781. if (nr >= AMDKFD_CORE_IOCTL_COUNT)
  782. goto err_i1;
  783. if ((nr >= AMDKFD_COMMAND_START) && (nr < AMDKFD_COMMAND_END)) {
  784. u32 amdkfd_size;
  785. ioctl = &amdkfd_ioctls[nr];
  786. amdkfd_size = _IOC_SIZE(ioctl->cmd);
  787. usize = asize = _IOC_SIZE(cmd);
  788. if (amdkfd_size > asize)
  789. asize = amdkfd_size;
  790. cmd = ioctl->cmd;
  791. } else
  792. goto err_i1;
  793. dev_dbg(kfd_device, "ioctl cmd 0x%x (#%d), arg 0x%lx\n", cmd, nr, arg);
  794. process = kfd_get_process(current);
  795. if (IS_ERR(process)) {
  796. dev_dbg(kfd_device, "no process\n");
  797. goto err_i1;
  798. }
  799. /* Do not trust userspace, use our own definition */
  800. func = ioctl->func;
  801. if (unlikely(!func)) {
  802. dev_dbg(kfd_device, "no function\n");
  803. retcode = -EINVAL;
  804. goto err_i1;
  805. }
  806. if (cmd & (IOC_IN | IOC_OUT)) {
  807. if (asize <= sizeof(stack_kdata)) {
  808. kdata = stack_kdata;
  809. } else {
  810. kdata = kmalloc(asize, GFP_KERNEL);
  811. if (!kdata) {
  812. retcode = -ENOMEM;
  813. goto err_i1;
  814. }
  815. }
  816. if (asize > usize)
  817. memset(kdata + usize, 0, asize - usize);
  818. }
  819. if (cmd & IOC_IN) {
  820. if (copy_from_user(kdata, (void __user *)arg, usize) != 0) {
  821. retcode = -EFAULT;
  822. goto err_i1;
  823. }
  824. } else if (cmd & IOC_OUT) {
  825. memset(kdata, 0, usize);
  826. }
  827. retcode = func(filep, process, kdata);
  828. if (cmd & IOC_OUT)
  829. if (copy_to_user((void __user *)arg, kdata, usize) != 0)
  830. retcode = -EFAULT;
  831. err_i1:
  832. if (!ioctl)
  833. dev_dbg(kfd_device, "invalid ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
  834. task_pid_nr(current), cmd, nr);
  835. if (kdata != stack_kdata)
  836. kfree(kdata);
  837. if (retcode)
  838. dev_dbg(kfd_device, "ret = %d\n", retcode);
  839. return retcode;
  840. }
  841. static int kfd_mmap(struct file *filp, struct vm_area_struct *vma)
  842. {
  843. struct kfd_process *process;
  844. process = kfd_get_process(current);
  845. if (IS_ERR(process))
  846. return PTR_ERR(process);
  847. if ((vma->vm_pgoff & KFD_MMAP_DOORBELL_MASK) ==
  848. KFD_MMAP_DOORBELL_MASK) {
  849. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_DOORBELL_MASK;
  850. return kfd_doorbell_mmap(process, vma);
  851. } else if ((vma->vm_pgoff & KFD_MMAP_EVENTS_MASK) ==
  852. KFD_MMAP_EVENTS_MASK) {
  853. vma->vm_pgoff = vma->vm_pgoff ^ KFD_MMAP_EVENTS_MASK;
  854. return kfd_event_mmap(process, vma);
  855. }
  856. return -EFAULT;
  857. }