amdgpu_object.c 35 KB

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