1 /*
2  * cpu_rmap.c: CPU affinity reverse-map support
3  * Copyright 2011 Solarflare Communications Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, incorporated herein by reference.
8  */
9 
10 #include <linux/cpumask.h>
11 #include <linux/gfp.h>
12 #include <linux/slab.h>
13 
14 /**
15  * struct cpu_rmap - CPU affinity reverse-map
16  * @size: Number of objects to be reverse-mapped
17  * @used: Number of objects added
18  * @obj: Pointer to array of object pointers
19  * @near: For each CPU, the index and distance to the nearest object,
20  *      based on affinity masks
21  */
22 struct cpu_rmap {
23 	u16		size, used;
24 	void		**obj;
25 	struct {
26 		u16	index;
27 		u16	dist;
28 	}		near[0];
29 };
30 #define CPU_RMAP_DIST_INF 0xffff
31 
32 extern struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags);
33 
34 /**
35  * free_cpu_rmap - free CPU affinity reverse-map
36  * @rmap: Reverse-map allocated with alloc_cpu_rmap(), or %NULL
37  */
free_cpu_rmap(struct cpu_rmap * rmap)38 static inline void free_cpu_rmap(struct cpu_rmap *rmap)
39 {
40 	kfree(rmap);
41 }
42 
43 extern int cpu_rmap_add(struct cpu_rmap *rmap, void *obj);
44 extern int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
45 			   const struct cpumask *affinity);
46 
cpu_rmap_lookup_index(struct cpu_rmap * rmap,unsigned int cpu)47 static inline u16 cpu_rmap_lookup_index(struct cpu_rmap *rmap, unsigned int cpu)
48 {
49 	return rmap->near[cpu].index;
50 }
51 
cpu_rmap_lookup_obj(struct cpu_rmap * rmap,unsigned int cpu)52 static inline void *cpu_rmap_lookup_obj(struct cpu_rmap *rmap, unsigned int cpu)
53 {
54 	return rmap->obj[rmap->near[cpu].index];
55 }
56 
57 #ifdef CONFIG_GENERIC_HARDIRQS
58 
59 /**
60  * alloc_irq_cpu_rmap - allocate CPU affinity reverse-map for IRQs
61  * @size: Number of objects to be mapped
62  *
63  * Must be called in process context.
64  */
alloc_irq_cpu_rmap(unsigned int size)65 static inline struct cpu_rmap *alloc_irq_cpu_rmap(unsigned int size)
66 {
67 	return alloc_cpu_rmap(size, GFP_KERNEL);
68 }
69 extern void free_irq_cpu_rmap(struct cpu_rmap *rmap);
70 
71 extern int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq);
72 
73 #endif
74