amdgpu_object.c 35 KB

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