of: reserved_mem: fix cell count for memory-region property
[linux-3.10.git] / drivers / of / of_reserved_mem.c
index 69b8117..2275432 100644 (file)
@@ -170,6 +170,33 @@ static int __init __reserved_mem_alloc_size(unsigned long node,
        return 0;
 }
 
+static const struct of_device_id __rmem_of_table_sentinel
+       __used __section(__reservedmem_of_table_end);
+
+/**
+ * res_mem_init_node() - call region specific reserved memory init code
+ */
+static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
+{
+       extern const struct of_device_id __reservedmem_of_table[];
+       const struct of_device_id *i;
+
+       for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
+               reservedmem_of_init_fn initfn = i->data;
+               const char *compat = i->compatible;
+
+               if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
+                       continue;
+
+               if (initfn(rmem) == 0) {
+                       pr_info("Reserved memory: initialized node %s, compatible id %s\n",
+                               rmem->name, compat);
+                       return 0;
+               }
+       }
+       return -ENOENT;
+}
+
 /**
  * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
  */
@@ -179,10 +206,97 @@ void __init fdt_init_reserved_mem(void)
        for (i = 0; i < reserved_mem_count; i++) {
                struct reserved_mem *rmem = &reserved_mem[i];
                unsigned long node = rmem->fdt_node;
+               int len;
+               const __be32 *prop;
                int err = 0;
 
+               prop = of_get_flat_dt_prop(node, "phandle", &len);
+               if (!prop)
+                       prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
+               if (prop)
+                       rmem->phandle = of_read_number(prop, len/4);
+
                if (rmem->size == 0)
                        err = __reserved_mem_alloc_size(node, rmem->name,
                                                 &rmem->base, &rmem->size);
+               if (err == 0)
+                       __reserved_mem_init_node(rmem);
+       }
+}
+
+static inline struct reserved_mem *__find_rmem(struct device_node *node)
+{
+       unsigned int i;
+
+       if (!node->phandle)
+               return NULL;
+
+       for (i = 0; i < reserved_mem_count; i++)
+               if (reserved_mem[i].phandle == node->phandle)
+                       return &reserved_mem[i];
+       return NULL;
+}
+
+/**
+ * of_reserved_mem_device_init() - assign reserved memory region to given device
+ *
+ * This function assign memory region pointed by "memory-region" device tree
+ * property to the given device.
+ */
+int of_reserved_mem_device_init(struct device *dev)
+{
+       struct reserved_mem *rmem;
+       struct of_phandle_iter iter;
+       int err = -ENODEV;
+
+       of_property_for_each_phandle_with_args(iter, dev->of_node, "memory-region",
+                                              NULL, 0) {
+               struct of_phandle_args *ret = &iter.out_args;
+
+               if (!ret->np)
+                       return -ENODEV;
+
+               of_node_get(ret->np);
+               rmem = __find_rmem(ret->np);
+               of_node_put(ret->np);
+
+               if (!rmem || !rmem->ops || !rmem->ops->device_init)
+                       return -EINVAL;
+
+               err = rmem->ops->device_init(rmem, dev);
+               if (err != 0)
+                       break;
+               dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
+       }
+
+       return err;
+}
+
+/**
+ * of_reserved_mem_device_release() - release reserved memory device structures
+ *
+ * This function releases structures allocated for memory region handling for
+ * the given device.
+ */
+void of_reserved_mem_device_release(struct device *dev)
+{
+       struct reserved_mem *rmem;
+       struct of_phandle_iter iter;
+
+       of_property_for_each_phandle_with_args(iter, dev->of_node, "memory-region",
+                                              NULL, 1) {
+               struct of_phandle_args *ret = &iter.out_args;
+
+               if (!ret->np)
+                       return;
+
+               of_node_get(ret->np);
+               rmem = __find_rmem(ret->np);
+               of_node_put(ret->np);
+
+               if (!rmem || !rmem->ops || !rmem->ops->device_release)
+                       return;
+
+               rmem->ops->device_release(rmem, dev);
        }
 }