amdgpu_object.c 35 KB

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