| .. | .. |
|---|
| 5 | 5 | #include <linux/of_device.h> |
|---|
| 6 | 6 | #include <linux/of_address.h> |
|---|
| 7 | 7 | #include <linux/of_iommu.h> |
|---|
| 8 | | -#include <linux/dma-mapping.h> |
|---|
| 8 | +#include <linux/dma-direct.h> /* for bus_dma_region */ |
|---|
| 9 | +#include <linux/dma-map-ops.h> |
|---|
| 9 | 10 | #include <linux/init.h> |
|---|
| 10 | 11 | #include <linux/module.h> |
|---|
| 11 | 12 | #include <linux/mod_devicetable.h> |
|---|
| .. | .. |
|---|
| 17 | 18 | |
|---|
| 18 | 19 | /** |
|---|
| 19 | 20 | * of_match_device - Tell if a struct device matches an of_device_id list |
|---|
| 20 | | - * @ids: array of of device match structures to search in |
|---|
| 21 | + * @matches: array of of device match structures to search in |
|---|
| 21 | 22 | * @dev: the of device structure to match against |
|---|
| 22 | 23 | * |
|---|
| 23 | 24 | * Used by a driver to check whether an platform_device present in the |
|---|
| .. | .. |
|---|
| 78 | 79 | * @np: Pointer to OF node having DMA configuration |
|---|
| 79 | 80 | * @force_dma: Whether device is to be set up by of_dma_configure() even if |
|---|
| 80 | 81 | * DMA capability is not explicitly described by firmware. |
|---|
| 82 | + * @id: Optional const pointer value input id |
|---|
| 81 | 83 | * |
|---|
| 82 | 84 | * Try to get devices's DMA configuration from DT and update it |
|---|
| 83 | 85 | * accordingly. |
|---|
| .. | .. |
|---|
| 86 | 88 | * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events |
|---|
| 87 | 89 | * to fix up DMA configuration. |
|---|
| 88 | 90 | */ |
|---|
| 89 | | -int of_dma_configure(struct device *dev, struct device_node *np, bool force_dma) |
|---|
| 91 | +int of_dma_configure_id(struct device *dev, struct device_node *np, |
|---|
| 92 | + bool force_dma, const u32 *id) |
|---|
| 90 | 93 | { |
|---|
| 91 | | - u64 dma_addr, paddr, size = 0; |
|---|
| 92 | | - int ret; |
|---|
| 93 | | - bool coherent; |
|---|
| 94 | | - unsigned long offset; |
|---|
| 95 | 94 | const struct iommu_ops *iommu; |
|---|
| 96 | | - u64 mask; |
|---|
| 95 | + const struct bus_dma_region *map = NULL; |
|---|
| 96 | + u64 dma_start = 0; |
|---|
| 97 | + u64 mask, end, size = 0; |
|---|
| 98 | + bool coherent; |
|---|
| 99 | + int ret; |
|---|
| 97 | 100 | |
|---|
| 98 | | - ret = of_dma_get_range(np, &dma_addr, &paddr, &size); |
|---|
| 101 | + ret = of_dma_get_range(np, &map); |
|---|
| 99 | 102 | if (ret < 0) { |
|---|
| 100 | 103 | /* |
|---|
| 101 | 104 | * For legacy reasons, we have to assume some devices need |
|---|
| .. | .. |
|---|
| 104 | 107 | */ |
|---|
| 105 | 108 | if (!force_dma) |
|---|
| 106 | 109 | return ret == -ENODEV ? 0 : ret; |
|---|
| 107 | | - |
|---|
| 108 | | - dma_addr = offset = 0; |
|---|
| 109 | 110 | } else { |
|---|
| 110 | | - offset = PFN_DOWN(paddr - dma_addr); |
|---|
| 111 | + const struct bus_dma_region *r = map; |
|---|
| 112 | + u64 dma_end = 0; |
|---|
| 113 | + |
|---|
| 114 | + /* Determine the overall bounds of all DMA regions */ |
|---|
| 115 | + for (dma_start = ~0; r->size; r++) { |
|---|
| 116 | + /* Take lower and upper limits */ |
|---|
| 117 | + if (r->dma_start < dma_start) |
|---|
| 118 | + dma_start = r->dma_start; |
|---|
| 119 | + if (r->dma_start + r->size > dma_end) |
|---|
| 120 | + dma_end = r->dma_start + r->size; |
|---|
| 121 | + } |
|---|
| 122 | + size = dma_end - dma_start; |
|---|
| 111 | 123 | |
|---|
| 112 | 124 | /* |
|---|
| 113 | 125 | * Add a work around to treat the size as mask + 1 in case |
|---|
| 114 | 126 | * it is defined in DT as a mask. |
|---|
| 115 | 127 | */ |
|---|
| 116 | 128 | if (size & 1) { |
|---|
| 117 | | - dev_warn(dev, "Invalid size 0x%llx for dma-range\n", |
|---|
| 129 | + dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n", |
|---|
| 118 | 130 | size); |
|---|
| 119 | 131 | size = size + 1; |
|---|
| 120 | 132 | } |
|---|
| 121 | 133 | |
|---|
| 122 | 134 | if (!size) { |
|---|
| 123 | 135 | dev_err(dev, "Adjusted size 0x%llx invalid\n", size); |
|---|
| 136 | + kfree(map); |
|---|
| 124 | 137 | return -EINVAL; |
|---|
| 125 | 138 | } |
|---|
| 126 | | - dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset); |
|---|
| 127 | 139 | } |
|---|
| 128 | 140 | |
|---|
| 129 | 141 | /* |
|---|
| .. | .. |
|---|
| 142 | 154 | else if (!size) |
|---|
| 143 | 155 | size = 1ULL << 32; |
|---|
| 144 | 156 | |
|---|
| 145 | | - dev->dma_pfn_offset = offset; |
|---|
| 146 | | - |
|---|
| 147 | 157 | /* |
|---|
| 148 | 158 | * Limit coherent and dma mask based on size and default mask |
|---|
| 149 | 159 | * set by the driver. |
|---|
| 150 | 160 | */ |
|---|
| 151 | | - mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1); |
|---|
| 161 | + end = dma_start + size - 1; |
|---|
| 162 | + mask = DMA_BIT_MASK(ilog2(end) + 1); |
|---|
| 152 | 163 | dev->coherent_dma_mask &= mask; |
|---|
| 153 | 164 | *dev->dma_mask &= mask; |
|---|
| 154 | | - /* ...but only set bus mask if we found valid dma-ranges earlier */ |
|---|
| 155 | | - if (!ret) |
|---|
| 156 | | - dev->bus_dma_mask = mask; |
|---|
| 165 | + /* ...but only set bus limit and range map if we found valid dma-ranges earlier */ |
|---|
| 166 | + if (!ret) { |
|---|
| 167 | + dev->bus_dma_limit = end; |
|---|
| 168 | + dev->dma_range_map = map; |
|---|
| 169 | + } |
|---|
| 157 | 170 | |
|---|
| 158 | 171 | coherent = of_dma_is_coherent(np); |
|---|
| 159 | 172 | dev_dbg(dev, "device is%sdma coherent\n", |
|---|
| 160 | 173 | coherent ? " " : " not "); |
|---|
| 161 | 174 | |
|---|
| 162 | | - iommu = of_iommu_configure(dev, np); |
|---|
| 163 | | - if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER) |
|---|
| 175 | + iommu = of_iommu_configure(dev, np, id); |
|---|
| 176 | + if (PTR_ERR(iommu) == -EPROBE_DEFER) { |
|---|
| 177 | + /* Don't touch range map if it wasn't set from a valid dma-ranges */ |
|---|
| 178 | + if (!ret) |
|---|
| 179 | + dev->dma_range_map = NULL; |
|---|
| 180 | + kfree(map); |
|---|
| 164 | 181 | return -EPROBE_DEFER; |
|---|
| 182 | + } |
|---|
| 165 | 183 | |
|---|
| 166 | 184 | dev_dbg(dev, "device is%sbehind an iommu\n", |
|---|
| 167 | 185 | iommu ? " " : " not "); |
|---|
| 168 | 186 | |
|---|
| 169 | | - arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent); |
|---|
| 187 | + arch_setup_dma_ops(dev, dma_start, size, iommu, coherent); |
|---|
| 170 | 188 | |
|---|
| 171 | 189 | return 0; |
|---|
| 172 | 190 | } |
|---|
| 173 | | -EXPORT_SYMBOL_GPL(of_dma_configure); |
|---|
| 174 | | - |
|---|
| 175 | | -/** |
|---|
| 176 | | - * of_dma_deconfigure - Clean up DMA configuration |
|---|
| 177 | | - * @dev: Device for which to clean up DMA configuration |
|---|
| 178 | | - * |
|---|
| 179 | | - * Clean up all configuration performed by of_dma_configure_ops() and free all |
|---|
| 180 | | - * resources that have been allocated. |
|---|
| 181 | | - */ |
|---|
| 182 | | -void of_dma_deconfigure(struct device *dev) |
|---|
| 183 | | -{ |
|---|
| 184 | | - arch_teardown_dma_ops(dev); |
|---|
| 185 | | -} |
|---|
| 191 | +EXPORT_SYMBOL_GPL(of_dma_configure_id); |
|---|
| 186 | 192 | |
|---|
| 187 | 193 | int of_device_register(struct platform_device *pdev) |
|---|
| 188 | 194 | { |
|---|
| .. | .. |
|---|
| 223 | 229 | /* Name & Type */ |
|---|
| 224 | 230 | /* %p eats all alphanum characters, so %c must be used here */ |
|---|
| 225 | 231 | csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T', |
|---|
| 226 | | - dev->of_node->type); |
|---|
| 232 | + of_node_get_device_type(dev->of_node)); |
|---|
| 227 | 233 | tsize = csize; |
|---|
| 228 | 234 | len -= csize; |
|---|
| 229 | 235 | if (str) |
|---|
| .. | .. |
|---|
| 293 | 299 | */ |
|---|
| 294 | 300 | void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
|---|
| 295 | 301 | { |
|---|
| 296 | | - const char *compat; |
|---|
| 302 | + const char *compat, *type; |
|---|
| 297 | 303 | struct alias_prop *app; |
|---|
| 298 | 304 | struct property *p; |
|---|
| 299 | 305 | int seen = 0; |
|---|
| .. | .. |
|---|
| 303 | 309 | |
|---|
| 304 | 310 | add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node); |
|---|
| 305 | 311 | add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node); |
|---|
| 306 | | - if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0) |
|---|
| 307 | | - add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type); |
|---|
| 312 | + type = of_node_get_device_type(dev->of_node); |
|---|
| 313 | + if (type) |
|---|
| 314 | + add_uevent_var(env, "OF_TYPE=%s", type); |
|---|
| 308 | 315 | |
|---|
| 309 | 316 | /* Since the compatible field can contain pretty much anything |
|---|
| 310 | 317 | * it's not really legal to split it out with commas. We split it |
|---|