amdgpu_object.c 23 KB

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