amdgpu_object.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  30. * Dave Airlie
  31. */
  32. #include <linux/list.h>
  33. #include <linux/slab.h>
  34. #include <drm/drmP.h>
  35. #include <drm/amdgpu_drm.h>
  36. #include <drm/drm_cache.h>
  37. #include "amdgpu.h"
  38. #include "amdgpu_trace.h"
  39. #include "amdgpu_amdkfd.h"
  40. static bool amdgpu_need_backup(struct amdgpu_device *adev)
  41. {
  42. if (adev->flags & AMD_IS_APU)
  43. return false;
  44. if (amdgpu_gpu_recovery == 0 ||
  45. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))
  46. return false;
  47. return true;
  48. }
  49. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  50. {
  51. struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
  52. struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
  53. if (bo->kfd_bo)
  54. amdgpu_amdkfd_unreserve_system_memory_limit(bo);
  55. amdgpu_bo_kunmap(bo);
  56. if (bo->gem_base.import_attach)
  57. drm_prime_gem_destroy(&bo->gem_base, bo->tbo.sg);
  58. drm_gem_object_release(&bo->gem_base);
  59. amdgpu_bo_unref(&bo->parent);
  60. if (!list_empty(&bo->shadow_list)) {
  61. mutex_lock(&adev->shadow_list_lock);
  62. list_del_init(&bo->shadow_list);
  63. mutex_unlock(&adev->shadow_list_lock);
  64. }
  65. kfree(bo->metadata);
  66. kfree(bo);
  67. }
  68. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  69. {
  70. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  71. return true;
  72. return false;
  73. }
  74. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
  75. {
  76. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  77. struct ttm_placement *placement = &abo->placement;
  78. struct ttm_place *places = abo->placements;
  79. u64 flags = abo->flags;
  80. u32 c = 0;
  81. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  82. unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
  83. places[c].fpfn = 0;
  84. places[c].lpfn = 0;
  85. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  86. TTM_PL_FLAG_VRAM;
  87. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  88. places[c].lpfn = visible_pfn;
  89. else
  90. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  91. if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  92. places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
  93. c++;
  94. }
  95. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  96. places[c].fpfn = 0;
  97. if (flags & AMDGPU_GEM_CREATE_SHADOW)
  98. places[c].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
  99. else
  100. places[c].lpfn = 0;
  101. places[c].flags = TTM_PL_FLAG_TT;
  102. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  103. places[c].flags |= TTM_PL_FLAG_WC |
  104. TTM_PL_FLAG_UNCACHED;
  105. else
  106. places[c].flags |= TTM_PL_FLAG_CACHED;
  107. c++;
  108. }
  109. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  110. places[c].fpfn = 0;
  111. places[c].lpfn = 0;
  112. places[c].flags = TTM_PL_FLAG_SYSTEM;
  113. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  114. places[c].flags |= TTM_PL_FLAG_WC |
  115. TTM_PL_FLAG_UNCACHED;
  116. else
  117. places[c].flags |= TTM_PL_FLAG_CACHED;
  118. c++;
  119. }
  120. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  121. places[c].fpfn = 0;
  122. places[c].lpfn = 0;
  123. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  124. c++;
  125. }
  126. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  127. places[c].fpfn = 0;
  128. places[c].lpfn = 0;
  129. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  130. c++;
  131. }
  132. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  133. places[c].fpfn = 0;
  134. places[c].lpfn = 0;
  135. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  136. c++;
  137. }
  138. if (!c) {
  139. places[c].fpfn = 0;
  140. places[c].lpfn = 0;
  141. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  142. c++;
  143. }
  144. placement->num_placement = c;
  145. placement->placement = places;
  146. placement->num_busy_placement = c;
  147. placement->busy_placement = places;
  148. }
  149. /**
  150. * amdgpu_bo_create_reserved - create reserved BO for kernel use
  151. *
  152. * @adev: amdgpu device object
  153. * @size: size for the new BO
  154. * @align: alignment for the new BO
  155. * @domain: where to place it
  156. * @bo_ptr: used to initialize BOs in structures
  157. * @gpu_addr: GPU addr of the pinned BO
  158. * @cpu_addr: optional CPU address mapping
  159. *
  160. * Allocates and pins a BO for kernel internal use, and returns it still
  161. * reserved.
  162. *
  163. * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
  164. *
  165. * Returns 0 on success, negative error code otherwise.
  166. */
  167. int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
  168. unsigned long size, int align,
  169. u32 domain, struct amdgpu_bo **bo_ptr,
  170. u64 *gpu_addr, void **cpu_addr)
  171. {
  172. bool free = false;
  173. int r;
  174. if (!*bo_ptr) {
  175. r = amdgpu_bo_create(adev, size, align, domain,
  176. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  177. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  178. ttm_bo_type_kernel, NULL, bo_ptr);
  179. if (r) {
  180. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
  181. r);
  182. return r;
  183. }
  184. free = true;
  185. }
  186. r = amdgpu_bo_reserve(*bo_ptr, false);
  187. if (r) {
  188. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  189. goto error_free;
  190. }
  191. r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
  192. if (r) {
  193. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  194. goto error_unreserve;
  195. }
  196. if (cpu_addr) {
  197. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  198. if (r) {
  199. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  200. goto error_unreserve;
  201. }
  202. }
  203. return 0;
  204. error_unreserve:
  205. amdgpu_bo_unreserve(*bo_ptr);
  206. error_free:
  207. if (free)
  208. amdgpu_bo_unref(bo_ptr);
  209. return r;
  210. }
  211. /**
  212. * amdgpu_bo_create_kernel - create BO for kernel use
  213. *
  214. * @adev: amdgpu device object
  215. * @size: size for the new BO
  216. * @align: alignment for the new BO
  217. * @domain: where to place it
  218. * @bo_ptr: used to initialize BOs in structures
  219. * @gpu_addr: GPU addr of the pinned BO
  220. * @cpu_addr: optional CPU address mapping
  221. *
  222. * Allocates and pins a BO for kernel internal use.
  223. *
  224. * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
  225. *
  226. * Returns 0 on success, negative error code otherwise.
  227. */
  228. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  229. unsigned long size, int align,
  230. u32 domain, struct amdgpu_bo **bo_ptr,
  231. u64 *gpu_addr, void **cpu_addr)
  232. {
  233. int r;
  234. r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
  235. gpu_addr, cpu_addr);
  236. if (r)
  237. return r;
  238. amdgpu_bo_unreserve(*bo_ptr);
  239. return 0;
  240. }
  241. /**
  242. * amdgpu_bo_free_kernel - free BO for kernel use
  243. *
  244. * @bo: amdgpu BO to free
  245. *
  246. * unmaps and unpin a BO for kernel internal use.
  247. */
  248. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  249. void **cpu_addr)
  250. {
  251. if (*bo == NULL)
  252. return;
  253. if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
  254. if (cpu_addr)
  255. amdgpu_bo_kunmap(*bo);
  256. amdgpu_bo_unpin(*bo);
  257. amdgpu_bo_unreserve(*bo);
  258. }
  259. amdgpu_bo_unref(bo);
  260. if (gpu_addr)
  261. *gpu_addr = 0;
  262. if (cpu_addr)
  263. *cpu_addr = NULL;
  264. }
  265. /* Validate bo size is bit bigger then the request domain */
  266. static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
  267. unsigned long size, u32 domain)
  268. {
  269. struct ttm_mem_type_manager *man = NULL;
  270. /*
  271. * If GTT is part of requested domains the check must succeed to
  272. * allow fall back to GTT
  273. */
  274. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  275. man = &adev->mman.bdev.man[TTM_PL_TT];
  276. if (size < (man->size << PAGE_SHIFT))
  277. return true;
  278. else
  279. goto fail;
  280. }
  281. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  282. man = &adev->mman.bdev.man[TTM_PL_VRAM];
  283. if (size < (man->size << PAGE_SHIFT))
  284. return true;
  285. else
  286. goto fail;
  287. }
  288. /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
  289. return true;
  290. fail:
  291. DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
  292. man->size << PAGE_SHIFT);
  293. return false;
  294. }
  295. static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  296. struct amdgpu_bo_param *bp,
  297. struct amdgpu_bo **bo_ptr)
  298. {
  299. struct ttm_operation_ctx ctx = {
  300. .interruptible = (bp->type != ttm_bo_type_kernel),
  301. .no_wait_gpu = false,
  302. .resv = bp->resv,
  303. .flags = TTM_OPT_FLAG_ALLOW_RES_EVICT
  304. };
  305. struct amdgpu_bo *bo;
  306. unsigned long page_align, size = bp->size;
  307. size_t acc_size;
  308. int r;
  309. page_align = roundup(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  310. size = ALIGN(size, PAGE_SIZE);
  311. if (!amdgpu_bo_validate_size(adev, size, bp->domain))
  312. return -ENOMEM;
  313. *bo_ptr = NULL;
  314. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  315. sizeof(struct amdgpu_bo));
  316. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  317. if (bo == NULL)
  318. return -ENOMEM;
  319. drm_gem_private_object_init(adev->ddev, &bo->gem_base, size);
  320. INIT_LIST_HEAD(&bo->shadow_list);
  321. INIT_LIST_HEAD(&bo->va);
  322. bo->preferred_domains = bp->domain & (AMDGPU_GEM_DOMAIN_VRAM |
  323. AMDGPU_GEM_DOMAIN_GTT |
  324. AMDGPU_GEM_DOMAIN_CPU |
  325. AMDGPU_GEM_DOMAIN_GDS |
  326. AMDGPU_GEM_DOMAIN_GWS |
  327. AMDGPU_GEM_DOMAIN_OA);
  328. bo->allowed_domains = bo->preferred_domains;
  329. if (bp->type != ttm_bo_type_kernel &&
  330. bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  331. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  332. bo->flags = bp->flags;
  333. #ifdef CONFIG_X86_32
  334. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  335. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  336. */
  337. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  338. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  339. /* Don't try to enable write-combining when it can't work, or things
  340. * may be slow
  341. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  342. */
  343. #ifndef CONFIG_COMPILE_TEST
  344. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  345. thanks to write-combining
  346. #endif
  347. if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  348. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  349. "better performance thanks to write-combining\n");
  350. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  351. #else
  352. /* For architectures that don't support WC memory,
  353. * mask out the WC flag from the BO
  354. */
  355. if (!drm_arch_can_wc_memory())
  356. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  357. #endif
  358. bo->tbo.bdev = &adev->mman.bdev;
  359. amdgpu_ttm_placement_from_domain(bo, bp->domain);
  360. r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
  361. &bo->placement, page_align, &ctx, acc_size,
  362. NULL, bp->resv, &amdgpu_ttm_bo_destroy);
  363. if (unlikely(r != 0))
  364. return r;
  365. if (adev->gmc.visible_vram_size < adev->gmc.real_vram_size &&
  366. bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  367. bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
  368. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
  369. ctx.bytes_moved);
  370. else
  371. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
  372. if (bp->type == ttm_bo_type_kernel)
  373. bo->tbo.priority = 1;
  374. if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  375. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  376. struct dma_fence *fence;
  377. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  378. if (unlikely(r))
  379. goto fail_unreserve;
  380. amdgpu_bo_fence(bo, fence, false);
  381. dma_fence_put(bo->tbo.moving);
  382. bo->tbo.moving = dma_fence_get(fence);
  383. dma_fence_put(fence);
  384. }
  385. if (!bp->resv)
  386. amdgpu_bo_unreserve(bo);
  387. *bo_ptr = bo;
  388. trace_amdgpu_bo_create(bo);
  389. /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
  390. if (bp->type == ttm_bo_type_device)
  391. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  392. return 0;
  393. fail_unreserve:
  394. if (!bp->resv)
  395. ww_mutex_unlock(&bo->tbo.resv->lock);
  396. amdgpu_bo_unref(&bo);
  397. return r;
  398. }
  399. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  400. unsigned long size, int byte_align,
  401. struct amdgpu_bo *bo)
  402. {
  403. struct amdgpu_bo_param bp = {
  404. .size = size,
  405. .byte_align = byte_align,
  406. .domain = AMDGPU_GEM_DOMAIN_GTT,
  407. .flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC |
  408. AMDGPU_GEM_CREATE_SHADOW,
  409. .type = ttm_bo_type_kernel,
  410. .resv = bo->tbo.resv
  411. };
  412. int r;
  413. if (bo->shadow)
  414. return 0;
  415. r = amdgpu_bo_do_create(adev, &bp, &bo->shadow);
  416. if (!r) {
  417. bo->shadow->parent = amdgpu_bo_ref(bo);
  418. mutex_lock(&adev->shadow_list_lock);
  419. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  420. mutex_unlock(&adev->shadow_list_lock);
  421. }
  422. return r;
  423. }
  424. int amdgpu_bo_create(struct amdgpu_device *adev, unsigned long size,
  425. int byte_align, u32 domain,
  426. u64 flags, enum ttm_bo_type type,
  427. struct reservation_object *resv,
  428. struct amdgpu_bo **bo_ptr)
  429. {
  430. struct amdgpu_bo_param bp = {
  431. .size = size,
  432. .byte_align = byte_align,
  433. .domain = domain,
  434. .flags = flags & ~AMDGPU_GEM_CREATE_SHADOW,
  435. .type = type,
  436. .resv = resv
  437. };
  438. int r;
  439. r = amdgpu_bo_do_create(adev, &bp, bo_ptr);
  440. if (r)
  441. return r;
  442. if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
  443. if (!resv)
  444. WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
  445. NULL));
  446. r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
  447. if (!resv)
  448. reservation_object_unlock((*bo_ptr)->tbo.resv);
  449. if (r)
  450. amdgpu_bo_unref(bo_ptr);
  451. }
  452. return r;
  453. }
  454. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  455. struct amdgpu_ring *ring,
  456. struct amdgpu_bo *bo,
  457. struct reservation_object *resv,
  458. struct dma_fence **fence,
  459. bool direct)
  460. {
  461. struct amdgpu_bo *shadow = bo->shadow;
  462. uint64_t bo_addr, shadow_addr;
  463. int r;
  464. if (!shadow)
  465. return -EINVAL;
  466. bo_addr = amdgpu_bo_gpu_offset(bo);
  467. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  468. r = reservation_object_reserve_shared(bo->tbo.resv);
  469. if (r)
  470. goto err;
  471. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  472. amdgpu_bo_size(bo), resv, fence,
  473. direct, false);
  474. if (!r)
  475. amdgpu_bo_fence(bo, *fence, true);
  476. err:
  477. return r;
  478. }
  479. int amdgpu_bo_validate(struct amdgpu_bo *bo)
  480. {
  481. struct ttm_operation_ctx ctx = { false, false };
  482. uint32_t domain;
  483. int r;
  484. if (bo->pin_count)
  485. return 0;
  486. domain = bo->preferred_domains;
  487. retry:
  488. amdgpu_ttm_placement_from_domain(bo, domain);
  489. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  490. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  491. domain = bo->allowed_domains;
  492. goto retry;
  493. }
  494. return r;
  495. }
  496. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  497. struct amdgpu_ring *ring,
  498. struct amdgpu_bo *bo,
  499. struct reservation_object *resv,
  500. struct dma_fence **fence,
  501. bool direct)
  502. {
  503. struct amdgpu_bo *shadow = bo->shadow;
  504. uint64_t bo_addr, shadow_addr;
  505. int r;
  506. if (!shadow)
  507. return -EINVAL;
  508. bo_addr = amdgpu_bo_gpu_offset(bo);
  509. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  510. r = reservation_object_reserve_shared(bo->tbo.resv);
  511. if (r)
  512. goto err;
  513. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  514. amdgpu_bo_size(bo), resv, fence,
  515. direct, false);
  516. if (!r)
  517. amdgpu_bo_fence(bo, *fence, true);
  518. err:
  519. return r;
  520. }
  521. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  522. {
  523. void *kptr;
  524. long r;
  525. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  526. return -EPERM;
  527. kptr = amdgpu_bo_kptr(bo);
  528. if (kptr) {
  529. if (ptr)
  530. *ptr = kptr;
  531. return 0;
  532. }
  533. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  534. MAX_SCHEDULE_TIMEOUT);
  535. if (r < 0)
  536. return r;
  537. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  538. if (r)
  539. return r;
  540. if (ptr)
  541. *ptr = amdgpu_bo_kptr(bo);
  542. return 0;
  543. }
  544. void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
  545. {
  546. bool is_iomem;
  547. return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  548. }
  549. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  550. {
  551. if (bo->kmap.bo)
  552. ttm_bo_kunmap(&bo->kmap);
  553. }
  554. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  555. {
  556. if (bo == NULL)
  557. return NULL;
  558. ttm_bo_reference(&bo->tbo);
  559. return bo;
  560. }
  561. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  562. {
  563. struct ttm_buffer_object *tbo;
  564. if ((*bo) == NULL)
  565. return;
  566. tbo = &((*bo)->tbo);
  567. ttm_bo_unref(&tbo);
  568. if (tbo == NULL)
  569. *bo = NULL;
  570. }
  571. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  572. u64 min_offset, u64 max_offset,
  573. u64 *gpu_addr)
  574. {
  575. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  576. struct ttm_operation_ctx ctx = { false, false };
  577. int r, i;
  578. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  579. return -EPERM;
  580. if (WARN_ON_ONCE(min_offset > max_offset))
  581. return -EINVAL;
  582. /* A shared bo cannot be migrated to VRAM */
  583. if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM))
  584. return -EINVAL;
  585. if (bo->pin_count) {
  586. uint32_t mem_type = bo->tbo.mem.mem_type;
  587. if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
  588. return -EINVAL;
  589. bo->pin_count++;
  590. if (gpu_addr)
  591. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  592. if (max_offset != 0) {
  593. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  594. WARN_ON_ONCE(max_offset <
  595. (amdgpu_bo_gpu_offset(bo) - domain_start));
  596. }
  597. return 0;
  598. }
  599. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  600. /* force to pin into visible video ram */
  601. if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
  602. bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  603. amdgpu_ttm_placement_from_domain(bo, domain);
  604. for (i = 0; i < bo->placement.num_placement; i++) {
  605. unsigned fpfn, lpfn;
  606. fpfn = min_offset >> PAGE_SHIFT;
  607. lpfn = max_offset >> PAGE_SHIFT;
  608. if (fpfn > bo->placements[i].fpfn)
  609. bo->placements[i].fpfn = fpfn;
  610. if (!bo->placements[i].lpfn ||
  611. (lpfn && lpfn < bo->placements[i].lpfn))
  612. bo->placements[i].lpfn = lpfn;
  613. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  614. }
  615. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  616. if (unlikely(r)) {
  617. dev_err(adev->dev, "%p pin failed\n", bo);
  618. goto error;
  619. }
  620. r = amdgpu_ttm_alloc_gart(&bo->tbo);
  621. if (unlikely(r)) {
  622. dev_err(adev->dev, "%p bind failed\n", bo);
  623. goto error;
  624. }
  625. bo->pin_count = 1;
  626. if (gpu_addr != NULL)
  627. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  628. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  629. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  630. adev->vram_pin_size += amdgpu_bo_size(bo);
  631. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  632. adev->invisible_pin_size += amdgpu_bo_size(bo);
  633. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  634. adev->gart_pin_size += amdgpu_bo_size(bo);
  635. }
  636. error:
  637. return r;
  638. }
  639. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  640. {
  641. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  642. }
  643. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  644. {
  645. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  646. struct ttm_operation_ctx ctx = { false, false };
  647. int r, i;
  648. if (!bo->pin_count) {
  649. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  650. return 0;
  651. }
  652. bo->pin_count--;
  653. if (bo->pin_count)
  654. return 0;
  655. for (i = 0; i < bo->placement.num_placement; i++) {
  656. bo->placements[i].lpfn = 0;
  657. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  658. }
  659. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  660. if (unlikely(r)) {
  661. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  662. goto error;
  663. }
  664. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  665. adev->vram_pin_size -= amdgpu_bo_size(bo);
  666. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  667. adev->invisible_pin_size -= amdgpu_bo_size(bo);
  668. } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  669. adev->gart_pin_size -= amdgpu_bo_size(bo);
  670. }
  671. error:
  672. return r;
  673. }
  674. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  675. {
  676. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  677. if (0 && (adev->flags & AMD_IS_APU)) {
  678. /* Useless to evict on IGP chips */
  679. return 0;
  680. }
  681. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  682. }
  683. static const char *amdgpu_vram_names[] = {
  684. "UNKNOWN",
  685. "GDDR1",
  686. "DDR2",
  687. "GDDR3",
  688. "GDDR4",
  689. "GDDR5",
  690. "HBM",
  691. "DDR3",
  692. "DDR4",
  693. };
  694. int amdgpu_bo_init(struct amdgpu_device *adev)
  695. {
  696. /* reserve PAT memory space to WC for VRAM */
  697. arch_io_reserve_memtype_wc(adev->gmc.aper_base,
  698. adev->gmc.aper_size);
  699. /* Add an MTRR for the VRAM */
  700. adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
  701. adev->gmc.aper_size);
  702. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  703. adev->gmc.mc_vram_size >> 20,
  704. (unsigned long long)adev->gmc.aper_size >> 20);
  705. DRM_INFO("RAM width %dbits %s\n",
  706. adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
  707. return amdgpu_ttm_init(adev);
  708. }
  709. int amdgpu_bo_late_init(struct amdgpu_device *adev)
  710. {
  711. amdgpu_ttm_late_init(adev);
  712. return 0;
  713. }
  714. void amdgpu_bo_fini(struct amdgpu_device *adev)
  715. {
  716. amdgpu_ttm_fini(adev);
  717. arch_phys_wc_del(adev->gmc.vram_mtrr);
  718. arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
  719. }
  720. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  721. struct vm_area_struct *vma)
  722. {
  723. return ttm_fbdev_mmap(vma, &bo->tbo);
  724. }
  725. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  726. {
  727. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  728. if (adev->family <= AMDGPU_FAMILY_CZ &&
  729. AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  730. return -EINVAL;
  731. bo->tiling_flags = tiling_flags;
  732. return 0;
  733. }
  734. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  735. {
  736. lockdep_assert_held(&bo->tbo.resv->lock.base);
  737. if (tiling_flags)
  738. *tiling_flags = bo->tiling_flags;
  739. }
  740. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  741. uint32_t metadata_size, uint64_t flags)
  742. {
  743. void *buffer;
  744. if (!metadata_size) {
  745. if (bo->metadata_size) {
  746. kfree(bo->metadata);
  747. bo->metadata = NULL;
  748. bo->metadata_size = 0;
  749. }
  750. return 0;
  751. }
  752. if (metadata == NULL)
  753. return -EINVAL;
  754. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  755. if (buffer == NULL)
  756. return -ENOMEM;
  757. kfree(bo->metadata);
  758. bo->metadata_flags = flags;
  759. bo->metadata = buffer;
  760. bo->metadata_size = metadata_size;
  761. return 0;
  762. }
  763. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  764. size_t buffer_size, uint32_t *metadata_size,
  765. uint64_t *flags)
  766. {
  767. if (!buffer && !metadata_size)
  768. return -EINVAL;
  769. if (buffer) {
  770. if (buffer_size < bo->metadata_size)
  771. return -EINVAL;
  772. if (bo->metadata_size)
  773. memcpy(buffer, bo->metadata, bo->metadata_size);
  774. }
  775. if (metadata_size)
  776. *metadata_size = bo->metadata_size;
  777. if (flags)
  778. *flags = bo->metadata_flags;
  779. return 0;
  780. }
  781. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  782. bool evict,
  783. struct ttm_mem_reg *new_mem)
  784. {
  785. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  786. struct amdgpu_bo *abo;
  787. struct ttm_mem_reg *old_mem = &bo->mem;
  788. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  789. return;
  790. abo = ttm_to_amdgpu_bo(bo);
  791. amdgpu_vm_bo_invalidate(adev, abo, evict);
  792. amdgpu_bo_kunmap(abo);
  793. /* remember the eviction */
  794. if (evict)
  795. atomic64_inc(&adev->num_evictions);
  796. /* update statistics */
  797. if (!new_mem)
  798. return;
  799. /* move_notify is called before move happens */
  800. trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
  801. }
  802. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  803. {
  804. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  805. struct ttm_operation_ctx ctx = { false, false };
  806. struct amdgpu_bo *abo;
  807. unsigned long offset, size;
  808. int r;
  809. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  810. return 0;
  811. abo = ttm_to_amdgpu_bo(bo);
  812. /* Remember that this BO was accessed by the CPU */
  813. abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  814. if (bo->mem.mem_type != TTM_PL_VRAM)
  815. return 0;
  816. size = bo->mem.num_pages << PAGE_SHIFT;
  817. offset = bo->mem.start << PAGE_SHIFT;
  818. if ((offset + size) <= adev->gmc.visible_vram_size)
  819. return 0;
  820. /* Can't move a pinned BO to visible VRAM */
  821. if (abo->pin_count > 0)
  822. return -EINVAL;
  823. /* hurrah the memory is not visible ! */
  824. atomic64_inc(&adev->num_vram_cpu_page_faults);
  825. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
  826. AMDGPU_GEM_DOMAIN_GTT);
  827. /* Avoid costly evictions; only set GTT as a busy placement */
  828. abo->placement.num_busy_placement = 1;
  829. abo->placement.busy_placement = &abo->placements[1];
  830. r = ttm_bo_validate(bo, &abo->placement, &ctx);
  831. if (unlikely(r != 0))
  832. return r;
  833. offset = bo->mem.start << PAGE_SHIFT;
  834. /* this should never happen */
  835. if (bo->mem.mem_type == TTM_PL_VRAM &&
  836. (offset + size) > adev->gmc.visible_vram_size)
  837. return -EINVAL;
  838. return 0;
  839. }
  840. /**
  841. * amdgpu_bo_fence - add fence to buffer object
  842. *
  843. * @bo: buffer object in question
  844. * @fence: fence to add
  845. * @shared: true if fence should be added shared
  846. *
  847. */
  848. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  849. bool shared)
  850. {
  851. struct reservation_object *resv = bo->tbo.resv;
  852. if (shared)
  853. reservation_object_add_shared_fence(resv, fence);
  854. else
  855. reservation_object_add_excl_fence(resv, fence);
  856. }
  857. /**
  858. * amdgpu_bo_gpu_offset - return GPU offset of bo
  859. * @bo: amdgpu object for which we query the offset
  860. *
  861. * Returns current GPU offset of the object.
  862. *
  863. * Note: object should either be pinned or reserved when calling this
  864. * function, it might be useful to add check for this for debugging.
  865. */
  866. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  867. {
  868. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  869. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
  870. !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem));
  871. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  872. !bo->pin_count);
  873. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  874. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  875. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  876. return bo->tbo.offset;
  877. }