qxl_object.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 = to_qxl_bo(tbo);
  33. qdev = to_qxl(bo->tbo.base.dev);
  34. qxl_surface_evict(qdev, bo, false);
  35. WARN_ON_ONCE(bo->map_count > 0);
  36. mutex_lock(&qdev->gem.mutex);
  37. list_del_init(&bo->list);
  38. mutex_unlock(&qdev->gem.mutex);
  39. drm_gem_object_release(&bo->tbo.base);
  40. kfree(bo);
  41. }
  42. bool qxl_ttm_bo_is_qxl_bo(struct ttm_buffer_object *bo)
  43. {
  44. if (bo->destroy == &qxl_ttm_bo_destroy)
  45. return true;
  46. return false;
  47. }
  48. void qxl_ttm_placement_from_domain(struct qxl_bo *qbo, u32 domain, bool pinned)
  49. {
  50. u32 c = 0;
  51. u32 pflag = 0;
  52. unsigned int i;
  53. if (pinned)
  54. pflag |= TTM_PL_FLAG_NO_EVICT;
  55. if (qbo->tbo.base.size <= PAGE_SIZE)
  56. pflag |= TTM_PL_FLAG_TOPDOWN;
  57. qbo->placement.placement = qbo->placements;
  58. qbo->placement.busy_placement = qbo->placements;
  59. if (domain == QXL_GEM_DOMAIN_VRAM) {
  60. qbo->placements[c].mem_type = TTM_PL_VRAM;
  61. qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | pflag;
  62. }
  63. if (domain == QXL_GEM_DOMAIN_SURFACE) {
  64. qbo->placements[c].mem_type = TTM_PL_PRIV;
  65. qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | pflag;
  66. qbo->placements[c].mem_type = TTM_PL_VRAM;
  67. qbo->placements[c++].flags = TTM_PL_FLAG_CACHED | pflag;
  68. }
  69. if (domain == QXL_GEM_DOMAIN_CPU) {
  70. qbo->placements[c].mem_type = TTM_PL_SYSTEM;
  71. qbo->placements[c++].flags = TTM_PL_MASK_CACHING | pflag;
  72. }
  73. if (!c) {
  74. qbo->placements[c].mem_type = TTM_PL_SYSTEM;
  75. qbo->placements[c++].flags = TTM_PL_MASK_CACHING;
  76. }
  77. qbo->placement.num_placement = c;
  78. qbo->placement.num_busy_placement = c;
  79. for (i = 0; i < c; ++i) {
  80. qbo->placements[i].fpfn = 0;
  81. qbo->placements[i].lpfn = 0;
  82. }
  83. }
  84. static const struct drm_gem_object_funcs qxl_object_funcs = {
  85. .free = qxl_gem_object_free,
  86. .open = qxl_gem_object_open,
  87. .close = qxl_gem_object_close,
  88. .pin = qxl_gem_prime_pin,
  89. .unpin = qxl_gem_prime_unpin,
  90. .get_sg_table = qxl_gem_prime_get_sg_table,
  91. .vmap = qxl_gem_prime_vmap,
  92. .vunmap = qxl_gem_prime_vunmap,
  93. .mmap = drm_gem_ttm_mmap,
  94. .print_info = drm_gem_ttm_print_info,
  95. };
  96. int qxl_bo_create(struct qxl_device *qdev, unsigned long size,
  97. bool kernel, bool pinned, u32 domain, u32 priority,
  98. struct qxl_surface *surf,
  99. struct qxl_bo **bo_ptr)
  100. {
  101. struct qxl_bo *bo;
  102. enum ttm_bo_type type;
  103. int r;
  104. if (kernel)
  105. type = ttm_bo_type_kernel;
  106. else
  107. type = ttm_bo_type_device;
  108. *bo_ptr = NULL;
  109. bo = kzalloc(sizeof(struct qxl_bo), GFP_KERNEL);
  110. if (bo == NULL)
  111. return -ENOMEM;
  112. size = roundup(size, PAGE_SIZE);
  113. r = drm_gem_object_init(&qdev->ddev, &bo->tbo.base, size);
  114. if (unlikely(r)) {
  115. kfree(bo);
  116. return r;
  117. }
  118. bo->tbo.base.funcs = &qxl_object_funcs;
  119. bo->type = domain;
  120. bo->pin_count = pinned ? 1 : 0;
  121. bo->surface_id = 0;
  122. INIT_LIST_HEAD(&bo->list);
  123. if (surf)
  124. bo->surf = *surf;
  125. qxl_ttm_placement_from_domain(bo, domain, pinned);
  126. bo->tbo.priority = priority;
  127. r = ttm_bo_init(&qdev->mman.bdev, &bo->tbo, size, type,
  128. &bo->placement, 0, !kernel, size,
  129. NULL, NULL, &qxl_ttm_bo_destroy);
  130. if (unlikely(r != 0)) {
  131. if (r != -ERESTARTSYS)
  132. dev_err(qdev->ddev.dev,
  133. "object_init failed for (%lu, 0x%08X)\n",
  134. size, domain);
  135. return r;
  136. }
  137. *bo_ptr = bo;
  138. return 0;
  139. }
  140. int qxl_bo_kmap(struct qxl_bo *bo, void **ptr)
  141. {
  142. bool is_iomem;
  143. int r;
  144. if (bo->kptr) {
  145. if (ptr)
  146. *ptr = bo->kptr;
  147. bo->map_count++;
  148. return 0;
  149. }
  150. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  151. if (r)
  152. return r;
  153. bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  154. if (ptr)
  155. *ptr = bo->kptr;
  156. bo->map_count = 1;
  157. return 0;
  158. }
  159. void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev,
  160. struct qxl_bo *bo, int page_offset)
  161. {
  162. unsigned long offset;
  163. void *rptr;
  164. int ret;
  165. struct io_mapping *map;
  166. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  167. map = qdev->vram_mapping;
  168. else if (bo->tbo.mem.mem_type == TTM_PL_PRIV)
  169. map = qdev->surface_mapping;
  170. else
  171. goto fallback;
  172. offset = bo->tbo.mem.start << PAGE_SHIFT;
  173. return io_mapping_map_atomic_wc(map, offset + page_offset);
  174. fallback:
  175. if (bo->kptr) {
  176. rptr = bo->kptr + (page_offset * PAGE_SIZE);
  177. return rptr;
  178. }
  179. ret = qxl_bo_kmap(bo, &rptr);
  180. if (ret)
  181. return NULL;
  182. rptr += page_offset * PAGE_SIZE;
  183. return rptr;
  184. }
  185. void qxl_bo_kunmap(struct qxl_bo *bo)
  186. {
  187. if (bo->kptr == NULL)
  188. return;
  189. bo->map_count--;
  190. if (bo->map_count > 0)
  191. return;
  192. bo->kptr = NULL;
  193. ttm_bo_kunmap(&bo->kmap);
  194. }
  195. void qxl_bo_kunmap_atomic_page(struct qxl_device *qdev,
  196. struct qxl_bo *bo, void *pmap)
  197. {
  198. if ((bo->tbo.mem.mem_type != TTM_PL_VRAM) &&
  199. (bo->tbo.mem.mem_type != TTM_PL_PRIV))
  200. goto fallback;
  201. io_mapping_unmap_atomic(pmap);
  202. return;
  203. fallback:
  204. qxl_bo_kunmap(bo);
  205. }
  206. void qxl_bo_unref(struct qxl_bo **bo)
  207. {
  208. if ((*bo) == NULL)
  209. return;
  210. drm_gem_object_put(&(*bo)->tbo.base);
  211. *bo = NULL;
  212. }
  213. struct qxl_bo *qxl_bo_ref(struct qxl_bo *bo)
  214. {
  215. drm_gem_object_get(&bo->tbo.base);
  216. return bo;
  217. }
  218. static int __qxl_bo_pin(struct qxl_bo *bo)
  219. {
  220. struct ttm_operation_ctx ctx = { false, false };
  221. struct drm_device *ddev = bo->tbo.base.dev;
  222. int r;
  223. if (bo->pin_count) {
  224. bo->pin_count++;
  225. return 0;
  226. }
  227. qxl_ttm_placement_from_domain(bo, bo->type, true);
  228. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  229. if (likely(r == 0)) {
  230. bo->pin_count = 1;
  231. }
  232. if (unlikely(r != 0))
  233. dev_err(ddev->dev, "%p pin failed\n", bo);
  234. return r;
  235. }
  236. static int __qxl_bo_unpin(struct qxl_bo *bo)
  237. {
  238. struct ttm_operation_ctx ctx = { false, false };
  239. struct drm_device *ddev = bo->tbo.base.dev;
  240. int r, i;
  241. if (!bo->pin_count) {
  242. dev_warn(ddev->dev, "%p unpin not necessary\n", bo);
  243. return 0;
  244. }
  245. bo->pin_count--;
  246. if (bo->pin_count)
  247. return 0;
  248. for (i = 0; i < bo->placement.num_placement; i++)
  249. bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
  250. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  251. if (unlikely(r != 0))
  252. dev_err(ddev->dev, "%p validate failed for unpin\n", bo);
  253. return r;
  254. }
  255. /*
  256. * Reserve the BO before pinning the object. If the BO was reserved
  257. * beforehand, use the internal version directly __qxl_bo_pin.
  258. *
  259. */
  260. int qxl_bo_pin(struct qxl_bo *bo)
  261. {
  262. int r;
  263. r = qxl_bo_reserve(bo);
  264. if (r)
  265. return r;
  266. r = __qxl_bo_pin(bo);
  267. qxl_bo_unreserve(bo);
  268. return r;
  269. }
  270. /*
  271. * Reserve the BO before pinning the object. If the BO was reserved
  272. * beforehand, use the internal version directly __qxl_bo_unpin.
  273. *
  274. */
  275. int qxl_bo_unpin(struct qxl_bo *bo)
  276. {
  277. int r;
  278. r = qxl_bo_reserve(bo);
  279. if (r)
  280. return r;
  281. r = __qxl_bo_unpin(bo);
  282. qxl_bo_unreserve(bo);
  283. return r;
  284. }
  285. void qxl_bo_force_delete(struct qxl_device *qdev)
  286. {
  287. struct qxl_bo *bo, *n;
  288. if (list_empty(&qdev->gem.objects))
  289. return;
  290. dev_err(qdev->ddev.dev, "Userspace still has active objects !\n");
  291. list_for_each_entry_safe(bo, n, &qdev->gem.objects, list) {
  292. dev_err(qdev->ddev.dev, "%p %p %lu %lu force free\n",
  293. &bo->tbo.base, bo, (unsigned long)bo->tbo.base.size,
  294. *((unsigned long *)&bo->tbo.base.refcount));
  295. mutex_lock(&qdev->gem.mutex);
  296. list_del_init(&bo->list);
  297. mutex_unlock(&qdev->gem.mutex);
  298. /* this should unref the ttm bo */
  299. drm_gem_object_put(&bo->tbo.base);
  300. }
  301. }
  302. int qxl_bo_init(struct qxl_device *qdev)
  303. {
  304. return qxl_ttm_init(qdev);
  305. }
  306. void qxl_bo_fini(struct qxl_device *qdev)
  307. {
  308. qxl_ttm_fini(qdev);
  309. }
  310. int qxl_bo_check_id(struct qxl_device *qdev, struct qxl_bo *bo)
  311. {
  312. int ret;
  313. if (bo->type == QXL_GEM_DOMAIN_SURFACE && bo->surface_id == 0) {
  314. /* allocate a surface id for this surface now */
  315. ret = qxl_surface_id_alloc(qdev, bo);
  316. if (ret)
  317. return ret;
  318. ret = qxl_hw_surface_alloc(qdev, bo);
  319. if (ret)
  320. return ret;
  321. }
  322. return 0;
  323. }
  324. int qxl_surf_evict(struct qxl_device *qdev)
  325. {
  326. return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_PRIV);
  327. }
  328. int qxl_vram_evict(struct qxl_device *qdev)
  329. {
  330. return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_VRAM);
  331. }