xref: /openbmc/linux/include/linux/padata.h (revision 23966841)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * padata.h - header for the padata parallelization interface
4  *
5  * Copyright (C) 2008, 2009 secunet Security Networks AG
6  * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
7  */
8 
9 #ifndef PADATA_H
10 #define PADATA_H
11 
12 #include <linux/workqueue.h>
13 #include <linux/spinlock.h>
14 #include <linux/list.h>
15 #include <linux/notifier.h>
16 #include <linux/kobject.h>
17 
18 #define PADATA_CPU_SERIAL   0x01
19 #define PADATA_CPU_PARALLEL 0x02
20 
21 /**
22  * struct padata_priv -  Embedded to the users data structure.
23  *
24  * @list: List entry, to attach to the padata lists.
25  * @pd: Pointer to the internal control structure.
26  * @cb_cpu: Callback cpu for serializatioon.
27  * @cpu: Cpu for parallelization.
28  * @seq_nr: Sequence number of the parallelized data object.
29  * @info: Used to pass information from the parallel to the serial function.
30  * @parallel: Parallel execution function.
31  * @serial: Serial complete function.
32  */
33 struct padata_priv {
34 	struct list_head	list;
35 	struct parallel_data	*pd;
36 	int			cb_cpu;
37 	int			cpu;
38 	int			info;
39 	void                    (*parallel)(struct padata_priv *padata);
40 	void                    (*serial)(struct padata_priv *padata);
41 };
42 
43 /**
44  * struct padata_list
45  *
46  * @list: List head.
47  * @lock: List lock.
48  */
49 struct padata_list {
50 	struct list_head        list;
51 	spinlock_t              lock;
52 };
53 
54 /**
55 * struct padata_serial_queue - The percpu padata serial queue
56 *
57 * @serial: List to wait for serialization after reordering.
58 * @work: work struct for serialization.
59 * @pd: Backpointer to the internal control structure.
60 */
61 struct padata_serial_queue {
62        struct padata_list    serial;
63        struct work_struct    work;
64        struct parallel_data *pd;
65 };
66 
67 /**
68  * struct padata_parallel_queue - The percpu padata parallel queue
69  *
70  * @parallel: List to wait for parallelization.
71  * @reorder: List to wait for reordering after parallel processing.
72  * @serial: List to wait for serialization after reordering.
73  * @pwork: work struct for parallelization.
74  * @swork: work struct for serialization.
75  * @work: work struct for parallelization.
76  * @num_obj: Number of objects that are processed by this cpu.
77  * @cpu_index: Index of the cpu.
78  */
79 struct padata_parallel_queue {
80        struct padata_list    parallel;
81        struct padata_list    reorder;
82        struct work_struct    work;
83        atomic_t              num_obj;
84        int                   cpu_index;
85 };
86 
87 /**
88  * struct padata_cpumask - The cpumasks for the parallel/serial workers
89  *
90  * @pcpu: cpumask for the parallel workers.
91  * @cbcpu: cpumask for the serial (callback) workers.
92  */
93 struct padata_cpumask {
94 	cpumask_var_t	pcpu;
95 	cpumask_var_t	cbcpu;
96 };
97 
98 /**
99  * struct parallel_data - Internal control structure, covers everything
100  * that depends on the cpumask in use.
101  *
102  * @pinst: padata instance.
103  * @pqueue: percpu padata queues used for parallelization.
104  * @squeue: percpu padata queues used for serialuzation.
105  * @reorder_objects: Number of objects waiting in the reorder queues.
106  * @refcnt: Number of objects holding a reference on this parallel_data.
107  * @max_seq_nr:  Maximal used sequence number.
108  * @cpu: Next CPU to be processed.
109  * @cpumask: The cpumasks in use for parallel and serial workers.
110  * @reorder_work: work struct for reordering.
111  * @lock: Reorder lock.
112  */
113 struct parallel_data {
114 	struct padata_instance		*pinst;
115 	struct padata_parallel_queue	__percpu *pqueue;
116 	struct padata_serial_queue	__percpu *squeue;
117 	atomic_t			reorder_objects;
118 	atomic_t			refcnt;
119 	atomic_t			seq_nr;
120 	int				cpu;
121 	struct padata_cpumask		cpumask;
122 	struct work_struct		reorder_work;
123 	spinlock_t                      lock ____cacheline_aligned;
124 };
125 
126 /**
127  * struct padata_instance - The overall control structure.
128  *
129  * @cpu_notifier: cpu hotplug notifier.
130  * @wq: The workqueue in use.
131  * @pd: The internal control structure.
132  * @cpumask: User supplied cpumasks for parallel and serial works.
133  * @cpumask_change_notifier: Notifiers chain for user-defined notify
134  *            callbacks that will be called when either @pcpu or @cbcpu
135  *            or both cpumasks change.
136  * @kobj: padata instance kernel object.
137  * @lock: padata instance lock.
138  * @flags: padata flags.
139  */
140 struct padata_instance {
141 	struct hlist_node		 node;
142 	struct workqueue_struct		*wq;
143 	struct parallel_data		*pd;
144 	struct padata_cpumask		cpumask;
145 	struct blocking_notifier_head	 cpumask_change_notifier;
146 	struct kobject                   kobj;
147 	struct mutex			 lock;
148 	u8				 flags;
149 #define	PADATA_INIT	1
150 #define	PADATA_RESET	2
151 #define	PADATA_INVALID	4
152 };
153 
154 extern struct padata_instance *padata_alloc_possible(
155 					struct workqueue_struct *wq);
156 extern void padata_free(struct padata_instance *pinst);
157 extern int padata_do_parallel(struct padata_instance *pinst,
158 			      struct padata_priv *padata, int cb_cpu);
159 extern void padata_do_serial(struct padata_priv *padata);
160 extern int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
161 			      cpumask_var_t cpumask);
162 extern int padata_start(struct padata_instance *pinst);
163 extern void padata_stop(struct padata_instance *pinst);
164 extern int padata_register_cpumask_notifier(struct padata_instance *pinst,
165 					    struct notifier_block *nblock);
166 extern int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
167 					      struct notifier_block *nblock);
168 #endif
169