OSDN Git Service

PCI: keystone: Fix link training retries initiation
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / acpi / numa.c
1 /*
2  *  acpi_numa.c - ACPI NUMA support
3  *
4  *  Copyright (C) 2002 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
5  *
6  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19  *
20  */
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/acpi.h>
27 #include <linux/numa.h>
28 #include <linux/nodemask.h>
29 #include <linux/topology.h>
30
31 #define PREFIX "ACPI: "
32
33 #define ACPI_NUMA       0x80000000
34 #define _COMPONENT      ACPI_NUMA
35 ACPI_MODULE_NAME("numa");
36
37 static nodemask_t nodes_found_map = NODE_MASK_NONE;
38
39 /* maps to convert between proximity domain and logical node ID */
40 static int pxm_to_node_map[MAX_PXM_DOMAINS]
41                         = { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
42 static int node_to_pxm_map[MAX_NUMNODES]
43                         = { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
44
45 unsigned char acpi_srat_revision __initdata;
46
47 int pxm_to_node(int pxm)
48 {
49         if (pxm < 0)
50                 return NUMA_NO_NODE;
51         return pxm_to_node_map[pxm];
52 }
53
54 int node_to_pxm(int node)
55 {
56         if (node < 0)
57                 return PXM_INVAL;
58         return node_to_pxm_map[node];
59 }
60
61 static void __acpi_map_pxm_to_node(int pxm, int node)
62 {
63         if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
64                 pxm_to_node_map[pxm] = node;
65         if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
66                 node_to_pxm_map[node] = pxm;
67 }
68
69 int acpi_map_pxm_to_node(int pxm)
70 {
71         int node;
72
73         if (pxm < 0 || pxm >= MAX_PXM_DOMAINS)
74                 return NUMA_NO_NODE;
75
76         node = pxm_to_node_map[pxm];
77
78         if (node == NUMA_NO_NODE) {
79                 if (nodes_weight(nodes_found_map) >= MAX_NUMNODES)
80                         return NUMA_NO_NODE;
81                 node = first_unset_node(nodes_found_map);
82                 __acpi_map_pxm_to_node(pxm, node);
83                 node_set(node, nodes_found_map);
84         }
85
86         return node;
87 }
88
89 /**
90  * acpi_map_pxm_to_online_node - Map proximity ID to online node
91  * @pxm: ACPI proximity ID
92  *
93  * This is similar to acpi_map_pxm_to_node(), but always returns an online
94  * node.  When the mapped node from a given proximity ID is offline, it
95  * looks up the node distance table and returns the nearest online node.
96  *
97  * ACPI device drivers, which are called after the NUMA initialization has
98  * completed in the kernel, can call this interface to obtain their device
99  * NUMA topology from ACPI tables.  Such drivers do not have to deal with
100  * offline nodes.  A node may be offline when a device proximity ID is
101  * unique, SRAT memory entry does not exist, or NUMA is disabled, ex.
102  * "numa=off" on x86.
103  */
104 int acpi_map_pxm_to_online_node(int pxm)
105 {
106         int node, min_node;
107
108         node = acpi_map_pxm_to_node(pxm);
109
110         if (node == NUMA_NO_NODE)
111                 node = 0;
112
113         min_node = node;
114         if (!node_online(node)) {
115                 int min_dist = INT_MAX, dist, n;
116
117                 for_each_online_node(n) {
118                         dist = node_distance(node, n);
119                         if (dist < min_dist) {
120                                 min_dist = dist;
121                                 min_node = n;
122                         }
123                 }
124         }
125
126         return min_node;
127 }
128 EXPORT_SYMBOL(acpi_map_pxm_to_online_node);
129
130 static void __init
131 acpi_table_print_srat_entry(struct acpi_subtable_header *header)
132 {
133
134         ACPI_FUNCTION_NAME("acpi_table_print_srat_entry");
135
136         if (!header)
137                 return;
138
139         switch (header->type) {
140
141         case ACPI_SRAT_TYPE_CPU_AFFINITY:
142 #ifdef ACPI_DEBUG_OUTPUT
143                 {
144                         struct acpi_srat_cpu_affinity *p =
145                             (struct acpi_srat_cpu_affinity *)header;
146                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
147                                           "SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
148                                           p->apic_id, p->local_sapic_eid,
149                                           p->proximity_domain_lo,
150                                           (p->flags & ACPI_SRAT_CPU_ENABLED)?
151                                           "enabled" : "disabled"));
152                 }
153 #endif                          /* ACPI_DEBUG_OUTPUT */
154                 break;
155
156         case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
157 #ifdef ACPI_DEBUG_OUTPUT
158                 {
159                         struct acpi_srat_mem_affinity *p =
160                             (struct acpi_srat_mem_affinity *)header;
161                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
162                                           "SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s%s\n",
163                                           (unsigned long)p->base_address,
164                                           (unsigned long)p->length,
165                                           p->proximity_domain,
166                                           (p->flags & ACPI_SRAT_MEM_ENABLED)?
167                                           "enabled" : "disabled",
168                                           (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)?
169                                           " hot-pluggable" : "",
170                                           (p->flags & ACPI_SRAT_MEM_NON_VOLATILE)?
171                                           " non-volatile" : ""));
172                 }
173 #endif                          /* ACPI_DEBUG_OUTPUT */
174                 break;
175
176         case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
177 #ifdef ACPI_DEBUG_OUTPUT
178                 {
179                         struct acpi_srat_x2apic_cpu_affinity *p =
180                             (struct acpi_srat_x2apic_cpu_affinity *)header;
181                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
182                                           "SRAT Processor (x2apicid[0x%08x]) in"
183                                           " proximity domain %d %s\n",
184                                           p->apic_id,
185                                           p->proximity_domain,
186                                           (p->flags & ACPI_SRAT_CPU_ENABLED) ?
187                                           "enabled" : "disabled"));
188                 }
189 #endif                          /* ACPI_DEBUG_OUTPUT */
190                 break;
191         default:
192                 printk(KERN_WARNING PREFIX
193                        "Found unsupported SRAT entry (type = 0x%x)\n",
194                        header->type);
195                 break;
196         }
197 }
198
199 /*
200  * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
201  * up the NUMA heuristics which wants the local node to have a smaller
202  * distance than the others.
203  * Do some quick checks here and only use the SLIT if it passes.
204  */
205 static int __init slit_valid(struct acpi_table_slit *slit)
206 {
207         int i, j;
208         int d = slit->locality_count;
209         for (i = 0; i < d; i++) {
210                 for (j = 0; j < d; j++)  {
211                         u8 val = slit->entry[d*i + j];
212                         if (i == j) {
213                                 if (val != LOCAL_DISTANCE)
214                                         return 0;
215                         } else if (val <= LOCAL_DISTANCE)
216                                 return 0;
217                 }
218         }
219         return 1;
220 }
221
222 static int __init acpi_parse_slit(struct acpi_table_header *table)
223 {
224         struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
225
226         if (!slit_valid(slit)) {
227                 printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
228                 return -EINVAL;
229         }
230         acpi_numa_slit_init(slit);
231
232         return 0;
233 }
234
235 void __init __weak
236 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
237 {
238         printk(KERN_WARNING PREFIX
239                "Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
240         return;
241 }
242
243
244 static int __init
245 acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
246                            const unsigned long end)
247 {
248         struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
249
250         processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
251         if (!processor_affinity)
252                 return -EINVAL;
253
254         acpi_table_print_srat_entry(header);
255
256         /* let architecture-dependent part to do it */
257         acpi_numa_x2apic_affinity_init(processor_affinity);
258
259         return 0;
260 }
261
262 static int __init
263 acpi_parse_processor_affinity(struct acpi_subtable_header *header,
264                               const unsigned long end)
265 {
266         struct acpi_srat_cpu_affinity *processor_affinity;
267
268         processor_affinity = (struct acpi_srat_cpu_affinity *)header;
269         if (!processor_affinity)
270                 return -EINVAL;
271
272         acpi_table_print_srat_entry(header);
273
274         /* let architecture-dependent part to do it */
275         acpi_numa_processor_affinity_init(processor_affinity);
276
277         return 0;
278 }
279
280 static int __initdata parsed_numa_memblks;
281
282 static int __init
283 acpi_parse_memory_affinity(struct acpi_subtable_header * header,
284                            const unsigned long end)
285 {
286         struct acpi_srat_mem_affinity *memory_affinity;
287
288         memory_affinity = (struct acpi_srat_mem_affinity *)header;
289         if (!memory_affinity)
290                 return -EINVAL;
291
292         acpi_table_print_srat_entry(header);
293
294         /* let architecture-dependent part to do it */
295         if (!acpi_numa_memory_affinity_init(memory_affinity))
296                 parsed_numa_memblks++;
297         return 0;
298 }
299
300 static int __init acpi_parse_srat(struct acpi_table_header *table)
301 {
302         struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
303
304         acpi_srat_revision = srat->header.revision;
305
306         /* Real work done in acpi_table_parse_srat below. */
307
308         return 0;
309 }
310
311 static int __init
312 acpi_table_parse_srat(enum acpi_srat_type id,
313                       acpi_tbl_entry_handler handler, unsigned int max_entries)
314 {
315         return acpi_table_parse_entries(ACPI_SIG_SRAT,
316                                             sizeof(struct acpi_table_srat), id,
317                                             handler, max_entries);
318 }
319
320 int __init acpi_numa_init(void)
321 {
322         int cnt = 0;
323
324         /*
325          * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
326          * SRAT cpu entries could have different order with that in MADT.
327          * So go over all cpu entries in SRAT to get apicid to node mapping.
328          */
329
330         /* SRAT: Static Resource Affinity Table */
331         if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
332                 struct acpi_subtable_proc srat_proc[2];
333
334                 memset(srat_proc, 0, sizeof(srat_proc));
335                 srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
336                 srat_proc[0].handler = acpi_parse_processor_affinity;
337                 srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
338                 srat_proc[1].handler = acpi_parse_x2apic_affinity;
339
340                 acpi_table_parse_entries_array(ACPI_SIG_SRAT,
341                                         sizeof(struct acpi_table_srat),
342                                         srat_proc, ARRAY_SIZE(srat_proc), 0);
343
344                 cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
345                                             acpi_parse_memory_affinity,
346                                             NR_NODE_MEMBLKS);
347         }
348
349         /* SLIT: System Locality Information Table */
350         acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
351
352         acpi_numa_arch_fixup();
353
354         if (cnt < 0)
355                 return cnt;
356         else if (!parsed_numa_memblks)
357                 return -ENOENT;
358         return 0;
359 }
360
361 static int acpi_get_pxm(acpi_handle h)
362 {
363         unsigned long long pxm;
364         acpi_status status;
365         acpi_handle handle;
366         acpi_handle phandle = h;
367
368         do {
369                 handle = phandle;
370                 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
371                 if (ACPI_SUCCESS(status))
372                         return pxm;
373                 status = acpi_get_parent(handle, &phandle);
374         } while (ACPI_SUCCESS(status));
375         return -1;
376 }
377
378 int acpi_get_node(acpi_handle handle)
379 {
380         int pxm;
381
382         pxm = acpi_get_pxm(handle);
383
384         return acpi_map_pxm_to_node(pxm);
385 }
386 EXPORT_SYMBOL(acpi_get_node);