amdgpu_object.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. static bool amdgpu_need_backup(struct amdgpu_device *adev)
  40. {
  41. if (adev->flags & AMD_IS_APU)
  42. return false;
  43. if (amdgpu_gpu_recovery == 0 ||
  44. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))
  45. return false;
  46. return true;
  47. }
  48. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  49. {
  50. struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
  51. struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
  52. amdgpu_bo_kunmap(bo);
  53. drm_gem_object_release(&bo->gem_base);
  54. amdgpu_bo_unref(&bo->parent);
  55. if (!list_empty(&bo->shadow_list)) {
  56. mutex_lock(&adev->shadow_list_lock);
  57. list_del_init(&bo->shadow_list);
  58. mutex_unlock(&adev->shadow_list_lock);
  59. }
  60. kfree(bo->metadata);
  61. kfree(bo);
  62. }
  63. bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
  64. {
  65. if (bo->destroy == &amdgpu_ttm_bo_destroy)
  66. return true;
  67. return false;
  68. }
  69. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
  70. {
  71. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  72. struct ttm_placement *placement = &abo->placement;
  73. struct ttm_place *places = abo->placements;
  74. u64 flags = abo->flags;
  75. u32 c = 0;
  76. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  77. unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
  78. places[c].fpfn = 0;
  79. places[c].lpfn = 0;
  80. places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  81. TTM_PL_FLAG_VRAM;
  82. if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
  83. places[c].lpfn = visible_pfn;
  84. else
  85. places[c].flags |= TTM_PL_FLAG_TOPDOWN;
  86. if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
  87. places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
  88. c++;
  89. }
  90. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  91. places[c].fpfn = 0;
  92. if (flags & AMDGPU_GEM_CREATE_SHADOW)
  93. places[c].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
  94. else
  95. places[c].lpfn = 0;
  96. places[c].flags = TTM_PL_FLAG_TT;
  97. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  98. places[c].flags |= TTM_PL_FLAG_WC |
  99. TTM_PL_FLAG_UNCACHED;
  100. else
  101. places[c].flags |= TTM_PL_FLAG_CACHED;
  102. c++;
  103. }
  104. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  105. places[c].fpfn = 0;
  106. places[c].lpfn = 0;
  107. places[c].flags = TTM_PL_FLAG_SYSTEM;
  108. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  109. places[c].flags |= TTM_PL_FLAG_WC |
  110. TTM_PL_FLAG_UNCACHED;
  111. else
  112. places[c].flags |= TTM_PL_FLAG_CACHED;
  113. c++;
  114. }
  115. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  116. places[c].fpfn = 0;
  117. places[c].lpfn = 0;
  118. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
  119. c++;
  120. }
  121. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  122. places[c].fpfn = 0;
  123. places[c].lpfn = 0;
  124. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
  125. c++;
  126. }
  127. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  128. places[c].fpfn = 0;
  129. places[c].lpfn = 0;
  130. places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
  131. c++;
  132. }
  133. if (!c) {
  134. places[c].fpfn = 0;
  135. places[c].lpfn = 0;
  136. places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  137. c++;
  138. }
  139. placement->num_placement = c;
  140. placement->placement = places;
  141. placement->num_busy_placement = c;
  142. placement->busy_placement = places;
  143. }
  144. /**
  145. * amdgpu_bo_create_reserved - create reserved BO for kernel use
  146. *
  147. * @adev: amdgpu device object
  148. * @size: size for the new BO
  149. * @align: alignment for the new BO
  150. * @domain: where to place it
  151. * @bo_ptr: resulting BO
  152. * @gpu_addr: GPU addr of the pinned BO
  153. * @cpu_addr: optional CPU address mapping
  154. *
  155. * Allocates and pins a BO for kernel internal use, and returns it still
  156. * reserved.
  157. *
  158. * Returns 0 on success, negative error code otherwise.
  159. */
  160. int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
  161. unsigned long size, int align,
  162. u32 domain, struct amdgpu_bo **bo_ptr,
  163. u64 *gpu_addr, void **cpu_addr)
  164. {
  165. bool free = false;
  166. int r;
  167. if (!*bo_ptr) {
  168. r = amdgpu_bo_create(adev, size, align, true, domain,
  169. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  170. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  171. NULL, NULL, bo_ptr);
  172. if (r) {
  173. dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
  174. r);
  175. return r;
  176. }
  177. free = true;
  178. }
  179. r = amdgpu_bo_reserve(*bo_ptr, false);
  180. if (r) {
  181. dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
  182. goto error_free;
  183. }
  184. r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
  185. if (r) {
  186. dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
  187. goto error_unreserve;
  188. }
  189. if (cpu_addr) {
  190. r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  191. if (r) {
  192. dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
  193. goto error_unreserve;
  194. }
  195. }
  196. return 0;
  197. error_unreserve:
  198. amdgpu_bo_unreserve(*bo_ptr);
  199. error_free:
  200. if (free)
  201. amdgpu_bo_unref(bo_ptr);
  202. return r;
  203. }
  204. /**
  205. * amdgpu_bo_create_kernel - create BO for kernel use
  206. *
  207. * @adev: amdgpu device object
  208. * @size: size for the new BO
  209. * @align: alignment for the new BO
  210. * @domain: where to place it
  211. * @bo_ptr: resulting BO
  212. * @gpu_addr: GPU addr of the pinned BO
  213. * @cpu_addr: optional CPU address mapping
  214. *
  215. * Allocates and pins a BO for kernel internal use.
  216. *
  217. * Returns 0 on success, negative error code otherwise.
  218. */
  219. int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  220. unsigned long size, int align,
  221. u32 domain, struct amdgpu_bo **bo_ptr,
  222. u64 *gpu_addr, void **cpu_addr)
  223. {
  224. int r;
  225. r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
  226. gpu_addr, cpu_addr);
  227. if (r)
  228. return r;
  229. amdgpu_bo_unreserve(*bo_ptr);
  230. return 0;
  231. }
  232. /**
  233. * amdgpu_bo_free_kernel - free BO for kernel use
  234. *
  235. * @bo: amdgpu BO to free
  236. *
  237. * unmaps and unpin a BO for kernel internal use.
  238. */
  239. void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
  240. void **cpu_addr)
  241. {
  242. if (*bo == NULL)
  243. return;
  244. if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
  245. if (cpu_addr)
  246. amdgpu_bo_kunmap(*bo);
  247. amdgpu_bo_unpin(*bo);
  248. amdgpu_bo_unreserve(*bo);
  249. }
  250. amdgpu_bo_unref(bo);
  251. if (gpu_addr)
  252. *gpu_addr = 0;
  253. if (cpu_addr)
  254. *cpu_addr = NULL;
  255. }
  256. /* Validate bo size is bit bigger then the request domain */
  257. static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
  258. unsigned long size, u32 domain)
  259. {
  260. struct ttm_mem_type_manager *man = NULL;
  261. /*
  262. * If GTT is part of requested domains the check must succeed to
  263. * allow fall back to GTT
  264. */
  265. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  266. man = &adev->mman.bdev.man[TTM_PL_TT];
  267. if (size < (man->size << PAGE_SHIFT))
  268. return true;
  269. else
  270. goto fail;
  271. }
  272. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  273. man = &adev->mman.bdev.man[TTM_PL_VRAM];
  274. if (size < (man->size << PAGE_SHIFT))
  275. return true;
  276. else
  277. goto fail;
  278. }
  279. /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
  280. return true;
  281. fail:
  282. DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
  283. man->size << PAGE_SHIFT);
  284. return false;
  285. }
  286. static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  287. unsigned long size, int byte_align,
  288. bool kernel, u32 domain, u64 flags,
  289. struct sg_table *sg,
  290. struct reservation_object *resv,
  291. struct amdgpu_bo **bo_ptr)
  292. {
  293. struct ttm_operation_ctx ctx = {
  294. .interruptible = !kernel,
  295. .no_wait_gpu = false,
  296. .resv = resv,
  297. .flags = TTM_OPT_FLAG_ALLOW_RES_EVICT
  298. };
  299. struct amdgpu_bo *bo;
  300. enum ttm_bo_type type;
  301. unsigned long page_align;
  302. size_t acc_size;
  303. int r;
  304. page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  305. size = ALIGN(size, PAGE_SIZE);
  306. if (!amdgpu_bo_validate_size(adev, size, domain))
  307. return -ENOMEM;
  308. if (kernel) {
  309. type = ttm_bo_type_kernel;
  310. } else if (sg) {
  311. type = ttm_bo_type_sg;
  312. } else {
  313. type = ttm_bo_type_device;
  314. }
  315. *bo_ptr = NULL;
  316. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  317. sizeof(struct amdgpu_bo));
  318. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  319. if (bo == NULL)
  320. return -ENOMEM;
  321. drm_gem_private_object_init(adev->ddev, &bo->gem_base, size);
  322. INIT_LIST_HEAD(&bo->shadow_list);
  323. INIT_LIST_HEAD(&bo->va);
  324. bo->preferred_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
  325. AMDGPU_GEM_DOMAIN_GTT |
  326. AMDGPU_GEM_DOMAIN_CPU |
  327. AMDGPU_GEM_DOMAIN_GDS |
  328. AMDGPU_GEM_DOMAIN_GWS |
  329. AMDGPU_GEM_DOMAIN_OA);
  330. bo->allowed_domains = bo->preferred_domains;
  331. if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
  332. bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
  333. bo->flags = flags;
  334. #ifdef CONFIG_X86_32
  335. /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
  336. * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
  337. */
  338. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  339. #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
  340. /* Don't try to enable write-combining when it can't work, or things
  341. * may be slow
  342. * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
  343. */
  344. #ifndef CONFIG_COMPILE_TEST
  345. #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
  346. thanks to write-combining
  347. #endif
  348. if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  349. DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
  350. "better performance thanks to write-combining\n");
  351. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  352. #else
  353. /* For architectures that don't support WC memory,
  354. * mask out the WC flag from the BO
  355. */
  356. if (!drm_arch_can_wc_memory())
  357. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  358. #endif
  359. bo->tbo.bdev = &adev->mman.bdev;
  360. amdgpu_ttm_placement_from_domain(bo, domain);
  361. r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
  362. &bo->placement, page_align, &ctx, acc_size,
  363. sg, resv, &amdgpu_ttm_bo_destroy);
  364. if (unlikely(r != 0))
  365. return r;
  366. if (adev->gmc.visible_vram_size < adev->gmc.real_vram_size &&
  367. bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  368. bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
  369. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
  370. ctx.bytes_moved);
  371. else
  372. amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
  373. if (kernel)
  374. bo->tbo.priority = 1;
  375. if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
  376. bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
  377. struct dma_fence *fence;
  378. r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
  379. if (unlikely(r))
  380. goto fail_unreserve;
  381. amdgpu_bo_fence(bo, fence, false);
  382. dma_fence_put(bo->tbo.moving);
  383. bo->tbo.moving = dma_fence_get(fence);
  384. dma_fence_put(fence);
  385. }
  386. if (!resv)
  387. amdgpu_bo_unreserve(bo);
  388. *bo_ptr = bo;
  389. trace_amdgpu_bo_create(bo);
  390. /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
  391. if (type == ttm_bo_type_device)
  392. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  393. return 0;
  394. fail_unreserve:
  395. if (!resv)
  396. ww_mutex_unlock(&bo->tbo.resv->lock);
  397. amdgpu_bo_unref(&bo);
  398. return r;
  399. }
  400. static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  401. unsigned long size, int byte_align,
  402. struct amdgpu_bo *bo)
  403. {
  404. int r;
  405. if (bo->shadow)
  406. return 0;
  407. r = amdgpu_bo_do_create(adev, size, byte_align, true,
  408. AMDGPU_GEM_DOMAIN_GTT,
  409. AMDGPU_GEM_CREATE_CPU_GTT_USWC |
  410. AMDGPU_GEM_CREATE_SHADOW,
  411. NULL, bo->tbo.resv,
  412. &bo->shadow);
  413. if (!r) {
  414. bo->shadow->parent = amdgpu_bo_ref(bo);
  415. mutex_lock(&adev->shadow_list_lock);
  416. list_add_tail(&bo->shadow_list, &adev->shadow_list);
  417. mutex_unlock(&adev->shadow_list_lock);
  418. }
  419. return r;
  420. }
  421. int amdgpu_bo_create(struct amdgpu_device *adev,
  422. unsigned long size, int byte_align,
  423. bool kernel, u32 domain, u64 flags,
  424. struct sg_table *sg,
  425. struct reservation_object *resv,
  426. struct amdgpu_bo **bo_ptr)
  427. {
  428. uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
  429. int r;
  430. r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain,
  431. parent_flags, sg, resv, bo_ptr);
  432. if (r)
  433. return r;
  434. if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
  435. if (!resv)
  436. WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
  437. NULL));
  438. r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
  439. if (!resv)
  440. reservation_object_unlock((*bo_ptr)->tbo.resv);
  441. if (r)
  442. amdgpu_bo_unref(bo_ptr);
  443. }
  444. return r;
  445. }
  446. int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
  447. struct amdgpu_ring *ring,
  448. struct amdgpu_bo *bo,
  449. struct reservation_object *resv,
  450. struct dma_fence **fence,
  451. bool direct)
  452. {
  453. struct amdgpu_bo *shadow = bo->shadow;
  454. uint64_t bo_addr, shadow_addr;
  455. int r;
  456. if (!shadow)
  457. return -EINVAL;
  458. bo_addr = amdgpu_bo_gpu_offset(bo);
  459. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  460. r = reservation_object_reserve_shared(bo->tbo.resv);
  461. if (r)
  462. goto err;
  463. r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
  464. amdgpu_bo_size(bo), resv, fence,
  465. direct, false);
  466. if (!r)
  467. amdgpu_bo_fence(bo, *fence, true);
  468. err:
  469. return r;
  470. }
  471. int amdgpu_bo_validate(struct amdgpu_bo *bo)
  472. {
  473. struct ttm_operation_ctx ctx = { false, false };
  474. uint32_t domain;
  475. int r;
  476. if (bo->pin_count)
  477. return 0;
  478. domain = bo->preferred_domains;
  479. retry:
  480. amdgpu_ttm_placement_from_domain(bo, domain);
  481. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  482. if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
  483. domain = bo->allowed_domains;
  484. goto retry;
  485. }
  486. return r;
  487. }
  488. int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
  489. struct amdgpu_ring *ring,
  490. struct amdgpu_bo *bo,
  491. struct reservation_object *resv,
  492. struct dma_fence **fence,
  493. bool direct)
  494. {
  495. struct amdgpu_bo *shadow = bo->shadow;
  496. uint64_t bo_addr, shadow_addr;
  497. int r;
  498. if (!shadow)
  499. return -EINVAL;
  500. bo_addr = amdgpu_bo_gpu_offset(bo);
  501. shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
  502. r = reservation_object_reserve_shared(bo->tbo.resv);
  503. if (r)
  504. goto err;
  505. r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
  506. amdgpu_bo_size(bo), resv, fence,
  507. direct, false);
  508. if (!r)
  509. amdgpu_bo_fence(bo, *fence, true);
  510. err:
  511. return r;
  512. }
  513. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  514. {
  515. void *kptr;
  516. long r;
  517. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  518. return -EPERM;
  519. kptr = amdgpu_bo_kptr(bo);
  520. if (kptr) {
  521. if (ptr)
  522. *ptr = kptr;
  523. return 0;
  524. }
  525. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
  526. MAX_SCHEDULE_TIMEOUT);
  527. if (r < 0)
  528. return r;
  529. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  530. if (r)
  531. return r;
  532. if (ptr)
  533. *ptr = amdgpu_bo_kptr(bo);
  534. return 0;
  535. }
  536. void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
  537. {
  538. bool is_iomem;
  539. return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  540. }
  541. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  542. {
  543. if (bo->kmap.bo)
  544. ttm_bo_kunmap(&bo->kmap);
  545. }
  546. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  547. {
  548. if (bo == NULL)
  549. return NULL;
  550. ttm_bo_reference(&bo->tbo);
  551. return bo;
  552. }
  553. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  554. {
  555. struct ttm_buffer_object *tbo;
  556. if ((*bo) == NULL)
  557. return;
  558. tbo = &((*bo)->tbo);
  559. ttm_bo_unref(&tbo);
  560. if (tbo == NULL)
  561. *bo = NULL;
  562. }
  563. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  564. u64 min_offset, u64 max_offset,
  565. u64 *gpu_addr)
  566. {
  567. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  568. struct ttm_operation_ctx ctx = { false, false };
  569. int r, i;
  570. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
  571. return -EPERM;
  572. if (WARN_ON_ONCE(min_offset > max_offset))
  573. return -EINVAL;
  574. /* A shared bo cannot be migrated to VRAM */
  575. if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM))
  576. return -EINVAL;
  577. if (bo->pin_count) {
  578. uint32_t mem_type = bo->tbo.mem.mem_type;
  579. if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
  580. return -EINVAL;
  581. bo->pin_count++;
  582. if (gpu_addr)
  583. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  584. if (max_offset != 0) {
  585. u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
  586. WARN_ON_ONCE(max_offset <
  587. (amdgpu_bo_gpu_offset(bo) - domain_start));
  588. }
  589. return 0;
  590. }
  591. bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  592. /* force to pin into visible video ram */
  593. if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
  594. bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  595. amdgpu_ttm_placement_from_domain(bo, domain);
  596. for (i = 0; i < bo->placement.num_placement; i++) {
  597. unsigned fpfn, lpfn;
  598. fpfn = min_offset >> PAGE_SHIFT;
  599. lpfn = max_offset >> PAGE_SHIFT;
  600. if (fpfn > bo->placements[i].fpfn)
  601. bo->placements[i].fpfn = fpfn;
  602. if (!bo->placements[i].lpfn ||
  603. (lpfn && lpfn < bo->placements[i].lpfn))
  604. bo->placements[i].lpfn = lpfn;
  605. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  606. }
  607. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  608. if (unlikely(r)) {
  609. dev_err(adev->dev, "%p pin failed\n", bo);
  610. goto error;
  611. }
  612. r = amdgpu_ttm_alloc_gart(&bo->tbo);
  613. if (unlikely(r)) {
  614. dev_err(adev->dev, "%p bind failed\n", bo);
  615. goto error;
  616. }
  617. bo->pin_count = 1;
  618. if (gpu_addr != NULL)
  619. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  620. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  621. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  622. adev->vram_pin_size += amdgpu_bo_size(bo);
  623. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  624. adev->invisible_pin_size += amdgpu_bo_size(bo);
  625. } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  626. adev->gart_pin_size += amdgpu_bo_size(bo);
  627. }
  628. error:
  629. return r;
  630. }
  631. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  632. {
  633. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  634. }
  635. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  636. {
  637. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  638. struct ttm_operation_ctx ctx = { false, false };
  639. int r, i;
  640. if (!bo->pin_count) {
  641. dev_warn(adev->dev, "%p unpin not necessary\n", bo);
  642. return 0;
  643. }
  644. bo->pin_count--;
  645. if (bo->pin_count)
  646. return 0;
  647. for (i = 0; i < bo->placement.num_placement; i++) {
  648. bo->placements[i].lpfn = 0;
  649. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  650. }
  651. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  652. if (unlikely(r)) {
  653. dev_err(adev->dev, "%p validate failed for unpin\n", bo);
  654. goto error;
  655. }
  656. if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
  657. adev->vram_pin_size -= amdgpu_bo_size(bo);
  658. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  659. adev->invisible_pin_size -= amdgpu_bo_size(bo);
  660. } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  661. adev->gart_pin_size -= amdgpu_bo_size(bo);
  662. }
  663. error:
  664. return r;
  665. }
  666. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  667. {
  668. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  669. if (0 && (adev->flags & AMD_IS_APU)) {
  670. /* Useless to evict on IGP chips */
  671. return 0;
  672. }
  673. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  674. }
  675. static const char *amdgpu_vram_names[] = {
  676. "UNKNOWN",
  677. "GDDR1",
  678. "DDR2",
  679. "GDDR3",
  680. "GDDR4",
  681. "GDDR5",
  682. "HBM",
  683. "DDR3"
  684. };
  685. int amdgpu_bo_init(struct amdgpu_device *adev)
  686. {
  687. /* reserve PAT memory space to WC for VRAM */
  688. arch_io_reserve_memtype_wc(adev->gmc.aper_base,
  689. adev->gmc.aper_size);
  690. /* Add an MTRR for the VRAM */
  691. adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
  692. adev->gmc.aper_size);
  693. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  694. adev->gmc.mc_vram_size >> 20,
  695. (unsigned long long)adev->gmc.aper_size >> 20);
  696. DRM_INFO("RAM width %dbits %s\n",
  697. adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
  698. return amdgpu_ttm_init(adev);
  699. }
  700. void amdgpu_bo_fini(struct amdgpu_device *adev)
  701. {
  702. amdgpu_ttm_fini(adev);
  703. arch_phys_wc_del(adev->gmc.vram_mtrr);
  704. arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
  705. }
  706. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  707. struct vm_area_struct *vma)
  708. {
  709. return ttm_fbdev_mmap(vma, &bo->tbo);
  710. }
  711. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  712. {
  713. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  714. if (adev->family <= AMDGPU_FAMILY_CZ &&
  715. AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  716. return -EINVAL;
  717. bo->tiling_flags = tiling_flags;
  718. return 0;
  719. }
  720. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  721. {
  722. lockdep_assert_held(&bo->tbo.resv->lock.base);
  723. if (tiling_flags)
  724. *tiling_flags = bo->tiling_flags;
  725. }
  726. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  727. uint32_t metadata_size, uint64_t flags)
  728. {
  729. void *buffer;
  730. if (!metadata_size) {
  731. if (bo->metadata_size) {
  732. kfree(bo->metadata);
  733. bo->metadata = NULL;
  734. bo->metadata_size = 0;
  735. }
  736. return 0;
  737. }
  738. if (metadata == NULL)
  739. return -EINVAL;
  740. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  741. if (buffer == NULL)
  742. return -ENOMEM;
  743. kfree(bo->metadata);
  744. bo->metadata_flags = flags;
  745. bo->metadata = buffer;
  746. bo->metadata_size = metadata_size;
  747. return 0;
  748. }
  749. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  750. size_t buffer_size, uint32_t *metadata_size,
  751. uint64_t *flags)
  752. {
  753. if (!buffer && !metadata_size)
  754. return -EINVAL;
  755. if (buffer) {
  756. if (buffer_size < bo->metadata_size)
  757. return -EINVAL;
  758. if (bo->metadata_size)
  759. memcpy(buffer, bo->metadata, bo->metadata_size);
  760. }
  761. if (metadata_size)
  762. *metadata_size = bo->metadata_size;
  763. if (flags)
  764. *flags = bo->metadata_flags;
  765. return 0;
  766. }
  767. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  768. bool evict,
  769. struct ttm_mem_reg *new_mem)
  770. {
  771. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  772. struct amdgpu_bo *abo;
  773. struct ttm_mem_reg *old_mem = &bo->mem;
  774. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  775. return;
  776. abo = ttm_to_amdgpu_bo(bo);
  777. amdgpu_vm_bo_invalidate(adev, abo, evict);
  778. amdgpu_bo_kunmap(abo);
  779. /* remember the eviction */
  780. if (evict)
  781. atomic64_inc(&adev->num_evictions);
  782. /* update statistics */
  783. if (!new_mem)
  784. return;
  785. /* move_notify is called before move happens */
  786. trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
  787. }
  788. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  789. {
  790. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  791. struct ttm_operation_ctx ctx = { false, false };
  792. struct amdgpu_bo *abo;
  793. unsigned long offset, size;
  794. int r;
  795. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  796. return 0;
  797. abo = ttm_to_amdgpu_bo(bo);
  798. /* Remember that this BO was accessed by the CPU */
  799. abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  800. if (bo->mem.mem_type != TTM_PL_VRAM)
  801. return 0;
  802. size = bo->mem.num_pages << PAGE_SHIFT;
  803. offset = bo->mem.start << PAGE_SHIFT;
  804. if ((offset + size) <= adev->gmc.visible_vram_size)
  805. return 0;
  806. /* Can't move a pinned BO to visible VRAM */
  807. if (abo->pin_count > 0)
  808. return -EINVAL;
  809. /* hurrah the memory is not visible ! */
  810. atomic64_inc(&adev->num_vram_cpu_page_faults);
  811. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
  812. AMDGPU_GEM_DOMAIN_GTT);
  813. /* Avoid costly evictions; only set GTT as a busy placement */
  814. abo->placement.num_busy_placement = 1;
  815. abo->placement.busy_placement = &abo->placements[1];
  816. r = ttm_bo_validate(bo, &abo->placement, &ctx);
  817. if (unlikely(r != 0))
  818. return r;
  819. offset = bo->mem.start << PAGE_SHIFT;
  820. /* this should never happen */
  821. if (bo->mem.mem_type == TTM_PL_VRAM &&
  822. (offset + size) > adev->gmc.visible_vram_size)
  823. return -EINVAL;
  824. return 0;
  825. }
  826. /**
  827. * amdgpu_bo_fence - add fence to buffer object
  828. *
  829. * @bo: buffer object in question
  830. * @fence: fence to add
  831. * @shared: true if fence should be added shared
  832. *
  833. */
  834. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
  835. bool shared)
  836. {
  837. struct reservation_object *resv = bo->tbo.resv;
  838. if (shared)
  839. reservation_object_add_shared_fence(resv, fence);
  840. else
  841. reservation_object_add_excl_fence(resv, fence);
  842. }
  843. /**
  844. * amdgpu_bo_gpu_offset - return GPU offset of bo
  845. * @bo: amdgpu object for which we query the offset
  846. *
  847. * Returns current GPU offset of the object.
  848. *
  849. * Note: object should either be pinned or reserved when calling this
  850. * function, it might be useful to add check for this for debugging.
  851. */
  852. u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
  853. {
  854. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
  855. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
  856. !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem));
  857. WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
  858. !bo->pin_count);
  859. WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
  860. WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
  861. !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
  862. return bo->tbo.offset;
  863. }