spcom.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. /*
  6. * Secure-Processor-Communication (SPCOM).
  7. *
  8. * This driver provides communication to Secure Processor (SP)
  9. * over RPMSG framework.
  10. *
  11. * It provides interface to userspace spcomlib.
  12. *
  13. * Userspace application shall use spcomlib for communication with SP.
  14. * Userspace application can be either client or server. spcomlib shall
  15. * use write() file operation to send data, and read() file operation
  16. * to read data.
  17. *
  18. * This driver uses RPMSG with glink-spss as a transport layer.
  19. * This driver exposes "/dev/<sp-channel-name>" file node for each rpmsg
  20. * logical channel.
  21. * This driver exposes "/dev/spcom" file node for some debug/control command.
  22. * The predefined channel "/dev/sp_kernel" is used for loading SP application
  23. * from HLOS.
  24. * This driver exposes "/dev/sp_ssr" file node to allow user space poll for SSR.
  25. * After the remote SP App is loaded, this driver exposes a new file node
  26. * "/dev/<ch-name>" for the matching HLOS App to use.
  27. * The access to predefined file nodes and dynamically allocated file nodes is
  28. * restricted by using unix group and SELinux.
  29. *
  30. * No message routing is used, but using the rpmsg/G-Link "multiplexing" feature
  31. * to use a dedicated logical channel for HLOS and SP Application-Pair.
  32. *
  33. * Each HLOS/SP Application can be either Client or Server or both,
  34. * Messaging is allways point-to-point between 2 HLOS<=>SP applications.
  35. * Each channel exclusevly used by single Client or Server.
  36. *
  37. * User Space Request & Response are synchronous.
  38. * read() & write() operations are blocking until completed or terminated.
  39. */
  40. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  41. #include <linux/kernel.h> /* min() */
  42. #include <linux/module.h> /* MODULE_LICENSE */
  43. #include <linux/device.h> /* class_create() */
  44. #include <linux/slab.h> /* kzalloc() */
  45. #include <linux/fs.h> /* file_operations */
  46. #include <linux/cdev.h> /* cdev_add() */
  47. #include <linux/errno.h> /* EINVAL, ETIMEDOUT */
  48. #include <linux/printk.h> /* pr_err() */
  49. #include <linux/bitops.h> /* BIT(x) */
  50. #include <linux/completion.h> /* wait_for_completion_timeout() */
  51. #include <linux/poll.h> /* POLLOUT */
  52. #include <linux/platform_device.h>
  53. #include <linux/of.h> /* of_property_count_strings() */
  54. #include <linux/workqueue.h>
  55. #include <linux/delay.h> /* msleep() */
  56. #include <linux/dma-buf.h>
  57. #include <linux/limits.h>
  58. #include <linux/rpmsg.h>
  59. #include <linux/atomic.h>
  60. #include <linux/list.h>
  61. #include <linux/spcom.h>
  62. #include <linux/remoteproc.h>
  63. #include <linux/remoteproc.h>
  64. #include <linux/ioctl.h>
  65. #include <linux/ipc_logging.h>
  66. #include <linux/pm.h>
  67. #include <linux/string.h>
  68. #define SPCOM_LOG_PAGE_CNT 10
  69. #define spcom_ipc_log_string(_x...) \
  70. ipc_log_string(spcom_ipc_log_context, _x)
  71. #define spcom_pr_err(_fmt, ...) do { \
  72. pr_err(_fmt, ##__VA_ARGS__); \
  73. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  74. } while (0)
  75. #define spcom_pr_warn(_fmt, ...) do { \
  76. pr_warn(_fmt, ##__VA_ARGS__); \
  77. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  78. } while (0)
  79. #define spcom_pr_info(_fmt, ...) do { \
  80. pr_info(_fmt, ##__VA_ARGS__); \
  81. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  82. } while (0)
  83. #if defined(DEBUG)
  84. #define spcom_pr_dbg(_fmt, ...) do { \
  85. pr_debug(_fmt, ##__VA_ARGS__); \
  86. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  87. } while (0)
  88. #else
  89. #define spcom_pr_dbg(_fmt, ...) do { \
  90. no_printk("%s" pr_fmt(_fmt), KERN_DEBUG, ##__VA_ARGS__); \
  91. spcom_ipc_log_string("%s" pr_fmt(_fmt), "", ##__VA_ARGS__); \
  92. } while (0)
  93. #endif
  94. /**
  95. * Request buffer size.
  96. * Any large data (multiply of 4KB) is provided by temp buffer in DDR.
  97. * Request shall provide the temp buffer physical address (align to 4KB).
  98. * Maximum request/response size of 268 is used to accommodate APDU size.
  99. * From kernel spcom driver perspective a PAGE_SIZE of 4K
  100. * is the actual maximum size for a single read/write file operation.
  101. */
  102. #define SPCOM_MAX_RESPONSE_SIZE 268
  103. /* SPCOM driver name */
  104. #define DEVICE_NAME "spcom"
  105. /* maximum clients that can register over a single channel */
  106. #define SPCOM_MAX_CHANNEL_CLIENTS 2
  107. /* maximum shared DMA_buf buffers should be >= SPCOM_MAX_CHANNELS */
  108. #define SPCOM_MAX_DMA_BUF_PER_CH (SPCOM_MAX_CHANNELS + 4)
  109. /* maximum ION buffer per send request/response command */
  110. #define SPCOM_MAX_ION_BUF_PER_CMD SPCOM_MAX_ION_BUF
  111. /* Maximum command size */
  112. #define SPCOM_MAX_COMMAND_SIZE (PAGE_SIZE)
  113. /* Maximum input size */
  114. #define SPCOM_MAX_READ_SIZE (PAGE_SIZE)
  115. /* Current Process ID */
  116. #define current_pid() ((u32)(current->tgid))
  117. /*
  118. * After both sides get CONNECTED,
  119. * there is a race between one side queueing rx buffer and the other side
  120. * trying to call glink_tx() , this race is only on the 1st tx.
  121. * Do tx retry with some delay to allow the other side to queue rx buffer.
  122. */
  123. #define TX_RETRY_DELAY_MSEC 100
  124. /* SPCOM_MAX_REQUEST_SIZE-or-SPCOM_MAX_RESPONSE_SIZE + header */
  125. #define SPCOM_RX_BUF_SIZE 300
  126. /*
  127. * Initial transaction id, use non-zero nonce for debug.
  128. * Incremented by client on request, and copied back by server on response.
  129. */
  130. #define INITIAL_TXN_ID 0x12345678
  131. /*
  132. * Maximum number of control channels between spcom driver and
  133. * user-mode processes
  134. */
  135. #define SPCOM_MAX_CONTROL_CHANNELS SPCOM_MAX_CHANNELS
  136. /*
  137. * To be used for ioctl copy arg from user if the IOCTL direction is _IOC_WRITE
  138. * Update the union when new ioctl struct is added
  139. */
  140. union spcom_ioctl_arg {
  141. struct spcom_poll_param poll;
  142. struct spcom_ioctl_poll_event poll_event;
  143. struct spcom_ioctl_ch channel;
  144. struct spcom_ioctl_message message;
  145. struct spcom_ioctl_modified_message modified_message;
  146. struct spcom_ioctl_next_request_size next_req_size;
  147. struct spcom_ioctl_dmabuf_lock dmabuf_lock;
  148. } __packed;
  149. /*
  150. * Max time to keep PM from suspend.
  151. * From receive RPMSG packet till wakeup source will be deactivated.
  152. */
  153. #define SPCOM_PM_PACKET_HANDLE_TIMEOUT (2 * MSEC_PER_SEC)
  154. /**
  155. * struct spcom_msg_hdr - Request/Response message header between HLOS and SP.
  156. *
  157. * This header is proceeding any request specific parameters.
  158. * The transaction id is used to match request with response.
  159. * Note: rpmsg API provides the rx/tx data size, so user payload size is
  160. * calculated by reducing the header size.
  161. */
  162. struct spcom_msg_hdr {
  163. uint32_t reserved; /* for future use */
  164. uint32_t txn_id; /* transaction id */
  165. char buf[0]; /* Variable buffer size, must be last field */
  166. } __packed;
  167. /**
  168. * struct spcom_client - Client handle
  169. */
  170. struct spcom_client {
  171. struct spcom_channel *ch;
  172. };
  173. /**
  174. * struct spcom_server - Server handle
  175. */
  176. struct spcom_server {
  177. struct spcom_channel *ch;
  178. };
  179. /**
  180. * struct dma_buf_info - DMA BUF support information
  181. */
  182. struct dma_buf_info {
  183. int fd;
  184. struct dma_buf *handle;
  185. struct dma_buf_attachment *attach;
  186. struct sg_table *sg;
  187. u32 owner_pid;
  188. };
  189. /**
  190. * struct spcom_channel - channel context
  191. */
  192. struct spcom_channel {
  193. char name[SPCOM_CHANNEL_NAME_SIZE];
  194. struct mutex lock;
  195. uint32_t txn_id; /* incrementing nonce per client request */
  196. bool is_server; /* for txn_id and response_timeout_msec */
  197. bool comm_role_undefined; /* is true on channel creation before first tx/rx on channel */
  198. uint32_t response_timeout_msec; /* for client only */
  199. /* char dev */
  200. struct cdev *cdev;
  201. struct device *dev;
  202. struct device_attribute attr;
  203. dev_t devt;
  204. /* rpmsg */
  205. struct rpmsg_driver *rpdrv;
  206. struct rpmsg_device *rpdev;
  207. /* Events notification */
  208. struct completion rx_done;
  209. struct completion connect;
  210. /**
  211. * Only one client or server per non-sharable channel
  212. * SPCOM_MAX_CHANNEL_CLIENTS clients for sharable channel
  213. * Only one tx-rx transaction at a time (request + response)
  214. */
  215. bool is_busy;
  216. bool is_sharable; /* channel's sharable property */
  217. u32 max_clients; /* number of max clients */
  218. u32 active_pid; /* current tx-rx transaction pid */
  219. uint8_t num_clients; /* current number of clients */
  220. struct mutex shared_sync_lock;
  221. u32 pid[SPCOM_MAX_CHANNEL_CLIENTS];
  222. /* abort flags */
  223. bool rpmsg_abort;
  224. /* rx data info */
  225. size_t actual_rx_size; /* actual data size received */
  226. void *rpmsg_rx_buf;
  227. /**
  228. * to track if rx_buf is read in the same session
  229. * in which it is updated
  230. */
  231. uint32_t rx_buf_txn_id;
  232. /* shared buffer lock/unlock support */
  233. struct dma_buf_info dmabuf_array[SPCOM_MAX_DMA_BUF_PER_CH];
  234. };
  235. /**
  236. * struct rx_buff_list - holds rx rpmsg data, before it will be consumed
  237. * by spcom_signal_rx_done worker, item per rx packet
  238. */
  239. struct rx_buff_list {
  240. struct list_head list;
  241. void *rpmsg_rx_buf;
  242. int rx_buf_size;
  243. struct spcom_channel *ch;
  244. };
  245. /**
  246. * struct spcom_channel - control channel information
  247. */
  248. struct spcom_control_channel_info {
  249. u32 pid;
  250. u32 ref_cnt;
  251. } spcom_control_channel_info;
  252. /**
  253. * struct spcom_device - device state structure.
  254. */
  255. struct spcom_device {
  256. char predefined_ch_name[SPCOM_MAX_CHANNELS][SPCOM_CHANNEL_NAME_SIZE];
  257. /* char device info */
  258. struct cdev cdev;
  259. dev_t device_no;
  260. struct class *driver_class;
  261. struct device *class_dev;
  262. struct platform_device *pdev;
  263. struct wakeup_source *ws;
  264. /* rpmsg channels */
  265. struct spcom_channel channels[SPCOM_MAX_CHANNELS];
  266. unsigned int chdev_count;
  267. struct mutex chdev_count_lock;
  268. struct mutex ch_list_lock;
  269. struct completion rpmsg_state_change;
  270. atomic_t rpmsg_dev_count;
  271. atomic_t remove_in_progress;
  272. /* rx data path */
  273. struct list_head rx_list_head;
  274. spinlock_t rx_lock;
  275. int32_t nvm_ion_fd;
  276. uint32_t rmb_error; /* PBL error value storet here */
  277. atomic_t subsys_req;
  278. struct rproc *spss_rproc;
  279. struct property *rproc_prop;
  280. /* Control channels */
  281. struct spcom_control_channel_info control_channels[SPCOM_MAX_CONTROL_CHANNELS];
  282. };
  283. /* Device Driver State */
  284. static struct spcom_device *spcom_dev;
  285. static void *spcom_ipc_log_context;
  286. /* static functions declaration */
  287. static int spcom_create_channel_chardev(const char *name, bool is_sharable);
  288. static int spcom_destroy_channel_chardev(const char *name);
  289. static struct spcom_channel *spcom_find_channel_by_name(const char *name);
  290. static int spcom_register_rpmsg_drv(struct spcom_channel *ch);
  291. static int spcom_unregister_rpmsg_drv(struct spcom_channel *ch);
  292. static void spcom_release_all_channels_of_process(u32 pid);
  293. static int spom_control_channel_add_client(u32 pid);
  294. static int spom_control_channel_remove_client(u32 pid);
  295. /**
  296. * spcom_is_channel_open() - channel is open on this side.
  297. *
  298. * Channel is fully connected, when rpmsg driver is registered and
  299. * rpmsg device probed
  300. */
  301. static inline bool spcom_is_channel_open(struct spcom_channel *ch)
  302. {
  303. return ch->rpdrv != NULL;
  304. }
  305. /**
  306. * spcom_is_channel_connected() - channel is fully connected by both sides.
  307. */
  308. static inline bool spcom_is_channel_connected(struct spcom_channel *ch)
  309. {
  310. /* Channel must be open before it gets connected */
  311. if (!spcom_is_channel_open(ch))
  312. return false;
  313. return ch->rpdev != NULL;
  314. }
  315. /**
  316. * spcom_create_predefined_channels_chardev() - expose predefined channels to
  317. * user space.
  318. *
  319. * Predefined channels list is provided by device tree. Typically, it is for
  320. * known servers on remote side that are not loaded by the HLOS
  321. */
  322. static int spcom_create_predefined_channels_chardev(void)
  323. {
  324. int i, j;
  325. int ret, rc;
  326. static bool is_predefined_created;
  327. const char *name;
  328. if (is_predefined_created)
  329. return 0;
  330. for (i = 0; i < SPCOM_MAX_CHANNELS; i++) {
  331. name = spcom_dev->predefined_ch_name[i];
  332. if (name[0] == 0)
  333. break;
  334. mutex_lock(&spcom_dev->chdev_count_lock);
  335. ret = spcom_create_channel_chardev(name, false);
  336. mutex_unlock(&spcom_dev->chdev_count_lock);
  337. if (ret) {
  338. spcom_pr_err("fail to create chardev [%s], ret [%d]\n",
  339. name, ret);
  340. goto destroy_channels;
  341. }
  342. }
  343. is_predefined_created = true;
  344. return 0;
  345. destroy_channels:
  346. /* destroy previously created channels */
  347. for (j = 0; j < i; j++) {
  348. name = spcom_dev->predefined_ch_name[j];
  349. if (name[0] == 0)
  350. break;
  351. rc = spcom_destroy_channel_chardev(name);
  352. if (rc) {
  353. spcom_pr_err("fail to destroy chardev [%s], ret [%d]\n",
  354. name, rc);
  355. }
  356. }
  357. return ret;
  358. }
  359. /*======================================================================*/
  360. /* UTILITIES */
  361. /*======================================================================*/
  362. /**
  363. * spcom_init_channel() - initialize channel state.
  364. *
  365. * @ch: channel state struct pointer
  366. * @is_sharable: whether channel is sharable
  367. * @name: channel name
  368. */
  369. static int spcom_init_channel(struct spcom_channel *ch,
  370. bool is_sharable,
  371. const char *name)
  372. {
  373. if (!ch || !name || !name[0]) {
  374. spcom_pr_err("invalid parameters\n");
  375. return -EINVAL;
  376. }
  377. strscpy(ch->name, name, SPCOM_CHANNEL_NAME_SIZE);
  378. init_completion(&ch->rx_done);
  379. init_completion(&ch->connect);
  380. mutex_init(&ch->lock);
  381. ch->rpdrv = NULL;
  382. ch->rpdev = NULL;
  383. ch->actual_rx_size = 0;
  384. ch->is_busy = false;
  385. ch->txn_id = INITIAL_TXN_ID; /* use non-zero nonce for debug */
  386. ch->rx_buf_txn_id = ch->txn_id;
  387. memset(ch->pid, 0, sizeof(ch->pid));
  388. ch->rpmsg_abort = false;
  389. ch->rpmsg_rx_buf = NULL;
  390. ch->comm_role_undefined = true;
  391. ch->is_sharable = is_sharable;
  392. ch->max_clients = is_sharable ? SPCOM_MAX_CHANNEL_CLIENTS : 1;
  393. ch->active_pid = 0;
  394. ch->num_clients = 0;
  395. mutex_init(&ch->shared_sync_lock);
  396. return 0;
  397. }
  398. /**
  399. * spcom_find_channel_by_name() - find a channel by name.
  400. *
  401. * @name: channel name
  402. *
  403. * Return: a channel state struct.
  404. */
  405. static struct spcom_channel *spcom_find_channel_by_name(const char *name)
  406. {
  407. int i;
  408. for (i = 0 ; i < ARRAY_SIZE(spcom_dev->channels); i++) {
  409. struct spcom_channel *ch = &spcom_dev->channels[i];
  410. if (strcmp(ch->name, name) == 0)
  411. return ch;
  412. }
  413. return NULL;
  414. }
  415. /**
  416. * spcom_rx() - wait for received data until timeout, unless pending rx data is
  417. * already ready
  418. *
  419. * @ch: channel state struct pointer
  420. * @buf: buffer pointer
  421. * @size: buffer size
  422. *
  423. * Return: size in bytes on success, negative value on failure.
  424. */
  425. static int spcom_rx(struct spcom_channel *ch,
  426. void *buf,
  427. uint32_t size,
  428. uint32_t timeout_msec)
  429. {
  430. unsigned long jiffies = msecs_to_jiffies(timeout_msec);
  431. long timeleft = 1;
  432. int ret = 0;
  433. mutex_lock(&ch->lock);
  434. if (ch->rx_buf_txn_id != ch->txn_id) {
  435. spcom_pr_dbg("ch[%s]:ch->rx_buf_txn_id=%d is updated in a different session\n",
  436. ch->name, ch->rx_buf_txn_id);
  437. if (ch->rpmsg_rx_buf) {
  438. memset(ch->rpmsg_rx_buf, 0, ch->actual_rx_size);
  439. kfree((void *)ch->rpmsg_rx_buf);
  440. ch->rpmsg_rx_buf = NULL;
  441. ch->actual_rx_size = 0;
  442. }
  443. }
  444. /* check for already pending data */
  445. if (!ch->actual_rx_size) {
  446. reinit_completion(&ch->rx_done);
  447. mutex_unlock(&ch->lock); /* unlock while waiting */
  448. /* wait for rx response */
  449. if (timeout_msec)
  450. timeleft = wait_for_completion_interruptible_timeout(
  451. &ch->rx_done, jiffies);
  452. else
  453. ret = wait_for_completion_interruptible(&ch->rx_done);
  454. mutex_lock(&ch->lock);
  455. if (timeout_msec && timeleft == 0) {
  456. spcom_pr_err("ch[%s]: timeout expired %d ms, set txn_id=%d\n",
  457. ch->name, timeout_msec, ch->txn_id);
  458. ch->txn_id++; /* to drop expired rx packet later */
  459. ret = -ETIMEDOUT;
  460. goto exit_err;
  461. } else if (ch->rpmsg_abort) {
  462. spcom_pr_warn("rpmsg channel is closing\n");
  463. ret = -ERESTART;
  464. goto exit_err;
  465. } else if (ret < 0 || timeleft < 0) {
  466. spcom_pr_err("rx wait was interrupted!");
  467. ret = -EINTR; /* abort, not restartable */
  468. goto exit_err;
  469. } else if (ch->actual_rx_size) {
  470. spcom_pr_dbg("ch[%s]:actual_rx_size is [%zu], txn_id %d\n",
  471. ch->name, ch->actual_rx_size, ch->txn_id);
  472. } else {
  473. spcom_pr_err("ch[%s]:actual_rx_size==0\n", ch->name);
  474. ret = -EFAULT;
  475. goto exit_err;
  476. }
  477. } else {
  478. spcom_pr_dbg("ch[%s]:rx data size [%zu], txn_id:%d\n",
  479. ch->name, ch->actual_rx_size, ch->txn_id);
  480. }
  481. if (!ch->rpmsg_rx_buf) {
  482. spcom_pr_err("ch[%s]:invalid rpmsg_rx_buf\n", ch->name);
  483. ret = -ENOMEM;
  484. goto exit_err;
  485. }
  486. size = min_t(size_t, ch->actual_rx_size, size);
  487. memcpy(buf, ch->rpmsg_rx_buf, size);
  488. memset(ch->rpmsg_rx_buf, 0, ch->actual_rx_size);
  489. kfree((void *)ch->rpmsg_rx_buf);
  490. ch->rpmsg_rx_buf = NULL;
  491. ch->actual_rx_size = 0;
  492. mutex_unlock(&ch->lock);
  493. return size;
  494. exit_err:
  495. mutex_unlock(&ch->lock);
  496. return ret;
  497. }
  498. /**
  499. * spcom_get_next_request_size() - get request size.
  500. * already ready
  501. *
  502. * @ch: channel state struct pointer
  503. *
  504. * Server needs the size of the next request to allocate a request buffer.
  505. * Initially used intent-request, however this complicated the remote side,
  506. * so both sides are not using glink_tx() with INTENT_REQ anymore.
  507. *
  508. * Return: size in bytes on success, negative value on failure.
  509. */
  510. static int spcom_get_next_request_size(struct spcom_channel *ch)
  511. {
  512. int size = -1;
  513. int ret = 0;
  514. /* NOTE: Remote clients might not be connected yet.*/
  515. mutex_lock(&ch->lock);
  516. /* Update communication role of channel if not set yet */
  517. if (ch->comm_role_undefined) {
  518. spcom_pr_dbg("server [%s] reading it's first request\n", ch->name);
  519. ch->comm_role_undefined = false;
  520. ch->is_server = true;
  521. }
  522. reinit_completion(&ch->rx_done);
  523. /* check if already got it via callback */
  524. if (ch->actual_rx_size) {
  525. spcom_pr_dbg("next-req-size already ready ch [%s] size [%zu]\n",
  526. ch->name, ch->actual_rx_size);
  527. goto exit_ready;
  528. }
  529. mutex_unlock(&ch->lock); /* unlock while waiting */
  530. ret = wait_for_completion_interruptible(&ch->rx_done);
  531. if (ret < 0) {
  532. spcom_pr_dbg("ch [%s]:interrupted wait ret=%d\n",
  533. ch->name, ret);
  534. goto exit_error;
  535. }
  536. mutex_lock(&ch->lock); /* re-lock after waiting */
  537. if (ch->actual_rx_size == 0) {
  538. spcom_pr_err("invalid rx size [%zu] ch [%s]\n",
  539. ch->actual_rx_size, ch->name);
  540. mutex_unlock(&ch->lock);
  541. ret = -EFAULT;
  542. goto exit_error;
  543. }
  544. exit_ready:
  545. /* actual_rx_size not ecxeeds SPCOM_RX_BUF_SIZE*/
  546. size = (int)ch->actual_rx_size;
  547. if (size > sizeof(struct spcom_msg_hdr)) {
  548. size -= sizeof(struct spcom_msg_hdr);
  549. } else {
  550. spcom_pr_err("rx size [%d] too small\n", size);
  551. ret = -EFAULT;
  552. mutex_unlock(&ch->lock);
  553. goto exit_error;
  554. }
  555. mutex_unlock(&ch->lock);
  556. return size;
  557. exit_error:
  558. return ret;
  559. }
  560. /*======================================================================*/
  561. /* USER SPACE commands handling */
  562. /*======================================================================*/
  563. /**
  564. * spcom_handle_create_channel_command() - Handle Create Channel command from
  565. * user space.
  566. *
  567. * @cmd_buf: command buffer.
  568. * @cmd_size: command buffer size.
  569. *
  570. * Return: 0 on successful operation, negative value otherwise.
  571. */
  572. static int spcom_handle_create_channel_command(void *cmd_buf, int cmd_size)
  573. {
  574. int ret = 0;
  575. struct spcom_user_create_channel_command *cmd = cmd_buf;
  576. if (cmd_size != sizeof(*cmd)) {
  577. spcom_pr_err("cmd_size [%d] , expected [%d]\n",
  578. (int) cmd_size, (int) sizeof(*cmd));
  579. return -EINVAL;
  580. }
  581. mutex_lock(&spcom_dev->chdev_count_lock);
  582. ret = spcom_create_channel_chardev(cmd->ch_name, cmd->is_sharable);
  583. mutex_unlock(&spcom_dev->chdev_count_lock);
  584. if (ret)
  585. spcom_pr_err("failed to create ch[%s], ret [%d]\n", cmd->ch_name, ret);
  586. return ret;
  587. }
  588. /**
  589. * spcom_handle_restart_sp_command() - Handle Restart SP command from
  590. * user space.
  591. *
  592. * @cmd_buf: command buffer.
  593. * @cmd_size: command buffer size.
  594. *
  595. * Return: 0 on successful operation, negative value otherwise.
  596. */
  597. static int spcom_handle_restart_sp_command(void *cmd_buf, int cmd_size)
  598. {
  599. struct spcom_user_restart_sp_command *cmd = cmd_buf;
  600. int ret;
  601. if (!cmd) {
  602. spcom_pr_err("NULL cmd_buf\n");
  603. return -EINVAL;
  604. }
  605. if (cmd_size != sizeof(*cmd)) {
  606. spcom_pr_err("cmd_size [%d] , expected [%d]\n",
  607. (int) cmd_size, (int) sizeof(*cmd));
  608. return -EINVAL;
  609. }
  610. spcom_dev->spss_rproc = rproc_get_by_phandle(be32_to_cpup(spcom_dev->rproc_prop->value));
  611. if (!spcom_dev->spss_rproc) {
  612. pr_err("rproc device not found\n");
  613. return -ENODEV; /* no spss peripheral exist */
  614. }
  615. ret = rproc_boot(spcom_dev->spss_rproc);
  616. if (ret == -ETIMEDOUT) {
  617. /* userspace handles retry if needed */
  618. spcom_pr_err("FW loading process timeout\n");
  619. } else if (ret) {
  620. /*
  621. * SPU shutdown. Return value comes from SPU PBL message.
  622. * The error is not recoverable and userspace handles it
  623. * by request and analyse rmb_error value
  624. */
  625. spcom_dev->rmb_error = (uint32_t)ret;
  626. spcom_pr_err("spss crashed during device bootup rmb_error[0x%x]\n",
  627. spcom_dev->rmb_error);
  628. ret = -ENODEV;
  629. } else {
  630. spcom_pr_info("FW loading process is complete\n");
  631. }
  632. return ret;
  633. }
  634. /**
  635. * spcom_handle_send_command() - Handle send request/response from user space.
  636. *
  637. * @buf: command buffer.
  638. * @buf_size: command buffer size.
  639. *
  640. * Return: 0 on successful operation, negative value otherwise.
  641. */
  642. static int spcom_handle_send_command(struct spcom_channel *ch,
  643. void *cmd_buf, int size)
  644. {
  645. int ret = 0;
  646. struct spcom_send_command *cmd = cmd_buf;
  647. uint32_t buf_size;
  648. void *buf;
  649. struct spcom_msg_hdr *hdr;
  650. void *tx_buf;
  651. int tx_buf_size;
  652. uint32_t timeout_msec;
  653. int time_msec = 0;
  654. spcom_pr_dbg("send req/resp ch [%s] size [%d]\n", ch->name, size);
  655. /*
  656. * check that cmd buf size is at least struct size,
  657. * to allow access to struct fields.
  658. */
  659. if (size < sizeof(*cmd)) {
  660. spcom_pr_err("ch [%s] invalid cmd buf\n",
  661. ch->name);
  662. return -EINVAL;
  663. }
  664. /* Check if remote side connect */
  665. if (!spcom_is_channel_connected(ch)) {
  666. spcom_pr_err("ch [%s] remote side not connect\n", ch->name);
  667. return -ENOTCONN;
  668. }
  669. /* parse command buffer */
  670. buf = &cmd->buf;
  671. buf_size = cmd->buf_size;
  672. timeout_msec = cmd->timeout_msec;
  673. /* Check param validity */
  674. if (buf_size > SPCOM_MAX_RESPONSE_SIZE) {
  675. spcom_pr_err("ch [%s] invalid buf size [%d]\n",
  676. ch->name, buf_size);
  677. return -EINVAL;
  678. }
  679. if (size != sizeof(*cmd) + buf_size) {
  680. spcom_pr_err("ch [%s] invalid cmd size [%d]\n",
  681. ch->name, size);
  682. return -EINVAL;
  683. }
  684. /* Allocate Buffers*/
  685. tx_buf_size = sizeof(*hdr) + buf_size;
  686. tx_buf = kzalloc(tx_buf_size, GFP_KERNEL);
  687. if (!tx_buf)
  688. return -ENOMEM;
  689. /* Prepare Tx Buf */
  690. hdr = tx_buf;
  691. mutex_lock(&ch->lock);
  692. if (ch->comm_role_undefined) {
  693. spcom_pr_dbg("ch [%s] send first -> it is client\n", ch->name);
  694. ch->comm_role_undefined = false;
  695. ch->is_server = false;
  696. }
  697. if (!ch->is_server) {
  698. ch->txn_id++; /* client sets the request txn_id */
  699. ch->response_timeout_msec = timeout_msec;
  700. }
  701. hdr->txn_id = ch->txn_id;
  702. /* user buf */
  703. memcpy(hdr->buf, buf, buf_size);
  704. time_msec = 0;
  705. do {
  706. if (ch->rpmsg_abort) {
  707. spcom_pr_err("ch [%s] aborted\n", ch->name);
  708. ret = -ECANCELED;
  709. break;
  710. }
  711. /* may fail when RX intent not queued by SP */
  712. ret = rpmsg_trysend(ch->rpdev->ept, tx_buf, tx_buf_size);
  713. if (ret == 0) {
  714. spcom_pr_dbg("ch[%s]: successfully sent txn_id=%d\n",
  715. ch->name, ch->txn_id);
  716. break;
  717. }
  718. time_msec += TX_RETRY_DELAY_MSEC;
  719. mutex_unlock(&ch->lock);
  720. msleep(TX_RETRY_DELAY_MSEC);
  721. mutex_lock(&ch->lock);
  722. } while ((ret == -EBUSY || ret == -EAGAIN) && time_msec < timeout_msec);
  723. if (ret)
  724. spcom_pr_err("ch [%s] rpmsg_trysend() error (%d), timeout_msec=%d\n",
  725. ch->name, ret, timeout_msec);
  726. if (ch->is_server) {
  727. __pm_relax(spcom_dev->ws);
  728. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  729. ch->name);
  730. }
  731. mutex_unlock(&ch->lock);
  732. kfree(tx_buf);
  733. return ret;
  734. }
  735. /**
  736. * modify_dma_buf_addr() - replace the ION buffer virtual address with physical
  737. * address in a request or response buffer.
  738. *
  739. * @buf: buffer to modify
  740. * @buf_size: buffer size
  741. * @info: DMA buffer info such as FD and offset in buffer.
  742. *
  743. * Return: 0 on successful operation, negative value otherwise.
  744. */
  745. static int modify_dma_buf_addr(struct spcom_channel *ch, void *buf,
  746. uint32_t buf_size,
  747. struct spcom_dma_buf_info *info)
  748. {
  749. struct dma_buf *dma_buf = NULL;
  750. struct dma_buf_attachment *attach = NULL;
  751. struct sg_table *sg = NULL;
  752. char *ptr = (char *)buf;
  753. dma_addr_t phy_addr = 0;
  754. uint32_t buf_offset = 0;
  755. int fd, ret = 0;
  756. int i = 0;
  757. bool found_handle = false;
  758. fd = info->fd;
  759. buf_offset = info->offset;
  760. ptr += buf_offset;
  761. if (fd < 0) {
  762. spcom_pr_err("invalid fd [%d]\n", fd);
  763. return -ENODEV;
  764. }
  765. if (buf_size < sizeof(uint64_t)) {
  766. spcom_pr_err("buf size too small [%d]\n", buf_size);
  767. return -ENODEV;
  768. }
  769. if (buf_offset % sizeof(uint64_t))
  770. spcom_pr_dbg("offset [%d] is NOT 64-bit aligned\n", buf_offset);
  771. else
  772. spcom_pr_dbg("offset [%d] is 64-bit aligned\n", buf_offset);
  773. if (buf_offset > buf_size - sizeof(uint64_t)) {
  774. spcom_pr_err("invalid buf_offset [%d]\n", buf_offset);
  775. return -ENODEV;
  776. }
  777. dma_buf = dma_buf_get(fd);
  778. if (IS_ERR_OR_NULL(dma_buf)) {
  779. spcom_pr_err("fail to get dma buf handle\n");
  780. return -EINVAL;
  781. }
  782. attach = dma_buf_attach(dma_buf, &spcom_dev->pdev->dev);
  783. if (IS_ERR_OR_NULL(attach)) {
  784. ret = PTR_ERR(attach);
  785. spcom_pr_err("fail to attach dma buf %d\n", ret);
  786. dma_buf_put(dma_buf);
  787. goto mem_map_table_failed;
  788. }
  789. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  790. if (IS_ERR_OR_NULL(sg)) {
  791. ret = PTR_ERR(sg);
  792. spcom_pr_err("fail to get sg table of dma buf %d\n", ret);
  793. goto mem_map_table_failed;
  794. }
  795. if (sg->sgl) {
  796. phy_addr = sg->sgl->dma_address;
  797. } else {
  798. spcom_pr_err("sgl is NULL\n");
  799. ret = -ENOMEM;
  800. goto mem_map_sg_failed;
  801. }
  802. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  803. if (ch->dmabuf_array[i].handle == dma_buf) {
  804. ch->dmabuf_array[i].attach = attach;
  805. ch->dmabuf_array[i].sg = sg;
  806. found_handle = true;
  807. break;
  808. }
  809. }
  810. if (!found_handle) {
  811. spcom_pr_err("ch [%s]: trying to send modified command on unlocked buffer\n",
  812. ch->name);
  813. ret = -EPERM;
  814. goto mem_map_sg_failed;
  815. }
  816. /* Set the physical address at the buffer offset */
  817. spcom_pr_dbg("dma phys addr = [0x%lx]\n", (long) phy_addr);
  818. memcpy(ptr, &phy_addr, sizeof(phy_addr));
  819. /* Don't unmap the buffer to allow dmabuf sync start/end. */
  820. dma_buf_put(dma_buf);
  821. return 0;
  822. mem_map_sg_failed:
  823. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  824. mem_map_table_failed:
  825. dma_buf_detach(dma_buf, attach);
  826. dma_buf_put(dma_buf);
  827. return ret;
  828. }
  829. /**
  830. * spcom_handle_send_modified_command() - send a request/response with ION
  831. * buffer address. Modify the request/response by replacing the ION buffer
  832. * virtual address with the physical address.
  833. *
  834. * @ch: channel pointer
  835. * @cmd_buf: User space command buffer
  836. * @size: size of user command buffer
  837. *
  838. * Return: 0 on successful operation, negative value otherwise.
  839. */
  840. static int spcom_handle_send_modified_command(struct spcom_channel *ch,
  841. void *cmd_buf, int size)
  842. {
  843. int ret = 0;
  844. struct spcom_user_send_modified_command *cmd = cmd_buf;
  845. uint32_t buf_size;
  846. void *buf;
  847. struct spcom_msg_hdr *hdr;
  848. void *tx_buf;
  849. int tx_buf_size;
  850. struct spcom_ion_info ion_info[SPCOM_MAX_ION_BUF_PER_CMD];
  851. int i;
  852. uint32_t timeout_msec;
  853. int time_msec = 0;
  854. struct spcom_dma_buf_info curr_info = {0};
  855. spcom_pr_dbg("send req/resp ch [%s] size [%d]\n", ch->name, size);
  856. /*
  857. * check that cmd buf size is at least struct size,
  858. * to allow access to struct fields.
  859. */
  860. if (size < sizeof(*cmd)) {
  861. spcom_pr_err("ch [%s] invalid cmd buf\n",
  862. ch->name);
  863. return -EINVAL;
  864. }
  865. /* Check if remote side connect */
  866. if (!spcom_is_channel_connected(ch)) {
  867. spcom_pr_err("ch [%s] remote side not connect\n", ch->name);
  868. return -ENOTCONN;
  869. }
  870. /* parse command buffer */
  871. buf = &cmd->buf;
  872. buf_size = cmd->buf_size;
  873. timeout_msec = cmd->timeout_msec;
  874. memcpy(ion_info, cmd->ion_info, sizeof(ion_info));
  875. /* Check param validity */
  876. if (buf_size > SPCOM_MAX_RESPONSE_SIZE) {
  877. spcom_pr_err("ch [%s] invalid buf size [%d]\n",
  878. ch->name, buf_size);
  879. return -EINVAL;
  880. }
  881. if (size != sizeof(*cmd) + buf_size) {
  882. spcom_pr_err("ch [%s] invalid cmd size [%d]\n",
  883. ch->name, size);
  884. return -EINVAL;
  885. }
  886. /* Allocate Buffers*/
  887. tx_buf_size = sizeof(*hdr) + buf_size;
  888. tx_buf = kzalloc(tx_buf_size, GFP_KERNEL);
  889. if (!tx_buf)
  890. return -ENOMEM;
  891. /* Prepare Tx Buf */
  892. hdr = tx_buf;
  893. mutex_lock(&ch->lock);
  894. if (ch->comm_role_undefined) {
  895. spcom_pr_dbg("ch [%s] send first -> it is client\n", ch->name);
  896. ch->comm_role_undefined = false;
  897. ch->is_server = false;
  898. }
  899. if (!ch->is_server) {
  900. ch->txn_id++; /* client sets the request txn_id */
  901. ch->response_timeout_msec = timeout_msec;
  902. }
  903. hdr->txn_id = ch->txn_id;
  904. /* user buf */
  905. memcpy(hdr->buf, buf, buf_size);
  906. for (i = 0 ; i < ARRAY_SIZE(ion_info) ; i++) {
  907. if (ion_info[i].fd >= 0) {
  908. curr_info.fd = ion_info[i].fd;
  909. curr_info.offset = ion_info[i].buf_offset;
  910. ret = modify_dma_buf_addr(ch, hdr->buf, buf_size, &curr_info);
  911. if (ret < 0) {
  912. mutex_unlock(&ch->lock);
  913. memset(tx_buf, 0, tx_buf_size);
  914. kfree(tx_buf);
  915. return -EFAULT;
  916. }
  917. }
  918. }
  919. time_msec = 0;
  920. do {
  921. if (ch->rpmsg_abort) {
  922. spcom_pr_err("ch[%s]: aborted, txn_id=%d\n",
  923. ch->name, ch->txn_id);
  924. ret = -ECANCELED;
  925. break;
  926. }
  927. /* may fail when RX intent not queued by SP */
  928. ret = rpmsg_trysend(ch->rpdev->ept, tx_buf, tx_buf_size);
  929. if (ret == 0)
  930. break;
  931. time_msec += TX_RETRY_DELAY_MSEC;
  932. mutex_unlock(&ch->lock);
  933. msleep(TX_RETRY_DELAY_MSEC);
  934. mutex_lock(&ch->lock);
  935. } while ((ret == -EBUSY || ret == -EAGAIN) && time_msec < timeout_msec);
  936. if (ret)
  937. spcom_pr_err("ch [%s] rpmsg_trysend() error (%d), timeout_msec=%d\n",
  938. ch->name, ret, timeout_msec);
  939. if (ch->is_server) {
  940. __pm_relax(spcom_dev->ws);
  941. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  942. ch->name);
  943. }
  944. mutex_unlock(&ch->lock);
  945. memset(tx_buf, 0, tx_buf_size);
  946. kfree(tx_buf);
  947. return ret;
  948. }
  949. /**
  950. * spcom_handle_lock_ion_buf_command() - Lock an shared buffer.
  951. *
  952. * Lock an shared buffer, prevent it from being free if the userspace App crash,
  953. * while it is used by the remote subsystem.
  954. */
  955. static int spcom_handle_lock_ion_buf_command(struct spcom_channel *ch,
  956. void *cmd_buf, int size)
  957. {
  958. struct spcom_user_command *cmd = cmd_buf;
  959. int fd;
  960. int i;
  961. struct dma_buf *dma_buf;
  962. if (size != sizeof(*cmd)) {
  963. spcom_pr_err("cmd size [%d] , expected [%d]\n",
  964. (int) size, (int) sizeof(*cmd));
  965. return -EINVAL;
  966. }
  967. if (cmd->arg > (unsigned int)INT_MAX) {
  968. spcom_pr_err("int overflow [%u]\n", cmd->arg);
  969. return -EINVAL;
  970. }
  971. fd = cmd->arg;
  972. dma_buf = dma_buf_get(fd);
  973. if (IS_ERR_OR_NULL(dma_buf)) {
  974. spcom_pr_err("fail to get dma buf handle\n");
  975. return -EINVAL;
  976. }
  977. /* shared buf lock doesn't involve any rx/tx data to SP. */
  978. mutex_lock(&ch->lock);
  979. /* Check if this shared buffer is already locked */
  980. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  981. if (ch->dmabuf_array[i].handle == dma_buf) {
  982. spcom_pr_dbg("fd [%d] shared buf is already locked\n",
  983. fd);
  984. /* decrement back the ref count */
  985. mutex_unlock(&ch->lock);
  986. dma_buf_put(dma_buf);
  987. return -EINVAL;
  988. }
  989. }
  990. /* Store the dma_buf handle */
  991. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  992. if (ch->dmabuf_array[i].handle == NULL) {
  993. ch->dmabuf_array[i].handle = dma_buf;
  994. ch->dmabuf_array[i].fd = fd;
  995. spcom_pr_dbg("ch [%s] locked ion buf #%d fd [%d] dma_buf=0x%pK\n",
  996. ch->name, i,
  997. ch->dmabuf_array[i].fd,
  998. ch->dmabuf_array[i].handle);
  999. mutex_unlock(&ch->lock);
  1000. return 0;
  1001. }
  1002. }
  1003. mutex_unlock(&ch->lock);
  1004. /* decrement back the ref count */
  1005. dma_buf_put(dma_buf);
  1006. spcom_pr_err("no free entry to store ion handle of fd [%d]\n", fd);
  1007. return -EFAULT;
  1008. }
  1009. /**
  1010. * spcom_dmabuf_unlock() - unattach and free dmabuf
  1011. *
  1012. * unattach the dmabuf from spcom driver.
  1013. * decrememt dmabuf ref count.
  1014. */
  1015. static int spcom_dmabuf_unlock(struct dma_buf_info *info, bool verify_buf_owner)
  1016. {
  1017. u32 pid = current_pid();
  1018. if (info == NULL) {
  1019. spcom_pr_err("Invalid dmabuf info pointer\n");
  1020. return -EINVAL;
  1021. }
  1022. if (info->handle == NULL) {
  1023. spcom_pr_err("DMA buffer handle is NULL\n");
  1024. return -EINVAL;
  1025. }
  1026. if (verify_buf_owner) {
  1027. if (pid == 0) {
  1028. spcom_pr_err("Unknown PID\n");
  1029. return -EINVAL;
  1030. }
  1031. if (info->owner_pid != pid) {
  1032. spcom_pr_err("PID [%u] is not the owner of this DMA buffer\n", pid);
  1033. return -EPERM;
  1034. }
  1035. }
  1036. spcom_pr_dbg("unlock dmbuf fd [%d], PID [%u]\n", info->fd, pid);
  1037. if (info->attach) {
  1038. dma_buf_unmap_attachment(info->attach, info->sg, DMA_BIDIRECTIONAL);
  1039. dma_buf_detach(info->handle, info->attach);
  1040. info->attach = NULL;
  1041. info->sg = NULL;
  1042. }
  1043. dma_buf_put(info->handle);
  1044. info->handle = NULL;
  1045. info->fd = -1;
  1046. info->owner_pid = 0;
  1047. return 0;
  1048. }
  1049. /**
  1050. * spcom_handle_unlock_ion_buf_command() - Unlock an ION buffer.
  1051. *
  1052. * Unlock an ION buffer, let it be free, when it is no longer being used by
  1053. * the remote subsystem.
  1054. */
  1055. static int spcom_handle_unlock_ion_buf_command(struct spcom_channel *ch,
  1056. void *cmd_buf, int size)
  1057. {
  1058. int i;
  1059. struct spcom_user_command *cmd = cmd_buf;
  1060. int fd;
  1061. bool found = false;
  1062. struct dma_buf *dma_buf;
  1063. if (size != sizeof(*cmd)) {
  1064. spcom_pr_err("cmd size [%d], expected [%d]\n",
  1065. (int)size, (int)sizeof(*cmd));
  1066. return -EINVAL;
  1067. }
  1068. if (cmd->arg > (unsigned int)INT_MAX) {
  1069. spcom_pr_err("int overflow [%u]\n", cmd->arg);
  1070. return -EINVAL;
  1071. }
  1072. fd = cmd->arg;
  1073. spcom_pr_dbg("Unlock ion buf ch [%s] fd [%d]\n", ch->name, fd);
  1074. dma_buf = dma_buf_get(fd);
  1075. if (IS_ERR_OR_NULL(dma_buf)) {
  1076. spcom_pr_err("fail to get dma buf handle\n");
  1077. return -EINVAL;
  1078. }
  1079. dma_buf_put(dma_buf);
  1080. /* shared buf unlock doesn't involve any rx/tx data to SP. */
  1081. mutex_lock(&ch->lock);
  1082. if (fd == (int) SPCOM_ION_FD_UNLOCK_ALL) {
  1083. spcom_pr_dbg("unlocked ALL ion buf ch [%s]\n", ch->name);
  1084. found = true;
  1085. /* unlock all buf */
  1086. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++)
  1087. spcom_dmabuf_unlock(&ch->dmabuf_array[i], true);
  1088. } else {
  1089. /* unlock specific buf */
  1090. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array) ; i++) {
  1091. if (!ch->dmabuf_array[i].handle)
  1092. continue;
  1093. if (ch->dmabuf_array[i].handle == dma_buf) {
  1094. spcom_dmabuf_unlock(&ch->dmabuf_array[i], true);
  1095. found = true;
  1096. break;
  1097. }
  1098. }
  1099. }
  1100. mutex_unlock(&ch->lock);
  1101. if (!found) {
  1102. spcom_pr_err("ch [%s] fd [%d] was not found\n", ch->name, fd);
  1103. return -ENODEV;
  1104. }
  1105. return 0;
  1106. }
  1107. /**
  1108. * spcom_handle_enable_ssr_command() - Handle user space request to enable ssr
  1109. *
  1110. * After FOTA SSR is disabled until IAR update occurs.
  1111. * Then - enable SSR again
  1112. *
  1113. * Return: size in bytes on success, negative value on failure.
  1114. */
  1115. static int spcom_handle_enable_ssr_command(void)
  1116. {
  1117. spcom_pr_info("TBD: SSR is enabled after FOTA\n");
  1118. return 0;
  1119. }
  1120. /**
  1121. * spcom_handle_write() - Handle user space write commands.
  1122. *
  1123. * @buf: command buffer.
  1124. * @buf_size: command buffer size.
  1125. *
  1126. * Return: 0 on successful operation, negative value otherwise.
  1127. */
  1128. static int spcom_handle_write(struct spcom_channel *ch,
  1129. void *buf,
  1130. int buf_size)
  1131. {
  1132. int ret = 0;
  1133. struct spcom_user_command *cmd = NULL;
  1134. int cmd_id = 0;
  1135. /* Minimal command should have command-id and argument */
  1136. if (buf_size < sizeof(struct spcom_user_command)) {
  1137. spcom_pr_err("Command buffer size [%d] too small\n", buf_size);
  1138. return -EINVAL;
  1139. }
  1140. cmd = (struct spcom_user_command *)buf;
  1141. cmd_id = (int) cmd->cmd_id;
  1142. spcom_pr_dbg("cmd_id [0x%x]\n", cmd_id);
  1143. if (!ch && cmd_id != SPCOM_CMD_CREATE_CHANNEL
  1144. && cmd_id != SPCOM_CMD_RESTART_SP
  1145. && cmd_id != SPCOM_CMD_ENABLE_SSR) {
  1146. spcom_pr_err("channel context is null\n");
  1147. return -EINVAL;
  1148. }
  1149. if (cmd_id == SPCOM_CMD_SEND || cmd_id == SPCOM_CMD_SEND_MODIFIED) {
  1150. if (!spcom_is_channel_connected(ch)) {
  1151. pr_err("ch [%s] remote side not connected\n", ch->name);
  1152. return -ENOTCONN;
  1153. }
  1154. }
  1155. switch (cmd_id) {
  1156. case SPCOM_CMD_SEND:
  1157. if (ch->is_sharable) {
  1158. /* Channel shared, mutex protect TxRx */
  1159. mutex_lock(&ch->shared_sync_lock);
  1160. /* pid indicates the current active ch */
  1161. ch->active_pid = current_pid();
  1162. }
  1163. ret = spcom_handle_send_command(ch, buf, buf_size);
  1164. break;
  1165. case SPCOM_CMD_SEND_MODIFIED:
  1166. if (ch->is_sharable) {
  1167. /* Channel shared, mutex protect TxRx */
  1168. mutex_lock(&ch->shared_sync_lock);
  1169. /* pid indicates the current active ch */
  1170. ch->active_pid = current_pid();
  1171. }
  1172. ret = spcom_handle_send_modified_command(ch, buf, buf_size);
  1173. break;
  1174. case SPCOM_CMD_LOCK_ION_BUF:
  1175. ret = spcom_handle_lock_ion_buf_command(ch, buf, buf_size);
  1176. break;
  1177. case SPCOM_CMD_UNLOCK_ION_BUF:
  1178. ret = spcom_handle_unlock_ion_buf_command(ch, buf, buf_size);
  1179. break;
  1180. case SPCOM_CMD_CREATE_CHANNEL:
  1181. ret = spcom_handle_create_channel_command(buf, buf_size);
  1182. break;
  1183. case SPCOM_CMD_RESTART_SP:
  1184. ret = spcom_handle_restart_sp_command(buf, buf_size);
  1185. break;
  1186. case SPCOM_CMD_ENABLE_SSR:
  1187. ret = spcom_handle_enable_ssr_command();
  1188. break;
  1189. default:
  1190. spcom_pr_err("Invalid Command Id [0x%x]\n", (int) cmd->cmd_id);
  1191. ret = -EINVAL;
  1192. }
  1193. return ret;
  1194. }
  1195. /**
  1196. * spcom_handle_get_req_size() - Handle user space get request size command
  1197. *
  1198. * @ch: channel handle
  1199. * @buf: command buffer.
  1200. * @size: command buffer size.
  1201. *
  1202. * Return: size in bytes on success, negative value on failure.
  1203. */
  1204. static int spcom_handle_get_req_size(struct spcom_channel *ch,
  1205. void *buf,
  1206. uint32_t size)
  1207. {
  1208. int ret = -1;
  1209. uint32_t next_req_size = 0;
  1210. if (size < sizeof(next_req_size)) {
  1211. spcom_pr_err("buf size [%d] too small\n", (int) size);
  1212. return -EINVAL;
  1213. }
  1214. ret = spcom_get_next_request_size(ch);
  1215. if (ret < 0)
  1216. return ret;
  1217. next_req_size = (uint32_t) ret;
  1218. memcpy(buf, &next_req_size, sizeof(next_req_size));
  1219. spcom_pr_dbg("next_req_size [%d]\n", next_req_size);
  1220. return sizeof(next_req_size); /* can't exceed user buffer size */
  1221. }
  1222. /**
  1223. * spcom_handle_read_req_resp() - Handle user space get request/response command
  1224. *
  1225. * @ch: channel handle
  1226. * @buf: command buffer.
  1227. * @size: command buffer size.
  1228. *
  1229. * Return: size in bytes on success, negative value on failure.
  1230. */
  1231. static int spcom_handle_read_req_resp(struct spcom_channel *ch,
  1232. void *buf,
  1233. uint32_t size)
  1234. {
  1235. int ret;
  1236. struct spcom_msg_hdr *hdr;
  1237. void *rx_buf;
  1238. int rx_buf_size;
  1239. uint32_t timeout_msec = 0; /* client only */
  1240. /* Check if remote side connect */
  1241. if (!spcom_is_channel_connected(ch)) {
  1242. spcom_pr_err("ch [%s] remote side not connect\n", ch->name);
  1243. return -ENOTCONN;
  1244. }
  1245. /* Check param validity */
  1246. if (size > SPCOM_MAX_RESPONSE_SIZE) {
  1247. spcom_pr_err("ch [%s] invalid size [%d]\n",
  1248. ch->name, size);
  1249. return -EINVAL;
  1250. }
  1251. /* Allocate Buffers*/
  1252. rx_buf_size = sizeof(*hdr) + size;
  1253. rx_buf = kzalloc(rx_buf_size, GFP_KERNEL);
  1254. if (!rx_buf)
  1255. return -ENOMEM;
  1256. /*
  1257. * client response timeout depends on the request
  1258. * handling time on the remote side .
  1259. */
  1260. if (!ch->is_server) {
  1261. timeout_msec = ch->response_timeout_msec;
  1262. spcom_pr_dbg("response_timeout_msec:%d\n", (int) timeout_msec);
  1263. }
  1264. ret = spcom_rx(ch, rx_buf, rx_buf_size, timeout_msec);
  1265. if (ret < 0) {
  1266. spcom_pr_err("rx error %d\n", ret);
  1267. goto exit_err;
  1268. } else {
  1269. size = ret; /* actual_rx_size */
  1270. }
  1271. hdr = rx_buf;
  1272. if (ch->is_server) {
  1273. ch->txn_id = hdr->txn_id;
  1274. spcom_pr_dbg("ch[%s]:request txn_id [0x%x]\n",
  1275. ch->name, ch->txn_id);
  1276. }
  1277. /* copy data to user without the header */
  1278. if (size > sizeof(*hdr)) {
  1279. size -= sizeof(*hdr);
  1280. memcpy(buf, hdr->buf, size);
  1281. } else {
  1282. spcom_pr_err("rx size [%d] too small\n", size);
  1283. ret = -EFAULT;
  1284. goto exit_err;
  1285. }
  1286. kfree(rx_buf);
  1287. return size;
  1288. exit_err:
  1289. kfree(rx_buf);
  1290. return ret;
  1291. }
  1292. /**
  1293. * spcom_handle_read() - Handle user space read request/response or
  1294. * request-size command
  1295. *
  1296. * @ch: channel handle
  1297. * @buf: command buffer.
  1298. * @size: command buffer size.
  1299. *
  1300. * A special size SPCOM_GET_NEXT_REQUEST_SIZE, which is bigger than the max
  1301. * response/request tells the kernel that user space only need the size.
  1302. *
  1303. * Return: size in bytes on success, negative value on failure.
  1304. */
  1305. static int spcom_handle_read(struct spcom_channel *ch,
  1306. void *buf,
  1307. uint32_t size)
  1308. {
  1309. int ret = -1;
  1310. if (size == SPCOM_GET_NEXT_REQUEST_SIZE) {
  1311. ch->is_server = true;
  1312. ret = spcom_handle_get_req_size(ch, buf, size);
  1313. } else {
  1314. ret = spcom_handle_read_req_resp(ch, buf, size);
  1315. }
  1316. mutex_lock(&ch->lock);
  1317. if (!ch->is_server) {
  1318. __pm_relax(spcom_dev->ws);
  1319. spcom_pr_dbg("ch[%s]:pm_relax() called for client\n",
  1320. ch->name);
  1321. }
  1322. mutex_unlock(&ch->lock);
  1323. return ret;
  1324. }
  1325. /*======================================================================*/
  1326. /* CHAR DEVICE USER SPACE INTERFACE */
  1327. /*======================================================================*/
  1328. /**
  1329. * file_to_filename() - get the filename from file pointer.
  1330. *
  1331. * @filp: file pointer
  1332. *
  1333. * it is used for debug prints.
  1334. *
  1335. * Return: filename string or "unknown".
  1336. */
  1337. static char *file_to_filename(struct file *filp)
  1338. {
  1339. struct dentry *dentry = NULL;
  1340. char *filename = NULL;
  1341. if (!filp || !filp->f_path.dentry)
  1342. return "unknown";
  1343. dentry = filp->f_path.dentry;
  1344. filename = dentry->d_iname;
  1345. return filename;
  1346. }
  1347. bool is_proc_channel_owner(struct spcom_channel *ch, u32 pid)
  1348. {
  1349. int i = 0;
  1350. for (i = 0; i < ch->max_clients; ++i) {
  1351. if (ch->pid[i] == pid)
  1352. return true;
  1353. }
  1354. return false;
  1355. }
  1356. /**
  1357. * spcom_device_open() - handle channel file open() from user space.
  1358. *
  1359. * @filp: file pointer
  1360. *
  1361. * The file name (without path) is the channel name.
  1362. * Register rpmsg driver matching with channel name.
  1363. * Store the channel context in the file private date pointer for future
  1364. * read/write/close operations.
  1365. */
  1366. static int spcom_device_open(struct inode *inode, struct file *filp)
  1367. {
  1368. struct spcom_channel *ch;
  1369. int ret;
  1370. const char *name = file_to_filename(filp);
  1371. u32 pid = current_pid();
  1372. int i = 0;
  1373. if (atomic_read(&spcom_dev->remove_in_progress)) {
  1374. spcom_pr_err("module remove in progress\n");
  1375. return -ENODEV;
  1376. }
  1377. if (strcmp(name, "unknown") == 0) {
  1378. spcom_pr_err("name is unknown\n");
  1379. return -EINVAL;
  1380. }
  1381. if (strcmp(name, "sp_ssr") == 0) {
  1382. spcom_pr_dbg("sp_ssr dev node skipped\n");
  1383. return 0;
  1384. }
  1385. if (pid == 0) {
  1386. spcom_pr_err("unknown PID\n");
  1387. return -EINVAL;
  1388. }
  1389. if (strcmp(name, DEVICE_NAME) == 0) {
  1390. spcom_pr_dbg("control channel is opened by pid %u\n", pid);
  1391. return spom_control_channel_add_client(pid);
  1392. }
  1393. ch = spcom_find_channel_by_name(name);
  1394. if (!ch) {
  1395. spcom_pr_err("ch[%s] doesn't exist, load app first\n", name);
  1396. return -ENODEV;
  1397. }
  1398. mutex_lock(&ch->lock);
  1399. if (!spcom_is_channel_open(ch)) {
  1400. reinit_completion(&ch->connect);
  1401. /* channel was closed need to register drv again */
  1402. ret = spcom_register_rpmsg_drv(ch);
  1403. if (ret < 0) {
  1404. spcom_pr_err("register rpmsg driver failed %d\n", ret);
  1405. mutex_unlock(&ch->lock);
  1406. return ret;
  1407. }
  1408. }
  1409. /* max number of channel clients reached */
  1410. if (ch->is_busy) {
  1411. spcom_pr_err("channel [%s] is BUSY and has %d of clients, already in use\n",
  1412. name, ch->num_clients);
  1413. mutex_unlock(&ch->lock);
  1414. return -EBUSY;
  1415. }
  1416. /*
  1417. * if same client trying to register again, this will fail
  1418. */
  1419. for (i = 0; i < SPCOM_MAX_CHANNEL_CLIENTS; i++) {
  1420. if (ch->pid[i] == pid) {
  1421. spcom_pr_err("client with pid [%d] is already registered with channel[%s]\n",
  1422. pid, name);
  1423. mutex_unlock(&ch->lock);
  1424. return -EINVAL;
  1425. }
  1426. }
  1427. if (ch->is_sharable) {
  1428. ch->num_clients++;
  1429. if (ch->num_clients >= SPCOM_MAX_CHANNEL_CLIENTS)
  1430. ch->is_busy = true;
  1431. else
  1432. ch->is_busy = false;
  1433. /* pid array has pid of all the registered client.
  1434. * If we reach here, the is_busy flag check above guarantees
  1435. * that we have at least one non-zero pid index
  1436. */
  1437. for (i = 0; i < SPCOM_MAX_CHANNEL_CLIENTS; i++) {
  1438. if (ch->pid[i] == 0) {
  1439. ch->pid[i] = pid;
  1440. break;
  1441. }
  1442. }
  1443. } else {
  1444. ch->num_clients = 1;
  1445. ch->is_busy = true;
  1446. /* Only first index of pid is relevant in case of
  1447. * non-shareable
  1448. */
  1449. ch->pid[0] = pid;
  1450. }
  1451. mutex_unlock(&ch->lock);
  1452. filp->private_data = ch;
  1453. return 0;
  1454. }
  1455. /**
  1456. * spcom_device_release() - handle channel file close() from user space.
  1457. *
  1458. * @filp: file pointer
  1459. *
  1460. * The file name (without path) is the channel name.
  1461. * Open the relevant glink channel.
  1462. * Store the channel context in the file private
  1463. * date pointer for future read/write/close
  1464. * operations.
  1465. */
  1466. static int spcom_device_release(struct inode *inode, struct file *filp)
  1467. {
  1468. struct spcom_channel *ch;
  1469. const char *name = file_to_filename(filp);
  1470. int ret = 0;
  1471. int i = 0;
  1472. u32 pid = current_pid();
  1473. if (strcmp(name, "unknown") == 0) {
  1474. spcom_pr_err("name is unknown\n");
  1475. return -EINVAL;
  1476. }
  1477. if (strcmp(name, "sp_ssr") == 0) {
  1478. spcom_pr_dbg("sp_ssr dev node skipped\n");
  1479. return 0;
  1480. }
  1481. if (pid == 0) {
  1482. spcom_pr_err("unknown PID\n");
  1483. return -EINVAL;
  1484. }
  1485. if (strcmp(name, DEVICE_NAME) == 0) {
  1486. spcom_pr_dbg("PID [%d] release control channel\n", pid);
  1487. return spom_control_channel_remove_client(pid);
  1488. }
  1489. ch = filp->private_data;
  1490. if (!ch) {
  1491. spcom_pr_dbg("ch is NULL, file name %s\n",
  1492. file_to_filename(filp));
  1493. return -ENODEV;
  1494. }
  1495. mutex_lock(&ch->lock);
  1496. /* channel might be already closed or disconnected */
  1497. if (!spcom_is_channel_open(ch)) {
  1498. spcom_pr_dbg("ch [%s] already closed\n", name);
  1499. mutex_unlock(&ch->lock);
  1500. return 0;
  1501. }
  1502. for (i = 0; i < SPCOM_MAX_CHANNEL_CLIENTS; i++) {
  1503. if (ch->pid[i] == pid) {
  1504. spcom_pr_dbg("PID [%x] is releasing ch [%s]\n", pid, name);
  1505. ch->pid[i] = 0;
  1506. break;
  1507. }
  1508. }
  1509. if (ch->num_clients > 1) {
  1510. /*
  1511. * Shared client is trying to close channel,
  1512. * release the sync_lock if applicable
  1513. */
  1514. if (ch->active_pid == pid) {
  1515. spcom_pr_dbg("active_pid [%x] is releasing ch [%s] sync lock\n",
  1516. ch->active_pid, name);
  1517. /* No longer the current active user of the channel */
  1518. ch->active_pid = 0;
  1519. mutex_unlock(&ch->shared_sync_lock);
  1520. }
  1521. ch->num_clients--;
  1522. ch->is_busy = false;
  1523. mutex_unlock(&ch->lock);
  1524. return 0;
  1525. }
  1526. ch->is_busy = false;
  1527. ch->num_clients = 0;
  1528. ch->active_pid = 0;
  1529. if (ch->rpmsg_rx_buf) {
  1530. spcom_pr_dbg("ch [%s] discarding unconsumed rx packet actual_rx_size=%zd\n",
  1531. name, ch->actual_rx_size);
  1532. kfree(ch->rpmsg_rx_buf);
  1533. ch->rpmsg_rx_buf = NULL;
  1534. }
  1535. ch->actual_rx_size = 0;
  1536. mutex_unlock(&ch->lock);
  1537. filp->private_data = NULL;
  1538. return ret;
  1539. }
  1540. /**
  1541. * spcom_device_write() - handle channel file write() from user space.
  1542. *
  1543. * @filp: file pointer
  1544. *
  1545. * Return: On Success - same size as number of bytes to write.
  1546. * On Failure - negative value.
  1547. */
  1548. static ssize_t spcom_device_write(struct file *filp,
  1549. const char __user *user_buff,
  1550. size_t size, loff_t *f_pos)
  1551. {
  1552. int ret;
  1553. char *buf;
  1554. struct spcom_channel *ch;
  1555. const char *name = file_to_filename(filp);
  1556. int buf_size = 0;
  1557. if (!user_buff || !f_pos || !filp) {
  1558. spcom_pr_err("invalid null parameters\n");
  1559. return -EINVAL;
  1560. }
  1561. if (atomic_read(&spcom_dev->remove_in_progress)) {
  1562. spcom_pr_err("module remove in progress\n");
  1563. return -ENODEV;
  1564. }
  1565. if (*f_pos != 0) {
  1566. spcom_pr_err("offset should be zero, no sparse buffer\n");
  1567. return -EINVAL;
  1568. }
  1569. if (!name) {
  1570. spcom_pr_err("name is NULL\n");
  1571. return -EINVAL;
  1572. }
  1573. if (strcmp(name, "unknown") == 0) {
  1574. spcom_pr_err("name is unknown\n");
  1575. return -EINVAL;
  1576. }
  1577. if (size > SPCOM_MAX_COMMAND_SIZE) {
  1578. spcom_pr_err("size [%d] > max size [%d]\n",
  1579. (int) size, (int) SPCOM_MAX_COMMAND_SIZE);
  1580. return -EINVAL;
  1581. }
  1582. ch = filp->private_data;
  1583. if (!ch) {
  1584. if (strcmp(name, DEVICE_NAME) != 0) {
  1585. spcom_pr_err("NULL ch, command not allowed\n");
  1586. return -EINVAL;
  1587. }
  1588. }
  1589. buf_size = size; /* explicit casting size_t to int */
  1590. buf = kzalloc(size, GFP_KERNEL);
  1591. if (buf == NULL)
  1592. return -ENOMEM;
  1593. ret = copy_from_user(buf, user_buff, size);
  1594. if (ret) {
  1595. spcom_pr_err("Unable to copy from user (err %d)\n", ret);
  1596. kfree(buf);
  1597. return -EFAULT;
  1598. }
  1599. ret = spcom_handle_write(ch, buf, buf_size);
  1600. if (ret) {
  1601. spcom_pr_err("handle command error [%d]\n", ret);
  1602. kfree(buf);
  1603. if (ch && ch->active_pid == current_pid()) {
  1604. ch->active_pid = 0;
  1605. mutex_unlock(&ch->shared_sync_lock);
  1606. }
  1607. return ret;
  1608. }
  1609. kfree(buf);
  1610. return size;
  1611. }
  1612. /**
  1613. * spcom_device_read() - handle channel file read() from user space.
  1614. *
  1615. * @filp: file pointer
  1616. *
  1617. * Return: number of bytes to read on success, negative value on
  1618. * failure.
  1619. */
  1620. static ssize_t spcom_device_read(struct file *filp, char __user *user_buff,
  1621. size_t size, loff_t *f_pos)
  1622. {
  1623. int ret = 0;
  1624. int actual_size = 0;
  1625. char *buf;
  1626. struct spcom_channel *ch;
  1627. const char *name = file_to_filename(filp);
  1628. uint32_t buf_size = 0;
  1629. u32 cur_pid = current_pid();
  1630. spcom_pr_dbg("read file [%s], size = %d bytes\n", name, (int) size);
  1631. if (atomic_read(&spcom_dev->remove_in_progress)) {
  1632. spcom_pr_err("module remove in progress\n");
  1633. return -ENODEV;
  1634. }
  1635. if (strcmp(name, "unknown") == 0) {
  1636. spcom_pr_err("name is unknown\n");
  1637. return -EINVAL;
  1638. }
  1639. if (!user_buff || !f_pos ||
  1640. (size == 0) || (size > SPCOM_MAX_READ_SIZE)) {
  1641. spcom_pr_err("invalid parameters\n");
  1642. return -EINVAL;
  1643. }
  1644. buf_size = size; /* explicit casting size_t to uint32_t */
  1645. ch = filp->private_data;
  1646. if (ch == NULL) {
  1647. spcom_pr_err("invalid ch pointer, file [%s]\n", name);
  1648. return -EINVAL;
  1649. }
  1650. if (!spcom_is_channel_open(ch)) {
  1651. spcom_pr_err("ch is not open, file [%s]\n", name);
  1652. return -EINVAL;
  1653. }
  1654. buf = kzalloc(size, GFP_KERNEL);
  1655. if (buf == NULL) {
  1656. ret = -ENOMEM;
  1657. goto exit_err;
  1658. }
  1659. ret = spcom_handle_read(ch, buf, buf_size);
  1660. if (ret < 0) {
  1661. if (ret != -ERESTARTSYS)
  1662. spcom_pr_err("read error [%d]\n", ret);
  1663. goto exit_err;
  1664. }
  1665. actual_size = ret;
  1666. if ((actual_size == 0) || (actual_size > size)) {
  1667. spcom_pr_err("invalid actual_size [%d]\n", actual_size);
  1668. ret = -EFAULT;
  1669. goto exit_err;
  1670. }
  1671. ret = copy_to_user(user_buff, buf, actual_size);
  1672. if (ret) {
  1673. spcom_pr_err("Unable to copy to user, err = %d\n", ret);
  1674. ret = -EFAULT;
  1675. goto exit_err;
  1676. }
  1677. kfree(buf);
  1678. if (ch->active_pid == cur_pid) {
  1679. ch->active_pid = 0;
  1680. mutex_unlock(&ch->shared_sync_lock);
  1681. }
  1682. return actual_size;
  1683. exit_err:
  1684. kfree(buf);
  1685. if (ch->active_pid == cur_pid) {
  1686. ch->active_pid = 0;
  1687. mutex_unlock(&ch->shared_sync_lock);
  1688. }
  1689. return ret;
  1690. }
  1691. static inline int handle_poll(struct file *file,
  1692. struct spcom_poll_param *op, int *user_retval)
  1693. {
  1694. struct spcom_channel *ch = NULL;
  1695. const char *name = file_to_filename(file);
  1696. int ready = 0;
  1697. int ret = 0;
  1698. switch (op->cmd_id) {
  1699. case SPCOM_LINK_STATE_REQ:
  1700. if (op->wait) {
  1701. reinit_completion(&spcom_dev->rpmsg_state_change);
  1702. ready = wait_for_completion_interruptible(
  1703. &spcom_dev->rpmsg_state_change);
  1704. spcom_pr_dbg("ch [%s] link state change signaled\n",
  1705. name);
  1706. }
  1707. op->retval = atomic_read(&spcom_dev->rpmsg_dev_count) > 0;
  1708. break;
  1709. case SPCOM_CH_CONN_STATE_REQ:
  1710. if (strcmp(name, DEVICE_NAME) == 0) {
  1711. spcom_pr_err("invalid control device: %s\n", name);
  1712. return -EINVAL;
  1713. }
  1714. /*
  1715. * ch is not expected to be NULL since user must call open()
  1716. * to get FD before it can call poll().
  1717. * open() will fail if no ch related to the char-device.
  1718. */
  1719. ch = file->private_data;
  1720. if (!ch) {
  1721. spcom_pr_err("invalid ch pointer, file [%s]\n", name);
  1722. ret = -EINVAL;
  1723. break;
  1724. }
  1725. if (op->wait) {
  1726. reinit_completion(&ch->connect);
  1727. ready = wait_for_completion_interruptible(&ch->connect);
  1728. spcom_pr_dbg("ch [%s] connect signaled\n", name);
  1729. }
  1730. mutex_lock(&ch->lock);
  1731. op->retval = (ch->rpdev != NULL);
  1732. mutex_unlock(&ch->lock);
  1733. break;
  1734. default:
  1735. spcom_pr_err("ch [%s] unsupported ioctl:%u\n",
  1736. name, op->cmd_id);
  1737. ret = -EINVAL;
  1738. }
  1739. if (ready < 0) { /* wait was interrupted */
  1740. spcom_pr_info("interrupted wait retval=%d\n", op->retval);
  1741. ret = -EINTR;
  1742. }
  1743. if (!ret) {
  1744. ret = put_user(op->retval, user_retval);
  1745. if (ret) {
  1746. spcom_pr_err("Unable to copy link state to user [%d]\n", ret);
  1747. ret = -EFAULT;
  1748. }
  1749. }
  1750. return ret;
  1751. }
  1752. /*======================================================================*/
  1753. /* IOCTL USER SPACE COMMANDS HANDLING */
  1754. /*======================================================================*/
  1755. /**
  1756. * spcom_register_channel
  1757. *
  1758. * @brief Helper function to register SPCOM channel
  1759. *
  1760. * @param[in] ch SPCOM channel
  1761. *
  1762. * @return zero on success, negative value otherwise.
  1763. */
  1764. static int spcom_register_channel(struct spcom_channel *ch)
  1765. {
  1766. const char *ch_name = NULL;
  1767. u32 pid = current_pid();
  1768. u32 i = 0;
  1769. ch_name = ch->name;
  1770. mutex_lock(&ch->lock);
  1771. spcom_pr_dbg("the pid name [%s] of pid [%d] try to open [%s] channel\n",
  1772. current->comm, pid, ch_name);
  1773. if (!spcom_is_channel_open(ch))
  1774. spcom_pr_err("channel [%s] is not open\n", ch_name);
  1775. /* max number of channel clients reached */
  1776. if (ch->is_busy) {
  1777. spcom_pr_err("channel [%s] is occupied by max num of clients [%d]\n",
  1778. ch_name, ch->num_clients);
  1779. mutex_unlock(&ch->lock);
  1780. return -EBUSY;
  1781. }
  1782. /* check if same client trying to register again */
  1783. for (i = 0; i < ch->max_clients; ++i) {
  1784. if (ch->pid[i] == pid) {
  1785. spcom_pr_err("client with pid[%d] is already registered with channel[%s]\n",
  1786. pid, ch_name);
  1787. mutex_unlock(&ch->lock);
  1788. return -EINVAL;
  1789. }
  1790. }
  1791. if (ch->is_sharable) {
  1792. /* set or add channel owner PID */
  1793. for (i = 0; i < ch->max_clients; ++i) {
  1794. if (ch->pid[i] == 0)
  1795. break;
  1796. }
  1797. } else {
  1798. i = 0;
  1799. }
  1800. /* update channel client, whether the channel is shared or not */
  1801. ch->pid[i] = pid;
  1802. ch->num_clients++;
  1803. ch->is_busy = (ch->num_clients == ch->max_clients) ? true : false;
  1804. mutex_unlock(&ch->lock);
  1805. return 0;
  1806. }
  1807. /**
  1808. * is_valid_ch_name
  1809. *
  1810. * @brief Helper function to verify channel name pointer
  1811. *
  1812. * @param[in] ch_name channel name
  1813. *
  1814. * @return true if valid channel name pointer, false otherwise.
  1815. */
  1816. static inline bool is_valid_ch_name(const char *ch_name)
  1817. {
  1818. static const uint32_t maxlen = SPCOM_CHANNEL_NAME_SIZE;
  1819. return (ch_name && ch_name[0] && (strnlen(ch_name, maxlen) < maxlen));
  1820. }
  1821. /**
  1822. * is_control_channel_name
  1823. *
  1824. * @brief Helper function to check if channel name is the control channel name
  1825. *
  1826. * @param[in] ch_name channel name
  1827. *
  1828. * @return true if control channel name, false otherwise.
  1829. */
  1830. static inline bool is_control_channel_name(const char *ch_name)
  1831. {
  1832. return (is_valid_ch_name(ch_name) && (!strcmp(ch_name, DEVICE_NAME)));
  1833. }
  1834. /**
  1835. * spcom_channel_deinit_locked
  1836. *
  1837. * @brief Helper function to handle deinit of SPCOM channel while holding the channel's lock
  1838. *
  1839. * @param[in] ch SPCOM channel
  1840. *
  1841. * @return zero on successful operation, negative value otherwise.
  1842. */
  1843. static int spcom_channel_deinit_locked(struct spcom_channel *ch, u32 pid)
  1844. {
  1845. const char *ch_name = ch->name;
  1846. bool found = false;
  1847. u32 i = 0;
  1848. /* channel might be already closed or disconnected */
  1849. if (!spcom_is_channel_open(ch)) {
  1850. spcom_pr_dbg("ch [%s] already closed\n", ch_name);
  1851. return 0;
  1852. }
  1853. /* check that current process is a client of this channel */
  1854. for (i = 0; i < ch->max_clients; ++i) {
  1855. if (ch->pid[i] == pid) {
  1856. found = true;
  1857. spcom_pr_dbg("pid [%x] is releasing ch [%s]\n", pid, ch_name);
  1858. ch->pid[i] = 0;
  1859. break;
  1860. }
  1861. }
  1862. /* if the current process is not a valid client of this channel, return an error */
  1863. if (!found) {
  1864. spcom_pr_dbg("pid [%d] is not a client of ch [%s]\n", pid, ch_name);
  1865. return -EFAULT;
  1866. }
  1867. /* If shared client owner is trying to close channel, release the sync_lock if
  1868. * applicable
  1869. */
  1870. if (ch->active_pid == pid) {
  1871. spcom_pr_dbg("active_pid [%d] is releasing ch [%s] sync lock\n",
  1872. ch->active_pid, ch_name);
  1873. ch->active_pid = 0;
  1874. mutex_unlock(&ch->shared_sync_lock);
  1875. }
  1876. ch->num_clients--;
  1877. ch->is_busy = false;
  1878. if (ch->rpmsg_rx_buf) {
  1879. spcom_pr_dbg("ch [%s] discarding unconsumed rx packet actual_rx_size=%zd\n",
  1880. ch_name, ch->actual_rx_size);
  1881. kfree(ch->rpmsg_rx_buf);
  1882. ch->rpmsg_rx_buf = NULL;
  1883. }
  1884. ch->actual_rx_size = 0;
  1885. return 0;
  1886. }
  1887. /**
  1888. * spcom_channel_deinit
  1889. *
  1890. * @brief Helper function to handle deinit of SPCOM channel
  1891. *
  1892. * @param[in] ch SPCOM channel
  1893. *
  1894. * @return zero on successful operation, negative value otherwise.
  1895. */
  1896. static int spcom_channel_deinit(struct spcom_channel *ch)
  1897. {
  1898. uint32_t pid = current_pid();
  1899. int ret;
  1900. if (!pid) {
  1901. spcom_pr_err("unknown PID\n");
  1902. return -EINVAL;
  1903. }
  1904. mutex_lock(&ch->lock);
  1905. ret = spcom_channel_deinit_locked(ch, pid);
  1906. mutex_unlock(&ch->lock);
  1907. return ret;
  1908. }
  1909. /**
  1910. * spcom_send_message
  1911. *
  1912. * @brief Helper function to send request/response IOCTL command from user space
  1913. *
  1914. * @param[in] arg IOCTL command arguments
  1915. * @param[in] buffer user message buffer
  1916. * @param[in] is_modified flag to indicate if this is a modified message or regular message
  1917. *
  1918. * Return: 0 on successful operation, negative value otherwise.
  1919. */
  1920. static int spcom_send_message(void *arg, void *buffer, bool is_modified)
  1921. {
  1922. struct spcom_channel *ch = NULL;
  1923. struct spcom_msg_hdr *hdr = NULL;
  1924. struct spcom_ioctl_message *usr_msg = NULL;
  1925. struct spcom_ioctl_modified_message *usr_mod_msg = NULL;
  1926. const char *ch_name = NULL;
  1927. void *msg_buf = NULL;
  1928. void *tx_buf = NULL;
  1929. int tx_buf_size = 0;
  1930. uint32_t msg_buf_sz = 0;
  1931. uint32_t dma_info_array_sz = 0;
  1932. int ret = 0;
  1933. int time_msec = 0;
  1934. int timeout_msec = 0;
  1935. int i = 0;
  1936. /* Parse message command arguments */
  1937. if (is_modified) {
  1938. /* Send regular message */
  1939. usr_mod_msg = arg;
  1940. msg_buf = buffer;
  1941. msg_buf_sz = usr_mod_msg->buffer_size;
  1942. timeout_msec = usr_mod_msg->timeout_msec;
  1943. ch_name = usr_mod_msg->ch_name;
  1944. } else {
  1945. /* Send modified message */
  1946. usr_msg = arg;
  1947. msg_buf = buffer;
  1948. msg_buf_sz = usr_msg->buffer_size;
  1949. timeout_msec = usr_msg->timeout_msec;
  1950. ch_name = usr_msg->ch_name;
  1951. }
  1952. /* Verify channel name */
  1953. if (!is_valid_ch_name(ch_name)) {
  1954. spcom_pr_err("invalid channel name\n");
  1955. return -EINVAL;
  1956. }
  1957. /* Verify message buffer size */
  1958. if (msg_buf_sz > SPCOM_MAX_RESPONSE_SIZE) {
  1959. spcom_pr_err("ch [%s] message size is too big [%d]\n", ch_name, msg_buf_sz);
  1960. return -EINVAL;
  1961. }
  1962. /* DEVICE_NAME is reserved for control channel */
  1963. if (is_control_channel_name(ch_name)) {
  1964. spcom_pr_err("cannot send message on control channel\n");
  1965. return -EFAULT;
  1966. }
  1967. /* Find spcom channel in spcom channel list by name */
  1968. ch = spcom_find_channel_by_name(ch_name);
  1969. if (!ch)
  1970. return -ENODEV;
  1971. /* Check if remote side connect */
  1972. if (!spcom_is_channel_connected(ch)) {
  1973. spcom_pr_err("ch [%s] remote side not connected\n", ch_name);
  1974. return -ENOTCONN;
  1975. }
  1976. spcom_pr_dbg("sending message with size [%d], ch [%s]\n", msg_buf_sz, ch_name);
  1977. /* Allocate and prepare Tx buffer */
  1978. tx_buf_size = sizeof(*hdr) + msg_buf_sz;
  1979. tx_buf = kzalloc(tx_buf_size, GFP_KERNEL);
  1980. if (!tx_buf)
  1981. return -ENOMEM;
  1982. hdr = tx_buf;
  1983. mutex_lock(&ch->lock);
  1984. /* For SPCOM server, get next request size must be called before sending a response
  1985. * if we got here and the role is not set it means the channel is SPCOM client
  1986. */
  1987. if (ch->comm_role_undefined) {
  1988. spcom_pr_dbg("client ch [%s] sending it's first message\n", ch_name);
  1989. ch->comm_role_undefined = false;
  1990. ch->is_server = false;
  1991. }
  1992. /* Protect shared channel Tx by lock and set the current process as the owner */
  1993. if (ch->is_sharable) {
  1994. if (ch->is_server) {
  1995. spcom_pr_err("server spcom channel cannot be shared\n");
  1996. goto send_message_err;
  1997. }
  1998. mutex_lock(&ch->shared_sync_lock);
  1999. ch->active_pid = current_pid();
  2000. }
  2001. /* SPCom client sets the request txn_id */
  2002. if (!ch->is_server) {
  2003. ch->txn_id++;
  2004. ch->response_timeout_msec = timeout_msec;
  2005. }
  2006. hdr->txn_id = ch->txn_id;
  2007. /* Copy user buffer to tx */
  2008. memcpy(hdr->buf, msg_buf, msg_buf_sz);
  2009. /* For modified message write the DMA buffer addresses to the user defined offset in the
  2010. * message buffer
  2011. */
  2012. if (is_modified) {
  2013. dma_info_array_sz = ARRAY_SIZE(usr_mod_msg->info);
  2014. if (dma_info_array_sz != SPCOM_MAX_DMA_BUF) {
  2015. spcom_pr_err("invalid info array size [%d], ch[%s]\n", dma_info_array_sz,
  2016. ch_name);
  2017. ret = -EINVAL;
  2018. goto send_message_err;
  2019. }
  2020. for (i = 0; i < dma_info_array_sz; ++i) {
  2021. if (usr_mod_msg->info[i].fd >= 0) {
  2022. ret = modify_dma_buf_addr(ch, hdr->buf, msg_buf_sz,
  2023. &usr_mod_msg->info[i]);
  2024. if (ret) {
  2025. ret = -EFAULT;
  2026. goto send_message_err;
  2027. }
  2028. }
  2029. }
  2030. }
  2031. /* Send Tx to remote edge */
  2032. do {
  2033. if (ch->rpmsg_abort) {
  2034. spcom_pr_err("ch [%s] aborted\n", ch_name);
  2035. ret = -ECANCELED;
  2036. break;
  2037. }
  2038. /* may fail when Rx intent not queued by remote edge */
  2039. ret = rpmsg_trysend(ch->rpdev->ept, tx_buf, tx_buf_size);
  2040. if (ret == 0) {
  2041. spcom_pr_dbg("ch[%s]: successfully sent txn_id=%d\n", ch_name, ch->txn_id);
  2042. break;
  2043. }
  2044. time_msec += TX_RETRY_DELAY_MSEC;
  2045. /* release channel lock before sleep */
  2046. mutex_unlock(&ch->lock);
  2047. msleep(TX_RETRY_DELAY_MSEC);
  2048. mutex_lock(&ch->lock);
  2049. } while ((ret == -EBUSY || ret == -EAGAIN) && time_msec < timeout_msec);
  2050. if (ret)
  2051. spcom_pr_err("Tx failed: ch [%s], err [%d], timeout [%d]ms\n",
  2052. ch_name, ret, timeout_msec);
  2053. ret = msg_buf_sz;
  2054. send_message_err:
  2055. if (ret < 0 && ch->is_sharable && ch->active_pid == current_pid()) {
  2056. ch->active_pid = 0;
  2057. mutex_unlock(&ch->shared_sync_lock);
  2058. }
  2059. /* close pm awake window after spcom server response */
  2060. if (ch->is_server) {
  2061. __pm_relax(spcom_dev->ws);
  2062. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  2063. ch->name);
  2064. }
  2065. mutex_unlock(&ch->lock);
  2066. memset(tx_buf, 0, tx_buf_size);
  2067. kfree(tx_buf);
  2068. return ret;
  2069. }
  2070. /**
  2071. * is_control_channel
  2072. *
  2073. * @brief Helper function to check if device file if of a control channel
  2074. *
  2075. * @param[in] file device file
  2076. *
  2077. * @return true if file is control device file, false otherwise.
  2078. */
  2079. static inline bool is_control_channel(struct file *file)
  2080. {
  2081. return (!strcmp(file_to_filename(file), DEVICE_NAME)) ? true : false;
  2082. }
  2083. /**
  2084. * spcom_ioctl_handle_restart_spu_command
  2085. *
  2086. * @brief Handle SPU restart IOCTL command from user space
  2087. *
  2088. * @return zero on success, negative value otherwise.
  2089. */
  2090. static int spcom_ioctl_handle_restart_spu_command(void)
  2091. {
  2092. int ret = 0;
  2093. spcom_pr_dbg("SPSS restart command\n");
  2094. spcom_dev->spss_rproc = rproc_get_by_phandle(be32_to_cpup(spcom_dev->rproc_prop->value));
  2095. if (!spcom_dev->spss_rproc) {
  2096. pr_err("rproc device not found\n");
  2097. return -ENODEV; /* no spss peripheral exist */
  2098. }
  2099. ret = rproc_boot(spcom_dev->spss_rproc);
  2100. if (ret == -ETIMEDOUT) {
  2101. /* userspace should handle retry if needed */
  2102. spcom_pr_err("FW loading process timeout\n");
  2103. } else if (ret) {
  2104. /*
  2105. * SPU shutdown. Return value comes from SPU PBL message.
  2106. * The error is not recoverable and userspace handles it
  2107. * by request and analyse rmb_error value
  2108. */
  2109. spcom_dev->rmb_error = (uint32_t)ret;
  2110. spcom_pr_err("spss crashed during device bootup rmb_error[0x%x]\n",
  2111. spcom_dev->rmb_error);
  2112. ret = -ENODEV;
  2113. } else {
  2114. spcom_pr_info("FW loading process is complete\n");
  2115. }
  2116. return ret;
  2117. }
  2118. /**
  2119. * spcom_create_channel
  2120. *
  2121. * @brief Helper function to create spcom channel
  2122. *
  2123. * @param[in] ch_name spcom channel name
  2124. * @param[in] is_sharable true if sharable channel, false otherwise
  2125. *
  2126. * @return zero on success, negative value otherwise.
  2127. */
  2128. static int spcom_create_channel(const char *ch_name, bool is_sharable)
  2129. {
  2130. struct spcom_channel *ch = NULL;
  2131. struct spcom_channel *free_ch = NULL;
  2132. int ret = 0;
  2133. int i = 0;
  2134. /* check if spcom remove was called */
  2135. if (atomic_read(&spcom_dev->remove_in_progress)) {
  2136. spcom_pr_err("module remove in progress\n");
  2137. ret = -ENODEV;
  2138. }
  2139. if (!is_valid_ch_name(ch_name)) {
  2140. spcom_pr_err("invalid channel name\n");
  2141. return -EINVAL;
  2142. }
  2143. if (is_control_channel_name(ch_name)) {
  2144. spcom_pr_err("cannot create control channel\n");
  2145. return -EINVAL;
  2146. }
  2147. spcom_pr_dbg("create spcom channel, name[%s], is sharable[%d]\n", ch_name, is_sharable);
  2148. for (i = 0; i < SPCOM_MAX_CHANNELS; ++i) {
  2149. /* Check if channel already exist */
  2150. ch = &spcom_dev->channels[i];
  2151. if (!strcmp(ch->name, ch_name))
  2152. break;
  2153. /* Keep address of first free channel */
  2154. if (!free_ch && ch->name[0] == 0)
  2155. free_ch = ch;
  2156. }
  2157. /* Channel doesn't exist */
  2158. if (i == SPCOM_MAX_CHANNELS) {
  2159. /* No free slot to create a new channel */
  2160. if (!free_ch) {
  2161. spcom_pr_err("no free channel\n");
  2162. return -ENODEV;
  2163. }
  2164. /* Create a new channel */
  2165. ret = spcom_init_channel(free_ch, is_sharable, ch_name);
  2166. if (ret)
  2167. ret = -ENODEV;
  2168. } else if (is_sharable) {
  2169. /* Channel is already created as sharable */
  2170. if (spcom_dev->channels[i].is_sharable) {
  2171. spcom_pr_err("already created channel as sharable\n");
  2172. return -EALREADY;
  2173. }
  2174. /* Cannot create sharable channel if channel already created */
  2175. spcom_pr_err("channel already exist, cannot create sharable channel\n");
  2176. ret = -EINVAL;
  2177. }
  2178. if (ret)
  2179. spcom_pr_err("create channel [%s] failed, ret[%d]\n", ch_name, ret);
  2180. else
  2181. spcom_pr_dbg("create channel [%s] is done\n", ch_name);
  2182. return ret;
  2183. }
  2184. /**
  2185. * spcom_ioctl_handle_create_shared_ch_command
  2186. *
  2187. * @brief Handle SPCOM create shared channel IOCTL command from user space
  2188. *
  2189. * @param[in] arg SPCOM create shared channel IOCTL command arguments
  2190. *
  2191. * @return zero on success, negative value otherwise.
  2192. */
  2193. static inline int spcom_ioctl_handle_create_shared_ch_command(struct spcom_ioctl_ch *arg)
  2194. {
  2195. int ret = 0;
  2196. /* Lock before modifying spcom device global channel list */
  2197. mutex_lock(&spcom_dev->ch_list_lock);
  2198. ret = spcom_create_channel(arg->ch_name, true /*sharable*/);
  2199. /* Unlock spcom device global channel list */
  2200. mutex_unlock(&spcom_dev->ch_list_lock);
  2201. return ret;
  2202. }
  2203. /**
  2204. * spcom_handle_channel_register_command
  2205. *
  2206. * @brief Handle register to SPCOM channel IOCTL command from user space
  2207. *
  2208. * Handle both create SPCOM channel (if needed) and register SPCOM channel to avoid
  2209. * race condition between two processes trying to register to the same channel. The
  2210. * channel list is protected by a single lock during the create and register flow.
  2211. *
  2212. * @param[in] arg IOCTL command arguments
  2213. *
  2214. * @return zero on success, negative value otherwise.
  2215. */
  2216. static int spcom_ioctl_handle_channel_register_command(struct spcom_ioctl_ch *arg)
  2217. {
  2218. struct spcom_channel *ch = NULL;
  2219. const char *ch_name = arg->ch_name;
  2220. int ret = 0;
  2221. if (!current_pid()) {
  2222. spcom_pr_err("unknown PID\n");
  2223. return -EINVAL;
  2224. }
  2225. /* Lock before modifying spcom device global channel list */
  2226. mutex_lock(&spcom_dev->ch_list_lock);
  2227. ret = spcom_create_channel(ch_name, false /*non-sharable*/);
  2228. if (ret) {
  2229. mutex_unlock(&spcom_dev->ch_list_lock);
  2230. return ret;
  2231. }
  2232. ch = spcom_find_channel_by_name(ch_name);
  2233. if (!ch) {
  2234. mutex_unlock(&spcom_dev->ch_list_lock);
  2235. return -ENODEV;
  2236. }
  2237. /* If channel open is called for the first time need to register rpmsg_drv
  2238. * Please note: spcom_register_rpmsg_drv acquire the channel lock
  2239. */
  2240. if (!spcom_is_channel_open(ch)) {
  2241. reinit_completion(&ch->connect);
  2242. ret = spcom_register_rpmsg_drv(ch);
  2243. if (ret < 0) {
  2244. mutex_unlock(&spcom_dev->ch_list_lock);
  2245. spcom_pr_err("register rpmsg driver failed %d\n", ret);
  2246. return ret;
  2247. }
  2248. }
  2249. ret = spcom_register_channel(ch);
  2250. /* Unlock spcom device global channel list */
  2251. mutex_unlock(&spcom_dev->ch_list_lock);
  2252. return ret;
  2253. }
  2254. /**
  2255. * spcom_ioctl_handle_channel_unregister_commnad
  2256. *
  2257. * @brief Handle SPCOM channel unregister IOCTL command from user space
  2258. *
  2259. * @param[in] arg IOCTL command arguments
  2260. *
  2261. * @return zero on successful operation, negative value otherwise.
  2262. */
  2263. static int spcom_ioctl_handle_channel_unregister_command(struct spcom_ioctl_ch *arg)
  2264. {
  2265. struct spcom_channel *ch = NULL;
  2266. const char *ch_name = NULL;
  2267. int ret = 0;
  2268. if (!current_pid()) {
  2269. spcom_pr_err("unknown PID\n");
  2270. return -EINVAL;
  2271. }
  2272. spcom_pr_dbg("unregister channel cmd arg: ch_name[%s]\n", arg->ch_name);
  2273. ch_name = arg->ch_name;
  2274. if (!is_valid_ch_name(ch_name)) {
  2275. spcom_pr_err("invalid channel name\n");
  2276. return -EINVAL;
  2277. }
  2278. if (is_control_channel_name(ch_name)) {
  2279. spcom_pr_err("cannot unregister control channel\n");
  2280. return -EINVAL;
  2281. }
  2282. /* Lock before modifying spcom device global channel list */
  2283. mutex_lock(&spcom_dev->ch_list_lock);
  2284. ch = spcom_find_channel_by_name(ch_name);
  2285. if (!ch) {
  2286. spcom_pr_err("could not find channel[%s]\n", ch_name);
  2287. mutex_unlock(&spcom_dev->ch_list_lock);
  2288. return -ENODEV;
  2289. }
  2290. /* Reset channel context */
  2291. ret = spcom_channel_deinit(ch);
  2292. /* Unlock spcom device global channel list */
  2293. mutex_unlock(&spcom_dev->ch_list_lock);
  2294. spcom_pr_dbg("spcom unregister ch[%s] is done, ret[%d]\n", ch_name, ret);
  2295. return ret;
  2296. }
  2297. /**
  2298. * spcom_ioctl_handle_is_channel_connected
  2299. *
  2300. * @brief Handle check if SPCOM channel is connected IOCTL command from user space
  2301. *
  2302. * @arg[in] IOCTL command arguments
  2303. *
  2304. * @return zero if not connected, positive value if connected, negative value otherwise.
  2305. */
  2306. static int spcom_ioctl_handle_is_channel_connected(struct spcom_ioctl_ch *arg)
  2307. {
  2308. const char *ch_name = arg->ch_name;
  2309. struct spcom_channel *ch = NULL;
  2310. int ret = 0;
  2311. spcom_pr_dbg("Is channel connected cmd arg: ch_name[%s]\n", arg->ch_name);
  2312. if (!is_valid_ch_name(ch_name)) {
  2313. spcom_pr_err("invalid channel name\n");
  2314. return -EINVAL;
  2315. }
  2316. if (is_control_channel_name(ch_name)) {
  2317. spcom_pr_err("invalid control device: %s\n", ch_name);
  2318. return -EINVAL;
  2319. }
  2320. ch = spcom_find_channel_by_name(ch_name);
  2321. if (!ch) {
  2322. spcom_pr_err("could not find channel[%s]\n", ch_name);
  2323. return -EINVAL;
  2324. }
  2325. mutex_lock(&ch->lock);
  2326. /* rpdev is set during spcom_rpdev_probe when remote app is loaded */
  2327. ret = (ch->rpdev != NULL) ? 1 : 0;
  2328. mutex_unlock(&ch->lock);
  2329. return ret;
  2330. }
  2331. /**
  2332. * spcom_ioctl_handle_lock_dmabuf_commnad
  2333. *
  2334. * @brief Handle DMA buffer lock IOCTL command from user space
  2335. *
  2336. * @param[in] arg IOCTL command arguments
  2337. *
  2338. * @return zero on successful operation, negative value otherwise.
  2339. */
  2340. static int spcom_ioctl_handle_lock_dmabuf_command(struct spcom_ioctl_dmabuf_lock *arg)
  2341. {
  2342. struct spcom_channel *ch = NULL;
  2343. struct dma_buf *dma_buf = NULL;
  2344. const char *ch_name = NULL;
  2345. uint32_t pid = current_pid();
  2346. int fd = 0;
  2347. int i = 0;
  2348. spcom_pr_dbg("Lock dmabuf cmd arg: ch_name[%s], fd[%d], padding[%u], PID[%ld]\n",
  2349. arg->ch_name, arg->fd, arg->padding, current_pid());
  2350. ch_name = arg->ch_name;
  2351. if (!is_valid_ch_name(ch_name)) {
  2352. spcom_pr_err("invalid channel name\n");
  2353. return -EINVAL;
  2354. }
  2355. fd = arg->fd;
  2356. if (!pid) {
  2357. spcom_pr_err("unknown PID\n");
  2358. return -EINVAL;
  2359. }
  2360. if (fd > (unsigned int)INT_MAX) {
  2361. spcom_pr_err("int overflow [%u]\n", fd);
  2362. return -EINVAL;
  2363. }
  2364. ch = spcom_find_channel_by_name(ch_name);
  2365. if (!ch) {
  2366. spcom_pr_err("could not find channel[%s]\n", ch_name);
  2367. return -ENODEV;
  2368. }
  2369. dma_buf = dma_buf_get(fd);
  2370. if (IS_ERR_OR_NULL(dma_buf)) {
  2371. spcom_pr_err("fail to get dma buf handle\n");
  2372. return -EINVAL;
  2373. }
  2374. /* DMA buffer lock doesn't involve any Rx/Tx data to remote edge */
  2375. mutex_lock(&ch->lock);
  2376. /* Check if channel is open */
  2377. if (!spcom_is_channel_open(ch)) {
  2378. spcom_pr_err("Channel [%s] is closed\n", ch_name);
  2379. mutex_unlock(&ch->lock);
  2380. dma_buf_put(dma_buf);
  2381. return -EINVAL;
  2382. }
  2383. /* Check if this shared buffer is already locked */
  2384. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2385. if (ch->dmabuf_array[i].handle == dma_buf) {
  2386. spcom_pr_dbg("fd [%d] shared buf is already locked\n", fd);
  2387. mutex_unlock(&ch->lock);
  2388. dma_buf_put(dma_buf); /* decrement back the ref count */
  2389. return -EINVAL;
  2390. }
  2391. }
  2392. /* Store the dma_buf handle */
  2393. for (i = 0 ; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2394. struct dma_buf_info *curr_buf = &ch->dmabuf_array[i];
  2395. if (curr_buf->handle == NULL) {
  2396. curr_buf->handle = dma_buf;
  2397. curr_buf->fd = fd;
  2398. curr_buf->owner_pid = pid;
  2399. spcom_pr_dbg("ch [%s] locked dma buf #%d fd [%d] dma_buf=0x%pK pid #%d\n",
  2400. ch_name, i, curr_buf->fd, curr_buf->handle, curr_buf->owner_pid);
  2401. mutex_unlock(&ch->lock);
  2402. return 0;
  2403. }
  2404. }
  2405. mutex_unlock(&ch->lock);
  2406. /* decrement back the ref count */
  2407. dma_buf_put(dma_buf);
  2408. spcom_pr_err("No free entry to store dmabuf handle of fd [%d] on ch [%s]\n", fd, ch_name);
  2409. return -EFAULT;
  2410. }
  2411. /**
  2412. * spcom_ioctl_handle_unlock_dmabuf_commnad
  2413. *
  2414. * @brief Handle DMA buffer unlock IOCTL command from user space
  2415. *
  2416. * @param[in] arg IOCTL command arguments
  2417. *
  2418. * @return zero on success, negative value otherwise.
  2419. */
  2420. static int spcom_ioctl_handle_unlock_dmabuf_command(struct spcom_ioctl_dmabuf_lock *arg)
  2421. {
  2422. struct spcom_channel *ch = NULL;
  2423. struct dma_buf *dma_buf = NULL;
  2424. const char *ch_name = NULL;
  2425. struct dma_buf *curr_handle = NULL;
  2426. bool found = false;
  2427. int fd = 0;
  2428. int i = 0;
  2429. int ret = 0;
  2430. bool unlock_all = false;
  2431. spcom_pr_dbg("Unlock dmabuf cmd arg: ch_name[%s], fd[%d], padding[%u], PID[%ld]\n",
  2432. arg->ch_name, arg->fd, arg->padding, current_pid());
  2433. ch_name = arg->ch_name;
  2434. if (!is_valid_ch_name(ch_name))
  2435. return -EINVAL;
  2436. fd = arg->fd;
  2437. if (fd > (unsigned int)INT_MAX) {
  2438. spcom_pr_err("int overflow [%u]\n", fd);
  2439. return -EINVAL;
  2440. }
  2441. if (fd == (int) SPCOM_DMABUF_FD_UNLOCK_ALL) {
  2442. spcom_pr_dbg("unlock all FDs of PID [%d]\n", current_pid());
  2443. unlock_all = true;
  2444. }
  2445. ch = spcom_find_channel_by_name(ch_name);
  2446. if (!ch)
  2447. return -ENODEV;
  2448. dma_buf = dma_buf_get(fd);
  2449. if (IS_ERR_OR_NULL(dma_buf)) {
  2450. spcom_pr_err("Failed to get dma buf handle, fd [%d]\n", fd);
  2451. return -EINVAL;
  2452. }
  2453. dma_buf_put(dma_buf);
  2454. mutex_lock(&ch->lock);
  2455. if (unlock_all) { /* Unlock all buffers of current PID on channel */
  2456. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2457. if (spcom_dmabuf_unlock(&ch->dmabuf_array[i], true) == 0)
  2458. found = true;
  2459. }
  2460. } else { /* Unlock specific buffer if owned by current PID */
  2461. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++) {
  2462. curr_handle = ch->dmabuf_array[i].handle;
  2463. if (curr_handle && curr_handle == dma_buf) {
  2464. ret = spcom_dmabuf_unlock(&ch->dmabuf_array[i], true);
  2465. found = true;
  2466. break;
  2467. }
  2468. }
  2469. }
  2470. mutex_unlock(&ch->lock);
  2471. if (!found) {
  2472. spcom_pr_err("Buffer fd [%d] was not found for PID [%u] on channel [%s]\n",
  2473. fd, current_pid(), ch_name);
  2474. return -ENODEV;
  2475. }
  2476. return ret;
  2477. }
  2478. /**
  2479. * spcom_ioctl_handle_get_message
  2480. *
  2481. * @brief Handle get message (request or response) IOCTL command from user space
  2482. *
  2483. * @param[in] arg IOCTL command arguments
  2484. * @param[out] user_buffer user space buffer to copy message to
  2485. *
  2486. * @return size in bytes on success, negative value on failure.
  2487. */
  2488. static int spcom_ioctl_handle_get_message(struct spcom_ioctl_message *arg, void *user_buffer)
  2489. {
  2490. struct spcom_channel *ch = NULL;
  2491. struct spcom_msg_hdr *hdr = NULL;
  2492. const char *ch_name = NULL;
  2493. void *rx_buf = NULL;
  2494. int rx_buf_size = 0;
  2495. uint32_t msg_sz = arg->buffer_size;
  2496. uint32_t timeout_msec = 0; /* client only */
  2497. int ret = 0;
  2498. spcom_pr_dbg("Get message cmd arg: ch_name[%s], timeout_msec [%u], buffer size[%u]\n",
  2499. arg->ch_name, arg->timeout_msec, arg->buffer_size);
  2500. ch_name = arg->ch_name;
  2501. if (!is_valid_ch_name(ch_name)) {
  2502. spcom_pr_err("invalid channel name\n");
  2503. ret = -EINVAL;
  2504. goto get_message_out;
  2505. }
  2506. /* DEVICE_NAME name is reserved for control channel */
  2507. if (is_control_channel_name(ch_name)) {
  2508. spcom_pr_err("cannot send message on management channel %s\n", ch_name);
  2509. ret = -EFAULT;
  2510. goto get_message_out;
  2511. }
  2512. ch = spcom_find_channel_by_name(ch_name);
  2513. if (!ch)
  2514. return -ENODEV;
  2515. /* Check if remote side connect */
  2516. if (!spcom_is_channel_connected(ch)) {
  2517. spcom_pr_err("ch [%s] remote side not connect\n", ch_name);
  2518. ret = -ENOTCONN;
  2519. goto get_message_out;
  2520. }
  2521. /* Check param validity */
  2522. if (msg_sz > SPCOM_MAX_RESPONSE_SIZE) {
  2523. spcom_pr_err("ch [%s] invalid size [%d]\n", ch_name, msg_sz);
  2524. ret = -EINVAL;
  2525. goto get_message_out;
  2526. }
  2527. spcom_pr_dbg("waiting for incoming message, ch[%s], size[%u]\n", ch_name, msg_sz);
  2528. /* Allocate Buffers*/
  2529. rx_buf_size = sizeof(*hdr) + msg_sz;
  2530. rx_buf = kzalloc(rx_buf_size, GFP_KERNEL);
  2531. if (!rx_buf) {
  2532. ret = -ENOMEM;
  2533. goto get_message_out;
  2534. }
  2535. /* Client response timeout depends on the request handling time on the remote side
  2536. * Server send response to remote edge and return immediately, timeout isn't needed
  2537. */
  2538. if (!ch->is_server) {
  2539. timeout_msec = ch->response_timeout_msec;
  2540. spcom_pr_dbg("response timeout_msec [%d]\n", (int) timeout_msec);
  2541. }
  2542. ret = spcom_rx(ch, rx_buf, rx_buf_size, timeout_msec);
  2543. if (ret < 0) {
  2544. spcom_pr_err("rx error %d\n", ret);
  2545. goto get_message_out;
  2546. }
  2547. msg_sz = ret; /* actual_rx_size */
  2548. hdr = rx_buf;
  2549. if (ch->is_server) {
  2550. ch->txn_id = hdr->txn_id; /* SPCOM server sets the request tnx_id */
  2551. spcom_pr_dbg("ch[%s]: request txn_id [0x%x]\n", ch_name, ch->txn_id);
  2552. }
  2553. /* Verify incoming message size */
  2554. if (msg_sz <= sizeof(*hdr)) {
  2555. spcom_pr_err("rx size [%d] too small\n", msg_sz);
  2556. ret = -EFAULT;
  2557. goto get_message_out;
  2558. }
  2559. /* Copy message to user */
  2560. msg_sz -= sizeof(*hdr);
  2561. spcom_pr_dbg("copying message to user space, size: [%d]\n", msg_sz);
  2562. ret = copy_to_user(user_buffer, hdr->buf, msg_sz);
  2563. if (ret) {
  2564. spcom_pr_err("failed to copy to user, ret [%d]\n", ret);
  2565. ret = -EFAULT;
  2566. goto get_message_out;
  2567. }
  2568. ret = msg_sz;
  2569. spcom_pr_dbg("get message done, msg size[%d]\n", msg_sz);
  2570. get_message_out:
  2571. if (ch && ch->active_pid == current_pid()) {
  2572. ch->active_pid = 0;
  2573. mutex_unlock(&ch->shared_sync_lock);
  2574. }
  2575. kfree(rx_buf);
  2576. /* close pm awake window for spcom client get response */
  2577. mutex_lock(&ch->lock);
  2578. if (!ch->is_server) {
  2579. __pm_relax(spcom_dev->ws);
  2580. spcom_pr_dbg("ch[%s]:pm_relax() called for server, after tx\n",
  2581. ch->name);
  2582. }
  2583. mutex_unlock(&ch->lock);
  2584. return ret;
  2585. }
  2586. /**
  2587. * spcom_ioctl_handle_poll_event
  2588. *
  2589. * @brief Handle SPCOM event poll ioctl command from user space
  2590. *
  2591. * @param[in] arg IOCTL command arguments
  2592. * @param[out] user_retval user space address of poll return value
  2593. *
  2594. * @return Zero on success, negative value on failure.
  2595. */
  2596. static int spcom_ioctl_handle_poll_event(struct spcom_ioctl_poll_event *arg, int32_t *user_retval)
  2597. {
  2598. int ret = 0;
  2599. uint32_t link_state = 0;
  2600. spcom_pr_dbg("Handle poll event cmd args: event_id[%d], wait[%u], retval[%d], padding[%d]\n",
  2601. arg->event_id, arg->wait, arg->retval, arg->padding);
  2602. switch (arg->event_id) {
  2603. case SPCOM_EVENT_LINK_STATE:
  2604. {
  2605. if (arg->wait) {
  2606. reinit_completion(&spcom_dev->rpmsg_state_change);
  2607. ret = wait_for_completion_interruptible(
  2608. &spcom_dev->rpmsg_state_change);
  2609. if (ret) {/* wait was interrupted */
  2610. spcom_pr_info("Wait for link state change interrupted, ret[%d]\n",
  2611. ret);
  2612. return -EINTR;
  2613. }
  2614. }
  2615. if (atomic_read(&spcom_dev->rpmsg_dev_count) > 0)
  2616. link_state = 1;
  2617. spcom_pr_dbg("SPCOM link state change: Signaled [%d], PID [%d]\n",
  2618. link_state, current_pid());
  2619. ret = put_user(link_state, user_retval);
  2620. if (ret) {
  2621. spcom_pr_err("unable to copy link state to user [%d]\n", ret);
  2622. return -EFAULT;
  2623. }
  2624. return 0;
  2625. }
  2626. default:
  2627. spcom_pr_err("SPCOM handle poll unsupported event id [%u]\n", arg->event_id);
  2628. return -EINVAL;
  2629. }
  2630. return -EBADRQC;
  2631. }
  2632. /**
  2633. * spcom_ioctl_handle_get_next_req_msg_size
  2634. *
  2635. * @brief Handle user space get next request message size IOCTL command from user space
  2636. *
  2637. * @param arg[in] IOCTL command arguments
  2638. * @param user_req_size[out] user space next request size pointer
  2639. *
  2640. * @Return size in bytes on success, negative value on failure.
  2641. */
  2642. static int spcom_ioctl_handle_get_next_req_msg_size(struct spcom_ioctl_next_request_size *arg,
  2643. uint32_t *user_size)
  2644. {
  2645. struct spcom_channel *ch = NULL;
  2646. const char *ch_name = NULL;
  2647. int ret = 0;
  2648. spcom_pr_dbg("Get next request msg size cmd arg: ch_name[%s], size[%u], padding[%d]\n",
  2649. arg->ch_name, arg->size, arg->padding);
  2650. ch_name = arg->ch_name;
  2651. if (!is_valid_ch_name(ch_name))
  2652. return -EINVAL;
  2653. ch = spcom_find_channel_by_name(ch_name);
  2654. if (!ch)
  2655. return -ENODEV;
  2656. ret = spcom_get_next_request_size(ch);
  2657. if (ret < 0)
  2658. return ret;
  2659. spcom_pr_dbg("Channel[%s], next request size[%d]\n", ch_name, ret);
  2660. /* Copy next request size to user space */
  2661. ret = put_user(ret, user_size);
  2662. if (ret) {
  2663. spcom_pr_err("unable to copy to user [%d]\n", ret);
  2664. return -EFAULT;
  2665. }
  2666. return 0;
  2667. }
  2668. /**
  2669. * spcom_ioctl_handle_copy_and_send_message
  2670. *
  2671. * @brief Handle SPCOM send message (request or response) IOCTL command from user space
  2672. *
  2673. * @param[in] arg IOCTL command arguments
  2674. * @param[in] user_msg_buffer user message buffer
  2675. * @param[in] is_modified flag to indicate if this is a modified message or regular message
  2676. *
  2677. * @return: zero on success, negative value otherwise.
  2678. */
  2679. static int spcom_ioctl_handle_copy_and_send_message(void *arg, void *user_msg_buffer,
  2680. bool is_modified)
  2681. {
  2682. struct spcom_ioctl_modified_message *mod_msg = NULL;
  2683. struct spcom_ioctl_message *msg = NULL;
  2684. void *msg_buffer_copy = NULL;
  2685. uint32_t buffer_size = 0;
  2686. int ret = 0;
  2687. if (is_modified) {
  2688. mod_msg = (struct spcom_ioctl_modified_message *)arg;
  2689. buffer_size = mod_msg->buffer_size;
  2690. } else {
  2691. msg = (struct spcom_ioctl_message *)arg;
  2692. buffer_size = msg->buffer_size;
  2693. }
  2694. msg_buffer_copy = kzalloc(buffer_size, GFP_KERNEL);
  2695. if (!msg_buffer_copy)
  2696. return -ENOMEM;
  2697. spcom_pr_dbg("copying message buffer from user space, size[%u]\n", buffer_size);
  2698. ret = copy_from_user(msg_buffer_copy, user_msg_buffer, buffer_size);
  2699. if (ret) {
  2700. spcom_pr_err("failed to copy from user, ret [%d]\n", ret);
  2701. kfree(msg_buffer_copy);
  2702. return -EFAULT;
  2703. }
  2704. /* Send SPCOM message to remote edge */
  2705. ret = spcom_send_message(arg, msg_buffer_copy, is_modified);
  2706. kfree(msg_buffer_copy);
  2707. return ret;
  2708. }
  2709. /**
  2710. * spcom_ioctl_copy_user_arg
  2711. *
  2712. * Helper function to copy user arguments of IOCTL commands
  2713. *
  2714. * @user_arg: user IOCTL command arguments pointer
  2715. * @arg_copy: internal copy of user arguments
  2716. * @size: size of user arguments struct
  2717. *
  2718. * @return: zero on success, negative value otherwise.
  2719. */
  2720. static inline int spcom_ioctl_copy_user_arg(void *user_arg, void *arg_copy, uint32_t size)
  2721. {
  2722. int ret = 0;
  2723. if (!user_arg) {
  2724. spcom_pr_err("user arg is NULL\n");
  2725. return -EINVAL;
  2726. }
  2727. ret = copy_from_user(arg_copy, user_arg, size);
  2728. if (ret) {
  2729. spcom_pr_err("copy from user failed, size [%u], ret[%d]\n", size, ret);
  2730. return -EFAULT;
  2731. }
  2732. return 0;
  2733. }
  2734. bool is_arg_size_expected(unsigned int cmd, uint32_t arg_size)
  2735. {
  2736. uint32_t expected_size = 0;
  2737. switch (cmd) {
  2738. case SPCOM_POLL_STATE:
  2739. expected_size = sizeof(struct spcom_poll_param);
  2740. break;
  2741. case SPCOM_IOCTL_STATE_POLL:
  2742. expected_size = sizeof(struct spcom_ioctl_poll_event);
  2743. break;
  2744. case SPCOM_IOCTL_SEND_MSG:
  2745. case SPCOM_IOCTL_GET_MSG:
  2746. expected_size = sizeof(struct spcom_ioctl_message);
  2747. break;
  2748. case SPCOM_IOCTL_SEND_MOD_MSG:
  2749. expected_size = sizeof(struct spcom_ioctl_modified_message);
  2750. break;
  2751. case SPCOM_IOCTL_GET_NEXT_REQ_SZ:
  2752. expected_size = sizeof(struct spcom_ioctl_next_request_size);
  2753. break;
  2754. case SPCOM_IOCTL_SHARED_CH_CREATE:
  2755. case SPCOM_IOCTL_CH_REGISTER:
  2756. case SPCOM_IOCTL_CH_UNREGISTER:
  2757. case SPCOM_IOCTL_CH_IS_CONNECTED:
  2758. expected_size = sizeof(struct spcom_ioctl_ch);
  2759. break;
  2760. case SPCOM_IOCTL_DMABUF_LOCK:
  2761. case SPCOM_IOCTL_DMABUF_UNLOCK:
  2762. expected_size = sizeof(struct spcom_ioctl_dmabuf_lock);
  2763. break;
  2764. default:
  2765. spcom_pr_err("No userspace data for ioctl cmd[%ld]\n", cmd);
  2766. return false;
  2767. }
  2768. if (arg_size != expected_size) {
  2769. spcom_pr_err("Invalid cmd size: cmd[%ld], arg size[%u], expected[%u]\n",
  2770. cmd, arg_size, expected_size);
  2771. return false;
  2772. }
  2773. return true;
  2774. }
  2775. static long spcom_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2776. {
  2777. void __user *user_arg = (void __user *)arg;
  2778. union spcom_ioctl_arg arg_copy = {0};
  2779. uint32_t arg_size = 0;
  2780. int ret = 0;
  2781. spcom_pr_dbg("ioctl cmd [%u], PID [%d]\n", _IOC_NR(cmd), current_pid());
  2782. if (atomic_read(&spcom_dev->remove_in_progress)) {
  2783. spcom_pr_err("module remove in progress\n");
  2784. return -ENODEV;
  2785. }
  2786. if (!is_control_channel(file) && cmd != SPCOM_POLL_STATE) {
  2787. spcom_pr_err("ioctl is supported only for control channel\n");
  2788. return -EINVAL;
  2789. }
  2790. if ((_IOC_DIR(cmd) & _IOC_WRITE)) {
  2791. arg_size = _IOC_SIZE(cmd);
  2792. if (!is_arg_size_expected(cmd, arg_size))
  2793. return -EFAULT;
  2794. ret = spcom_ioctl_copy_user_arg(user_arg, &arg_copy, arg_size);
  2795. if (ret)
  2796. return ret;
  2797. }
  2798. switch (cmd) {
  2799. case SPCOM_POLL_STATE:
  2800. return handle_poll(file, &(arg_copy.poll),
  2801. &((struct spcom_poll_param *)user_arg)->retval);
  2802. case SPCOM_GET_RMB_ERROR:
  2803. return put_user(spcom_dev->rmb_error, (uint32_t *)arg);
  2804. case SPCOM_IOCTL_STATE_POLL:
  2805. return spcom_ioctl_handle_poll_event(
  2806. &(arg_copy.poll_event),
  2807. &((struct spcom_ioctl_poll_event *)user_arg)->retval);
  2808. case SPCOM_IOCTL_SEND_MSG:
  2809. return spcom_ioctl_handle_copy_and_send_message(&(arg_copy.message),
  2810. ((struct spcom_ioctl_message *)user_arg)->buffer, false);
  2811. case SPCOM_IOCTL_SEND_MOD_MSG:
  2812. return spcom_ioctl_handle_copy_and_send_message(&(arg_copy.message),
  2813. ((struct spcom_ioctl_modified_message *)user_arg)->buffer, true);
  2814. case SPCOM_IOCTL_GET_NEXT_REQ_SZ:
  2815. return spcom_ioctl_handle_get_next_req_msg_size(&(arg_copy.next_req_size),
  2816. &((struct spcom_ioctl_next_request_size *)user_arg)->size);
  2817. case SPCOM_IOCTL_GET_MSG:
  2818. return spcom_ioctl_handle_get_message(&(arg_copy.message),
  2819. ((struct spcom_ioctl_message *)user_arg)->buffer);
  2820. case SPCOM_IOCTL_SHARED_CH_CREATE:
  2821. return spcom_ioctl_handle_create_shared_ch_command(&(arg_copy.channel));
  2822. case SPCOM_IOCTL_CH_REGISTER:
  2823. return spcom_ioctl_handle_channel_register_command(&(arg_copy.channel));
  2824. case SPCOM_IOCTL_CH_UNREGISTER:
  2825. return spcom_ioctl_handle_channel_unregister_command(&(arg_copy.channel));
  2826. case SPCOM_IOCTL_CH_IS_CONNECTED:
  2827. return spcom_ioctl_handle_is_channel_connected(&(arg_copy.channel));
  2828. case SPCOM_IOCTL_DMABUF_LOCK:
  2829. return spcom_ioctl_handle_lock_dmabuf_command(&(arg_copy.dmabuf_lock));
  2830. case SPCOM_IOCTL_DMABUF_UNLOCK:
  2831. return spcom_ioctl_handle_unlock_dmabuf_command(&(arg_copy.dmabuf_lock));
  2832. case SPCOM_IOCTL_RESTART_SPU:
  2833. return spcom_ioctl_handle_restart_spu_command();
  2834. case SPCOM_IOCTL_ENABLE_SSR:
  2835. return spcom_handle_enable_ssr_command();
  2836. default:
  2837. spcom_pr_err("ioctl cmd[%d] is not supported\n", cmd);
  2838. }
  2839. return -ENOIOCTLCMD;
  2840. }
  2841. /* file operation supported from user space */
  2842. static const struct file_operations fops = {
  2843. .read = spcom_device_read,
  2844. .write = spcom_device_write,
  2845. .open = spcom_device_open,
  2846. .release = spcom_device_release,
  2847. .unlocked_ioctl = spcom_device_ioctl,
  2848. };
  2849. /**
  2850. * spcom_create_channel_chardev() - Create a channel char-dev node file
  2851. * for user space interface
  2852. */
  2853. static int spcom_create_channel_chardev(const char *name, bool is_sharable)
  2854. {
  2855. int ret;
  2856. struct device *dev;
  2857. struct spcom_channel *ch;
  2858. dev_t devt;
  2859. struct class *cls = spcom_dev->driver_class;
  2860. struct device *parent = spcom_dev->class_dev;
  2861. void *priv;
  2862. struct cdev *cdev;
  2863. if (!name || strnlen(name, SPCOM_CHANNEL_NAME_SIZE) ==
  2864. SPCOM_CHANNEL_NAME_SIZE) {
  2865. spcom_pr_err("invalid channel name\n");
  2866. return -EINVAL;
  2867. }
  2868. spcom_pr_dbg("creating channel [%s]\n", name);
  2869. ch = spcom_find_channel_by_name(name);
  2870. if (ch) {
  2871. spcom_pr_err("channel [%s] already exist\n", name);
  2872. return -EBUSY;
  2873. }
  2874. ch = spcom_find_channel_by_name(""); /* find reserved channel */
  2875. if (!ch) {
  2876. spcom_pr_err("no free channel\n");
  2877. return -ENODEV;
  2878. }
  2879. ret = spcom_init_channel(ch, is_sharable, name);
  2880. if (ret < 0) {
  2881. spcom_pr_err("can't init channel %d\n", ret);
  2882. return ret;
  2883. }
  2884. ret = spcom_register_rpmsg_drv(ch);
  2885. if (ret < 0) {
  2886. spcom_pr_err("register rpmsg driver failed %d\n", ret);
  2887. goto exit_destroy_channel;
  2888. }
  2889. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  2890. if (!cdev) {
  2891. ret = -ENOMEM;
  2892. goto exit_unregister_drv;
  2893. }
  2894. devt = spcom_dev->device_no + spcom_dev->chdev_count;
  2895. priv = ch;
  2896. /*
  2897. * Pass channel name as formatted string to avoid abuse by using a
  2898. * formatted string as channel name
  2899. */
  2900. dev = device_create(cls, parent, devt, priv, "%s", name);
  2901. if (IS_ERR(dev)) {
  2902. spcom_pr_err("device_create failed\n");
  2903. ret = -ENODEV;
  2904. goto exit_free_cdev;
  2905. }
  2906. cdev_init(cdev, &fops);
  2907. cdev->owner = THIS_MODULE;
  2908. ret = cdev_add(cdev, devt, 1);
  2909. if (ret < 0) {
  2910. spcom_pr_err("cdev_add failed %d\n", ret);
  2911. ret = -ENODEV;
  2912. goto exit_destroy_device;
  2913. }
  2914. spcom_dev->chdev_count++;
  2915. mutex_lock(&ch->lock);
  2916. ch->cdev = cdev;
  2917. ch->dev = dev;
  2918. ch->devt = devt;
  2919. mutex_unlock(&ch->lock);
  2920. return 0;
  2921. exit_destroy_device:
  2922. device_destroy(spcom_dev->driver_class, devt);
  2923. exit_free_cdev:
  2924. kfree(cdev);
  2925. exit_unregister_drv:
  2926. ret = spcom_unregister_rpmsg_drv(ch);
  2927. if (ret != 0)
  2928. spcom_pr_err("can't unregister rpmsg drv %d\n", ret);
  2929. exit_destroy_channel:
  2930. /* empty channel leaves free slot for next time*/
  2931. mutex_lock(&ch->lock);
  2932. memset(ch->name, 0, SPCOM_CHANNEL_NAME_SIZE);
  2933. mutex_unlock(&ch->lock);
  2934. return ret;
  2935. }
  2936. // TODO: error handling
  2937. static int spcom_destroy_channel_chardev(const char *name)
  2938. {
  2939. int ret;
  2940. struct spcom_channel *ch;
  2941. spcom_pr_err("destroy channel [%s]\n", name);
  2942. ch = spcom_find_channel_by_name(name);
  2943. if (!ch) {
  2944. spcom_pr_err("channel [%s] not exist\n", name);
  2945. return -EINVAL;
  2946. }
  2947. ret = spcom_unregister_rpmsg_drv(ch);
  2948. if (ret < 0)
  2949. spcom_pr_err("unregister rpmsg driver failed %d\n", ret);
  2950. mutex_lock(&ch->lock);
  2951. device_destroy(spcom_dev->driver_class, ch->devt);
  2952. kfree(ch->cdev);
  2953. mutex_unlock(&ch->lock);
  2954. mutex_lock(&spcom_dev->chdev_count_lock);
  2955. spcom_dev->chdev_count--;
  2956. mutex_unlock(&spcom_dev->chdev_count_lock);
  2957. return 0;
  2958. }
  2959. static int spcom_register_chardev(void)
  2960. {
  2961. int ret;
  2962. unsigned int baseminor = 0;
  2963. unsigned int count = 1;
  2964. ret = alloc_chrdev_region(&spcom_dev->device_no, baseminor, count,
  2965. DEVICE_NAME);
  2966. if (ret < 0) {
  2967. spcom_pr_err("alloc_chrdev_region failed %d\n", ret);
  2968. return ret;
  2969. }
  2970. spcom_dev->driver_class = class_create(THIS_MODULE, DEVICE_NAME);
  2971. if (IS_ERR(spcom_dev->driver_class)) {
  2972. ret = -ENOMEM;
  2973. spcom_pr_err("class_create failed %d\n", ret);
  2974. goto exit_unreg_chrdev_region;
  2975. }
  2976. spcom_dev->class_dev = device_create(spcom_dev->driver_class, NULL,
  2977. spcom_dev->device_no, spcom_dev, DEVICE_NAME);
  2978. if (IS_ERR(spcom_dev->class_dev)) {
  2979. spcom_pr_err("class_device_create failed %d\n", ret);
  2980. ret = -ENOMEM;
  2981. goto exit_destroy_class;
  2982. }
  2983. cdev_init(&spcom_dev->cdev, &fops);
  2984. spcom_dev->cdev.owner = THIS_MODULE;
  2985. ret = cdev_add(&spcom_dev->cdev,
  2986. MKDEV(MAJOR(spcom_dev->device_no), 0),
  2987. SPCOM_MAX_CHANNELS);
  2988. if (ret < 0) {
  2989. spcom_pr_err("cdev_add failed %d\n", ret);
  2990. goto exit_destroy_device;
  2991. }
  2992. spcom_pr_dbg("char device created\n");
  2993. return 0;
  2994. exit_destroy_device:
  2995. device_destroy(spcom_dev->driver_class, spcom_dev->device_no);
  2996. exit_destroy_class:
  2997. class_destroy(spcom_dev->driver_class);
  2998. exit_unreg_chrdev_region:
  2999. unregister_chrdev_region(spcom_dev->device_no, 1);
  3000. return ret;
  3001. }
  3002. static void spcom_unregister_chrdev(void)
  3003. {
  3004. cdev_del(&spcom_dev->cdev);
  3005. device_destroy(spcom_dev->driver_class, spcom_dev->device_no);
  3006. class_destroy(spcom_dev->driver_class);
  3007. mutex_lock(&spcom_dev->chdev_count_lock);
  3008. unregister_chrdev_region(spcom_dev->device_no, spcom_dev->chdev_count);
  3009. mutex_unlock(&spcom_dev->chdev_count_lock);
  3010. spcom_pr_dbg("control spcom device removed\n");
  3011. }
  3012. static int spcom_parse_dt(struct device_node *np)
  3013. {
  3014. int ret;
  3015. const char *propname = "qcom,spcom-ch-names";
  3016. int num_ch;
  3017. int i;
  3018. const char *name;
  3019. /* Get predefined channels info */
  3020. num_ch = of_property_count_strings(np, propname);
  3021. if (num_ch < 0) {
  3022. spcom_pr_err("wrong format of predefined channels definition [%d]\n",
  3023. num_ch);
  3024. return num_ch;
  3025. }
  3026. if (num_ch > ARRAY_SIZE(spcom_dev->predefined_ch_name)) {
  3027. spcom_pr_err("too many predefined channels [%d]\n", num_ch);
  3028. return -EINVAL;
  3029. }
  3030. spcom_pr_dbg("num of predefined channels [%d]\n", num_ch);
  3031. for (i = 0; i < num_ch; i++) {
  3032. ret = of_property_read_string_index(np, propname, i, &name);
  3033. if (ret) {
  3034. spcom_pr_err("failed to read DT ch#%d name\n", i);
  3035. return -EFAULT;
  3036. }
  3037. strscpy(spcom_dev->predefined_ch_name[i],
  3038. name,
  3039. sizeof(spcom_dev->predefined_ch_name[i]));
  3040. spcom_pr_dbg("found ch [%s]\n", name);
  3041. }
  3042. return num_ch;
  3043. }
  3044. /*
  3045. * the function is running on system workqueue context,
  3046. * processes delayed (by rpmsg rx callback) packets:
  3047. * each packet belong to its destination spcom channel ch
  3048. */
  3049. static void spcom_signal_rx_done(struct work_struct *ignored)
  3050. {
  3051. struct spcom_channel *ch;
  3052. struct rx_buff_list *rx_item;
  3053. struct spcom_msg_hdr *hdr;
  3054. unsigned long flags;
  3055. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3056. while (!list_empty(&spcom_dev->rx_list_head)) {
  3057. /* detach last entry */
  3058. rx_item = list_last_entry(&spcom_dev->rx_list_head,
  3059. struct rx_buff_list, list);
  3060. list_del(&rx_item->list);
  3061. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3062. if (!rx_item) {
  3063. spcom_pr_err("empty entry in pending rx list\n");
  3064. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3065. continue;
  3066. }
  3067. ch = rx_item->ch;
  3068. hdr = (struct spcom_msg_hdr *)rx_item->rpmsg_rx_buf;
  3069. mutex_lock(&ch->lock);
  3070. if (ch->comm_role_undefined) {
  3071. ch->comm_role_undefined = false;
  3072. ch->is_server = true;
  3073. ch->txn_id = hdr->txn_id;
  3074. spcom_pr_dbg("ch [%s] first packet txn_id=%d, it is server\n",
  3075. ch->name, ch->txn_id);
  3076. }
  3077. if (ch->rpmsg_abort) {
  3078. if (ch->rpmsg_rx_buf) {
  3079. spcom_pr_dbg("ch [%s] rx aborted free %zd bytes\n",
  3080. ch->name, ch->actual_rx_size);
  3081. kfree(ch->rpmsg_rx_buf);
  3082. ch->actual_rx_size = 0;
  3083. }
  3084. goto rx_aborted;
  3085. }
  3086. if (ch->rpmsg_rx_buf) {
  3087. spcom_pr_err("ch [%s] previous buffer not consumed %zd bytes\n",
  3088. ch->name, ch->actual_rx_size);
  3089. kfree(ch->rpmsg_rx_buf);
  3090. ch->rpmsg_rx_buf = NULL;
  3091. ch->actual_rx_size = 0;
  3092. }
  3093. if (!ch->is_server && (hdr->txn_id != ch->txn_id)) {
  3094. spcom_pr_err("ch [%s] client: rx dropped txn_id %d, ch->txn_id %d\n",
  3095. ch->name, hdr->txn_id, ch->txn_id);
  3096. goto rx_aborted;
  3097. }
  3098. spcom_pr_dbg("ch[%s] rx txn_id %d, ch->txn_id %d, size=%d\n",
  3099. ch->name, hdr->txn_id, ch->txn_id,
  3100. rx_item->rx_buf_size);
  3101. ch->rpmsg_rx_buf = rx_item->rpmsg_rx_buf;
  3102. ch->actual_rx_size = rx_item->rx_buf_size;
  3103. ch->rx_buf_txn_id = ch->txn_id;
  3104. complete_all(&ch->rx_done);
  3105. mutex_unlock(&ch->lock);
  3106. kfree(rx_item);
  3107. /* lock for the next list entry */
  3108. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3109. }
  3110. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3111. return;
  3112. rx_aborted:
  3113. mutex_unlock(&ch->lock);
  3114. kfree(rx_item->rpmsg_rx_buf);
  3115. kfree(rx_item);
  3116. }
  3117. static int spcom_rpdev_cb(struct rpmsg_device *rpdev,
  3118. void *data, int len, void *priv, u32 src)
  3119. {
  3120. struct spcom_channel *ch;
  3121. static DECLARE_WORK(rpmsg_rx_consumer, spcom_signal_rx_done);
  3122. struct rx_buff_list *rx_item;
  3123. unsigned long flags;
  3124. if (!rpdev || !data) {
  3125. spcom_pr_err("rpdev or data is NULL\n");
  3126. return -EINVAL;
  3127. }
  3128. ch = dev_get_drvdata(&rpdev->dev);
  3129. if (!ch) {
  3130. spcom_pr_err("%s: invalid ch\n", ch->name);
  3131. return -EINVAL;
  3132. }
  3133. if (len > SPCOM_RX_BUF_SIZE || len <= 0) {
  3134. spcom_pr_err("got msg size %d, max allowed %d\n",
  3135. len, SPCOM_RX_BUF_SIZE);
  3136. return -EINVAL;
  3137. }
  3138. rx_item = kzalloc(sizeof(*rx_item), GFP_ATOMIC);
  3139. if (!rx_item)
  3140. return -ENOMEM;
  3141. rx_item->rpmsg_rx_buf = kmemdup(data, len, GFP_ATOMIC);
  3142. if (!rx_item->rpmsg_rx_buf)
  3143. return -ENOMEM;
  3144. rx_item->rx_buf_size = len;
  3145. rx_item->ch = ch;
  3146. pm_wakeup_ws_event(spcom_dev->ws, SPCOM_PM_PACKET_HANDLE_TIMEOUT, true);
  3147. spcom_pr_dbg("%s:got new packet, wakeup requested\n", ch->name);
  3148. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3149. list_add(&rx_item->list, &spcom_dev->rx_list_head);
  3150. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3151. schedule_work(&rpmsg_rx_consumer);
  3152. return 0;
  3153. }
  3154. static int spcom_rpdev_probe(struct rpmsg_device *rpdev)
  3155. {
  3156. const char *name;
  3157. struct spcom_channel *ch;
  3158. if (!rpdev) {
  3159. spcom_pr_err("rpdev is NULL\n");
  3160. return -EINVAL;
  3161. }
  3162. name = rpdev->id.name;
  3163. /* module exiting */
  3164. if (atomic_read(&spcom_dev->remove_in_progress)) {
  3165. spcom_pr_warn("remove in progress, ignore rpmsg probe for ch %s\n",
  3166. name);
  3167. return 0;
  3168. }
  3169. spcom_pr_dbg("new channel %s rpmsg_device arrived\n", name);
  3170. ch = spcom_find_channel_by_name(name);
  3171. if (!ch) {
  3172. spcom_pr_err("channel %s not found\n", name);
  3173. return -ENODEV;
  3174. }
  3175. mutex_lock(&ch->lock);
  3176. ch->rpdev = rpdev;
  3177. ch->rpmsg_abort = false;
  3178. ch->txn_id = INITIAL_TXN_ID;
  3179. complete_all(&ch->connect);
  3180. mutex_unlock(&ch->lock);
  3181. dev_set_drvdata(&rpdev->dev, ch);
  3182. /* used to evaluate underlying transport link up/down */
  3183. atomic_inc(&spcom_dev->rpmsg_dev_count);
  3184. if (atomic_read(&spcom_dev->rpmsg_dev_count) == 1) {
  3185. spcom_pr_info("Signal link up\n");
  3186. complete_all(&spcom_dev->rpmsg_state_change);
  3187. }
  3188. return 0;
  3189. }
  3190. static void spcom_rpdev_remove(struct rpmsg_device *rpdev)
  3191. {
  3192. struct spcom_channel *ch;
  3193. int i;
  3194. if (!rpdev) {
  3195. spcom_pr_err("rpdev is NULL\n");
  3196. return;
  3197. }
  3198. dev_info(&rpdev->dev, "rpmsg device %s removed\n", rpdev->id.name);
  3199. ch = dev_get_drvdata(&rpdev->dev);
  3200. if (!ch) {
  3201. spcom_pr_err("channel %s not found\n", rpdev->id.name);
  3202. return;
  3203. }
  3204. mutex_lock(&ch->lock);
  3205. /* unlock all ion buffers of sp_kernel channel*/
  3206. if (strcmp(ch->name, "sp_kernel") == 0) {
  3207. for (i = 0; i < ARRAY_SIZE(ch->dmabuf_array); i++)
  3208. if (ch->dmabuf_array[i].handle)
  3209. spcom_dmabuf_unlock(&ch->dmabuf_array[i], false);
  3210. }
  3211. ch->rpdev = NULL;
  3212. ch->rpmsg_abort = true;
  3213. ch->txn_id = 0;
  3214. complete_all(&ch->rx_done);
  3215. mutex_unlock(&ch->lock);
  3216. /* used to evaluate underlying transport link up/down */
  3217. if (atomic_dec_and_test(&spcom_dev->rpmsg_dev_count)) {
  3218. spcom_pr_err("Signal link down\n");
  3219. complete_all(&spcom_dev->rpmsg_state_change);
  3220. }
  3221. }
  3222. /* register rpmsg driver to match with channel ch_name */
  3223. static int spcom_register_rpmsg_drv(struct spcom_channel *ch)
  3224. {
  3225. struct rpmsg_driver *rpdrv;
  3226. struct rpmsg_device_id *match;
  3227. char *drv_name;
  3228. int ret;
  3229. if (ch->rpdrv) {
  3230. spcom_pr_err("ch:%s, rpmsg driver %s already registered\n",
  3231. ch->name, ch->rpdrv->id_table->name);
  3232. return -ENODEV;
  3233. }
  3234. rpdrv = kzalloc(sizeof(*rpdrv), GFP_KERNEL);
  3235. if (!rpdrv)
  3236. return -ENOMEM;
  3237. /* zalloc array of two to NULL terminate the match list */
  3238. match = kzalloc(2 * sizeof(*match), GFP_KERNEL);
  3239. if (!match) {
  3240. kfree(rpdrv);
  3241. return -ENOMEM;
  3242. }
  3243. snprintf(match->name, RPMSG_NAME_SIZE, "%s", ch->name);
  3244. drv_name = kasprintf(GFP_KERNEL, "%s_%s", "spcom_rpmsg_drv", ch->name);
  3245. if (!drv_name) {
  3246. spcom_pr_err("can't allocate drv_name for %s\n", ch->name);
  3247. kfree(rpdrv);
  3248. kfree(match);
  3249. return -ENOMEM;
  3250. }
  3251. rpdrv->probe = spcom_rpdev_probe;
  3252. rpdrv->remove = spcom_rpdev_remove;
  3253. rpdrv->callback = spcom_rpdev_cb;
  3254. rpdrv->id_table = match;
  3255. rpdrv->drv.name = drv_name;
  3256. ret = register_rpmsg_driver(rpdrv);
  3257. if (ret) {
  3258. spcom_pr_err("can't register rpmsg_driver for %s\n", ch->name);
  3259. kfree(rpdrv);
  3260. kfree(match);
  3261. kfree(drv_name);
  3262. return ret;
  3263. }
  3264. mutex_lock(&ch->lock);
  3265. ch->rpdrv = rpdrv;
  3266. ch->rpmsg_abort = false;
  3267. mutex_unlock(&ch->lock);
  3268. return 0;
  3269. }
  3270. static int spcom_unregister_rpmsg_drv(struct spcom_channel *ch)
  3271. {
  3272. if (!ch->rpdrv) {
  3273. spcom_pr_err("rpdev is NULL, can't unregister rpmsg drv\n");
  3274. return -ENODEV;
  3275. }
  3276. unregister_rpmsg_driver(ch->rpdrv);
  3277. mutex_lock(&ch->lock);
  3278. kfree(ch->rpdrv->drv.name);
  3279. kfree((void *)ch->rpdrv->id_table);
  3280. kfree(ch->rpdrv);
  3281. ch->rpdrv = NULL;
  3282. ch->rpmsg_abort = true; /* will unblock spcom_rx() */
  3283. mutex_unlock(&ch->lock);
  3284. return 0;
  3285. }
  3286. static int spcom_probe(struct platform_device *pdev)
  3287. {
  3288. int ret;
  3289. struct spcom_device *dev = NULL;
  3290. struct device_node *np;
  3291. struct property *prop;
  3292. if (!pdev) {
  3293. pr_err("invalid pdev\n");
  3294. return -ENODEV;
  3295. }
  3296. np = pdev->dev.of_node;
  3297. if (!np) {
  3298. pr_err("invalid DT node\n");
  3299. return -EINVAL;
  3300. }
  3301. prop = of_find_property(np, "qcom,rproc-handle", NULL);
  3302. if (!prop) {
  3303. spcom_pr_err("can't find qcom,rproc-hable property");
  3304. return -EINVAL;
  3305. }
  3306. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  3307. if (dev == NULL)
  3308. return -ENOMEM;
  3309. spcom_dev = dev;
  3310. spcom_dev->pdev = pdev;
  3311. spcom_dev->rproc_prop = prop;
  3312. /* start counting exposed channel char devices from 1 */
  3313. spcom_dev->chdev_count = 1;
  3314. mutex_init(&spcom_dev->chdev_count_lock);
  3315. init_completion(&spcom_dev->rpmsg_state_change);
  3316. atomic_set(&spcom_dev->rpmsg_dev_count, 0);
  3317. atomic_set(&spcom_dev->remove_in_progress, 0);
  3318. INIT_LIST_HEAD(&spcom_dev->rx_list_head);
  3319. spin_lock_init(&spcom_dev->rx_lock);
  3320. spcom_dev->nvm_ion_fd = -1;
  3321. spcom_dev->rmb_error = 0;
  3322. mutex_init(&spcom_dev->ch_list_lock);
  3323. // register wakeup source
  3324. spcom_dev->ws =
  3325. wakeup_source_register(&spcom_dev->pdev->dev, "spcom_wakeup");
  3326. if (!spcom_dev->ws) {
  3327. pr_err("failed to register wakeup source\n");
  3328. ret = -ENOMEM;
  3329. goto fail_while_chardev_reg;
  3330. }
  3331. ret = spcom_register_chardev();
  3332. if (ret) {
  3333. pr_err("create character device failed\n");
  3334. goto fail_while_chardev_reg;
  3335. }
  3336. ret = spcom_parse_dt(np);
  3337. if (ret < 0)
  3338. goto fail_reg_chardev;
  3339. if (of_property_read_bool(np, "qcom,boot-enabled"))
  3340. atomic_set(&dev->subsys_req, 1);
  3341. ret = spcom_create_predefined_channels_chardev();
  3342. if (ret < 0) {
  3343. pr_err("create character device failed (%d)\n", ret);
  3344. goto fail_reg_chardev;
  3345. }
  3346. spcom_ipc_log_context = ipc_log_context_create(SPCOM_LOG_PAGE_CNT,
  3347. "spcom", 0);
  3348. if (!spcom_ipc_log_context)
  3349. pr_err("Unable to create IPC log context\n");
  3350. spcom_pr_info("Driver Initialization completed ok\n");
  3351. return 0;
  3352. fail_reg_chardev:
  3353. pr_err("failed to init driver\n");
  3354. spcom_unregister_chrdev();
  3355. fail_while_chardev_reg:
  3356. kfree(dev);
  3357. spcom_dev = NULL;
  3358. return -ENODEV;
  3359. }
  3360. static int spcom_remove(struct platform_device *pdev)
  3361. {
  3362. int ret;
  3363. struct rx_buff_list *rx_item;
  3364. unsigned long flags;
  3365. int i;
  3366. atomic_inc(&spcom_dev->remove_in_progress);
  3367. if (spcom_dev->spss_rproc) {
  3368. spcom_pr_info("shutdown spss\n");
  3369. rproc_shutdown(spcom_dev->spss_rproc);
  3370. spcom_dev->spss_rproc = NULL;
  3371. }
  3372. /* destroy existing channel char devices */
  3373. for (i = 0; i < SPCOM_MAX_CHANNELS; i++) {
  3374. const char *name = spcom_dev->channels[i].name;
  3375. if (name[0] == 0)
  3376. break;
  3377. ret = spcom_destroy_channel_chardev(name);
  3378. if (ret) {
  3379. spcom_pr_err("failed to destroy chardev [%s], ret [%d]\n",
  3380. name, ret);
  3381. return -EFAULT;
  3382. }
  3383. spcom_pr_dbg("destroyed channel %s", name);
  3384. }
  3385. /* destroy control char device */
  3386. spcom_unregister_chrdev();
  3387. /* release uncompleted rx */
  3388. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3389. while (!list_empty(&spcom_dev->rx_list_head)) {
  3390. /* detach last entry */
  3391. rx_item = list_last_entry(&spcom_dev->rx_list_head,
  3392. struct rx_buff_list, list);
  3393. list_del(&rx_item->list);
  3394. if (!rx_item) {
  3395. spcom_pr_err("empty entry in pending rx list\n");
  3396. spin_lock_irqsave(&spcom_dev->rx_lock, flags);
  3397. continue;
  3398. }
  3399. kfree(rx_item);
  3400. }
  3401. spin_unlock_irqrestore(&spcom_dev->rx_lock, flags);
  3402. wakeup_source_unregister(spcom_dev->ws);
  3403. if (spcom_ipc_log_context)
  3404. ipc_log_context_destroy(spcom_ipc_log_context);
  3405. /* free global device struct */
  3406. kfree(spcom_dev);
  3407. spcom_dev = NULL;
  3408. pr_info("successfully released all module resources\n");
  3409. return 0;
  3410. }
  3411. static void spcom_release_all_channels_of_process(u32 pid)
  3412. {
  3413. u32 i;
  3414. /* Iterate over all channels and release all channels that belong to
  3415. * the given process
  3416. */
  3417. for (i = 0; i < SPCOM_MAX_CHANNELS; i++) {
  3418. struct spcom_channel *ch = &spcom_dev->channels[i];
  3419. if (ch->name[0] != '\0') {
  3420. u32 j;
  3421. /* Check if the given process is a client of the current channel, and
  3422. * if so release the channel
  3423. */
  3424. for (j = 0; j < SPCOM_MAX_CHANNEL_CLIENTS; j++) {
  3425. if (ch->pid[j] == pid) {
  3426. mutex_lock(&ch->lock);
  3427. spcom_channel_deinit_locked(ch, pid);
  3428. mutex_unlock(&ch->lock);
  3429. break;
  3430. }
  3431. }
  3432. }
  3433. }
  3434. }
  3435. static int spom_control_channel_add_client(u32 pid)
  3436. {
  3437. u32 i;
  3438. int free_index;
  3439. struct spcom_control_channel_info *ch_info;
  3440. mutex_lock(&spcom_dev->ch_list_lock);
  3441. for (i = 0, free_index = -1; i < SPCOM_MAX_CONTROL_CHANNELS; i++) {
  3442. ch_info = &spcom_dev->control_channels[i];
  3443. /* A process may open only a single control channel */
  3444. if (ch_info->pid == pid) {
  3445. ch_info->ref_cnt++;
  3446. spcom_pr_dbg("Control channel for pid %u already exists, ref_cnt=%u\n",
  3447. pid, ch_info->ref_cnt);
  3448. mutex_unlock(&spcom_dev->ch_list_lock);
  3449. return 0;
  3450. }
  3451. /* Remember the first free entry */
  3452. if (free_index < 0 && ch_info->pid == 0)
  3453. free_index = i;
  3454. }
  3455. /* If no free entry was found then the control channel can't be opened */
  3456. if (free_index < 0) {
  3457. mutex_unlock(&spcom_dev->ch_list_lock);
  3458. spcom_pr_err("Too many open control channels\n");
  3459. return -EMFILE;
  3460. }
  3461. /* Add the process opening the control channel in the free entry */
  3462. ch_info = &spcom_dev->control_channels[free_index];
  3463. ch_info->pid = pid;
  3464. ch_info->ref_cnt = 1;
  3465. spcom_pr_dbg("Add pid %u at index %u\n", pid, free_index);
  3466. mutex_unlock(&spcom_dev->ch_list_lock);
  3467. return 0;
  3468. }
  3469. static int spom_control_channel_remove_client(u32 pid)
  3470. {
  3471. u32 i;
  3472. int ret = -ESRCH;
  3473. mutex_lock(&spcom_dev->ch_list_lock);
  3474. for (i = 0; i < SPCOM_MAX_CONTROL_CHANNELS; i++) {
  3475. struct spcom_control_channel_info *ch_info = &spcom_dev->control_channels[i];
  3476. /* When a process closes the control channel we release all its channels
  3477. * to allow re-registration if another instance of the process will be created
  3478. */
  3479. if (ch_info->pid == pid) {
  3480. ch_info->ref_cnt--;
  3481. spcom_pr_dbg("Remove pid %u from index %u, ref_cnt=%u\n",
  3482. pid, i, ch_info->ref_cnt);
  3483. if (ch_info->ref_cnt == 0) {
  3484. ch_info->pid = 0;
  3485. spcom_release_all_channels_of_process(pid);
  3486. }
  3487. ret = 0;
  3488. break;
  3489. }
  3490. }
  3491. mutex_unlock(&spcom_dev->ch_list_lock);
  3492. return ret;
  3493. }
  3494. static const struct of_device_id spcom_match_table[] = {
  3495. { .compatible = "qcom,spcom", },
  3496. { },
  3497. };
  3498. MODULE_DEVICE_TABLE(of, spcom_match_table);
  3499. static struct platform_driver spcom_driver = {
  3500. .probe = spcom_probe,
  3501. .remove = spcom_remove,
  3502. .driver = {
  3503. .name = DEVICE_NAME,
  3504. .of_match_table = of_match_ptr(spcom_match_table),
  3505. },
  3506. };
  3507. module_platform_driver(spcom_driver);
  3508. MODULE_SOFTDEP("pre: spss_utils");
  3509. MODULE_IMPORT_NS(DMA_BUF);
  3510. MODULE_LICENSE("GPL v2");
  3511. MODULE_DESCRIPTION("Secure Processor Communication");