amdgpu_object.c 35 KB

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