fastrpc.c 60 KB

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