qxl_cmd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. /* QXL cmd/ring handling */
  26. #include "qxl_drv.h"
  27. #include "qxl_object.h"
  28. static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap);
  29. struct ring {
  30. struct qxl_ring_header header;
  31. uint8_t elements[0];
  32. };
  33. struct qxl_ring {
  34. struct ring *ring;
  35. int element_size;
  36. int n_elements;
  37. int prod_notify;
  38. wait_queue_head_t *push_event;
  39. spinlock_t lock;
  40. };
  41. void qxl_ring_free(struct qxl_ring *ring)
  42. {
  43. kfree(ring);
  44. }
  45. struct qxl_ring *
  46. qxl_ring_create(struct qxl_ring_header *header,
  47. int element_size,
  48. int n_elements,
  49. int prod_notify,
  50. bool set_prod_notify,
  51. wait_queue_head_t *push_event)
  52. {
  53. struct qxl_ring *ring;
  54. ring = kmalloc(sizeof(*ring), GFP_KERNEL);
  55. if (!ring)
  56. return NULL;
  57. ring->ring = (struct ring *)header;
  58. ring->element_size = element_size;
  59. ring->n_elements = n_elements;
  60. ring->prod_notify = prod_notify;
  61. ring->push_event = push_event;
  62. if (set_prod_notify)
  63. header->notify_on_prod = ring->n_elements;
  64. spin_lock_init(&ring->lock);
  65. return ring;
  66. }
  67. static int qxl_check_header(struct qxl_ring *ring)
  68. {
  69. int ret;
  70. struct qxl_ring_header *header = &(ring->ring->header);
  71. unsigned long flags;
  72. spin_lock_irqsave(&ring->lock, flags);
  73. ret = header->prod - header->cons < header->num_items;
  74. if (ret == 0)
  75. header->notify_on_cons = header->cons + 1;
  76. spin_unlock_irqrestore(&ring->lock, flags);
  77. return ret;
  78. }
  79. static int qxl_check_idle(struct qxl_ring *ring)
  80. {
  81. int ret;
  82. struct qxl_ring_header *header = &(ring->ring->header);
  83. unsigned long flags;
  84. spin_lock_irqsave(&ring->lock, flags);
  85. ret = header->prod == header->cons;
  86. spin_unlock_irqrestore(&ring->lock, flags);
  87. return ret;
  88. }
  89. int qxl_ring_push(struct qxl_ring *ring,
  90. const void *new_elt, bool interruptible)
  91. {
  92. struct qxl_ring_header *header = &(ring->ring->header);
  93. uint8_t *elt;
  94. int idx, ret;
  95. unsigned long flags;
  96. spin_lock_irqsave(&ring->lock, flags);
  97. if (header->prod - header->cons == header->num_items) {
  98. header->notify_on_cons = header->cons + 1;
  99. mb();
  100. spin_unlock_irqrestore(&ring->lock, flags);
  101. if (!drm_can_sleep()) {
  102. while (!qxl_check_header(ring))
  103. udelay(1);
  104. } else {
  105. if (interruptible) {
  106. ret = wait_event_interruptible(*ring->push_event,
  107. qxl_check_header(ring));
  108. if (ret)
  109. return ret;
  110. } else {
  111. wait_event(*ring->push_event,
  112. qxl_check_header(ring));
  113. }
  114. }
  115. spin_lock_irqsave(&ring->lock, flags);
  116. }
  117. idx = header->prod & (ring->n_elements - 1);
  118. elt = ring->ring->elements + idx * ring->element_size;
  119. memcpy((void *)elt, new_elt, ring->element_size);
  120. header->prod++;
  121. mb();
  122. if (header->prod == header->notify_on_prod)
  123. outb(0, ring->prod_notify);
  124. spin_unlock_irqrestore(&ring->lock, flags);
  125. return 0;
  126. }
  127. static bool qxl_ring_pop(struct qxl_ring *ring,
  128. void *element)
  129. {
  130. volatile struct qxl_ring_header *header = &(ring->ring->header);
  131. volatile uint8_t *ring_elt;
  132. int idx;
  133. unsigned long flags;
  134. spin_lock_irqsave(&ring->lock, flags);
  135. if (header->cons == header->prod) {
  136. header->notify_on_prod = header->cons + 1;
  137. spin_unlock_irqrestore(&ring->lock, flags);
  138. return false;
  139. }
  140. idx = header->cons & (ring->n_elements - 1);
  141. ring_elt = ring->ring->elements + idx * ring->element_size;
  142. memcpy(element, (void *)ring_elt, ring->element_size);
  143. header->cons++;
  144. spin_unlock_irqrestore(&ring->lock, flags);
  145. return true;
  146. }
  147. int
  148. qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release,
  149. uint32_t type, bool interruptible)
  150. {
  151. struct qxl_command cmd;
  152. cmd.type = type;
  153. cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset);
  154. return qxl_ring_push(qdev->command_ring, &cmd, interruptible);
  155. }
  156. int
  157. qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release,
  158. uint32_t type, bool interruptible)
  159. {
  160. struct qxl_command cmd;
  161. cmd.type = type;
  162. cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset);
  163. return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible);
  164. }
  165. bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush)
  166. {
  167. if (!qxl_check_idle(qdev->release_ring)) {
  168. queue_work(qdev->gc_queue, &qdev->gc_work);
  169. if (flush)
  170. flush_work(&qdev->gc_work);
  171. return true;
  172. }
  173. return false;
  174. }
  175. int qxl_garbage_collect(struct qxl_device *qdev)
  176. {
  177. struct qxl_release *release;
  178. uint64_t id, next_id;
  179. int i = 0;
  180. int ret;
  181. union qxl_release_info *info;
  182. while (qxl_ring_pop(qdev->release_ring, &id)) {
  183. QXL_INFO(qdev, "popped %lld\n", id);
  184. while (id) {
  185. release = qxl_release_from_id_locked(qdev, id);
  186. if (release == NULL)
  187. break;
  188. ret = qxl_release_reserve(qdev, release, false);
  189. if (ret) {
  190. qxl_io_log(qdev, "failed to reserve release on garbage collect %lld\n", id);
  191. DRM_ERROR("failed to reserve release %lld\n", id);
  192. }
  193. info = qxl_release_map(qdev, release);
  194. next_id = info->next;
  195. qxl_release_unmap(qdev, release, info);
  196. qxl_release_unreserve(qdev, release);
  197. QXL_INFO(qdev, "popped %lld, next %lld\n", id,
  198. next_id);
  199. switch (release->type) {
  200. case QXL_RELEASE_DRAWABLE:
  201. case QXL_RELEASE_SURFACE_CMD:
  202. case QXL_RELEASE_CURSOR_CMD:
  203. break;
  204. default:
  205. DRM_ERROR("unexpected release type\n");
  206. break;
  207. }
  208. id = next_id;
  209. qxl_release_free(qdev, release);
  210. ++i;
  211. }
  212. }
  213. QXL_INFO(qdev, "%s: %lld\n", __func__, i);
  214. return i;
  215. }
  216. int qxl_alloc_bo_reserved(struct qxl_device *qdev, unsigned long size,
  217. struct qxl_bo **_bo)
  218. {
  219. struct qxl_bo *bo;
  220. int ret;
  221. ret = qxl_bo_create(qdev, size, false /* not kernel - device */,
  222. QXL_GEM_DOMAIN_VRAM, NULL, &bo);
  223. if (ret) {
  224. DRM_ERROR("failed to allocate VRAM BO\n");
  225. return ret;
  226. }
  227. ret = qxl_bo_reserve(bo, false);
  228. if (unlikely(ret != 0))
  229. goto out_unref;
  230. *_bo = bo;
  231. return 0;
  232. out_unref:
  233. qxl_bo_unref(&bo);
  234. return 0;
  235. }
  236. static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port)
  237. {
  238. int irq_num;
  239. long addr = qdev->io_base + port;
  240. int ret;
  241. mutex_lock(&qdev->async_io_mutex);
  242. irq_num = atomic_read(&qdev->irq_received_io_cmd);
  243. if (qdev->last_sent_io_cmd > irq_num) {
  244. ret = wait_event_interruptible(qdev->io_cmd_event,
  245. atomic_read(&qdev->irq_received_io_cmd) > irq_num);
  246. if (ret)
  247. goto out;
  248. irq_num = atomic_read(&qdev->irq_received_io_cmd);
  249. }
  250. outb(val, addr);
  251. qdev->last_sent_io_cmd = irq_num + 1;
  252. ret = wait_event_interruptible(qdev->io_cmd_event,
  253. atomic_read(&qdev->irq_received_io_cmd) > irq_num);
  254. out:
  255. mutex_unlock(&qdev->async_io_mutex);
  256. return ret;
  257. }
  258. static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port)
  259. {
  260. int ret;
  261. restart:
  262. ret = wait_for_io_cmd_user(qdev, val, port);
  263. if (ret == -ERESTARTSYS)
  264. goto restart;
  265. }
  266. int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf,
  267. const struct qxl_rect *area)
  268. {
  269. int surface_id;
  270. uint32_t surface_width, surface_height;
  271. int ret;
  272. if (!surf->hw_surf_alloc)
  273. DRM_ERROR("got io update area with no hw surface\n");
  274. if (surf->is_primary)
  275. surface_id = 0;
  276. else
  277. surface_id = surf->surface_id;
  278. surface_width = surf->surf.width;
  279. surface_height = surf->surf.height;
  280. if (area->left < 0 || area->top < 0 ||
  281. area->right > surface_width || area->bottom > surface_height) {
  282. qxl_io_log(qdev, "%s: not doing area update for "
  283. "%d, (%d,%d,%d,%d) (%d,%d)\n", __func__, surface_id, area->left,
  284. area->top, area->right, area->bottom, surface_width, surface_height);
  285. return -EINVAL;
  286. }
  287. mutex_lock(&qdev->update_area_mutex);
  288. qdev->ram_header->update_area = *area;
  289. qdev->ram_header->update_surface = surface_id;
  290. ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC);
  291. mutex_unlock(&qdev->update_area_mutex);
  292. return ret;
  293. }
  294. void qxl_io_notify_oom(struct qxl_device *qdev)
  295. {
  296. outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM);
  297. }
  298. void qxl_io_flush_release(struct qxl_device *qdev)
  299. {
  300. outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE);
  301. }
  302. void qxl_io_flush_surfaces(struct qxl_device *qdev)
  303. {
  304. wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC);
  305. }
  306. void qxl_io_destroy_primary(struct qxl_device *qdev)
  307. {
  308. wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC);
  309. }
  310. void qxl_io_create_primary(struct qxl_device *qdev, unsigned width,
  311. unsigned height, unsigned offset, struct qxl_bo *bo)
  312. {
  313. struct qxl_surface_create *create;
  314. QXL_INFO(qdev, "%s: qdev %p, ram_header %p\n", __func__, qdev,
  315. qdev->ram_header);
  316. create = &qdev->ram_header->create_surface;
  317. create->format = bo->surf.format;
  318. create->width = width;
  319. create->height = height;
  320. create->stride = bo->surf.stride;
  321. create->mem = qxl_bo_physical_address(qdev, bo, offset);
  322. QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem,
  323. bo->kptr);
  324. create->flags = QXL_SURF_FLAG_KEEP_DATA;
  325. create->type = QXL_SURF_TYPE_PRIMARY;
  326. wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
  327. }
  328. void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
  329. {
  330. QXL_INFO(qdev, "qxl_memslot_add %d\n", id);
  331. wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
  332. }
  333. void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...)
  334. {
  335. va_list args;
  336. va_start(args, fmt);
  337. vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args);
  338. va_end(args);
  339. /*
  340. * DO not do a DRM output here - this will call printk, which will
  341. * call back into qxl for rendering (qxl_fb)
  342. */
  343. outb(0, qdev->io_base + QXL_IO_LOG);
  344. }
  345. void qxl_io_reset(struct qxl_device *qdev)
  346. {
  347. outb(0, qdev->io_base + QXL_IO_RESET);
  348. }
  349. void qxl_io_monitors_config(struct qxl_device *qdev)
  350. {
  351. qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__,
  352. qdev->monitors_config ?
  353. qdev->monitors_config->count : -1,
  354. qdev->monitors_config && qdev->monitors_config->count ?
  355. qdev->monitors_config->heads[0].width : -1,
  356. qdev->monitors_config && qdev->monitors_config->count ?
  357. qdev->monitors_config->heads[0].height : -1,
  358. qdev->monitors_config && qdev->monitors_config->count ?
  359. qdev->monitors_config->heads[0].x : -1,
  360. qdev->monitors_config && qdev->monitors_config->count ?
  361. qdev->monitors_config->heads[0].y : -1
  362. );
  363. wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
  364. }
  365. int qxl_surface_id_alloc(struct qxl_device *qdev,
  366. struct qxl_bo *surf)
  367. {
  368. uint32_t handle = -ENOMEM;
  369. int idr_ret;
  370. int count = 0;
  371. again:
  372. if (idr_pre_get(&qdev->surf_id_idr, GFP_ATOMIC) == 0) {
  373. DRM_ERROR("Out of memory for surf idr\n");
  374. kfree(surf);
  375. goto alloc_fail;
  376. }
  377. spin_lock(&qdev->surf_id_idr_lock);
  378. idr_ret = idr_get_new_above(&qdev->surf_id_idr, NULL, 1, &handle);
  379. spin_unlock(&qdev->surf_id_idr_lock);
  380. if (idr_ret == -EAGAIN)
  381. goto again;
  382. if (handle >= qdev->rom->n_surfaces) {
  383. count++;
  384. spin_lock(&qdev->surf_id_idr_lock);
  385. idr_remove(&qdev->surf_id_idr, handle);
  386. spin_unlock(&qdev->surf_id_idr_lock);
  387. qxl_reap_surface_id(qdev, 2);
  388. goto again;
  389. }
  390. surf->surface_id = handle;
  391. spin_lock(&qdev->surf_id_idr_lock);
  392. qdev->last_alloced_surf_id = handle;
  393. spin_unlock(&qdev->surf_id_idr_lock);
  394. alloc_fail:
  395. return 0;
  396. }
  397. void qxl_surface_id_dealloc(struct qxl_device *qdev,
  398. uint32_t surface_id)
  399. {
  400. spin_lock(&qdev->surf_id_idr_lock);
  401. idr_remove(&qdev->surf_id_idr, surface_id);
  402. spin_unlock(&qdev->surf_id_idr_lock);
  403. }
  404. int qxl_hw_surface_alloc(struct qxl_device *qdev,
  405. struct qxl_bo *surf,
  406. struct ttm_mem_reg *new_mem)
  407. {
  408. struct qxl_surface_cmd *cmd;
  409. struct qxl_release *release;
  410. int ret;
  411. if (surf->hw_surf_alloc)
  412. return 0;
  413. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
  414. NULL,
  415. &release);
  416. if (ret)
  417. return ret;
  418. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  419. cmd->type = QXL_SURFACE_CMD_CREATE;
  420. cmd->u.surface_create.format = surf->surf.format;
  421. cmd->u.surface_create.width = surf->surf.width;
  422. cmd->u.surface_create.height = surf->surf.height;
  423. cmd->u.surface_create.stride = surf->surf.stride;
  424. if (new_mem) {
  425. int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
  426. struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
  427. /* TODO - need to hold one of the locks to read tbo.offset */
  428. cmd->u.surface_create.data = slot->high_bits;
  429. cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
  430. } else
  431. cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
  432. cmd->surface_id = surf->surface_id;
  433. qxl_release_unmap(qdev, release, &cmd->release_info);
  434. surf->surf_create = release;
  435. /* no need to add a release to the fence for this bo,
  436. since it is only released when we ask to destroy the surface
  437. and it would never signal otherwise */
  438. qxl_fence_releaseable(qdev, release);
  439. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  440. qxl_release_unreserve(qdev, release);
  441. surf->hw_surf_alloc = true;
  442. spin_lock(&qdev->surf_id_idr_lock);
  443. idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
  444. spin_unlock(&qdev->surf_id_idr_lock);
  445. return 0;
  446. }
  447. int qxl_hw_surface_dealloc(struct qxl_device *qdev,
  448. struct qxl_bo *surf)
  449. {
  450. struct qxl_surface_cmd *cmd;
  451. struct qxl_release *release;
  452. int ret;
  453. int id;
  454. if (!surf->hw_surf_alloc)
  455. return 0;
  456. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
  457. surf->surf_create,
  458. &release);
  459. if (ret)
  460. return ret;
  461. surf->surf_create = NULL;
  462. /* remove the surface from the idr, but not the surface id yet */
  463. spin_lock(&qdev->surf_id_idr_lock);
  464. idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
  465. spin_unlock(&qdev->surf_id_idr_lock);
  466. surf->hw_surf_alloc = false;
  467. id = surf->surface_id;
  468. surf->surface_id = 0;
  469. release->surface_release_id = id;
  470. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  471. cmd->type = QXL_SURFACE_CMD_DESTROY;
  472. cmd->surface_id = id;
  473. qxl_release_unmap(qdev, release, &cmd->release_info);
  474. qxl_fence_releaseable(qdev, release);
  475. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  476. qxl_release_unreserve(qdev, release);
  477. return 0;
  478. }
  479. int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
  480. {
  481. struct qxl_rect rect;
  482. int ret;
  483. /* if we are evicting, we need to make sure the surface is up
  484. to date */
  485. rect.left = 0;
  486. rect.right = surf->surf.width;
  487. rect.top = 0;
  488. rect.bottom = surf->surf.height;
  489. retry:
  490. ret = qxl_io_update_area(qdev, surf, &rect);
  491. if (ret == -ERESTARTSYS)
  492. goto retry;
  493. return ret;
  494. }
  495. static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  496. {
  497. /* no need to update area if we are just freeing the surface normally */
  498. if (do_update_area)
  499. qxl_update_surface(qdev, surf);
  500. /* nuke the surface id at the hw */
  501. qxl_hw_surface_dealloc(qdev, surf);
  502. }
  503. void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  504. {
  505. mutex_lock(&qdev->surf_evict_mutex);
  506. qxl_surface_evict_locked(qdev, surf, do_update_area);
  507. mutex_unlock(&qdev->surf_evict_mutex);
  508. }
  509. static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
  510. {
  511. int ret;
  512. ret = qxl_bo_reserve(surf, false);
  513. if (ret == -EBUSY)
  514. return -EBUSY;
  515. if (surf->fence.num_active_releases > 0 && stall == false) {
  516. qxl_bo_unreserve(surf);
  517. return -EBUSY;
  518. }
  519. if (stall)
  520. mutex_unlock(&qdev->surf_evict_mutex);
  521. spin_lock(&surf->tbo.bdev->fence_lock);
  522. ret = ttm_bo_wait(&surf->tbo, true, true, !stall);
  523. spin_unlock(&surf->tbo.bdev->fence_lock);
  524. if (stall)
  525. mutex_lock(&qdev->surf_evict_mutex);
  526. if (ret == -EBUSY) {
  527. qxl_bo_unreserve(surf);
  528. return -EBUSY;
  529. }
  530. qxl_surface_evict_locked(qdev, surf, true);
  531. qxl_bo_unreserve(surf);
  532. return 0;
  533. }
  534. static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
  535. {
  536. int num_reaped = 0;
  537. int i, ret;
  538. bool stall = false;
  539. int start = 0;
  540. mutex_lock(&qdev->surf_evict_mutex);
  541. again:
  542. spin_lock(&qdev->surf_id_idr_lock);
  543. start = qdev->last_alloced_surf_id + 1;
  544. spin_unlock(&qdev->surf_id_idr_lock);
  545. for (i = start; i < start + qdev->rom->n_surfaces; i++) {
  546. void *objptr;
  547. int surfid = i % qdev->rom->n_surfaces;
  548. /* this avoids the case where the objects is in the
  549. idr but has been evicted half way - its makes
  550. the idr lookup atomic with the eviction */
  551. spin_lock(&qdev->surf_id_idr_lock);
  552. objptr = idr_find(&qdev->surf_id_idr, surfid);
  553. spin_unlock(&qdev->surf_id_idr_lock);
  554. if (!objptr)
  555. continue;
  556. ret = qxl_reap_surf(qdev, objptr, stall);
  557. if (ret == 0)
  558. num_reaped++;
  559. if (num_reaped >= max_to_reap)
  560. break;
  561. }
  562. if (num_reaped == 0 && stall == false) {
  563. stall = true;
  564. goto again;
  565. }
  566. mutex_unlock(&qdev->surf_evict_mutex);
  567. if (num_reaped) {
  568. usleep_range(500, 1000);
  569. qxl_queue_garbage_collect(qdev, true);
  570. }
  571. return 0;
  572. }