kfd_device.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/amd-iommu.h>
  23. #include <linux/bsearch.h>
  24. #include <linux/pci.h>
  25. #include <linux/slab.h>
  26. #include "kfd_priv.h"
  27. #include "kfd_device_queue_manager.h"
  28. #define MQD_SIZE_ALIGNED 768
  29. static const struct kfd_device_info kaveri_device_info = {
  30. .max_pasid_bits = 16,
  31. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  32. .mqd_size_aligned = MQD_SIZE_ALIGNED
  33. };
  34. struct kfd_deviceid {
  35. unsigned short did;
  36. const struct kfd_device_info *device_info;
  37. };
  38. /* Please keep this sorted by increasing device id. */
  39. static const struct kfd_deviceid supported_devices[] = {
  40. { 0x1304, &kaveri_device_info }, /* Kaveri */
  41. { 0x1305, &kaveri_device_info }, /* Kaveri */
  42. { 0x1306, &kaveri_device_info }, /* Kaveri */
  43. { 0x1307, &kaveri_device_info }, /* Kaveri */
  44. { 0x1309, &kaveri_device_info }, /* Kaveri */
  45. { 0x130A, &kaveri_device_info }, /* Kaveri */
  46. { 0x130B, &kaveri_device_info }, /* Kaveri */
  47. { 0x130C, &kaveri_device_info }, /* Kaveri */
  48. { 0x130D, &kaveri_device_info }, /* Kaveri */
  49. { 0x130E, &kaveri_device_info }, /* Kaveri */
  50. { 0x130F, &kaveri_device_info }, /* Kaveri */
  51. { 0x1310, &kaveri_device_info }, /* Kaveri */
  52. { 0x1311, &kaveri_device_info }, /* Kaveri */
  53. { 0x1312, &kaveri_device_info }, /* Kaveri */
  54. { 0x1313, &kaveri_device_info }, /* Kaveri */
  55. { 0x1315, &kaveri_device_info }, /* Kaveri */
  56. { 0x1316, &kaveri_device_info }, /* Kaveri */
  57. { 0x1317, &kaveri_device_info }, /* Kaveri */
  58. { 0x1318, &kaveri_device_info }, /* Kaveri */
  59. { 0x131B, &kaveri_device_info }, /* Kaveri */
  60. { 0x131C, &kaveri_device_info }, /* Kaveri */
  61. { 0x131D, &kaveri_device_info }, /* Kaveri */
  62. };
  63. static const struct kfd_device_info *lookup_device_info(unsigned short did)
  64. {
  65. size_t i;
  66. for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
  67. if (supported_devices[i].did == did) {
  68. BUG_ON(supported_devices[i].device_info == NULL);
  69. return supported_devices[i].device_info;
  70. }
  71. }
  72. return NULL;
  73. }
  74. struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev)
  75. {
  76. struct kfd_dev *kfd;
  77. const struct kfd_device_info *device_info =
  78. lookup_device_info(pdev->device);
  79. if (!device_info)
  80. return NULL;
  81. kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
  82. if (!kfd)
  83. return NULL;
  84. kfd->kgd = kgd;
  85. kfd->device_info = device_info;
  86. kfd->pdev = pdev;
  87. kfd->init_complete = false;
  88. return kfd;
  89. }
  90. static bool device_iommu_pasid_init(struct kfd_dev *kfd)
  91. {
  92. const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
  93. AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
  94. AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  95. struct amd_iommu_device_info iommu_info;
  96. unsigned int pasid_limit;
  97. int err;
  98. err = amd_iommu_device_info(kfd->pdev, &iommu_info);
  99. if (err < 0) {
  100. dev_err(kfd_device,
  101. "error getting iommu info. is the iommu enabled?\n");
  102. return false;
  103. }
  104. if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
  105. dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
  106. (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
  107. (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
  108. (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
  109. return false;
  110. }
  111. pasid_limit = min_t(unsigned int,
  112. (unsigned int)1 << kfd->device_info->max_pasid_bits,
  113. iommu_info.max_pasids);
  114. /*
  115. * last pasid is used for kernel queues doorbells
  116. * in the future the last pasid might be used for a kernel thread.
  117. */
  118. pasid_limit = min_t(unsigned int,
  119. pasid_limit,
  120. kfd->doorbell_process_limit - 1);
  121. err = amd_iommu_init_device(kfd->pdev, pasid_limit);
  122. if (err < 0) {
  123. dev_err(kfd_device, "error initializing iommu device\n");
  124. return false;
  125. }
  126. if (!kfd_set_pasid_limit(pasid_limit)) {
  127. dev_err(kfd_device, "error setting pasid limit\n");
  128. amd_iommu_free_device(kfd->pdev);
  129. return false;
  130. }
  131. return true;
  132. }
  133. static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
  134. {
  135. struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
  136. if (dev)
  137. kfd_unbind_process_from_device(dev, pasid);
  138. }
  139. bool kgd2kfd_device_init(struct kfd_dev *kfd,
  140. const struct kgd2kfd_shared_resources *gpu_resources)
  141. {
  142. unsigned int size;
  143. kfd->shared_resources = *gpu_resources;
  144. /* calculate max size of mqds needed for queues */
  145. size = max_num_of_processes *
  146. max_num_of_queues_per_process *
  147. kfd->device_info->mqd_size_aligned;
  148. /* add another 512KB for all other allocations on gart */
  149. size += 512 * 1024;
  150. if (kfd2kgd->init_sa_manager(kfd->kgd, size)) {
  151. dev_err(kfd_device,
  152. "Error initializing sa manager for device (%x:%x)\n",
  153. kfd->pdev->vendor, kfd->pdev->device);
  154. goto out;
  155. }
  156. kfd_doorbell_init(kfd);
  157. if (kfd_topology_add_device(kfd) != 0) {
  158. dev_err(kfd_device,
  159. "Error adding device (%x:%x) to topology\n",
  160. kfd->pdev->vendor, kfd->pdev->device);
  161. goto kfd_topology_add_device_error;
  162. }
  163. if (!device_iommu_pasid_init(kfd)) {
  164. dev_err(kfd_device,
  165. "Error initializing iommuv2 for device (%x:%x)\n",
  166. kfd->pdev->vendor, kfd->pdev->device);
  167. goto device_iommu_pasid_error;
  168. }
  169. amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
  170. iommu_pasid_shutdown_callback);
  171. kfd->dqm = device_queue_manager_init(kfd);
  172. if (!kfd->dqm) {
  173. dev_err(kfd_device,
  174. "Error initializing queue manager for device (%x:%x)\n",
  175. kfd->pdev->vendor, kfd->pdev->device);
  176. goto device_queue_manager_error;
  177. }
  178. if (kfd->dqm->start(kfd->dqm) != 0) {
  179. dev_err(kfd_device,
  180. "Error starting queuen manager for device (%x:%x)\n",
  181. kfd->pdev->vendor, kfd->pdev->device);
  182. goto dqm_start_error;
  183. }
  184. kfd->init_complete = true;
  185. dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
  186. kfd->pdev->device);
  187. pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
  188. sched_policy);
  189. goto out;
  190. dqm_start_error:
  191. device_queue_manager_uninit(kfd->dqm);
  192. device_queue_manager_error:
  193. amd_iommu_free_device(kfd->pdev);
  194. device_iommu_pasid_error:
  195. kfd_topology_remove_device(kfd);
  196. kfd_topology_add_device_error:
  197. kfd2kgd->fini_sa_manager(kfd->kgd);
  198. dev_err(kfd_device,
  199. "device (%x:%x) NOT added due to errors\n",
  200. kfd->pdev->vendor, kfd->pdev->device);
  201. out:
  202. return kfd->init_complete;
  203. }
  204. void kgd2kfd_device_exit(struct kfd_dev *kfd)
  205. {
  206. if (kfd->init_complete) {
  207. device_queue_manager_uninit(kfd->dqm);
  208. amd_iommu_free_device(kfd->pdev);
  209. kfd_topology_remove_device(kfd);
  210. }
  211. kfree(kfd);
  212. }
  213. void kgd2kfd_suspend(struct kfd_dev *kfd)
  214. {
  215. BUG_ON(kfd == NULL);
  216. if (kfd->init_complete) {
  217. kfd->dqm->stop(kfd->dqm);
  218. amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
  219. amd_iommu_free_device(kfd->pdev);
  220. }
  221. }
  222. int kgd2kfd_resume(struct kfd_dev *kfd)
  223. {
  224. unsigned int pasid_limit;
  225. int err;
  226. BUG_ON(kfd == NULL);
  227. pasid_limit = kfd_get_pasid_limit();
  228. if (kfd->init_complete) {
  229. err = amd_iommu_init_device(kfd->pdev, pasid_limit);
  230. if (err < 0)
  231. return -ENXIO;
  232. amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
  233. iommu_pasid_shutdown_callback);
  234. kfd->dqm->start(kfd->dqm);
  235. }
  236. return 0;
  237. }
  238. /* This is called directly from KGD at ISR. */
  239. void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
  240. {
  241. /* Process interrupts / schedule work as necessary */
  242. }