amdgpu_object.c 24 KB

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