qxl_release.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright 2011 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. * on the rights to use, copy, modify, merge, publish, distribute, sub
  8. * license, and/or sell copies of the Software, and to permit persons to whom
  9. * the Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "qxl_drv.h"
  23. #include "qxl_object.h"
  24. #include <trace/events/fence.h>
  25. /*
  26. * drawable cmd cache - allocate a bunch of VRAM pages, suballocate
  27. * into 256 byte chunks for now - gives 16 cmds per page.
  28. *
  29. * use an ida to index into the chunks?
  30. */
  31. /* manage releaseables */
  32. /* stack them 16 high for now -drawable object is 191 */
  33. #define RELEASE_SIZE 256
  34. #define RELEASES_PER_BO (4096 / RELEASE_SIZE)
  35. /* put an alloc/dealloc surface cmd into one bo and round up to 128 */
  36. #define SURFACE_RELEASE_SIZE 128
  37. #define SURFACE_RELEASES_PER_BO (4096 / SURFACE_RELEASE_SIZE)
  38. static const int release_size_per_bo[] = { RELEASE_SIZE, SURFACE_RELEASE_SIZE, RELEASE_SIZE };
  39. static const int releases_per_bo[] = { RELEASES_PER_BO, SURFACE_RELEASES_PER_BO, RELEASES_PER_BO };
  40. static const char *qxl_get_driver_name(struct fence *fence)
  41. {
  42. return "qxl";
  43. }
  44. static const char *qxl_get_timeline_name(struct fence *fence)
  45. {
  46. return "release";
  47. }
  48. static bool qxl_nop_signaling(struct fence *fence)
  49. {
  50. /* fences are always automatically signaled, so just pretend we did this.. */
  51. return true;
  52. }
  53. static long qxl_fence_wait(struct fence *fence, bool intr, signed long timeout)
  54. {
  55. struct qxl_device *qdev;
  56. struct qxl_release *release;
  57. int count = 0, sc = 0;
  58. bool have_drawable_releases;
  59. unsigned long cur, end = jiffies + timeout;
  60. qdev = container_of(fence->lock, struct qxl_device, release_lock);
  61. release = container_of(fence, struct qxl_release, base);
  62. have_drawable_releases = release->type == QXL_RELEASE_DRAWABLE;
  63. retry:
  64. sc++;
  65. if (fence_is_signaled(fence))
  66. goto signaled;
  67. qxl_io_notify_oom(qdev);
  68. for (count = 0; count < 11; count++) {
  69. if (!qxl_queue_garbage_collect(qdev, true))
  70. break;
  71. if (fence_is_signaled(fence))
  72. goto signaled;
  73. }
  74. if (fence_is_signaled(fence))
  75. goto signaled;
  76. if (have_drawable_releases || sc < 4) {
  77. if (sc > 2)
  78. /* back off */
  79. usleep_range(500, 1000);
  80. if (time_after(jiffies, end))
  81. return 0;
  82. if (have_drawable_releases && sc > 300) {
  83. FENCE_WARN(fence, "failed to wait on release %d "
  84. "after spincount %d\n",
  85. fence->context & ~0xf0000000, sc);
  86. goto signaled;
  87. }
  88. goto retry;
  89. }
  90. /*
  91. * yeah, original sync_obj_wait gave up after 3 spins when
  92. * have_drawable_releases is not set.
  93. */
  94. signaled:
  95. cur = jiffies;
  96. if (time_after(cur, end))
  97. return 0;
  98. return end - cur;
  99. }
  100. static const struct fence_ops qxl_fence_ops = {
  101. .get_driver_name = qxl_get_driver_name,
  102. .get_timeline_name = qxl_get_timeline_name,
  103. .enable_signaling = qxl_nop_signaling,
  104. .wait = qxl_fence_wait,
  105. };
  106. static int
  107. qxl_release_alloc(struct qxl_device *qdev, int type,
  108. struct qxl_release **ret)
  109. {
  110. struct qxl_release *release;
  111. int handle;
  112. size_t size = sizeof(*release);
  113. release = kmalloc(size, GFP_KERNEL);
  114. if (!release) {
  115. DRM_ERROR("Out of memory\n");
  116. return 0;
  117. }
  118. release->base.ops = NULL;
  119. release->type = type;
  120. release->release_offset = 0;
  121. release->surface_release_id = 0;
  122. INIT_LIST_HEAD(&release->bos);
  123. idr_preload(GFP_KERNEL);
  124. spin_lock(&qdev->release_idr_lock);
  125. handle = idr_alloc(&qdev->release_idr, release, 1, 0, GFP_NOWAIT);
  126. release->base.seqno = ++qdev->release_seqno;
  127. spin_unlock(&qdev->release_idr_lock);
  128. idr_preload_end();
  129. if (handle < 0) {
  130. kfree(release);
  131. *ret = NULL;
  132. return handle;
  133. }
  134. *ret = release;
  135. QXL_INFO(qdev, "allocated release %d\n", handle);
  136. release->id = handle;
  137. return handle;
  138. }
  139. static void
  140. qxl_release_free_list(struct qxl_release *release)
  141. {
  142. while (!list_empty(&release->bos)) {
  143. struct qxl_bo_list *entry;
  144. struct qxl_bo *bo;
  145. entry = container_of(release->bos.next,
  146. struct qxl_bo_list, tv.head);
  147. bo = to_qxl_bo(entry->tv.bo);
  148. qxl_bo_unref(&bo);
  149. list_del(&entry->tv.head);
  150. kfree(entry);
  151. }
  152. }
  153. void
  154. qxl_release_free(struct qxl_device *qdev,
  155. struct qxl_release *release)
  156. {
  157. QXL_INFO(qdev, "release %d, type %d\n", release->id,
  158. release->type);
  159. if (release->surface_release_id)
  160. qxl_surface_id_dealloc(qdev, release->surface_release_id);
  161. spin_lock(&qdev->release_idr_lock);
  162. idr_remove(&qdev->release_idr, release->id);
  163. spin_unlock(&qdev->release_idr_lock);
  164. if (release->base.ops) {
  165. WARN_ON(list_empty(&release->bos));
  166. qxl_release_free_list(release);
  167. fence_signal(&release->base);
  168. fence_put(&release->base);
  169. } else {
  170. qxl_release_free_list(release);
  171. kfree(release);
  172. }
  173. }
  174. static int qxl_release_bo_alloc(struct qxl_device *qdev,
  175. struct qxl_bo **bo)
  176. {
  177. int ret;
  178. /* pin releases bo's they are too messy to evict */
  179. ret = qxl_bo_create(qdev, PAGE_SIZE, false, true,
  180. QXL_GEM_DOMAIN_VRAM, NULL,
  181. bo);
  182. return ret;
  183. }
  184. int qxl_release_list_add(struct qxl_release *release, struct qxl_bo *bo)
  185. {
  186. struct qxl_bo_list *entry;
  187. list_for_each_entry(entry, &release->bos, tv.head) {
  188. if (entry->tv.bo == &bo->tbo)
  189. return 0;
  190. }
  191. entry = kmalloc(sizeof(struct qxl_bo_list), GFP_KERNEL);
  192. if (!entry)
  193. return -ENOMEM;
  194. qxl_bo_ref(bo);
  195. entry->tv.bo = &bo->tbo;
  196. entry->tv.shared = false;
  197. list_add_tail(&entry->tv.head, &release->bos);
  198. return 0;
  199. }
  200. static int qxl_release_validate_bo(struct qxl_bo *bo)
  201. {
  202. int ret;
  203. if (!bo->pin_count) {
  204. qxl_ttm_placement_from_domain(bo, bo->type, false);
  205. ret = ttm_bo_validate(&bo->tbo, &bo->placement,
  206. true, false);
  207. if (ret)
  208. return ret;
  209. }
  210. ret = reservation_object_reserve_shared(bo->tbo.resv);
  211. if (ret)
  212. return ret;
  213. /* allocate a surface for reserved + validated buffers */
  214. ret = qxl_bo_check_id(bo->gem_base.dev->dev_private, bo);
  215. if (ret)
  216. return ret;
  217. return 0;
  218. }
  219. int qxl_release_reserve_list(struct qxl_release *release, bool no_intr)
  220. {
  221. int ret;
  222. struct qxl_bo_list *entry;
  223. /* if only one object on the release its the release itself
  224. since these objects are pinned no need to reserve */
  225. if (list_is_singular(&release->bos))
  226. return 0;
  227. ret = ttm_eu_reserve_buffers(&release->ticket, &release->bos,
  228. !no_intr, NULL);
  229. if (ret)
  230. return ret;
  231. list_for_each_entry(entry, &release->bos, tv.head) {
  232. struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
  233. ret = qxl_release_validate_bo(bo);
  234. if (ret) {
  235. ttm_eu_backoff_reservation(&release->ticket, &release->bos);
  236. return ret;
  237. }
  238. }
  239. return 0;
  240. }
  241. void qxl_release_backoff_reserve_list(struct qxl_release *release)
  242. {
  243. /* if only one object on the release its the release itself
  244. since these objects are pinned no need to reserve */
  245. if (list_is_singular(&release->bos))
  246. return;
  247. ttm_eu_backoff_reservation(&release->ticket, &release->bos);
  248. }
  249. int qxl_alloc_surface_release_reserved(struct qxl_device *qdev,
  250. enum qxl_surface_cmd_type surface_cmd_type,
  251. struct qxl_release *create_rel,
  252. struct qxl_release **release)
  253. {
  254. if (surface_cmd_type == QXL_SURFACE_CMD_DESTROY && create_rel) {
  255. int idr_ret;
  256. struct qxl_bo_list *entry = list_first_entry(&create_rel->bos, struct qxl_bo_list, tv.head);
  257. struct qxl_bo *bo;
  258. union qxl_release_info *info;
  259. /* stash the release after the create command */
  260. idr_ret = qxl_release_alloc(qdev, QXL_RELEASE_SURFACE_CMD, release);
  261. if (idr_ret < 0)
  262. return idr_ret;
  263. bo = to_qxl_bo(entry->tv.bo);
  264. (*release)->release_offset = create_rel->release_offset + 64;
  265. qxl_release_list_add(*release, bo);
  266. info = qxl_release_map(qdev, *release);
  267. info->id = idr_ret;
  268. qxl_release_unmap(qdev, *release, info);
  269. return 0;
  270. }
  271. return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_surface_cmd),
  272. QXL_RELEASE_SURFACE_CMD, release, NULL);
  273. }
  274. int qxl_alloc_release_reserved(struct qxl_device *qdev, unsigned long size,
  275. int type, struct qxl_release **release,
  276. struct qxl_bo **rbo)
  277. {
  278. struct qxl_bo *bo;
  279. int idr_ret;
  280. int ret = 0;
  281. union qxl_release_info *info;
  282. int cur_idx;
  283. if (type == QXL_RELEASE_DRAWABLE)
  284. cur_idx = 0;
  285. else if (type == QXL_RELEASE_SURFACE_CMD)
  286. cur_idx = 1;
  287. else if (type == QXL_RELEASE_CURSOR_CMD)
  288. cur_idx = 2;
  289. else {
  290. DRM_ERROR("got illegal type: %d\n", type);
  291. return -EINVAL;
  292. }
  293. idr_ret = qxl_release_alloc(qdev, type, release);
  294. if (idr_ret < 0) {
  295. if (rbo)
  296. *rbo = NULL;
  297. return idr_ret;
  298. }
  299. mutex_lock(&qdev->release_mutex);
  300. if (qdev->current_release_bo_offset[cur_idx] + 1 >= releases_per_bo[cur_idx]) {
  301. qxl_bo_unref(&qdev->current_release_bo[cur_idx]);
  302. qdev->current_release_bo_offset[cur_idx] = 0;
  303. qdev->current_release_bo[cur_idx] = NULL;
  304. }
  305. if (!qdev->current_release_bo[cur_idx]) {
  306. ret = qxl_release_bo_alloc(qdev, &qdev->current_release_bo[cur_idx]);
  307. if (ret) {
  308. mutex_unlock(&qdev->release_mutex);
  309. qxl_release_free(qdev, *release);
  310. return ret;
  311. }
  312. }
  313. bo = qxl_bo_ref(qdev->current_release_bo[cur_idx]);
  314. (*release)->release_offset = qdev->current_release_bo_offset[cur_idx] * release_size_per_bo[cur_idx];
  315. qdev->current_release_bo_offset[cur_idx]++;
  316. if (rbo)
  317. *rbo = bo;
  318. mutex_unlock(&qdev->release_mutex);
  319. ret = qxl_release_list_add(*release, bo);
  320. qxl_bo_unref(&bo);
  321. if (ret) {
  322. qxl_release_free(qdev, *release);
  323. return ret;
  324. }
  325. info = qxl_release_map(qdev, *release);
  326. info->id = idr_ret;
  327. qxl_release_unmap(qdev, *release, info);
  328. return ret;
  329. }
  330. struct qxl_release *qxl_release_from_id_locked(struct qxl_device *qdev,
  331. uint64_t id)
  332. {
  333. struct qxl_release *release;
  334. spin_lock(&qdev->release_idr_lock);
  335. release = idr_find(&qdev->release_idr, id);
  336. spin_unlock(&qdev->release_idr_lock);
  337. if (!release) {
  338. DRM_ERROR("failed to find id in release_idr\n");
  339. return NULL;
  340. }
  341. return release;
  342. }
  343. union qxl_release_info *qxl_release_map(struct qxl_device *qdev,
  344. struct qxl_release *release)
  345. {
  346. void *ptr;
  347. union qxl_release_info *info;
  348. struct qxl_bo_list *entry = list_first_entry(&release->bos, struct qxl_bo_list, tv.head);
  349. struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
  350. ptr = qxl_bo_kmap_atomic_page(qdev, bo, release->release_offset & PAGE_SIZE);
  351. if (!ptr)
  352. return NULL;
  353. info = ptr + (release->release_offset & ~PAGE_SIZE);
  354. return info;
  355. }
  356. void qxl_release_unmap(struct qxl_device *qdev,
  357. struct qxl_release *release,
  358. union qxl_release_info *info)
  359. {
  360. struct qxl_bo_list *entry = list_first_entry(&release->bos, struct qxl_bo_list, tv.head);
  361. struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
  362. void *ptr;
  363. ptr = ((void *)info) - (release->release_offset & ~PAGE_SIZE);
  364. qxl_bo_kunmap_atomic_page(qdev, bo, ptr);
  365. }
  366. void qxl_release_fence_buffer_objects(struct qxl_release *release)
  367. {
  368. struct ttm_buffer_object *bo;
  369. struct ttm_bo_global *glob;
  370. struct ttm_bo_device *bdev;
  371. struct ttm_bo_driver *driver;
  372. struct qxl_bo *qbo;
  373. struct ttm_validate_buffer *entry;
  374. struct qxl_device *qdev;
  375. /* if only one object on the release its the release itself
  376. since these objects are pinned no need to reserve */
  377. if (list_is_singular(&release->bos) || list_empty(&release->bos))
  378. return;
  379. bo = list_first_entry(&release->bos, struct ttm_validate_buffer, head)->bo;
  380. bdev = bo->bdev;
  381. qdev = container_of(bdev, struct qxl_device, mman.bdev);
  382. /*
  383. * Since we never really allocated a context and we don't want to conflict,
  384. * set the highest bits. This will break if we really allow exporting of dma-bufs.
  385. */
  386. fence_init(&release->base, &qxl_fence_ops, &qdev->release_lock,
  387. release->id | 0xf0000000, release->base.seqno);
  388. trace_fence_emit(&release->base);
  389. driver = bdev->driver;
  390. glob = bo->glob;
  391. spin_lock(&glob->lru_lock);
  392. list_for_each_entry(entry, &release->bos, head) {
  393. bo = entry->bo;
  394. qbo = to_qxl_bo(bo);
  395. reservation_object_add_shared_fence(bo->resv, &release->base);
  396. ttm_bo_add_to_lru(bo);
  397. __ttm_bo_unreserve(bo);
  398. }
  399. spin_unlock(&glob->lru_lock);
  400. ww_acquire_fini(&release->ticket);
  401. }