kfd_device.c 27 KB

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