amdgpu_object.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. /**
  41. * DOC: amdgpu_object
  42. *
  43. * This defines the interfaces to operate on an &amdgpu_bo buffer object which
  44. * represents memory used by driver (VRAM, system memory, etc.). The driver
  45. * provides DRM/GEM APIs to userspace. DRM/GEM APIs then use these interfaces
  46. * to create/destroy/set buffer object which are then managed by the kernel TTM
  47. * memory manager.
  48. * The interfaces are also used internally by kernel clients, including gfx,
  49. * uvd, etc. for kernel managed allocations used by the GPU.
  50. *
  51. */
  52. static bool amdgpu_need_backup(struct amdgpu_device *adev)
  53. {
  54. if (adev->flags & AMD_IS_APU)
  55. return false;
  56. if (amdgpu_gpu_recovery == 0 ||
  57. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))
  58. return false;
  59. return true;
  60. }
  61. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  62. {
  63. struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
  64. struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
  65. if (bo->kfd_bo)
  66. amdgpu_amdkfd_unreserve_system_memory_limit(bo);
  67. amdgpu_bo_kunmap(bo);
  68. if (bo->gem_base.import_attach)
  69. drm_prime_gem_destroy(&bo->gem_base, bo->tbo.sg);
  70. drm_gem_object_release(&bo->gem_base);
  71. amdgpu_bo_unref(&bo->parent);
  72. if (!list_empty(&bo->shadow_list)) {
  73. mutex_lock(&adev->shadow_list_lock);
  74. list_del_init(&bo->shadow_list);
  75. mutex_unlock(&adev->shadow_list_lock);
  76. }
  77. kfree(bo->metadata);
  78. kfree(bo);
  79. }
  80. /**
  81. * amdgpu_ttm_bo_is_amdgpu_bo - check if the buffer object is an &amdgpu_bo
  82. * @bo: buffer object to be checked
  83. *
  84. * Uses destroy function associated with the object to determine if this is
  85. * an &amdgpu_bo.
  86. *
  87. * Returns:
  88. * true if the object belongs to &amdgpu_bo, false if not.
  89. */
  90. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  91. {
  92. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  93. return true;
  94. return false;
  95. }
  96. /**
  97. * amdgpu_ttm_placement_from_domain - set buffer's placement
  98. * @abo: &amdgpu_bo buffer object whose placement is to be set
  99. * @domain: requested domain
  100. *
  101. * Sets buffer's placement according to requested domain and the buffer's
  102. * flags.
  103. */
  104. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
  105. {
  106. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  107. struct ttm_placement *placement = &abo->placement;
  108. struct ttm_place *places = abo->placements;
  109. u64 flags = abo->flags;
  110. u32 c = 0;
  111. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  112. unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
  113. places[c].fpfn = 0;
  114. places[c].lpfn = 0;
  115. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  116. TTM_PL_FLAG_VRAM;
  117. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  118. places[c].lpfn = visible_pfn;
  119. else
  120. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  121. if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  122. places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
  123. c++;
  124. }
  125. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  126. places[c].fpfn = 0;
  127. if (flags & AMDGPU_GEM_CREATE_SHADOW)
  128. places[c].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
  129. else
  130. places[c].lpfn = 0;
  131. places[c].flags = TTM_PL_FLAG_TT;
  132. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  133. places[c].flags |= TTM_PL_FLAG_WC |
  134. TTM_PL_FLAG_UNCACHED;
  135. else
  136. places[c].flags |= TTM_PL_FLAG_CACHED;
  137. c++;
  138. }
  139. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  140. places[c].fpfn = 0;
  141. places[c].lpfn = 0;
  142. places[c].flags = TTM_PL_FLAG_SYSTEM;
  143. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  144. places[c].flags |= TTM_PL_FLAG_WC |
  145. TTM_PL_FLAG_UNCACHED;
  146. else
  147. places[c].flags |= TTM_PL_FLAG_CACHED;
  148. c++;
  149. }
  150. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  151. places[c].fpfn = 0;
  152. places[c].lpfn = 0;
  153. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  154. c++;
  155. }
  156. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  157. places[c].fpfn = 0;
  158. places[c].lpfn = 0;
  159. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  160. c++;
  161. }
  162. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  163. places[c].fpfn = 0;
  164. places[c].lpfn = 0;
  165. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  166. c++;
  167. }
  168. if (!c) {
  169. places[c].fpfn = 0;
  170. places[c].lpfn = 0;
  171. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  172. c++;
  173. }
  174. placement->num_placement = c;
  175. placement->placement = places;
  176. placement->num_busy_placement = c;
  177. placement->busy_placement = places;
  178. }
  179. /**
  180. * amdgpu_bo_create_reserved - create reserved BO for kernel use
  181. *
  182. * @adev: amdgpu device object
  183. * @size: size for the new BO
  184. * @align: alignment for the new BO
  185. * @domain: where to place it
  186. * @bo_ptr: used to initialize BOs in structures
  187. * @gpu_addr: GPU addr of the pinned BO
  188. * @cpu_addr: optional CPU address mapping
  189. *
  190. * Allocates and pins a BO for kernel internal use, and returns it still
  191. * reserved.
  192. *
  193. * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
  194. *
  195. * Returns:
  196. * 0 on success, negative error code otherwise.
  197. */
  198. int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
  199. unsigned long size, int align,
  200. u32 domain, struct amdgpu_bo **bo_ptr,
  201. u64 *gpu_addr, void **cpu_addr)
  202. {
  203. struct amdgpu_bo_param bp;
  204. bool free = false;
  205. int r;
  206. memset(&bp, 0, sizeof(bp));
  207. bp.size = size;
  208. bp.byte_align = align;
  209. bp.domain = domain;
  210. bp.flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  211. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  212. bp.type = ttm_bo_type_kernel;
  213. bp.resv = NULL;
  214. if (!*bo_ptr) {
  215. r = amdgpu_bo_create(adev, &bp, bo_ptr);
  216. if (r) {
  217. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
  218. r);
  219. return r;
  220. }
  221. free = true;
  222. }
  223. r = amdgpu_bo_reserve(*bo_ptr, false);
  224. if (r) {
  225. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  226. goto error_free;
  227. }
  228. r = amdgpu_bo_pin(*bo_ptr, domain);
  229. if (r) {
  230. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  231. goto error_unreserve;
  232. }
  233. r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
  234. if (r) {
  235. dev_err(adev->dev, "%p bind failed\n", *bo_ptr);
  236. goto error_unpin;
  237. }
  238. if (gpu_addr)
  239. *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
  240. if (cpu_addr) {
  241. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  242. if (r) {
  243. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  244. goto error_unpin;
  245. }
  246. }
  247. return 0;
  248. error_unpin:
  249. amdgpu_bo_unpin(*bo_ptr);
  250. error_unreserve:
  251. amdgpu_bo_unreserve(*bo_ptr);
  252. error_free:
  253. if (free)
  254. amdgpu_bo_unref(bo_ptr);
  255. return r;
  256. }
  257. /**
  258. * amdgpu_bo_create_kernel - create BO for kernel use
  259. *
  260. * @adev: amdgpu device object
  261. * @size: size for the new BO
  262. * @align: alignment for the new BO
  263. * @domain: where to place it
  264. * @bo_ptr: used to initialize BOs in structures
  265. * @gpu_addr: GPU addr of the pinned BO
  266. * @cpu_addr: optional CPU address mapping
  267. *
  268. * Allocates and pins a BO for kernel internal use.
  269. *
  270. * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
  271. *
  272. * Returns:
  273. * 0 on success, negative error code otherwise.
  274. */
  275. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  276. unsigned long size, int align,
  277. u32 domain, struct amdgpu_bo **bo_ptr,
  278. u64 *gpu_addr, void **cpu_addr)
  279. {
  280. int r;
  281. r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
  282. gpu_addr, cpu_addr);
  283. if (r)
  284. return r;
  285. amdgpu_bo_unreserve(*bo_ptr);
  286. return 0;
  287. }
  288. /**
  289. * amdgpu_bo_free_kernel - free BO for kernel use
  290. *
  291. * @bo: amdgpu BO to free
  292. * @gpu_addr: pointer to where the BO's GPU memory space address was stored
  293. * @cpu_addr: pointer to where the BO's CPU memory space address was stored
  294. *
  295. * unmaps and unpin a BO for kernel internal use.
  296. */
  297. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  298. void **cpu_addr)
  299. {
  300. if (*bo == NULL)
  301. return;
  302. if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
  303. if (cpu_addr)
  304. amdgpu_bo_kunmap(*bo);
  305. amdgpu_bo_unpin(*bo);
  306. amdgpu_bo_unreserve(*bo);
  307. }
  308. amdgpu_bo_unref(bo);
  309. if (gpu_addr)
  310. *gpu_addr = 0;
  311. if (cpu_addr)
  312. *cpu_addr = NULL;
  313. }
  314. /* Validate bo size is bit bigger then the request domain */
  315. static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
  316. unsigned long size, u32 domain)
  317. {
  318. struct ttm_mem_type_manager *man = NULL;
  319. /*
  320. * If GTT is part of requested domains the check must succeed to
  321. * allow fall back to GTT
  322. */
  323. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  324. man = &adev->mman.bdev.man[TTM_PL_TT];
  325. if (size < (man->size << PAGE_SHIFT))
  326. return true;
  327. else
  328. goto fail;
  329. }
  330. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  331. man = &adev->mman.bdev.man[TTM_PL_VRAM];
  332. if (size < (man->size << PAGE_SHIFT))
  333. return true;
  334. else
  335. goto fail;
  336. }
  337. /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
  338. return true;
  339. fail:
  340. DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
  341. man->size << PAGE_SHIFT);
  342. return false;
  343. }
  344. static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  345. struct amdgpu_bo_param *bp,
  346. struct amdgpu_bo **bo_ptr)
  347. {
  348. struct ttm_operation_ctx ctx = {
  349. .interruptible = (bp->type != ttm_bo_type_kernel),
  350. .no_wait_gpu = false,
  351. .resv = bp->resv,
  352. .flags = TTM_OPT_FLAG_ALLOW_RES_EVICT
  353. };
  354. struct amdgpu_bo *bo;
  355. unsigned long page_align, size = bp->size;
  356. size_t acc_size;
  357. int r;
  358. page_align = roundup(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  359. size = ALIGN(size, PAGE_SIZE);
  360. if (!amdgpu_bo_validate_size(adev, size, bp->domain))
  361. return -ENOMEM;
  362. *bo_ptr = NULL;
  363. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  364. sizeof(struct amdgpu_bo));
  365. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  366. if (bo == NULL)
  367. return -ENOMEM;
  368. drm_gem_private_object_init(adev->ddev, &bo->gem_base, size);
  369. INIT_LIST_HEAD(&bo->shadow_list);
  370. INIT_LIST_HEAD(&bo->va);
  371. bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain :
  372. bp->domain;
  373. bo->allowed_domains = bo->preferred_domains;
  374. if (bp->type != ttm_bo_type_kernel &&
  375. bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  376. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  377. bo->flags = bp->flags;
  378. #ifdef CONFIG_X86_32
  379. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  380. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  381. */
  382. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  383. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  384. /* Don't try to enable write-combining when it can't work, or things
  385. * may be slow
  386. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  387. */
  388. #ifndef CONFIG_COMPILE_TEST
  389. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  390. thanks to write-combining
  391. #endif
  392. if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  393. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  394. "better performance thanks to write-combining\n");
  395. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  396. #else
  397. /* For architectures that don't support WC memory,
  398. * mask out the WC flag from the BO
  399. */
  400. if (!drm_arch_can_wc_memory())
  401. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  402. #endif
  403. bo->tbo.bdev = &adev->mman.bdev;
  404. amdgpu_ttm_placement_from_domain(bo, bp->domain);
  405. if (bp->type == ttm_bo_type_kernel)
  406. bo->tbo.priority = 1;
  407. r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
  408. &bo->placement, page_align, &ctx, acc_size,
  409. NULL, bp->resv, &amdgpu_ttm_bo_destroy);
  410. if (unlikely(r != 0))
  411. return r;
  412. if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
  413. bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  414. bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
  415. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
  416. ctx.bytes_moved);
  417. else
  418. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
  419. if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  420. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  421. struct dma_fence *fence;
  422. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  423. if (unlikely(r))
  424. goto fail_unreserve;
  425. amdgpu_bo_fence(bo, fence, false);
  426. dma_fence_put(bo->tbo.moving);
  427. bo->tbo.moving = dma_fence_get(fence);
  428. dma_fence_put(fence);
  429. }
  430. if (!bp->resv)
  431. amdgpu_bo_unreserve(bo);
  432. *bo_ptr = bo;
  433. trace_amdgpu_bo_create(bo);
  434. /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
  435. if (bp->type == ttm_bo_type_device)
  436. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  437. return 0;
  438. fail_unreserve:
  439. if (!bp->resv)
  440. ww_mutex_unlock(&bo->tbo.resv->lock);
  441. amdgpu_bo_unref(&bo);
  442. return r;
  443. }
  444. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  445. unsigned long size, int byte_align,
  446. struct amdgpu_bo *bo)
  447. {
  448. struct amdgpu_bo_param bp;
  449. int r;
  450. if (bo->shadow)
  451. return 0;
  452. memset(&bp, 0, sizeof(bp));
  453. bp.size = size;
  454. bp.byte_align = byte_align;
  455. bp.domain = AMDGPU_GEM_DOMAIN_GTT;
  456. bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC |
  457. AMDGPU_GEM_CREATE_SHADOW;
  458. bp.type = ttm_bo_type_kernel;
  459. bp.resv = bo->tbo.resv;
  460. r = amdgpu_bo_do_create(adev, &bp, &bo->shadow);
  461. if (!r) {
  462. bo->shadow->parent = amdgpu_bo_ref(bo);
  463. mutex_lock(&adev->shadow_list_lock);
  464. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  465. mutex_unlock(&adev->shadow_list_lock);
  466. }
  467. return r;
  468. }
  469. /**
  470. * amdgpu_bo_create - create an &amdgpu_bo buffer object
  471. * @adev: amdgpu device object
  472. * @bp: parameters to be used for the buffer object
  473. * @bo_ptr: pointer to the buffer object pointer
  474. *
  475. * Creates an &amdgpu_bo buffer object; and if requested, also creates a
  476. * shadow object.
  477. * Shadow object is used to backup the original buffer object, and is always
  478. * in GTT.
  479. *
  480. * Returns:
  481. * 0 for success or a negative error code on failure.
  482. */
  483. int amdgpu_bo_create(struct amdgpu_device *adev,
  484. struct amdgpu_bo_param *bp,
  485. struct amdgpu_bo **bo_ptr)
  486. {
  487. u64 flags = bp->flags;
  488. int r;
  489. bp->flags = bp->flags & ~AMDGPU_GEM_CREATE_SHADOW;
  490. r = amdgpu_bo_do_create(adev, bp, bo_ptr);
  491. if (r)
  492. return r;
  493. if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
  494. if (!bp->resv)
  495. WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
  496. NULL));
  497. r = amdgpu_bo_create_shadow(adev, bp->size, bp->byte_align, (*bo_ptr));
  498. if (!bp->resv)
  499. reservation_object_unlock((*bo_ptr)->tbo.resv);
  500. if (r)
  501. amdgpu_bo_unref(bo_ptr);
  502. }
  503. return r;
  504. }
  505. /**
  506. * amdgpu_bo_backup_to_shadow - Backs up an &amdgpu_bo buffer object
  507. * @adev: amdgpu device object
  508. * @ring: amdgpu_ring for the engine handling the buffer operations
  509. * @bo: &amdgpu_bo buffer to be backed up
  510. * @resv: reservation object with embedded fence
  511. * @fence: dma_fence associated with the operation
  512. * @direct: whether to submit the job directly
  513. *
  514. * Copies an &amdgpu_bo buffer object to its shadow object.
  515. * Not used for now.
  516. *
  517. * Returns:
  518. * 0 for success or a negative error code on failure.
  519. */
  520. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  521. struct amdgpu_ring *ring,
  522. struct amdgpu_bo *bo,
  523. struct reservation_object *resv,
  524. struct dma_fence **fence,
  525. bool direct)
  526. {
  527. struct amdgpu_bo *shadow = bo->shadow;
  528. uint64_t bo_addr, shadow_addr;
  529. int r;
  530. if (!shadow)
  531. return -EINVAL;
  532. bo_addr = amdgpu_bo_gpu_offset(bo);
  533. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  534. r = reservation_object_reserve_shared(bo->tbo.resv);
  535. if (r)
  536. goto err;
  537. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  538. amdgpu_bo_size(bo), resv, fence,
  539. direct, false);
  540. if (!r)
  541. amdgpu_bo_fence(bo, *fence, true);
  542. err:
  543. return r;
  544. }
  545. /**
  546. * amdgpu_bo_validate - validate an &amdgpu_bo buffer object
  547. * @bo: pointer to the buffer object
  548. *
  549. * Sets placement according to domain; and changes placement and caching
  550. * policy of the buffer object according to the placement.
  551. * This is used for validating shadow bos. It calls ttm_bo_validate() to
  552. * make sure the buffer is resident where it needs to be.
  553. *
  554. * Returns:
  555. * 0 for success or a negative error code on failure.
  556. */
  557. int amdgpu_bo_validate(struct amdgpu_bo *bo)
  558. {
  559. struct ttm_operation_ctx ctx = { false, false };
  560. uint32_t domain;
  561. int r;
  562. if (bo->pin_count)
  563. return 0;
  564. domain = bo->preferred_domains;
  565. retry:
  566. amdgpu_ttm_placement_from_domain(bo, domain);
  567. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  568. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  569. domain = bo->allowed_domains;
  570. goto retry;
  571. }
  572. return r;
  573. }
  574. /**
  575. * amdgpu_bo_restore_from_shadow - restore an &amdgpu_bo buffer object
  576. * @adev: amdgpu device object
  577. * @ring: amdgpu_ring for the engine handling the buffer operations
  578. * @bo: &amdgpu_bo buffer to be restored
  579. * @resv: reservation object with embedded fence
  580. * @fence: dma_fence associated with the operation
  581. * @direct: whether to submit the job directly
  582. *
  583. * Copies a buffer object's shadow content back to the object.
  584. * This is used for recovering a buffer from its shadow in case of a gpu
  585. * reset where vram context may be lost.
  586. *
  587. * Returns:
  588. * 0 for success or a negative error code on failure.
  589. */
  590. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  591. struct amdgpu_ring *ring,
  592. struct amdgpu_bo *bo,
  593. struct reservation_object *resv,
  594. struct dma_fence **fence,
  595. bool direct)
  596. {
  597. struct amdgpu_bo *shadow = bo->shadow;
  598. uint64_t bo_addr, shadow_addr;
  599. int r;
  600. if (!shadow)
  601. return -EINVAL;
  602. bo_addr = amdgpu_bo_gpu_offset(bo);
  603. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  604. r = reservation_object_reserve_shared(bo->tbo.resv);
  605. if (r)
  606. goto err;
  607. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  608. amdgpu_bo_size(bo), resv, fence,
  609. direct, false);
  610. if (!r)
  611. amdgpu_bo_fence(bo, *fence, true);
  612. err:
  613. return r;
  614. }
  615. /**
  616. * amdgpu_bo_kmap - map an &amdgpu_bo buffer object
  617. * @bo: &amdgpu_bo buffer object to be mapped
  618. * @ptr: kernel virtual address to be returned
  619. *
  620. * Calls ttm_bo_kmap() to set up the kernel virtual mapping; calls
  621. * amdgpu_bo_kptr() to get the kernel virtual address.
  622. *
  623. * Returns:
  624. * 0 for success or a negative error code on failure.
  625. */
  626. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  627. {
  628. void *kptr;
  629. long r;
  630. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  631. return -EPERM;
  632. kptr = amdgpu_bo_kptr(bo);
  633. if (kptr) {
  634. if (ptr)
  635. *ptr = kptr;
  636. return 0;
  637. }
  638. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  639. MAX_SCHEDULE_TIMEOUT);
  640. if (r < 0)
  641. return r;
  642. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  643. if (r)
  644. return r;
  645. if (ptr)
  646. *ptr = amdgpu_bo_kptr(bo);
  647. return 0;
  648. }
  649. /**
  650. * amdgpu_bo_kptr - returns a kernel virtual address of the buffer object
  651. * @bo: &amdgpu_bo buffer object
  652. *
  653. * Calls ttm_kmap_obj_virtual() to get the kernel virtual address
  654. *
  655. * Returns:
  656. * the virtual address of a buffer object area.
  657. */
  658. void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
  659. {
  660. bool is_iomem;
  661. return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  662. }
  663. /**
  664. * amdgpu_bo_kunmap - unmap an &amdgpu_bo buffer object
  665. * @bo: &amdgpu_bo buffer object to be unmapped
  666. *
  667. * Unmaps a kernel map set up by amdgpu_bo_kmap().
  668. */
  669. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  670. {
  671. if (bo->kmap.bo)
  672. ttm_bo_kunmap(&bo->kmap);
  673. }
  674. /**
  675. * amdgpu_bo_ref - reference an &amdgpu_bo buffer object
  676. * @bo: &amdgpu_bo buffer object
  677. *
  678. * References the contained &ttm_buffer_object.
  679. *
  680. * Returns:
  681. * a refcounted pointer to the &amdgpu_bo buffer object.
  682. */
  683. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  684. {
  685. if (bo == NULL)
  686. return NULL;
  687. ttm_bo_reference(&bo->tbo);
  688. return bo;
  689. }
  690. /**
  691. * amdgpu_bo_unref - unreference an &amdgpu_bo buffer object
  692. * @bo: &amdgpu_bo buffer object
  693. *
  694. * Unreferences the contained &ttm_buffer_object and clear the pointer
  695. */
  696. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  697. {
  698. struct ttm_buffer_object *tbo;
  699. if ((*bo) == NULL)
  700. return;
  701. tbo = &((*bo)->tbo);
  702. ttm_bo_unref(&tbo);
  703. if (tbo == NULL)
  704. *bo = NULL;
  705. }
  706. /**
  707. * amdgpu_bo_pin_restricted - pin an &amdgpu_bo buffer object
  708. * @bo: &amdgpu_bo buffer object to be pinned
  709. * @domain: domain to be pinned to
  710. * @min_offset: the start of requested address range
  711. * @max_offset: the end of requested address range
  712. *
  713. * Pins the buffer object according to requested domain and address range. If
  714. * the memory is unbound gart memory, binds the pages into gart table. Adjusts
  715. * pin_count and pin_size accordingly.
  716. *
  717. * Pinning means to lock pages in memory along with keeping them at a fixed
  718. * offset. It is required when a buffer can not be moved, for example, when
  719. * a display buffer is being scanned out.
  720. *
  721. * Compared with amdgpu_bo_pin(), this function gives more flexibility on
  722. * where to pin a buffer if there are specific restrictions on where a buffer
  723. * must be located.
  724. *
  725. * Returns:
  726. * 0 for success or a negative error code on failure.
  727. */
  728. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  729. u64 min_offset, u64 max_offset)
  730. {
  731. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  732. struct ttm_operation_ctx ctx = { false, false };
  733. int r, i;
  734. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  735. return -EPERM;
  736. if (WARN_ON_ONCE(min_offset > max_offset))
  737. return -EINVAL;
  738. /* A shared bo cannot be migrated to VRAM */
  739. if (bo->prime_shared_count) {
  740. if (domain & AMDGPU_GEM_DOMAIN_GTT)
  741. domain = AMDGPU_GEM_DOMAIN_GTT;
  742. else
  743. return -EINVAL;
  744. }
  745. /* This assumes only APU display buffers are pinned with (VRAM|GTT).
  746. * See function amdgpu_display_supported_domains()
  747. */
  748. domain = amdgpu_bo_get_preferred_pin_domain(adev, domain);
  749. if (bo->pin_count) {
  750. uint32_t mem_type = bo->tbo.mem.mem_type;
  751. if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
  752. return -EINVAL;
  753. bo->pin_count++;
  754. if (max_offset != 0) {
  755. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  756. WARN_ON_ONCE(max_offset <
  757. (amdgpu_bo_gpu_offset(bo) - domain_start));
  758. }
  759. return 0;
  760. }
  761. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  762. /* force to pin into visible video ram */
  763. if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
  764. bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  765. amdgpu_ttm_placement_from_domain(bo, domain);
  766. for (i = 0; i < bo->placement.num_placement; i++) {
  767. unsigned fpfn, lpfn;
  768. fpfn = min_offset >> PAGE_SHIFT;
  769. lpfn = max_offset >> PAGE_SHIFT;
  770. if (fpfn > bo->placements[i].fpfn)
  771. bo->placements[i].fpfn = fpfn;
  772. if (!bo->placements[i].lpfn ||
  773. (lpfn && lpfn < bo->placements[i].lpfn))
  774. bo->placements[i].lpfn = lpfn;
  775. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  776. }
  777. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  778. if (unlikely(r)) {
  779. dev_err(adev->dev, "%p pin failed\n", bo);
  780. goto error;
  781. }
  782. bo->pin_count = 1;
  783. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  784. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  785. atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);
  786. atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo),
  787. &adev->visible_pin_size);
  788. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  789. atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
  790. }
  791. error:
  792. return r;
  793. }
  794. /**
  795. * amdgpu_bo_pin - pin an &amdgpu_bo buffer object
  796. * @bo: &amdgpu_bo buffer object to be pinned
  797. * @domain: domain to be pinned to
  798. *
  799. * A simple wrapper to amdgpu_bo_pin_restricted().
  800. * Provides a simpler API for buffers that do not have any strict restrictions
  801. * on where a buffer must be located.
  802. *
  803. * Returns:
  804. * 0 for success or a negative error code on failure.
  805. */
  806. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)
  807. {
  808. return amdgpu_bo_pin_restricted(bo, domain, 0, 0);
  809. }
  810. /**
  811. * amdgpu_bo_unpin - unpin an &amdgpu_bo buffer object
  812. * @bo: &amdgpu_bo buffer object to be unpinned
  813. *
  814. * Decreases the pin_count, and clears the flags if pin_count reaches 0.
  815. * Changes placement and pin size accordingly.
  816. *
  817. * Returns:
  818. * 0 for success or a negative error code on failure.
  819. */
  820. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  821. {
  822. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  823. struct ttm_operation_ctx ctx = { false, false };
  824. int r, i;
  825. if (!bo->pin_count) {
  826. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  827. return 0;
  828. }
  829. bo->pin_count--;
  830. if (bo->pin_count)
  831. return 0;
  832. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  833. atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size);
  834. atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo),
  835. &adev->visible_pin_size);
  836. } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  837. atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size);
  838. }
  839. for (i = 0; i < bo->placement.num_placement; i++) {
  840. bo->placements[i].lpfn = 0;
  841. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  842. }
  843. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  844. if (unlikely(r))
  845. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  846. return r;
  847. }
  848. /**
  849. * amdgpu_bo_evict_vram - evict VRAM buffers
  850. * @adev: amdgpu device object
  851. *
  852. * Evicts all VRAM buffers on the lru list of the memory type.
  853. * Mainly used for evicting vram at suspend time.
  854. *
  855. * Returns:
  856. * 0 for success or a negative error code on failure.
  857. */
  858. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  859. {
  860. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  861. if (0 && (adev->flags & AMD_IS_APU)) {
  862. /* Useless to evict on IGP chips */
  863. return 0;
  864. }
  865. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  866. }
  867. static const char *amdgpu_vram_names[] = {
  868. "UNKNOWN",
  869. "GDDR1",
  870. "DDR2",
  871. "GDDR3",
  872. "GDDR4",
  873. "GDDR5",
  874. "HBM",
  875. "DDR3",
  876. "DDR4",
  877. };
  878. /**
  879. * amdgpu_bo_init - initialize memory manager
  880. * @adev: amdgpu device object
  881. *
  882. * Calls amdgpu_ttm_init() to initialize amdgpu memory manager.
  883. *
  884. * Returns:
  885. * 0 for success or a negative error code on failure.
  886. */
  887. int amdgpu_bo_init(struct amdgpu_device *adev)
  888. {
  889. /* reserve PAT memory space to WC for VRAM */
  890. arch_io_reserve_memtype_wc(adev->gmc.aper_base,
  891. adev->gmc.aper_size);
  892. /* Add an MTRR for the VRAM */
  893. adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
  894. adev->gmc.aper_size);
  895. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  896. adev->gmc.mc_vram_size >> 20,
  897. (unsigned long long)adev->gmc.aper_size >> 20);
  898. DRM_INFO("RAM width %dbits %s\n",
  899. adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
  900. return amdgpu_ttm_init(adev);
  901. }
  902. /**
  903. * amdgpu_bo_late_init - late init
  904. * @adev: amdgpu device object
  905. *
  906. * Calls amdgpu_ttm_late_init() to free resources used earlier during
  907. * initialization.
  908. *
  909. * Returns:
  910. * 0 for success or a negative error code on failure.
  911. */
  912. int amdgpu_bo_late_init(struct amdgpu_device *adev)
  913. {
  914. amdgpu_ttm_late_init(adev);
  915. return 0;
  916. }
  917. /**
  918. * amdgpu_bo_fini - tear down memory manager
  919. * @adev: amdgpu device object
  920. *
  921. * Reverses amdgpu_bo_init() to tear down memory manager.
  922. */
  923. void amdgpu_bo_fini(struct amdgpu_device *adev)
  924. {
  925. amdgpu_ttm_fini(adev);
  926. arch_phys_wc_del(adev->gmc.vram_mtrr);
  927. arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
  928. }
  929. /**
  930. * amdgpu_bo_fbdev_mmap - mmap fbdev memory
  931. * @bo: &amdgpu_bo buffer object
  932. * @vma: vma as input from the fbdev mmap method
  933. *
  934. * Calls ttm_fbdev_mmap() to mmap fbdev memory if it is backed by a bo.
  935. *
  936. * Returns:
  937. * 0 for success or a negative error code on failure.
  938. */
  939. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  940. struct vm_area_struct *vma)
  941. {
  942. return ttm_fbdev_mmap(vma, &bo->tbo);
  943. }
  944. /**
  945. * amdgpu_bo_set_tiling_flags - set tiling flags
  946. * @bo: &amdgpu_bo buffer object
  947. * @tiling_flags: new flags
  948. *
  949. * Sets buffer object's tiling flags with the new one. Used by GEM ioctl or
  950. * kernel driver to set the tiling flags on a buffer.
  951. *
  952. * Returns:
  953. * 0 for success or a negative error code on failure.
  954. */
  955. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  956. {
  957. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  958. if (adev->family <= AMDGPU_FAMILY_CZ &&
  959. AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  960. return -EINVAL;
  961. bo->tiling_flags = tiling_flags;
  962. return 0;
  963. }
  964. /**
  965. * amdgpu_bo_get_tiling_flags - get tiling flags
  966. * @bo: &amdgpu_bo buffer object
  967. * @tiling_flags: returned flags
  968. *
  969. * Gets buffer object's tiling flags. Used by GEM ioctl or kernel driver to
  970. * set the tiling flags on a buffer.
  971. */
  972. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  973. {
  974. lockdep_assert_held(&bo->tbo.resv->lock.base);
  975. if (tiling_flags)
  976. *tiling_flags = bo->tiling_flags;
  977. }
  978. /**
  979. * amdgpu_bo_set_metadata - set metadata
  980. * @bo: &amdgpu_bo buffer object
  981. * @metadata: new metadata
  982. * @metadata_size: size of the new metadata
  983. * @flags: flags of the new metadata
  984. *
  985. * Sets buffer object's metadata, its size and flags.
  986. * Used via GEM ioctl.
  987. *
  988. * Returns:
  989. * 0 for success or a negative error code on failure.
  990. */
  991. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  992. uint32_t metadata_size, uint64_t flags)
  993. {
  994. void *buffer;
  995. if (!metadata_size) {
  996. if (bo->metadata_size) {
  997. kfree(bo->metadata);
  998. bo->metadata = NULL;
  999. bo->metadata_size = 0;
  1000. }
  1001. return 0;
  1002. }
  1003. if (metadata == NULL)
  1004. return -EINVAL;
  1005. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  1006. if (buffer == NULL)
  1007. return -ENOMEM;
  1008. kfree(bo->metadata);
  1009. bo->metadata_flags = flags;
  1010. bo->metadata = buffer;
  1011. bo->metadata_size = metadata_size;
  1012. return 0;
  1013. }
  1014. /**
  1015. * amdgpu_bo_get_metadata - get metadata
  1016. * @bo: &amdgpu_bo buffer object
  1017. * @buffer: returned metadata
  1018. * @buffer_size: size of the buffer
  1019. * @metadata_size: size of the returned metadata
  1020. * @flags: flags of the returned metadata
  1021. *
  1022. * Gets buffer object's metadata, its size and flags. buffer_size shall not be
  1023. * less than metadata_size.
  1024. * Used via GEM ioctl.
  1025. *
  1026. * Returns:
  1027. * 0 for success or a negative error code on failure.
  1028. */
  1029. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  1030. size_t buffer_size, uint32_t *metadata_size,
  1031. uint64_t *flags)
  1032. {
  1033. if (!buffer && !metadata_size)
  1034. return -EINVAL;
  1035. if (buffer) {
  1036. if (buffer_size < bo->metadata_size)
  1037. return -EINVAL;
  1038. if (bo->metadata_size)
  1039. memcpy(buffer, bo->metadata, bo->metadata_size);
  1040. }
  1041. if (metadata_size)
  1042. *metadata_size = bo->metadata_size;
  1043. if (flags)
  1044. *flags = bo->metadata_flags;
  1045. return 0;
  1046. }
  1047. /**
  1048. * amdgpu_bo_move_notify - notification about a memory move
  1049. * @bo: pointer to a buffer object
  1050. * @evict: if this move is evicting the buffer from the graphics address space
  1051. * @new_mem: new information of the bufer object
  1052. *
  1053. * Marks the corresponding &amdgpu_bo buffer object as invalid, also performs
  1054. * bookkeeping.
  1055. * TTM driver callback which is called when ttm moves a buffer.
  1056. */
  1057. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  1058. bool evict,
  1059. struct ttm_mem_reg *new_mem)
  1060. {
  1061. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  1062. struct amdgpu_bo *abo;
  1063. struct ttm_mem_reg *old_mem = &bo->mem;
  1064. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  1065. return;
  1066. abo = ttm_to_amdgpu_bo(bo);
  1067. amdgpu_vm_bo_invalidate(adev, abo, evict);
  1068. amdgpu_bo_kunmap(abo);
  1069. /* remember the eviction */
  1070. if (evict)
  1071. atomic64_inc(&adev->num_evictions);
  1072. /* update statistics */
  1073. if (!new_mem)
  1074. return;
  1075. /* move_notify is called before move happens */
  1076. trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
  1077. }
  1078. /**
  1079. * amdgpu_bo_fault_reserve_notify - notification about a memory fault
  1080. * @bo: pointer to a buffer object
  1081. *
  1082. * Notifies the driver we are taking a fault on this BO and have reserved it,
  1083. * also performs bookkeeping.
  1084. * TTM driver callback for dealing with vm faults.
  1085. *
  1086. * Returns:
  1087. * 0 for success or a negative error code on failure.
  1088. */
  1089. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  1090. {
  1091. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  1092. struct ttm_operation_ctx ctx = { false, false };
  1093. struct amdgpu_bo *abo;
  1094. unsigned long offset, size;
  1095. int r;
  1096. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  1097. return 0;
  1098. abo = ttm_to_amdgpu_bo(bo);
  1099. /* Remember that this BO was accessed by the CPU */
  1100. abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  1101. if (bo->mem.mem_type != TTM_PL_VRAM)
  1102. return 0;
  1103. size = bo->mem.num_pages << PAGE_SHIFT;
  1104. offset = bo->mem.start << PAGE_SHIFT;
  1105. if ((offset + size) <= adev->gmc.visible_vram_size)
  1106. return 0;
  1107. /* Can't move a pinned BO to visible VRAM */
  1108. if (abo->pin_count > 0)
  1109. return -EINVAL;
  1110. /* hurrah the memory is not visible ! */
  1111. atomic64_inc(&adev->num_vram_cpu_page_faults);
  1112. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
  1113. AMDGPU_GEM_DOMAIN_GTT);
  1114. /* Avoid costly evictions; only set GTT as a busy placement */
  1115. abo->placement.num_busy_placement = 1;
  1116. abo->placement.busy_placement = &abo->placements[1];
  1117. r = ttm_bo_validate(bo, &abo->placement, &ctx);
  1118. if (unlikely(r != 0))
  1119. return r;
  1120. offset = bo->mem.start << PAGE_SHIFT;
  1121. /* this should never happen */
  1122. if (bo->mem.mem_type == TTM_PL_VRAM &&
  1123. (offset + size) > adev->gmc.visible_vram_size)
  1124. return -EINVAL;
  1125. return 0;
  1126. }
  1127. /**
  1128. * amdgpu_bo_fence - add fence to buffer object
  1129. *
  1130. * @bo: buffer object in question
  1131. * @fence: fence to add
  1132. * @shared: true if fence should be added shared
  1133. *
  1134. */
  1135. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  1136. bool shared)
  1137. {
  1138. struct reservation_object *resv = bo->tbo.resv;
  1139. if (shared)
  1140. reservation_object_add_shared_fence(resv, fence);
  1141. else
  1142. reservation_object_add_excl_fence(resv, fence);
  1143. }
  1144. /**
  1145. * amdgpu_bo_gpu_offset - return GPU offset of bo
  1146. * @bo: amdgpu object for which we query the offset
  1147. *
  1148. * Note: object should either be pinned or reserved when calling this
  1149. * function, it might be useful to add check for this for debugging.
  1150. *
  1151. * Returns:
  1152. * current GPU offset of the object.
  1153. */
  1154. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  1155. {
  1156. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  1157. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
  1158. !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem));
  1159. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  1160. !bo->pin_count);
  1161. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  1162. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  1163. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  1164. return bo->tbo.offset;
  1165. }
  1166. /**
  1167. * amdgpu_bo_get_preferred_pin_domain - get preferred domain for scanout
  1168. * @adev: amdgpu device object
  1169. * @domain: allowed :ref:`memory domains <amdgpu_memory_domains>`
  1170. *
  1171. * Returns:
  1172. * Which of the allowed domains is preferred for pinning the BO for scanout.
  1173. */
  1174. uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev,
  1175. uint32_t domain)
  1176. {
  1177. if (domain == (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
  1178. domain = AMDGPU_GEM_DOMAIN_VRAM;
  1179. if (adev->gmc.real_vram_size <= AMDGPU_SG_THRESHOLD)
  1180. domain = AMDGPU_GEM_DOMAIN_GTT;
  1181. }
  1182. return domain;
  1183. }