amdgpu_object.c 35 KB

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