18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR MIT */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * Xen frontend/backend page directory based shared buffer
58c2ecf20Sopenharmony_ci * helper module.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2018 EPAM Systems Inc.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef __XEN_FRONT_PGDIR_SHBUF_H_
138c2ecf20Sopenharmony_ci#define __XEN_FRONT_PGDIR_SHBUF_H_
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <xen/grant_table.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct xen_front_pgdir_shbuf_ops;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct xen_front_pgdir_shbuf {
228c2ecf20Sopenharmony_ci	/*
238c2ecf20Sopenharmony_ci	 * Number of references granted for the backend use:
248c2ecf20Sopenharmony_ci	 *
258c2ecf20Sopenharmony_ci	 *  - for frontend allocated/imported buffers this holds the number
268c2ecf20Sopenharmony_ci	 *    of grant references for the page directory and the pages
278c2ecf20Sopenharmony_ci	 *    of the buffer
288c2ecf20Sopenharmony_ci	 *
298c2ecf20Sopenharmony_ci	 *  - for the buffer provided by the backend this only holds the number
308c2ecf20Sopenharmony_ci	 *    of grant references for the page directory itself as grant
318c2ecf20Sopenharmony_ci	 *    references for the buffer will be provided by the backend.
328c2ecf20Sopenharmony_ci	 */
338c2ecf20Sopenharmony_ci	int num_grefs;
348c2ecf20Sopenharmony_ci	grant_ref_t *grefs;
358c2ecf20Sopenharmony_ci	/* Page directory backing storage. */
368c2ecf20Sopenharmony_ci	u8 *directory;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/*
398c2ecf20Sopenharmony_ci	 * Number of pages for the shared buffer itself (excluding the page
408c2ecf20Sopenharmony_ci	 * directory).
418c2ecf20Sopenharmony_ci	 */
428c2ecf20Sopenharmony_ci	int num_pages;
438c2ecf20Sopenharmony_ci	/*
448c2ecf20Sopenharmony_ci	 * Backing storage of the shared buffer: these are the pages being
458c2ecf20Sopenharmony_ci	 * shared.
468c2ecf20Sopenharmony_ci	 */
478c2ecf20Sopenharmony_ci	struct page **pages;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	struct xenbus_device *xb_dev;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	/* These are the ops used internally depending on be_alloc mode. */
528c2ecf20Sopenharmony_ci	const struct xen_front_pgdir_shbuf_ops *ops;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* Xen map handles for the buffer allocated by the backend. */
558c2ecf20Sopenharmony_ci	grant_handle_t *backend_map_handles;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct xen_front_pgdir_shbuf_cfg {
598c2ecf20Sopenharmony_ci	struct xenbus_device *xb_dev;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	/* Number of pages of the buffer backing storage. */
628c2ecf20Sopenharmony_ci	int num_pages;
638c2ecf20Sopenharmony_ci	/* Pages of the buffer to be shared. */
648c2ecf20Sopenharmony_ci	struct page **pages;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/*
678c2ecf20Sopenharmony_ci	 * This is allocated outside because there are use-cases when
688c2ecf20Sopenharmony_ci	 * the buffer structure is allocated as a part of a bigger one.
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	struct xen_front_pgdir_shbuf *pgdir;
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * Mode of grant reference sharing: if set then backend will share
738c2ecf20Sopenharmony_ci	 * grant references to the buffer with the frontend.
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci	int be_alloc;
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciint xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg *cfg);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cigrant_ref_t
818c2ecf20Sopenharmony_cixen_front_pgdir_shbuf_get_dir_start(struct xen_front_pgdir_shbuf *buf);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciint xen_front_pgdir_shbuf_map(struct xen_front_pgdir_shbuf *buf);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciint xen_front_pgdir_shbuf_unmap(struct xen_front_pgdir_shbuf *buf);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_civoid xen_front_pgdir_shbuf_free(struct xen_front_pgdir_shbuf *buf);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#endif /* __XEN_FRONT_PGDIR_SHBUF_H_ */
90