qxl_cmd.c 18 KB

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