xref: /linux/drivers/gpu/drm/qxl/qxl_cmd.c (revision e4a76442efe137b52bd493698e8134ba49d592fa)
1f64122c1SDave Airlie /*
2f64122c1SDave Airlie  * Copyright 2013 Red Hat Inc.
3f64122c1SDave Airlie  *
4f64122c1SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
5f64122c1SDave Airlie  * copy of this software and associated documentation files (the "Software"),
6f64122c1SDave Airlie  * to deal in the Software without restriction, including without limitation
7f64122c1SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f64122c1SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
9f64122c1SDave Airlie  * Software is furnished to do so, subject to the following conditions:
10f64122c1SDave Airlie  *
11f64122c1SDave Airlie  * The above copyright notice and this permission notice shall be included in
12f64122c1SDave Airlie  * all copies or substantial portions of the Software.
13f64122c1SDave Airlie  *
14f64122c1SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f64122c1SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f64122c1SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f64122c1SDave Airlie  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f64122c1SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f64122c1SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f64122c1SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
21f64122c1SDave Airlie  *
22f64122c1SDave Airlie  * Authors: Dave Airlie
23f64122c1SDave Airlie  *          Alon Levy
24f64122c1SDave Airlie  */
25f64122c1SDave Airlie 
26f64122c1SDave Airlie /* QXL cmd/ring handling */
27f64122c1SDave Airlie 
28f64122c1SDave Airlie #include "qxl_drv.h"
29f64122c1SDave Airlie #include "qxl_object.h"
30f64122c1SDave Airlie 
31f64122c1SDave Airlie static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap);
32f64122c1SDave Airlie 
33f64122c1SDave Airlie struct ring {
34f64122c1SDave Airlie 	struct qxl_ring_header      header;
35f64122c1SDave Airlie 	uint8_t                     elements[0];
36f64122c1SDave Airlie };
37f64122c1SDave Airlie 
38f64122c1SDave Airlie struct qxl_ring {
39f64122c1SDave Airlie 	struct ring	       *ring;
40f64122c1SDave Airlie 	int			element_size;
41f64122c1SDave Airlie 	int			n_elements;
42f64122c1SDave Airlie 	int			prod_notify;
43f64122c1SDave Airlie 	wait_queue_head_t      *push_event;
44f64122c1SDave Airlie 	spinlock_t             lock;
45f64122c1SDave Airlie };
46f64122c1SDave Airlie 
47f64122c1SDave Airlie void qxl_ring_free(struct qxl_ring *ring)
48f64122c1SDave Airlie {
49f64122c1SDave Airlie 	kfree(ring);
50f64122c1SDave Airlie }
51f64122c1SDave Airlie 
521e209117SDave Airlie void qxl_ring_init_hdr(struct qxl_ring *ring)
531e209117SDave Airlie {
541e209117SDave Airlie 	ring->ring->header.notify_on_prod = ring->n_elements;
551e209117SDave Airlie }
561e209117SDave Airlie 
57f64122c1SDave Airlie struct qxl_ring *
58f64122c1SDave Airlie qxl_ring_create(struct qxl_ring_header *header,
59f64122c1SDave Airlie 		int element_size,
60f64122c1SDave Airlie 		int n_elements,
61f64122c1SDave Airlie 		int prod_notify,
62f64122c1SDave Airlie 		bool set_prod_notify,
63f64122c1SDave Airlie 		wait_queue_head_t *push_event)
64f64122c1SDave Airlie {
65f64122c1SDave Airlie 	struct qxl_ring *ring;
66f64122c1SDave Airlie 
67f64122c1SDave Airlie 	ring = kmalloc(sizeof(*ring), GFP_KERNEL);
68f64122c1SDave Airlie 	if (!ring)
69f64122c1SDave Airlie 		return NULL;
70f64122c1SDave Airlie 
71f64122c1SDave Airlie 	ring->ring = (struct ring *)header;
72f64122c1SDave Airlie 	ring->element_size = element_size;
73f64122c1SDave Airlie 	ring->n_elements = n_elements;
74f64122c1SDave Airlie 	ring->prod_notify = prod_notify;
75f64122c1SDave Airlie 	ring->push_event = push_event;
76f64122c1SDave Airlie 	if (set_prod_notify)
771e209117SDave Airlie 		qxl_ring_init_hdr(ring);
78f64122c1SDave Airlie 	spin_lock_init(&ring->lock);
79f64122c1SDave Airlie 	return ring;
80f64122c1SDave Airlie }
81f64122c1SDave Airlie 
82f64122c1SDave Airlie static int qxl_check_header(struct qxl_ring *ring)
83f64122c1SDave Airlie {
84f64122c1SDave Airlie 	int ret;
85f64122c1SDave Airlie 	struct qxl_ring_header *header = &(ring->ring->header);
86f64122c1SDave Airlie 	unsigned long flags;
87f64122c1SDave Airlie 	spin_lock_irqsave(&ring->lock, flags);
88f64122c1SDave Airlie 	ret = header->prod - header->cons < header->num_items;
89f64122c1SDave Airlie 	if (ret == 0)
90f64122c1SDave Airlie 		header->notify_on_cons = header->cons + 1;
91f64122c1SDave Airlie 	spin_unlock_irqrestore(&ring->lock, flags);
92f64122c1SDave Airlie 	return ret;
93f64122c1SDave Airlie }
94f64122c1SDave Airlie 
951e209117SDave Airlie int qxl_check_idle(struct qxl_ring *ring)
96f64122c1SDave Airlie {
97f64122c1SDave Airlie 	int ret;
98f64122c1SDave Airlie 	struct qxl_ring_header *header = &(ring->ring->header);
99f64122c1SDave Airlie 	unsigned long flags;
100f64122c1SDave Airlie 	spin_lock_irqsave(&ring->lock, flags);
101f64122c1SDave Airlie 	ret = header->prod == header->cons;
102f64122c1SDave Airlie 	spin_unlock_irqrestore(&ring->lock, flags);
103f64122c1SDave Airlie 	return ret;
104f64122c1SDave Airlie }
105f64122c1SDave Airlie 
106f64122c1SDave Airlie int qxl_ring_push(struct qxl_ring *ring,
107f64122c1SDave Airlie 		  const void *new_elt, bool interruptible)
108f64122c1SDave Airlie {
109f64122c1SDave Airlie 	struct qxl_ring_header *header = &(ring->ring->header);
110f64122c1SDave Airlie 	uint8_t *elt;
111f64122c1SDave Airlie 	int idx, ret;
112f64122c1SDave Airlie 	unsigned long flags;
113f64122c1SDave Airlie 	spin_lock_irqsave(&ring->lock, flags);
114f64122c1SDave Airlie 	if (header->prod - header->cons == header->num_items) {
115f64122c1SDave Airlie 		header->notify_on_cons = header->cons + 1;
116f64122c1SDave Airlie 		mb();
117f64122c1SDave Airlie 		spin_unlock_irqrestore(&ring->lock, flags);
118f64122c1SDave Airlie 		if (!drm_can_sleep()) {
119f64122c1SDave Airlie 			while (!qxl_check_header(ring))
120f64122c1SDave Airlie 				udelay(1);
121f64122c1SDave Airlie 		} else {
122f64122c1SDave Airlie 			if (interruptible) {
123f64122c1SDave Airlie 				ret = wait_event_interruptible(*ring->push_event,
124f64122c1SDave Airlie 							       qxl_check_header(ring));
125f64122c1SDave Airlie 				if (ret)
126f64122c1SDave Airlie 					return ret;
127f64122c1SDave Airlie 			} else {
128f64122c1SDave Airlie 				wait_event(*ring->push_event,
129f64122c1SDave Airlie 					   qxl_check_header(ring));
130f64122c1SDave Airlie 			}
131f64122c1SDave Airlie 
132f64122c1SDave Airlie 		}
133f64122c1SDave Airlie 		spin_lock_irqsave(&ring->lock, flags);
134f64122c1SDave Airlie 	}
135f64122c1SDave Airlie 
136f64122c1SDave Airlie 	idx = header->prod & (ring->n_elements - 1);
137f64122c1SDave Airlie 	elt = ring->ring->elements + idx * ring->element_size;
138f64122c1SDave Airlie 
139f64122c1SDave Airlie 	memcpy((void *)elt, new_elt, ring->element_size);
140f64122c1SDave Airlie 
141f64122c1SDave Airlie 	header->prod++;
142f64122c1SDave Airlie 
143f64122c1SDave Airlie 	mb();
144f64122c1SDave Airlie 
145f64122c1SDave Airlie 	if (header->prod == header->notify_on_prod)
146f64122c1SDave Airlie 		outb(0, ring->prod_notify);
147f64122c1SDave Airlie 
148f64122c1SDave Airlie 	spin_unlock_irqrestore(&ring->lock, flags);
149f64122c1SDave Airlie 	return 0;
150f64122c1SDave Airlie }
151f64122c1SDave Airlie 
1526d01f1f5SDave Airlie static bool qxl_ring_pop(struct qxl_ring *ring,
153f64122c1SDave Airlie 			 void *element)
154f64122c1SDave Airlie {
155f64122c1SDave Airlie 	volatile struct qxl_ring_header *header = &(ring->ring->header);
156f64122c1SDave Airlie 	volatile uint8_t *ring_elt;
157f64122c1SDave Airlie 	int idx;
158f64122c1SDave Airlie 	unsigned long flags;
159f64122c1SDave Airlie 	spin_lock_irqsave(&ring->lock, flags);
160f64122c1SDave Airlie 	if (header->cons == header->prod) {
161f64122c1SDave Airlie 		header->notify_on_prod = header->cons + 1;
162f64122c1SDave Airlie 		spin_unlock_irqrestore(&ring->lock, flags);
163f64122c1SDave Airlie 		return false;
164f64122c1SDave Airlie 	}
165f64122c1SDave Airlie 
166f64122c1SDave Airlie 	idx = header->cons & (ring->n_elements - 1);
167f64122c1SDave Airlie 	ring_elt = ring->ring->elements + idx * ring->element_size;
168f64122c1SDave Airlie 
169f64122c1SDave Airlie 	memcpy(element, (void *)ring_elt, ring->element_size);
170f64122c1SDave Airlie 
171f64122c1SDave Airlie 	header->cons++;
172f64122c1SDave Airlie 
173f64122c1SDave Airlie 	spin_unlock_irqrestore(&ring->lock, flags);
174f64122c1SDave Airlie 	return true;
175f64122c1SDave Airlie }
176f64122c1SDave Airlie 
177f64122c1SDave Airlie int
178f64122c1SDave Airlie qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release,
179f64122c1SDave Airlie 			      uint32_t type, bool interruptible)
180f64122c1SDave Airlie {
181f64122c1SDave Airlie 	struct qxl_command cmd;
1828002db63SDave Airlie 	struct qxl_bo_list *entry = list_first_entry(&release->bos, struct qxl_bo_list, tv.head);
183f64122c1SDave Airlie 
184f64122c1SDave Airlie 	cmd.type = type;
1858002db63SDave Airlie 	cmd.data = qxl_bo_physical_address(qdev, to_qxl_bo(entry->tv.bo), release->release_offset);
186f64122c1SDave Airlie 
187f64122c1SDave Airlie 	return qxl_ring_push(qdev->command_ring, &cmd, interruptible);
188f64122c1SDave Airlie }
189f64122c1SDave Airlie 
190f64122c1SDave Airlie int
191f64122c1SDave Airlie qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release,
192f64122c1SDave Airlie 			     uint32_t type, bool interruptible)
193f64122c1SDave Airlie {
194f64122c1SDave Airlie 	struct qxl_command cmd;
1958002db63SDave Airlie 	struct qxl_bo_list *entry = list_first_entry(&release->bos, struct qxl_bo_list, tv.head);
196f64122c1SDave Airlie 
197f64122c1SDave Airlie 	cmd.type = type;
1988002db63SDave Airlie 	cmd.data = qxl_bo_physical_address(qdev, to_qxl_bo(entry->tv.bo), release->release_offset);
199f64122c1SDave Airlie 
200f64122c1SDave Airlie 	return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible);
201f64122c1SDave Airlie }
202f64122c1SDave Airlie 
203f64122c1SDave Airlie bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush)
204f64122c1SDave Airlie {
205f64122c1SDave Airlie 	if (!qxl_check_idle(qdev->release_ring)) {
2067b2d16f5SBhaktipriya Shridhar 		schedule_work(&qdev->gc_work);
207f64122c1SDave Airlie 		if (flush)
208f64122c1SDave Airlie 			flush_work(&qdev->gc_work);
209f64122c1SDave Airlie 		return true;
210f64122c1SDave Airlie 	}
211f64122c1SDave Airlie 	return false;
212f64122c1SDave Airlie }
213f64122c1SDave Airlie 
214f64122c1SDave Airlie int qxl_garbage_collect(struct qxl_device *qdev)
215f64122c1SDave Airlie {
216f64122c1SDave Airlie 	struct qxl_release *release;
217f64122c1SDave Airlie 	uint64_t id, next_id;
218f64122c1SDave Airlie 	int i = 0;
219f64122c1SDave Airlie 	union qxl_release_info *info;
220f64122c1SDave Airlie 
221f64122c1SDave Airlie 	while (qxl_ring_pop(qdev->release_ring, &id)) {
222f64122c1SDave Airlie 		QXL_INFO(qdev, "popped %lld\n", id);
223f64122c1SDave Airlie 		while (id) {
224f64122c1SDave Airlie 			release = qxl_release_from_id_locked(qdev, id);
225f64122c1SDave Airlie 			if (release == NULL)
226f64122c1SDave Airlie 				break;
227f64122c1SDave Airlie 
228f64122c1SDave Airlie 			info = qxl_release_map(qdev, release);
229f64122c1SDave Airlie 			next_id = info->next;
230f64122c1SDave Airlie 			qxl_release_unmap(qdev, release, info);
231f64122c1SDave Airlie 
232f64122c1SDave Airlie 			QXL_INFO(qdev, "popped %lld, next %lld\n", id,
233f64122c1SDave Airlie 				next_id);
234f64122c1SDave Airlie 
235f64122c1SDave Airlie 			switch (release->type) {
236f64122c1SDave Airlie 			case QXL_RELEASE_DRAWABLE:
237f64122c1SDave Airlie 			case QXL_RELEASE_SURFACE_CMD:
238f64122c1SDave Airlie 			case QXL_RELEASE_CURSOR_CMD:
239f64122c1SDave Airlie 				break;
240f64122c1SDave Airlie 			default:
241f64122c1SDave Airlie 				DRM_ERROR("unexpected release type\n");
242f64122c1SDave Airlie 				break;
243f64122c1SDave Airlie 			}
244f64122c1SDave Airlie 			id = next_id;
245f64122c1SDave Airlie 
246f64122c1SDave Airlie 			qxl_release_free(qdev, release);
247f64122c1SDave Airlie 			++i;
248f64122c1SDave Airlie 		}
249f64122c1SDave Airlie 	}
250f64122c1SDave Airlie 
25172ec5650SFrediano Ziglio 	QXL_INFO(qdev, "%s: %d\n", __func__, i);
252f64122c1SDave Airlie 
253f64122c1SDave Airlie 	return i;
254f64122c1SDave Airlie }
255f64122c1SDave Airlie 
2568002db63SDave Airlie int qxl_alloc_bo_reserved(struct qxl_device *qdev,
2578002db63SDave Airlie 			  struct qxl_release *release,
2588002db63SDave Airlie 			  unsigned long size,
259f64122c1SDave Airlie 			  struct qxl_bo **_bo)
260f64122c1SDave Airlie {
261f64122c1SDave Airlie 	struct qxl_bo *bo;
262f64122c1SDave Airlie 	int ret;
263f64122c1SDave Airlie 
264f64122c1SDave Airlie 	ret = qxl_bo_create(qdev, size, false /* not kernel - device */,
2654f49ec92SDave Airlie 			    false, QXL_GEM_DOMAIN_VRAM, NULL, &bo);
266f64122c1SDave Airlie 	if (ret) {
267f64122c1SDave Airlie 		DRM_ERROR("failed to allocate VRAM BO\n");
268f64122c1SDave Airlie 		return ret;
269f64122c1SDave Airlie 	}
2708002db63SDave Airlie 	ret = qxl_release_list_add(release, bo);
2718002db63SDave Airlie 	if (ret)
272f64122c1SDave Airlie 		goto out_unref;
273f64122c1SDave Airlie 
274f64122c1SDave Airlie 	*_bo = bo;
275f64122c1SDave Airlie 	return 0;
276f64122c1SDave Airlie out_unref:
277f64122c1SDave Airlie 	qxl_bo_unref(&bo);
2788002db63SDave Airlie 	return ret;
279f64122c1SDave Airlie }
280f64122c1SDave Airlie 
281a6ac1bc3SDave Airlie static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port, bool intr)
282f64122c1SDave Airlie {
283f64122c1SDave Airlie 	int irq_num;
284f64122c1SDave Airlie 	long addr = qdev->io_base + port;
285f64122c1SDave Airlie 	int ret;
286f64122c1SDave Airlie 
287f64122c1SDave Airlie 	mutex_lock(&qdev->async_io_mutex);
288f64122c1SDave Airlie 	irq_num = atomic_read(&qdev->irq_received_io_cmd);
289f64122c1SDave Airlie 	if (qdev->last_sent_io_cmd > irq_num) {
290a6ac1bc3SDave Airlie 		if (intr)
291a6ac1bc3SDave Airlie 			ret = wait_event_interruptible_timeout(qdev->io_cmd_event,
292a6ac1bc3SDave Airlie 							       atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
293a6ac1bc3SDave Airlie 		else
294a6ac1bc3SDave Airlie 			ret = wait_event_timeout(qdev->io_cmd_event,
295a6ac1bc3SDave Airlie 						 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
296a6ac1bc3SDave Airlie 		/* 0 is timeout, just bail the "hw" has gone away */
297a6ac1bc3SDave Airlie 		if (ret <= 0)
298f64122c1SDave Airlie 			goto out;
299f64122c1SDave Airlie 		irq_num = atomic_read(&qdev->irq_received_io_cmd);
300f64122c1SDave Airlie 	}
301f64122c1SDave Airlie 	outb(val, addr);
302f64122c1SDave Airlie 	qdev->last_sent_io_cmd = irq_num + 1;
303a6ac1bc3SDave Airlie 	if (intr)
304a6ac1bc3SDave Airlie 		ret = wait_event_interruptible_timeout(qdev->io_cmd_event,
305a6ac1bc3SDave Airlie 						       atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
306a6ac1bc3SDave Airlie 	else
307a6ac1bc3SDave Airlie 		ret = wait_event_timeout(qdev->io_cmd_event,
308a6ac1bc3SDave Airlie 					 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
309f64122c1SDave Airlie out:
310a6ac1bc3SDave Airlie 	if (ret > 0)
311a6ac1bc3SDave Airlie 		ret = 0;
312f64122c1SDave Airlie 	mutex_unlock(&qdev->async_io_mutex);
313f64122c1SDave Airlie 	return ret;
314f64122c1SDave Airlie }
315f64122c1SDave Airlie 
316f64122c1SDave Airlie static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port)
317f64122c1SDave Airlie {
318f64122c1SDave Airlie 	int ret;
319f64122c1SDave Airlie 
320f64122c1SDave Airlie restart:
321a6ac1bc3SDave Airlie 	ret = wait_for_io_cmd_user(qdev, val, port, false);
322f64122c1SDave Airlie 	if (ret == -ERESTARTSYS)
323f64122c1SDave Airlie 		goto restart;
324f64122c1SDave Airlie }
325f64122c1SDave Airlie 
326f64122c1SDave Airlie int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf,
327f64122c1SDave Airlie 			const struct qxl_rect *area)
328f64122c1SDave Airlie {
329f64122c1SDave Airlie 	int surface_id;
330f64122c1SDave Airlie 	uint32_t surface_width, surface_height;
331f64122c1SDave Airlie 	int ret;
332f64122c1SDave Airlie 
333f64122c1SDave Airlie 	if (!surf->hw_surf_alloc)
334f64122c1SDave Airlie 		DRM_ERROR("got io update area with no hw surface\n");
335f64122c1SDave Airlie 
336f64122c1SDave Airlie 	if (surf->is_primary)
337f64122c1SDave Airlie 		surface_id = 0;
338f64122c1SDave Airlie 	else
339f64122c1SDave Airlie 		surface_id = surf->surface_id;
340f64122c1SDave Airlie 	surface_width = surf->surf.width;
341f64122c1SDave Airlie 	surface_height = surf->surf.height;
342f64122c1SDave Airlie 
343f64122c1SDave Airlie 	if (area->left < 0 || area->top < 0 ||
344f64122c1SDave Airlie 	    area->right > surface_width || area->bottom > surface_height) {
345f64122c1SDave Airlie 		qxl_io_log(qdev, "%s: not doing area update for "
346f64122c1SDave Airlie 			   "%d, (%d,%d,%d,%d) (%d,%d)\n", __func__, surface_id, area->left,
347f64122c1SDave Airlie 			   area->top, area->right, area->bottom, surface_width, surface_height);
348f64122c1SDave Airlie 		return -EINVAL;
349f64122c1SDave Airlie 	}
350f64122c1SDave Airlie 	mutex_lock(&qdev->update_area_mutex);
351f64122c1SDave Airlie 	qdev->ram_header->update_area = *area;
352f64122c1SDave Airlie 	qdev->ram_header->update_surface = surface_id;
353a6ac1bc3SDave Airlie 	ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC, true);
354f64122c1SDave Airlie 	mutex_unlock(&qdev->update_area_mutex);
355f64122c1SDave Airlie 	return ret;
356f64122c1SDave Airlie }
357f64122c1SDave Airlie 
358f64122c1SDave Airlie void qxl_io_notify_oom(struct qxl_device *qdev)
359f64122c1SDave Airlie {
360f64122c1SDave Airlie 	outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM);
361f64122c1SDave Airlie }
362f64122c1SDave Airlie 
363f64122c1SDave Airlie void qxl_io_flush_release(struct qxl_device *qdev)
364f64122c1SDave Airlie {
365f64122c1SDave Airlie 	outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE);
366f64122c1SDave Airlie }
367f64122c1SDave Airlie 
368f64122c1SDave Airlie void qxl_io_flush_surfaces(struct qxl_device *qdev)
369f64122c1SDave Airlie {
370f64122c1SDave Airlie 	wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC);
371f64122c1SDave Airlie }
372f64122c1SDave Airlie 
373f64122c1SDave Airlie 
374f64122c1SDave Airlie void qxl_io_destroy_primary(struct qxl_device *qdev)
375f64122c1SDave Airlie {
376f64122c1SDave Airlie 	wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC);
377f64122c1SDave Airlie }
378f64122c1SDave Airlie 
37907f8d9bdSDave Airlie void qxl_io_create_primary(struct qxl_device *qdev,
38007f8d9bdSDave Airlie 			   unsigned offset, struct qxl_bo *bo)
381f64122c1SDave Airlie {
382f64122c1SDave Airlie 	struct qxl_surface_create *create;
383f64122c1SDave Airlie 
384f64122c1SDave Airlie 	QXL_INFO(qdev, "%s: qdev %p, ram_header %p\n", __func__, qdev,
385f64122c1SDave Airlie 		 qdev->ram_header);
386f64122c1SDave Airlie 	create = &qdev->ram_header->create_surface;
387f64122c1SDave Airlie 	create->format = bo->surf.format;
38807f8d9bdSDave Airlie 	create->width = bo->surf.width;
38907f8d9bdSDave Airlie 	create->height = bo->surf.height;
390f64122c1SDave Airlie 	create->stride = bo->surf.stride;
391f64122c1SDave Airlie 	create->mem = qxl_bo_physical_address(qdev, bo, offset);
392f64122c1SDave Airlie 
393f64122c1SDave Airlie 	QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem,
394f64122c1SDave Airlie 		 bo->kptr);
395f64122c1SDave Airlie 
396f64122c1SDave Airlie 	create->flags = QXL_SURF_FLAG_KEEP_DATA;
397f64122c1SDave Airlie 	create->type = QXL_SURF_TYPE_PRIMARY;
398f64122c1SDave Airlie 
399f64122c1SDave Airlie 	wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
400f64122c1SDave Airlie }
401f64122c1SDave Airlie 
402f64122c1SDave Airlie void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
403f64122c1SDave Airlie {
404f64122c1SDave Airlie 	QXL_INFO(qdev, "qxl_memslot_add %d\n", id);
405f64122c1SDave Airlie 	wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
406f64122c1SDave Airlie }
407f64122c1SDave Airlie 
408f64122c1SDave Airlie void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...)
409f64122c1SDave Airlie {
410f64122c1SDave Airlie 	va_list args;
411f64122c1SDave Airlie 
412f64122c1SDave Airlie 	va_start(args, fmt);
413f64122c1SDave Airlie 	vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args);
414f64122c1SDave Airlie 	va_end(args);
415f64122c1SDave Airlie 	/*
416f64122c1SDave Airlie 	 * DO not do a DRM output here - this will call printk, which will
417f64122c1SDave Airlie 	 * call back into qxl for rendering (qxl_fb)
418f64122c1SDave Airlie 	 */
419f64122c1SDave Airlie 	outb(0, qdev->io_base + QXL_IO_LOG);
420f64122c1SDave Airlie }
421f64122c1SDave Airlie 
422f64122c1SDave Airlie void qxl_io_reset(struct qxl_device *qdev)
423f64122c1SDave Airlie {
424f64122c1SDave Airlie 	outb(0, qdev->io_base + QXL_IO_RESET);
425f64122c1SDave Airlie }
426f64122c1SDave Airlie 
427f64122c1SDave Airlie void qxl_io_monitors_config(struct qxl_device *qdev)
428f64122c1SDave Airlie {
429f64122c1SDave Airlie 	qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__,
430f64122c1SDave Airlie 		   qdev->monitors_config ?
431f64122c1SDave Airlie 		   qdev->monitors_config->count : -1,
432f64122c1SDave Airlie 		   qdev->monitors_config && qdev->monitors_config->count ?
433f64122c1SDave Airlie 		   qdev->monitors_config->heads[0].width : -1,
434f64122c1SDave Airlie 		   qdev->monitors_config && qdev->monitors_config->count ?
435f64122c1SDave Airlie 		   qdev->monitors_config->heads[0].height : -1,
436f64122c1SDave Airlie 		   qdev->monitors_config && qdev->monitors_config->count ?
437f64122c1SDave Airlie 		   qdev->monitors_config->heads[0].x : -1,
438f64122c1SDave Airlie 		   qdev->monitors_config && qdev->monitors_config->count ?
439f64122c1SDave Airlie 		   qdev->monitors_config->heads[0].y : -1
440f64122c1SDave Airlie 		   );
441f64122c1SDave Airlie 
442f64122c1SDave Airlie 	wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
443f64122c1SDave Airlie }
444f64122c1SDave Airlie 
445f64122c1SDave Airlie int qxl_surface_id_alloc(struct qxl_device *qdev,
446f64122c1SDave Airlie 		      struct qxl_bo *surf)
447f64122c1SDave Airlie {
448307b9c02SDave Airlie 	uint32_t handle;
449f64122c1SDave Airlie 	int idr_ret;
450f64122c1SDave Airlie 	int count = 0;
451f64122c1SDave Airlie again:
452307b9c02SDave Airlie 	idr_preload(GFP_ATOMIC);
453f64122c1SDave Airlie 	spin_lock(&qdev->surf_id_idr_lock);
454307b9c02SDave Airlie 	idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT);
455f64122c1SDave Airlie 	spin_unlock(&qdev->surf_id_idr_lock);
456307b9c02SDave Airlie 	idr_preload_end();
457307b9c02SDave Airlie 	if (idr_ret < 0)
458307b9c02SDave Airlie 		return idr_ret;
459307b9c02SDave Airlie 	handle = idr_ret;
460f64122c1SDave Airlie 
461f64122c1SDave Airlie 	if (handle >= qdev->rom->n_surfaces) {
462f64122c1SDave Airlie 		count++;
463f64122c1SDave Airlie 		spin_lock(&qdev->surf_id_idr_lock);
464f64122c1SDave Airlie 		idr_remove(&qdev->surf_id_idr, handle);
465f64122c1SDave Airlie 		spin_unlock(&qdev->surf_id_idr_lock);
466f64122c1SDave Airlie 		qxl_reap_surface_id(qdev, 2);
467f64122c1SDave Airlie 		goto again;
468f64122c1SDave Airlie 	}
469f64122c1SDave Airlie 	surf->surface_id = handle;
470f64122c1SDave Airlie 
471f64122c1SDave Airlie 	spin_lock(&qdev->surf_id_idr_lock);
472f64122c1SDave Airlie 	qdev->last_alloced_surf_id = handle;
473f64122c1SDave Airlie 	spin_unlock(&qdev->surf_id_idr_lock);
474f64122c1SDave Airlie 	return 0;
475f64122c1SDave Airlie }
476f64122c1SDave Airlie 
477f64122c1SDave Airlie void qxl_surface_id_dealloc(struct qxl_device *qdev,
478f64122c1SDave Airlie 			    uint32_t surface_id)
479f64122c1SDave Airlie {
480f64122c1SDave Airlie 	spin_lock(&qdev->surf_id_idr_lock);
481f64122c1SDave Airlie 	idr_remove(&qdev->surf_id_idr, surface_id);
482f64122c1SDave Airlie 	spin_unlock(&qdev->surf_id_idr_lock);
483f64122c1SDave Airlie }
484f64122c1SDave Airlie 
485f64122c1SDave Airlie int qxl_hw_surface_alloc(struct qxl_device *qdev,
486f64122c1SDave Airlie 			 struct qxl_bo *surf,
487f64122c1SDave Airlie 			 struct ttm_mem_reg *new_mem)
488f64122c1SDave Airlie {
489f64122c1SDave Airlie 	struct qxl_surface_cmd *cmd;
490f64122c1SDave Airlie 	struct qxl_release *release;
491f64122c1SDave Airlie 	int ret;
492f64122c1SDave Airlie 
493f64122c1SDave Airlie 	if (surf->hw_surf_alloc)
494f64122c1SDave Airlie 		return 0;
495f64122c1SDave Airlie 
496f64122c1SDave Airlie 	ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
497f64122c1SDave Airlie 						 NULL,
498f64122c1SDave Airlie 						 &release);
499f64122c1SDave Airlie 	if (ret)
500f64122c1SDave Airlie 		return ret;
501f64122c1SDave Airlie 
5028002db63SDave Airlie 	ret = qxl_release_reserve_list(release, true);
5038002db63SDave Airlie 	if (ret)
5048002db63SDave Airlie 		return ret;
5058002db63SDave Airlie 
506f64122c1SDave Airlie 	cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
507f64122c1SDave Airlie 	cmd->type = QXL_SURFACE_CMD_CREATE;
5082fa19535SFrediano Ziglio 	cmd->flags = QXL_SURF_FLAG_KEEP_DATA;
509f64122c1SDave Airlie 	cmd->u.surface_create.format = surf->surf.format;
510f64122c1SDave Airlie 	cmd->u.surface_create.width = surf->surf.width;
511f64122c1SDave Airlie 	cmd->u.surface_create.height = surf->surf.height;
512f64122c1SDave Airlie 	cmd->u.surface_create.stride = surf->surf.stride;
513f64122c1SDave Airlie 	if (new_mem) {
514f64122c1SDave Airlie 		int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
515f64122c1SDave Airlie 		struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
516f64122c1SDave Airlie 
517f64122c1SDave Airlie 		/* TODO - need to hold one of the locks to read tbo.offset */
518f64122c1SDave Airlie 		cmd->u.surface_create.data = slot->high_bits;
519f64122c1SDave Airlie 
520f64122c1SDave Airlie 		cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
521f64122c1SDave Airlie 	} else
522f64122c1SDave Airlie 		cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
523f64122c1SDave Airlie 	cmd->surface_id = surf->surface_id;
524f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
525f64122c1SDave Airlie 
526f64122c1SDave Airlie 	surf->surf_create = release;
527f64122c1SDave Airlie 
5288002db63SDave Airlie 	/* no need to add a release to the fence for this surface bo,
529f64122c1SDave Airlie 	   since it is only released when we ask to destroy the surface
530f64122c1SDave Airlie 	   and it would never signal otherwise */
531f64122c1SDave Airlie 	qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
5328002db63SDave Airlie 	qxl_release_fence_buffer_objects(release);
533f64122c1SDave Airlie 
534f64122c1SDave Airlie 	surf->hw_surf_alloc = true;
535f64122c1SDave Airlie 	spin_lock(&qdev->surf_id_idr_lock);
536f64122c1SDave Airlie 	idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
537f64122c1SDave Airlie 	spin_unlock(&qdev->surf_id_idr_lock);
538f64122c1SDave Airlie 	return 0;
539f64122c1SDave Airlie }
540f64122c1SDave Airlie 
541f64122c1SDave Airlie int qxl_hw_surface_dealloc(struct qxl_device *qdev,
542f64122c1SDave Airlie 			   struct qxl_bo *surf)
543f64122c1SDave Airlie {
544f64122c1SDave Airlie 	struct qxl_surface_cmd *cmd;
545f64122c1SDave Airlie 	struct qxl_release *release;
546f64122c1SDave Airlie 	int ret;
547f64122c1SDave Airlie 	int id;
548f64122c1SDave Airlie 
549f64122c1SDave Airlie 	if (!surf->hw_surf_alloc)
550f64122c1SDave Airlie 		return 0;
551f64122c1SDave Airlie 
552f64122c1SDave Airlie 	ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
553f64122c1SDave Airlie 						 surf->surf_create,
554f64122c1SDave Airlie 						 &release);
555f64122c1SDave Airlie 	if (ret)
556f64122c1SDave Airlie 		return ret;
557f64122c1SDave Airlie 
558f64122c1SDave Airlie 	surf->surf_create = NULL;
559f64122c1SDave Airlie 	/* remove the surface from the idr, but not the surface id yet */
560f64122c1SDave Airlie 	spin_lock(&qdev->surf_id_idr_lock);
561f64122c1SDave Airlie 	idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
562f64122c1SDave Airlie 	spin_unlock(&qdev->surf_id_idr_lock);
563f64122c1SDave Airlie 	surf->hw_surf_alloc = false;
564f64122c1SDave Airlie 
565f64122c1SDave Airlie 	id = surf->surface_id;
566f64122c1SDave Airlie 	surf->surface_id = 0;
567f64122c1SDave Airlie 
568f64122c1SDave Airlie 	release->surface_release_id = id;
569f64122c1SDave Airlie 	cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
570f64122c1SDave Airlie 	cmd->type = QXL_SURFACE_CMD_DESTROY;
571f64122c1SDave Airlie 	cmd->surface_id = id;
572f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
573f64122c1SDave Airlie 
574f64122c1SDave Airlie 	qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
575f64122c1SDave Airlie 
5768002db63SDave Airlie 	qxl_release_fence_buffer_objects(release);
577f64122c1SDave Airlie 
578f64122c1SDave Airlie 	return 0;
579f64122c1SDave Airlie }
580f64122c1SDave Airlie 
581*e4a76442SChristophe Fergeau static int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
582f64122c1SDave Airlie {
583f64122c1SDave Airlie 	struct qxl_rect rect;
584f64122c1SDave Airlie 	int ret;
585f64122c1SDave Airlie 
586f64122c1SDave Airlie 	/* if we are evicting, we need to make sure the surface is up
587f64122c1SDave Airlie 	   to date */
588f64122c1SDave Airlie 	rect.left = 0;
589f64122c1SDave Airlie 	rect.right = surf->surf.width;
590f64122c1SDave Airlie 	rect.top = 0;
591f64122c1SDave Airlie 	rect.bottom = surf->surf.height;
592f64122c1SDave Airlie retry:
593f64122c1SDave Airlie 	ret = qxl_io_update_area(qdev, surf, &rect);
594f64122c1SDave Airlie 	if (ret == -ERESTARTSYS)
595f64122c1SDave Airlie 		goto retry;
596f64122c1SDave Airlie 	return ret;
597f64122c1SDave Airlie }
598f64122c1SDave Airlie 
5996d01f1f5SDave Airlie static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
600f64122c1SDave Airlie {
601f64122c1SDave Airlie 	/* no need to update area if we are just freeing the surface normally */
602f64122c1SDave Airlie 	if (do_update_area)
603f64122c1SDave Airlie 		qxl_update_surface(qdev, surf);
604f64122c1SDave Airlie 
605f64122c1SDave Airlie 	/* nuke the surface id at the hw */
606f64122c1SDave Airlie 	qxl_hw_surface_dealloc(qdev, surf);
607f64122c1SDave Airlie }
608f64122c1SDave Airlie 
609f64122c1SDave Airlie void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
610f64122c1SDave Airlie {
611f64122c1SDave Airlie 	mutex_lock(&qdev->surf_evict_mutex);
612f64122c1SDave Airlie 	qxl_surface_evict_locked(qdev, surf, do_update_area);
613f64122c1SDave Airlie 	mutex_unlock(&qdev->surf_evict_mutex);
614f64122c1SDave Airlie }
615f64122c1SDave Airlie 
616f64122c1SDave Airlie static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
617f64122c1SDave Airlie {
618f64122c1SDave Airlie 	int ret;
619f64122c1SDave Airlie 
620f64122c1SDave Airlie 	ret = qxl_bo_reserve(surf, false);
621c610c713SFrediano Ziglio 	if (ret)
622c610c713SFrediano Ziglio 		return ret;
623f64122c1SDave Airlie 
624f64122c1SDave Airlie 	if (stall)
625f64122c1SDave Airlie 		mutex_unlock(&qdev->surf_evict_mutex);
626f64122c1SDave Airlie 
6278aa6d4fcSChristian König 	ret = ttm_bo_wait(&surf->tbo, true, !stall);
628f64122c1SDave Airlie 
629f64122c1SDave Airlie 	if (stall)
630f64122c1SDave Airlie 		mutex_lock(&qdev->surf_evict_mutex);
631c610c713SFrediano Ziglio 	if (ret) {
632f64122c1SDave Airlie 		qxl_bo_unreserve(surf);
633c610c713SFrediano Ziglio 		return ret;
634f64122c1SDave Airlie 	}
635f64122c1SDave Airlie 
636f64122c1SDave Airlie 	qxl_surface_evict_locked(qdev, surf, true);
637f64122c1SDave Airlie 	qxl_bo_unreserve(surf);
638f64122c1SDave Airlie 	return 0;
639f64122c1SDave Airlie }
640f64122c1SDave Airlie 
641f64122c1SDave Airlie static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
642f64122c1SDave Airlie {
643f64122c1SDave Airlie 	int num_reaped = 0;
644f64122c1SDave Airlie 	int i, ret;
645f64122c1SDave Airlie 	bool stall = false;
646f64122c1SDave Airlie 	int start = 0;
647f64122c1SDave Airlie 
648f64122c1SDave Airlie 	mutex_lock(&qdev->surf_evict_mutex);
649f64122c1SDave Airlie again:
650f64122c1SDave Airlie 
651f64122c1SDave Airlie 	spin_lock(&qdev->surf_id_idr_lock);
652f64122c1SDave Airlie 	start = qdev->last_alloced_surf_id + 1;
653f64122c1SDave Airlie 	spin_unlock(&qdev->surf_id_idr_lock);
654f64122c1SDave Airlie 
655f64122c1SDave Airlie 	for (i = start; i < start + qdev->rom->n_surfaces; i++) {
656f64122c1SDave Airlie 		void *objptr;
657f64122c1SDave Airlie 		int surfid = i % qdev->rom->n_surfaces;
658f64122c1SDave Airlie 
659f64122c1SDave Airlie 		/* this avoids the case where the objects is in the
660f64122c1SDave Airlie 		   idr but has been evicted half way - its makes
661f64122c1SDave Airlie 		   the idr lookup atomic with the eviction */
662f64122c1SDave Airlie 		spin_lock(&qdev->surf_id_idr_lock);
663f64122c1SDave Airlie 		objptr = idr_find(&qdev->surf_id_idr, surfid);
664f64122c1SDave Airlie 		spin_unlock(&qdev->surf_id_idr_lock);
665f64122c1SDave Airlie 
666f64122c1SDave Airlie 		if (!objptr)
667f64122c1SDave Airlie 			continue;
668f64122c1SDave Airlie 
669f64122c1SDave Airlie 		ret = qxl_reap_surf(qdev, objptr, stall);
670f64122c1SDave Airlie 		if (ret == 0)
671f64122c1SDave Airlie 			num_reaped++;
672f64122c1SDave Airlie 		if (num_reaped >= max_to_reap)
673f64122c1SDave Airlie 			break;
674f64122c1SDave Airlie 	}
675f64122c1SDave Airlie 	if (num_reaped == 0 && stall == false) {
676f64122c1SDave Airlie 		stall = true;
677f64122c1SDave Airlie 		goto again;
678f64122c1SDave Airlie 	}
679f64122c1SDave Airlie 
680f64122c1SDave Airlie 	mutex_unlock(&qdev->surf_evict_mutex);
681f64122c1SDave Airlie 	if (num_reaped) {
682f64122c1SDave Airlie 		usleep_range(500, 1000);
683f64122c1SDave Airlie 		qxl_queue_garbage_collect(qdev, true);
684f64122c1SDave Airlie 	}
685f64122c1SDave Airlie 
686f64122c1SDave Airlie 	return 0;
687f64122c1SDave Airlie }
688