kfd_priv.h 30 KB

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