radeon_object.c 12 KB

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