amdgpu_gfx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include <drm/drmP.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_gfx.h"
  28. /* 0.5 second timeout */
  29. #define GFX_OFF_DELAY_ENABLE msecs_to_jiffies(500)
  30. /*
  31. * GPU scratch registers helpers function.
  32. */
  33. /**
  34. * amdgpu_gfx_scratch_get - Allocate a scratch register
  35. *
  36. * @adev: amdgpu_device pointer
  37. * @reg: scratch register mmio offset
  38. *
  39. * Allocate a CP scratch register for use by the driver (all asics).
  40. * Returns 0 on success or -EINVAL on failure.
  41. */
  42. int amdgpu_gfx_scratch_get(struct amdgpu_device *adev, uint32_t *reg)
  43. {
  44. int i;
  45. i = ffs(adev->gfx.scratch.free_mask);
  46. if (i != 0 && i <= adev->gfx.scratch.num_reg) {
  47. i--;
  48. adev->gfx.scratch.free_mask &= ~(1u << i);
  49. *reg = adev->gfx.scratch.reg_base + i;
  50. return 0;
  51. }
  52. return -EINVAL;
  53. }
  54. /**
  55. * amdgpu_gfx_scratch_free - Free a scratch register
  56. *
  57. * @adev: amdgpu_device pointer
  58. * @reg: scratch register mmio offset
  59. *
  60. * Free a CP scratch register allocated for use by the driver (all asics)
  61. */
  62. void amdgpu_gfx_scratch_free(struct amdgpu_device *adev, uint32_t reg)
  63. {
  64. adev->gfx.scratch.free_mask |= 1u << (reg - adev->gfx.scratch.reg_base);
  65. }
  66. /**
  67. * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
  68. *
  69. * @mask: array in which the per-shader array disable masks will be stored
  70. * @max_se: number of SEs
  71. * @max_sh: number of SHs
  72. *
  73. * The bitmask of CUs to be disabled in the shader array determined by se and
  74. * sh is stored in mask[se * max_sh + sh].
  75. */
  76. void amdgpu_gfx_parse_disable_cu(unsigned *mask, unsigned max_se, unsigned max_sh)
  77. {
  78. unsigned se, sh, cu;
  79. const char *p;
  80. memset(mask, 0, sizeof(*mask) * max_se * max_sh);
  81. if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
  82. return;
  83. p = amdgpu_disable_cu;
  84. for (;;) {
  85. char *next;
  86. int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
  87. if (ret < 3) {
  88. DRM_ERROR("amdgpu: could not parse disable_cu\n");
  89. return;
  90. }
  91. if (se < max_se && sh < max_sh && cu < 16) {
  92. DRM_INFO("amdgpu: disabling CU %u.%u.%u\n", se, sh, cu);
  93. mask[se * max_sh + sh] |= 1u << cu;
  94. } else {
  95. DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
  96. se, sh, cu);
  97. }
  98. next = strchr(p, ',');
  99. if (!next)
  100. break;
  101. p = next + 1;
  102. }
  103. }
  104. static bool amdgpu_gfx_is_multipipe_capable(struct amdgpu_device *adev)
  105. {
  106. if (amdgpu_compute_multipipe != -1) {
  107. DRM_INFO("amdgpu: forcing compute pipe policy %d\n",
  108. amdgpu_compute_multipipe);
  109. return amdgpu_compute_multipipe == 1;
  110. }
  111. /* FIXME: spreading the queues across pipes causes perf regressions
  112. * on POLARIS11 compute workloads */
  113. if (adev->asic_type == CHIP_POLARIS11)
  114. return false;
  115. return adev->gfx.mec.num_mec > 1;
  116. }
  117. void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
  118. {
  119. int i, queue, pipe, mec;
  120. bool multipipe_policy = amdgpu_gfx_is_multipipe_capable(adev);
  121. /* policy for amdgpu compute queue ownership */
  122. for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
  123. queue = i % adev->gfx.mec.num_queue_per_pipe;
  124. pipe = (i / adev->gfx.mec.num_queue_per_pipe)
  125. % adev->gfx.mec.num_pipe_per_mec;
  126. mec = (i / adev->gfx.mec.num_queue_per_pipe)
  127. / adev->gfx.mec.num_pipe_per_mec;
  128. /* we've run out of HW */
  129. if (mec >= adev->gfx.mec.num_mec)
  130. break;
  131. if (multipipe_policy) {
  132. /* policy: amdgpu owns the first two queues of the first MEC */
  133. if (mec == 0 && queue < 2)
  134. set_bit(i, adev->gfx.mec.queue_bitmap);
  135. } else {
  136. /* policy: amdgpu owns all queues in the first pipe */
  137. if (mec == 0 && pipe == 0)
  138. set_bit(i, adev->gfx.mec.queue_bitmap);
  139. }
  140. }
  141. /* update the number of active compute rings */
  142. adev->gfx.num_compute_rings =
  143. bitmap_weight(adev->gfx.mec.queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  144. /* If you hit this case and edited the policy, you probably just
  145. * need to increase AMDGPU_MAX_COMPUTE_RINGS */
  146. if (WARN_ON(adev->gfx.num_compute_rings > AMDGPU_MAX_COMPUTE_RINGS))
  147. adev->gfx.num_compute_rings = AMDGPU_MAX_COMPUTE_RINGS;
  148. }
  149. static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
  150. struct amdgpu_ring *ring)
  151. {
  152. int queue_bit;
  153. int mec, pipe, queue;
  154. queue_bit = adev->gfx.mec.num_mec
  155. * adev->gfx.mec.num_pipe_per_mec
  156. * adev->gfx.mec.num_queue_per_pipe;
  157. while (queue_bit-- >= 0) {
  158. if (test_bit(queue_bit, adev->gfx.mec.queue_bitmap))
  159. continue;
  160. amdgpu_gfx_bit_to_queue(adev, queue_bit, &mec, &pipe, &queue);
  161. /*
  162. * 1. Using pipes 2/3 from MEC 2 seems cause problems.
  163. * 2. It must use queue id 0, because CGPG_IDLE/SAVE/LOAD/RUN
  164. * only can be issued on queue 0.
  165. */
  166. if ((mec == 1 && pipe > 1) || queue != 0)
  167. continue;
  168. ring->me = mec + 1;
  169. ring->pipe = pipe;
  170. ring->queue = queue;
  171. return 0;
  172. }
  173. dev_err(adev->dev, "Failed to find a queue for KIQ\n");
  174. return -EINVAL;
  175. }
  176. int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
  177. struct amdgpu_ring *ring,
  178. struct amdgpu_irq_src *irq)
  179. {
  180. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  181. int r = 0;
  182. spin_lock_init(&kiq->ring_lock);
  183. r = amdgpu_device_wb_get(adev, &adev->virt.reg_val_offs);
  184. if (r)
  185. return r;
  186. ring->adev = NULL;
  187. ring->ring_obj = NULL;
  188. ring->use_doorbell = true;
  189. ring->doorbell_index = AMDGPU_DOORBELL_KIQ;
  190. r = amdgpu_gfx_kiq_acquire(adev, ring);
  191. if (r)
  192. return r;
  193. ring->eop_gpu_addr = kiq->eop_gpu_addr;
  194. sprintf(ring->name, "kiq_%d.%d.%d", ring->me, ring->pipe, ring->queue);
  195. r = amdgpu_ring_init(adev, ring, 1024,
  196. irq, AMDGPU_CP_KIQ_IRQ_DRIVER0);
  197. if (r)
  198. dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
  199. return r;
  200. }
  201. void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring,
  202. struct amdgpu_irq_src *irq)
  203. {
  204. amdgpu_device_wb_free(ring->adev, ring->adev->virt.reg_val_offs);
  205. amdgpu_ring_fini(ring);
  206. }
  207. void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev)
  208. {
  209. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  210. amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
  211. }
  212. int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
  213. unsigned hpd_size)
  214. {
  215. int r;
  216. u32 *hpd;
  217. struct amdgpu_kiq *kiq = &adev->gfx.kiq;
  218. r = amdgpu_bo_create_kernel(adev, hpd_size, PAGE_SIZE,
  219. AMDGPU_GEM_DOMAIN_GTT, &kiq->eop_obj,
  220. &kiq->eop_gpu_addr, (void **)&hpd);
  221. if (r) {
  222. dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
  223. return r;
  224. }
  225. memset(hpd, 0, hpd_size);
  226. r = amdgpu_bo_reserve(kiq->eop_obj, true);
  227. if (unlikely(r != 0))
  228. dev_warn(adev->dev, "(%d) reserve kiq eop bo failed\n", r);
  229. amdgpu_bo_kunmap(kiq->eop_obj);
  230. amdgpu_bo_unreserve(kiq->eop_obj);
  231. return 0;
  232. }
  233. /* create MQD for each compute queue */
  234. int amdgpu_gfx_compute_mqd_sw_init(struct amdgpu_device *adev,
  235. unsigned mqd_size)
  236. {
  237. struct amdgpu_ring *ring = NULL;
  238. int r, i;
  239. /* create MQD for KIQ */
  240. ring = &adev->gfx.kiq.ring;
  241. if (!ring->mqd_obj) {
  242. /* originaly the KIQ MQD is put in GTT domain, but for SRIOV VRAM domain is a must
  243. * otherwise hypervisor trigger SAVE_VF fail after driver unloaded which mean MQD
  244. * deallocated and gart_unbind, to strict diverage we decide to use VRAM domain for
  245. * KIQ MQD no matter SRIOV or Bare-metal
  246. */
  247. r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
  248. AMDGPU_GEM_DOMAIN_VRAM, &ring->mqd_obj,
  249. &ring->mqd_gpu_addr, &ring->mqd_ptr);
  250. if (r) {
  251. dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
  252. return r;
  253. }
  254. /* prepare MQD backup */
  255. adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS] = kmalloc(mqd_size, GFP_KERNEL);
  256. if (!adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS])
  257. dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
  258. }
  259. /* create MQD for each KCQ */
  260. for (i = 0; i < adev->gfx.num_compute_rings; i++) {
  261. ring = &adev->gfx.compute_ring[i];
  262. if (!ring->mqd_obj) {
  263. r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
  264. AMDGPU_GEM_DOMAIN_GTT, &ring->mqd_obj,
  265. &ring->mqd_gpu_addr, &ring->mqd_ptr);
  266. if (r) {
  267. dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
  268. return r;
  269. }
  270. /* prepare MQD backup */
  271. adev->gfx.mec.mqd_backup[i] = kmalloc(mqd_size, GFP_KERNEL);
  272. if (!adev->gfx.mec.mqd_backup[i])
  273. dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
  274. }
  275. }
  276. return 0;
  277. }
  278. void amdgpu_gfx_compute_mqd_sw_fini(struct amdgpu_device *adev)
  279. {
  280. struct amdgpu_ring *ring = NULL;
  281. int i;
  282. for (i = 0; i < adev->gfx.num_compute_rings; i++) {
  283. ring = &adev->gfx.compute_ring[i];
  284. kfree(adev->gfx.mec.mqd_backup[i]);
  285. amdgpu_bo_free_kernel(&ring->mqd_obj,
  286. &ring->mqd_gpu_addr,
  287. &ring->mqd_ptr);
  288. }
  289. ring = &adev->gfx.kiq.ring;
  290. kfree(adev->gfx.mec.mqd_backup[AMDGPU_MAX_COMPUTE_RINGS]);
  291. amdgpu_bo_free_kernel(&ring->mqd_obj,
  292. &ring->mqd_gpu_addr,
  293. &ring->mqd_ptr);
  294. }
  295. /* amdgpu_gfx_off_ctrl - Handle gfx off feature enable/disable
  296. *
  297. * @adev: amdgpu_device pointer
  298. * @bool enable true: enable gfx off feature, false: disable gfx off feature
  299. *
  300. * 1. gfx off feature will be enabled by gfx ip after gfx cg gp enabled.
  301. * 2. other client can send request to disable gfx off feature, the request should be honored.
  302. * 3. other client can cancel their request of disable gfx off feature
  303. * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
  304. */
  305. void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
  306. {
  307. if (!(adev->powerplay.pp_feature & PP_GFXOFF_MASK))
  308. return;
  309. if (!adev->powerplay.pp_funcs->set_powergating_by_smu)
  310. return;
  311. mutex_lock(&adev->gfx.gfx_off_mutex);
  312. if (!enable)
  313. adev->gfx.gfx_off_req_count++;
  314. else if (adev->gfx.gfx_off_req_count > 0)
  315. adev->gfx.gfx_off_req_count--;
  316. if (enable && !adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
  317. schedule_delayed_work(&adev->gfx.gfx_off_delay_work, GFX_OFF_DELAY_ENABLE);
  318. } else if (!enable && adev->gfx.gfx_off_state) {
  319. if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false))
  320. adev->gfx.gfx_off_state = false;
  321. }
  322. mutex_unlock(&adev->gfx.gfx_off_mutex);
  323. }