Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * VFIO based AP device driver
  4 *
  5 * Copyright IBM Corp. 2018
  6 *
  7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  8 *	      Pierre Morel <pmorel@linux.ibm.com>
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/mod_devicetable.h>
 13#include <linux/slab.h>
 14#include <linux/string.h>
 15#include <asm/facility.h>
 16#include "vfio_ap_private.h"
 17#include "vfio_ap_debug.h"
 18
 19#define VFIO_AP_ROOT_NAME "vfio_ap"
 20#define VFIO_AP_DEV_NAME "matrix"
 21
 22MODULE_AUTHOR("IBM Corporation");
 23MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
 24MODULE_LICENSE("GPL v2");
 25
 
 
 26struct ap_matrix_dev *matrix_dev;
 27debug_info_t *vfio_ap_dbf_info;
 28
 29/* Only type 10 adapters (CEX4 and later) are supported
 30 * by the AP matrix device driver
 31 */
 32static struct ap_device_id ap_queue_ids[] = {
 33	{ .dev_type = AP_DEVICE_TYPE_CEX4,
 34	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 35	{ .dev_type = AP_DEVICE_TYPE_CEX5,
 36	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 37	{ .dev_type = AP_DEVICE_TYPE_CEX6,
 38	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 39	{ .dev_type = AP_DEVICE_TYPE_CEX7,
 40	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 41	{ .dev_type = AP_DEVICE_TYPE_CEX8,
 42	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 43	{ /* end of sibling */ },
 44};
 45
 46static struct ap_driver vfio_ap_drv = {
 47	.probe = vfio_ap_mdev_probe_queue,
 48	.remove = vfio_ap_mdev_remove_queue,
 49	.in_use = vfio_ap_mdev_resource_in_use,
 50	.on_config_changed = vfio_ap_on_cfg_changed,
 51	.on_scan_complete = vfio_ap_on_scan_complete,
 52	.ids = ap_queue_ids,
 53};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54
 55static void vfio_ap_matrix_dev_release(struct device *dev)
 56{
 57	struct ap_matrix_dev *matrix_dev;
 58
 59	matrix_dev = container_of(dev, struct ap_matrix_dev, device);
 60	kfree(matrix_dev);
 61}
 62
 
 
 
 
 
 63static struct bus_type matrix_bus = {
 64	.name = "matrix",
 
 65};
 66
 67static struct device_driver matrix_driver = {
 68	.name = "vfio_ap",
 69	.bus = &matrix_bus,
 70	.suppress_bind_attrs = true,
 71};
 72
 73static int vfio_ap_matrix_dev_create(void)
 74{
 75	int ret;
 76	struct device *root_device;
 77
 78	root_device = root_device_register(VFIO_AP_ROOT_NAME);
 79	if (IS_ERR(root_device))
 80		return PTR_ERR(root_device);
 81
 82	ret = bus_register(&matrix_bus);
 83	if (ret)
 84		goto bus_register_err;
 85
 86	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
 87	if (!matrix_dev) {
 88		ret = -ENOMEM;
 89		goto matrix_alloc_err;
 90	}
 91
 92	/* Fill in config info via PQAP(QCI), if available */
 93	if (test_facility(12)) {
 94		ret = ap_qci(&matrix_dev->info);
 95		if (ret)
 96			goto matrix_alloc_err;
 97	}
 98
 99	mutex_init(&matrix_dev->mdevs_lock);
100	INIT_LIST_HEAD(&matrix_dev->mdev_list);
101	mutex_init(&matrix_dev->guests_lock);
102
103	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
104	matrix_dev->device.parent = root_device;
105	matrix_dev->device.bus = &matrix_bus;
106	matrix_dev->device.release = vfio_ap_matrix_dev_release;
107	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
108
109	ret = device_register(&matrix_dev->device);
110	if (ret)
111		goto matrix_reg_err;
112
113	ret = driver_register(&matrix_driver);
114	if (ret)
115		goto matrix_drv_err;
116
117	return 0;
118
119matrix_drv_err:
120	device_del(&matrix_dev->device);
121matrix_reg_err:
122	put_device(&matrix_dev->device);
123matrix_alloc_err:
124	bus_unregister(&matrix_bus);
125bus_register_err:
126	root_device_unregister(root_device);
127	return ret;
128}
129
130static void vfio_ap_matrix_dev_destroy(void)
131{
132	struct device *root_device = matrix_dev->device.parent;
133
134	driver_unregister(&matrix_driver);
135	device_unregister(&matrix_dev->device);
136	bus_unregister(&matrix_bus);
137	root_device_unregister(root_device);
138}
139
140static int __init vfio_ap_dbf_info_init(void)
141{
142	vfio_ap_dbf_info = debug_register("vfio_ap", 1, 1,
143					  DBF_MAX_SPRINTF_ARGS * sizeof(long));
144
145	if (!vfio_ap_dbf_info)
146		return -ENOENT;
147
148	debug_register_view(vfio_ap_dbf_info, &debug_sprintf_view);
149	debug_set_level(vfio_ap_dbf_info, DBF_WARN);
150
151	return 0;
152}
153
154static int __init vfio_ap_init(void)
155{
156	int ret;
157
158	ret = vfio_ap_dbf_info_init();
159	if (ret)
160		return ret;
161
162	/* If there are no AP instructions, there is nothing to pass through. */
163	if (!ap_instructions_available())
164		return -ENODEV;
165
166	ret = vfio_ap_matrix_dev_create();
167	if (ret)
168		return ret;
169
 
 
 
 
 
170	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
171	if (ret) {
172		vfio_ap_matrix_dev_destroy();
173		return ret;
174	}
175
176	ret = vfio_ap_mdev_register();
177	if (ret) {
178		ap_driver_unregister(&vfio_ap_drv);
179		vfio_ap_matrix_dev_destroy();
180
181		return ret;
182	}
183
184	return 0;
185}
186
187static void __exit vfio_ap_exit(void)
188{
189	vfio_ap_mdev_unregister();
190	ap_driver_unregister(&vfio_ap_drv);
191	vfio_ap_matrix_dev_destroy();
192	debug_unregister(vfio_ap_dbf_info);
193}
194
195module_init(vfio_ap_init);
196module_exit(vfio_ap_exit);
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * VFIO based AP device driver
  4 *
  5 * Copyright IBM Corp. 2018
  6 *
  7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  8 *	      Pierre Morel <pmorel@linux.ibm.com>
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/mod_devicetable.h>
 13#include <linux/slab.h>
 14#include <linux/string.h>
 15#include <asm/facility.h>
 16#include "vfio_ap_private.h"
 
 17
 18#define VFIO_AP_ROOT_NAME "vfio_ap"
 19#define VFIO_AP_DEV_NAME "matrix"
 20
 21MODULE_AUTHOR("IBM Corporation");
 22MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
 23MODULE_LICENSE("GPL v2");
 24
 25static struct ap_driver vfio_ap_drv;
 26
 27struct ap_matrix_dev *matrix_dev;
 
 28
 29/* Only type 10 adapters (CEX4 and later) are supported
 30 * by the AP matrix device driver
 31 */
 32static struct ap_device_id ap_queue_ids[] = {
 33	{ .dev_type = AP_DEVICE_TYPE_CEX4,
 34	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 35	{ .dev_type = AP_DEVICE_TYPE_CEX5,
 36	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 37	{ .dev_type = AP_DEVICE_TYPE_CEX6,
 38	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 39	{ .dev_type = AP_DEVICE_TYPE_CEX7,
 40	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
 
 
 41	{ /* end of sibling */ },
 42};
 43
 44MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
 45
 46/**
 47 * vfio_ap_queue_dev_probe:
 48 *
 49 * Allocate a vfio_ap_queue structure and associate it
 50 * with the device as driver_data.
 51 */
 52static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
 53{
 54	struct vfio_ap_queue *q;
 55
 56	q = kzalloc(sizeof(*q), GFP_KERNEL);
 57	if (!q)
 58		return -ENOMEM;
 59	dev_set_drvdata(&apdev->device, q);
 60	q->apqn = to_ap_queue(&apdev->device)->qid;
 61	q->saved_isc = VFIO_AP_ISC_INVALID;
 62	return 0;
 63}
 64
 65/**
 66 * vfio_ap_queue_dev_remove:
 67 *
 68 * Takes the matrix lock to avoid actions on this device while removing
 69 * Free the associated vfio_ap_queue structure
 70 */
 71static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
 72{
 73	struct vfio_ap_queue *q;
 74	int apid, apqi;
 75
 76	mutex_lock(&matrix_dev->lock);
 77	q = dev_get_drvdata(&apdev->device);
 78	dev_set_drvdata(&apdev->device, NULL);
 79	apid = AP_QID_CARD(q->apqn);
 80	apqi = AP_QID_QUEUE(q->apqn);
 81	vfio_ap_mdev_reset_queue(apid, apqi, 1);
 82	vfio_ap_irq_disable(q);
 83	kfree(q);
 84	mutex_unlock(&matrix_dev->lock);
 85}
 86
 87static void vfio_ap_matrix_dev_release(struct device *dev)
 88{
 89	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
 90
 
 91	kfree(matrix_dev);
 92}
 93
 94static int matrix_bus_match(struct device *dev, struct device_driver *drv)
 95{
 96	return 1;
 97}
 98
 99static struct bus_type matrix_bus = {
100	.name = "matrix",
101	.match = &matrix_bus_match,
102};
103
104static struct device_driver matrix_driver = {
105	.name = "vfio_ap",
106	.bus = &matrix_bus,
107	.suppress_bind_attrs = true,
108};
109
110static int vfio_ap_matrix_dev_create(void)
111{
112	int ret;
113	struct device *root_device;
114
115	root_device = root_device_register(VFIO_AP_ROOT_NAME);
116	if (IS_ERR(root_device))
117		return PTR_ERR(root_device);
118
119	ret = bus_register(&matrix_bus);
120	if (ret)
121		goto bus_register_err;
122
123	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
124	if (!matrix_dev) {
125		ret = -ENOMEM;
126		goto matrix_alloc_err;
127	}
128
129	/* Fill in config info via PQAP(QCI), if available */
130	if (test_facility(12)) {
131		ret = ap_qci(&matrix_dev->info);
132		if (ret)
133			goto matrix_alloc_err;
134	}
135
136	mutex_init(&matrix_dev->lock);
137	INIT_LIST_HEAD(&matrix_dev->mdev_list);
 
138
139	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
140	matrix_dev->device.parent = root_device;
141	matrix_dev->device.bus = &matrix_bus;
142	matrix_dev->device.release = vfio_ap_matrix_dev_release;
143	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
144
145	ret = device_register(&matrix_dev->device);
146	if (ret)
147		goto matrix_reg_err;
148
149	ret = driver_register(&matrix_driver);
150	if (ret)
151		goto matrix_drv_err;
152
153	return 0;
154
155matrix_drv_err:
156	device_unregister(&matrix_dev->device);
157matrix_reg_err:
158	put_device(&matrix_dev->device);
159matrix_alloc_err:
160	bus_unregister(&matrix_bus);
161bus_register_err:
162	root_device_unregister(root_device);
163	return ret;
164}
165
166static void vfio_ap_matrix_dev_destroy(void)
167{
168	struct device *root_device = matrix_dev->device.parent;
169
170	driver_unregister(&matrix_driver);
171	device_unregister(&matrix_dev->device);
172	bus_unregister(&matrix_bus);
173	root_device_unregister(root_device);
174}
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176static int __init vfio_ap_init(void)
177{
178	int ret;
179
 
 
 
 
180	/* If there are no AP instructions, there is nothing to pass through. */
181	if (!ap_instructions_available())
182		return -ENODEV;
183
184	ret = vfio_ap_matrix_dev_create();
185	if (ret)
186		return ret;
187
188	memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
189	vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
190	vfio_ap_drv.remove = vfio_ap_queue_dev_remove;
191	vfio_ap_drv.ids = ap_queue_ids;
192
193	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
194	if (ret) {
195		vfio_ap_matrix_dev_destroy();
196		return ret;
197	}
198
199	ret = vfio_ap_mdev_register();
200	if (ret) {
201		ap_driver_unregister(&vfio_ap_drv);
202		vfio_ap_matrix_dev_destroy();
203
204		return ret;
205	}
206
207	return 0;
208}
209
210static void __exit vfio_ap_exit(void)
211{
212	vfio_ap_mdev_unregister();
213	ap_driver_unregister(&vfio_ap_drv);
214	vfio_ap_matrix_dev_destroy();
 
215}
216
217module_init(vfio_ap_init);
218module_exit(vfio_ap_exit);