vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / xen / xen_drm_front_shbuf.c
blobd333b67cc1a06b4cbfe838ddf3ab4245c1de3ae1
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 /*
4 * Xen para-virtual DRM device
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
9 */
11 #include <drm/drmP.h>
13 #if defined(CONFIG_X86)
14 #include <drm/drm_cache.h>
15 #endif
16 #include <linux/errno.h>
17 #include <linux/mm.h>
19 #include <asm/xen/hypervisor.h>
20 #include <xen/balloon.h>
21 #include <xen/xen.h>
22 #include <xen/xenbus.h>
23 #include <xen/interface/io/ring.h>
24 #include <xen/interface/io/displif.h>
26 #include "xen_drm_front.h"
27 #include "xen_drm_front_shbuf.h"
29 struct xen_drm_front_shbuf_ops {
31 * Calculate number of grefs required to handle this buffer,
32 * e.g. if grefs are required for page directory only or the buffer
33 * pages as well.
35 void (*calc_num_grefs)(struct xen_drm_front_shbuf *buf);
36 /* Fill page directory according to para-virtual display protocol. */
37 void (*fill_page_dir)(struct xen_drm_front_shbuf *buf);
38 /* Claim grant references for the pages of the buffer. */
39 int (*grant_refs_for_buffer)(struct xen_drm_front_shbuf *buf,
40 grant_ref_t *priv_gref_head, int gref_idx);
41 /* Map grant references of the buffer. */
42 int (*map)(struct xen_drm_front_shbuf *buf);
43 /* Unmap grant references of the buffer. */
44 int (*unmap)(struct xen_drm_front_shbuf *buf);
47 grant_ref_t xen_drm_front_shbuf_get_dir_start(struct xen_drm_front_shbuf *buf)
49 if (!buf->grefs)
50 return GRANT_INVALID_REF;
52 return buf->grefs[0];
55 int xen_drm_front_shbuf_map(struct xen_drm_front_shbuf *buf)
57 if (buf->ops->map)
58 return buf->ops->map(buf);
60 /* no need to map own grant references */
61 return 0;
64 int xen_drm_front_shbuf_unmap(struct xen_drm_front_shbuf *buf)
66 if (buf->ops->unmap)
67 return buf->ops->unmap(buf);
69 /* no need to unmap own grant references */
70 return 0;
73 void xen_drm_front_shbuf_flush(struct xen_drm_front_shbuf *buf)
75 #if defined(CONFIG_X86)
76 drm_clflush_pages(buf->pages, buf->num_pages);
77 #endif
80 void xen_drm_front_shbuf_free(struct xen_drm_front_shbuf *buf)
82 if (buf->grefs) {
83 int i;
85 for (i = 0; i < buf->num_grefs; i++)
86 if (buf->grefs[i] != GRANT_INVALID_REF)
87 gnttab_end_foreign_access(buf->grefs[i],
88 0, 0UL);
90 kfree(buf->grefs);
91 kfree(buf->directory);
92 kfree(buf);
96 * number of grefs a page can hold with respect to the
97 * struct xendispl_page_directory header
99 #define XEN_DRM_NUM_GREFS_PER_PAGE ((PAGE_SIZE - \
100 offsetof(struct xendispl_page_directory, gref)) / \
101 sizeof(grant_ref_t))
103 static int get_num_pages_dir(struct xen_drm_front_shbuf *buf)
105 /* number of pages the page directory consumes itself */
106 return DIV_ROUND_UP(buf->num_pages, XEN_DRM_NUM_GREFS_PER_PAGE);
109 static void backend_calc_num_grefs(struct xen_drm_front_shbuf *buf)
111 /* only for pages the page directory consumes itself */
112 buf->num_grefs = get_num_pages_dir(buf);
115 static void guest_calc_num_grefs(struct xen_drm_front_shbuf *buf)
118 * number of pages the page directory consumes itself
119 * plus grefs for the buffer pages
121 buf->num_grefs = get_num_pages_dir(buf) + buf->num_pages;
124 #define xen_page_to_vaddr(page) \
125 ((uintptr_t)pfn_to_kaddr(page_to_xen_pfn(page)))
127 static int backend_unmap(struct xen_drm_front_shbuf *buf)
129 struct gnttab_unmap_grant_ref *unmap_ops;
130 int i, ret;
132 if (!buf->pages || !buf->backend_map_handles || !buf->grefs)
133 return 0;
135 unmap_ops = kcalloc(buf->num_pages, sizeof(*unmap_ops),
136 GFP_KERNEL);
137 if (!unmap_ops) {
138 DRM_ERROR("Failed to get memory while unmapping\n");
139 return -ENOMEM;
142 for (i = 0; i < buf->num_pages; i++) {
143 phys_addr_t addr;
145 addr = xen_page_to_vaddr(buf->pages[i]);
146 gnttab_set_unmap_op(&unmap_ops[i], addr, GNTMAP_host_map,
147 buf->backend_map_handles[i]);
150 ret = gnttab_unmap_refs(unmap_ops, NULL, buf->pages,
151 buf->num_pages);
153 for (i = 0; i < buf->num_pages; i++) {
154 if (unlikely(unmap_ops[i].status != GNTST_okay))
155 DRM_ERROR("Failed to unmap page %d: %d\n",
156 i, unmap_ops[i].status);
159 if (ret)
160 DRM_ERROR("Failed to unmap grant references, ret %d", ret);
162 kfree(unmap_ops);
163 kfree(buf->backend_map_handles);
164 buf->backend_map_handles = NULL;
165 return ret;
168 static int backend_map(struct xen_drm_front_shbuf *buf)
170 struct gnttab_map_grant_ref *map_ops = NULL;
171 unsigned char *ptr;
172 int ret, cur_gref, cur_dir_page, cur_page, grefs_left;
174 map_ops = kcalloc(buf->num_pages, sizeof(*map_ops), GFP_KERNEL);
175 if (!map_ops)
176 return -ENOMEM;
178 buf->backend_map_handles = kcalloc(buf->num_pages,
179 sizeof(*buf->backend_map_handles),
180 GFP_KERNEL);
181 if (!buf->backend_map_handles) {
182 kfree(map_ops);
183 return -ENOMEM;
187 * read page directory to get grefs from the backend: for external
188 * buffer we only allocate buf->grefs for the page directory,
189 * so buf->num_grefs has number of pages in the page directory itself
191 ptr = buf->directory;
192 grefs_left = buf->num_pages;
193 cur_page = 0;
194 for (cur_dir_page = 0; cur_dir_page < buf->num_grefs; cur_dir_page++) {
195 struct xendispl_page_directory *page_dir =
196 (struct xendispl_page_directory *)ptr;
197 int to_copy = XEN_DRM_NUM_GREFS_PER_PAGE;
199 if (to_copy > grefs_left)
200 to_copy = grefs_left;
202 for (cur_gref = 0; cur_gref < to_copy; cur_gref++) {
203 phys_addr_t addr;
205 addr = xen_page_to_vaddr(buf->pages[cur_page]);
206 gnttab_set_map_op(&map_ops[cur_page], addr,
207 GNTMAP_host_map,
208 page_dir->gref[cur_gref],
209 buf->xb_dev->otherend_id);
210 cur_page++;
213 grefs_left -= to_copy;
214 ptr += PAGE_SIZE;
216 ret = gnttab_map_refs(map_ops, NULL, buf->pages, buf->num_pages);
218 /* save handles even if error, so we can unmap */
219 for (cur_page = 0; cur_page < buf->num_pages; cur_page++) {
220 buf->backend_map_handles[cur_page] = map_ops[cur_page].handle;
221 if (unlikely(map_ops[cur_page].status != GNTST_okay))
222 DRM_ERROR("Failed to map page %d: %d\n",
223 cur_page, map_ops[cur_page].status);
226 if (ret) {
227 DRM_ERROR("Failed to map grant references, ret %d", ret);
228 backend_unmap(buf);
231 kfree(map_ops);
232 return ret;
235 static void backend_fill_page_dir(struct xen_drm_front_shbuf *buf)
237 struct xendispl_page_directory *page_dir;
238 unsigned char *ptr;
239 int i, num_pages_dir;
241 ptr = buf->directory;
242 num_pages_dir = get_num_pages_dir(buf);
244 /* fill only grefs for the page directory itself */
245 for (i = 0; i < num_pages_dir - 1; i++) {
246 page_dir = (struct xendispl_page_directory *)ptr;
248 page_dir->gref_dir_next_page = buf->grefs[i + 1];
249 ptr += PAGE_SIZE;
251 /* last page must say there is no more pages */
252 page_dir = (struct xendispl_page_directory *)ptr;
253 page_dir->gref_dir_next_page = GRANT_INVALID_REF;
256 static void guest_fill_page_dir(struct xen_drm_front_shbuf *buf)
258 unsigned char *ptr;
259 int cur_gref, grefs_left, to_copy, i, num_pages_dir;
261 ptr = buf->directory;
262 num_pages_dir = get_num_pages_dir(buf);
265 * while copying, skip grefs at start, they are for pages
266 * granted for the page directory itself
268 cur_gref = num_pages_dir;
269 grefs_left = buf->num_pages;
270 for (i = 0; i < num_pages_dir; i++) {
271 struct xendispl_page_directory *page_dir =
272 (struct xendispl_page_directory *)ptr;
274 if (grefs_left <= XEN_DRM_NUM_GREFS_PER_PAGE) {
275 to_copy = grefs_left;
276 page_dir->gref_dir_next_page = GRANT_INVALID_REF;
277 } else {
278 to_copy = XEN_DRM_NUM_GREFS_PER_PAGE;
279 page_dir->gref_dir_next_page = buf->grefs[i + 1];
281 memcpy(&page_dir->gref, &buf->grefs[cur_gref],
282 to_copy * sizeof(grant_ref_t));
283 ptr += PAGE_SIZE;
284 grefs_left -= to_copy;
285 cur_gref += to_copy;
289 static int guest_grant_refs_for_buffer(struct xen_drm_front_shbuf *buf,
290 grant_ref_t *priv_gref_head,
291 int gref_idx)
293 int i, cur_ref, otherend_id;
295 otherend_id = buf->xb_dev->otherend_id;
296 for (i = 0; i < buf->num_pages; i++) {
297 cur_ref = gnttab_claim_grant_reference(priv_gref_head);
298 if (cur_ref < 0)
299 return cur_ref;
301 gnttab_grant_foreign_access_ref(cur_ref, otherend_id,
302 xen_page_to_gfn(buf->pages[i]),
304 buf->grefs[gref_idx++] = cur_ref;
306 return 0;
309 static int grant_references(struct xen_drm_front_shbuf *buf)
311 grant_ref_t priv_gref_head;
312 int ret, i, j, cur_ref;
313 int otherend_id, num_pages_dir;
315 ret = gnttab_alloc_grant_references(buf->num_grefs, &priv_gref_head);
316 if (ret < 0) {
317 DRM_ERROR("Cannot allocate grant references\n");
318 return ret;
321 otherend_id = buf->xb_dev->otherend_id;
322 j = 0;
323 num_pages_dir = get_num_pages_dir(buf);
324 for (i = 0; i < num_pages_dir; i++) {
325 unsigned long frame;
327 cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
328 if (cur_ref < 0)
329 return cur_ref;
331 frame = xen_page_to_gfn(virt_to_page(buf->directory +
332 PAGE_SIZE * i));
333 gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0);
334 buf->grefs[j++] = cur_ref;
337 if (buf->ops->grant_refs_for_buffer) {
338 ret = buf->ops->grant_refs_for_buffer(buf, &priv_gref_head, j);
339 if (ret)
340 return ret;
343 gnttab_free_grant_references(priv_gref_head);
344 return 0;
347 static int alloc_storage(struct xen_drm_front_shbuf *buf)
349 buf->grefs = kcalloc(buf->num_grefs, sizeof(*buf->grefs), GFP_KERNEL);
350 if (!buf->grefs)
351 return -ENOMEM;
353 buf->directory = kcalloc(get_num_pages_dir(buf), PAGE_SIZE, GFP_KERNEL);
354 if (!buf->directory)
355 return -ENOMEM;
357 return 0;
361 * For be allocated buffers we don't need grant_refs_for_buffer as those
362 * grant references are allocated at backend side
364 static const struct xen_drm_front_shbuf_ops backend_ops = {
365 .calc_num_grefs = backend_calc_num_grefs,
366 .fill_page_dir = backend_fill_page_dir,
367 .map = backend_map,
368 .unmap = backend_unmap
371 /* For locally granted references we do not need to map/unmap the references */
372 static const struct xen_drm_front_shbuf_ops local_ops = {
373 .calc_num_grefs = guest_calc_num_grefs,
374 .fill_page_dir = guest_fill_page_dir,
375 .grant_refs_for_buffer = guest_grant_refs_for_buffer,
378 struct xen_drm_front_shbuf *
379 xen_drm_front_shbuf_alloc(struct xen_drm_front_shbuf_cfg *cfg)
381 struct xen_drm_front_shbuf *buf;
382 int ret;
384 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
385 if (!buf)
386 return ERR_PTR(-ENOMEM);
388 if (cfg->be_alloc)
389 buf->ops = &backend_ops;
390 else
391 buf->ops = &local_ops;
393 buf->xb_dev = cfg->xb_dev;
394 buf->num_pages = DIV_ROUND_UP(cfg->size, PAGE_SIZE);
395 buf->pages = cfg->pages;
397 buf->ops->calc_num_grefs(buf);
399 ret = alloc_storage(buf);
400 if (ret)
401 goto fail;
403 ret = grant_references(buf);
404 if (ret)
405 goto fail;
407 buf->ops->fill_page_dir(buf);
409 return buf;
411 fail:
412 xen_drm_front_shbuf_free(buf);
413 return ERR_PTR(ret);