kfd_device.c 24 KB

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