radeon_object.c 14 KB

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