adsprpc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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. VERIFY(err, map != NULL);
  1074. if (err) {
  1075. err = -EINVAL;
  1076. goto bail;
  1077. }
  1078. spin_lock(&fdata->hlock);
  1079. hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
  1080. if (map->vaddrout == munmap->vaddrout &&
  1081. map->size == munmap->size) {
  1082. hlist_del(&map->hn);
  1083. mapfree = map;
  1084. map = 0;
  1085. break;
  1086. }
  1087. }
  1088. spin_unlock(&fdata->hlock);
  1089. bail:
  1090. if (mapfree) {
  1091. free_map(mapfree, fdata->cid);
  1092. kfree(mapfree);
  1093. }
  1094. mutex_unlock(&fdata->map_mutex);
  1095. return err;
  1096. }
  1097. static int fastrpc_internal_mmap(struct fastrpc_apps *me,
  1098. struct file_data *fdata,
  1099. struct fastrpc_ioctl_mmap *mmap)
  1100. {
  1101. struct ion_client *clnt = gfa.iclient;
  1102. struct fastrpc_mmap *map = NULL;
  1103. struct smq_phy_page *pages = NULL;
  1104. struct ion_handle *handles;
  1105. void *buf;
  1106. unsigned long len;
  1107. int num;
  1108. int err = 0;
  1109. mutex_lock(&fdata->map_mutex);
  1110. VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL)));
  1111. if (err)
  1112. goto bail;
  1113. map->handle = ion_import_dma_buf(clnt, mmap->fd);
  1114. VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle));
  1115. if (err)
  1116. goto bail;
  1117. map->virt = ion_map_kernel(clnt, map->handle);
  1118. VERIFY(err, 0 == IS_ERR_OR_NULL(map->virt));
  1119. if (err)
  1120. goto bail;
  1121. buf = (void *)mmap->vaddrin;
  1122. len = mmap->size;
  1123. num = buf_num_pages(buf, len);
  1124. VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL)));
  1125. if (err)
  1126. goto bail;
  1127. if (me->channel[fdata->cid].smmu.enabled) {
  1128. handles = ion_import_dma_buf(clnt, mmap->fd);
  1129. VERIFY(err, 0 == IS_ERR_OR_NULL(handles));
  1130. if (err)
  1131. goto bail;
  1132. VERIFY(err, 0 == ion_map_iommu(clnt, handles,
  1133. me->channel[fdata->cid].smmu.domain_id, 0,
  1134. SZ_4K, 0, &map->phys, &len, 0, 0));
  1135. if (err)
  1136. goto bail;
  1137. pages->addr = map->phys;
  1138. pages->size = len;
  1139. num = 1;
  1140. } else {
  1141. VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1,
  1142. pages, num)));
  1143. if (err)
  1144. goto bail;
  1145. }
  1146. VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, fdata->cid, num));
  1147. if (err)
  1148. goto bail;
  1149. map->vaddrin = mmap->vaddrin;
  1150. map->vaddrout = mmap->vaddrout;
  1151. map->size = mmap->size;
  1152. INIT_HLIST_NODE(&map->hn);
  1153. spin_lock(&fdata->hlock);
  1154. hlist_add_head(&map->hn, &fdata->hlst);
  1155. spin_unlock(&fdata->hlock);
  1156. bail:
  1157. if (err && map) {
  1158. free_map(map, fdata->cid);
  1159. kfree(map);
  1160. }
  1161. kfree(pages);
  1162. mutex_unlock(&fdata->map_mutex);
  1163. return err;
  1164. }
  1165. static void cleanup_current_dev(int cid)
  1166. {
  1167. struct fastrpc_apps *me = &gfa;
  1168. uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
  1169. struct hlist_head *head;
  1170. struct hlist_node *pos, *n;
  1171. struct fastrpc_device *dev, *devfree;
  1172. rnext:
  1173. devfree = dev = NULL;
  1174. spin_lock(&me->hlock);
  1175. head = &me->htbl[h];
  1176. hlist_for_each_entry_safe(dev, pos, n, head, hn) {
  1177. if (dev->tgid == current->tgid) {
  1178. hlist_del(&dev->hn);
  1179. devfree = dev;
  1180. break;
  1181. }
  1182. }
  1183. spin_unlock(&me->hlock);
  1184. if (devfree) {
  1185. free_dev(devfree, cid);
  1186. goto rnext;
  1187. }
  1188. return;
  1189. }
  1190. static void fastrpc_channel_close(struct kref *kref)
  1191. {
  1192. struct fastrpc_apps *me = &gfa;
  1193. struct fastrpc_channel_context *ctx;
  1194. int cid;
  1195. ctx = container_of(kref, struct fastrpc_channel_context, kref);
  1196. smd_close(ctx->chan);
  1197. ctx->chan = 0;
  1198. mutex_unlock(&me->smd_mutex);
  1199. cid = ctx - &me->channel[0];
  1200. pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
  1201. MAJOR(me->dev_no), cid);
  1202. }
  1203. static int fastrpc_device_release(struct inode *inode, struct file *file)
  1204. {
  1205. struct file_data *fdata = (struct file_data *)file->private_data;
  1206. struct fastrpc_apps *me = &gfa;
  1207. int cid = MINOR(inode->i_rdev);
  1208. (void)fastrpc_release_current_dsp_process(cid);
  1209. cleanup_current_dev(cid);
  1210. if (fdata) {
  1211. struct fastrpc_mmap *map = NULL;
  1212. struct hlist_node *pos, *n;
  1213. file->private_data = 0;
  1214. hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
  1215. hlist_del(&map->hn);
  1216. free_map(map, cid);
  1217. kfree(map);
  1218. }
  1219. mutex_destroy(&fdata->map_mutex);
  1220. kfree(fdata);
  1221. kref_put_mutex(&me->channel[cid].kref, fastrpc_channel_close,
  1222. &me->smd_mutex);
  1223. }
  1224. return 0;
  1225. }
  1226. static int fastrpc_device_open(struct inode *inode, struct file *filp)
  1227. {
  1228. int cid = MINOR(inode->i_rdev);
  1229. int err = 0;
  1230. struct fastrpc_apps *me = &gfa;
  1231. mutex_lock(&me->smd_mutex);
  1232. if (kref_get_unless_zero(&me->channel[cid].kref) == 0) {
  1233. VERIFY(err, 0 == smd_named_open_on_edge(
  1234. FASTRPC_SMD_GUID,
  1235. gcinfo[cid].channel,
  1236. &me->channel[cid].chan, (void*)cid,
  1237. smd_event_handler));
  1238. if (err)
  1239. goto smd_bail;
  1240. VERIFY(err, 0 != wait_for_completion_timeout(
  1241. &me->channel[cid].work,
  1242. RPC_TIMEOUT));
  1243. if (err)
  1244. goto completion_bail;
  1245. kref_init(&me->channel[cid].kref);
  1246. pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
  1247. MAJOR(me->dev_no), cid);
  1248. }
  1249. mutex_unlock(&me->smd_mutex);
  1250. filp->private_data = NULL;
  1251. if (0 != try_module_get(THIS_MODULE)) {
  1252. struct file_data *fdata = NULL;
  1253. /* This call will cause a dev to be created
  1254. * which will addref this module
  1255. */
  1256. VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL)));
  1257. if (err)
  1258. goto bail;
  1259. spin_lock_init(&fdata->hlock);
  1260. INIT_HLIST_HEAD(&fdata->hlst);
  1261. fdata->cid = cid;
  1262. VERIFY(err, 0 == fastrpc_create_current_dsp_process(cid));
  1263. if (err)
  1264. goto bail;
  1265. mutex_init(&fdata->map_mutex);
  1266. filp->private_data = fdata;
  1267. bail:
  1268. if (err) {
  1269. cleanup_current_dev(cid);
  1270. kfree(fdata);
  1271. kref_put_mutex(&me->channel[cid].kref,
  1272. fastrpc_channel_close, &me->smd_mutex);
  1273. }
  1274. module_put(THIS_MODULE);
  1275. }
  1276. return err;
  1277. completion_bail:
  1278. smd_close(me->channel[cid].chan);
  1279. me->channel[cid].chan = NULL;
  1280. smd_bail:
  1281. mutex_unlock(&me->smd_mutex);
  1282. return err;
  1283. }
  1284. static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
  1285. unsigned long ioctl_param)
  1286. {
  1287. struct fastrpc_apps *me = &gfa;
  1288. struct fastrpc_ioctl_invoke_fd invokefd;
  1289. struct fastrpc_ioctl_invoke *invoke = &invokefd.inv;
  1290. struct fastrpc_ioctl_mmap mmap;
  1291. struct fastrpc_ioctl_munmap munmap;
  1292. void *param = (char *)ioctl_param;
  1293. struct file_data *fdata = (struct file_data *)file->private_data;
  1294. int size = 0, err = 0;
  1295. switch (ioctl_num) {
  1296. case FASTRPC_IOCTL_INVOKE_FD:
  1297. case FASTRPC_IOCTL_INVOKE:
  1298. invokefd.fds = NULL;
  1299. size = (ioctl_num == FASTRPC_IOCTL_INVOKE) ?
  1300. sizeof(*invoke) : sizeof(invokefd);
  1301. VERIFY(err, 0 == copy_from_user(&invokefd, param, size));
  1302. if (err)
  1303. goto bail;
  1304. VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, fdata->mode,
  1305. 0, &invokefd, fdata->cid)));
  1306. if (err)
  1307. goto bail;
  1308. break;
  1309. case FASTRPC_IOCTL_MMAP:
  1310. VERIFY(err, 0 == copy_from_user(&mmap, param,
  1311. sizeof(mmap)));
  1312. if (err)
  1313. goto bail;
  1314. VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata,
  1315. &mmap)));
  1316. if (err)
  1317. goto bail;
  1318. VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap)));
  1319. if (err)
  1320. goto bail;
  1321. break;
  1322. case FASTRPC_IOCTL_MUNMAP:
  1323. VERIFY(err, 0 == copy_from_user(&munmap, param,
  1324. sizeof(munmap)));
  1325. if (err)
  1326. goto bail;
  1327. VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata,
  1328. &munmap)));
  1329. if (err)
  1330. goto bail;
  1331. break;
  1332. case FASTRPC_IOCTL_SETMODE:
  1333. switch ((uint32_t)ioctl_param) {
  1334. case FASTRPC_MODE_PARALLEL:
  1335. case FASTRPC_MODE_SERIAL:
  1336. fdata->mode = (uint32_t)ioctl_param;
  1337. break;
  1338. default:
  1339. err = -ENOTTY;
  1340. break;
  1341. }
  1342. break;
  1343. // Handle FASTRPC_IOCTL_CONTROL so a STS test passes, we don't have most functionality of this driver in 3.4
  1344. // #define FASTRPC_IOCTL_CONTROL _IOWR('R', 12, struct fastrpc_ioctl_control)
  1345. case _IOC(_IOC_READ|_IOC_WRITE, 'R', 12, 12):
  1346. pr_info("adsprpc: FASTRPC_IOCTL_CONTROL is a stub!\n");
  1347. break;
  1348. default:
  1349. err = -ENOTTY;
  1350. break;
  1351. }
  1352. bail:
  1353. return err;
  1354. }
  1355. static const struct file_operations fops = {
  1356. .open = fastrpc_device_open,
  1357. .release = fastrpc_device_release,
  1358. .unlocked_ioctl = fastrpc_device_ioctl,
  1359. };
  1360. static int __init fastrpc_device_init(void)
  1361. {
  1362. struct fastrpc_apps *me = &gfa;
  1363. int i, err = 0;
  1364. memset(me, 0, sizeof(*me));
  1365. VERIFY(err, 0 == fastrpc_init());
  1366. if (err)
  1367. goto fastrpc_bail;
  1368. VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
  1369. DEVICE_NAME));
  1370. if (err)
  1371. goto alloc_chrdev_bail;
  1372. cdev_init(&me->cdev, &fops);
  1373. me->cdev.owner = THIS_MODULE;
  1374. VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
  1375. NUM_CHANNELS));
  1376. if (err)
  1377. goto cdev_init_bail;
  1378. me->class = class_create(THIS_MODULE, "fastrpc");
  1379. VERIFY(err, !IS_ERR(me->class));
  1380. if (err)
  1381. goto class_create_bail;
  1382. for (i = 0; i < NUM_CHANNELS; i++) {
  1383. me->channel[i].dev = device_create(me->class, NULL,
  1384. MKDEV(MAJOR(me->dev_no), i),
  1385. NULL, gcinfo[i].name);
  1386. VERIFY(err, !IS_ERR(me->channel[i].dev));
  1387. if (err)
  1388. goto device_create_bail;
  1389. }
  1390. return 0;
  1391. device_create_bail:
  1392. class_destroy(me->class);
  1393. class_create_bail:
  1394. cdev_del(&me->cdev);
  1395. cdev_init_bail:
  1396. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  1397. alloc_chrdev_bail:
  1398. fastrpc_deinit();
  1399. fastrpc_bail:
  1400. return err;
  1401. }
  1402. static void __exit fastrpc_device_exit(void)
  1403. {
  1404. struct fastrpc_apps *me = &gfa;
  1405. int i;
  1406. context_list_dtor(me, &me->clst);
  1407. fastrpc_deinit();
  1408. for (i = 0; i < NUM_CHANNELS; i++) {
  1409. cleanup_current_dev(i);
  1410. device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
  1411. }
  1412. class_destroy(me->class);
  1413. cdev_del(&me->cdev);
  1414. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  1415. }
  1416. module_init(fastrpc_device_init);
  1417. module_exit(fastrpc_device_exit);
  1418. MODULE_LICENSE("GPL v2");