xref: /linux-6.15/include/linux/cpu_rmap.h (revision bd7c0060)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2122733a1SAmir Vadai #ifndef __LINUX_CPU_RMAP_H
3122733a1SAmir Vadai #define __LINUX_CPU_RMAP_H
4122733a1SAmir Vadai 
5c39649c3SBen Hutchings /*
6c39649c3SBen Hutchings  * cpu_rmap.c: CPU affinity reverse-map support
7c39649c3SBen Hutchings  * Copyright 2011 Solarflare Communications Inc.
8c39649c3SBen Hutchings  */
9c39649c3SBen Hutchings 
10e1b6705bSYury Norov #include <linux/cpumask_types.h>
11c39649c3SBen Hutchings #include <linux/gfp.h>
12c39649c3SBen Hutchings #include <linux/slab.h>
13896f97eaSDavid Decotigny #include <linux/kref.h>
14c39649c3SBen Hutchings 
15c39649c3SBen Hutchings /**
16c39649c3SBen Hutchings  * struct cpu_rmap - CPU affinity reverse-map
17896f97eaSDavid Decotigny  * @refcount: kref for object
18c39649c3SBen Hutchings  * @size: Number of objects to be reverse-mapped
19c39649c3SBen Hutchings  * @obj: Pointer to array of object pointers
20c39649c3SBen Hutchings  * @near: For each CPU, the index and distance to the nearest object,
21c39649c3SBen Hutchings  *      based on affinity masks
22c39649c3SBen Hutchings  */
23c39649c3SBen Hutchings struct cpu_rmap {
24896f97eaSDavid Decotigny 	struct kref	refcount;
259821d8d4SEli Cohen 	u16		size;
26c39649c3SBen Hutchings 	void		**obj;
27c39649c3SBen Hutchings 	struct {
28c39649c3SBen Hutchings 		u16	index;
29c39649c3SBen Hutchings 		u16	dist;
3031232272SGustavo A. R. Silva 	}		near[];
31c39649c3SBen Hutchings };
32c39649c3SBen Hutchings #define CPU_RMAP_DIST_INF 0xffff
33c39649c3SBen Hutchings 
34c39649c3SBen Hutchings extern struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags);
35*bd7c0060SAhmed Zaki extern void cpu_rmap_get(struct cpu_rmap *rmap);
36896f97eaSDavid Decotigny extern int cpu_rmap_put(struct cpu_rmap *rmap);
37c39649c3SBen Hutchings 
38c39649c3SBen Hutchings extern int cpu_rmap_add(struct cpu_rmap *rmap, void *obj);
39c39649c3SBen Hutchings extern int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
40c39649c3SBen Hutchings 			   const struct cpumask *affinity);
41c39649c3SBen Hutchings 
cpu_rmap_lookup_index(struct cpu_rmap * rmap,unsigned int cpu)42c39649c3SBen Hutchings static inline u16 cpu_rmap_lookup_index(struct cpu_rmap *rmap, unsigned int cpu)
43c39649c3SBen Hutchings {
44c39649c3SBen Hutchings 	return rmap->near[cpu].index;
45c39649c3SBen Hutchings }
46c39649c3SBen Hutchings 
cpu_rmap_lookup_obj(struct cpu_rmap * rmap,unsigned int cpu)47c39649c3SBen Hutchings static inline void *cpu_rmap_lookup_obj(struct cpu_rmap *rmap, unsigned int cpu)
48c39649c3SBen Hutchings {
49c39649c3SBen Hutchings 	return rmap->obj[rmap->near[cpu].index];
50c39649c3SBen Hutchings }
51c39649c3SBen Hutchings 
52c39649c3SBen Hutchings /**
53c39649c3SBen Hutchings  * alloc_irq_cpu_rmap - allocate CPU affinity reverse-map for IRQs
54c39649c3SBen Hutchings  * @size: Number of objects to be mapped
55c39649c3SBen Hutchings  *
56c39649c3SBen Hutchings  * Must be called in process context.
57c39649c3SBen Hutchings  */
alloc_irq_cpu_rmap(unsigned int size)58c39649c3SBen Hutchings static inline struct cpu_rmap *alloc_irq_cpu_rmap(unsigned int size)
59c39649c3SBen Hutchings {
60c39649c3SBen Hutchings 	return alloc_cpu_rmap(size, GFP_KERNEL);
61c39649c3SBen Hutchings }
62c39649c3SBen Hutchings extern void free_irq_cpu_rmap(struct cpu_rmap *rmap);
63c39649c3SBen Hutchings 
6471f0a247SEli Cohen int irq_cpu_rmap_remove(struct cpu_rmap *rmap, int irq);
65c39649c3SBen Hutchings extern int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq);
66c39649c3SBen Hutchings 
67122733a1SAmir Vadai #endif /* __LINUX_CPU_RMAP_H */
68