kfd_priv.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Copyright 2014-2022 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef KFD_PRIV_H_INCLUDED
  24. #define KFD_PRIV_H_INCLUDED
  25. #include <linux/hashtable.h>
  26. #include <linux/mmu_notifier.h>
  27. #include <linux/memremap.h>
  28. #include <linux/mutex.h>
  29. #include <linux/types.h>
  30. #include <linux/atomic.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/kfd_ioctl.h>
  34. #include <linux/idr.h>
  35. #include <linux/kfifo.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/kref.h>
  38. #include <linux/sysfs.h>
  39. #include <linux/device_cgroup.h>
  40. #include <drm/drm_file.h>
  41. #include <drm/drm_drv.h>
  42. #include <drm/drm_device.h>
  43. #include <drm/drm_ioctl.h>
  44. #include <kgd_kfd_interface.h>
  45. #include <linux/swap.h>
  46. #include "amd_shared.h"
  47. #include "amdgpu.h"
  48. #define KFD_MAX_RING_ENTRY_SIZE 8
  49. #define KFD_SYSFS_FILE_MODE 0444
  50. /* GPU ID hash width in bits */
  51. #define KFD_GPU_ID_HASH_WIDTH 16
  52. /* Use upper bits of mmap offset to store KFD driver specific information.
  53. * BITS[63:62] - Encode MMAP type
  54. * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to
  55. * BITS[45:0] - MMAP offset value
  56. *
  57. * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
  58. * defines are w.r.t to PAGE_SIZE
  59. */
  60. #define KFD_MMAP_TYPE_SHIFT 62
  61. #define KFD_MMAP_TYPE_MASK (0x3ULL << KFD_MMAP_TYPE_SHIFT)
  62. #define KFD_MMAP_TYPE_DOORBELL (0x3ULL << KFD_MMAP_TYPE_SHIFT)
  63. #define KFD_MMAP_TYPE_EVENTS (0x2ULL << KFD_MMAP_TYPE_SHIFT)
  64. #define KFD_MMAP_TYPE_RESERVED_MEM (0x1ULL << KFD_MMAP_TYPE_SHIFT)
  65. #define KFD_MMAP_TYPE_MMIO (0x0ULL << KFD_MMAP_TYPE_SHIFT)
  66. #define KFD_MMAP_GPU_ID_SHIFT 46
  67. #define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \
  68. << KFD_MMAP_GPU_ID_SHIFT)
  69. #define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\
  70. & KFD_MMAP_GPU_ID_MASK)
  71. #define KFD_MMAP_GET_GPU_ID(offset) ((offset & KFD_MMAP_GPU_ID_MASK) \
  72. >> KFD_MMAP_GPU_ID_SHIFT)
  73. /*
  74. * When working with cp scheduler we should assign the HIQ manually or via
  75. * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot
  76. * definitions for Kaveri. In Kaveri only the first ME queues participates
  77. * in the cp scheduling taking that in mind we set the HIQ slot in the
  78. * second ME.
  79. */
  80. #define KFD_CIK_HIQ_PIPE 4
  81. #define KFD_CIK_HIQ_QUEUE 0
  82. /* Macro for allocating structures */
  83. #define kfd_alloc_struct(ptr_to_struct) \
  84. ((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
  85. #define KFD_MAX_NUM_OF_PROCESSES 512
  86. #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
  87. /*
  88. * Size of the per-process TBA+TMA buffer: 2 pages
  89. *
  90. * The first chunk is the TBA used for the CWSR ISA code. The second
  91. * chunk is used as TMA for user-mode trap handler setup in daisy-chain mode.
  92. */
  93. #define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
  94. #define KFD_CWSR_TMA_OFFSET (PAGE_SIZE + 2048)
  95. #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE \
  96. (KFD_MAX_NUM_OF_PROCESSES * \
  97. KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
  98. #define KFD_KERNEL_QUEUE_SIZE 2048
  99. #define KFD_UNMAP_LATENCY_MS (4000)
  100. #define KFD_MAX_SDMA_QUEUES 128
  101. /*
  102. * 512 = 0x200
  103. * The doorbell index distance between SDMA RLC (2*i) and (2*i+1) in the
  104. * same SDMA engine on SOC15, which has 8-byte doorbells for SDMA.
  105. * 512 8-byte doorbell distance (i.e. one page away) ensures that SDMA RLC
  106. * (2*i+1) doorbells (in terms of the lower 12 bit address) lie exactly in
  107. * the OFFSET and SIZE set in registers like BIF_SDMA0_DOORBELL_RANGE.
  108. */
  109. #define KFD_QUEUE_DOORBELL_MIRROR_OFFSET 512
  110. /**
  111. * enum kfd_ioctl_flags - KFD ioctl flags
  112. * Various flags that can be set in &amdkfd_ioctl_desc.flags to control how
  113. * userspace can use a given ioctl.
  114. */
  115. enum kfd_ioctl_flags {
  116. /*
  117. * @KFD_IOC_FLAG_CHECKPOINT_RESTORE:
  118. * Certain KFD ioctls such as AMDKFD_IOC_CRIU_OP can potentially
  119. * perform privileged operations and load arbitrary data into MQDs and
  120. * eventually HQD registers when the queue is mapped by HWS. In order to
  121. * prevent this we should perform additional security checks.
  122. *
  123. * This is equivalent to callers with the CHECKPOINT_RESTORE capability.
  124. *
  125. * Note: Since earlier versions of docker do not support CHECKPOINT_RESTORE,
  126. * we also allow ioctls with SYS_ADMIN capability.
  127. */
  128. KFD_IOC_FLAG_CHECKPOINT_RESTORE = BIT(0),
  129. };
  130. /*
  131. * Kernel module parameter to specify maximum number of supported queues per
  132. * device
  133. */
  134. extern int max_num_of_queues_per_device;
  135. /* Kernel module parameter to specify the scheduling policy */
  136. extern int sched_policy;
  137. /*
  138. * Kernel module parameter to specify the maximum process
  139. * number per HW scheduler
  140. */
  141. extern int hws_max_conc_proc;
  142. extern int cwsr_enable;
  143. /*
  144. * Kernel module parameter to specify whether to send sigterm to HSA process on
  145. * unhandled exception
  146. */
  147. extern int send_sigterm;
  148. /*
  149. * This kernel module is used to simulate large bar machine on non-large bar
  150. * enabled machines.
  151. */
  152. extern int debug_largebar;
  153. /* Set sh_mem_config.retry_disable on GFX v9 */
  154. extern int amdgpu_noretry;
  155. /* Halt if HWS hang is detected */
  156. extern int halt_if_hws_hang;
  157. /* Whether MEC FW support GWS barriers */
  158. extern bool hws_gws_support;
  159. /* Queue preemption timeout in ms */
  160. extern int queue_preemption_timeout_ms;
  161. /*
  162. * Don't evict process queues on vm fault
  163. */
  164. extern int amdgpu_no_queue_eviction_on_vm_fault;
  165. /* Enable eviction debug messages */
  166. extern bool debug_evictions;
  167. extern struct mutex kfd_processes_mutex;
  168. enum cache_policy {
  169. cache_policy_coherent,
  170. cache_policy_noncoherent
  171. };
  172. #define KFD_GC_VERSION(dev) (amdgpu_ip_version((dev)->adev, GC_HWIP, 0))
  173. #define KFD_IS_SOC15(dev) ((KFD_GC_VERSION(dev)) >= (IP_VERSION(9, 0, 1)))
  174. #define KFD_SUPPORT_XNACK_PER_PROCESS(dev)\
  175. ((KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 2)) || \
  176. (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3)) || \
  177. (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 4)))
  178. struct kfd_node;
  179. struct kfd_event_interrupt_class {
  180. bool (*interrupt_isr)(struct kfd_node *dev,
  181. const uint32_t *ih_ring_entry, uint32_t *patched_ihre,
  182. bool *patched_flag);
  183. void (*interrupt_wq)(struct kfd_node *dev,
  184. const uint32_t *ih_ring_entry);
  185. };
  186. struct kfd_device_info {
  187. uint32_t gfx_target_version;
  188. const struct kfd_event_interrupt_class *event_interrupt_class;
  189. unsigned int max_pasid_bits;
  190. unsigned int max_no_of_hqd;
  191. unsigned int doorbell_size;
  192. size_t ih_ring_entry_size;
  193. uint8_t num_of_watch_points;
  194. uint16_t mqd_size_aligned;
  195. bool supports_cwsr;
  196. bool needs_pci_atomics;
  197. uint32_t no_atomic_fw_version;
  198. unsigned int num_sdma_queues_per_engine;
  199. unsigned int num_reserved_sdma_queues_per_engine;
  200. DECLARE_BITMAP(reserved_sdma_queues_bitmap, KFD_MAX_SDMA_QUEUES);
  201. };
  202. unsigned int kfd_get_num_sdma_engines(struct kfd_node *kdev);
  203. unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_node *kdev);
  204. struct kfd_mem_obj {
  205. uint32_t range_start;
  206. uint32_t range_end;
  207. uint64_t gpu_addr;
  208. uint32_t *cpu_ptr;
  209. void *gtt_mem;
  210. };
  211. struct kfd_vmid_info {
  212. uint32_t first_vmid_kfd;
  213. uint32_t last_vmid_kfd;
  214. uint32_t vmid_num_kfd;
  215. };
  216. #define MAX_KFD_NODES 8
  217. struct kfd_dev;
  218. struct kfd_node {
  219. unsigned int node_id;
  220. struct amdgpu_device *adev; /* Duplicated here along with keeping
  221. * a copy in kfd_dev to save a hop
  222. */
  223. const struct kfd2kgd_calls *kfd2kgd; /* Duplicated here along with
  224. * keeping a copy in kfd_dev to
  225. * save a hop
  226. */
  227. struct kfd_vmid_info vm_info;
  228. unsigned int id; /* topology stub index */
  229. uint32_t xcc_mask; /* Instance mask of XCCs present */
  230. struct amdgpu_xcp *xcp;
  231. /* Interrupts */
  232. struct kfifo ih_fifo;
  233. struct work_struct interrupt_work;
  234. spinlock_t interrupt_lock;
  235. /*
  236. * Interrupts of interest to KFD are copied
  237. * from the HW ring into a SW ring.
  238. */
  239. bool interrupts_active;
  240. uint32_t interrupt_bitmap; /* Only used for GFX 9.4.3 */
  241. /* QCM Device instance */
  242. struct device_queue_manager *dqm;
  243. /* Global GWS resource shared between processes */
  244. void *gws;
  245. bool gws_debug_workaround;
  246. /* Clients watching SMI events */
  247. struct list_head smi_clients;
  248. spinlock_t smi_lock;
  249. uint32_t reset_seq_num;
  250. /* SRAM ECC flag */
  251. atomic_t sram_ecc_flag;
  252. /*spm process id */
  253. unsigned int spm_pasid;
  254. /* Maximum process number mapped to HW scheduler */
  255. unsigned int max_proc_per_quantum;
  256. unsigned int compute_vmid_bitmap;
  257. struct kfd_local_mem_info local_mem_info;
  258. struct kfd_dev *kfd;
  259. /* Track per device allocated watch points */
  260. uint32_t alloc_watch_ids;
  261. spinlock_t watch_points_lock;
  262. };
  263. struct kfd_dev {
  264. struct amdgpu_device *adev;
  265. struct kfd_device_info device_info;
  266. u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
  267. * page used by kernel queue
  268. */
  269. struct kgd2kfd_shared_resources shared_resources;
  270. const struct kfd2kgd_calls *kfd2kgd;
  271. struct mutex doorbell_mutex;
  272. void *gtt_mem;
  273. uint64_t gtt_start_gpu_addr;
  274. void *gtt_start_cpu_ptr;
  275. void *gtt_sa_bitmap;
  276. struct mutex gtt_sa_lock;
  277. unsigned int gtt_sa_chunk_size;
  278. unsigned int gtt_sa_num_of_chunks;
  279. bool init_complete;
  280. /* Firmware versions */
  281. uint16_t mec_fw_version;
  282. uint16_t mec2_fw_version;
  283. uint16_t sdma_fw_version;
  284. /* CWSR */
  285. bool cwsr_enabled;
  286. const void *cwsr_isa;
  287. unsigned int cwsr_isa_size;
  288. /* xGMI */
  289. uint64_t hive_id;
  290. bool pci_atomic_requested;
  291. /* Compute Profile ref. count */
  292. atomic_t compute_profile;
  293. struct ida doorbell_ida;
  294. unsigned int max_doorbell_slices;
  295. int noretry;
  296. struct kfd_node *nodes[MAX_KFD_NODES];
  297. unsigned int num_nodes;
  298. struct workqueue_struct *ih_wq;
  299. /* Kernel doorbells for KFD device */
  300. struct amdgpu_bo *doorbells;
  301. /* bitmap for dynamic doorbell allocation from doorbell object */
  302. unsigned long *doorbell_bitmap;
  303. };
  304. enum kfd_mempool {
  305. KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
  306. KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
  307. KFD_MEMPOOL_FRAMEBUFFER = 3,
  308. };
  309. /* Character device interface */
  310. int kfd_chardev_init(void);
  311. void kfd_chardev_exit(void);
  312. /**
  313. * enum kfd_unmap_queues_filter - Enum for queue filters.
  314. *
  315. * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
  316. * running queues list.
  317. *
  318. * @KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES: Preempts all non-static queues
  319. * in the run list.
  320. *
  321. * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
  322. * specific process.
  323. *
  324. */
  325. enum kfd_unmap_queues_filter {
  326. KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES = 1,
  327. KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES = 2,
  328. KFD_UNMAP_QUEUES_FILTER_BY_PASID = 3
  329. };
  330. /**
  331. * enum kfd_queue_type - Enum for various queue types.
  332. *
  333. * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
  334. *
  335. * @KFD_QUEUE_TYPE_SDMA: SDMA user mode queue type.
  336. *
  337. * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
  338. *
  339. * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
  340. *
  341. * @KFD_QUEUE_TYPE_SDMA_XGMI: Special SDMA queue for XGMI interface.
  342. *
  343. * @KFD_QUEUE_TYPE_SDMA_BY_ENG_ID: SDMA user mode queue with target SDMA engine ID.
  344. */
  345. enum kfd_queue_type {
  346. KFD_QUEUE_TYPE_COMPUTE,
  347. KFD_QUEUE_TYPE_SDMA,
  348. KFD_QUEUE_TYPE_HIQ,
  349. KFD_QUEUE_TYPE_DIQ,
  350. KFD_QUEUE_TYPE_SDMA_XGMI,
  351. KFD_QUEUE_TYPE_SDMA_BY_ENG_ID
  352. };
  353. enum kfd_queue_format {
  354. KFD_QUEUE_FORMAT_PM4,
  355. KFD_QUEUE_FORMAT_AQL
  356. };
  357. enum KFD_QUEUE_PRIORITY {
  358. KFD_QUEUE_PRIORITY_MINIMUM = 0,
  359. KFD_QUEUE_PRIORITY_MAXIMUM = 15
  360. };
  361. /**
  362. * struct queue_properties
  363. *
  364. * @type: The queue type.
  365. *
  366. * @queue_id: Queue identifier.
  367. *
  368. * @queue_address: Queue ring buffer address.
  369. *
  370. * @queue_size: Queue ring buffer size.
  371. *
  372. * @priority: Defines the queue priority relative to other queues in the
  373. * process.
  374. * This is just an indication and HW scheduling may override the priority as
  375. * necessary while keeping the relative prioritization.
  376. * the priority granularity is from 0 to f which f is the highest priority.
  377. * currently all queues are initialized with the highest priority.
  378. *
  379. * @queue_percent: This field is partially implemented and currently a zero in
  380. * this field defines that the queue is non active.
  381. *
  382. * @read_ptr: User space address which points to the number of dwords the
  383. * cp read from the ring buffer. This field updates automatically by the H/W.
  384. *
  385. * @write_ptr: Defines the number of dwords written to the ring buffer.
  386. *
  387. * @doorbell_ptr: Notifies the H/W of new packet written to the queue ring
  388. * buffer. This field should be similar to write_ptr and the user should
  389. * update this field after updating the write_ptr.
  390. *
  391. * @doorbell_off: The doorbell offset in the doorbell pci-bar.
  392. *
  393. * @is_interop: Defines if this is a interop queue. Interop queue means that
  394. * the queue can access both graphics and compute resources.
  395. *
  396. * @is_evicted: Defines if the queue is evicted. Only active queues
  397. * are evicted, rendering them inactive.
  398. *
  399. * @is_active: Defines if the queue is active or not. @is_active and
  400. * @is_evicted are protected by the DQM lock.
  401. *
  402. * @is_gws: Defines if the queue has been updated to be GWS-capable or not.
  403. * @is_gws should be protected by the DQM lock, since changing it can yield the
  404. * possibility of updating DQM state on number of GWS queues.
  405. *
  406. * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
  407. * of the queue.
  408. *
  409. * This structure represents the queue properties for each queue no matter if
  410. * it's user mode or kernel mode queue.
  411. *
  412. */
  413. struct queue_properties {
  414. enum kfd_queue_type type;
  415. enum kfd_queue_format format;
  416. unsigned int queue_id;
  417. uint64_t queue_address;
  418. uint64_t queue_size;
  419. uint32_t priority;
  420. uint32_t queue_percent;
  421. void __user *read_ptr;
  422. void __user *write_ptr;
  423. void __iomem *doorbell_ptr;
  424. uint32_t doorbell_off;
  425. bool is_interop;
  426. bool is_evicted;
  427. bool is_suspended;
  428. bool is_being_destroyed;
  429. bool is_active;
  430. bool is_gws;
  431. uint32_t pm4_target_xcc;
  432. bool is_dbg_wa;
  433. bool is_user_cu_masked;
  434. /* Not relevant for user mode queues in cp scheduling */
  435. unsigned int vmid;
  436. /* Relevant only for sdma queues*/
  437. uint32_t sdma_engine_id;
  438. uint32_t sdma_queue_id;
  439. uint32_t sdma_vm_addr;
  440. /* Relevant only for VI */
  441. uint64_t eop_ring_buffer_address;
  442. uint32_t eop_ring_buffer_size;
  443. uint64_t ctx_save_restore_area_address;
  444. uint32_t ctx_save_restore_area_size;
  445. uint32_t ctl_stack_size;
  446. uint64_t tba_addr;
  447. uint64_t tma_addr;
  448. uint64_t exception_status;
  449. struct amdgpu_bo *wptr_bo;
  450. struct amdgpu_bo *rptr_bo;
  451. struct amdgpu_bo *ring_bo;
  452. struct amdgpu_bo *eop_buf_bo;
  453. struct amdgpu_bo *cwsr_bo;
  454. };
  455. #define QUEUE_IS_ACTIVE(q) ((q).queue_size > 0 && \
  456. (q).queue_address != 0 && \
  457. (q).queue_percent > 0 && \
  458. !(q).is_evicted && \
  459. !(q).is_suspended)
  460. enum mqd_update_flag {
  461. UPDATE_FLAG_DBG_WA_ENABLE = 1,
  462. UPDATE_FLAG_DBG_WA_DISABLE = 2,
  463. UPDATE_FLAG_IS_GWS = 4, /* quirk for gfx9 IP */
  464. };
  465. struct mqd_update_info {
  466. union {
  467. struct {
  468. uint32_t count; /* Must be a multiple of 32 */
  469. uint32_t *ptr;
  470. } cu_mask;
  471. };
  472. enum mqd_update_flag update_flag;
  473. };
  474. /**
  475. * struct queue
  476. *
  477. * @list: Queue linked list.
  478. *
  479. * @mqd: The queue MQD (memory queue descriptor).
  480. *
  481. * @mqd_mem_obj: The MQD local gpu memory object.
  482. *
  483. * @gart_mqd_addr: The MQD gart mc address.
  484. *
  485. * @properties: The queue properties.
  486. *
  487. * @mec: Used only in no cp scheduling mode and identifies to micro engine id
  488. * that the queue should be executed on.
  489. *
  490. * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
  491. * id.
  492. *
  493. * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
  494. *
  495. * @process: The kfd process that created this queue.
  496. *
  497. * @device: The kfd device that created this queue.
  498. *
  499. * @gws: Pointing to gws kgd_mem if this is a gws control queue; NULL
  500. * otherwise.
  501. *
  502. * This structure represents user mode compute queues.
  503. * It contains all the necessary data to handle such queues.
  504. *
  505. */
  506. struct queue {
  507. struct list_head list;
  508. void *mqd;
  509. struct kfd_mem_obj *mqd_mem_obj;
  510. uint64_t gart_mqd_addr;
  511. struct queue_properties properties;
  512. uint32_t mec;
  513. uint32_t pipe;
  514. uint32_t queue;
  515. unsigned int sdma_id;
  516. unsigned int doorbell_id;
  517. struct kfd_process *process;
  518. struct kfd_node *device;
  519. void *gws;
  520. /* procfs */
  521. struct kobject kobj;
  522. void *gang_ctx_bo;
  523. uint64_t gang_ctx_gpu_addr;
  524. void *gang_ctx_cpu_ptr;
  525. struct amdgpu_bo *wptr_bo_gart;
  526. };
  527. enum KFD_MQD_TYPE {
  528. KFD_MQD_TYPE_HIQ = 0, /* for hiq */
  529. KFD_MQD_TYPE_CP, /* for cp queues and diq */
  530. KFD_MQD_TYPE_SDMA, /* for sdma queues */
  531. KFD_MQD_TYPE_DIQ, /* for diq */
  532. KFD_MQD_TYPE_MAX
  533. };
  534. enum KFD_PIPE_PRIORITY {
  535. KFD_PIPE_PRIORITY_CS_LOW = 0,
  536. KFD_PIPE_PRIORITY_CS_MEDIUM,
  537. KFD_PIPE_PRIORITY_CS_HIGH
  538. };
  539. struct scheduling_resources {
  540. unsigned int vmid_mask;
  541. enum kfd_queue_type type;
  542. uint64_t queue_mask;
  543. uint64_t gws_mask;
  544. uint32_t oac_mask;
  545. uint32_t gds_heap_base;
  546. uint32_t gds_heap_size;
  547. };
  548. struct process_queue_manager {
  549. /* data */
  550. struct kfd_process *process;
  551. struct list_head queues;
  552. unsigned long *queue_slot_bitmap;
  553. };
  554. struct qcm_process_device {
  555. /* The Device Queue Manager that owns this data */
  556. struct device_queue_manager *dqm;
  557. struct process_queue_manager *pqm;
  558. /* Queues list */
  559. struct list_head queues_list;
  560. struct list_head priv_queue_list;
  561. unsigned int queue_count;
  562. unsigned int vmid;
  563. bool is_debug;
  564. unsigned int evicted; /* eviction counter, 0=active */
  565. /* This flag tells if we should reset all wavefronts on
  566. * process termination
  567. */
  568. bool reset_wavefronts;
  569. /* This flag tells us if this process has a GWS-capable
  570. * queue that will be mapped into the runlist. It's
  571. * possible to request a GWS BO, but not have the queue
  572. * currently mapped, and this changes how the MAP_PROCESS
  573. * PM4 packet is configured.
  574. */
  575. bool mapped_gws_queue;
  576. /* All the memory management data should be here too */
  577. uint64_t gds_context_area;
  578. /* Contains page table flags such as AMDGPU_PTE_VALID since gfx9 */
  579. uint64_t page_table_base;
  580. uint32_t sh_mem_config;
  581. uint32_t sh_mem_bases;
  582. uint32_t sh_mem_ape1_base;
  583. uint32_t sh_mem_ape1_limit;
  584. uint32_t gds_size;
  585. uint32_t num_gws;
  586. uint32_t num_oac;
  587. uint32_t sh_hidden_private_base;
  588. /* CWSR memory */
  589. struct kgd_mem *cwsr_mem;
  590. void *cwsr_kaddr;
  591. uint64_t cwsr_base;
  592. uint64_t tba_addr;
  593. uint64_t tma_addr;
  594. /* IB memory */
  595. struct kgd_mem *ib_mem;
  596. uint64_t ib_base;
  597. void *ib_kaddr;
  598. /* doorbells for kfd process */
  599. struct amdgpu_bo *proc_doorbells;
  600. /* bitmap for dynamic doorbell allocation from the bo */
  601. unsigned long *doorbell_bitmap;
  602. };
  603. /* KFD Memory Eviction */
  604. /* Approx. wait time before attempting to restore evicted BOs */
  605. #define PROCESS_RESTORE_TIME_MS 100
  606. /* Approx. back off time if restore fails due to lack of memory */
  607. #define PROCESS_BACK_OFF_TIME_MS 100
  608. /* Approx. time before evicting the process again */
  609. #define PROCESS_ACTIVE_TIME_MS 10
  610. /* 8 byte handle containing GPU ID in the most significant 4 bytes and
  611. * idr_handle in the least significant 4 bytes
  612. */
  613. #define MAKE_HANDLE(gpu_id, idr_handle) \
  614. (((uint64_t)(gpu_id) << 32) + idr_handle)
  615. #define GET_GPU_ID(handle) (handle >> 32)
  616. #define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF)
  617. enum kfd_pdd_bound {
  618. PDD_UNBOUND = 0,
  619. PDD_BOUND,
  620. PDD_BOUND_SUSPENDED,
  621. };
  622. #define MAX_SYSFS_FILENAME_LEN 15
  623. /*
  624. * SDMA counter runs at 100MHz frequency.
  625. * We display SDMA activity in microsecond granularity in sysfs.
  626. * As a result, the divisor is 100.
  627. */
  628. #define SDMA_ACTIVITY_DIVISOR 100
  629. /* Data that is per-process-per device. */
  630. struct kfd_process_device {
  631. /* The device that owns this data. */
  632. struct kfd_node *dev;
  633. /* The process that owns this kfd_process_device. */
  634. struct kfd_process *process;
  635. /* per-process-per device QCM data structure */
  636. struct qcm_process_device qpd;
  637. /*Apertures*/
  638. uint64_t lds_base;
  639. uint64_t lds_limit;
  640. uint64_t gpuvm_base;
  641. uint64_t gpuvm_limit;
  642. uint64_t scratch_base;
  643. uint64_t scratch_limit;
  644. /* VM context for GPUVM allocations */
  645. struct file *drm_file;
  646. void *drm_priv;
  647. /* GPUVM allocations storage */
  648. struct idr alloc_idr;
  649. /* Flag used to tell the pdd has dequeued from the dqm.
  650. * This is used to prevent dev->dqm->ops.process_termination() from
  651. * being called twice when it is already called in IOMMU callback
  652. * function.
  653. */
  654. bool already_dequeued;
  655. bool runtime_inuse;
  656. /* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
  657. enum kfd_pdd_bound bound;
  658. /* VRAM usage */
  659. atomic64_t vram_usage;
  660. struct attribute attr_vram;
  661. char vram_filename[MAX_SYSFS_FILENAME_LEN];
  662. /* SDMA activity tracking */
  663. uint64_t sdma_past_activity_counter;
  664. struct attribute attr_sdma;
  665. char sdma_filename[MAX_SYSFS_FILENAME_LEN];
  666. /* Eviction activity tracking */
  667. uint64_t last_evict_timestamp;
  668. atomic64_t evict_duration_counter;
  669. struct attribute attr_evict;
  670. struct kobject *kobj_stats;
  671. /*
  672. * @cu_occupancy: Reports occupancy of Compute Units (CU) of a process
  673. * that is associated with device encoded by "this" struct instance. The
  674. * value reflects CU usage by all of the waves launched by this process
  675. * on this device. A very important property of occupancy parameter is
  676. * that its value is a snapshot of current use.
  677. *
  678. * Following is to be noted regarding how this parameter is reported:
  679. *
  680. * The number of waves that a CU can launch is limited by couple of
  681. * parameters. These are encoded by struct amdgpu_cu_info instance
  682. * that is part of every device definition. For GFX9 devices this
  683. * translates to 40 waves (simd_per_cu * max_waves_per_simd) when waves
  684. * do not use scratch memory and 32 waves (max_scratch_slots_per_cu)
  685. * when they do use scratch memory. This could change for future
  686. * devices and therefore this example should be considered as a guide.
  687. *
  688. * All CU's of a device are available for the process. This may not be true
  689. * under certain conditions - e.g. CU masking.
  690. *
  691. * Finally number of CU's that are occupied by a process is affected by both
  692. * number of CU's a device has along with number of other competing processes
  693. */
  694. struct attribute attr_cu_occupancy;
  695. /* sysfs counters for GPU retry fault and page migration tracking */
  696. struct kobject *kobj_counters;
  697. struct attribute attr_faults;
  698. struct attribute attr_page_in;
  699. struct attribute attr_page_out;
  700. uint64_t faults;
  701. uint64_t page_in;
  702. uint64_t page_out;
  703. /* Exception code status*/
  704. uint64_t exception_status;
  705. void *vm_fault_exc_data;
  706. size_t vm_fault_exc_data_size;
  707. /* Tracks debug per-vmid request settings */
  708. uint32_t spi_dbg_override;
  709. uint32_t spi_dbg_launch_mode;
  710. uint32_t watch_points[4];
  711. uint32_t alloc_watch_ids;
  712. /*
  713. * If this process has been checkpointed before, then the user
  714. * application will use the original gpu_id on the
  715. * checkpointed node to refer to this device.
  716. */
  717. uint32_t user_gpu_id;
  718. void *proc_ctx_bo;
  719. uint64_t proc_ctx_gpu_addr;
  720. void *proc_ctx_cpu_ptr;
  721. /* Tracks queue reset status */
  722. bool has_reset_queue;
  723. };
  724. #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
  725. struct svm_range_list {
  726. struct mutex lock;
  727. struct rb_root_cached objects;
  728. struct list_head list;
  729. struct work_struct deferred_list_work;
  730. struct list_head deferred_range_list;
  731. struct list_head criu_svm_metadata_list;
  732. spinlock_t deferred_list_lock;
  733. atomic_t evicted_ranges;
  734. atomic_t drain_pagefaults;
  735. struct delayed_work restore_work;
  736. DECLARE_BITMAP(bitmap_supported, MAX_GPU_INSTANCE);
  737. struct task_struct *faulting_task;
  738. /* check point ts decides if page fault recovery need be dropped */
  739. uint64_t checkpoint_ts[MAX_GPU_INSTANCE];
  740. /* Default granularity to use in buffer migration
  741. * and restoration of backing memory while handling
  742. * recoverable page faults
  743. */
  744. uint8_t default_granularity;
  745. };
  746. /* Process data */
  747. struct kfd_process {
  748. /*
  749. * kfd_process are stored in an mm_struct*->kfd_process*
  750. * hash table (kfd_processes in kfd_process.c)
  751. */
  752. struct hlist_node kfd_processes;
  753. /*
  754. * Opaque pointer to mm_struct. We don't hold a reference to
  755. * it so it should never be dereferenced from here. This is
  756. * only used for looking up processes by their mm.
  757. */
  758. void *mm;
  759. struct kref ref;
  760. struct work_struct release_work;
  761. struct mutex mutex;
  762. /*
  763. * In any process, the thread that started main() is the lead
  764. * thread and outlives the rest.
  765. * It is here because amd_iommu_bind_pasid wants a task_struct.
  766. * It can also be used for safely getting a reference to the
  767. * mm_struct of the process.
  768. */
  769. struct task_struct *lead_thread;
  770. /* We want to receive a notification when the mm_struct is destroyed */
  771. struct mmu_notifier mmu_notifier;
  772. u32 pasid;
  773. /*
  774. * Array of kfd_process_device pointers,
  775. * one for each device the process is using.
  776. */
  777. struct kfd_process_device *pdds[MAX_GPU_INSTANCE];
  778. uint32_t n_pdds;
  779. struct process_queue_manager pqm;
  780. /*Is the user space process 32 bit?*/
  781. bool is_32bit_user_mode;
  782. /* Event-related data */
  783. struct mutex event_mutex;
  784. /* Event ID allocator and lookup */
  785. struct idr event_idr;
  786. /* Event page */
  787. u64 signal_handle;
  788. struct kfd_signal_page *signal_page;
  789. size_t signal_mapped_size;
  790. size_t signal_event_count;
  791. bool signal_event_limit_reached;
  792. /* Information used for memory eviction */
  793. void *kgd_process_info;
  794. /* Eviction fence that is attached to all the BOs of this process. The
  795. * fence will be triggered during eviction and new one will be created
  796. * during restore
  797. */
  798. struct dma_fence __rcu *ef;
  799. /* Work items for evicting and restoring BOs */
  800. struct delayed_work eviction_work;
  801. struct delayed_work restore_work;
  802. /* seqno of the last scheduled eviction */
  803. unsigned int last_eviction_seqno;
  804. /* Approx. the last timestamp (in jiffies) when the process was
  805. * restored after an eviction
  806. */
  807. unsigned long last_restore_timestamp;
  808. /* Indicates device process is debug attached with reserved vmid. */
  809. bool debug_trap_enabled;
  810. /* per-process-per device debug event fd file */
  811. struct file *dbg_ev_file;
  812. /* If the process is a kfd debugger, we need to know so we can clean
  813. * up at exit time. If a process enables debugging on itself, it does
  814. * its own clean-up, so we don't set the flag here. We track this by
  815. * counting the number of processes this process is debugging.
  816. */
  817. atomic_t debugged_process_count;
  818. /* If the process is a debugged, this is the debugger process */
  819. struct kfd_process *debugger_process;
  820. /* Kobj for our procfs */
  821. struct kobject *kobj;
  822. struct kobject *kobj_queues;
  823. struct attribute attr_pasid;
  824. /* Keep track cwsr init */
  825. bool has_cwsr;
  826. /* Exception code enable mask and status */
  827. uint64_t exception_enable_mask;
  828. uint64_t exception_status;
  829. /* Used to drain stale interrupts */
  830. wait_queue_head_t wait_irq_drain;
  831. bool irq_drain_is_open;
  832. /* shared virtual memory registered by this process */
  833. struct svm_range_list svms;
  834. bool xnack_enabled;
  835. /* Work area for debugger event writer worker. */
  836. struct work_struct debug_event_workarea;
  837. /* Tracks debug per-vmid request for debug flags */
  838. u32 dbg_flags;
  839. atomic_t poison;
  840. /* Queues are in paused stated because we are in the process of doing a CRIU checkpoint */
  841. bool queues_paused;
  842. /* Tracks runtime enable status */
  843. struct semaphore runtime_enable_sema;
  844. bool is_runtime_retry;
  845. struct kfd_runtime_info runtime_info;
  846. };
  847. #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
  848. extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  849. extern struct srcu_struct kfd_processes_srcu;
  850. /**
  851. * typedef amdkfd_ioctl_t - typedef for ioctl function pointer.
  852. *
  853. * @filep: pointer to file structure.
  854. * @p: amdkfd process pointer.
  855. * @data: pointer to arg that was copied from user.
  856. *
  857. * Return: returns ioctl completion code.
  858. */
  859. typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
  860. void *data);
  861. struct amdkfd_ioctl_desc {
  862. unsigned int cmd;
  863. int flags;
  864. amdkfd_ioctl_t *func;
  865. unsigned int cmd_drv;
  866. const char *name;
  867. };
  868. bool kfd_dev_is_large_bar(struct kfd_node *dev);
  869. int kfd_process_create_wq(void);
  870. void kfd_process_destroy_wq(void);
  871. void kfd_cleanup_processes(void);
  872. struct kfd_process *kfd_create_process(struct task_struct *thread);
  873. struct kfd_process *kfd_get_process(const struct task_struct *task);
  874. struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid);
  875. struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
  876. int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id);
  877. int kfd_process_gpuid_from_node(struct kfd_process *p, struct kfd_node *node,
  878. uint32_t *gpuid, uint32_t *gpuidx);
  879. static inline int kfd_process_gpuid_from_gpuidx(struct kfd_process *p,
  880. uint32_t gpuidx, uint32_t *gpuid) {
  881. return gpuidx < p->n_pdds ? p->pdds[gpuidx]->dev->id : -EINVAL;
  882. }
  883. static inline struct kfd_process_device *kfd_process_device_from_gpuidx(
  884. struct kfd_process *p, uint32_t gpuidx) {
  885. return gpuidx < p->n_pdds ? p->pdds[gpuidx] : NULL;
  886. }
  887. void kfd_unref_process(struct kfd_process *p);
  888. int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger);
  889. int kfd_process_restore_queues(struct kfd_process *p);
  890. void kfd_suspend_all_processes(void);
  891. int kfd_resume_all_processes(void);
  892. struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *process,
  893. uint32_t gpu_id);
  894. int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id);
  895. int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  896. struct file *drm_file);
  897. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
  898. struct kfd_process *p);
  899. struct kfd_process_device *kfd_get_process_device_data(struct kfd_node *dev,
  900. struct kfd_process *p);
  901. struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
  902. struct kfd_process *p);
  903. bool kfd_process_xnack_mode(struct kfd_process *p, bool supported);
  904. int kfd_reserved_mem_mmap(struct kfd_node *dev, struct kfd_process *process,
  905. struct vm_area_struct *vma);
  906. /* KFD process API for creating and translating handles */
  907. int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
  908. void *mem);
  909. void *kfd_process_device_translate_handle(struct kfd_process_device *p,
  910. int handle);
  911. void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
  912. int handle);
  913. struct kfd_process *kfd_lookup_process_by_pid(struct pid *pid);
  914. /* PASIDs */
  915. int kfd_pasid_init(void);
  916. void kfd_pasid_exit(void);
  917. bool kfd_set_pasid_limit(unsigned int new_limit);
  918. unsigned int kfd_get_pasid_limit(void);
  919. u32 kfd_pasid_alloc(void);
  920. void kfd_pasid_free(u32 pasid);
  921. /* Doorbells */
  922. size_t kfd_doorbell_process_slice(struct kfd_dev *kfd);
  923. int kfd_doorbell_init(struct kfd_dev *kfd);
  924. void kfd_doorbell_fini(struct kfd_dev *kfd);
  925. int kfd_doorbell_mmap(struct kfd_node *dev, struct kfd_process *process,
  926. struct vm_area_struct *vma);
  927. void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
  928. unsigned int *doorbell_off);
  929. void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
  930. u32 read_kernel_doorbell(u32 __iomem *db);
  931. void write_kernel_doorbell(void __iomem *db, u32 value);
  932. void write_kernel_doorbell64(void __iomem *db, u64 value);
  933. unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd,
  934. struct kfd_process_device *pdd,
  935. unsigned int doorbell_id);
  936. phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd);
  937. int kfd_alloc_process_doorbells(struct kfd_dev *kfd,
  938. struct kfd_process_device *pdd);
  939. void kfd_free_process_doorbells(struct kfd_dev *kfd,
  940. struct kfd_process_device *pdd);
  941. /* GTT Sub-Allocator */
  942. int kfd_gtt_sa_allocate(struct kfd_node *node, unsigned int size,
  943. struct kfd_mem_obj **mem_obj);
  944. int kfd_gtt_sa_free(struct kfd_node *node, struct kfd_mem_obj *mem_obj);
  945. extern struct device *kfd_device;
  946. /* KFD's procfs */
  947. void kfd_procfs_init(void);
  948. void kfd_procfs_shutdown(void);
  949. int kfd_procfs_add_queue(struct queue *q);
  950. void kfd_procfs_del_queue(struct queue *q);
  951. /* Topology */
  952. int kfd_topology_init(void);
  953. void kfd_topology_shutdown(void);
  954. int kfd_topology_add_device(struct kfd_node *gpu);
  955. int kfd_topology_remove_device(struct kfd_node *gpu);
  956. struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
  957. uint32_t proximity_domain);
  958. struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
  959. uint32_t proximity_domain);
  960. struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id);
  961. struct kfd_node *kfd_device_by_id(uint32_t gpu_id);
  962. struct kfd_node *kfd_device_by_pci_dev(const struct pci_dev *pdev);
  963. static inline bool kfd_irq_is_from_node(struct kfd_node *node, uint32_t node_id,
  964. uint32_t vmid)
  965. {
  966. return (node->interrupt_bitmap & (1 << node_id)) != 0 &&
  967. (node->compute_vmid_bitmap & (1 << vmid)) != 0;
  968. }
  969. static inline struct kfd_node *kfd_node_by_irq_ids(struct amdgpu_device *adev,
  970. uint32_t node_id, uint32_t vmid) {
  971. struct kfd_dev *dev = adev->kfd.dev;
  972. uint32_t i;
  973. if (KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 3) &&
  974. KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 4))
  975. return dev->nodes[0];
  976. for (i = 0; i < dev->num_nodes; i++)
  977. if (kfd_irq_is_from_node(dev->nodes[i], node_id, vmid))
  978. return dev->nodes[i];
  979. return NULL;
  980. }
  981. int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev);
  982. int kfd_numa_node_to_apic_id(int numa_node_id);
  983. /* Interrupts */
  984. #define KFD_IRQ_FENCE_CLIENTID 0xff
  985. #define KFD_IRQ_FENCE_SOURCEID 0xff
  986. #define KFD_IRQ_IS_FENCE(client, source) \
  987. ((client) == KFD_IRQ_FENCE_CLIENTID && \
  988. (source) == KFD_IRQ_FENCE_SOURCEID)
  989. int kfd_interrupt_init(struct kfd_node *dev);
  990. void kfd_interrupt_exit(struct kfd_node *dev);
  991. bool enqueue_ih_ring_entry(struct kfd_node *kfd, const void *ih_ring_entry);
  992. bool interrupt_is_wanted(struct kfd_node *dev,
  993. const uint32_t *ih_ring_entry,
  994. uint32_t *patched_ihre, bool *flag);
  995. int kfd_process_drain_interrupts(struct kfd_process_device *pdd);
  996. void kfd_process_close_interrupt_drain(unsigned int pasid);
  997. /* amdkfd Apertures */
  998. int kfd_init_apertures(struct kfd_process *process);
  999. void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
  1000. uint64_t tba_addr,
  1001. uint64_t tma_addr);
  1002. void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
  1003. bool enabled);
  1004. /* CWSR initialization */
  1005. int kfd_process_init_cwsr_apu(struct kfd_process *process, struct file *filep);
  1006. /* CRIU */
  1007. /*
  1008. * Need to increment KFD_CRIU_PRIV_VERSION each time a change is made to any of the CRIU private
  1009. * structures:
  1010. * kfd_criu_process_priv_data
  1011. * kfd_criu_device_priv_data
  1012. * kfd_criu_bo_priv_data
  1013. * kfd_criu_queue_priv_data
  1014. * kfd_criu_event_priv_data
  1015. * kfd_criu_svm_range_priv_data
  1016. */
  1017. #define KFD_CRIU_PRIV_VERSION 1
  1018. struct kfd_criu_process_priv_data {
  1019. uint32_t version;
  1020. uint32_t xnack_mode;
  1021. };
  1022. struct kfd_criu_device_priv_data {
  1023. /* For future use */
  1024. uint64_t reserved;
  1025. };
  1026. struct kfd_criu_bo_priv_data {
  1027. uint64_t user_addr;
  1028. uint32_t idr_handle;
  1029. uint32_t mapped_gpuids[MAX_GPU_INSTANCE];
  1030. };
  1031. /*
  1032. * The first 4 bytes of kfd_criu_queue_priv_data, kfd_criu_event_priv_data,
  1033. * kfd_criu_svm_range_priv_data is the object type
  1034. */
  1035. enum kfd_criu_object_type {
  1036. KFD_CRIU_OBJECT_TYPE_QUEUE,
  1037. KFD_CRIU_OBJECT_TYPE_EVENT,
  1038. KFD_CRIU_OBJECT_TYPE_SVM_RANGE,
  1039. };
  1040. struct kfd_criu_svm_range_priv_data {
  1041. uint32_t object_type;
  1042. uint64_t start_addr;
  1043. uint64_t size;
  1044. /* Variable length array of attributes */
  1045. struct kfd_ioctl_svm_attribute attrs[];
  1046. };
  1047. struct kfd_criu_queue_priv_data {
  1048. uint32_t object_type;
  1049. uint64_t q_address;
  1050. uint64_t q_size;
  1051. uint64_t read_ptr_addr;
  1052. uint64_t write_ptr_addr;
  1053. uint64_t doorbell_off;
  1054. uint64_t eop_ring_buffer_address;
  1055. uint64_t ctx_save_restore_area_address;
  1056. uint32_t gpu_id;
  1057. uint32_t type;
  1058. uint32_t format;
  1059. uint32_t q_id;
  1060. uint32_t priority;
  1061. uint32_t q_percent;
  1062. uint32_t doorbell_id;
  1063. uint32_t gws;
  1064. uint32_t sdma_id;
  1065. uint32_t eop_ring_buffer_size;
  1066. uint32_t ctx_save_restore_area_size;
  1067. uint32_t ctl_stack_size;
  1068. uint32_t mqd_size;
  1069. };
  1070. struct kfd_criu_event_priv_data {
  1071. uint32_t object_type;
  1072. uint64_t user_handle;
  1073. uint32_t event_id;
  1074. uint32_t auto_reset;
  1075. uint32_t type;
  1076. uint32_t signaled;
  1077. union {
  1078. struct kfd_hsa_memory_exception_data memory_exception_data;
  1079. struct kfd_hsa_hw_exception_data hw_exception_data;
  1080. };
  1081. };
  1082. int kfd_process_get_queue_info(struct kfd_process *p,
  1083. uint32_t *num_queues,
  1084. uint64_t *priv_data_sizes);
  1085. int kfd_criu_checkpoint_queues(struct kfd_process *p,
  1086. uint8_t __user *user_priv_data,
  1087. uint64_t *priv_data_offset);
  1088. int kfd_criu_restore_queue(struct kfd_process *p,
  1089. uint8_t __user *user_priv_data,
  1090. uint64_t *priv_data_offset,
  1091. uint64_t max_priv_data_size);
  1092. int kfd_criu_checkpoint_events(struct kfd_process *p,
  1093. uint8_t __user *user_priv_data,
  1094. uint64_t *priv_data_offset);
  1095. int kfd_criu_restore_event(struct file *devkfd,
  1096. struct kfd_process *p,
  1097. uint8_t __user *user_priv_data,
  1098. uint64_t *priv_data_offset,
  1099. uint64_t max_priv_data_size);
  1100. /* CRIU - End */
  1101. /* Queue Context Management */
  1102. int init_queue(struct queue **q, const struct queue_properties *properties);
  1103. void uninit_queue(struct queue *q);
  1104. void print_queue_properties(struct queue_properties *q);
  1105. void print_queue(struct queue *q);
  1106. int kfd_queue_buffer_get(struct amdgpu_vm *vm, void __user *addr, struct amdgpu_bo **pbo,
  1107. u64 expected_size);
  1108. void kfd_queue_buffer_put(struct amdgpu_bo **bo);
  1109. int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_properties *properties);
  1110. int kfd_queue_release_buffers(struct kfd_process_device *pdd, struct queue_properties *properties);
  1111. void kfd_queue_unref_bo_va(struct amdgpu_vm *vm, struct amdgpu_bo **bo);
  1112. int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd,
  1113. struct queue_properties *properties);
  1114. void kfd_queue_ctx_save_restore_size(struct kfd_topology_device *dev);
  1115. struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
  1116. struct kfd_node *dev);
  1117. struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
  1118. struct kfd_node *dev);
  1119. struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
  1120. struct kfd_node *dev);
  1121. struct mqd_manager *mqd_manager_init_v10(enum KFD_MQD_TYPE type,
  1122. struct kfd_node *dev);
  1123. struct mqd_manager *mqd_manager_init_v11(enum KFD_MQD_TYPE type,
  1124. struct kfd_node *dev);
  1125. struct mqd_manager *mqd_manager_init_v12(enum KFD_MQD_TYPE type,
  1126. struct kfd_node *dev);
  1127. struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev);
  1128. void device_queue_manager_uninit(struct device_queue_manager *dqm);
  1129. struct kernel_queue *kernel_queue_init(struct kfd_node *dev,
  1130. enum kfd_queue_type type);
  1131. void kernel_queue_uninit(struct kernel_queue *kq);
  1132. int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid);
  1133. int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbell_id);
  1134. /* Process Queue Manager */
  1135. struct process_queue_node {
  1136. struct queue *q;
  1137. struct kernel_queue *kq;
  1138. struct list_head process_queue_list;
  1139. };
  1140. void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
  1141. void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
  1142. int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
  1143. void pqm_uninit(struct process_queue_manager *pqm);
  1144. int pqm_create_queue(struct process_queue_manager *pqm,
  1145. struct kfd_node *dev,
  1146. struct file *f,
  1147. struct queue_properties *properties,
  1148. unsigned int *qid,
  1149. const struct kfd_criu_queue_priv_data *q_data,
  1150. const void *restore_mqd,
  1151. const void *restore_ctl_stack,
  1152. uint32_t *p_doorbell_offset_in_process);
  1153. int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
  1154. int pqm_update_queue_properties(struct process_queue_manager *pqm, unsigned int qid,
  1155. struct queue_properties *p);
  1156. int pqm_update_mqd(struct process_queue_manager *pqm, unsigned int qid,
  1157. struct mqd_update_info *minfo);
  1158. int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
  1159. void *gws);
  1160. struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
  1161. unsigned int qid);
  1162. struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
  1163. unsigned int qid);
  1164. int pqm_get_wave_state(struct process_queue_manager *pqm,
  1165. unsigned int qid,
  1166. void __user *ctl_stack,
  1167. u32 *ctl_stack_used_size,
  1168. u32 *save_area_used_size);
  1169. int pqm_get_queue_snapshot(struct process_queue_manager *pqm,
  1170. uint64_t exception_clear_mask,
  1171. void __user *buf,
  1172. int *num_qss_entries,
  1173. uint32_t *entry_size);
  1174. int amdkfd_fence_wait_timeout(struct device_queue_manager *dqm,
  1175. uint64_t fence_value,
  1176. unsigned int timeout_ms);
  1177. int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
  1178. unsigned int qid,
  1179. u32 *mqd_size,
  1180. u32 *ctl_stack_size);
  1181. /* Packet Manager */
  1182. #define KFD_FENCE_COMPLETED (100)
  1183. #define KFD_FENCE_INIT (10)
  1184. struct packet_manager {
  1185. struct device_queue_manager *dqm;
  1186. struct kernel_queue *priv_queue;
  1187. struct mutex lock;
  1188. bool allocated;
  1189. struct kfd_mem_obj *ib_buffer_obj;
  1190. unsigned int ib_size_bytes;
  1191. bool is_over_subscription;
  1192. const struct packet_manager_funcs *pmf;
  1193. };
  1194. struct packet_manager_funcs {
  1195. /* Support ASIC-specific packet formats for PM4 packets */
  1196. int (*map_process)(struct packet_manager *pm, uint32_t *buffer,
  1197. struct qcm_process_device *qpd);
  1198. int (*runlist)(struct packet_manager *pm, uint32_t *buffer,
  1199. uint64_t ib, size_t ib_size_in_dwords, bool chain);
  1200. int (*set_resources)(struct packet_manager *pm, uint32_t *buffer,
  1201. struct scheduling_resources *res);
  1202. int (*map_queues)(struct packet_manager *pm, uint32_t *buffer,
  1203. struct queue *q, bool is_static);
  1204. int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer,
  1205. enum kfd_unmap_queues_filter mode,
  1206. uint32_t filter_param, bool reset);
  1207. int (*set_grace_period)(struct packet_manager *pm, uint32_t *buffer,
  1208. uint32_t grace_period);
  1209. int (*query_status)(struct packet_manager *pm, uint32_t *buffer,
  1210. uint64_t fence_address, uint64_t fence_value);
  1211. int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer);
  1212. /* Packet sizes */
  1213. int map_process_size;
  1214. int runlist_size;
  1215. int set_resources_size;
  1216. int map_queues_size;
  1217. int unmap_queues_size;
  1218. int set_grace_period_size;
  1219. int query_status_size;
  1220. int release_mem_size;
  1221. };
  1222. extern const struct packet_manager_funcs kfd_vi_pm_funcs;
  1223. extern const struct packet_manager_funcs kfd_v9_pm_funcs;
  1224. extern const struct packet_manager_funcs kfd_aldebaran_pm_funcs;
  1225. int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
  1226. void pm_uninit(struct packet_manager *pm);
  1227. int pm_send_set_resources(struct packet_manager *pm,
  1228. struct scheduling_resources *res);
  1229. int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
  1230. int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
  1231. uint64_t fence_value);
  1232. int pm_send_unmap_queue(struct packet_manager *pm,
  1233. enum kfd_unmap_queues_filter mode,
  1234. uint32_t filter_param, bool reset);
  1235. void pm_release_ib(struct packet_manager *pm);
  1236. int pm_update_grace_period(struct packet_manager *pm, uint32_t grace_period);
  1237. /* Following PM funcs can be shared among VI and AI */
  1238. unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size);
  1239. uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
  1240. /* Events */
  1241. extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
  1242. extern const struct kfd_event_interrupt_class event_interrupt_class_v9;
  1243. extern const struct kfd_event_interrupt_class event_interrupt_class_v9_4_3;
  1244. extern const struct kfd_event_interrupt_class event_interrupt_class_v10;
  1245. extern const struct kfd_event_interrupt_class event_interrupt_class_v11;
  1246. extern const struct kfd_device_global_init_class device_global_init_class_cik;
  1247. int kfd_event_init_process(struct kfd_process *p);
  1248. void kfd_event_free_process(struct kfd_process *p);
  1249. int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
  1250. int kfd_wait_on_events(struct kfd_process *p,
  1251. uint32_t num_events, void __user *data,
  1252. bool all, uint32_t *user_timeout_ms,
  1253. uint32_t *wait_result);
  1254. void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
  1255. uint32_t valid_id_bits);
  1256. void kfd_signal_hw_exception_event(u32 pasid);
  1257. int kfd_set_event(struct kfd_process *p, uint32_t event_id);
  1258. int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
  1259. int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset);
  1260. int kfd_event_create(struct file *devkfd, struct kfd_process *p,
  1261. uint32_t event_type, bool auto_reset, uint32_t node_id,
  1262. uint32_t *event_id, uint32_t *event_trigger_data,
  1263. uint64_t *event_page_offset, uint32_t *event_slot_index);
  1264. int kfd_get_num_events(struct kfd_process *p);
  1265. int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
  1266. void kfd_signal_vm_fault_event(struct kfd_node *dev, u32 pasid,
  1267. struct kfd_vm_fault_info *info,
  1268. struct kfd_hsa_memory_exception_data *data);
  1269. void kfd_signal_reset_event(struct kfd_node *dev);
  1270. void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid);
  1271. static inline void kfd_flush_tlb(struct kfd_process_device *pdd,
  1272. enum TLB_FLUSH_TYPE type)
  1273. {
  1274. struct amdgpu_device *adev = pdd->dev->adev;
  1275. struct amdgpu_vm *vm = drm_priv_to_vm(pdd->drm_priv);
  1276. amdgpu_vm_flush_compute_tlb(adev, vm, type, pdd->dev->xcc_mask);
  1277. }
  1278. static inline bool kfd_flush_tlb_after_unmap(struct kfd_dev *dev)
  1279. {
  1280. return KFD_GC_VERSION(dev) >= IP_VERSION(9, 4, 2) ||
  1281. (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 1) && dev->sdma_fw_version >= 18) ||
  1282. KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 0);
  1283. }
  1284. int kfd_send_exception_to_runtime(struct kfd_process *p,
  1285. unsigned int queue_id,
  1286. uint64_t error_reason);
  1287. bool kfd_is_locked(void);
  1288. /* Compute profile */
  1289. void kfd_inc_compute_active(struct kfd_node *dev);
  1290. void kfd_dec_compute_active(struct kfd_node *dev);
  1291. /* Cgroup Support */
  1292. /* Check with device cgroup if @kfd device is accessible */
  1293. static inline int kfd_devcgroup_check_permission(struct kfd_node *node)
  1294. {
  1295. #if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
  1296. struct drm_device *ddev;
  1297. if (node->xcp)
  1298. ddev = node->xcp->ddev;
  1299. else
  1300. ddev = adev_to_drm(node->adev);
  1301. return devcgroup_check_permission(DEVCG_DEV_CHAR, DRM_MAJOR,
  1302. ddev->render->index,
  1303. DEVCG_ACC_WRITE | DEVCG_ACC_READ);
  1304. #else
  1305. return 0;
  1306. #endif
  1307. }
  1308. static inline bool kfd_is_first_node(struct kfd_node *node)
  1309. {
  1310. return (node == node->kfd->nodes[0]);
  1311. }
  1312. /* Debugfs */
  1313. #if defined(CONFIG_DEBUG_FS)
  1314. void kfd_debugfs_init(void);
  1315. void kfd_debugfs_fini(void);
  1316. int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
  1317. int pqm_debugfs_mqds(struct seq_file *m, void *data);
  1318. int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
  1319. int dqm_debugfs_hqds(struct seq_file *m, void *data);
  1320. int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
  1321. int pm_debugfs_runlist(struct seq_file *m, void *data);
  1322. int kfd_debugfs_hang_hws(struct kfd_node *dev);
  1323. int pm_debugfs_hang_hws(struct packet_manager *pm);
  1324. int dqm_debugfs_hang_hws(struct device_queue_manager *dqm);
  1325. #else
  1326. static inline void kfd_debugfs_init(void) {}
  1327. static inline void kfd_debugfs_fini(void) {}
  1328. #endif
  1329. #endif