kfd_priv.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. #ifndef KFD_PRIV_H_INCLUDED
  23. #define KFD_PRIV_H_INCLUDED
  24. #include <linux/hashtable.h>
  25. #include <linux/mmu_notifier.h>
  26. #include <linux/mutex.h>
  27. #include <linux/types.h>
  28. #include <linux/atomic.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/kfd_ioctl.h>
  32. #include <linux/idr.h>
  33. #include <linux/kfifo.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/kref.h>
  36. #include <kgd_kfd_interface.h>
  37. #include "amd_shared.h"
  38. #define KFD_MAX_RING_ENTRY_SIZE 8
  39. #define KFD_SYSFS_FILE_MODE 0444
  40. /* GPU ID hash width in bits */
  41. #define KFD_GPU_ID_HASH_WIDTH 16
  42. /* Use upper bits of mmap offset to store KFD driver specific information.
  43. * BITS[63:62] - Encode MMAP type
  44. * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to
  45. * BITS[45:0] - MMAP offset value
  46. *
  47. * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
  48. * defines are w.r.t to PAGE_SIZE
  49. */
  50. #define KFD_MMAP_TYPE_SHIFT (62 - PAGE_SHIFT)
  51. #define KFD_MMAP_TYPE_MASK (0x3ULL << KFD_MMAP_TYPE_SHIFT)
  52. #define KFD_MMAP_TYPE_DOORBELL (0x3ULL << KFD_MMAP_TYPE_SHIFT)
  53. #define KFD_MMAP_TYPE_EVENTS (0x2ULL << KFD_MMAP_TYPE_SHIFT)
  54. #define KFD_MMAP_TYPE_RESERVED_MEM (0x1ULL << KFD_MMAP_TYPE_SHIFT)
  55. #define KFD_MMAP_GPU_ID_SHIFT (46 - PAGE_SHIFT)
  56. #define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \
  57. << KFD_MMAP_GPU_ID_SHIFT)
  58. #define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\
  59. & KFD_MMAP_GPU_ID_MASK)
  60. #define KFD_MMAP_GPU_ID_GET(offset) ((offset & KFD_MMAP_GPU_ID_MASK) \
  61. >> KFD_MMAP_GPU_ID_SHIFT)
  62. #define KFD_MMAP_OFFSET_VALUE_MASK (0x3FFFFFFFFFFFULL >> PAGE_SHIFT)
  63. #define KFD_MMAP_OFFSET_VALUE_GET(offset) (offset & KFD_MMAP_OFFSET_VALUE_MASK)
  64. /*
  65. * When working with cp scheduler we should assign the HIQ manually or via
  66. * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot
  67. * definitions for Kaveri. In Kaveri only the first ME queues participates
  68. * in the cp scheduling taking that in mind we set the HIQ slot in the
  69. * second ME.
  70. */
  71. #define KFD_CIK_HIQ_PIPE 4
  72. #define KFD_CIK_HIQ_QUEUE 0
  73. /* Macro for allocating structures */
  74. #define kfd_alloc_struct(ptr_to_struct) \
  75. ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
  76. #define KFD_MAX_NUM_OF_PROCESSES 512
  77. #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
  78. /*
  79. * Size of the per-process TBA+TMA buffer: 2 pages
  80. *
  81. * The first page is the TBA used for the CWSR ISA code. The second
  82. * page is used as TMA for daisy changing a user-mode trap handler.
  83. */
  84. #define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
  85. #define KFD_CWSR_TMA_OFFSET PAGE_SIZE
  86. /*
  87. * Kernel module parameter to specify maximum number of supported queues per
  88. * device
  89. */
  90. extern int max_num_of_queues_per_device;
  91. #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT 4096
  92. #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \
  93. (KFD_MAX_NUM_OF_PROCESSES * \
  94. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
  95. #define KFD_KERNEL_QUEUE_SIZE 2048
  96. /* Kernel module parameter to specify the scheduling policy */
  97. extern int sched_policy;
  98. /*
  99. * Kernel module parameter to specify the maximum process
  100. * number per HW scheduler
  101. */
  102. extern int hws_max_conc_proc;
  103. extern int cwsr_enable;
  104. /*
  105. * Kernel module parameter to specify whether to send sigterm to HSA process on
  106. * unhandled exception
  107. */
  108. extern int send_sigterm;
  109. /*
  110. * This kernel module is used to simulate large bar machine on non-large bar
  111. * enabled machines.
  112. */
  113. extern int debug_largebar;
  114. /*
  115. * Ignore CRAT table during KFD initialization, can be used to work around
  116. * broken CRAT tables on some AMD systems
  117. */
  118. extern int ignore_crat;
  119. /*
  120. * Set sh_mem_config.retry_disable on Vega10
  121. */
  122. extern int noretry;
  123. /*
  124. * Halt if HWS hang is detected
  125. */
  126. extern int halt_if_hws_hang;
  127. /**
  128. * enum kfd_sched_policy
  129. *
  130. * @KFD_SCHED_POLICY_HWS: H/W scheduling policy known as command processor (cp)
  131. * scheduling. In this scheduling mode we're using the firmware code to
  132. * schedule the user mode queues and kernel queues such as HIQ and DIQ.
  133. * the HIQ queue is used as a special queue that dispatches the configuration
  134. * to the cp and the user mode queues list that are currently running.
  135. * the DIQ queue is a debugging queue that dispatches debugging commands to the
  136. * firmware.
  137. * in this scheduling mode user mode queues over subscription feature is
  138. * enabled.
  139. *
  140. * @KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION: The same as above but the over
  141. * subscription feature disabled.
  142. *
  143. * @KFD_SCHED_POLICY_NO_HWS: no H/W scheduling policy is a mode which directly
  144. * set the command processor registers and sets the queues "manually". This
  145. * mode is used *ONLY* for debugging proposes.
  146. *
  147. */
  148. enum kfd_sched_policy {
  149. KFD_SCHED_POLICY_HWS = 0,
  150. KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION,
  151. KFD_SCHED_POLICY_NO_HWS
  152. };
  153. enum cache_policy {
  154. cache_policy_coherent,
  155. cache_policy_noncoherent
  156. };
  157. #define KFD_IS_SOC15(chip) ((chip) >= CHIP_VEGA10)
  158. struct kfd_event_interrupt_class {
  159. bool (*interrupt_isr)(struct kfd_dev *dev,
  160. const uint32_t *ih_ring_entry, uint32_t *patched_ihre,
  161. bool *patched_flag);
  162. void (*interrupt_wq)(struct kfd_dev *dev,
  163. const uint32_t *ih_ring_entry);
  164. };
  165. struct kfd_device_info {
  166. enum amd_asic_type asic_family;
  167. const struct kfd_event_interrupt_class *event_interrupt_class;
  168. unsigned int max_pasid_bits;
  169. unsigned int max_no_of_hqd;
  170. unsigned int doorbell_size;
  171. size_t ih_ring_entry_size;
  172. uint8_t num_of_watch_points;
  173. uint16_t mqd_size_aligned;
  174. bool supports_cwsr;
  175. bool needs_iommu_device;
  176. bool needs_pci_atomics;
  177. unsigned int num_sdma_engines;
  178. };
  179. struct kfd_mem_obj {
  180. uint32_t range_start;
  181. uint32_t range_end;
  182. uint64_t gpu_addr;
  183. uint32_t *cpu_ptr;
  184. void *gtt_mem;
  185. };
  186. struct kfd_vmid_info {
  187. uint32_t first_vmid_kfd;
  188. uint32_t last_vmid_kfd;
  189. uint32_t vmid_num_kfd;
  190. };
  191. struct kfd_dev {
  192. struct kgd_dev *kgd;
  193. const struct kfd_device_info *device_info;
  194. struct pci_dev *pdev;
  195. unsigned int id; /* topology stub index */
  196. phys_addr_t doorbell_base; /* Start of actual doorbells used by
  197. * KFD. It is aligned for mapping
  198. * into user mode
  199. */
  200. size_t doorbell_id_offset; /* Doorbell offset (from KFD doorbell
  201. * to HW doorbell, GFX reserved some
  202. * at the start)
  203. */
  204. u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
  205. * page used by kernel queue
  206. */
  207. struct kgd2kfd_shared_resources shared_resources;
  208. struct kfd_vmid_info vm_info;
  209. const struct kfd2kgd_calls *kfd2kgd;
  210. struct mutex doorbell_mutex;
  211. DECLARE_BITMAP(doorbell_available_index,
  212. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
  213. void *gtt_mem;
  214. uint64_t gtt_start_gpu_addr;
  215. void *gtt_start_cpu_ptr;
  216. void *gtt_sa_bitmap;
  217. struct mutex gtt_sa_lock;
  218. unsigned int gtt_sa_chunk_size;
  219. unsigned int gtt_sa_num_of_chunks;
  220. /* Interrupts */
  221. struct kfifo ih_fifo;
  222. struct workqueue_struct *ih_wq;
  223. struct work_struct interrupt_work;
  224. spinlock_t interrupt_lock;
  225. /* QCM Device instance */
  226. struct device_queue_manager *dqm;
  227. bool init_complete;
  228. /*
  229. * Interrupts of interest to KFD are copied
  230. * from the HW ring into a SW ring.
  231. */
  232. bool interrupts_active;
  233. /* Debug manager */
  234. struct kfd_dbgmgr *dbgmgr;
  235. /* Maximum process number mapped to HW scheduler */
  236. unsigned int max_proc_per_quantum;
  237. /* CWSR */
  238. bool cwsr_enabled;
  239. const void *cwsr_isa;
  240. unsigned int cwsr_isa_size;
  241. };
  242. /* KGD2KFD callbacks */
  243. void kgd2kfd_exit(void);
  244. struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
  245. struct pci_dev *pdev, const struct kfd2kgd_calls *f2g);
  246. bool kgd2kfd_device_init(struct kfd_dev *kfd,
  247. const struct kgd2kfd_shared_resources *gpu_resources);
  248. void kgd2kfd_device_exit(struct kfd_dev *kfd);
  249. enum kfd_mempool {
  250. KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
  251. KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
  252. KFD_MEMPOOL_FRAMEBUFFER = 3,
  253. };
  254. /* Character device interface */
  255. int kfd_chardev_init(void);
  256. void kfd_chardev_exit(void);
  257. struct device *kfd_chardev(void);
  258. /**
  259. * enum kfd_unmap_queues_filter
  260. *
  261. * @KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE: Preempts single queue.
  262. *
  263. * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
  264. * running queues list.
  265. *
  266. * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
  267. * specific process.
  268. *
  269. */
  270. enum kfd_unmap_queues_filter {
  271. KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE,
  272. KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
  273. KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
  274. KFD_UNMAP_QUEUES_FILTER_BY_PASID
  275. };
  276. /**
  277. * enum kfd_queue_type
  278. *
  279. * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
  280. *
  281. * @KFD_QUEUE_TYPE_SDMA: Sdma user mode queue type.
  282. *
  283. * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
  284. *
  285. * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
  286. */
  287. enum kfd_queue_type {
  288. KFD_QUEUE_TYPE_COMPUTE,
  289. KFD_QUEUE_TYPE_SDMA,
  290. KFD_QUEUE_TYPE_HIQ,
  291. KFD_QUEUE_TYPE_DIQ
  292. };
  293. enum kfd_queue_format {
  294. KFD_QUEUE_FORMAT_PM4,
  295. KFD_QUEUE_FORMAT_AQL
  296. };
  297. /**
  298. * struct queue_properties
  299. *
  300. * @type: The queue type.
  301. *
  302. * @queue_id: Queue identifier.
  303. *
  304. * @queue_address: Queue ring buffer address.
  305. *
  306. * @queue_size: Queue ring buffer size.
  307. *
  308. * @priority: Defines the queue priority relative to other queues in the
  309. * process.
  310. * This is just an indication and HW scheduling may override the priority as
  311. * necessary while keeping the relative prioritization.
  312. * the priority granularity is from 0 to f which f is the highest priority.
  313. * currently all queues are initialized with the highest priority.
  314. *
  315. * @queue_percent: This field is partially implemented and currently a zero in
  316. * this field defines that the queue is non active.
  317. *
  318. * @read_ptr: User space address which points to the number of dwords the
  319. * cp read from the ring buffer. This field updates automatically by the H/W.
  320. *
  321. * @write_ptr: Defines the number of dwords written to the ring buffer.
  322. *
  323. * @doorbell_ptr: This field aim is to notify the H/W of new packet written to
  324. * the queue ring buffer. This field should be similar to write_ptr and the
  325. * user should update this field after he updated the write_ptr.
  326. *
  327. * @doorbell_off: The doorbell offset in the doorbell pci-bar.
  328. *
  329. * @is_interop: Defines if this is a interop queue. Interop queue means that
  330. * the queue can access both graphics and compute resources.
  331. *
  332. * @is_evicted: Defines if the queue is evicted. Only active queues
  333. * are evicted, rendering them inactive.
  334. *
  335. * @is_active: Defines if the queue is active or not. @is_active and
  336. * @is_evicted are protected by the DQM lock.
  337. *
  338. * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
  339. * of the queue.
  340. *
  341. * This structure represents the queue properties for each queue no matter if
  342. * it's user mode or kernel mode queue.
  343. *
  344. */
  345. struct queue_properties {
  346. enum kfd_queue_type type;
  347. enum kfd_queue_format format;
  348. unsigned int queue_id;
  349. uint64_t queue_address;
  350. uint64_t queue_size;
  351. uint32_t priority;
  352. uint32_t queue_percent;
  353. uint32_t *read_ptr;
  354. uint32_t *write_ptr;
  355. void __iomem *doorbell_ptr;
  356. uint32_t doorbell_off;
  357. bool is_interop;
  358. bool is_evicted;
  359. bool is_active;
  360. /* Not relevant for user mode queues in cp scheduling */
  361. unsigned int vmid;
  362. /* Relevant only for sdma queues*/
  363. uint32_t sdma_engine_id;
  364. uint32_t sdma_queue_id;
  365. uint32_t sdma_vm_addr;
  366. /* Relevant only for VI */
  367. uint64_t eop_ring_buffer_address;
  368. uint32_t eop_ring_buffer_size;
  369. uint64_t ctx_save_restore_area_address;
  370. uint32_t ctx_save_restore_area_size;
  371. uint32_t ctl_stack_size;
  372. uint64_t tba_addr;
  373. uint64_t tma_addr;
  374. /* Relevant for CU */
  375. uint32_t cu_mask_count; /* Must be a multiple of 32 */
  376. uint32_t *cu_mask;
  377. };
  378. /**
  379. * struct queue
  380. *
  381. * @list: Queue linked list.
  382. *
  383. * @mqd: The queue MQD.
  384. *
  385. * @mqd_mem_obj: The MQD local gpu memory object.
  386. *
  387. * @gart_mqd_addr: The MQD gart mc address.
  388. *
  389. * @properties: The queue properties.
  390. *
  391. * @mec: Used only in no cp scheduling mode and identifies to micro engine id
  392. * that the queue should be execute on.
  393. *
  394. * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
  395. * id.
  396. *
  397. * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
  398. *
  399. * @process: The kfd process that created this queue.
  400. *
  401. * @device: The kfd device that created this queue.
  402. *
  403. * This structure represents user mode compute queues.
  404. * It contains all the necessary data to handle such queues.
  405. *
  406. */
  407. struct queue {
  408. struct list_head list;
  409. void *mqd;
  410. struct kfd_mem_obj *mqd_mem_obj;
  411. uint64_t gart_mqd_addr;
  412. struct queue_properties properties;
  413. uint32_t mec;
  414. uint32_t pipe;
  415. uint32_t queue;
  416. unsigned int sdma_id;
  417. unsigned int doorbell_id;
  418. struct kfd_process *process;
  419. struct kfd_dev *device;
  420. };
  421. /*
  422. * Please read the kfd_mqd_manager.h description.
  423. */
  424. enum KFD_MQD_TYPE {
  425. KFD_MQD_TYPE_COMPUTE = 0, /* for no cp scheduling */
  426. KFD_MQD_TYPE_HIQ, /* for hiq */
  427. KFD_MQD_TYPE_CP, /* for cp queues and diq */
  428. KFD_MQD_TYPE_SDMA, /* for sdma queues */
  429. KFD_MQD_TYPE_MAX
  430. };
  431. struct scheduling_resources {
  432. unsigned int vmid_mask;
  433. enum kfd_queue_type type;
  434. uint64_t queue_mask;
  435. uint64_t gws_mask;
  436. uint32_t oac_mask;
  437. uint32_t gds_heap_base;
  438. uint32_t gds_heap_size;
  439. };
  440. struct process_queue_manager {
  441. /* data */
  442. struct kfd_process *process;
  443. struct list_head queues;
  444. unsigned long *queue_slot_bitmap;
  445. };
  446. struct qcm_process_device {
  447. /* The Device Queue Manager that owns this data */
  448. struct device_queue_manager *dqm;
  449. struct process_queue_manager *pqm;
  450. /* Queues list */
  451. struct list_head queues_list;
  452. struct list_head priv_queue_list;
  453. unsigned int queue_count;
  454. unsigned int vmid;
  455. bool is_debug;
  456. unsigned int evicted; /* eviction counter, 0=active */
  457. /* This flag tells if we should reset all wavefronts on
  458. * process termination
  459. */
  460. bool reset_wavefronts;
  461. /*
  462. * All the memory management data should be here too
  463. */
  464. uint64_t gds_context_area;
  465. uint32_t sh_mem_config;
  466. uint32_t sh_mem_bases;
  467. uint32_t sh_mem_ape1_base;
  468. uint32_t sh_mem_ape1_limit;
  469. uint32_t page_table_base;
  470. uint32_t gds_size;
  471. uint32_t num_gws;
  472. uint32_t num_oac;
  473. uint32_t sh_hidden_private_base;
  474. /* CWSR memory */
  475. void *cwsr_kaddr;
  476. uint64_t cwsr_base;
  477. uint64_t tba_addr;
  478. uint64_t tma_addr;
  479. /* IB memory */
  480. uint64_t ib_base;
  481. void *ib_kaddr;
  482. /* doorbell resources per process per device */
  483. unsigned long *doorbell_bitmap;
  484. };
  485. /* KFD Memory Eviction */
  486. /* Approx. wait time before attempting to restore evicted BOs */
  487. #define PROCESS_RESTORE_TIME_MS 100
  488. /* Approx. back off time if restore fails due to lack of memory */
  489. #define PROCESS_BACK_OFF_TIME_MS 100
  490. /* Approx. time before evicting the process again */
  491. #define PROCESS_ACTIVE_TIME_MS 10
  492. int kgd2kfd_quiesce_mm(struct mm_struct *mm);
  493. int kgd2kfd_resume_mm(struct mm_struct *mm);
  494. int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
  495. struct dma_fence *fence);
  496. /* 8 byte handle containing GPU ID in the most significant 4 bytes and
  497. * idr_handle in the least significant 4 bytes
  498. */
  499. #define MAKE_HANDLE(gpu_id, idr_handle) \
  500. (((uint64_t)(gpu_id) << 32) + idr_handle)
  501. #define GET_GPU_ID(handle) (handle >> 32)
  502. #define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF)
  503. enum kfd_pdd_bound {
  504. PDD_UNBOUND = 0,
  505. PDD_BOUND,
  506. PDD_BOUND_SUSPENDED,
  507. };
  508. /* Data that is per-process-per device. */
  509. struct kfd_process_device {
  510. /*
  511. * List of all per-device data for a process.
  512. * Starts from kfd_process.per_device_data.
  513. */
  514. struct list_head per_device_list;
  515. /* The device that owns this data. */
  516. struct kfd_dev *dev;
  517. /* The process that owns this kfd_process_device. */
  518. struct kfd_process *process;
  519. /* per-process-per device QCM data structure */
  520. struct qcm_process_device qpd;
  521. /*Apertures*/
  522. uint64_t lds_base;
  523. uint64_t lds_limit;
  524. uint64_t gpuvm_base;
  525. uint64_t gpuvm_limit;
  526. uint64_t scratch_base;
  527. uint64_t scratch_limit;
  528. /* VM context for GPUVM allocations */
  529. struct file *drm_file;
  530. void *vm;
  531. /* GPUVM allocations storage */
  532. struct idr alloc_idr;
  533. /* Flag used to tell the pdd has dequeued from the dqm.
  534. * This is used to prevent dev->dqm->ops.process_termination() from
  535. * being called twice when it is already called in IOMMU callback
  536. * function.
  537. */
  538. bool already_dequeued;
  539. /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
  540. enum kfd_pdd_bound bound;
  541. };
  542. #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
  543. /* Process data */
  544. struct kfd_process {
  545. /*
  546. * kfd_process are stored in an mm_struct*->kfd_process*
  547. * hash table (kfd_processes in kfd_process.c)
  548. */
  549. struct hlist_node kfd_processes;
  550. /*
  551. * Opaque pointer to mm_struct. We don't hold a reference to
  552. * it so it should never be dereferenced from here. This is
  553. * only used for looking up processes by their mm.
  554. */
  555. void *mm;
  556. struct kref ref;
  557. struct work_struct release_work;
  558. struct mutex mutex;
  559. /*
  560. * In any process, the thread that started main() is the lead
  561. * thread and outlives the rest.
  562. * It is here because amd_iommu_bind_pasid wants a task_struct.
  563. * It can also be used for safely getting a reference to the
  564. * mm_struct of the process.
  565. */
  566. struct task_struct *lead_thread;
  567. /* We want to receive a notification when the mm_struct is destroyed */
  568. struct mmu_notifier mmu_notifier;
  569. /* Use for delayed freeing of kfd_process structure */
  570. struct rcu_head rcu;
  571. unsigned int pasid;
  572. unsigned int doorbell_index;
  573. /*
  574. * List of kfd_process_device structures,
  575. * one for each device the process is using.
  576. */
  577. struct list_head per_device_data;
  578. struct process_queue_manager pqm;
  579. /*Is the user space process 32 bit?*/
  580. bool is_32bit_user_mode;
  581. /* Event-related data */
  582. struct mutex event_mutex;
  583. /* Event ID allocator and lookup */
  584. struct idr event_idr;
  585. /* Event page */
  586. struct kfd_signal_page *signal_page;
  587. size_t signal_mapped_size;
  588. size_t signal_event_count;
  589. bool signal_event_limit_reached;
  590. /* Information used for memory eviction */
  591. void *kgd_process_info;
  592. /* Eviction fence that is attached to all the BOs of this process. The
  593. * fence will be triggered during eviction and new one will be created
  594. * during restore
  595. */
  596. struct dma_fence *ef;
  597. /* Work items for evicting and restoring BOs */
  598. struct delayed_work eviction_work;
  599. struct delayed_work restore_work;
  600. /* seqno of the last scheduled eviction */
  601. unsigned int last_eviction_seqno;
  602. /* Approx. the last timestamp (in jiffies) when the process was
  603. * restored after an eviction
  604. */
  605. unsigned long last_restore_timestamp;
  606. };
  607. #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
  608. extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  609. extern struct srcu_struct kfd_processes_srcu;
  610. /**
  611. * Ioctl function type.
  612. *
  613. * \param filep pointer to file structure.
  614. * \param p amdkfd process pointer.
  615. * \param data pointer to arg that was copied from user.
  616. */
  617. typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
  618. void *data);
  619. struct amdkfd_ioctl_desc {
  620. unsigned int cmd;
  621. int flags;
  622. amdkfd_ioctl_t *func;
  623. unsigned int cmd_drv;
  624. const char *name;
  625. };
  626. int kfd_process_create_wq(void);
  627. void kfd_process_destroy_wq(void);
  628. struct kfd_process *kfd_create_process(struct file *filep);
  629. struct kfd_process *kfd_get_process(const struct task_struct *);
  630. struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
  631. struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
  632. void kfd_unref_process(struct kfd_process *p);
  633. int kfd_process_evict_queues(struct kfd_process *p);
  634. int kfd_process_restore_queues(struct kfd_process *p);
  635. void kfd_suspend_all_processes(void);
  636. int kfd_resume_all_processes(void);
  637. int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  638. struct file *drm_file);
  639. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
  640. struct kfd_process *p);
  641. struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
  642. struct kfd_process *p);
  643. struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
  644. struct kfd_process *p);
  645. int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
  646. struct vm_area_struct *vma);
  647. /* KFD process API for creating and translating handles */
  648. int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
  649. void *mem);
  650. void *kfd_process_device_translate_handle(struct kfd_process_device *p,
  651. int handle);
  652. void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
  653. int handle);
  654. /* Process device data iterator */
  655. struct kfd_process_device *kfd_get_first_process_device_data(
  656. struct kfd_process *p);
  657. struct kfd_process_device *kfd_get_next_process_device_data(
  658. struct kfd_process *p,
  659. struct kfd_process_device *pdd);
  660. bool kfd_has_process_device_data(struct kfd_process *p);
  661. /* PASIDs */
  662. int kfd_pasid_init(void);
  663. void kfd_pasid_exit(void);
  664. bool kfd_set_pasid_limit(unsigned int new_limit);
  665. unsigned int kfd_get_pasid_limit(void);
  666. unsigned int kfd_pasid_alloc(void);
  667. void kfd_pasid_free(unsigned int pasid);
  668. /* Doorbells */
  669. size_t kfd_doorbell_process_slice(struct kfd_dev *kfd);
  670. int kfd_doorbell_init(struct kfd_dev *kfd);
  671. void kfd_doorbell_fini(struct kfd_dev *kfd);
  672. int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process,
  673. struct vm_area_struct *vma);
  674. void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
  675. unsigned int *doorbell_off);
  676. void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
  677. u32 read_kernel_doorbell(u32 __iomem *db);
  678. void write_kernel_doorbell(void __iomem *db, u32 value);
  679. void write_kernel_doorbell64(void __iomem *db, u64 value);
  680. unsigned int kfd_doorbell_id_to_offset(struct kfd_dev *kfd,
  681. struct kfd_process *process,
  682. unsigned int doorbell_id);
  683. phys_addr_t kfd_get_process_doorbells(struct kfd_dev *dev,
  684. struct kfd_process *process);
  685. int kfd_alloc_process_doorbells(struct kfd_process *process);
  686. void kfd_free_process_doorbells(struct kfd_process *process);
  687. /* GTT Sub-Allocator */
  688. int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
  689. struct kfd_mem_obj **mem_obj);
  690. int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
  691. extern struct device *kfd_device;
  692. /* Topology */
  693. int kfd_topology_init(void);
  694. void kfd_topology_shutdown(void);
  695. int kfd_topology_add_device(struct kfd_dev *gpu);
  696. int kfd_topology_remove_device(struct kfd_dev *gpu);
  697. struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
  698. uint32_t proximity_domain);
  699. struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id);
  700. struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
  701. struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
  702. int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev);
  703. int kfd_numa_node_to_apic_id(int numa_node_id);
  704. /* Interrupts */
  705. int kfd_interrupt_init(struct kfd_dev *dev);
  706. void kfd_interrupt_exit(struct kfd_dev *dev);
  707. void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
  708. bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry);
  709. bool interrupt_is_wanted(struct kfd_dev *dev,
  710. const uint32_t *ih_ring_entry,
  711. uint32_t *patched_ihre, bool *flag);
  712. /* Power Management */
  713. void kgd2kfd_suspend(struct kfd_dev *kfd);
  714. int kgd2kfd_resume(struct kfd_dev *kfd);
  715. /* GPU reset */
  716. int kgd2kfd_pre_reset(struct kfd_dev *kfd);
  717. int kgd2kfd_post_reset(struct kfd_dev *kfd);
  718. /* amdkfd Apertures */
  719. int kfd_init_apertures(struct kfd_process *process);
  720. /* Queue Context Management */
  721. int init_queue(struct queue **q, const struct queue_properties *properties);
  722. void uninit_queue(struct queue *q);
  723. void print_queue_properties(struct queue_properties *q);
  724. void print_queue(struct queue *q);
  725. struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
  726. struct kfd_dev *dev);
  727. struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
  728. struct kfd_dev *dev);
  729. struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type,
  730. struct kfd_dev *dev);
  731. struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
  732. struct kfd_dev *dev);
  733. struct mqd_manager *mqd_manager_init_vi_tonga(enum KFD_MQD_TYPE type,
  734. struct kfd_dev *dev);
  735. struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
  736. struct kfd_dev *dev);
  737. struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
  738. void device_queue_manager_uninit(struct device_queue_manager *dqm);
  739. struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
  740. enum kfd_queue_type type);
  741. void kernel_queue_uninit(struct kernel_queue *kq);
  742. int kfd_process_vm_fault(struct device_queue_manager *dqm, unsigned int pasid);
  743. /* Process Queue Manager */
  744. struct process_queue_node {
  745. struct queue *q;
  746. struct kernel_queue *kq;
  747. struct list_head process_queue_list;
  748. };
  749. void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
  750. void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
  751. int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
  752. void pqm_uninit(struct process_queue_manager *pqm);
  753. int pqm_create_queue(struct process_queue_manager *pqm,
  754. struct kfd_dev *dev,
  755. struct file *f,
  756. struct queue_properties *properties,
  757. unsigned int *qid);
  758. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
  759. int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
  760. struct queue_properties *p);
  761. int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid,
  762. struct queue_properties *p);
  763. struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
  764. unsigned int qid);
  765. int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
  766. unsigned int fence_value,
  767. unsigned int timeout_ms);
  768. /* Packet Manager */
  769. #define KFD_FENCE_COMPLETED (100)
  770. #define KFD_FENCE_INIT (10)
  771. struct packet_manager {
  772. struct device_queue_manager *dqm;
  773. struct kernel_queue *priv_queue;
  774. struct mutex lock;
  775. bool allocated;
  776. struct kfd_mem_obj *ib_buffer_obj;
  777. unsigned int ib_size_bytes;
  778. const struct packet_manager_funcs *pmf;
  779. };
  780. struct packet_manager_funcs {
  781. /* Support ASIC-specific packet formats for PM4 packets */
  782. int (*map_process)(struct packet_manager *pm, uint32_t *buffer,
  783. struct qcm_process_device *qpd);
  784. int (*runlist)(struct packet_manager *pm, uint32_t *buffer,
  785. uint64_t ib, size_t ib_size_in_dwords, bool chain);
  786. int (*set_resources)(struct packet_manager *pm, uint32_t *buffer,
  787. struct scheduling_resources *res);
  788. int (*map_queues)(struct packet_manager *pm, uint32_t *buffer,
  789. struct queue *q, bool is_static);
  790. int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer,
  791. enum kfd_queue_type type,
  792. enum kfd_unmap_queues_filter mode,
  793. uint32_t filter_param, bool reset,
  794. unsigned int sdma_engine);
  795. int (*query_status)(struct packet_manager *pm, uint32_t *buffer,
  796. uint64_t fence_address, uint32_t fence_value);
  797. int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer);
  798. /* Packet sizes */
  799. int map_process_size;
  800. int runlist_size;
  801. int set_resources_size;
  802. int map_queues_size;
  803. int unmap_queues_size;
  804. int query_status_size;
  805. int release_mem_size;
  806. };
  807. extern const struct packet_manager_funcs kfd_vi_pm_funcs;
  808. extern const struct packet_manager_funcs kfd_v9_pm_funcs;
  809. int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
  810. void pm_uninit(struct packet_manager *pm);
  811. int pm_send_set_resources(struct packet_manager *pm,
  812. struct scheduling_resources *res);
  813. int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
  814. int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
  815. uint32_t fence_value);
  816. int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
  817. enum kfd_unmap_queues_filter mode,
  818. uint32_t filter_param, bool reset,
  819. unsigned int sdma_engine);
  820. void pm_release_ib(struct packet_manager *pm);
  821. /* Following PM funcs can be shared among VI and AI */
  822. unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size);
  823. int pm_set_resources_vi(struct packet_manager *pm, uint32_t *buffer,
  824. struct scheduling_resources *res);
  825. uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
  826. /* Events */
  827. extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
  828. extern const struct kfd_event_interrupt_class event_interrupt_class_v9;
  829. extern const struct kfd_device_global_init_class device_global_init_class_cik;
  830. void kfd_event_init_process(struct kfd_process *p);
  831. void kfd_event_free_process(struct kfd_process *p);
  832. int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
  833. int kfd_wait_on_events(struct kfd_process *p,
  834. uint32_t num_events, void __user *data,
  835. bool all, uint32_t user_timeout_ms,
  836. uint32_t *wait_result);
  837. void kfd_signal_event_interrupt(unsigned int pasid, uint32_t partial_id,
  838. uint32_t valid_id_bits);
  839. void kfd_signal_iommu_event(struct kfd_dev *dev,
  840. unsigned int pasid, unsigned long address,
  841. bool is_write_requested, bool is_execute_requested);
  842. void kfd_signal_hw_exception_event(unsigned int pasid);
  843. int kfd_set_event(struct kfd_process *p, uint32_t event_id);
  844. int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
  845. int kfd_event_page_set(struct kfd_process *p, void *kernel_address,
  846. uint64_t size);
  847. int kfd_event_create(struct file *devkfd, struct kfd_process *p,
  848. uint32_t event_type, bool auto_reset, uint32_t node_id,
  849. uint32_t *event_id, uint32_t *event_trigger_data,
  850. uint64_t *event_page_offset, uint32_t *event_slot_index);
  851. int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
  852. void kfd_signal_vm_fault_event(struct kfd_dev *dev, unsigned int pasid,
  853. struct kfd_vm_fault_info *info);
  854. void kfd_signal_reset_event(struct kfd_dev *dev);
  855. void kfd_flush_tlb(struct kfd_process_device *pdd);
  856. int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p);
  857. bool kfd_is_locked(void);
  858. /* Debugfs */
  859. #if defined(CONFIG_DEBUG_FS)
  860. void kfd_debugfs_init(void);
  861. void kfd_debugfs_fini(void);
  862. int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
  863. int pqm_debugfs_mqds(struct seq_file *m, void *data);
  864. int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
  865. int dqm_debugfs_hqds(struct seq_file *m, void *data);
  866. int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
  867. int pm_debugfs_runlist(struct seq_file *m, void *data);
  868. int kfd_debugfs_hang_hws(struct kfd_dev *dev);
  869. int pm_debugfs_hang_hws(struct packet_manager *pm);
  870. int dqm_debugfs_execute_queues(struct device_queue_manager *dqm);
  871. #else
  872. static inline void kfd_debugfs_init(void) {}
  873. static inline void kfd_debugfs_fini(void) {}
  874. #endif
  875. #endif