Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 // Copyright (c) 2018-2021 Intel Corporation
0003 
0004 #include <linux/bug.h>
0005 #include <linux/device.h>
0006 #include <linux/export.h>
0007 #include <linux/idr.h>
0008 #include <linux/module.h>
0009 #include <linux/of.h>
0010 #include <linux/peci.h>
0011 #include <linux/pm_runtime.h>
0012 #include <linux/property.h>
0013 #include <linux/slab.h>
0014 
0015 #include "internal.h"
0016 
0017 static DEFINE_IDA(peci_controller_ida);
0018 
0019 static void peci_controller_dev_release(struct device *dev)
0020 {
0021     struct peci_controller *controller = to_peci_controller(dev);
0022 
0023     mutex_destroy(&controller->bus_lock);
0024     ida_free(&peci_controller_ida, controller->id);
0025     kfree(controller);
0026 }
0027 
0028 struct device_type peci_controller_type = {
0029     .release    = peci_controller_dev_release,
0030 };
0031 
0032 int peci_controller_scan_devices(struct peci_controller *controller)
0033 {
0034     int ret;
0035     u8 addr;
0036 
0037     for (addr = PECI_BASE_ADDR; addr < PECI_BASE_ADDR + PECI_DEVICE_NUM_MAX; addr++) {
0038         ret = peci_device_create(controller, addr);
0039         if (ret)
0040             return ret;
0041     }
0042 
0043     return 0;
0044 }
0045 
0046 static struct peci_controller *peci_controller_alloc(struct device *dev,
0047                              struct peci_controller_ops *ops)
0048 {
0049     struct peci_controller *controller;
0050     int ret;
0051 
0052     if (!ops->xfer)
0053         return ERR_PTR(-EINVAL);
0054 
0055     controller = kzalloc(sizeof(*controller), GFP_KERNEL);
0056     if (!controller)
0057         return ERR_PTR(-ENOMEM);
0058 
0059     ret = ida_alloc_max(&peci_controller_ida, U8_MAX, GFP_KERNEL);
0060     if (ret < 0)
0061         goto err;
0062     controller->id = ret;
0063 
0064     controller->ops = ops;
0065 
0066     controller->dev.parent = dev;
0067     controller->dev.bus = &peci_bus_type;
0068     controller->dev.type = &peci_controller_type;
0069 
0070     device_initialize(&controller->dev);
0071 
0072     mutex_init(&controller->bus_lock);
0073 
0074     return controller;
0075 
0076 err:
0077     kfree(controller);
0078     return ERR_PTR(ret);
0079 }
0080 
0081 static int unregister_child(struct device *dev, void *dummy)
0082 {
0083     peci_device_destroy(to_peci_device(dev));
0084 
0085     return 0;
0086 }
0087 
0088 static void unregister_controller(void *_controller)
0089 {
0090     struct peci_controller *controller = _controller;
0091 
0092     /*
0093      * Detach any active PECI devices. This can't fail, thus we do not
0094      * check the returned value.
0095      */
0096     device_for_each_child_reverse(&controller->dev, NULL, unregister_child);
0097 
0098     device_unregister(&controller->dev);
0099 
0100     fwnode_handle_put(controller->dev.fwnode);
0101 
0102     pm_runtime_disable(&controller->dev);
0103 }
0104 
0105 /**
0106  * devm_peci_controller_add() - add PECI controller
0107  * @dev: device for devm operations
0108  * @ops: pointer to controller specific methods
0109  *
0110  * In final stage of its probe(), peci_controller driver calls
0111  * devm_peci_controller_add() to register itself with the PECI bus.
0112  *
0113  * Return: Pointer to the newly allocated controller or ERR_PTR() in case of failure.
0114  */
0115 struct peci_controller *devm_peci_controller_add(struct device *dev,
0116                          struct peci_controller_ops *ops)
0117 {
0118     struct peci_controller *controller;
0119     int ret;
0120 
0121     controller = peci_controller_alloc(dev, ops);
0122     if (IS_ERR(controller))
0123         return controller;
0124 
0125     ret = dev_set_name(&controller->dev, "peci-%d", controller->id);
0126     if (ret)
0127         goto err_put;
0128 
0129     pm_runtime_no_callbacks(&controller->dev);
0130     pm_suspend_ignore_children(&controller->dev, true);
0131     pm_runtime_enable(&controller->dev);
0132 
0133     device_set_node(&controller->dev, fwnode_handle_get(dev_fwnode(dev)));
0134 
0135     ret = device_add(&controller->dev);
0136     if (ret)
0137         goto err_fwnode;
0138 
0139     ret = devm_add_action_or_reset(dev, unregister_controller, controller);
0140     if (ret)
0141         return ERR_PTR(ret);
0142 
0143     /*
0144      * Ignoring retval since failures during scan are non-critical for
0145      * controller itself.
0146      */
0147     peci_controller_scan_devices(controller);
0148 
0149     return controller;
0150 
0151 err_fwnode:
0152     fwnode_handle_put(controller->dev.fwnode);
0153 
0154     pm_runtime_disable(&controller->dev);
0155 
0156 err_put:
0157     put_device(&controller->dev);
0158 
0159     return ERR_PTR(ret);
0160 }
0161 EXPORT_SYMBOL_NS_GPL(devm_peci_controller_add, PECI);
0162 
0163 static const struct peci_device_id *
0164 peci_bus_match_device_id(const struct peci_device_id *id, struct peci_device *device)
0165 {
0166     while (id->family != 0) {
0167         if (id->family == device->info.family &&
0168             id->model == device->info.model)
0169             return id;
0170         id++;
0171     }
0172 
0173     return NULL;
0174 }
0175 
0176 static int peci_bus_device_match(struct device *dev, struct device_driver *drv)
0177 {
0178     struct peci_device *device = to_peci_device(dev);
0179     struct peci_driver *peci_drv = to_peci_driver(drv);
0180 
0181     if (dev->type != &peci_device_type)
0182         return 0;
0183 
0184     return !!peci_bus_match_device_id(peci_drv->id_table, device);
0185 }
0186 
0187 static int peci_bus_device_probe(struct device *dev)
0188 {
0189     struct peci_device *device = to_peci_device(dev);
0190     struct peci_driver *driver = to_peci_driver(dev->driver);
0191 
0192     return driver->probe(device, peci_bus_match_device_id(driver->id_table, device));
0193 }
0194 
0195 static void peci_bus_device_remove(struct device *dev)
0196 {
0197     struct peci_device *device = to_peci_device(dev);
0198     struct peci_driver *driver = to_peci_driver(dev->driver);
0199 
0200     if (driver->remove)
0201         driver->remove(device);
0202 }
0203 
0204 struct bus_type peci_bus_type = {
0205     .name       = "peci",
0206     .match      = peci_bus_device_match,
0207     .probe      = peci_bus_device_probe,
0208     .remove     = peci_bus_device_remove,
0209     .bus_groups = peci_bus_groups,
0210 };
0211 
0212 static int __init peci_init(void)
0213 {
0214     int ret;
0215 
0216     ret = bus_register(&peci_bus_type);
0217     if (ret < 0) {
0218         pr_err("peci: failed to register PECI bus type!\n");
0219         return ret;
0220     }
0221 
0222     return 0;
0223 }
0224 module_init(peci_init);
0225 
0226 static void __exit peci_exit(void)
0227 {
0228     bus_unregister(&peci_bus_type);
0229 }
0230 module_exit(peci_exit);
0231 
0232 MODULE_AUTHOR("Jason M Bills <jason.m.bills@linux.intel.com>");
0233 MODULE_AUTHOR("Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>");
0234 MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
0235 MODULE_DESCRIPTION("PECI bus core module");
0236 MODULE_LICENSE("GPL");