radeon_object.c 13 KB

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