kfd_device.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #if defined(CONFIG_AMD_IOMMU_V2_MODULE) || defined(CONFIG_AMD_IOMMU_V2)
  23. #include <linux/amd-iommu.h>
  24. #endif
  25. #include <linux/bsearch.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include "kfd_priv.h"
  29. #include "kfd_device_queue_manager.h"
  30. #include "kfd_pm4_headers_vi.h"
  31. #include "cwsr_trap_handler_gfx8.asm"
  32. #include "kfd_iommu.h"
  33. #define MQD_SIZE_ALIGNED 768
  34. static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
  35. #ifdef KFD_SUPPORT_IOMMU_V2
  36. static const struct kfd_device_info kaveri_device_info = {
  37. .asic_family = CHIP_KAVERI,
  38. .max_pasid_bits = 16,
  39. /* max num of queues for KV.TODO should be a dynamic value */
  40. .max_no_of_hqd = 24,
  41. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  42. .event_interrupt_class = &event_interrupt_class_cik,
  43. .num_of_watch_points = 4,
  44. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  45. .supports_cwsr = false,
  46. .needs_iommu_device = true,
  47. .needs_pci_atomics = false,
  48. };
  49. static const struct kfd_device_info carrizo_device_info = {
  50. .asic_family = CHIP_CARRIZO,
  51. .max_pasid_bits = 16,
  52. /* max num of queues for CZ.TODO should be a dynamic value */
  53. .max_no_of_hqd = 24,
  54. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  55. .event_interrupt_class = &event_interrupt_class_cik,
  56. .num_of_watch_points = 4,
  57. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  58. .supports_cwsr = true,
  59. .needs_iommu_device = true,
  60. .needs_pci_atomics = false,
  61. };
  62. #endif
  63. static const struct kfd_device_info hawaii_device_info = {
  64. .asic_family = CHIP_HAWAII,
  65. .max_pasid_bits = 16,
  66. /* max num of queues for KV.TODO should be a dynamic value */
  67. .max_no_of_hqd = 24,
  68. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  69. .event_interrupt_class = &event_interrupt_class_cik,
  70. .num_of_watch_points = 4,
  71. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  72. .supports_cwsr = false,
  73. .needs_iommu_device = false,
  74. .needs_pci_atomics = false,
  75. };
  76. static const struct kfd_device_info tonga_device_info = {
  77. .asic_family = CHIP_TONGA,
  78. .max_pasid_bits = 16,
  79. .max_no_of_hqd = 24,
  80. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  81. .event_interrupt_class = &event_interrupt_class_cik,
  82. .num_of_watch_points = 4,
  83. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  84. .supports_cwsr = false,
  85. .needs_iommu_device = false,
  86. .needs_pci_atomics = true,
  87. };
  88. static const struct kfd_device_info tonga_vf_device_info = {
  89. .asic_family = CHIP_TONGA,
  90. .max_pasid_bits = 16,
  91. .max_no_of_hqd = 24,
  92. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  93. .event_interrupt_class = &event_interrupt_class_cik,
  94. .num_of_watch_points = 4,
  95. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  96. .supports_cwsr = false,
  97. .needs_iommu_device = false,
  98. .needs_pci_atomics = false,
  99. };
  100. static const struct kfd_device_info fiji_device_info = {
  101. .asic_family = CHIP_FIJI,
  102. .max_pasid_bits = 16,
  103. .max_no_of_hqd = 24,
  104. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  105. .event_interrupt_class = &event_interrupt_class_cik,
  106. .num_of_watch_points = 4,
  107. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  108. .supports_cwsr = true,
  109. .needs_iommu_device = false,
  110. .needs_pci_atomics = true,
  111. };
  112. static const struct kfd_device_info fiji_vf_device_info = {
  113. .asic_family = CHIP_FIJI,
  114. .max_pasid_bits = 16,
  115. .max_no_of_hqd = 24,
  116. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  117. .event_interrupt_class = &event_interrupt_class_cik,
  118. .num_of_watch_points = 4,
  119. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  120. .supports_cwsr = true,
  121. .needs_iommu_device = false,
  122. .needs_pci_atomics = false,
  123. };
  124. static const struct kfd_device_info polaris10_device_info = {
  125. .asic_family = CHIP_POLARIS10,
  126. .max_pasid_bits = 16,
  127. .max_no_of_hqd = 24,
  128. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  129. .event_interrupt_class = &event_interrupt_class_cik,
  130. .num_of_watch_points = 4,
  131. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  132. .supports_cwsr = true,
  133. .needs_iommu_device = false,
  134. .needs_pci_atomics = true,
  135. };
  136. static const struct kfd_device_info polaris10_vf_device_info = {
  137. .asic_family = CHIP_POLARIS10,
  138. .max_pasid_bits = 16,
  139. .max_no_of_hqd = 24,
  140. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  141. .event_interrupt_class = &event_interrupt_class_cik,
  142. .num_of_watch_points = 4,
  143. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  144. .supports_cwsr = true,
  145. .needs_iommu_device = false,
  146. .needs_pci_atomics = false,
  147. };
  148. static const struct kfd_device_info polaris11_device_info = {
  149. .asic_family = CHIP_POLARIS11,
  150. .max_pasid_bits = 16,
  151. .max_no_of_hqd = 24,
  152. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  153. .event_interrupt_class = &event_interrupt_class_cik,
  154. .num_of_watch_points = 4,
  155. .mqd_size_aligned = MQD_SIZE_ALIGNED,
  156. .supports_cwsr = true,
  157. .needs_iommu_device = false,
  158. .needs_pci_atomics = true,
  159. };
  160. struct kfd_deviceid {
  161. unsigned short did;
  162. const struct kfd_device_info *device_info;
  163. };
  164. static const struct kfd_deviceid supported_devices[] = {
  165. #ifdef KFD_SUPPORT_IOMMU_V2
  166. { 0x1304, &kaveri_device_info }, /* Kaveri */
  167. { 0x1305, &kaveri_device_info }, /* Kaveri */
  168. { 0x1306, &kaveri_device_info }, /* Kaveri */
  169. { 0x1307, &kaveri_device_info }, /* Kaveri */
  170. { 0x1309, &kaveri_device_info }, /* Kaveri */
  171. { 0x130A, &kaveri_device_info }, /* Kaveri */
  172. { 0x130B, &kaveri_device_info }, /* Kaveri */
  173. { 0x130C, &kaveri_device_info }, /* Kaveri */
  174. { 0x130D, &kaveri_device_info }, /* Kaveri */
  175. { 0x130E, &kaveri_device_info }, /* Kaveri */
  176. { 0x130F, &kaveri_device_info }, /* Kaveri */
  177. { 0x1310, &kaveri_device_info }, /* Kaveri */
  178. { 0x1311, &kaveri_device_info }, /* Kaveri */
  179. { 0x1312, &kaveri_device_info }, /* Kaveri */
  180. { 0x1313, &kaveri_device_info }, /* Kaveri */
  181. { 0x1315, &kaveri_device_info }, /* Kaveri */
  182. { 0x1316, &kaveri_device_info }, /* Kaveri */
  183. { 0x1317, &kaveri_device_info }, /* Kaveri */
  184. { 0x1318, &kaveri_device_info }, /* Kaveri */
  185. { 0x131B, &kaveri_device_info }, /* Kaveri */
  186. { 0x131C, &kaveri_device_info }, /* Kaveri */
  187. { 0x131D, &kaveri_device_info }, /* Kaveri */
  188. { 0x9870, &carrizo_device_info }, /* Carrizo */
  189. { 0x9874, &carrizo_device_info }, /* Carrizo */
  190. { 0x9875, &carrizo_device_info }, /* Carrizo */
  191. { 0x9876, &carrizo_device_info }, /* Carrizo */
  192. { 0x9877, &carrizo_device_info }, /* Carrizo */
  193. #endif
  194. { 0x67A0, &hawaii_device_info }, /* Hawaii */
  195. { 0x67A1, &hawaii_device_info }, /* Hawaii */
  196. { 0x67A2, &hawaii_device_info }, /* Hawaii */
  197. { 0x67A8, &hawaii_device_info }, /* Hawaii */
  198. { 0x67A9, &hawaii_device_info }, /* Hawaii */
  199. { 0x67AA, &hawaii_device_info }, /* Hawaii */
  200. { 0x67B0, &hawaii_device_info }, /* Hawaii */
  201. { 0x67B1, &hawaii_device_info }, /* Hawaii */
  202. { 0x67B8, &hawaii_device_info }, /* Hawaii */
  203. { 0x67B9, &hawaii_device_info }, /* Hawaii */
  204. { 0x67BA, &hawaii_device_info }, /* Hawaii */
  205. { 0x67BE, &hawaii_device_info }, /* Hawaii */
  206. { 0x6920, &tonga_device_info }, /* Tonga */
  207. { 0x6921, &tonga_device_info }, /* Tonga */
  208. { 0x6928, &tonga_device_info }, /* Tonga */
  209. { 0x6929, &tonga_device_info }, /* Tonga */
  210. { 0x692B, &tonga_device_info }, /* Tonga */
  211. { 0x692F, &tonga_vf_device_info }, /* Tonga vf */
  212. { 0x6938, &tonga_device_info }, /* Tonga */
  213. { 0x6939, &tonga_device_info }, /* Tonga */
  214. { 0x7300, &fiji_device_info }, /* Fiji */
  215. { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/
  216. { 0x67C0, &polaris10_device_info }, /* Polaris10 */
  217. { 0x67C1, &polaris10_device_info }, /* Polaris10 */
  218. { 0x67C2, &polaris10_device_info }, /* Polaris10 */
  219. { 0x67C4, &polaris10_device_info }, /* Polaris10 */
  220. { 0x67C7, &polaris10_device_info }, /* Polaris10 */
  221. { 0x67C8, &polaris10_device_info }, /* Polaris10 */
  222. { 0x67C9, &polaris10_device_info }, /* Polaris10 */
  223. { 0x67CA, &polaris10_device_info }, /* Polaris10 */
  224. { 0x67CC, &polaris10_device_info }, /* Polaris10 */
  225. { 0x67CF, &polaris10_device_info }, /* Polaris10 */
  226. { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/
  227. { 0x67DF, &polaris10_device_info }, /* Polaris10 */
  228. { 0x67E0, &polaris11_device_info }, /* Polaris11 */
  229. { 0x67E1, &polaris11_device_info }, /* Polaris11 */
  230. { 0x67E3, &polaris11_device_info }, /* Polaris11 */
  231. { 0x67E7, &polaris11_device_info }, /* Polaris11 */
  232. { 0x67E8, &polaris11_device_info }, /* Polaris11 */
  233. { 0x67E9, &polaris11_device_info }, /* Polaris11 */
  234. { 0x67EB, &polaris11_device_info }, /* Polaris11 */
  235. { 0x67EF, &polaris11_device_info }, /* Polaris11 */
  236. { 0x67FF, &polaris11_device_info }, /* Polaris11 */
  237. };
  238. static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
  239. unsigned int chunk_size);
  240. static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
  241. static int kfd_resume(struct kfd_dev *kfd);
  242. static const struct kfd_device_info *lookup_device_info(unsigned short did)
  243. {
  244. size_t i;
  245. for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
  246. if (supported_devices[i].did == did) {
  247. WARN_ON(!supported_devices[i].device_info);
  248. return supported_devices[i].device_info;
  249. }
  250. }
  251. dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
  252. did);
  253. return NULL;
  254. }
  255. struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
  256. struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
  257. {
  258. struct kfd_dev *kfd;
  259. const struct kfd_device_info *device_info =
  260. lookup_device_info(pdev->device);
  261. if (!device_info) {
  262. dev_err(kfd_device, "kgd2kfd_probe failed\n");
  263. return NULL;
  264. }
  265. if (device_info->needs_pci_atomics) {
  266. /* Allow BIF to recode atomics to PCIe 3.0
  267. * AtomicOps. 32 and 64-bit requests are possible and
  268. * must be supported.
  269. */
  270. if (pci_enable_atomic_ops_to_root(pdev,
  271. PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
  272. PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
  273. dev_info(kfd_device,
  274. "skipped device %x:%x, PCI rejects atomics",
  275. pdev->vendor, pdev->device);
  276. return NULL;
  277. }
  278. }
  279. kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
  280. if (!kfd)
  281. return NULL;
  282. kfd->kgd = kgd;
  283. kfd->device_info = device_info;
  284. kfd->pdev = pdev;
  285. kfd->init_complete = false;
  286. kfd->kfd2kgd = f2g;
  287. mutex_init(&kfd->doorbell_mutex);
  288. memset(&kfd->doorbell_available_index, 0,
  289. sizeof(kfd->doorbell_available_index));
  290. return kfd;
  291. }
  292. static void kfd_cwsr_init(struct kfd_dev *kfd)
  293. {
  294. if (cwsr_enable && kfd->device_info->supports_cwsr) {
  295. BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
  296. kfd->cwsr_isa = cwsr_trap_gfx8_hex;
  297. kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
  298. kfd->cwsr_enabled = true;
  299. }
  300. }
  301. bool kgd2kfd_device_init(struct kfd_dev *kfd,
  302. const struct kgd2kfd_shared_resources *gpu_resources)
  303. {
  304. unsigned int size;
  305. kfd->shared_resources = *gpu_resources;
  306. kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
  307. kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
  308. kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
  309. - kfd->vm_info.first_vmid_kfd + 1;
  310. /* Verify module parameters regarding mapped process number*/
  311. if ((hws_max_conc_proc < 0)
  312. || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
  313. dev_err(kfd_device,
  314. "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
  315. hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
  316. kfd->vm_info.vmid_num_kfd);
  317. kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
  318. } else
  319. kfd->max_proc_per_quantum = hws_max_conc_proc;
  320. /* calculate max size of mqds needed for queues */
  321. size = max_num_of_queues_per_device *
  322. kfd->device_info->mqd_size_aligned;
  323. /*
  324. * calculate max size of runlist packet.
  325. * There can be only 2 packets at once
  326. */
  327. size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
  328. max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
  329. + sizeof(struct pm4_mes_runlist)) * 2;
  330. /* Add size of HIQ & DIQ */
  331. size += KFD_KERNEL_QUEUE_SIZE * 2;
  332. /* add another 512KB for all other allocations on gart (HPD, fences) */
  333. size += 512 * 1024;
  334. if (kfd->kfd2kgd->init_gtt_mem_allocation(
  335. kfd->kgd, size, &kfd->gtt_mem,
  336. &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
  337. dev_err(kfd_device, "Could not allocate %d bytes\n", size);
  338. goto out;
  339. }
  340. dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
  341. /* Initialize GTT sa with 512 byte chunk size */
  342. if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
  343. dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
  344. goto kfd_gtt_sa_init_error;
  345. }
  346. if (kfd_doorbell_init(kfd)) {
  347. dev_err(kfd_device,
  348. "Error initializing doorbell aperture\n");
  349. goto kfd_doorbell_error;
  350. }
  351. if (kfd_topology_add_device(kfd)) {
  352. dev_err(kfd_device, "Error adding device to topology\n");
  353. goto kfd_topology_add_device_error;
  354. }
  355. if (kfd_interrupt_init(kfd)) {
  356. dev_err(kfd_device, "Error initializing interrupts\n");
  357. goto kfd_interrupt_error;
  358. }
  359. kfd->dqm = device_queue_manager_init(kfd);
  360. if (!kfd->dqm) {
  361. dev_err(kfd_device, "Error initializing queue manager\n");
  362. goto device_queue_manager_error;
  363. }
  364. if (kfd_iommu_device_init(kfd)) {
  365. dev_err(kfd_device, "Error initializing iommuv2\n");
  366. goto device_iommu_error;
  367. }
  368. kfd_cwsr_init(kfd);
  369. if (kfd_resume(kfd))
  370. goto kfd_resume_error;
  371. kfd->dbgmgr = NULL;
  372. kfd->init_complete = true;
  373. dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
  374. kfd->pdev->device);
  375. pr_debug("Starting kfd with the following scheduling policy %d\n",
  376. kfd->dqm->sched_policy);
  377. goto out;
  378. kfd_resume_error:
  379. device_iommu_error:
  380. device_queue_manager_uninit(kfd->dqm);
  381. device_queue_manager_error:
  382. kfd_interrupt_exit(kfd);
  383. kfd_interrupt_error:
  384. kfd_topology_remove_device(kfd);
  385. kfd_topology_add_device_error:
  386. kfd_doorbell_fini(kfd);
  387. kfd_doorbell_error:
  388. kfd_gtt_sa_fini(kfd);
  389. kfd_gtt_sa_init_error:
  390. kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
  391. dev_err(kfd_device,
  392. "device %x:%x NOT added due to errors\n",
  393. kfd->pdev->vendor, kfd->pdev->device);
  394. out:
  395. return kfd->init_complete;
  396. }
  397. void kgd2kfd_device_exit(struct kfd_dev *kfd)
  398. {
  399. if (kfd->init_complete) {
  400. kgd2kfd_suspend(kfd);
  401. device_queue_manager_uninit(kfd->dqm);
  402. kfd_interrupt_exit(kfd);
  403. kfd_topology_remove_device(kfd);
  404. kfd_doorbell_fini(kfd);
  405. kfd_gtt_sa_fini(kfd);
  406. kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
  407. }
  408. kfree(kfd);
  409. }
  410. void kgd2kfd_suspend(struct kfd_dev *kfd)
  411. {
  412. if (!kfd->init_complete)
  413. return;
  414. /* For first KFD device suspend all the KFD processes */
  415. if (atomic_inc_return(&kfd_device_suspended) == 1)
  416. kfd_suspend_all_processes();
  417. kfd->dqm->ops.stop(kfd->dqm);
  418. kfd_iommu_suspend(kfd);
  419. }
  420. int kgd2kfd_resume(struct kfd_dev *kfd)
  421. {
  422. int ret, count;
  423. if (!kfd->init_complete)
  424. return 0;
  425. ret = kfd_resume(kfd);
  426. if (ret)
  427. return ret;
  428. count = atomic_dec_return(&kfd_device_suspended);
  429. WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
  430. if (count == 0)
  431. ret = kfd_resume_all_processes();
  432. return ret;
  433. }
  434. static int kfd_resume(struct kfd_dev *kfd)
  435. {
  436. int err = 0;
  437. err = kfd_iommu_resume(kfd);
  438. if (err) {
  439. dev_err(kfd_device,
  440. "Failed to resume IOMMU for device %x:%x\n",
  441. kfd->pdev->vendor, kfd->pdev->device);
  442. return err;
  443. }
  444. err = kfd->dqm->ops.start(kfd->dqm);
  445. if (err) {
  446. dev_err(kfd_device,
  447. "Error starting queue manager for device %x:%x\n",
  448. kfd->pdev->vendor, kfd->pdev->device);
  449. goto dqm_start_error;
  450. }
  451. return err;
  452. dqm_start_error:
  453. kfd_iommu_suspend(kfd);
  454. return err;
  455. }
  456. /* This is called directly from KGD at ISR. */
  457. void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
  458. {
  459. if (!kfd->init_complete)
  460. return;
  461. spin_lock(&kfd->interrupt_lock);
  462. if (kfd->interrupts_active
  463. && interrupt_is_wanted(kfd, ih_ring_entry)
  464. && enqueue_ih_ring_entry(kfd, ih_ring_entry))
  465. queue_work(kfd->ih_wq, &kfd->interrupt_work);
  466. spin_unlock(&kfd->interrupt_lock);
  467. }
  468. /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
  469. * prepare for safe eviction of KFD BOs that belong to the specified
  470. * process.
  471. *
  472. * @mm: mm_struct that identifies the specified KFD process
  473. * @fence: eviction fence attached to KFD process BOs
  474. *
  475. */
  476. int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
  477. struct dma_fence *fence)
  478. {
  479. struct kfd_process *p;
  480. unsigned long active_time;
  481. unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
  482. if (!fence)
  483. return -EINVAL;
  484. if (dma_fence_is_signaled(fence))
  485. return 0;
  486. p = kfd_lookup_process_by_mm(mm);
  487. if (!p)
  488. return -ENODEV;
  489. if (fence->seqno == p->last_eviction_seqno)
  490. goto out;
  491. p->last_eviction_seqno = fence->seqno;
  492. /* Avoid KFD process starvation. Wait for at least
  493. * PROCESS_ACTIVE_TIME_MS before evicting the process again
  494. */
  495. active_time = get_jiffies_64() - p->last_restore_timestamp;
  496. if (delay_jiffies > active_time)
  497. delay_jiffies -= active_time;
  498. else
  499. delay_jiffies = 0;
  500. /* During process initialization eviction_work.dwork is initialized
  501. * to kfd_evict_bo_worker
  502. */
  503. schedule_delayed_work(&p->eviction_work, delay_jiffies);
  504. out:
  505. kfd_unref_process(p);
  506. return 0;
  507. }
  508. static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
  509. unsigned int chunk_size)
  510. {
  511. unsigned int num_of_longs;
  512. if (WARN_ON(buf_size < chunk_size))
  513. return -EINVAL;
  514. if (WARN_ON(buf_size == 0))
  515. return -EINVAL;
  516. if (WARN_ON(chunk_size == 0))
  517. return -EINVAL;
  518. kfd->gtt_sa_chunk_size = chunk_size;
  519. kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
  520. num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
  521. BITS_PER_LONG;
  522. kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
  523. if (!kfd->gtt_sa_bitmap)
  524. return -ENOMEM;
  525. pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
  526. kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
  527. mutex_init(&kfd->gtt_sa_lock);
  528. return 0;
  529. }
  530. static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
  531. {
  532. mutex_destroy(&kfd->gtt_sa_lock);
  533. kfree(kfd->gtt_sa_bitmap);
  534. }
  535. static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
  536. unsigned int bit_num,
  537. unsigned int chunk_size)
  538. {
  539. return start_addr + bit_num * chunk_size;
  540. }
  541. static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
  542. unsigned int bit_num,
  543. unsigned int chunk_size)
  544. {
  545. return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
  546. }
  547. int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
  548. struct kfd_mem_obj **mem_obj)
  549. {
  550. unsigned int found, start_search, cur_size;
  551. if (size == 0)
  552. return -EINVAL;
  553. if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
  554. return -ENOMEM;
  555. *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
  556. if ((*mem_obj) == NULL)
  557. return -ENOMEM;
  558. pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
  559. start_search = 0;
  560. mutex_lock(&kfd->gtt_sa_lock);
  561. kfd_gtt_restart_search:
  562. /* Find the first chunk that is free */
  563. found = find_next_zero_bit(kfd->gtt_sa_bitmap,
  564. kfd->gtt_sa_num_of_chunks,
  565. start_search);
  566. pr_debug("Found = %d\n", found);
  567. /* If there wasn't any free chunk, bail out */
  568. if (found == kfd->gtt_sa_num_of_chunks)
  569. goto kfd_gtt_no_free_chunk;
  570. /* Update fields of mem_obj */
  571. (*mem_obj)->range_start = found;
  572. (*mem_obj)->range_end = found;
  573. (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
  574. kfd->gtt_start_gpu_addr,
  575. found,
  576. kfd->gtt_sa_chunk_size);
  577. (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
  578. kfd->gtt_start_cpu_ptr,
  579. found,
  580. kfd->gtt_sa_chunk_size);
  581. pr_debug("gpu_addr = %p, cpu_addr = %p\n",
  582. (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
  583. /* If we need only one chunk, mark it as allocated and get out */
  584. if (size <= kfd->gtt_sa_chunk_size) {
  585. pr_debug("Single bit\n");
  586. set_bit(found, kfd->gtt_sa_bitmap);
  587. goto kfd_gtt_out;
  588. }
  589. /* Otherwise, try to see if we have enough contiguous chunks */
  590. cur_size = size - kfd->gtt_sa_chunk_size;
  591. do {
  592. (*mem_obj)->range_end =
  593. find_next_zero_bit(kfd->gtt_sa_bitmap,
  594. kfd->gtt_sa_num_of_chunks, ++found);
  595. /*
  596. * If next free chunk is not contiguous than we need to
  597. * restart our search from the last free chunk we found (which
  598. * wasn't contiguous to the previous ones
  599. */
  600. if ((*mem_obj)->range_end != found) {
  601. start_search = found;
  602. goto kfd_gtt_restart_search;
  603. }
  604. /*
  605. * If we reached end of buffer, bail out with error
  606. */
  607. if (found == kfd->gtt_sa_num_of_chunks)
  608. goto kfd_gtt_no_free_chunk;
  609. /* Check if we don't need another chunk */
  610. if (cur_size <= kfd->gtt_sa_chunk_size)
  611. cur_size = 0;
  612. else
  613. cur_size -= kfd->gtt_sa_chunk_size;
  614. } while (cur_size > 0);
  615. pr_debug("range_start = %d, range_end = %d\n",
  616. (*mem_obj)->range_start, (*mem_obj)->range_end);
  617. /* Mark the chunks as allocated */
  618. for (found = (*mem_obj)->range_start;
  619. found <= (*mem_obj)->range_end;
  620. found++)
  621. set_bit(found, kfd->gtt_sa_bitmap);
  622. kfd_gtt_out:
  623. mutex_unlock(&kfd->gtt_sa_lock);
  624. return 0;
  625. kfd_gtt_no_free_chunk:
  626. pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
  627. mutex_unlock(&kfd->gtt_sa_lock);
  628. kfree(mem_obj);
  629. return -ENOMEM;
  630. }
  631. int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
  632. {
  633. unsigned int bit;
  634. /* Act like kfree when trying to free a NULL object */
  635. if (!mem_obj)
  636. return 0;
  637. pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
  638. mem_obj, mem_obj->range_start, mem_obj->range_end);
  639. mutex_lock(&kfd->gtt_sa_lock);
  640. /* Mark the chunks as free */
  641. for (bit = mem_obj->range_start;
  642. bit <= mem_obj->range_end;
  643. bit++)
  644. clear_bit(bit, kfd->gtt_sa_bitmap);
  645. mutex_unlock(&kfd->gtt_sa_lock);
  646. kfree(mem_obj);
  647. return 0;
  648. }