radeon_object.c 13 KB

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