radeon_object.c 13 KB

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