amdgpu_object.c 35 KB

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