xref: /optee_os/core/include/kernel/interrupt.h (revision 50cbe7eb8047887f17c993ceb6bbe34fed4625f2)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2016-2019, Linaro Limited
4  */
5 #ifndef __KERNEL_INTERRUPT_H
6 #define __KERNEL_INTERRUPT_H
7 
8 #include <dt-bindings/interrupt-controller/irq.h>
9 #include <kernel/dt_driver.h>
10 #include <mm/core_memprot.h>
11 #include <sys/queue.h>
12 #include <tee_api_types.h>
13 #include <types_ext.h>
14 #include <util.h>
15 
16 #define ITRF_TRIGGER_LEVEL	BIT(0)
17 #define ITRF_SHARED		BIT(1)
18 
19 /* Forward the interrupt only to the current CPU */
20 #define ITR_CPU_MASK_TO_THIS_CPU	BIT(31)
21 /* Forward the interrupt to all CPUs except the current CPU */
22 #define ITR_CPU_MASK_TO_OTHER_CPUS	BIT(30)
23 
24 struct itr_handler;
25 
26 /*
27  * struct itr_chip - Interrupt controller
28  *
29  * @ops Operation callback functions
30  * @name Controller name, for debug purpose
31  * @handlers Registered handlers list head
32  * @dt_get_irq Device tree node parsing function
33  */
34 struct itr_chip {
35 	const struct itr_ops *ops;
36 	const char *name;
37 	SLIST_HEAD(, itr_handler) handlers;
38 	/*
39 	 * dt_get_irq - parse a device tree interrupt property
40 	 *
41 	 * @properties Big-endian interrupt property array from device tree
42 	 * @count number of elements in @properties
43 	 * @type If not NULL, output interrupt type (IRQ_TYPE_* defines)
44 	 * or IRQ_TYPE_NONE if unknown
45 	 * @prio If not NULL, output interrupt priority value or 0 if unknown
46 	 */
47 	int (*dt_get_irq)(const uint32_t *properties, int count, uint32_t *type,
48 			  uint32_t *prio);
49 };
50 
51 /*
52  * struct itr_ops - Interrupt controller operations
53  * @add		Register and configure an interrupt
54  * @enable	Enable an interrupt
55  * @disable	Disable an interrupt
56  * @mask	Mask an interrupt, may be called from an interrupt context
57  * @unmask	Unmask an interrupt, may be called from an interrupt context
58  * @raise_pi	Raise per-cpu interrupt or NULL if not applicable
59  * @raise_sgi	Raise a SGI or NULL if not applicable to that controller
60  * @set_affinity Set interrupt/cpu affinity or NULL if not applicable
61  *
62  * Handlers @enable, @disable, @mask, @unmask and @add are mandated. Handlers
63  * @mask and @unmask have unpaged memory contrainsts. See itr_chip_is_valid().
64  */
65 struct itr_ops {
66 	void (*add)(struct itr_chip *chip, size_t it, uint32_t type,
67 		    uint32_t prio);
68 	void (*enable)(struct itr_chip *chip, size_t it);
69 	void (*disable)(struct itr_chip *chip, size_t it);
70 	void (*mask)(struct itr_chip *chip, size_t it);
71 	void (*unmask)(struct itr_chip *chip, size_t it);
72 	void (*raise_pi)(struct itr_chip *chip, size_t it);
73 	void (*raise_sgi)(struct itr_chip *chip, size_t it,
74 			  uint32_t cpu_mask);
75 	void (*set_affinity)(struct itr_chip *chip, size_t it,
76 		uint8_t cpu_mask);
77 };
78 
79 /*
80  * struct itr_desc - Interrupt description
81  * @chip	Interrupt controller reference
82  * @itr_num	Interrupt number
83  *
84  * This struct is used for binding interrupt device data between
85  * drivers when using DT_DRIVERS means. See itr_dt_get_func type
86  * definition.
87  */
88 struct itr_desc {
89 	struct itr_chip *chip;
90 	size_t itr_num;
91 };
92 
93 /* Interrupt handler return value */
94 enum itr_return {
95 	ITRR_NONE,
96 	ITRR_HANDLED,
97 };
98 
99 /* Interrupt handler signature */
100 typedef enum itr_return (*itr_handler_t)(struct itr_handler *h);
101 
102 /*
103  * struct itr_handler - Interrupt handler reference
104  * @it Interrupt number
105  * @flags Property bit flags (ITRF_*) or 0
106  * @data Private data for that interrupt handler
107  * @chip Interrupt controller chip device
108  * @link Reference in controller handler list
109  */
110 struct itr_handler {
111 	size_t it;
112 	uint32_t flags;
113 	itr_handler_t handler;
114 	void *data;
115 	struct itr_chip *chip;
116 	SLIST_ENTRY(itr_handler) link;
117 };
118 
119 #define ITR_HANDLER(_chip, _itr_num, _flags, _fn, _priv) \
120 	((struct itr_handler){ \
121 		.chip = (_chip), .it = (_itr_num), .flags = (_flags), \
122 		.handler = (_fn), .data = (_priv), \
123 	})
124 
125 /*
126  * Return true only if interrupt chip provides required handlers
127  * @chip: Interrupt controller reference
128  */
129 static inline bool itr_chip_is_valid(struct itr_chip *chip)
130 {
131 	return chip && is_unpaged(chip) && chip->ops &&
132 	       is_unpaged((void *)chip->ops) &&
133 	       chip->ops->mask && is_unpaged(chip->ops->mask) &&
134 	       chip->ops->unmask && is_unpaged(chip->ops->unmask) &&
135 	       chip->ops->enable && chip->ops->disable &&
136 	       chip->ops->add;
137 }
138 
139 /*
140  * Initialise an interrupt controller handle
141  * @chip	Interrupt controller
142  */
143 TEE_Result itr_chip_init(struct itr_chip *chip);
144 
145 /*
146  * Initialise main interrupt controller driver
147  * @data Main controller main data reference to register
148  */
149 void interrupt_main_init(struct itr_chip *data);
150 
151 /* Retrieve main interrupt controller reference */
152 struct itr_chip *interrupt_get_main_chip(void);
153 /* Retrieve main interrupt controller reference, or NULL on failure */
154 struct itr_chip *interrupt_get_main_chip_may_fail(void);
155 
156 #ifdef CFG_DT
157 /*
158  * Get the DT interrupt property at @node. In the DT an interrupt property can
159  * specify additional information which can be retrieved with @type and @prio.
160  *
161  * @fdt reference to the Device Tree
162  * @node is the node offset to read the interrupt property from
163  * @type interrupt type (IRQ_TYPE_* defines) if specified by interrupt property
164  * or IRQ_TYPE_NONE if not. Can be NULL if not needed
165  * @prio interrupt priority if specified by interrupt property or 0 if not. Can
166  * be NULL if not needed
167  *
168  * Returns the interrupt number if value >= 0
169  * otherwise DT_INFO_INVALID_INTERRUPT
170  */
171 int dt_get_irq_type_prio(const void *fdt, int node, uint32_t *type,
172 			 uint32_t *prio);
173 
174 /*
175  * Get the DT interrupt property at @node
176  */
177 static inline int dt_get_irq(const void *fdt, int node)
178 {
179 	return dt_get_irq_type_prio(fdt, node, NULL, NULL);
180 }
181 #endif
182 
183 /*
184  * __weak overridable function which is called when a secure interrupt is
185  * received. The default function calls panic() immediately, platforms which
186  * expects to receive secure interrupts should override this function.
187  */
188 void interrupt_main_handler(void);
189 
190 /*
191  * Interrupt controller chip API functions
192  */
193 
194 /*
195  * interrupt_call_handlers() - Call registered handlers for an interrupt
196  * @chip	Interrupt controller
197  * @itr_num	Interrupt number
198  *
199  * This function is called from an interrupt context by a primary interrupt
200  * handler. This function calls the handlers registered for that interrupt.
201  * If interrupt is not handled, it is masked.
202  */
203 void interrupt_call_handlers(struct itr_chip *chip, size_t itr_num);
204 
205 /*
206  * interrupt_mask() - Mask an interrupt
207  * @chip	Interrupt controller
208  * @itr_num	Interrupt number
209  *
210  * This function may be called in interrupt context
211  */
212 static inline void interrupt_mask(struct itr_chip *chip, size_t itr_num)
213 {
214 	chip->ops->mask(chip, itr_num);
215 }
216 
217 /*
218  * interrupt_unmask() - Unmask an interrupt
219  * @chip	Interrupt controller
220  * @itr_num	Interrupt number
221  *
222  * This function may be called in interrupt context
223  */
224 static inline void interrupt_unmask(struct itr_chip *chip, size_t itr_num)
225 {
226 	chip->ops->unmask(chip, itr_num);
227 }
228 
229 /*
230  * interrupt_enable() - Enable an interrupt
231  * @chip	Interrupt controller
232  * @itr_num	Interrupt number
233  */
234 static inline void interrupt_enable(struct itr_chip *chip, size_t itr_num)
235 {
236 	chip->ops->enable(chip, itr_num);
237 }
238 
239 /*
240  * interrupt_disable() - Disable an interrupt
241  * @chip	Interrupt controller
242  * @itr_num	Interrupt number
243  */
244 static inline void interrupt_disable(struct itr_chip *chip, size_t itr_num)
245 {
246 	chip->ops->disable(chip, itr_num);
247 }
248 
249 /*
250  * interrupt_can_raise_pi() - Return whether controller embeds raise_pi
251  * @chip	Interrupt controller
252  */
253 static inline bool interrupt_can_raise_pi(struct itr_chip *chip)
254 {
255 	return chip->ops->raise_pi;
256 }
257 
258 /*
259  * interrupt_can_raise_sgi() - Return whether controller embeds raise_sgi
260  * @chip	Interrupt controller
261  */
262 static inline bool interrupt_can_raise_sgi(struct itr_chip *chip)
263 {
264 	return chip->ops->raise_sgi;
265 }
266 
267 /*
268  * interrupt_can_set_affinity() - Return whether controller embeds set_affinity
269  * @chip	Interrupt controller
270  */
271 static inline bool interrupt_can_set_affinity(struct itr_chip *chip)
272 {
273 	return chip->ops->set_affinity;
274 }
275 
276 /*
277  * interrupt_raise_pi() - Raise a peripheral interrupt of a controller
278  * @chip	Interrupt controller
279  * @itr_num	Interrupt number to raise
280  */
281 static inline void interrupt_raise_pi(struct itr_chip *chip, size_t itr_num)
282 {
283 	assert(interrupt_can_raise_pi(chip));
284 	chip->ops->raise_pi(chip, itr_num);
285 }
286 
287 /*
288  * interrupt_raise_sgi() - Raise a software generiated interrupt of a controller
289  * @chip	Interrupt controller
290  * @itr_num	Interrupt number to raise
291  * @cpu_mask:	A bitfield of CPUs to forward the interrupt to, unless
292  *		ITR_CPU_MASK_TO_THIS_CPU or ITR_CPU_MASK_TO_OTHER_CPUS
293  *		(mutually exclusive) are set.
294  */
295 static inline void interrupt_raise_sgi(struct itr_chip *chip, size_t itr_num,
296 				       uint32_t cpu_mask)
297 {
298 	assert(interrupt_can_raise_sgi(chip));
299 	chip->ops->raise_sgi(chip, itr_num, cpu_mask);
300 }
301 
302 /*
303  * interrupt_set_affinity() - Set CPU affinity for a controller interrupt
304  * @chip	Interrupt controller
305  * @itr_num	Interrupt number to raise
306  * @cpu_mask	Mask of the CPUs targeted by the interrupt
307  */
308 static inline void interrupt_set_affinity(struct itr_chip *chip, size_t itr_num,
309 					  uint8_t cpu_mask)
310 {
311 	assert(interrupt_can_set_affinity(chip));
312 	chip->ops->set_affinity(chip, itr_num, cpu_mask);
313 }
314 
315 /*
316  * interrupt_configure() - Configure an interrupt in an interrupt controller
317  * @chip	Interrupt controller
318  * @itr_num	Interrupt number
319  * @type	Interrupt trigger type (IRQ_TYPE_* defines) or IRQ_TYPE_NONE
320  * @prio	Interrupt priority or 0
321  *
322  * Interrupt consumers that get their interrupt from the DT do not need to
323  * call interrupt_configure() since the interrupt configuration has already
324  * been done by interrupt controller based on the DT bidings.
325  */
326 TEE_Result interrupt_configure(struct itr_chip *chip, size_t itr_num,
327 			       uint32_t type, uint32_t prio);
328 
329 /*
330  * interrupt_add_and_configure_handler() - Register and configure a handler
331  * @hdl		Interrupt handler to register
332  * @type	Interrupt trigger type (IRQ_TYPE_* defines) or IRQ_TYPE_NONE
333  * @prio	Interrupt priority or 0
334  */
335 TEE_Result interrupt_add_configure_handler(struct itr_handler *hdl,
336 					   uint32_t type, uint32_t prio);
337 
338 /*
339  * interrupt_add_handler() - Register an interrupt handler
340  * @hdl		Interrupt handler to register
341  *
342  * This helper function assumes interrupt type is set to IRQ_TYPE_NONE
343  * and interrupt priority to 0.
344  */
345 static inline TEE_Result interrupt_add_handler(struct itr_handler *hdl)
346 {
347 	return interrupt_add_configure_handler(hdl, IRQ_TYPE_NONE, 0);
348 }
349 
350 /*
351  * interrupt_create_handler() - Allocate/register an interrupt callback handler
352  * @itr_chip	Interrupt chip obtained from interrupt_dt_get_by_*() or like
353  * @itr_num	Interrupt number obtained from interrupt_dt_get_by_*() or like
354  * @callback	Callback handler function
355  * @priv	Private dat to pssa to @callback
356  * @flags	INTERRUPT_FLAGS_* or 0
357  * @out_hdl	Output allocated and registered handler or NULL
358  *
359  * This function differs from interrupt_add_handler() in that the
360  * interrupt is not reconfigured. interrupt_create_handler() expects
361  * @itr_chip and @itr_num were obtained from a call
362  * to interrupt_dt_get_by_index() or interrupt_dt_get_by_name() that
363  * are in charge of configuring the interrupt according to its DT property.
364  */
365 TEE_Result interrupt_create_handler(struct itr_chip *itr_chip, size_t itr_num,
366 				    itr_handler_t callback, void *priv,
367 				    uint32_t flags,
368 				    struct itr_handler **out_hdl);
369 
370 /*
371  * interrupt_add_handler_with_chip() - Register an interrupt handler providing
372  *	the interrupt chip reference in specific argument @chip.
373  * @chip	Interrupt controller
374  * @h		Interrupt handler to register
375  */
376 static inline TEE_Result interrupt_add_handler_with_chip(struct itr_chip *chip,
377 							 struct itr_handler *h)
378 {
379 	h->chip = chip;
380 	return interrupt_add_handler(h);
381 }
382 
383 /*
384  * interrupt_remove_handler() - Remove a registered interrupt handler
385  * @hdl		Interrupt handler to remove
386  *
387  * This function is the counterpart of interrupt_add_handler().
388  * This function may panic on non-NULL invalid @hdl reference.
389  */
390 void interrupt_remove_handler(struct itr_handler *hdl);
391 
392 /*
393  * interrupt_alloc_add_conf_handler() - Allocate, configure, register a handler
394  * @chip	Interrupt controller
395  * @itr_num	Interrupt number
396  * @handler	Interrupt handler to register
397  * @flags	Bitmask flag ITRF_*
398  * @data	Private data reference passed to @handler
399  * @type	Interrupt trigger type (IRQ_TYPE_* defines) or IRQ_TYPE_NONE
400  * @prio	Interrupt priority or 0
401  * @out_hdl	NULL or output pointer to allocated struct itr_handler
402  */
403 TEE_Result interrupt_alloc_add_conf_handler(struct itr_chip *chip,
404 					    size_t it_num,
405 					    itr_handler_t handler,
406 					    uint32_t flags, void *data,
407 					    uint32_t type, uint32_t prio,
408 					    struct itr_handler **out_hdl);
409 
410 /*
411  * interrupt_alloc_add_handler() - Allocate and register an interrupt handler
412  * @chip	Interrupt controller
413  * @itr_num	Interrupt number
414  * @handler	Interrupt handler to register
415  * @flags	Bitmask flag ITRF_*
416  * @data	Private data reference passed to @handler
417  * @out_hdl	NULL or output pointer to allocated struct itr_handler
418  */
419 static inline TEE_Result interrupt_alloc_add_handler(struct itr_chip *chip,
420 						     size_t it_num,
421 						     itr_handler_t handler,
422 						     uint32_t flags,
423 						     void *data,
424 						     struct itr_handler **hdl)
425 {
426 	return interrupt_alloc_add_conf_handler(chip, it_num, handler, flags,
427 						data, IRQ_TYPE_NONE, 0, hdl);
428 }
429 
430 /*
431  * interrupt_remove_free_handler() - Remove/free a registered interrupt handler
432  * @hdl		Interrupt handler to remove and free
433  *
434  * This function is the counterpart of interrupt_alloc_add_handler()
435  * and interrupt_alloc_add_conf_handler().
436  * This function may panic on non-NULL invalid @hdl reference.
437  */
438 void interrupt_remove_free_handler(struct itr_handler *hdl);
439 
440 /*
441  * itr_dt_get_func - Typedef of function to get an interrupt in DT node
442  *
443  * @args	Reference to phandle arguments
444  * @data	Pointer to data given at interrupt_register_provider() call
445  * @itr_desc_p	Pointer to the struct itr_desc to fill
446  * Return TEE_SUCCESS in case of success.
447  * Return TEE_ERROR_DEFER_DRIVER_INIT if controller is not initialized.
448  * Return another TEE_Result code otherwise.
449  *
450  * Upon success, the interrupt is configured and consumer can add a handler
451  * function to the interrupt. Yet, the interrupt is not enabled until consumer
452  * calls interrupt_enable().
453  */
454 typedef TEE_Result (*itr_dt_get_func)(struct dt_pargs *args, void *data,
455 				      struct itr_desc *itr_desc_p);
456 
457 #ifdef CFG_DT
458 /**
459  * interrupt_register_provider() - Register an interrupt provider
460  *
461  * @fdt		Device tree to work on
462  * @node	Node offset of the interrupt controller in the DT
463  * @dt_get_itr	Callback to match the devicetree interrupt reference with
464  * @data	Data which will be passed to the get_dt_its callback
465  */
466 TEE_Result interrupt_register_provider(const void *fdt, int node,
467 				       itr_dt_get_func dt_get_itr, void *data);
468 
469 /**
470  * interrupt_dt_get_by_index() - Get an interrupt from DT by interrupt index
471  *
472  * Interrupt index (@index) refers to the index of the target interrupt to be
473  * retrieved as DT binding property "interrupts" may define several
474  * interrupts.
475  *
476  * @fdt		Device tree to work on
477  * @node	Node offset of the subnode containing interrupt(s) references
478  * @index	Index in "interrupts" or "interrupts-extended" property list
479  * @chip	Output interrupt controller reference upon success
480  * @itr_num	Output interrupt number upon success
481  *
482  * Return TEE_SUCCESS in case of success
483  * Return TEE_ERROR_DEFER_DRIVER_INIT if interrupt driver is not yet initialized
484  * Return TEE_ERROR_ITEM_NOT_FOUND if the DT does not reference target interrupt
485  * Return any other TEE_Result compliant code in case of error
486  */
487 TEE_Result interrupt_dt_get_by_index(const void *fdt, int node,
488 				     unsigned int index, struct itr_chip **chip,
489 				     size_t *itr_num);
490 
491 /**
492  * interrupt_dt_get_by_name() - Get an interrupt from DT by interrupt name
493  *
494  * @fdt		Device tree to work on
495  * @node	Node offset of the subnode containing interrupt(s) references
496  * @name	Name identifier used in "interrupt-names" property
497  * @chip	Output interrupt controller reference upon success
498  * @itr_num	Output interrupt number upon success
499  *
500  * Return TEE_SUCCESS in case of success
501  * Return TEE_ERROR_DEFER_DRIVER_INIT if interrupt driver is not yet initialized
502  * Return TEE_ERROR_ITEM_NOT_FOUND if the DT does not reference target interrupt
503  * Return any other TEE_Result compliant code in case of error
504  */
505 TEE_Result interrupt_dt_get_by_name(const void *fdt, int node, const char *name,
506 				    struct itr_chip **chip, size_t *itr_num);
507 #else
508 static inline TEE_Result interrupt_register_provider(const void *dt __unused,
509 						     int node __unused,
510 						     itr_dt_get_func f __unused,
511 						     void *data __unused)
512 {
513 	return TEE_ERROR_NOT_IMPLEMENTED;
514 }
515 
516 static inline TEE_Result interrupt_dt_get_by_index(const void *fdt __unused,
517 						   int node __unused,
518 						   unsigned int index __unused,
519 						   struct itr_chip **c __unused,
520 						   size_t *itr_num __unused)
521 {
522 	return TEE_ERROR_NOT_IMPLEMENTED;
523 }
524 
525 static inline TEE_Result interrupt_dt_get_by_name(const void *fdt __unused,
526 						  int node __unused,
527 						  const char *name __unused,
528 						  struct itr_chip **ch __unused,
529 						  size_t *itr_num __unused)
530 {
531 	return TEE_ERROR_NOT_IMPLEMENTED;
532 }
533 #endif /*CFG_DT*/
534 
535 /*
536  * Helper function for when caller retrieves the first interrupt defined
537  * in "interrupts" or "interrupts-extended" DT binding property list.
538  */
539 static inline TEE_Result interrupt_dt_get(const void *fdt, int node,
540 					  struct itr_chip **chip,
541 					  size_t *itr_num)
542 {
543 	return interrupt_dt_get_by_index(fdt, node, 0, chip, itr_num);
544 }
545 #endif /*__KERNEL_INTERRUPT_H*/
546