qxl_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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, bool intr)
  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. if (intr)
  245. ret = wait_event_interruptible_timeout(qdev->io_cmd_event,
  246. atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
  247. else
  248. ret = wait_event_timeout(qdev->io_cmd_event,
  249. atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
  250. /* 0 is timeout, just bail the "hw" has gone away */
  251. if (ret <= 0)
  252. goto out;
  253. irq_num = atomic_read(&qdev->irq_received_io_cmd);
  254. }
  255. outb(val, addr);
  256. qdev->last_sent_io_cmd = irq_num + 1;
  257. if (intr)
  258. ret = wait_event_interruptible_timeout(qdev->io_cmd_event,
  259. atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
  260. else
  261. ret = wait_event_timeout(qdev->io_cmd_event,
  262. atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
  263. out:
  264. if (ret > 0)
  265. ret = 0;
  266. mutex_unlock(&qdev->async_io_mutex);
  267. return ret;
  268. }
  269. static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port)
  270. {
  271. int ret;
  272. restart:
  273. ret = wait_for_io_cmd_user(qdev, val, port, false);
  274. if (ret == -ERESTARTSYS)
  275. goto restart;
  276. }
  277. int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf,
  278. const struct qxl_rect *area)
  279. {
  280. int surface_id;
  281. uint32_t surface_width, surface_height;
  282. int ret;
  283. if (!surf->hw_surf_alloc)
  284. DRM_ERROR("got io update area with no hw surface\n");
  285. if (surf->is_primary)
  286. surface_id = 0;
  287. else
  288. surface_id = surf->surface_id;
  289. surface_width = surf->surf.width;
  290. surface_height = surf->surf.height;
  291. if (area->left < 0 || area->top < 0 ||
  292. area->right > surface_width || area->bottom > surface_height) {
  293. qxl_io_log(qdev, "%s: not doing area update for "
  294. "%d, (%d,%d,%d,%d) (%d,%d)\n", __func__, surface_id, area->left,
  295. area->top, area->right, area->bottom, surface_width, surface_height);
  296. return -EINVAL;
  297. }
  298. mutex_lock(&qdev->update_area_mutex);
  299. qdev->ram_header->update_area = *area;
  300. qdev->ram_header->update_surface = surface_id;
  301. ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC, true);
  302. mutex_unlock(&qdev->update_area_mutex);
  303. return ret;
  304. }
  305. void qxl_io_notify_oom(struct qxl_device *qdev)
  306. {
  307. outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM);
  308. }
  309. void qxl_io_flush_release(struct qxl_device *qdev)
  310. {
  311. outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE);
  312. }
  313. void qxl_io_flush_surfaces(struct qxl_device *qdev)
  314. {
  315. wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC);
  316. }
  317. void qxl_io_destroy_primary(struct qxl_device *qdev)
  318. {
  319. wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC);
  320. }
  321. void qxl_io_create_primary(struct qxl_device *qdev, unsigned width,
  322. unsigned height, unsigned offset, struct qxl_bo *bo)
  323. {
  324. struct qxl_surface_create *create;
  325. QXL_INFO(qdev, "%s: qdev %p, ram_header %p\n", __func__, qdev,
  326. qdev->ram_header);
  327. create = &qdev->ram_header->create_surface;
  328. create->format = bo->surf.format;
  329. create->width = width;
  330. create->height = height;
  331. create->stride = bo->surf.stride;
  332. create->mem = qxl_bo_physical_address(qdev, bo, offset);
  333. QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem,
  334. bo->kptr);
  335. create->flags = QXL_SURF_FLAG_KEEP_DATA;
  336. create->type = QXL_SURF_TYPE_PRIMARY;
  337. wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
  338. }
  339. void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
  340. {
  341. QXL_INFO(qdev, "qxl_memslot_add %d\n", id);
  342. wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
  343. }
  344. void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...)
  345. {
  346. va_list args;
  347. va_start(args, fmt);
  348. vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args);
  349. va_end(args);
  350. /*
  351. * DO not do a DRM output here - this will call printk, which will
  352. * call back into qxl for rendering (qxl_fb)
  353. */
  354. outb(0, qdev->io_base + QXL_IO_LOG);
  355. }
  356. void qxl_io_reset(struct qxl_device *qdev)
  357. {
  358. outb(0, qdev->io_base + QXL_IO_RESET);
  359. }
  360. void qxl_io_monitors_config(struct qxl_device *qdev)
  361. {
  362. qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__,
  363. qdev->monitors_config ?
  364. qdev->monitors_config->count : -1,
  365. qdev->monitors_config && qdev->monitors_config->count ?
  366. qdev->monitors_config->heads[0].width : -1,
  367. qdev->monitors_config && qdev->monitors_config->count ?
  368. qdev->monitors_config->heads[0].height : -1,
  369. qdev->monitors_config && qdev->monitors_config->count ?
  370. qdev->monitors_config->heads[0].x : -1,
  371. qdev->monitors_config && qdev->monitors_config->count ?
  372. qdev->monitors_config->heads[0].y : -1
  373. );
  374. wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
  375. }
  376. int qxl_surface_id_alloc(struct qxl_device *qdev,
  377. struct qxl_bo *surf)
  378. {
  379. uint32_t handle;
  380. int idr_ret;
  381. int count = 0;
  382. again:
  383. idr_preload(GFP_ATOMIC);
  384. spin_lock(&qdev->surf_id_idr_lock);
  385. idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT);
  386. spin_unlock(&qdev->surf_id_idr_lock);
  387. idr_preload_end();
  388. if (idr_ret < 0)
  389. return idr_ret;
  390. handle = idr_ret;
  391. if (handle >= qdev->rom->n_surfaces) {
  392. count++;
  393. spin_lock(&qdev->surf_id_idr_lock);
  394. idr_remove(&qdev->surf_id_idr, handle);
  395. spin_unlock(&qdev->surf_id_idr_lock);
  396. qxl_reap_surface_id(qdev, 2);
  397. goto again;
  398. }
  399. surf->surface_id = handle;
  400. spin_lock(&qdev->surf_id_idr_lock);
  401. qdev->last_alloced_surf_id = handle;
  402. spin_unlock(&qdev->surf_id_idr_lock);
  403. return 0;
  404. }
  405. void qxl_surface_id_dealloc(struct qxl_device *qdev,
  406. uint32_t surface_id)
  407. {
  408. spin_lock(&qdev->surf_id_idr_lock);
  409. idr_remove(&qdev->surf_id_idr, surface_id);
  410. spin_unlock(&qdev->surf_id_idr_lock);
  411. }
  412. int qxl_hw_surface_alloc(struct qxl_device *qdev,
  413. struct qxl_bo *surf,
  414. struct ttm_mem_reg *new_mem)
  415. {
  416. struct qxl_surface_cmd *cmd;
  417. struct qxl_release *release;
  418. int ret;
  419. if (surf->hw_surf_alloc)
  420. return 0;
  421. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
  422. NULL,
  423. &release);
  424. if (ret)
  425. return ret;
  426. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  427. cmd->type = QXL_SURFACE_CMD_CREATE;
  428. cmd->u.surface_create.format = surf->surf.format;
  429. cmd->u.surface_create.width = surf->surf.width;
  430. cmd->u.surface_create.height = surf->surf.height;
  431. cmd->u.surface_create.stride = surf->surf.stride;
  432. if (new_mem) {
  433. int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
  434. struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
  435. /* TODO - need to hold one of the locks to read tbo.offset */
  436. cmd->u.surface_create.data = slot->high_bits;
  437. cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
  438. } else
  439. cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
  440. cmd->surface_id = surf->surface_id;
  441. qxl_release_unmap(qdev, release, &cmd->release_info);
  442. surf->surf_create = release;
  443. /* no need to add a release to the fence for this bo,
  444. since it is only released when we ask to destroy the surface
  445. and it would never signal otherwise */
  446. qxl_fence_releaseable(qdev, release);
  447. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  448. qxl_release_unreserve(qdev, release);
  449. surf->hw_surf_alloc = true;
  450. spin_lock(&qdev->surf_id_idr_lock);
  451. idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
  452. spin_unlock(&qdev->surf_id_idr_lock);
  453. return 0;
  454. }
  455. int qxl_hw_surface_dealloc(struct qxl_device *qdev,
  456. struct qxl_bo *surf)
  457. {
  458. struct qxl_surface_cmd *cmd;
  459. struct qxl_release *release;
  460. int ret;
  461. int id;
  462. if (!surf->hw_surf_alloc)
  463. return 0;
  464. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
  465. surf->surf_create,
  466. &release);
  467. if (ret)
  468. return ret;
  469. surf->surf_create = NULL;
  470. /* remove the surface from the idr, but not the surface id yet */
  471. spin_lock(&qdev->surf_id_idr_lock);
  472. idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
  473. spin_unlock(&qdev->surf_id_idr_lock);
  474. surf->hw_surf_alloc = false;
  475. id = surf->surface_id;
  476. surf->surface_id = 0;
  477. release->surface_release_id = id;
  478. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  479. cmd->type = QXL_SURFACE_CMD_DESTROY;
  480. cmd->surface_id = id;
  481. qxl_release_unmap(qdev, release, &cmd->release_info);
  482. qxl_fence_releaseable(qdev, release);
  483. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  484. qxl_release_unreserve(qdev, release);
  485. return 0;
  486. }
  487. int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
  488. {
  489. struct qxl_rect rect;
  490. int ret;
  491. /* if we are evicting, we need to make sure the surface is up
  492. to date */
  493. rect.left = 0;
  494. rect.right = surf->surf.width;
  495. rect.top = 0;
  496. rect.bottom = surf->surf.height;
  497. retry:
  498. ret = qxl_io_update_area(qdev, surf, &rect);
  499. if (ret == -ERESTARTSYS)
  500. goto retry;
  501. return ret;
  502. }
  503. static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  504. {
  505. /* no need to update area if we are just freeing the surface normally */
  506. if (do_update_area)
  507. qxl_update_surface(qdev, surf);
  508. /* nuke the surface id at the hw */
  509. qxl_hw_surface_dealloc(qdev, surf);
  510. }
  511. void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  512. {
  513. mutex_lock(&qdev->surf_evict_mutex);
  514. qxl_surface_evict_locked(qdev, surf, do_update_area);
  515. mutex_unlock(&qdev->surf_evict_mutex);
  516. }
  517. static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
  518. {
  519. int ret;
  520. ret = qxl_bo_reserve(surf, false);
  521. if (ret == -EBUSY)
  522. return -EBUSY;
  523. if (surf->fence.num_active_releases > 0 && stall == false) {
  524. qxl_bo_unreserve(surf);
  525. return -EBUSY;
  526. }
  527. if (stall)
  528. mutex_unlock(&qdev->surf_evict_mutex);
  529. spin_lock(&surf->tbo.bdev->fence_lock);
  530. ret = ttm_bo_wait(&surf->tbo, true, true, !stall);
  531. spin_unlock(&surf->tbo.bdev->fence_lock);
  532. if (stall)
  533. mutex_lock(&qdev->surf_evict_mutex);
  534. if (ret == -EBUSY) {
  535. qxl_bo_unreserve(surf);
  536. return -EBUSY;
  537. }
  538. qxl_surface_evict_locked(qdev, surf, true);
  539. qxl_bo_unreserve(surf);
  540. return 0;
  541. }
  542. static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
  543. {
  544. int num_reaped = 0;
  545. int i, ret;
  546. bool stall = false;
  547. int start = 0;
  548. mutex_lock(&qdev->surf_evict_mutex);
  549. again:
  550. spin_lock(&qdev->surf_id_idr_lock);
  551. start = qdev->last_alloced_surf_id + 1;
  552. spin_unlock(&qdev->surf_id_idr_lock);
  553. for (i = start; i < start + qdev->rom->n_surfaces; i++) {
  554. void *objptr;
  555. int surfid = i % qdev->rom->n_surfaces;
  556. /* this avoids the case where the objects is in the
  557. idr but has been evicted half way - its makes
  558. the idr lookup atomic with the eviction */
  559. spin_lock(&qdev->surf_id_idr_lock);
  560. objptr = idr_find(&qdev->surf_id_idr, surfid);
  561. spin_unlock(&qdev->surf_id_idr_lock);
  562. if (!objptr)
  563. continue;
  564. ret = qxl_reap_surf(qdev, objptr, stall);
  565. if (ret == 0)
  566. num_reaped++;
  567. if (num_reaped >= max_to_reap)
  568. break;
  569. }
  570. if (num_reaped == 0 && stall == false) {
  571. stall = true;
  572. goto again;
  573. }
  574. mutex_unlock(&qdev->surf_evict_mutex);
  575. if (num_reaped) {
  576. usleep_range(500, 1000);
  577. qxl_queue_garbage_collect(qdev, true);
  578. }
  579. return 0;
  580. }