radeon_object.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 <drm/drmP.h>
  34. #include "radeon_drm.h"
  35. #include "radeon.h"
  36. int radeon_ttm_init(struct radeon_device *rdev);
  37. void radeon_ttm_fini(struct radeon_device *rdev);
  38. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
  39. /*
  40. * To exclude mutual BO access we rely on bo_reserve exclusion, as all
  41. * function are calling it.
  42. */
  43. static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  44. {
  45. struct radeon_bo *bo;
  46. bo = container_of(tbo, struct radeon_bo, tbo);
  47. mutex_lock(&bo->rdev->gem.mutex);
  48. list_del_init(&bo->list);
  49. mutex_unlock(&bo->rdev->gem.mutex);
  50. radeon_bo_clear_surface_reg(bo);
  51. kfree(bo);
  52. }
  53. void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
  54. {
  55. u32 c = 0;
  56. rbo->placement.fpfn = 0;
  57. rbo->placement.lpfn = 0;
  58. rbo->placement.placement = rbo->placements;
  59. rbo->placement.busy_placement = rbo->placements;
  60. if (domain & RADEON_GEM_DOMAIN_VRAM)
  61. rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  62. TTM_PL_FLAG_VRAM;
  63. if (domain & RADEON_GEM_DOMAIN_GTT)
  64. rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  65. if (domain & RADEON_GEM_DOMAIN_CPU)
  66. rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  67. rbo->placement.num_placement = c;
  68. rbo->placement.num_busy_placement = c;
  69. }
  70. int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj,
  71. unsigned long size, bool kernel, u32 domain,
  72. struct radeon_bo **bo_ptr)
  73. {
  74. struct radeon_bo *bo;
  75. enum ttm_bo_type type;
  76. int r;
  77. if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
  78. rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
  79. }
  80. if (kernel) {
  81. type = ttm_bo_type_kernel;
  82. } else {
  83. type = ttm_bo_type_device;
  84. }
  85. *bo_ptr = NULL;
  86. bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
  87. if (bo == NULL)
  88. return -ENOMEM;
  89. bo->rdev = rdev;
  90. bo->gobj = gobj;
  91. bo->surface_reg = -1;
  92. INIT_LIST_HEAD(&bo->list);
  93. radeon_ttm_placement_from_domain(bo, domain);
  94. /* Kernel allocation are uninterruptible */
  95. r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
  96. &bo->placement, 0, 0, !kernel, NULL, size,
  97. &radeon_ttm_bo_destroy);
  98. if (unlikely(r != 0)) {
  99. if (r != -ERESTARTSYS)
  100. dev_err(rdev->dev,
  101. "object_init failed for (%lu, 0x%08X)\n",
  102. size, domain);
  103. return r;
  104. }
  105. *bo_ptr = bo;
  106. if (gobj) {
  107. mutex_lock(&bo->rdev->gem.mutex);
  108. list_add_tail(&bo->list, &rdev->gem.objects);
  109. mutex_unlock(&bo->rdev->gem.mutex);
  110. }
  111. return 0;
  112. }
  113. int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
  114. {
  115. bool is_iomem;
  116. int r;
  117. if (bo->kptr) {
  118. if (ptr) {
  119. *ptr = bo->kptr;
  120. }
  121. return 0;
  122. }
  123. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  124. if (r) {
  125. return r;
  126. }
  127. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  128. if (ptr) {
  129. *ptr = bo->kptr;
  130. }
  131. radeon_bo_check_tiling(bo, 0, 0);
  132. return 0;
  133. }
  134. void radeon_bo_kunmap(struct radeon_bo *bo)
  135. {
  136. if (bo->kptr == NULL)
  137. return;
  138. bo->kptr = NULL;
  139. radeon_bo_check_tiling(bo, 0, 0);
  140. ttm_bo_kunmap(&bo->kmap);
  141. }
  142. void radeon_bo_unref(struct radeon_bo **bo)
  143. {
  144. struct ttm_buffer_object *tbo;
  145. if ((*bo) == NULL)
  146. return;
  147. tbo = &((*bo)->tbo);
  148. ttm_bo_unref(&tbo);
  149. if (tbo == NULL)
  150. *bo = NULL;
  151. }
  152. int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
  153. {
  154. int r, i;
  155. radeon_ttm_placement_from_domain(bo, domain);
  156. if (bo->pin_count) {
  157. bo->pin_count++;
  158. if (gpu_addr)
  159. *gpu_addr = radeon_bo_gpu_offset(bo);
  160. return 0;
  161. }
  162. radeon_ttm_placement_from_domain(bo, domain);
  163. for (i = 0; i < bo->placement.num_placement; i++)
  164. bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  165. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  166. if (likely(r == 0)) {
  167. bo->pin_count = 1;
  168. if (gpu_addr != NULL)
  169. *gpu_addr = radeon_bo_gpu_offset(bo);
  170. }
  171. if (unlikely(r != 0))
  172. dev_err(bo->rdev->dev, "%p pin failed\n", bo);
  173. return r;
  174. }
  175. int radeon_bo_unpin(struct radeon_bo *bo)
  176. {
  177. int r, i;
  178. if (!bo->pin_count) {
  179. dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
  180. return 0;
  181. }
  182. bo->pin_count--;
  183. if (bo->pin_count)
  184. return 0;
  185. for (i = 0; i < bo->placement.num_placement; i++)
  186. bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
  187. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  188. if (unlikely(r != 0))
  189. dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
  190. return r;
  191. }
  192. int radeon_bo_evict_vram(struct radeon_device *rdev)
  193. {
  194. if (rdev->flags & RADEON_IS_IGP) {
  195. /* Useless to evict on IGP chips */
  196. return 0;
  197. }
  198. return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  199. }
  200. void radeon_bo_force_delete(struct radeon_device *rdev)
  201. {
  202. struct radeon_bo *bo, *n;
  203. struct drm_gem_object *gobj;
  204. if (list_empty(&rdev->gem.objects)) {
  205. return;
  206. }
  207. dev_err(rdev->dev, "Userspace still has active objects !\n");
  208. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  209. mutex_lock(&rdev->ddev->struct_mutex);
  210. gobj = bo->gobj;
  211. dev_err(rdev->dev, "%p %p %lu %lu force free\n",
  212. gobj, bo, (unsigned long)gobj->size,
  213. *((unsigned long *)&gobj->refcount));
  214. mutex_lock(&bo->rdev->gem.mutex);
  215. list_del_init(&bo->list);
  216. mutex_unlock(&bo->rdev->gem.mutex);
  217. radeon_bo_unref(&bo);
  218. gobj->driver_private = NULL;
  219. drm_gem_object_unreference(gobj);
  220. mutex_unlock(&rdev->ddev->struct_mutex);
  221. }
  222. }
  223. int radeon_bo_init(struct radeon_device *rdev)
  224. {
  225. /* Add an MTRR for the VRAM */
  226. rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
  227. MTRR_TYPE_WRCOMB, 1);
  228. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  229. rdev->mc.mc_vram_size >> 20,
  230. (unsigned long long)rdev->mc.aper_size >> 20);
  231. DRM_INFO("RAM width %dbits %cDR\n",
  232. rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
  233. return radeon_ttm_init(rdev);
  234. }
  235. void radeon_bo_fini(struct radeon_device *rdev)
  236. {
  237. radeon_ttm_fini(rdev);
  238. }
  239. void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
  240. struct list_head *head)
  241. {
  242. if (lobj->wdomain) {
  243. list_add(&lobj->list, head);
  244. } else {
  245. list_add_tail(&lobj->list, head);
  246. }
  247. }
  248. int radeon_bo_list_reserve(struct list_head *head)
  249. {
  250. struct radeon_bo_list *lobj;
  251. int r;
  252. list_for_each_entry(lobj, head, list){
  253. r = radeon_bo_reserve(lobj->bo, false);
  254. if (unlikely(r != 0))
  255. return r;
  256. }
  257. return 0;
  258. }
  259. void radeon_bo_list_unreserve(struct list_head *head)
  260. {
  261. struct radeon_bo_list *lobj;
  262. list_for_each_entry(lobj, head, list) {
  263. /* only unreserve object we successfully reserved */
  264. if (radeon_bo_is_reserved(lobj->bo))
  265. radeon_bo_unreserve(lobj->bo);
  266. }
  267. }
  268. int radeon_bo_list_validate(struct list_head *head, void *fence)
  269. {
  270. struct radeon_bo_list *lobj;
  271. struct radeon_bo *bo;
  272. struct radeon_fence *old_fence = NULL;
  273. int r;
  274. r = radeon_bo_list_reserve(head);
  275. if (unlikely(r != 0)) {
  276. return r;
  277. }
  278. list_for_each_entry(lobj, head, list) {
  279. bo = lobj->bo;
  280. if (!bo->pin_count) {
  281. if (lobj->wdomain) {
  282. radeon_ttm_placement_from_domain(bo,
  283. lobj->wdomain);
  284. } else {
  285. radeon_ttm_placement_from_domain(bo,
  286. lobj->rdomain);
  287. }
  288. r = ttm_bo_validate(&bo->tbo, &bo->placement,
  289. true, false);
  290. if (unlikely(r))
  291. return r;
  292. }
  293. lobj->gpu_offset = radeon_bo_gpu_offset(bo);
  294. lobj->tiling_flags = bo->tiling_flags;
  295. if (fence) {
  296. old_fence = (struct radeon_fence *)bo->tbo.sync_obj;
  297. bo->tbo.sync_obj = radeon_fence_ref(fence);
  298. bo->tbo.sync_obj_arg = NULL;
  299. }
  300. if (old_fence) {
  301. radeon_fence_unref(&old_fence);
  302. }
  303. }
  304. return 0;
  305. }
  306. void radeon_bo_list_unvalidate(struct list_head *head, void *fence)
  307. {
  308. struct radeon_bo_list *lobj;
  309. struct radeon_fence *old_fence;
  310. if (fence)
  311. list_for_each_entry(lobj, head, list) {
  312. old_fence = to_radeon_fence(lobj->bo->tbo.sync_obj);
  313. if (old_fence == fence) {
  314. lobj->bo->tbo.sync_obj = NULL;
  315. radeon_fence_unref(&old_fence);
  316. }
  317. }
  318. radeon_bo_list_unreserve(head);
  319. }
  320. int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
  321. struct vm_area_struct *vma)
  322. {
  323. return ttm_fbdev_mmap(vma, &bo->tbo);
  324. }
  325. int radeon_bo_get_surface_reg(struct radeon_bo *bo)
  326. {
  327. struct radeon_device *rdev = bo->rdev;
  328. struct radeon_surface_reg *reg;
  329. struct radeon_bo *old_object;
  330. int steal;
  331. int i;
  332. BUG_ON(!atomic_read(&bo->tbo.reserved));
  333. if (!bo->tiling_flags)
  334. return 0;
  335. if (bo->surface_reg >= 0) {
  336. reg = &rdev->surface_regs[bo->surface_reg];
  337. i = bo->surface_reg;
  338. goto out;
  339. }
  340. steal = -1;
  341. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  342. reg = &rdev->surface_regs[i];
  343. if (!reg->bo)
  344. break;
  345. old_object = reg->bo;
  346. if (old_object->pin_count == 0)
  347. steal = i;
  348. }
  349. /* if we are all out */
  350. if (i == RADEON_GEM_MAX_SURFACES) {
  351. if (steal == -1)
  352. return -ENOMEM;
  353. /* find someone with a surface reg and nuke their BO */
  354. reg = &rdev->surface_regs[steal];
  355. old_object = reg->bo;
  356. /* blow away the mapping */
  357. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  358. ttm_bo_unmap_virtual(&old_object->tbo);
  359. old_object->surface_reg = -1;
  360. i = steal;
  361. }
  362. bo->surface_reg = i;
  363. reg->bo = bo;
  364. out:
  365. radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
  366. bo->tbo.mem.mm_node->start << PAGE_SHIFT,
  367. bo->tbo.num_pages << PAGE_SHIFT);
  368. return 0;
  369. }
  370. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
  371. {
  372. struct radeon_device *rdev = bo->rdev;
  373. struct radeon_surface_reg *reg;
  374. if (bo->surface_reg == -1)
  375. return;
  376. reg = &rdev->surface_regs[bo->surface_reg];
  377. radeon_clear_surface_reg(rdev, bo->surface_reg);
  378. reg->bo = NULL;
  379. bo->surface_reg = -1;
  380. }
  381. int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
  382. uint32_t tiling_flags, uint32_t pitch)
  383. {
  384. int r;
  385. r = radeon_bo_reserve(bo, false);
  386. if (unlikely(r != 0))
  387. return r;
  388. bo->tiling_flags = tiling_flags;
  389. bo->pitch = pitch;
  390. radeon_bo_unreserve(bo);
  391. return 0;
  392. }
  393. void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
  394. uint32_t *tiling_flags,
  395. uint32_t *pitch)
  396. {
  397. BUG_ON(!atomic_read(&bo->tbo.reserved));
  398. if (tiling_flags)
  399. *tiling_flags = bo->tiling_flags;
  400. if (pitch)
  401. *pitch = bo->pitch;
  402. }
  403. int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
  404. bool force_drop)
  405. {
  406. BUG_ON(!atomic_read(&bo->tbo.reserved));
  407. if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
  408. return 0;
  409. if (force_drop) {
  410. radeon_bo_clear_surface_reg(bo);
  411. return 0;
  412. }
  413. if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
  414. if (!has_moved)
  415. return 0;
  416. if (bo->surface_reg >= 0)
  417. radeon_bo_clear_surface_reg(bo);
  418. return 0;
  419. }
  420. if ((bo->surface_reg >= 0) && !has_moved)
  421. return 0;
  422. return radeon_bo_get_surface_reg(bo);
  423. }
  424. void radeon_bo_move_notify(struct ttm_buffer_object *bo,
  425. struct ttm_mem_reg *mem)
  426. {
  427. struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
  428. radeon_bo_check_tiling(rbo, 0, 1);
  429. }
  430. void radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  431. {
  432. struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
  433. radeon_bo_check_tiling(rbo, 0, 0);
  434. }