amdgpu_object.c 35 KB

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