layerscape: add ls1088ardb device support
[openwrt/openwrt.git] / target / linux / layerscape / patches-4.4 / 7197-staging-fsl-mc-Management-Complex-restool-driver.patch
1 From fb4881d149742e4c5595aca8bf86c99d2ea155ad Mon Sep 17 00:00:00 2001
2 From: Lijun Pan <Lijun.Pan@freescale.com>
3 Date: Mon, 8 Feb 2016 17:40:18 -0600
4 Subject: [PATCH 197/226] staging: fsl-mc: Management Complex restool driver
5
6 The kernel support for the restool (a user space resource management
7 tool) is a driver for the /dev/dprc.N device file.
8 Its purpose is to provide an ioctl interface,
9 which the restool uses to interact with the MC bus driver
10 and with the MC firmware.
11 We allocate a dpmcp at driver initialization,
12 and keep that dpmcp until driver exit.
13 We use that dpmcp by default.
14 If that dpmcp is in use, we create another portal at run time
15 and destroy the newly created portal after use.
16 The ioctl RESTOOL_SEND_MC_COMMAND sends user space command to fsl-mc
17 bus and utilizes the fsl-mc bus to communicate with MC firmware.
18 The ioctl RESTOOL_DPRC_SYNC request the mc-bus launch
19 objects scan under root dprc.
20 In order to support multiple root dprc, we utilize the bus notify
21 mechanism to scan fsl_mc_bus_type for the newly added root dprc.
22 After discovering the root dprc, it creates a miscdevice
23 /dev/dprc.N to associate with this root dprc.
24
25 Signed-off-by: Lijun Pan <Lijun.Pan@freescale.com>
26 [Stuart: minor fix to resolve compile error]
27 Signed-off-by: Stuart Yoder <stuart.yoder@nxp.com>
28 ---
29 Documentation/ioctl/ioctl-number.txt | 1 +
30 drivers/staging/fsl-mc/bus/Kconfig | 7 +-
31 drivers/staging/fsl-mc/bus/Makefile | 3 +
32 drivers/staging/fsl-mc/bus/mc-ioctl.h | 22 ++
33 drivers/staging/fsl-mc/bus/mc-restool.c | 392 +++++++++++++++++++++++++++++++
34 5 files changed, 424 insertions(+), 1 deletion(-)
35 create mode 100644 drivers/staging/fsl-mc/bus/mc-ioctl.h
36 create mode 100644 drivers/staging/fsl-mc/bus/mc-restool.c
37
38 --- a/Documentation/ioctl/ioctl-number.txt
39 +++ b/Documentation/ioctl/ioctl-number.txt
40 @@ -170,6 +170,7 @@ Code Seq#(hex) Include File Comments
41 'R' 00-1F linux/random.h conflict!
42 'R' 01 linux/rfkill.h conflict!
43 'R' C0-DF net/bluetooth/rfcomm.h
44 +'R' E0-EF drivers/staging/fsl-mc/bus/mc-ioctl.h
45 'S' all linux/cdrom.h conflict!
46 'S' 80-81 scsi/scsi_ioctl.h conflict!
47 'S' 82-FF scsi/scsi.h conflict!
48 --- a/drivers/staging/fsl-mc/bus/Kconfig
49 +++ b/drivers/staging/fsl-mc/bus/Kconfig
50 @@ -22,4 +22,9 @@ config FSL_MC_BUS
51 Only enable this option when building the kernel for
52 Freescale QorQIQ LS2xxxx SoCs.
53
54 -
55 +config FSL_MC_RESTOOL
56 + tristate "Freescale Management Complex (MC) restool driver"
57 + depends on FSL_MC_BUS
58 + help
59 + Driver that provides kernel support for the Freescale Management
60 + Complex resource manager user-space tool.
61 --- a/drivers/staging/fsl-mc/bus/Makefile
62 +++ b/drivers/staging/fsl-mc/bus/Makefile
63 @@ -18,3 +18,6 @@ mc-bus-driver-objs := mc-bus.o \
64 dpmcp.o \
65 dpbp.o \
66 dpcon.o
67 +
68 +# MC restool kernel support
69 +obj-$(CONFIG_FSL_MC_RESTOOL) += mc-restool.o
70 --- /dev/null
71 +++ b/drivers/staging/fsl-mc/bus/mc-ioctl.h
72 @@ -0,0 +1,22 @@
73 +/*
74 + * Freescale Management Complex (MC) ioclt interface
75 + *
76 + * Copyright (C) 2014 Freescale Semiconductor, Inc.
77 + * Author: Lijun Pan <Lijun.Pan@freescale.com>
78 + *
79 + * This file is licensed under the terms of the GNU General Public
80 + * License version 2. This program is licensed "as is" without any
81 + * warranty of any kind, whether express or implied.
82 + */
83 +#ifndef _FSL_MC_IOCTL_H_
84 +#define _FSL_MC_IOCTL_H_
85 +
86 +#include <linux/ioctl.h>
87 +#include "../include/mc-sys.h"
88 +
89 +#define RESTOOL_IOCTL_TYPE 'R'
90 +
91 +#define RESTOOL_SEND_MC_COMMAND \
92 + _IOWR(RESTOOL_IOCTL_TYPE, 0xE0, struct mc_command)
93 +
94 +#endif /* _FSL_MC_IOCTL_H_ */
95 --- /dev/null
96 +++ b/drivers/staging/fsl-mc/bus/mc-restool.c
97 @@ -0,0 +1,392 @@
98 +/*
99 + * Freescale Management Complex (MC) restool driver
100 + *
101 + * Copyright (C) 2014 Freescale Semiconductor, Inc.
102 + * Author: Lijun Pan <Lijun.Pan@freescale.com>
103 + *
104 + * This file is licensed under the terms of the GNU General Public
105 + * License version 2. This program is licensed "as is" without any
106 + * warranty of any kind, whether express or implied.
107 + */
108 +
109 +#include "../include/mc-private.h"
110 +#include <linux/module.h>
111 +#include <linux/fs.h>
112 +#include <linux/miscdevice.h>
113 +#include <linux/mm.h>
114 +#include <linux/slab.h>
115 +#include <linux/uaccess.h>
116 +#include <linux/mutex.h>
117 +#include <linux/platform_device.h>
118 +#include "mc-ioctl.h"
119 +#include "../include/mc-sys.h"
120 +#include "../include/mc-cmd.h"
121 +#include "../include/dpmng.h"
122 +
123 +/**
124 + * Maximum number of DPRCs that can be opened at the same time
125 + */
126 +#define MAX_DPRC_HANDLES 64
127 +
128 +/**
129 + * restool_misc - information associated with the newly added miscdevice
130 + * @misc: newly created miscdevice associated with root dprc
131 + * @miscdevt: device id of this miscdevice
132 + * @list: a linked list node representing this miscdevcie
133 + * @static_mc_io: pointer to the static MC I/O object used by the restool
134 + * @dynamic_instance_count: number of dynamically created instances
135 + * @static_instance_in_use: static instance is in use or not
136 + * @mutex: mutex lock to serialze the open/release operations
137 + * @dev: root dprc associated with this miscdevice
138 + */
139 +struct restool_misc {
140 + struct miscdevice misc;
141 + dev_t miscdevt;
142 + struct list_head list;
143 + struct fsl_mc_io *static_mc_io;
144 + u32 dynamic_instance_count;
145 + bool static_instance_in_use;
146 + struct mutex mutex; /* serialze the open/release operations */
147 + struct device *dev;
148 +};
149 +
150 +/*
151 + * initialize a global list to link all
152 + * the miscdevice nodes (struct restool_misc)
153 + */
154 +static LIST_HEAD(misc_list);
155 +static DEFINE_MUTEX(misc_list_mutex);
156 +
157 +static int fsl_mc_restool_dev_open(struct inode *inode, struct file *filep)
158 +{
159 + struct fsl_mc_device *root_mc_dev;
160 + int error;
161 + struct fsl_mc_io *dynamic_mc_io = NULL;
162 + struct restool_misc *restool_misc = NULL;
163 + struct restool_misc *restool_misc_cursor;
164 +
165 + mutex_lock(&misc_list_mutex);
166 +
167 + list_for_each_entry(restool_misc_cursor, &misc_list, list) {
168 + if (restool_misc_cursor->miscdevt == inode->i_rdev) {
169 + restool_misc = restool_misc_cursor;
170 + break;
171 + }
172 + }
173 +
174 + mutex_unlock(&misc_list_mutex);
175 +
176 + if (!restool_misc)
177 + return -EINVAL;
178 +
179 + if (WARN_ON(!restool_misc->dev))
180 + return -EINVAL;
181 +
182 + mutex_lock(&restool_misc->mutex);
183 +
184 + if (!restool_misc->static_instance_in_use) {
185 + restool_misc->static_instance_in_use = true;
186 + filep->private_data = restool_misc->static_mc_io;
187 + } else {
188 + dynamic_mc_io = kzalloc(sizeof(*dynamic_mc_io), GFP_KERNEL);
189 + if (!dynamic_mc_io) {
190 + error = -ENOMEM;
191 + goto err_unlock;
192 + }
193 +
194 + root_mc_dev = to_fsl_mc_device(restool_misc->dev);
195 + error = fsl_mc_portal_allocate(root_mc_dev, 0, &dynamic_mc_io);
196 + if (error < 0) {
197 + pr_err("Not able to allocate MC portal\n");
198 + goto free_dynamic_mc_io;
199 + }
200 + ++restool_misc->dynamic_instance_count;
201 + filep->private_data = dynamic_mc_io;
202 + }
203 +
204 + mutex_unlock(&restool_misc->mutex);
205 +
206 + return 0;
207 +
208 +free_dynamic_mc_io:
209 + kfree(dynamic_mc_io);
210 +err_unlock:
211 + mutex_unlock(&restool_misc->mutex);
212 +
213 + return error;
214 +}
215 +
216 +static int fsl_mc_restool_dev_release(struct inode *inode, struct file *filep)
217 +{
218 + struct fsl_mc_io *local_mc_io = filep->private_data;
219 + struct restool_misc *restool_misc = NULL;
220 + struct restool_misc *restool_misc_cursor;
221 +
222 + if (WARN_ON(!filep->private_data))
223 + return -EINVAL;
224 +
225 + mutex_lock(&misc_list_mutex);
226 +
227 + list_for_each_entry(restool_misc_cursor, &misc_list, list) {
228 + if (restool_misc_cursor->miscdevt == inode->i_rdev) {
229 + restool_misc = restool_misc_cursor;
230 + break;
231 + }
232 + }
233 +
234 + mutex_unlock(&misc_list_mutex);
235 +
236 + if (!restool_misc)
237 + return -EINVAL;
238 +
239 + mutex_lock(&restool_misc->mutex);
240 +
241 + if (WARN_ON(restool_misc->dynamic_instance_count == 0 &&
242 + !restool_misc->static_instance_in_use)) {
243 + mutex_unlock(&restool_misc->mutex);
244 + return -EINVAL;
245 + }
246 +
247 + /* Globally clean up opened/untracked handles */
248 + fsl_mc_portal_reset(local_mc_io);
249 +
250 + /*
251 + * must check
252 + * whether local_mc_io is dynamic or static instance
253 + * Otherwise it will free up the reserved portal by accident
254 + * or even not free up the dynamic allocated portal
255 + * if 2 or more instances running concurrently
256 + */
257 + if (local_mc_io == restool_misc->static_mc_io) {
258 + restool_misc->static_instance_in_use = false;
259 + } else {
260 + fsl_mc_portal_free(local_mc_io);
261 + kfree(filep->private_data);
262 + --restool_misc->dynamic_instance_count;
263 + }
264 +
265 + filep->private_data = NULL;
266 + mutex_unlock(&restool_misc->mutex);
267 +
268 + return 0;
269 +}
270 +
271 +static int restool_send_mc_command(unsigned long arg,
272 + struct fsl_mc_io *local_mc_io)
273 +{
274 + int error;
275 + struct mc_command mc_cmd;
276 +
277 + if (copy_from_user(&mc_cmd, (void __user *)arg, sizeof(mc_cmd)))
278 + return -EFAULT;
279 +
280 + /*
281 + * Send MC command to the MC:
282 + */
283 + error = mc_send_command(local_mc_io, &mc_cmd);
284 + if (error < 0)
285 + return error;
286 +
287 + if (copy_to_user((void __user *)arg, &mc_cmd, sizeof(mc_cmd)))
288 + return -EFAULT;
289 +
290 + return 0;
291 +}
292 +
293 +static long
294 +fsl_mc_restool_dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
295 +{
296 + int error;
297 +
298 + switch (cmd) {
299 + case RESTOOL_SEND_MC_COMMAND:
300 + error = restool_send_mc_command(arg, file->private_data);
301 + break;
302 + default:
303 + pr_err("%s: unexpected ioctl call number\n", __func__);
304 + error = -EINVAL;
305 + }
306 +
307 + return error;
308 +}
309 +
310 +static const struct file_operations fsl_mc_restool_dev_fops = {
311 + .owner = THIS_MODULE,
312 + .open = fsl_mc_restool_dev_open,
313 + .release = fsl_mc_restool_dev_release,
314 + .unlocked_ioctl = fsl_mc_restool_dev_ioctl,
315 +};
316 +
317 +static int restool_add_device_file(struct device *dev)
318 +{
319 + u32 name1 = 0;
320 + char name2[20] = {0};
321 + int error;
322 + struct fsl_mc_device *root_mc_dev;
323 + struct restool_misc *restool_misc;
324 +
325 + if (dev->bus == &platform_bus_type && dev->driver_data) {
326 + if (sscanf(dev_name(dev), "%x.%s", &name1, name2) != 2)
327 + return -EINVAL;
328 +
329 + if (strcmp(name2, "fsl-mc") == 0)
330 + pr_debug("platform's root dprc name is: %s\n",
331 + dev_name(&(((struct fsl_mc *)
332 + (dev->driver_data))->root_mc_bus_dev->dev)));
333 + }
334 +
335 + if (!fsl_mc_is_root_dprc(dev))
336 + return 0;
337 +
338 + restool_misc = kzalloc(sizeof(*restool_misc), GFP_KERNEL);
339 + if (!restool_misc)
340 + return -ENOMEM;
341 +
342 + restool_misc->dev = dev;
343 + root_mc_dev = to_fsl_mc_device(dev);
344 + error = fsl_mc_portal_allocate(root_mc_dev, 0,
345 + &restool_misc->static_mc_io);
346 + if (error < 0) {
347 + pr_err("Not able to allocate MC portal\n");
348 + goto free_restool_misc;
349 + }
350 +
351 + restool_misc->misc.minor = MISC_DYNAMIC_MINOR;
352 + restool_misc->misc.name = dev_name(dev);
353 + restool_misc->misc.fops = &fsl_mc_restool_dev_fops;
354 +
355 + error = misc_register(&restool_misc->misc);
356 + if (error < 0) {
357 + pr_err("misc_register() failed: %d\n", error);
358 + goto free_portal;
359 + }
360 +
361 + restool_misc->miscdevt = restool_misc->misc.this_device->devt;
362 + mutex_init(&restool_misc->mutex);
363 + mutex_lock(&misc_list_mutex);
364 + list_add(&restool_misc->list, &misc_list);
365 + mutex_unlock(&misc_list_mutex);
366 +
367 + pr_info("/dev/%s driver registered\n", dev_name(dev));
368 +
369 + return 0;
370 +
371 +free_portal:
372 + fsl_mc_portal_free(restool_misc->static_mc_io);
373 +free_restool_misc:
374 + kfree(restool_misc);
375 +
376 + return error;
377 +}
378 +
379 +static int restool_bus_notifier(struct notifier_block *nb,
380 + unsigned long action, void *data)
381 +{
382 + int error;
383 + struct device *dev = data;
384 +
385 + switch (action) {
386 + case BUS_NOTIFY_ADD_DEVICE:
387 + error = restool_add_device_file(dev);
388 + if (error)
389 + return error;
390 + break;
391 + case BUS_NOTIFY_DEL_DEVICE:
392 + case BUS_NOTIFY_REMOVED_DEVICE:
393 + case BUS_NOTIFY_BIND_DRIVER:
394 + case BUS_NOTIFY_BOUND_DRIVER:
395 + case BUS_NOTIFY_UNBIND_DRIVER:
396 + case BUS_NOTIFY_UNBOUND_DRIVER:
397 + break;
398 + default:
399 + pr_err("%s: unrecognized device action from %s\n", __func__,
400 + dev_name(dev));
401 + return -EINVAL;
402 + }
403 +
404 + return 0;
405 +}
406 +
407 +static int add_to_restool(struct device *dev, void *data)
408 +{
409 + return restool_add_device_file(dev);
410 +}
411 +
412 +static int __init fsl_mc_restool_driver_init(void)
413 +{
414 + int error;
415 + struct notifier_block *nb;
416 +
417 + nb = kzalloc(sizeof(*nb), GFP_KERNEL);
418 + if (!nb)
419 + return -ENOMEM;
420 +
421 + nb->notifier_call = restool_bus_notifier;
422 + error = bus_register_notifier(&fsl_mc_bus_type, nb);
423 + if (error)
424 + goto free_nb;
425 +
426 + /*
427 + * This driver runs after fsl-mc bus driver runs.
428 + * Hence, many of the root dprcs are already attached to fsl-mc bus
429 + * In order to make sure we find all the root dprcs,
430 + * we need to scan the fsl_mc_bus_type.
431 + */
432 + error = bus_for_each_dev(&fsl_mc_bus_type, NULL, NULL, add_to_restool);
433 + if (error) {
434 + bus_unregister_notifier(&fsl_mc_bus_type, nb);
435 + kfree(nb);
436 + pr_err("restool driver registration failure\n");
437 + return error;
438 + }
439 +
440 + return 0;
441 +
442 +free_nb:
443 + kfree(nb);
444 + return error;
445 +}
446 +
447 +module_init(fsl_mc_restool_driver_init);
448 +
449 +static void __exit fsl_mc_restool_driver_exit(void)
450 +{
451 + struct restool_misc *restool_misc;
452 + struct restool_misc *restool_misc_tmp;
453 + char name1[20] = {0};
454 + u32 name2 = 0;
455 +
456 + list_for_each_entry_safe(restool_misc, restool_misc_tmp,
457 + &misc_list, list) {
458 + if (sscanf(restool_misc->misc.name, "%4s.%u", name1, &name2)
459 + != 2)
460 + continue;
461 +
462 + pr_debug("name1=%s,name2=%u\n", name1, name2);
463 + pr_debug("misc-device: %s\n", restool_misc->misc.name);
464 + if (strcmp(name1, "dprc") != 0)
465 + continue;
466 +
467 + if (WARN_ON(!restool_misc->static_mc_io))
468 + return;
469 +
470 + if (WARN_ON(restool_misc->dynamic_instance_count != 0))
471 + return;
472 +
473 + if (WARN_ON(restool_misc->static_instance_in_use))
474 + return;
475 +
476 + misc_deregister(&restool_misc->misc);
477 + pr_info("/dev/%s driver unregistered\n",
478 + restool_misc->misc.name);
479 + fsl_mc_portal_free(restool_misc->static_mc_io);
480 + list_del(&restool_misc->list);
481 + kfree(restool_misc);
482 + }
483 +}
484 +
485 +module_exit(fsl_mc_restool_driver_exit);
486 +
487 +MODULE_AUTHOR("Freescale Semiconductor Inc.");
488 +MODULE_DESCRIPTION("Freescale's MC restool driver");
489 +MODULE_LICENSE("GPL");