15d22db05054526a95c233ea7f9de1c5510c4b42
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / resource.c
1 /*
2  * drivers/acpi/resource.c - ACPI device resources interpretation.
3  *
4  * Copyright (C) 2012, Intel Corp.
5  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as published
11  *  by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  *
18  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19  */
20
21 #include <linux/acpi.h>
22 #include <linux/device.h>
23 #include <linux/export.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26
27 #ifdef CONFIG_X86
28 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
29 #else
30 #define valid_IRQ(i) (true)
31 #endif
32
33 static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
34 {
35         u64 reslen = end - start + 1;
36
37         /*
38          * CHECKME: len might be required to check versus a minimum
39          * length as well. 1 for io is fine, but for memory it does
40          * not make any sense at all.
41          * Note: some BIOSes report incorrect length for ACPI address space
42          * descriptor, so remove check of 'reslen == len' to avoid regression.
43          */
44         if (len && reslen && start <= end)
45                 return true;
46
47         pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
48                 io ? "io" : "mem", start, end, len);
49
50         return false;
51 }
52
53 static void acpi_dev_memresource_flags(struct resource *res, u64 len,
54                                        u8 write_protect)
55 {
56         res->flags = IORESOURCE_MEM;
57
58         if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
59                 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
60
61         if (write_protect == ACPI_READ_WRITE_MEMORY)
62                 res->flags |= IORESOURCE_MEM_WRITEABLE;
63 }
64
65 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
66                                      u8 write_protect)
67 {
68         res->start = start;
69         res->end = start + len - 1;
70         acpi_dev_memresource_flags(res, len, write_protect);
71 }
72
73 /**
74  * acpi_dev_resource_memory - Extract ACPI memory resource information.
75  * @ares: Input ACPI resource object.
76  * @res: Output generic resource object.
77  *
78  * Check if the given ACPI resource object represents a memory resource and
79  * if that's the case, use the information in it to populate the generic
80  * resource object pointed to by @res.
81  *
82  * Return:
83  * 1) false with res->flags setting to zero: not the expected resource type
84  * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
85  * 3) true: valid assigned resource
86  */
87 bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
88 {
89         struct acpi_resource_memory24 *memory24;
90         struct acpi_resource_memory32 *memory32;
91         struct acpi_resource_fixed_memory32 *fixed_memory32;
92
93         switch (ares->type) {
94         case ACPI_RESOURCE_TYPE_MEMORY24:
95                 memory24 = &ares->data.memory24;
96                 acpi_dev_get_memresource(res, memory24->minimum << 8,
97                                          memory24->address_length << 8,
98                                          memory24->write_protect);
99                 break;
100         case ACPI_RESOURCE_TYPE_MEMORY32:
101                 memory32 = &ares->data.memory32;
102                 acpi_dev_get_memresource(res, memory32->minimum,
103                                          memory32->address_length,
104                                          memory32->write_protect);
105                 break;
106         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
107                 fixed_memory32 = &ares->data.fixed_memory32;
108                 acpi_dev_get_memresource(res, fixed_memory32->address,
109                                          fixed_memory32->address_length,
110                                          fixed_memory32->write_protect);
111                 break;
112         default:
113                 res->flags = 0;
114                 return false;
115         }
116
117         return !(res->flags & IORESOURCE_DISABLED);
118 }
119 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
120
121 static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
122                                       u8 io_decode)
123 {
124         res->flags = IORESOURCE_IO;
125
126         if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
127                 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
128
129         if (res->end >= 0x10003)
130                 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
131
132         if (io_decode == ACPI_DECODE_16)
133                 res->flags |= IORESOURCE_IO_16BIT_ADDR;
134 }
135
136 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
137                                     u8 io_decode)
138 {
139         res->start = start;
140         res->end = start + len - 1;
141         acpi_dev_ioresource_flags(res, len, io_decode);
142 }
143
144 /**
145  * acpi_dev_resource_io - Extract ACPI I/O resource information.
146  * @ares: Input ACPI resource object.
147  * @res: Output generic resource object.
148  *
149  * Check if the given ACPI resource object represents an I/O resource and
150  * if that's the case, use the information in it to populate the generic
151  * resource object pointed to by @res.
152  *
153  * Return:
154  * 1) false with res->flags setting to zero: not the expected resource type
155  * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
156  * 3) true: valid assigned resource
157  */
158 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
159 {
160         struct acpi_resource_io *io;
161         struct acpi_resource_fixed_io *fixed_io;
162
163         switch (ares->type) {
164         case ACPI_RESOURCE_TYPE_IO:
165                 io = &ares->data.io;
166                 acpi_dev_get_ioresource(res, io->minimum,
167                                         io->address_length,
168                                         io->io_decode);
169                 break;
170         case ACPI_RESOURCE_TYPE_FIXED_IO:
171                 fixed_io = &ares->data.fixed_io;
172                 acpi_dev_get_ioresource(res, fixed_io->address,
173                                         fixed_io->address_length,
174                                         ACPI_DECODE_10);
175                 break;
176         default:
177                 res->flags = 0;
178                 return false;
179         }
180
181         return !(res->flags & IORESOURCE_DISABLED);
182 }
183 EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
184
185 static bool acpi_decode_space(struct resource_win *win,
186                               struct acpi_resource_address *addr,
187                               struct acpi_address64_attribute *attr)
188 {
189         u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
190         bool wp = addr->info.mem.write_protect;
191         u64 len = attr->address_length;
192         u64 start, end, offset = 0;
193         struct resource *res = &win->res;
194
195         /*
196          * Filter out invalid descriptor according to ACPI Spec 5.0, section
197          * 6.4.3.5 Address Space Resource Descriptors.
198          */
199         if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
200             (addr->min_address_fixed && addr->max_address_fixed && !len))
201                 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
202                          addr->min_address_fixed, addr->max_address_fixed, len);
203
204         /*
205          * For bridges that translate addresses across the bridge,
206          * translation_offset is the offset that must be added to the
207          * address on the secondary side to obtain the address on the
208          * primary side. Non-bridge devices must list 0 for all Address
209          * Translation offset bits.
210          */
211         if (addr->producer_consumer == ACPI_PRODUCER)
212                 offset = attr->translation_offset;
213         else if (attr->translation_offset)
214                 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
215                          attr->translation_offset);
216         start = attr->minimum + offset;
217         end = attr->maximum + offset;
218
219         win->offset = offset;
220         res->start = start;
221         res->end = end;
222         if (sizeof(resource_size_t) < sizeof(u64) &&
223             (offset != win->offset || start != res->start || end != res->end)) {
224                 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
225                         attr->minimum, attr->maximum);
226                 return false;
227         }
228
229         switch (addr->resource_type) {
230         case ACPI_MEMORY_RANGE:
231                 acpi_dev_memresource_flags(res, len, wp);
232                 break;
233         case ACPI_IO_RANGE:
234                 acpi_dev_ioresource_flags(res, len, iodec);
235                 break;
236         case ACPI_BUS_NUMBER_RANGE:
237                 res->flags = IORESOURCE_BUS;
238                 break;
239         default:
240                 return false;
241         }
242
243         if (addr->producer_consumer == ACPI_PRODUCER)
244                 res->flags |= IORESOURCE_WINDOW;
245
246         if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
247                 res->flags |= IORESOURCE_PREFETCH;
248
249         return !(res->flags & IORESOURCE_DISABLED);
250 }
251
252 /**
253  * acpi_dev_resource_address_space - Extract ACPI address space information.
254  * @ares: Input ACPI resource object.
255  * @win: Output generic resource object.
256  *
257  * Check if the given ACPI resource object represents an address space resource
258  * and if that's the case, use the information in it to populate the generic
259  * resource object pointed to by @win.
260  *
261  * Return:
262  * 1) false with win->res.flags setting to zero: not the expected resource type
263  * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
264  *    resource
265  * 3) true: valid assigned resource
266  */
267 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
268                                      struct resource_win *win)
269 {
270         struct acpi_resource_address64 addr;
271
272         win->res.flags = 0;
273         if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
274                 return false;
275
276         return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
277                                  &addr.address);
278 }
279 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
280
281 /**
282  * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
283  * @ares: Input ACPI resource object.
284  * @win: Output generic resource object.
285  *
286  * Check if the given ACPI resource object represents an extended address space
287  * resource and if that's the case, use the information in it to populate the
288  * generic resource object pointed to by @win.
289  *
290  * Return:
291  * 1) false with win->res.flags setting to zero: not the expected resource type
292  * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
293  *    resource
294  * 3) true: valid assigned resource
295  */
296 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
297                                          struct resource_win *win)
298 {
299         struct acpi_resource_extended_address64 *ext_addr;
300
301         win->res.flags = 0;
302         if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
303                 return false;
304
305         ext_addr = &ares->data.ext_address64;
306
307         return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
308                                  &ext_addr->address);
309 }
310 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
311
312 /**
313  * acpi_dev_irq_flags - Determine IRQ resource flags.
314  * @triggering: Triggering type as provided by ACPI.
315  * @polarity: Interrupt polarity as provided by ACPI.
316  * @shareable: Whether or not the interrupt is shareable.
317  */
318 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
319 {
320         unsigned long flags;
321
322         if (triggering == ACPI_LEVEL_SENSITIVE)
323                 flags = polarity == ACPI_ACTIVE_LOW ?
324                         IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
325         else
326                 flags = polarity == ACPI_ACTIVE_LOW ?
327                         IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
328
329         if (shareable == ACPI_SHARED)
330                 flags |= IORESOURCE_IRQ_SHAREABLE;
331
332         return flags | IORESOURCE_IRQ;
333 }
334 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
335
336 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
337 {
338         res->start = gsi;
339         res->end = gsi;
340         res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
341 }
342
343 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
344                                      u8 triggering, u8 polarity, u8 shareable,
345                                      bool legacy)
346 {
347         int irq, p, t;
348
349         if (!valid_IRQ(gsi)) {
350                 acpi_dev_irqresource_disabled(res, gsi);
351                 return;
352         }
353
354         /*
355          * In IO-APIC mode, use overrided attribute. Two reasons:
356          * 1. BIOS bug in DSDT
357          * 2. BIOS uses IO-APIC mode Interrupt Source Override
358          *
359          * We do this only if we are dealing with IRQ() or IRQNoFlags()
360          * resource (the legacy ISA resources). With modern ACPI 5 devices
361          * using extended IRQ descriptors we take the IRQ configuration
362          * from _CRS directly.
363          */
364         if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
365                 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
366                 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
367
368                 if (triggering != trig || polarity != pol) {
369                         pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
370                                    t ? "level" : "edge", p ? "low" : "high");
371                         triggering = trig;
372                         polarity = pol;
373                 }
374         }
375
376         res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
377         irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
378         if (irq >= 0) {
379                 res->start = irq;
380                 res->end = irq;
381         } else {
382                 acpi_dev_irqresource_disabled(res, gsi);
383         }
384 }
385
386 /**
387  * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
388  * @ares: Input ACPI resource object.
389  * @index: Index into the array of GSIs represented by the resource.
390  * @res: Output generic resource object.
391  *
392  * Check if the given ACPI resource object represents an interrupt resource
393  * and @index does not exceed the resource's interrupt count (true is returned
394  * in that case regardless of the results of the other checks)).  If that's the
395  * case, register the GSI corresponding to @index from the array of interrupts
396  * represented by the resource and populate the generic resource object pointed
397  * to by @res accordingly.  If the registration of the GSI is not successful,
398  * IORESOURCE_DISABLED will be set it that object's flags.
399  *
400  * Return:
401  * 1) false with res->flags setting to zero: not the expected resource type
402  * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
403  * 3) true: valid assigned resource
404  */
405 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
406                                  struct resource *res)
407 {
408         struct acpi_resource_irq *irq;
409         struct acpi_resource_extended_irq *ext_irq;
410
411         switch (ares->type) {
412         case ACPI_RESOURCE_TYPE_IRQ:
413                 /*
414                  * Per spec, only one interrupt per descriptor is allowed in
415                  * _CRS, but some firmware violates this, so parse them all.
416                  */
417                 irq = &ares->data.irq;
418                 if (index >= irq->interrupt_count) {
419                         acpi_dev_irqresource_disabled(res, 0);
420                         return false;
421                 }
422                 acpi_dev_get_irqresource(res, irq->interrupts[index],
423                                          irq->triggering, irq->polarity,
424                                          irq->sharable, true);
425                 break;
426         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
427                 ext_irq = &ares->data.extended_irq;
428                 if (index >= ext_irq->interrupt_count) {
429                         acpi_dev_irqresource_disabled(res, 0);
430                         return false;
431                 }
432                 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
433                                          ext_irq->triggering, ext_irq->polarity,
434                                          ext_irq->sharable, false);
435                 break;
436         default:
437                 res->flags = 0;
438                 return false;
439         }
440
441         return true;
442 }
443 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
444
445 /**
446  * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
447  * @list: The head of the resource list to free.
448  */
449 void acpi_dev_free_resource_list(struct list_head *list)
450 {
451         resource_list_free(list);
452 }
453 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
454
455 struct res_proc_context {
456         struct list_head *list;
457         int (*preproc)(struct acpi_resource *, void *);
458         void *preproc_data;
459         int count;
460         int error;
461 };
462
463 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
464                                                struct res_proc_context *c)
465 {
466         struct resource_entry *rentry;
467
468         rentry = resource_list_create_entry(NULL, 0);
469         if (!rentry) {
470                 c->error = -ENOMEM;
471                 return AE_NO_MEMORY;
472         }
473         *rentry->res = win->res;
474         rentry->offset = win->offset;
475         resource_list_add_tail(rentry, c->list);
476         c->count++;
477         return AE_OK;
478 }
479
480 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
481                                              void *context)
482 {
483         struct res_proc_context *c = context;
484         struct resource_win win;
485         struct resource *res = &win.res;
486         int i;
487
488         if (c->preproc) {
489                 int ret;
490
491                 ret = c->preproc(ares, c->preproc_data);
492                 if (ret < 0) {
493                         c->error = ret;
494                         return AE_CTRL_TERMINATE;
495                 } else if (ret > 0) {
496                         return AE_OK;
497                 }
498         }
499
500         memset(&win, 0, sizeof(win));
501
502         if (acpi_dev_resource_memory(ares, res)
503             || acpi_dev_resource_io(ares, res)
504             || acpi_dev_resource_address_space(ares, &win)
505             || acpi_dev_resource_ext_address_space(ares, &win))
506                 return acpi_dev_new_resource_entry(&win, c);
507
508         for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
509                 acpi_status status;
510
511                 status = acpi_dev_new_resource_entry(&win, c);
512                 if (ACPI_FAILURE(status))
513                         return status;
514         }
515
516         return AE_OK;
517 }
518
519 /**
520  * acpi_dev_get_resources - Get current resources of a device.
521  * @adev: ACPI device node to get the resources for.
522  * @list: Head of the resultant list of resources (must be empty).
523  * @preproc: The caller's preprocessing routine.
524  * @preproc_data: Pointer passed to the caller's preprocessing routine.
525  *
526  * Evaluate the _CRS method for the given device node and process its output by
527  * (1) executing the @preproc() rountine provided by the caller, passing the
528  * resource pointer and @preproc_data to it as arguments, for each ACPI resource
529  * returned and (2) converting all of the returned ACPI resources into struct
530  * resource objects if possible.  If the return value of @preproc() in step (1)
531  * is different from 0, step (2) is not applied to the given ACPI resource and
532  * if that value is negative, the whole processing is aborted and that value is
533  * returned as the final error code.
534  *
535  * The resultant struct resource objects are put on the list pointed to by
536  * @list, that must be empty initially, as members of struct resource_entry
537  * objects.  Callers of this routine should use %acpi_dev_free_resource_list() to
538  * free that list.
539  *
540  * The number of resources in the output list is returned on success, an error
541  * code reflecting the error condition is returned otherwise.
542  */
543 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
544                            int (*preproc)(struct acpi_resource *, void *),
545                            void *preproc_data)
546 {
547         struct res_proc_context c;
548         acpi_status status;
549
550         if (!adev || !adev->handle || !list_empty(list))
551                 return -EINVAL;
552
553         if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
554                 return 0;
555
556         c.list = list;
557         c.preproc = preproc;
558         c.preproc_data = preproc_data;
559         c.count = 0;
560         c.error = 0;
561         status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
562                                      acpi_dev_process_resource, &c);
563         if (ACPI_FAILURE(status)) {
564                 acpi_dev_free_resource_list(list);
565                 return c.error ? c.error : -EIO;
566         }
567
568         return c.count;
569 }
570 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
571
572 /**
573  * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
574  *                                 types
575  * @ares: Input ACPI resource object.
576  * @types: Valid resource types of IORESOURCE_XXX
577  *
578  * This is a helper function to support acpi_dev_get_resources(), which filters
579  * ACPI resource objects according to resource types.
580  */
581 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
582                                   unsigned long types)
583 {
584         unsigned long type = 0;
585
586         switch (ares->type) {
587         case ACPI_RESOURCE_TYPE_MEMORY24:
588         case ACPI_RESOURCE_TYPE_MEMORY32:
589         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
590                 type = IORESOURCE_MEM;
591                 break;
592         case ACPI_RESOURCE_TYPE_IO:
593         case ACPI_RESOURCE_TYPE_FIXED_IO:
594                 type = IORESOURCE_IO;
595                 break;
596         case ACPI_RESOURCE_TYPE_IRQ:
597         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
598                 type = IORESOURCE_IRQ;
599                 break;
600         case ACPI_RESOURCE_TYPE_DMA:
601         case ACPI_RESOURCE_TYPE_FIXED_DMA:
602                 type = IORESOURCE_DMA;
603                 break;
604         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
605                 type = IORESOURCE_REG;
606                 break;
607         case ACPI_RESOURCE_TYPE_ADDRESS16:
608         case ACPI_RESOURCE_TYPE_ADDRESS32:
609         case ACPI_RESOURCE_TYPE_ADDRESS64:
610         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
611                 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
612                         type = IORESOURCE_MEM;
613                 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
614                         type = IORESOURCE_IO;
615                 else if (ares->data.address.resource_type ==
616                          ACPI_BUS_NUMBER_RANGE)
617                         type = IORESOURCE_BUS;
618                 break;
619         default:
620                 break;
621         }
622
623         return (type & types) ? 0 : 1;
624 }
625 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);