amdgpu_object.c 35 KB

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