amdgpu_object.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  30. * Dave Airlie
  31. */
  32. #include <linux/list.h>
  33. #include <linux/slab.h>
  34. #include <drm/drmP.h>
  35. #include <drm/amdgpu_drm.h>
  36. #include <drm/drm_cache.h>
  37. #include "amdgpu.h"
  38. #include "amdgpu_trace.h"
  39. #include "amdgpu_amdkfd.h"
  40. /**
  41. * DOC: amdgpu_object
  42. *
  43. * This defines the interfaces to operate on an &amdgpu_bo buffer object which
  44. * represents memory used by driver (VRAM, system memory, etc.). The driver
  45. * provides DRM/GEM APIs to userspace. DRM/GEM APIs then use these interfaces
  46. * to create/destroy/set buffer object which are then managed by the kernel TTM
  47. * memory manager.
  48. * The interfaces are also used internally by kernel clients, including gfx,
  49. * uvd, etc. for kernel managed allocations used by the GPU.
  50. *
  51. */
  52. static bool amdgpu_need_backup(struct amdgpu_device *adev)
  53. {
  54. if (adev->flags & AMD_IS_APU)
  55. return false;
  56. if (amdgpu_gpu_recovery == 0 ||
  57. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))
  58. return false;
  59. return true;
  60. }
  61. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  62. {
  63. struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
  64. struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
  65. if (bo->kfd_bo)
  66. amdgpu_amdkfd_unreserve_system_memory_limit(bo);
  67. amdgpu_bo_kunmap(bo);
  68. if (bo->gem_base.import_attach)
  69. drm_prime_gem_destroy(&bo->gem_base, bo->tbo.sg);
  70. drm_gem_object_release(&bo->gem_base);
  71. amdgpu_bo_unref(&bo->parent);
  72. if (!list_empty(&bo->shadow_list)) {
  73. mutex_lock(&adev->shadow_list_lock);
  74. list_del_init(&bo->shadow_list);
  75. mutex_unlock(&adev->shadow_list_lock);
  76. }
  77. kfree(bo->metadata);
  78. kfree(bo);
  79. }
  80. /**
  81. * amdgpu_ttm_bo_is_amdgpu_bo - check if the buffer object is an &amdgpu_bo
  82. * @bo: buffer object to be checked
  83. *
  84. * Uses destroy function associated with the object to determine if this is
  85. * an &amdgpu_bo.
  86. *
  87. * Returns:
  88. * true if the object belongs to &amdgpu_bo, false if not.
  89. */
  90. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  91. {
  92. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  93. return true;
  94. return false;
  95. }
  96. /**
  97. * amdgpu_ttm_placement_from_domain - set buffer's placement
  98. * @abo: &amdgpu_bo buffer object whose placement is to be set
  99. * @domain: requested domain
  100. *
  101. * Sets buffer's placement according to requested domain and the buffer's
  102. * flags.
  103. */
  104. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
  105. {
  106. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  107. struct ttm_placement *placement = &abo->placement;
  108. struct ttm_place *places = abo->placements;
  109. u64 flags = abo->flags;
  110. u32 c = 0;
  111. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  112. unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
  113. places[c].fpfn = 0;
  114. places[c].lpfn = 0;
  115. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  116. TTM_PL_FLAG_VRAM;
  117. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  118. places[c].lpfn = visible_pfn;
  119. else
  120. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  121. if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  122. places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
  123. c++;
  124. }
  125. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  126. places[c].fpfn = 0;
  127. if (flags & AMDGPU_GEM_CREATE_SHADOW)
  128. places[c].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
  129. else
  130. places[c].lpfn = 0;
  131. places[c].flags = TTM_PL_FLAG_TT;
  132. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  133. places[c].flags |= TTM_PL_FLAG_WC |
  134. TTM_PL_FLAG_UNCACHED;
  135. else
  136. places[c].flags |= TTM_PL_FLAG_CACHED;
  137. c++;
  138. }
  139. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  140. places[c].fpfn = 0;
  141. places[c].lpfn = 0;
  142. places[c].flags = TTM_PL_FLAG_SYSTEM;
  143. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  144. places[c].flags |= TTM_PL_FLAG_WC |
  145. TTM_PL_FLAG_UNCACHED;
  146. else
  147. places[c].flags |= TTM_PL_FLAG_CACHED;
  148. c++;
  149. }
  150. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  151. places[c].fpfn = 0;
  152. places[c].lpfn = 0;
  153. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  154. c++;
  155. }
  156. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  157. places[c].fpfn = 0;
  158. places[c].lpfn = 0;
  159. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  160. c++;
  161. }
  162. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  163. places[c].fpfn = 0;
  164. places[c].lpfn = 0;
  165. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  166. c++;
  167. }
  168. if (!c) {
  169. places[c].fpfn = 0;
  170. places[c].lpfn = 0;
  171. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  172. c++;
  173. }
  174. placement->num_placement = c;
  175. placement->placement = places;
  176. placement->num_busy_placement = c;
  177. placement->busy_placement = places;
  178. }
  179. /**
  180. * amdgpu_bo_create_reserved - create reserved BO for kernel use
  181. *
  182. * @adev: amdgpu device object
  183. * @size: size for the new BO
  184. * @align: alignment for the new BO
  185. * @domain: where to place it
  186. * @bo_ptr: used to initialize BOs in structures
  187. * @gpu_addr: GPU addr of the pinned BO
  188. * @cpu_addr: optional CPU address mapping
  189. *
  190. * Allocates and pins a BO for kernel internal use, and returns it still
  191. * reserved.
  192. *
  193. * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
  194. *
  195. * Returns:
  196. * 0 on success, negative error code otherwise.
  197. */
  198. int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
  199. unsigned long size, int align,
  200. u32 domain, struct amdgpu_bo **bo_ptr,
  201. u64 *gpu_addr, void **cpu_addr)
  202. {
  203. struct amdgpu_bo_param bp;
  204. bool free = false;
  205. int r;
  206. memset(&bp, 0, sizeof(bp));
  207. bp.size = size;
  208. bp.byte_align = align;
  209. bp.domain = domain;
  210. bp.flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  211. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  212. bp.type = ttm_bo_type_kernel;
  213. bp.resv = NULL;
  214. if (!*bo_ptr) {
  215. r = amdgpu_bo_create(adev, &bp, bo_ptr);
  216. if (r) {
  217. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
  218. r);
  219. return r;
  220. }
  221. free = true;
  222. }
  223. r = amdgpu_bo_reserve(*bo_ptr, false);
  224. if (r) {
  225. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  226. goto error_free;
  227. }
  228. r = amdgpu_bo_pin(*bo_ptr, domain);
  229. if (r) {
  230. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  231. goto error_unreserve;
  232. }
  233. if (gpu_addr)
  234. *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
  235. if (cpu_addr) {
  236. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  237. if (r) {
  238. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  239. goto error_unreserve;
  240. }
  241. }
  242. return 0;
  243. error_unreserve:
  244. amdgpu_bo_unreserve(*bo_ptr);
  245. error_free:
  246. if (free)
  247. amdgpu_bo_unref(bo_ptr);
  248. return r;
  249. }
  250. /**
  251. * amdgpu_bo_create_kernel - create BO for kernel use
  252. *
  253. * @adev: amdgpu device object
  254. * @size: size for the new BO
  255. * @align: alignment for the new BO
  256. * @domain: where to place it
  257. * @bo_ptr: used to initialize BOs in structures
  258. * @gpu_addr: GPU addr of the pinned BO
  259. * @cpu_addr: optional CPU address mapping
  260. *
  261. * Allocates and pins a BO for kernel internal use.
  262. *
  263. * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
  264. *
  265. * Returns:
  266. * 0 on success, negative error code otherwise.
  267. */
  268. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  269. unsigned long size, int align,
  270. u32 domain, struct amdgpu_bo **bo_ptr,
  271. u64 *gpu_addr, void **cpu_addr)
  272. {
  273. int r;
  274. r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
  275. gpu_addr, cpu_addr);
  276. if (r)
  277. return r;
  278. amdgpu_bo_unreserve(*bo_ptr);
  279. return 0;
  280. }
  281. /**
  282. * amdgpu_bo_free_kernel - free BO for kernel use
  283. *
  284. * @bo: amdgpu BO to free
  285. * @gpu_addr: pointer to where the BO's GPU memory space address was stored
  286. * @cpu_addr: pointer to where the BO's CPU memory space address was stored
  287. *
  288. * unmaps and unpin a BO for kernel internal use.
  289. */
  290. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  291. void **cpu_addr)
  292. {
  293. if (*bo == NULL)
  294. return;
  295. if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
  296. if (cpu_addr)
  297. amdgpu_bo_kunmap(*bo);
  298. amdgpu_bo_unpin(*bo);
  299. amdgpu_bo_unreserve(*bo);
  300. }
  301. amdgpu_bo_unref(bo);
  302. if (gpu_addr)
  303. *gpu_addr = 0;
  304. if (cpu_addr)
  305. *cpu_addr = NULL;
  306. }
  307. /* Validate bo size is bit bigger then the request domain */
  308. static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
  309. unsigned long size, u32 domain)
  310. {
  311. struct ttm_mem_type_manager *man = NULL;
  312. /*
  313. * If GTT is part of requested domains the check must succeed to
  314. * allow fall back to GTT
  315. */
  316. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  317. man = &adev->mman.bdev.man[TTM_PL_TT];
  318. if (size < (man->size << PAGE_SHIFT))
  319. return true;
  320. else
  321. goto fail;
  322. }
  323. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  324. man = &adev->mman.bdev.man[TTM_PL_VRAM];
  325. if (size < (man->size << PAGE_SHIFT))
  326. return true;
  327. else
  328. goto fail;
  329. }
  330. /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
  331. return true;
  332. fail:
  333. DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
  334. man->size << PAGE_SHIFT);
  335. return false;
  336. }
  337. static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  338. struct amdgpu_bo_param *bp,
  339. struct amdgpu_bo **bo_ptr)
  340. {
  341. struct ttm_operation_ctx ctx = {
  342. .interruptible = (bp->type != ttm_bo_type_kernel),
  343. .no_wait_gpu = false,
  344. .resv = bp->resv,
  345. .flags = TTM_OPT_FLAG_ALLOW_RES_EVICT
  346. };
  347. struct amdgpu_bo *bo;
  348. unsigned long page_align, size = bp->size;
  349. size_t acc_size;
  350. int r;
  351. page_align = roundup(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  352. size = ALIGN(size, PAGE_SIZE);
  353. if (!amdgpu_bo_validate_size(adev, size, bp->domain))
  354. return -ENOMEM;
  355. *bo_ptr = NULL;
  356. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  357. sizeof(struct amdgpu_bo));
  358. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  359. if (bo == NULL)
  360. return -ENOMEM;
  361. drm_gem_private_object_init(adev->ddev, &bo->gem_base, size);
  362. INIT_LIST_HEAD(&bo->shadow_list);
  363. INIT_LIST_HEAD(&bo->va);
  364. bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain :
  365. bp->domain;
  366. bo->allowed_domains = bo->preferred_domains;
  367. if (bp->type != ttm_bo_type_kernel &&
  368. bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  369. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  370. bo->flags = bp->flags;
  371. #ifdef CONFIG_X86_32
  372. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  373. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  374. */
  375. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  376. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  377. /* Don't try to enable write-combining when it can't work, or things
  378. * may be slow
  379. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  380. */
  381. #ifndef CONFIG_COMPILE_TEST
  382. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  383. thanks to write-combining
  384. #endif
  385. if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  386. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  387. "better performance thanks to write-combining\n");
  388. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  389. #else
  390. /* For architectures that don't support WC memory,
  391. * mask out the WC flag from the BO
  392. */
  393. if (!drm_arch_can_wc_memory())
  394. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  395. #endif
  396. bo->tbo.bdev = &adev->mman.bdev;
  397. amdgpu_ttm_placement_from_domain(bo, bp->domain);
  398. if (bp->type == ttm_bo_type_kernel)
  399. bo->tbo.priority = 1;
  400. r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
  401. &bo->placement, page_align, &ctx, acc_size,
  402. NULL, bp->resv, &amdgpu_ttm_bo_destroy);
  403. if (unlikely(r != 0))
  404. return r;
  405. if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
  406. bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  407. bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
  408. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
  409. ctx.bytes_moved);
  410. else
  411. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
  412. if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  413. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  414. struct dma_fence *fence;
  415. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  416. if (unlikely(r))
  417. goto fail_unreserve;
  418. amdgpu_bo_fence(bo, fence, false);
  419. dma_fence_put(bo->tbo.moving);
  420. bo->tbo.moving = dma_fence_get(fence);
  421. dma_fence_put(fence);
  422. }
  423. if (!bp->resv)
  424. amdgpu_bo_unreserve(bo);
  425. *bo_ptr = bo;
  426. trace_amdgpu_bo_create(bo);
  427. /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
  428. if (bp->type == ttm_bo_type_device)
  429. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  430. return 0;
  431. fail_unreserve:
  432. if (!bp->resv)
  433. ww_mutex_unlock(&bo->tbo.resv->lock);
  434. amdgpu_bo_unref(&bo);
  435. return r;
  436. }
  437. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  438. unsigned long size, int byte_align,
  439. struct amdgpu_bo *bo)
  440. {
  441. struct amdgpu_bo_param bp;
  442. int r;
  443. if (bo->shadow)
  444. return 0;
  445. memset(&bp, 0, sizeof(bp));
  446. bp.size = size;
  447. bp.byte_align = byte_align;
  448. bp.domain = AMDGPU_GEM_DOMAIN_GTT;
  449. bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC |
  450. AMDGPU_GEM_CREATE_SHADOW;
  451. bp.type = ttm_bo_type_kernel;
  452. bp.resv = bo->tbo.resv;
  453. r = amdgpu_bo_do_create(adev, &bp, &bo->shadow);
  454. if (!r) {
  455. bo->shadow->parent = amdgpu_bo_ref(bo);
  456. mutex_lock(&adev->shadow_list_lock);
  457. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  458. mutex_unlock(&adev->shadow_list_lock);
  459. }
  460. return r;
  461. }
  462. /**
  463. * amdgpu_bo_create - create an &amdgpu_bo buffer object
  464. * @adev: amdgpu device object
  465. * @bp: parameters to be used for the buffer object
  466. * @bo_ptr: pointer to the buffer object pointer
  467. *
  468. * Creates an &amdgpu_bo buffer object; and if requested, also creates a
  469. * shadow object.
  470. * Shadow object is used to backup the original buffer object, and is always
  471. * in GTT.
  472. *
  473. * Returns:
  474. * 0 for success or a negative error code on failure.
  475. */
  476. int amdgpu_bo_create(struct amdgpu_device *adev,
  477. struct amdgpu_bo_param *bp,
  478. struct amdgpu_bo **bo_ptr)
  479. {
  480. u64 flags = bp->flags;
  481. int r;
  482. bp->flags = bp->flags & ~AMDGPU_GEM_CREATE_SHADOW;
  483. r = amdgpu_bo_do_create(adev, bp, bo_ptr);
  484. if (r)
  485. return r;
  486. if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
  487. if (!bp->resv)
  488. WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
  489. NULL));
  490. r = amdgpu_bo_create_shadow(adev, bp->size, bp->byte_align, (*bo_ptr));
  491. if (!bp->resv)
  492. reservation_object_unlock((*bo_ptr)->tbo.resv);
  493. if (r)
  494. amdgpu_bo_unref(bo_ptr);
  495. }
  496. return r;
  497. }
  498. /**
  499. * amdgpu_bo_backup_to_shadow - Backs up an &amdgpu_bo buffer object
  500. * @adev: amdgpu device object
  501. * @ring: amdgpu_ring for the engine handling the buffer operations
  502. * @bo: &amdgpu_bo buffer to be backed up
  503. * @resv: reservation object with embedded fence
  504. * @fence: dma_fence associated with the operation
  505. * @direct: whether to submit the job directly
  506. *
  507. * Copies an &amdgpu_bo buffer object to its shadow object.
  508. * Not used for now.
  509. *
  510. * Returns:
  511. * 0 for success or a negative error code on failure.
  512. */
  513. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  514. struct amdgpu_ring *ring,
  515. struct amdgpu_bo *bo,
  516. struct reservation_object *resv,
  517. struct dma_fence **fence,
  518. bool direct)
  519. {
  520. struct amdgpu_bo *shadow = bo->shadow;
  521. uint64_t bo_addr, shadow_addr;
  522. int r;
  523. if (!shadow)
  524. return -EINVAL;
  525. bo_addr = amdgpu_bo_gpu_offset(bo);
  526. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  527. r = reservation_object_reserve_shared(bo->tbo.resv);
  528. if (r)
  529. goto err;
  530. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  531. amdgpu_bo_size(bo), resv, fence,
  532. direct, false);
  533. if (!r)
  534. amdgpu_bo_fence(bo, *fence, true);
  535. err:
  536. return r;
  537. }
  538. /**
  539. * amdgpu_bo_validate - validate an &amdgpu_bo buffer object
  540. * @bo: pointer to the buffer object
  541. *
  542. * Sets placement according to domain; and changes placement and caching
  543. * policy of the buffer object according to the placement.
  544. * This is used for validating shadow bos. It calls ttm_bo_validate() to
  545. * make sure the buffer is resident where it needs to be.
  546. *
  547. * Returns:
  548. * 0 for success or a negative error code on failure.
  549. */
  550. int amdgpu_bo_validate(struct amdgpu_bo *bo)
  551. {
  552. struct ttm_operation_ctx ctx = { false, false };
  553. uint32_t domain;
  554. int r;
  555. if (bo->pin_count)
  556. return 0;
  557. domain = bo->preferred_domains;
  558. retry:
  559. amdgpu_ttm_placement_from_domain(bo, domain);
  560. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  561. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  562. domain = bo->allowed_domains;
  563. goto retry;
  564. }
  565. return r;
  566. }
  567. /**
  568. * amdgpu_bo_restore_from_shadow - restore an &amdgpu_bo buffer object
  569. * @adev: amdgpu device object
  570. * @ring: amdgpu_ring for the engine handling the buffer operations
  571. * @bo: &amdgpu_bo buffer to be restored
  572. * @resv: reservation object with embedded fence
  573. * @fence: dma_fence associated with the operation
  574. * @direct: whether to submit the job directly
  575. *
  576. * Copies a buffer object's shadow content back to the object.
  577. * This is used for recovering a buffer from its shadow in case of a gpu
  578. * reset where vram context may be lost.
  579. *
  580. * Returns:
  581. * 0 for success or a negative error code on failure.
  582. */
  583. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  584. struct amdgpu_ring *ring,
  585. struct amdgpu_bo *bo,
  586. struct reservation_object *resv,
  587. struct dma_fence **fence,
  588. bool direct)
  589. {
  590. struct amdgpu_bo *shadow = bo->shadow;
  591. uint64_t bo_addr, shadow_addr;
  592. int r;
  593. if (!shadow)
  594. return -EINVAL;
  595. bo_addr = amdgpu_bo_gpu_offset(bo);
  596. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  597. r = reservation_object_reserve_shared(bo->tbo.resv);
  598. if (r)
  599. goto err;
  600. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  601. amdgpu_bo_size(bo), resv, fence,
  602. direct, false);
  603. if (!r)
  604. amdgpu_bo_fence(bo, *fence, true);
  605. err:
  606. return r;
  607. }
  608. /**
  609. * amdgpu_bo_kmap - map an &amdgpu_bo buffer object
  610. * @bo: &amdgpu_bo buffer object to be mapped
  611. * @ptr: kernel virtual address to be returned
  612. *
  613. * Calls ttm_bo_kmap() to set up the kernel virtual mapping; calls
  614. * amdgpu_bo_kptr() to get the kernel virtual address.
  615. *
  616. * Returns:
  617. * 0 for success or a negative error code on failure.
  618. */
  619. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  620. {
  621. void *kptr;
  622. long r;
  623. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  624. return -EPERM;
  625. kptr = amdgpu_bo_kptr(bo);
  626. if (kptr) {
  627. if (ptr)
  628. *ptr = kptr;
  629. return 0;
  630. }
  631. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  632. MAX_SCHEDULE_TIMEOUT);
  633. if (r < 0)
  634. return r;
  635. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  636. if (r)
  637. return r;
  638. if (ptr)
  639. *ptr = amdgpu_bo_kptr(bo);
  640. return 0;
  641. }
  642. /**
  643. * amdgpu_bo_kptr - returns a kernel virtual address of the buffer object
  644. * @bo: &amdgpu_bo buffer object
  645. *
  646. * Calls ttm_kmap_obj_virtual() to get the kernel virtual address
  647. *
  648. * Returns:
  649. * the virtual address of a buffer object area.
  650. */
  651. void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
  652. {
  653. bool is_iomem;
  654. return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  655. }
  656. /**
  657. * amdgpu_bo_kunmap - unmap an &amdgpu_bo buffer object
  658. * @bo: &amdgpu_bo buffer object to be unmapped
  659. *
  660. * Unmaps a kernel map set up by amdgpu_bo_kmap().
  661. */
  662. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  663. {
  664. if (bo->kmap.bo)
  665. ttm_bo_kunmap(&bo->kmap);
  666. }
  667. /**
  668. * amdgpu_bo_ref - reference an &amdgpu_bo buffer object
  669. * @bo: &amdgpu_bo buffer object
  670. *
  671. * References the contained &ttm_buffer_object.
  672. *
  673. * Returns:
  674. * a refcounted pointer to the &amdgpu_bo buffer object.
  675. */
  676. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  677. {
  678. if (bo == NULL)
  679. return NULL;
  680. ttm_bo_reference(&bo->tbo);
  681. return bo;
  682. }
  683. /**
  684. * amdgpu_bo_unref - unreference an &amdgpu_bo buffer object
  685. * @bo: &amdgpu_bo buffer object
  686. *
  687. * Unreferences the contained &ttm_buffer_object and clear the pointer
  688. */
  689. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  690. {
  691. struct ttm_buffer_object *tbo;
  692. if ((*bo) == NULL)
  693. return;
  694. tbo = &((*bo)->tbo);
  695. ttm_bo_unref(&tbo);
  696. if (tbo == NULL)
  697. *bo = NULL;
  698. }
  699. /**
  700. * amdgpu_bo_pin_restricted - pin an &amdgpu_bo buffer object
  701. * @bo: &amdgpu_bo buffer object to be pinned
  702. * @domain: domain to be pinned to
  703. * @min_offset: the start of requested address range
  704. * @max_offset: the end of requested address range
  705. *
  706. * Pins the buffer object according to requested domain and address range. If
  707. * the memory is unbound gart memory, binds the pages into gart table. Adjusts
  708. * pin_count and pin_size accordingly.
  709. *
  710. * Pinning means to lock pages in memory along with keeping them at a fixed
  711. * offset. It is required when a buffer can not be moved, for example, when
  712. * a display buffer is being scanned out.
  713. *
  714. * Compared with amdgpu_bo_pin(), this function gives more flexibility on
  715. * where to pin a buffer if there are specific restrictions on where a buffer
  716. * must be located.
  717. *
  718. * Returns:
  719. * 0 for success or a negative error code on failure.
  720. */
  721. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  722. u64 min_offset, u64 max_offset)
  723. {
  724. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  725. struct ttm_operation_ctx ctx = { false, false };
  726. int r, i;
  727. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  728. return -EPERM;
  729. if (WARN_ON_ONCE(min_offset > max_offset))
  730. return -EINVAL;
  731. /* A shared bo cannot be migrated to VRAM */
  732. if (bo->prime_shared_count) {
  733. if (domain & AMDGPU_GEM_DOMAIN_GTT)
  734. domain = AMDGPU_GEM_DOMAIN_GTT;
  735. else
  736. return -EINVAL;
  737. }
  738. /* This assumes only APU display buffers are pinned with (VRAM|GTT).
  739. * See function amdgpu_display_supported_domains()
  740. */
  741. domain = amdgpu_bo_get_preferred_pin_domain(adev, domain);
  742. if (bo->pin_count) {
  743. uint32_t mem_type = bo->tbo.mem.mem_type;
  744. if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
  745. return -EINVAL;
  746. bo->pin_count++;
  747. if (max_offset != 0) {
  748. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  749. WARN_ON_ONCE(max_offset <
  750. (amdgpu_bo_gpu_offset(bo) - domain_start));
  751. }
  752. return 0;
  753. }
  754. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  755. /* force to pin into visible video ram */
  756. if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
  757. bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  758. amdgpu_ttm_placement_from_domain(bo, domain);
  759. for (i = 0; i < bo->placement.num_placement; i++) {
  760. unsigned fpfn, lpfn;
  761. fpfn = min_offset >> PAGE_SHIFT;
  762. lpfn = max_offset >> PAGE_SHIFT;
  763. if (fpfn > bo->placements[i].fpfn)
  764. bo->placements[i].fpfn = fpfn;
  765. if (!bo->placements[i].lpfn ||
  766. (lpfn && lpfn < bo->placements[i].lpfn))
  767. bo->placements[i].lpfn = lpfn;
  768. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  769. }
  770. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  771. if (unlikely(r)) {
  772. dev_err(adev->dev, "%p pin failed\n", bo);
  773. goto error;
  774. }
  775. r = amdgpu_ttm_alloc_gart(&bo->tbo);
  776. if (unlikely(r)) {
  777. dev_err(adev->dev, "%p bind failed\n", bo);
  778. goto error;
  779. }
  780. bo->pin_count = 1;
  781. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  782. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  783. adev->vram_pin_size += amdgpu_bo_size(bo);
  784. adev->invisible_pin_size += amdgpu_vram_mgr_bo_invisible_size(bo);
  785. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  786. adev->gart_pin_size += amdgpu_bo_size(bo);
  787. }
  788. error:
  789. return r;
  790. }
  791. /**
  792. * amdgpu_bo_pin - pin an &amdgpu_bo buffer object
  793. * @bo: &amdgpu_bo buffer object to be pinned
  794. * @domain: domain to be pinned to
  795. *
  796. * A simple wrapper to amdgpu_bo_pin_restricted().
  797. * Provides a simpler API for buffers that do not have any strict restrictions
  798. * on where a buffer must be located.
  799. *
  800. * Returns:
  801. * 0 for success or a negative error code on failure.
  802. */
  803. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)
  804. {
  805. return amdgpu_bo_pin_restricted(bo, domain, 0, 0);
  806. }
  807. /**
  808. * amdgpu_bo_unpin - unpin an &amdgpu_bo buffer object
  809. * @bo: &amdgpu_bo buffer object to be unpinned
  810. *
  811. * Decreases the pin_count, and clears the flags if pin_count reaches 0.
  812. * Changes placement and pin size accordingly.
  813. *
  814. * Returns:
  815. * 0 for success or a negative error code on failure.
  816. */
  817. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  818. {
  819. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  820. struct ttm_operation_ctx ctx = { false, false };
  821. int r, i;
  822. if (!bo->pin_count) {
  823. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  824. return 0;
  825. }
  826. bo->pin_count--;
  827. if (bo->pin_count)
  828. return 0;
  829. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  830. adev->vram_pin_size -= amdgpu_bo_size(bo);
  831. adev->invisible_pin_size -= amdgpu_vram_mgr_bo_invisible_size(bo);
  832. } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  833. adev->gart_pin_size -= amdgpu_bo_size(bo);
  834. }
  835. for (i = 0; i < bo->placement.num_placement; i++) {
  836. bo->placements[i].lpfn = 0;
  837. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  838. }
  839. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  840. if (unlikely(r))
  841. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  842. return r;
  843. }
  844. /**
  845. * amdgpu_bo_evict_vram - evict VRAM buffers
  846. * @adev: amdgpu device object
  847. *
  848. * Evicts all VRAM buffers on the lru list of the memory type.
  849. * Mainly used for evicting vram at suspend time.
  850. *
  851. * Returns:
  852. * 0 for success or a negative error code on failure.
  853. */
  854. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  855. {
  856. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  857. if (0 && (adev->flags & AMD_IS_APU)) {
  858. /* Useless to evict on IGP chips */
  859. return 0;
  860. }
  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_ttm_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_ttm_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_ttm_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_ttm_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(bo->tbo.mem.mem_type == TTM_PL_TT &&
  1154. !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem));
  1155. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  1156. !bo->pin_count);
  1157. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  1158. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  1159. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  1160. return bo->tbo.offset;
  1161. }
  1162. /**
  1163. * amdgpu_bo_get_preferred_pin_domain - get preferred domain for scanout
  1164. * @adev: amdgpu device object
  1165. * @domain: allowed :ref:`memory domains <amdgpu_memory_domains>`
  1166. *
  1167. * Returns:
  1168. * Which of the allowed domains is preferred for pinning the BO for scanout.
  1169. */
  1170. uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev,
  1171. uint32_t domain)
  1172. {
  1173. if (domain == (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
  1174. domain = AMDGPU_GEM_DOMAIN_VRAM;
  1175. if (adev->gmc.real_vram_size <= AMDGPU_SG_THRESHOLD)
  1176. domain = AMDGPU_GEM_DOMAIN_GTT;
  1177. }
  1178. return domain;
  1179. }