slimbus.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259
  1. /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/completion.h>
  17. #include <linux/idr.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slimbus/slimbus.h>
  20. #define SLIM_PORT_HDL(la, f, p) ((la)<<24 | (f) << 16 | (p))
  21. #define SLIM_HDL_TO_LA(hdl) ((u32)((hdl) & 0xFF000000) >> 24)
  22. #define SLIM_HDL_TO_FLOW(hdl) (((u32)(hdl) & 0xFF0000) >> 16)
  23. #define SLIM_HDL_TO_PORT(hdl) ((u32)(hdl) & 0xFF)
  24. #define SLIM_HDL_TO_CHIDX(hdl) ((u16)(hdl) & 0xFF)
  25. #define SLIM_GRP_TO_NCHAN(hdl) ((u16)(hdl >> 8) & 0xFF)
  26. #define SLIM_SLAVE_PORT(p, la) (((la)<<16) | (p))
  27. #define SLIM_MGR_PORT(p) ((0xFF << 16) | (p))
  28. #define SLIM_LA_MANAGER 0xFF
  29. #define SLIM_START_GRP (1 << 8)
  30. #define SLIM_END_GRP (1 << 9)
  31. #define SLIM_MAX_INTR_COEFF_3 (SLIM_SL_PER_SUPERFRAME/3)
  32. #define SLIM_MAX_INTR_COEFF_1 SLIM_SL_PER_SUPERFRAME
  33. static DEFINE_MUTEX(slim_lock);
  34. static DEFINE_IDR(ctrl_idr);
  35. static struct device_type slim_dev_type;
  36. static struct device_type slim_ctrl_type;
  37. extern unsigned int system_rev;
  38. static const struct slim_device_id *slim_match(const struct slim_device_id *id,
  39. const struct slim_device *slim_dev)
  40. {
  41. while (id->name[0]) {
  42. if (strncmp(slim_dev->name, id->name, SLIMBUS_NAME_SIZE) == 0)
  43. return id;
  44. id++;
  45. }
  46. return NULL;
  47. }
  48. static int slim_device_match(struct device *dev, struct device_driver *driver)
  49. {
  50. struct slim_device *slim_dev;
  51. struct slim_driver *drv = to_slim_driver(driver);
  52. if (dev->type == &slim_dev_type)
  53. slim_dev = to_slim_device(dev);
  54. else
  55. return 0;
  56. if (drv->id_table)
  57. return slim_match(drv->id_table, slim_dev) != NULL;
  58. if (driver->name)
  59. return strncmp(slim_dev->name, driver->name, SLIMBUS_NAME_SIZE)
  60. == 0;
  61. return 0;
  62. }
  63. #ifdef CONFIG_PM_SLEEP
  64. static int slim_legacy_suspend(struct device *dev, pm_message_t mesg)
  65. {
  66. struct slim_device *slim_dev = NULL;
  67. struct slim_driver *driver;
  68. if (dev->type == &slim_dev_type)
  69. slim_dev = to_slim_device(dev);
  70. if (!slim_dev || !dev->driver)
  71. return 0;
  72. driver = to_slim_driver(dev->driver);
  73. if (!driver->suspend)
  74. return 0;
  75. return driver->suspend(slim_dev, mesg);
  76. }
  77. static int slim_legacy_resume(struct device *dev)
  78. {
  79. struct slim_device *slim_dev = NULL;
  80. struct slim_driver *driver;
  81. if (dev->type == &slim_dev_type)
  82. slim_dev = to_slim_device(dev);
  83. if (!slim_dev || !dev->driver)
  84. return 0;
  85. driver = to_slim_driver(dev->driver);
  86. if (!driver->resume)
  87. return 0;
  88. return driver->resume(slim_dev);
  89. }
  90. static int slim_pm_suspend(struct device *dev)
  91. {
  92. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  93. if (pm)
  94. return pm_generic_suspend(dev);
  95. else
  96. return slim_legacy_suspend(dev, PMSG_SUSPEND);
  97. }
  98. static int slim_pm_resume(struct device *dev)
  99. {
  100. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  101. if (pm)
  102. return pm_generic_resume(dev);
  103. else
  104. return slim_legacy_resume(dev);
  105. }
  106. #else
  107. #define slim_pm_suspend NULL
  108. #define slim_pm_resume NULL
  109. #endif
  110. static const struct dev_pm_ops slimbus_pm = {
  111. .suspend = slim_pm_suspend,
  112. .resume = slim_pm_resume,
  113. SET_RUNTIME_PM_OPS(
  114. pm_generic_suspend,
  115. pm_generic_resume,
  116. pm_generic_runtime_idle
  117. )
  118. };
  119. struct bus_type slimbus_type = {
  120. .name = "slimbus",
  121. .match = slim_device_match,
  122. .pm = &slimbus_pm,
  123. };
  124. EXPORT_SYMBOL_GPL(slimbus_type);
  125. struct device slimbus_dev = {
  126. .init_name = "slimbus",
  127. };
  128. static void __exit slimbus_exit(void)
  129. {
  130. device_unregister(&slimbus_dev);
  131. bus_unregister(&slimbus_type);
  132. }
  133. static int __init slimbus_init(void)
  134. {
  135. int retval;
  136. retval = bus_register(&slimbus_type);
  137. if (!retval)
  138. retval = device_register(&slimbus_dev);
  139. if (retval)
  140. bus_unregister(&slimbus_type);
  141. return retval;
  142. }
  143. postcore_initcall(slimbus_init);
  144. module_exit(slimbus_exit);
  145. static int slim_drv_probe(struct device *dev)
  146. {
  147. const struct slim_driver *sdrv = to_slim_driver(dev->driver);
  148. struct slim_device *sbdev = to_slim_device(dev);
  149. struct slim_controller *ctrl = sbdev->ctrl;
  150. if (sdrv->probe) {
  151. int ret;
  152. ret = sdrv->probe(sbdev);
  153. if (ret)
  154. return ret;
  155. if (sdrv->device_up)
  156. queue_work(ctrl->wq, &sbdev->wd);
  157. return 0;
  158. }
  159. return -ENODEV;
  160. }
  161. static int slim_drv_remove(struct device *dev)
  162. {
  163. const struct slim_driver *sdrv = to_slim_driver(dev->driver);
  164. struct slim_device *sbdev = to_slim_device(dev);
  165. sbdev->notified = false;
  166. if (sdrv->remove)
  167. return sdrv->remove(to_slim_device(dev));
  168. return -ENODEV;
  169. }
  170. static void slim_drv_shutdown(struct device *dev)
  171. {
  172. const struct slim_driver *sdrv = to_slim_driver(dev->driver);
  173. if (sdrv->shutdown)
  174. sdrv->shutdown(to_slim_device(dev));
  175. }
  176. /*
  177. * slim_driver_register: Client driver registration with slimbus
  178. * @drv:Client driver to be associated with client-device.
  179. * This API will register the client driver with the slimbus
  180. * It is called from the driver's module-init function.
  181. */
  182. int slim_driver_register(struct slim_driver *drv)
  183. {
  184. drv->driver.bus = &slimbus_type;
  185. if (drv->probe)
  186. drv->driver.probe = slim_drv_probe;
  187. if (drv->remove)
  188. drv->driver.remove = slim_drv_remove;
  189. if (drv->shutdown)
  190. drv->driver.shutdown = slim_drv_shutdown;
  191. return driver_register(&drv->driver);
  192. }
  193. EXPORT_SYMBOL_GPL(slim_driver_register);
  194. /*
  195. * slim_driver_unregister: Undo effects of slim_driver_register
  196. * @drv: Client driver to be unregistered
  197. */
  198. void slim_driver_unregister(struct slim_driver *drv)
  199. {
  200. if (drv)
  201. driver_unregister(&drv->driver);
  202. }
  203. EXPORT_SYMBOL_GPL(slim_driver_unregister);
  204. #define slim_ctrl_attr_gr NULL
  205. static void slim_ctrl_release(struct device *dev)
  206. {
  207. struct slim_controller *ctrl = to_slim_controller(dev);
  208. complete(&ctrl->dev_released);
  209. }
  210. static struct device_type slim_ctrl_type = {
  211. .groups = slim_ctrl_attr_gr,
  212. .release = slim_ctrl_release,
  213. };
  214. static struct slim_controller *slim_ctrl_get(struct slim_controller *ctrl)
  215. {
  216. if (!ctrl || !get_device(&ctrl->dev))
  217. return NULL;
  218. return ctrl;
  219. }
  220. static void slim_ctrl_put(struct slim_controller *ctrl)
  221. {
  222. if (ctrl)
  223. put_device(&ctrl->dev);
  224. }
  225. #define slim_device_attr_gr NULL
  226. #define slim_device_uevent NULL
  227. static void slim_dev_release(struct device *dev)
  228. {
  229. struct slim_device *sbdev = to_slim_device(dev);
  230. slim_ctrl_put(sbdev->ctrl);
  231. }
  232. static struct device_type slim_dev_type = {
  233. .groups = slim_device_attr_gr,
  234. .uevent = slim_device_uevent,
  235. .release = slim_dev_release,
  236. };
  237. static void slim_report(struct work_struct *work)
  238. {
  239. struct slim_driver *sbdrv;
  240. struct slim_device *sbdev =
  241. container_of(work, struct slim_device, wd);
  242. if (!sbdev->dev.driver)
  243. return;
  244. /* check if device-up or down needs to be called */
  245. if ((!sbdev->reported && !sbdev->notified) ||
  246. (sbdev->reported && sbdev->notified))
  247. return;
  248. sbdrv = to_slim_driver(sbdev->dev.driver);
  249. /*
  250. * address no longer valid, means device reported absent, whereas
  251. * address valid, means device reported present
  252. */
  253. if (sbdev->notified && !sbdev->reported) {
  254. sbdev->notified = false;
  255. if (sbdrv->device_down)
  256. sbdrv->device_down(sbdev);
  257. } else if (!sbdev->notified && sbdev->reported) {
  258. sbdev->notified = true;
  259. if (sbdrv->device_up)
  260. sbdrv->device_up(sbdev);
  261. }
  262. }
  263. /*
  264. * slim_add_device: Add a new device without register board info.
  265. * @ctrl: Controller to which this device is to be added to.
  266. * Called when device doesn't have an explicit client-driver to be probed, or
  267. * the client-driver is a module installed dynamically.
  268. */
  269. int slim_add_device(struct slim_controller *ctrl, struct slim_device *sbdev)
  270. {
  271. sbdev->dev.bus = &slimbus_type;
  272. sbdev->dev.parent = ctrl->dev.parent;
  273. sbdev->dev.type = &slim_dev_type;
  274. sbdev->dev.driver = NULL;
  275. sbdev->ctrl = ctrl;
  276. slim_ctrl_get(ctrl);
  277. dev_set_name(&sbdev->dev, "%s", sbdev->name);
  278. mutex_init(&sbdev->sldev_reconf);
  279. INIT_LIST_HEAD(&sbdev->mark_define);
  280. INIT_LIST_HEAD(&sbdev->mark_suspend);
  281. INIT_LIST_HEAD(&sbdev->mark_removal);
  282. INIT_WORK(&sbdev->wd, slim_report);
  283. mutex_lock(&ctrl->m_ctrl);
  284. list_add_tail(&sbdev->dev_list, &ctrl->devs);
  285. mutex_unlock(&ctrl->m_ctrl);
  286. /* probe slave on this controller */
  287. return device_register(&sbdev->dev);
  288. }
  289. EXPORT_SYMBOL_GPL(slim_add_device);
  290. struct sbi_boardinfo {
  291. struct list_head list;
  292. struct slim_boardinfo board_info;
  293. };
  294. static LIST_HEAD(board_list);
  295. static LIST_HEAD(slim_ctrl_list);
  296. static DEFINE_MUTEX(board_lock);
  297. /* If controller is not present, only add to boards list */
  298. static void slim_match_ctrl_to_boardinfo(struct slim_controller *ctrl,
  299. struct slim_boardinfo *bi)
  300. {
  301. int ret;
  302. if (ctrl->nr != bi->bus_num)
  303. return;
  304. ret = slim_add_device(ctrl, bi->slim_slave);
  305. if (ret != 0)
  306. dev_err(ctrl->dev.parent, "can't create new device for %s\n",
  307. bi->slim_slave->name);
  308. }
  309. /*
  310. * slim_register_board_info: Board-initialization routine.
  311. * @info: List of all devices on all controllers present on the board.
  312. * @n: number of entries.
  313. * API enumerates respective devices on corresponding controller.
  314. * Called from board-init function.
  315. */
  316. int slim_register_board_info(struct slim_boardinfo const *info, unsigned n)
  317. {
  318. struct sbi_boardinfo *bi;
  319. int i;
  320. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  321. if (!bi)
  322. return -ENOMEM;
  323. for (i = 0; i < n; i++, bi++, info++) {
  324. struct slim_controller *ctrl;
  325. memcpy(&bi->board_info, info, sizeof(*info));
  326. mutex_lock(&board_lock);
  327. list_add_tail(&bi->list, &board_list);
  328. list_for_each_entry(ctrl, &slim_ctrl_list, list)
  329. slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
  330. mutex_unlock(&board_lock);
  331. }
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(slim_register_board_info);
  335. /*
  336. * slim_ctrl_add_boarddevs: Add devices registered by board-info
  337. * @ctrl: Controller to which these devices are to be added to.
  338. * This API is called by controller when it is up and running.
  339. * If devices on a controller were registered before controller,
  340. * this will make sure that they get probed when controller is up.
  341. */
  342. void slim_ctrl_add_boarddevs(struct slim_controller *ctrl)
  343. {
  344. struct sbi_boardinfo *bi;
  345. mutex_lock(&board_lock);
  346. list_add_tail(&ctrl->list, &slim_ctrl_list);
  347. list_for_each_entry(bi, &board_list, list)
  348. slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
  349. mutex_unlock(&board_lock);
  350. }
  351. EXPORT_SYMBOL_GPL(slim_ctrl_add_boarddevs);
  352. /*
  353. * slim_busnum_to_ctrl: Map bus number to controller
  354. * @busnum: Bus number
  355. * Returns controller representing this bus number
  356. */
  357. struct slim_controller *slim_busnum_to_ctrl(u32 bus_num)
  358. {
  359. struct slim_controller *ctrl;
  360. mutex_lock(&board_lock);
  361. list_for_each_entry(ctrl, &slim_ctrl_list, list)
  362. if (bus_num == ctrl->nr) {
  363. mutex_unlock(&board_lock);
  364. return ctrl;
  365. }
  366. mutex_unlock(&board_lock);
  367. return NULL;
  368. }
  369. EXPORT_SYMBOL_GPL(slim_busnum_to_ctrl);
  370. static int slim_register_controller(struct slim_controller *ctrl)
  371. {
  372. int ret = 0;
  373. /* Can't register until after driver model init */
  374. if (WARN_ON(!slimbus_type.p)) {
  375. ret = -EAGAIN;
  376. goto out_list;
  377. }
  378. dev_set_name(&ctrl->dev, "sb-%d", ctrl->nr);
  379. ctrl->dev.bus = &slimbus_type;
  380. ctrl->dev.type = &slim_ctrl_type;
  381. ctrl->num_dev = 0;
  382. if (!ctrl->min_cg)
  383. ctrl->min_cg = SLIM_MIN_CLK_GEAR;
  384. if (!ctrl->max_cg)
  385. ctrl->max_cg = SLIM_MAX_CLK_GEAR;
  386. mutex_init(&ctrl->m_ctrl);
  387. mutex_init(&ctrl->sched.m_reconf);
  388. ret = device_register(&ctrl->dev);
  389. if (ret)
  390. goto out_list;
  391. dev_dbg(&ctrl->dev, "Bus [%s] registered:dev:%x\n", ctrl->name,
  392. (u32)&ctrl->dev);
  393. if (ctrl->nports) {
  394. ctrl->ports = kzalloc(ctrl->nports * sizeof(struct slim_port),
  395. GFP_KERNEL);
  396. if (!ctrl->ports) {
  397. ret = -ENOMEM;
  398. goto err_port_failed;
  399. }
  400. }
  401. if (ctrl->nchans) {
  402. ctrl->chans = kzalloc(ctrl->nchans * sizeof(struct slim_ich),
  403. GFP_KERNEL);
  404. if (!ctrl->chans) {
  405. ret = -ENOMEM;
  406. goto err_chan_failed;
  407. }
  408. ctrl->sched.chc1 =
  409. kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
  410. GFP_KERNEL);
  411. if (!ctrl->sched.chc1) {
  412. kfree(ctrl->chans);
  413. ret = -ENOMEM;
  414. goto err_chan_failed;
  415. }
  416. ctrl->sched.chc3 =
  417. kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
  418. GFP_KERNEL);
  419. if (!ctrl->sched.chc3) {
  420. kfree(ctrl->sched.chc1);
  421. kfree(ctrl->chans);
  422. ret = -ENOMEM;
  423. goto err_chan_failed;
  424. }
  425. }
  426. #ifdef DEBUG
  427. ctrl->sched.slots = kzalloc(SLIM_SL_PER_SUPERFRAME, GFP_KERNEL);
  428. #endif
  429. init_completion(&ctrl->pause_comp);
  430. INIT_LIST_HEAD(&ctrl->devs);
  431. ctrl->wq = create_singlethread_workqueue(dev_name(&ctrl->dev));
  432. if (!ctrl->wq)
  433. goto err_workq_failed;
  434. return 0;
  435. err_workq_failed:
  436. kfree(ctrl->sched.chc3);
  437. kfree(ctrl->sched.chc1);
  438. kfree(ctrl->chans);
  439. err_chan_failed:
  440. kfree(ctrl->ports);
  441. err_port_failed:
  442. device_unregister(&ctrl->dev);
  443. out_list:
  444. mutex_lock(&slim_lock);
  445. idr_remove(&ctrl_idr, ctrl->nr);
  446. mutex_unlock(&slim_lock);
  447. return ret;
  448. }
  449. /* slim_remove_device: Remove the effect of slim_add_device() */
  450. void slim_remove_device(struct slim_device *sbdev)
  451. {
  452. device_unregister(&sbdev->dev);
  453. }
  454. EXPORT_SYMBOL_GPL(slim_remove_device);
  455. static void slim_ctrl_remove_device(struct slim_controller *ctrl,
  456. struct slim_boardinfo *bi)
  457. {
  458. if (ctrl->nr == bi->bus_num)
  459. slim_remove_device(bi->slim_slave);
  460. }
  461. /*
  462. * slim_del_controller: Controller tear-down.
  463. * Controller added with the above API is teared down using this API.
  464. */
  465. int slim_del_controller(struct slim_controller *ctrl)
  466. {
  467. struct slim_controller *found;
  468. struct sbi_boardinfo *bi;
  469. /* First make sure that this bus was added */
  470. mutex_lock(&slim_lock);
  471. found = idr_find(&ctrl_idr, ctrl->nr);
  472. mutex_unlock(&slim_lock);
  473. if (found != ctrl)
  474. return -EINVAL;
  475. /* Remove all clients */
  476. mutex_lock(&board_lock);
  477. list_for_each_entry(bi, &board_list, list)
  478. slim_ctrl_remove_device(ctrl, &bi->board_info);
  479. mutex_unlock(&board_lock);
  480. init_completion(&ctrl->dev_released);
  481. device_unregister(&ctrl->dev);
  482. wait_for_completion(&ctrl->dev_released);
  483. list_del(&ctrl->list);
  484. destroy_workqueue(ctrl->wq);
  485. /* free bus id */
  486. mutex_lock(&slim_lock);
  487. idr_remove(&ctrl_idr, ctrl->nr);
  488. mutex_unlock(&slim_lock);
  489. kfree(ctrl->sched.chc1);
  490. kfree(ctrl->sched.chc3);
  491. #ifdef DEBUG
  492. kfree(ctrl->sched.slots);
  493. #endif
  494. kfree(ctrl->chans);
  495. kfree(ctrl->ports);
  496. return 0;
  497. }
  498. EXPORT_SYMBOL_GPL(slim_del_controller);
  499. /*
  500. * slim_add_numbered_controller: Controller bring-up.
  501. * @ctrl: Controller to be registered.
  502. * A controller is registered with the framework using this API. ctrl->nr is the
  503. * desired number with which slimbus framework registers the controller.
  504. * Function will return -EBUSY if the number is in use.
  505. */
  506. int slim_add_numbered_controller(struct slim_controller *ctrl)
  507. {
  508. int id;
  509. int status;
  510. if (ctrl->nr & ~MAX_ID_MASK)
  511. return -EINVAL;
  512. retry:
  513. if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0)
  514. return -ENOMEM;
  515. mutex_lock(&slim_lock);
  516. status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
  517. if (status == 0 && id != ctrl->nr) {
  518. status = -EAGAIN;
  519. idr_remove(&ctrl_idr, id);
  520. }
  521. mutex_unlock(&slim_lock);
  522. if (status == -EAGAIN)
  523. goto retry;
  524. if (status == 0)
  525. status = slim_register_controller(ctrl);
  526. return status;
  527. }
  528. EXPORT_SYMBOL_GPL(slim_add_numbered_controller);
  529. /*
  530. * slim_report_absent: Controller calls this function when a device
  531. * reports absent, OR when the device cannot be communicated with
  532. * @sbdev: Device that cannot be reached, or sent report absent
  533. */
  534. void slim_report_absent(struct slim_device *sbdev)
  535. {
  536. struct slim_controller *ctrl;
  537. int i;
  538. if (!sbdev)
  539. return;
  540. ctrl = sbdev->ctrl;
  541. if (!ctrl)
  542. return;
  543. /* invalidate logical addresses */
  544. mutex_lock(&ctrl->m_ctrl);
  545. for (i = 0; i < ctrl->num_dev; i++) {
  546. if (sbdev->laddr == ctrl->addrt[i].laddr)
  547. ctrl->addrt[i].valid = false;
  548. }
  549. mutex_unlock(&ctrl->m_ctrl);
  550. sbdev->reported = false;
  551. queue_work(ctrl->wq, &sbdev->wd);
  552. }
  553. EXPORT_SYMBOL(slim_report_absent);
  554. /*
  555. * slim_framer_booted: This function is called by controller after the active
  556. * framer has booted (using Bus Reset sequence, or after it has shutdown and has
  557. * come back up). Components, devices on the bus may be in undefined state,
  558. * and this function triggers their drivers to do the needful
  559. * to bring them back in Reset state so that they can acquire sync, report
  560. * present and be operational again.
  561. */
  562. void slim_framer_booted(struct slim_controller *ctrl)
  563. {
  564. struct slim_device *sbdev;
  565. struct list_head *pos, *next;
  566. if (!ctrl)
  567. return;
  568. mutex_lock(&ctrl->m_ctrl);
  569. list_for_each_safe(pos, next, &ctrl->devs) {
  570. struct slim_driver *sbdrv;
  571. sbdev = list_entry(pos, struct slim_device, dev_list);
  572. mutex_unlock(&ctrl->m_ctrl);
  573. if (sbdev && sbdev->dev.driver) {
  574. sbdrv = to_slim_driver(sbdev->dev.driver);
  575. if (sbdrv->reset_device)
  576. sbdrv->reset_device(sbdev);
  577. }
  578. mutex_lock(&ctrl->m_ctrl);
  579. }
  580. mutex_unlock(&ctrl->m_ctrl);
  581. }
  582. EXPORT_SYMBOL(slim_framer_booted);
  583. /*
  584. * slim_msg_response: Deliver Message response received from a device to the
  585. * framework.
  586. * @ctrl: Controller handle
  587. * @reply: Reply received from the device
  588. * @len: Length of the reply
  589. * @tid: Transaction ID received with which framework can associate reply.
  590. * Called by controller to inform framework about the response received.
  591. * This helps in making the API asynchronous, and controller-driver doesn't need
  592. * to manage 1 more table other than the one managed by framework mapping TID
  593. * with buffers
  594. */
  595. void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
  596. {
  597. int i;
  598. struct slim_msg_txn *txn;
  599. mutex_lock(&ctrl->m_ctrl);
  600. txn = ctrl->txnt[tid];
  601. if (txn == NULL || txn->rbuf == NULL) {
  602. if (txn == NULL)
  603. dev_err(&ctrl->dev, "Got response to invalid TID:%d, len:%d",
  604. tid, len);
  605. else
  606. dev_err(&ctrl->dev, "Invalid client buffer passed\n");
  607. mutex_unlock(&ctrl->m_ctrl);
  608. return;
  609. }
  610. for (i = 0; i < len; i++)
  611. txn->rbuf[i] = reply[i];
  612. if (txn->comp)
  613. complete(txn->comp);
  614. ctrl->txnt[tid] = NULL;
  615. mutex_unlock(&ctrl->m_ctrl);
  616. kfree(txn);
  617. }
  618. EXPORT_SYMBOL_GPL(slim_msg_response);
  619. static int slim_processtxn(struct slim_controller *ctrl, u8 dt, u16 mc, u16 ec,
  620. u8 mt, u8 *rbuf, const u8 *wbuf, u8 len, u8 mlen,
  621. struct completion *comp, u8 la, u8 *tid)
  622. {
  623. u8 i = 0;
  624. int ret = 0;
  625. struct slim_msg_txn *txn = kmalloc(sizeof(struct slim_msg_txn),
  626. GFP_KERNEL);
  627. if (!txn)
  628. return -ENOMEM;
  629. if (tid) {
  630. mutex_lock(&ctrl->m_ctrl);
  631. for (i = 0; i < ctrl->last_tid; i++) {
  632. if (ctrl->txnt[i] == NULL)
  633. break;
  634. }
  635. if (i >= ctrl->last_tid) {
  636. if (ctrl->last_tid == 255) {
  637. mutex_unlock(&ctrl->m_ctrl);
  638. kfree(txn);
  639. return -ENOMEM;
  640. }
  641. ctrl->txnt = krealloc(ctrl->txnt,
  642. (i + 1) * sizeof(struct slim_msg_txn *),
  643. GFP_KERNEL);
  644. if (!ctrl->txnt) {
  645. mutex_unlock(&ctrl->m_ctrl);
  646. kfree(txn);
  647. return -ENOMEM;
  648. }
  649. ctrl->last_tid++;
  650. }
  651. ctrl->txnt[i] = txn;
  652. mutex_unlock(&ctrl->m_ctrl);
  653. txn->tid = i;
  654. *tid = i;
  655. }
  656. txn->mc = mc;
  657. txn->mt = mt;
  658. txn->dt = dt;
  659. txn->ec = ec;
  660. txn->la = la;
  661. txn->rbuf = rbuf;
  662. txn->wbuf = wbuf;
  663. txn->rl = mlen;
  664. txn->len = len;
  665. txn->comp = comp;
  666. ret = ctrl->xfer_msg(ctrl, txn);
  667. if (!tid)
  668. kfree(txn);
  669. return ret;
  670. }
  671. static int ctrl_getlogical_addr(struct slim_controller *ctrl, const u8 *eaddr,
  672. u8 e_len, u8 *entry)
  673. {
  674. u8 i;
  675. for (i = 0; i < ctrl->num_dev; i++) {
  676. if (ctrl->addrt[i].valid &&
  677. memcmp(ctrl->addrt[i].eaddr, eaddr, e_len) == 0) {
  678. *entry = i;
  679. return 0;
  680. }
  681. }
  682. return -ENXIO;
  683. }
  684. /*
  685. * slim_assign_laddr: Assign logical address to a device enumerated.
  686. * @ctrl: Controller with which device is enumerated.
  687. * @e_addr: 6-byte elemental address of the device.
  688. * @e_len: buffer length for e_addr
  689. * @laddr: Return logical address (if valid flag is false)
  690. * @valid: true if laddr holds a valid address that controller wants to
  691. * set for this enumeration address. Otherwise framework sets index into
  692. * address table as logical address.
  693. * Called by controller in response to REPORT_PRESENT. Framework will assign
  694. * a logical address to this enumeration address.
  695. * Function returns -EXFULL to indicate that all logical addresses are already
  696. * taken.
  697. */
  698. int slim_assign_laddr(struct slim_controller *ctrl, const u8 *e_addr,
  699. u8 e_len, u8 *laddr, bool valid)
  700. {
  701. int ret;
  702. u8 i = 0;
  703. bool exists = false;
  704. struct slim_device *sbdev;
  705. struct list_head *pos, *next;
  706. mutex_lock(&ctrl->m_ctrl);
  707. /* already assigned */
  708. if (ctrl_getlogical_addr(ctrl, e_addr, e_len, &i) == 0) {
  709. *laddr = ctrl->addrt[i].laddr;
  710. exists = true;
  711. } else {
  712. if (ctrl->num_dev >= 254) {
  713. ret = -EXFULL;
  714. goto ret_assigned_laddr;
  715. }
  716. for (i = 0; i < ctrl->num_dev; i++) {
  717. if (ctrl->addrt[i].valid == false)
  718. break;
  719. }
  720. if (i == ctrl->num_dev) {
  721. ctrl->addrt = krealloc(ctrl->addrt,
  722. (ctrl->num_dev + 1) *
  723. sizeof(struct slim_addrt),
  724. GFP_KERNEL);
  725. if (!ctrl->addrt) {
  726. ret = -ENOMEM;
  727. goto ret_assigned_laddr;
  728. }
  729. ctrl->num_dev++;
  730. }
  731. memcpy(ctrl->addrt[i].eaddr, e_addr, e_len);
  732. ctrl->addrt[i].valid = true;
  733. /* Preferred address is index into table */
  734. if (!valid)
  735. *laddr = i;
  736. }
  737. ret = ctrl->set_laddr(ctrl, (const u8 *)&ctrl->addrt[i].eaddr, 6,
  738. *laddr);
  739. if (ret) {
  740. ctrl->addrt[i].valid = false;
  741. goto ret_assigned_laddr;
  742. }
  743. ctrl->addrt[i].laddr = *laddr;
  744. dev_dbg(&ctrl->dev, "setting slimbus l-addr:%x\n", *laddr);
  745. ret_assigned_laddr:
  746. mutex_unlock(&ctrl->m_ctrl);
  747. if (exists || ret)
  748. return ret;
  749. pr_info("slimbus:%d laddr:0x%x, EAPC:0x%x:0x%x", ctrl->nr, *laddr,
  750. e_addr[1], e_addr[2]);
  751. mutex_lock(&ctrl->m_ctrl);
  752. list_for_each_safe(pos, next, &ctrl->devs) {
  753. sbdev = list_entry(pos, struct slim_device, dev_list);
  754. if (memcmp(sbdev->e_addr, e_addr, 6) == 0) {
  755. struct slim_driver *sbdrv;
  756. sbdev->laddr = *laddr;
  757. sbdev->reported = true;
  758. if (sbdev->dev.driver) {
  759. sbdrv = to_slim_driver(sbdev->dev.driver);
  760. if (sbdrv->device_up)
  761. queue_work(ctrl->wq, &sbdev->wd);
  762. }
  763. break;
  764. }
  765. }
  766. mutex_unlock(&ctrl->m_ctrl);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL_GPL(slim_assign_laddr);
  770. /*
  771. * slim_get_logical_addr: Return the logical address of a slimbus device.
  772. * @sb: client handle requesting the adddress.
  773. * @e_addr: Elemental address of the device.
  774. * @e_len: Length of e_addr
  775. * @laddr: output buffer to store the address
  776. * context: can sleep
  777. * -EINVAL is returned in case of invalid parameters, and -ENXIO is returned if
  778. * the device with this elemental address is not found.
  779. */
  780. int slim_get_logical_addr(struct slim_device *sb, const u8 *e_addr,
  781. u8 e_len, u8 *laddr)
  782. {
  783. int ret = 0;
  784. u8 entry;
  785. struct slim_controller *ctrl = sb->ctrl;
  786. if (!ctrl || !laddr || !e_addr || e_len != 6)
  787. return -EINVAL;
  788. mutex_lock(&ctrl->m_ctrl);
  789. ret = ctrl_getlogical_addr(ctrl, e_addr, e_len, &entry);
  790. if (!ret)
  791. *laddr = ctrl->addrt[entry].laddr;
  792. mutex_unlock(&ctrl->m_ctrl);
  793. if (ret == -ENXIO && ctrl->get_laddr) {
  794. ret = ctrl->get_laddr(ctrl, e_addr, e_len, laddr);
  795. if (!ret)
  796. ret = slim_assign_laddr(ctrl, e_addr, e_len, laddr,
  797. true);
  798. }
  799. return ret;
  800. }
  801. EXPORT_SYMBOL_GPL(slim_get_logical_addr);
  802. static int slim_ele_access_sanity(struct slim_ele_access *msg, int oper,
  803. u8 *rbuf, const u8 *wbuf, u8 len)
  804. {
  805. if (!msg || msg->num_bytes > 16 || msg->start_offset + len > 0xC00)
  806. return -EINVAL;
  807. switch (oper) {
  808. case SLIM_MSG_MC_REQUEST_VALUE:
  809. case SLIM_MSG_MC_REQUEST_INFORMATION:
  810. if (rbuf == NULL)
  811. return -EINVAL;
  812. return 0;
  813. case SLIM_MSG_MC_CHANGE_VALUE:
  814. case SLIM_MSG_MC_CLEAR_INFORMATION:
  815. if (wbuf == NULL)
  816. return -EINVAL;
  817. return 0;
  818. case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
  819. case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
  820. if (rbuf == NULL || wbuf == NULL)
  821. return -EINVAL;
  822. return 0;
  823. default:
  824. return -EINVAL;
  825. }
  826. }
  827. static u16 slim_slicecodefromsize(u32 req)
  828. {
  829. u8 codetosize[8] = {1, 2, 3, 4, 6, 8, 12, 16};
  830. if (req >= 8)
  831. return 0;
  832. else
  833. return codetosize[req];
  834. }
  835. static u16 slim_slicesize(u32 code)
  836. {
  837. u8 sizetocode[16] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7};
  838. if (code == 0)
  839. code = 1;
  840. if (code > 16)
  841. code = 16;
  842. return sizetocode[code - 1];
  843. }
  844. /* Message APIs Unicast message APIs used by slimbus slave drivers */
  845. /*
  846. * Message API access routines.
  847. * @sb: client handle requesting elemental message reads, writes.
  848. * @msg: Input structure for start-offset, number of bytes to read.
  849. * @rbuf: data buffer to be filled with values read.
  850. * @len: data buffer size
  851. * @wbuf: data buffer containing value/information to be written
  852. * context: can sleep
  853. * Returns:
  854. * -EINVAL: Invalid parameters
  855. * -ETIMEDOUT: If controller could not complete the request. This may happen if
  856. * the bus lines are not clocked, controller is not powered-on, slave with
  857. * given address is not enumerated/responding.
  858. */
  859. int slim_request_val_element(struct slim_device *sb,
  860. struct slim_ele_access *msg, u8 *buf, u8 len)
  861. {
  862. struct slim_controller *ctrl = sb->ctrl;
  863. if (!ctrl)
  864. return -EINVAL;
  865. return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_VALUE, buf,
  866. NULL, len);
  867. }
  868. EXPORT_SYMBOL_GPL(slim_request_val_element);
  869. int slim_request_inf_element(struct slim_device *sb,
  870. struct slim_ele_access *msg, u8 *buf, u8 len)
  871. {
  872. struct slim_controller *ctrl = sb->ctrl;
  873. if (!ctrl)
  874. return -EINVAL;
  875. return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_INFORMATION,
  876. buf, NULL, len);
  877. }
  878. EXPORT_SYMBOL_GPL(slim_request_inf_element);
  879. int slim_change_val_element(struct slim_device *sb, struct slim_ele_access *msg,
  880. const u8 *buf, u8 len)
  881. {
  882. struct slim_controller *ctrl = sb->ctrl;
  883. if (!ctrl)
  884. return -EINVAL;
  885. return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CHANGE_VALUE, NULL, buf,
  886. len);
  887. }
  888. EXPORT_SYMBOL_GPL(slim_change_val_element);
  889. int slim_clear_inf_element(struct slim_device *sb, struct slim_ele_access *msg,
  890. u8 *buf, u8 len)
  891. {
  892. struct slim_controller *ctrl = sb->ctrl;
  893. if (!ctrl)
  894. return -EINVAL;
  895. return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CLEAR_INFORMATION, NULL,
  896. buf, len);
  897. }
  898. EXPORT_SYMBOL_GPL(slim_clear_inf_element);
  899. int slim_request_change_val_element(struct slim_device *sb,
  900. struct slim_ele_access *msg, u8 *rbuf,
  901. const u8 *wbuf, u8 len)
  902. {
  903. struct slim_controller *ctrl = sb->ctrl;
  904. if (!ctrl)
  905. return -EINVAL;
  906. return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_CHANGE_VALUE,
  907. rbuf, wbuf, len);
  908. }
  909. EXPORT_SYMBOL_GPL(slim_request_change_val_element);
  910. int slim_request_clear_inf_element(struct slim_device *sb,
  911. struct slim_ele_access *msg, u8 *rbuf,
  912. const u8 *wbuf, u8 len)
  913. {
  914. struct slim_controller *ctrl = sb->ctrl;
  915. if (!ctrl)
  916. return -EINVAL;
  917. return slim_xfer_msg(ctrl, sb, msg,
  918. SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION,
  919. rbuf, wbuf, len);
  920. }
  921. EXPORT_SYMBOL_GPL(slim_request_clear_inf_element);
  922. /*
  923. * Broadcast message API:
  924. * call this API directly with sbdev = NULL.
  925. * For broadcast reads, make sure that buffers are big-enough to incorporate
  926. * replies from all logical addresses.
  927. * All controllers may not support broadcast
  928. */
  929. int slim_xfer_msg(struct slim_controller *ctrl, struct slim_device *sbdev,
  930. struct slim_ele_access *msg, u16 mc, u8 *rbuf,
  931. const u8 *wbuf, u8 len)
  932. {
  933. DECLARE_COMPLETION_ONSTACK(complete);
  934. int ret;
  935. u16 sl, cur;
  936. u16 ec;
  937. u8 tid, mlen = 6;
  938. ret = slim_ele_access_sanity(msg, mc, rbuf, wbuf, len);
  939. if (ret)
  940. goto xfer_err;
  941. sl = slim_slicesize(len);
  942. dev_dbg(&ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
  943. msg->start_offset, len, mc, sl);
  944. cur = slim_slicecodefromsize(sl);
  945. ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
  946. if (wbuf)
  947. mlen += len;
  948. if (rbuf) {
  949. mlen++;
  950. if (!msg->comp)
  951. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
  952. mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
  953. &complete, sbdev->laddr, &tid);
  954. else
  955. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
  956. mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
  957. msg->comp, sbdev->laddr, &tid);
  958. /* sync read */
  959. if (!ret && !msg->comp) {
  960. ret = wait_for_completion_timeout(&complete, HZ);
  961. if (!ret) {
  962. struct slim_msg_txn *txn;
  963. dev_err(&ctrl->dev, "slimbus Read timed out");
  964. mutex_lock(&ctrl->m_ctrl);
  965. txn = ctrl->txnt[tid];
  966. /* Invalidate the transaction */
  967. ctrl->txnt[tid] = NULL;
  968. mutex_unlock(&ctrl->m_ctrl);
  969. kfree(txn);
  970. ret = -ETIMEDOUT;
  971. } else
  972. ret = 0;
  973. } else if (ret < 0 && !msg->comp) {
  974. struct slim_msg_txn *txn;
  975. dev_err(&ctrl->dev, "slimbus Read error");
  976. mutex_lock(&ctrl->m_ctrl);
  977. txn = ctrl->txnt[tid];
  978. /* Invalidate the transaction */
  979. ctrl->txnt[tid] = NULL;
  980. mutex_unlock(&ctrl->m_ctrl);
  981. kfree(txn);
  982. }
  983. } else
  984. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, ec,
  985. SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
  986. msg->comp, sbdev->laddr, NULL);
  987. xfer_err:
  988. return ret;
  989. }
  990. EXPORT_SYMBOL_GPL(slim_xfer_msg);
  991. /*
  992. * User message:
  993. * slim_user_msg: Send user message that is interpreted by destination device
  994. * @sb: Client handle sending the message
  995. * @la: Destination device for this user message
  996. * @mt: Message Type (Soruce-referred, or Destination-referred)
  997. * @mc: Message Code
  998. * @msg: Message structure (start offset, number of bytes) to be sent
  999. * @buf: data buffer to be sent
  1000. * @len: data buffer size in bytes
  1001. */
  1002. int slim_user_msg(struct slim_device *sb, u8 la, u8 mt, u8 mc,
  1003. struct slim_ele_access *msg, u8 *buf, u8 len)
  1004. {
  1005. if (!sb || !sb->ctrl || !msg || mt == SLIM_MSG_MT_CORE)
  1006. return -EINVAL;
  1007. if (!sb->ctrl->xfer_user_msg)
  1008. return -EPROTONOSUPPORT;
  1009. return sb->ctrl->xfer_user_msg(sb->ctrl, la, mt, mc, msg, buf, len);
  1010. }
  1011. EXPORT_SYMBOL(slim_user_msg);
  1012. /*
  1013. * slim_alloc_mgrports: Allocate port on manager side.
  1014. * @sb: device/client handle.
  1015. * @req: Port request type.
  1016. * @nports: Number of ports requested
  1017. * @rh: output buffer to store the port handles
  1018. * @hsz: size of buffer storing handles
  1019. * context: can sleep
  1020. * This port will be typically used by SW. e.g. client driver wants to receive
  1021. * some data from audio codec HW using a data channel.
  1022. * Port allocated using this API will be used to receive the data.
  1023. * If half-duplex ports are requested, two adjacent ports are allocated for
  1024. * 1 half-duplex port. So the handle-buffer size should be twice the number
  1025. * of half-duplex ports to be allocated.
  1026. * -EDQUOT is returned if all ports are in use.
  1027. */
  1028. int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
  1029. int nports, u32 *rh, int hsz)
  1030. {
  1031. int i, j;
  1032. int ret = -EINVAL;
  1033. int nphysp = nports;
  1034. struct slim_controller *ctrl = sb->ctrl;
  1035. if (!rh || !ctrl)
  1036. return -EINVAL;
  1037. if (req == SLIM_REQ_HALF_DUP)
  1038. nphysp *= 2;
  1039. if (hsz/sizeof(u32) < nphysp)
  1040. return -EINVAL;
  1041. mutex_lock(&ctrl->m_ctrl);
  1042. for (i = 0; i < ctrl->nports; i++) {
  1043. bool multiok = true;
  1044. if (ctrl->ports[i].state != SLIM_P_FREE)
  1045. continue;
  1046. /* Start half duplex channel at even port */
  1047. if (req == SLIM_REQ_HALF_DUP && (i % 2))
  1048. continue;
  1049. /* Allocate ports contiguously for multi-ch */
  1050. if (ctrl->nports < (i + nphysp)) {
  1051. i = ctrl->nports;
  1052. break;
  1053. }
  1054. if (req == SLIM_REQ_MULTI_CH) {
  1055. multiok = true;
  1056. for (j = i; j < i + nphysp; j++) {
  1057. if (ctrl->ports[j].state != SLIM_P_FREE) {
  1058. multiok = false;
  1059. break;
  1060. }
  1061. }
  1062. if (!multiok)
  1063. continue;
  1064. }
  1065. break;
  1066. }
  1067. if (i >= ctrl->nports) {
  1068. ret = -EDQUOT;
  1069. goto alloc_err;
  1070. }
  1071. ret = 0;
  1072. for (j = i; j < i + nphysp; j++) {
  1073. ctrl->ports[j].state = SLIM_P_UNCFG;
  1074. ctrl->ports[j].req = req;
  1075. if (req == SLIM_REQ_HALF_DUP && (j % 2))
  1076. ctrl->ports[j].flow = SLIM_SINK;
  1077. else
  1078. ctrl->ports[j].flow = SLIM_SRC;
  1079. if (ctrl->alloc_port)
  1080. ret = ctrl->alloc_port(ctrl, j);
  1081. if (ret) {
  1082. for (; j >= i; j--)
  1083. ctrl->ports[j].state = SLIM_P_FREE;
  1084. goto alloc_err;
  1085. }
  1086. *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
  1087. }
  1088. alloc_err:
  1089. mutex_unlock(&ctrl->m_ctrl);
  1090. return ret;
  1091. }
  1092. EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
  1093. /* Deallocate the port(s) allocated using the API above */
  1094. int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
  1095. {
  1096. int i;
  1097. struct slim_controller *ctrl = sb->ctrl;
  1098. if (!ctrl || !hdl)
  1099. return -EINVAL;
  1100. mutex_lock(&ctrl->m_ctrl);
  1101. for (i = 0; i < nports; i++) {
  1102. u8 pn;
  1103. pn = SLIM_HDL_TO_PORT(hdl[i]);
  1104. if (pn >= ctrl->nports || ctrl->ports[pn].state == SLIM_P_CFG) {
  1105. int j, ret;
  1106. if (pn >= ctrl->nports) {
  1107. dev_err(&ctrl->dev, "invalid port number");
  1108. ret = -EINVAL;
  1109. } else {
  1110. dev_err(&ctrl->dev,
  1111. "Can't dealloc connected port:%d", i);
  1112. ret = -EISCONN;
  1113. }
  1114. for (j = i - 1; j >= 0; j--) {
  1115. pn = SLIM_HDL_TO_PORT(hdl[j]);
  1116. ctrl->ports[pn].state = SLIM_P_UNCFG;
  1117. }
  1118. mutex_unlock(&ctrl->m_ctrl);
  1119. return ret;
  1120. }
  1121. if (ctrl->dealloc_port)
  1122. ctrl->dealloc_port(ctrl, pn);
  1123. ctrl->ports[pn].state = SLIM_P_FREE;
  1124. }
  1125. mutex_unlock(&ctrl->m_ctrl);
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
  1129. /*
  1130. * slim_get_slaveport: Get slave port handle
  1131. * @la: slave device logical address.
  1132. * @idx: port index at slave
  1133. * @rh: return handle
  1134. * @flw: Flow type (source or destination)
  1135. * This API only returns a slave port's representation as expected by slimbus
  1136. * driver. This port is not managed by the slimbus driver. Caller is expected
  1137. * to have visibility of this port since it's a device-port.
  1138. */
  1139. int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
  1140. {
  1141. if (rh == NULL)
  1142. return -EINVAL;
  1143. *rh = SLIM_PORT_HDL(la, flw, idx);
  1144. return 0;
  1145. }
  1146. EXPORT_SYMBOL_GPL(slim_get_slaveport);
  1147. static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
  1148. enum slim_port_flow flow)
  1149. {
  1150. int ret;
  1151. u16 mc;
  1152. u8 buf[2];
  1153. u32 la = SLIM_HDL_TO_LA(ph);
  1154. u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
  1155. if (flow == SLIM_SRC)
  1156. mc = SLIM_MSG_MC_CONNECT_SOURCE;
  1157. else
  1158. mc = SLIM_MSG_MC_CONNECT_SINK;
  1159. buf[0] = pn;
  1160. buf[1] = ctrl->chans[ch].chan;
  1161. if (la == SLIM_LA_MANAGER)
  1162. ctrl->ports[pn].flow = flow;
  1163. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
  1164. SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
  1165. NULL);
  1166. if (!ret && la == SLIM_LA_MANAGER)
  1167. ctrl->ports[pn].state = SLIM_P_CFG;
  1168. return ret;
  1169. }
  1170. static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
  1171. {
  1172. int ret;
  1173. u16 mc;
  1174. u32 la = SLIM_HDL_TO_LA(ph);
  1175. u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
  1176. mc = SLIM_MSG_MC_DISCONNECT_PORT;
  1177. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
  1178. SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
  1179. NULL, la, NULL);
  1180. if (ret)
  1181. return ret;
  1182. if (la == SLIM_LA_MANAGER)
  1183. ctrl->ports[pn].state = SLIM_P_UNCFG;
  1184. return 0;
  1185. }
  1186. /*
  1187. * slim_connect_src: Connect source port to channel.
  1188. * @sb: client handle
  1189. * @srch: source handle to be connected to this channel
  1190. * @chanh: Channel with which the ports need to be associated with.
  1191. * Per slimbus specification, a channel may have 1 source port.
  1192. * Channel specified in chanh needs to be allocated first.
  1193. * Returns -EALREADY if source is already configured for this channel.
  1194. * Returns -ENOTCONN if channel is not allocated
  1195. * Returns -EINVAL if invalid direction is specified for non-manager port,
  1196. * or if the manager side port number is out of bounds, or in incorrect state
  1197. */
  1198. int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
  1199. {
  1200. struct slim_controller *ctrl = sb->ctrl;
  1201. int ret;
  1202. u8 chan = SLIM_HDL_TO_CHIDX(chanh);
  1203. struct slim_ich *slc = &ctrl->chans[chan];
  1204. enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
  1205. u8 la = SLIM_HDL_TO_LA(srch);
  1206. /* manager ports don't have direction when they are allocated */
  1207. if (la != SLIM_LA_MANAGER && flow != SLIM_SRC)
  1208. return -EINVAL;
  1209. mutex_lock(&ctrl->sched.m_reconf);
  1210. if (la == SLIM_LA_MANAGER) {
  1211. u8 pn = SLIM_HDL_TO_PORT(srch);
  1212. if (pn >= ctrl->nports ||
  1213. ctrl->ports[pn].state != SLIM_P_UNCFG) {
  1214. ret = -EINVAL;
  1215. goto connect_src_err;
  1216. }
  1217. }
  1218. if (slc->state == SLIM_CH_FREE) {
  1219. ret = -ENOTCONN;
  1220. goto connect_src_err;
  1221. }
  1222. /*
  1223. * Once channel is removed, its ports can be considered disconnected
  1224. * So its ports can be reassigned. Source port is zeroed
  1225. * when channel is deallocated.
  1226. */
  1227. if (slc->srch) {
  1228. ret = -EALREADY;
  1229. goto connect_src_err;
  1230. }
  1231. ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
  1232. if (!ret)
  1233. slc->srch = srch;
  1234. connect_src_err:
  1235. mutex_unlock(&ctrl->sched.m_reconf);
  1236. return ret;
  1237. }
  1238. EXPORT_SYMBOL_GPL(slim_connect_src);
  1239. /*
  1240. * slim_connect_sink: Connect sink port(s) to channel.
  1241. * @sb: client handle
  1242. * @sinkh: sink handle(s) to be connected to this channel
  1243. * @nsink: number of sinks
  1244. * @chanh: Channel with which the ports need to be associated with.
  1245. * Per slimbus specification, a channel may have multiple sink-ports.
  1246. * Channel specified in chanh needs to be allocated first.
  1247. * Returns -EALREADY if sink is already configured for this channel.
  1248. * Returns -ENOTCONN if channel is not allocated
  1249. * Returns -EINVAL if invalid parameters are passed, or invalid direction is
  1250. * specified for non-manager port, or if the manager side port number is out of
  1251. * bounds, or in incorrect state
  1252. */
  1253. int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
  1254. {
  1255. struct slim_controller *ctrl = sb->ctrl;
  1256. int j;
  1257. int ret = 0;
  1258. u8 chan = SLIM_HDL_TO_CHIDX(chanh);
  1259. struct slim_ich *slc = &ctrl->chans[chan];
  1260. if (!sinkh || !nsink)
  1261. return -EINVAL;
  1262. mutex_lock(&ctrl->sched.m_reconf);
  1263. /*
  1264. * Once channel is removed, its ports can be considered disconnected
  1265. * So its ports can be reassigned. Sink ports are freed when channel
  1266. * is deallocated.
  1267. */
  1268. if (slc->state == SLIM_CH_FREE) {
  1269. ret = -ENOTCONN;
  1270. goto connect_sink_err;
  1271. }
  1272. for (j = 0; j < nsink; j++) {
  1273. enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
  1274. u8 la = SLIM_HDL_TO_LA(sinkh[j]);
  1275. u8 pn = SLIM_HDL_TO_PORT(sinkh[j]);
  1276. if (la != SLIM_LA_MANAGER && flow != SLIM_SINK)
  1277. ret = -EINVAL;
  1278. else if (la == SLIM_LA_MANAGER &&
  1279. (pn >= ctrl->nports ||
  1280. ctrl->ports[pn].state != SLIM_P_UNCFG))
  1281. ret = -EINVAL;
  1282. else
  1283. ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
  1284. if (ret) {
  1285. for (j = j - 1; j >= 0; j--)
  1286. disconnect_port_ch(ctrl, sinkh[j]);
  1287. goto connect_sink_err;
  1288. }
  1289. }
  1290. slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
  1291. GFP_KERNEL);
  1292. if (!slc->sinkh) {
  1293. ret = -ENOMEM;
  1294. for (j = 0; j < nsink; j++)
  1295. disconnect_port_ch(ctrl, sinkh[j]);
  1296. goto connect_sink_err;
  1297. }
  1298. memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
  1299. slc->nsink += nsink;
  1300. connect_sink_err:
  1301. mutex_unlock(&ctrl->sched.m_reconf);
  1302. return ret;
  1303. }
  1304. EXPORT_SYMBOL_GPL(slim_connect_sink);
  1305. /*
  1306. * slim_disconnect_ports: Disconnect port(s) from channel
  1307. * @sb: client handle
  1308. * @ph: ports to be disconnected
  1309. * @nph: number of ports.
  1310. * Disconnects ports from a channel.
  1311. */
  1312. int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
  1313. {
  1314. struct slim_controller *ctrl = sb->ctrl;
  1315. int i;
  1316. mutex_lock(&ctrl->sched.m_reconf);
  1317. for (i = 0; i < nph; i++)
  1318. disconnect_port_ch(ctrl, ph[i]);
  1319. mutex_unlock(&ctrl->sched.m_reconf);
  1320. return 0;
  1321. }
  1322. EXPORT_SYMBOL_GPL(slim_disconnect_ports);
  1323. /*
  1324. * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
  1325. * @sb: client handle
  1326. * @ph: port-handle
  1327. * @iobuf: buffer to be transferred or populated
  1328. * @len: buffer size.
  1329. * @comp: completion signal to indicate transfer done or error.
  1330. * context: can sleep
  1331. * Returns number of bytes transferred/received if used synchronously.
  1332. * Will return 0 if used asynchronously.
  1333. * Client will call slim_port_get_xfer_status to get error and/or number of
  1334. * bytes transferred if used asynchronously.
  1335. */
  1336. int slim_port_xfer(struct slim_device *sb, u32 ph, phys_addr_t iobuf, u32 len,
  1337. struct completion *comp)
  1338. {
  1339. struct slim_controller *ctrl = sb->ctrl;
  1340. u8 pn = SLIM_HDL_TO_PORT(ph);
  1341. dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
  1342. return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
  1343. }
  1344. EXPORT_SYMBOL_GPL(slim_port_xfer);
  1345. /*
  1346. * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
  1347. * after completion is done.
  1348. * @sb: client handle
  1349. * @ph: port-handle
  1350. * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
  1351. * @done_len: Number of bytes transferred.
  1352. * This can be called when port_xfer complition is signalled.
  1353. * The API will return port transfer error (underflow/overflow/disconnect)
  1354. * and/or done_len will reflect number of bytes transferred. Note that
  1355. * done_len may be valid even if port error (overflow/underflow) has happened.
  1356. * e.g. If the transfer was scheduled with a few bytes to be transferred and
  1357. * client has not supplied more data to be transferred, done_len will indicate
  1358. * number of bytes transferred with underflow error. To avoid frequent underflow
  1359. * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
  1360. * channel has data to be transferred even if client is not ready to transfer
  1361. * data all the time. done_buf will indicate address of the last buffer
  1362. * processed from the multiple transfers.
  1363. */
  1364. enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
  1365. phys_addr_t *done_buf, u32 *done_len)
  1366. {
  1367. struct slim_controller *ctrl = sb->ctrl;
  1368. u8 pn = SLIM_HDL_TO_PORT(ph);
  1369. u32 la = SLIM_HDL_TO_LA(ph);
  1370. enum slim_port_err err;
  1371. dev_dbg(&ctrl->dev, "get status port num:%d", pn);
  1372. /*
  1373. * Framework only has insight into ports managed by ported device
  1374. * used by the manager and not slave
  1375. */
  1376. if (la != SLIM_LA_MANAGER) {
  1377. if (done_buf)
  1378. *done_buf = 0;
  1379. if (done_len)
  1380. *done_len = 0;
  1381. return SLIM_P_NOT_OWNED;
  1382. }
  1383. err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
  1384. if (err == SLIM_P_INPROGRESS)
  1385. err = ctrl->ports[pn].err;
  1386. return err;
  1387. }
  1388. EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
  1389. static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
  1390. {
  1391. struct slim_ich **arr;
  1392. int i, j;
  1393. int *len;
  1394. int sl = slc->seglen << slc->rootexp;
  1395. /* Channel is already active and other end is transmitting data */
  1396. if (slc->state >= SLIM_CH_ACTIVE)
  1397. return;
  1398. if (slc->coeff == SLIM_COEFF_1) {
  1399. arr = ctrl->sched.chc1;
  1400. len = &ctrl->sched.num_cc1;
  1401. } else {
  1402. arr = ctrl->sched.chc3;
  1403. len = &ctrl->sched.num_cc3;
  1404. sl *= 3;
  1405. }
  1406. *len += 1;
  1407. /* Insert the channel based on rootexp and seglen */
  1408. for (i = 0; i < *len - 1; i++) {
  1409. /*
  1410. * Primary key: exp low to high.
  1411. * Secondary key: seglen: high to low
  1412. */
  1413. if ((slc->rootexp > arr[i]->rootexp) ||
  1414. ((slc->rootexp == arr[i]->rootexp) &&
  1415. (slc->seglen < arr[i]->seglen)))
  1416. continue;
  1417. else
  1418. break;
  1419. }
  1420. for (j = *len - 1; j > i; j--)
  1421. arr[j] = arr[j - 1];
  1422. arr[i] = slc;
  1423. if (!ctrl->allocbw)
  1424. ctrl->sched.usedslots += sl;
  1425. return;
  1426. }
  1427. static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
  1428. {
  1429. struct slim_ich **arr;
  1430. int i;
  1431. u32 la, ph;
  1432. int *len;
  1433. if (slc->coeff == SLIM_COEFF_1) {
  1434. arr = ctrl->sched.chc1;
  1435. len = &ctrl->sched.num_cc1;
  1436. } else {
  1437. arr = ctrl->sched.chc3;
  1438. len = &ctrl->sched.num_cc3;
  1439. }
  1440. for (i = 0; i < *len; i++) {
  1441. if (arr[i] == slc)
  1442. break;
  1443. }
  1444. if (i >= *len)
  1445. return -EXFULL;
  1446. for (; i < *len - 1; i++)
  1447. arr[i] = arr[i + 1];
  1448. *len -= 1;
  1449. arr[*len] = NULL;
  1450. slc->state = SLIM_CH_ALLOCATED;
  1451. slc->newintr = 0;
  1452. slc->newoff = 0;
  1453. for (i = 0; i < slc->nsink; i++) {
  1454. ph = slc->sinkh[i];
  1455. la = SLIM_HDL_TO_LA(ph);
  1456. /*
  1457. * For ports managed by manager's ported device, no need to send
  1458. * disconnect. It is client's responsibility to call disconnect
  1459. * on ports owned by the slave device
  1460. */
  1461. if (la == SLIM_LA_MANAGER)
  1462. ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
  1463. }
  1464. ph = slc->srch;
  1465. la = SLIM_HDL_TO_LA(ph);
  1466. if (la == SLIM_LA_MANAGER)
  1467. ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
  1468. kfree(slc->sinkh);
  1469. slc->sinkh = NULL;
  1470. slc->srch = 0;
  1471. slc->nsink = 0;
  1472. return 0;
  1473. }
  1474. static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
  1475. {
  1476. u32 rate = 0, rate4k = 0, rate11k = 0;
  1477. u32 exp = 0;
  1478. u32 pr = 0;
  1479. bool exact = true;
  1480. bool done = false;
  1481. enum slim_ch_rate ratefam;
  1482. if (prop->prot >= SLIM_PUSH)
  1483. return 0;
  1484. if (prop->baser == SLIM_RATE_1HZ) {
  1485. rate = prop->ratem / 4000;
  1486. rate4k = rate;
  1487. if (rate * 4000 == prop->ratem)
  1488. ratefam = SLIM_RATE_4000HZ;
  1489. else {
  1490. rate = prop->ratem / 11025;
  1491. rate11k = rate;
  1492. if (rate * 11025 == prop->ratem)
  1493. ratefam = SLIM_RATE_11025HZ;
  1494. else
  1495. ratefam = SLIM_RATE_1HZ;
  1496. }
  1497. } else {
  1498. ratefam = prop->baser;
  1499. rate = prop->ratem;
  1500. }
  1501. if (ratefam == SLIM_RATE_1HZ) {
  1502. exact = false;
  1503. if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
  1504. rate = rate4k + 1;
  1505. ratefam = SLIM_RATE_4000HZ;
  1506. } else {
  1507. rate = rate11k + 1;
  1508. ratefam = SLIM_RATE_11025HZ;
  1509. }
  1510. }
  1511. /* covert rate to coeff-exp */
  1512. while (!done) {
  1513. while ((rate & 0x1) != 0x1) {
  1514. rate >>= 1;
  1515. exp++;
  1516. }
  1517. if (rate > 3) {
  1518. /* roundup if not exact */
  1519. rate++;
  1520. exact = false;
  1521. } else
  1522. done = true;
  1523. }
  1524. if (ratefam == SLIM_RATE_4000HZ) {
  1525. if (rate == 1)
  1526. pr = 0x10;
  1527. else {
  1528. pr = 0;
  1529. exp++;
  1530. }
  1531. } else {
  1532. pr = 8;
  1533. exp++;
  1534. }
  1535. if (exp <= 7) {
  1536. pr |= exp;
  1537. if (exact)
  1538. pr |= 0x80;
  1539. } else
  1540. pr = 0;
  1541. return pr;
  1542. }
  1543. static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
  1544. {
  1545. struct slim_controller *ctrl = sb->ctrl;
  1546. u32 chrate = 0;
  1547. u32 exp = 0;
  1548. u32 coeff = 0;
  1549. bool exact = true;
  1550. bool done = false;
  1551. int ret = 0;
  1552. struct slim_ich *slc = &ctrl->chans[chan];
  1553. struct slim_ch *prop = &slc->prop;
  1554. slc->prrate = slim_calc_prrate(ctrl, prop);
  1555. dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
  1556. if (prop->baser == SLIM_RATE_4000HZ)
  1557. chrate = 4000 * prop->ratem;
  1558. else if (prop->baser == SLIM_RATE_11025HZ)
  1559. chrate = 11025 * prop->ratem;
  1560. else
  1561. chrate = prop->ratem;
  1562. /* max allowed sample freq = 768 seg/frame */
  1563. if (chrate > 3600000)
  1564. return -EDQUOT;
  1565. if (prop->baser == SLIM_RATE_4000HZ &&
  1566. ctrl->a_framer->superfreq == 4000)
  1567. coeff = prop->ratem;
  1568. else if (prop->baser == SLIM_RATE_11025HZ &&
  1569. ctrl->a_framer->superfreq == 3675)
  1570. coeff = 3 * prop->ratem;
  1571. else {
  1572. u32 tempr = 0;
  1573. tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
  1574. coeff = tempr / ctrl->a_framer->rootfreq;
  1575. if (coeff * ctrl->a_framer->rootfreq != tempr) {
  1576. coeff++;
  1577. exact = false;
  1578. }
  1579. }
  1580. /* convert coeff to coeff-exponent */
  1581. exp = 0;
  1582. while (!done) {
  1583. while ((coeff & 0x1) != 0x1) {
  1584. coeff >>= 1;
  1585. exp++;
  1586. }
  1587. if (coeff > 3) {
  1588. coeff++;
  1589. exact = false;
  1590. } else
  1591. done = true;
  1592. }
  1593. if (prop->prot == SLIM_HARD_ISO && !exact)
  1594. return -EPROTONOSUPPORT;
  1595. else if (prop->prot == SLIM_AUTO_ISO) {
  1596. if (exact)
  1597. prop->prot = SLIM_HARD_ISO;
  1598. else {
  1599. /* Push-Pull not supported for now */
  1600. return -EPROTONOSUPPORT;
  1601. }
  1602. }
  1603. slc->rootexp = exp;
  1604. slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
  1605. if (prop->prot != SLIM_HARD_ISO)
  1606. slc->seglen++;
  1607. if (prop->prot >= SLIM_EXT_SMPLX)
  1608. slc->seglen++;
  1609. /* convert coeff to enum */
  1610. if (coeff == 1) {
  1611. if (exp > 9)
  1612. ret = -EIO;
  1613. coeff = SLIM_COEFF_1;
  1614. } else {
  1615. if (exp > 8)
  1616. ret = -EIO;
  1617. coeff = SLIM_COEFF_3;
  1618. }
  1619. slc->coeff = coeff;
  1620. return ret;
  1621. }
  1622. /*
  1623. * slim_alloc_ch: Allocate a slimbus channel and return its handle.
  1624. * @sb: client handle.
  1625. * @chanh: return channel handle
  1626. * Slimbus channels are limited to 256 per specification.
  1627. * -EXFULL is returned if all channels are in use.
  1628. * Although slimbus specification supports 256 channels, a controller may not
  1629. * support that many channels.
  1630. */
  1631. int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
  1632. {
  1633. struct slim_controller *ctrl = sb->ctrl;
  1634. u16 i;
  1635. if (!ctrl)
  1636. return -EINVAL;
  1637. mutex_lock(&ctrl->sched.m_reconf);
  1638. for (i = 0; i < ctrl->nchans; i++) {
  1639. if (ctrl->chans[i].state == SLIM_CH_FREE)
  1640. break;
  1641. }
  1642. if (i >= ctrl->nchans) {
  1643. mutex_unlock(&ctrl->sched.m_reconf);
  1644. return -EXFULL;
  1645. }
  1646. *chanh = i;
  1647. ctrl->chans[i].nextgrp = 0;
  1648. ctrl->chans[i].state = SLIM_CH_ALLOCATED;
  1649. ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
  1650. mutex_unlock(&ctrl->sched.m_reconf);
  1651. return 0;
  1652. }
  1653. EXPORT_SYMBOL_GPL(slim_alloc_ch);
  1654. /*
  1655. * slim_query_ch: Get reference-counted handle for a channel number. Every
  1656. * channel is reference counted by upto one as producer and the others as
  1657. * consumer)
  1658. * @sb: client handle
  1659. * @chan: slimbus channel number
  1660. * @chanh: return channel handle
  1661. * If request channel number is not in use, it is allocated, and reference
  1662. * count is set to one. If the channel was was already allocated, this API
  1663. * will return handle to that channel and reference count is incremented.
  1664. * -EXFULL is returned if all channels are in use
  1665. */
  1666. int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
  1667. {
  1668. struct slim_controller *ctrl = sb->ctrl;
  1669. u16 i, j;
  1670. int ret = 0;
  1671. if (!ctrl || !chanh)
  1672. return -EINVAL;
  1673. mutex_lock(&ctrl->sched.m_reconf);
  1674. /* start with modulo number */
  1675. i = ch % ctrl->nchans;
  1676. for (j = 0; j < ctrl->nchans; j++) {
  1677. if (ctrl->chans[i].chan == ch) {
  1678. *chanh = i;
  1679. ctrl->chans[i].ref++;
  1680. if (ctrl->chans[i].state == SLIM_CH_FREE)
  1681. ctrl->chans[i].state = SLIM_CH_ALLOCATED;
  1682. goto query_out;
  1683. }
  1684. i = (i + 1) % ctrl->nchans;
  1685. }
  1686. /* Channel not in table yet */
  1687. ret = -EXFULL;
  1688. for (j = 0; j < ctrl->nchans; j++) {
  1689. if (ctrl->chans[i].state == SLIM_CH_FREE) {
  1690. ctrl->chans[i].state =
  1691. SLIM_CH_ALLOCATED;
  1692. *chanh = i;
  1693. ctrl->chans[i].ref++;
  1694. ctrl->chans[i].chan = ch;
  1695. ctrl->chans[i].nextgrp = 0;
  1696. ret = 0;
  1697. break;
  1698. }
  1699. i = (i + 1) % ctrl->nchans;
  1700. }
  1701. query_out:
  1702. mutex_unlock(&ctrl->sched.m_reconf);
  1703. dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
  1704. ch, i, ctrl->chans[i].ref, ret);
  1705. return ret;
  1706. }
  1707. EXPORT_SYMBOL_GPL(slim_query_ch);
  1708. /*
  1709. * slim_dealloc_ch: Deallocate channel allocated using the API above
  1710. * -EISCONN is returned if the channel is tried to be deallocated without
  1711. * being removed first.
  1712. * -ENOTCONN is returned if deallocation is tried on a channel that's not
  1713. * allocated.
  1714. */
  1715. int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
  1716. {
  1717. struct slim_controller *ctrl = sb->ctrl;
  1718. u8 chan = SLIM_HDL_TO_CHIDX(chanh);
  1719. struct slim_ich *slc;
  1720. if (!ctrl)
  1721. return -EINVAL;
  1722. slc = &ctrl->chans[chan];
  1723. mutex_lock(&ctrl->sched.m_reconf);
  1724. if (slc->state == SLIM_CH_FREE) {
  1725. mutex_unlock(&ctrl->sched.m_reconf);
  1726. return -ENOTCONN;
  1727. }
  1728. if (slc->ref > 1) {
  1729. slc->ref--;
  1730. mutex_unlock(&ctrl->sched.m_reconf);
  1731. dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
  1732. slc->chan, chanh, slc->ref);
  1733. return 0;
  1734. }
  1735. if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
  1736. dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
  1737. mutex_unlock(&ctrl->sched.m_reconf);
  1738. return -EISCONN;
  1739. }
  1740. slc->ref--;
  1741. slc->state = SLIM_CH_FREE;
  1742. mutex_unlock(&ctrl->sched.m_reconf);
  1743. dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
  1744. slc->chan, chanh, slc->ref);
  1745. return 0;
  1746. }
  1747. EXPORT_SYMBOL_GPL(slim_dealloc_ch);
  1748. /*
  1749. * slim_get_ch_state: Channel state.
  1750. * This API returns the channel's state (active, suspended, inactive etc)
  1751. */
  1752. enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
  1753. {
  1754. u8 chan = SLIM_HDL_TO_CHIDX(chanh);
  1755. struct slim_ich *slc = &sb->ctrl->chans[chan];
  1756. return slc->state;
  1757. }
  1758. EXPORT_SYMBOL_GPL(slim_get_ch_state);
  1759. /*
  1760. * slim_define_ch: Define a channel.This API defines channel parameters for a
  1761. * given channel.
  1762. * @sb: client handle.
  1763. * @prop: slim_ch structure with channel parameters desired to be used.
  1764. * @chanh: list of channels to be defined.
  1765. * @nchan: number of channels in a group (1 if grp is false)
  1766. * @grp: Are the channels grouped
  1767. * @grph: return group handle if grouping of channels is desired.
  1768. * Channels can be grouped if multiple channels use same parameters
  1769. * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
  1770. * and given 1 handle for simplicity and avoid repeatedly calling the API)
  1771. * -EISCONN is returned if channel is already used with different parameters.
  1772. * -ENXIO is returned if the channel is not yet allocated.
  1773. */
  1774. int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
  1775. u8 nchan, bool grp, u16 *grph)
  1776. {
  1777. struct slim_controller *ctrl = sb->ctrl;
  1778. int i, ret = 0;
  1779. if (!ctrl || !chanh || !prop || !nchan)
  1780. return -EINVAL;
  1781. mutex_lock(&ctrl->sched.m_reconf);
  1782. for (i = 0; i < nchan; i++) {
  1783. u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
  1784. struct slim_ich *slc = &ctrl->chans[chan];
  1785. dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
  1786. (int)ctrl->chans[chan].state);
  1787. if (slc->state < SLIM_CH_ALLOCATED) {
  1788. ret = -ENXIO;
  1789. goto err_define_ch;
  1790. }
  1791. if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
  1792. if (prop->ratem != slc->prop.ratem ||
  1793. prop->sampleszbits != slc->prop.sampleszbits ||
  1794. prop->baser != slc->prop.baser) {
  1795. ret = -EISCONN;
  1796. goto err_define_ch;
  1797. }
  1798. } else if (slc->state > SLIM_CH_DEFINED) {
  1799. ret = -EISCONN;
  1800. goto err_define_ch;
  1801. } else {
  1802. ctrl->chans[chan].prop = *prop;
  1803. ret = slim_nextdefine_ch(sb, chan);
  1804. if (ret)
  1805. goto err_define_ch;
  1806. }
  1807. if (i < (nchan - 1))
  1808. ctrl->chans[chan].nextgrp = chanh[i + 1];
  1809. if (i == 0)
  1810. ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
  1811. if (i == (nchan - 1))
  1812. ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
  1813. }
  1814. if (grp)
  1815. *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
  1816. for (i = 0; i < nchan; i++) {
  1817. u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
  1818. struct slim_ich *slc = &ctrl->chans[chan];
  1819. if (slc->state == SLIM_CH_ALLOCATED)
  1820. slc->state = SLIM_CH_DEFINED;
  1821. }
  1822. err_define_ch:
  1823. dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
  1824. mutex_unlock(&ctrl->sched.m_reconf);
  1825. return ret;
  1826. }
  1827. EXPORT_SYMBOL_GPL(slim_define_ch);
  1828. static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
  1829. {
  1830. u32 code = 0;
  1831. if (*ctrlw == *subfrml) {
  1832. *ctrlw = 8;
  1833. *subfrml = 8;
  1834. *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
  1835. - SLIM_GDE_SLOTS_PER_SUPERFRAME;
  1836. return 0;
  1837. }
  1838. if (*subfrml == 6) {
  1839. code = 0;
  1840. *msgsl = 256;
  1841. } else if (*subfrml == 8) {
  1842. code = 1;
  1843. *msgsl = 192;
  1844. } else if (*subfrml == 24) {
  1845. code = 2;
  1846. *msgsl = 64;
  1847. } else { /* 32 */
  1848. code = 3;
  1849. *msgsl = 48;
  1850. }
  1851. if (*ctrlw < 8) {
  1852. if (*ctrlw >= 6) {
  1853. *ctrlw = 6;
  1854. code |= 0x14;
  1855. } else {
  1856. if (*ctrlw == 5)
  1857. *ctrlw = 4;
  1858. code |= (*ctrlw << 2);
  1859. }
  1860. } else {
  1861. code -= 2;
  1862. if (*ctrlw >= 24) {
  1863. *ctrlw = 24;
  1864. code |= 0x1e;
  1865. } else if (*ctrlw >= 16) {
  1866. *ctrlw = 16;
  1867. code |= 0x1c;
  1868. } else if (*ctrlw >= 12) {
  1869. *ctrlw = 12;
  1870. code |= 0x1a;
  1871. } else {
  1872. *ctrlw = 8;
  1873. code |= 0x18;
  1874. }
  1875. }
  1876. *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
  1877. SLIM_GDE_SLOTS_PER_SUPERFRAME;
  1878. return code;
  1879. }
  1880. static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
  1881. int sz, u32 shft)
  1882. {
  1883. int i;
  1884. u32 oldoff;
  1885. for (i = 0; i < sz; i++) {
  1886. struct slim_ich *slc;
  1887. if (ach[i] == NULL)
  1888. continue;
  1889. slc = ach[i];
  1890. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  1891. continue;
  1892. oldoff = slc->newoff;
  1893. slc->newoff += shft;
  1894. /* seg. offset must be <= interval */
  1895. if (slc->newoff >= slc->newintr)
  1896. slc->newoff -= slc->newintr;
  1897. }
  1898. }
  1899. static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
  1900. u32 *ctrlw, u32 *subfrml)
  1901. {
  1902. int coeff1, coeff3;
  1903. enum slim_ch_coeff bias;
  1904. struct slim_controller *ctrl = sb->ctrl;
  1905. int last1 = ctrl->sched.num_cc1 - 1;
  1906. int last3 = ctrl->sched.num_cc3 - 1;
  1907. /*
  1908. * Find first channels with coeff 1 & 3 as starting points for
  1909. * scheduling
  1910. */
  1911. for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
  1912. struct slim_ich *slc = ctrl->sched.chc3[coeff3];
  1913. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  1914. continue;
  1915. else
  1916. break;
  1917. }
  1918. for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
  1919. struct slim_ich *slc = ctrl->sched.chc1[coeff1];
  1920. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  1921. continue;
  1922. else
  1923. break;
  1924. }
  1925. if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
  1926. *ctrlw = 8;
  1927. *subfrml = 8;
  1928. return 0;
  1929. } else if (coeff3 == ctrl->sched.num_cc3)
  1930. bias = SLIM_COEFF_1;
  1931. else
  1932. bias = SLIM_COEFF_3;
  1933. /*
  1934. * Find last chan in coeff1, 3 list, we will use to know when we
  1935. * have done scheduling all coeff1 channels
  1936. */
  1937. while (last1 >= 0) {
  1938. if (ctrl->sched.chc1[last1] != NULL &&
  1939. (ctrl->sched.chc1[last1])->state !=
  1940. SLIM_CH_PENDING_REMOVAL)
  1941. break;
  1942. last1--;
  1943. }
  1944. while (last3 >= 0) {
  1945. if (ctrl->sched.chc3[last3] != NULL &&
  1946. (ctrl->sched.chc3[last3])->state !=
  1947. SLIM_CH_PENDING_REMOVAL)
  1948. break;
  1949. last3--;
  1950. }
  1951. if (bias == SLIM_COEFF_1) {
  1952. struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
  1953. u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
  1954. int curexp, finalexp;
  1955. u32 curintr, curmaxsl;
  1956. int opensl1[2];
  1957. int maxctrlw1;
  1958. finalexp = (ctrl->sched.chc1[last1])->rootexp;
  1959. curexp = (int)expshft - 1;
  1960. curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
  1961. curmaxsl = curintr >> 1;
  1962. opensl1[0] = opensl1[1] = curmaxsl;
  1963. while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
  1964. curintr >>= 1;
  1965. curmaxsl >>= 1;
  1966. /* update 4K family open slot records */
  1967. if (opensl1[1] < opensl1[0])
  1968. opensl1[1] -= curmaxsl;
  1969. else
  1970. opensl1[1] = opensl1[0] - curmaxsl;
  1971. opensl1[0] = curmaxsl;
  1972. if (opensl1[1] < 0) {
  1973. opensl1[0] += opensl1[1];
  1974. opensl1[1] = 0;
  1975. }
  1976. if (opensl1[0] <= 0) {
  1977. dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
  1978. __LINE__);
  1979. return -EXFULL;
  1980. }
  1981. curexp++;
  1982. /* schedule 4k family channels */
  1983. while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
  1984. (int)(slc1->rootexp + expshft))) {
  1985. if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
  1986. coeff1++;
  1987. slc1 = ctrl->sched.chc1[coeff1];
  1988. continue;
  1989. }
  1990. if (opensl1[1] >= opensl1[0] ||
  1991. (finalexp == (int)slc1->rootexp &&
  1992. curintr <= 24 &&
  1993. opensl1[0] == curmaxsl)) {
  1994. opensl1[1] -= slc1->seglen;
  1995. slc1->newoff = curmaxsl + opensl1[1];
  1996. if (opensl1[1] < 0 &&
  1997. opensl1[0] == curmaxsl) {
  1998. opensl1[0] += opensl1[1];
  1999. opensl1[1] = 0;
  2000. if (opensl1[0] < 0) {
  2001. dev_dbg(&ctrl->dev,
  2002. "reconfig failed:%d\n",
  2003. __LINE__);
  2004. return -EXFULL;
  2005. }
  2006. }
  2007. } else {
  2008. if (slc1->seglen > opensl1[0]) {
  2009. dev_dbg(&ctrl->dev,
  2010. "reconfig failed:%d\n",
  2011. __LINE__);
  2012. return -EXFULL;
  2013. }
  2014. slc1->newoff = opensl1[0] -
  2015. slc1->seglen;
  2016. opensl1[0] = slc1->newoff;
  2017. }
  2018. slc1->newintr = curintr;
  2019. coeff1++;
  2020. slc1 = ctrl->sched.chc1[coeff1];
  2021. }
  2022. }
  2023. /* Leave some slots for messaging space */
  2024. if (opensl1[1] <= 0 && opensl1[0] <= 0)
  2025. return -EXFULL;
  2026. if (opensl1[1] > opensl1[0]) {
  2027. int temp = opensl1[0];
  2028. opensl1[0] = opensl1[1];
  2029. opensl1[1] = temp;
  2030. shiftsegoffsets(ctrl, ctrl->sched.chc1,
  2031. ctrl->sched.num_cc1, curmaxsl);
  2032. }
  2033. /* choose subframe mode to maximize bw */
  2034. maxctrlw1 = opensl1[0];
  2035. if (opensl1[0] == curmaxsl)
  2036. maxctrlw1 += opensl1[1];
  2037. if (curintr >= 24) {
  2038. *subfrml = 24;
  2039. *ctrlw = maxctrlw1;
  2040. } else if (curintr == 12) {
  2041. if (maxctrlw1 > opensl1[1] * 4) {
  2042. *subfrml = 24;
  2043. *ctrlw = maxctrlw1;
  2044. } else {
  2045. *subfrml = 6;
  2046. *ctrlw = opensl1[1];
  2047. }
  2048. } else {
  2049. *subfrml = 6;
  2050. *ctrlw = maxctrlw1;
  2051. }
  2052. } else {
  2053. struct slim_ich *slc1 = NULL;
  2054. struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
  2055. u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
  2056. int curexp, finalexp, exp1;
  2057. u32 curintr, curmaxsl;
  2058. int opensl3[2];
  2059. int opensl1[6];
  2060. bool opensl1valid = false;
  2061. int maxctrlw1, maxctrlw3, i;
  2062. finalexp = (ctrl->sched.chc3[last3])->rootexp;
  2063. if (last1 >= 0) {
  2064. slc1 = ctrl->sched.chc1[coeff1];
  2065. exp1 = (ctrl->sched.chc1[last1])->rootexp;
  2066. if (exp1 > finalexp)
  2067. finalexp = exp1;
  2068. }
  2069. curexp = (int)expshft - 1;
  2070. curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
  2071. curmaxsl = curintr >> 1;
  2072. opensl3[0] = opensl3[1] = curmaxsl;
  2073. while (coeff1 < ctrl->sched.num_cc1 ||
  2074. coeff3 < ctrl->sched.num_cc3 ||
  2075. curintr > 32) {
  2076. curintr >>= 1;
  2077. curmaxsl >>= 1;
  2078. /* update 12k family open slot records */
  2079. if (opensl3[1] < opensl3[0])
  2080. opensl3[1] -= curmaxsl;
  2081. else
  2082. opensl3[1] = opensl3[0] - curmaxsl;
  2083. opensl3[0] = curmaxsl;
  2084. if (opensl3[1] < 0) {
  2085. opensl3[0] += opensl3[1];
  2086. opensl3[1] = 0;
  2087. }
  2088. if (opensl3[0] <= 0) {
  2089. dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
  2090. __LINE__);
  2091. return -EXFULL;
  2092. }
  2093. curexp++;
  2094. /* schedule 12k family channels */
  2095. while (coeff3 < ctrl->sched.num_cc3 &&
  2096. curexp == (int)slc3->rootexp + expshft) {
  2097. if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
  2098. coeff3++;
  2099. slc3 = ctrl->sched.chc3[coeff3];
  2100. continue;
  2101. }
  2102. opensl1valid = false;
  2103. if (opensl3[1] >= opensl3[0] ||
  2104. (finalexp == (int)slc3->rootexp &&
  2105. curintr <= 32 &&
  2106. opensl3[0] == curmaxsl &&
  2107. last1 < 0)) {
  2108. opensl3[1] -= slc3->seglen;
  2109. slc3->newoff = curmaxsl + opensl3[1];
  2110. if (opensl3[1] < 0 &&
  2111. opensl3[0] == curmaxsl) {
  2112. opensl3[0] += opensl3[1];
  2113. opensl3[1] = 0;
  2114. }
  2115. if (opensl3[0] < 0) {
  2116. dev_dbg(&ctrl->dev,
  2117. "reconfig failed:%d\n",
  2118. __LINE__);
  2119. return -EXFULL;
  2120. }
  2121. } else {
  2122. if (slc3->seglen > opensl3[0]) {
  2123. dev_dbg(&ctrl->dev,
  2124. "reconfig failed:%d\n",
  2125. __LINE__);
  2126. return -EXFULL;
  2127. }
  2128. slc3->newoff = opensl3[0] -
  2129. slc3->seglen;
  2130. opensl3[0] = slc3->newoff;
  2131. }
  2132. slc3->newintr = curintr;
  2133. coeff3++;
  2134. slc3 = ctrl->sched.chc3[coeff3];
  2135. }
  2136. /* update 4k openslot records */
  2137. if (opensl1valid == false) {
  2138. for (i = 0; i < 3; i++) {
  2139. opensl1[i * 2] = opensl3[0];
  2140. opensl1[(i * 2) + 1] = opensl3[1];
  2141. }
  2142. } else {
  2143. int opensl1p[6];
  2144. memcpy(opensl1p, opensl1, sizeof(opensl1));
  2145. for (i = 0; i < 3; i++) {
  2146. if (opensl1p[i] < opensl1p[i + 3])
  2147. opensl1[(i * 2) + 1] =
  2148. opensl1p[i];
  2149. else
  2150. opensl1[(i * 2) + 1] =
  2151. opensl1p[i + 3];
  2152. }
  2153. for (i = 0; i < 3; i++) {
  2154. opensl1[(i * 2) + 1] -= curmaxsl;
  2155. opensl1[i * 2] = curmaxsl;
  2156. if (opensl1[(i * 2) + 1] < 0) {
  2157. opensl1[i * 2] +=
  2158. opensl1[(i * 2) + 1];
  2159. opensl1[(i * 2) + 1] = 0;
  2160. }
  2161. if (opensl1[i * 2] < 0) {
  2162. dev_dbg(&ctrl->dev,
  2163. "reconfig failed:%d\n",
  2164. __LINE__);
  2165. return -EXFULL;
  2166. }
  2167. }
  2168. }
  2169. /* schedule 4k family channels */
  2170. while (coeff1 < ctrl->sched.num_cc1 &&
  2171. curexp == (int)slc1->rootexp + expshft) {
  2172. /* searchorder effective when opensl valid */
  2173. static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
  2174. int maxopensl = 0;
  2175. int maxi = 0;
  2176. if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
  2177. coeff1++;
  2178. slc1 = ctrl->sched.chc1[coeff1];
  2179. continue;
  2180. }
  2181. opensl1valid = true;
  2182. for (i = 0; i < 6; i++) {
  2183. if (opensl1[srcho[i]] > maxopensl) {
  2184. maxopensl = opensl1[srcho[i]];
  2185. maxi = srcho[i];
  2186. }
  2187. }
  2188. opensl1[maxi] -= slc1->seglen;
  2189. slc1->newoff = (curmaxsl * maxi) +
  2190. opensl1[maxi];
  2191. if (opensl1[maxi] < 0) {
  2192. if (((maxi & 1) == 1) &&
  2193. (opensl1[maxi - 1] == curmaxsl)) {
  2194. opensl1[maxi - 1] +=
  2195. opensl1[maxi];
  2196. if (opensl3[0] >
  2197. opensl1[maxi - 1])
  2198. opensl3[0] =
  2199. opensl1[maxi - 1];
  2200. opensl3[1] = 0;
  2201. opensl1[maxi] = 0;
  2202. if (opensl1[maxi - 1] < 0) {
  2203. dev_dbg(&ctrl->dev,
  2204. "reconfig failed:%d\n",
  2205. __LINE__);
  2206. return -EXFULL;
  2207. }
  2208. } else {
  2209. dev_dbg(&ctrl->dev,
  2210. "reconfig failed:%d\n",
  2211. __LINE__);
  2212. return -EXFULL;
  2213. }
  2214. } else {
  2215. if (opensl3[maxi & 1] > opensl1[maxi])
  2216. opensl3[maxi & 1] =
  2217. opensl1[maxi];
  2218. }
  2219. slc1->newintr = curintr * 3;
  2220. coeff1++;
  2221. slc1 = ctrl->sched.chc1[coeff1];
  2222. }
  2223. }
  2224. /* Leave some slots for messaging space */
  2225. if (opensl3[1] <= 0 && opensl3[0] <= 0)
  2226. return -EXFULL;
  2227. /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
  2228. if (opensl3[1] > opensl3[0]) {
  2229. int temp = opensl3[0];
  2230. opensl3[0] = opensl3[1];
  2231. opensl3[1] = temp;
  2232. temp = opensl1[5];
  2233. opensl1[5] = opensl1[4];
  2234. opensl1[4] = opensl1[3];
  2235. opensl1[3] = opensl1[2];
  2236. opensl1[2] = opensl1[1];
  2237. opensl1[1] = opensl1[0];
  2238. opensl1[0] = temp;
  2239. shiftsegoffsets(ctrl, ctrl->sched.chc1,
  2240. ctrl->sched.num_cc1, curmaxsl);
  2241. shiftsegoffsets(ctrl, ctrl->sched.chc3,
  2242. ctrl->sched.num_cc3, curmaxsl);
  2243. }
  2244. /* subframe mode to maximize BW */
  2245. maxctrlw3 = opensl3[0];
  2246. maxctrlw1 = opensl1[0];
  2247. if (opensl3[0] == curmaxsl)
  2248. maxctrlw3 += opensl3[1];
  2249. for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
  2250. maxctrlw1 += opensl1[i + 1];
  2251. if (curintr >= 32) {
  2252. *subfrml = 32;
  2253. *ctrlw = maxctrlw3;
  2254. } else if (curintr == 16) {
  2255. if (maxctrlw3 > (opensl3[1] * 4)) {
  2256. *subfrml = 32;
  2257. *ctrlw = maxctrlw3;
  2258. } else {
  2259. *subfrml = 8;
  2260. *ctrlw = opensl3[1];
  2261. }
  2262. } else {
  2263. if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
  2264. *subfrml = 24;
  2265. *ctrlw = maxctrlw1;
  2266. } else {
  2267. *subfrml = 8;
  2268. *ctrlw = maxctrlw3;
  2269. }
  2270. }
  2271. }
  2272. return 0;
  2273. }
  2274. #ifdef DEBUG
  2275. static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
  2276. u32 subfrml, u32 clkgear)
  2277. {
  2278. int sl, i;
  2279. int cc1 = 0;
  2280. int cc3 = 0;
  2281. struct slim_ich *slc = NULL;
  2282. if (!ctrl->sched.slots)
  2283. return 0;
  2284. memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
  2285. dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
  2286. for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
  2287. for (i = 0; i < ctrlw; i++)
  2288. ctrl->sched.slots[sl + i] = 33;
  2289. }
  2290. while (cc1 < ctrl->sched.num_cc1) {
  2291. slc = ctrl->sched.chc1[cc1];
  2292. if (slc == NULL) {
  2293. dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
  2294. cc1);
  2295. return -EIO;
  2296. }
  2297. dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
  2298. (slc - ctrl->chans), slc->newoff,
  2299. slc->newintr, slc->seglen);
  2300. if (slc->state != SLIM_CH_PENDING_REMOVAL) {
  2301. for (sl = slc->newoff;
  2302. sl < SLIM_SL_PER_SUPERFRAME;
  2303. sl += slc->newintr) {
  2304. for (i = 0; i < slc->seglen; i++) {
  2305. if (ctrl->sched.slots[sl + i])
  2306. return -EXFULL;
  2307. ctrl->sched.slots[sl + i] = cc1 + 1;
  2308. }
  2309. }
  2310. }
  2311. cc1++;
  2312. }
  2313. while (cc3 < ctrl->sched.num_cc3) {
  2314. slc = ctrl->sched.chc3[cc3];
  2315. if (slc == NULL) {
  2316. dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
  2317. cc3);
  2318. return -EIO;
  2319. }
  2320. dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
  2321. (slc - ctrl->chans), slc->newoff,
  2322. slc->newintr, slc->seglen);
  2323. if (slc->state != SLIM_CH_PENDING_REMOVAL) {
  2324. for (sl = slc->newoff;
  2325. sl < SLIM_SL_PER_SUPERFRAME;
  2326. sl += slc->newintr) {
  2327. for (i = 0; i < slc->seglen; i++) {
  2328. if (ctrl->sched.slots[sl + i])
  2329. return -EXFULL;
  2330. ctrl->sched.slots[sl + i] = cc3 + 1;
  2331. }
  2332. }
  2333. }
  2334. cc3++;
  2335. }
  2336. return 0;
  2337. }
  2338. #else
  2339. static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
  2340. u32 subfrml, u32 clkgear)
  2341. {
  2342. return 0;
  2343. }
  2344. #endif
  2345. static void slim_sort_chan_grp(struct slim_controller *ctrl,
  2346. struct slim_ich *slc)
  2347. {
  2348. u8 last = (u8)-1;
  2349. u8 second = 0;
  2350. for (; last > 0; last--) {
  2351. struct slim_ich *slc1 = slc;
  2352. struct slim_ich *slc2;
  2353. u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
  2354. slc2 = &ctrl->chans[next];
  2355. for (second = 1; second <= last && slc2 &&
  2356. (slc2->state == SLIM_CH_ACTIVE ||
  2357. slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
  2358. if (slc1->newoff > slc2->newoff) {
  2359. u32 temp = slc2->newoff;
  2360. slc2->newoff = slc1->newoff;
  2361. slc1->newoff = temp;
  2362. }
  2363. if (slc2->nextgrp & SLIM_END_GRP) {
  2364. last = second;
  2365. break;
  2366. }
  2367. slc1 = slc2;
  2368. next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
  2369. slc2 = &ctrl->chans[next];
  2370. }
  2371. if (slc2 == NULL)
  2372. last = second - 1;
  2373. }
  2374. }
  2375. static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
  2376. {
  2377. u32 msgsl = 0;
  2378. u32 ctrlw = 0;
  2379. u32 subfrml = 0;
  2380. int ret = -EIO;
  2381. struct slim_controller *ctrl = sb->ctrl;
  2382. u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
  2383. u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
  2384. SLIM_GDE_SLOTS_PER_SUPERFRAME;
  2385. *clkgear = SLIM_MAX_CLK_GEAR;
  2386. dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
  2387. dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
  2388. ctrl->sched.usedslots,
  2389. ctrl->sched.pending_msgsl, *clkgear);
  2390. /*
  2391. * If number of slots are 0, that means channels are inactive.
  2392. * It is very likely that the manager will call clock pause very soon.
  2393. * By making sure that bus is in MAX_GEAR, clk pause sequence will take
  2394. * minimum amount of time.
  2395. */
  2396. if (ctrl->sched.usedslots != 0) {
  2397. while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
  2398. *clkgear -= 1;
  2399. usedsl *= 2;
  2400. }
  2401. }
  2402. /*
  2403. * Try scheduling data channels at current clock gear, if all channels
  2404. * can be scheduled, or reserved BW can't be satisfied, increase clock
  2405. * gear and try again
  2406. */
  2407. for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
  2408. ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
  2409. if (ret == 0) {
  2410. *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
  2411. if ((msgsl >> (ctrl->max_cg - *clkgear) <
  2412. ctrl->sched.pending_msgsl) &&
  2413. (*clkgear < ctrl->max_cg))
  2414. continue;
  2415. else
  2416. break;
  2417. }
  2418. }
  2419. if (ret == 0) {
  2420. int i;
  2421. /* Sort channel-groups */
  2422. for (i = 0; i < ctrl->sched.num_cc1; i++) {
  2423. struct slim_ich *slc = ctrl->sched.chc1[i];
  2424. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  2425. continue;
  2426. if ((slc->nextgrp & SLIM_START_GRP) &&
  2427. !(slc->nextgrp & SLIM_END_GRP)) {
  2428. slim_sort_chan_grp(ctrl, slc);
  2429. }
  2430. }
  2431. for (i = 0; i < ctrl->sched.num_cc3; i++) {
  2432. struct slim_ich *slc = ctrl->sched.chc3[i];
  2433. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  2434. continue;
  2435. if ((slc->nextgrp & SLIM_START_GRP) &&
  2436. !(slc->nextgrp & SLIM_END_GRP)) {
  2437. slim_sort_chan_grp(ctrl, slc);
  2438. }
  2439. }
  2440. ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
  2441. }
  2442. return ret;
  2443. }
  2444. static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
  2445. {
  2446. struct slim_ich **arr;
  2447. int len, i;
  2448. if (coeff == SLIM_COEFF_1) {
  2449. arr = ctrl->sched.chc1;
  2450. len = ctrl->sched.num_cc1;
  2451. } else {
  2452. arr = ctrl->sched.chc3;
  2453. len = ctrl->sched.num_cc3;
  2454. }
  2455. for (i = 0; i < len; i++) {
  2456. struct slim_ich *slc = arr[i];
  2457. if (slc->state == SLIM_CH_ACTIVE ||
  2458. slc->state == SLIM_CH_SUSPENDED)
  2459. slc->offset = slc->newoff;
  2460. slc->interval = slc->newintr;
  2461. }
  2462. }
  2463. static void slim_chan_changes(struct slim_device *sb, bool revert)
  2464. {
  2465. struct slim_controller *ctrl = sb->ctrl;
  2466. while (!list_empty(&sb->mark_define)) {
  2467. struct slim_ich *slc;
  2468. struct slim_pending_ch *pch =
  2469. list_entry(sb->mark_define.next,
  2470. struct slim_pending_ch, pending);
  2471. slc = &ctrl->chans[pch->chan];
  2472. if (revert) {
  2473. if (slc->state == SLIM_CH_PENDING_ACTIVE) {
  2474. u32 sl = slc->seglen << slc->rootexp;
  2475. if (slc->coeff == SLIM_COEFF_3)
  2476. sl *= 3;
  2477. if (!ctrl->allocbw)
  2478. ctrl->sched.usedslots -= sl;
  2479. slim_remove_ch(ctrl, slc);
  2480. slc->state = SLIM_CH_DEFINED;
  2481. }
  2482. } else {
  2483. slc->state = SLIM_CH_ACTIVE;
  2484. slc->def++;
  2485. }
  2486. list_del_init(&pch->pending);
  2487. kfree(pch);
  2488. }
  2489. while (!list_empty(&sb->mark_removal)) {
  2490. struct slim_pending_ch *pch =
  2491. list_entry(sb->mark_removal.next,
  2492. struct slim_pending_ch, pending);
  2493. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2494. u32 sl = slc->seglen << slc->rootexp;
  2495. if (revert || slc->def > 0) {
  2496. if (slc->coeff == SLIM_COEFF_3)
  2497. sl *= 3;
  2498. if (!ctrl->allocbw)
  2499. ctrl->sched.usedslots += sl;
  2500. if (revert)
  2501. slc->def++;
  2502. slc->state = SLIM_CH_ACTIVE;
  2503. } else
  2504. slim_remove_ch(ctrl, slc);
  2505. list_del_init(&pch->pending);
  2506. kfree(pch);
  2507. }
  2508. while (!list_empty(&sb->mark_suspend)) {
  2509. struct slim_pending_ch *pch =
  2510. list_entry(sb->mark_suspend.next,
  2511. struct slim_pending_ch, pending);
  2512. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2513. if (revert)
  2514. slc->state = SLIM_CH_ACTIVE;
  2515. list_del_init(&pch->pending);
  2516. kfree(pch);
  2517. }
  2518. /* Change already active channel if reconfig succeeded */
  2519. if (!revert) {
  2520. slim_change_existing_chans(ctrl, SLIM_COEFF_1);
  2521. slim_change_existing_chans(ctrl, SLIM_COEFF_3);
  2522. }
  2523. }
  2524. /*
  2525. * slim_reconfigure_now: Request reconfiguration now.
  2526. * @sb: client handle
  2527. * This API does what commit flag in other scheduling APIs do.
  2528. * -EXFULL is returned if there is no space in TDM to reserve the
  2529. * bandwidth. -EBUSY is returned if reconfiguration request is already in
  2530. * progress.
  2531. */
  2532. int slim_reconfigure_now(struct slim_device *sb)
  2533. {
  2534. u8 i;
  2535. u8 wbuf[4];
  2536. u32 clkgear, subframe;
  2537. u32 curexp;
  2538. int ret;
  2539. struct slim_controller *ctrl = sb->ctrl;
  2540. u32 expshft;
  2541. u32 segdist;
  2542. struct slim_pending_ch *pch;
  2543. mutex_lock(&ctrl->sched.m_reconf);
  2544. /*
  2545. * If there are no pending changes from this client, avoid sending
  2546. * the reconfiguration sequence
  2547. */
  2548. if (sb->pending_msgsl == sb->cur_msgsl &&
  2549. list_empty(&sb->mark_define) &&
  2550. list_empty(&sb->mark_suspend)) {
  2551. struct list_head *pos, *next;
  2552. list_for_each_safe(pos, next, &sb->mark_removal) {
  2553. struct slim_ich *slc;
  2554. pch = list_entry(pos, struct slim_pending_ch, pending);
  2555. slc = &ctrl->chans[pch->chan];
  2556. if (slc->def > 0)
  2557. slc->def--;
  2558. /* Disconnect source port to free it up */
  2559. if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
  2560. slc->srch = 0;
  2561. /*
  2562. * If controller overrides BW allocation,
  2563. * delete this in remove channel itself
  2564. */
  2565. if (slc->def != 0 && !ctrl->allocbw) {
  2566. list_del(&pch->pending);
  2567. kfree(pch);
  2568. }
  2569. }
  2570. if (list_empty(&sb->mark_removal)) {
  2571. mutex_unlock(&ctrl->sched.m_reconf);
  2572. pr_info("SLIM_CL: skip reconfig sequence");
  2573. return 0;
  2574. }
  2575. }
  2576. ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
  2577. list_for_each_entry(pch, &sb->mark_define, pending) {
  2578. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2579. slim_add_ch(ctrl, slc);
  2580. if (slc->state < SLIM_CH_ACTIVE)
  2581. slc->state = SLIM_CH_PENDING_ACTIVE;
  2582. }
  2583. list_for_each_entry(pch, &sb->mark_removal, pending) {
  2584. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2585. u32 sl = slc->seglen << slc->rootexp;
  2586. if (slc->coeff == SLIM_COEFF_3)
  2587. sl *= 3;
  2588. if (!ctrl->allocbw)
  2589. ctrl->sched.usedslots -= sl;
  2590. slc->state = SLIM_CH_PENDING_REMOVAL;
  2591. }
  2592. list_for_each_entry(pch, &sb->mark_suspend, pending) {
  2593. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2594. slc->state = SLIM_CH_SUSPENDED;
  2595. }
  2596. /*
  2597. * Controller can override default channel scheduling algorithm.
  2598. * (e.g. if controller needs to use fixed channel scheduling based
  2599. * on number of channels)
  2600. */
  2601. if (ctrl->allocbw)
  2602. ret = ctrl->allocbw(sb, &subframe, &clkgear);
  2603. else
  2604. ret = slim_allocbw(sb, &subframe, &clkgear);
  2605. if (!ret) {
  2606. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2607. SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
  2608. NULL, NULL, 0, 3, NULL, 0, NULL);
  2609. dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
  2610. }
  2611. if (!ret && subframe != ctrl->sched.subfrmcode) {
  2612. wbuf[0] = (u8)(subframe & 0xFF);
  2613. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2614. SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
  2615. NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
  2616. dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
  2617. (int)wbuf[0], ret);
  2618. }
  2619. if (!ret && clkgear != ctrl->clkgear) {
  2620. wbuf[0] = (u8)(clkgear & 0xFF);
  2621. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2622. SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
  2623. NULL, wbuf, 1, 4, NULL, 0, NULL);
  2624. dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
  2625. (int)wbuf[0], ret);
  2626. }
  2627. if (ret)
  2628. goto revert_reconfig;
  2629. expshft = SLIM_MAX_CLK_GEAR - clkgear;
  2630. /* activate/remove channel */
  2631. list_for_each_entry(pch, &sb->mark_define, pending) {
  2632. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2633. /* Define content */
  2634. wbuf[0] = slc->chan;
  2635. wbuf[1] = slc->prrate;
  2636. wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
  2637. wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
  2638. dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
  2639. wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
  2640. /* Right now, channel link bit is not supported */
  2641. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2642. SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
  2643. SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
  2644. NULL, 0, NULL);
  2645. if (ret)
  2646. goto revert_reconfig;
  2647. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2648. SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
  2649. SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
  2650. NULL, 0, NULL);
  2651. if (ret)
  2652. goto revert_reconfig;
  2653. }
  2654. list_for_each_entry(pch, &sb->mark_removal, pending) {
  2655. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2656. dev_dbg(&ctrl->dev, "remove chan:%x\n", pch->chan);
  2657. wbuf[0] = slc->chan;
  2658. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2659. SLIM_MSG_MC_NEXT_REMOVE_CHANNEL, 0,
  2660. SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
  2661. NULL, 0, NULL);
  2662. if (ret)
  2663. goto revert_reconfig;
  2664. }
  2665. list_for_each_entry(pch, &sb->mark_suspend, pending) {
  2666. struct slim_ich *slc = &ctrl->chans[pch->chan];
  2667. dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
  2668. wbuf[0] = slc->chan;
  2669. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2670. SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
  2671. SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
  2672. NULL, 0, NULL);
  2673. if (ret)
  2674. goto revert_reconfig;
  2675. }
  2676. /* Define CC1 channel */
  2677. for (i = 0; i < ctrl->sched.num_cc1; i++) {
  2678. struct slim_ich *slc = ctrl->sched.chc1[i];
  2679. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  2680. continue;
  2681. curexp = slc->rootexp + expshft;
  2682. segdist = (slc->newoff << curexp) & 0x1FF;
  2683. expshft = SLIM_MAX_CLK_GEAR - clkgear;
  2684. dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
  2685. slc->newintr, slc->interval, segdist);
  2686. dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
  2687. slc->newoff, slc->offset);
  2688. if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
  2689. slc->newintr != slc->interval ||
  2690. slc->newoff != slc->offset) {
  2691. segdist |= 0x200;
  2692. segdist >>= curexp;
  2693. segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
  2694. wbuf[0] = slc->chan;
  2695. wbuf[1] = (u8)(segdist & 0xFF);
  2696. wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
  2697. (slc->prop.prot << 4);
  2698. wbuf[3] = slc->seglen;
  2699. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2700. SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
  2701. SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
  2702. 7, NULL, 0, NULL);
  2703. if (ret)
  2704. goto revert_reconfig;
  2705. }
  2706. }
  2707. /* Define CC3 channels */
  2708. for (i = 0; i < ctrl->sched.num_cc3; i++) {
  2709. struct slim_ich *slc = ctrl->sched.chc3[i];
  2710. if (slc->state == SLIM_CH_PENDING_REMOVAL)
  2711. continue;
  2712. curexp = slc->rootexp + expshft;
  2713. segdist = (slc->newoff << curexp) & 0x1FF;
  2714. expshft = SLIM_MAX_CLK_GEAR - clkgear;
  2715. dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
  2716. slc->newintr, slc->interval, segdist);
  2717. dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
  2718. slc->newoff, slc->offset);
  2719. if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
  2720. slc->newintr != slc->interval ||
  2721. slc->newoff != slc->offset) {
  2722. segdist |= 0x200;
  2723. segdist >>= curexp;
  2724. segdist |= 0xC00;
  2725. wbuf[0] = slc->chan;
  2726. wbuf[1] = (u8)(segdist & 0xFF);
  2727. wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
  2728. (slc->prop.prot << 4);
  2729. wbuf[3] = (u8)(slc->seglen);
  2730. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2731. SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
  2732. SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
  2733. 7, NULL, 0, NULL);
  2734. if (ret)
  2735. goto revert_reconfig;
  2736. }
  2737. }
  2738. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2739. SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
  2740. NULL, 0, 3, NULL, 0, NULL);
  2741. dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
  2742. if (!ret) {
  2743. ctrl->sched.subfrmcode = subframe;
  2744. ctrl->clkgear = clkgear;
  2745. ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
  2746. sb->cur_msgsl = sb->pending_msgsl;
  2747. slim_chan_changes(sb, false);
  2748. mutex_unlock(&ctrl->sched.m_reconf);
  2749. return 0;
  2750. }
  2751. revert_reconfig:
  2752. /* Revert channel changes */
  2753. slim_chan_changes(sb, true);
  2754. mutex_unlock(&ctrl->sched.m_reconf);
  2755. return ret;
  2756. }
  2757. EXPORT_SYMBOL_GPL(slim_reconfigure_now);
  2758. static int add_pending_ch(struct list_head *listh, u8 chan)
  2759. {
  2760. struct slim_pending_ch *pch;
  2761. pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
  2762. if (!pch)
  2763. return -ENOMEM;
  2764. pch->chan = chan;
  2765. list_add_tail(&pch->pending, listh);
  2766. return 0;
  2767. }
  2768. /*
  2769. * slim_control_ch: Channel control API.
  2770. * @sb: client handle
  2771. * @chanh: group or channel handle to be controlled
  2772. * @chctrl: Control command (activate/suspend/remove)
  2773. * @commit: flag to indicate whether the control should take effect right-away.
  2774. * This API activates, removes or suspends a channel (or group of channels)
  2775. * chanh indicates the channel or group handle (returned by the define_ch API).
  2776. * Reconfiguration may be time-consuming since it can change all other active
  2777. * channel allocations on the bus, change in clock gear used by the slimbus,
  2778. * and change in the control space width used for messaging.
  2779. * commit makes sure that multiple channels can be activated/deactivated before
  2780. * reconfiguration is started.
  2781. * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
  2782. * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
  2783. * yet defined.
  2784. * -EINVAL is returned if individual control of a grouped-channel is attempted.
  2785. */
  2786. int slim_control_ch(struct slim_device *sb, u16 chanh,
  2787. enum slim_ch_control chctrl, bool commit)
  2788. {
  2789. struct slim_controller *ctrl = sb->ctrl;
  2790. int ret = 0;
  2791. /* Get rid of the group flag in MSB if any */
  2792. u8 chan = SLIM_HDL_TO_CHIDX(chanh);
  2793. u8 nchan = 0;
  2794. struct slim_ich *slc = &ctrl->chans[chan];
  2795. if (!(slc->nextgrp & SLIM_START_GRP))
  2796. return -EINVAL;
  2797. mutex_lock(&sb->sldev_reconf);
  2798. do {
  2799. struct slim_pending_ch *pch;
  2800. u8 add_mark_removal = true;
  2801. slc = &ctrl->chans[chan];
  2802. dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
  2803. slc->def);
  2804. if (slc->state < SLIM_CH_DEFINED) {
  2805. ret = -ENOTCONN;
  2806. break;
  2807. }
  2808. if (chctrl == SLIM_CH_SUSPEND) {
  2809. ret = add_pending_ch(&sb->mark_suspend, chan);
  2810. if (ret)
  2811. break;
  2812. } else if (chctrl == SLIM_CH_ACTIVATE) {
  2813. if (slc->state > SLIM_CH_ACTIVE) {
  2814. ret = -EISCONN;
  2815. break;
  2816. }
  2817. ret = add_pending_ch(&sb->mark_define, chan);
  2818. if (ret)
  2819. break;
  2820. } else {
  2821. if (slc->state < SLIM_CH_ACTIVE) {
  2822. ret = -ENOTCONN;
  2823. break;
  2824. }
  2825. /* If channel removal request comes when pending
  2826. * in the mark_define, remove it from the define
  2827. * list instead of adding it to removal list
  2828. */
  2829. if (!list_empty(&sb->mark_define)) {
  2830. struct list_head *pos, *next;
  2831. list_for_each_safe(pos, next,
  2832. &sb->mark_define) {
  2833. pch = list_entry(pos,
  2834. struct slim_pending_ch,
  2835. pending);
  2836. if (pch->chan == chan) {
  2837. list_del(&pch->pending);
  2838. kfree(pch);
  2839. add_mark_removal = false;
  2840. break;
  2841. }
  2842. }
  2843. }
  2844. if (add_mark_removal == true) {
  2845. ret = add_pending_ch(&sb->mark_removal, chan);
  2846. if (ret)
  2847. break;
  2848. }
  2849. }
  2850. nchan++;
  2851. if (nchan < SLIM_GRP_TO_NCHAN(chanh))
  2852. chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
  2853. } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
  2854. if (!ret && commit == true)
  2855. ret = slim_reconfigure_now(sb);
  2856. mutex_unlock(&sb->sldev_reconf);
  2857. return ret;
  2858. }
  2859. EXPORT_SYMBOL_GPL(slim_control_ch);
  2860. /*
  2861. * slim_reservemsg_bw: Request to reserve bandwidth for messages.
  2862. * @sb: client handle
  2863. * @bw_bps: message bandwidth in bits per second to be requested
  2864. * @commit: indicates whether the reconfiguration needs to be acted upon.
  2865. * This API call can be grouped with slim_control_ch API call with only one of
  2866. * the APIs specifying the commit flag to avoid reconfiguration being called too
  2867. * frequently. -EXFULL is returned if there is no space in TDM to reserve the
  2868. * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
  2869. * is already in progress.
  2870. */
  2871. int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
  2872. {
  2873. struct slim_controller *ctrl = sb->ctrl;
  2874. int ret = 0;
  2875. int sl;
  2876. mutex_lock(&sb->sldev_reconf);
  2877. if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
  2878. sl = SLIM_SL_PER_SUPERFRAME;
  2879. else {
  2880. sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
  2881. (ctrl->a_framer->rootfreq/2 - 1)) /
  2882. (ctrl->a_framer->rootfreq/2);
  2883. }
  2884. dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
  2885. sb->cur_msgsl);
  2886. sb->pending_msgsl = sl;
  2887. if (commit == true)
  2888. ret = slim_reconfigure_now(sb);
  2889. mutex_unlock(&sb->sldev_reconf);
  2890. return ret;
  2891. }
  2892. EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
  2893. /*
  2894. * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
  2895. * paused or woken up out of clock pause
  2896. * or woken up from clock pause
  2897. * @ctrl: controller requesting bus to be paused or woken up
  2898. * @wakeup: Wakeup this controller from clock pause.
  2899. * @restart: Restart time value per spec used for clock pause. This value
  2900. * isn't used when controller is to be woken up.
  2901. * This API executes clock pause reconfiguration sequence if wakeup is false.
  2902. * If wakeup is true, controller's wakeup is called
  2903. * Slimbus clock is idle and can be disabled by the controller later.
  2904. */
  2905. int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
  2906. {
  2907. int ret = 0;
  2908. int i;
  2909. if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
  2910. return -EINVAL;
  2911. mutex_lock(&ctrl->m_ctrl);
  2912. if (wakeup) {
  2913. if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
  2914. mutex_unlock(&ctrl->m_ctrl);
  2915. return 0;
  2916. }
  2917. wait_for_completion(&ctrl->pause_comp);
  2918. /*
  2919. * Slimbus framework will call controller wakeup
  2920. * Controller should make sure that it sets active framer
  2921. * out of clock pause by doing appropriate setting
  2922. */
  2923. if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
  2924. ret = ctrl->wakeup(ctrl);
  2925. /*
  2926. * If wakeup fails, make sure that next attempt can succeed.
  2927. * Since we already consumed pause_comp, complete it so
  2928. * that next wakeup isn't blocked forever
  2929. */
  2930. if (!ret)
  2931. ctrl->clk_state = SLIM_CLK_ACTIVE;
  2932. else
  2933. complete(&ctrl->pause_comp);
  2934. mutex_unlock(&ctrl->m_ctrl);
  2935. return ret;
  2936. } else {
  2937. switch (ctrl->clk_state) {
  2938. case SLIM_CLK_ENTERING_PAUSE:
  2939. case SLIM_CLK_PAUSE_FAILED:
  2940. /*
  2941. * If controller is already trying to enter clock pause,
  2942. * let it finish.
  2943. * In case of error, retry
  2944. * In both cases, previous clock pause has signalled
  2945. * completion.
  2946. */
  2947. wait_for_completion(&ctrl->pause_comp);
  2948. /* retry upon failure */
  2949. if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
  2950. ctrl->clk_state = SLIM_CLK_ACTIVE;
  2951. break;
  2952. } else {
  2953. mutex_unlock(&ctrl->m_ctrl);
  2954. /*
  2955. * Signal completion so that wakeup can wait on
  2956. * it.
  2957. */
  2958. complete(&ctrl->pause_comp);
  2959. return 0;
  2960. }
  2961. break;
  2962. case SLIM_CLK_PAUSED:
  2963. /* already paused */
  2964. mutex_unlock(&ctrl->m_ctrl);
  2965. return 0;
  2966. case SLIM_CLK_ACTIVE:
  2967. default:
  2968. break;
  2969. }
  2970. }
  2971. /* Pending response for a message */
  2972. for (i = 0; i < ctrl->last_tid; i++) {
  2973. if (ctrl->txnt[i]) {
  2974. ret = -EBUSY;
  2975. pr_info("slim_clk_pause: txn-rsp for %d pending", i);
  2976. mutex_unlock(&ctrl->m_ctrl);
  2977. return -EBUSY;
  2978. }
  2979. }
  2980. ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
  2981. mutex_unlock(&ctrl->m_ctrl);
  2982. mutex_lock(&ctrl->sched.m_reconf);
  2983. /* Data channels active */
  2984. if (ctrl->sched.usedslots) {
  2985. pr_info("slim_clk_pause: data channel active");
  2986. ret = -EBUSY;
  2987. goto clk_pause_ret;
  2988. }
  2989. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2990. SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
  2991. 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
  2992. if (ret)
  2993. goto clk_pause_ret;
  2994. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  2995. SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
  2996. SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
  2997. if (ret)
  2998. goto clk_pause_ret;
  2999. ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
  3000. SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
  3001. SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
  3002. if (ret)
  3003. goto clk_pause_ret;
  3004. clk_pause_ret:
  3005. if (ret)
  3006. ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
  3007. else
  3008. ctrl->clk_state = SLIM_CLK_PAUSED;
  3009. complete(&ctrl->pause_comp);
  3010. mutex_unlock(&ctrl->sched.m_reconf);
  3011. return ret;
  3012. }
  3013. EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
  3014. MODULE_LICENSE("GPL v2");
  3015. MODULE_VERSION("0.1");
  3016. MODULE_DESCRIPTION("Slimbus module");
  3017. MODULE_ALIAS("platform:slimbus");