radeon_object.c 12 KB

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