Martin Roth | 239b5df | 2022-07-26 22:18:26 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 3 | #ifndef DEVICE_RESOURCE_H |
| 4 | #define DEVICE_RESOURCE_H |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 5 | |
Eric Biederman | 992cd00 | 2004-10-14 21:10:23 +0000 | [diff] [blame] | 6 | #include <stdint.h> |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 7 | #include <stddef.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 8 | |
| 9 | #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */ |
| 10 | |
| 11 | #define IORESOURCE_IO 0x00000100 /* Resource type */ |
| 12 | #define IORESOURCE_MEM 0x00000200 |
| 13 | #define IORESOURCE_IRQ 0x00000400 |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 14 | #define IORESOURCE_DRQ 0x00000800 |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 15 | |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 16 | #define IORESOURCE_TYPE_MASK (IORESOURCE_IO | IORESOURCE_MEM \ |
| 17 | | IORESOURCE_IRQ | IORESOURCE_DRQ) |
Kyösti Mälkki | fdc0a90 | 2015-03-26 20:04:38 +0200 | [diff] [blame] | 18 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 19 | #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */ |
| 20 | #define IORESOURCE_READONLY 0x00002000 |
| 21 | #define IORESOURCE_CACHEABLE 0x00004000 |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 22 | /* This resource filters all of the unclaimed transactions to the bus below. */ |
| 23 | #define IORESOURCE_SUBTRACTIVE 0x00040000 |
| 24 | /* The IO resource has a bus below it. */ |
| 25 | #define IORESOURCE_BRIDGE 0x00080000 |
Furquan Shaikh | 1bb05ef30 | 2020-05-15 17:33:52 -0700 | [diff] [blame] | 26 | /* This is a request to allocate resource about 4G boundary. */ |
| 27 | #define IORESOURCE_ABOVE_4G 0x00100000 |
Jonathan Zhang | e111de0 | 2021-04-21 10:40:31 -0700 | [diff] [blame] | 28 | /* The resource needs to be soft reserved in the coreboot table */ |
| 29 | #define IORESOURCE_SOFT_RESERVE 0x00200000 |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 30 | /* The resource needs to be reserved in the coreboot table */ |
| 31 | #define IORESOURCE_RESERVE 0x10000000 |
| 32 | /* The IO resource assignment has been stored in the device */ |
| 33 | #define IORESOURCE_STORED 0x20000000 |
| 34 | /* An IO resource that has been assigned a value */ |
| 35 | #define IORESOURCE_ASSIGNED 0x40000000 |
| 36 | /* An IO resource the allocator must not change */ |
| 37 | #define IORESOURCE_FIXED 0x80000000 |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 38 | |
Kyösti Mälkki | d2245bb | 2012-08-27 09:09:23 +0300 | [diff] [blame] | 39 | /* PCI specific resource bits (IORESOURCE_BITS) */ |
Tim Wawrzynczak | 8c93fed | 2022-01-13 16:45:07 -0700 | [diff] [blame] | 40 | #define IORESOURCE_PCI64 (1<<0) /* 64bit long pci resource */ |
| 41 | #define IORESOURCE_PCI_BRIDGE (1<<1) /* A bridge pci resource */ |
| 42 | #define IORESOURCE_PCIE_RESIZABLE_BAR (1<<2) /* A Resizable BAR */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 43 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 44 | typedef u64 resource_t; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 45 | struct resource { |
Eric Biederman | 992cd00 | 2004-10-14 21:10:23 +0000 | [diff] [blame] | 46 | resource_t base; /* Base address of the resource */ |
| 47 | resource_t size; /* Size of the resource */ |
| 48 | resource_t limit; /* Largest valid value base + size -1 */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 49 | DEVTREE_CONST struct resource *next; /* Next resource in the list */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 50 | unsigned long flags; /* Descriptions of the kind of resource */ |
| 51 | unsigned long index; /* Bus specific per device resource id */ |
Li-Ta Lo | e3c79ed | 2004-03-19 00:08:48 +0000 | [diff] [blame] | 52 | unsigned char align; /* Required alignment (log 2) of the resource */ |
| 53 | unsigned char gran; /* Granularity (log 2) of the resource */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 54 | /* Alignment must be >= the granularity of the resource */ |
| 55 | }; |
| 56 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 57 | /* Macros to generate index values for resources */ |
Lee Leahy | ae3fd34 | 2017-03-07 12:55:23 -0800 | [diff] [blame] | 58 | #define IOINDEX_SUBTRACTIVE(IDX, LINK) (0x10000000 + ((IDX) << 8) + LINK) |
Eric Biederman | 992cd00 | 2004-10-14 21:10:23 +0000 | [diff] [blame] | 59 | #define IOINDEX_SUBTRACTIVE_LINK(IDX) (IDX & 0xff) |
| 60 | |
Lee Leahy | ae3fd34 | 2017-03-07 12:55:23 -0800 | [diff] [blame] | 61 | #define IOINDEX(IDX, LINK) (((LINK) << 16) + IDX) |
Lee Leahy | 91d1e76 | 2017-03-07 14:31:19 -0800 | [diff] [blame] | 62 | #define IOINDEX_LINK(IDX) ((IDX & 0xf0000) >> 16) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 63 | #define IOINDEX_IDX(IDX) (IDX & 0xffff) |
| 64 | |
Eric Biederman | 992cd00 | 2004-10-14 21:10:23 +0000 | [diff] [blame] | 65 | /* Generic resource helper functions */ |
| 66 | struct device; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 67 | struct bus; |
Kyösti Mälkki | b228771 | 2021-06-14 11:29:51 +0300 | [diff] [blame] | 68 | void compact_resources(struct device *dev); |
| 69 | struct resource *probe_resource(const struct device *dev, unsigned int index); |
| 70 | struct resource *new_resource(struct device *dev, unsigned int index); |
| 71 | struct resource *find_resource(const struct device *dev, unsigned int index); |
| 72 | resource_t resource_end(const struct resource *resource); |
| 73 | resource_t resource_max(const struct resource *resource); |
| 74 | void report_resource_stored(struct device *dev, const struct resource *resource, |
| 75 | const char *comment); |
Eric Biederman | 992cd00 | 2004-10-14 21:10:23 +0000 | [diff] [blame] | 76 | |
Kyösti Mälkki | b228771 | 2021-06-14 11:29:51 +0300 | [diff] [blame] | 77 | typedef void (*resource_search_t)(void *gp, struct device *dev, struct resource *res); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 78 | |
Kyösti Mälkki | b228771 | 2021-06-14 11:29:51 +0300 | [diff] [blame] | 79 | void search_bus_resources(struct bus *bus, unsigned long type_mask, unsigned long type, |
| 80 | resource_search_t search, void *gp); |
| 81 | |
| 82 | void search_global_resources(unsigned long type_mask, unsigned long type, |
| 83 | resource_search_t search, void *gp); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 84 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 85 | #define RESOURCE_TYPE_MAX 20 |
Kyösti Mälkki | b228771 | 2021-06-14 11:29:51 +0300 | [diff] [blame] | 86 | const char *resource_type(const struct resource *resource); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 87 | |
Kyösti Mälkki | b228771 | 2021-06-14 11:29:51 +0300 | [diff] [blame] | 88 | static inline void *res2mmio(const struct resource *res, unsigned long offset, |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 89 | unsigned long mask) |
| 90 | { |
| 91 | return (void *)(uintptr_t)((res->base + offset) & ~mask); |
| 92 | } |
| 93 | |
Kyösti Mälkki | 68e6dc9 | 2021-06-14 00:40:22 +0300 | [diff] [blame] | 94 | void log_resource(const char *type, const struct device *dev, const struct resource *res, |
| 95 | const char *srcfile, const int line); |
| 96 | |
| 97 | #define LOG_RESOURCE(type, dev, res) \ |
| 98 | do { \ |
| 99 | if (CONFIG(DEBUG_RESOURCES) && (dev) && (res)) \ |
| 100 | log_resource(type, (dev), (res), __func__, __LINE__); \ |
| 101 | } while (0) |
| 102 | |
Furquan Shaikh | 6939574 | 2020-05-15 15:43:15 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Pick largest resource on the bus using the given mask and type. |
| 105 | * Params: |
| 106 | * bus = Bus from which the resource needs to picked from. |
| 107 | * result_res = If NULL, there was no previous resource picked on this bus, else it points to |
| 108 | * the last picked resource. |
| 109 | * type_mask = Mask to be applied when searching for resource |
| 110 | * type = Expected type for the resource |
| 111 | * |
| 112 | * Returns: |
| 113 | * If resource is found, returns the device and sets result_rest to point to the resource. Else |
| 114 | * returns NULL. |
| 115 | */ |
| 116 | const struct device *largest_resource(struct bus *bus, struct resource **result_res, |
| 117 | unsigned long type_mask, unsigned long type); |
| 118 | |
Furquan Shaikh | 6939574 | 2020-05-15 15:43:15 -0700 | [diff] [blame] | 119 | /* Compute and allocate resources. This is the main resource allocator entry point. */ |
| 120 | void allocate_resources(const struct device *root); |
| 121 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 122 | #endif /* DEVICE_RESOURCE_H */ |