amdgpu_object.c 23 KB

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