4444
4545#include "remoteproc_internal.h"
4646
47+ static DEFINE_MUTEX (rproc_list_mutex );
48+ static LIST_HEAD (rproc_list );
49+
4750typedef int (* rproc_handle_resources_t )(struct rproc * rproc ,
4851 struct resource_table * table , int len );
4952typedef int (* rproc_handle_resource_t )(struct rproc * rproc ,
@@ -132,32 +135,48 @@ static void rproc_disable_iommu(struct rproc *rproc)
132135
133136 iommu_detach_device (domain , dev );
134137 iommu_domain_free (domain );
135-
136- return ;
137138}
138139
139- /*
140+ /**
141+ * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
142+ * @rproc: handle of a remote processor
143+ * @da: remoteproc device address to translate
144+ * @len: length of the memory region @da is pointing to
145+ *
140146 * Some remote processors will ask us to allocate them physically contiguous
141147 * memory regions (which we call "carveouts"), and map them to specific
142- * device addresses (which are hardcoded in the firmware).
148+ * device addresses (which are hardcoded in the firmware). They may also have
149+ * dedicated memory regions internal to the processors, and use them either
150+ * exclusively or alongside carveouts.
143151 *
144152 * They may then ask us to copy objects into specific device addresses (e.g.
145153 * code/data sections) or expose us certain symbols in other device address
146154 * (e.g. their trace buffer).
147155 *
148- * This function is an internal helper with which we can go over the allocated
149- * carveouts and translate specific device address to kernel virtual addresses
150- * so we can access the referenced memory.
156+ * This function is a helper function with which we can go over the allocated
157+ * carveouts and translate specific device addresses to kernel virtual addresses
158+ * so we can access the referenced memory. This function also allows to perform
159+ * translations on the internal remoteproc memory regions through a platform
160+ * implementation specific da_to_va ops, if present.
161+ *
162+ * The function returns a valid kernel address on success or NULL on failure.
151163 *
152164 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
153165 * but only on kernel direct mapped RAM memory. Instead, we're just using
154- * here the output of the DMA API, which should be more correct.
166+ * here the output of the DMA API for the carveouts, which should be more
167+ * correct.
155168 */
156169void * rproc_da_to_va (struct rproc * rproc , u64 da , int len )
157170{
158171 struct rproc_mem_entry * carveout ;
159172 void * ptr = NULL ;
160173
174+ if (rproc -> ops -> da_to_va ) {
175+ ptr = rproc -> ops -> da_to_va (rproc , da , len );
176+ if (ptr )
177+ goto out ;
178+ }
179+
161180 list_for_each_entry (carveout , & rproc -> carveouts , node ) {
162181 int offset = da - carveout -> da ;
163182
@@ -174,6 +193,7 @@ void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
174193 break ;
175194 }
176195
196+ out :
177197 return ptr ;
178198}
179199EXPORT_SYMBOL (rproc_da_to_va );
@@ -411,10 +431,8 @@ static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
411431 }
412432
413433 trace = kzalloc (sizeof (* trace ), GFP_KERNEL );
414- if (!trace ) {
415- dev_err (dev , "kzalloc trace failed\n" );
434+ if (!trace )
416435 return - ENOMEM ;
417- }
418436
419437 /* set the trace buffer dma properties */
420438 trace -> len = rsc -> len ;
@@ -489,10 +507,8 @@ static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
489507 }
490508
491509 mapping = kzalloc (sizeof (* mapping ), GFP_KERNEL );
492- if (!mapping ) {
493- dev_err (dev , "kzalloc mapping failed\n" );
510+ if (!mapping )
494511 return - ENOMEM ;
495- }
496512
497513 ret = iommu_map (rproc -> domain , rsc -> da , rsc -> pa , rsc -> len , rsc -> flags );
498514 if (ret ) {
@@ -565,10 +581,8 @@ static int rproc_handle_carveout(struct rproc *rproc,
565581 rsc -> da , rsc -> pa , rsc -> len , rsc -> flags );
566582
567583 carveout = kzalloc (sizeof (* carveout ), GFP_KERNEL );
568- if (!carveout ) {
569- dev_err (dev , "kzalloc carveout failed\n" );
584+ if (!carveout )
570585 return - ENOMEM ;
571- }
572586
573587 va = dma_alloc_coherent (dev -> parent , rsc -> len , & dma , GFP_KERNEL );
574588 if (!va ) {
@@ -768,7 +782,8 @@ static void rproc_resource_cleanup(struct rproc *rproc)
768782
769783 /* clean up carveout allocations */
770784 list_for_each_entry_safe (entry , tmp , & rproc -> carveouts , node ) {
771- dma_free_coherent (dev -> parent , entry -> len , entry -> va , entry -> dma );
785+ dma_free_coherent (dev -> parent , entry -> len , entry -> va ,
786+ entry -> dma );
772787 list_del (& entry -> node );
773788 kfree (entry );
774789 }
@@ -808,9 +823,8 @@ static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
808823
809824 /* look for the resource table */
810825 table = rproc_find_rsc_table (rproc , fw , & tablesz );
811- if (!table ) {
826+ if (!table )
812827 goto clean_up ;
813- }
814828
815829 /* Verify that resource table in loaded fw is unchanged */
816830 if (rproc -> table_csum != crc32 (0 , table , tablesz )) {
@@ -911,7 +925,8 @@ static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
911925
912926 /* count the number of notify-ids */
913927 rproc -> max_notifyid = -1 ;
914- ret = rproc_handle_resources (rproc , tablesz , rproc_count_vrings_handler );
928+ ret = rproc_handle_resources (rproc , tablesz ,
929+ rproc_count_vrings_handler );
915930 if (ret )
916931 goto out ;
917932
@@ -1151,6 +1166,50 @@ void rproc_shutdown(struct rproc *rproc)
11511166}
11521167EXPORT_SYMBOL (rproc_shutdown );
11531168
1169+ /**
1170+ * rproc_get_by_phandle() - find a remote processor by phandle
1171+ * @phandle: phandle to the rproc
1172+ *
1173+ * Finds an rproc handle using the remote processor's phandle, and then
1174+ * return a handle to the rproc.
1175+ *
1176+ * This function increments the remote processor's refcount, so always
1177+ * use rproc_put() to decrement it back once rproc isn't needed anymore.
1178+ *
1179+ * Returns the rproc handle on success, and NULL on failure.
1180+ */
1181+ #ifdef CONFIG_OF
1182+ struct rproc * rproc_get_by_phandle (phandle phandle )
1183+ {
1184+ struct rproc * rproc = NULL , * r ;
1185+ struct device_node * np ;
1186+
1187+ np = of_find_node_by_phandle (phandle );
1188+ if (!np )
1189+ return NULL ;
1190+
1191+ mutex_lock (& rproc_list_mutex );
1192+ list_for_each_entry (r , & rproc_list , node ) {
1193+ if (r -> dev .parent && r -> dev .parent -> of_node == np ) {
1194+ rproc = r ;
1195+ get_device (& rproc -> dev );
1196+ break ;
1197+ }
1198+ }
1199+ mutex_unlock (& rproc_list_mutex );
1200+
1201+ of_node_put (np );
1202+
1203+ return rproc ;
1204+ }
1205+ #else
1206+ struct rproc * rproc_get_by_phandle (phandle phandle )
1207+ {
1208+ return NULL ;
1209+ }
1210+ #endif
1211+ EXPORT_SYMBOL (rproc_get_by_phandle );
1212+
11541213/**
11551214 * rproc_add() - register a remote processor
11561215 * @rproc: the remote processor handle to register
@@ -1180,6 +1239,11 @@ int rproc_add(struct rproc *rproc)
11801239 if (ret < 0 )
11811240 return ret ;
11821241
1242+ /* expose to rproc_get_by_phandle users */
1243+ mutex_lock (& rproc_list_mutex );
1244+ list_add (& rproc -> node , & rproc_list );
1245+ mutex_unlock (& rproc_list_mutex );
1246+
11831247 dev_info (dev , "%s is available\n" , rproc -> name );
11841248
11851249 dev_info (dev , "Note: remoteproc is still under development and considered experimental.\n" );
@@ -1268,10 +1332,8 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,
12681332 name_len = strlen (name ) + strlen (template ) - 2 + 1 ;
12691333
12701334 rproc = kzalloc (sizeof (struct rproc ) + len + name_len , GFP_KERNEL );
1271- if (!rproc ) {
1272- dev_err (dev , "%s: kzalloc failed\n" , __func__ );
1335+ if (!rproc )
12731336 return NULL ;
1274- }
12751337
12761338 if (!firmware ) {
12771339 p = (char * )rproc + sizeof (struct rproc ) + len ;
@@ -1369,6 +1431,11 @@ int rproc_del(struct rproc *rproc)
13691431 /* Free the copy of the resource table */
13701432 kfree (rproc -> cached_table );
13711433
1434+ /* the rproc is downref'ed as soon as it's removed from the klist */
1435+ mutex_lock (& rproc_list_mutex );
1436+ list_del (& rproc -> node );
1437+ mutex_unlock (& rproc_list_mutex );
1438+
13721439 device_del (& rproc -> dev );
13731440
13741441 return 0 ;
0 commit comments