fastrpc.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/completion.h>
  5. #include <linux/device.h>
  6. #include <linux/dma-buf.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/dma-resv.h>
  9. #include <linux/idr.h>
  10. #include <linux/list.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/sort.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/rpmsg.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/slab.h>
  21. #include <linux/firmware/qcom/qcom_scm.h>
  22. #include <uapi/misc/fastrpc.h>
  23. #include <linux/of_reserved_mem.h>
  24. #include <linux/bits.h>
  25. #define ADSP_DOMAIN_ID (0)
  26. #define MDSP_DOMAIN_ID (1)
  27. #define SDSP_DOMAIN_ID (2)
  28. #define CDSP_DOMAIN_ID (3)
  29. #define GDSP_DOMAIN_ID (4)
  30. #define FASTRPC_MAX_SESSIONS 14
  31. #define FASTRPC_MAX_VMIDS 16
  32. #define FASTRPC_ALIGN 128
  33. #define FASTRPC_MAX_FDLIST 16
  34. #define FASTRPC_MAX_CRCLIST 64
  35. #define FASTRPC_CTX_MAX (256)
  36. #define FASTRPC_INIT_HANDLE 1
  37. #define FASTRPC_DSP_UTILITIES_HANDLE 2
  38. #define FASTRPC_CTXID_MASK (0xFF0)
  39. #define INIT_FILELEN_MAX (2 * 1024 * 1024)
  40. #define INIT_FILE_NAMELEN_MAX (128)
  41. #define FASTRPC_DEVICE_NAME "fastrpc"
  42. /* Add memory to static PD pool, protection thru XPU */
  43. #define ADSP_MMAP_HEAP_ADDR 4
  44. /* MAP static DMA buffer on DSP User PD */
  45. #define ADSP_MMAP_DMA_BUFFER 6
  46. /* Add memory to static PD pool protection thru hypervisor */
  47. #define ADSP_MMAP_REMOTE_HEAP_ADDR 8
  48. /* Add memory to userPD pool, for user heap */
  49. #define ADSP_MMAP_ADD_PAGES 0x1000
  50. /* Add memory to userPD pool, for LLC heap */
  51. #define ADSP_MMAP_ADD_PAGES_LLC 0x3000,
  52. #define DSP_UNSUPPORTED_API (0x80000414)
  53. /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
  54. #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
  55. #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
  56. /* Retrives number of input buffers from the scalars parameter */
  57. #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
  58. /* Retrives number of output buffers from the scalars parameter */
  59. #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
  60. /* Retrives number of input handles from the scalars parameter */
  61. #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
  62. /* Retrives number of output handles from the scalars parameter */
  63. #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
  64. #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
  65. REMOTE_SCALARS_OUTBUFS(sc) + \
  66. REMOTE_SCALARS_INHANDLES(sc)+ \
  67. REMOTE_SCALARS_OUTHANDLES(sc))
  68. #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
  69. (((attr & 0x07) << 29) | \
  70. ((method & 0x1f) << 24) | \
  71. ((in & 0xff) << 16) | \
  72. ((out & 0xff) << 8) | \
  73. ((oin & 0x0f) << 4) | \
  74. (oout & 0x0f))
  75. #define FASTRPC_SCALARS(method, in, out) \
  76. FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
  77. #define FASTRPC_CREATE_PROCESS_NARGS 6
  78. #define FASTRPC_CREATE_STATIC_PROCESS_NARGS 3
  79. /* Remote Method id table */
  80. #define FASTRPC_RMID_INIT_ATTACH 0
  81. #define FASTRPC_RMID_INIT_RELEASE 1
  82. #define FASTRPC_RMID_INIT_MMAP 4
  83. #define FASTRPC_RMID_INIT_MUNMAP 5
  84. #define FASTRPC_RMID_INIT_CREATE 6
  85. #define FASTRPC_RMID_INIT_CREATE_ATTR 7
  86. #define FASTRPC_RMID_INIT_CREATE_STATIC 8
  87. #define FASTRPC_RMID_INIT_MEM_MAP 10
  88. #define FASTRPC_RMID_INIT_MEM_UNMAP 11
  89. /* Protection Domain(PD) ids */
  90. #define ROOT_PD (0)
  91. #define USER_PD (1)
  92. #define SENSORS_PD (2)
  93. #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
  94. struct fastrpc_phy_page {
  95. dma_addr_t addr; /* dma address */
  96. u64 size; /* size of contiguous region */
  97. };
  98. struct fastrpc_invoke_buf {
  99. u32 num; /* number of contiguous regions */
  100. u32 pgidx; /* index to start of contiguous region */
  101. };
  102. struct fastrpc_remote_dmahandle {
  103. s32 fd; /* dma handle fd */
  104. u32 offset; /* dma handle offset */
  105. u32 len; /* dma handle length */
  106. };
  107. struct fastrpc_remote_buf {
  108. u64 pv; /* buffer pointer */
  109. u64 len; /* length of buffer */
  110. };
  111. union fastrpc_remote_arg {
  112. struct fastrpc_remote_buf buf;
  113. struct fastrpc_remote_dmahandle dma;
  114. };
  115. struct fastrpc_mmap_rsp_msg {
  116. u64 vaddr;
  117. };
  118. struct fastrpc_mmap_req_msg {
  119. s32 client_id;
  120. u32 flags;
  121. u64 vaddr;
  122. s32 num;
  123. };
  124. struct fastrpc_mem_map_req_msg {
  125. s32 client_id;
  126. s32 fd;
  127. s32 offset;
  128. u32 flags;
  129. u64 vaddrin;
  130. s32 num;
  131. s32 data_len;
  132. };
  133. struct fastrpc_munmap_req_msg {
  134. s32 client_id;
  135. u64 vaddr;
  136. u64 size;
  137. };
  138. struct fastrpc_mem_unmap_req_msg {
  139. s32 client_id;
  140. s32 fd;
  141. u64 vaddrin;
  142. u64 len;
  143. };
  144. struct fastrpc_msg {
  145. int client_id; /* process client id */
  146. int tid; /* thread id */
  147. u64 ctx; /* invoke caller context */
  148. u32 handle; /* handle to invoke */
  149. u32 sc; /* scalars structure describing the data */
  150. dma_addr_t addr; /* dma address */
  151. u64 size; /* size of contiguous region */
  152. };
  153. struct fastrpc_invoke_rsp {
  154. u64 ctx; /* invoke caller context */
  155. int retval; /* invoke return value */
  156. };
  157. struct fastrpc_buf_overlap {
  158. u64 start;
  159. u64 end;
  160. int raix;
  161. u64 mstart;
  162. u64 mend;
  163. u64 offset;
  164. };
  165. struct fastrpc_buf {
  166. struct fastrpc_user *fl;
  167. struct dma_buf *dmabuf;
  168. struct device *dev;
  169. void *virt;
  170. dma_addr_t dma_addr;
  171. u64 size;
  172. /* Lock for dma buf attachments */
  173. struct mutex lock;
  174. struct list_head attachments;
  175. /* mmap support */
  176. struct list_head node; /* list of user requested mmaps */
  177. uintptr_t raddr;
  178. };
  179. struct fastrpc_dma_buf_attachment {
  180. struct device *dev;
  181. struct sg_table sgt;
  182. struct list_head node;
  183. };
  184. struct fastrpc_map {
  185. struct list_head node;
  186. struct fastrpc_user *fl;
  187. int fd;
  188. struct dma_buf *buf;
  189. struct sg_table *table;
  190. struct dma_buf_attachment *attach;
  191. dma_addr_t dma_addr;
  192. u64 size;
  193. void *va;
  194. u64 len;
  195. u64 raddr;
  196. u32 attr;
  197. struct kref refcount;
  198. };
  199. struct fastrpc_invoke_ctx {
  200. int nscalars;
  201. int nbufs;
  202. int retval;
  203. int pid;
  204. int client_id;
  205. u32 sc;
  206. u32 *crc;
  207. u64 ctxid;
  208. u64 msg_sz;
  209. struct kref refcount;
  210. struct list_head node; /* list of ctxs */
  211. struct completion work;
  212. struct work_struct put_work;
  213. struct fastrpc_msg msg;
  214. struct fastrpc_user *fl;
  215. union fastrpc_remote_arg *rpra;
  216. struct fastrpc_map **maps;
  217. struct fastrpc_buf *buf;
  218. struct fastrpc_invoke_args *args;
  219. struct fastrpc_buf_overlap *olaps;
  220. struct fastrpc_channel_ctx *cctx;
  221. };
  222. struct fastrpc_session_ctx {
  223. struct device *dev;
  224. int sid;
  225. bool used;
  226. bool valid;
  227. };
  228. struct fastrpc_soc_data {
  229. u32 sid_pos;
  230. u32 dma_addr_bits_cdsp;
  231. u32 dma_addr_bits_default;
  232. };
  233. struct fastrpc_channel_ctx {
  234. int domain_id;
  235. int sesscount;
  236. int vmcount;
  237. struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS];
  238. struct rpmsg_device *rpdev;
  239. struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
  240. spinlock_t lock;
  241. struct idr ctx_idr;
  242. struct list_head users;
  243. struct kref refcount;
  244. /* Flag if dsp attributes are cached */
  245. bool valid_attributes;
  246. u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
  247. struct fastrpc_device *secure_fdevice;
  248. struct fastrpc_device *fdevice;
  249. struct fastrpc_buf *remote_heap;
  250. struct list_head invoke_interrupted_mmaps;
  251. bool secure;
  252. bool unsigned_support;
  253. u64 dma_mask;
  254. const struct fastrpc_soc_data *soc_data;
  255. };
  256. struct fastrpc_device {
  257. struct fastrpc_channel_ctx *cctx;
  258. struct miscdevice miscdev;
  259. bool secure;
  260. };
  261. struct fastrpc_user {
  262. struct list_head user;
  263. struct list_head maps;
  264. struct list_head pending;
  265. struct list_head mmaps;
  266. struct fastrpc_channel_ctx *cctx;
  267. struct fastrpc_session_ctx *sctx;
  268. struct fastrpc_buf *init_mem;
  269. int client_id;
  270. int pd;
  271. bool is_secure_dev;
  272. /* Lock for lists */
  273. spinlock_t lock;
  274. /* lock for allocations */
  275. struct mutex mutex;
  276. };
  277. /* Extract SMMU PA from consolidated IOVA */
  278. static inline dma_addr_t fastrpc_ipa_to_dma_addr(struct fastrpc_channel_ctx *cctx, dma_addr_t iova)
  279. {
  280. if (!cctx->soc_data->sid_pos)
  281. return 0;
  282. return iova & GENMASK_ULL(cctx->soc_data->sid_pos - 1, 0);
  283. }
  284. /*
  285. * Prepare the consolidated iova to send to DSP by prepending the SID
  286. * to smmu PA at the appropriate position
  287. */
  288. static inline u64 fastrpc_sid_offset(struct fastrpc_channel_ctx *cctx,
  289. struct fastrpc_session_ctx *sctx)
  290. {
  291. return (u64)sctx->sid << cctx->soc_data->sid_pos;
  292. }
  293. static void fastrpc_free_map(struct kref *ref)
  294. {
  295. struct fastrpc_map *map;
  296. map = container_of(ref, struct fastrpc_map, refcount);
  297. if (map->table) {
  298. if (map->attr & FASTRPC_ATTR_SECUREMAP) {
  299. struct qcom_scm_vmperm perm;
  300. int vmid = map->fl->cctx->vmperms[0].vmid;
  301. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS) | BIT(vmid);
  302. int err = 0;
  303. perm.vmid = QCOM_SCM_VMID_HLOS;
  304. perm.perm = QCOM_SCM_PERM_RWX;
  305. err = qcom_scm_assign_mem(map->dma_addr, map->len,
  306. &src_perms, &perm, 1);
  307. if (err) {
  308. dev_err(map->fl->sctx->dev,
  309. "Failed to assign memory dma_addr %pad size 0x%llx err %d\n",
  310. &map->dma_addr, map->len, err);
  311. return;
  312. }
  313. }
  314. dma_buf_unmap_attachment_unlocked(map->attach, map->table,
  315. DMA_BIDIRECTIONAL);
  316. dma_buf_detach(map->buf, map->attach);
  317. dma_buf_put(map->buf);
  318. }
  319. if (map->fl) {
  320. spin_lock(&map->fl->lock);
  321. list_del(&map->node);
  322. spin_unlock(&map->fl->lock);
  323. map->fl = NULL;
  324. }
  325. kfree(map);
  326. }
  327. static void fastrpc_map_put(struct fastrpc_map *map)
  328. {
  329. if (map)
  330. kref_put(&map->refcount, fastrpc_free_map);
  331. }
  332. static int fastrpc_map_get(struct fastrpc_map *map)
  333. {
  334. if (!map)
  335. return -ENOENT;
  336. return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT;
  337. }
  338. static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
  339. struct fastrpc_map **ppmap)
  340. {
  341. struct fastrpc_map *map = NULL;
  342. struct dma_buf *buf;
  343. int ret = -ENOENT;
  344. buf = dma_buf_get(fd);
  345. if (IS_ERR(buf))
  346. return PTR_ERR(buf);
  347. spin_lock(&fl->lock);
  348. list_for_each_entry(map, &fl->maps, node) {
  349. if (map->fd != fd || map->buf != buf)
  350. continue;
  351. *ppmap = map;
  352. ret = 0;
  353. break;
  354. }
  355. spin_unlock(&fl->lock);
  356. dma_buf_put(buf);
  357. return ret;
  358. }
  359. static void fastrpc_buf_free(struct fastrpc_buf *buf)
  360. {
  361. dma_free_coherent(buf->dev, buf->size, buf->virt,
  362. fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr));
  363. kfree(buf);
  364. }
  365. static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
  366. u64 size, struct fastrpc_buf **obuf)
  367. {
  368. struct fastrpc_buf *buf;
  369. buf = kzalloc_obj(*buf);
  370. if (!buf)
  371. return -ENOMEM;
  372. INIT_LIST_HEAD(&buf->attachments);
  373. INIT_LIST_HEAD(&buf->node);
  374. mutex_init(&buf->lock);
  375. buf->fl = fl;
  376. buf->virt = NULL;
  377. buf->dma_addr = 0;
  378. buf->size = size;
  379. buf->dev = dev;
  380. buf->raddr = 0;
  381. buf->virt = dma_alloc_coherent(dev, buf->size, &buf->dma_addr,
  382. GFP_KERNEL);
  383. if (!buf->virt) {
  384. mutex_destroy(&buf->lock);
  385. kfree(buf);
  386. return -ENOMEM;
  387. }
  388. *obuf = buf;
  389. return 0;
  390. }
  391. static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
  392. u64 size, struct fastrpc_buf **obuf)
  393. {
  394. int ret;
  395. struct fastrpc_buf *buf;
  396. ret = __fastrpc_buf_alloc(fl, dev, size, obuf);
  397. if (ret)
  398. return ret;
  399. buf = *obuf;
  400. if (fl->sctx && fl->sctx->sid)
  401. buf->dma_addr += fastrpc_sid_offset(fl->cctx, fl->sctx);
  402. return 0;
  403. }
  404. static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev,
  405. u64 size, struct fastrpc_buf **obuf)
  406. {
  407. struct device *rdev = &fl->cctx->rpdev->dev;
  408. return __fastrpc_buf_alloc(fl, rdev, size, obuf);
  409. }
  410. static void fastrpc_channel_ctx_free(struct kref *ref)
  411. {
  412. struct fastrpc_channel_ctx *cctx;
  413. cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
  414. kfree(cctx);
  415. }
  416. static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
  417. {
  418. kref_get(&cctx->refcount);
  419. }
  420. static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
  421. {
  422. kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
  423. }
  424. static void fastrpc_context_free(struct kref *ref)
  425. {
  426. struct fastrpc_invoke_ctx *ctx;
  427. struct fastrpc_channel_ctx *cctx;
  428. unsigned long flags;
  429. int i;
  430. ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
  431. cctx = ctx->cctx;
  432. for (i = 0; i < ctx->nbufs; i++)
  433. fastrpc_map_put(ctx->maps[i]);
  434. if (ctx->buf)
  435. fastrpc_buf_free(ctx->buf);
  436. spin_lock_irqsave(&cctx->lock, flags);
  437. idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
  438. spin_unlock_irqrestore(&cctx->lock, flags);
  439. kfree(ctx->maps);
  440. kfree(ctx->olaps);
  441. kfree(ctx);
  442. fastrpc_channel_ctx_put(cctx);
  443. }
  444. static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
  445. {
  446. kref_get(&ctx->refcount);
  447. }
  448. static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
  449. {
  450. kref_put(&ctx->refcount, fastrpc_context_free);
  451. }
  452. static void fastrpc_context_put_wq(struct work_struct *work)
  453. {
  454. struct fastrpc_invoke_ctx *ctx =
  455. container_of(work, struct fastrpc_invoke_ctx, put_work);
  456. fastrpc_context_put(ctx);
  457. }
  458. #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
  459. static int olaps_cmp(const void *a, const void *b)
  460. {
  461. struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
  462. struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
  463. /* sort with lowest starting buffer first */
  464. int st = CMP(pa->start, pb->start);
  465. /* sort with highest ending buffer first */
  466. int ed = CMP(pb->end, pa->end);
  467. return st == 0 ? ed : st;
  468. }
  469. static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
  470. {
  471. u64 max_end = 0;
  472. int i;
  473. for (i = 0; i < ctx->nbufs; ++i) {
  474. ctx->olaps[i].start = ctx->args[i].ptr;
  475. ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
  476. ctx->olaps[i].raix = i;
  477. }
  478. sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
  479. for (i = 0; i < ctx->nbufs; ++i) {
  480. /* Falling inside previous range */
  481. if (ctx->olaps[i].start < max_end) {
  482. ctx->olaps[i].mstart = max_end;
  483. ctx->olaps[i].mend = ctx->olaps[i].end;
  484. ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
  485. if (ctx->olaps[i].end > max_end) {
  486. max_end = ctx->olaps[i].end;
  487. } else {
  488. ctx->olaps[i].mend = 0;
  489. ctx->olaps[i].mstart = 0;
  490. }
  491. } else {
  492. ctx->olaps[i].mend = ctx->olaps[i].end;
  493. ctx->olaps[i].mstart = ctx->olaps[i].start;
  494. ctx->olaps[i].offset = 0;
  495. max_end = ctx->olaps[i].end;
  496. }
  497. }
  498. }
  499. static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
  500. struct fastrpc_user *user, u32 kernel, u32 sc,
  501. struct fastrpc_invoke_args *args)
  502. {
  503. struct fastrpc_channel_ctx *cctx = user->cctx;
  504. struct fastrpc_invoke_ctx *ctx = NULL;
  505. unsigned long flags;
  506. int ret;
  507. ctx = kzalloc_obj(*ctx);
  508. if (!ctx)
  509. return ERR_PTR(-ENOMEM);
  510. INIT_LIST_HEAD(&ctx->node);
  511. ctx->fl = user;
  512. ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
  513. ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
  514. REMOTE_SCALARS_OUTBUFS(sc);
  515. if (ctx->nscalars) {
  516. ctx->maps = kzalloc_objs(*ctx->maps, ctx->nscalars);
  517. if (!ctx->maps) {
  518. kfree(ctx);
  519. return ERR_PTR(-ENOMEM);
  520. }
  521. ctx->olaps = kzalloc_objs(*ctx->olaps, ctx->nscalars);
  522. if (!ctx->olaps) {
  523. kfree(ctx->maps);
  524. kfree(ctx);
  525. return ERR_PTR(-ENOMEM);
  526. }
  527. ctx->args = args;
  528. fastrpc_get_buff_overlaps(ctx);
  529. }
  530. /* Released in fastrpc_context_put() */
  531. fastrpc_channel_ctx_get(cctx);
  532. ctx->sc = sc;
  533. ctx->retval = -1;
  534. ctx->pid = current->pid;
  535. ctx->client_id = user->client_id;
  536. ctx->cctx = cctx;
  537. init_completion(&ctx->work);
  538. INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
  539. spin_lock(&user->lock);
  540. list_add_tail(&ctx->node, &user->pending);
  541. spin_unlock(&user->lock);
  542. spin_lock_irqsave(&cctx->lock, flags);
  543. ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
  544. FASTRPC_CTX_MAX, GFP_ATOMIC);
  545. if (ret < 0) {
  546. spin_unlock_irqrestore(&cctx->lock, flags);
  547. goto err_idr;
  548. }
  549. ctx->ctxid = ret << 4;
  550. spin_unlock_irqrestore(&cctx->lock, flags);
  551. kref_init(&ctx->refcount);
  552. return ctx;
  553. err_idr:
  554. spin_lock(&user->lock);
  555. list_del(&ctx->node);
  556. spin_unlock(&user->lock);
  557. fastrpc_channel_ctx_put(cctx);
  558. kfree(ctx->maps);
  559. kfree(ctx->olaps);
  560. kfree(ctx);
  561. return ERR_PTR(ret);
  562. }
  563. static struct sg_table *
  564. fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
  565. enum dma_data_direction dir)
  566. {
  567. struct fastrpc_dma_buf_attachment *a = attachment->priv;
  568. struct sg_table *table;
  569. int ret;
  570. table = &a->sgt;
  571. ret = dma_map_sgtable(attachment->dev, table, dir, 0);
  572. if (ret)
  573. table = ERR_PTR(ret);
  574. return table;
  575. }
  576. static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
  577. struct sg_table *table,
  578. enum dma_data_direction dir)
  579. {
  580. dma_unmap_sgtable(attach->dev, table, dir, 0);
  581. }
  582. static void fastrpc_release(struct dma_buf *dmabuf)
  583. {
  584. struct fastrpc_buf *buffer = dmabuf->priv;
  585. fastrpc_buf_free(buffer);
  586. }
  587. static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
  588. struct dma_buf_attachment *attachment)
  589. {
  590. struct fastrpc_dma_buf_attachment *a;
  591. struct fastrpc_buf *buffer = dmabuf->priv;
  592. int ret;
  593. a = kzalloc_obj(*a);
  594. if (!a)
  595. return -ENOMEM;
  596. ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
  597. fastrpc_ipa_to_dma_addr(buffer->fl->cctx, buffer->dma_addr),
  598. buffer->size);
  599. if (ret < 0) {
  600. dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
  601. kfree(a);
  602. return -EINVAL;
  603. }
  604. a->dev = attachment->dev;
  605. INIT_LIST_HEAD(&a->node);
  606. attachment->priv = a;
  607. mutex_lock(&buffer->lock);
  608. list_add(&a->node, &buffer->attachments);
  609. mutex_unlock(&buffer->lock);
  610. return 0;
  611. }
  612. static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
  613. struct dma_buf_attachment *attachment)
  614. {
  615. struct fastrpc_dma_buf_attachment *a = attachment->priv;
  616. struct fastrpc_buf *buffer = dmabuf->priv;
  617. mutex_lock(&buffer->lock);
  618. list_del(&a->node);
  619. mutex_unlock(&buffer->lock);
  620. sg_free_table(&a->sgt);
  621. kfree(a);
  622. }
  623. static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
  624. {
  625. struct fastrpc_buf *buf = dmabuf->priv;
  626. iosys_map_set_vaddr(map, buf->virt);
  627. return 0;
  628. }
  629. static int fastrpc_mmap(struct dma_buf *dmabuf,
  630. struct vm_area_struct *vma)
  631. {
  632. struct fastrpc_buf *buf = dmabuf->priv;
  633. size_t size = vma->vm_end - vma->vm_start;
  634. dma_resv_assert_held(dmabuf->resv);
  635. return dma_mmap_coherent(buf->dev, vma, buf->virt,
  636. fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr), size);
  637. }
  638. static const struct dma_buf_ops fastrpc_dma_buf_ops = {
  639. .attach = fastrpc_dma_buf_attach,
  640. .detach = fastrpc_dma_buf_detatch,
  641. .map_dma_buf = fastrpc_map_dma_buf,
  642. .unmap_dma_buf = fastrpc_unmap_dma_buf,
  643. .mmap = fastrpc_mmap,
  644. .vmap = fastrpc_vmap,
  645. .release = fastrpc_release,
  646. };
  647. static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t sg_dma_addr)
  648. {
  649. return sg_dma_addr + fastrpc_sid_offset(fl->cctx, fl->sctx);
  650. }
  651. static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
  652. u64 len, u32 attr, struct fastrpc_map **ppmap)
  653. {
  654. struct fastrpc_session_ctx *sess = fl->sctx;
  655. struct fastrpc_map *map = NULL;
  656. struct sg_table *table;
  657. struct scatterlist *sgl = NULL;
  658. int err = 0, sgl_index = 0;
  659. map = kzalloc_obj(*map);
  660. if (!map)
  661. return -ENOMEM;
  662. INIT_LIST_HEAD(&map->node);
  663. kref_init(&map->refcount);
  664. map->fl = fl;
  665. map->fd = fd;
  666. map->buf = dma_buf_get(fd);
  667. if (IS_ERR(map->buf)) {
  668. err = PTR_ERR(map->buf);
  669. goto get_err;
  670. }
  671. map->attach = dma_buf_attach(map->buf, sess->dev);
  672. if (IS_ERR(map->attach)) {
  673. dev_err(sess->dev, "Failed to attach dmabuf\n");
  674. err = PTR_ERR(map->attach);
  675. goto attach_err;
  676. }
  677. table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL);
  678. if (IS_ERR(table)) {
  679. err = PTR_ERR(table);
  680. goto map_err;
  681. }
  682. map->table = table;
  683. if (attr & FASTRPC_ATTR_SECUREMAP)
  684. map->dma_addr = sg_phys(map->table->sgl);
  685. else
  686. map->dma_addr = fastrpc_compute_dma_addr(fl, sg_dma_address(map->table->sgl));
  687. for_each_sg(map->table->sgl, sgl, map->table->nents,
  688. sgl_index)
  689. map->size += sg_dma_len(sgl);
  690. if (len > map->size) {
  691. dev_dbg(sess->dev, "Bad size passed len 0x%llx map size 0x%llx\n",
  692. len, map->size);
  693. err = -EINVAL;
  694. goto map_err;
  695. }
  696. map->va = sg_virt(map->table->sgl);
  697. map->len = len;
  698. if (attr & FASTRPC_ATTR_SECUREMAP) {
  699. /*
  700. * If subsystem VMIDs are defined in DTSI, then do
  701. * hyp_assign from HLOS to those VM(s)
  702. */
  703. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  704. struct qcom_scm_vmperm dst_perms[2] = {0};
  705. dst_perms[0].vmid = QCOM_SCM_VMID_HLOS;
  706. dst_perms[0].perm = QCOM_SCM_PERM_RW;
  707. dst_perms[1].vmid = fl->cctx->vmperms[0].vmid;
  708. dst_perms[1].perm = QCOM_SCM_PERM_RWX;
  709. map->attr = attr;
  710. err = qcom_scm_assign_mem(map->dma_addr, (u64)map->len, &src_perms, dst_perms, 2);
  711. if (err) {
  712. dev_err(sess->dev,
  713. "Failed to assign memory with dma_addr %pad size 0x%llx err %d\n",
  714. &map->dma_addr, map->len, err);
  715. goto map_err;
  716. }
  717. }
  718. spin_lock(&fl->lock);
  719. list_add_tail(&map->node, &fl->maps);
  720. spin_unlock(&fl->lock);
  721. *ppmap = map;
  722. return 0;
  723. map_err:
  724. dma_buf_detach(map->buf, map->attach);
  725. attach_err:
  726. dma_buf_put(map->buf);
  727. get_err:
  728. fastrpc_map_put(map);
  729. return err;
  730. }
  731. static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
  732. u64 len, u32 attr, struct fastrpc_map **ppmap)
  733. {
  734. struct fastrpc_session_ctx *sess = fl->sctx;
  735. int err = 0;
  736. if (!fastrpc_map_lookup(fl, fd, ppmap)) {
  737. if (!fastrpc_map_get(*ppmap))
  738. return 0;
  739. dev_dbg(sess->dev, "%s: Failed to get map fd=%d\n",
  740. __func__, fd);
  741. }
  742. err = fastrpc_map_attach(fl, fd, len, attr, ppmap);
  743. return err;
  744. }
  745. /*
  746. * Fastrpc payload buffer with metadata looks like:
  747. *
  748. * >>>>>> START of METADATA <<<<<<<<<
  749. * +---------------------------------+
  750. * | Arguments |
  751. * | type:(union fastrpc_remote_arg)|
  752. * | (0 - N) |
  753. * +---------------------------------+
  754. * | Invoke Buffer list |
  755. * | type:(struct fastrpc_invoke_buf)|
  756. * | (0 - N) |
  757. * +---------------------------------+
  758. * | Page info list |
  759. * | type:(struct fastrpc_phy_page) |
  760. * | (0 - N) |
  761. * +---------------------------------+
  762. * | Optional info |
  763. * |(can be specific to SoC/Firmware)|
  764. * +---------------------------------+
  765. * >>>>>>>> END of METADATA <<<<<<<<<
  766. * +---------------------------------+
  767. * | Inline ARGS |
  768. * | (0-N) |
  769. * +---------------------------------+
  770. */
  771. static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
  772. {
  773. int size = 0;
  774. size = (sizeof(struct fastrpc_remote_buf) +
  775. sizeof(struct fastrpc_invoke_buf) +
  776. sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
  777. sizeof(u64) * FASTRPC_MAX_FDLIST +
  778. sizeof(u32) * FASTRPC_MAX_CRCLIST;
  779. return size;
  780. }
  781. static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
  782. {
  783. u64 size = 0;
  784. int oix;
  785. size = ALIGN(metalen, FASTRPC_ALIGN);
  786. for (oix = 0; oix < ctx->nbufs; oix++) {
  787. int i = ctx->olaps[oix].raix;
  788. if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
  789. if (ctx->olaps[oix].offset == 0)
  790. size = ALIGN(size, FASTRPC_ALIGN);
  791. size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
  792. }
  793. }
  794. return size;
  795. }
  796. static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
  797. {
  798. struct device *dev = ctx->fl->sctx->dev;
  799. int i, err;
  800. for (i = 0; i < ctx->nscalars; ++i) {
  801. if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
  802. ctx->args[i].length == 0)
  803. continue;
  804. if (i < ctx->nbufs)
  805. err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
  806. ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
  807. else
  808. err = fastrpc_map_attach(ctx->fl, ctx->args[i].fd,
  809. ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
  810. if (err) {
  811. dev_err(dev, "Error Creating map %d\n", err);
  812. return -EINVAL;
  813. }
  814. }
  815. return 0;
  816. }
  817. static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
  818. {
  819. return (struct fastrpc_invoke_buf *)(&pra[len]);
  820. }
  821. static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
  822. {
  823. return (struct fastrpc_phy_page *)(&buf[len]);
  824. }
  825. static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
  826. {
  827. struct device *dev = ctx->fl->sctx->dev;
  828. union fastrpc_remote_arg *rpra;
  829. struct fastrpc_invoke_buf *list;
  830. struct fastrpc_phy_page *pages;
  831. int inbufs, i, oix, err = 0;
  832. u64 len, rlen, pkt_size;
  833. u64 pg_start, pg_end;
  834. uintptr_t args;
  835. int metalen;
  836. inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
  837. metalen = fastrpc_get_meta_size(ctx);
  838. pkt_size = fastrpc_get_payload_size(ctx, metalen);
  839. err = fastrpc_create_maps(ctx);
  840. if (err)
  841. return err;
  842. ctx->msg_sz = pkt_size;
  843. if (ctx->fl->sctx->sid)
  844. err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
  845. else
  846. err = fastrpc_remote_heap_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
  847. if (err)
  848. return err;
  849. memset(ctx->buf->virt, 0, pkt_size);
  850. rpra = ctx->buf->virt;
  851. list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
  852. pages = fastrpc_phy_page_start(list, ctx->nscalars);
  853. args = (uintptr_t)ctx->buf->virt + metalen;
  854. rlen = pkt_size - metalen;
  855. ctx->rpra = rpra;
  856. for (oix = 0; oix < ctx->nbufs; ++oix) {
  857. int mlen;
  858. i = ctx->olaps[oix].raix;
  859. len = ctx->args[i].length;
  860. rpra[i].buf.pv = 0;
  861. rpra[i].buf.len = len;
  862. list[i].num = len ? 1 : 0;
  863. list[i].pgidx = i;
  864. if (!len)
  865. continue;
  866. if (ctx->maps[i]) {
  867. struct vm_area_struct *vma = NULL;
  868. rpra[i].buf.pv = (u64) ctx->args[i].ptr;
  869. pages[i].addr = ctx->maps[i]->dma_addr;
  870. mmap_read_lock(current->mm);
  871. vma = find_vma(current->mm, ctx->args[i].ptr);
  872. if (vma)
  873. pages[i].addr += (ctx->args[i].ptr & PAGE_MASK) -
  874. vma->vm_start;
  875. mmap_read_unlock(current->mm);
  876. pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
  877. pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
  878. PAGE_SHIFT;
  879. pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
  880. } else {
  881. if (ctx->olaps[oix].offset == 0) {
  882. rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
  883. args = ALIGN(args, FASTRPC_ALIGN);
  884. }
  885. mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
  886. if (rlen < mlen)
  887. goto bail;
  888. rpra[i].buf.pv = args - ctx->olaps[oix].offset;
  889. pages[i].addr = ctx->buf->dma_addr -
  890. ctx->olaps[oix].offset +
  891. (pkt_size - rlen);
  892. pages[i].addr = pages[i].addr & PAGE_MASK;
  893. pg_start = (rpra[i].buf.pv & PAGE_MASK) >> PAGE_SHIFT;
  894. pg_end = ((rpra[i].buf.pv + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
  895. pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
  896. args = args + mlen;
  897. rlen -= mlen;
  898. }
  899. if (i < inbufs && !ctx->maps[i]) {
  900. void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
  901. void *src = (void *)(uintptr_t)ctx->args[i].ptr;
  902. if (!kernel) {
  903. if (copy_from_user(dst, (void __user *)src,
  904. len)) {
  905. err = -EFAULT;
  906. goto bail;
  907. }
  908. } else {
  909. memcpy(dst, src, len);
  910. }
  911. }
  912. }
  913. for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
  914. list[i].num = ctx->args[i].length ? 1 : 0;
  915. list[i].pgidx = i;
  916. if (ctx->maps[i]) {
  917. pages[i].addr = ctx->maps[i]->dma_addr;
  918. pages[i].size = ctx->maps[i]->size;
  919. }
  920. rpra[i].dma.fd = ctx->args[i].fd;
  921. rpra[i].dma.len = ctx->args[i].length;
  922. rpra[i].dma.offset = (u64) ctx->args[i].ptr;
  923. }
  924. bail:
  925. if (err)
  926. dev_err(dev, "Error: get invoke args failed:%d\n", err);
  927. return err;
  928. }
  929. static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
  930. u32 kernel)
  931. {
  932. union fastrpc_remote_arg *rpra = ctx->rpra;
  933. struct fastrpc_user *fl = ctx->fl;
  934. struct fastrpc_map *mmap = NULL;
  935. struct fastrpc_invoke_buf *list;
  936. struct fastrpc_phy_page *pages;
  937. u64 *fdlist;
  938. int i, inbufs, outbufs, handles;
  939. int ret = 0;
  940. inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
  941. outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
  942. handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
  943. list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
  944. pages = fastrpc_phy_page_start(list, ctx->nscalars);
  945. fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
  946. for (i = inbufs; i < ctx->nbufs; ++i) {
  947. if (!ctx->maps[i]) {
  948. void *src = (void *)(uintptr_t)rpra[i].buf.pv;
  949. void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
  950. u64 len = rpra[i].buf.len;
  951. if (!kernel) {
  952. if (copy_to_user((void __user *)dst, src, len)) {
  953. ret = -EFAULT;
  954. goto cleanup_fdlist;
  955. }
  956. } else {
  957. memcpy(dst, src, len);
  958. }
  959. }
  960. }
  961. cleanup_fdlist:
  962. /* Clean up fdlist which is updated by DSP */
  963. for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
  964. if (!fdlist[i])
  965. break;
  966. if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap))
  967. fastrpc_map_put(mmap);
  968. }
  969. return ret;
  970. }
  971. static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
  972. struct fastrpc_invoke_ctx *ctx,
  973. u32 kernel, uint32_t handle)
  974. {
  975. struct fastrpc_channel_ctx *cctx;
  976. struct fastrpc_user *fl = ctx->fl;
  977. struct fastrpc_msg *msg = &ctx->msg;
  978. int ret;
  979. cctx = fl->cctx;
  980. msg->client_id = fl->client_id;
  981. msg->tid = current->pid;
  982. if (kernel)
  983. msg->client_id = 0;
  984. msg->ctx = ctx->ctxid | fl->pd;
  985. msg->handle = handle;
  986. msg->sc = ctx->sc;
  987. msg->addr = ctx->buf ? ctx->buf->dma_addr : 0;
  988. msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
  989. fastrpc_context_get(ctx);
  990. ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
  991. if (ret)
  992. fastrpc_context_put(ctx);
  993. return ret;
  994. }
  995. static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
  996. u32 handle, u32 sc,
  997. struct fastrpc_invoke_args *args)
  998. {
  999. struct fastrpc_invoke_ctx *ctx = NULL;
  1000. struct fastrpc_buf *buf, *b;
  1001. int err = 0;
  1002. if (!fl->sctx)
  1003. return -EINVAL;
  1004. if (!fl->cctx->rpdev)
  1005. return -EPIPE;
  1006. if (handle == FASTRPC_INIT_HANDLE && !kernel) {
  1007. dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle);
  1008. return -EPERM;
  1009. }
  1010. ctx = fastrpc_context_alloc(fl, kernel, sc, args);
  1011. if (IS_ERR(ctx))
  1012. return PTR_ERR(ctx);
  1013. err = fastrpc_get_args(kernel, ctx);
  1014. if (err)
  1015. goto bail;
  1016. /* make sure that all CPU memory writes are seen by DSP */
  1017. dma_wmb();
  1018. /* Send invoke buffer to remote dsp */
  1019. err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
  1020. if (err)
  1021. goto bail;
  1022. if (kernel) {
  1023. if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
  1024. err = -ETIMEDOUT;
  1025. } else {
  1026. err = wait_for_completion_interruptible(&ctx->work);
  1027. }
  1028. if (err)
  1029. goto bail;
  1030. /* make sure that all memory writes by DSP are seen by CPU */
  1031. dma_rmb();
  1032. /* populate all the output buffers with results */
  1033. err = fastrpc_put_args(ctx, kernel);
  1034. if (err)
  1035. goto bail;
  1036. /* Check the response from remote dsp */
  1037. err = ctx->retval;
  1038. if (err)
  1039. goto bail;
  1040. bail:
  1041. if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
  1042. /* We are done with this compute context */
  1043. spin_lock(&fl->lock);
  1044. list_del(&ctx->node);
  1045. spin_unlock(&fl->lock);
  1046. fastrpc_context_put(ctx);
  1047. }
  1048. if (err == -ERESTARTSYS) {
  1049. list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
  1050. list_del(&buf->node);
  1051. list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);
  1052. }
  1053. }
  1054. if (err)
  1055. dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
  1056. return err;
  1057. }
  1058. static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request)
  1059. {
  1060. /* Check if the device node is non-secure and channel is secure*/
  1061. if (!fl->is_secure_dev && fl->cctx->secure) {
  1062. /*
  1063. * Allow untrusted applications to offload only to Unsigned PD when
  1064. * channel is configured as secure and block untrusted apps on channel
  1065. * that does not support unsigned PD offload
  1066. */
  1067. if (!fl->cctx->unsigned_support || !unsigned_pd_request) {
  1068. dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD\n");
  1069. return true;
  1070. }
  1071. }
  1072. return false;
  1073. }
  1074. static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
  1075. char __user *argp)
  1076. {
  1077. struct fastrpc_init_create_static init;
  1078. struct fastrpc_invoke_args *args;
  1079. struct fastrpc_phy_page pages[1];
  1080. char *name;
  1081. int err;
  1082. bool scm_done = false;
  1083. struct {
  1084. int client_id;
  1085. u32 namelen;
  1086. u32 pageslen;
  1087. } inbuf;
  1088. u32 sc;
  1089. args = kzalloc_objs(*args, FASTRPC_CREATE_STATIC_PROCESS_NARGS);
  1090. if (!args)
  1091. return -ENOMEM;
  1092. if (copy_from_user(&init, argp, sizeof(init))) {
  1093. err = -EFAULT;
  1094. goto err;
  1095. }
  1096. if (init.namelen > INIT_FILE_NAMELEN_MAX) {
  1097. err = -EINVAL;
  1098. goto err;
  1099. }
  1100. name = memdup_user(u64_to_user_ptr(init.name), init.namelen);
  1101. if (IS_ERR(name)) {
  1102. err = PTR_ERR(name);
  1103. goto err;
  1104. }
  1105. if (!fl->cctx->remote_heap) {
  1106. err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
  1107. &fl->cctx->remote_heap);
  1108. if (err)
  1109. goto err_name;
  1110. /* Map if we have any heap VMIDs associated with this ADSP Static Process. */
  1111. if (fl->cctx->vmcount) {
  1112. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  1113. err = qcom_scm_assign_mem(fl->cctx->remote_heap->dma_addr,
  1114. (u64)fl->cctx->remote_heap->size,
  1115. &src_perms,
  1116. fl->cctx->vmperms, fl->cctx->vmcount);
  1117. if (err) {
  1118. dev_err(fl->sctx->dev,
  1119. "Failed to assign memory with dma_addr %pad size 0x%llx err %d\n",
  1120. &fl->cctx->remote_heap->dma_addr,
  1121. fl->cctx->remote_heap->size, err);
  1122. goto err_map;
  1123. }
  1124. scm_done = true;
  1125. }
  1126. }
  1127. inbuf.client_id = fl->client_id;
  1128. inbuf.namelen = init.namelen;
  1129. inbuf.pageslen = 0;
  1130. fl->pd = USER_PD;
  1131. args[0].ptr = (u64)(uintptr_t)&inbuf;
  1132. args[0].length = sizeof(inbuf);
  1133. args[0].fd = -1;
  1134. args[1].ptr = (u64)(uintptr_t)name;
  1135. args[1].length = inbuf.namelen;
  1136. args[1].fd = -1;
  1137. pages[0].addr = fl->cctx->remote_heap->dma_addr;
  1138. pages[0].size = fl->cctx->remote_heap->size;
  1139. args[2].ptr = (u64)(uintptr_t) pages;
  1140. args[2].length = sizeof(*pages);
  1141. args[2].fd = -1;
  1142. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0);
  1143. err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
  1144. sc, args);
  1145. if (err)
  1146. goto err_invoke;
  1147. kfree(args);
  1148. kfree(name);
  1149. return 0;
  1150. err_invoke:
  1151. if (fl->cctx->vmcount && scm_done) {
  1152. u64 src_perms = 0;
  1153. struct qcom_scm_vmperm dst_perms;
  1154. u32 i;
  1155. for (i = 0; i < fl->cctx->vmcount; i++)
  1156. src_perms |= BIT(fl->cctx->vmperms[i].vmid);
  1157. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  1158. dst_perms.perm = QCOM_SCM_PERM_RWX;
  1159. err = qcom_scm_assign_mem(fl->cctx->remote_heap->dma_addr,
  1160. (u64)fl->cctx->remote_heap->size,
  1161. &src_perms, &dst_perms, 1);
  1162. if (err)
  1163. dev_err(fl->sctx->dev, "Failed to assign memory dma_addr %pad size 0x%llx err %d\n",
  1164. &fl->cctx->remote_heap->dma_addr, fl->cctx->remote_heap->size, err);
  1165. }
  1166. err_map:
  1167. fastrpc_buf_free(fl->cctx->remote_heap);
  1168. fl->cctx->remote_heap = NULL;
  1169. err_name:
  1170. kfree(name);
  1171. err:
  1172. kfree(args);
  1173. return err;
  1174. }
  1175. static int fastrpc_init_create_process(struct fastrpc_user *fl,
  1176. char __user *argp)
  1177. {
  1178. struct fastrpc_init_create init;
  1179. struct fastrpc_invoke_args *args;
  1180. struct fastrpc_phy_page pages[1];
  1181. struct fastrpc_map *map = NULL;
  1182. struct fastrpc_buf *imem = NULL;
  1183. int memlen;
  1184. int err;
  1185. struct {
  1186. int client_id;
  1187. u32 namelen;
  1188. u32 filelen;
  1189. u32 pageslen;
  1190. u32 attrs;
  1191. u32 siglen;
  1192. } inbuf;
  1193. u32 sc;
  1194. bool unsigned_module = false;
  1195. args = kzalloc_objs(*args, FASTRPC_CREATE_PROCESS_NARGS);
  1196. if (!args)
  1197. return -ENOMEM;
  1198. if (copy_from_user(&init, argp, sizeof(init))) {
  1199. err = -EFAULT;
  1200. goto err;
  1201. }
  1202. if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
  1203. unsigned_module = true;
  1204. if (is_session_rejected(fl, unsigned_module)) {
  1205. err = -ECONNREFUSED;
  1206. goto err;
  1207. }
  1208. if (init.filelen > INIT_FILELEN_MAX) {
  1209. err = -EINVAL;
  1210. goto err;
  1211. }
  1212. inbuf.client_id = fl->client_id;
  1213. inbuf.namelen = strlen(current->comm) + 1;
  1214. inbuf.filelen = init.filelen;
  1215. inbuf.pageslen = 1;
  1216. inbuf.attrs = init.attrs;
  1217. inbuf.siglen = init.siglen;
  1218. fl->pd = USER_PD;
  1219. if (init.filelen && init.filefd) {
  1220. err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
  1221. if (err)
  1222. goto err;
  1223. }
  1224. memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
  1225. 1024 * 1024);
  1226. err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
  1227. &imem);
  1228. if (err)
  1229. goto err_alloc;
  1230. fl->init_mem = imem;
  1231. args[0].ptr = (u64)(uintptr_t)&inbuf;
  1232. args[0].length = sizeof(inbuf);
  1233. args[0].fd = -1;
  1234. args[1].ptr = (u64)(uintptr_t)current->comm;
  1235. args[1].length = inbuf.namelen;
  1236. args[1].fd = -1;
  1237. args[2].ptr = (u64) init.file;
  1238. args[2].length = inbuf.filelen;
  1239. args[2].fd = init.filefd;
  1240. pages[0].addr = imem->dma_addr;
  1241. pages[0].size = imem->size;
  1242. args[3].ptr = (u64)(uintptr_t) pages;
  1243. args[3].length = 1 * sizeof(*pages);
  1244. args[3].fd = -1;
  1245. args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
  1246. args[4].length = sizeof(inbuf.attrs);
  1247. args[4].fd = -1;
  1248. args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
  1249. args[5].length = sizeof(inbuf.siglen);
  1250. args[5].fd = -1;
  1251. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
  1252. if (init.attrs)
  1253. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 4, 0);
  1254. err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
  1255. sc, args);
  1256. if (err)
  1257. goto err_invoke;
  1258. kfree(args);
  1259. return 0;
  1260. err_invoke:
  1261. fl->init_mem = NULL;
  1262. fastrpc_buf_free(imem);
  1263. err_alloc:
  1264. fastrpc_map_put(map);
  1265. err:
  1266. kfree(args);
  1267. return err;
  1268. }
  1269. static struct fastrpc_session_ctx *fastrpc_session_alloc(
  1270. struct fastrpc_user *fl)
  1271. {
  1272. struct fastrpc_channel_ctx *cctx = fl->cctx;
  1273. struct fastrpc_session_ctx *session = NULL;
  1274. unsigned long flags;
  1275. int i;
  1276. spin_lock_irqsave(&cctx->lock, flags);
  1277. for (i = 0; i < cctx->sesscount; i++) {
  1278. if (!cctx->session[i].used && cctx->session[i].valid) {
  1279. cctx->session[i].used = true;
  1280. session = &cctx->session[i];
  1281. /* any non-zero ID will work, session_idx + 1 is the simplest one */
  1282. fl->client_id = i + 1;
  1283. break;
  1284. }
  1285. }
  1286. spin_unlock_irqrestore(&cctx->lock, flags);
  1287. return session;
  1288. }
  1289. static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
  1290. struct fastrpc_session_ctx *session)
  1291. {
  1292. unsigned long flags;
  1293. spin_lock_irqsave(&cctx->lock, flags);
  1294. session->used = false;
  1295. spin_unlock_irqrestore(&cctx->lock, flags);
  1296. }
  1297. static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
  1298. {
  1299. struct fastrpc_invoke_args args[1];
  1300. int client_id = 0;
  1301. u32 sc;
  1302. client_id = fl->client_id;
  1303. args[0].ptr = (u64)(uintptr_t) &client_id;
  1304. args[0].length = sizeof(client_id);
  1305. args[0].fd = -1;
  1306. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
  1307. return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
  1308. sc, &args[0]);
  1309. }
  1310. static int fastrpc_device_release(struct inode *inode, struct file *file)
  1311. {
  1312. struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
  1313. struct fastrpc_channel_ctx *cctx = fl->cctx;
  1314. struct fastrpc_invoke_ctx *ctx, *n;
  1315. struct fastrpc_map *map, *m;
  1316. struct fastrpc_buf *buf, *b;
  1317. unsigned long flags;
  1318. fastrpc_release_current_dsp_process(fl);
  1319. spin_lock_irqsave(&cctx->lock, flags);
  1320. list_del(&fl->user);
  1321. spin_unlock_irqrestore(&cctx->lock, flags);
  1322. if (fl->init_mem)
  1323. fastrpc_buf_free(fl->init_mem);
  1324. list_for_each_entry_safe(ctx, n, &fl->pending, node) {
  1325. list_del(&ctx->node);
  1326. fastrpc_context_put(ctx);
  1327. }
  1328. list_for_each_entry_safe(map, m, &fl->maps, node)
  1329. fastrpc_map_put(map);
  1330. list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
  1331. list_del(&buf->node);
  1332. fastrpc_buf_free(buf);
  1333. }
  1334. fastrpc_session_free(cctx, fl->sctx);
  1335. fastrpc_channel_ctx_put(cctx);
  1336. mutex_destroy(&fl->mutex);
  1337. kfree(fl);
  1338. file->private_data = NULL;
  1339. return 0;
  1340. }
  1341. static int fastrpc_device_open(struct inode *inode, struct file *filp)
  1342. {
  1343. struct fastrpc_channel_ctx *cctx;
  1344. struct fastrpc_device *fdevice;
  1345. struct fastrpc_user *fl = NULL;
  1346. unsigned long flags;
  1347. fdevice = miscdev_to_fdevice(filp->private_data);
  1348. cctx = fdevice->cctx;
  1349. fl = kzalloc_obj(*fl);
  1350. if (!fl)
  1351. return -ENOMEM;
  1352. /* Released in fastrpc_device_release() */
  1353. fastrpc_channel_ctx_get(cctx);
  1354. filp->private_data = fl;
  1355. spin_lock_init(&fl->lock);
  1356. mutex_init(&fl->mutex);
  1357. INIT_LIST_HEAD(&fl->pending);
  1358. INIT_LIST_HEAD(&fl->maps);
  1359. INIT_LIST_HEAD(&fl->mmaps);
  1360. INIT_LIST_HEAD(&fl->user);
  1361. fl->cctx = cctx;
  1362. fl->is_secure_dev = fdevice->secure;
  1363. fl->sctx = fastrpc_session_alloc(fl);
  1364. if (!fl->sctx) {
  1365. dev_err(&cctx->rpdev->dev, "No session available\n");
  1366. mutex_destroy(&fl->mutex);
  1367. kfree(fl);
  1368. return -EBUSY;
  1369. }
  1370. spin_lock_irqsave(&cctx->lock, flags);
  1371. list_add_tail(&fl->user, &cctx->users);
  1372. spin_unlock_irqrestore(&cctx->lock, flags);
  1373. return 0;
  1374. }
  1375. static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
  1376. {
  1377. struct fastrpc_alloc_dma_buf bp;
  1378. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  1379. struct fastrpc_buf *buf = NULL;
  1380. int err;
  1381. if (copy_from_user(&bp, argp, sizeof(bp)))
  1382. return -EFAULT;
  1383. err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
  1384. if (err)
  1385. return err;
  1386. exp_info.ops = &fastrpc_dma_buf_ops;
  1387. exp_info.size = bp.size;
  1388. exp_info.flags = O_RDWR;
  1389. exp_info.priv = buf;
  1390. buf->dmabuf = dma_buf_export(&exp_info);
  1391. if (IS_ERR(buf->dmabuf)) {
  1392. err = PTR_ERR(buf->dmabuf);
  1393. fastrpc_buf_free(buf);
  1394. return err;
  1395. }
  1396. bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
  1397. if (bp.fd < 0) {
  1398. dma_buf_put(buf->dmabuf);
  1399. return -EINVAL;
  1400. }
  1401. if (copy_to_user(argp, &bp, sizeof(bp))) {
  1402. /*
  1403. * The usercopy failed, but we can't do much about it, as
  1404. * dma_buf_fd() already called fd_install() and made the
  1405. * file descriptor accessible for the current process. It
  1406. * might already be closed and dmabuf no longer valid when
  1407. * we reach this point. Therefore "leak" the fd and rely on
  1408. * the process exit path to do any required cleanup.
  1409. */
  1410. return -EFAULT;
  1411. }
  1412. return 0;
  1413. }
  1414. static int fastrpc_init_attach(struct fastrpc_user *fl, int pd)
  1415. {
  1416. struct fastrpc_invoke_args args[1];
  1417. int client_id = fl->client_id;
  1418. u32 sc;
  1419. args[0].ptr = (u64)(uintptr_t) &client_id;
  1420. args[0].length = sizeof(client_id);
  1421. args[0].fd = -1;
  1422. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
  1423. fl->pd = pd;
  1424. return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
  1425. sc, &args[0]);
  1426. }
  1427. static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
  1428. {
  1429. struct fastrpc_invoke_args *args = NULL;
  1430. struct fastrpc_invoke inv;
  1431. u32 nscalars;
  1432. int err;
  1433. if (copy_from_user(&inv, argp, sizeof(inv)))
  1434. return -EFAULT;
  1435. /* nscalars is truncated here to max supported value */
  1436. nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
  1437. if (nscalars) {
  1438. args = kzalloc_objs(*args, nscalars);
  1439. if (!args)
  1440. return -ENOMEM;
  1441. if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
  1442. nscalars * sizeof(*args))) {
  1443. kfree(args);
  1444. return -EFAULT;
  1445. }
  1446. }
  1447. err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
  1448. kfree(args);
  1449. return err;
  1450. }
  1451. static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf,
  1452. uint32_t dsp_attr_buf_len)
  1453. {
  1454. struct fastrpc_invoke_args args[2] = { 0 };
  1455. /*
  1456. * Capability filled in userspace. This carries the information
  1457. * about the remoteproc support which is fetched from the remoteproc
  1458. * sysfs node by userspace.
  1459. */
  1460. dsp_attr_buf[0] = 0;
  1461. dsp_attr_buf_len -= 1;
  1462. args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len;
  1463. args[0].length = sizeof(dsp_attr_buf_len);
  1464. args[0].fd = -1;
  1465. args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1];
  1466. args[1].length = dsp_attr_buf_len * sizeof(u32);
  1467. args[1].fd = -1;
  1468. return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE,
  1469. FASTRPC_SCALARS(0, 1, 1), args);
  1470. }
  1471. static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap,
  1472. struct fastrpc_user *fl)
  1473. {
  1474. struct fastrpc_channel_ctx *cctx = fl->cctx;
  1475. uint32_t attribute_id = cap->attribute_id;
  1476. uint32_t *dsp_attributes;
  1477. unsigned long flags;
  1478. int err;
  1479. spin_lock_irqsave(&cctx->lock, flags);
  1480. /* check if we already have queried dsp for attributes */
  1481. if (cctx->valid_attributes) {
  1482. spin_unlock_irqrestore(&cctx->lock, flags);
  1483. goto done;
  1484. }
  1485. spin_unlock_irqrestore(&cctx->lock, flags);
  1486. dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL);
  1487. if (!dsp_attributes)
  1488. return -ENOMEM;
  1489. err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES);
  1490. if (err == DSP_UNSUPPORTED_API) {
  1491. dev_info(&cctx->rpdev->dev,
  1492. "Warning: DSP capabilities not supported\n");
  1493. kfree(dsp_attributes);
  1494. return -EOPNOTSUPP;
  1495. } else if (err) {
  1496. dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err);
  1497. kfree(dsp_attributes);
  1498. return err;
  1499. }
  1500. spin_lock_irqsave(&cctx->lock, flags);
  1501. memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
  1502. cctx->valid_attributes = true;
  1503. spin_unlock_irqrestore(&cctx->lock, flags);
  1504. kfree(dsp_attributes);
  1505. done:
  1506. cap->capability = cctx->dsp_attributes[attribute_id];
  1507. return 0;
  1508. }
  1509. static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp)
  1510. {
  1511. struct fastrpc_ioctl_capability cap = {0};
  1512. int err = 0;
  1513. if (copy_from_user(&cap, argp, sizeof(cap)))
  1514. return -EFAULT;
  1515. cap.capability = 0;
  1516. if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) {
  1517. dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n",
  1518. cap.attribute_id, err);
  1519. return -EOVERFLOW;
  1520. }
  1521. err = fastrpc_get_info_from_kernel(&cap, fl);
  1522. if (err)
  1523. return err;
  1524. if (copy_to_user(argp, &cap, sizeof(cap)))
  1525. return -EFAULT;
  1526. return 0;
  1527. }
  1528. static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf)
  1529. {
  1530. struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
  1531. struct fastrpc_munmap_req_msg req_msg;
  1532. struct device *dev = fl->sctx->dev;
  1533. int err;
  1534. u32 sc;
  1535. req_msg.client_id = fl->client_id;
  1536. req_msg.size = buf->size;
  1537. req_msg.vaddr = buf->raddr;
  1538. args[0].ptr = (u64) (uintptr_t) &req_msg;
  1539. args[0].length = sizeof(req_msg);
  1540. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0);
  1541. err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
  1542. &args[0]);
  1543. if (!err) {
  1544. dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
  1545. spin_lock(&fl->lock);
  1546. list_del(&buf->node);
  1547. spin_unlock(&fl->lock);
  1548. fastrpc_buf_free(buf);
  1549. } else {
  1550. dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr);
  1551. }
  1552. return err;
  1553. }
  1554. static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
  1555. {
  1556. struct fastrpc_buf *buf = NULL, *iter, *b;
  1557. struct fastrpc_req_munmap req;
  1558. struct device *dev = fl->sctx->dev;
  1559. if (copy_from_user(&req, argp, sizeof(req)))
  1560. return -EFAULT;
  1561. spin_lock(&fl->lock);
  1562. list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
  1563. if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
  1564. buf = iter;
  1565. break;
  1566. }
  1567. }
  1568. spin_unlock(&fl->lock);
  1569. if (!buf) {
  1570. dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n",
  1571. req.vaddrout, req.size);
  1572. return -EINVAL;
  1573. }
  1574. return fastrpc_req_munmap_impl(fl, buf);
  1575. }
  1576. static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
  1577. {
  1578. struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } };
  1579. struct fastrpc_buf *buf = NULL;
  1580. struct fastrpc_mmap_req_msg req_msg;
  1581. struct fastrpc_mmap_rsp_msg rsp_msg;
  1582. struct fastrpc_phy_page pages;
  1583. struct fastrpc_req_mmap req;
  1584. struct device *dev = fl->sctx->dev;
  1585. int err;
  1586. u32 sc;
  1587. if (copy_from_user(&req, argp, sizeof(req)))
  1588. return -EFAULT;
  1589. if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) {
  1590. dev_err(dev, "flag not supported 0x%x\n", req.flags);
  1591. return -EINVAL;
  1592. }
  1593. if (req.vaddrin) {
  1594. dev_err(dev, "adding user allocated pages is not supported\n");
  1595. return -EINVAL;
  1596. }
  1597. if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
  1598. err = fastrpc_remote_heap_alloc(fl, dev, req.size, &buf);
  1599. else
  1600. err = fastrpc_buf_alloc(fl, dev, req.size, &buf);
  1601. if (err) {
  1602. dev_err(dev, "failed to allocate buffer\n");
  1603. return err;
  1604. }
  1605. req_msg.client_id = fl->client_id;
  1606. req_msg.flags = req.flags;
  1607. req_msg.vaddr = req.vaddrin;
  1608. req_msg.num = sizeof(pages);
  1609. args[0].ptr = (u64) (uintptr_t) &req_msg;
  1610. args[0].length = sizeof(req_msg);
  1611. pages.addr = buf->dma_addr;
  1612. pages.size = buf->size;
  1613. args[1].ptr = (u64) (uintptr_t) &pages;
  1614. args[1].length = sizeof(pages);
  1615. args[2].ptr = (u64) (uintptr_t) &rsp_msg;
  1616. args[2].length = sizeof(rsp_msg);
  1617. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1);
  1618. err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
  1619. &args[0]);
  1620. if (err) {
  1621. dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
  1622. fastrpc_buf_free(buf);
  1623. return err;
  1624. }
  1625. /* update the buffer to be able to deallocate the memory on the DSP */
  1626. buf->raddr = (uintptr_t) rsp_msg.vaddr;
  1627. /* let the client know the address to use */
  1628. req.vaddrout = rsp_msg.vaddr;
  1629. /* Add memory to static PD pool, protection thru hypervisor */
  1630. if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) {
  1631. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  1632. err = qcom_scm_assign_mem(buf->dma_addr, (u64)buf->size,
  1633. &src_perms, fl->cctx->vmperms, fl->cctx->vmcount);
  1634. if (err) {
  1635. dev_err(fl->sctx->dev,
  1636. "Failed to assign memory dma_addr %pad size 0x%llx err %d",
  1637. &buf->dma_addr, buf->size, err);
  1638. goto err_assign;
  1639. }
  1640. }
  1641. spin_lock(&fl->lock);
  1642. list_add_tail(&buf->node, &fl->mmaps);
  1643. spin_unlock(&fl->lock);
  1644. if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
  1645. err = -EFAULT;
  1646. goto err_assign;
  1647. }
  1648. dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n",
  1649. buf->raddr, buf->size);
  1650. return 0;
  1651. err_assign:
  1652. fastrpc_req_munmap_impl(fl, buf);
  1653. return err;
  1654. }
  1655. static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req)
  1656. {
  1657. struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
  1658. struct fastrpc_map *map = NULL, *iter, *m;
  1659. struct fastrpc_mem_unmap_req_msg req_msg = { 0 };
  1660. int err = 0;
  1661. u32 sc;
  1662. struct device *dev = fl->sctx->dev;
  1663. spin_lock(&fl->lock);
  1664. list_for_each_entry_safe(iter, m, &fl->maps, node) {
  1665. if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
  1666. map = iter;
  1667. break;
  1668. }
  1669. }
  1670. spin_unlock(&fl->lock);
  1671. if (!map) {
  1672. dev_err(dev, "map not in list\n");
  1673. return -EINVAL;
  1674. }
  1675. req_msg.client_id = fl->client_id;
  1676. req_msg.len = map->len;
  1677. req_msg.vaddrin = map->raddr;
  1678. req_msg.fd = map->fd;
  1679. args[0].ptr = (u64) (uintptr_t) &req_msg;
  1680. args[0].length = sizeof(req_msg);
  1681. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0);
  1682. err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
  1683. &args[0]);
  1684. if (err) {
  1685. dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr);
  1686. return err;
  1687. }
  1688. fastrpc_map_put(map);
  1689. return 0;
  1690. }
  1691. static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp)
  1692. {
  1693. struct fastrpc_mem_unmap req;
  1694. if (copy_from_user(&req, argp, sizeof(req)))
  1695. return -EFAULT;
  1696. return fastrpc_req_mem_unmap_impl(fl, &req);
  1697. }
  1698. static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
  1699. {
  1700. struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } };
  1701. struct fastrpc_mem_map_req_msg req_msg = { 0 };
  1702. struct fastrpc_mmap_rsp_msg rsp_msg = { 0 };
  1703. struct fastrpc_mem_unmap req_unmap = { 0 };
  1704. struct fastrpc_phy_page pages = { 0 };
  1705. struct fastrpc_mem_map req;
  1706. struct device *dev = fl->sctx->dev;
  1707. struct fastrpc_map *map = NULL;
  1708. int err;
  1709. u32 sc;
  1710. if (copy_from_user(&req, argp, sizeof(req)))
  1711. return -EFAULT;
  1712. /* create SMMU mapping */
  1713. err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
  1714. if (err) {
  1715. dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
  1716. return err;
  1717. }
  1718. req_msg.client_id = fl->client_id;
  1719. req_msg.fd = req.fd;
  1720. req_msg.offset = req.offset;
  1721. req_msg.vaddrin = req.vaddrin;
  1722. map->va = (void *) (uintptr_t) req.vaddrin;
  1723. req_msg.flags = req.flags;
  1724. req_msg.num = sizeof(pages);
  1725. req_msg.data_len = 0;
  1726. args[0].ptr = (u64) (uintptr_t) &req_msg;
  1727. args[0].length = sizeof(req_msg);
  1728. pages.addr = map->dma_addr;
  1729. pages.size = map->len;
  1730. args[1].ptr = (u64) (uintptr_t) &pages;
  1731. args[1].length = sizeof(pages);
  1732. args[2].ptr = (u64) (uintptr_t) &pages;
  1733. args[2].length = 0;
  1734. args[3].ptr = (u64) (uintptr_t) &rsp_msg;
  1735. args[3].length = sizeof(rsp_msg);
  1736. sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1);
  1737. err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]);
  1738. if (err) {
  1739. dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n",
  1740. req.fd, req.vaddrin, map->len);
  1741. goto err_invoke;
  1742. }
  1743. /* update the buffer to be able to deallocate the memory on the DSP */
  1744. map->raddr = rsp_msg.vaddr;
  1745. /* let the client know the address to use */
  1746. req.vaddrout = rsp_msg.vaddr;
  1747. if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
  1748. /* unmap the memory and release the buffer */
  1749. req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr;
  1750. req_unmap.length = map->len;
  1751. fastrpc_req_mem_unmap_impl(fl, &req_unmap);
  1752. return -EFAULT;
  1753. }
  1754. return 0;
  1755. err_invoke:
  1756. fastrpc_map_put(map);
  1757. return err;
  1758. }
  1759. static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
  1760. unsigned long arg)
  1761. {
  1762. struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
  1763. char __user *argp = (char __user *)arg;
  1764. int err;
  1765. switch (cmd) {
  1766. case FASTRPC_IOCTL_INVOKE:
  1767. err = fastrpc_invoke(fl, argp);
  1768. break;
  1769. case FASTRPC_IOCTL_INIT_ATTACH:
  1770. err = fastrpc_init_attach(fl, ROOT_PD);
  1771. break;
  1772. case FASTRPC_IOCTL_INIT_ATTACH_SNS:
  1773. err = fastrpc_init_attach(fl, SENSORS_PD);
  1774. break;
  1775. case FASTRPC_IOCTL_INIT_CREATE_STATIC:
  1776. err = fastrpc_init_create_static_process(fl, argp);
  1777. break;
  1778. case FASTRPC_IOCTL_INIT_CREATE:
  1779. err = fastrpc_init_create_process(fl, argp);
  1780. break;
  1781. case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
  1782. err = fastrpc_dmabuf_alloc(fl, argp);
  1783. break;
  1784. case FASTRPC_IOCTL_MMAP:
  1785. err = fastrpc_req_mmap(fl, argp);
  1786. break;
  1787. case FASTRPC_IOCTL_MUNMAP:
  1788. err = fastrpc_req_munmap(fl, argp);
  1789. break;
  1790. case FASTRPC_IOCTL_MEM_MAP:
  1791. err = fastrpc_req_mem_map(fl, argp);
  1792. break;
  1793. case FASTRPC_IOCTL_MEM_UNMAP:
  1794. err = fastrpc_req_mem_unmap(fl, argp);
  1795. break;
  1796. case FASTRPC_IOCTL_GET_DSP_INFO:
  1797. err = fastrpc_get_dsp_info(fl, argp);
  1798. break;
  1799. default:
  1800. err = -ENOTTY;
  1801. break;
  1802. }
  1803. return err;
  1804. }
  1805. static const struct file_operations fastrpc_fops = {
  1806. .open = fastrpc_device_open,
  1807. .release = fastrpc_device_release,
  1808. .unlocked_ioctl = fastrpc_device_ioctl,
  1809. .compat_ioctl = fastrpc_device_ioctl,
  1810. };
  1811. static int fastrpc_cb_probe(struct platform_device *pdev)
  1812. {
  1813. struct fastrpc_channel_ctx *cctx;
  1814. struct fastrpc_session_ctx *sess;
  1815. struct device *dev = &pdev->dev;
  1816. int i, sessions = 0;
  1817. unsigned long flags;
  1818. int rc;
  1819. u32 dma_bits;
  1820. cctx = dev_get_drvdata(dev->parent);
  1821. if (!cctx)
  1822. return -EINVAL;
  1823. of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
  1824. spin_lock_irqsave(&cctx->lock, flags);
  1825. if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
  1826. dev_err(&pdev->dev, "too many sessions\n");
  1827. spin_unlock_irqrestore(&cctx->lock, flags);
  1828. return -ENOSPC;
  1829. }
  1830. dma_bits = cctx->soc_data->dma_addr_bits_default;
  1831. sess = &cctx->session[cctx->sesscount++];
  1832. sess->used = false;
  1833. sess->valid = true;
  1834. sess->dev = dev;
  1835. dev_set_drvdata(dev, sess);
  1836. if (cctx->domain_id == CDSP_DOMAIN_ID)
  1837. dma_bits = cctx->soc_data->dma_addr_bits_cdsp;
  1838. if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
  1839. dev_info(dev, "FastRPC Session ID not specified in DT\n");
  1840. if (sessions > 0) {
  1841. struct fastrpc_session_ctx *dup_sess;
  1842. for (i = 1; i < sessions; i++) {
  1843. if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
  1844. break;
  1845. dup_sess = &cctx->session[cctx->sesscount++];
  1846. memcpy(dup_sess, sess, sizeof(*dup_sess));
  1847. }
  1848. }
  1849. spin_unlock_irqrestore(&cctx->lock, flags);
  1850. rc = dma_set_mask(dev, DMA_BIT_MASK(dma_bits));
  1851. if (rc) {
  1852. dev_err(dev, "%u-bit DMA enable failed\n", dma_bits);
  1853. return rc;
  1854. }
  1855. return 0;
  1856. }
  1857. static void fastrpc_cb_remove(struct platform_device *pdev)
  1858. {
  1859. struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
  1860. struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
  1861. unsigned long flags;
  1862. int i;
  1863. spin_lock_irqsave(&cctx->lock, flags);
  1864. for (i = 0; i < FASTRPC_MAX_SESSIONS; i++) {
  1865. if (cctx->session[i].sid == sess->sid) {
  1866. cctx->session[i].valid = false;
  1867. cctx->sesscount--;
  1868. }
  1869. }
  1870. spin_unlock_irqrestore(&cctx->lock, flags);
  1871. }
  1872. static const struct of_device_id fastrpc_match_table[] = {
  1873. { .compatible = "qcom,fastrpc-compute-cb", },
  1874. {}
  1875. };
  1876. static struct platform_driver fastrpc_cb_driver = {
  1877. .probe = fastrpc_cb_probe,
  1878. .remove = fastrpc_cb_remove,
  1879. .driver = {
  1880. .name = "qcom,fastrpc-cb",
  1881. .of_match_table = fastrpc_match_table,
  1882. .suppress_bind_attrs = true,
  1883. },
  1884. };
  1885. static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx,
  1886. bool is_secured, const char *domain)
  1887. {
  1888. struct fastrpc_device *fdev;
  1889. int err;
  1890. fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL);
  1891. if (!fdev)
  1892. return -ENOMEM;
  1893. fdev->secure = is_secured;
  1894. fdev->cctx = cctx;
  1895. fdev->miscdev.minor = MISC_DYNAMIC_MINOR;
  1896. fdev->miscdev.fops = &fastrpc_fops;
  1897. fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s",
  1898. domain, is_secured ? "-secure" : "");
  1899. if (!fdev->miscdev.name)
  1900. return -ENOMEM;
  1901. err = misc_register(&fdev->miscdev);
  1902. if (!err) {
  1903. if (is_secured)
  1904. cctx->secure_fdevice = fdev;
  1905. else
  1906. cctx->fdevice = fdev;
  1907. }
  1908. return err;
  1909. }
  1910. static int fastrpc_get_domain_id(const char *domain)
  1911. {
  1912. if (!strncmp(domain, "adsp", 4))
  1913. return ADSP_DOMAIN_ID;
  1914. else if (!strncmp(domain, "cdsp", 4))
  1915. return CDSP_DOMAIN_ID;
  1916. else if (!strncmp(domain, "mdsp", 4))
  1917. return MDSP_DOMAIN_ID;
  1918. else if (!strncmp(domain, "sdsp", 4))
  1919. return SDSP_DOMAIN_ID;
  1920. else if (!strncmp(domain, "gdsp", 4))
  1921. return GDSP_DOMAIN_ID;
  1922. return -EINVAL;
  1923. }
  1924. static const struct fastrpc_soc_data kaanapali_soc_data = {
  1925. .sid_pos = 56,
  1926. .dma_addr_bits_cdsp = 34,
  1927. .dma_addr_bits_default = 32,
  1928. };
  1929. static const struct fastrpc_soc_data default_soc_data = {
  1930. .sid_pos = 32,
  1931. .dma_addr_bits_cdsp = 32,
  1932. .dma_addr_bits_default = 32,
  1933. };
  1934. static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
  1935. {
  1936. struct device *rdev = &rpdev->dev;
  1937. struct fastrpc_channel_ctx *data;
  1938. int i, err, domain_id = -1, vmcount;
  1939. const char *domain;
  1940. bool secure_dsp;
  1941. unsigned int vmids[FASTRPC_MAX_VMIDS];
  1942. const struct fastrpc_soc_data *soc_data;
  1943. soc_data = device_get_match_data(rdev);
  1944. err = of_property_read_string(rdev->of_node, "label", &domain);
  1945. if (err) {
  1946. dev_info(rdev, "FastRPC Domain not specified in DT\n");
  1947. return err;
  1948. }
  1949. domain_id = fastrpc_get_domain_id(domain);
  1950. if (domain_id < 0) {
  1951. dev_info(rdev, "FastRPC Domain %s not supported\n", domain);
  1952. return -EINVAL;
  1953. }
  1954. if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0))
  1955. dev_info(rdev, "no reserved DMA memory for FASTRPC\n");
  1956. vmcount = of_property_read_variable_u32_array(rdev->of_node,
  1957. "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS);
  1958. if (vmcount < 0)
  1959. vmcount = 0;
  1960. else if (!qcom_scm_is_available())
  1961. return -EPROBE_DEFER;
  1962. data = kzalloc_obj(*data);
  1963. if (!data)
  1964. return -ENOMEM;
  1965. if (vmcount) {
  1966. data->vmcount = vmcount;
  1967. for (i = 0; i < data->vmcount; i++) {
  1968. data->vmperms[i].vmid = vmids[i];
  1969. data->vmperms[i].perm = QCOM_SCM_PERM_RWX;
  1970. }
  1971. }
  1972. if (domain_id == SDSP_DOMAIN_ID) {
  1973. struct resource res;
  1974. u64 src_perms;
  1975. err = of_reserved_mem_region_to_resource(rdev->of_node, 0, &res);
  1976. if (!err) {
  1977. src_perms = BIT(QCOM_SCM_VMID_HLOS);
  1978. err = qcom_scm_assign_mem(res.start, resource_size(&res), &src_perms,
  1979. data->vmperms, data->vmcount);
  1980. if (err)
  1981. goto err_free_data;
  1982. }
  1983. }
  1984. secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain"));
  1985. data->secure = secure_dsp;
  1986. data->soc_data = soc_data;
  1987. switch (domain_id) {
  1988. case ADSP_DOMAIN_ID:
  1989. case MDSP_DOMAIN_ID:
  1990. case SDSP_DOMAIN_ID:
  1991. /* Unsigned PD offloading is only supported on CDSP and GDSP */
  1992. data->unsigned_support = false;
  1993. err = fastrpc_device_register(rdev, data, secure_dsp, domain);
  1994. if (err)
  1995. goto err_free_data;
  1996. break;
  1997. case CDSP_DOMAIN_ID:
  1998. case GDSP_DOMAIN_ID:
  1999. data->unsigned_support = true;
  2000. /* Create both device nodes so that we can allow both Signed and Unsigned PD */
  2001. err = fastrpc_device_register(rdev, data, true, domain);
  2002. if (err)
  2003. goto err_free_data;
  2004. err = fastrpc_device_register(rdev, data, false, domain);
  2005. if (err)
  2006. goto err_deregister_fdev;
  2007. break;
  2008. default:
  2009. err = -EINVAL;
  2010. goto err_free_data;
  2011. }
  2012. kref_init(&data->refcount);
  2013. dev_set_drvdata(&rpdev->dev, data);
  2014. rdev->dma_mask = &data->dma_mask;
  2015. dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
  2016. INIT_LIST_HEAD(&data->users);
  2017. INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
  2018. spin_lock_init(&data->lock);
  2019. idr_init(&data->ctx_idr);
  2020. data->domain_id = domain_id;
  2021. data->rpdev = rpdev;
  2022. err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
  2023. if (err)
  2024. goto err_deregister_fdev;
  2025. return 0;
  2026. err_deregister_fdev:
  2027. if (data->fdevice)
  2028. misc_deregister(&data->fdevice->miscdev);
  2029. if (data->secure_fdevice)
  2030. misc_deregister(&data->secure_fdevice->miscdev);
  2031. err_free_data:
  2032. kfree(data);
  2033. return err;
  2034. }
  2035. static void fastrpc_notify_users(struct fastrpc_user *user)
  2036. {
  2037. struct fastrpc_invoke_ctx *ctx;
  2038. spin_lock(&user->lock);
  2039. list_for_each_entry(ctx, &user->pending, node) {
  2040. ctx->retval = -EPIPE;
  2041. complete(&ctx->work);
  2042. }
  2043. spin_unlock(&user->lock);
  2044. }
  2045. static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
  2046. {
  2047. struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
  2048. struct fastrpc_buf *buf, *b;
  2049. struct fastrpc_user *user;
  2050. unsigned long flags;
  2051. /* No invocations past this point */
  2052. spin_lock_irqsave(&cctx->lock, flags);
  2053. cctx->rpdev = NULL;
  2054. list_for_each_entry(user, &cctx->users, user)
  2055. fastrpc_notify_users(user);
  2056. spin_unlock_irqrestore(&cctx->lock, flags);
  2057. if (cctx->fdevice)
  2058. misc_deregister(&cctx->fdevice->miscdev);
  2059. if (cctx->secure_fdevice)
  2060. misc_deregister(&cctx->secure_fdevice->miscdev);
  2061. list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
  2062. list_del(&buf->node);
  2063. if (cctx->remote_heap)
  2064. fastrpc_buf_free(cctx->remote_heap);
  2065. of_platform_depopulate(&rpdev->dev);
  2066. fastrpc_channel_ctx_put(cctx);
  2067. }
  2068. static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
  2069. int len, void *priv, u32 addr)
  2070. {
  2071. struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
  2072. struct fastrpc_invoke_rsp *rsp = data;
  2073. struct fastrpc_invoke_ctx *ctx;
  2074. unsigned long flags;
  2075. unsigned long ctxid;
  2076. if (len < sizeof(*rsp))
  2077. return -EINVAL;
  2078. ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
  2079. spin_lock_irqsave(&cctx->lock, flags);
  2080. ctx = idr_find(&cctx->ctx_idr, ctxid);
  2081. spin_unlock_irqrestore(&cctx->lock, flags);
  2082. if (!ctx) {
  2083. dev_err(&rpdev->dev, "No context ID matches response\n");
  2084. return -ENOENT;
  2085. }
  2086. ctx->retval = rsp->retval;
  2087. complete(&ctx->work);
  2088. /*
  2089. * The DMA buffer associated with the context cannot be freed in
  2090. * interrupt context so schedule it through a worker thread to
  2091. * avoid a kernel BUG.
  2092. */
  2093. schedule_work(&ctx->put_work);
  2094. return 0;
  2095. }
  2096. static const struct of_device_id fastrpc_rpmsg_of_match[] = {
  2097. { .compatible = "qcom,kaanapali-fastrpc", .data = &kaanapali_soc_data },
  2098. { .compatible = "qcom,fastrpc", .data = &default_soc_data },
  2099. { },
  2100. };
  2101. MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
  2102. static struct rpmsg_driver fastrpc_driver = {
  2103. .probe = fastrpc_rpmsg_probe,
  2104. .remove = fastrpc_rpmsg_remove,
  2105. .callback = fastrpc_rpmsg_callback,
  2106. .drv = {
  2107. .name = "qcom,fastrpc",
  2108. .of_match_table = fastrpc_rpmsg_of_match,
  2109. },
  2110. };
  2111. static int fastrpc_init(void)
  2112. {
  2113. int ret;
  2114. ret = platform_driver_register(&fastrpc_cb_driver);
  2115. if (ret < 0) {
  2116. pr_err("fastrpc: failed to register cb driver\n");
  2117. return ret;
  2118. }
  2119. ret = register_rpmsg_driver(&fastrpc_driver);
  2120. if (ret < 0) {
  2121. pr_err("fastrpc: failed to register rpmsg driver\n");
  2122. platform_driver_unregister(&fastrpc_cb_driver);
  2123. return ret;
  2124. }
  2125. return 0;
  2126. }
  2127. module_init(fastrpc_init);
  2128. static void fastrpc_exit(void)
  2129. {
  2130. platform_driver_unregister(&fastrpc_cb_driver);
  2131. unregister_rpmsg_driver(&fastrpc_driver);
  2132. }
  2133. module_exit(fastrpc_exit);
  2134. MODULE_DESCRIPTION("Qualcomm FastRPC");
  2135. MODULE_LICENSE("GPL v2");
  2136. MODULE_IMPORT_NS("DMA_BUF");