amdgpu_object.c 25 KB

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