radeon_object.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. radeon_ttm_placement_from_domain(bo, domain);
  164. if (bo->pin_count) {
  165. bo->pin_count++;
  166. if (gpu_addr)
  167. *gpu_addr = radeon_bo_gpu_offset(bo);
  168. return 0;
  169. }
  170. radeon_ttm_placement_from_domain(bo, domain);
  171. for (i = 0; i < bo->placement.num_placement; i++)
  172. bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  173. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  174. if (likely(r == 0)) {
  175. bo->pin_count = 1;
  176. if (gpu_addr != NULL)
  177. *gpu_addr = radeon_bo_gpu_offset(bo);
  178. }
  179. if (unlikely(r != 0))
  180. dev_err(bo->rdev->dev, "%p pin failed\n", bo);
  181. return r;
  182. }
  183. int radeon_bo_unpin(struct radeon_bo *bo)
  184. {
  185. int r, i;
  186. if (!bo->pin_count) {
  187. dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
  188. return 0;
  189. }
  190. bo->pin_count--;
  191. if (bo->pin_count)
  192. return 0;
  193. for (i = 0; i < bo->placement.num_placement; i++)
  194. bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
  195. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  196. if (unlikely(r != 0))
  197. dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
  198. return r;
  199. }
  200. int radeon_bo_evict_vram(struct radeon_device *rdev)
  201. {
  202. /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
  203. if (0 && (rdev->flags & RADEON_IS_IGP)) {
  204. if (rdev->mc.igp_sideport_enabled == false)
  205. /* Useless to evict on IGP chips */
  206. return 0;
  207. }
  208. return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
  209. }
  210. void radeon_bo_force_delete(struct radeon_device *rdev)
  211. {
  212. struct radeon_bo *bo, *n;
  213. struct drm_gem_object *gobj;
  214. if (list_empty(&rdev->gem.objects)) {
  215. return;
  216. }
  217. dev_err(rdev->dev, "Userspace still has active objects !\n");
  218. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  219. mutex_lock(&rdev->ddev->struct_mutex);
  220. gobj = bo->gobj;
  221. dev_err(rdev->dev, "%p %p %lu %lu force free\n",
  222. gobj, bo, (unsigned long)gobj->size,
  223. *((unsigned long *)&gobj->refcount));
  224. mutex_lock(&bo->rdev->gem.mutex);
  225. list_del_init(&bo->list);
  226. mutex_unlock(&bo->rdev->gem.mutex);
  227. radeon_bo_unref(&bo);
  228. gobj->driver_private = NULL;
  229. drm_gem_object_unreference(gobj);
  230. mutex_unlock(&rdev->ddev->struct_mutex);
  231. }
  232. }
  233. int radeon_bo_init(struct radeon_device *rdev)
  234. {
  235. /* Add an MTRR for the VRAM */
  236. rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
  237. MTRR_TYPE_WRCOMB, 1);
  238. DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
  239. rdev->mc.mc_vram_size >> 20,
  240. (unsigned long long)rdev->mc.aper_size >> 20);
  241. DRM_INFO("RAM width %dbits %cDR\n",
  242. rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
  243. return radeon_ttm_init(rdev);
  244. }
  245. void radeon_bo_fini(struct radeon_device *rdev)
  246. {
  247. radeon_ttm_fini(rdev);
  248. }
  249. void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
  250. struct list_head *head)
  251. {
  252. if (lobj->wdomain) {
  253. list_add(&lobj->list, head);
  254. } else {
  255. list_add_tail(&lobj->list, head);
  256. }
  257. }
  258. int radeon_bo_list_reserve(struct list_head *head)
  259. {
  260. struct radeon_bo_list *lobj;
  261. int r;
  262. list_for_each_entry(lobj, head, list){
  263. r = radeon_bo_reserve(lobj->bo, false);
  264. if (unlikely(r != 0))
  265. return r;
  266. }
  267. return 0;
  268. }
  269. void radeon_bo_list_unreserve(struct list_head *head)
  270. {
  271. struct radeon_bo_list *lobj;
  272. list_for_each_entry(lobj, head, list) {
  273. /* only unreserve object we successfully reserved */
  274. if (radeon_bo_is_reserved(lobj->bo))
  275. radeon_bo_unreserve(lobj->bo);
  276. }
  277. }
  278. int radeon_bo_list_validate(struct list_head *head)
  279. {
  280. struct radeon_bo_list *lobj;
  281. struct radeon_bo *bo;
  282. int r;
  283. r = radeon_bo_list_reserve(head);
  284. if (unlikely(r != 0)) {
  285. return r;
  286. }
  287. list_for_each_entry(lobj, head, list) {
  288. bo = lobj->bo;
  289. if (!bo->pin_count) {
  290. if (lobj->wdomain) {
  291. radeon_ttm_placement_from_domain(bo,
  292. lobj->wdomain);
  293. } else {
  294. radeon_ttm_placement_from_domain(bo,
  295. lobj->rdomain);
  296. }
  297. r = ttm_bo_validate(&bo->tbo, &bo->placement,
  298. true, false);
  299. if (unlikely(r))
  300. return r;
  301. }
  302. lobj->gpu_offset = radeon_bo_gpu_offset(bo);
  303. lobj->tiling_flags = bo->tiling_flags;
  304. }
  305. return 0;
  306. }
  307. void radeon_bo_list_fence(struct list_head *head, void *fence)
  308. {
  309. struct radeon_bo_list *lobj;
  310. struct radeon_bo *bo;
  311. struct radeon_fence *old_fence = NULL;
  312. list_for_each_entry(lobj, head, list) {
  313. bo = lobj->bo;
  314. spin_lock(&bo->tbo.lock);
  315. old_fence = (struct radeon_fence *)bo->tbo.sync_obj;
  316. bo->tbo.sync_obj = radeon_fence_ref(fence);
  317. bo->tbo.sync_obj_arg = NULL;
  318. spin_unlock(&bo->tbo.lock);
  319. if (old_fence) {
  320. radeon_fence_unref(&old_fence);
  321. }
  322. }
  323. }
  324. int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
  325. struct vm_area_struct *vma)
  326. {
  327. return ttm_fbdev_mmap(vma, &bo->tbo);
  328. }
  329. int radeon_bo_get_surface_reg(struct radeon_bo *bo)
  330. {
  331. struct radeon_device *rdev = bo->rdev;
  332. struct radeon_surface_reg *reg;
  333. struct radeon_bo *old_object;
  334. int steal;
  335. int i;
  336. BUG_ON(!atomic_read(&bo->tbo.reserved));
  337. if (!bo->tiling_flags)
  338. return 0;
  339. if (bo->surface_reg >= 0) {
  340. reg = &rdev->surface_regs[bo->surface_reg];
  341. i = bo->surface_reg;
  342. goto out;
  343. }
  344. steal = -1;
  345. for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
  346. reg = &rdev->surface_regs[i];
  347. if (!reg->bo)
  348. break;
  349. old_object = reg->bo;
  350. if (old_object->pin_count == 0)
  351. steal = i;
  352. }
  353. /* if we are all out */
  354. if (i == RADEON_GEM_MAX_SURFACES) {
  355. if (steal == -1)
  356. return -ENOMEM;
  357. /* find someone with a surface reg and nuke their BO */
  358. reg = &rdev->surface_regs[steal];
  359. old_object = reg->bo;
  360. /* blow away the mapping */
  361. DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
  362. ttm_bo_unmap_virtual(&old_object->tbo);
  363. old_object->surface_reg = -1;
  364. i = steal;
  365. }
  366. bo->surface_reg = i;
  367. reg->bo = bo;
  368. out:
  369. radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
  370. bo->tbo.mem.mm_node->start << PAGE_SHIFT,
  371. bo->tbo.num_pages << PAGE_SHIFT);
  372. return 0;
  373. }
  374. static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
  375. {
  376. struct radeon_device *rdev = bo->rdev;
  377. struct radeon_surface_reg *reg;
  378. if (bo->surface_reg == -1)
  379. return;
  380. reg = &rdev->surface_regs[bo->surface_reg];
  381. radeon_clear_surface_reg(rdev, bo->surface_reg);
  382. reg->bo = NULL;
  383. bo->surface_reg = -1;
  384. }
  385. int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
  386. uint32_t tiling_flags, uint32_t pitch)
  387. {
  388. int r;
  389. r = radeon_bo_reserve(bo, false);
  390. if (unlikely(r != 0))
  391. return r;
  392. bo->tiling_flags = tiling_flags;
  393. bo->pitch = pitch;
  394. radeon_bo_unreserve(bo);
  395. return 0;
  396. }
  397. void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
  398. uint32_t *tiling_flags,
  399. uint32_t *pitch)
  400. {
  401. BUG_ON(!atomic_read(&bo->tbo.reserved));
  402. if (tiling_flags)
  403. *tiling_flags = bo->tiling_flags;
  404. if (pitch)
  405. *pitch = bo->pitch;
  406. }
  407. int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
  408. bool force_drop)
  409. {
  410. BUG_ON(!atomic_read(&bo->tbo.reserved));
  411. if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
  412. return 0;
  413. if (force_drop) {
  414. radeon_bo_clear_surface_reg(bo);
  415. return 0;
  416. }
  417. if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
  418. if (!has_moved)
  419. return 0;
  420. if (bo->surface_reg >= 0)
  421. radeon_bo_clear_surface_reg(bo);
  422. return 0;
  423. }
  424. if ((bo->surface_reg >= 0) && !has_moved)
  425. return 0;
  426. return radeon_bo_get_surface_reg(bo);
  427. }
  428. void radeon_bo_move_notify(struct ttm_buffer_object *bo,
  429. struct ttm_mem_reg *mem)
  430. {
  431. struct radeon_bo *rbo;
  432. if (!radeon_ttm_bo_is_radeon_bo(bo))
  433. return;
  434. rbo = container_of(bo, struct radeon_bo, tbo);
  435. radeon_bo_check_tiling(rbo, 0, 1);
  436. }
  437. void radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
  438. {
  439. struct radeon_bo *rbo;
  440. if (!radeon_ttm_bo_is_radeon_bo(bo))
  441. return;
  442. rbo = container_of(bo, struct radeon_bo, tbo);
  443. radeon_bo_check_tiling(rbo, 0, 0);
  444. }