1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393 |
- From f5a5cd2721763f3759fc49d08f1b1ca0b039fcf2 Mon Sep 17 00:00:00 2001
- From: Tim Gover <tgover@broadcom.com>
- Date: Tue, 22 Jul 2014 15:41:04 +0100
- Subject: [PATCH 038/381] vcsm: VideoCore shared memory service for BCM2835
- MIME-Version: 1.0
- Content-Type: text/plain; charset=UTF-8
- Content-Transfer-Encoding: 8bit
- Add experimental support for the VideoCore shared memory service.
- This allows user processes to allocate memory from VideoCore's
- GPU relocatable heap and mmap the buffers. Additionally, the memory
- handles can passed to other VideoCore services such as MMAL, OpenMax
- and DispmanX
- TODO
- * This driver was originally released for BCM28155 which has a different
- cache architecture to BCM2835. Consequently, in this release only
- uncached mappings are supported. However, there's no fundamental
- reason which cached mappings cannot be support or BCM2835
- * More refactoring is required to remove the typedefs.
- * Re-enable the some of the commented out debug-fs statistics which were
- disabled when migrating code from proc-fs.
- * There's a lot of code to support sharing of VCSM in order to support
- Android. This could probably done more cleanly or perhaps just
- removed.
- Signed-off-by: Tim Gover <timgover@gmail.com>
- config: Disable VC_SM for now to fix hang with cutdown kernel
- vcsm: Use boolean as it cannot be built as module
- On building the bcm_vc_sm as a module we get the following error:
- v7_dma_flush_range and do_munmap are undefined in vc-sm.ko.
- Fix by making it not an option to build as module
- vcsm: Add ioctl for custom cache flushing
- vc-sm: Move headers out of arch directory
- Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
- ---
- drivers/char/broadcom/Kconfig | 9 +
- drivers/char/broadcom/Makefile | 1 +
- drivers/char/broadcom/vc_sm/Makefile | 20 +
- drivers/char/broadcom/vc_sm/vc_sm_defs.h | 181 ++
- drivers/char/broadcom/vc_sm/vc_sm_knl.h | 55 +
- drivers/char/broadcom/vc_sm/vc_vchi_sm.c | 492 +++++
- drivers/char/broadcom/vc_sm/vc_vchi_sm.h | 82 +
- drivers/char/broadcom/vc_sm/vmcs_sm.c | 3211 ++++++++++++++++++++++++++++++
- include/linux/broadcom/vmcs_sm_ioctl.h | 248 +++
- 9 files changed, 4299 insertions(+)
- create mode 100644 drivers/char/broadcom/vc_sm/Makefile
- create mode 100644 drivers/char/broadcom/vc_sm/vc_sm_defs.h
- create mode 100644 drivers/char/broadcom/vc_sm/vc_sm_knl.h
- create mode 100644 drivers/char/broadcom/vc_sm/vc_vchi_sm.c
- create mode 100644 drivers/char/broadcom/vc_sm/vc_vchi_sm.h
- create mode 100644 drivers/char/broadcom/vc_sm/vmcs_sm.c
- create mode 100644 include/linux/broadcom/vmcs_sm_ioctl.h
- --- a/drivers/char/broadcom/Kconfig
- +++ b/drivers/char/broadcom/Kconfig
- @@ -23,3 +23,12 @@ config BCM2708_VCMEM
- Helper for videocore memory access and total size allocation.
-
- endif
- +
- +config BCM_VC_SM
- + bool "VMCS Shared Memory"
- + depends on BCM2708_VCHIQ
- + select BCM2708_VCMEM
- + default n
- + help
- + Support for the VC shared memory on the Broadcom reference
- + design. Uses the VCHIQ stack.
- --- a/drivers/char/broadcom/Makefile
- +++ b/drivers/char/broadcom/Makefile
- @@ -1,2 +1,3 @@
- obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
- obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
- +obj-$(CONFIG_BCM_VC_SM) += vc_sm/
- --- /dev/null
- +++ b/drivers/char/broadcom/vc_sm/Makefile
- @@ -0,0 +1,20 @@
- +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs -O2
- +
- +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
- +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
- +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
- +EXTRA_CFLAGS += -I"$(srctree)/fs/"
- +
- +EXTRA_CFLAGS += -DOS_ASSERT_FAILURE
- +EXTRA_CFLAGS += -D__STDC_VERSION=199901L
- +EXTRA_CFLAGS += -D__STDC_VERSION__=199901L
- +EXTRA_CFLAGS += -D__VCCOREVER__=0
- +EXTRA_CFLAGS += -D__KERNEL__
- +EXTRA_CFLAGS += -D__linux__
- +EXTRA_CFLAGS += -Werror
- +
- +obj-$(CONFIG_BCM_VC_SM) := vc-sm.o
- +
- +vc-sm-objs := \
- + vmcs_sm.o \
- + vc_vchi_sm.o
- --- /dev/null
- +++ b/drivers/char/broadcom/vc_sm/vc_sm_defs.h
- @@ -0,0 +1,181 @@
- +/*****************************************************************************
- +* Copyright 2011 Broadcom Corporation. All rights reserved.
- +*
- +* Unless you and Broadcom execute a separate written software license
- +* agreement governing use of this software, this software is licensed to you
- +* under the terms of the GNU General Public License version 2, available at
- +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
- +*
- +* Notwithstanding the above, under no circumstances may you combine this
- +* software in any way with any other Broadcom software provided under a
- +* license other than the GPL, without Broadcom's express prior written
- +* consent.
- +*****************************************************************************/
- +
- +#ifndef __VC_SM_DEFS_H__INCLUDED__
- +#define __VC_SM_DEFS_H__INCLUDED__
- +
- +/* FourCC code used for VCHI connection */
- +#define VC_SM_SERVER_NAME MAKE_FOURCC("SMEM")
- +
- +/* Maximum message length */
- +#define VC_SM_MAX_MSG_LEN (sizeof(VC_SM_MSG_UNION_T) + \
- + sizeof(VC_SM_MSG_HDR_T))
- +#define VC_SM_MAX_RSP_LEN (sizeof(VC_SM_MSG_UNION_T))
- +
- +/* Resource name maximum size */
- +#define VC_SM_RESOURCE_NAME 32
- +
- +/* All message types supported for HOST->VC direction */
- +typedef enum {
- + /* Allocate shared memory block */
- + VC_SM_MSG_TYPE_ALLOC,
- + /* Lock allocated shared memory block */
- + VC_SM_MSG_TYPE_LOCK,
- + /* Unlock allocated shared memory block */
- + VC_SM_MSG_TYPE_UNLOCK,
- + /* Unlock allocated shared memory block, do not answer command */
- + VC_SM_MSG_TYPE_UNLOCK_NOANS,
- + /* Free shared memory block */
- + VC_SM_MSG_TYPE_FREE,
- + /* Resize a shared memory block */
- + VC_SM_MSG_TYPE_RESIZE,
- + /* Walk the allocated shared memory block(s) */
- + VC_SM_MSG_TYPE_WALK_ALLOC,
- +
- + /* A previously applied action will need to be reverted */
- + VC_SM_MSG_TYPE_ACTION_CLEAN,
- + VC_SM_MSG_TYPE_MAX
- +} VC_SM_MSG_TYPE;
- +
- +/* Type of memory to be allocated */
- +typedef enum {
- + VC_SM_ALLOC_CACHED,
- + VC_SM_ALLOC_NON_CACHED,
- +
- +} VC_SM_ALLOC_TYPE_T;
- +
- +/* Message header for all messages in HOST->VC direction */
- +typedef struct {
- + int32_t type;
- + uint32_t trans_id;
- + uint8_t body[0];
- +
- +} VC_SM_MSG_HDR_T;
- +
- +/* Request to allocate memory (HOST->VC) */
- +typedef struct {
- + /* type of memory to allocate */
- + VC_SM_ALLOC_TYPE_T type;
- + /* byte amount of data to allocate per unit */
- + uint32_t base_unit;
- + /* number of unit to allocate */
- + uint32_t num_unit;
- + /* alignement to be applied on allocation */
- + uint32_t alignement;
- + /* identity of who allocated this block */
- + uint32_t allocator;
- + /* resource name (for easier tracking on vc side) */
- + char name[VC_SM_RESOURCE_NAME];
- +
- +} VC_SM_ALLOC_T;
- +
- +/* Result of a requested memory allocation (VC->HOST) */
- +typedef struct {
- + /* Transaction identifier */
- + uint32_t trans_id;
- +
- + /* Resource handle */
- + uint32_t res_handle;
- + /* Pointer to resource buffer */
- + void *res_mem;
- + /* Resource base size (bytes) */
- + uint32_t res_base_size;
- + /* Resource number */
- + uint32_t res_num;
- +
- +} VC_SM_ALLOC_RESULT_T;
- +
- +/* Request to free a previously allocated memory (HOST->VC) */
- +typedef struct {
- + /* Resource handle (returned from alloc) */
- + uint32_t res_handle;
- + /* Resource buffer (returned from alloc) */
- + void *res_mem;
- +
- +} VC_SM_FREE_T;
- +
- +/* Request to lock a previously allocated memory (HOST->VC) */
- +typedef struct {
- + /* Resource handle (returned from alloc) */
- + uint32_t res_handle;
- + /* Resource buffer (returned from alloc) */
- + void *res_mem;
- +
- +} VC_SM_LOCK_UNLOCK_T;
- +
- +/* Request to resize a previously allocated memory (HOST->VC) */
- +typedef struct {
- + /* Resource handle (returned from alloc) */
- + uint32_t res_handle;
- + /* Resource buffer (returned from alloc) */
- + void *res_mem;
- + /* Resource *new* size requested (bytes) */
- + uint32_t res_new_size;
- +
- +} VC_SM_RESIZE_T;
- +
- +/* Result of a requested memory lock (VC->HOST) */
- +typedef struct {
- + /* Transaction identifier */
- + uint32_t trans_id;
- +
- + /* Resource handle */
- + uint32_t res_handle;
- + /* Pointer to resource buffer */
- + void *res_mem;
- + /* Pointer to former resource buffer if the memory
- + * was reallocated */
- + void *res_old_mem;
- +
- +} VC_SM_LOCK_RESULT_T;
- +
- +/* Generic result for a request (VC->HOST) */
- +typedef struct {
- + /* Transaction identifier */
- + uint32_t trans_id;
- +
- + int32_t success;
- +
- +} VC_SM_RESULT_T;
- +
- +/* Request to revert a previously applied action (HOST->VC) */
- +typedef struct {
- + /* Action of interest */
- + VC_SM_MSG_TYPE res_action;
- + /* Transaction identifier for the action of interest */
- + uint32_t action_trans_id;
- +
- +} VC_SM_ACTION_CLEAN_T;
- +
- +/* Request to remove all data associated with a given allocator (HOST->VC) */
- +typedef struct {
- + /* Allocator identifier */
- + uint32_t allocator;
- +
- +} VC_SM_FREE_ALL_T;
- +
- +/* Union of ALL messages */
- +typedef union {
- + VC_SM_ALLOC_T alloc;
- + VC_SM_ALLOC_RESULT_T alloc_result;
- + VC_SM_FREE_T free;
- + VC_SM_ACTION_CLEAN_T action_clean;
- + VC_SM_RESIZE_T resize;
- + VC_SM_LOCK_RESULT_T lock_result;
- + VC_SM_RESULT_T result;
- + VC_SM_FREE_ALL_T free_all;
- +
- +} VC_SM_MSG_UNION_T;
- +
- +#endif /* __VC_SM_DEFS_H__INCLUDED__ */
- --- /dev/null
- +++ b/drivers/char/broadcom/vc_sm/vc_sm_knl.h
- @@ -0,0 +1,55 @@
- +/*****************************************************************************
- +* Copyright 2011 Broadcom Corporation. All rights reserved.
- +*
- +* Unless you and Broadcom execute a separate written software license
- +* agreement governing use of this software, this software is licensed to you
- +* under the terms of the GNU General Public License version 2, available at
- +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
- +*
- +* Notwithstanding the above, under no circumstances may you combine this
- +* software in any way with any other Broadcom software provided under a
- +* license other than the GPL, without Broadcom's express prior written
- +* consent.
- +*****************************************************************************/
- +
- +#ifndef __VC_SM_KNL_H__INCLUDED__
- +#define __VC_SM_KNL_H__INCLUDED__
- +
- +#if !defined(__KERNEL__)
- +#error "This interface is for kernel use only..."
- +#endif
- +
- +/* Type of memory to be locked (ie mapped) */
- +typedef enum {
- + VC_SM_LOCK_CACHED,
- + VC_SM_LOCK_NON_CACHED,
- +
- +} VC_SM_LOCK_CACHE_MODE_T;
- +
- +/* Allocate a shared memory handle and block.
- +*/
- +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle);
- +
- +/* Free a previously allocated shared memory handle and block.
- +*/
- +int vc_sm_free(int handle);
- +
- +/* Lock a memory handle for use by kernel.
- +*/
- +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
- + long unsigned int *data);
- +
- +/* Unlock a memory handle in use by kernel.
- +*/
- +int vc_sm_unlock(int handle, int flush, int no_vc_unlock);
- +
- +/* Get an internal resource handle mapped from the external one.
- +*/
- +int vc_sm_int_handle(int handle);
- +
- +/* Map a shared memory region for use by kernel.
- +*/
- +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
- + long unsigned int *data);
- +
- +#endif /* __VC_SM_KNL_H__INCLUDED__ */
- --- /dev/null
- +++ b/drivers/char/broadcom/vc_sm/vc_vchi_sm.c
- @@ -0,0 +1,492 @@
- +/*****************************************************************************
- +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
- +*
- +* Unless you and Broadcom execute a separate written software license
- +* agreement governing use of this software, this software is licensed to you
- +* under the terms of the GNU General Public License version 2, available at
- +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
- +*
- +* Notwithstanding the above, under no circumstances may you combine this
- +* software in any way with any other Broadcom software provided under a
- +* license other than the GPL, without Broadcom's express prior written
- +* consent.
- +*****************************************************************************/
- +
- +/* ---- Include Files ----------------------------------------------------- */
- +#include <linux/types.h>
- +#include <linux/kernel.h>
- +#include <linux/list.h>
- +#include <linux/semaphore.h>
- +#include <linux/mutex.h>
- +#include <linux/slab.h>
- +#include <linux/kthread.h>
- +
- +#include "vc_vchi_sm.h"
- +
- +#define VC_SM_VER 1
- +#define VC_SM_MIN_VER 0
- +
- +/* ---- Private Constants and Types -------------------------------------- */
- +
- +/* Command blocks come from a pool */
- +#define SM_MAX_NUM_CMD_RSP_BLKS 32
- +
- +struct sm_cmd_rsp_blk {
- + struct list_head head; /* To create lists */
- + struct semaphore sema; /* To be signaled when the response is there */
- +
- + uint16_t id;
- + uint16_t length;
- +
- + uint8_t msg[VC_SM_MAX_MSG_LEN];
- +
- + uint32_t wait:1;
- + uint32_t sent:1;
- + uint32_t alloc:1;
- +
- +};
- +
- +struct sm_instance {
- + uint32_t num_connections;
- + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
- + struct task_struct *io_thread;
- + struct semaphore io_sema;
- +
- + uint32_t trans_id;
- +
- + struct mutex lock;
- + struct list_head cmd_list;
- + struct list_head rsp_list;
- + struct list_head dead_list;
- +
- + struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
- + struct list_head free_list;
- + struct mutex free_lock;
- + struct semaphore free_sema;
- +
- +};
- +
- +/* ---- Private Variables ------------------------------------------------ */
- +
- +/* ---- Private Function Prototypes -------------------------------------- */
- +
- +/* ---- Private Functions ------------------------------------------------ */
- +static struct
- +sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
- + VC_SM_MSG_TYPE id, void *msg,
- + uint32_t size, int wait)
- +{
- + struct sm_cmd_rsp_blk *blk;
- + VC_SM_MSG_HDR_T *hdr;
- +
- + if (down_interruptible(&instance->free_sema)) {
- + blk = kmalloc(sizeof(*blk), GFP_KERNEL);
- + if (!blk)
- + return NULL;
- +
- + blk->alloc = 1;
- + sema_init(&blk->sema, 0);
- + } else {
- + mutex_lock(&instance->free_lock);
- + blk =
- + list_first_entry(&instance->free_list,
- + struct sm_cmd_rsp_blk, head);
- + list_del(&blk->head);
- + mutex_unlock(&instance->free_lock);
- + }
- +
- + blk->sent = 0;
- + blk->wait = wait;
- + blk->length = sizeof(*hdr) + size;
- +
- + hdr = (VC_SM_MSG_HDR_T *) blk->msg;
- + hdr->type = id;
- + mutex_lock(&instance->lock);
- + hdr->trans_id = blk->id = ++instance->trans_id;
- + mutex_unlock(&instance->lock);
- +
- + if (size)
- + memcpy(hdr->body, msg, size);
- +
- + return blk;
- +}
- +
- +static void
- +vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
- +{
- + if (blk->alloc) {
- + kfree(blk);
- + return;
- + }
- +
- + mutex_lock(&instance->free_lock);
- + list_add(&blk->head, &instance->free_list);
- + mutex_unlock(&instance->free_lock);
- + up(&instance->free_sema);
- +}
- +
- +static int vc_vchi_sm_videocore_io(void *arg)
- +{
- + struct sm_instance *instance = arg;
- + struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
- + VC_SM_RESULT_T *reply;
- + uint32_t reply_len;
- + int32_t status;
- + int svc_use = 1;
- +
- + while (1) {
- + if (svc_use)
- + vchi_service_release(instance->vchi_handle[0]);
- + svc_use = 0;
- + if (!down_interruptible(&instance->io_sema)) {
- + vchi_service_use(instance->vchi_handle[0]);
- + svc_use = 1;
- +
- + do {
- + unsigned int flags;
- + /*
- + * Get new command and move it to response list
- + */
- + mutex_lock(&instance->lock);
- + if (list_empty(&instance->cmd_list)) {
- + /* no more commands to process */
- + mutex_unlock(&instance->lock);
- + break;
- + }
- + cmd =
- + list_first_entry(&instance->cmd_list,
- + struct sm_cmd_rsp_blk,
- + head);
- + list_move(&cmd->head, &instance->rsp_list);
- + cmd->sent = 1;
- + mutex_unlock(&instance->lock);
- +
- + /* Send the command */
- + flags = VCHI_FLAGS_BLOCK_UNTIL_QUEUED;
- + status = vchi_msg_queue(
- + instance->vchi_handle[0],
- + cmd->msg, cmd->length,
- + flags, NULL);
- + if (status) {
- + pr_err("%s: failed to queue message (%d)",
- + __func__, status);
- + }
- +
- + /* If no reply is needed then we're done */
- + if (!cmd->wait) {
- + mutex_lock(&instance->lock);
- + list_del(&cmd->head);
- + mutex_unlock(&instance->lock);
- + vc_vchi_cmd_delete(instance, cmd);
- + continue;
- + }
- +
- + if (status) {
- + up(&cmd->sema);
- + continue;
- + }
- +
- + } while (1);
- +
- + while (!vchi_msg_peek
- + (instance->vchi_handle[0], (void **)&reply,
- + &reply_len, VCHI_FLAGS_NONE)) {
- + mutex_lock(&instance->lock);
- + list_for_each_entry(cmd, &instance->rsp_list,
- + head) {
- + if (cmd->id == reply->trans_id)
- + break;
- + }
- + mutex_unlock(&instance->lock);
- +
- + if (&cmd->head == &instance->rsp_list) {
- + pr_debug("%s: received response %u, throw away...",
- + __func__, reply->trans_id);
- + } else if (reply_len > sizeof(cmd->msg)) {
- + pr_err("%s: reply too big (%u) %u, throw away...",
- + __func__, reply_len,
- + reply->trans_id);
- + } else {
- + memcpy(cmd->msg, reply, reply_len);
- + up(&cmd->sema);
- + }
- +
- + vchi_msg_remove(instance->vchi_handle[0]);
- + }
- +
- + /* Go through the dead list and free them */
- + mutex_lock(&instance->lock);
- + list_for_each_entry_safe(cmd, cmd_tmp,
- + &instance->dead_list, head) {
- + list_del(&cmd->head);
- + vc_vchi_cmd_delete(instance, cmd);
- + }
- + mutex_unlock(&instance->lock);
- + }
- + }
- +
- + return 0;
- +}
- +
- +static void vc_sm_vchi_callback(void *param,
- + const VCHI_CALLBACK_REASON_T reason,
- + void *msg_handle)
- +{
- + struct sm_instance *instance = param;
- +
- + (void)msg_handle;
- +
- + switch (reason) {
- + case VCHI_CALLBACK_MSG_AVAILABLE:
- + up(&instance->io_sema);
- + break;
- +
- + case VCHI_CALLBACK_SERVICE_CLOSED:
- + pr_info("%s: service CLOSED!!", __func__);
- + default:
- + break;
- + }
- +}
- +
- +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
- + VCHI_CONNECTION_T **vchi_connections,
- + uint32_t num_connections)
- +{
- + uint32_t i;
- + struct sm_instance *instance;
- + int status;
- +
- + pr_debug("%s: start", __func__);
- +
- + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
- + pr_err("%s: unsupported number of connections %u (max=%u)",
- + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
- +
- + goto err_null;
- + }
- + /* Allocate memory for this instance */
- + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
- +
- + /* Misc initialisations */
- + mutex_init(&instance->lock);
- + sema_init(&instance->io_sema, 0);
- + INIT_LIST_HEAD(&instance->cmd_list);
- + INIT_LIST_HEAD(&instance->rsp_list);
- + INIT_LIST_HEAD(&instance->dead_list);
- + INIT_LIST_HEAD(&instance->free_list);
- + sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
- + mutex_init(&instance->free_lock);
- + for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
- + sema_init(&instance->free_blk[i].sema, 0);
- + list_add(&instance->free_blk[i].head, &instance->free_list);
- + }
- +
- + /* Open the VCHI service connections */
- + instance->num_connections = num_connections;
- + for (i = 0; i < num_connections; i++) {
- + SERVICE_CREATION_T params = {
- + VCHI_VERSION_EX(VC_SM_VER, VC_SM_MIN_VER),
- + VC_SM_SERVER_NAME,
- + vchi_connections[i],
- + 0,
- + 0,
- + vc_sm_vchi_callback,
- + instance,
- + 0,
- + 0,
- + 0,
- + };
- +
- + status = vchi_service_open(vchi_instance,
- + ¶ms, &instance->vchi_handle[i]);
- + if (status) {
- + pr_err("%s: failed to open VCHI service (%d)",
- + __func__, status);
- +
- + goto err_close_services;
- + }
- + }
- +
- + /* Create the thread which takes care of all io to/from videoocore. */
- + instance->io_thread = kthread_create(&vc_vchi_sm_videocore_io,
- + (void *)instance, "SMIO");
- + if (instance->io_thread == NULL) {
- + pr_err("%s: failed to create SMIO thread", __func__);
- +
- + goto err_close_services;
- + }
- + set_user_nice(instance->io_thread, -10);
- + wake_up_process(instance->io_thread);
- +
- + pr_debug("%s: success - instance 0x%x", __func__, (unsigned)instance);
- + return instance;
- +
- +err_close_services:
- + for (i = 0; i < instance->num_connections; i++) {
- + if (instance->vchi_handle[i] != NULL)
- + vchi_service_close(instance->vchi_handle[i]);
- + }
- + kfree(instance);
- +err_null:
- + pr_debug("%s: FAILED", __func__);
- + return NULL;
- +}
- +
- +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle)
- +{
- + struct sm_instance *instance;
- + uint32_t i;
- +
- + if (handle == NULL) {
- + pr_err("%s: invalid pointer to handle %p", __func__, handle);
- + goto lock;
- + }
- +
- + if (*handle == NULL) {
- + pr_err("%s: invalid handle %p", __func__, *handle);
- + goto lock;
- + }
- +
- + instance = *handle;
- +
- + /* Close all VCHI service connections */
- + for (i = 0; i < instance->num_connections; i++) {
- + int32_t success;
- + vchi_service_use(instance->vchi_handle[i]);
- +
- + success = vchi_service_close(instance->vchi_handle[i]);
- + }
- +
- + kfree(instance);
- +
- + *handle = NULL;
- + return 0;
- +
- +lock:
- + return -EINVAL;
- +}
- +
- +int vc_vchi_sm_send_msg(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_MSG_TYPE msg_id,
- + void *msg, uint32_t msg_size,
- + void *result, uint32_t result_size,
- + uint32_t *cur_trans_id, uint8_t wait_reply)
- +{
- + int status = 0;
- + struct sm_instance *instance = handle;
- + struct sm_cmd_rsp_blk *cmd_blk;
- +
- + if (handle == NULL) {
- + pr_err("%s: invalid handle", __func__);
- + return -EINVAL;
- + }
- + if (msg == NULL) {
- + pr_err("%s: invalid msg pointer", __func__);
- + return -EINVAL;
- + }
- +
- + cmd_blk =
- + vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
- + if (cmd_blk == NULL) {
- + pr_err("[%s]: failed to allocate global tracking resource",
- + __func__);
- + return -ENOMEM;
- + }
- +
- + if (cur_trans_id != NULL)
- + *cur_trans_id = cmd_blk->id;
- +
- + mutex_lock(&instance->lock);
- + list_add_tail(&cmd_blk->head, &instance->cmd_list);
- + mutex_unlock(&instance->lock);
- + up(&instance->io_sema);
- +
- + if (!wait_reply)
- + /* We're done */
- + return 0;
- +
- + /* Wait for the response */
- + if (down_interruptible(&cmd_blk->sema)) {
- + mutex_lock(&instance->lock);
- + if (!cmd_blk->sent) {
- + list_del(&cmd_blk->head);
- + mutex_unlock(&instance->lock);
- + vc_vchi_cmd_delete(instance, cmd_blk);
- + return -ENXIO;
- + }
- + mutex_unlock(&instance->lock);
- +
- + mutex_lock(&instance->lock);
- + list_move(&cmd_blk->head, &instance->dead_list);
- + mutex_unlock(&instance->lock);
- + up(&instance->io_sema);
- + return -EINTR; /* We're done */
- + }
- +
- + if (result && result_size) {
- + memcpy(result, cmd_blk->msg, result_size);
- + } else {
- + VC_SM_RESULT_T *res = (VC_SM_RESULT_T *) cmd_blk->msg;
- + status = (res->success == 0) ? 0 : -ENXIO;
- + }
- +
- + mutex_lock(&instance->lock);
- + list_del(&cmd_blk->head);
- + mutex_unlock(&instance->lock);
- + vc_vchi_cmd_delete(instance, cmd_blk);
- + return status;
- +}
- +
- +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle, VC_SM_ALLOC_T *msg,
- + VC_SM_ALLOC_RESULT_T *result, uint32_t *cur_trans_id)
- +{
- + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ALLOC,
- + msg, sizeof(*msg), result, sizeof(*result),
- + cur_trans_id, 1);
- +}
- +
- +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_FREE_T *msg, uint32_t *cur_trans_id)
- +{
- + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_FREE,
- + msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
- +}
- +
- +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_LOCK_UNLOCK_T *msg,
- + VC_SM_LOCK_RESULT_T *result, uint32_t *cur_trans_id)
- +{
- + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_LOCK,
- + msg, sizeof(*msg), result, sizeof(*result),
- + cur_trans_id, 1);
- +}
- +
- +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_LOCK_UNLOCK_T *msg,
- + uint32_t *cur_trans_id, uint8_t wait_reply)
- +{
- + return vc_vchi_sm_send_msg(handle, wait_reply ?
- + VC_SM_MSG_TYPE_UNLOCK :
- + VC_SM_MSG_TYPE_UNLOCK_NOANS, msg,
- + sizeof(*msg), 0, 0, cur_trans_id,
- + wait_reply);
- +}
- +
- +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle, VC_SM_RESIZE_T *msg,
- + uint32_t *cur_trans_id)
- +{
- + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_RESIZE,
- + msg, sizeof(*msg), 0, 0, cur_trans_id, 1);
- +}
- +
- +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle)
- +{
- + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_WALK_ALLOC,
- + 0, 0, 0, 0, 0, 0);
- +}
- +
- +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle, VC_SM_ACTION_CLEAN_T *msg)
- +{
- + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ACTION_CLEAN,
- + msg, sizeof(*msg), 0, 0, 0, 0);
- +}
- --- /dev/null
- +++ b/drivers/char/broadcom/vc_sm/vc_vchi_sm.h
- @@ -0,0 +1,82 @@
- +/*****************************************************************************
- +* Copyright 2011 Broadcom Corporation. All rights reserved.
- +*
- +* Unless you and Broadcom execute a separate written software license
- +* agreement governing use of this software, this software is licensed to you
- +* under the terms of the GNU General Public License version 2, available at
- +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
- +*
- +* Notwithstanding the above, under no circumstances may you combine this
- +* software in any way with any other Broadcom software provided under a
- +* license other than the GPL, without Broadcom's express prior written
- +* consent.
- +*****************************************************************************/
- +
- +#ifndef __VC_VCHI_SM_H__INCLUDED__
- +#define __VC_VCHI_SM_H__INCLUDED__
- +
- +#include "interface/vchi/vchi.h"
- +
- +#include "vc_sm_defs.h"
- +
- +/* Forward declare.
- +*/
- +typedef struct sm_instance *VC_VCHI_SM_HANDLE_T;
- +
- +/* Initialize the shared memory service, opens up vchi connection to talk to it.
- +*/
- +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
- + VCHI_CONNECTION_T **vchi_connections,
- + uint32_t num_connections);
- +
- +/* Terminates the shared memory service.
- +*/
- +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle);
- +
- +/* Ask the shared memory service to allocate some memory on videocre and
- +** return the result of this allocation (which upon success will be a pointer
- +** to some memory in videocore space).
- +*/
- +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_ALLOC_T *alloc,
- + VC_SM_ALLOC_RESULT_T *alloc_result, uint32_t *trans_id);
- +
- +/* Ask the shared memory service to free up some memory that was previously
- +** allocated by the vc_vchi_sm_alloc function call.
- +*/
- +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_FREE_T *free, uint32_t *trans_id);
- +
- +/* Ask the shared memory service to lock up some memory that was previously
- +** allocated by the vc_vchi_sm_alloc function call.
- +*/
- +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_LOCK_UNLOCK_T *lock_unlock,
- + VC_SM_LOCK_RESULT_T *lock_result, uint32_t *trans_id);
- +
- +/* Ask the shared memory service to unlock some memory that was previously
- +** allocated by the vc_vchi_sm_alloc function call.
- +*/
- +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_LOCK_UNLOCK_T *lock_unlock,
- + uint32_t *trans_id, uint8_t wait_reply);
- +
- +/* Ask the shared memory service to resize some memory that was previously
- +** allocated by the vc_vchi_sm_alloc function call.
- +*/
- +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_RESIZE_T *resize, uint32_t *trans_id);
- +
- +/* Walk the allocated resources on the videocore side, the allocation will
- +** show up in the log. This is purely for debug/information and takes no
- +** specific actions.
- +*/
- +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle);
- +
- +/* Clean up following a previously interrupted action which left the system
- +** in a bad state of some sort.
- +*/
- +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle,
- + VC_SM_ACTION_CLEAN_T *action_clean);
- +
- +#endif /* __VC_VCHI_SM_H__INCLUDED__ */
- --- /dev/null
- +++ b/drivers/char/broadcom/vc_sm/vmcs_sm.c
- @@ -0,0 +1,3211 @@
- +/*****************************************************************************
- +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
- +*
- +* Unless you and Broadcom execute a separate written software license
- +* agreement governing use of this software, this software is licensed to you
- +* under the terms of the GNU General Public License version 2, available at
- +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
- +*
- +* Notwithstanding the above, under no circumstances may you combine this
- +* software in any way with any other Broadcom software provided under a
- +* license other than the GPL, without Broadcom's express prior written
- +* consent.
- +*****************************************************************************/
- +
- +/* ---- Include Files ----------------------------------------------------- */
- +
- +#include <linux/cdev.h>
- +#include <linux/broadcom/vc_mem.h>
- +#include <linux/device.h>
- +#include <linux/debugfs.h>
- +#include <linux/dma-mapping.h>
- +#include <linux/errno.h>
- +#include <linux/fs.h>
- +#include <linux/hugetlb.h>
- +#include <linux/ioctl.h>
- +#include <linux/kernel.h>
- +#include <linux/list.h>
- +#include <linux/module.h>
- +#include <linux/mm.h>
- +#include <linux/pfn.h>
- +#include <linux/proc_fs.h>
- +#include <linux/pagemap.h>
- +#include <linux/semaphore.h>
- +#include <linux/slab.h>
- +#include <linux/seq_file.h>
- +#include <linux/types.h>
- +#include <asm/cacheflush.h>
- +
- +#include "vchiq_connected.h"
- +#include "vc_vchi_sm.h"
- +
- +#include <linux/broadcom/vmcs_sm_ioctl.h>
- +#include "vc_sm_knl.h"
- +
- +/* ---- Private Constants and Types --------------------------------------- */
- +
- +#define DEVICE_NAME "vcsm"
- +#define DEVICE_MINOR 0
- +
- +#define VC_SM_DIR_ROOT_NAME "vc-smem"
- +#define VC_SM_DIR_ALLOC_NAME "alloc"
- +#define VC_SM_STATE "state"
- +#define VC_SM_STATS "statistics"
- +#define VC_SM_RESOURCES "resources"
- +#define VC_SM_DEBUG "debug"
- +#define VC_SM_WRITE_BUF_SIZE 128
- +
- +/* Statistics tracked per resource and globally.
- +*/
- +enum SM_STATS_T {
- + /* Attempt. */
- + ALLOC,
- + FREE,
- + LOCK,
- + UNLOCK,
- + MAP,
- + FLUSH,
- + INVALID,
- +
- + END_ATTEMPT,
- +
- + /* Failure. */
- + ALLOC_FAIL,
- + FREE_FAIL,
- + LOCK_FAIL,
- + UNLOCK_FAIL,
- + MAP_FAIL,
- + FLUSH_FAIL,
- + INVALID_FAIL,
- +
- + END_ALL,
- +
- +};
- +
- +static const char *const sm_stats_human_read[] = {
- + "Alloc",
- + "Free",
- + "Lock",
- + "Unlock",
- + "Map",
- + "Cache Flush",
- + "Cache Invalidate",
- +};
- +
- +typedef int (*VC_SM_SHOW) (struct seq_file *s, void *v);
- +struct SM_PDE_T {
- + VC_SM_SHOW show; /* Debug fs function hookup. */
- + struct dentry *dir_entry; /* Debug fs directory entry. */
- + void *priv_data; /* Private data */
- +
- +};
- +
- +/* Single resource allocation tracked for all devices.
- +*/
- +struct sm_mmap {
- + struct list_head map_list; /* Linked list of maps. */
- +
- + struct SM_RESOURCE_T *resource; /* Pointer to the resource. */
- +
- + pid_t res_pid; /* PID owning that resource. */
- + unsigned int res_vc_hdl; /* Resource handle (videocore). */
- + unsigned int res_usr_hdl; /* Resource handle (user). */
- +
- + long unsigned int res_addr; /* Mapped virtual address. */
- + struct vm_area_struct *vma; /* VM area for this mapping. */
- + unsigned int ref_count; /* Reference count to this vma. */
- +
- + /* Used to link maps associated with a resource. */
- + struct list_head resource_map_list;
- +};
- +
- +/* Single resource allocation tracked for each opened device.
- +*/
- +struct SM_RESOURCE_T {
- + struct list_head resource_list; /* List of resources. */
- + struct list_head global_resource_list; /* Global list of resources. */
- +
- + pid_t pid; /* PID owning that resource. */
- + uint32_t res_guid; /* Unique identifier. */
- + uint32_t lock_count; /* Lock count for this resource. */
- + uint32_t ref_count; /* Ref count for this resource. */
- +
- + uint32_t res_handle; /* Resource allocation handle. */
- + void *res_base_mem; /* Resource base memory address. */
- + uint32_t res_size; /* Resource size allocated. */
- + enum vmcs_sm_cache_e res_cached; /* Resource cache type. */
- + struct SM_RESOURCE_T *res_shared; /* Shared resource */
- +
- + enum SM_STATS_T res_stats[END_ALL]; /* Resource statistics. */
- +
- + uint8_t map_count; /* Counter of mappings for this resource. */
- + struct list_head map_list; /* Maps associated with a resource. */
- +
- + struct SM_PRIV_DATA_T *private;
- +};
- +
- +/* Private file data associated with each opened device.
- +*/
- +struct SM_PRIV_DATA_T {
- + struct list_head resource_list; /* List of resources. */
- +
- + pid_t pid; /* PID of creator. */
- +
- + struct dentry *dir_pid; /* Debug fs entries root. */
- + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
- + struct SM_PDE_T dir_res; /* Debug fs resource sub-tree. */
- +
- + int restart_sys; /* Tracks restart on interrupt. */
- + VC_SM_MSG_TYPE int_action; /* Interrupted action. */
- + uint32_t int_trans_id; /* Interrupted transaction. */
- +
- +};
- +
- +/* Global state information.
- +*/
- +struct SM_STATE_T {
- + VC_VCHI_SM_HANDLE_T sm_handle; /* Handle for videocore service. */
- + struct dentry *dir_root; /* Debug fs entries root. */
- + struct dentry *dir_alloc; /* Debug fs entries allocations. */
- + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
- + struct SM_PDE_T dir_state; /* Debug fs entries state sub-tree. */
- + struct dentry *debug; /* Debug fs entries debug. */
- +
- + struct mutex map_lock; /* Global map lock. */
- + struct list_head map_list; /* List of maps. */
- + struct list_head resource_list; /* List of resources. */
- +
- + enum SM_STATS_T deceased[END_ALL]; /* Natural termination stats. */
- + enum SM_STATS_T terminated[END_ALL]; /* Forced termination stats. */
- + uint32_t res_deceased_cnt; /* Natural termination counter. */
- + uint32_t res_terminated_cnt; /* Forced termination counter. */
- +
- + struct cdev sm_cdev; /* Device. */
- + dev_t sm_devid; /* Device identifier. */
- + struct class *sm_class; /* Class. */
- + struct device *sm_dev; /* Device. */
- +
- + struct SM_PRIV_DATA_T *data_knl; /* Kernel internal data tracking. */
- +
- + struct mutex lock; /* Global lock. */
- + uint32_t guid; /* GUID (next) tracker. */
- +
- +};
- +
- +/* ---- Private Variables ----------------------------------------------- */
- +
- +static struct SM_STATE_T *sm_state;
- +static int sm_inited;
- +
- +static const char *const sm_cache_map_vector[] = {
- + "(null)",
- + "host",
- + "videocore",
- + "host+videocore",
- +};
- +
- +/* ---- Private Function Prototypes -------------------------------------- */
- +
- +/* ---- Private Functions ------------------------------------------------ */
- +
- +static inline unsigned vcaddr_to_pfn(unsigned long vc_addr)
- +{
- + unsigned long pfn = vc_addr & 0x3FFFFFFF;
- + pfn += mm_vc_mem_phys_addr;
- + pfn >>= PAGE_SHIFT;
- + return pfn;
- +}
- +
- +/* Carries over to the state statistics the statistics once owned by a deceased
- +** resource.
- +*/
- +static void vc_sm_resource_deceased(struct SM_RESOURCE_T *p_res, int terminated)
- +{
- + if (sm_state != NULL) {
- + if (p_res != NULL) {
- + int ix;
- +
- + if (terminated)
- + sm_state->res_terminated_cnt++;
- + else
- + sm_state->res_deceased_cnt++;
- +
- + for (ix = 0; ix < END_ALL; ix++) {
- + if (terminated)
- + sm_state->terminated[ix] +=
- + p_res->res_stats[ix];
- + else
- + sm_state->deceased[ix] +=
- + p_res->res_stats[ix];
- + }
- + }
- + }
- +}
- +
- +/* Fetch a videocore handle corresponding to a mapping of the pid+address
- +** returns 0 (ie NULL) if no such handle exists in the global map.
- +*/
- +static unsigned int vmcs_sm_vc_handle_from_pid_and_address(unsigned int pid,
- + unsigned int addr)
- +{
- + struct sm_mmap *map = NULL;
- + unsigned int handle = 0;
- +
- + if (!sm_state || addr == 0)
- + goto out;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + /* Lookup the resource.
- + */
- + if (!list_empty(&sm_state->map_list)) {
- + list_for_each_entry(map, &sm_state->map_list, map_list) {
- + if (map->res_pid != pid || map->res_addr != addr)
- + continue;
- +
- + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> vc-hdl %x (usr-hdl %x)\n",
- + __func__, map, map->res_pid, map->res_addr,
- + map->res_vc_hdl, map->res_usr_hdl);
- +
- + handle = map->res_vc_hdl;
- + break;
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- +out:
- + /* Use a debug log here as it may be a valid situation that we query
- + ** for something that is not mapped, we do not want a kernel log each
- + ** time around.
- + **
- + ** There are other error log that would pop up accordingly if someone
- + ** subsequently tries to use something invalid after being told not to
- + ** use it...
- + */
- + if (handle == 0) {
- + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
- + __func__, pid, addr);
- + }
- +
- + return handle;
- +}
- +
- +/* Fetch a user handle corresponding to a mapping of the pid+address
- +** returns 0 (ie NULL) if no such handle exists in the global map.
- +*/
- +static unsigned int vmcs_sm_usr_handle_from_pid_and_address(unsigned int pid,
- + unsigned int addr)
- +{
- + struct sm_mmap *map = NULL;
- + unsigned int handle = 0;
- +
- + if (!sm_state || addr == 0)
- + goto out;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + /* Lookup the resource.
- + */
- + if (!list_empty(&sm_state->map_list)) {
- + list_for_each_entry(map, &sm_state->map_list, map_list) {
- + if (map->res_pid != pid || map->res_addr != addr)
- + continue;
- +
- + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> usr-hdl %x (vc-hdl %x)\n",
- + __func__, map, map->res_pid, map->res_addr,
- + map->res_usr_hdl, map->res_vc_hdl);
- +
- + handle = map->res_usr_hdl;
- + break;
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- +out:
- + /* Use a debug log here as it may be a valid situation that we query
- + * for something that is not mapped yet.
- + *
- + * There are other error log that would pop up accordingly if someone
- + * subsequently tries to use something invalid after being told not to
- + * use it...
- + */
- + if (handle == 0)
- + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
- + __func__, pid, addr);
- +
- + return handle;
- +}
- +
- +#if defined(DO_NOT_USE)
- +/* Fetch an address corresponding to a mapping of the pid+handle
- +** returns 0 (ie NULL) if no such address exists in the global map.
- +*/
- +static unsigned int vmcs_sm_usr_address_from_pid_and_vc_handle(unsigned int pid,
- + unsigned int hdl)
- +{
- + struct sm_mmap *map = NULL;
- + unsigned int addr = 0;
- +
- + if (sm_state == NULL || hdl == 0)
- + goto out;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + /* Lookup the resource.
- + */
- + if (!list_empty(&sm_state->map_list)) {
- + list_for_each_entry(map, &sm_state->map_list, map_list) {
- + if (map->res_pid != pid || map->res_vc_hdl != hdl)
- + continue;
- +
- + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
- + __func__, map, map->res_pid, map->res_vc_hdl,
- + map->res_usr_hdl, map->res_addr);
- +
- + addr = map->res_addr;
- + break;
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- +out:
- + /* Use a debug log here as it may be a valid situation that we query
- + ** for something that is not mapped, we do not want a kernel log each
- + ** time around.
- + **
- + ** There are other error log that would pop up accordingly if someone
- + ** subsequently tries to use something invalid after being told not to
- + ** use it...
- + */
- + if (addr == 0)
- + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n",
- + __func__, pid, hdl);
- +
- + return addr;
- +}
- +#endif
- +
- +/* Fetch an address corresponding to a mapping of the pid+handle
- +** returns 0 (ie NULL) if no such address exists in the global map.
- +*/
- +static unsigned int vmcs_sm_usr_address_from_pid_and_usr_handle(unsigned int
- + pid,
- + unsigned int
- + hdl)
- +{
- + struct sm_mmap *map = NULL;
- + unsigned int addr = 0;
- +
- + if (sm_state == NULL || hdl == 0)
- + goto out;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + /* Lookup the resource.
- + */
- + if (!list_empty(&sm_state->map_list)) {
- + list_for_each_entry(map, &sm_state->map_list, map_list) {
- + if (map->res_pid != pid || map->res_usr_hdl != hdl)
- + continue;
- +
- + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
- + __func__, map, map->res_pid, map->res_vc_hdl,
- + map->res_usr_hdl, map->res_addr);
- +
- + addr = map->res_addr;
- + break;
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- +out:
- + /* Use a debug log here as it may be a valid situation that we query
- + * for something that is not mapped, we do not want a kernel log each
- + * time around.
- + *
- + * There are other error log that would pop up accordingly if someone
- + * subsequently tries to use something invalid after being told not to
- + * use it...
- + */
- + if (addr == 0)
- + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n", __func__,
- + pid, hdl);
- +
- + return addr;
- +}
- +
- +/* Adds a resource mapping to the global data list.
- +*/
- +static void vmcs_sm_add_map(struct SM_STATE_T *state,
- + struct SM_RESOURCE_T *resource, struct sm_mmap *map)
- +{
- + mutex_lock(&(state->map_lock));
- +
- + /* Add to the global list of mappings
- + */
- + list_add(&map->map_list, &state->map_list);
- +
- + /* Add to the list of mappings for this resource
- + */
- + list_add(&map->resource_map_list, &resource->map_list);
- + resource->map_count++;
- +
- + mutex_unlock(&(state->map_lock));
- +
- + pr_debug("[%s]: added map %p (pid %u, vc-hdl %x, usr-hdl %x, addr %lx)\n",
- + __func__, map, map->res_pid, map->res_vc_hdl,
- + map->res_usr_hdl, map->res_addr);
- +}
- +
- +/* Removes a resource mapping from the global data list.
- +*/
- +static void vmcs_sm_remove_map(struct SM_STATE_T *state,
- + struct SM_RESOURCE_T *resource,
- + struct sm_mmap *map)
- +{
- + mutex_lock(&(state->map_lock));
- +
- + /* Remove from the global list of mappings
- + */
- + list_del(&map->map_list);
- +
- + /* Remove from the list of mapping for this resource
- + */
- + list_del(&map->resource_map_list);
- + if (resource->map_count > 0)
- + resource->map_count--;
- +
- + mutex_unlock(&(state->map_lock));
- +
- + pr_debug("[%s]: removed map %p (pid %d, vc-hdl %x, usr-hdl %x, addr %lx)\n",
- + __func__, map, map->res_pid, map->res_vc_hdl, map->res_usr_hdl,
- + map->res_addr);
- +
- + kfree(map);
- +}
- +
- +/* Read callback for the global state proc entry.
- +*/
- +static int vc_sm_global_state_show(struct seq_file *s, void *v)
- +{
- + struct sm_mmap *map = NULL;
- + int map_count = 0;
- +
- + if (sm_state == NULL)
- + return 0;
- +
- + seq_printf(s, "\nVC-ServiceHandle 0x%x\n",
- + (unsigned int)sm_state->sm_handle);
- +
- + /* Log all applicable mapping(s).
- + */
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + if (!list_empty(&sm_state->map_list)) {
- + list_for_each_entry(map, &sm_state->map_list, map_list) {
- + map_count++;
- +
- + seq_printf(s, "\nMapping 0x%x\n",
- + (unsigned int)map);
- + seq_printf(s, " TGID %u\n",
- + map->res_pid);
- + seq_printf(s, " VC-HDL 0x%x\n",
- + map->res_vc_hdl);
- + seq_printf(s, " USR-HDL 0x%x\n",
- + map->res_usr_hdl);
- + seq_printf(s, " USR-ADDR 0x%lx\n",
- + map->res_addr);
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- + seq_printf(s, "\n\nTotal map count: %d\n\n", map_count);
- +
- + return 0;
- +}
- +
- +static int vc_sm_global_statistics_show(struct seq_file *s, void *v)
- +{
- + int ix;
- +
- + /* Global state tracked statistics.
- + */
- + if (sm_state != NULL) {
- + seq_puts(s, "\nDeceased Resources Statistics\n");
- +
- + seq_printf(s, "\nNatural Cause (%u occurences)\n",
- + sm_state->res_deceased_cnt);
- + for (ix = 0; ix < END_ATTEMPT; ix++) {
- + if (sm_state->deceased[ix] > 0) {
- + seq_printf(s, " %u\t%s\n",
- + sm_state->deceased[ix],
- + sm_stats_human_read[ix]);
- + }
- + }
- + seq_puts(s, "\n");
- + for (ix = 0; ix < END_ATTEMPT; ix++) {
- + if (sm_state->deceased[ix + END_ATTEMPT] > 0) {
- + seq_printf(s, " %u\tFAILED %s\n",
- + sm_state->deceased[ix + END_ATTEMPT],
- + sm_stats_human_read[ix]);
- + }
- + }
- +
- + seq_printf(s, "\nForcefull (%u occurences)\n",
- + sm_state->res_terminated_cnt);
- + for (ix = 0; ix < END_ATTEMPT; ix++) {
- + if (sm_state->terminated[ix] > 0) {
- + seq_printf(s, " %u\t%s\n",
- + sm_state->terminated[ix],
- + sm_stats_human_read[ix]);
- + }
- + }
- + seq_puts(s, "\n");
- + for (ix = 0; ix < END_ATTEMPT; ix++) {
- + if (sm_state->terminated[ix + END_ATTEMPT] > 0) {
- + seq_printf(s, " %u\tFAILED %s\n",
- + sm_state->terminated[ix +
- + END_ATTEMPT],
- + sm_stats_human_read[ix]);
- + }
- + }
- + }
- +
- + return 0;
- +}
- +
- +#if 0
- +/* Read callback for the statistics proc entry.
- +*/
- +static int vc_sm_statistics_show(struct seq_file *s, void *v)
- +{
- + int ix;
- + struct SM_PRIV_DATA_T *file_data;
- + struct SM_RESOURCE_T *resource;
- + int res_count = 0;
- + struct SM_PDE_T *p_pde;
- +
- + p_pde = (struct SM_PDE_T *)(s->private);
- + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
- +
- + if (file_data == NULL)
- + return 0;
- +
- + /* Per process statistics.
- + */
- +
- + seq_printf(s, "\nStatistics for TGID %d\n", file_data->pid);
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + if (!list_empty(&file_data->resource_list)) {
- + list_for_each_entry(resource, &file_data->resource_list,
- + resource_list) {
- + res_count++;
- +
- + seq_printf(s, "\nGUID: 0x%x\n\n",
- + resource->res_guid);
- + for (ix = 0; ix < END_ATTEMPT; ix++) {
- + if (resource->res_stats[ix] > 0) {
- + seq_printf(s,
- + " %u\t%s\n",
- + resource->res_stats[ix],
- + sm_stats_human_read[ix]);
- + }
- + }
- + seq_puts(s, "\n");
- + for (ix = 0; ix < END_ATTEMPT; ix++) {
- + if (resource->res_stats[ix + END_ATTEMPT] > 0) {
- + seq_printf(s,
- + " %u\tFAILED %s\n",
- + resource->res_stats[
- + ix + END_ATTEMPT],
- + sm_stats_human_read[ix]);
- + }
- + }
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + seq_printf(s, "\nResources Count %d\n", res_count);
- +
- + return 0;
- +}
- +#endif
- +
- +#if 0
- +/* Read callback for the allocation proc entry. */
- +static int vc_sm_alloc_show(struct seq_file *s, void *v)
- +{
- + struct SM_PRIV_DATA_T *file_data;
- + struct SM_RESOURCE_T *resource;
- + int alloc_count = 0;
- + struct SM_PDE_T *p_pde;
- +
- + p_pde = (struct SM_PDE_T *)(s->private);
- + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
- +
- + if (!file_data)
- + return 0;
- +
- + /* Per process statistics. */
- + seq_printf(s, "\nAllocation for TGID %d\n", file_data->pid);
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + if (!list_empty(&file_data->resource_list)) {
- + list_for_each_entry(resource, &file_data->resource_list,
- + resource_list) {
- + alloc_count++;
- +
- + seq_printf(s, "\nGUID: 0x%x\n",
- + resource->res_guid);
- + seq_printf(s, "Lock Count: %u\n",
- + resource->lock_count);
- + seq_printf(s, "Mapped: %s\n",
- + (resource->map_count ? "yes" : "no"));
- + seq_printf(s, "VC-handle: 0x%x\n",
- + resource->res_handle);
- + seq_printf(s, "VC-address: 0x%p\n",
- + resource->res_base_mem);
- + seq_printf(s, "VC-size (bytes): %u\n",
- + resource->res_size);
- + seq_printf(s, "Cache: %s\n",
- + sm_cache_map_vector[resource->res_cached]);
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + seq_printf(s, "\n\nTotal allocation count: %d\n\n", alloc_count);
- +
- + return 0;
- +}
- +#endif
- +
- +static int vc_sm_seq_file_show(struct seq_file *s, void *v)
- +{
- + struct SM_PDE_T *sm_pde;
- +
- + sm_pde = (struct SM_PDE_T *)(s->private);
- +
- + if (sm_pde && sm_pde->show)
- + sm_pde->show(s, v);
- +
- + return 0;
- +}
- +
- +static int vc_sm_single_open(struct inode *inode, struct file *file)
- +{
- + return single_open(file, vc_sm_seq_file_show, inode->i_private);
- +}
- +
- +static const struct file_operations vc_sm_debug_fs_fops = {
- + .open = vc_sm_single_open,
- + .read = seq_read,
- + .llseek = seq_lseek,
- + .release = single_release,
- +};
- +
- +/* Adds a resource to the private data list which tracks all the allocated
- +** data.
- +*/
- +static void vmcs_sm_add_resource(struct SM_PRIV_DATA_T *privdata,
- + struct SM_RESOURCE_T *resource)
- +{
- + mutex_lock(&(sm_state->map_lock));
- + list_add(&resource->resource_list, &privdata->resource_list);
- + list_add(&resource->global_resource_list, &sm_state->resource_list);
- + mutex_unlock(&(sm_state->map_lock));
- +
- + pr_debug("[%s]: added resource %p (base addr %p, hdl %x, size %u, cache %u)\n",
- + __func__, resource, resource->res_base_mem,
- + resource->res_handle, resource->res_size, resource->res_cached);
- +}
- +
- +/* Locates a resource and acquire a reference on it.
- +** The resource won't be deleted while there is a reference on it.
- +*/
- +static struct SM_RESOURCE_T *vmcs_sm_acquire_resource(struct SM_PRIV_DATA_T
- + *private,
- + unsigned int res_guid)
- +{
- + struct SM_RESOURCE_T *resource, *ret = NULL;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + list_for_each_entry(resource, &private->resource_list, resource_list) {
- + if (resource->res_guid != res_guid)
- + continue;
- +
- + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
- + __func__, resource, resource->res_guid,
- + resource->res_base_mem, resource->res_handle,
- + resource->res_size, resource->res_cached);
- + resource->ref_count++;
- + ret = resource;
- + break;
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + return ret;
- +}
- +
- +/* Locates a resource and acquire a reference on it.
- +** The resource won't be deleted while there is a reference on it.
- +*/
- +static struct SM_RESOURCE_T *vmcs_sm_acquire_first_resource(
- + struct SM_PRIV_DATA_T *private)
- +{
- + struct SM_RESOURCE_T *resource, *ret = NULL;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + list_for_each_entry(resource, &private->resource_list, resource_list) {
- + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
- + __func__, resource, resource->res_guid,
- + resource->res_base_mem, resource->res_handle,
- + resource->res_size, resource->res_cached);
- + resource->ref_count++;
- + ret = resource;
- + break;
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + return ret;
- +}
- +
- +/* Locates a resource and acquire a reference on it.
- +** The resource won't be deleted while there is a reference on it.
- +*/
- +static struct SM_RESOURCE_T *vmcs_sm_acquire_global_resource(unsigned int
- + res_guid)
- +{
- + struct SM_RESOURCE_T *resource, *ret = NULL;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + list_for_each_entry(resource, &sm_state->resource_list,
- + global_resource_list) {
- + if (resource->res_guid != res_guid)
- + continue;
- +
- + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
- + __func__, resource, resource->res_guid,
- + resource->res_base_mem, resource->res_handle,
- + resource->res_size, resource->res_cached);
- + resource->ref_count++;
- + ret = resource;
- + break;
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + return ret;
- +}
- +
- +/* Release a previously acquired resource.
- +** The resource will be deleted when its refcount reaches 0.
- +*/
- +static void vmcs_sm_release_resource(struct SM_RESOURCE_T *resource, int force)
- +{
- + struct SM_PRIV_DATA_T *private = resource->private;
- + struct sm_mmap *map, *map_tmp;
- + struct SM_RESOURCE_T *res_tmp;
- + int ret;
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + if (--resource->ref_count) {
- + if (force)
- + pr_err("[%s]: resource %p in use\n", __func__, resource);
- +
- + mutex_unlock(&(sm_state->map_lock));
- + return;
- + }
- +
- + /* Time to free the resource. Start by removing it from the list */
- + list_del(&resource->resource_list);
- + list_del(&resource->global_resource_list);
- +
- + /* Walk the global resource list, find out if the resource is used
- + * somewhere else. In which case we don't want to delete it.
- + */
- + list_for_each_entry(res_tmp, &sm_state->resource_list,
- + global_resource_list) {
- + if (res_tmp->res_handle == resource->res_handle) {
- + resource->res_handle = 0;
- + break;
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + pr_debug("[%s]: freeing data - guid %x, hdl %x, base address %p\n",
- + __func__, resource->res_guid, resource->res_handle,
- + resource->res_base_mem);
- + resource->res_stats[FREE]++;
- +
- + /* Make sure the resource we're removing is unmapped first */
- + if (resource->map_count && !list_empty(&resource->map_list)) {
- + down_write(¤t->mm->mmap_sem);
- + list_for_each_entry_safe(map, map_tmp, &resource->map_list,
- + resource_map_list) {
- + ret =
- + do_munmap(current->mm, map->res_addr,
- + resource->res_size);
- + if (ret) {
- + pr_err("[%s]: could not unmap resource %p\n",
- + __func__, resource);
- + }
- + }
- + up_write(¤t->mm->mmap_sem);
- + }
- +
- + /* Free up the videocore allocated resource.
- + */
- + if (resource->res_handle) {
- + VC_SM_FREE_T free = {
- + resource->res_handle, resource->res_base_mem
- + };
- + int status = vc_vchi_sm_free(sm_state->sm_handle, &free,
- + &private->int_trans_id);
- + if (status != 0 && status != -EINTR) {
- + pr_err("[%s]: failed to free memory on videocore (status: %u, trans_id: %u)\n",
- + __func__, status, private->int_trans_id);
- + resource->res_stats[FREE_FAIL]++;
- + ret = -EPERM;
- + }
- + }
- +
- + /* Free up the shared resource.
- + */
- + if (resource->res_shared)
- + vmcs_sm_release_resource(resource->res_shared, 0);
- +
- + /* Free up the local resource tracking this allocation.
- + */
- + vc_sm_resource_deceased(resource, force);
- + kfree(resource);
- +}
- +
- +/* Dump the map table for the driver. If process is -1, dumps the whole table,
- +** if process is a valid pid (non -1) dump only the entries associated with the
- +** pid of interest.
- +*/
- +static void vmcs_sm_host_walk_map_per_pid(int pid)
- +{
- + struct sm_mmap *map = NULL;
- +
- + /* Make sure the device was started properly.
- + */
- + if (sm_state == NULL) {
- + pr_err("[%s]: invalid device\n", __func__);
- + return;
- + }
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + /* Log all applicable mapping(s).
- + */
- + if (!list_empty(&sm_state->map_list)) {
- + list_for_each_entry(map, &sm_state->map_list, map_list) {
- + if (pid == -1 || map->res_pid == pid) {
- + pr_info("[%s]: tgid: %u - vc-hdl: %x, usr-hdl: %x, usr-addr: %lx\n",
- + __func__, map->res_pid, map->res_vc_hdl,
- + map->res_usr_hdl, map->res_addr);
- + }
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + return;
- +}
- +
- +/* Dump the allocation table from host side point of view. This only dumps the
- +** data allocated for this process/device referenced by the file_data.
- +*/
- +static void vmcs_sm_host_walk_alloc(struct SM_PRIV_DATA_T *file_data)
- +{
- + struct SM_RESOURCE_T *resource = NULL;
- +
- + /* Make sure the device was started properly.
- + */
- + if ((sm_state == NULL) || (file_data == NULL)) {
- + pr_err("[%s]: invalid device\n", __func__);
- + return;
- + }
- +
- + mutex_lock(&(sm_state->map_lock));
- +
- + if (!list_empty(&file_data->resource_list)) {
- + list_for_each_entry(resource, &file_data->resource_list,
- + resource_list) {
- + pr_info("[%s]: guid: %x - hdl: %x, vc-mem: %p, size: %u, cache: %u\n",
- + __func__, resource->res_guid, resource->res_handle,
- + resource->res_base_mem, resource->res_size,
- + resource->res_cached);
- + }
- + }
- +
- + mutex_unlock(&(sm_state->map_lock));
- +
- + return;
- +}
- +
- +/* Create support for private data tracking.
- +*/
- +static struct SM_PRIV_DATA_T *vc_sm_create_priv_data(pid_t id)
- +{
- + char alloc_name[32];
- + struct SM_PRIV_DATA_T *file_data = NULL;
- +
- + /* Allocate private structure. */
- + file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
- +
- + if (!file_data) {
- + pr_err("[%s]: cannot allocate file data\n", __func__);
- + goto out;
- + }
- +
- + snprintf(alloc_name, sizeof(alloc_name), "%d", id);
- +
- + INIT_LIST_HEAD(&file_data->resource_list);
- + file_data->pid = id;
- + file_data->dir_pid = debugfs_create_dir(alloc_name,
- + sm_state->dir_alloc);
- +#if 0
- + /* TODO: fix this to support querying statistics per pid */
- +
- + if (IS_ERR_OR_NULL(file_data->dir_pid)) {
- + file_data->dir_pid = NULL;
- + } else {
- + struct dentry *dir_entry;
- +
- + dir_entry = debugfs_create_file(VC_SM_RESOURCES, S_IRUGO,
- + file_data->dir_pid, file_data,
- + vc_sm_debug_fs_fops);
- +
- + file_data->dir_res.dir_entry = dir_entry;
- + file_data->dir_res.priv_data = file_data;
- + file_data->dir_res.show = &vc_sm_alloc_show;
- +
- + dir_entry = debugfs_create_file(VC_SM_STATS, S_IRUGO,
- + file_data->dir_pid, file_data,
- + vc_sm_debug_fs_fops);
- +
- + file_data->dir_res.dir_entry = dir_entry;
- + file_data->dir_res.priv_data = file_data;
- + file_data->dir_res.show = &vc_sm_statistics_show;
- + }
- + pr_debug("[%s]: private data allocated %p\n", __func__, file_data);
- +
- +#endif
- +out:
- + return file_data;
- +}
- +
- +/* Open the device. Creates a private state to help track all allocation
- +** associated with this device.
- +*/
- +static int vc_sm_open(struct inode *inode, struct file *file)
- +{
- + int ret = 0;
- +
- + /* Make sure the device was started properly.
- + */
- + if (!sm_state) {
- + pr_err("[%s]: invalid device\n", __func__);
- + ret = -EPERM;
- + goto out;
- + }
- +
- + file->private_data = vc_sm_create_priv_data(current->tgid);
- + if (file->private_data == NULL) {
- + pr_err("[%s]: failed to create data tracker\n", __func__);
- +
- + ret = -ENOMEM;
- + goto out;
- + }
- +
- +out:
- + return ret;
- +}
- +
- +/* Close the device. Free up all resources still associated with this device
- +** at the time.
- +*/
- +static int vc_sm_release(struct inode *inode, struct file *file)
- +{
- + struct SM_PRIV_DATA_T *file_data =
- + (struct SM_PRIV_DATA_T *)file->private_data;
- + struct SM_RESOURCE_T *resource;
- + int ret = 0;
- +
- + /* Make sure the device was started properly.
- + */
- + if (sm_state == NULL || file_data == NULL) {
- + pr_err("[%s]: invalid device\n", __func__);
- + ret = -EPERM;
- + goto out;
- + }
- +
- + pr_debug("[%s]: using private data %p\n", __func__, file_data);
- +
- + if (file_data->restart_sys == -EINTR) {
- + VC_SM_ACTION_CLEAN_T action_clean;
- +
- + pr_debug("[%s]: releasing following EINTR on %u (trans_id: %u) (likely due to signal)...\n",
- + __func__, file_data->int_action,
- + file_data->int_trans_id);
- +
- + action_clean.res_action = file_data->int_action;
- + action_clean.action_trans_id = file_data->int_trans_id;
- +
- + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
- + }
- +
- + while ((resource = vmcs_sm_acquire_first_resource(file_data)) != NULL) {
- + vmcs_sm_release_resource(resource, 0);
- + vmcs_sm_release_resource(resource, 1);
- + }
- +
- + /* Remove the corresponding proc entry. */
- + debugfs_remove_recursive(file_data->dir_pid);
- +
- + /* Terminate the private data.
- + */
- + kfree(file_data);
- +
- +out:
- + return ret;
- +}
- +
- +static void vcsm_vma_open(struct vm_area_struct *vma)
- +{
- + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
- +
- + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
- + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
- + (int)vma->vm_pgoff);
- +
- + map->ref_count++;
- +}
- +
- +static void vcsm_vma_close(struct vm_area_struct *vma)
- +{
- + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
- +
- + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
- + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
- + (int)vma->vm_pgoff);
- +
- + map->ref_count--;
- +
- + /* Remove from the map table.
- + */
- + if (map->ref_count == 0)
- + vmcs_sm_remove_map(sm_state, map->resource, map);
- +}
- +
- +static int vcsm_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
- +{
- + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
- + struct SM_RESOURCE_T *resource = map->resource;
- + pgoff_t page_offset;
- + unsigned long pfn;
- + int ret = 0;
- +
- + /* Lock the resource if necessary.
- + */
- + if (!resource->lock_count) {
- + VC_SM_LOCK_UNLOCK_T lock_unlock;
- + VC_SM_LOCK_RESULT_T lock_result;
- + int status;
- +
- + lock_unlock.res_handle = resource->res_handle;
- + lock_unlock.res_mem = resource->res_base_mem;
- +
- + pr_debug("[%s]: attempt to lock data - hdl %x, base address %p\n",
- + __func__, lock_unlock.res_handle, lock_unlock.res_mem);
- +
- + /* Lock the videocore allocated resource.
- + */
- + status = vc_vchi_sm_lock(sm_state->sm_handle,
- + &lock_unlock, &lock_result, 0);
- + if ((status != 0) ||
- + ((status == 0) && (lock_result.res_mem == NULL))) {
- + pr_err("[%s]: failed to lock memory on videocore (status: %u)\n",
- + __func__, status);
- + resource->res_stats[LOCK_FAIL]++;
- + return VM_FAULT_SIGBUS;
- + }
- +
- + pfn = vcaddr_to_pfn((unsigned long)resource->res_base_mem);
- + outer_inv_range(__pfn_to_phys(pfn),
- + __pfn_to_phys(pfn) + resource->res_size);
- +
- + resource->res_stats[LOCK]++;
- + resource->lock_count++;
- +
- + /* Keep track of the new base memory.
- + */
- + if ((lock_result.res_mem != NULL) &&
- + (lock_result.res_old_mem != NULL) &&
- + (lock_result.res_mem != lock_result.res_old_mem)) {
- + resource->res_base_mem = lock_result.res_mem;
- + }
- + }
- +
- + /* We don't use vmf->pgoff since that has the fake offset */
- + page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start);
- + pfn = (uint32_t)resource->res_base_mem & 0x3FFFFFFF;
- + pfn += mm_vc_mem_phys_addr;
- + pfn += page_offset;
- + pfn >>= PAGE_SHIFT;
- +
- + /* Finally, remap it */
- + ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
- +
- + switch (ret) {
- + case 0:
- + case -ERESTARTSYS:
- + return VM_FAULT_NOPAGE;
- + case -ENOMEM:
- + case -EAGAIN:
- + return VM_FAULT_OOM;
- + default:
- + return VM_FAULT_SIGBUS;
- + }
- +}
- +
- +static struct vm_operations_struct vcsm_vm_ops = {
- + .open = vcsm_vma_open,
- + .close = vcsm_vma_close,
- + .fault = vcsm_vma_fault,
- +};
- +
- +/* Walks a VMA and clean each valid page from the cache */
- +static void vcsm_vma_cache_clean_page_range(unsigned long addr,
- + unsigned long end)
- +{
- + pgd_t *pgd;
- + pud_t *pud;
- + pmd_t *pmd;
- + pte_t *pte;
- + unsigned long pgd_next, pud_next, pmd_next;
- +
- + if (addr >= end)
- + return;
- +
- + /* Walk PGD */
- + pgd = pgd_offset(current->mm, addr);
- + do {
- + pgd_next = pgd_addr_end(addr, end);
- +
- + if (pgd_none(*pgd) || pgd_bad(*pgd))
- + continue;
- +
- + /* Walk PUD */
- + pud = pud_offset(pgd, addr);
- + do {
- + pud_next = pud_addr_end(addr, pgd_next);
- + if (pud_none(*pud) || pud_bad(*pud))
- + continue;
- +
- + /* Walk PMD */
- + pmd = pmd_offset(pud, addr);
- + do {
- + pmd_next = pmd_addr_end(addr, pud_next);
- + if (pmd_none(*pmd) || pmd_bad(*pmd))
- + continue;
- +
- + /* Walk PTE */
- + pte = pte_offset_map(pmd, addr);
- + do {
- + if (pte_none(*pte)
- + || !pte_present(*pte))
- + continue;
- +
- + /* Clean + invalidate */
- + dmac_flush_range((const void *) addr,
- + (const void *)
- + (addr + PAGE_SIZE));
- +
- + } while (pte++, addr +=
- + PAGE_SIZE, addr != pmd_next);
- + pte_unmap(pte);
- +
- + } while (pmd++, addr = pmd_next, addr != pud_next);
- +
- + } while (pud++, addr = pud_next, addr != pgd_next);
- + } while (pgd++, addr = pgd_next, addr != end);
- +}
- +
- +/* Map an allocated data into something that the user space.
- +*/
- +static int vc_sm_mmap(struct file *file, struct vm_area_struct *vma)
- +{
- + int ret = 0;
- + struct SM_PRIV_DATA_T *file_data =
- + (struct SM_PRIV_DATA_T *)file->private_data;
- + struct SM_RESOURCE_T *resource = NULL;
- + struct sm_mmap *map = NULL;
- +
- + /* Make sure the device was started properly.
- + */
- + if ((sm_state == NULL) || (file_data == NULL)) {
- + pr_err("[%s]: invalid device\n", __func__);
- + return -EPERM;
- + }
- +
- + pr_debug("[%s]: private data %p, guid %x\n", __func__, file_data,
- + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
- +
- + /* We lookup to make sure that the data we are being asked to mmap is
- + ** something that we allocated.
- + **
- + ** We use the offset information as the key to tell us which resource
- + ** we are mapping.
- + */
- + resource = vmcs_sm_acquire_resource(file_data,
- + ((unsigned int)vma->vm_pgoff <<
- + PAGE_SHIFT));
- + if (resource == NULL) {
- + pr_err("[%s]: failed to locate resource for guid %x\n", __func__,
- + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
- + return -ENOMEM;
- + }
- +
- + pr_debug("[%s]: guid %x, tgid %u, %u, %u\n",
- + __func__, resource->res_guid, current->tgid, resource->pid,
- + file_data->pid);
- +
- + /* Check permissions.
- + */
- + if (resource->pid && (resource->pid != current->tgid)) {
- + pr_err("[%s]: current tgid %u != %u owner\n",
- + __func__, current->tgid, resource->pid);
- + ret = -EPERM;
- + goto error;
- + }
- +
- + /* Verify that what we are asked to mmap is proper.
- + */
- + if (resource->res_size != (unsigned int)(vma->vm_end - vma->vm_start)) {
- + pr_err("[%s]: size inconsistency (resource: %u - mmap: %u)\n",
- + __func__,
- + resource->res_size,
- + (unsigned int)(vma->vm_end - vma->vm_start));
- +
- + ret = -EINVAL;
- + goto error;
- + }
- +
- + /* Keep track of the tuple in the global resource list such that one
- + * can do a mapping lookup for address/memory handle.
- + */
- + map = kzalloc(sizeof(*map), GFP_KERNEL);
- + if (map == NULL) {
- + pr_err("[%s]: failed to allocate global tracking resource\n",
- + __func__);
- + ret = -ENOMEM;
- + goto error;
- + }
- +
- + map->res_pid = current->tgid;
- + map->res_vc_hdl = resource->res_handle;
- + map->res_usr_hdl = resource->res_guid;
- + map->res_addr = (long unsigned int)vma->vm_start;
- + map->resource = resource;
- + map->vma = vma;
- + vmcs_sm_add_map(sm_state, resource, map);
- +
- + /* We are not actually mapping the pages, we just provide a fault
- + ** handler to allow pages to be mapped when accessed
- + */
- + vma->vm_flags |=
- + VM_IO | VM_PFNMAP | VM_DONTCOPY | VM_DONTEXPAND;
- + vma->vm_ops = &vcsm_vm_ops;
- + vma->vm_private_data = map;
- +
- + /* vm_pgoff is the first PFN of the mapped memory */
- + vma->vm_pgoff = (unsigned long)resource->res_base_mem & 0x3FFFFFFF;
- + vma->vm_pgoff += mm_vc_mem_phys_addr;
- + vma->vm_pgoff >>= PAGE_SHIFT;
- +
- + if ((resource->res_cached == VMCS_SM_CACHE_NONE) ||
- + (resource->res_cached == VMCS_SM_CACHE_VC)) {
- + /* Allocated non host cached memory, honour it.
- + */
- + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
- + }
- +
- + pr_debug("[%s]: resource %p (guid %x) - cnt %u, base address %p, handle %x, size %u (%u), cache %u\n",
- + __func__,
- + resource, resource->res_guid, resource->lock_count,
- + resource->res_base_mem, resource->res_handle,
- + resource->res_size, (unsigned int)(vma->vm_end - vma->vm_start),
- + resource->res_cached);
- +
- + pr_debug("[%s]: resource %p (base address %p, handle %x) - map-count %d, usr-addr %x\n",
- + __func__, resource, resource->res_base_mem,
- + resource->res_handle, resource->map_count,
- + (unsigned int)vma->vm_start);
- +
- + vcsm_vma_open(vma);
- + resource->res_stats[MAP]++;
- + vmcs_sm_release_resource(resource, 0);
- + return 0;
- +
- +error:
- + resource->res_stats[MAP_FAIL]++;
- + vmcs_sm_release_resource(resource, 0);
- + return ret;
- +}
- +
- +/* Allocate a shared memory handle and block.
- +*/
- +int vc_sm_ioctl_alloc(struct SM_PRIV_DATA_T *private,
- + struct vmcs_sm_ioctl_alloc *ioparam)
- +{
- + int ret = 0;
- + int status;
- + struct SM_RESOURCE_T *resource;
- + VC_SM_ALLOC_T alloc = { 0 };
- + VC_SM_ALLOC_RESULT_T result = { 0 };
- +
- + /* Setup our allocation parameters */
- + alloc.type = ((ioparam->cached == VMCS_SM_CACHE_VC)
- + || (ioparam->cached ==
- + VMCS_SM_CACHE_BOTH)) ? VC_SM_ALLOC_CACHED :
- + VC_SM_ALLOC_NON_CACHED;
- + alloc.base_unit = ioparam->size;
- + alloc.num_unit = ioparam->num;
- + alloc.allocator = current->tgid;
- + /* Align to kernel page size */
- + alloc.alignement = 4096;
- + /* Align the size to the kernel page size */
- + alloc.base_unit =
- + (alloc.base_unit + alloc.alignement - 1) & ~(alloc.alignement - 1);
- + if (*ioparam->name) {
- + memcpy(alloc.name, ioparam->name, sizeof(alloc.name) - 1);
- + } else {
- + memcpy(alloc.name, VMCS_SM_RESOURCE_NAME_DEFAULT,
- + sizeof(VMCS_SM_RESOURCE_NAME_DEFAULT));
- + }
- +
- + pr_debug("[%s]: attempt to allocate \"%s\" data - type %u, base %u (%u), num %u, alignement %u\n",
- + __func__, alloc.name, alloc.type, ioparam->size,
- + alloc.base_unit, alloc.num_unit, alloc.alignement);
- +
- + /* Allocate local resource to track this allocation.
- + */
- + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
- + if (!resource) {
- + ret = -ENOMEM;
- + goto error;
- + }
- + INIT_LIST_HEAD(&resource->map_list);
- + resource->ref_count++;
- + resource->pid = current->tgid;
- +
- + /* Allocate the videocore resource.
- + */
- + status = vc_vchi_sm_alloc(sm_state->sm_handle, &alloc, &result,
- + &private->int_trans_id);
- + if (status == -EINTR) {
- + pr_debug("[%s]: requesting allocate memory action restart (trans_id: %u)\n",
- + __func__, private->int_trans_id);
- + ret = -ERESTARTSYS;
- + private->restart_sys = -EINTR;
- + private->int_action = VC_SM_MSG_TYPE_ALLOC;
- + goto error;
- + } else if (status != 0 || (status == 0 && result.res_mem == NULL)) {
- + pr_err("[%s]: failed to allocate memory on videocore (status: %u, trans_id: %u)\n",
- + __func__, status, private->int_trans_id);
- + ret = -ENOMEM;
- + resource->res_stats[ALLOC_FAIL]++;
- + goto error;
- + }
- +
- + /* Keep track of the resource we created.
- + */
- + resource->private = private;
- + resource->res_handle = result.res_handle;
- + resource->res_base_mem = result.res_mem;
- + resource->res_size = alloc.base_unit * alloc.num_unit;
- + resource->res_cached = ioparam->cached;
- +
- + /* Kernel/user GUID. This global identifier is used for mmap'ing the
- + * allocated region from user space, it is passed as the mmap'ing
- + * offset, we use it to 'hide' the videocore handle/address.
- + */
- + mutex_lock(&sm_state->lock);
- + resource->res_guid = ++sm_state->guid;
- + mutex_unlock(&sm_state->lock);
- + resource->res_guid <<= PAGE_SHIFT;
- +
- + vmcs_sm_add_resource(private, resource);
- +
- + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
- + __func__, resource->res_guid, resource->res_handle,
- + resource->res_base_mem, resource->res_size,
- + resource->res_cached);
- +
- + /* We're done */
- + resource->res_stats[ALLOC]++;
- + ioparam->handle = resource->res_guid;
- + return 0;
- +
- +error:
- + pr_err("[%s]: failed to allocate \"%s\" data (%i) - type %u, base %u (%u), num %u, alignment %u\n",
- + __func__, alloc.name, ret, alloc.type, ioparam->size,
- + alloc.base_unit, alloc.num_unit, alloc.alignement);
- + if (resource != NULL) {
- + vc_sm_resource_deceased(resource, 1);
- + kfree(resource);
- + }
- + return ret;
- +}
- +
- +/* Share an allocate memory handle and block.
- +*/
- +int vc_sm_ioctl_alloc_share(struct SM_PRIV_DATA_T *private,
- + struct vmcs_sm_ioctl_alloc_share *ioparam)
- +{
- + struct SM_RESOURCE_T *resource, *shared_resource;
- + int ret = 0;
- +
- + pr_debug("[%s]: attempt to share resource %u\n", __func__,
- + ioparam->handle);
- +
- + shared_resource = vmcs_sm_acquire_global_resource(ioparam->handle);
- + if (shared_resource == NULL) {
- + ret = -ENOMEM;
- + goto error;
- + }
- +
- + /* Allocate local resource to track this allocation.
- + */
- + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
- + if (resource == NULL) {
- + pr_err("[%s]: failed to allocate local tracking resource\n",
- + __func__);
- + ret = -ENOMEM;
- + goto error;
- + }
- + INIT_LIST_HEAD(&resource->map_list);
- + resource->ref_count++;
- + resource->pid = current->tgid;
- +
- + /* Keep track of the resource we created.
- + */
- + resource->private = private;
- + resource->res_handle = shared_resource->res_handle;
- + resource->res_base_mem = shared_resource->res_base_mem;
- + resource->res_size = shared_resource->res_size;
- + resource->res_cached = shared_resource->res_cached;
- + resource->res_shared = shared_resource;
- +
- + mutex_lock(&sm_state->lock);
- + resource->res_guid = ++sm_state->guid;
- + mutex_unlock(&sm_state->lock);
- + resource->res_guid <<= PAGE_SHIFT;
- +
- + vmcs_sm_add_resource(private, resource);
- +
- + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
- + __func__, resource->res_guid, resource->res_handle,
- + resource->res_base_mem, resource->res_size,
- + resource->res_cached);
- +
- + /* We're done */
- + resource->res_stats[ALLOC]++;
- + ioparam->handle = resource->res_guid;
- + ioparam->size = resource->res_size;
- + return 0;
- +
- +error:
- + pr_err("[%s]: failed to share %u\n", __func__, ioparam->handle);
- + if (shared_resource != NULL)
- + vmcs_sm_release_resource(shared_resource, 0);
- +
- + return ret;
- +}
- +
- +/* Free a previously allocated shared memory handle and block.
- +*/
- +static int vc_sm_ioctl_free(struct SM_PRIV_DATA_T *private,
- + struct vmcs_sm_ioctl_free *ioparam)
- +{
- + struct SM_RESOURCE_T *resource =
- + vmcs_sm_acquire_resource(private, ioparam->handle);
- +
- + if (resource == NULL) {
- + pr_err("[%s]: resource for guid %u does not exist\n", __func__,
- + ioparam->handle);
- + return -EINVAL;
- + }
- +
- + /* Check permissions.
- + */
- + if (resource->pid && (resource->pid != current->tgid)) {
- + pr_err("[%s]: current tgid %u != %u owner\n",
- + __func__, current->tgid, resource->pid);
- + vmcs_sm_release_resource(resource, 0);
- + return -EPERM;
- + }
- +
- + vmcs_sm_release_resource(resource, 0);
- + vmcs_sm_release_resource(resource, 0);
- + return 0;
- +}
- +
- +/* Resize a previously allocated shared memory handle and block.
- +*/
- +static int vc_sm_ioctl_resize(struct SM_PRIV_DATA_T *private,
- + struct vmcs_sm_ioctl_resize *ioparam)
- +{
- + int ret = 0;
- + int status;
- + VC_SM_RESIZE_T resize;
- + struct SM_RESOURCE_T *resource;
- +
- + /* Locate resource from GUID.
- + */
- + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
- + if (!resource) {
- + pr_err("[%s]: failed resource - guid %x\n",
- + __func__, ioparam->handle);
- + ret = -EFAULT;
- + goto error;
- + }
- +
- + /* If the resource is locked, its reference count will be not NULL,
- + ** in which case we will not be allowed to resize it anyways, so
- + ** reject the attempt here.
- + */
- + if (resource->lock_count != 0) {
- + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
- + __func__, ioparam->handle, resource->lock_count);
- + ret = -EFAULT;
- + goto error;
- + }
- +
- + /* Check permissions.
- + */
- + if (resource->pid && (resource->pid != current->tgid)) {
- + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
- + current->tgid, resource->pid);
- + ret = -EPERM;
- + goto error;
- + }
- +
- + if (resource->map_count != 0) {
- + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
- + __func__, ioparam->handle, resource->map_count);
- + ret = -EFAULT;
- + goto error;
- + }
- +
- + resize.res_handle = resource->res_handle;
- + resize.res_mem = resource->res_base_mem;
- + resize.res_new_size = ioparam->new_size;
- +
- + pr_debug("[%s]: attempt to resize data - guid %x, hdl %x, base address %p\n",
- + __func__, ioparam->handle, resize.res_handle, resize.res_mem);
- +
- + /* Resize the videocore allocated resource.
- + */
- + status = vc_vchi_sm_resize(sm_state->sm_handle, &resize,
- + &private->int_trans_id);
- + if (status == -EINTR) {
- + pr_debug("[%s]: requesting resize memory action restart (trans_id: %u)\n",
- + __func__, private->int_trans_id);
- + ret = -ERESTARTSYS;
- + private->restart_sys = -EINTR;
- + private->int_action = VC_SM_MSG_TYPE_RESIZE;
- + goto error;
- + } else if (status != 0) {
- + pr_err("[%s]: failed to resize memory on videocore (status: %u, trans_id: %u)\n",
- + __func__, status, private->int_trans_id);
- + ret = -EPERM;
- + goto error;
- + }
- +
- + pr_debug("[%s]: success to resize data - hdl %x, size %d -> %d\n",
- + __func__, resize.res_handle, resource->res_size,
- + resize.res_new_size);
- +
- + /* Successfully resized, save the information and inform the user.
- + */
- + ioparam->old_size = resource->res_size;
- + resource->res_size = resize.res_new_size;
- +
- +error:
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + return ret;
- +}
- +
- +/* Lock a previously allocated shared memory handle and block.
- +*/
- +static int vc_sm_ioctl_lock(struct SM_PRIV_DATA_T *private,
- + struct vmcs_sm_ioctl_lock_unlock *ioparam,
- + int change_cache, enum vmcs_sm_cache_e cache_type,
- + unsigned int vc_addr)
- +{
- + int status;
- + VC_SM_LOCK_UNLOCK_T lock;
- + VC_SM_LOCK_RESULT_T result;
- + struct SM_RESOURCE_T *resource;
- + int ret = 0;
- + struct sm_mmap *map, *map_tmp;
- + long unsigned int phys_addr;
- +
- + map = NULL;
- +
- + /* Locate resource from GUID.
- + */
- + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
- + if (resource == NULL) {
- + ret = -EINVAL;
- + goto error;
- + }
- +
- + /* Check permissions.
- + */
- + if (resource->pid && (resource->pid != current->tgid)) {
- + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
- + current->tgid, resource->pid);
- + ret = -EPERM;
- + goto error;
- + }
- +
- + lock.res_handle = resource->res_handle;
- + lock.res_mem = resource->res_base_mem;
- +
- + /* Take the lock and get the address to be mapped.
- + */
- + if (vc_addr == 0) {
- + pr_debug("[%s]: attempt to lock data - guid %x, hdl %x, base address %p\n",
- + __func__, ioparam->handle, lock.res_handle,
- + lock.res_mem);
- +
- + /* Lock the videocore allocated resource.
- + */
- + status = vc_vchi_sm_lock(sm_state->sm_handle, &lock, &result,
- + &private->int_trans_id);
- + if (status == -EINTR) {
- + pr_debug("[%s]: requesting lock memory action restart (trans_id: %u)\n",
- + __func__, private->int_trans_id);
- + ret = -ERESTARTSYS;
- + private->restart_sys = -EINTR;
- + private->int_action = VC_SM_MSG_TYPE_LOCK;
- + goto error;
- + } else if (status != 0 ||
- + (status == 0 && result.res_mem == NULL)) {
- + pr_err("[%s]: failed to lock memory on videocore (status: %u, trans_id: %u)\n",
- + __func__, status, private->int_trans_id);
- + ret = -EPERM;
- + resource->res_stats[LOCK_FAIL]++;
- + goto error;
- + }
- +
- + pr_debug("[%s]: succeed to lock data - hdl %x, base address %p (%p), ref-cnt %d\n",
- + __func__, lock.res_handle, result.res_mem,
- + lock.res_mem, resource->lock_count);
- + }
- + /* Lock assumed taken already, address to be mapped is known.
- + */
- + else
- + resource->res_base_mem = (void *)vc_addr;
- +
- + resource->res_stats[LOCK]++;
- + resource->lock_count++;
- +
- + /* Keep track of the new base memory allocation if it has changed.
- + */
- + if ((vc_addr == 0) &&
- + (result.res_mem != NULL) &&
- + (result.res_old_mem != NULL) &&
- + (result.res_mem != result.res_old_mem)) {
- + resource->res_base_mem = result.res_mem;
- +
- + /* Kernel allocated resources.
- + */
- + if (resource->pid == 0) {
- + if (!list_empty(&resource->map_list)) {
- + list_for_each_entry_safe(map, map_tmp,
- + &resource->map_list,
- + resource_map_list) {
- + if (map->res_addr) {
- + iounmap((void *)map->res_addr);
- + map->res_addr = 0;
- +
- + vmcs_sm_remove_map(sm_state,
- + map->resource,
- + map);
- + break;
- + }
- + }
- + }
- + }
- + }
- +
- + if (change_cache)
- + resource->res_cached = cache_type;
- +
- + if (resource->map_count) {
- + ioparam->addr =
- + vmcs_sm_usr_address_from_pid_and_usr_handle(
- + current->tgid, ioparam->handle);
- +
- + pr_debug("[%s] map_count %d private->pid %d current->tgid %d hnd %x addr %u\n",
- + __func__, resource->map_count, private->pid,
- + current->tgid, ioparam->handle, ioparam->addr);
- + } else {
- + /* Kernel allocated resources.
- + */
- + if (resource->pid == 0) {
- + pr_debug("[%s]: attempt mapping kernel resource - guid %x, hdl %x\n",
- + __func__, ioparam->handle, lock.res_handle);
- +
- + ioparam->addr = 0;
- +
- + map = kzalloc(sizeof(*map), GFP_KERNEL);
- + if (map == NULL) {
- + pr_err("[%s]: failed allocating tracker\n",
- + __func__);
- + ret = -ENOMEM;
- + goto error;
- + } else {
- + phys_addr = (uint32_t)resource->res_base_mem &
- + 0x3FFFFFFF;
- + phys_addr += mm_vc_mem_phys_addr;
- + if (resource->res_cached
- + == VMCS_SM_CACHE_HOST) {
- + ioparam->addr = (long unsigned int)
- + /* TODO - make cached work */
- + ioremap_nocache(phys_addr,
- + resource->res_size);
- +
- + pr_debug("[%s]: mapping kernel - guid %x, hdl %x - cached mapping %u\n",
- + __func__, ioparam->handle,
- + lock.res_handle, ioparam->addr);
- + } else {
- + ioparam->addr = (long unsigned int)
- + ioremap_nocache(phys_addr,
- + resource->res_size);
- +
- + pr_debug("[%s]: mapping kernel- guid %x, hdl %x - non cached mapping %u\n",
- + __func__, ioparam->handle,
- + lock.res_handle, ioparam->addr);
- + }
- +
- + map->res_pid = 0;
- + map->res_vc_hdl = resource->res_handle;
- + map->res_usr_hdl = resource->res_guid;
- + map->res_addr = ioparam->addr;
- + map->resource = resource;
- + map->vma = NULL;
- +
- + vmcs_sm_add_map(sm_state, resource, map);
- + }
- + } else
- + ioparam->addr = 0;
- + }
- +
- +error:
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + return ret;
- +}
- +
- +/* Unlock a previously allocated shared memory handle and block.
- +*/
- +static int vc_sm_ioctl_unlock(struct SM_PRIV_DATA_T *private,
- + struct vmcs_sm_ioctl_lock_unlock *ioparam,
- + int flush, int wait_reply, int no_vc_unlock)
- +{
- + int status;
- + VC_SM_LOCK_UNLOCK_T unlock;
- + struct sm_mmap *map, *map_tmp;
- + struct SM_RESOURCE_T *resource;
- + int ret = 0;
- +
- + map = NULL;
- +
- + /* Locate resource from GUID.
- + */
- + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
- + if (resource == NULL) {
- + ret = -EINVAL;
- + goto error;
- + }
- +
- + /* Check permissions.
- + */
- + if (resource->pid && (resource->pid != current->tgid)) {
- + pr_err("[%s]: current tgid %u != %u owner\n",
- + __func__, current->tgid, resource->pid);
- + ret = -EPERM;
- + goto error;
- + }
- +
- + unlock.res_handle = resource->res_handle;
- + unlock.res_mem = resource->res_base_mem;
- +
- + pr_debug("[%s]: attempt to unlock data - guid %x, hdl %x, base address %p\n",
- + __func__, ioparam->handle, unlock.res_handle, unlock.res_mem);
- +
- + /* User space allocated resources.
- + */
- + if (resource->pid) {
- + /* Flush if requested */
- + if (resource->res_cached && flush) {
- + dma_addr_t phys_addr = 0;
- + resource->res_stats[FLUSH]++;
- +
- + phys_addr =
- + (dma_addr_t)((uint32_t)resource->res_base_mem &
- + 0x3FFFFFFF);
- + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
- +
- + /* L1 cache flush */
- + down_read(¤t->mm->mmap_sem);
- + list_for_each_entry(map, &resource->map_list,
- + resource_map_list) {
- + if (map->vma) {
- + unsigned long start;
- + unsigned long end;
- + start = map->vma->vm_start;
- + end = map->vma->vm_end;
- +
- + vcsm_vma_cache_clean_page_range(
- + start, end);
- + }
- + }
- + up_read(¤t->mm->mmap_sem);
- +
- + /* L2 cache flush */
- + outer_clean_range(phys_addr,
- + phys_addr +
- + (size_t) resource->res_size);
- + }
- +
- + /* We need to zap all the vmas associated with this resource */
- + if (resource->lock_count == 1) {
- + down_read(¤t->mm->mmap_sem);
- + list_for_each_entry(map, &resource->map_list,
- + resource_map_list) {
- + if (map->vma) {
- + zap_vma_ptes(map->vma,
- + map->vma->vm_start,
- + map->vma->vm_end -
- + map->vma->vm_start);
- + }
- + }
- + up_read(¤t->mm->mmap_sem);
- + }
- + }
- + /* Kernel allocated resources. */
- + else {
- + /* Global + Taken in this context */
- + if (resource->ref_count == 2) {
- + if (!list_empty(&resource->map_list)) {
- + list_for_each_entry_safe(map, map_tmp,
- + &resource->map_list,
- + resource_map_list) {
- + if (map->res_addr) {
- + if (flush &&
- + (resource->res_cached ==
- + VMCS_SM_CACHE_HOST)) {
- + long unsigned int
- + phys_addr;
- + phys_addr = (uint32_t)
- + resource->res_base_mem & 0x3FFFFFFF;
- + phys_addr +=
- + mm_vc_mem_phys_addr;
- +
- + /* L1 cache flush */
- + dmac_flush_range((const
- + void
- + *)
- + map->res_addr, (const void *)
- + (map->res_addr + resource->res_size));
- +
- + /* L2 cache flush */
- + outer_clean_range
- + (phys_addr,
- + phys_addr +
- + (size_t)
- + resource->res_size);
- + }
- +
- + iounmap((void *)map->res_addr);
- + map->res_addr = 0;
- +
- + vmcs_sm_remove_map(sm_state,
- + map->resource,
- + map);
- + break;
- + }
- + }
- + }
- + }
- + }
- +
- + if (resource->lock_count) {
- + /* Bypass the videocore unlock.
- + */
- + if (no_vc_unlock)
- + status = 0;
- + /* Unlock the videocore allocated resource.
- + */
- + else {
- + status =
- + vc_vchi_sm_unlock(sm_state->sm_handle, &unlock,
- + &private->int_trans_id,
- + wait_reply);
- + if (status == -EINTR) {
- + pr_debug("[%s]: requesting unlock memory action restart (trans_id: %u)\n",
- + __func__, private->int_trans_id);
- +
- + ret = -ERESTARTSYS;
- + resource->res_stats[UNLOCK]--;
- + private->restart_sys = -EINTR;
- + private->int_action = VC_SM_MSG_TYPE_UNLOCK;
- + goto error;
- + } else if (status != 0) {
- + pr_err("[%s]: failed to unlock vc mem (status: %u, trans_id: %u)\n",
- + __func__, status, private->int_trans_id);
- +
- + ret = -EPERM;
- + resource->res_stats[UNLOCK_FAIL]++;
- + goto error;
- + }
- + }
- +
- + resource->res_stats[UNLOCK]++;
- + resource->lock_count--;
- + }
- +
- + pr_debug("[%s]: success to unlock data - hdl %x, base address %p, ref-cnt %d\n",
- + __func__, unlock.res_handle, unlock.res_mem,
- + resource->lock_count);
- +
- +error:
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + return ret;
- +}
- +
- +/* Handle control from host. */
- +static long vc_sm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- +{
- + int ret = 0;
- + unsigned int cmdnr = _IOC_NR(cmd);
- + struct SM_PRIV_DATA_T *file_data =
- + (struct SM_PRIV_DATA_T *)file->private_data;
- + struct SM_RESOURCE_T *resource = NULL;
- +
- + /* Validate we can work with this device. */
- + if ((sm_state == NULL) || (file_data == NULL)) {
- + pr_err("[%s]: invalid device\n", __func__);
- + ret = -EPERM;
- + goto out;
- + }
- +
- + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
- + current->tgid, file_data->pid);
- +
- + /* Action is a re-post of a previously interrupted action? */
- + if (file_data->restart_sys == -EINTR) {
- + VC_SM_ACTION_CLEAN_T action_clean;
- +
- + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
- + __func__, file_data->int_action,
- + file_data->int_trans_id);
- +
- + action_clean.res_action = file_data->int_action;
- + action_clean.action_trans_id = file_data->int_trans_id;
- +
- + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
- +
- + file_data->restart_sys = 0;
- + }
- +
- + /* Now process the command.
- + */
- + switch (cmdnr) {
- + /* New memory allocation.
- + */
- + case VMCS_SM_CMD_ALLOC:
- + {
- + struct vmcs_sm_ioctl_alloc ioparam;
- +
- + /* Get the parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ret = vc_sm_ioctl_alloc(file_data, &ioparam);
- + if (!ret &&
- + (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0)) {
- + struct vmcs_sm_ioctl_free freeparam = {
- + ioparam.handle
- + };
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + vc_sm_ioctl_free(file_data, &freeparam);
- + ret = -EFAULT;
- + }
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Share existing memory allocation.
- + */
- + case VMCS_SM_CMD_ALLOC_SHARE:
- + {
- + struct vmcs_sm_ioctl_alloc_share ioparam;
- +
- + /* Get the parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ret = vc_sm_ioctl_alloc_share(file_data, &ioparam);
- +
- + /* Copy result back to user.
- + */
- + if (!ret
- + && copy_to_user((void *)arg, &ioparam,
- + sizeof(ioparam)) != 0) {
- + struct vmcs_sm_ioctl_free freeparam = {
- + ioparam.handle
- + };
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + vc_sm_ioctl_free(file_data, &freeparam);
- + ret = -EFAULT;
- + }
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Lock (attempt to) *and* register a cache behavior change.
- + */
- + case VMCS_SM_CMD_LOCK_CACHE:
- + {
- + struct vmcs_sm_ioctl_lock_cache ioparam;
- + struct vmcs_sm_ioctl_lock_unlock lock;
- +
- + /* Get parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + lock.handle = ioparam.handle;
- + ret =
- + vc_sm_ioctl_lock(file_data, &lock, 1,
- + ioparam.cached, 0);
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Lock (attempt to) existing memory allocation.
- + */
- + case VMCS_SM_CMD_LOCK:
- + {
- + struct vmcs_sm_ioctl_lock_unlock ioparam;
- +
- + /* Get parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ret = vc_sm_ioctl_lock(file_data, &ioparam, 0, 0, 0);
- +
- + /* Copy result back to user.
- + */
- + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
- + != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Unlock (attempt to) existing memory allocation.
- + */
- + case VMCS_SM_CMD_UNLOCK:
- + {
- + struct vmcs_sm_ioctl_lock_unlock ioparam;
- +
- + /* Get parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ret = vc_sm_ioctl_unlock(file_data, &ioparam, 0, 1, 0);
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Resize (attempt to) existing memory allocation.
- + */
- + case VMCS_SM_CMD_RESIZE:
- + {
- + struct vmcs_sm_ioctl_resize ioparam;
- +
- + /* Get parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ret = vc_sm_ioctl_resize(file_data, &ioparam);
- +
- + /* Copy result back to user.
- + */
- + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
- + != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Terminate existing memory allocation.
- + */
- + case VMCS_SM_CMD_FREE:
- + {
- + struct vmcs_sm_ioctl_free ioparam;
- +
- + /* Get parameter data.
- + */
- + if (copy_from_user
- + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ret = vc_sm_ioctl_free(file_data, &ioparam);
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Walk allocation on videocore, information shows up in the
- + ** videocore log.
- + */
- + case VMCS_SM_CMD_VC_WALK_ALLOC:
- + {
- + pr_debug("[%s]: invoking walk alloc\n", __func__);
- +
- + if (vc_vchi_sm_walk_alloc(sm_state->sm_handle) != 0)
- + pr_err("[%s]: failed to walk-alloc on videocore\n",
- + __func__);
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +/* Walk mapping table on host, information shows up in the
- + ** kernel log.
- + */
- + case VMCS_SM_CMD_HOST_WALK_MAP:
- + {
- + /* Use pid of -1 to tell to walk the whole map. */
- + vmcs_sm_host_walk_map_per_pid(-1);
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Walk mapping table per process on host. */
- + case VMCS_SM_CMD_HOST_WALK_PID_ALLOC:
- + {
- + struct vmcs_sm_ioctl_walk ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + vmcs_sm_host_walk_alloc(file_data);
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Walk allocation per process on host. */
- + case VMCS_SM_CMD_HOST_WALK_PID_MAP:
- + {
- + struct vmcs_sm_ioctl_walk ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + vmcs_sm_host_walk_map_per_pid(ioparam.pid);
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Gets the size of the memory associated with a user handle. */
- + case VMCS_SM_CMD_SIZE_USR_HANDLE:
- + {
- + struct vmcs_sm_ioctl_size ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /* Locate resource from GUID. */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- + if (resource != NULL) {
- + ioparam.size = resource->res_size;
- + vmcs_sm_release_resource(resource, 0);
- + } else {
- + ioparam.size = 0;
- + }
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Verify we are dealing with a valid resource. */
- + case VMCS_SM_CMD_CHK_USR_HANDLE:
- + {
- + struct vmcs_sm_ioctl_chk ioparam;
- +
- + /* Get parameter data.
- + */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- +
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /* Locate resource from GUID. */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- + if (resource == NULL)
- + ret = -EINVAL;
- + /* If the resource is cacheable, return additional
- + * information that may be needed to flush the cache.
- + */
- + else if ((resource->res_cached == VMCS_SM_CACHE_HOST) ||
- + (resource->res_cached == VMCS_SM_CACHE_BOTH)) {
- + ioparam.addr =
- + vmcs_sm_usr_address_from_pid_and_usr_handle
- + (current->tgid, ioparam.handle);
- + ioparam.size = resource->res_size;
- + ioparam.cache = resource->res_cached;
- + } else {
- + ioparam.addr = 0;
- + ioparam.size = 0;
- + ioparam.cache = resource->res_cached;
- + }
- +
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /*
- + * Maps a user handle given the process and the virtual address.
- + */
- + case VMCS_SM_CMD_MAPPED_USR_HANDLE:
- + {
- + struct vmcs_sm_ioctl_map ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- +
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ioparam.handle =
- + vmcs_sm_usr_handle_from_pid_and_address(
- + ioparam.pid, ioparam.addr);
- +
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- + if ((resource != NULL)
- + && ((resource->res_cached == VMCS_SM_CACHE_HOST)
- + || (resource->res_cached ==
- + VMCS_SM_CACHE_BOTH))) {
- + ioparam.size = resource->res_size;
- + } else {
- + ioparam.size = 0;
- + }
- +
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /*
- + * Maps a videocore handle given process and virtual address.
- + */
- + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR:
- + {
- + struct vmcs_sm_ioctl_map ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + ioparam.handle = vmcs_sm_vc_handle_from_pid_and_address(
- + ioparam.pid, ioparam.addr);
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- +
- + ret = -EFAULT;
- + }
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Maps a videocore handle given process and user handle. */
- + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL:
- + {
- + struct vmcs_sm_ioctl_map ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /* Locate resource from GUID. */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- + if (resource != NULL) {
- + ioparam.handle = resource->res_handle;
- + vmcs_sm_release_resource(resource, 0);
- + } else {
- + ioparam.handle = 0;
- + }
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- +
- + ret = -EFAULT;
- + }
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /*
- + * Maps a videocore address given process and videocore handle.
- + */
- + case VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL:
- + {
- + struct vmcs_sm_ioctl_map ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- +
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /* Locate resource from GUID. */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- + if (resource != NULL) {
- + ioparam.addr =
- + (unsigned int)resource->res_base_mem;
- + vmcs_sm_release_resource(resource, 0);
- + } else {
- + ioparam.addr = 0;
- + }
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Maps a user address given process and vc handle.
- + */
- + case VMCS_SM_CMD_MAPPED_USR_ADDRESS:
- + {
- + struct vmcs_sm_ioctl_map ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /*
- + * Return the address information from the mapping,
- + * 0 (ie NULL) if it cannot locate the actual mapping.
- + */
- + ioparam.addr =
- + vmcs_sm_usr_address_from_pid_and_usr_handle
- + (ioparam.pid, ioparam.handle);
- +
- + if (copy_to_user((void *)arg,
- + &ioparam, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + }
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Flush the cache for a given mapping. */
- + case VMCS_SM_CMD_FLUSH:
- + {
- + struct vmcs_sm_ioctl_cache ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /* Locate resource from GUID. */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- +
- + if ((resource != NULL) && resource->res_cached) {
- + dma_addr_t phys_addr = 0;
- +
- + resource->res_stats[FLUSH]++;
- +
- + phys_addr =
- + (dma_addr_t)((uint32_t)
- + resource->res_base_mem &
- + 0x3FFFFFFF);
- + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
- +
- + /* L1 cache flush */
- + down_read(¤t->mm->mmap_sem);
- + vcsm_vma_cache_clean_page_range((unsigned long)
- + ioparam.addr,
- + (unsigned long)
- + ioparam.addr +
- + ioparam.size);
- + up_read(¤t->mm->mmap_sem);
- +
- + /* L2 cache flush */
- + outer_clean_range(phys_addr,
- + phys_addr +
- + (size_t) ioparam.size);
- + } else if (resource == NULL) {
- + ret = -EINVAL;
- + goto out;
- + }
- +
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + /* Done. */
- + goto out;
- + }
- + break;
- +
- + /* Invalidate the cache for a given mapping. */
- + case VMCS_SM_CMD_INVALID:
- + {
- + struct vmcs_sm_ioctl_cache ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- +
- + /* Locate resource from GUID.
- + */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.handle);
- +
- + if ((resource != NULL) && resource->res_cached) {
- + dma_addr_t phys_addr = 0;
- +
- + resource->res_stats[INVALID]++;
- +
- + phys_addr =
- + (dma_addr_t)((uint32_t)
- + resource->res_base_mem &
- + 0x3FFFFFFF);
- + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
- +
- + /* L2 cache invalidate */
- + outer_inv_range(phys_addr,
- + phys_addr +
- + (size_t) ioparam.size);
- +
- + /* L1 cache invalidate */
- + down_read(¤t->mm->mmap_sem);
- + vcsm_vma_cache_clean_page_range((unsigned long)
- + ioparam.addr,
- + (unsigned long)
- + ioparam.addr +
- + ioparam.size);
- + up_read(¤t->mm->mmap_sem);
- + } else if (resource == NULL) {
- + ret = -EINVAL;
- + goto out;
- + }
- +
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- +
- + /* Done.
- + */
- + goto out;
- + }
- + break;
- +
- + /* Flush/Invalidate the cache for a given mapping. */
- + case VMCS_SM_CMD_CLEAN_INVALID:
- + {
- + int i;
- + struct vmcs_sm_ioctl_clean_invalid ioparam;
- +
- + /* Get parameter data. */
- + if (copy_from_user(&ioparam,
- + (void *)arg, sizeof(ioparam)) != 0) {
- + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
- + __func__, cmdnr);
- + ret = -EFAULT;
- + goto out;
- + }
- + for (i=0; i<sizeof ioparam.s/sizeof *ioparam.s; i++) {
- + switch (ioparam.s[i].cmd) {
- + default: case 0: break; /* NOOP */
- + case 1: /* L1/L2 invalidate virtual range */
- + case 2: /* L1/L2 clean physical range */
- + case 3: /* L1/L2 clean+invalidate all */
- + {
- + /* Locate resource from GUID.
- + */
- + resource =
- + vmcs_sm_acquire_resource(file_data, ioparam.s[i].handle);
- +
- + if ((resource != NULL) && resource->res_cached) {
- + unsigned long base = ioparam.s[i].addr & ~(PAGE_SIZE-1);
- + unsigned long end = (ioparam.s[i].addr + ioparam.s[i].size + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
- + resource->res_stats[ioparam.s[i].cmd == 1 ? INVALID:FLUSH]++;
- +
- + /* L1/L2 cache flush */
- + down_read(¤t->mm->mmap_sem);
- + vcsm_vma_cache_clean_page_range(base, end);
- + up_read(¤t->mm->mmap_sem);
- + } else if (resource == NULL) {
- + ret = -EINVAL;
- + goto out;
- + }
- +
- + if (resource)
- + vmcs_sm_release_resource(resource, 0);
- + }
- + break;
- + }
- + }
- + }
- + break;
- +
- + default:
- + {
- + ret = -EINVAL;
- + goto out;
- + }
- + break;
- + }
- +
- +out:
- + return ret;
- +}
- +
- +/* Device operations that we managed in this driver.
- +*/
- +static const struct file_operations vmcs_sm_ops = {
- + .owner = THIS_MODULE,
- + .unlocked_ioctl = vc_sm_ioctl,
- + .open = vc_sm_open,
- + .release = vc_sm_release,
- + .mmap = vc_sm_mmap,
- +};
- +
- +/* Creation of device.
- +*/
- +static int vc_sm_create_sharedmemory(void)
- +{
- + int ret;
- +
- + if (sm_state == NULL) {
- + ret = -ENOMEM;
- + goto out;
- + }
- +
- + /* Create a device class for creating dev nodes.
- + */
- + sm_state->sm_class = class_create(THIS_MODULE, "vc-sm");
- + if (IS_ERR(sm_state->sm_class)) {
- + pr_err("[%s]: unable to create device class\n", __func__);
- + ret = PTR_ERR(sm_state->sm_class);
- + goto out;
- + }
- +
- + /* Create a character driver.
- + */
- + ret = alloc_chrdev_region(&sm_state->sm_devid,
- + DEVICE_MINOR, 1, DEVICE_NAME);
- + if (ret != 0) {
- + pr_err("[%s]: unable to allocate device number\n", __func__);
- + goto out_dev_class_destroy;
- + }
- +
- + cdev_init(&sm_state->sm_cdev, &vmcs_sm_ops);
- + ret = cdev_add(&sm_state->sm_cdev, sm_state->sm_devid, 1);
- + if (ret != 0) {
- + pr_err("[%s]: unable to register device\n", __func__);
- + goto out_chrdev_unreg;
- + }
- +
- + /* Create a device node.
- + */
- + sm_state->sm_dev = device_create(sm_state->sm_class,
- + NULL,
- + MKDEV(MAJOR(sm_state->sm_devid),
- + DEVICE_MINOR), NULL,
- + DEVICE_NAME);
- + if (IS_ERR(sm_state->sm_dev)) {
- + pr_err("[%s]: unable to create device node\n", __func__);
- + ret = PTR_ERR(sm_state->sm_dev);
- + goto out_chrdev_del;
- + }
- +
- + goto out;
- +
- +out_chrdev_del:
- + cdev_del(&sm_state->sm_cdev);
- +out_chrdev_unreg:
- + unregister_chrdev_region(sm_state->sm_devid, 1);
- +out_dev_class_destroy:
- + class_destroy(sm_state->sm_class);
- + sm_state->sm_class = NULL;
- +out:
- + return ret;
- +}
- +
- +/* Termination of the device.
- +*/
- +static int vc_sm_remove_sharedmemory(void)
- +{
- + int ret;
- +
- + if (sm_state == NULL) {
- + /* Nothing to do.
- + */
- + ret = 0;
- + goto out;
- + }
- +
- + /* Remove the sharedmemory character driver.
- + */
- + cdev_del(&sm_state->sm_cdev);
- +
- + /* Unregister region.
- + */
- + unregister_chrdev_region(sm_state->sm_devid, 1);
- +
- + ret = 0;
- + goto out;
- +
- +out:
- + return ret;
- +}
- +
- +/* Videocore connected. */
- +static void vc_sm_connected_init(void)
- +{
- + int ret;
- + VCHI_INSTANCE_T vchi_instance;
- + VCHI_CONNECTION_T *vchi_connection = NULL;
- +
- + pr_info("[%s]: start\n", __func__);
- +
- + /* Allocate memory for the state structure.
- + */
- + sm_state = kzalloc(sizeof(struct SM_STATE_T), GFP_KERNEL);
- + if (sm_state == NULL) {
- + pr_err("[%s]: failed to allocate memory\n", __func__);
- + ret = -ENOMEM;
- + goto out;
- + }
- +
- + mutex_init(&sm_state->lock);
- + mutex_init(&sm_state->map_lock);
- +
- + /* Initialize and create a VCHI connection for the shared memory service
- + ** running on videocore.
- + */
- + ret = vchi_initialise(&vchi_instance);
- + if (ret != 0) {
- + pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
- + __func__, ret);
- +
- + ret = -EIO;
- + goto err_free_mem;
- + }
- +
- + ret = vchi_connect(NULL, 0, vchi_instance);
- + if (ret != 0) {
- + pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
- + __func__, ret);
- +
- + ret = -EIO;
- + goto err_free_mem;
- + }
- +
- + /* Initialize an instance of the shared memory service. */
- + sm_state->sm_handle =
- + vc_vchi_sm_init(vchi_instance, &vchi_connection, 1);
- + if (sm_state->sm_handle == NULL) {
- + pr_err("[%s]: failed to initialize shared memory service\n",
- + __func__);
- +
- + ret = -EPERM;
- + goto err_free_mem;
- + }
- +
- + /* Create a debug fs directory entry (root). */
- + sm_state->dir_root = debugfs_create_dir(VC_SM_DIR_ROOT_NAME, NULL);
- + if (!sm_state->dir_root) {
- + pr_err("[%s]: failed to create \'%s\' directory entry\n",
- + __func__, VC_SM_DIR_ROOT_NAME);
- +
- + ret = -EPERM;
- + goto err_stop_sm_service;
- + }
- +
- + sm_state->dir_state.show = &vc_sm_global_state_show;
- + sm_state->dir_state.dir_entry = debugfs_create_file(VC_SM_STATE,
- + S_IRUGO, sm_state->dir_root, &sm_state->dir_state,
- + &vc_sm_debug_fs_fops);
- +
- + sm_state->dir_stats.show = &vc_sm_global_statistics_show;
- + sm_state->dir_stats.dir_entry = debugfs_create_file(VC_SM_STATS,
- + S_IRUGO, sm_state->dir_root, &sm_state->dir_stats,
- + &vc_sm_debug_fs_fops);
- +
- + /* Create the proc entry children. */
- + sm_state->dir_alloc = debugfs_create_dir(VC_SM_DIR_ALLOC_NAME,
- + sm_state->dir_root);
- +
- + /* Create a shared memory device. */
- + ret = vc_sm_create_sharedmemory();
- + if (ret != 0) {
- + pr_err("[%s]: failed to create shared memory device\n",
- + __func__);
- + goto err_remove_debugfs;
- + }
- +
- + INIT_LIST_HEAD(&sm_state->map_list);
- + INIT_LIST_HEAD(&sm_state->resource_list);
- +
- + sm_state->data_knl = vc_sm_create_priv_data(0);
- + if (sm_state->data_knl == NULL) {
- + pr_err("[%s]: failed to create kernel private data tracker\n",
- + __func__);
- + goto err_remove_shared_memory;
- + }
- +
- + /* Done!
- + */
- + sm_inited = 1;
- + goto out;
- +
- +err_remove_shared_memory:
- + vc_sm_remove_sharedmemory();
- +err_remove_debugfs:
- + debugfs_remove_recursive(sm_state->dir_root);
- +err_stop_sm_service:
- + vc_vchi_sm_stop(&sm_state->sm_handle);
- +err_free_mem:
- + kfree(sm_state);
- +out:
- + pr_info("[%s]: end - returning %d\n", __func__, ret);
- +}
- +
- +/* Driver loading. */
- +static int __init vc_sm_init(void)
- +{
- + pr_info("vc-sm: Videocore shared memory driver\n");
- + vchiq_add_connected_callback(vc_sm_connected_init);
- + return 0;
- +}
- +
- +/* Driver unloading. */
- +static void __exit vc_sm_exit(void)
- +{
- + pr_debug("[%s]: start\n", __func__);
- + if (sm_inited) {
- + /* Remove shared memory device.
- + */
- + vc_sm_remove_sharedmemory();
- +
- + /* Remove all proc entries.
- + */
- + debugfs_remove_recursive(sm_state->dir_root);
- +
- + /* Stop the videocore shared memory service.
- + */
- + vc_vchi_sm_stop(&sm_state->sm_handle);
- +
- + /* Free the memory for the state structure.
- + */
- + mutex_destroy(&(sm_state->map_lock));
- + kfree(sm_state);
- + }
- +
- + pr_debug("[%s]: end\n", __func__);
- +}
- +
- +#if defined(__KERNEL__)
- +/* Allocate a shared memory handle and block. */
- +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle)
- +{
- + struct vmcs_sm_ioctl_alloc ioparam = { 0 };
- + int ret;
- + struct SM_RESOURCE_T *resource;
- +
- + /* Validate we can work with this device.
- + */
- + if (sm_state == NULL || alloc == NULL || handle == NULL) {
- + pr_err("[%s]: invalid input\n", __func__);
- + return -EPERM;
- + }
- +
- + ioparam.size = alloc->base_unit;
- + ioparam.num = alloc->num_unit;
- + ioparam.cached =
- + alloc->type == VC_SM_ALLOC_CACHED ? VMCS_SM_CACHE_VC : 0;
- +
- + ret = vc_sm_ioctl_alloc(sm_state->data_knl, &ioparam);
- +
- + if (ret == 0) {
- + resource =
- + vmcs_sm_acquire_resource(sm_state->data_knl,
- + ioparam.handle);
- + if (resource) {
- + resource->pid = 0;
- + vmcs_sm_release_resource(resource, 0);
- +
- + /* Assign valid handle at this time.
- + */
- + *handle = ioparam.handle;
- + } else {
- + ret = -ENOMEM;
- + }
- + }
- +
- + return ret;
- +}
- +EXPORT_SYMBOL_GPL(vc_sm_alloc);
- +
- +/* Get an internal resource handle mapped from the external one.
- +*/
- +int vc_sm_int_handle(int handle)
- +{
- + struct SM_RESOURCE_T *resource;
- + int ret = 0;
- +
- + /* Validate we can work with this device.
- + */
- + if (sm_state == NULL || handle == 0) {
- + pr_err("[%s]: invalid input\n", __func__);
- + return 0;
- + }
- +
- + /* Locate resource from GUID.
- + */
- + resource = vmcs_sm_acquire_resource(sm_state->data_knl, handle);
- + if (resource) {
- + ret = resource->res_handle;
- + vmcs_sm_release_resource(resource, 0);
- + }
- +
- + return ret;
- +}
- +EXPORT_SYMBOL_GPL(vc_sm_int_handle);
- +
- +/* Free a previously allocated shared memory handle and block.
- +*/
- +int vc_sm_free(int handle)
- +{
- + struct vmcs_sm_ioctl_free ioparam = { handle };
- +
- + /* Validate we can work with this device.
- + */
- + if (sm_state == NULL || handle == 0) {
- + pr_err("[%s]: invalid input\n", __func__);
- + return -EPERM;
- + }
- +
- + return vc_sm_ioctl_free(sm_state->data_knl, &ioparam);
- +}
- +EXPORT_SYMBOL_GPL(vc_sm_free);
- +
- +/* Lock a memory handle for use by kernel.
- +*/
- +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
- + long unsigned int *data)
- +{
- + struct vmcs_sm_ioctl_lock_unlock ioparam;
- + int ret;
- +
- + /* Validate we can work with this device.
- + */
- + if (sm_state == NULL || handle == 0 || data == NULL) {
- + pr_err("[%s]: invalid input\n", __func__);
- + return -EPERM;
- + }
- +
- + *data = 0;
- +
- + ioparam.handle = handle;
- + ret = vc_sm_ioctl_lock(sm_state->data_knl,
- + &ioparam,
- + 1,
- + ((mode ==
- + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
- + VMCS_SM_CACHE_NONE), 0);
- +
- + *data = ioparam.addr;
- + return ret;
- +}
- +EXPORT_SYMBOL_GPL(vc_sm_lock);
- +
- +/* Unlock a memory handle in use by kernel.
- +*/
- +int vc_sm_unlock(int handle, int flush, int no_vc_unlock)
- +{
- + struct vmcs_sm_ioctl_lock_unlock ioparam;
- +
- + /* Validate we can work with this device.
- + */
- + if (sm_state == NULL || handle == 0) {
- + pr_err("[%s]: invalid input\n", __func__);
- + return -EPERM;
- + }
- +
- + ioparam.handle = handle;
- + return vc_sm_ioctl_unlock(sm_state->data_knl,
- + &ioparam, flush, 0, no_vc_unlock);
- +}
- +EXPORT_SYMBOL_GPL(vc_sm_unlock);
- +
- +/* Map a shared memory region for use by kernel.
- +*/
- +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
- + long unsigned int *data)
- +{
- + struct vmcs_sm_ioctl_lock_unlock ioparam;
- + int ret;
- +
- + /* Validate we can work with this device.
- + */
- + if (sm_state == NULL || handle == 0 || data == NULL || sm_addr == 0) {
- + pr_err("[%s]: invalid input\n", __func__);
- + return -EPERM;
- + }
- +
- + *data = 0;
- +
- + ioparam.handle = handle;
- + ret = vc_sm_ioctl_lock(sm_state->data_knl,
- + &ioparam,
- + 1,
- + ((mode ==
- + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
- + VMCS_SM_CACHE_NONE), sm_addr);
- +
- + *data = ioparam.addr;
- + return ret;
- +}
- +EXPORT_SYMBOL_GPL(vc_sm_map);
- +#endif
- +
- +late_initcall(vc_sm_init);
- +module_exit(vc_sm_exit);
- +
- +MODULE_AUTHOR("Broadcom");
- +MODULE_DESCRIPTION("VideoCore SharedMemory Driver");
- +MODULE_LICENSE("GPL v2");
- --- /dev/null
- +++ b/include/linux/broadcom/vmcs_sm_ioctl.h
- @@ -0,0 +1,248 @@
- +/*****************************************************************************
- +* Copyright 2011 Broadcom Corporation. All rights reserved.
- +*
- +* Unless you and Broadcom execute a separate written software license
- +* agreement governing use of this software, this software is licensed to you
- +* under the terms of the GNU General Public License version 2, available at
- +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
- +*
- +* Notwithstanding the above, under no circumstances may you combine this
- +* software in any way with any other Broadcom software provided under a
- +* license other than the GPL, without Broadcom's express prior written
- +* consent.
- +*
- +*****************************************************************************/
- +
- +#if !defined(__VMCS_SM_IOCTL_H__INCLUDED__)
- +#define __VMCS_SM_IOCTL_H__INCLUDED__
- +
- +/* ---- Include Files ---------------------------------------------------- */
- +
- +#if defined(__KERNEL__)
- +#include <linux/types.h> /* Needed for standard types */
- +#else
- +#include <stdint.h>
- +#endif
- +
- +#include <linux/ioctl.h>
- +
- +/* ---- Constants and Types ---------------------------------------------- */
- +
- +#define VMCS_SM_RESOURCE_NAME 32
- +#define VMCS_SM_RESOURCE_NAME_DEFAULT "sm-host-resource"
- +
- +/* Type define used to create unique IOCTL number */
- +#define VMCS_SM_MAGIC_TYPE 'I'
- +
- +/* IOCTL commands */
- +enum vmcs_sm_cmd_e {
- + VMCS_SM_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
- + VMCS_SM_CMD_ALLOC_SHARE,
- + VMCS_SM_CMD_LOCK,
- + VMCS_SM_CMD_LOCK_CACHE,
- + VMCS_SM_CMD_UNLOCK,
- + VMCS_SM_CMD_RESIZE,
- + VMCS_SM_CMD_UNMAP,
- + VMCS_SM_CMD_FREE,
- + VMCS_SM_CMD_FLUSH,
- + VMCS_SM_CMD_INVALID,
- +
- + VMCS_SM_CMD_SIZE_USR_HANDLE,
- + VMCS_SM_CMD_CHK_USR_HANDLE,
- +
- + VMCS_SM_CMD_MAPPED_USR_HANDLE,
- + VMCS_SM_CMD_MAPPED_USR_ADDRESS,
- + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,
- + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,
- + VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,
- +
- + VMCS_SM_CMD_VC_WALK_ALLOC,
- + VMCS_SM_CMD_HOST_WALK_MAP,
- + VMCS_SM_CMD_HOST_WALK_PID_ALLOC,
- + VMCS_SM_CMD_HOST_WALK_PID_MAP,
- +
- + VMCS_SM_CMD_CLEAN_INVALID,
- +
- + VMCS_SM_CMD_LAST /* Do no delete */
- +};
- +
- +/* Cache type supported, conveniently matches the user space definition in
- +** user-vcsm.h.
- +*/
- +enum vmcs_sm_cache_e {
- + VMCS_SM_CACHE_NONE,
- + VMCS_SM_CACHE_HOST,
- + VMCS_SM_CACHE_VC,
- + VMCS_SM_CACHE_BOTH,
- +};
- +
- +/* IOCTL Data structures */
- +struct vmcs_sm_ioctl_alloc {
- + /* user -> kernel */
- + unsigned int size;
- + unsigned int num;
- + enum vmcs_sm_cache_e cached;
- + char name[VMCS_SM_RESOURCE_NAME];
- +
- + /* kernel -> user */
- + unsigned int handle;
- + /* unsigned int base_addr; */
- +};
- +
- +struct vmcs_sm_ioctl_alloc_share {
- + /* user -> kernel */
- + unsigned int handle;
- + unsigned int size;
- +};
- +
- +struct vmcs_sm_ioctl_free {
- + /* user -> kernel */
- + unsigned int handle;
- + /* unsigned int base_addr; */
- +};
- +
- +struct vmcs_sm_ioctl_lock_unlock {
- + /* user -> kernel */
- + unsigned int handle;
- +
- + /* kernel -> user */
- + unsigned int addr;
- +};
- +
- +struct vmcs_sm_ioctl_lock_cache {
- + /* user -> kernel */
- + unsigned int handle;
- + enum vmcs_sm_cache_e cached;
- +};
- +
- +struct vmcs_sm_ioctl_resize {
- + /* user -> kernel */
- + unsigned int handle;
- + unsigned int new_size;
- +
- + /* kernel -> user */
- + unsigned int old_size;
- +};
- +
- +struct vmcs_sm_ioctl_map {
- + /* user -> kernel */
- + /* and kernel -> user */
- + unsigned int pid;
- + unsigned int handle;
- + unsigned int addr;
- +
- + /* kernel -> user */
- + unsigned int size;
- +};
- +
- +struct vmcs_sm_ioctl_walk {
- + /* user -> kernel */
- + unsigned int pid;
- +};
- +
- +struct vmcs_sm_ioctl_chk {
- + /* user -> kernel */
- + unsigned int handle;
- +
- + /* kernel -> user */
- + unsigned int addr;
- + unsigned int size;
- + enum vmcs_sm_cache_e cache;
- +};
- +
- +struct vmcs_sm_ioctl_size {
- + /* user -> kernel */
- + unsigned int handle;
- +
- + /* kernel -> user */
- + unsigned int size;
- +};
- +
- +struct vmcs_sm_ioctl_cache {
- + /* user -> kernel */
- + unsigned int handle;
- + unsigned int addr;
- + unsigned int size;
- +};
- +
- +struct vmcs_sm_ioctl_clean_invalid {
- + /* user -> kernel */
- + struct {
- + unsigned int cmd;
- + unsigned int handle;
- + unsigned int addr;
- + unsigned int size;
- + } s[8];
- +};
- +
- +/* IOCTL numbers */
- +#define VMCS_SM_IOCTL_MEM_ALLOC\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC,\
- + struct vmcs_sm_ioctl_alloc)
- +#define VMCS_SM_IOCTL_MEM_ALLOC_SHARE\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC_SHARE,\
- + struct vmcs_sm_ioctl_alloc_share)
- +#define VMCS_SM_IOCTL_MEM_LOCK\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK,\
- + struct vmcs_sm_ioctl_lock_unlock)
- +#define VMCS_SM_IOCTL_MEM_LOCK_CACHE\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK_CACHE,\
- + struct vmcs_sm_ioctl_lock_cache)
- +#define VMCS_SM_IOCTL_MEM_UNLOCK\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_UNLOCK,\
- + struct vmcs_sm_ioctl_lock_unlock)
- +#define VMCS_SM_IOCTL_MEM_RESIZE\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_RESIZE,\
- + struct vmcs_sm_ioctl_resize)
- +#define VMCS_SM_IOCTL_MEM_FREE\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FREE,\
- + struct vmcs_sm_ioctl_free)
- +#define VMCS_SM_IOCTL_MEM_FLUSH\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FLUSH,\
- + struct vmcs_sm_ioctl_cache)
- +#define VMCS_SM_IOCTL_MEM_INVALID\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_INVALID,\
- + struct vmcs_sm_ioctl_cache)
- +#define VMCS_SM_IOCTL_MEM_CLEAN_INVALID\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_CLEAN_INVALID,\
- + struct vmcs_sm_ioctl_clean_invalid)
- +
- +#define VMCS_SM_IOCTL_SIZE_USR_HDL\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_SIZE_USR_HANDLE,\
- + struct vmcs_sm_ioctl_size)
- +#define VMCS_SM_IOCTL_CHK_USR_HDL\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_CHK_USR_HANDLE,\
- + struct vmcs_sm_ioctl_chk)
- +
- +#define VMCS_SM_IOCTL_MAP_USR_HDL\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_HANDLE,\
- + struct vmcs_sm_ioctl_map)
- +#define VMCS_SM_IOCTL_MAP_USR_ADDRESS\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_ADDRESS,\
- + struct vmcs_sm_ioctl_map)
- +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_ADDR\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,\
- + struct vmcs_sm_ioctl_map)
- +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_HDL\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,\
- + struct vmcs_sm_ioctl_map)
- +#define VMCS_SM_IOCTL_MAP_VC_ADDR_FR_HDL\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,\
- + struct vmcs_sm_ioctl_map)
- +
- +#define VMCS_SM_IOCTL_VC_WALK_ALLOC\
- + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_VC_WALK_ALLOC)
- +#define VMCS_SM_IOCTL_HOST_WALK_MAP\
- + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_MAP)
- +#define VMCS_SM_IOCTL_HOST_WALK_PID_ALLOC\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_ALLOC,\
- + struct vmcs_sm_ioctl_walk)
- +#define VMCS_SM_IOCTL_HOST_WALK_PID_MAP\
- + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_MAP,\
- + struct vmcs_sm_ioctl_walk)
- +
- +/* ---- Variable Externs ------------------------------------------------- */
- +
- +/* ---- Function Prototypes ---------------------------------------------- */
- +
- +#endif /* __VMCS_SM_IOCTL_H__INCLUDED__ */
|