sps.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2019, 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /* Smart-Peripheral-Switch (SPS) Module. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/mutex.h>
  12. #include <linux/device.h>
  13. #include <linux/fs.h>
  14. #include <linux/list.h>
  15. #include <linux/memory.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include "sps_bam.h"
  24. #include "spsi.h"
  25. #include "sps_core.h"
  26. #define SPS_DRV_NAME "msm_sps" /* must match the platform_device name */
  27. /**
  28. * SPS driver state
  29. */
  30. struct sps_drv *sps;
  31. u32 d_type;
  32. bool enhd_pipe;
  33. bool imem;
  34. enum sps_bam_type bam_type;
  35. static enum sps_bam_type bam_types[] = {
  36. SPS_BAM_LEGACY, SPS_BAM_NDP, SPS_BAM_NDP_4K};
  37. static void sps_device_de_init(void);
  38. #ifdef CONFIG_DEBUG_FS
  39. u8 debugfs_record_enabled;
  40. u8 logging_option;
  41. u8 debug_level_option;
  42. u8 print_limit_option;
  43. static u8 reg_dump_option;
  44. static u32 testbus_sel;
  45. static u32 bam_pipe_sel;
  46. static u32 desc_option;
  47. /*
  48. * Specifies range of log level from level 0 to level 3 to have fine-granularity
  49. * for logging to serve all BAM use cases.
  50. */
  51. static u32 log_level_sel;
  52. static char *debugfs_buf;
  53. static u32 debugfs_buf_size;
  54. static u32 debugfs_buf_used;
  55. static int wraparound;
  56. static struct mutex sps_debugfs_lock;
  57. static struct dentry *dent;
  58. static struct dentry *dfile_info;
  59. static struct dentry *dfile_logging_option;
  60. static struct dentry *dfile_bam_addr;
  61. static struct sps_bam *phy2bam(phys_addr_t phys_addr);
  62. /* record debug info for debugfs */
  63. void sps_debugfs_record(const char *msg)
  64. {
  65. mutex_lock(&sps_debugfs_lock);
  66. if (debugfs_record_enabled) {
  67. if (debugfs_buf_used + MAX_MSG_LEN >= debugfs_buf_size) {
  68. debugfs_buf_used = 0;
  69. wraparound = true;
  70. }
  71. debugfs_buf_used += scnprintf(debugfs_buf + debugfs_buf_used,
  72. debugfs_buf_size - debugfs_buf_used, msg);
  73. if (wraparound)
  74. scnprintf(debugfs_buf + debugfs_buf_used,
  75. debugfs_buf_size - debugfs_buf_used,
  76. "\n**** end line of sps log ****\n\n");
  77. }
  78. mutex_unlock(&sps_debugfs_lock);
  79. }
  80. /* read the recorded debug info to userspace */
  81. static ssize_t sps_read_info(struct file *file, char __user *ubuf,
  82. size_t count, loff_t *ppos)
  83. {
  84. int ret = 0;
  85. int size;
  86. mutex_lock(&sps_debugfs_lock);
  87. if (debugfs_record_enabled) {
  88. if (wraparound)
  89. size = debugfs_buf_size - MAX_MSG_LEN;
  90. else
  91. size = debugfs_buf_used;
  92. ret = simple_read_from_buffer(ubuf, count, ppos,
  93. debugfs_buf, size);
  94. }
  95. mutex_unlock(&sps_debugfs_lock);
  96. return ret;
  97. }
  98. /*
  99. * set the buffer size (in KB) for debug info
  100. */
  101. static ssize_t sps_set_info(struct file *file, const char __user *buf,
  102. size_t count, loff_t *ppos)
  103. {
  104. unsigned long missing;
  105. char str[MAX_MSG_LEN];
  106. int i;
  107. u32 buf_size_kb = 0;
  108. u32 new_buf_size;
  109. u32 size = sizeof(str) < count ? sizeof(str) : count;
  110. memset(str, 0, sizeof(str));
  111. missing = copy_from_user(str, buf, size);
  112. if (missing)
  113. return -EFAULT;
  114. for (i = 0; i < sizeof(str) && (str[i] >= '0') && (str[i] <= '9'); ++i)
  115. buf_size_kb = (buf_size_kb * 10) + (str[i] - '0');
  116. pr_info("sps:debugfs: input buffer size is %dKB\n", buf_size_kb);
  117. if ((logging_option == 0) || (logging_option == 2)) {
  118. pr_info("sps:debugfs: need to first turn on recording\n");
  119. return -EFAULT;
  120. }
  121. if (buf_size_kb < 1) {
  122. pr_info("sps:debugfs:buffer size should be no less than 1KB\n");
  123. return -EFAULT;
  124. }
  125. if (buf_size_kb > (INT_MAX/SZ_1K)) {
  126. pr_err("sps:debugfs: buffer size is too large\n");
  127. return -EFAULT;
  128. }
  129. new_buf_size = buf_size_kb * SZ_1K;
  130. mutex_lock(&sps_debugfs_lock);
  131. if (debugfs_record_enabled) {
  132. if (debugfs_buf_size == new_buf_size) {
  133. /* need do nothing */
  134. pr_info(
  135. "sps:debugfs: input buffer size is the same as before\n"
  136. );
  137. mutex_unlock(&sps_debugfs_lock);
  138. return count;
  139. }
  140. /* release the current buffer */
  141. debugfs_record_enabled = false;
  142. debugfs_buf_used = 0;
  143. wraparound = false;
  144. kfree(debugfs_buf);
  145. debugfs_buf = NULL;
  146. }
  147. /* allocate new buffer */
  148. debugfs_buf_size = new_buf_size;
  149. debugfs_buf = kzalloc(debugfs_buf_size, GFP_KERNEL);
  150. if (!debugfs_buf) {
  151. debugfs_buf_size = 0;
  152. mutex_unlock(&sps_debugfs_lock);
  153. return -ENOMEM;
  154. }
  155. debugfs_buf_used = 0;
  156. wraparound = false;
  157. debugfs_record_enabled = true;
  158. mutex_unlock(&sps_debugfs_lock);
  159. return count;
  160. }
  161. static const struct file_operations sps_info_ops = {
  162. .read = sps_read_info,
  163. .write = sps_set_info,
  164. };
  165. /* return the current logging option to userspace */
  166. static ssize_t sps_read_logging_option(struct file *file, char __user *ubuf,
  167. size_t count, loff_t *ppos)
  168. {
  169. char value[MAX_MSG_LEN];
  170. int nbytes;
  171. nbytes = scnprintf(value, MAX_MSG_LEN, "%d\n", logging_option);
  172. return simple_read_from_buffer(ubuf, count, ppos, value, nbytes);
  173. }
  174. /*
  175. * set the logging option
  176. */
  177. static ssize_t sps_set_logging_option(struct file *file, const char __user *buf,
  178. size_t count, loff_t *ppos)
  179. {
  180. int ret;
  181. u32 option = 0;
  182. ret = kstrtouint_from_user(buf, count, 10, &option);
  183. if (ret)
  184. return ret;
  185. pr_info("sps:debugfs: try to change logging option to %d\n", option);
  186. if (option > 3) {
  187. pr_err("sps:debugfs: invalid logging option:%d\n", option);
  188. return count;
  189. }
  190. mutex_lock(&sps_debugfs_lock);
  191. if (((option == 0) || (option == 2)) &&
  192. ((logging_option == 1) || (logging_option == 3))) {
  193. debugfs_record_enabled = false;
  194. kfree(debugfs_buf);
  195. debugfs_buf = NULL;
  196. debugfs_buf_used = 0;
  197. debugfs_buf_size = 0;
  198. wraparound = false;
  199. }
  200. logging_option = option;
  201. mutex_unlock(&sps_debugfs_lock);
  202. return count;
  203. }
  204. static const struct file_operations sps_logging_option_ops = {
  205. .read = sps_read_logging_option,
  206. .write = sps_set_logging_option,
  207. };
  208. /*
  209. * input the bam physical address
  210. */
  211. static ssize_t sps_set_bam_addr(struct file *file, const char __user *buf,
  212. size_t count, loff_t *ppos)
  213. {
  214. u32 ret;
  215. int i;
  216. u32 bam_addr = 0;
  217. struct sps_bam *bam;
  218. u32 num_pipes = 0;
  219. void *vir_addr;
  220. ret = kstrtouint_from_user(buf, count, 10, &bam_addr);
  221. if (ret)
  222. return ret;
  223. pr_info("sps:debugfs:input BAM physical address:0x%x\n", bam_addr);
  224. bam = phy2bam(bam_addr);
  225. if (bam == NULL) {
  226. pr_err("sps:debugfs:BAM 0x%x is not registered\n", bam_addr);
  227. return count;
  228. }
  229. vir_addr = &bam->base;
  230. num_pipes = bam->props.num_pipes;
  231. if (log_level_sel <= SPS_IPC_MAX_LOGLEVEL)
  232. bam->ipc_loglevel = log_level_sel;
  233. switch (reg_dump_option) {
  234. case 1: /* output all registers of this BAM */
  235. print_bam_reg(bam->base);
  236. for (i = 0; i < num_pipes; i++)
  237. print_bam_pipe_reg(bam->base, i);
  238. break;
  239. case 2: /* output BAM-level registers */
  240. print_bam_reg(bam->base);
  241. break;
  242. case 3: /* output selected BAM-level registers */
  243. print_bam_selected_reg(vir_addr, bam->props.ee);
  244. break;
  245. case 4: /* output selected registers of all pipes */
  246. for (i = 0; i < num_pipes; i++)
  247. print_bam_pipe_selected_reg(vir_addr, i);
  248. break;
  249. case 5: /* output selected registers of selected pipes */
  250. for (i = 0; i < num_pipes; i++)
  251. if (bam_pipe_sel & (1UL << i))
  252. print_bam_pipe_selected_reg(vir_addr, i);
  253. break;
  254. case 6: /* output selected registers of typical pipes */
  255. print_bam_pipe_selected_reg(vir_addr, 4);
  256. print_bam_pipe_selected_reg(vir_addr, 5);
  257. break;
  258. case 7: /* output desc FIFO of all pipes */
  259. for (i = 0; i < num_pipes; i++)
  260. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  261. break;
  262. case 8: /* output desc FIFO of selected pipes */
  263. for (i = 0; i < num_pipes; i++)
  264. if (bam_pipe_sel & (1UL << i))
  265. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  266. break;
  267. case 9: /* output desc FIFO of typical pipes */
  268. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  269. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  270. break;
  271. case 10: /* output selected registers and desc FIFO of all pipes */
  272. for (i = 0; i < num_pipes; i++) {
  273. print_bam_pipe_selected_reg(vir_addr, i);
  274. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  275. }
  276. break;
  277. case 11: /* output selected registers and desc FIFO of selected pipes */
  278. for (i = 0; i < num_pipes; i++)
  279. if (bam_pipe_sel & (1UL << i)) {
  280. print_bam_pipe_selected_reg(vir_addr, i);
  281. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  282. }
  283. break;
  284. case 12: /* output selected registers and desc FIFO of typical pipes */
  285. print_bam_pipe_selected_reg(vir_addr, 4);
  286. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  287. print_bam_pipe_selected_reg(vir_addr, 5);
  288. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  289. break;
  290. case 13: /* output BAM_TEST_BUS_REG */
  291. if (testbus_sel)
  292. print_bam_test_bus_reg(vir_addr, testbus_sel);
  293. else {
  294. pr_info("sps:output TEST_BUS_REG for all TEST_BUS_SEL\n");
  295. print_bam_test_bus_reg(vir_addr, testbus_sel);
  296. }
  297. break;
  298. case 14: /* output partial desc FIFO of selected pipes */
  299. if (desc_option == 0)
  300. desc_option = 1;
  301. for (i = 0; i < num_pipes; i++)
  302. if (bam_pipe_sel & (1UL << i))
  303. print_bam_pipe_desc_fifo(vir_addr, i,
  304. desc_option);
  305. break;
  306. case 15: /* output partial data blocks of descriptors */
  307. for (i = 0; i < num_pipes; i++)
  308. if (bam_pipe_sel & (1UL << i))
  309. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  310. break;
  311. case 16: /* output all registers of selected pipes */
  312. for (i = 0; i < num_pipes; i++)
  313. if (bam_pipe_sel & (1UL << i))
  314. print_bam_pipe_reg(bam->base, i);
  315. break;
  316. case 91: /*
  317. * output testbus register, BAM global regisers
  318. * and registers of all pipes
  319. */
  320. print_bam_test_bus_reg(vir_addr, testbus_sel);
  321. print_bam_selected_reg(vir_addr, bam->props.ee);
  322. for (i = 0; i < num_pipes; i++)
  323. print_bam_pipe_selected_reg(vir_addr, i);
  324. break;
  325. case 92: /*
  326. * output testbus register, BAM global regisers
  327. * and registers of selected pipes
  328. */
  329. print_bam_test_bus_reg(vir_addr, testbus_sel);
  330. print_bam_selected_reg(vir_addr, bam->props.ee);
  331. for (i = 0; i < num_pipes; i++)
  332. if (bam_pipe_sel & (1UL << i))
  333. print_bam_pipe_selected_reg(vir_addr, i);
  334. break;
  335. case 93: /*
  336. * output registers and partial desc FIFOs
  337. * of selected pipes: format 1
  338. */
  339. if (desc_option == 0)
  340. desc_option = 1;
  341. print_bam_test_bus_reg(vir_addr, testbus_sel);
  342. print_bam_selected_reg(vir_addr, bam->props.ee);
  343. for (i = 0; i < num_pipes; i++)
  344. if (bam_pipe_sel & (1UL << i))
  345. print_bam_pipe_selected_reg(vir_addr, i);
  346. for (i = 0; i < num_pipes; i++)
  347. if (bam_pipe_sel & (1UL << i))
  348. print_bam_pipe_desc_fifo(vir_addr, i,
  349. desc_option);
  350. break;
  351. case 94: /*
  352. * output registers and partial desc FIFOs
  353. * of selected pipes: format 2
  354. */
  355. if (desc_option == 0)
  356. desc_option = 1;
  357. print_bam_test_bus_reg(vir_addr, testbus_sel);
  358. print_bam_selected_reg(vir_addr, bam->props.ee);
  359. for (i = 0; i < num_pipes; i++)
  360. if (bam_pipe_sel & (1UL << i)) {
  361. print_bam_pipe_selected_reg(vir_addr, i);
  362. print_bam_pipe_desc_fifo(vir_addr, i,
  363. desc_option);
  364. }
  365. break;
  366. case 95: /*
  367. * output registers and desc FIFOs
  368. * of selected pipes: format 1
  369. */
  370. print_bam_test_bus_reg(vir_addr, testbus_sel);
  371. print_bam_selected_reg(vir_addr, bam->props.ee);
  372. for (i = 0; i < num_pipes; i++)
  373. if (bam_pipe_sel & (1UL << i))
  374. print_bam_pipe_selected_reg(vir_addr, i);
  375. for (i = 0; i < num_pipes; i++)
  376. if (bam_pipe_sel & (1UL << i))
  377. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  378. break;
  379. case 96: /*
  380. * output registers and desc FIFOs
  381. * of selected pipes: format 2
  382. */
  383. print_bam_test_bus_reg(vir_addr, testbus_sel);
  384. print_bam_selected_reg(vir_addr, bam->props.ee);
  385. for (i = 0; i < num_pipes; i++)
  386. if (bam_pipe_sel & (1UL << i)) {
  387. print_bam_pipe_selected_reg(vir_addr, i);
  388. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  389. }
  390. break;
  391. case 97: /*
  392. * output registers, desc FIFOs and partial data blocks
  393. * of selected pipes: format 1
  394. */
  395. print_bam_test_bus_reg(vir_addr, testbus_sel);
  396. print_bam_selected_reg(vir_addr, bam->props.ee);
  397. for (i = 0; i < num_pipes; i++)
  398. if (bam_pipe_sel & (1UL << i))
  399. print_bam_pipe_selected_reg(vir_addr, i);
  400. for (i = 0; i < num_pipes; i++)
  401. if (bam_pipe_sel & (1UL << i))
  402. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  403. for (i = 0; i < num_pipes; i++)
  404. if (bam_pipe_sel & (1UL << i))
  405. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  406. break;
  407. case 98: /*
  408. * output registers, desc FIFOs and partial data blocks
  409. * of selected pipes: format 2
  410. */
  411. print_bam_test_bus_reg(vir_addr, testbus_sel);
  412. print_bam_selected_reg(vir_addr, bam->props.ee);
  413. for (i = 0; i < num_pipes; i++)
  414. if (bam_pipe_sel & (1UL << i)) {
  415. print_bam_pipe_selected_reg(vir_addr, i);
  416. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  417. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  418. }
  419. break;
  420. case 99: /* output all registers, desc FIFOs and partial data blocks */
  421. print_bam_test_bus_reg(vir_addr, testbus_sel);
  422. print_bam_reg(bam->base);
  423. for (i = 0; i < num_pipes; i++)
  424. print_bam_pipe_reg(bam->base, i);
  425. print_bam_selected_reg(vir_addr, bam->props.ee);
  426. for (i = 0; i < num_pipes; i++)
  427. print_bam_pipe_selected_reg(vir_addr, i);
  428. for (i = 0; i < num_pipes; i++)
  429. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  430. for (i = 0; i < num_pipes; i++)
  431. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  432. break;
  433. default:
  434. pr_info("sps:no dump option is chosen yet\n");
  435. }
  436. return count;
  437. }
  438. static const struct file_operations sps_bam_addr_ops = {
  439. .write = sps_set_bam_addr,
  440. };
  441. static void sps_debugfs_init(void)
  442. {
  443. debugfs_record_enabled = false;
  444. logging_option = 0;
  445. debug_level_option = 0;
  446. print_limit_option = 0;
  447. reg_dump_option = 0;
  448. testbus_sel = 0;
  449. bam_pipe_sel = 0;
  450. desc_option = 0;
  451. debugfs_buf_size = 0;
  452. debugfs_buf_used = 0;
  453. wraparound = false;
  454. log_level_sel = SPS_IPC_MAX_LOGLEVEL + 1;
  455. dent = debugfs_create_dir("sps", NULL);
  456. if (IS_ERR(dent)) {
  457. pr_err("sps:fail to create the folder for debug_fs\n");
  458. return;
  459. }
  460. dfile_info = debugfs_create_file("info", 0664, dent, NULL,
  461. &sps_info_ops);
  462. if (!dfile_info || IS_ERR(dfile_info)) {
  463. pr_err("sps:fail to create the file for debug_fs info\n");
  464. goto cleanup;
  465. }
  466. dfile_logging_option = debugfs_create_file("logging_option", 0664,
  467. dent, NULL, &sps_logging_option_ops);
  468. if (!dfile_logging_option || IS_ERR(dfile_logging_option)) {
  469. pr_err("sps:fail to create debug_fs for logging_option\n");
  470. goto cleanup;
  471. }
  472. debugfs_create_u8("debug_level_option",
  473. 0664, dent, &debug_level_option);
  474. debugfs_create_u8("print_limit_option",
  475. 0664, dent, &print_limit_option);
  476. debugfs_create_u8("reg_dump_option", 0664, dent, &reg_dump_option);
  477. debugfs_create_u32("testbus_sel", 0664, dent, &testbus_sel);
  478. debugfs_create_u32("bam_pipe_sel", 0664, dent, &bam_pipe_sel);
  479. debugfs_create_u32("desc_option", 0664, dent, &desc_option);
  480. dfile_bam_addr = debugfs_create_file("bam_addr", 0664,
  481. dent, NULL, &sps_bam_addr_ops);
  482. if (!dfile_bam_addr || IS_ERR(dfile_bam_addr)) {
  483. pr_err("sps:fail to create the file for debug_fs bam_addr\n");
  484. goto cleanup;
  485. }
  486. debugfs_create_u32("log_level_sel", 0664, dent, &log_level_sel);
  487. mutex_init(&sps_debugfs_lock);
  488. return;
  489. cleanup:
  490. debugfs_remove_recursive(dent);
  491. }
  492. static void sps_debugfs_exit(void)
  493. {
  494. debugfs_remove_recursive(dent);
  495. kfree(debugfs_buf);
  496. debugfs_buf = NULL;
  497. }
  498. #endif
  499. /* Get the debug info of BAM registers and descriptor FIFOs */
  500. int sps_get_bam_debug_info(unsigned long dev, u32 option, u32 para,
  501. u32 tb_sel, u32 desc_sel)
  502. {
  503. int res = 0;
  504. struct sps_bam *bam;
  505. u32 i;
  506. u32 num_pipes = 0;
  507. void *vir_addr;
  508. if (dev == 0) {
  509. SPS_ERR(sps, "sps: device handle should not be 0\n");
  510. return SPS_ERROR;
  511. }
  512. if (sps == NULL || !sps->is_ready) {
  513. SPS_DBG3(sps, "sps: sps driver is not ready\n");
  514. return -EPROBE_DEFER;
  515. }
  516. mutex_lock(&sps->lock);
  517. /* Search for the target BAM device */
  518. bam = sps_h2bam(dev);
  519. if (bam == NULL) {
  520. pr_err("sps:Can't find any BAM with handle 0x%pK\n",
  521. (void *)dev);
  522. mutex_unlock(&sps->lock);
  523. return SPS_ERROR;
  524. }
  525. mutex_unlock(&sps->lock);
  526. vir_addr = &bam->base;
  527. num_pipes = bam->props.num_pipes;
  528. SPS_DUMP("sps:<bam-addr> dump BAM:%pa\n", &bam->props.phys_addr);
  529. switch (option) {
  530. case 1: /* output all registers of this BAM */
  531. print_bam_reg(bam->base);
  532. for (i = 0; i < num_pipes; i++)
  533. print_bam_pipe_reg(bam->base, i);
  534. break;
  535. case 2: /* output BAM-level registers */
  536. print_bam_reg(bam->base);
  537. break;
  538. case 3: /* output selected BAM-level registers */
  539. print_bam_selected_reg(vir_addr, bam->props.ee);
  540. break;
  541. case 4: /* output selected registers of all pipes */
  542. for (i = 0; i < num_pipes; i++)
  543. print_bam_pipe_selected_reg(vir_addr, i);
  544. break;
  545. case 5: /* output selected registers of selected pipes */
  546. for (i = 0; i < num_pipes; i++)
  547. if (para & (1UL << i))
  548. print_bam_pipe_selected_reg(vir_addr, i);
  549. break;
  550. case 6: /* output selected registers of typical pipes */
  551. print_bam_pipe_selected_reg(vir_addr, 4);
  552. print_bam_pipe_selected_reg(vir_addr, 5);
  553. break;
  554. case 7: /* output desc FIFO of all pipes */
  555. for (i = 0; i < num_pipes; i++)
  556. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  557. break;
  558. case 8: /* output desc FIFO of selected pipes */
  559. for (i = 0; i < num_pipes; i++)
  560. if (para & (1UL << i))
  561. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  562. break;
  563. case 9: /* output desc FIFO of typical pipes */
  564. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  565. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  566. break;
  567. case 10: /* output selected registers and desc FIFO of all pipes */
  568. for (i = 0; i < num_pipes; i++) {
  569. print_bam_pipe_selected_reg(vir_addr, i);
  570. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  571. }
  572. break;
  573. case 11: /* output selected registers and desc FIFO of selected pipes */
  574. for (i = 0; i < num_pipes; i++)
  575. if (para & (1UL << i)) {
  576. print_bam_pipe_selected_reg(vir_addr, i);
  577. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  578. }
  579. break;
  580. case 12: /* output selected registers and desc FIFO of typical pipes */
  581. print_bam_pipe_selected_reg(vir_addr, 4);
  582. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  583. print_bam_pipe_selected_reg(vir_addr, 5);
  584. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  585. break;
  586. case 13: /* output BAM_TEST_BUS_REG */
  587. if (tb_sel)
  588. print_bam_test_bus_reg(vir_addr, tb_sel);
  589. else
  590. pr_info("sps:TEST_BUS_SEL should NOT be zero\n");
  591. break;
  592. case 14: /* output partial desc FIFO of selected pipes */
  593. if (desc_sel == 0)
  594. desc_sel = 1;
  595. for (i = 0; i < num_pipes; i++)
  596. if (para & (1UL << i))
  597. print_bam_pipe_desc_fifo(vir_addr, i,
  598. desc_sel);
  599. break;
  600. case 15: /* output partial data blocks of descriptors */
  601. for (i = 0; i < num_pipes; i++)
  602. if (para & (1UL << i))
  603. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  604. break;
  605. case 16: /* output all registers of selected pipes */
  606. for (i = 0; i < num_pipes; i++)
  607. if (para & (1UL << i))
  608. print_bam_pipe_reg(bam->base, i);
  609. break;
  610. case 91: /*
  611. * output testbus register, BAM global regisers
  612. * and registers of all pipes
  613. */
  614. print_bam_test_bus_reg(vir_addr, tb_sel);
  615. print_bam_selected_reg(vir_addr, bam->props.ee);
  616. for (i = 0; i < num_pipes; i++)
  617. print_bam_pipe_selected_reg(vir_addr, i);
  618. break;
  619. case 92: /*
  620. * output testbus register, BAM global regisers
  621. * and registers of selected pipes
  622. */
  623. print_bam_test_bus_reg(vir_addr, tb_sel);
  624. print_bam_selected_reg(vir_addr, bam->props.ee);
  625. for (i = 0; i < num_pipes; i++)
  626. if (para & (1UL << i))
  627. print_bam_pipe_selected_reg(vir_addr, i);
  628. break;
  629. case 93: /*
  630. * output registers and partial desc FIFOs
  631. * of selected pipes: format 1
  632. */
  633. if (desc_sel == 0)
  634. desc_sel = 1;
  635. print_bam_test_bus_reg(vir_addr, tb_sel);
  636. print_bam_selected_reg(vir_addr, bam->props.ee);
  637. for (i = 0; i < num_pipes; i++)
  638. if (para & (1UL << i))
  639. print_bam_pipe_selected_reg(vir_addr, i);
  640. for (i = 0; i < num_pipes; i++)
  641. if (para & (1UL << i))
  642. print_bam_pipe_desc_fifo(vir_addr, i,
  643. desc_sel);
  644. break;
  645. case 94: /*
  646. * output registers and partial desc FIFOs
  647. * of selected pipes: format 2
  648. */
  649. if (desc_sel == 0)
  650. desc_sel = 1;
  651. print_bam_test_bus_reg(vir_addr, tb_sel);
  652. print_bam_selected_reg(vir_addr, bam->props.ee);
  653. for (i = 0; i < num_pipes; i++)
  654. if (para & (1UL << i)) {
  655. print_bam_pipe_selected_reg(vir_addr, i);
  656. print_bam_pipe_desc_fifo(vir_addr, i,
  657. desc_sel);
  658. }
  659. break;
  660. case 95: /*
  661. * output registers and desc FIFOs
  662. * of selected pipes: format 1
  663. */
  664. print_bam_test_bus_reg(vir_addr, tb_sel);
  665. print_bam_selected_reg(vir_addr, bam->props.ee);
  666. for (i = 0; i < num_pipes; i++)
  667. if (para & (1UL << i))
  668. print_bam_pipe_selected_reg(vir_addr, i);
  669. for (i = 0; i < num_pipes; i++)
  670. if (para & (1UL << i))
  671. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  672. break;
  673. case 96: /*
  674. * output registers and desc FIFOs
  675. * of selected pipes: format 2
  676. */
  677. print_bam_test_bus_reg(vir_addr, tb_sel);
  678. print_bam_selected_reg(vir_addr, bam->props.ee);
  679. for (i = 0; i < num_pipes; i++)
  680. if (para & (1UL << i)) {
  681. print_bam_pipe_selected_reg(vir_addr, i);
  682. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  683. }
  684. break;
  685. case 97: /*
  686. * output registers, desc FIFOs and partial data blocks
  687. * of selected pipes: format 1
  688. */
  689. print_bam_test_bus_reg(vir_addr, tb_sel);
  690. print_bam_selected_reg(vir_addr, bam->props.ee);
  691. for (i = 0; i < num_pipes; i++)
  692. if (para & (1UL << i))
  693. print_bam_pipe_selected_reg(vir_addr, i);
  694. for (i = 0; i < num_pipes; i++)
  695. if (para & (1UL << i))
  696. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  697. for (i = 0; i < num_pipes; i++)
  698. if (para & (1UL << i))
  699. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  700. break;
  701. case 98: /*
  702. * output registers, desc FIFOs and partial data blocks
  703. * of selected pipes: format 2
  704. */
  705. print_bam_test_bus_reg(vir_addr, tb_sel);
  706. print_bam_selected_reg(vir_addr, bam->props.ee);
  707. for (i = 0; i < num_pipes; i++)
  708. if (para & (1UL << i)) {
  709. print_bam_pipe_selected_reg(vir_addr, i);
  710. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  711. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  712. }
  713. break;
  714. case 99: /* output all registers, desc FIFOs and partial data blocks */
  715. print_bam_test_bus_reg(vir_addr, tb_sel);
  716. print_bam_reg(bam->base);
  717. for (i = 0; i < num_pipes; i++)
  718. print_bam_pipe_reg(bam->base, i);
  719. print_bam_selected_reg(vir_addr, bam->props.ee);
  720. for (i = 0; i < num_pipes; i++)
  721. print_bam_pipe_selected_reg(vir_addr, i);
  722. for (i = 0; i < num_pipes; i++)
  723. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  724. for (i = 0; i < num_pipes; i++)
  725. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  726. break;
  727. default:
  728. pr_info("sps:no option is chosen yet\n");
  729. }
  730. return res;
  731. }
  732. EXPORT_SYMBOL(sps_get_bam_debug_info);
  733. /**
  734. * Initialize SPS device
  735. *
  736. * This function initializes the SPS device.
  737. *
  738. * @return 0 on success, negative value on error
  739. *
  740. */
  741. static int sps_device_init(void)
  742. {
  743. int result;
  744. int success;
  745. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  746. struct sps_bam_props bamdma_props = {0};
  747. #endif
  748. SPS_DBG3(sps, "Enter\n");
  749. success = false;
  750. result = sps_mem_init(sps->pipemem_phys_base, sps->pipemem_size);
  751. if (result) {
  752. SPS_ERR(sps, "sps: SPS memory init failed\n");
  753. goto exit_err;
  754. }
  755. INIT_LIST_HEAD(&sps->bams_q);
  756. mutex_init(&sps->lock);
  757. if (sps_rm_init(&sps->connection_ctrl, sps->options)) {
  758. SPS_ERR(sps, "sps: Fail to init SPS resource manager\n");
  759. goto exit_err;
  760. }
  761. result = sps_bam_driver_init(sps->options);
  762. if (result) {
  763. SPS_ERR(sps, "sps: SPS BAM driver init failed\n");
  764. goto exit_err;
  765. }
  766. /* Initialize the BAM DMA device */
  767. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  768. bamdma_props.phys_addr = sps->bamdma_bam_phys_base;
  769. bamdma_props.virt_addr = ioremap(sps->bamdma_bam_phys_base,
  770. sps->bamdma_bam_size);
  771. if (!bamdma_props.virt_addr) {
  772. SPS_ERR(sps, "sps: Fail to IO map BAM-DMA BAM registers\n");
  773. goto exit_err;
  774. }
  775. SPS_DBG3(sps, "sps:bamdma_bam.phys=%pa.virt=0x%pK\n",
  776. &bamdma_props.phys_addr,
  777. bamdma_props.virt_addr);
  778. bamdma_props.periph_phys_addr = sps->bamdma_dma_phys_base;
  779. bamdma_props.periph_virt_size = sps->bamdma_dma_size;
  780. bamdma_props.periph_virt_addr = ioremap(sps->bamdma_dma_phys_base,
  781. sps->bamdma_dma_size);
  782. if (!bamdma_props.periph_virt_addr) {
  783. SPS_ERR(sps, "sps: Fail to IO map BAM-DMA peripheral reg\n");
  784. goto exit_err;
  785. }
  786. SPS_DBG3(sps, "sps:bamdma_dma.phys=%pa.virt=0x%pK\n",
  787. &bamdma_props.periph_phys_addr,
  788. bamdma_props.periph_virt_addr);
  789. bamdma_props.irq = sps->bamdma_irq;
  790. bamdma_props.event_threshold = 0x10; /* Pipe event threshold */
  791. bamdma_props.summing_threshold = 0x10; /* BAM event threshold */
  792. bamdma_props.options = SPS_BAM_OPT_BAMDMA;
  793. bamdma_props.restricted_pipes = sps->bamdma_restricted_pipes;
  794. result = sps_dma_init(&bamdma_props);
  795. if (result) {
  796. SPS_ERR(sps, "sps: SPS BAM DMA driver init failed\n");
  797. goto exit_err;
  798. }
  799. #endif /* CONFIG_SPS_SUPPORT_BAMDMA */
  800. result = sps_map_init(NULL, sps->options);
  801. if (result) {
  802. SPS_ERR(sps,
  803. "sps: SPS connection mapping init failed\n");
  804. goto exit_err;
  805. }
  806. success = true;
  807. exit_err:
  808. if (!success) {
  809. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  810. sps_device_de_init();
  811. #endif
  812. return SPS_ERROR;
  813. }
  814. return 0;
  815. }
  816. /**
  817. * De-initialize SPS device
  818. *
  819. * This function de-initializes the SPS device.
  820. *
  821. * @return 0 on success, negative value on error
  822. *
  823. */
  824. static void sps_device_de_init(void)
  825. {
  826. SPS_DBG3(sps, "Enter\n");
  827. if (sps != NULL) {
  828. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  829. sps_dma_de_init();
  830. #endif
  831. /* Are there any remaining BAM registrations? */
  832. if (!list_empty(&sps->bams_q))
  833. SPS_ERR(sps,
  834. "sps: BAMs are still registered\n");
  835. sps_map_de_init();
  836. }
  837. sps_mem_de_init();
  838. }
  839. /**
  840. * Initialize client state context
  841. *
  842. * This function initializes a client state context struct.
  843. *
  844. * @client - Pointer to client state context
  845. *
  846. * @return 0 on success, negative value on error
  847. *
  848. */
  849. static int sps_client_init(struct sps_pipe *client)
  850. {
  851. SPS_DBG(sps, "Enter\n");
  852. if (client == NULL)
  853. return -EINVAL;
  854. /*
  855. * NOTE: Cannot store any state within the SPS driver because
  856. * the driver init function may not have been called yet.
  857. */
  858. memset(client, 0, sizeof(*client));
  859. sps_rm_config_init(&client->connect);
  860. client->client_state = SPS_STATE_DISCONNECT;
  861. client->bam = NULL;
  862. return 0;
  863. }
  864. /**
  865. * De-initialize client state context
  866. *
  867. * This function de-initializes a client state context struct.
  868. *
  869. * @client - Pointer to client state context
  870. *
  871. * @return 0 on success, negative value on error
  872. *
  873. */
  874. static int sps_client_de_init(struct sps_pipe *client)
  875. {
  876. SPS_DBG(sps, "Enter\n");
  877. if (client->client_state != SPS_STATE_DISCONNECT) {
  878. SPS_ERR(sps, "sps:De-init client in connected state: 0x%x\n",
  879. client->client_state);
  880. return SPS_ERROR;
  881. }
  882. client->bam = NULL;
  883. client->map = NULL;
  884. memset(&client->connect, 0, sizeof(client->connect));
  885. return 0;
  886. }
  887. /**
  888. * Find the BAM device from the physical address
  889. *
  890. * This function finds a BAM device in the BAM registration list that
  891. * matches the specified physical address.
  892. *
  893. * @phys_addr - physical address of the BAM
  894. *
  895. * @return - pointer to the BAM device struct, or NULL on error
  896. *
  897. */
  898. static struct sps_bam *phy2bam(phys_addr_t phys_addr)
  899. {
  900. struct sps_bam *bam;
  901. SPS_DBG2(sps, "Enter\n");
  902. list_for_each_entry(bam, &sps->bams_q, list) {
  903. if (bam->props.phys_addr == phys_addr)
  904. return bam;
  905. }
  906. return NULL;
  907. }
  908. /**
  909. * Find the handle of a BAM device based on the physical address
  910. *
  911. * This function finds a BAM device in the BAM registration list that
  912. * matches the specified physical address, and returns its handle.
  913. *
  914. * @phys_addr - physical address of the BAM
  915. *
  916. * @h - device handle of the BAM
  917. *
  918. * @return 0 on success, negative value on error
  919. *
  920. */
  921. int sps_phy2h(phys_addr_t phys_addr, unsigned long *handle)
  922. {
  923. struct sps_bam *bam;
  924. SPS_DBG2(sps, "Enter\n");
  925. if (sps == NULL || !sps->is_ready) {
  926. SPS_DBG3(sps, "sps: sps driver is not ready\n");
  927. return -EPROBE_DEFER;
  928. }
  929. if (handle == NULL) {
  930. SPS_ERR(sps, "sps: handle is NULL\n");
  931. return SPS_ERROR;
  932. }
  933. list_for_each_entry(bam, &sps->bams_q, list) {
  934. if (bam->props.phys_addr == phys_addr) {
  935. *handle = (uintptr_t) bam;
  936. return 0;
  937. }
  938. }
  939. SPS_ERR(sps,
  940. "sps: BAM device %pa is not registered yet\n", &phys_addr);
  941. return -ENODEV;
  942. }
  943. EXPORT_SYMBOL(sps_phy2h);
  944. /**
  945. * Setup desc/data FIFO for bam-to-bam connection
  946. *
  947. * @mem_buffer - Pointer to struct for allocated memory properties.
  948. *
  949. * @addr - address of FIFO
  950. *
  951. * @size - FIFO size
  952. *
  953. * @use_offset - use address offset instead of absolute address
  954. *
  955. * @return 0 on success, negative value on error
  956. *
  957. */
  958. int sps_setup_bam2bam_fifo(struct sps_mem_buffer *mem_buffer,
  959. u32 addr, u32 size, int use_offset)
  960. {
  961. SPS_DBG1(sps, "Enter\n");
  962. if ((mem_buffer == NULL) || (size == 0)) {
  963. SPS_ERR(sps, "sps: invalid buffer address or size\n");
  964. return SPS_ERROR;
  965. }
  966. if (sps == NULL || !sps->is_ready) {
  967. SPS_DBG3(sps, "sps: sps driver is not ready\n");
  968. return -EPROBE_DEFER;
  969. }
  970. if (use_offset) {
  971. if ((addr + size) <= sps->pipemem_size)
  972. mem_buffer->phys_base = sps->pipemem_phys_base + addr;
  973. else {
  974. SPS_ERR(sps,
  975. "sps: requested mem is out of pipe mem range\n");
  976. return SPS_ERROR;
  977. }
  978. } else {
  979. if (addr >= sps->pipemem_phys_base &&
  980. (addr + size) <= (sps->pipemem_phys_base
  981. + sps->pipemem_size))
  982. mem_buffer->phys_base = addr;
  983. else {
  984. SPS_ERR(sps,
  985. "sps: requested mem is out of pipe mem range\n");
  986. return SPS_ERROR;
  987. }
  988. }
  989. mem_buffer->base = spsi_get_mem_ptr(mem_buffer->phys_base);
  990. mem_buffer->size = size;
  991. memset(mem_buffer->base, 0, mem_buffer->size);
  992. return 0;
  993. }
  994. EXPORT_SYMBOL(sps_setup_bam2bam_fifo);
  995. /**
  996. * Find the BAM device from the handle
  997. *
  998. * This function finds a BAM device in the BAM registration list that
  999. * matches the specified device handle.
  1000. *
  1001. * @h - device handle of the BAM
  1002. *
  1003. * @return - pointer to the BAM device struct, or NULL on error
  1004. *
  1005. */
  1006. struct sps_bam *sps_h2bam(unsigned long h)
  1007. {
  1008. struct sps_bam *bam;
  1009. SPS_DBG1(sps, "sps: BAM handle:0x%pK\n", (void *)h);
  1010. if (h == SPS_DEV_HANDLE_MEM || h == SPS_DEV_HANDLE_INVALID)
  1011. return NULL;
  1012. list_for_each_entry(bam, &sps->bams_q, list) {
  1013. if ((uintptr_t) bam == h)
  1014. return bam;
  1015. }
  1016. SPS_ERR(sps, "sps:Can't find BAM device for handle 0x%pK\n", (void *)h);
  1017. return NULL;
  1018. }
  1019. /**
  1020. * Lock BAM device
  1021. *
  1022. * This function obtains the BAM spinlock on the client's connection.
  1023. *
  1024. * @pipe - pointer to client pipe state
  1025. *
  1026. * @return pointer to BAM device struct, or NULL on error
  1027. *
  1028. */
  1029. static struct sps_bam *sps_bam_lock(struct sps_pipe *pipe)
  1030. {
  1031. struct sps_bam *bam;
  1032. u32 pipe_index;
  1033. bam = pipe->bam;
  1034. if (bam == NULL) {
  1035. SPS_ERR(sps, "sps: Connection is not in connected state\n");
  1036. return NULL;
  1037. }
  1038. spin_lock_irqsave(&bam->connection_lock, bam->irqsave_flags);
  1039. /* Verify client owns this pipe */
  1040. pipe_index = pipe->pipe_index;
  1041. if (pipe_index >= bam->props.num_pipes ||
  1042. pipe != bam->pipes[pipe_index]) {
  1043. SPS_ERR(bam,
  1044. "sps:Client not owner of BAM %pa pipe: %d (max %d)\n",
  1045. &bam->props.phys_addr, pipe_index,
  1046. bam->props.num_pipes);
  1047. spin_unlock_irqrestore(&bam->connection_lock,
  1048. bam->irqsave_flags);
  1049. return NULL;
  1050. }
  1051. return bam;
  1052. }
  1053. /**
  1054. * Unlock BAM device
  1055. *
  1056. * This function releases the BAM spinlock on the client's connection.
  1057. *
  1058. * @bam - pointer to BAM device struct
  1059. *
  1060. */
  1061. static inline void sps_bam_unlock(struct sps_bam *bam)
  1062. {
  1063. spin_unlock_irqrestore(&bam->connection_lock, bam->irqsave_flags);
  1064. }
  1065. /**
  1066. * Connect an SPS connection end point
  1067. *
  1068. */
  1069. int sps_connect(struct sps_pipe *h, struct sps_connect *connect)
  1070. {
  1071. struct sps_pipe *pipe = h;
  1072. unsigned long dev;
  1073. struct sps_bam *bam;
  1074. int result;
  1075. if (h == NULL) {
  1076. SPS_ERR(sps, "sps: pipe is NULL\n");
  1077. return SPS_ERROR;
  1078. } else if (connect == NULL) {
  1079. SPS_ERR(sps, "sps: connection is NULL\n");
  1080. return SPS_ERROR;
  1081. }
  1082. if (sps == NULL)
  1083. return -ENODEV;
  1084. if (!sps->is_ready) {
  1085. SPS_ERR(sps, "sps: sps driver is not ready\n");
  1086. return -EAGAIN;
  1087. }
  1088. if ((connect->lock_group != SPSRM_CLEAR)
  1089. && (connect->lock_group > BAM_MAX_P_LOCK_GROUP_NUM)) {
  1090. SPS_ERR(sps,
  1091. "sps: The value of pipe lock group is invalid\n");
  1092. return SPS_ERROR;
  1093. }
  1094. mutex_lock(&sps->lock);
  1095. /*
  1096. * Must lock the BAM device at the top level function, so must
  1097. * determine which BAM is the target for the connection
  1098. */
  1099. if (connect->mode == SPS_MODE_SRC)
  1100. dev = connect->source;
  1101. else
  1102. dev = connect->destination;
  1103. bam = sps_h2bam(dev);
  1104. if (bam == NULL) {
  1105. SPS_ERR(sps, "sps:Invalid BAM device handle: 0x%pK\n",
  1106. (void *)dev);
  1107. result = SPS_ERROR;
  1108. goto exit_err;
  1109. }
  1110. mutex_lock(&bam->lock);
  1111. SPS_DBG2(bam, "sps: bam %pa src 0x%pK dest 0x%pK mode %s\n",
  1112. BAM_ID(bam),
  1113. (void *)connect->source,
  1114. (void *)connect->destination,
  1115. connect->mode == SPS_MODE_SRC ? "SRC" : "DEST");
  1116. /* Allocate resources for the specified connection */
  1117. pipe->connect = *connect;
  1118. result = sps_rm_state_change(pipe, SPS_STATE_ALLOCATE);
  1119. if (result) {
  1120. mutex_unlock(&bam->lock);
  1121. goto exit_err;
  1122. }
  1123. /* Configure the connection */
  1124. result = sps_rm_state_change(pipe, SPS_STATE_CONNECT);
  1125. mutex_unlock(&bam->lock);
  1126. if (result) {
  1127. sps_disconnect(h);
  1128. goto exit_err;
  1129. }
  1130. exit_err:
  1131. mutex_unlock(&sps->lock);
  1132. return result;
  1133. }
  1134. EXPORT_SYMBOL(sps_connect);
  1135. /**
  1136. * Disconnect an SPS connection end point
  1137. *
  1138. * This function disconnects an SPS connection end point.
  1139. * The SPS hardware associated with that end point will be disabled.
  1140. * For a connection involving system memory (SPS_DEV_HANDLE_MEM), all
  1141. * connection resources are deallocated. For a peripheral-to-peripheral
  1142. * connection, the resources associated with the connection will not be
  1143. * deallocated until both end points are closed.
  1144. *
  1145. * The client must call sps_connect() for the handle before calling
  1146. * this function.
  1147. *
  1148. * @h - client context for SPS connection end point
  1149. *
  1150. * @return 0 on success, negative value on error
  1151. *
  1152. */
  1153. int sps_disconnect(struct sps_pipe *h)
  1154. {
  1155. struct sps_pipe *pipe = h;
  1156. struct sps_pipe *check;
  1157. struct sps_bam *bam;
  1158. int result;
  1159. if (pipe == NULL) {
  1160. SPS_ERR(sps, "sps: Invalid pipe\n");
  1161. return SPS_ERROR;
  1162. }
  1163. bam = pipe->bam;
  1164. if (bam == NULL) {
  1165. SPS_ERR(sps,
  1166. "sps: BAM device of this pipe is NULL\n");
  1167. return SPS_ERROR;
  1168. }
  1169. SPS_DBG2(bam,
  1170. "sps: bam %pa src 0x%pK dest 0x%pK mode %s\n",
  1171. BAM_ID(bam),
  1172. (void *)pipe->connect.source,
  1173. (void *)pipe->connect.destination,
  1174. pipe->connect.mode == SPS_MODE_SRC ? "SRC" : "DEST");
  1175. result = SPS_ERROR;
  1176. /* Cross-check client with map table */
  1177. if (pipe->connect.mode == SPS_MODE_SRC)
  1178. check = pipe->map->client_src;
  1179. else
  1180. check = pipe->map->client_dest;
  1181. if (check != pipe) {
  1182. SPS_ERR(sps, "sps: Client context is corrupt\n");
  1183. goto exit_err;
  1184. }
  1185. /* Disconnect the BAM pipe */
  1186. mutex_lock(&bam->lock);
  1187. result = sps_rm_state_change(pipe, SPS_STATE_DISCONNECT);
  1188. mutex_unlock(&bam->lock);
  1189. if (result)
  1190. goto exit_err;
  1191. sps_rm_config_init(&pipe->connect);
  1192. result = 0;
  1193. exit_err:
  1194. return result;
  1195. }
  1196. EXPORT_SYMBOL(sps_disconnect);
  1197. /**
  1198. * Register an event object for an SPS connection end point
  1199. *
  1200. */
  1201. int sps_register_event(struct sps_pipe *h, struct sps_register_event *reg)
  1202. {
  1203. struct sps_pipe *pipe = h;
  1204. struct sps_bam *bam;
  1205. int result;
  1206. if (h == NULL) {
  1207. SPS_ERR(sps, "sps: pipe is NULL\n");
  1208. return SPS_ERROR;
  1209. } else if (reg == NULL) {
  1210. SPS_ERR(sps, "sps: registered event is NULL\n");
  1211. return SPS_ERROR;
  1212. }
  1213. if (sps == NULL)
  1214. return -ENODEV;
  1215. if (!sps->is_ready) {
  1216. SPS_ERR(sps, "sps: sps driver not ready\n");
  1217. return -EAGAIN;
  1218. }
  1219. bam = sps_bam_lock(pipe);
  1220. if (bam == NULL)
  1221. return SPS_ERROR;
  1222. SPS_DBG2(bam, "sps: events:%d\n", reg->options);
  1223. result = sps_bam_pipe_reg_event(bam, pipe->pipe_index, reg);
  1224. sps_bam_unlock(bam);
  1225. if (result)
  1226. SPS_ERR(bam,
  1227. "sps:Fail to register event for BAM %pa pipe %d\n",
  1228. &pipe->bam->props.phys_addr, pipe->pipe_index);
  1229. return result;
  1230. }
  1231. EXPORT_SYMBOL(sps_register_event);
  1232. /**
  1233. * Enable an SPS connection end point
  1234. *
  1235. */
  1236. int sps_flow_on(struct sps_pipe *h)
  1237. {
  1238. struct sps_pipe *pipe = h;
  1239. struct sps_bam *bam;
  1240. int result = 0;
  1241. if (h == NULL) {
  1242. SPS_ERR(sps, "sps: pipe is NULL\n");
  1243. return SPS_ERROR;
  1244. }
  1245. bam = sps_bam_lock(pipe);
  1246. if (bam == NULL)
  1247. return SPS_ERROR;
  1248. bam_pipe_halt(&bam->base, pipe->pipe_index, false);
  1249. sps_bam_unlock(bam);
  1250. return result;
  1251. }
  1252. EXPORT_SYMBOL(sps_flow_on);
  1253. /**
  1254. * Disable an SPS connection end point
  1255. *
  1256. */
  1257. int sps_flow_off(struct sps_pipe *h, enum sps_flow_off mode)
  1258. {
  1259. struct sps_pipe *pipe = h;
  1260. struct sps_bam *bam;
  1261. int result = 0;
  1262. if (h == NULL) {
  1263. SPS_ERR(sps, "sps: pipe is NULL\n");
  1264. return SPS_ERROR;
  1265. }
  1266. bam = sps_bam_lock(pipe);
  1267. if (bam == NULL)
  1268. return SPS_ERROR;
  1269. bam_pipe_halt(&bam->base, pipe->pipe_index, true);
  1270. sps_bam_unlock(bam);
  1271. return result;
  1272. }
  1273. EXPORT_SYMBOL(sps_flow_off);
  1274. /**
  1275. * Check if the flags on a descriptor/iovec are valid
  1276. *
  1277. * @flags - flags on a descriptor/iovec
  1278. *
  1279. * @return 0 on success, negative value on error
  1280. *
  1281. */
  1282. static int sps_check_iovec_flags(u32 flags)
  1283. {
  1284. if ((flags & SPS_IOVEC_FLAG_NWD) &&
  1285. !(flags & (SPS_IOVEC_FLAG_EOT | SPS_IOVEC_FLAG_CMD))) {
  1286. SPS_ERR(sps,
  1287. "sps: NWD is only valid with EOT or CMD\n");
  1288. return SPS_ERROR;
  1289. } else if ((flags & SPS_IOVEC_FLAG_EOT) &&
  1290. (flags & SPS_IOVEC_FLAG_CMD)) {
  1291. SPS_ERR(sps,
  1292. "sps: EOT and CMD are not allowed to coexist\n");
  1293. return SPS_ERROR;
  1294. } else if (!(flags & SPS_IOVEC_FLAG_CMD) &&
  1295. (flags & (SPS_IOVEC_FLAG_LOCK | SPS_IOVEC_FLAG_UNLOCK))) {
  1296. SPS_ERR(sps,
  1297. "sps: pipe lock/unlock flags are only valid with Command Descriptor\n");
  1298. return SPS_ERROR;
  1299. } else if ((flags & SPS_IOVEC_FLAG_LOCK) &&
  1300. (flags & SPS_IOVEC_FLAG_UNLOCK)) {
  1301. SPS_ERR(sps,
  1302. "sps: Can't lock and unlock a pipe by the same Command Descriptor\n");
  1303. return SPS_ERROR;
  1304. } else if ((flags & SPS_IOVEC_FLAG_IMME) &&
  1305. (flags & SPS_IOVEC_FLAG_CMD)) {
  1306. SPS_ERR(sps,
  1307. "sps: Immediate and CMD are not allowed to coexist\n");
  1308. return SPS_ERROR;
  1309. } else if ((flags & SPS_IOVEC_FLAG_IMME) &&
  1310. (flags & SPS_IOVEC_FLAG_NWD)) {
  1311. SPS_ERR(sps,
  1312. "sps: Immediate and NWD are not allowed to coexist\n");
  1313. return SPS_ERROR;
  1314. }
  1315. return 0;
  1316. }
  1317. /**
  1318. * Perform a DMA transfer on an SPS connection end point
  1319. *
  1320. */
  1321. int sps_transfer(struct sps_pipe *h, struct sps_transfer *transfer)
  1322. {
  1323. struct sps_pipe *pipe = h;
  1324. struct sps_bam *bam;
  1325. int result;
  1326. struct sps_iovec *iovec;
  1327. int i;
  1328. if (h == NULL) {
  1329. SPS_ERR(sps, "sps: pipe is NULL\n");
  1330. return SPS_ERROR;
  1331. } else if (transfer == NULL) {
  1332. SPS_ERR(sps, "sps: transfer is NULL\n");
  1333. return SPS_ERROR;
  1334. } else if (transfer->iovec == NULL) {
  1335. SPS_ERR(sps, "sps: iovec list is NULL\n");
  1336. return SPS_ERROR;
  1337. } else if (transfer->iovec_count == 0) {
  1338. SPS_ERR(sps, "sps: iovec list is empty\n");
  1339. return SPS_ERROR;
  1340. } else if (transfer->iovec_phys == 0) {
  1341. SPS_ERR(sps,
  1342. "sps: iovec list address is invalid\n");
  1343. return SPS_ERROR;
  1344. }
  1345. /* Verify content of IOVECs */
  1346. iovec = transfer->iovec;
  1347. for (i = 0; i < transfer->iovec_count; i++) {
  1348. u32 flags = iovec->flags;
  1349. if (iovec->size > SPS_IOVEC_MAX_SIZE) {
  1350. SPS_ERR(sps,
  1351. "sps: iovec size is invalid\n");
  1352. return SPS_ERROR;
  1353. }
  1354. if (sps_check_iovec_flags(flags))
  1355. return SPS_ERROR;
  1356. iovec++;
  1357. }
  1358. bam = sps_bam_lock(pipe);
  1359. if (bam == NULL)
  1360. return SPS_ERROR;
  1361. result = sps_bam_pipe_transfer(bam, pipe->pipe_index, transfer);
  1362. sps_bam_unlock(bam);
  1363. return result;
  1364. }
  1365. EXPORT_SYMBOL(sps_transfer);
  1366. /**
  1367. * Perform a single DMA transfer on an SPS connection end point
  1368. *
  1369. */
  1370. int sps_transfer_one(struct sps_pipe *h, phys_addr_t addr, u32 size,
  1371. void *user, u32 flags)
  1372. {
  1373. struct sps_pipe *pipe = h;
  1374. struct sps_bam *bam;
  1375. int result;
  1376. if (h == NULL) {
  1377. SPS_ERR(sps, "sps: pipe is NULL\n");
  1378. return SPS_ERROR;
  1379. }
  1380. if (sps_check_iovec_flags(flags))
  1381. return SPS_ERROR;
  1382. bam = sps_bam_lock(pipe);
  1383. if (bam == NULL)
  1384. return SPS_ERROR;
  1385. result = sps_bam_pipe_transfer_one(bam, pipe->pipe_index,
  1386. SPS_GET_LOWER_ADDR(addr), size, user,
  1387. DESC_FLAG_WORD(flags, addr));
  1388. sps_bam_unlock(bam);
  1389. return result;
  1390. }
  1391. EXPORT_SYMBOL(sps_transfer_one);
  1392. /**
  1393. * Read event queue for an SPS connection end point
  1394. *
  1395. */
  1396. int sps_get_event(struct sps_pipe *h, struct sps_event_notify *notify)
  1397. {
  1398. struct sps_pipe *pipe = h;
  1399. struct sps_bam *bam;
  1400. int result;
  1401. if (h == NULL) {
  1402. SPS_ERR(sps, "sps: pipe is NULL\n");
  1403. return SPS_ERROR;
  1404. } else if (notify == NULL) {
  1405. SPS_ERR(sps, "sps: event_notify is NULL\n");
  1406. return SPS_ERROR;
  1407. }
  1408. bam = sps_bam_lock(pipe);
  1409. if (bam == NULL)
  1410. return SPS_ERROR;
  1411. result = sps_bam_pipe_get_event(bam, pipe->pipe_index, notify);
  1412. sps_bam_unlock(bam);
  1413. return result;
  1414. }
  1415. EXPORT_SYMBOL(sps_get_event);
  1416. /**
  1417. * Determine whether an SPS connection end point FIFO is empty
  1418. *
  1419. */
  1420. int sps_is_pipe_empty(struct sps_pipe *h, u32 *empty)
  1421. {
  1422. struct sps_pipe *pipe = h;
  1423. struct sps_bam *bam;
  1424. int result;
  1425. if (h == NULL) {
  1426. SPS_ERR(sps, "sps: pipe is NULL\n");
  1427. return SPS_ERROR;
  1428. } else if (empty == NULL) {
  1429. SPS_ERR(sps, "sps: result pointer is NULL\n");
  1430. return SPS_ERROR;
  1431. }
  1432. bam = sps_bam_lock(pipe);
  1433. if (bam == NULL)
  1434. return SPS_ERROR;
  1435. result = sps_bam_pipe_is_empty(bam, pipe->pipe_index, empty);
  1436. sps_bam_unlock(bam);
  1437. return result;
  1438. }
  1439. EXPORT_SYMBOL(sps_is_pipe_empty);
  1440. /**
  1441. * Get number of free transfer entries for an SPS connection end point
  1442. *
  1443. */
  1444. int sps_get_free_count(struct sps_pipe *h, u32 *count)
  1445. {
  1446. struct sps_pipe *pipe = h;
  1447. struct sps_bam *bam;
  1448. int result;
  1449. if (h == NULL) {
  1450. SPS_ERR(sps, "sps: pipe is NULL\n");
  1451. return SPS_ERROR;
  1452. } else if (count == NULL) {
  1453. SPS_ERR(sps, "sps: result pointer is NULL\n");
  1454. return SPS_ERROR;
  1455. }
  1456. bam = sps_bam_lock(pipe);
  1457. if (bam == NULL)
  1458. return SPS_ERROR;
  1459. result = sps_bam_get_free_count(bam, pipe->pipe_index, count);
  1460. sps_bam_unlock(bam);
  1461. return result;
  1462. }
  1463. EXPORT_SYMBOL(sps_get_free_count);
  1464. /**
  1465. * Reset an SPS BAM device
  1466. *
  1467. */
  1468. int sps_device_reset(unsigned long dev)
  1469. {
  1470. struct sps_bam *bam;
  1471. int result;
  1472. if (dev == 0) {
  1473. SPS_ERR(sps,
  1474. "sps: device handle should not be 0\n");
  1475. return SPS_ERROR;
  1476. }
  1477. if (sps == NULL || !sps->is_ready) {
  1478. SPS_DBG3(sps, "sps: sps driver is not ready\n");
  1479. return -EPROBE_DEFER;
  1480. }
  1481. mutex_lock(&sps->lock);
  1482. /* Search for the target BAM device */
  1483. bam = sps_h2bam(dev);
  1484. if (bam == NULL) {
  1485. SPS_ERR(sps, "sps:Invalid BAM device handle: 0x%pK\n",
  1486. (void *)dev);
  1487. result = SPS_ERROR;
  1488. goto exit_err;
  1489. }
  1490. mutex_lock(&bam->lock);
  1491. result = sps_bam_reset(bam);
  1492. mutex_unlock(&bam->lock);
  1493. if (result) {
  1494. SPS_ERR(sps, "sps:Fail to reset BAM device: 0x%pK\n",
  1495. (void *)dev);
  1496. goto exit_err;
  1497. }
  1498. exit_err:
  1499. mutex_unlock(&sps->lock);
  1500. return result;
  1501. }
  1502. EXPORT_SYMBOL(sps_device_reset);
  1503. /**
  1504. * Get the configuration parameters for an SPS connection end point
  1505. *
  1506. */
  1507. int sps_get_config(struct sps_pipe *h, struct sps_connect *config)
  1508. {
  1509. struct sps_pipe *pipe = h;
  1510. if (h == NULL) {
  1511. SPS_ERR(sps, "sps: pipe is NULL\n");
  1512. return SPS_ERROR;
  1513. } else if (config == NULL) {
  1514. SPS_ERR(sps, "sps: config pointer is NULL\n");
  1515. return SPS_ERROR;
  1516. }
  1517. if (pipe->bam == NULL)
  1518. SPS_DBG(sps, "sps:%s\n", __func__);
  1519. else
  1520. SPS_DBG(pipe->bam,
  1521. "sps: BAM: %pa; pipe index:%d; options:0x%x\n",
  1522. BAM_ID(pipe->bam), pipe->pipe_index,
  1523. pipe->connect.options);
  1524. /* Copy current client connection state */
  1525. *config = pipe->connect;
  1526. return 0;
  1527. }
  1528. EXPORT_SYMBOL(sps_get_config);
  1529. /**
  1530. * Set the configuration parameters for an SPS connection end point
  1531. *
  1532. */
  1533. int sps_set_config(struct sps_pipe *h, struct sps_connect *config)
  1534. {
  1535. struct sps_pipe *pipe = h;
  1536. struct sps_bam *bam;
  1537. int result;
  1538. if (h == NULL) {
  1539. SPS_ERR(sps, "sps: pipe is NULL\n");
  1540. return SPS_ERROR;
  1541. } else if (config == NULL) {
  1542. SPS_ERR(sps, "sps: config pointer is NULL\n");
  1543. return SPS_ERROR;
  1544. }
  1545. bam = sps_bam_lock(pipe);
  1546. if (bam == NULL) {
  1547. SPS_ERR(sps, "sps: BAM is NULL\n");
  1548. return SPS_ERROR;
  1549. }
  1550. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d, config-options:0x%x\n",
  1551. BAM_ID(bam), pipe->pipe_index, config->options);
  1552. result = sps_bam_pipe_set_params(bam, pipe->pipe_index,
  1553. config->options);
  1554. if (result == 0)
  1555. pipe->connect.options = config->options;
  1556. sps_bam_unlock(bam);
  1557. return result;
  1558. }
  1559. EXPORT_SYMBOL(sps_set_config);
  1560. /**
  1561. * Set ownership of an SPS connection end point
  1562. *
  1563. */
  1564. int sps_set_owner(struct sps_pipe *h, enum sps_owner owner,
  1565. struct sps_satellite *connect)
  1566. {
  1567. struct sps_pipe *pipe = h;
  1568. struct sps_bam *bam;
  1569. int result;
  1570. if (h == NULL) {
  1571. SPS_ERR(sps, "sps: pipe is NULL\n");
  1572. return SPS_ERROR;
  1573. } else if (connect == NULL) {
  1574. SPS_ERR(sps, "sps: connection is NULL\n");
  1575. return SPS_ERROR;
  1576. }
  1577. if (owner != SPS_OWNER_REMOTE) {
  1578. SPS_ERR(sps, "sps: Unsupported ownership state: %d\n", owner);
  1579. return SPS_ERROR;
  1580. }
  1581. bam = sps_bam_lock(pipe);
  1582. if (bam == NULL)
  1583. return SPS_ERROR;
  1584. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d\n",
  1585. BAM_ID(bam), pipe->pipe_index);
  1586. result = sps_bam_set_satellite(bam, pipe->pipe_index);
  1587. if (result)
  1588. goto exit_err;
  1589. /* Return satellite connect info */
  1590. if (connect == NULL)
  1591. goto exit_err;
  1592. if (pipe->connect.mode == SPS_MODE_SRC) {
  1593. connect->dev = pipe->map->src.bam_phys;
  1594. connect->pipe_index = pipe->map->src.pipe_index;
  1595. } else {
  1596. connect->dev = pipe->map->dest.bam_phys;
  1597. connect->pipe_index = pipe->map->dest.pipe_index;
  1598. }
  1599. connect->config = SPS_CONFIG_SATELLITE;
  1600. connect->options = (enum sps_option) 0;
  1601. exit_err:
  1602. sps_bam_unlock(bam);
  1603. return result;
  1604. }
  1605. EXPORT_SYMBOL(sps_set_owner);
  1606. /**
  1607. * Allocate memory from the SPS Pipe-Memory.
  1608. *
  1609. */
  1610. int sps_alloc_mem(struct sps_pipe *h, enum sps_mem mem,
  1611. struct sps_mem_buffer *mem_buffer)
  1612. {
  1613. if (sps == NULL)
  1614. return -ENODEV;
  1615. if (!sps->is_ready) {
  1616. SPS_ERR(sps, "sps: sps driver is not ready\n");
  1617. return -EAGAIN;
  1618. }
  1619. if (mem_buffer == NULL || mem_buffer->size == 0) {
  1620. SPS_ERR(sps, "sps: invalid memory buffer address or size\n");
  1621. return SPS_ERROR;
  1622. }
  1623. if (h == NULL)
  1624. SPS_DBG2(sps,
  1625. "sps: allocate pipe memory before setup pipe\n");
  1626. else
  1627. SPS_DBG2(sps,
  1628. "sps:allocate pipe memory for pipe %d\n",
  1629. h->pipe_index);
  1630. mem_buffer->phys_base = sps_mem_alloc_io(mem_buffer->size);
  1631. if (mem_buffer->phys_base == SPS_ADDR_INVALID) {
  1632. SPS_ERR(sps, "sps: invalid address of allocated memory\n");
  1633. return SPS_ERROR;
  1634. }
  1635. mem_buffer->base = spsi_get_mem_ptr(mem_buffer->phys_base);
  1636. return 0;
  1637. }
  1638. EXPORT_SYMBOL(sps_alloc_mem);
  1639. /**
  1640. * Free memory from the SPS Pipe-Memory.
  1641. *
  1642. */
  1643. int sps_free_mem(struct sps_pipe *h, struct sps_mem_buffer *mem_buffer)
  1644. {
  1645. SPS_DBG(sps, "sps: Enter\n");
  1646. if (mem_buffer == NULL || mem_buffer->phys_base == SPS_ADDR_INVALID) {
  1647. SPS_ERR(sps, "sps: invalid memory to free\n");
  1648. return SPS_ERROR;
  1649. }
  1650. if (h == NULL)
  1651. SPS_DBG2(sps, "sps: free pipe memory\n");
  1652. else
  1653. SPS_DBG2(sps,
  1654. "sps:free pipe memory for pipe %d\n", h->pipe_index);
  1655. sps_mem_free_io(mem_buffer->phys_base, mem_buffer->size);
  1656. return 0;
  1657. }
  1658. EXPORT_SYMBOL(sps_free_mem);
  1659. /**
  1660. * Get the number of unused descriptors in the descriptor FIFO
  1661. * of a pipe
  1662. *
  1663. */
  1664. int sps_get_unused_desc_num(struct sps_pipe *h, u32 *desc_num)
  1665. {
  1666. struct sps_pipe *pipe = h;
  1667. struct sps_bam *bam;
  1668. int result;
  1669. if (h == NULL) {
  1670. SPS_ERR(sps, "sps: pipe is NULL\n");
  1671. return SPS_ERROR;
  1672. } else if (desc_num == NULL) {
  1673. SPS_ERR(sps, "sps: result pointer is NULL\n");
  1674. return SPS_ERROR;
  1675. }
  1676. bam = sps_bam_lock(pipe);
  1677. if (bam == NULL)
  1678. return SPS_ERROR;
  1679. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d\n", BAM_ID(bam),
  1680. pipe->pipe_index);
  1681. result = sps_bam_pipe_get_unused_desc_num(bam, pipe->pipe_index,
  1682. desc_num);
  1683. sps_bam_unlock(bam);
  1684. return result;
  1685. }
  1686. EXPORT_SYMBOL(sps_get_unused_desc_num);
  1687. /**
  1688. * Vote for or relinquish BAM DMA clock
  1689. *
  1690. */
  1691. int sps_ctrl_bam_dma_clk(bool clk_on)
  1692. {
  1693. int ret;
  1694. if (sps == NULL || !sps->is_ready) {
  1695. SPS_DBG3(sps, "sps: sps driver is not ready\n");
  1696. return -EPROBE_DEFER;
  1697. }
  1698. if (clk_on) {
  1699. SPS_DBG1(sps, "%s", "sps:vote for bam dma clk\n");
  1700. ret = clk_prepare_enable(sps->bamdma_clk);
  1701. if (ret) {
  1702. SPS_ERR(sps,
  1703. "sps:fail to enable bamdma_clk:ret=%d\n", ret);
  1704. return ret;
  1705. }
  1706. } else {
  1707. SPS_DBG1(sps, "%s", "sps:relinquish bam dma clk\n");
  1708. clk_disable_unprepare(sps->bamdma_clk);
  1709. }
  1710. return 0;
  1711. }
  1712. EXPORT_SYMBOL(sps_ctrl_bam_dma_clk);
  1713. /**
  1714. * Register a BAM device
  1715. *
  1716. */
  1717. int sps_register_bam_device(const struct sps_bam_props *bam_props,
  1718. unsigned long *dev_handle)
  1719. {
  1720. struct sps_bam *bam = NULL;
  1721. void __iomem *virt_addr = NULL;
  1722. char bam_name[MAX_MSG_LEN];
  1723. u32 manage;
  1724. int ok;
  1725. int result;
  1726. if (bam_props == NULL) {
  1727. SPS_ERR(sps, "sps: bam_props is NULL\n");
  1728. return SPS_ERROR;
  1729. } else if (dev_handle == NULL) {
  1730. SPS_ERR(sps, "sps: device handle is NULL\n");
  1731. return SPS_ERROR;
  1732. }
  1733. if (sps == NULL) {
  1734. pr_err("sps: sps driver is not ready\n");
  1735. return -EPROBE_DEFER;
  1736. }
  1737. SPS_DBG3(sps, "sps: Client requests to register BAM %pa\n",
  1738. &bam_props->phys_addr);
  1739. /* BAM-DMA is registered internally during power-up */
  1740. if ((!sps->is_ready) && !(bam_props->options & SPS_BAM_OPT_BAMDMA)) {
  1741. SPS_ERR(sps, "sps: sps driver not ready\n");
  1742. return -EAGAIN;
  1743. }
  1744. /* Check BAM parameters */
  1745. manage = bam_props->manage & SPS_BAM_MGR_ACCESS_MASK;
  1746. if (manage != SPS_BAM_MGR_NONE) {
  1747. if (bam_props->virt_addr == NULL && bam_props->virt_size == 0) {
  1748. SPS_ERR(sps, "sps:Invalid properties for BAM: %pa\n",
  1749. &bam_props->phys_addr);
  1750. return SPS_ERROR;
  1751. }
  1752. }
  1753. if ((bam_props->manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0) {
  1754. /* BAM global is configured by local processor */
  1755. if (bam_props->summing_threshold == 0) {
  1756. SPS_ERR(sps,
  1757. "sps:Invalid device ctrl properties for BAM: %pa\n",
  1758. &bam_props->phys_addr);
  1759. return SPS_ERROR;
  1760. }
  1761. }
  1762. manage = bam_props->manage &
  1763. (SPS_BAM_MGR_PIPE_NO_CONFIG | SPS_BAM_MGR_PIPE_NO_CTRL);
  1764. /* In case of error */
  1765. *dev_handle = SPS_DEV_HANDLE_INVALID;
  1766. result = SPS_ERROR;
  1767. mutex_lock(&sps->lock);
  1768. /* Is this BAM already registered? */
  1769. bam = phy2bam(bam_props->phys_addr);
  1770. if (bam != NULL) {
  1771. mutex_unlock(&sps->lock);
  1772. SPS_ERR(sps, "sps:BAM is already registered: %pa\n",
  1773. &bam->props.phys_addr);
  1774. result = -EEXIST;
  1775. bam = NULL; /* Avoid error clean-up kfree(bam) */
  1776. goto exit_err;
  1777. }
  1778. /* Perform virtual mapping if required */
  1779. if ((bam_props->manage & SPS_BAM_MGR_ACCESS_MASK) !=
  1780. SPS_BAM_MGR_NONE && bam_props->virt_addr == NULL) {
  1781. /* Map the memory region */
  1782. virt_addr = ioremap(bam_props->phys_addr, bam_props->virt_size);
  1783. if (virt_addr == NULL) {
  1784. SPS_ERR(sps,
  1785. "sps:Unable to map BAM IO mem:%pa size:0x%x\n",
  1786. &bam_props->phys_addr, bam_props->virt_size);
  1787. goto exit_err;
  1788. }
  1789. }
  1790. bam = kzalloc(sizeof(*bam), GFP_KERNEL);
  1791. if (bam == NULL)
  1792. goto exit_err;
  1793. memset(bam, 0, sizeof(*bam));
  1794. mutex_init(&bam->lock);
  1795. mutex_lock(&bam->lock);
  1796. /* Copy configuration to BAM device descriptor */
  1797. bam->props = *bam_props;
  1798. if (virt_addr != NULL)
  1799. bam->props.virt_addr = virt_addr;
  1800. snprintf(bam_name, sizeof(bam_name), "sps_bam_%pa_0",
  1801. &bam->props.phys_addr);
  1802. bam->ipc_log0 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  1803. bam_name, 0);
  1804. if (!bam->ipc_log0)
  1805. SPS_ERR(sps, "unable to create IPC Log 0 for bam %pa\n",
  1806. &bam->props.phys_addr);
  1807. snprintf(bam_name, sizeof(bam_name), "sps_bam_%pa_1",
  1808. &bam->props.phys_addr);
  1809. bam->ipc_log1 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  1810. bam_name, 0);
  1811. if (!bam->ipc_log1)
  1812. SPS_ERR(sps, "unable to create IPC Log 1 for bam %pa\n",
  1813. &bam->props.phys_addr);
  1814. snprintf(bam_name, sizeof(bam_name), "sps_bam_%pa_2",
  1815. &bam->props.phys_addr);
  1816. bam->ipc_log2 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  1817. bam_name, 0);
  1818. if (!bam->ipc_log2)
  1819. SPS_ERR(sps, "unable to create IPC Log 2 for bam %pa\n",
  1820. &bam->props.phys_addr);
  1821. snprintf(bam_name, sizeof(bam_name), "sps_bam_%pa_3",
  1822. &bam->props.phys_addr);
  1823. bam->ipc_log3 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  1824. bam_name, 0);
  1825. if (!bam->ipc_log3)
  1826. SPS_ERR(sps, "unable to create IPC Log 3 for bam %pa\n",
  1827. &bam->props.phys_addr);
  1828. snprintf(bam_name, sizeof(bam_name), "sps_bam_%pa_4",
  1829. &bam->props.phys_addr);
  1830. bam->ipc_log4 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  1831. bam_name, 0);
  1832. if (!bam->ipc_log4)
  1833. SPS_ERR(sps, "unable to create IPC Log 4 for bam %pa\n",
  1834. &bam->props.phys_addr);
  1835. if (bam_props->ipc_loglevel)
  1836. bam->ipc_loglevel = bam_props->ipc_loglevel;
  1837. else
  1838. bam->ipc_loglevel = SPS_IPC_DEFAULT_LOGLEVEL;
  1839. ok = sps_bam_device_init(bam);
  1840. mutex_unlock(&bam->lock);
  1841. if (ok) {
  1842. SPS_ERR(bam, "sps:Fail to init BAM device: phys %pa\n",
  1843. &bam->props.phys_addr);
  1844. goto exit_err;
  1845. }
  1846. /* Add BAM to the list */
  1847. list_add_tail(&bam->list, &sps->bams_q);
  1848. *dev_handle = (uintptr_t) bam;
  1849. result = 0;
  1850. exit_err:
  1851. mutex_unlock(&sps->lock);
  1852. if (result) {
  1853. if (bam != NULL) {
  1854. if (virt_addr != NULL)
  1855. iounmap(bam->props.virt_addr);
  1856. kfree(bam);
  1857. }
  1858. return result;
  1859. }
  1860. /* If this BAM is attached to a BAM-DMA, init the BAM-DMA device */
  1861. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  1862. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  1863. if (sps_dma_device_init((uintptr_t) bam)) {
  1864. bam->props.options &= ~SPS_BAM_OPT_BAMDMA;
  1865. sps_deregister_bam_device((uintptr_t) bam);
  1866. SPS_ERR(bam, "sps:Fail to init BAM-DMA BAM: phys %pa\n",
  1867. &bam->props.phys_addr);
  1868. return SPS_ERROR;
  1869. }
  1870. }
  1871. #endif /* CONFIG_SPS_SUPPORT_BAMDMA */
  1872. SPS_INFO(bam, "sps:BAM %pa is registered\n", &bam->props.phys_addr);
  1873. return 0;
  1874. }
  1875. EXPORT_SYMBOL(sps_register_bam_device);
  1876. /**
  1877. * Deregister a BAM device
  1878. *
  1879. */
  1880. int sps_deregister_bam_device(unsigned long dev_handle)
  1881. {
  1882. struct sps_bam *bam;
  1883. int n;
  1884. if (dev_handle == 0) {
  1885. SPS_ERR(sps, "sps: device handle should not be 0\n");
  1886. return SPS_ERROR;
  1887. }
  1888. bam = sps_h2bam(dev_handle);
  1889. if (bam == NULL) {
  1890. SPS_ERR(sps, "sps: did not find a BAM for this handle\n");
  1891. return SPS_ERROR;
  1892. }
  1893. SPS_DBG3(sps, "sps: SPS deregister BAM: phys %pa\n",
  1894. &bam->props.phys_addr);
  1895. if (bam->props.options & SPS_BAM_HOLD_MEM) {
  1896. for (n = 0; n < BAM_MAX_PIPES; n++)
  1897. kfree(bam->desc_cache_pointers[n]);
  1898. }
  1899. /* If this BAM is attached to a BAM-DMA, init the BAM-DMA device */
  1900. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  1901. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  1902. mutex_lock(&bam->lock);
  1903. (void)sps_dma_device_de_init((uintptr_t) bam);
  1904. bam->props.options &= ~SPS_BAM_OPT_BAMDMA;
  1905. mutex_unlock(&bam->lock);
  1906. }
  1907. #endif
  1908. /* Remove the BAM from the registration list */
  1909. mutex_lock(&sps->lock);
  1910. list_del(&bam->list);
  1911. mutex_unlock(&sps->lock);
  1912. /* De-init the BAM and free resources */
  1913. mutex_lock(&bam->lock);
  1914. sps_bam_device_de_init(bam);
  1915. mutex_unlock(&bam->lock);
  1916. ipc_log_context_destroy(bam->ipc_log0);
  1917. ipc_log_context_destroy(bam->ipc_log1);
  1918. ipc_log_context_destroy(bam->ipc_log2);
  1919. ipc_log_context_destroy(bam->ipc_log3);
  1920. ipc_log_context_destroy(bam->ipc_log4);
  1921. if (bam->props.virt_size)
  1922. (void)iounmap(bam->props.virt_addr);
  1923. kfree(bam);
  1924. return 0;
  1925. }
  1926. EXPORT_SYMBOL(sps_deregister_bam_device);
  1927. /**
  1928. * Get processed I/O vector (completed transfers)
  1929. *
  1930. */
  1931. int sps_get_iovec(struct sps_pipe *h, struct sps_iovec *iovec)
  1932. {
  1933. struct sps_pipe *pipe = h;
  1934. struct sps_bam *bam;
  1935. int result;
  1936. if (h == NULL) {
  1937. SPS_ERR(sps, "sps: pipe is NULL\n");
  1938. return SPS_ERROR;
  1939. } else if (iovec == NULL) {
  1940. SPS_ERR(sps, "sps: iovec pointer is NULL\n");
  1941. return SPS_ERROR;
  1942. }
  1943. bam = sps_bam_lock(pipe);
  1944. if (bam == NULL) {
  1945. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  1946. return SPS_ERROR;
  1947. }
  1948. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d\n",
  1949. BAM_ID(bam), pipe->pipe_index);
  1950. /* Get an iovec from the BAM pipe descriptor FIFO */
  1951. result = sps_bam_pipe_get_iovec(bam, pipe->pipe_index, iovec);
  1952. sps_bam_unlock(bam);
  1953. return result;
  1954. }
  1955. EXPORT_SYMBOL(sps_get_iovec);
  1956. /**
  1957. * Perform timer control
  1958. *
  1959. */
  1960. int sps_timer_ctrl(struct sps_pipe *h,
  1961. struct sps_timer_ctrl *timer_ctrl,
  1962. struct sps_timer_result *timer_result)
  1963. {
  1964. return 0;
  1965. }
  1966. EXPORT_SYMBOL(sps_timer_ctrl);
  1967. /*
  1968. * Reset a BAM pipe
  1969. */
  1970. int sps_pipe_reset(unsigned long dev, u32 pipe)
  1971. {
  1972. struct sps_bam *bam;
  1973. if (!dev) {
  1974. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  1975. return SPS_ERROR;
  1976. }
  1977. if (pipe >= BAM_MAX_PIPES) {
  1978. SPS_ERR(sps, "sps: pipe index is invalid\n");
  1979. return SPS_ERROR;
  1980. }
  1981. bam = sps_h2bam(dev);
  1982. if (bam == NULL) {
  1983. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  1984. return SPS_ERROR;
  1985. }
  1986. SPS_DBG2(bam, "sps: BAM: %pa; pipe index:%d\n", BAM_ID(bam), pipe);
  1987. bam_pipe_reset(&bam->base, pipe);
  1988. return 0;
  1989. }
  1990. EXPORT_SYMBOL(sps_pipe_reset);
  1991. /*
  1992. * Disable a BAM pipe
  1993. */
  1994. int sps_pipe_disable(unsigned long dev, u32 pipe)
  1995. {
  1996. struct sps_bam *bam;
  1997. if (!dev) {
  1998. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  1999. return SPS_ERROR;
  2000. }
  2001. if (pipe >= BAM_MAX_PIPES) {
  2002. SPS_ERR(sps, "sps: pipe index is invalid\n");
  2003. return SPS_ERROR;
  2004. }
  2005. bam = sps_h2bam(dev);
  2006. if (bam == NULL) {
  2007. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  2008. return SPS_ERROR;
  2009. }
  2010. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d\n", BAM_ID(bam), pipe);
  2011. bam_disable_pipe(&bam->base, pipe);
  2012. return 0;
  2013. }
  2014. EXPORT_SYMBOL(sps_pipe_disable);
  2015. /*
  2016. * Check pending descriptors in the descriptor FIFO
  2017. * of a pipe
  2018. */
  2019. int sps_pipe_pending_desc(unsigned long dev, u32 pipe, bool *pending)
  2020. {
  2021. struct sps_bam *bam;
  2022. if (!dev) {
  2023. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  2024. return SPS_ERROR;
  2025. }
  2026. if (pipe >= BAM_MAX_PIPES) {
  2027. SPS_ERR(sps, "sps: pipe index is invalid\n");
  2028. return SPS_ERROR;
  2029. }
  2030. if (!pending) {
  2031. SPS_ERR(sps, "sps: input flag is NULL\n");
  2032. return SPS_ERROR;
  2033. }
  2034. bam = sps_h2bam(dev);
  2035. if (bam == NULL) {
  2036. SPS_ERR(sps, "sps: :BAM is not found by handle\n");
  2037. return SPS_ERROR;
  2038. }
  2039. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d\n", BAM_ID(bam), pipe);
  2040. *pending = sps_bam_pipe_pending_desc(bam, pipe);
  2041. return 0;
  2042. }
  2043. EXPORT_SYMBOL(sps_pipe_pending_desc);
  2044. /*
  2045. * Process any pending IRQ of a BAM
  2046. */
  2047. int sps_bam_process_irq(unsigned long dev)
  2048. {
  2049. struct sps_bam *bam;
  2050. int ret = 0;
  2051. if (!dev) {
  2052. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  2053. return SPS_ERROR;
  2054. }
  2055. bam = sps_h2bam(dev);
  2056. if (bam == NULL) {
  2057. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  2058. return SPS_ERROR;
  2059. }
  2060. SPS_DBG1(bam, "sps: BAM: %pa\n", BAM_ID(bam));
  2061. ret = sps_bam_check_irq(bam);
  2062. return ret;
  2063. }
  2064. EXPORT_SYMBOL(sps_bam_process_irq);
  2065. /*
  2066. * Enable all IRQs of a BAM
  2067. */
  2068. int sps_bam_enable_irqs(unsigned long dev)
  2069. {
  2070. struct sps_bam *bam;
  2071. if (!dev) {
  2072. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  2073. return SPS_ERROR;
  2074. }
  2075. bam = sps_h2bam(dev);
  2076. if (bam == NULL) {
  2077. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  2078. return SPS_ERROR;
  2079. }
  2080. SPS_DBG1(bam, "sps: BAM: %pa\n", BAM_ID(bam));
  2081. sps_bam_enable_all_irqs(bam);
  2082. return 0;
  2083. }
  2084. EXPORT_SYMBOL(sps_bam_enable_irqs);
  2085. /*
  2086. * Disable all IRQs of a BAM
  2087. */
  2088. int sps_bam_disable_irqs(unsigned long dev)
  2089. {
  2090. struct sps_bam *bam;
  2091. if (!dev) {
  2092. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  2093. return SPS_ERROR;
  2094. }
  2095. bam = sps_h2bam(dev);
  2096. if (bam == NULL) {
  2097. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  2098. return SPS_ERROR;
  2099. }
  2100. SPS_DBG1(bam, "sps: BAM: %pa\n", BAM_ID(bam));
  2101. sps_bam_disable_all_irqs(bam);
  2102. return 0;
  2103. }
  2104. EXPORT_SYMBOL(sps_bam_disable_irqs);
  2105. /*
  2106. * Get address info of a BAM
  2107. */
  2108. int sps_get_bam_addr(unsigned long dev, phys_addr_t *base,
  2109. u32 *size)
  2110. {
  2111. struct sps_bam *bam;
  2112. if (!dev) {
  2113. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  2114. return SPS_ERROR;
  2115. }
  2116. bam = sps_h2bam(dev);
  2117. if (bam == NULL) {
  2118. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  2119. return SPS_ERROR;
  2120. }
  2121. *base = bam->props.phys_addr;
  2122. *size = bam->props.virt_size;
  2123. SPS_DBG2(bam, "sps: BAM: %pa; base:%pa; size:%d\n",
  2124. BAM_ID(bam), base, *size);
  2125. return 0;
  2126. }
  2127. EXPORT_SYMBOL(sps_get_bam_addr);
  2128. /*
  2129. * Inject a ZLT with EOT for a BAM pipe
  2130. */
  2131. int sps_pipe_inject_zlt(unsigned long dev, u32 pipe_index)
  2132. {
  2133. struct sps_bam *bam;
  2134. int rc;
  2135. if (!dev) {
  2136. SPS_ERR(sps, "sps: BAM handle is NULL\n");
  2137. return SPS_ERROR;
  2138. }
  2139. if (pipe_index >= BAM_MAX_PIPES) {
  2140. SPS_ERR(sps, "sps: pipe index is invalid\n");
  2141. return SPS_ERROR;
  2142. }
  2143. bam = sps_h2bam(dev);
  2144. if (bam == NULL) {
  2145. SPS_ERR(sps, "sps: BAM is not found by handle\n");
  2146. return SPS_ERROR;
  2147. }
  2148. SPS_DBG(bam, "sps: BAM: %pa; pipe index:%d\n", BAM_ID(bam), pipe_index);
  2149. rc = sps_bam_pipe_inject_zlt(bam, pipe_index);
  2150. if (rc)
  2151. SPS_ERR(bam, "sps: failed to inject a ZLT\n");
  2152. return rc;
  2153. }
  2154. EXPORT_SYMBOL(sps_pipe_inject_zlt);
  2155. /**
  2156. * Allocate client state context
  2157. *
  2158. */
  2159. struct sps_pipe *sps_alloc_endpoint(void)
  2160. {
  2161. struct sps_pipe *ctx = NULL;
  2162. SPS_DBG(sps, "sps: Enter\n");
  2163. ctx = kzalloc(sizeof(struct sps_pipe), GFP_KERNEL);
  2164. if (ctx == NULL)
  2165. return NULL;
  2166. sps_client_init(ctx);
  2167. return ctx;
  2168. }
  2169. EXPORT_SYMBOL(sps_alloc_endpoint);
  2170. /**
  2171. * Free client state context
  2172. *
  2173. */
  2174. int sps_free_endpoint(struct sps_pipe *ctx)
  2175. {
  2176. int res;
  2177. SPS_DBG(sps, "sps: Enter\n");
  2178. if (ctx == NULL) {
  2179. SPS_ERR(sps, "sps: pipe is NULL\n");
  2180. return SPS_ERROR;
  2181. }
  2182. res = sps_client_de_init(ctx);
  2183. if (res == 0)
  2184. kfree(ctx);
  2185. return res;
  2186. }
  2187. EXPORT_SYMBOL(sps_free_endpoint);
  2188. /**
  2189. * Platform Driver.
  2190. */
  2191. static int get_platform_data(struct platform_device *pdev)
  2192. {
  2193. struct resource *resource;
  2194. struct msm_sps_platform_data *pdata;
  2195. SPS_DBG3(sps, "sps: Enter\n");
  2196. pdata = pdev->dev.platform_data;
  2197. if (pdata == NULL) {
  2198. SPS_ERR(sps, "sps: invalid platform data\n");
  2199. sps->bamdma_restricted_pipes = 0;
  2200. return -EINVAL;
  2201. }
  2202. sps->bamdma_restricted_pipes = pdata->bamdma_restricted_pipes;
  2203. SPS_DBG3(sps, "sps:bamdma_restricted_pipes=0x%x\n",
  2204. sps->bamdma_restricted_pipes);
  2205. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2206. "pipe_mem");
  2207. if (resource) {
  2208. sps->pipemem_phys_base = resource->start;
  2209. sps->pipemem_size = resource_size(resource);
  2210. SPS_DBG3(sps, "sps:pipemem.base=%pa,size=0x%x\n",
  2211. &sps->pipemem_phys_base,
  2212. sps->pipemem_size);
  2213. }
  2214. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2215. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2216. "bamdma_bam");
  2217. if (resource) {
  2218. sps->bamdma_bam_phys_base = resource->start;
  2219. sps->bamdma_bam_size = resource_size(resource);
  2220. SPS_DBG(sps, "sps:bamdma_bam.base=%pa,size=0x%x\n",
  2221. &sps->bamdma_bam_phys_base,
  2222. sps->bamdma_bam_size);
  2223. }
  2224. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2225. "bamdma_dma");
  2226. if (resource) {
  2227. sps->bamdma_dma_phys_base = resource->start;
  2228. sps->bamdma_dma_size = resource_size(resource);
  2229. SPS_DBG(sps, "sps:bamdma_dma.base=%pa,size=0x%x\n",
  2230. &sps->bamdma_dma_phys_base,
  2231. sps->bamdma_dma_size);
  2232. }
  2233. resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  2234. "bamdma_irq");
  2235. if (resource) {
  2236. sps->bamdma_irq = resource->start;
  2237. SPS_DBG(sps, "sps:bamdma_irq=%d\n", sps->bamdma_irq);
  2238. }
  2239. #endif
  2240. return 0;
  2241. }
  2242. /**
  2243. * Read data from device tree
  2244. */
  2245. static int get_device_tree_data(struct platform_device *pdev)
  2246. {
  2247. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2248. struct resource *resource;
  2249. SPS_DBG(sps, "sps: Enter\n");
  2250. if (of_property_read_u32((&pdev->dev)->of_node,
  2251. "qcom,bam-dma-res-pipes",
  2252. &sps->bamdma_restricted_pipes))
  2253. SPS_DBG(sps,
  2254. "sps: No restricted bamdma pipes on this target\n");
  2255. else
  2256. SPS_DBG(sps, "sps:bamdma_restricted_pipes=0x%x\n",
  2257. sps->bamdma_restricted_pipes);
  2258. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2259. if (resource) {
  2260. sps->bamdma_bam_phys_base = resource->start;
  2261. sps->bamdma_bam_size = resource_size(resource);
  2262. SPS_DBG(sps, "sps:bamdma_bam.base=%pa,size=0x%x\n",
  2263. &sps->bamdma_bam_phys_base,
  2264. sps->bamdma_bam_size);
  2265. } else {
  2266. SPS_ERR(sps, "sps: BAM DMA BAM mem unavailable\n");
  2267. return -ENODEV;
  2268. }
  2269. resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2270. if (resource) {
  2271. sps->bamdma_dma_phys_base = resource->start;
  2272. sps->bamdma_dma_size = resource_size(resource);
  2273. SPS_DBG(sps, "sps:bamdma_dma.base=%pa,size=0x%x\n",
  2274. &sps->bamdma_dma_phys_base,
  2275. sps->bamdma_dma_size);
  2276. } else {
  2277. SPS_ERR(sps, "sps: BAM DMA mem unavailable\n");
  2278. return -ENODEV;
  2279. }
  2280. resource = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  2281. if (resource) {
  2282. imem = true;
  2283. sps->pipemem_phys_base = resource->start;
  2284. sps->pipemem_size = resource_size(resource);
  2285. SPS_DBG(sps, "sps:pipemem.base=%pa,size=0x%x\n",
  2286. &sps->pipemem_phys_base,
  2287. sps->pipemem_size);
  2288. } else {
  2289. imem = false;
  2290. SPS_DBG(sps, "sps: No pipe memory on this target\n");
  2291. }
  2292. resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2293. if (resource) {
  2294. sps->bamdma_irq = resource->start;
  2295. SPS_DBG(sps, "sps:bamdma_irq=%d\n", sps->bamdma_irq);
  2296. } else {
  2297. SPS_ERR(sps, "sps: BAM DMA IRQ unavailable\n");
  2298. return -ENODEV;
  2299. }
  2300. #endif
  2301. if (of_property_read_u32((&pdev->dev)->of_node,
  2302. "qcom,device-type",
  2303. &d_type)) {
  2304. d_type = 3;
  2305. SPS_DBG3(sps, "sps:default device type %d\n", d_type);
  2306. } else
  2307. SPS_DBG3(sps, "sps:device type is %d\n", d_type);
  2308. enhd_pipe = of_property_read_bool((&pdev->dev)->of_node,
  2309. "qcom,pipe-attr-ee");
  2310. SPS_DBG3(sps, "sps:PIPE_ATTR_EE is %s supported\n",
  2311. (enhd_pipe ? "" : "not"));
  2312. return 0;
  2313. }
  2314. static const struct of_device_id msm_sps_match[] = {
  2315. { .compatible = "qcom,msm-sps",
  2316. .data = &bam_types[SPS_BAM_NDP]
  2317. },
  2318. { .compatible = "qcom,msm-sps-4k",
  2319. .data = &bam_types[SPS_BAM_NDP_4K]
  2320. },
  2321. {},
  2322. };
  2323. static int msm_sps_probe(struct platform_device *pdev)
  2324. {
  2325. int ret = -ENODEV;
  2326. SPS_DBG3(sps, "sps: Enter\n");
  2327. if (pdev->dev.of_node) {
  2328. const struct of_device_id *match;
  2329. if (get_device_tree_data(pdev)) {
  2330. SPS_ERR(sps,
  2331. "sps: Fail to get data from device tree\n");
  2332. return -ENODEV;
  2333. }
  2334. SPS_DBG(sps, "%s", "sps:get data from device tree\n");
  2335. match = of_match_device(msm_sps_match, &pdev->dev);
  2336. if (match) {
  2337. bam_type = *((enum sps_bam_type *)(match->data));
  2338. SPS_DBG3(sps, "sps:BAM type is:%d\n", bam_type);
  2339. } else {
  2340. bam_type = SPS_BAM_NDP;
  2341. SPS_DBG3(sps, "sps:use default BAM type:%d\n",
  2342. bam_type);
  2343. }
  2344. } else {
  2345. d_type = 0;
  2346. if (get_platform_data(pdev)) {
  2347. SPS_ERR(sps, "sps: :Fail to get platform data\n");
  2348. return -ENODEV;
  2349. }
  2350. SPS_DBG(sps, "%s", "sps:get platform data\n");
  2351. bam_type = SPS_BAM_LEGACY;
  2352. }
  2353. /* Create Device */
  2354. sps->dev_class = class_create(THIS_MODULE, SPS_DRV_NAME);
  2355. ret = alloc_chrdev_region(&sps->dev_num, 0, 1, SPS_DRV_NAME);
  2356. if (ret) {
  2357. SPS_ERR(sps, "sps: alloc_chrdev_region err\n");
  2358. goto alloc_chrdev_region_err;
  2359. }
  2360. sps->dev = device_create(sps->dev_class, NULL, sps->dev_num, sps,
  2361. SPS_DRV_NAME);
  2362. if (IS_ERR(sps->dev)) {
  2363. SPS_ERR(sps, "sps: device_create err\n");
  2364. goto device_create_err;
  2365. }
  2366. if (pdev->dev.of_node)
  2367. sps->dev->of_node = pdev->dev.of_node;
  2368. if (!d_type) {
  2369. sps->pmem_clk = clk_get(sps->dev, "mem_clk");
  2370. if (IS_ERR(sps->pmem_clk)) {
  2371. if (PTR_ERR(sps->pmem_clk) == -EPROBE_DEFER)
  2372. ret = -EPROBE_DEFER;
  2373. else
  2374. SPS_ERR(sps, "sps: fail to get pmem_clk\n");
  2375. goto pmem_clk_err;
  2376. } else {
  2377. ret = clk_prepare_enable(sps->pmem_clk);
  2378. if (ret) {
  2379. SPS_ERR(sps,
  2380. "sps: failed to enable pmem_clk\n");
  2381. goto pmem_clk_en_err;
  2382. }
  2383. }
  2384. }
  2385. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2386. sps->dfab_clk = clk_get(sps->dev, "dfab_clk");
  2387. if (IS_ERR(sps->dfab_clk)) {
  2388. if (PTR_ERR(sps->dfab_clk) == -EPROBE_DEFER)
  2389. ret = -EPROBE_DEFER;
  2390. else
  2391. SPS_ERR(sps, "sps: fail to get dfab_clk\n");
  2392. goto dfab_clk_err;
  2393. } else {
  2394. ret = clk_set_rate(sps->dfab_clk, 64000000);
  2395. if (ret) {
  2396. SPS_ERR(sps, "sps: failed to set dfab_clk rate\n");
  2397. clk_put(sps->dfab_clk);
  2398. goto dfab_clk_err;
  2399. }
  2400. }
  2401. sps->bamdma_clk = clk_get(sps->dev, "dma_bam_pclk");
  2402. if (IS_ERR(sps->bamdma_clk)) {
  2403. if (PTR_ERR(sps->bamdma_clk) == -EPROBE_DEFER)
  2404. ret = -EPROBE_DEFER;
  2405. else
  2406. SPS_ERR(sps, "sps: fail to get bamdma_clk\n");
  2407. clk_put(sps->dfab_clk);
  2408. goto dfab_clk_err;
  2409. } else {
  2410. ret = clk_prepare_enable(sps->bamdma_clk);
  2411. if (ret) {
  2412. SPS_ERR(sps, "sps:failed to enable bamdma_clk ret=%d\n",
  2413. ret);
  2414. clk_put(sps->bamdma_clk);
  2415. clk_put(sps->dfab_clk);
  2416. goto dfab_clk_err;
  2417. }
  2418. }
  2419. ret = clk_prepare_enable(sps->dfab_clk);
  2420. if (ret) {
  2421. SPS_ERR(sps, "sps:failed to enable dfab_clk ret=%d\n", ret);
  2422. clk_disable_unprepare(sps->bamdma_clk);
  2423. clk_put(sps->bamdma_clk);
  2424. clk_put(sps->dfab_clk);
  2425. goto dfab_clk_err;
  2426. }
  2427. #endif
  2428. ret = sps_device_init();
  2429. if (ret) {
  2430. SPS_ERR(sps, "sps: sps_device_init err\n");
  2431. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2432. clk_disable_unprepare(sps->dfab_clk);
  2433. clk_disable_unprepare(sps->bamdma_clk);
  2434. clk_put(sps->bamdma_clk);
  2435. clk_put(sps->dfab_clk);
  2436. #endif
  2437. goto dfab_clk_err;
  2438. }
  2439. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2440. clk_disable_unprepare(sps->dfab_clk);
  2441. clk_disable_unprepare(sps->bamdma_clk);
  2442. #endif
  2443. sps->is_ready = true;
  2444. SPS_INFO(sps, "%s", "sps:sps is ready\n");
  2445. return 0;
  2446. dfab_clk_err:
  2447. if (!d_type)
  2448. clk_disable_unprepare(sps->pmem_clk);
  2449. pmem_clk_en_err:
  2450. if (!d_type)
  2451. clk_put(sps->pmem_clk);
  2452. pmem_clk_err:
  2453. device_destroy(sps->dev_class, sps->dev_num);
  2454. device_create_err:
  2455. unregister_chrdev_region(sps->dev_num, 1);
  2456. alloc_chrdev_region_err:
  2457. class_destroy(sps->dev_class);
  2458. return ret;
  2459. }
  2460. static int msm_sps_remove(struct platform_device *pdev)
  2461. {
  2462. SPS_DBG3(sps, "sps: Enter\n");
  2463. device_destroy(sps->dev_class, sps->dev_num);
  2464. unregister_chrdev_region(sps->dev_num, 1);
  2465. class_destroy(sps->dev_class);
  2466. sps_device_de_init();
  2467. clk_put(sps->dfab_clk);
  2468. if (!d_type)
  2469. clk_put(sps->pmem_clk);
  2470. clk_put(sps->bamdma_clk);
  2471. return 0;
  2472. }
  2473. static struct platform_driver msm_sps_driver = {
  2474. .probe = msm_sps_probe,
  2475. .driver = {
  2476. .name = SPS_DRV_NAME,
  2477. .of_match_table = msm_sps_match,
  2478. .suppress_bind_attrs = true,
  2479. },
  2480. .remove = msm_sps_remove,
  2481. };
  2482. /**
  2483. * Module Init.
  2484. */
  2485. static int __init sps_init(void)
  2486. {
  2487. int ret;
  2488. #ifdef CONFIG_DEBUG_FS
  2489. sps_debugfs_init();
  2490. #endif
  2491. pr_debug("sps:%s\n", __func__);
  2492. /* Allocate the SPS driver state struct */
  2493. sps = kzalloc(sizeof(*sps), GFP_KERNEL);
  2494. if (sps == NULL)
  2495. return -ENOMEM;
  2496. sps->ipc_log0 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  2497. "sps_ipc_log0", 0);
  2498. if (!sps->ipc_log0)
  2499. pr_err("Failed to create IPC log0\n");
  2500. sps->ipc_log1 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  2501. "sps_ipc_log1", 0);
  2502. if (!sps->ipc_log1)
  2503. pr_err("Failed to create IPC log1\n");
  2504. sps->ipc_log2 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  2505. "sps_ipc_log2", 0);
  2506. if (!sps->ipc_log2)
  2507. pr_err("Failed to create IPC log2\n");
  2508. sps->ipc_log3 = ipc_log_context_create(SPS_IPC_LOGPAGES,
  2509. "sps_ipc_log3", 0);
  2510. if (!sps->ipc_log3)
  2511. pr_err("Failed to create IPC log3\n");
  2512. sps->ipc_log4 = ipc_log_context_create(SPS_IPC_LOGPAGES *
  2513. SPS_IPC_REG_DUMP_FACTOR, "sps_ipc_log4", 0);
  2514. if (!sps->ipc_log4)
  2515. pr_err("Failed to create IPC log4\n");
  2516. ret = platform_driver_register(&msm_sps_driver);
  2517. return ret;
  2518. }
  2519. /**
  2520. * Module Exit.
  2521. */
  2522. static void __exit sps_exit(void)
  2523. {
  2524. pr_debug("sps:%s\n", __func__);
  2525. platform_driver_unregister(&msm_sps_driver);
  2526. kfree(sps);
  2527. sps = NULL;
  2528. #ifdef CONFIG_DEBUG_FS
  2529. sps_debugfs_exit();
  2530. #endif
  2531. }
  2532. arch_initcall(sps_init);
  2533. module_exit(sps_exit);
  2534. MODULE_LICENSE("GPL");
  2535. MODULE_DESCRIPTION("Smart Peripheral Switch (SPS)");