kfd_priv.h 27 KB

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