Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-microblaze.git] / drivers / cdx / cdx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CDX bus driver.
4  *
5  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
6  */
7
8 /*
9  * Architecture Overview
10  * =====================
11  * CDX is a Hardware Architecture designed for AMD FPGA devices. It
12  * consists of sophisticated mechanism for interaction between FPGA,
13  * Firmware and the APUs (Application CPUs).
14  *
15  * Firmware resides on RPU (Realtime CPUs) which interacts with
16  * the FPGA program manager and the APUs. The RPU provides memory-mapped
17  * interface (RPU if) which is used to communicate with APUs.
18  *
19  * The diagram below shows an overview of the CDX architecture:
20  *
21  *          +--------------------------------------+
22  *          |    Application CPUs (APU)            |
23  *          |                                      |
24  *          |                    CDX device drivers|
25  *          |     Linux OS                |        |
26  *          |                        CDX bus       |
27  *          |                             |        |
28  *          |                     CDX controller   |
29  *          |                             |        |
30  *          +-----------------------------|--------+
31  *                                        | (discover, config,
32  *                                        |  reset, rescan)
33  *                                        |
34  *          +------------------------| RPU if |----+
35  *          |                             |        |
36  *          |                             V        |
37  *          |          Realtime CPUs (RPU)         |
38  *          |                                      |
39  *          +--------------------------------------+
40  *                                |
41  *          +---------------------|----------------+
42  *          |  FPGA               |                |
43  *          |      +-----------------------+       |
44  *          |      |           |           |       |
45  *          | +-------+    +-------+   +-------+   |
46  *          | | dev 1 |    | dev 2 |   | dev 3 |   |
47  *          | +-------+    +-------+   +-------+   |
48  *          +--------------------------------------+
49  *
50  * The RPU firmware extracts the device information from the loaded FPGA
51  * image and implements a mechanism that allows the APU drivers to
52  * enumerate such devices (device personality and resource details) via
53  * a dedicated communication channel. RPU mediates operations such as
54  * discover, reset and rescan of the FPGA devices for the APU. This is
55  * done using memory mapped interface provided by the RPU to APU.
56  */
57
58 #include <linux/init.h>
59 #include <linux/kernel.h>
60 #include <linux/of_device.h>
61 #include <linux/slab.h>
62 #include <linux/mm.h>
63 #include <linux/xarray.h>
64 #include <linux/cdx/cdx_bus.h>
65 #include "cdx.h"
66
67 /* Default DMA mask for devices on a CDX bus */
68 #define CDX_DEFAULT_DMA_MASK    (~0ULL)
69 #define MAX_CDX_CONTROLLERS 16
70
71 /* CDX controllers registered with the CDX bus */
72 static DEFINE_XARRAY_ALLOC(cdx_controllers);
73
74 /**
75  * cdx_dev_reset - Reset a CDX device
76  * @dev: CDX device
77  *
78  * Return: -errno on failure, 0 on success.
79  */
80 int cdx_dev_reset(struct device *dev)
81 {
82         struct cdx_device *cdx_dev = to_cdx_device(dev);
83         struct cdx_controller *cdx = cdx_dev->cdx;
84         struct cdx_device_config dev_config = {0};
85         struct cdx_driver *cdx_drv;
86         int ret;
87
88         cdx_drv = to_cdx_driver(dev->driver);
89         /* Notify driver that device is being reset */
90         if (cdx_drv && cdx_drv->reset_prepare)
91                 cdx_drv->reset_prepare(cdx_dev);
92
93         dev_config.type = CDX_DEV_RESET_CONF;
94         ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
95                                       cdx_dev->dev_num, &dev_config);
96         if (ret)
97                 dev_err(dev, "cdx device reset failed\n");
98
99         /* Notify driver that device reset is complete */
100         if (cdx_drv && cdx_drv->reset_done)
101                 cdx_drv->reset_done(cdx_dev);
102
103         return ret;
104 }
105 EXPORT_SYMBOL_GPL(cdx_dev_reset);
106
107 /**
108  * cdx_unregister_device - Unregister a CDX device
109  * @dev: CDX device
110  * @data: This is always passed as NULL, and is not used in this API,
111  *        but is required here as the bus_for_each_dev() API expects
112  *        the passed function (cdx_unregister_device) to have this
113  *        as an argument.
114  *
115  * Return: 0 on success.
116  */
117 static int cdx_unregister_device(struct device *dev,
118                                  void *data)
119 {
120         struct cdx_device *cdx_dev = to_cdx_device(dev);
121
122         kfree(cdx_dev->driver_override);
123         cdx_dev->driver_override = NULL;
124         /*
125          * Do not free cdx_dev here as it would be freed in
126          * cdx_device_release() called from within put_device().
127          */
128         device_del(&cdx_dev->dev);
129         put_device(&cdx_dev->dev);
130
131         return 0;
132 }
133
134 static void cdx_unregister_devices(struct bus_type *bus)
135 {
136         /* Reset all the devices attached to cdx bus */
137         bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
138 }
139
140 /**
141  * cdx_match_one_device - Tell if a CDX device structure has a matching
142  *                        CDX device id structure
143  * @id: single CDX device id structure to match
144  * @dev: the CDX device structure to match against
145  *
146  * Return: matching cdx_device_id structure or NULL if there is no match.
147  */
148 static inline const struct cdx_device_id *
149 cdx_match_one_device(const struct cdx_device_id *id,
150                      const struct cdx_device *dev)
151 {
152         /* Use vendor ID and device ID for matching */
153         if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
154             (id->device == CDX_ANY_ID || id->device == dev->device))
155                 return id;
156         return NULL;
157 }
158
159 /**
160  * cdx_match_id - See if a CDX device matches a given cdx_id table
161  * @ids: array of CDX device ID structures to search in
162  * @dev: the CDX device structure to match against.
163  *
164  * Used by a driver to check whether a CDX device is in its list of
165  * supported devices. Returns the matching cdx_device_id structure or
166  * NULL if there is no match.
167  *
168  * Return: matching cdx_device_id structure or NULL if there is no match.
169  */
170 static inline const struct cdx_device_id *
171 cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev)
172 {
173         if (ids) {
174                 while (ids->vendor || ids->device) {
175                         if (cdx_match_one_device(ids, dev))
176                                 return ids;
177                         ids++;
178                 }
179         }
180         return NULL;
181 }
182
183 /**
184  * cdx_bus_match - device to driver matching callback
185  * @dev: the cdx device to match against
186  * @drv: the device driver to search for matching cdx device
187  * structures
188  *
189  * Return: true on success, false otherwise.
190  */
191 static int cdx_bus_match(struct device *dev, struct device_driver *drv)
192 {
193         struct cdx_device *cdx_dev = to_cdx_device(dev);
194         struct cdx_driver *cdx_drv = to_cdx_driver(drv);
195         const struct cdx_device_id *found_id = NULL;
196         const struct cdx_device_id *ids;
197
198         ids = cdx_drv->match_id_table;
199
200         /* When driver_override is set, only bind to the matching driver */
201         if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
202                 return false;
203
204         found_id = cdx_match_id(ids, cdx_dev);
205         if (!found_id)
206                 return false;
207
208         do {
209                 /*
210                  * In case override_only was set, enforce driver_override
211                  * matching.
212                  */
213                 if (!found_id->override_only)
214                         return true;
215                 if (cdx_dev->driver_override)
216                         return true;
217
218                 ids = found_id + 1;
219                 found_id = cdx_match_id(ids, cdx_dev);
220         } while (found_id);
221
222         return false;
223 }
224
225 static int cdx_probe(struct device *dev)
226 {
227         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
228         struct cdx_device *cdx_dev = to_cdx_device(dev);
229         int error;
230
231         error = cdx_drv->probe(cdx_dev);
232         if (error) {
233                 dev_err_probe(dev, error, "%s failed\n", __func__);
234                 return error;
235         }
236
237         return 0;
238 }
239
240 static void cdx_remove(struct device *dev)
241 {
242         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
243         struct cdx_device *cdx_dev = to_cdx_device(dev);
244
245         if (cdx_drv && cdx_drv->remove)
246                 cdx_drv->remove(cdx_dev);
247 }
248
249 static void cdx_shutdown(struct device *dev)
250 {
251         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
252         struct cdx_device *cdx_dev = to_cdx_device(dev);
253
254         if (cdx_drv && cdx_drv->shutdown)
255                 cdx_drv->shutdown(cdx_dev);
256 }
257
258 static int cdx_dma_configure(struct device *dev)
259 {
260         struct cdx_device *cdx_dev = to_cdx_device(dev);
261         u32 input_id = cdx_dev->req_id;
262         int ret;
263
264         ret = of_dma_configure_id(dev, dev->parent->of_node, 0, &input_id);
265         if (ret && ret != -EPROBE_DEFER) {
266                 dev_err(dev, "of_dma_configure_id() failed\n");
267                 return ret;
268         }
269
270         return 0;
271 }
272
273 /* show configuration fields */
274 #define cdx_config_attr(field, format_string)   \
275 static ssize_t  \
276 field##_show(struct device *dev, struct device_attribute *attr, char *buf)      \
277 {       \
278         struct cdx_device *cdx_dev = to_cdx_device(dev);        \
279         return sysfs_emit(buf, format_string, cdx_dev->field);  \
280 }       \
281 static DEVICE_ATTR_RO(field)
282
283 cdx_config_attr(vendor, "0x%04x\n");
284 cdx_config_attr(device, "0x%04x\n");
285
286 static ssize_t remove_store(struct device *dev,
287                             struct device_attribute *attr,
288                             const char *buf, size_t count)
289 {
290         bool val;
291
292         if (kstrtobool(buf, &val) < 0)
293                 return -EINVAL;
294
295         if (!val)
296                 return -EINVAL;
297
298         if (device_remove_file_self(dev, attr)) {
299                 int ret;
300
301                 ret = cdx_unregister_device(dev, NULL);
302                 if (ret)
303                         return ret;
304         }
305
306         return count;
307 }
308 static DEVICE_ATTR_WO(remove);
309
310 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
311                            const char *buf, size_t count)
312 {
313         bool val;
314         int ret;
315
316         if (kstrtobool(buf, &val) < 0)
317                 return -EINVAL;
318
319         if (!val)
320                 return -EINVAL;
321
322         ret = cdx_dev_reset(dev);
323         if (ret)
324                 return ret;
325
326         return count;
327 }
328 static DEVICE_ATTR_WO(reset);
329
330 static ssize_t driver_override_store(struct device *dev,
331                                      struct device_attribute *attr,
332                                      const char *buf, size_t count)
333 {
334         struct cdx_device *cdx_dev = to_cdx_device(dev);
335         int ret;
336
337         if (WARN_ON(dev->bus != &cdx_bus_type))
338                 return -EINVAL;
339
340         ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
341         if (ret)
342                 return ret;
343
344         return count;
345 }
346
347 static ssize_t driver_override_show(struct device *dev,
348                                     struct device_attribute *attr, char *buf)
349 {
350         struct cdx_device *cdx_dev = to_cdx_device(dev);
351
352         return sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
353 }
354 static DEVICE_ATTR_RW(driver_override);
355
356 static struct attribute *cdx_dev_attrs[] = {
357         &dev_attr_remove.attr,
358         &dev_attr_reset.attr,
359         &dev_attr_vendor.attr,
360         &dev_attr_device.attr,
361         &dev_attr_driver_override.attr,
362         NULL,
363 };
364 ATTRIBUTE_GROUPS(cdx_dev);
365
366 static ssize_t rescan_store(const struct bus_type *bus,
367                             const char *buf, size_t count)
368 {
369         struct cdx_controller *cdx;
370         unsigned long index;
371         bool val;
372
373         if (kstrtobool(buf, &val) < 0)
374                 return -EINVAL;
375
376         if (!val)
377                 return -EINVAL;
378
379         /* Unregister all the devices on the bus */
380         cdx_unregister_devices(&cdx_bus_type);
381
382         /* Rescan all the devices */
383         xa_for_each(&cdx_controllers, index, cdx) {
384                 int ret;
385
386                 ret = cdx->ops->scan(cdx);
387                 if (ret)
388                         dev_err(cdx->dev, "cdx bus scanning failed\n");
389         }
390
391         return count;
392 }
393 static BUS_ATTR_WO(rescan);
394
395 static struct attribute *cdx_bus_attrs[] = {
396         &bus_attr_rescan.attr,
397         NULL,
398 };
399 ATTRIBUTE_GROUPS(cdx_bus);
400
401 struct bus_type cdx_bus_type = {
402         .name           = "cdx",
403         .match          = cdx_bus_match,
404         .probe          = cdx_probe,
405         .remove         = cdx_remove,
406         .shutdown       = cdx_shutdown,
407         .dma_configure  = cdx_dma_configure,
408         .bus_groups     = cdx_bus_groups,
409         .dev_groups     = cdx_dev_groups,
410 };
411 EXPORT_SYMBOL_GPL(cdx_bus_type);
412
413 int __cdx_driver_register(struct cdx_driver *cdx_driver,
414                           struct module *owner)
415 {
416         int error;
417
418         cdx_driver->driver.owner = owner;
419         cdx_driver->driver.bus = &cdx_bus_type;
420
421         error = driver_register(&cdx_driver->driver);
422         if (error) {
423                 pr_err("driver_register() failed for %s: %d\n",
424                        cdx_driver->driver.name, error);
425                 return error;
426         }
427
428         return 0;
429 }
430 EXPORT_SYMBOL_GPL(__cdx_driver_register);
431
432 void cdx_driver_unregister(struct cdx_driver *cdx_driver)
433 {
434         driver_unregister(&cdx_driver->driver);
435 }
436 EXPORT_SYMBOL_GPL(cdx_driver_unregister);
437
438 static void cdx_device_release(struct device *dev)
439 {
440         struct cdx_device *cdx_dev = to_cdx_device(dev);
441
442         kfree(cdx_dev);
443 }
444
445 int cdx_device_add(struct cdx_dev_params *dev_params)
446 {
447         struct cdx_controller *cdx = dev_params->cdx;
448         struct device *parent = cdx->dev;
449         struct cdx_device *cdx_dev;
450         int ret;
451
452         cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
453         if (!cdx_dev)
454                 return -ENOMEM;
455
456         /* Populate resource */
457         memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
458                 dev_params->res_count);
459         cdx_dev->res_count = dev_params->res_count;
460
461         /* Populate CDX dev params */
462         cdx_dev->req_id = dev_params->req_id;
463         cdx_dev->vendor = dev_params->vendor;
464         cdx_dev->device = dev_params->device;
465         cdx_dev->bus_num = dev_params->bus_num;
466         cdx_dev->dev_num = dev_params->dev_num;
467         cdx_dev->cdx = dev_params->cdx;
468         cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
469
470         /* Initialize generic device */
471         device_initialize(&cdx_dev->dev);
472         cdx_dev->dev.parent = parent;
473         cdx_dev->dev.bus = &cdx_bus_type;
474         cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
475         cdx_dev->dev.release = cdx_device_release;
476
477         /* Set Name */
478         dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
479                      ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
480                      cdx_dev->dev_num);
481
482         ret = device_add(&cdx_dev->dev);
483         if (ret) {
484                 dev_err(&cdx_dev->dev,
485                         "cdx device add failed: %d", ret);
486                 goto fail;
487         }
488
489         return 0;
490 fail:
491         /*
492          * Do not free cdx_dev here as it would be freed in
493          * cdx_device_release() called from put_device().
494          */
495         put_device(&cdx_dev->dev);
496
497         return ret;
498 }
499 EXPORT_SYMBOL_GPL(cdx_device_add);
500
501 int cdx_register_controller(struct cdx_controller *cdx)
502 {
503         int ret;
504
505         ret = xa_alloc(&cdx_controllers, &cdx->id, cdx,
506                        XA_LIMIT(0, MAX_CDX_CONTROLLERS - 1), GFP_KERNEL);
507         if (ret) {
508                 dev_err(cdx->dev,
509                         "No free index available. Maximum controllers already registered\n");
510                 cdx->id = (u8)MAX_CDX_CONTROLLERS;
511                 return ret;
512         }
513
514         /* Scan all the devices */
515         cdx->ops->scan(cdx);
516
517         return 0;
518 }
519 EXPORT_SYMBOL_GPL(cdx_register_controller);
520
521 void cdx_unregister_controller(struct cdx_controller *cdx)
522 {
523         if (cdx->id >= MAX_CDX_CONTROLLERS)
524                 return;
525
526         device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
527         xa_erase(&cdx_controllers, cdx->id);
528 }
529 EXPORT_SYMBOL_GPL(cdx_unregister_controller);
530
531 static int __init cdx_bus_init(void)
532 {
533         return bus_register(&cdx_bus_type);
534 }
535 postcore_initcall(cdx_bus_init);