adsprpc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*
  2. * Copyright (c) 2012-2019 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include "adsprpc_shared.h"
  15. #include <linux/slab.h>
  16. #include <linux/completion.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/mm.h>
  19. #include <linux/fs.h>
  20. #include <linux/sched.h>
  21. #include <linux/module.h>
  22. #include <linux/cdev.h>
  23. #include <linux/list.h>
  24. #include <linux/hash.h>
  25. #include <linux/msm_ion.h>
  26. #include <mach/msm_smd.h>
  27. #include <mach/ion.h>
  28. #include <mach/iommu_domains.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/fs.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/device.h>
  33. #include <linux/of.h>
  34. #include <linux/iommu.h>
  35. #include <linux/kref.h>
  36. #ifndef ION_ADSPRPC_HEAP_ID
  37. #define ION_ADSPRPC_HEAP_ID ION_AUDIO_HEAP_ID
  38. #endif /*ION_ADSPRPC_HEAP_ID*/
  39. #define RPC_TIMEOUT (5 * HZ)
  40. #define RPC_HASH_BITS 5
  41. #define RPC_HASH_SZ (1 << RPC_HASH_BITS)
  42. #define BALIGN 32
  43. #define NUM_CHANNELS 1
  44. #define FASTRPC_STATIC_HANDLE_KERNEL (1)
  45. #define LOCK_MMAP(kernel)\
  46. do {\
  47. if (!kernel)\
  48. down_read(&current->mm->mmap_sem);\
  49. } while (0)
  50. #define UNLOCK_MMAP(kernel)\
  51. do {\
  52. if (!kernel)\
  53. up_read(&current->mm->mmap_sem);\
  54. } while (0)
  55. #define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
  56. static inline uint32_t buf_page_start(void *buf)
  57. {
  58. uint32_t start = (uint32_t) buf & PAGE_MASK;
  59. return start;
  60. }
  61. static inline uint32_t buf_page_offset(void *buf)
  62. {
  63. uint32_t offset = (uint32_t) buf & (PAGE_SIZE - 1);
  64. return offset;
  65. }
  66. static inline int buf_num_pages(void *buf, size_t len)
  67. {
  68. uint32_t start = buf_page_start(buf) >> PAGE_SHIFT;
  69. uint32_t end = (((uint32_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
  70. int nPages = end - start + 1;
  71. return nPages;
  72. }
  73. static inline uint32_t buf_page_size(uint32_t size)
  74. {
  75. uint32_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
  76. return sz > PAGE_SIZE ? sz : PAGE_SIZE;
  77. }
  78. static inline int buf_get_pages(void *addr, int sz, int nr_pages, int access,
  79. struct smq_phy_page *pages, int nr_elems)
  80. {
  81. struct vm_area_struct *vma, *vmaend;
  82. uint32_t start = buf_page_start(addr);
  83. uint32_t end = buf_page_start((void *)((uint32_t)addr + sz - 1));
  84. uint32_t len = nr_pages << PAGE_SHIFT;
  85. unsigned long pfn, pfnend;
  86. int n = -1, err = 0;
  87. VERIFY(err, 0 != access_ok(access ? VERIFY_WRITE : VERIFY_READ,
  88. (void __user *)start, len));
  89. if (err)
  90. goto bail;
  91. VERIFY(err, 0 != (vma = find_vma(current->mm, start)));
  92. if (err)
  93. goto bail;
  94. VERIFY(err, 0 != (vmaend = find_vma(current->mm, end)));
  95. if (err)
  96. goto bail;
  97. n = 0;
  98. VERIFY(err, 0 == follow_pfn(vma, start, &pfn));
  99. if (err)
  100. goto bail;
  101. VERIFY(err, 0 == follow_pfn(vmaend, end, &pfnend));
  102. if (err)
  103. goto bail;
  104. VERIFY(err, (pfn + nr_pages - 1) == pfnend);
  105. if (err)
  106. goto bail;
  107. VERIFY(err, nr_elems > 0);
  108. if (err)
  109. goto bail;
  110. pages->addr = __pfn_to_phys(pfn);
  111. pages->size = len;
  112. n++;
  113. bail:
  114. return n;
  115. }
  116. struct fastrpc_buf {
  117. struct ion_handle *handle;
  118. void *virt;
  119. ion_phys_addr_t phys;
  120. size_t size;
  121. int used;
  122. };
  123. struct smq_context_list;
  124. struct smq_invoke_ctx {
  125. struct hlist_node hn;
  126. struct completion work;
  127. int retval;
  128. int cid;
  129. int pid;
  130. remote_arg_t *pra;
  131. remote_arg_t *rpra;
  132. struct fastrpc_buf obuf;
  133. struct fastrpc_buf *abufs;
  134. struct fastrpc_device *dev;
  135. struct fastrpc_apps *apps;
  136. int* fds;
  137. struct ion_handle** handles;
  138. int nbufs;
  139. bool smmu;
  140. uint32_t sc;
  141. };
  142. struct smq_context_list {
  143. struct hlist_head pending;
  144. struct hlist_head interrupted;
  145. spinlock_t hlock;
  146. };
  147. struct fastrpc_smmu {
  148. struct iommu_group *group;
  149. struct iommu_domain *domain;
  150. int domain_id;
  151. bool enabled;
  152. };
  153. struct fastrpc_channel_context {
  154. smd_channel_t *chan;
  155. struct device *dev;
  156. struct completion work;
  157. struct fastrpc_smmu smmu;
  158. struct kref kref;
  159. };
  160. struct fastrpc_apps {
  161. struct fastrpc_channel_context channel[NUM_CHANNELS];
  162. struct smq_context_list clst;
  163. struct ion_client *iclient;
  164. struct cdev cdev;
  165. struct class *class;
  166. struct mutex smd_mutex;
  167. dev_t dev_no;
  168. spinlock_t wrlock;
  169. spinlock_t hlock;
  170. struct hlist_head htbl[RPC_HASH_SZ];
  171. };
  172. struct fastrpc_mmap {
  173. struct hlist_node hn;
  174. struct ion_handle *handle;
  175. void *virt;
  176. ion_phys_addr_t phys;
  177. uint32_t vaddrin;
  178. uint32_t vaddrout;
  179. size_t size;
  180. };
  181. struct file_data {
  182. spinlock_t hlock;
  183. struct hlist_head hlst;
  184. uint32_t mode;
  185. int cid;
  186. struct mutex map_mutex;
  187. };
  188. struct fastrpc_device {
  189. uint32_t tgid;
  190. struct hlist_node hn;
  191. struct fastrpc_buf buf;
  192. };
  193. struct fastrpc_channel_info {
  194. char *name;
  195. char *node;
  196. char *group;
  197. int channel;
  198. };
  199. static struct fastrpc_apps gfa;
  200. static const struct fastrpc_channel_info gcinfo[NUM_CHANNELS] = {
  201. {
  202. .name = "adsprpc-smd",
  203. .node = "qcom,msm-audio-ion",
  204. .group = "lpass_audio",
  205. .channel = SMD_APPS_QDSP,
  206. }
  207. };
  208. static void free_mem(struct fastrpc_buf *buf, int cid)
  209. {
  210. struct fastrpc_apps *me = &gfa;
  211. if (!IS_ERR_OR_NULL(buf->handle)) {
  212. if (me->channel[cid].smmu.enabled && buf->phys) {
  213. ion_unmap_iommu(me->iclient, buf->handle,
  214. me->channel[cid].smmu.domain_id, 0);
  215. buf->phys = 0;
  216. }
  217. if (!IS_ERR_OR_NULL(buf->virt)) {
  218. ion_unmap_kernel(me->iclient, buf->handle);
  219. buf->virt = NULL;
  220. }
  221. ion_free(me->iclient, buf->handle);
  222. buf->handle = NULL;
  223. }
  224. }
  225. static void free_map(struct fastrpc_mmap *map, int cid)
  226. {
  227. struct fastrpc_apps *me = &gfa;
  228. if (!IS_ERR_OR_NULL(map->handle)) {
  229. if (me->channel[cid].smmu.enabled && map->phys) {
  230. ion_unmap_iommu(me->iclient, map->handle,
  231. me->channel[cid].smmu.domain_id, 0);
  232. map->phys = 0;
  233. }
  234. if (!IS_ERR_OR_NULL(map->virt)) {
  235. ion_unmap_kernel(me->iclient, map->handle);
  236. map->virt = NULL;
  237. }
  238. ion_free(me->iclient, map->handle);
  239. }
  240. map->handle = NULL;
  241. }
  242. static int alloc_mem(struct fastrpc_buf *buf, int cid)
  243. {
  244. struct fastrpc_apps *me = &gfa;
  245. struct ion_client *clnt = gfa.iclient;
  246. struct sg_table *sg;
  247. int err = 0;
  248. unsigned int heap;
  249. unsigned long len;
  250. buf->handle = NULL;
  251. buf->virt = NULL;
  252. buf->phys = 0;
  253. heap = me->channel[cid].smmu.enabled ? ION_HEAP(ION_IOMMU_HEAP_ID) :
  254. ION_HEAP(ION_ADSP_HEAP_ID) | ION_HEAP(ION_AUDIO_HEAP_ID);
  255. buf->handle = ion_alloc(clnt, buf->size, SZ_4K, heap, ION_FLAG_CACHED);
  256. VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle));
  257. if (err)
  258. goto bail;
  259. buf->virt = ion_map_kernel(clnt, buf->handle);
  260. VERIFY(err, 0 == IS_ERR_OR_NULL(buf->virt));
  261. if (err)
  262. goto bail;
  263. if (me->channel[cid].smmu.enabled) {
  264. len = buf->size;
  265. VERIFY(err, 0 == ion_map_iommu(clnt, buf->handle,
  266. me->channel[cid].smmu.domain_id, 0,
  267. SZ_4K, 0, &buf->phys, &len, 0, 0));
  268. if (err)
  269. goto bail;
  270. } else {
  271. VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle)));
  272. if (err)
  273. goto bail;
  274. buf->phys = sg_dma_address(sg->sgl);
  275. }
  276. bail:
  277. if (err && !IS_ERR_OR_NULL(buf->handle))
  278. free_mem(buf, cid);
  279. return err;
  280. }
  281. static void context_list_ctor(struct smq_context_list *me)
  282. {
  283. INIT_HLIST_HEAD(&me->interrupted);
  284. INIT_HLIST_HEAD(&me->pending);
  285. spin_lock_init(&me->hlock);
  286. }
  287. static void context_free(struct smq_invoke_ctx *ctx, bool lock);
  288. static void context_list_dtor(struct fastrpc_apps *me, struct smq_context_list *clst) {
  289. struct smq_invoke_ctx *ictx = NULL;
  290. struct hlist_node *pos, *n;
  291. spin_lock(&clst->hlock);
  292. hlist_for_each_entry_safe(ictx, pos, n, &clst->interrupted, hn) {
  293. context_free(ictx, 0);
  294. }
  295. hlist_for_each_entry_safe(ictx, pos, n, &clst->pending, hn) {
  296. context_free(ictx, 0);
  297. }
  298. spin_unlock(&clst->hlock);
  299. }
  300. static int context_restore_interrupted(struct fastrpc_apps *me,
  301. struct fastrpc_ioctl_invoke_fd *invokefd,
  302. int cid, struct smq_invoke_ctx **po)
  303. {
  304. int err = 0;
  305. struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
  306. struct hlist_node *pos, *n;
  307. struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
  308. spin_lock(&me->clst.hlock);
  309. hlist_for_each_entry_safe(ictx, pos, n, &me->clst.interrupted, hn) {
  310. if(ictx->pid == current->pid) {
  311. if(invoke->sc != ictx->sc || ictx->cid != cid)
  312. err = -1;
  313. else {
  314. ctx = ictx;
  315. hlist_del(&ctx->hn);
  316. hlist_add_head(&ctx->hn, &me->clst.pending);
  317. }
  318. break;
  319. }
  320. }
  321. spin_unlock(&me->clst.hlock);
  322. if(ctx) {
  323. *po = ctx;
  324. }
  325. return err;
  326. }
  327. static int context_alloc(struct fastrpc_apps *me, uint32_t kernel,
  328. struct fastrpc_ioctl_invoke_fd *invokefd,
  329. int cid,
  330. struct smq_invoke_ctx **po)
  331. {
  332. int err = 0, bufs, size = 0;
  333. struct smq_invoke_ctx *ctx = NULL;
  334. struct smq_context_list *clst = &me->clst;
  335. struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
  336. bufs = REMOTE_SCALARS_INBUFS(invoke->sc) +
  337. REMOTE_SCALARS_OUTBUFS(invoke->sc);
  338. if (bufs) {
  339. size = bufs * sizeof(*ctx->pra);
  340. if (invokefd->fds)
  341. size = size + bufs * sizeof(*ctx->fds) +
  342. bufs * sizeof(*ctx->handles);
  343. }
  344. VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
  345. if (err)
  346. goto bail;
  347. INIT_HLIST_NODE(&ctx->hn);
  348. hlist_add_fake(&ctx->hn);
  349. ctx->pra = (remote_arg_t *)(&ctx[1]);
  350. ctx->fds = invokefd->fds == 0 ? 0 : (int *)(&ctx->pra[bufs]);
  351. ctx->handles = invokefd->fds == 0 ? 0 :(struct ion_handle **)(&ctx->fds[bufs]);
  352. if (!kernel) {
  353. VERIFY(err, 0 == copy_from_user(ctx->pra, invoke->pra,
  354. bufs * sizeof(*ctx->pra)));
  355. if (err)
  356. goto bail;
  357. } else {
  358. memmove(ctx->pra, invoke->pra, bufs * sizeof(*ctx->pra));
  359. }
  360. if (invokefd->fds) {
  361. if (!kernel) {
  362. VERIFY(err, 0 == copy_from_user(ctx->fds, invokefd->fds,
  363. bufs * sizeof(*ctx->fds)));
  364. if (err)
  365. goto bail;
  366. } else {
  367. memmove(ctx->fds, invokefd->fds,
  368. bufs * sizeof(*ctx->fds));
  369. }
  370. }
  371. ctx->sc = invoke->sc;
  372. ctx->retval = -1;
  373. ctx->cid = cid;
  374. ctx->pid = current->pid;
  375. ctx->apps = me;
  376. init_completion(&ctx->work);
  377. spin_lock(&clst->hlock);
  378. hlist_add_head(&ctx->hn, &clst->pending);
  379. spin_unlock(&clst->hlock);
  380. *po = ctx;
  381. bail:
  382. if(ctx && err)
  383. kfree(ctx);
  384. return err;
  385. }
  386. static void context_save_interrupted(struct smq_invoke_ctx *ctx)
  387. {
  388. struct smq_context_list *clst = &ctx->apps->clst;
  389. spin_lock(&clst->hlock);
  390. hlist_del(&ctx->hn);
  391. hlist_add_head(&ctx->hn, &clst->interrupted);
  392. spin_unlock(&clst->hlock);
  393. }
  394. static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev);
  395. static void context_free(struct smq_invoke_ctx *ctx, bool lock)
  396. {
  397. struct smq_context_list *clst = &ctx->apps->clst;
  398. struct fastrpc_apps *apps = ctx->apps;
  399. struct fastrpc_buf *b;
  400. int i, bufs;
  401. if (ctx->smmu) {
  402. bufs = REMOTE_SCALARS_INBUFS(ctx->sc) + REMOTE_SCALARS_OUTBUFS(ctx->sc);
  403. if (ctx->fds) {
  404. for (i = 0; i < bufs; i++)
  405. if (!IS_ERR_OR_NULL(ctx->handles[i])) {
  406. ion_unmap_iommu(apps->iclient, ctx->handles[i],
  407. apps->channel[ctx->cid].smmu.domain_id,
  408. 0);
  409. ion_free(apps->iclient, ctx->handles[i]);
  410. }
  411. }
  412. iommu_detach_group(apps->channel[ctx->cid].smmu.domain,
  413. apps->channel[ctx->cid].smmu.group);
  414. }
  415. for (i = 0, b = ctx->abufs; i < ctx->nbufs; ++i, ++b)
  416. free_mem(b, ctx->cid);
  417. kfree(ctx->abufs);
  418. if (ctx->dev) {
  419. add_dev(apps, ctx->dev);
  420. if (ctx->obuf.handle != ctx->dev->buf.handle)
  421. free_mem(&ctx->obuf, ctx->cid);
  422. }
  423. if(lock) {
  424. spin_lock(&clst->hlock);
  425. }
  426. hlist_del(&ctx->hn);
  427. if(lock) {
  428. spin_unlock(&clst->hlock);
  429. }
  430. kfree(ctx);
  431. }
  432. static void context_notify_user(struct smq_invoke_ctx *me, int retval)
  433. {
  434. me->retval = retval;
  435. complete(&me->work);
  436. }
  437. static void context_notify_all_users(struct smq_context_list *me, int cid)
  438. {
  439. struct smq_invoke_ctx *ictx = NULL;
  440. struct hlist_node *pos, *n;
  441. spin_lock(&me->hlock);
  442. hlist_for_each_entry_safe(ictx, pos, n, &me->pending, hn) {
  443. if(ictx->cid == cid) {
  444. complete(&ictx->work);
  445. }
  446. }
  447. hlist_for_each_entry_safe(ictx, pos, n, &me->interrupted, hn) {
  448. if(ictx->cid == cid) {
  449. complete(&ictx->work);
  450. }
  451. }
  452. spin_unlock(&me->hlock);
  453. }
  454. static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
  455. struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf, int cid)
  456. {
  457. struct smq_phy_page *pgstart, *pages;
  458. struct smq_invoke_buf *list;
  459. size_t rlen;
  460. int i, err = 0;
  461. int inbufs = REMOTE_SCALARS_INBUFS(sc);
  462. int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  463. LOCK_MMAP(kernel);
  464. *obuf = *ibuf;
  465. retry:
  466. list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc);
  467. pgstart = smq_phy_page_start(sc, list);
  468. pages = pgstart + 1;
  469. rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt);
  470. if (rlen < 0) {
  471. rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size;
  472. obuf->size += buf_page_size(rlen);
  473. VERIFY(err, 0 == alloc_mem(obuf, cid));
  474. if (err)
  475. goto bail;
  476. goto retry;
  477. }
  478. pgstart->addr = obuf->phys;
  479. pgstart->size = obuf->size;
  480. for (i = 0; i < inbufs + outbufs; ++i) {
  481. void *buf;
  482. int num;
  483. size_t len;
  484. list[i].num = 0;
  485. list[i].pgidx = 0;
  486. len = pra[i].buf.len;
  487. VERIFY(err, len >= 0);
  488. if (err)
  489. goto bail;
  490. if (!len)
  491. continue;
  492. buf = pra[i].buf.pv;
  493. num = buf_num_pages(buf, len);
  494. if (!kernel)
  495. list[i].num = buf_get_pages(buf, len, num,
  496. i >= inbufs, pages, rlen / sizeof(*pages));
  497. else
  498. list[i].num = 0;
  499. VERIFY(err, list[i].num >= 0);
  500. if (err)
  501. goto bail;
  502. if (list[i].num) {
  503. list[i].pgidx = pages - pgstart;
  504. pages = pages + list[i].num;
  505. } else if (rlen > sizeof(*pages)) {
  506. list[i].pgidx = pages - pgstart;
  507. pages = pages + 1;
  508. } else {
  509. if (obuf->handle != ibuf->handle)
  510. free_mem(obuf, cid);
  511. obuf->size += buf_page_size(sizeof(*pages));
  512. VERIFY(err, 0 == alloc_mem(obuf, cid));
  513. if (err)
  514. goto bail;
  515. goto retry;
  516. }
  517. rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt);
  518. }
  519. obuf->used = obuf->size - rlen;
  520. bail:
  521. if (err && (obuf->handle != ibuf->handle))
  522. free_mem(obuf, cid);
  523. UNLOCK_MMAP(kernel);
  524. return err;
  525. }
  526. static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
  527. remote_arg_t *rpra, remote_arg_t *upra,
  528. struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs,
  529. int *nbufs, int *fds, struct ion_handle **handles, int cid)
  530. {
  531. struct fastrpc_apps *me = &gfa;
  532. struct smq_invoke_buf *list;
  533. struct fastrpc_buf *pbuf = ibuf, *obufs = NULL;
  534. struct smq_phy_page *pages;
  535. void *args;
  536. size_t rlen, used, size;
  537. int i, inh, bufs = 0, err = 0;
  538. int inbufs = REMOTE_SCALARS_INBUFS(sc);
  539. int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  540. unsigned long len;
  541. ion_phys_addr_t iova;
  542. list = smq_invoke_buf_start(rpra, sc);
  543. pages = smq_phy_page_start(sc, list);
  544. used = ALIGN(pbuf->used, BALIGN);
  545. args = (void *)((char *)pbuf->virt + used);
  546. rlen = pbuf->size - used;
  547. for (i = 0; i < inbufs + outbufs; ++i) {
  548. rpra[i].buf.len = pra[i].buf.len;
  549. if (!rpra[i].buf.len)
  550. continue;
  551. if (me->channel[cid].smmu.enabled && fds && (fds[i] >= 0)) {
  552. len = buf_page_size(pra[i].buf.len);
  553. handles[i] = ion_import_dma_buf(me->iclient, fds[i]);
  554. VERIFY(err, 0 == IS_ERR_OR_NULL(handles[i]));
  555. if (err)
  556. goto bail;
  557. VERIFY(err, 0 == ion_map_iommu(me->iclient, handles[i],
  558. me->channel[cid].smmu.domain_id,
  559. 0, SZ_4K, 0, &iova, &len, 0, 0));
  560. if (err)
  561. goto bail;
  562. rpra[i].buf.pv = pra[i].buf.pv;
  563. list[i].num = 1;
  564. pages[list[i].pgidx].addr = iova;
  565. pages[list[i].pgidx].size = len;
  566. continue;
  567. } else if (list[i].num) {
  568. rpra[i].buf.pv = pra[i].buf.pv;
  569. continue;
  570. }
  571. if (rlen < pra[i].buf.len) {
  572. struct fastrpc_buf *b;
  573. pbuf->used = pbuf->size - rlen;
  574. VERIFY(err, 0 != (b = krealloc(obufs,
  575. (bufs + 1) * sizeof(*obufs), GFP_KERNEL)));
  576. if (err)
  577. goto bail;
  578. obufs = b;
  579. pbuf = obufs + bufs;
  580. pbuf->size = buf_num_pages(0, pra[i].buf.len) *
  581. PAGE_SIZE;
  582. VERIFY(err, 0 == alloc_mem(pbuf, cid));
  583. if (err)
  584. goto bail;
  585. bufs++;
  586. args = pbuf->virt;
  587. rlen = pbuf->size;
  588. }
  589. list[i].num = 1;
  590. pages[list[i].pgidx].addr =
  591. buf_page_start((void *)((uint32_t)pbuf->phys +
  592. (pbuf->size - rlen)));
  593. pages[list[i].pgidx].size =
  594. buf_page_size(pra[i].buf.len);
  595. if (i < inbufs) {
  596. if (!kernel) {
  597. VERIFY(err, 0 == copy_from_user(args,
  598. pra[i].buf.pv, pra[i].buf.len));
  599. if (err)
  600. goto bail;
  601. } else {
  602. memmove(args, pra[i].buf.pv, pra[i].buf.len);
  603. }
  604. }
  605. rpra[i].buf.pv = args;
  606. args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN));
  607. rlen -= ALIGN(pra[i].buf.len, BALIGN);
  608. }
  609. for (i = 0; i < inbufs; ++i) {
  610. if (rpra[i].buf.len)
  611. dmac_flush_range(rpra[i].buf.pv,
  612. (char *)rpra[i].buf.pv + rpra[i].buf.len);
  613. }
  614. pbuf->used = pbuf->size - rlen;
  615. size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc);
  616. if (size) {
  617. inh = inbufs + outbufs;
  618. if (!kernel) {
  619. VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh],
  620. size));
  621. if (err)
  622. goto bail;
  623. } else {
  624. memmove(&rpra[inh], &upra[inh], size);
  625. }
  626. }
  627. dmac_flush_range(rpra, (char *)rpra + used);
  628. bail:
  629. *abufs = obufs;
  630. *nbufs = bufs;
  631. return err;
  632. }
  633. static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
  634. remote_arg_t *rpra, remote_arg_t *upra)
  635. {
  636. int i, inbufs, outbufs, outh, size;
  637. int err = 0;
  638. inbufs = REMOTE_SCALARS_INBUFS(sc);
  639. outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  640. for (i = inbufs; i < inbufs + outbufs; ++i) {
  641. if (rpra[i].buf.pv != pra[i].buf.pv) {
  642. if (!kernel) {
  643. VERIFY(err, 0 == copy_to_user(pra[i].buf.pv,
  644. rpra[i].buf.pv, rpra[i].buf.len));
  645. if (err)
  646. goto bail;
  647. } else {
  648. memmove(pra[i].buf.pv, rpra[i].buf.pv,
  649. rpra[i].buf.len);
  650. }
  651. }
  652. }
  653. size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc);
  654. if (size) {
  655. outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc);
  656. if (!kernel) {
  657. VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh],
  658. size));
  659. if (err)
  660. goto bail;
  661. } else {
  662. memmove(&upra[outh], &rpra[outh], size);
  663. }
  664. }
  665. bail:
  666. return err;
  667. }
  668. static void inv_args_pre(uint32_t sc, remote_arg_t *rpra)
  669. {
  670. int i, inbufs, outbufs;
  671. uint32_t end;
  672. inbufs = REMOTE_SCALARS_INBUFS(sc);
  673. outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  674. for (i = inbufs; i < inbufs + outbufs; ++i) {
  675. if (!rpra[i].buf.len)
  676. continue;
  677. if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
  678. continue;
  679. if (!IS_CACHE_ALIGNED((uint32_t)rpra[i].buf.pv))
  680. dmac_flush_range(rpra[i].buf.pv,
  681. (char *)rpra[i].buf.pv + 1);
  682. end = (uint32_t)rpra[i].buf.pv + rpra[i].buf.len;
  683. if (!IS_CACHE_ALIGNED(end))
  684. dmac_flush_range((char *)end,
  685. (char *)end + 1);
  686. }
  687. }
  688. static void inv_args(uint32_t sc, remote_arg_t *rpra, int used)
  689. {
  690. int i, inbufs, outbufs;
  691. int inv = 0;
  692. inbufs = REMOTE_SCALARS_INBUFS(sc);
  693. outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  694. for (i = inbufs; i < inbufs + outbufs; ++i) {
  695. if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
  696. inv = 1;
  697. else if (rpra[i].buf.len)
  698. dmac_inv_range(rpra[i].buf.pv,
  699. (char *)rpra[i].buf.pv + rpra[i].buf.len);
  700. }
  701. if (inv || REMOTE_SCALARS_OUTHANDLES(sc))
  702. dmac_inv_range(rpra, (char *)rpra + used);
  703. }
  704. static int fastrpc_invoke_send(struct fastrpc_apps *me,
  705. uint32_t kernel, uint32_t handle,
  706. uint32_t sc, struct smq_invoke_ctx *ctx,
  707. struct fastrpc_buf *buf)
  708. {
  709. struct smq_msg msg;
  710. int err = 0, len;
  711. msg.pid = current->tgid;
  712. msg.tid = current->pid;
  713. if (kernel)
  714. msg.pid = 0;
  715. msg.invoke.header.ctx = ctx;
  716. msg.invoke.header.handle = handle;
  717. msg.invoke.header.sc = sc;
  718. msg.invoke.page.addr = buf->phys;
  719. msg.invoke.page.size = buf_page_size(buf->used);
  720. spin_lock(&me->wrlock);
  721. len = smd_write(me->channel[ctx->cid].chan, &msg, sizeof(msg));
  722. spin_unlock(&me->wrlock);
  723. VERIFY(err, len == sizeof(msg));
  724. return err;
  725. }
  726. static void fastrpc_deinit(void)
  727. {
  728. struct fastrpc_apps *me = &gfa;
  729. int i;
  730. for (i = 0; i < NUM_CHANNELS; i++) {
  731. if (me->channel[i].chan) {
  732. (void)smd_close(me->channel[i].chan);
  733. me->channel[i].chan = 0;
  734. }
  735. }
  736. ion_client_destroy(me->iclient);
  737. me->iclient = 0;
  738. }
  739. static void fastrpc_read_handler(int cid)
  740. {
  741. struct fastrpc_apps *me = &gfa;
  742. struct smq_invoke_rsp rsp;
  743. int err = 0;
  744. do {
  745. VERIFY(err, sizeof(rsp) == smd_read_from_cb(
  746. me->channel[cid].chan,
  747. &rsp, sizeof(rsp)));
  748. if (err)
  749. goto bail;
  750. context_notify_user(rsp.ctx, rsp.retval);
  751. } while (!err);
  752. bail:
  753. return;
  754. }
  755. static void smd_event_handler(void *priv, unsigned event)
  756. {
  757. struct fastrpc_apps *me = &gfa;
  758. int cid = (int)priv;
  759. switch (event) {
  760. case SMD_EVENT_OPEN:
  761. complete(&me->channel[cid].work);
  762. break;
  763. case SMD_EVENT_CLOSE:
  764. context_notify_all_users(&me->clst, cid);
  765. break;
  766. case SMD_EVENT_DATA:
  767. fastrpc_read_handler(cid);
  768. break;
  769. }
  770. }
  771. static int fastrpc_init(void)
  772. {
  773. int i, err = 0;
  774. struct fastrpc_apps *me = &gfa;
  775. struct device_node *node;
  776. struct fastrpc_smmu *smmu;
  777. bool enabled = 0;
  778. spin_lock_init(&me->hlock);
  779. spin_lock_init(&me->wrlock);
  780. mutex_init(&me->smd_mutex);
  781. context_list_ctor(&me->clst);
  782. for (i = 0; i < RPC_HASH_SZ; ++i)
  783. INIT_HLIST_HEAD(&me->htbl[i]);
  784. me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
  785. DEVICE_NAME);
  786. VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
  787. if (err)
  788. goto bail;
  789. for (i = 0; i < NUM_CHANNELS; i++) {
  790. init_completion(&me->channel[i].work);
  791. if (!gcinfo[i].node)
  792. continue;
  793. smmu = &me->channel[i].smmu;
  794. node = of_find_compatible_node(NULL, NULL, gcinfo[i].node);
  795. if (node)
  796. enabled = of_property_read_bool(node,
  797. "qcom,smmu-enabled");
  798. if (enabled)
  799. smmu->group = iommu_group_find(gcinfo[i].group);
  800. if (smmu->group)
  801. smmu->domain = iommu_group_get_iommudata(smmu->group);
  802. if (!IS_ERR_OR_NULL(smmu->domain)) {
  803. smmu->domain_id = msm_find_domain_no(smmu->domain);
  804. if (smmu->domain_id >= 0)
  805. smmu->enabled = enabled;
  806. }
  807. }
  808. return 0;
  809. bail:
  810. return err;
  811. }
  812. static void free_dev(struct fastrpc_device *dev, int cid)
  813. {
  814. if (dev) {
  815. free_mem(&dev->buf, cid);
  816. kfree(dev);
  817. module_put(THIS_MODULE);
  818. }
  819. }
  820. static int alloc_dev(struct fastrpc_device **dev, int cid)
  821. {
  822. int err = 0;
  823. struct fastrpc_device *fd = NULL;
  824. VERIFY(err, 0 != try_module_get(THIS_MODULE));
  825. if (err)
  826. goto bail;
  827. VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
  828. if (err)
  829. goto bail;
  830. INIT_HLIST_NODE(&fd->hn);
  831. fd->buf.size = PAGE_SIZE;
  832. VERIFY(err, 0 == alloc_mem(&fd->buf, cid));
  833. if (err)
  834. goto bail;
  835. fd->tgid = current->tgid;
  836. *dev = fd;
  837. bail:
  838. if (err)
  839. free_dev(fd, cid);
  840. return err;
  841. }
  842. static int get_dev(struct fastrpc_apps *me, int cid,
  843. struct fastrpc_device **rdev)
  844. {
  845. struct hlist_head *head;
  846. struct fastrpc_device *dev = NULL, *devfree = NULL;
  847. struct hlist_node *pos, *n;
  848. uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
  849. int err = 0;
  850. spin_lock(&me->hlock);
  851. head = &me->htbl[h];
  852. hlist_for_each_entry_safe(dev, pos, n, head, hn) {
  853. if (dev->tgid == current->tgid) {
  854. hlist_del(&dev->hn);
  855. devfree = dev;
  856. break;
  857. }
  858. }
  859. spin_unlock(&me->hlock);
  860. VERIFY(err, devfree != 0);
  861. if (err)
  862. goto bail;
  863. *rdev = devfree;
  864. bail:
  865. if (err) {
  866. free_dev(devfree, cid);
  867. err = alloc_dev(rdev, cid);
  868. }
  869. return err;
  870. }
  871. static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev)
  872. {
  873. struct hlist_head *head;
  874. uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
  875. spin_lock(&me->hlock);
  876. head = &me->htbl[h];
  877. hlist_add_head(&dev->hn, head);
  878. spin_unlock(&me->hlock);
  879. return;
  880. }
  881. static int fastrpc_release_current_dsp_process(int cid);
  882. static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t mode,
  883. uint32_t kernel, struct fastrpc_ioctl_invoke_fd *invokefd,
  884. int cid)
  885. {
  886. struct smq_invoke_ctx *ctx = NULL;
  887. struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
  888. int interrupted = 0;
  889. int err = 0;
  890. if (!kernel) {
  891. VERIFY(err, invoke->handle != FASTRPC_STATIC_HANDLE_KERNEL);
  892. if (err) {
  893. pr_err("adsprpc: ERROR: %s: user application %s trying to send a kernel RPC message to channel %d",
  894. __func__, current->comm, cid);
  895. goto bail;
  896. }
  897. }
  898. if(!kernel) {
  899. VERIFY(err, 0 == context_restore_interrupted(me, invokefd, cid, &ctx));
  900. if (err)
  901. goto bail;
  902. if(ctx)
  903. goto wait;
  904. }
  905. VERIFY(err, 0 == context_alloc(me, kernel, invokefd, cid, &ctx));
  906. if (err)
  907. goto bail;
  908. if (me->channel[cid].smmu.enabled) {
  909. VERIFY(err, 0 == iommu_attach_group(
  910. me->channel[cid].smmu.domain,
  911. me->channel[cid].smmu.group));
  912. if (err)
  913. goto bail;
  914. ctx->smmu = 1;
  915. }
  916. if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
  917. VERIFY(err, 0 == get_dev(me, cid, &ctx->dev));
  918. if (err)
  919. goto bail;
  920. VERIFY(err, 0 == get_page_list(kernel, ctx->sc, ctx->pra, &ctx->dev->buf,
  921. &ctx->obuf, cid));
  922. if (err)
  923. goto bail;
  924. ctx->rpra = (remote_arg_t *)ctx->obuf.virt;
  925. VERIFY(err, 0 == get_args(kernel, ctx->sc, ctx->pra, ctx->rpra, invoke->pra,
  926. &ctx->obuf, &ctx->abufs, &ctx->nbufs, ctx->fds, ctx->handles, cid));
  927. if (err)
  928. goto bail;
  929. }
  930. inv_args_pre(ctx->sc, ctx->rpra);
  931. if (FASTRPC_MODE_SERIAL == mode)
  932. inv_args(ctx->sc, ctx->rpra, ctx->obuf.used);
  933. VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, ctx->sc,
  934. ctx, &ctx->obuf));
  935. if (err)
  936. goto bail;
  937. if (FASTRPC_MODE_PARALLEL == mode)
  938. inv_args(ctx->sc, ctx->rpra, ctx->obuf.used);
  939. wait:
  940. if(kernel)
  941. wait_for_completion(&ctx->work);
  942. else {
  943. interrupted = wait_for_completion_interruptible(&ctx->work);
  944. VERIFY(err, 0 == (err = interrupted));
  945. if (err)
  946. goto bail;
  947. }
  948. VERIFY(err, 0 == (err = ctx->retval));
  949. if (err)
  950. goto bail;
  951. VERIFY(err, 0 == put_args(kernel, ctx->sc, ctx->pra, ctx->rpra, invoke->pra));
  952. if (err)
  953. goto bail;
  954. bail:
  955. if (ctx && interrupted == -ERESTARTSYS) {
  956. context_save_interrupted(ctx);
  957. err = -ERESTARTSYS;
  958. } else if(ctx) {
  959. context_free(ctx, 1);
  960. }
  961. return err;
  962. }
  963. static int fastrpc_create_current_dsp_process(int cid)
  964. {
  965. int err = 0;
  966. struct fastrpc_ioctl_invoke_fd ioctl;
  967. struct fastrpc_apps *me = &gfa;
  968. remote_arg_t ra[1];
  969. int tgid = 0;
  970. tgid = current->tgid;
  971. ra[0].buf.pv = &tgid;
  972. ra[0].buf.len = sizeof(tgid);
  973. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
  974. ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
  975. ioctl.inv.pra = ra;
  976. ioctl.fds = 0;
  977. VERIFY(err, 0 == (err = fastrpc_internal_invoke(me,
  978. FASTRPC_MODE_PARALLEL, 1, &ioctl, cid)));
  979. return err;
  980. }
  981. static int fastrpc_release_current_dsp_process(int cid)
  982. {
  983. int err = 0;
  984. struct fastrpc_apps *me = &gfa;
  985. struct fastrpc_ioctl_invoke_fd ioctl;
  986. remote_arg_t ra[1];
  987. int tgid = 0;
  988. tgid = current->tgid;
  989. ra[0].buf.pv = &tgid;
  990. ra[0].buf.len = sizeof(tgid);
  991. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
  992. ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
  993. ioctl.inv.pra = ra;
  994. ioctl.fds = 0;
  995. VERIFY(err, 0 == (err = fastrpc_internal_invoke(me,
  996. FASTRPC_MODE_PARALLEL, 1, &ioctl, cid)));
  997. return err;
  998. }
  999. static int fastrpc_mmap_on_dsp(struct fastrpc_apps *me,
  1000. struct fastrpc_ioctl_mmap *mmap,
  1001. struct smq_phy_page *pages,
  1002. int cid, int num)
  1003. {
  1004. struct fastrpc_ioctl_invoke_fd ioctl;
  1005. remote_arg_t ra[3];
  1006. int err = 0;
  1007. struct {
  1008. int pid;
  1009. uint32_t flags;
  1010. uint32_t vaddrin;
  1011. int num;
  1012. } inargs;
  1013. struct {
  1014. uint32_t vaddrout;
  1015. } routargs;
  1016. inargs.pid = current->tgid;
  1017. inargs.vaddrin = mmap->vaddrin;
  1018. inargs.flags = mmap->flags;
  1019. inargs.num = num;
  1020. ra[0].buf.pv = &inargs;
  1021. ra[0].buf.len = sizeof(inargs);
  1022. ra[1].buf.pv = pages;
  1023. ra[1].buf.len = num * sizeof(*pages);
  1024. ra[2].buf.pv = &routargs;
  1025. ra[2].buf.len = sizeof(routargs);
  1026. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
  1027. ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
  1028. ioctl.inv.pra = ra;
  1029. ioctl.fds = 0;
  1030. VERIFY(err, 0 == (err = fastrpc_internal_invoke(me,
  1031. FASTRPC_MODE_PARALLEL, 1, &ioctl, cid)));
  1032. mmap->vaddrout = routargs.vaddrout;
  1033. if (err)
  1034. goto bail;
  1035. bail:
  1036. return err;
  1037. }
  1038. static int fastrpc_munmap_on_dsp(struct fastrpc_apps *me,
  1039. struct fastrpc_ioctl_munmap *munmap, int cid)
  1040. {
  1041. struct fastrpc_ioctl_invoke_fd ioctl;
  1042. remote_arg_t ra[1];
  1043. int err = 0;
  1044. struct {
  1045. int pid;
  1046. uint32_t vaddrout;
  1047. size_t size;
  1048. } inargs;
  1049. inargs.pid = current->tgid;
  1050. inargs.size = munmap->size;
  1051. inargs.vaddrout = munmap->vaddrout;
  1052. ra[0].buf.pv = &inargs;
  1053. ra[0].buf.len = sizeof(inargs);
  1054. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
  1055. ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
  1056. ioctl.inv.pra = ra;
  1057. ioctl.fds = 0;
  1058. VERIFY(err, 0 == (err = fastrpc_internal_invoke(me,
  1059. FASTRPC_MODE_PARALLEL, 1, &ioctl, cid)));
  1060. return err;
  1061. }
  1062. static int fastrpc_internal_munmap(struct fastrpc_apps *me,
  1063. struct file_data *fdata,
  1064. struct fastrpc_ioctl_munmap *munmap)
  1065. {
  1066. int err = 0;
  1067. struct fastrpc_mmap *map = 0, *mapfree = 0;
  1068. struct hlist_node *pos, *n;
  1069. mutex_lock(&fdata->map_mutex);
  1070. VERIFY(err, 0 == (err = fastrpc_munmap_on_dsp(me, munmap, fdata->cid)));
  1071. if (err)
  1072. goto bail;
  1073. spin_lock(&fdata->hlock);
  1074. hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
  1075. if (map->vaddrout == munmap->vaddrout &&
  1076. map->size == munmap->size) {
  1077. hlist_del(&map->hn);
  1078. mapfree = map;
  1079. map = 0;
  1080. break;
  1081. }
  1082. }
  1083. spin_unlock(&fdata->hlock);
  1084. bail:
  1085. if (mapfree) {
  1086. free_map(mapfree, fdata->cid);
  1087. kfree(mapfree);
  1088. }
  1089. mutex_unlock(&fdata->map_mutex);
  1090. return err;
  1091. }
  1092. static int fastrpc_internal_mmap(struct fastrpc_apps *me,
  1093. struct file_data *fdata,
  1094. struct fastrpc_ioctl_mmap *mmap)
  1095. {
  1096. struct ion_client *clnt = gfa.iclient;
  1097. struct fastrpc_mmap *map = NULL;
  1098. struct smq_phy_page *pages = NULL;
  1099. struct ion_handle *handles;
  1100. void *buf;
  1101. unsigned long len;
  1102. int num;
  1103. int err = 0;
  1104. mutex_lock(&fdata->map_mutex);
  1105. VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL)));
  1106. if (err)
  1107. goto bail;
  1108. map->handle = ion_import_dma_buf(clnt, mmap->fd);
  1109. VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle));
  1110. if (err)
  1111. goto bail;
  1112. map->virt = ion_map_kernel(clnt, map->handle);
  1113. VERIFY(err, 0 == IS_ERR_OR_NULL(map->virt));
  1114. if (err)
  1115. goto bail;
  1116. buf = (void *)mmap->vaddrin;
  1117. len = mmap->size;
  1118. num = buf_num_pages(buf, len);
  1119. VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL)));
  1120. if (err)
  1121. goto bail;
  1122. if (me->channel[fdata->cid].smmu.enabled) {
  1123. handles = ion_import_dma_buf(clnt, mmap->fd);
  1124. VERIFY(err, 0 == IS_ERR_OR_NULL(handles));
  1125. if (err)
  1126. goto bail;
  1127. VERIFY(err, 0 == ion_map_iommu(clnt, handles,
  1128. me->channel[fdata->cid].smmu.domain_id, 0,
  1129. SZ_4K, 0, &map->phys, &len, 0, 0));
  1130. if (err)
  1131. goto bail;
  1132. pages->addr = map->phys;
  1133. pages->size = len;
  1134. num = 1;
  1135. } else {
  1136. VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1,
  1137. pages, num)));
  1138. if (err)
  1139. goto bail;
  1140. }
  1141. VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, fdata->cid, num));
  1142. if (err)
  1143. goto bail;
  1144. map->vaddrin = mmap->vaddrin;
  1145. map->vaddrout = mmap->vaddrout;
  1146. map->size = mmap->size;
  1147. INIT_HLIST_NODE(&map->hn);
  1148. spin_lock(&fdata->hlock);
  1149. hlist_add_head(&map->hn, &fdata->hlst);
  1150. spin_unlock(&fdata->hlock);
  1151. bail:
  1152. if (err && map) {
  1153. free_map(map, fdata->cid);
  1154. kfree(map);
  1155. }
  1156. kfree(pages);
  1157. mutex_unlock(&fdata->map_mutex);
  1158. return err;
  1159. }
  1160. static void cleanup_current_dev(int cid)
  1161. {
  1162. struct fastrpc_apps *me = &gfa;
  1163. uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
  1164. struct hlist_head *head;
  1165. struct hlist_node *pos, *n;
  1166. struct fastrpc_device *dev, *devfree;
  1167. rnext:
  1168. devfree = dev = NULL;
  1169. spin_lock(&me->hlock);
  1170. head = &me->htbl[h];
  1171. hlist_for_each_entry_safe(dev, pos, n, head, hn) {
  1172. if (dev->tgid == current->tgid) {
  1173. hlist_del(&dev->hn);
  1174. devfree = dev;
  1175. break;
  1176. }
  1177. }
  1178. spin_unlock(&me->hlock);
  1179. if (devfree) {
  1180. free_dev(devfree, cid);
  1181. goto rnext;
  1182. }
  1183. return;
  1184. }
  1185. static void fastrpc_channel_close(struct kref *kref)
  1186. {
  1187. struct fastrpc_apps *me = &gfa;
  1188. struct fastrpc_channel_context *ctx;
  1189. int cid;
  1190. ctx = container_of(kref, struct fastrpc_channel_context, kref);
  1191. smd_close(ctx->chan);
  1192. ctx->chan = 0;
  1193. mutex_unlock(&me->smd_mutex);
  1194. cid = ctx - &me->channel[0];
  1195. pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
  1196. MAJOR(me->dev_no), cid);
  1197. }
  1198. static int fastrpc_device_release(struct inode *inode, struct file *file)
  1199. {
  1200. struct file_data *fdata = (struct file_data *)file->private_data;
  1201. struct fastrpc_apps *me = &gfa;
  1202. int cid = MINOR(inode->i_rdev);
  1203. (void)fastrpc_release_current_dsp_process(cid);
  1204. cleanup_current_dev(cid);
  1205. if (fdata) {
  1206. struct fastrpc_mmap *map = NULL;
  1207. struct hlist_node *pos, *n;
  1208. file->private_data = 0;
  1209. hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
  1210. hlist_del(&map->hn);
  1211. free_map(map, cid);
  1212. kfree(map);
  1213. }
  1214. mutex_destroy(&fdata->map_mutex);
  1215. kfree(fdata);
  1216. kref_put_mutex(&me->channel[cid].kref, fastrpc_channel_close,
  1217. &me->smd_mutex);
  1218. }
  1219. return 0;
  1220. }
  1221. static int fastrpc_device_open(struct inode *inode, struct file *filp)
  1222. {
  1223. int cid = MINOR(inode->i_rdev);
  1224. int err = 0;
  1225. struct fastrpc_apps *me = &gfa;
  1226. mutex_lock(&me->smd_mutex);
  1227. if (kref_get_unless_zero(&me->channel[cid].kref) == 0) {
  1228. VERIFY(err, 0 == smd_named_open_on_edge(
  1229. FASTRPC_SMD_GUID,
  1230. gcinfo[cid].channel,
  1231. &me->channel[cid].chan, (void*)cid,
  1232. smd_event_handler));
  1233. if (err)
  1234. goto smd_bail;
  1235. VERIFY(err, 0 != wait_for_completion_timeout(
  1236. &me->channel[cid].work,
  1237. RPC_TIMEOUT));
  1238. if (err)
  1239. goto completion_bail;
  1240. kref_init(&me->channel[cid].kref);
  1241. pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
  1242. MAJOR(me->dev_no), cid);
  1243. }
  1244. mutex_unlock(&me->smd_mutex);
  1245. filp->private_data = NULL;
  1246. if (0 != try_module_get(THIS_MODULE)) {
  1247. struct file_data *fdata = NULL;
  1248. /* This call will cause a dev to be created
  1249. * which will addref this module
  1250. */
  1251. VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL)));
  1252. if (err)
  1253. goto bail;
  1254. spin_lock_init(&fdata->hlock);
  1255. INIT_HLIST_HEAD(&fdata->hlst);
  1256. fdata->cid = cid;
  1257. VERIFY(err, 0 == fastrpc_create_current_dsp_process(cid));
  1258. if (err)
  1259. goto bail;
  1260. mutex_init(&fdata->map_mutex);
  1261. filp->private_data = fdata;
  1262. bail:
  1263. if (err) {
  1264. cleanup_current_dev(cid);
  1265. kfree(fdata);
  1266. kref_put_mutex(&me->channel[cid].kref,
  1267. fastrpc_channel_close, &me->smd_mutex);
  1268. }
  1269. module_put(THIS_MODULE);
  1270. }
  1271. return err;
  1272. completion_bail:
  1273. smd_close(me->channel[cid].chan);
  1274. me->channel[cid].chan = NULL;
  1275. smd_bail:
  1276. mutex_unlock(&me->smd_mutex);
  1277. return err;
  1278. }
  1279. static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
  1280. unsigned long ioctl_param)
  1281. {
  1282. struct fastrpc_apps *me = &gfa;
  1283. struct fastrpc_ioctl_invoke_fd invokefd;
  1284. struct fastrpc_ioctl_invoke *invoke = &invokefd.inv;
  1285. struct fastrpc_ioctl_mmap mmap;
  1286. struct fastrpc_ioctl_munmap munmap;
  1287. void *param = (char *)ioctl_param;
  1288. struct file_data *fdata = (struct file_data *)file->private_data;
  1289. int size = 0, err = 0;
  1290. switch (ioctl_num) {
  1291. case FASTRPC_IOCTL_INVOKE_FD:
  1292. case FASTRPC_IOCTL_INVOKE:
  1293. invokefd.fds = NULL;
  1294. size = (ioctl_num == FASTRPC_IOCTL_INVOKE) ?
  1295. sizeof(*invoke) : sizeof(invokefd);
  1296. VERIFY(err, 0 == copy_from_user(&invokefd, param, size));
  1297. if (err)
  1298. goto bail;
  1299. VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, fdata->mode,
  1300. 0, &invokefd, fdata->cid)));
  1301. if (err)
  1302. goto bail;
  1303. break;
  1304. case FASTRPC_IOCTL_MMAP:
  1305. VERIFY(err, 0 == copy_from_user(&mmap, param,
  1306. sizeof(mmap)));
  1307. if (err)
  1308. goto bail;
  1309. VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata,
  1310. &mmap)));
  1311. if (err)
  1312. goto bail;
  1313. VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap)));
  1314. if (err)
  1315. goto bail;
  1316. break;
  1317. case FASTRPC_IOCTL_MUNMAP:
  1318. VERIFY(err, 0 == copy_from_user(&munmap, param,
  1319. sizeof(munmap)));
  1320. if (err)
  1321. goto bail;
  1322. VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata,
  1323. &munmap)));
  1324. if (err)
  1325. goto bail;
  1326. break;
  1327. case FASTRPC_IOCTL_SETMODE:
  1328. switch ((uint32_t)ioctl_param) {
  1329. case FASTRPC_MODE_PARALLEL:
  1330. case FASTRPC_MODE_SERIAL:
  1331. fdata->mode = (uint32_t)ioctl_param;
  1332. break;
  1333. default:
  1334. err = -ENOTTY;
  1335. break;
  1336. }
  1337. break;
  1338. default:
  1339. err = -ENOTTY;
  1340. break;
  1341. }
  1342. bail:
  1343. return err;
  1344. }
  1345. static const struct file_operations fops = {
  1346. .open = fastrpc_device_open,
  1347. .release = fastrpc_device_release,
  1348. .unlocked_ioctl = fastrpc_device_ioctl,
  1349. };
  1350. static int __init fastrpc_device_init(void)
  1351. {
  1352. struct fastrpc_apps *me = &gfa;
  1353. int i, err = 0;
  1354. memset(me, 0, sizeof(*me));
  1355. VERIFY(err, 0 == fastrpc_init());
  1356. if (err)
  1357. goto fastrpc_bail;
  1358. VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
  1359. DEVICE_NAME));
  1360. if (err)
  1361. goto alloc_chrdev_bail;
  1362. cdev_init(&me->cdev, &fops);
  1363. me->cdev.owner = THIS_MODULE;
  1364. VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
  1365. NUM_CHANNELS));
  1366. if (err)
  1367. goto cdev_init_bail;
  1368. me->class = class_create(THIS_MODULE, "fastrpc");
  1369. VERIFY(err, !IS_ERR(me->class));
  1370. if (err)
  1371. goto class_create_bail;
  1372. for (i = 0; i < NUM_CHANNELS; i++) {
  1373. me->channel[i].dev = device_create(me->class, NULL,
  1374. MKDEV(MAJOR(me->dev_no), i),
  1375. NULL, gcinfo[i].name);
  1376. VERIFY(err, !IS_ERR(me->channel[i].dev));
  1377. if (err)
  1378. goto device_create_bail;
  1379. }
  1380. return 0;
  1381. device_create_bail:
  1382. class_destroy(me->class);
  1383. class_create_bail:
  1384. cdev_del(&me->cdev);
  1385. cdev_init_bail:
  1386. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  1387. alloc_chrdev_bail:
  1388. fastrpc_deinit();
  1389. fastrpc_bail:
  1390. return err;
  1391. }
  1392. static void __exit fastrpc_device_exit(void)
  1393. {
  1394. struct fastrpc_apps *me = &gfa;
  1395. int i;
  1396. context_list_dtor(me, &me->clst);
  1397. fastrpc_deinit();
  1398. for (i = 0; i < NUM_CHANNELS; i++) {
  1399. cleanup_current_dev(i);
  1400. device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
  1401. }
  1402. class_destroy(me->class);
  1403. cdev_del(&me->cdev);
  1404. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  1405. }
  1406. module_init(fastrpc_device_init);
  1407. module_exit(fastrpc_device_exit);
  1408. MODULE_LICENSE("GPL v2");