amdgpu_object.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. u64 ret = 0;
  45. if (mem->start << PAGE_SHIFT < adev->mc.visible_vram_size) {
  46. ret = (u64)((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. return ret;
  52. }
  53. static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
  54. struct ttm_mem_reg *old_mem,
  55. struct ttm_mem_reg *new_mem)
  56. {
  57. u64 vis_size;
  58. if (!adev)
  59. return;
  60. if (new_mem) {
  61. switch (new_mem->mem_type) {
  62. case TTM_PL_TT:
  63. atomic64_add(new_mem->size, &adev->gtt_usage);
  64. break;
  65. case TTM_PL_VRAM:
  66. atomic64_add(new_mem->size, &adev->vram_usage);
  67. vis_size = amdgpu_get_vis_part_size(adev, new_mem);
  68. atomic64_add(vis_size, &adev->vram_vis_usage);
  69. break;
  70. }
  71. }
  72. if (old_mem) {
  73. switch (old_mem->mem_type) {
  74. case TTM_PL_TT:
  75. atomic64_sub(old_mem->size, &adev->gtt_usage);
  76. break;
  77. case TTM_PL_VRAM:
  78. atomic64_sub(old_mem->size, &adev->vram_usage);
  79. vis_size = amdgpu_get_vis_part_size(adev, old_mem);
  80. atomic64_sub(vis_size, &adev->vram_vis_usage);
  81. break;
  82. }
  83. }
  84. }
  85. static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  86. {
  87. struct amdgpu_bo *bo;
  88. bo = container_of(tbo, struct amdgpu_bo, tbo);
  89. amdgpu_update_memory_usage(bo->adev, &bo->tbo.mem, NULL);
  90. mutex_lock(&bo->adev->gem.mutex);
  91. list_del_init(&bo->list);
  92. mutex_unlock(&bo->adev->gem.mutex);
  93. drm_gem_object_release(&bo->gem_base);
  94. amdgpu_bo_unref(&bo->parent);
  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 *placements,
  107. u32 domain, u64 flags)
  108. {
  109. u32 c = 0, i;
  110. placement->placement = placements;
  111. placement->busy_placement = placements;
  112. if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
  113. if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS &&
  114. adev->mc.visible_vram_size < adev->mc.real_vram_size) {
  115. placements[c].fpfn =
  116. adev->mc.visible_vram_size >> PAGE_SHIFT;
  117. placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  118. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_TOPDOWN;
  119. }
  120. placements[c].fpfn = 0;
  121. placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  122. TTM_PL_FLAG_VRAM;
  123. if (!(flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED))
  124. placements[c - 1].flags |= TTM_PL_FLAG_TOPDOWN;
  125. }
  126. if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  127. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) {
  128. placements[c].fpfn = 0;
  129. placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_TT |
  130. TTM_PL_FLAG_UNCACHED;
  131. } else {
  132. placements[c].fpfn = 0;
  133. placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_TT;
  134. }
  135. }
  136. if (domain & AMDGPU_GEM_DOMAIN_CPU) {
  137. if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) {
  138. placements[c].fpfn = 0;
  139. placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_SYSTEM |
  140. TTM_PL_FLAG_UNCACHED;
  141. } else {
  142. placements[c].fpfn = 0;
  143. placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM;
  144. }
  145. }
  146. if (domain & AMDGPU_GEM_DOMAIN_GDS) {
  147. placements[c].fpfn = 0;
  148. placements[c++].flags = TTM_PL_FLAG_UNCACHED |
  149. AMDGPU_PL_FLAG_GDS;
  150. }
  151. if (domain & AMDGPU_GEM_DOMAIN_GWS) {
  152. placements[c].fpfn = 0;
  153. placements[c++].flags = TTM_PL_FLAG_UNCACHED |
  154. AMDGPU_PL_FLAG_GWS;
  155. }
  156. if (domain & AMDGPU_GEM_DOMAIN_OA) {
  157. placements[c].fpfn = 0;
  158. placements[c++].flags = TTM_PL_FLAG_UNCACHED |
  159. AMDGPU_PL_FLAG_OA;
  160. }
  161. if (!c) {
  162. placements[c].fpfn = 0;
  163. placements[c++].flags = TTM_PL_MASK_CACHING |
  164. TTM_PL_FLAG_SYSTEM;
  165. }
  166. placement->num_placement = c;
  167. placement->num_busy_placement = c;
  168. for (i = 0; i < c; i++) {
  169. if ((flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
  170. (placements[i].flags & TTM_PL_FLAG_VRAM) &&
  171. !placements[i].fpfn)
  172. placements[i].lpfn =
  173. adev->mc.visible_vram_size >> PAGE_SHIFT;
  174. else
  175. placements[i].lpfn = 0;
  176. }
  177. }
  178. void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *rbo, u32 domain)
  179. {
  180. amdgpu_ttm_placement_init(rbo->adev, &rbo->placement,
  181. rbo->placements, domain, rbo->flags);
  182. }
  183. static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
  184. struct ttm_placement *placement)
  185. {
  186. BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
  187. memcpy(bo->placements, placement->placement,
  188. placement->num_placement * sizeof(struct ttm_place));
  189. bo->placement.num_placement = placement->num_placement;
  190. bo->placement.num_busy_placement = placement->num_busy_placement;
  191. bo->placement.placement = bo->placements;
  192. bo->placement.busy_placement = bo->placements;
  193. }
  194. int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
  195. unsigned long size, int byte_align,
  196. bool kernel, u32 domain, u64 flags,
  197. struct sg_table *sg,
  198. struct ttm_placement *placement,
  199. struct reservation_object *resv,
  200. struct amdgpu_bo **bo_ptr)
  201. {
  202. struct amdgpu_bo *bo;
  203. enum ttm_bo_type type;
  204. unsigned long page_align;
  205. size_t acc_size;
  206. int r;
  207. page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
  208. size = ALIGN(size, PAGE_SIZE);
  209. if (kernel) {
  210. type = ttm_bo_type_kernel;
  211. } else if (sg) {
  212. type = ttm_bo_type_sg;
  213. } else {
  214. type = ttm_bo_type_device;
  215. }
  216. *bo_ptr = NULL;
  217. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  218. sizeof(struct amdgpu_bo));
  219. bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
  220. if (bo == NULL)
  221. return -ENOMEM;
  222. r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
  223. if (unlikely(r)) {
  224. kfree(bo);
  225. return r;
  226. }
  227. bo->adev = adev;
  228. INIT_LIST_HEAD(&bo->list);
  229. INIT_LIST_HEAD(&bo->va);
  230. bo->initial_domain = domain & (AMDGPU_GEM_DOMAIN_VRAM |
  231. AMDGPU_GEM_DOMAIN_GTT |
  232. AMDGPU_GEM_DOMAIN_CPU |
  233. AMDGPU_GEM_DOMAIN_GDS |
  234. AMDGPU_GEM_DOMAIN_GWS |
  235. AMDGPU_GEM_DOMAIN_OA);
  236. bo->flags = flags;
  237. /* For architectures that don't support WC memory,
  238. * mask out the WC flag from the BO
  239. */
  240. if (!drm_arch_can_wc_memory())
  241. bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  242. amdgpu_fill_placement_to_bo(bo, placement);
  243. /* Kernel allocation are uninterruptible */
  244. r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
  245. &bo->placement, page_align, !kernel, NULL,
  246. acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
  247. if (unlikely(r != 0)) {
  248. return r;
  249. }
  250. *bo_ptr = bo;
  251. trace_amdgpu_bo_create(bo);
  252. return 0;
  253. }
  254. int amdgpu_bo_create(struct amdgpu_device *adev,
  255. unsigned long size, int byte_align,
  256. bool kernel, u32 domain, u64 flags,
  257. struct sg_table *sg,
  258. struct reservation_object *resv,
  259. struct amdgpu_bo **bo_ptr)
  260. {
  261. struct ttm_placement placement = {0};
  262. struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
  263. memset(&placements, 0,
  264. (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
  265. amdgpu_ttm_placement_init(adev, &placement,
  266. placements, domain, flags);
  267. return amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
  268. domain, flags, sg, &placement,
  269. resv, bo_ptr);
  270. }
  271. int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
  272. {
  273. bool is_iomem;
  274. int r;
  275. if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
  276. return -EPERM;
  277. if (bo->kptr) {
  278. if (ptr) {
  279. *ptr = bo->kptr;
  280. }
  281. return 0;
  282. }
  283. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  284. if (r) {
  285. return r;
  286. }
  287. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  288. if (ptr) {
  289. *ptr = bo->kptr;
  290. }
  291. return 0;
  292. }
  293. void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
  294. {
  295. if (bo->kptr == NULL)
  296. return;
  297. bo->kptr = NULL;
  298. ttm_bo_kunmap(&bo->kmap);
  299. }
  300. struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
  301. {
  302. if (bo == NULL)
  303. return NULL;
  304. ttm_bo_reference(&bo->tbo);
  305. return bo;
  306. }
  307. void amdgpu_bo_unref(struct amdgpu_bo **bo)
  308. {
  309. struct ttm_buffer_object *tbo;
  310. if ((*bo) == NULL)
  311. return;
  312. tbo = &((*bo)->tbo);
  313. ttm_bo_unref(&tbo);
  314. if (tbo == NULL)
  315. *bo = NULL;
  316. }
  317. int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
  318. u64 min_offset, u64 max_offset,
  319. u64 *gpu_addr)
  320. {
  321. int r, i;
  322. unsigned fpfn, lpfn;
  323. if (amdgpu_ttm_tt_has_userptr(bo->tbo.ttm))
  324. return -EPERM;
  325. if (WARN_ON_ONCE(min_offset > max_offset))
  326. return -EINVAL;
  327. if (bo->pin_count) {
  328. bo->pin_count++;
  329. if (gpu_addr)
  330. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  331. if (max_offset != 0) {
  332. u64 domain_start;
  333. if (domain == AMDGPU_GEM_DOMAIN_VRAM)
  334. domain_start = bo->adev->mc.vram_start;
  335. else
  336. domain_start = bo->adev->mc.gtt_start;
  337. WARN_ON_ONCE(max_offset <
  338. (amdgpu_bo_gpu_offset(bo) - domain_start));
  339. }
  340. return 0;
  341. }
  342. amdgpu_ttm_placement_from_domain(bo, domain);
  343. for (i = 0; i < bo->placement.num_placement; i++) {
  344. /* force to pin into visible video ram */
  345. if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  346. !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
  347. (!max_offset || max_offset > bo->adev->mc.visible_vram_size)) {
  348. if (WARN_ON_ONCE(min_offset >
  349. bo->adev->mc.visible_vram_size))
  350. return -EINVAL;
  351. fpfn = min_offset >> PAGE_SHIFT;
  352. lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT;
  353. } else {
  354. fpfn = min_offset >> PAGE_SHIFT;
  355. lpfn = max_offset >> PAGE_SHIFT;
  356. }
  357. if (fpfn > bo->placements[i].fpfn)
  358. bo->placements[i].fpfn = fpfn;
  359. if (!bo->placements[i].lpfn ||
  360. (lpfn && lpfn < bo->placements[i].lpfn))
  361. bo->placements[i].lpfn = lpfn;
  362. bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
  363. }
  364. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  365. if (likely(r == 0)) {
  366. bo->pin_count = 1;
  367. if (gpu_addr != NULL)
  368. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  369. if (domain == AMDGPU_GEM_DOMAIN_VRAM)
  370. bo->adev->vram_pin_size += amdgpu_bo_size(bo);
  371. else
  372. bo->adev->gart_pin_size += amdgpu_bo_size(bo);
  373. } else {
  374. dev_err(bo->adev->dev, "%p pin failed\n", bo);
  375. }
  376. return r;
  377. }
  378. int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
  379. {
  380. return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
  381. }
  382. int amdgpu_bo_unpin(struct amdgpu_bo *bo)
  383. {
  384. int r, i;
  385. if (!bo->pin_count) {
  386. dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo);
  387. return 0;
  388. }
  389. bo->pin_count--;
  390. if (bo->pin_count)
  391. return 0;
  392. for (i = 0; i < bo->placement.num_placement; i++) {
  393. bo->placements[i].lpfn = 0;
  394. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  395. }
  396. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  397. if (likely(r == 0)) {
  398. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  399. bo->adev->vram_pin_size -= amdgpu_bo_size(bo);
  400. else
  401. bo->adev->gart_pin_size -= amdgpu_bo_size(bo);
  402. } else {
  403. dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo);
  404. }
  405. return r;
  406. }
  407. int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
  408. {
  409. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  410. if (0 && (adev->flags & AMD_IS_APU)) {
  411. /* Useless to evict on IGP chips */
  412. return 0;
  413. }
  414. return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
  415. }
  416. void amdgpu_bo_force_delete(struct amdgpu_device *adev)
  417. {
  418. struct amdgpu_bo *bo, *n;
  419. if (list_empty(&adev->gem.objects)) {
  420. return;
  421. }
  422. dev_err(adev->dev, "Userspace still has active objects !\n");
  423. list_for_each_entry_safe(bo, n, &adev->gem.objects, list) {
  424. dev_err(adev->dev, "%p %p %lu %lu force free\n",
  425. &bo->gem_base, bo, (unsigned long)bo->gem_base.size,
  426. *((unsigned long *)&bo->gem_base.refcount));
  427. mutex_lock(&bo->adev->gem.mutex);
  428. list_del_init(&bo->list);
  429. mutex_unlock(&bo->adev->gem.mutex);
  430. /* this should unref the ttm bo */
  431. drm_gem_object_unreference_unlocked(&bo->gem_base);
  432. }
  433. }
  434. int amdgpu_bo_init(struct amdgpu_device *adev)
  435. {
  436. /* reserve PAT memory space to WC for VRAM */
  437. arch_io_reserve_memtype_wc(adev->mc.aper_base,
  438. adev->mc.aper_size);
  439. /* Add an MTRR for the VRAM */
  440. adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
  441. adev->mc.aper_size);
  442. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  443. adev->mc.mc_vram_size >> 20,
  444. (unsigned long long)adev->mc.aper_size >> 20);
  445. DRM_INFO("RAM width %dbits DDR\n",
  446. adev->mc.vram_width);
  447. return amdgpu_ttm_init(adev);
  448. }
  449. void amdgpu_bo_fini(struct amdgpu_device *adev)
  450. {
  451. amdgpu_ttm_fini(adev);
  452. arch_phys_wc_del(adev->mc.vram_mtrr);
  453. arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.aper_size);
  454. }
  455. int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
  456. struct vm_area_struct *vma)
  457. {
  458. return ttm_fbdev_mmap(vma, &bo->tbo);
  459. }
  460. int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
  461. {
  462. if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
  463. return -EINVAL;
  464. bo->tiling_flags = tiling_flags;
  465. return 0;
  466. }
  467. void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
  468. {
  469. lockdep_assert_held(&bo->tbo.resv->lock.base);
  470. if (tiling_flags)
  471. *tiling_flags = bo->tiling_flags;
  472. }
  473. int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
  474. uint32_t metadata_size, uint64_t flags)
  475. {
  476. void *buffer;
  477. if (!metadata_size) {
  478. if (bo->metadata_size) {
  479. kfree(bo->metadata);
  480. bo->metadata = NULL;
  481. bo->metadata_size = 0;
  482. }
  483. return 0;
  484. }
  485. if (metadata == NULL)
  486. return -EINVAL;
  487. buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
  488. if (buffer == NULL)
  489. return -ENOMEM;
  490. kfree(bo->metadata);
  491. bo->metadata_flags = flags;
  492. bo->metadata = buffer;
  493. bo->metadata_size = metadata_size;
  494. return 0;
  495. }
  496. int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
  497. size_t buffer_size, uint32_t *metadata_size,
  498. uint64_t *flags)
  499. {
  500. if (!buffer && !metadata_size)
  501. return -EINVAL;
  502. if (buffer) {
  503. if (buffer_size < bo->metadata_size)
  504. return -EINVAL;
  505. if (bo->metadata_size)
  506. memcpy(buffer, bo->metadata, bo->metadata_size);
  507. }
  508. if (metadata_size)
  509. *metadata_size = bo->metadata_size;
  510. if (flags)
  511. *flags = bo->metadata_flags;
  512. return 0;
  513. }
  514. void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  515. struct ttm_mem_reg *new_mem)
  516. {
  517. struct amdgpu_bo *rbo;
  518. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  519. return;
  520. rbo = container_of(bo, struct amdgpu_bo, tbo);
  521. amdgpu_vm_bo_invalidate(rbo->adev, rbo);
  522. /* update statistics */
  523. if (!new_mem)
  524. return;
  525. /* move_notify is called before move happens */
  526. amdgpu_update_memory_usage(rbo->adev, &bo->mem, new_mem);
  527. }
  528. int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  529. {
  530. struct amdgpu_device *adev;
  531. struct amdgpu_bo *abo;
  532. unsigned long offset, size, lpfn;
  533. int i, r;
  534. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
  535. return 0;
  536. abo = container_of(bo, struct amdgpu_bo, tbo);
  537. adev = abo->adev;
  538. if (bo->mem.mem_type != TTM_PL_VRAM)
  539. return 0;
  540. size = bo->mem.num_pages << PAGE_SHIFT;
  541. offset = bo->mem.start << PAGE_SHIFT;
  542. if ((offset + size) <= adev->mc.visible_vram_size)
  543. return 0;
  544. /* hurrah the memory is not visible ! */
  545. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
  546. lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  547. for (i = 0; i < abo->placement.num_placement; i++) {
  548. /* Force into visible VRAM */
  549. if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
  550. (!abo->placements[i].lpfn || abo->placements[i].lpfn > lpfn))
  551. abo->placements[i].lpfn = lpfn;
  552. }
  553. r = ttm_bo_validate(bo, &abo->placement, false, false);
  554. if (unlikely(r == -ENOMEM)) {
  555. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
  556. return ttm_bo_validate(bo, &abo->placement, false, false);
  557. } else if (unlikely(r != 0)) {
  558. return r;
  559. }
  560. offset = bo->mem.start << PAGE_SHIFT;
  561. /* this should never happen */
  562. if ((offset + size) > adev->mc.visible_vram_size)
  563. return -EINVAL;
  564. return 0;
  565. }
  566. /**
  567. * amdgpu_bo_fence - add fence to buffer object
  568. *
  569. * @bo: buffer object in question
  570. * @fence: fence to add
  571. * @shared: true if fence should be added shared
  572. *
  573. */
  574. void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence,
  575. bool shared)
  576. {
  577. struct reservation_object *resv = bo->tbo.resv;
  578. if (shared)
  579. reservation_object_add_shared_fence(resv, fence);
  580. else
  581. reservation_object_add_excl_fence(resv, fence);
  582. }