qxl_object.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include "qxl_drv.h"
  26. #include "qxl_object.h"
  27. #include <linux/io-mapping.h>
  28. static void qxl_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  29. {
  30. struct qxl_bo *bo;
  31. struct qxl_device *qdev;
  32. bo = container_of(tbo, struct qxl_bo, tbo);
  33. qdev = (struct qxl_device *)bo->gem_base.dev->dev_private;
  34. qxl_surface_evict(qdev, bo, false);
  35. mutex_lock(&qdev->gem.mutex);
  36. list_del_init(&bo->list);
  37. mutex_unlock(&qdev->gem.mutex);
  38. drm_gem_object_release(&bo->gem_base);
  39. kfree(bo);
  40. }
  41. bool qxl_ttm_bo_is_qxl_bo(struct ttm_buffer_object *bo)
  42. {
  43. if (bo->destroy == &qxl_ttm_bo_destroy)
  44. return true;
  45. return false;
  46. }
  47. void qxl_ttm_placement_from_domain(struct qxl_bo *qbo, u32 domain, bool pinned)
  48. {
  49. u32 c = 0;
  50. u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
  51. unsigned i;
  52. qbo->placement.placement = qbo->placements;
  53. qbo->placement.busy_placement = qbo->placements;
  54. if (domain == QXL_GEM_DOMAIN_VRAM)
  55. qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_VRAM | pflag;
  56. if (domain == QXL_GEM_DOMAIN_SURFACE)
  57. qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_PRIV0 | pflag;
  58. if (domain == QXL_GEM_DOMAIN_CPU)
  59. qbo->placements[c++].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM | pflag;
  60. if (!c)
  61. qbo->placements[c++].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  62. qbo->placement.num_placement = c;
  63. qbo->placement.num_busy_placement = c;
  64. for (i = 0; i < c; ++i) {
  65. qbo->placements[i].fpfn = 0;
  66. qbo->placements[i].lpfn = 0;
  67. }
  68. }
  69. int qxl_bo_create(struct qxl_device *qdev,
  70. unsigned long size, bool kernel, bool pinned, u32 domain,
  71. struct qxl_surface *surf,
  72. struct qxl_bo **bo_ptr)
  73. {
  74. struct qxl_bo *bo;
  75. enum ttm_bo_type type;
  76. int r;
  77. if (kernel)
  78. type = ttm_bo_type_kernel;
  79. else
  80. type = ttm_bo_type_device;
  81. *bo_ptr = NULL;
  82. bo = kzalloc(sizeof(struct qxl_bo), GFP_KERNEL);
  83. if (bo == NULL)
  84. return -ENOMEM;
  85. size = roundup(size, PAGE_SIZE);
  86. r = drm_gem_object_init(qdev->ddev, &bo->gem_base, size);
  87. if (unlikely(r)) {
  88. kfree(bo);
  89. return r;
  90. }
  91. bo->type = domain;
  92. bo->pin_count = pinned ? 1 : 0;
  93. bo->surface_id = 0;
  94. INIT_LIST_HEAD(&bo->list);
  95. if (surf)
  96. bo->surf = *surf;
  97. qxl_ttm_placement_from_domain(bo, domain, pinned);
  98. r = ttm_bo_init(&qdev->mman.bdev, &bo->tbo, size, type,
  99. &bo->placement, 0, !kernel, NULL, size,
  100. NULL, NULL, &qxl_ttm_bo_destroy);
  101. if (unlikely(r != 0)) {
  102. if (r != -ERESTARTSYS)
  103. dev_err(qdev->dev,
  104. "object_init failed for (%lu, 0x%08X)\n",
  105. size, domain);
  106. return r;
  107. }
  108. *bo_ptr = bo;
  109. return 0;
  110. }
  111. int qxl_bo_kmap(struct qxl_bo *bo, void **ptr)
  112. {
  113. bool is_iomem;
  114. int r;
  115. if (bo->kptr) {
  116. if (ptr)
  117. *ptr = bo->kptr;
  118. return 0;
  119. }
  120. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  121. if (r)
  122. return r;
  123. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  124. if (ptr)
  125. *ptr = bo->kptr;
  126. return 0;
  127. }
  128. void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev,
  129. struct qxl_bo *bo, int page_offset)
  130. {
  131. struct ttm_mem_type_manager *man = &bo->tbo.bdev->man[bo->tbo.mem.mem_type];
  132. void *rptr;
  133. int ret;
  134. struct io_mapping *map;
  135. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  136. map = qdev->vram_mapping;
  137. else if (bo->tbo.mem.mem_type == TTM_PL_PRIV0)
  138. map = qdev->surface_mapping;
  139. else
  140. goto fallback;
  141. (void) ttm_mem_io_lock(man, false);
  142. ret = ttm_mem_io_reserve(bo->tbo.bdev, &bo->tbo.mem);
  143. ttm_mem_io_unlock(man);
  144. return io_mapping_map_atomic_wc(map, bo->tbo.mem.bus.offset + page_offset);
  145. fallback:
  146. if (bo->kptr) {
  147. rptr = bo->kptr + (page_offset * PAGE_SIZE);
  148. return rptr;
  149. }
  150. ret = qxl_bo_kmap(bo, &rptr);
  151. if (ret)
  152. return NULL;
  153. rptr += page_offset * PAGE_SIZE;
  154. return rptr;
  155. }
  156. void qxl_bo_kunmap(struct qxl_bo *bo)
  157. {
  158. if (bo->kptr == NULL)
  159. return;
  160. bo->kptr = NULL;
  161. ttm_bo_kunmap(&bo->kmap);
  162. }
  163. void qxl_bo_kunmap_atomic_page(struct qxl_device *qdev,
  164. struct qxl_bo *bo, void *pmap)
  165. {
  166. struct ttm_mem_type_manager *man = &bo->tbo.bdev->man[bo->tbo.mem.mem_type];
  167. struct io_mapping *map;
  168. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  169. map = qdev->vram_mapping;
  170. else if (bo->tbo.mem.mem_type == TTM_PL_PRIV0)
  171. map = qdev->surface_mapping;
  172. else
  173. goto fallback;
  174. io_mapping_unmap_atomic(pmap);
  175. (void) ttm_mem_io_lock(man, false);
  176. ttm_mem_io_free(bo->tbo.bdev, &bo->tbo.mem);
  177. ttm_mem_io_unlock(man);
  178. return ;
  179. fallback:
  180. qxl_bo_kunmap(bo);
  181. }
  182. void qxl_bo_unref(struct qxl_bo **bo)
  183. {
  184. if ((*bo) == NULL)
  185. return;
  186. drm_gem_object_unreference_unlocked(&(*bo)->gem_base);
  187. *bo = NULL;
  188. }
  189. struct qxl_bo *qxl_bo_ref(struct qxl_bo *bo)
  190. {
  191. drm_gem_object_reference(&bo->gem_base);
  192. return bo;
  193. }
  194. int qxl_bo_pin(struct qxl_bo *bo, u32 domain, u64 *gpu_addr)
  195. {
  196. struct qxl_device *qdev = (struct qxl_device *)bo->gem_base.dev->dev_private;
  197. int r;
  198. if (bo->pin_count) {
  199. bo->pin_count++;
  200. if (gpu_addr)
  201. *gpu_addr = qxl_bo_gpu_offset(bo);
  202. return 0;
  203. }
  204. qxl_ttm_placement_from_domain(bo, domain, true);
  205. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  206. if (likely(r == 0)) {
  207. bo->pin_count = 1;
  208. if (gpu_addr != NULL)
  209. *gpu_addr = qxl_bo_gpu_offset(bo);
  210. }
  211. if (unlikely(r != 0))
  212. dev_err(qdev->dev, "%p pin failed\n", bo);
  213. return r;
  214. }
  215. int qxl_bo_unpin(struct qxl_bo *bo)
  216. {
  217. struct qxl_device *qdev = (struct qxl_device *)bo->gem_base.dev->dev_private;
  218. int r, i;
  219. if (!bo->pin_count) {
  220. dev_warn(qdev->dev, "%p unpin not necessary\n", bo);
  221. return 0;
  222. }
  223. bo->pin_count--;
  224. if (bo->pin_count)
  225. return 0;
  226. for (i = 0; i < bo->placement.num_placement; i++)
  227. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  228. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  229. if (unlikely(r != 0))
  230. dev_err(qdev->dev, "%p validate failed for unpin\n", bo);
  231. return r;
  232. }
  233. void qxl_bo_force_delete(struct qxl_device *qdev)
  234. {
  235. struct qxl_bo *bo, *n;
  236. if (list_empty(&qdev->gem.objects))
  237. return;
  238. dev_err(qdev->dev, "Userspace still has active objects !\n");
  239. list_for_each_entry_safe(bo, n, &qdev->gem.objects, list) {
  240. dev_err(qdev->dev, "%p %p %lu %lu force free\n",
  241. &bo->gem_base, bo, (unsigned long)bo->gem_base.size,
  242. *((unsigned long *)&bo->gem_base.refcount));
  243. mutex_lock(&qdev->gem.mutex);
  244. list_del_init(&bo->list);
  245. mutex_unlock(&qdev->gem.mutex);
  246. /* this should unref the ttm bo */
  247. drm_gem_object_unreference_unlocked(&bo->gem_base);
  248. }
  249. }
  250. int qxl_bo_init(struct qxl_device *qdev)
  251. {
  252. return qxl_ttm_init(qdev);
  253. }
  254. void qxl_bo_fini(struct qxl_device *qdev)
  255. {
  256. qxl_ttm_fini(qdev);
  257. }
  258. int qxl_bo_check_id(struct qxl_device *qdev, struct qxl_bo *bo)
  259. {
  260. int ret;
  261. if (bo->type == QXL_GEM_DOMAIN_SURFACE && bo->surface_id == 0) {
  262. /* allocate a surface id for this surface now */
  263. ret = qxl_surface_id_alloc(qdev, bo);
  264. if (ret)
  265. return ret;
  266. ret = qxl_hw_surface_alloc(qdev, bo, NULL);
  267. if (ret)
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. int qxl_surf_evict(struct qxl_device *qdev)
  273. {
  274. return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_PRIV0);
  275. }
  276. int qxl_vram_evict(struct qxl_device *qdev)
  277. {
  278. return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_VRAM);
  279. }