kfd_priv.h 30 KB

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