1 #include <linux/string.h> 2 #include <linux/kernel.h> 3 #include <linux/of.h> 4 #include <linux/of_device.h> 5 #include <linux/of_address.h> 6 #include <linux/of_iommu.h> 7 #include <linux/dma-mapping.h> 8 #include <linux/init.h> 9 #include <linux/module.h> 10 #include <linux/mod_devicetable.h> 11 #include <linux/slab.h> 12 #include <linux/platform_device.h> 13 14 #include <asm/errno.h> 15 #include "of_private.h" 16 17 /** 18 * of_match_device - Tell if a struct device matches an of_device_id list 19 * @ids: array of of device match structures to search in 20 * @dev: the of device structure to match against 21 * 22 * Used by a driver to check whether an platform_device present in the 23 * system is in its list of supported devices. 24 */ 25 const struct of_device_id *of_match_device(const struct of_device_id *matches, 26 const struct device *dev) 27 { 28 if ((!matches) || (!dev->of_node)) 29 return NULL; 30 return of_match_node(matches, dev->of_node); 31 } 32 EXPORT_SYMBOL(of_match_device); 33 34 struct platform_device *of_dev_get(struct platform_device *dev) 35 { 36 struct device *tmp; 37 38 if (!dev) 39 return NULL; 40 tmp = get_device(&dev->dev); 41 if (tmp) 42 return to_platform_device(tmp); 43 else 44 return NULL; 45 } 46 EXPORT_SYMBOL(of_dev_get); 47 48 void of_dev_put(struct platform_device *dev) 49 { 50 if (dev) 51 put_device(&dev->dev); 52 } 53 EXPORT_SYMBOL(of_dev_put); 54 55 int of_device_add(struct platform_device *ofdev) 56 { 57 BUG_ON(ofdev->dev.of_node == NULL); 58 59 /* name and id have to be set so that the platform bus doesn't get 60 * confused on matching */ 61 ofdev->name = dev_name(&ofdev->dev); 62 ofdev->id = PLATFORM_DEVID_NONE; 63 64 /* 65 * If this device has not binding numa node in devicetree, that is 66 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this 67 * device is on the same node as the parent. 68 */ 69 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node)); 70 71 return device_add(&ofdev->dev); 72 } 73 74 /** 75 * of_dma_configure - Setup DMA configuration 76 * @dev: Device to apply DMA configuration 77 * @np: Pointer to OF node having DMA configuration 78 * 79 * Try to get devices's DMA configuration from DT and update it 80 * accordingly. 81 * 82 * If platform code needs to use its own special DMA configuration, it 83 * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events 84 * to fix up DMA configuration. 85 */ 86 int of_dma_configure(struct device *dev, struct device_node *np) 87 { 88 u64 dma_addr, paddr, size = 0; 89 int ret; 90 bool coherent; 91 unsigned long offset; 92 const struct iommu_ops *iommu; 93 u64 mask; 94 95 ret = of_dma_get_range(np, &dma_addr, &paddr, &size); 96 if (ret < 0) { 97 /* 98 * For legacy reasons, we have to assume some devices need 99 * DMA configuration regardless of whether "dma-ranges" is 100 * correctly specified or not. 101 */ 102 if (!dev->bus->force_dma) 103 return ret == -ENODEV ? 0 : ret; 104 105 dma_addr = offset = 0; 106 } else { 107 offset = PFN_DOWN(paddr - dma_addr); 108 109 /* 110 * Add a work around to treat the size as mask + 1 in case 111 * it is defined in DT as a mask. 112 */ 113 if (size & 1) { 114 dev_warn(dev, "Invalid size 0x%llx for dma-range\n", 115 size); 116 size = size + 1; 117 } 118 119 if (!size) { 120 dev_err(dev, "Adjusted size 0x%llx invalid\n", size); 121 return -EINVAL; 122 } 123 dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset); 124 } 125 126 /* 127 * Set default coherent_dma_mask to 32 bit. Drivers are expected to 128 * setup the correct supported mask. 129 */ 130 if (!dev->coherent_dma_mask) 131 dev->coherent_dma_mask = DMA_BIT_MASK(32); 132 /* 133 * Set it to coherent_dma_mask by default if the architecture 134 * code has not set it. 135 */ 136 if (!dev->dma_mask) 137 dev->dma_mask = &dev->coherent_dma_mask; 138 139 if (!size) 140 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); 141 142 dev->dma_pfn_offset = offset; 143 144 /* 145 * Limit coherent and dma mask based on size and default mask 146 * set by the driver. 147 */ 148 mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1); 149 dev->coherent_dma_mask &= mask; 150 *dev->dma_mask &= mask; 151 152 coherent = of_dma_is_coherent(np); 153 dev_dbg(dev, "device is%sdma coherent\n", 154 coherent ? " " : " not "); 155 156 iommu = of_iommu_configure(dev, np); 157 if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER) 158 return -EPROBE_DEFER; 159 160 dev_dbg(dev, "device is%sbehind an iommu\n", 161 iommu ? " " : " not "); 162 163 arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent); 164 165 return 0; 166 } 167 EXPORT_SYMBOL_GPL(of_dma_configure); 168 169 /** 170 * of_dma_deconfigure - Clean up DMA configuration 171 * @dev: Device for which to clean up DMA configuration 172 * 173 * Clean up all configuration performed by of_dma_configure_ops() and free all 174 * resources that have been allocated. 175 */ 176 void of_dma_deconfigure(struct device *dev) 177 { 178 arch_teardown_dma_ops(dev); 179 } 180 181 int of_device_register(struct platform_device *pdev) 182 { 183 device_initialize(&pdev->dev); 184 return of_device_add(pdev); 185 } 186 EXPORT_SYMBOL(of_device_register); 187 188 void of_device_unregister(struct platform_device *ofdev) 189 { 190 device_unregister(&ofdev->dev); 191 } 192 EXPORT_SYMBOL(of_device_unregister); 193 194 const void *of_device_get_match_data(const struct device *dev) 195 { 196 const struct of_device_id *match; 197 198 match = of_match_device(dev->driver->of_match_table, dev); 199 if (!match) 200 return NULL; 201 202 return match->data; 203 } 204 EXPORT_SYMBOL(of_device_get_match_data); 205 206 static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len) 207 { 208 const char *compat; 209 char *c; 210 struct property *p; 211 ssize_t csize; 212 ssize_t tsize; 213 214 if ((!dev) || (!dev->of_node)) 215 return -ENODEV; 216 217 /* Name & Type */ 218 csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name, 219 dev->of_node->type); 220 tsize = csize; 221 len -= csize; 222 if (str) 223 str += csize; 224 225 of_property_for_each_string(dev->of_node, "compatible", p, compat) { 226 csize = strlen(compat) + 1; 227 tsize += csize; 228 if (csize > len) 229 continue; 230 231 csize = snprintf(str, len, "C%s", compat); 232 for (c = str; c; ) { 233 c = strchr(c, ' '); 234 if (c) 235 *c++ = '_'; 236 } 237 len -= csize; 238 str += csize; 239 } 240 241 return tsize; 242 } 243 244 int of_device_request_module(struct device *dev) 245 { 246 char *str; 247 ssize_t size; 248 int ret; 249 250 size = of_device_get_modalias(dev, NULL, 0); 251 if (size < 0) 252 return size; 253 254 str = kmalloc(size + 1, GFP_KERNEL); 255 if (!str) 256 return -ENOMEM; 257 258 of_device_get_modalias(dev, str, size); 259 str[size] = '\0'; 260 ret = request_module(str); 261 kfree(str); 262 263 return ret; 264 } 265 EXPORT_SYMBOL_GPL(of_device_request_module); 266 267 /** 268 * of_device_modalias - Fill buffer with newline terminated modalias string 269 */ 270 ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len) 271 { 272 ssize_t sl = of_device_get_modalias(dev, str, len - 2); 273 if (sl < 0) 274 return sl; 275 if (sl > len - 2) 276 return -ENOMEM; 277 278 str[sl++] = '\n'; 279 str[sl] = 0; 280 return sl; 281 } 282 EXPORT_SYMBOL_GPL(of_device_modalias); 283 284 /** 285 * of_device_uevent - Display OF related uevent information 286 */ 287 void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) 288 { 289 const char *compat; 290 struct alias_prop *app; 291 struct property *p; 292 int seen = 0; 293 294 if ((!dev) || (!dev->of_node)) 295 return; 296 297 add_uevent_var(env, "OF_NAME=%s", dev->of_node->name); 298 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node); 299 if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0) 300 add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type); 301 302 /* Since the compatible field can contain pretty much anything 303 * it's not really legal to split it out with commas. We split it 304 * up using a number of environment variables instead. */ 305 of_property_for_each_string(dev->of_node, "compatible", p, compat) { 306 add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat); 307 seen++; 308 } 309 add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen); 310 311 seen = 0; 312 mutex_lock(&of_mutex); 313 list_for_each_entry(app, &aliases_lookup, link) { 314 if (dev->of_node == app->np) { 315 add_uevent_var(env, "OF_ALIAS_%d=%s", seen, 316 app->alias); 317 seen++; 318 } 319 } 320 mutex_unlock(&of_mutex); 321 } 322 323 int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) 324 { 325 int sl; 326 327 if ((!dev) || (!dev->of_node)) 328 return -ENODEV; 329 330 /* Devicetree modalias is tricky, we add it in 2 steps */ 331 if (add_uevent_var(env, "MODALIAS=")) 332 return -ENOMEM; 333 334 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1], 335 sizeof(env->buf) - env->buflen); 336 if (sl >= (sizeof(env->buf) - env->buflen)) 337 return -ENOMEM; 338 env->buflen += sl; 339 340 return 0; 341 } 342 EXPORT_SYMBOL_GPL(of_device_uevent_modalias); 343