qxl_cmd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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;
  369. int idr_ret;
  370. int count = 0;
  371. again:
  372. idr_preload(GFP_ATOMIC);
  373. spin_lock(&qdev->surf_id_idr_lock);
  374. idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT);
  375. spin_unlock(&qdev->surf_id_idr_lock);
  376. idr_preload_end();
  377. if (idr_ret < 0)
  378. return idr_ret;
  379. handle = idr_ret;
  380. if (handle >= qdev->rom->n_surfaces) {
  381. count++;
  382. spin_lock(&qdev->surf_id_idr_lock);
  383. idr_remove(&qdev->surf_id_idr, handle);
  384. spin_unlock(&qdev->surf_id_idr_lock);
  385. qxl_reap_surface_id(qdev, 2);
  386. goto again;
  387. }
  388. surf->surface_id = handle;
  389. spin_lock(&qdev->surf_id_idr_lock);
  390. qdev->last_alloced_surf_id = handle;
  391. spin_unlock(&qdev->surf_id_idr_lock);
  392. return 0;
  393. }
  394. void qxl_surface_id_dealloc(struct qxl_device *qdev,
  395. uint32_t surface_id)
  396. {
  397. spin_lock(&qdev->surf_id_idr_lock);
  398. idr_remove(&qdev->surf_id_idr, surface_id);
  399. spin_unlock(&qdev->surf_id_idr_lock);
  400. }
  401. int qxl_hw_surface_alloc(struct qxl_device *qdev,
  402. struct qxl_bo *surf,
  403. struct ttm_mem_reg *new_mem)
  404. {
  405. struct qxl_surface_cmd *cmd;
  406. struct qxl_release *release;
  407. int ret;
  408. if (surf->hw_surf_alloc)
  409. return 0;
  410. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
  411. NULL,
  412. &release);
  413. if (ret)
  414. return ret;
  415. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  416. cmd->type = QXL_SURFACE_CMD_CREATE;
  417. cmd->u.surface_create.format = surf->surf.format;
  418. cmd->u.surface_create.width = surf->surf.width;
  419. cmd->u.surface_create.height = surf->surf.height;
  420. cmd->u.surface_create.stride = surf->surf.stride;
  421. if (new_mem) {
  422. int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
  423. struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
  424. /* TODO - need to hold one of the locks to read tbo.offset */
  425. cmd->u.surface_create.data = slot->high_bits;
  426. cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
  427. } else
  428. cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
  429. cmd->surface_id = surf->surface_id;
  430. qxl_release_unmap(qdev, release, &cmd->release_info);
  431. surf->surf_create = release;
  432. /* no need to add a release to the fence for this bo,
  433. since it is only released when we ask to destroy the surface
  434. and it would never signal otherwise */
  435. qxl_fence_releaseable(qdev, release);
  436. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  437. qxl_release_unreserve(qdev, release);
  438. surf->hw_surf_alloc = true;
  439. spin_lock(&qdev->surf_id_idr_lock);
  440. idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
  441. spin_unlock(&qdev->surf_id_idr_lock);
  442. return 0;
  443. }
  444. int qxl_hw_surface_dealloc(struct qxl_device *qdev,
  445. struct qxl_bo *surf)
  446. {
  447. struct qxl_surface_cmd *cmd;
  448. struct qxl_release *release;
  449. int ret;
  450. int id;
  451. if (!surf->hw_surf_alloc)
  452. return 0;
  453. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
  454. surf->surf_create,
  455. &release);
  456. if (ret)
  457. return ret;
  458. surf->surf_create = NULL;
  459. /* remove the surface from the idr, but not the surface id yet */
  460. spin_lock(&qdev->surf_id_idr_lock);
  461. idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
  462. spin_unlock(&qdev->surf_id_idr_lock);
  463. surf->hw_surf_alloc = false;
  464. id = surf->surface_id;
  465. surf->surface_id = 0;
  466. release->surface_release_id = id;
  467. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  468. cmd->type = QXL_SURFACE_CMD_DESTROY;
  469. cmd->surface_id = id;
  470. qxl_release_unmap(qdev, release, &cmd->release_info);
  471. qxl_fence_releaseable(qdev, release);
  472. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  473. qxl_release_unreserve(qdev, release);
  474. return 0;
  475. }
  476. int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
  477. {
  478. struct qxl_rect rect;
  479. int ret;
  480. /* if we are evicting, we need to make sure the surface is up
  481. to date */
  482. rect.left = 0;
  483. rect.right = surf->surf.width;
  484. rect.top = 0;
  485. rect.bottom = surf->surf.height;
  486. retry:
  487. ret = qxl_io_update_area(qdev, surf, &rect);
  488. if (ret == -ERESTARTSYS)
  489. goto retry;
  490. return ret;
  491. }
  492. static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  493. {
  494. /* no need to update area if we are just freeing the surface normally */
  495. if (do_update_area)
  496. qxl_update_surface(qdev, surf);
  497. /* nuke the surface id at the hw */
  498. qxl_hw_surface_dealloc(qdev, surf);
  499. }
  500. void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  501. {
  502. mutex_lock(&qdev->surf_evict_mutex);
  503. qxl_surface_evict_locked(qdev, surf, do_update_area);
  504. mutex_unlock(&qdev->surf_evict_mutex);
  505. }
  506. static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
  507. {
  508. int ret;
  509. ret = qxl_bo_reserve(surf, false);
  510. if (ret == -EBUSY)
  511. return -EBUSY;
  512. if (surf->fence.num_active_releases > 0 && stall == false) {
  513. qxl_bo_unreserve(surf);
  514. return -EBUSY;
  515. }
  516. if (stall)
  517. mutex_unlock(&qdev->surf_evict_mutex);
  518. spin_lock(&surf->tbo.bdev->fence_lock);
  519. ret = ttm_bo_wait(&surf->tbo, true, true, !stall);
  520. spin_unlock(&surf->tbo.bdev->fence_lock);
  521. if (stall)
  522. mutex_lock(&qdev->surf_evict_mutex);
  523. if (ret == -EBUSY) {
  524. qxl_bo_unreserve(surf);
  525. return -EBUSY;
  526. }
  527. qxl_surface_evict_locked(qdev, surf, true);
  528. qxl_bo_unreserve(surf);
  529. return 0;
  530. }
  531. static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
  532. {
  533. int num_reaped = 0;
  534. int i, ret;
  535. bool stall = false;
  536. int start = 0;
  537. mutex_lock(&qdev->surf_evict_mutex);
  538. again:
  539. spin_lock(&qdev->surf_id_idr_lock);
  540. start = qdev->last_alloced_surf_id + 1;
  541. spin_unlock(&qdev->surf_id_idr_lock);
  542. for (i = start; i < start + qdev->rom->n_surfaces; i++) {
  543. void *objptr;
  544. int surfid = i % qdev->rom->n_surfaces;
  545. /* this avoids the case where the objects is in the
  546. idr but has been evicted half way - its makes
  547. the idr lookup atomic with the eviction */
  548. spin_lock(&qdev->surf_id_idr_lock);
  549. objptr = idr_find(&qdev->surf_id_idr, surfid);
  550. spin_unlock(&qdev->surf_id_idr_lock);
  551. if (!objptr)
  552. continue;
  553. ret = qxl_reap_surf(qdev, objptr, stall);
  554. if (ret == 0)
  555. num_reaped++;
  556. if (num_reaped >= max_to_reap)
  557. break;
  558. }
  559. if (num_reaped == 0 && stall == false) {
  560. stall = true;
  561. goto again;
  562. }
  563. mutex_unlock(&qdev->surf_evict_mutex);
  564. if (num_reaped) {
  565. usleep_range(500, 1000);
  566. qxl_queue_garbage_collect(qdev, true);
  567. }
  568. return 0;
  569. }