qxl_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. schedule_work(&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. if (bo->shadow) {
  334. create->mem = qxl_bo_physical_address(qdev, bo->shadow, offset);
  335. } else {
  336. create->mem = qxl_bo_physical_address(qdev, bo, offset);
  337. }
  338. QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem,
  339. bo->kptr);
  340. create->flags = QXL_SURF_FLAG_KEEP_DATA;
  341. create->type = QXL_SURF_TYPE_PRIMARY;
  342. wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
  343. }
  344. void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
  345. {
  346. QXL_INFO(qdev, "qxl_memslot_add %d\n", id);
  347. wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
  348. }
  349. void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...)
  350. {
  351. va_list args;
  352. va_start(args, fmt);
  353. vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args);
  354. va_end(args);
  355. /*
  356. * DO not do a DRM output here - this will call printk, which will
  357. * call back into qxl for rendering (qxl_fb)
  358. */
  359. outb(0, qdev->io_base + QXL_IO_LOG);
  360. }
  361. void qxl_io_reset(struct qxl_device *qdev)
  362. {
  363. outb(0, qdev->io_base + QXL_IO_RESET);
  364. }
  365. void qxl_io_monitors_config(struct qxl_device *qdev)
  366. {
  367. qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__,
  368. qdev->monitors_config ?
  369. qdev->monitors_config->count : -1,
  370. qdev->monitors_config && qdev->monitors_config->count ?
  371. qdev->monitors_config->heads[0].width : -1,
  372. qdev->monitors_config && qdev->monitors_config->count ?
  373. qdev->monitors_config->heads[0].height : -1,
  374. qdev->monitors_config && qdev->monitors_config->count ?
  375. qdev->monitors_config->heads[0].x : -1,
  376. qdev->monitors_config && qdev->monitors_config->count ?
  377. qdev->monitors_config->heads[0].y : -1
  378. );
  379. wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
  380. }
  381. int qxl_surface_id_alloc(struct qxl_device *qdev,
  382. struct qxl_bo *surf)
  383. {
  384. uint32_t handle;
  385. int idr_ret;
  386. int count = 0;
  387. again:
  388. idr_preload(GFP_ATOMIC);
  389. spin_lock(&qdev->surf_id_idr_lock);
  390. idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT);
  391. spin_unlock(&qdev->surf_id_idr_lock);
  392. idr_preload_end();
  393. if (idr_ret < 0)
  394. return idr_ret;
  395. handle = idr_ret;
  396. if (handle >= qdev->rom->n_surfaces) {
  397. count++;
  398. spin_lock(&qdev->surf_id_idr_lock);
  399. idr_remove(&qdev->surf_id_idr, handle);
  400. spin_unlock(&qdev->surf_id_idr_lock);
  401. qxl_reap_surface_id(qdev, 2);
  402. goto again;
  403. }
  404. surf->surface_id = handle;
  405. spin_lock(&qdev->surf_id_idr_lock);
  406. qdev->last_alloced_surf_id = handle;
  407. spin_unlock(&qdev->surf_id_idr_lock);
  408. return 0;
  409. }
  410. void qxl_surface_id_dealloc(struct qxl_device *qdev,
  411. uint32_t surface_id)
  412. {
  413. spin_lock(&qdev->surf_id_idr_lock);
  414. idr_remove(&qdev->surf_id_idr, surface_id);
  415. spin_unlock(&qdev->surf_id_idr_lock);
  416. }
  417. int qxl_hw_surface_alloc(struct qxl_device *qdev,
  418. struct qxl_bo *surf,
  419. struct ttm_mem_reg *new_mem)
  420. {
  421. struct qxl_surface_cmd *cmd;
  422. struct qxl_release *release;
  423. int ret;
  424. if (surf->hw_surf_alloc)
  425. return 0;
  426. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
  427. NULL,
  428. &release);
  429. if (ret)
  430. return ret;
  431. ret = qxl_release_reserve_list(release, true);
  432. if (ret) {
  433. qxl_release_free(qdev, release);
  434. return ret;
  435. }
  436. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  437. cmd->type = QXL_SURFACE_CMD_CREATE;
  438. cmd->flags = QXL_SURF_FLAG_KEEP_DATA;
  439. cmd->u.surface_create.format = surf->surf.format;
  440. cmd->u.surface_create.width = surf->surf.width;
  441. cmd->u.surface_create.height = surf->surf.height;
  442. cmd->u.surface_create.stride = surf->surf.stride;
  443. if (new_mem) {
  444. int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
  445. struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
  446. /* TODO - need to hold one of the locks to read tbo.offset */
  447. cmd->u.surface_create.data = slot->high_bits;
  448. cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
  449. } else
  450. cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
  451. cmd->surface_id = surf->surface_id;
  452. qxl_release_unmap(qdev, release, &cmd->release_info);
  453. surf->surf_create = release;
  454. /* no need to add a release to the fence for this surface bo,
  455. since it is only released when we ask to destroy the surface
  456. and it would never signal otherwise */
  457. qxl_release_fence_buffer_objects(release);
  458. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  459. surf->hw_surf_alloc = true;
  460. spin_lock(&qdev->surf_id_idr_lock);
  461. idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
  462. spin_unlock(&qdev->surf_id_idr_lock);
  463. return 0;
  464. }
  465. int qxl_hw_surface_dealloc(struct qxl_device *qdev,
  466. struct qxl_bo *surf)
  467. {
  468. struct qxl_surface_cmd *cmd;
  469. struct qxl_release *release;
  470. int ret;
  471. int id;
  472. if (!surf->hw_surf_alloc)
  473. return 0;
  474. ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
  475. surf->surf_create,
  476. &release);
  477. if (ret)
  478. return ret;
  479. surf->surf_create = NULL;
  480. /* remove the surface from the idr, but not the surface id yet */
  481. spin_lock(&qdev->surf_id_idr_lock);
  482. idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
  483. spin_unlock(&qdev->surf_id_idr_lock);
  484. surf->hw_surf_alloc = false;
  485. id = surf->surface_id;
  486. surf->surface_id = 0;
  487. release->surface_release_id = id;
  488. cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
  489. cmd->type = QXL_SURFACE_CMD_DESTROY;
  490. cmd->surface_id = id;
  491. qxl_release_unmap(qdev, release, &cmd->release_info);
  492. qxl_release_fence_buffer_objects(release);
  493. qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
  494. return 0;
  495. }
  496. static int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
  497. {
  498. struct qxl_rect rect;
  499. int ret;
  500. /* if we are evicting, we need to make sure the surface is up
  501. to date */
  502. rect.left = 0;
  503. rect.right = surf->surf.width;
  504. rect.top = 0;
  505. rect.bottom = surf->surf.height;
  506. retry:
  507. ret = qxl_io_update_area(qdev, surf, &rect);
  508. if (ret == -ERESTARTSYS)
  509. goto retry;
  510. return ret;
  511. }
  512. static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  513. {
  514. /* no need to update area if we are just freeing the surface normally */
  515. if (do_update_area)
  516. qxl_update_surface(qdev, surf);
  517. /* nuke the surface id at the hw */
  518. qxl_hw_surface_dealloc(qdev, surf);
  519. }
  520. void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
  521. {
  522. mutex_lock(&qdev->surf_evict_mutex);
  523. qxl_surface_evict_locked(qdev, surf, do_update_area);
  524. mutex_unlock(&qdev->surf_evict_mutex);
  525. }
  526. static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
  527. {
  528. int ret;
  529. ret = qxl_bo_reserve(surf, false);
  530. if (ret)
  531. return ret;
  532. if (stall)
  533. mutex_unlock(&qdev->surf_evict_mutex);
  534. ret = ttm_bo_wait(&surf->tbo, true, !stall);
  535. if (stall)
  536. mutex_lock(&qdev->surf_evict_mutex);
  537. if (ret) {
  538. qxl_bo_unreserve(surf);
  539. return ret;
  540. }
  541. qxl_surface_evict_locked(qdev, surf, true);
  542. qxl_bo_unreserve(surf);
  543. return 0;
  544. }
  545. static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
  546. {
  547. int num_reaped = 0;
  548. int i, ret;
  549. bool stall = false;
  550. int start = 0;
  551. mutex_lock(&qdev->surf_evict_mutex);
  552. again:
  553. spin_lock(&qdev->surf_id_idr_lock);
  554. start = qdev->last_alloced_surf_id + 1;
  555. spin_unlock(&qdev->surf_id_idr_lock);
  556. for (i = start; i < start + qdev->rom->n_surfaces; i++) {
  557. void *objptr;
  558. int surfid = i % qdev->rom->n_surfaces;
  559. /* this avoids the case where the objects is in the
  560. idr but has been evicted half way - its makes
  561. the idr lookup atomic with the eviction */
  562. spin_lock(&qdev->surf_id_idr_lock);
  563. objptr = idr_find(&qdev->surf_id_idr, surfid);
  564. spin_unlock(&qdev->surf_id_idr_lock);
  565. if (!objptr)
  566. continue;
  567. ret = qxl_reap_surf(qdev, objptr, stall);
  568. if (ret == 0)
  569. num_reaped++;
  570. if (num_reaped >= max_to_reap)
  571. break;
  572. }
  573. if (num_reaped == 0 && stall == false) {
  574. stall = true;
  575. goto again;
  576. }
  577. mutex_unlock(&qdev->surf_evict_mutex);
  578. if (num_reaped) {
  579. usleep_range(500, 1000);
  580. qxl_queue_garbage_collect(qdev, true);
  581. }
  582. return 0;
  583. }