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