0090-drm-vc4-Add-suport-for-3D-rendering-using-the-V3D-en.patch 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557
  1. From 5009df0a7714100a74d455893485ea9a8dd8a48d Mon Sep 17 00:00:00 2001
  2. From: Eric Anholt <eric@anholt.net>
  3. Date: Mon, 2 Mar 2015 13:01:12 -0800
  4. Subject: [PATCH] drm/vc4: Add suport for 3D rendering using the V3D engine.
  5. This is a squash of the out-of-tree development series. Since that
  6. series contained code from the first "get a demo triangle rendered
  7. using a hacked up driver using binary shader code" to "plug the last
  8. known security hole", it's hard to reconstruct a different series of
  9. incremental development that's mergeable without security holes
  10. throughout it.
  11. Signed-off-by: Eric Anholt <eric@anholt.net>
  12. ---
  13. drivers/gpu/drm/vc4/Makefile | 11 +-
  14. drivers/gpu/drm/vc4/vc4_bo.c | 476 +++++++++++++-
  15. drivers/gpu/drm/vc4/vc4_crtc.c | 98 ++-
  16. drivers/gpu/drm/vc4/vc4_debugfs.c | 3 +
  17. drivers/gpu/drm/vc4/vc4_drv.c | 45 +-
  18. drivers/gpu/drm/vc4/vc4_drv.h | 317 ++++++++++
  19. drivers/gpu/drm/vc4/vc4_gem.c | 686 +++++++++++++++++++++
  20. drivers/gpu/drm/vc4/vc4_irq.c | 211 +++++++
  21. drivers/gpu/drm/vc4/vc4_kms.c | 148 ++++-
  22. drivers/gpu/drm/vc4/vc4_packet.h | 384 ++++++++++++
  23. drivers/gpu/drm/vc4/vc4_plane.c | 40 ++
  24. drivers/gpu/drm/vc4/vc4_qpu_defines.h | 268 ++++++++
  25. drivers/gpu/drm/vc4/vc4_render_cl.c | 448 ++++++++++++++
  26. drivers/gpu/drm/vc4/vc4_trace.h | 63 ++
  27. drivers/gpu/drm/vc4/vc4_trace_points.c | 14 +
  28. drivers/gpu/drm/vc4/vc4_v3d.c | 268 ++++++++
  29. drivers/gpu/drm/vc4/vc4_validate.c | 958 +++++++++++++++++++++++++++++
  30. drivers/gpu/drm/vc4/vc4_validate_shaders.c | 521 ++++++++++++++++
  31. include/uapi/drm/vc4_drm.h | 229 +++++++
  32. 19 files changed, 5173 insertions(+), 15 deletions(-)
  33. create mode 100644 drivers/gpu/drm/vc4/vc4_gem.c
  34. create mode 100644 drivers/gpu/drm/vc4/vc4_irq.c
  35. create mode 100644 drivers/gpu/drm/vc4/vc4_packet.h
  36. create mode 100644 drivers/gpu/drm/vc4/vc4_qpu_defines.h
  37. create mode 100644 drivers/gpu/drm/vc4/vc4_render_cl.c
  38. create mode 100644 drivers/gpu/drm/vc4/vc4_trace.h
  39. create mode 100644 drivers/gpu/drm/vc4/vc4_trace_points.c
  40. create mode 100644 drivers/gpu/drm/vc4/vc4_v3d.c
  41. create mode 100644 drivers/gpu/drm/vc4/vc4_validate.c
  42. create mode 100644 drivers/gpu/drm/vc4/vc4_validate_shaders.c
  43. create mode 100644 include/uapi/drm/vc4_drm.h
  44. --- a/drivers/gpu/drm/vc4/Makefile
  45. +++ b/drivers/gpu/drm/vc4/Makefile
  46. @@ -8,10 +8,19 @@ vc4-y := \
  47. vc4_crtc.o \
  48. vc4_drv.o \
  49. vc4_kms.o \
  50. + vc4_gem.o \
  51. vc4_hdmi.o \
  52. vc4_hvs.o \
  53. - vc4_plane.o
  54. + vc4_irq.o \
  55. + vc4_plane.o \
  56. + vc4_render_cl.o \
  57. + vc4_trace_points.o \
  58. + vc4_v3d.o \
  59. + vc4_validate.o \
  60. + vc4_validate_shaders.o
  61. vc4-$(CONFIG_DEBUG_FS) += vc4_debugfs.o
  62. obj-$(CONFIG_DRM_VC4) += vc4.o
  63. +
  64. +CFLAGS_vc4_trace_points.o := -I$(src)
  65. --- a/drivers/gpu/drm/vc4/vc4_bo.c
  66. +++ b/drivers/gpu/drm/vc4/vc4_bo.c
  67. @@ -15,16 +15,174 @@
  68. */
  69. #include "vc4_drv.h"
  70. +#include "uapi/drm/vc4_drm.h"
  71. -struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size)
  72. +static void vc4_bo_stats_dump(struct vc4_dev *vc4)
  73. {
  74. + DRM_INFO("num bos allocated: %d\n",
  75. + vc4->bo_stats.num_allocated);
  76. + DRM_INFO("size bos allocated: %dkb\n",
  77. + vc4->bo_stats.size_allocated / 1024);
  78. + DRM_INFO("num bos used: %d\n",
  79. + vc4->bo_stats.num_allocated - vc4->bo_stats.num_cached);
  80. + DRM_INFO("size bos used: %dkb\n",
  81. + (vc4->bo_stats.size_allocated -
  82. + vc4->bo_stats.size_cached) / 1024);
  83. + DRM_INFO("num bos cached: %d\n",
  84. + vc4->bo_stats.num_cached);
  85. + DRM_INFO("size bos cached: %dkb\n",
  86. + vc4->bo_stats.size_cached / 1024);
  87. +}
  88. +
  89. +static uint32_t bo_page_index(size_t size)
  90. +{
  91. + return (size / PAGE_SIZE) - 1;
  92. +}
  93. +
  94. +/* Must be called with bo_lock held. */
  95. +static void vc4_bo_destroy(struct vc4_bo *bo)
  96. +{
  97. + struct drm_gem_object *obj = &bo->base.base;
  98. + struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
  99. +
  100. + if (bo->validated_shader) {
  101. + kfree(bo->validated_shader->texture_samples);
  102. + kfree(bo->validated_shader);
  103. + bo->validated_shader = NULL;
  104. + }
  105. +
  106. + vc4->bo_stats.num_allocated--;
  107. + vc4->bo_stats.size_allocated -= obj->size;
  108. + drm_gem_cma_free_object(obj);
  109. +}
  110. +
  111. +/* Must be called with bo_lock held. */
  112. +static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
  113. +{
  114. + struct drm_gem_object *obj = &bo->base.base;
  115. + struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
  116. +
  117. + vc4->bo_stats.num_cached--;
  118. + vc4->bo_stats.size_cached -= obj->size;
  119. +
  120. + list_del(&bo->unref_head);
  121. + list_del(&bo->size_head);
  122. +}
  123. +
  124. +static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
  125. + size_t size)
  126. +{
  127. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  128. + uint32_t page_index = bo_page_index(size);
  129. +
  130. + if (vc4->bo_cache.size_list_size <= page_index) {
  131. + uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
  132. + page_index + 1);
  133. + struct list_head *new_list;
  134. + uint32_t i;
  135. +
  136. + new_list = kmalloc(new_size * sizeof(struct list_head),
  137. + GFP_KERNEL);
  138. + if (!new_list)
  139. + return NULL;
  140. +
  141. + /* Rebase the old cached BO lists to their new list
  142. + * head locations.
  143. + */
  144. + for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
  145. + struct list_head *old_list = &vc4->bo_cache.size_list[i];
  146. + if (list_empty(old_list))
  147. + INIT_LIST_HEAD(&new_list[i]);
  148. + else
  149. + list_replace(old_list, &new_list[i]);
  150. + }
  151. + /* And initialize the brand new BO list heads. */
  152. + for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
  153. + INIT_LIST_HEAD(&new_list[i]);
  154. +
  155. + kfree(vc4->bo_cache.size_list);
  156. + vc4->bo_cache.size_list = new_list;
  157. + vc4->bo_cache.size_list_size = new_size;
  158. + }
  159. +
  160. + return &vc4->bo_cache.size_list[page_index];
  161. +}
  162. +
  163. +void vc4_bo_cache_purge(struct drm_device *dev)
  164. +{
  165. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  166. +
  167. + spin_lock(&vc4->bo_lock);
  168. + while (!list_empty(&vc4->bo_cache.time_list)) {
  169. + struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
  170. + struct vc4_bo, unref_head);
  171. + vc4_bo_remove_from_cache(bo);
  172. + vc4_bo_destroy(bo);
  173. + }
  174. + spin_unlock(&vc4->bo_lock);
  175. +}
  176. +
  177. +struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size)
  178. +{
  179. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  180. + uint32_t size = roundup(unaligned_size, PAGE_SIZE);
  181. + uint32_t page_index = bo_page_index(size);
  182. struct drm_gem_cma_object *cma_obj;
  183. + int pass;
  184. - cma_obj = drm_gem_cma_create(dev, size);
  185. - if (IS_ERR(cma_obj))
  186. + if (size == 0)
  187. return NULL;
  188. - else
  189. - return to_vc4_bo(&cma_obj->base);
  190. +
  191. + /* First, try to get a vc4_bo from the kernel BO cache. */
  192. + spin_lock(&vc4->bo_lock);
  193. + if (page_index < vc4->bo_cache.size_list_size &&
  194. + !list_empty(&vc4->bo_cache.size_list[page_index])) {
  195. + struct vc4_bo *bo =
  196. + list_first_entry(&vc4->bo_cache.size_list[page_index],
  197. + struct vc4_bo, size_head);
  198. + vc4_bo_remove_from_cache(bo);
  199. + spin_unlock(&vc4->bo_lock);
  200. + kref_init(&bo->base.base.refcount);
  201. + return bo;
  202. + }
  203. + spin_unlock(&vc4->bo_lock);
  204. +
  205. + /* Otherwise, make a new BO. */
  206. + for (pass = 0; ; pass++) {
  207. + cma_obj = drm_gem_cma_create(dev, size);
  208. + if (!IS_ERR(cma_obj))
  209. + break;
  210. +
  211. + switch (pass) {
  212. + case 0:
  213. + /*
  214. + * If we've run out of CMA memory, kill the cache of
  215. + * CMA allocations we've got laying around and try again.
  216. + */
  217. + vc4_bo_cache_purge(dev);
  218. + break;
  219. + case 1:
  220. + /*
  221. + * Getting desperate, so try to wait for any
  222. + * previous rendering to finish, free its
  223. + * unreferenced BOs to the cache, and then
  224. + * free the cache.
  225. + */
  226. + vc4_wait_for_seqno(dev, vc4->emit_seqno, ~0ull, true);
  227. + vc4_job_handle_completed(vc4);
  228. + vc4_bo_cache_purge(dev);
  229. + break;
  230. + case 3:
  231. + DRM_ERROR("Failed to allocate from CMA:\n");
  232. + vc4_bo_stats_dump(vc4);
  233. + return NULL;
  234. + }
  235. + }
  236. +
  237. + vc4->bo_stats.num_allocated++;
  238. + vc4->bo_stats.size_allocated += size;
  239. +
  240. + return to_vc4_bo(&cma_obj->base);
  241. }
  242. int vc4_dumb_create(struct drm_file *file_priv,
  243. @@ -41,7 +199,129 @@ int vc4_dumb_create(struct drm_file *fil
  244. if (args->size < args->pitch * args->height)
  245. args->size = args->pitch * args->height;
  246. - bo = vc4_bo_create(dev, roundup(args->size, PAGE_SIZE));
  247. + bo = vc4_bo_create(dev, args->size);
  248. + if (!bo)
  249. + return -ENOMEM;
  250. +
  251. + ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  252. + drm_gem_object_unreference_unlocked(&bo->base.base);
  253. +
  254. + return ret;
  255. +}
  256. +
  257. +static void
  258. +vc4_bo_cache_free_old(struct drm_device *dev)
  259. +{
  260. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  261. + unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
  262. +
  263. + spin_lock(&vc4->bo_lock);
  264. + while (!list_empty(&vc4->bo_cache.time_list)) {
  265. + struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
  266. + struct vc4_bo, unref_head);
  267. + if (time_before(expire_time, bo->free_time)) {
  268. + mod_timer(&vc4->bo_cache.time_timer,
  269. + round_jiffies_up(jiffies +
  270. + msecs_to_jiffies(1000)));
  271. + spin_unlock(&vc4->bo_lock);
  272. + return;
  273. + }
  274. +
  275. + vc4_bo_remove_from_cache(bo);
  276. + vc4_bo_destroy(bo);
  277. + }
  278. + spin_unlock(&vc4->bo_lock);
  279. +}
  280. +
  281. +/* Called on the last userspace/kernel unreference of the BO. Returns
  282. + * it to the BO cache if possible, otherwise frees it.
  283. + *
  284. + * Note that this is called with the struct_mutex held.
  285. + */
  286. +void vc4_free_object(struct drm_gem_object *gem_bo)
  287. +{
  288. + struct drm_device *dev = gem_bo->dev;
  289. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  290. + struct vc4_bo *bo = to_vc4_bo(gem_bo);
  291. + struct list_head *cache_list;
  292. +
  293. + /* If the object references someone else's memory, we can't cache it.
  294. + */
  295. + if (gem_bo->import_attach) {
  296. + vc4_bo_destroy(bo);
  297. + return;
  298. + }
  299. +
  300. + /* Don't cache if it was publicly named. */
  301. + if (gem_bo->name) {
  302. + vc4_bo_destroy(bo);
  303. + return;
  304. + }
  305. +
  306. + spin_lock(&vc4->bo_lock);
  307. + cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
  308. + if (!cache_list) {
  309. + vc4_bo_destroy(bo);
  310. + spin_unlock(&vc4->bo_lock);
  311. + return;
  312. + }
  313. +
  314. + if (bo->validated_shader) {
  315. + kfree(bo->validated_shader->texture_samples);
  316. + kfree(bo->validated_shader);
  317. + bo->validated_shader = NULL;
  318. + }
  319. +
  320. + bo->free_time = jiffies;
  321. + list_add(&bo->size_head, cache_list);
  322. + list_add(&bo->unref_head, &vc4->bo_cache.time_list);
  323. +
  324. + vc4->bo_stats.num_cached++;
  325. + vc4->bo_stats.size_cached += gem_bo->size;
  326. + spin_unlock(&vc4->bo_lock);
  327. +
  328. + vc4_bo_cache_free_old(dev);
  329. +}
  330. +
  331. +static void vc4_bo_cache_time_work(struct work_struct *work)
  332. +{
  333. + struct vc4_dev *vc4 =
  334. + container_of(work, struct vc4_dev, bo_cache.time_work);
  335. + struct drm_device *dev = vc4->dev;
  336. +
  337. + vc4_bo_cache_free_old(dev);
  338. +}
  339. +
  340. +static void vc4_bo_cache_time_timer(unsigned long data)
  341. +{
  342. + struct drm_device *dev = (struct drm_device *)data;
  343. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  344. +
  345. + schedule_work(&vc4->bo_cache.time_work);
  346. +}
  347. +
  348. +struct dma_buf *
  349. +vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
  350. +{
  351. + struct vc4_bo *bo = to_vc4_bo(obj);
  352. +
  353. + if (bo->validated_shader) {
  354. + DRM_ERROR("Attempting to export shader BO\n");
  355. + return ERR_PTR(-EINVAL);
  356. + }
  357. +
  358. + return drm_gem_prime_export(dev, obj, flags);
  359. +}
  360. +
  361. +int
  362. +vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  363. + struct drm_file *file_priv)
  364. +{
  365. + struct drm_vc4_create_bo *args = data;
  366. + struct vc4_bo *bo = NULL;
  367. + int ret;
  368. +
  369. + bo = vc4_bo_create(dev, args->size);
  370. if (!bo)
  371. return -ENOMEM;
  372. @@ -50,3 +330,187 @@ int vc4_dumb_create(struct drm_file *fil
  373. return ret;
  374. }
  375. +
  376. +int
  377. +vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  378. + struct drm_file *file_priv)
  379. +{
  380. + struct drm_vc4_create_shader_bo *args = data;
  381. + struct vc4_bo *bo = NULL;
  382. + int ret;
  383. +
  384. + if (args->size == 0)
  385. + return -EINVAL;
  386. +
  387. + if (args->size % sizeof(u64) != 0)
  388. + return -EINVAL;
  389. +
  390. + if (args->flags != 0) {
  391. + DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
  392. + return -EINVAL;
  393. + }
  394. +
  395. + if (args->pad != 0) {
  396. + DRM_INFO("Pad set: 0x%08x\n", args->pad);
  397. + return -EINVAL;
  398. + }
  399. +
  400. + bo = vc4_bo_create(dev, args->size);
  401. + if (!bo)
  402. + return -ENOMEM;
  403. +
  404. + ret = copy_from_user(bo->base.vaddr,
  405. + (void __user *)(uintptr_t)args->data,
  406. + args->size);
  407. + if (ret != 0)
  408. + goto fail;
  409. +
  410. + bo->validated_shader = vc4_validate_shader(&bo->base);
  411. + if (!bo->validated_shader) {
  412. + ret = -EINVAL;
  413. + goto fail;
  414. + }
  415. +
  416. + /* We have to create the handle after validation, to avoid
  417. + * races for users to do doing things like mmap the shader BO.
  418. + */
  419. + ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
  420. +
  421. + fail:
  422. + drm_gem_object_unreference_unlocked(&bo->base.base);
  423. +
  424. + return ret;
  425. +}
  426. +
  427. +int
  428. +vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  429. + struct drm_file *file_priv)
  430. +{
  431. + struct drm_vc4_mmap_bo *args = data;
  432. + struct drm_gem_object *gem_obj;
  433. +
  434. + gem_obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  435. + if (!gem_obj) {
  436. + DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  437. + return -EINVAL;
  438. + }
  439. +
  440. + /* The mmap offset was set up at BO allocation time. */
  441. + args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
  442. +
  443. + drm_gem_object_unreference(gem_obj);
  444. + return 0;
  445. +}
  446. +
  447. +int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
  448. +{
  449. + struct drm_gem_object *gem_obj;
  450. + struct vc4_bo *bo;
  451. + int ret;
  452. +
  453. + ret = drm_gem_mmap(filp, vma);
  454. + if (ret)
  455. + return ret;
  456. +
  457. + gem_obj = vma->vm_private_data;
  458. + bo = to_vc4_bo(gem_obj);
  459. +
  460. + if (bo->validated_shader) {
  461. + DRM_ERROR("mmaping of shader BOs not allowed.\n");
  462. + return -EINVAL;
  463. + }
  464. +
  465. + /*
  466. + * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
  467. + * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
  468. + * the whole buffer.
  469. + */
  470. + vma->vm_flags &= ~VM_PFNMAP;
  471. + vma->vm_pgoff = 0;
  472. +
  473. + ret = dma_mmap_writecombine(bo->base.base.dev->dev, vma,
  474. + bo->base.vaddr, bo->base.paddr,
  475. + vma->vm_end - vma->vm_start);
  476. + if (ret)
  477. + drm_gem_vm_close(vma);
  478. +
  479. + return ret;
  480. +}
  481. +
  482. +int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  483. +{
  484. + struct vc4_bo *bo = to_vc4_bo(obj);
  485. +
  486. + if (bo->validated_shader) {
  487. + DRM_ERROR("mmaping of shader BOs not allowed.\n");
  488. + return -EINVAL;
  489. + }
  490. +
  491. + return drm_gem_cma_prime_mmap(obj, vma);
  492. +}
  493. +
  494. +void *vc4_prime_vmap(struct drm_gem_object *obj)
  495. +{
  496. + struct vc4_bo *bo = to_vc4_bo(obj);
  497. +
  498. + if (bo->validated_shader) {
  499. + DRM_ERROR("mmaping of shader BOs not allowed.\n");
  500. + return ERR_PTR(-EINVAL);
  501. + }
  502. +
  503. + return drm_gem_cma_prime_vmap(obj);
  504. +}
  505. +
  506. +void vc4_bo_cache_init(struct drm_device *dev)
  507. +{
  508. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  509. +
  510. + spin_lock_init(&vc4->bo_lock);
  511. +
  512. + INIT_LIST_HEAD(&vc4->bo_cache.time_list);
  513. +
  514. + INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
  515. + setup_timer(&vc4->bo_cache.time_timer,
  516. + vc4_bo_cache_time_timer,
  517. + (unsigned long) dev);
  518. +}
  519. +
  520. +void vc4_bo_cache_destroy(struct drm_device *dev)
  521. +{
  522. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  523. +
  524. + del_timer(&vc4->bo_cache.time_timer);
  525. + cancel_work_sync(&vc4->bo_cache.time_work);
  526. +
  527. + vc4_bo_cache_purge(dev);
  528. +
  529. + if (vc4->bo_stats.num_allocated) {
  530. + DRM_ERROR("Destroying BO cache while BOs still allocated:\n");
  531. + vc4_bo_stats_dump(vc4);
  532. + }
  533. +}
  534. +
  535. +#ifdef CONFIG_DEBUG_FS
  536. +int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
  537. +{
  538. + struct drm_info_node *node = (struct drm_info_node *) m->private;
  539. + struct drm_device *dev = node->minor->dev;
  540. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  541. + struct vc4_bo_stats stats;
  542. +
  543. + spin_lock(&vc4->bo_lock);
  544. + stats = vc4->bo_stats;
  545. + spin_unlock(&vc4->bo_lock);
  546. +
  547. + seq_printf(m, "num bos allocated: %d\n", stats.num_allocated);
  548. + seq_printf(m, "size bos allocated: %dkb\n", stats.size_allocated / 1024);
  549. + seq_printf(m, "num bos used: %d\n", (stats.num_allocated -
  550. + stats.num_cached));
  551. + seq_printf(m, "size bos used: %dkb\n", (stats.size_allocated -
  552. + stats.size_cached) / 1024);
  553. + seq_printf(m, "num bos cached: %d\n", stats.num_cached);
  554. + seq_printf(m, "size bos cached: %dkb\n", stats.size_cached / 1024);
  555. +
  556. + return 0;
  557. +}
  558. +#endif
  559. --- a/drivers/gpu/drm/vc4/vc4_crtc.c
  560. +++ b/drivers/gpu/drm/vc4/vc4_crtc.c
  561. @@ -35,6 +35,7 @@
  562. #include "drm_atomic_helper.h"
  563. #include "drm_crtc_helper.h"
  564. #include "linux/clk.h"
  565. +#include "drm_fb_cma_helper.h"
  566. #include "linux/component.h"
  567. #include "linux/of_device.h"
  568. #include "vc4_drv.h"
  569. @@ -476,10 +477,105 @@ static irqreturn_t vc4_crtc_irq_handler(
  570. return ret;
  571. }
  572. +struct vc4_async_flip_state {
  573. + struct drm_crtc *crtc;
  574. + struct drm_framebuffer *fb;
  575. + struct drm_pending_vblank_event *event;
  576. +
  577. + struct vc4_seqno_cb cb;
  578. +};
  579. +
  580. +/* Called when the V3D execution for the BO being flipped to is done, so that
  581. + * we can actually update the plane's address to point to it.
  582. + */
  583. +static void
  584. +vc4_async_page_flip_complete(struct vc4_seqno_cb *cb)
  585. +{
  586. + struct vc4_async_flip_state *flip_state =
  587. + container_of(cb, struct vc4_async_flip_state, cb);
  588. + struct drm_crtc *crtc = flip_state->crtc;
  589. + struct drm_device *dev = crtc->dev;
  590. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  591. + struct drm_plane *plane = crtc->primary;
  592. +
  593. + vc4_plane_async_set_fb(plane, flip_state->fb);
  594. + if (flip_state->event) {
  595. + unsigned long flags;
  596. + spin_lock_irqsave(&dev->event_lock, flags);
  597. + drm_crtc_send_vblank_event(crtc, flip_state->event);
  598. + spin_unlock_irqrestore(&dev->event_lock, flags);
  599. + }
  600. +
  601. + drm_framebuffer_unreference(flip_state->fb);
  602. + kfree(flip_state);
  603. +
  604. + up(&vc4->async_modeset);
  605. +}
  606. +
  607. +/* Implements async (non-vblank-synced) page flips.
  608. + *
  609. + * The page flip ioctl needs to return immediately, so we grab the
  610. + * modeset semaphore on the pipe, and queue the address update for
  611. + * when V3D is done with the BO being flipped to.
  612. + */
  613. +static int vc4_async_page_flip(struct drm_crtc *crtc,
  614. + struct drm_framebuffer *fb,
  615. + struct drm_pending_vblank_event *event,
  616. + uint32_t flags)
  617. +{
  618. + struct drm_device *dev = crtc->dev;
  619. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  620. + struct drm_plane *plane = crtc->primary;
  621. + int ret = 0;
  622. + struct vc4_async_flip_state *flip_state;
  623. + struct drm_gem_cma_object *cma_bo = drm_fb_cma_get_gem_obj(fb, 0);
  624. + struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  625. +
  626. + flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL);
  627. + if (!flip_state)
  628. + return -ENOMEM;
  629. +
  630. + drm_framebuffer_reference(fb);
  631. + flip_state->fb = fb;
  632. + flip_state->crtc = crtc;
  633. + flip_state->event = event;
  634. +
  635. + /* Make sure all other async modesetes have landed. */
  636. + ret = down_interruptible(&vc4->async_modeset);
  637. + if (ret) {
  638. + kfree(flip_state);
  639. + return ret;
  640. + }
  641. +
  642. + /* Immediately update the plane's legacy fb pointer, so that later
  643. + * modeset prep sees the state that will be present when the semaphore
  644. + * is released.
  645. + */
  646. + drm_atomic_set_fb_for_plane(plane->state, fb);
  647. + plane->fb = fb;
  648. +
  649. + vc4_queue_seqno_cb(dev, &flip_state->cb, bo->seqno,
  650. + vc4_async_page_flip_complete);
  651. +
  652. + /* Driver takes ownership of state on successful async commit. */
  653. + return 0;
  654. +}
  655. +
  656. +static int vc4_page_flip(struct drm_crtc *crtc,
  657. + struct drm_framebuffer *fb,
  658. + struct drm_pending_vblank_event *event,
  659. + uint32_t flags)
  660. +{
  661. + if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
  662. + return vc4_async_page_flip(crtc, fb, event, flags);
  663. + else
  664. + return drm_atomic_helper_page_flip(crtc, fb, event, flags);
  665. +}
  666. +
  667. static const struct drm_crtc_funcs vc4_crtc_funcs = {
  668. .set_config = drm_atomic_helper_set_config,
  669. .destroy = vc4_crtc_destroy,
  670. - .page_flip = drm_atomic_helper_page_flip,
  671. + .page_flip = vc4_page_flip,
  672. .set_property = NULL,
  673. .cursor_set = NULL, /* handled by drm_mode_cursor_universal */
  674. .cursor_move = NULL, /* handled by drm_mode_cursor_universal */
  675. --- a/drivers/gpu/drm/vc4/vc4_debugfs.c
  676. +++ b/drivers/gpu/drm/vc4/vc4_debugfs.c
  677. @@ -16,11 +16,14 @@
  678. #include "vc4_regs.h"
  679. static const struct drm_info_list vc4_debugfs_list[] = {
  680. + {"bo_stats", vc4_bo_stats_debugfs, 0},
  681. {"hdmi_regs", vc4_hdmi_debugfs_regs, 0},
  682. {"hvs_regs", vc4_hvs_debugfs_regs, 0},
  683. {"crtc0_regs", vc4_crtc_debugfs_regs, 0, (void *)(uintptr_t)0},
  684. {"crtc1_regs", vc4_crtc_debugfs_regs, 0, (void *)(uintptr_t)1},
  685. {"crtc2_regs", vc4_crtc_debugfs_regs, 0, (void *)(uintptr_t)2},
  686. + {"v3d_ident", vc4_v3d_debugfs_ident, 0},
  687. + {"v3d_regs", vc4_v3d_debugfs_regs, 0},
  688. };
  689. #define VC4_DEBUGFS_ENTRIES ARRAY_SIZE(vc4_debugfs_list)
  690. --- a/drivers/gpu/drm/vc4/vc4_drv.c
  691. +++ b/drivers/gpu/drm/vc4/vc4_drv.c
  692. @@ -14,8 +14,10 @@
  693. #include <linux/module.h>
  694. #include <linux/of_platform.h>
  695. #include <linux/platform_device.h>
  696. +#include <soc/bcm2835/raspberrypi-firmware.h>
  697. #include "drm_fb_cma_helper.h"
  698. +#include "uapi/drm/vc4_drm.h"
  699. #include "vc4_drv.h"
  700. #include "vc4_regs.h"
  701. @@ -63,7 +65,7 @@ static const struct file_operations vc4_
  702. .open = drm_open,
  703. .release = drm_release,
  704. .unlocked_ioctl = drm_ioctl,
  705. - .mmap = drm_gem_cma_mmap,
  706. + .mmap = vc4_mmap,
  707. .poll = drm_poll,
  708. .read = drm_read,
  709. #ifdef CONFIG_COMPAT
  710. @@ -73,16 +75,28 @@ static const struct file_operations vc4_
  711. };
  712. static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
  713. + DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, 0),
  714. + DRM_IOCTL_DEF_DRV(VC4_WAIT_SEQNO, vc4_wait_seqno_ioctl, 0),
  715. + DRM_IOCTL_DEF_DRV(VC4_WAIT_BO, vc4_wait_bo_ioctl, 0),
  716. + DRM_IOCTL_DEF_DRV(VC4_CREATE_BO, vc4_create_bo_ioctl, 0),
  717. + DRM_IOCTL_DEF_DRV(VC4_MMAP_BO, vc4_mmap_bo_ioctl, 0),
  718. + DRM_IOCTL_DEF_DRV(VC4_CREATE_SHADER_BO, vc4_create_shader_bo_ioctl, 0),
  719. };
  720. static struct drm_driver vc4_drm_driver = {
  721. .driver_features = (DRIVER_MODESET |
  722. DRIVER_ATOMIC |
  723. DRIVER_GEM |
  724. + DRIVER_HAVE_IRQ |
  725. DRIVER_PRIME),
  726. .lastclose = vc4_lastclose,
  727. .preclose = vc4_drm_preclose,
  728. + .irq_handler = vc4_irq,
  729. + .irq_preinstall = vc4_irq_preinstall,
  730. + .irq_postinstall = vc4_irq_postinstall,
  731. + .irq_uninstall = vc4_irq_uninstall,
  732. +
  733. .enable_vblank = vc4_enable_vblank,
  734. .disable_vblank = vc4_disable_vblank,
  735. .get_vblank_counter = drm_vblank_count,
  736. @@ -92,18 +106,18 @@ static struct drm_driver vc4_drm_driver
  737. .debugfs_cleanup = vc4_debugfs_cleanup,
  738. #endif
  739. - .gem_free_object = drm_gem_cma_free_object,
  740. + .gem_free_object = vc4_free_object,
  741. .gem_vm_ops = &drm_gem_cma_vm_ops,
  742. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  743. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  744. .gem_prime_import = drm_gem_prime_import,
  745. - .gem_prime_export = drm_gem_prime_export,
  746. + .gem_prime_export = vc4_prime_export,
  747. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  748. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  749. - .gem_prime_vmap = drm_gem_cma_prime_vmap,
  750. + .gem_prime_vmap = vc4_prime_vmap,
  751. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  752. - .gem_prime_mmap = drm_gem_cma_prime_mmap,
  753. + .gem_prime_mmap = vc4_prime_mmap,
  754. .dumb_create = vc4_dumb_create,
  755. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  756. @@ -113,6 +127,8 @@ static struct drm_driver vc4_drm_driver
  757. .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
  758. .fops = &vc4_drm_fops,
  759. + .gem_obj_size = sizeof(struct vc4_bo),
  760. +
  761. .name = DRIVER_NAME,
  762. .desc = DRIVER_DESC,
  763. .date = DRIVER_DATE,
  764. @@ -153,6 +169,7 @@ static int vc4_drm_bind(struct device *d
  765. struct drm_device *drm;
  766. struct drm_connector *connector;
  767. struct vc4_dev *vc4;
  768. + struct device_node *firmware_node;
  769. int ret = 0;
  770. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  771. @@ -161,6 +178,14 @@ static int vc4_drm_bind(struct device *d
  772. if (!vc4)
  773. return -ENOMEM;
  774. + firmware_node = of_parse_phandle(dev->of_node, "firmware", 0);
  775. + vc4->firmware = rpi_firmware_get(firmware_node);
  776. + if (!vc4->firmware) {
  777. + DRM_DEBUG("Failed to get Raspberry Pi firmware reference.\n");
  778. + return -EPROBE_DEFER;
  779. + }
  780. + of_node_put(firmware_node);
  781. +
  782. drm = drm_dev_alloc(&vc4_drm_driver, dev);
  783. if (!drm)
  784. return -ENOMEM;
  785. @@ -170,13 +195,17 @@ static int vc4_drm_bind(struct device *d
  786. drm_dev_set_unique(drm, dev_name(dev));
  787. + vc4_bo_cache_init(drm);
  788. +
  789. drm_mode_config_init(drm);
  790. if (ret)
  791. goto unref;
  792. + vc4_gem_init(drm);
  793. +
  794. ret = component_bind_all(dev, drm);
  795. if (ret)
  796. - goto unref;
  797. + goto gem_destroy;
  798. ret = drm_dev_register(drm, 0);
  799. if (ret < 0)
  800. @@ -200,8 +229,11 @@ unregister:
  801. drm_dev_unregister(drm);
  802. unbind_all:
  803. component_unbind_all(dev, drm);
  804. +gem_destroy:
  805. + vc4_gem_destroy(drm);
  806. unref:
  807. drm_dev_unref(drm);
  808. + vc4_bo_cache_destroy(drm);
  809. return ret;
  810. }
  811. @@ -228,6 +260,7 @@ static struct platform_driver *const com
  812. &vc4_hdmi_driver,
  813. &vc4_crtc_driver,
  814. &vc4_hvs_driver,
  815. + &vc4_v3d_driver,
  816. };
  817. static int vc4_platform_drm_probe(struct platform_device *pdev)
  818. --- a/drivers/gpu/drm/vc4/vc4_drv.h
  819. +++ b/drivers/gpu/drm/vc4/vc4_drv.h
  820. @@ -15,8 +15,85 @@ struct vc4_dev {
  821. struct vc4_hdmi *hdmi;
  822. struct vc4_hvs *hvs;
  823. struct vc4_crtc *crtc[3];
  824. + struct vc4_v3d *v3d;
  825. struct drm_fbdev_cma *fbdev;
  826. + struct rpi_firmware *firmware;
  827. +
  828. + /* The kernel-space BO cache. Tracks buffers that have been
  829. + * unreferenced by all other users (refcounts of 0!) but not
  830. + * yet freed, so we can do cheap allocations.
  831. + */
  832. + struct vc4_bo_cache {
  833. + /* Array of list heads for entries in the BO cache,
  834. + * based on number of pages, so we can do O(1) lookups
  835. + * in the cache when allocating.
  836. + */
  837. + struct list_head *size_list;
  838. + uint32_t size_list_size;
  839. +
  840. + /* List of all BOs in the cache, ordered by age, so we
  841. + * can do O(1) lookups when trying to free old
  842. + * buffers.
  843. + */
  844. + struct list_head time_list;
  845. + struct work_struct time_work;
  846. + struct timer_list time_timer;
  847. + } bo_cache;
  848. +
  849. + struct vc4_bo_stats {
  850. + u32 num_allocated;
  851. + u32 size_allocated;
  852. + u32 num_cached;
  853. + u32 size_cached;
  854. + } bo_stats;
  855. +
  856. + /* Protects bo_cache and the BO stats. */
  857. + spinlock_t bo_lock;
  858. +
  859. + /* Sequence number for the last job queued in job_list.
  860. + * Starts at 0 (no jobs emitted).
  861. + */
  862. + uint64_t emit_seqno;
  863. +
  864. + /* Sequence number for the last completed job on the GPU.
  865. + * Starts at 0 (no jobs completed).
  866. + */
  867. + uint64_t finished_seqno;
  868. +
  869. + /* List of all struct vc4_exec_info for jobs to be executed.
  870. + * The first job in the list is the one currently programmed
  871. + * into ct0ca/ct1ca for execution.
  872. + */
  873. + struct list_head job_list;
  874. + /* List of the finished vc4_exec_infos waiting to be freed by
  875. + * job_done_work.
  876. + */
  877. + struct list_head job_done_list;
  878. + spinlock_t job_lock;
  879. + wait_queue_head_t job_wait_queue;
  880. + struct work_struct job_done_work;
  881. +
  882. + /* List of struct vc4_seqno_cb for callbacks to be made from a
  883. + * workqueue when the given seqno is passed.
  884. + */
  885. + struct list_head seqno_cb_list;
  886. +
  887. + /* The binner overflow memory that's currently set up in
  888. + * BPOA/BPOS registers. When overflow occurs and a new one is
  889. + * allocated, the previous one will be moved to
  890. + * vc4->current_exec's free list.
  891. + */
  892. + struct vc4_bo *overflow_mem;
  893. + struct work_struct overflow_mem_work;
  894. +
  895. + struct {
  896. + uint32_t last_ct0ca, last_ct1ca;
  897. + struct timer_list timer;
  898. + struct work_struct reset_work;
  899. + } hangcheck;
  900. +
  901. + struct semaphore async_modeset;
  902. };
  903. static inline struct vc4_dev *
  904. @@ -27,6 +104,25 @@ to_vc4_dev(struct drm_device *dev)
  905. struct vc4_bo {
  906. struct drm_gem_cma_object base;
  907. +
  908. + /* seqno of the last job to render to this BO. */
  909. + uint64_t seqno;
  910. +
  911. + /* List entry for the BO's position in either
  912. + * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
  913. + */
  914. + struct list_head unref_head;
  915. +
  916. + /* Time in jiffies when the BO was put in vc4->bo_cache. */
  917. + unsigned long free_time;
  918. +
  919. + /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
  920. + struct list_head size_head;
  921. +
  922. + /* Struct for shader validation state, if created by
  923. + * DRM_IOCTL_VC4_CREATE_SHADER_BO.
  924. + */
  925. + struct vc4_validated_shader_info *validated_shader;
  926. };
  927. static inline struct vc4_bo *
  928. @@ -35,6 +131,17 @@ to_vc4_bo(struct drm_gem_object *bo)
  929. return (struct vc4_bo *)bo;
  930. }
  931. +struct vc4_seqno_cb {
  932. + struct work_struct work;
  933. + uint64_t seqno;
  934. + void (*func)(struct vc4_seqno_cb *cb);
  935. +};
  936. +
  937. +struct vc4_v3d {
  938. + struct platform_device *pdev;
  939. + void __iomem *regs;
  940. +};
  941. +
  942. struct vc4_hvs {
  943. struct platform_device *pdev;
  944. void __iomem *regs;
  945. @@ -72,9 +179,151 @@ to_vc4_encoder(struct drm_encoder *encod
  946. return container_of(encoder, struct vc4_encoder, base);
  947. }
  948. +#define V3D_READ(offset) readl(vc4->v3d->regs + offset)
  949. +#define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
  950. #define HVS_READ(offset) readl(vc4->hvs->regs + offset)
  951. #define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
  952. +enum vc4_bo_mode {
  953. + VC4_MODE_UNDECIDED,
  954. + VC4_MODE_RENDER,
  955. + VC4_MODE_SHADER,
  956. +};
  957. +
  958. +struct vc4_bo_exec_state {
  959. + struct drm_gem_cma_object *bo;
  960. + enum vc4_bo_mode mode;
  961. +};
  962. +
  963. +struct vc4_exec_info {
  964. + /* Sequence number for this bin/render job. */
  965. + uint64_t seqno;
  966. +
  967. + /* Kernel-space copy of the ioctl arguments */
  968. + struct drm_vc4_submit_cl *args;
  969. +
  970. + /* This is the array of BOs that were looked up at the start of exec.
  971. + * Command validation will use indices into this array.
  972. + */
  973. + struct vc4_bo_exec_state *bo;
  974. + uint32_t bo_count;
  975. +
  976. + /* Pointers for our position in vc4->job_list */
  977. + struct list_head head;
  978. +
  979. + /* List of other BOs used in the job that need to be released
  980. + * once the job is complete.
  981. + */
  982. + struct list_head unref_list;
  983. +
  984. + /* Current unvalidated indices into @bo loaded by the non-hardware
  985. + * VC4_PACKET_GEM_HANDLES.
  986. + */
  987. + uint32_t bo_index[2];
  988. +
  989. + /* This is the BO where we store the validated command lists, shader
  990. + * records, and uniforms.
  991. + */
  992. + struct drm_gem_cma_object *exec_bo;
  993. +
  994. + /**
  995. + * This tracks the per-shader-record state (packet 64) that
  996. + * determines the length of the shader record and the offset
  997. + * it's expected to be found at. It gets read in from the
  998. + * command lists.
  999. + */
  1000. + struct vc4_shader_state {
  1001. + uint8_t packet;
  1002. + uint32_t addr;
  1003. + /* Maximum vertex index referenced by any primitive using this
  1004. + * shader state.
  1005. + */
  1006. + uint32_t max_index;
  1007. + } *shader_state;
  1008. +
  1009. + /** How many shader states the user declared they were using. */
  1010. + uint32_t shader_state_size;
  1011. + /** How many shader state records the validator has seen. */
  1012. + uint32_t shader_state_count;
  1013. +
  1014. + bool found_tile_binning_mode_config_packet;
  1015. + bool found_start_tile_binning_packet;
  1016. + bool found_increment_semaphore_packet;
  1017. + uint8_t bin_tiles_x, bin_tiles_y;
  1018. + struct drm_gem_cma_object *tile_bo;
  1019. + uint32_t tile_alloc_offset;
  1020. +
  1021. + /**
  1022. + * Computed addresses pointing into exec_bo where we start the
  1023. + * bin thread (ct0) and render thread (ct1).
  1024. + */
  1025. + uint32_t ct0ca, ct0ea;
  1026. + uint32_t ct1ca, ct1ea;
  1027. +
  1028. + /* Pointers to the shader recs. These paddr gets incremented as CL
  1029. + * packets are relocated in validate_gl_shader_state, and the vaddrs
  1030. + * (u and v) get incremented and size decremented as the shader recs
  1031. + * themselves are validated.
  1032. + */
  1033. + void *shader_rec_u;
  1034. + void *shader_rec_v;
  1035. + uint32_t shader_rec_p;
  1036. + uint32_t shader_rec_size;
  1037. +
  1038. + /* Pointers to the uniform data. These pointers are incremented, and
  1039. + * size decremented, as each batch of uniforms is uploaded.
  1040. + */
  1041. + void *uniforms_u;
  1042. + void *uniforms_v;
  1043. + uint32_t uniforms_p;
  1044. + uint32_t uniforms_size;
  1045. +};
  1046. +
  1047. +static inline struct vc4_exec_info *
  1048. +vc4_first_job(struct vc4_dev *vc4)
  1049. +{
  1050. + if (list_empty(&vc4->job_list))
  1051. + return NULL;
  1052. + return list_first_entry(&vc4->job_list, struct vc4_exec_info, head);
  1053. +}
  1054. +
  1055. +/**
  1056. + * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
  1057. + * setup parameters.
  1058. + *
  1059. + * This will be used at draw time to relocate the reference to the texture
  1060. + * contents in p0, and validate that the offset combined with
  1061. + * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
  1062. + * Note that the hardware treats unprovided config parameters as 0, so not all
  1063. + * of them need to be set up for every texure sample, and we'll store ~0 as
  1064. + * the offset to mark the unused ones.
  1065. + *
  1066. + * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
  1067. + * Setup") for definitions of the texture parameters.
  1068. + */
  1069. +struct vc4_texture_sample_info {
  1070. + bool is_direct;
  1071. + uint32_t p_offset[4];
  1072. +};
  1073. +
  1074. +/**
  1075. + * struct vc4_validated_shader_info - information about validated shaders that
  1076. + * needs to be used from command list validation.
  1077. + *
  1078. + * For a given shader, each time a shader state record references it, we need
  1079. + * to verify that the shader doesn't read more uniforms than the shader state
  1080. + * record's uniform BO pointer can provide, and we need to apply relocations
  1081. + * and validate the shader state record's uniforms that define the texture
  1082. + * samples.
  1083. + */
  1084. +struct vc4_validated_shader_info
  1085. +{
  1086. + uint32_t uniforms_size;
  1087. + uint32_t uniforms_src_size;
  1088. + uint32_t num_texture_samples;
  1089. + struct vc4_texture_sample_info *texture_samples;
  1090. +};
  1091. +
  1092. /**
  1093. * _wait_for - magic (register) wait macro
  1094. *
  1095. @@ -111,6 +360,18 @@ int vc4_dumb_create(struct drm_file *fil
  1096. struct drm_mode_create_dumb *args);
  1097. struct dma_buf *vc4_prime_export(struct drm_device *dev,
  1098. struct drm_gem_object *obj, int flags);
  1099. +int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
  1100. + struct drm_file *file_priv);
  1101. +int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
  1102. + struct drm_file *file_priv);
  1103. +int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
  1104. + struct drm_file *file_priv);
  1105. +int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
  1106. +int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
  1107. +void *vc4_prime_vmap(struct drm_gem_object *obj);
  1108. +void vc4_bo_cache_init(struct drm_device *dev);
  1109. +void vc4_bo_cache_destroy(struct drm_device *dev);
  1110. +int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
  1111. /* vc4_crtc.c */
  1112. extern struct platform_driver vc4_crtc_driver;
  1113. @@ -126,10 +387,34 @@ void vc4_debugfs_cleanup(struct drm_mino
  1114. /* vc4_drv.c */
  1115. void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
  1116. +/* vc4_gem.c */
  1117. +void vc4_gem_init(struct drm_device *dev);
  1118. +void vc4_gem_destroy(struct drm_device *dev);
  1119. +int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  1120. + struct drm_file *file_priv);
  1121. +int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  1122. + struct drm_file *file_priv);
  1123. +int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  1124. + struct drm_file *file_priv);
  1125. +void vc4_submit_next_job(struct drm_device *dev);
  1126. +int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
  1127. + uint64_t timeout_ns, bool interruptible);
  1128. +void vc4_job_handle_completed(struct vc4_dev *vc4);
  1129. +int vc4_queue_seqno_cb(struct drm_device *dev,
  1130. + struct vc4_seqno_cb *cb, uint64_t seqno,
  1131. + void (*func)(struct vc4_seqno_cb *cb));
  1132. +
  1133. /* vc4_hdmi.c */
  1134. extern struct platform_driver vc4_hdmi_driver;
  1135. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
  1136. +/* vc4_irq.c */
  1137. +irqreturn_t vc4_irq(int irq, void *arg);
  1138. +void vc4_irq_preinstall(struct drm_device *dev);
  1139. +int vc4_irq_postinstall(struct drm_device *dev);
  1140. +void vc4_irq_uninstall(struct drm_device *dev);
  1141. +void vc4_irq_reset(struct drm_device *dev);
  1142. +
  1143. /* vc4_hvs.c */
  1144. extern struct platform_driver vc4_hvs_driver;
  1145. void vc4_hvs_dump_state(struct drm_device *dev);
  1146. @@ -143,3 +428,35 @@ struct drm_plane *vc4_plane_init(struct
  1147. enum drm_plane_type type);
  1148. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
  1149. u32 vc4_plane_dlist_size(struct drm_plane_state *state);
  1150. +void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb);
  1151. +
  1152. +/* vc4_v3d.c */
  1153. +extern struct platform_driver vc4_v3d_driver;
  1154. +int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
  1155. +int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
  1156. +int vc4_v3d_set_power(struct vc4_dev *vc4, bool on);
  1157. +
  1158. +/* vc4_validate.c */
  1159. +int
  1160. +vc4_validate_bin_cl(struct drm_device *dev,
  1161. + void *validated,
  1162. + void *unvalidated,
  1163. + struct vc4_exec_info *exec);
  1164. +
  1165. +int
  1166. +vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
  1167. +
  1168. +struct vc4_validated_shader_info *
  1169. +vc4_validate_shader(struct drm_gem_cma_object *shader_obj);
  1170. +
  1171. +bool vc4_use_bo(struct vc4_exec_info *exec,
  1172. + uint32_t hindex,
  1173. + enum vc4_bo_mode mode,
  1174. + struct drm_gem_cma_object **obj);
  1175. +
  1176. +int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
  1177. +
  1178. +bool vc4_check_tex_size(struct vc4_exec_info *exec,
  1179. + struct drm_gem_cma_object *fbo,
  1180. + uint32_t offset, uint8_t tiling_format,
  1181. + uint32_t width, uint32_t height, uint8_t cpp);
  1182. --- /dev/null
  1183. +++ b/drivers/gpu/drm/vc4/vc4_gem.c
  1184. @@ -0,0 +1,686 @@
  1185. +/*
  1186. + * Copyright © 2014 Broadcom
  1187. + *
  1188. + * Permission is hereby granted, free of charge, to any person obtaining a
  1189. + * copy of this software and associated documentation files (the "Software"),
  1190. + * to deal in the Software without restriction, including without limitation
  1191. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  1192. + * and/or sell copies of the Software, and to permit persons to whom the
  1193. + * Software is furnished to do so, subject to the following conditions:
  1194. + *
  1195. + * The above copyright notice and this permission notice (including the next
  1196. + * paragraph) shall be included in all copies or substantial portions of the
  1197. + * Software.
  1198. + *
  1199. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1200. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1201. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  1202. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1203. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1204. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1205. + * IN THE SOFTWARE.
  1206. + */
  1207. +
  1208. +#include <linux/module.h>
  1209. +#include <linux/platform_device.h>
  1210. +#include <linux/device.h>
  1211. +#include <linux/io.h>
  1212. +
  1213. +#include "uapi/drm/vc4_drm.h"
  1214. +#include "vc4_drv.h"
  1215. +#include "vc4_regs.h"
  1216. +#include "vc4_trace.h"
  1217. +
  1218. +static void
  1219. +vc4_queue_hangcheck(struct drm_device *dev)
  1220. +{
  1221. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1222. +
  1223. + mod_timer(&vc4->hangcheck.timer,
  1224. + round_jiffies_up(jiffies + msecs_to_jiffies(100)));
  1225. +}
  1226. +
  1227. +static void
  1228. +vc4_reset(struct drm_device *dev)
  1229. +{
  1230. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1231. +
  1232. + DRM_INFO("Resetting GPU.\n");
  1233. + vc4_v3d_set_power(vc4, false);
  1234. + vc4_v3d_set_power(vc4, true);
  1235. +
  1236. + vc4_irq_reset(dev);
  1237. +
  1238. + /* Rearm the hangcheck -- another job might have been waiting
  1239. + * for our hung one to get kicked off, and vc4_irq_reset()
  1240. + * would have started it.
  1241. + */
  1242. + vc4_queue_hangcheck(dev);
  1243. +}
  1244. +
  1245. +static void
  1246. +vc4_reset_work(struct work_struct *work)
  1247. +{
  1248. + struct vc4_dev *vc4 =
  1249. + container_of(work, struct vc4_dev, hangcheck.reset_work);
  1250. +
  1251. + vc4_reset(vc4->dev);
  1252. +}
  1253. +
  1254. +static void
  1255. +vc4_hangcheck_elapsed(unsigned long data)
  1256. +{
  1257. + struct drm_device *dev = (struct drm_device *)data;
  1258. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1259. + uint32_t ct0ca, ct1ca;
  1260. +
  1261. + /* If idle, we can stop watching for hangs. */
  1262. + if (list_empty(&vc4->job_list))
  1263. + return;
  1264. +
  1265. + ct0ca = V3D_READ(V3D_CTNCA(0));
  1266. + ct1ca = V3D_READ(V3D_CTNCA(1));
  1267. +
  1268. + /* If we've made any progress in execution, rearm the timer
  1269. + * and wait.
  1270. + */
  1271. + if (ct0ca != vc4->hangcheck.last_ct0ca ||
  1272. + ct1ca != vc4->hangcheck.last_ct1ca) {
  1273. + vc4->hangcheck.last_ct0ca = ct0ca;
  1274. + vc4->hangcheck.last_ct1ca = ct1ca;
  1275. + vc4_queue_hangcheck(dev);
  1276. + return;
  1277. + }
  1278. +
  1279. + /* We've gone too long with no progress, reset. This has to
  1280. + * be done from a work struct, since resetting can sleep and
  1281. + * this timer hook isn't allowed to.
  1282. + */
  1283. + schedule_work(&vc4->hangcheck.reset_work);
  1284. +}
  1285. +
  1286. +static void
  1287. +submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
  1288. +{
  1289. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1290. +
  1291. + /* Stop any existing thread and set state to "stopped at halt" */
  1292. + V3D_WRITE(V3D_CTNCS(thread), V3D_CTRUN);
  1293. + barrier();
  1294. +
  1295. + V3D_WRITE(V3D_CTNCA(thread), start);
  1296. + barrier();
  1297. +
  1298. + /* Set the end address of the control list. Writing this
  1299. + * register is what starts the job.
  1300. + */
  1301. + V3D_WRITE(V3D_CTNEA(thread), end);
  1302. + barrier();
  1303. +}
  1304. +
  1305. +int
  1306. +vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
  1307. + bool interruptible)
  1308. +{
  1309. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1310. + int ret = 0;
  1311. + unsigned long timeout_expire;
  1312. + DEFINE_WAIT(wait);
  1313. +
  1314. + if (vc4->finished_seqno >= seqno)
  1315. + return 0;
  1316. +
  1317. + if (timeout_ns == 0)
  1318. + return -ETIME;
  1319. +
  1320. + timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
  1321. +
  1322. + trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
  1323. + for (;;) {
  1324. + prepare_to_wait(&vc4->job_wait_queue, &wait,
  1325. + interruptible ? TASK_INTERRUPTIBLE :
  1326. + TASK_UNINTERRUPTIBLE);
  1327. +
  1328. + if (interruptible && signal_pending(current)) {
  1329. + ret = -ERESTARTSYS;
  1330. + break;
  1331. + }
  1332. +
  1333. + if (vc4->finished_seqno >= seqno)
  1334. + break;
  1335. +
  1336. + if (timeout_ns != ~0ull) {
  1337. + if (time_after_eq(jiffies, timeout_expire)) {
  1338. + ret = -ETIME;
  1339. + break;
  1340. + }
  1341. + schedule_timeout(timeout_expire - jiffies);
  1342. + } else {
  1343. + schedule();
  1344. + }
  1345. + }
  1346. +
  1347. + finish_wait(&vc4->job_wait_queue, &wait);
  1348. + trace_vc4_wait_for_seqno_end(dev, seqno);
  1349. +
  1350. + if (ret && ret != -ERESTARTSYS) {
  1351. + DRM_ERROR("timeout waiting for render thread idle\n");
  1352. + return ret;
  1353. + }
  1354. +
  1355. + return 0;
  1356. +}
  1357. +
  1358. +static void
  1359. +vc4_flush_caches(struct drm_device *dev)
  1360. +{
  1361. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1362. +
  1363. + /* Flush the GPU L2 caches. These caches sit on top of system
  1364. + * L3 (the 128kb or so shared with the CPU), and are
  1365. + * non-allocating in the L3.
  1366. + */
  1367. + V3D_WRITE(V3D_L2CACTL,
  1368. + V3D_L2CACTL_L2CCLR);
  1369. +
  1370. + V3D_WRITE(V3D_SLCACTL,
  1371. + VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
  1372. + VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
  1373. + VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
  1374. + VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
  1375. +}
  1376. +
  1377. +/* Sets the registers for the next job to be actually be executed in
  1378. + * the hardware.
  1379. + *
  1380. + * The job_lock should be held during this.
  1381. + */
  1382. +void
  1383. +vc4_submit_next_job(struct drm_device *dev)
  1384. +{
  1385. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1386. + struct vc4_exec_info *exec = vc4_first_job(vc4);
  1387. +
  1388. + if (!exec)
  1389. + return;
  1390. +
  1391. + vc4_flush_caches(dev);
  1392. +
  1393. + /* Disable the binner's pre-loaded overflow memory address */
  1394. + V3D_WRITE(V3D_BPOA, 0);
  1395. + V3D_WRITE(V3D_BPOS, 0);
  1396. +
  1397. + if (exec->ct0ca != exec->ct0ea)
  1398. + submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
  1399. + submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
  1400. +}
  1401. +
  1402. +static void
  1403. +vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
  1404. +{
  1405. + struct vc4_bo *bo;
  1406. + unsigned i;
  1407. +
  1408. + for (i = 0; i < exec->bo_count; i++) {
  1409. + bo = to_vc4_bo(&exec->bo[i].bo->base);
  1410. + bo->seqno = seqno;
  1411. + }
  1412. +
  1413. + list_for_each_entry(bo, &exec->unref_list, unref_head) {
  1414. + bo->seqno = seqno;
  1415. + }
  1416. +}
  1417. +
  1418. +/* Queues a struct vc4_exec_info for execution. If no job is
  1419. + * currently executing, then submits it.
  1420. + *
  1421. + * Unlike most GPUs, our hardware only handles one command list at a
  1422. + * time. To queue multiple jobs at once, we'd need to edit the
  1423. + * previous command list to have a jump to the new one at the end, and
  1424. + * then bump the end address. That's a change for a later date,
  1425. + * though.
  1426. + */
  1427. +static void
  1428. +vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec)
  1429. +{
  1430. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1431. + uint64_t seqno = ++vc4->emit_seqno;
  1432. + unsigned long irqflags;
  1433. +
  1434. + exec->seqno = seqno;
  1435. + vc4_update_bo_seqnos(exec, seqno);
  1436. +
  1437. + spin_lock_irqsave(&vc4->job_lock, irqflags);
  1438. + list_add_tail(&exec->head, &vc4->job_list);
  1439. +
  1440. + /* If no job was executing, kick ours off. Otherwise, it'll
  1441. + * get started when the previous job's frame done interrupt
  1442. + * occurs.
  1443. + */
  1444. + if (vc4_first_job(vc4) == exec) {
  1445. + vc4_submit_next_job(dev);
  1446. + vc4_queue_hangcheck(dev);
  1447. + }
  1448. +
  1449. + spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  1450. +}
  1451. +
  1452. +/**
  1453. + * Looks up a bunch of GEM handles for BOs and stores the array for
  1454. + * use in the command validator that actually writes relocated
  1455. + * addresses pointing to them.
  1456. + */
  1457. +static int
  1458. +vc4_cl_lookup_bos(struct drm_device *dev,
  1459. + struct drm_file *file_priv,
  1460. + struct vc4_exec_info *exec)
  1461. +{
  1462. + struct drm_vc4_submit_cl *args = exec->args;
  1463. + uint32_t *handles;
  1464. + int ret = 0;
  1465. + int i;
  1466. +
  1467. + exec->bo_count = args->bo_handle_count;
  1468. +
  1469. + if (!exec->bo_count) {
  1470. + /* See comment on bo_index for why we have to check
  1471. + * this.
  1472. + */
  1473. + DRM_ERROR("Rendering requires BOs to validate\n");
  1474. + return -EINVAL;
  1475. + }
  1476. +
  1477. + exec->bo = kcalloc(exec->bo_count, sizeof(struct vc4_bo_exec_state),
  1478. + GFP_KERNEL);
  1479. + if (!exec->bo) {
  1480. + DRM_ERROR("Failed to allocate validated BO pointers\n");
  1481. + return -ENOMEM;
  1482. + }
  1483. +
  1484. + handles = drm_malloc_ab(exec->bo_count, sizeof(uint32_t));
  1485. + if (!handles) {
  1486. + DRM_ERROR("Failed to allocate incoming GEM handles\n");
  1487. + goto fail;
  1488. + }
  1489. +
  1490. + ret = copy_from_user(handles,
  1491. + (void __user *)(uintptr_t)args->bo_handles,
  1492. + exec->bo_count * sizeof(uint32_t));
  1493. + if (ret) {
  1494. + DRM_ERROR("Failed to copy in GEM handles\n");
  1495. + goto fail;
  1496. + }
  1497. +
  1498. + spin_lock(&file_priv->table_lock);
  1499. + for (i = 0; i < exec->bo_count; i++) {
  1500. + struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
  1501. + handles[i]);
  1502. + if (!bo) {
  1503. + DRM_ERROR("Failed to look up GEM BO %d: %d\n",
  1504. + i, handles[i]);
  1505. + ret = -EINVAL;
  1506. + spin_unlock(&file_priv->table_lock);
  1507. + goto fail;
  1508. + }
  1509. + drm_gem_object_reference(bo);
  1510. + exec->bo[i].bo = (struct drm_gem_cma_object *)bo;
  1511. + }
  1512. + spin_unlock(&file_priv->table_lock);
  1513. +
  1514. +fail:
  1515. + kfree(handles);
  1516. + return 0;
  1517. +}
  1518. +
  1519. +static int
  1520. +vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
  1521. +{
  1522. + struct drm_vc4_submit_cl *args = exec->args;
  1523. + void *temp = NULL;
  1524. + void *bin;
  1525. + int ret = 0;
  1526. + uint32_t bin_offset = 0;
  1527. + uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
  1528. + 16);
  1529. + uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
  1530. + uint32_t exec_size = uniforms_offset + args->uniforms_size;
  1531. + uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
  1532. + args->shader_rec_count);
  1533. + struct vc4_bo *bo;
  1534. +
  1535. + if (uniforms_offset < shader_rec_offset ||
  1536. + exec_size < uniforms_offset ||
  1537. + args->shader_rec_count >= (UINT_MAX /
  1538. + sizeof(struct vc4_shader_state)) ||
  1539. + temp_size < exec_size) {
  1540. + DRM_ERROR("overflow in exec arguments\n");
  1541. + goto fail;
  1542. + }
  1543. +
  1544. + /* Allocate space where we'll store the copied in user command lists
  1545. + * and shader records.
  1546. + *
  1547. + * We don't just copy directly into the BOs because we need to
  1548. + * read the contents back for validation, and I think the
  1549. + * bo->vaddr is uncached access.
  1550. + */
  1551. + temp = kmalloc(temp_size, GFP_KERNEL);
  1552. + if (!temp) {
  1553. + DRM_ERROR("Failed to allocate storage for copying "
  1554. + "in bin/render CLs.\n");
  1555. + ret = -ENOMEM;
  1556. + goto fail;
  1557. + }
  1558. + bin = temp + bin_offset;
  1559. + exec->shader_rec_u = temp + shader_rec_offset;
  1560. + exec->uniforms_u = temp + uniforms_offset;
  1561. + exec->shader_state = temp + exec_size;
  1562. + exec->shader_state_size = args->shader_rec_count;
  1563. +
  1564. + ret = copy_from_user(bin,
  1565. + (void __user *)(uintptr_t)args->bin_cl,
  1566. + args->bin_cl_size);
  1567. + if (ret) {
  1568. + DRM_ERROR("Failed to copy in bin cl\n");
  1569. + goto fail;
  1570. + }
  1571. +
  1572. + ret = copy_from_user(exec->shader_rec_u,
  1573. + (void __user *)(uintptr_t)args->shader_rec,
  1574. + args->shader_rec_size);
  1575. + if (ret) {
  1576. + DRM_ERROR("Failed to copy in shader recs\n");
  1577. + goto fail;
  1578. + }
  1579. +
  1580. + ret = copy_from_user(exec->uniforms_u,
  1581. + (void __user *)(uintptr_t)args->uniforms,
  1582. + args->uniforms_size);
  1583. + if (ret) {
  1584. + DRM_ERROR("Failed to copy in uniforms cl\n");
  1585. + goto fail;
  1586. + }
  1587. +
  1588. + bo = vc4_bo_create(dev, exec_size);
  1589. + if (!bo) {
  1590. + DRM_ERROR("Couldn't allocate BO for binning\n");
  1591. + ret = PTR_ERR(exec->exec_bo);
  1592. + goto fail;
  1593. + }
  1594. + exec->exec_bo = &bo->base;
  1595. +
  1596. + list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
  1597. + &exec->unref_list);
  1598. +
  1599. + exec->ct0ca = exec->exec_bo->paddr + bin_offset;
  1600. +
  1601. + exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
  1602. + exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
  1603. + exec->shader_rec_size = args->shader_rec_size;
  1604. +
  1605. + exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
  1606. + exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
  1607. + exec->uniforms_size = args->uniforms_size;
  1608. +
  1609. + ret = vc4_validate_bin_cl(dev,
  1610. + exec->exec_bo->vaddr + bin_offset,
  1611. + bin,
  1612. + exec);
  1613. + if (ret)
  1614. + goto fail;
  1615. +
  1616. + ret = vc4_validate_shader_recs(dev, exec);
  1617. +
  1618. +fail:
  1619. + kfree(temp);
  1620. + return ret;
  1621. +}
  1622. +
  1623. +static void
  1624. +vc4_complete_exec(struct vc4_exec_info *exec)
  1625. +{
  1626. + unsigned i;
  1627. +
  1628. + if (exec->bo) {
  1629. + for (i = 0; i < exec->bo_count; i++)
  1630. + drm_gem_object_unreference(&exec->bo[i].bo->base);
  1631. + kfree(exec->bo);
  1632. + }
  1633. +
  1634. + while (!list_empty(&exec->unref_list)) {
  1635. + struct vc4_bo *bo = list_first_entry(&exec->unref_list,
  1636. + struct vc4_bo, unref_head);
  1637. + list_del(&bo->unref_head);
  1638. + drm_gem_object_unreference(&bo->base.base);
  1639. + }
  1640. +
  1641. + kfree(exec);
  1642. +}
  1643. +
  1644. +void
  1645. +vc4_job_handle_completed(struct vc4_dev *vc4)
  1646. +{
  1647. + unsigned long irqflags;
  1648. + struct vc4_seqno_cb *cb, *cb_temp;
  1649. +
  1650. + spin_lock_irqsave(&vc4->job_lock, irqflags);
  1651. + while (!list_empty(&vc4->job_done_list)) {
  1652. + struct vc4_exec_info *exec =
  1653. + list_first_entry(&vc4->job_done_list,
  1654. + struct vc4_exec_info, head);
  1655. + list_del(&exec->head);
  1656. +
  1657. + spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  1658. + vc4_complete_exec(exec);
  1659. + spin_lock_irqsave(&vc4->job_lock, irqflags);
  1660. + }
  1661. + spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  1662. +
  1663. + list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
  1664. + if (cb->seqno <= vc4->finished_seqno) {
  1665. + list_del_init(&cb->work.entry);
  1666. + schedule_work(&cb->work);
  1667. + }
  1668. + }
  1669. +}
  1670. +
  1671. +static void vc4_seqno_cb_work(struct work_struct *work)
  1672. +{
  1673. + struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
  1674. + cb->func(cb);
  1675. +}
  1676. +
  1677. +int vc4_queue_seqno_cb(struct drm_device *dev,
  1678. + struct vc4_seqno_cb *cb, uint64_t seqno,
  1679. + void (*func)(struct vc4_seqno_cb *cb))
  1680. +{
  1681. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1682. + int ret = 0;
  1683. +
  1684. + cb->func = func;
  1685. + INIT_WORK(&cb->work, vc4_seqno_cb_work);
  1686. +
  1687. + mutex_lock(&dev->struct_mutex);
  1688. + if (seqno > vc4->finished_seqno) {
  1689. + cb->seqno = seqno;
  1690. + list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
  1691. + } else {
  1692. + schedule_work(&cb->work);
  1693. + }
  1694. + mutex_unlock(&dev->struct_mutex);
  1695. +
  1696. + return ret;
  1697. +}
  1698. +
  1699. +/* Scheduled when any job has been completed, this walks the list of
  1700. + * jobs that had completed and unrefs their BOs and frees their exec
  1701. + * structs.
  1702. + */
  1703. +static void
  1704. +vc4_job_done_work(struct work_struct *work)
  1705. +{
  1706. + struct vc4_dev *vc4 =
  1707. + container_of(work, struct vc4_dev, job_done_work);
  1708. + struct drm_device *dev = vc4->dev;
  1709. +
  1710. + /* Need the struct lock for drm_gem_object_unreference(). */
  1711. + mutex_lock(&dev->struct_mutex);
  1712. + vc4_job_handle_completed(vc4);
  1713. + mutex_unlock(&dev->struct_mutex);
  1714. +}
  1715. +
  1716. +static int
  1717. +vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
  1718. + uint64_t seqno,
  1719. + uint64_t *timeout_ns)
  1720. +{
  1721. + unsigned long start = jiffies;
  1722. + int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
  1723. +
  1724. + if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
  1725. + uint64_t delta = jiffies_to_nsecs(jiffies - start);
  1726. + if (*timeout_ns >= delta)
  1727. + *timeout_ns -= delta;
  1728. + }
  1729. +
  1730. + return ret;
  1731. +}
  1732. +
  1733. +int
  1734. +vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
  1735. + struct drm_file *file_priv)
  1736. +{
  1737. + struct drm_vc4_wait_seqno *args = data;
  1738. +
  1739. + return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
  1740. + &args->timeout_ns);
  1741. +}
  1742. +
  1743. +int
  1744. +vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
  1745. + struct drm_file *file_priv)
  1746. +{
  1747. + int ret;
  1748. + struct drm_vc4_wait_bo *args = data;
  1749. + struct drm_gem_object *gem_obj;
  1750. + struct vc4_bo *bo;
  1751. +
  1752. + gem_obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1753. + if (!gem_obj) {
  1754. + DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
  1755. + return -EINVAL;
  1756. + }
  1757. + bo = to_vc4_bo(gem_obj);
  1758. +
  1759. + ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno, &args->timeout_ns);
  1760. +
  1761. + drm_gem_object_unreference(gem_obj);
  1762. + return ret;
  1763. +}
  1764. +
  1765. +/**
  1766. + * Submits a command list to the VC4.
  1767. + *
  1768. + * This is what is called batchbuffer emitting on other hardware.
  1769. + */
  1770. +int
  1771. +vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
  1772. + struct drm_file *file_priv)
  1773. +{
  1774. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1775. + struct drm_vc4_submit_cl *args = data;
  1776. + struct vc4_exec_info *exec;
  1777. + int ret;
  1778. +
  1779. + if ((args->flags & ~VC4_SUBMIT_CL_USE_CLEAR_COLOR) != 0) {
  1780. + DRM_ERROR("Unknown flags: 0x%02x\n", args->flags);
  1781. + return -EINVAL;
  1782. + }
  1783. +
  1784. + exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
  1785. + if (!exec) {
  1786. + DRM_ERROR("malloc failure on exec struct\n");
  1787. + return -ENOMEM;
  1788. + }
  1789. +
  1790. + exec->args = args;
  1791. + INIT_LIST_HEAD(&exec->unref_list);
  1792. +
  1793. + mutex_lock(&dev->struct_mutex);
  1794. +
  1795. + ret = vc4_cl_lookup_bos(dev, file_priv, exec);
  1796. + if (ret)
  1797. + goto fail;
  1798. +
  1799. + if (exec->args->bin_cl_size != 0) {
  1800. + ret = vc4_get_bcl(dev, exec);
  1801. + if (ret)
  1802. + goto fail;
  1803. + } else {
  1804. + exec->ct0ca = exec->ct0ea = 0;
  1805. + }
  1806. +
  1807. + ret = vc4_get_rcl(dev, exec);
  1808. + if (ret)
  1809. + goto fail;
  1810. +
  1811. + /* Clear this out of the struct we'll be putting in the queue,
  1812. + * since it's part of our stack.
  1813. + */
  1814. + exec->args = NULL;
  1815. +
  1816. + vc4_queue_submit(dev, exec);
  1817. +
  1818. + /* Return the seqno for our job. */
  1819. + args->seqno = vc4->emit_seqno;
  1820. +
  1821. + mutex_unlock(&dev->struct_mutex);
  1822. +
  1823. + return 0;
  1824. +
  1825. +fail:
  1826. + vc4_complete_exec(exec);
  1827. +
  1828. + mutex_unlock(&dev->struct_mutex);
  1829. +
  1830. + return ret;
  1831. +}
  1832. +
  1833. +void
  1834. +vc4_gem_init(struct drm_device *dev)
  1835. +{
  1836. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1837. +
  1838. + INIT_LIST_HEAD(&vc4->job_list);
  1839. + INIT_LIST_HEAD(&vc4->job_done_list);
  1840. + INIT_LIST_HEAD(&vc4->seqno_cb_list);
  1841. + spin_lock_init(&vc4->job_lock);
  1842. +
  1843. + INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
  1844. + setup_timer(&vc4->hangcheck.timer,
  1845. + vc4_hangcheck_elapsed,
  1846. + (unsigned long) dev);
  1847. +
  1848. + INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
  1849. +}
  1850. +
  1851. +void
  1852. +vc4_gem_destroy(struct drm_device *dev)
  1853. +{
  1854. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1855. +
  1856. + /* Waiting for exec to finish would need to be done before
  1857. + * unregistering V3D.
  1858. + */
  1859. + WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
  1860. +
  1861. + /* V3D should already have disabled its interrupt and cleared
  1862. + * the overflow allocation registers. Now free the object.
  1863. + */
  1864. + if (vc4->overflow_mem) {
  1865. + drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  1866. + vc4->overflow_mem = NULL;
  1867. + }
  1868. +
  1869. + vc4_bo_cache_destroy(dev);
  1870. +}
  1871. --- /dev/null
  1872. +++ b/drivers/gpu/drm/vc4/vc4_irq.c
  1873. @@ -0,0 +1,211 @@
  1874. +/*
  1875. + * Copyright © 2014 Broadcom
  1876. + *
  1877. + * Permission is hereby granted, free of charge, to any person obtaining a
  1878. + * copy of this software and associated documentation files (the "Software"),
  1879. + * to deal in the Software without restriction, including without limitation
  1880. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  1881. + * and/or sell copies of the Software, and to permit persons to whom the
  1882. + * Software is furnished to do so, subject to the following conditions:
  1883. + *
  1884. + * The above copyright notice and this permission notice (including the next
  1885. + * paragraph) shall be included in all copies or substantial portions of the
  1886. + * Software.
  1887. + *
  1888. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1889. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1890. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  1891. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1892. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1893. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1894. + * IN THE SOFTWARE.
  1895. + */
  1896. +
  1897. +/** DOC: Interrupt management for the V3D engine.
  1898. + *
  1899. + * We have an interrupt status register (V3D_INTCTL) which reports
  1900. + * interrupts, and where writing 1 bits clears those interrupts.
  1901. + * There are also a pair of interrupt registers
  1902. + * (V3D_INTENA/V3D_INTDIS) where writing a 1 to their bits enables or
  1903. + * disables that specific interrupt, and 0s written are ignored
  1904. + * (reading either one returns the set of enabled interrupts).
  1905. + *
  1906. + * When we take a render frame interrupt, we need to wake the
  1907. + * processes waiting for some frame to be done, and get the next frame
  1908. + * submitted ASAP (so the hardware doesn't sit idle when there's work
  1909. + * to do).
  1910. + *
  1911. + * When we take the binner out of memory interrupt, we need to
  1912. + * allocate some new memory and pass it to the binner so that the
  1913. + * current job can make progress.
  1914. + */
  1915. +
  1916. +#include "vc4_drv.h"
  1917. +#include "vc4_regs.h"
  1918. +
  1919. +#define V3D_DRIVER_IRQS (V3D_INT_OUTOMEM | \
  1920. + V3D_INT_FRDONE)
  1921. +
  1922. +DECLARE_WAIT_QUEUE_HEAD(render_wait);
  1923. +
  1924. +static void
  1925. +vc4_overflow_mem_work(struct work_struct *work)
  1926. +{
  1927. + struct vc4_dev *vc4 =
  1928. + container_of(work, struct vc4_dev, overflow_mem_work);
  1929. + struct drm_device *dev = vc4->dev;
  1930. + struct vc4_bo *bo;
  1931. +
  1932. + bo = vc4_bo_create(dev, 256 * 1024);
  1933. + if (!bo) {
  1934. + DRM_ERROR("Couldn't allocate binner overflow mem\n");
  1935. + return;
  1936. + }
  1937. +
  1938. + /* If there's a job executing currently, then our previous
  1939. + * overflow allocation is getting used in that job and we need
  1940. + * to queue it to be released when the job is done. But if no
  1941. + * job is executing at all, then we can free the old overflow
  1942. + * object direcctly.
  1943. + *
  1944. + * No lock necessary for this pointer since we're the only
  1945. + * ones that update the pointer, and our workqueue won't
  1946. + * reenter.
  1947. + */
  1948. + if (vc4->overflow_mem) {
  1949. + struct vc4_exec_info *current_exec;
  1950. + unsigned long irqflags;
  1951. +
  1952. + spin_lock_irqsave(&vc4->job_lock, irqflags);
  1953. + current_exec = vc4_first_job(vc4);
  1954. + if (current_exec) {
  1955. + vc4->overflow_mem->seqno = vc4->finished_seqno + 1;
  1956. + list_add_tail(&vc4->overflow_mem->unref_head,
  1957. + &current_exec->unref_list);
  1958. + vc4->overflow_mem = NULL;
  1959. + }
  1960. + spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  1961. + }
  1962. +
  1963. + if (vc4->overflow_mem) {
  1964. + drm_gem_object_unreference_unlocked(&vc4->overflow_mem->base.base);
  1965. + }
  1966. + vc4->overflow_mem = bo;
  1967. +
  1968. + V3D_WRITE(V3D_BPOA, bo->base.paddr);
  1969. + V3D_WRITE(V3D_BPOS, bo->base.base.size);
  1970. + V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
  1971. + V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
  1972. +}
  1973. +
  1974. +static void
  1975. +vc4_irq_finish_job(struct drm_device *dev)
  1976. +{
  1977. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1978. + struct vc4_exec_info *exec = vc4_first_job(vc4);
  1979. +
  1980. + if (!exec)
  1981. + return;
  1982. +
  1983. + vc4->finished_seqno++;
  1984. + list_move_tail(&exec->head, &vc4->job_done_list);
  1985. + vc4_submit_next_job(dev);
  1986. +
  1987. + wake_up_all(&vc4->job_wait_queue);
  1988. + schedule_work(&vc4->job_done_work);
  1989. +}
  1990. +
  1991. +irqreturn_t
  1992. +vc4_irq(int irq, void *arg)
  1993. +{
  1994. + struct drm_device *dev = arg;
  1995. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  1996. + uint32_t intctl;
  1997. + irqreturn_t status = IRQ_NONE;
  1998. +
  1999. + barrier();
  2000. + intctl = V3D_READ(V3D_INTCTL);
  2001. +
  2002. + /* Acknowledge the interrupts we're handling here. The render
  2003. + * frame done interrupt will be cleared, while OUTOMEM will
  2004. + * stay high until the underlying cause is cleared.
  2005. + */
  2006. + V3D_WRITE(V3D_INTCTL, intctl);
  2007. +
  2008. + if (intctl & V3D_INT_OUTOMEM) {
  2009. + /* Disable OUTOMEM until the work is done. */
  2010. + V3D_WRITE(V3D_INTDIS, V3D_INT_OUTOMEM);
  2011. + schedule_work(&vc4->overflow_mem_work);
  2012. + status = IRQ_HANDLED;
  2013. + }
  2014. +
  2015. + if (intctl & V3D_INT_FRDONE) {
  2016. + spin_lock(&vc4->job_lock);
  2017. + vc4_irq_finish_job(dev);
  2018. + spin_unlock(&vc4->job_lock);
  2019. + status = IRQ_HANDLED;
  2020. + }
  2021. +
  2022. + return status;
  2023. +}
  2024. +
  2025. +void
  2026. +vc4_irq_preinstall(struct drm_device *dev)
  2027. +{
  2028. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  2029. +
  2030. + init_waitqueue_head(&vc4->job_wait_queue);
  2031. + INIT_WORK(&vc4->overflow_mem_work, vc4_overflow_mem_work);
  2032. +
  2033. + /* Clear any pending interrupts someone might have left around
  2034. + * for us.
  2035. + */
  2036. + V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  2037. +}
  2038. +
  2039. +int
  2040. +vc4_irq_postinstall(struct drm_device *dev)
  2041. +{
  2042. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  2043. +
  2044. + /* Enable both the render done and out of memory interrupts. */
  2045. + V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  2046. +
  2047. + return 0;
  2048. +}
  2049. +
  2050. +void
  2051. +vc4_irq_uninstall(struct drm_device *dev)
  2052. +{
  2053. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  2054. +
  2055. + /* Disable sending interrupts for our driver's IRQs. */
  2056. + V3D_WRITE(V3D_INTDIS, V3D_DRIVER_IRQS);
  2057. +
  2058. + /* Clear any pending interrupts we might have left. */
  2059. + V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  2060. +
  2061. + cancel_work_sync(&vc4->overflow_mem_work);
  2062. +}
  2063. +
  2064. +/** Reinitializes interrupt registers when a GPU reset is performed. */
  2065. +void vc4_irq_reset(struct drm_device *dev)
  2066. +{
  2067. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  2068. + unsigned long irqflags;
  2069. +
  2070. + /* Acknowledge any stale IRQs. */
  2071. + V3D_WRITE(V3D_INTCTL, V3D_DRIVER_IRQS);
  2072. +
  2073. + /*
  2074. + * Turn all our interrupts on. Binner out of memory is the
  2075. + * only one we expect to trigger at this point, since we've
  2076. + * just come from poweron and haven't supplied any overflow
  2077. + * memory yet.
  2078. + */
  2079. + V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
  2080. +
  2081. + spin_lock_irqsave(&vc4->job_lock, irqflags);
  2082. + vc4_irq_finish_job(dev);
  2083. + spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  2084. +}
  2085. --- a/drivers/gpu/drm/vc4/vc4_kms.c
  2086. +++ b/drivers/gpu/drm/vc4/vc4_kms.c
  2087. @@ -15,6 +15,7 @@
  2088. */
  2089. #include "drm_crtc.h"
  2090. +#include "drm_atomic.h"
  2091. #include "drm_atomic_helper.h"
  2092. #include "drm_crtc_helper.h"
  2093. #include "drm_plane_helper.h"
  2094. @@ -29,10 +30,151 @@ static void vc4_output_poll_changed(stru
  2095. drm_fbdev_cma_hotplug_event(vc4->fbdev);
  2096. }
  2097. +struct vc4_commit {
  2098. + struct drm_device *dev;
  2099. + struct drm_atomic_state *state;
  2100. + struct vc4_seqno_cb cb;
  2101. +};
  2102. +
  2103. +static void
  2104. +vc4_atomic_complete_commit(struct vc4_commit *c)
  2105. +{
  2106. + struct drm_atomic_state *state = c->state;
  2107. + struct drm_device *dev = state->dev;
  2108. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  2109. +
  2110. + drm_atomic_helper_commit_modeset_disables(dev, state);
  2111. +
  2112. + drm_atomic_helper_commit_planes(dev, state);
  2113. +
  2114. + drm_atomic_helper_commit_modeset_enables(dev, state);
  2115. +
  2116. + drm_atomic_helper_wait_for_vblanks(dev, state);
  2117. +
  2118. + drm_atomic_helper_cleanup_planes(dev, state);
  2119. +
  2120. + drm_atomic_state_free(state);
  2121. +
  2122. + up(&vc4->async_modeset);
  2123. +
  2124. + kfree(c);
  2125. +}
  2126. +
  2127. +static void
  2128. +vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
  2129. +{
  2130. + struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
  2131. +
  2132. + vc4_atomic_complete_commit(c);
  2133. +}
  2134. +
  2135. +static struct vc4_commit *commit_init(struct drm_atomic_state *state)
  2136. +{
  2137. + struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
  2138. +
  2139. + if (!c)
  2140. + return NULL;
  2141. + c->dev = state->dev;
  2142. + c->state = state;
  2143. +
  2144. + return c;
  2145. +}
  2146. +
  2147. +/**
  2148. + * vc4_atomic_commit - commit validated state object
  2149. + * @dev: DRM device
  2150. + * @state: the driver state object
  2151. + * @async: asynchronous commit
  2152. + *
  2153. + * This function commits a with drm_atomic_helper_check() pre-validated state
  2154. + * object. This can still fail when e.g. the framebuffer reservation fails. For
  2155. + * now this doesn't implement asynchronous commits.
  2156. + *
  2157. + * RETURNS
  2158. + * Zero for success or -errno.
  2159. + */
  2160. +static int vc4_atomic_commit(struct drm_device *dev,
  2161. + struct drm_atomic_state *state,
  2162. + bool async)
  2163. +{
  2164. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  2165. + int ret;
  2166. + int i;
  2167. + uint64_t wait_seqno = 0;
  2168. + struct vc4_commit *c;
  2169. +
  2170. + c = commit_init(state);
  2171. + if (!c)
  2172. + return -ENOMEM;
  2173. +
  2174. + /* Make sure that any outstanding modesets have finished. */
  2175. + ret = down_interruptible(&vc4->async_modeset);
  2176. + if (ret) {
  2177. + kfree(c);
  2178. + return ret;
  2179. + }
  2180. +
  2181. + ret = drm_atomic_helper_prepare_planes(dev, state);
  2182. + if (ret) {
  2183. + kfree(c);
  2184. + up(&vc4->async_modeset);
  2185. + return ret;
  2186. + }
  2187. +
  2188. + for (i = 0; i < dev->mode_config.num_total_plane; i++) {
  2189. + struct drm_plane *plane = state->planes[i];
  2190. + struct drm_plane_state *new_state = state->plane_states[i];
  2191. +
  2192. + if (!plane)
  2193. + continue;
  2194. +
  2195. + if ((plane->state->fb != new_state->fb) && new_state->fb) {
  2196. + struct drm_gem_cma_object *cma_bo =
  2197. + drm_fb_cma_get_gem_obj(new_state->fb, 0);
  2198. + struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  2199. + wait_seqno = max(bo->seqno, wait_seqno);
  2200. + }
  2201. + }
  2202. +
  2203. + /*
  2204. + * This is the point of no return - everything below never fails except
  2205. + * when the hw goes bonghits. Which means we can commit the new state on
  2206. + * the software side now.
  2207. + */
  2208. +
  2209. + drm_atomic_helper_swap_state(dev, state);
  2210. +
  2211. + /*
  2212. + * Everything below can be run asynchronously without the need to grab
  2213. + * any modeset locks at all under one condition: It must be guaranteed
  2214. + * that the asynchronous work has either been cancelled (if the driver
  2215. + * supports it, which at least requires that the framebuffers get
  2216. + * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  2217. + * before the new state gets committed on the software side with
  2218. + * drm_atomic_helper_swap_state().
  2219. + *
  2220. + * This scheme allows new atomic state updates to be prepared and
  2221. + * checked in parallel to the asynchronous completion of the previous
  2222. + * update. Which is important since compositors need to figure out the
  2223. + * composition of the next frame right after having submitted the
  2224. + * current layout.
  2225. + */
  2226. +
  2227. + if (async) {
  2228. + vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
  2229. + vc4_atomic_complete_commit_seqno_cb);
  2230. + } else {
  2231. + vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
  2232. + vc4_atomic_complete_commit(c);
  2233. + }
  2234. +
  2235. + return 0;
  2236. +}
  2237. +
  2238. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  2239. .output_poll_changed = vc4_output_poll_changed,
  2240. .atomic_check = drm_atomic_helper_check,
  2241. - .atomic_commit = drm_atomic_helper_commit,
  2242. + .atomic_commit = vc4_atomic_commit,
  2243. .fb_create = drm_fb_cma_create,
  2244. };
  2245. @@ -41,6 +183,8 @@ int vc4_kms_load(struct drm_device *dev)
  2246. struct vc4_dev *vc4 = to_vc4_dev(dev);
  2247. int ret;
  2248. + sema_init(&vc4->async_modeset, 1);
  2249. +
  2250. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  2251. if (ret < 0) {
  2252. dev_err(dev->dev, "failed to initialize vblank\n");
  2253. @@ -51,6 +195,8 @@ int vc4_kms_load(struct drm_device *dev)
  2254. dev->mode_config.max_height = 2048;
  2255. dev->mode_config.funcs = &vc4_mode_funcs;
  2256. dev->mode_config.preferred_depth = 24;
  2257. + dev->mode_config.async_page_flip = true;
  2258. +
  2259. dev->vblank_disable_allowed = true;
  2260. drm_mode_config_reset(dev);
  2261. --- /dev/null
  2262. +++ b/drivers/gpu/drm/vc4/vc4_packet.h
  2263. @@ -0,0 +1,384 @@
  2264. +/*
  2265. + * Copyright © 2014 Broadcom
  2266. + *
  2267. + * Permission is hereby granted, free of charge, to any person obtaining a
  2268. + * copy of this software and associated documentation files (the "Software"),
  2269. + * to deal in the Software without restriction, including without limitation
  2270. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  2271. + * and/or sell copies of the Software, and to permit persons to whom the
  2272. + * Software is furnished to do so, subject to the following conditions:
  2273. + *
  2274. + * The above copyright notice and this permission notice (including the next
  2275. + * paragraph) shall be included in all copies or substantial portions of the
  2276. + * Software.
  2277. + *
  2278. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2279. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2280. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  2281. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2282. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2283. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2284. + * IN THE SOFTWARE.
  2285. + */
  2286. +
  2287. +#ifndef VC4_PACKET_H
  2288. +#define VC4_PACKET_H
  2289. +
  2290. +#include "vc4_regs.h" /* for VC4_MASK, VC4_GET_FIELD, VC4_SET_FIELD */
  2291. +
  2292. +enum vc4_packet {
  2293. + VC4_PACKET_HALT = 0,
  2294. + VC4_PACKET_NOP = 1,
  2295. +
  2296. + VC4_PACKET_FLUSH = 4,
  2297. + VC4_PACKET_FLUSH_ALL = 5,
  2298. + VC4_PACKET_START_TILE_BINNING = 6,
  2299. + VC4_PACKET_INCREMENT_SEMAPHORE = 7,
  2300. + VC4_PACKET_WAIT_ON_SEMAPHORE = 8,
  2301. +
  2302. + VC4_PACKET_BRANCH = 16,
  2303. + VC4_PACKET_BRANCH_TO_SUB_LIST = 17,
  2304. +
  2305. + VC4_PACKET_STORE_MS_TILE_BUFFER = 24,
  2306. + VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF = 25,
  2307. + VC4_PACKET_STORE_FULL_RES_TILE_BUFFER = 26,
  2308. + VC4_PACKET_LOAD_FULL_RES_TILE_BUFFER = 27,
  2309. + VC4_PACKET_STORE_TILE_BUFFER_GENERAL = 28,
  2310. + VC4_PACKET_LOAD_TILE_BUFFER_GENERAL = 29,
  2311. +
  2312. + VC4_PACKET_GL_INDEXED_PRIMITIVE = 32,
  2313. + VC4_PACKET_GL_ARRAY_PRIMITIVE = 33,
  2314. +
  2315. + VC4_PACKET_COMPRESSED_PRIMITIVE = 48,
  2316. + VC4_PACKET_CLIPPED_COMPRESSED_PRIMITIVE = 49,
  2317. +
  2318. + VC4_PACKET_PRIMITIVE_LIST_FORMAT = 56,
  2319. +
  2320. + VC4_PACKET_GL_SHADER_STATE = 64,
  2321. + VC4_PACKET_NV_SHADER_STATE = 65,
  2322. + VC4_PACKET_VG_SHADER_STATE = 66,
  2323. +
  2324. + VC4_PACKET_CONFIGURATION_BITS = 96,
  2325. + VC4_PACKET_FLAT_SHADE_FLAGS = 97,
  2326. + VC4_PACKET_POINT_SIZE = 98,
  2327. + VC4_PACKET_LINE_WIDTH = 99,
  2328. + VC4_PACKET_RHT_X_BOUNDARY = 100,
  2329. + VC4_PACKET_DEPTH_OFFSET = 101,
  2330. + VC4_PACKET_CLIP_WINDOW = 102,
  2331. + VC4_PACKET_VIEWPORT_OFFSET = 103,
  2332. + VC4_PACKET_Z_CLIPPING = 104,
  2333. + VC4_PACKET_CLIPPER_XY_SCALING = 105,
  2334. + VC4_PACKET_CLIPPER_Z_SCALING = 106,
  2335. +
  2336. + VC4_PACKET_TILE_BINNING_MODE_CONFIG = 112,
  2337. + VC4_PACKET_TILE_RENDERING_MODE_CONFIG = 113,
  2338. + VC4_PACKET_CLEAR_COLORS = 114,
  2339. + VC4_PACKET_TILE_COORDINATES = 115,
  2340. +
  2341. + /* Not an actual hardware packet -- this is what we use to put
  2342. + * references to GEM bos in the command stream, since we need the u32
  2343. + * int the actual address packet in order to store the offset from the
  2344. + * start of the BO.
  2345. + */
  2346. + VC4_PACKET_GEM_HANDLES = 254,
  2347. +} __attribute__ ((__packed__));
  2348. +
  2349. +#define VC4_PACKET_HALT_SIZE 1
  2350. +#define VC4_PACKET_NOP_SIZE 1
  2351. +#define VC4_PACKET_FLUSH_SIZE 1
  2352. +#define VC4_PACKET_FLUSH_ALL_SIZE 1
  2353. +#define VC4_PACKET_START_TILE_BINNING_SIZE 1
  2354. +#define VC4_PACKET_INCREMENT_SEMAPHORE_SIZE 1
  2355. +#define VC4_PACKET_WAIT_ON_SEMAPHORE_SIZE 1
  2356. +#define VC4_PACKET_BRANCH_SIZE 5
  2357. +#define VC4_PACKET_BRANCH_TO_SUB_LIST_SIZE 5
  2358. +#define VC4_PACKET_STORE_MS_TILE_BUFFER_SIZE 1
  2359. +#define VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF_SIZE 1
  2360. +#define VC4_PACKET_STORE_FULL_RES_TILE_BUFFER_SIZE 5
  2361. +#define VC4_PACKET_LOAD_FULL_RES_TILE_BUFFER_SIZE 5
  2362. +#define VC4_PACKET_STORE_TILE_BUFFER_GENERAL_SIZE 7
  2363. +#define VC4_PACKET_LOAD_TILE_BUFFER_GENERAL_SIZE 7
  2364. +#define VC4_PACKET_GL_INDEXED_PRIMITIVE_SIZE 14
  2365. +#define VC4_PACKET_GL_ARRAY_PRIMITIVE_SIZE 10
  2366. +#define VC4_PACKET_COMPRESSED_PRIMITIVE_SIZE 1
  2367. +#define VC4_PACKET_CLIPPED_COMPRESSED_PRIMITIVE_SIZE 1
  2368. +#define VC4_PACKET_PRIMITIVE_LIST_FORMAT_SIZE 2
  2369. +#define VC4_PACKET_GL_SHADER_STATE_SIZE 5
  2370. +#define VC4_PACKET_NV_SHADER_STATE_SIZE 5
  2371. +#define VC4_PACKET_VG_SHADER_STATE_SIZE 5
  2372. +#define VC4_PACKET_CONFIGURATION_BITS_SIZE 4
  2373. +#define VC4_PACKET_FLAT_SHADE_FLAGS_SIZE 5
  2374. +#define VC4_PACKET_POINT_SIZE_SIZE 5
  2375. +#define VC4_PACKET_LINE_WIDTH_SIZE 5
  2376. +#define VC4_PACKET_RHT_X_BOUNDARY_SIZE 3
  2377. +#define VC4_PACKET_DEPTH_OFFSET_SIZE 5
  2378. +#define VC4_PACKET_CLIP_WINDOW_SIZE 9
  2379. +#define VC4_PACKET_VIEWPORT_OFFSET_SIZE 5
  2380. +#define VC4_PACKET_Z_CLIPPING_SIZE 9
  2381. +#define VC4_PACKET_CLIPPER_XY_SCALING_SIZE 9
  2382. +#define VC4_PACKET_CLIPPER_Z_SCALING_SIZE 9
  2383. +#define VC4_PACKET_TILE_BINNING_MODE_CONFIG_SIZE 16
  2384. +#define VC4_PACKET_TILE_RENDERING_MODE_CONFIG_SIZE 11
  2385. +#define VC4_PACKET_CLEAR_COLORS_SIZE 14
  2386. +#define VC4_PACKET_TILE_COORDINATES_SIZE 3
  2387. +#define VC4_PACKET_GEM_HANDLES_SIZE 9
  2388. +
  2389. +/** @{
  2390. + * Bits used by packets like VC4_PACKET_STORE_TILE_BUFFER_GENERAL and
  2391. + * VC4_PACKET_TILE_RENDERING_MODE_CONFIG.
  2392. +*/
  2393. +#define VC4_TILING_FORMAT_LINEAR 0
  2394. +#define VC4_TILING_FORMAT_T 1
  2395. +#define VC4_TILING_FORMAT_LT 2
  2396. +/** @} */
  2397. +
  2398. +/** @{
  2399. + *
  2400. + * low bits of VC4_PACKET_STORE_FULL_RES_TILE_BUFFER and
  2401. + * VC4_PACKET_LOAD_FULL_RES_TILE_BUFFER.
  2402. + */
  2403. +#define VC4_LOADSTORE_FULL_RES_EOF (1 << 3)
  2404. +#define VC4_LOADSTORE_FULL_RES_DISABLE_CLEAR_ALL (1 << 2)
  2405. +#define VC4_LOADSTORE_FULL_RES_DISABLE_ZS (1 << 1)
  2406. +#define VC4_LOADSTORE_FULL_RES_DISABLE_COLOR (1 << 0)
  2407. +
  2408. +/** @{
  2409. + *
  2410. + * byte 2 of VC4_PACKET_STORE_TILE_BUFFER_GENERAL and
  2411. + * VC4_PACKET_LOAD_TILE_BUFFER_GENERAL (low bits of the address)
  2412. + */
  2413. +
  2414. +#define VC4_LOADSTORE_TILE_BUFFER_EOF (1 << 3)
  2415. +#define VC4_LOADSTORE_TILE_BUFFER_DISABLE_FULL_VG_MASK (1 << 2)
  2416. +#define VC4_LOADSTORE_TILE_BUFFER_DISABLE_FULL_ZS (1 << 1)
  2417. +#define VC4_LOADSTORE_TILE_BUFFER_DISABLE_FULL_COLOR (1 << 0)
  2418. +
  2419. +/** @} */
  2420. +
  2421. +/** @{
  2422. + *
  2423. + * byte 0-1 of VC4_PACKET_STORE_TILE_BUFFER_GENERAL and
  2424. + * VC4_PACKET_LOAD_TILE_BUFFER_GENERAL
  2425. + */
  2426. +#define VC4_STORE_TILE_BUFFER_DISABLE_VG_MASK_CLEAR (1 << 15)
  2427. +#define VC4_STORE_TILE_BUFFER_DISABLE_ZS_CLEAR (1 << 14)
  2428. +#define VC4_STORE_TILE_BUFFER_DISABLE_COLOR_CLEAR (1 << 13)
  2429. +#define VC4_STORE_TILE_BUFFER_DISABLE_SWAP (1 << 12)
  2430. +
  2431. +#define VC4_LOADSTORE_TILE_BUFFER_FORMAT_MASK VC4_MASK(9, 8)
  2432. +#define VC4_LOADSTORE_TILE_BUFFER_FORMAT_SHIFT 8
  2433. +#define VC4_LOADSTORE_TILE_BUFFER_RGBA8888 0
  2434. +#define VC4_LOADSTORE_TILE_BUFFER_BGR565_DITHER 1
  2435. +#define VC4_LOADSTORE_TILE_BUFFER_BGR565 2
  2436. +/** @} */
  2437. +
  2438. +/** @{
  2439. + *
  2440. + * byte 0 of VC4_PACKET_STORE_TILE_BUFFER_GENERAL and
  2441. + * VC4_PACKET_LOAD_TILE_BUFFER_GENERAL
  2442. + */
  2443. +#define VC4_STORE_TILE_BUFFER_MODE_MASK VC4_MASK(7, 6)
  2444. +#define VC4_STORE_TILE_BUFFER_MODE_SHIFT 6
  2445. +#define VC4_STORE_TILE_BUFFER_MODE_SAMPLE0 (0 << 6)
  2446. +#define VC4_STORE_TILE_BUFFER_MODE_DECIMATE_X4 (1 << 6)
  2447. +#define VC4_STORE_TILE_BUFFER_MODE_DECIMATE_X16 (2 << 6)
  2448. +
  2449. +/** The values of the field are VC4_TILING_FORMAT_* */
  2450. +#define VC4_LOADSTORE_TILE_BUFFER_TILING_MASK VC4_MASK(5, 4)
  2451. +#define VC4_LOADSTORE_TILE_BUFFER_TILING_SHIFT 4
  2452. +
  2453. +#define VC4_LOADSTORE_TILE_BUFFER_BUFFER_MASK VC4_MASK(2, 0)
  2454. +#define VC4_LOADSTORE_TILE_BUFFER_BUFFER_SHIFT 0
  2455. +#define VC4_LOADSTORE_TILE_BUFFER_NONE 0
  2456. +#define VC4_LOADSTORE_TILE_BUFFER_COLOR 1
  2457. +#define VC4_LOADSTORE_TILE_BUFFER_ZS 2
  2458. +#define VC4_LOADSTORE_TILE_BUFFER_Z 3
  2459. +#define VC4_LOADSTORE_TILE_BUFFER_VG_MASK 4
  2460. +#define VC4_LOADSTORE_TILE_BUFFER_FULL 5
  2461. +/** @} */
  2462. +
  2463. +#define VC4_INDEX_BUFFER_U8 (0 << 4)
  2464. +#define VC4_INDEX_BUFFER_U16 (1 << 4)
  2465. +
  2466. +/* This flag is only present in NV shader state. */
  2467. +#define VC4_SHADER_FLAG_SHADED_CLIP_COORDS (1 << 3)
  2468. +#define VC4_SHADER_FLAG_ENABLE_CLIPPING (1 << 2)
  2469. +#define VC4_SHADER_FLAG_VS_POINT_SIZE (1 << 1)
  2470. +#define VC4_SHADER_FLAG_FS_SINGLE_THREAD (1 << 0)
  2471. +
  2472. +/** @{ byte 2 of config bits. */
  2473. +#define VC4_CONFIG_BITS_EARLY_Z_UPDATE (1 << 1)
  2474. +#define VC4_CONFIG_BITS_EARLY_Z (1 << 0)
  2475. +/** @} */
  2476. +
  2477. +/** @{ byte 1 of config bits. */
  2478. +#define VC4_CONFIG_BITS_Z_UPDATE (1 << 7)
  2479. +/** same values in this 3-bit field as PIPE_FUNC_* */
  2480. +#define VC4_CONFIG_BITS_DEPTH_FUNC_SHIFT 4
  2481. +#define VC4_CONFIG_BITS_COVERAGE_READ_LEAVE (1 << 3)
  2482. +
  2483. +#define VC4_CONFIG_BITS_COVERAGE_UPDATE_NONZERO (0 << 1)
  2484. +#define VC4_CONFIG_BITS_COVERAGE_UPDATE_ODD (1 << 1)
  2485. +#define VC4_CONFIG_BITS_COVERAGE_UPDATE_OR (2 << 1)
  2486. +#define VC4_CONFIG_BITS_COVERAGE_UPDATE_ZERO (3 << 1)
  2487. +
  2488. +#define VC4_CONFIG_BITS_COVERAGE_PIPE_SELECT (1 << 0)
  2489. +/** @} */
  2490. +
  2491. +/** @{ byte 0 of config bits. */
  2492. +#define VC4_CONFIG_BITS_RASTERIZER_OVERSAMPLE_NONE (0 << 6)
  2493. +#define VC4_CONFIG_BITS_RASTERIZER_OVERSAMPLE_4X (1 << 6)
  2494. +#define VC4_CONFIG_BITS_RASTERIZER_OVERSAMPLE_16X (2 << 6)
  2495. +
  2496. +#define VC4_CONFIG_BITS_AA_POINTS_AND_LINES (1 << 4)
  2497. +#define VC4_CONFIG_BITS_ENABLE_DEPTH_OFFSET (1 << 3)
  2498. +#define VC4_CONFIG_BITS_CW_PRIMITIVES (1 << 2)
  2499. +#define VC4_CONFIG_BITS_ENABLE_PRIM_BACK (1 << 1)
  2500. +#define VC4_CONFIG_BITS_ENABLE_PRIM_FRONT (1 << 0)
  2501. +/** @} */
  2502. +
  2503. +/** @{ bits in the last u8 of VC4_PACKET_TILE_BINNING_MODE_CONFIG */
  2504. +#define VC4_BIN_CONFIG_DB_NON_MS (1 << 7)
  2505. +
  2506. +#define VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_MASK VC4_MASK(6, 5)
  2507. +#define VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_SHIFT 5
  2508. +#define VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_32 0
  2509. +#define VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_64 1
  2510. +#define VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_128 2
  2511. +#define VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_256 3
  2512. +
  2513. +#define VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_MASK VC4_MASK(4, 3)
  2514. +#define VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_SHIFT 3
  2515. +#define VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_32 0
  2516. +#define VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_64 1
  2517. +#define VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_128 2
  2518. +#define VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_256 3
  2519. +
  2520. +#define VC4_BIN_CONFIG_AUTO_INIT_TSDA (1 << 2)
  2521. +#define VC4_BIN_CONFIG_TILE_BUFFER_64BIT (1 << 1)
  2522. +#define VC4_BIN_CONFIG_MS_MODE_4X (1 << 0)
  2523. +/** @} */
  2524. +
  2525. +/** @{ bits in the last u16 of VC4_PACKET_TILE_RENDERING_MODE_CONFIG */
  2526. +#define VC4_RENDER_CONFIG_DB_NON_MS (1 << 12)
  2527. +#define VC4_RENDER_CONFIG_EARLY_Z_COVERAGE_DISABLE (1 << 11)
  2528. +#define VC4_RENDER_CONFIG_EARLY_Z_DIRECTION_G (1 << 10)
  2529. +#define VC4_RENDER_CONFIG_COVERAGE_MODE (1 << 9)
  2530. +#define VC4_RENDER_CONFIG_ENABLE_VG_MASK (1 << 8)
  2531. +
  2532. +/** The values of the field are VC4_TILING_FORMAT_* */
  2533. +#define VC4_RENDER_CONFIG_MEMORY_FORMAT_MASK VC4_MASK(7, 6)
  2534. +#define VC4_RENDER_CONFIG_MEMORY_FORMAT_SHIFT 6
  2535. +
  2536. +#define VC4_RENDER_CONFIG_DECIMATE_MODE_1X (0 << 4)
  2537. +#define VC4_RENDER_CONFIG_DECIMATE_MODE_4X (1 << 4)
  2538. +#define VC4_RENDER_CONFIG_DECIMATE_MODE_16X (2 << 4)
  2539. +
  2540. +#define VC4_RENDER_CONFIG_FORMAT_MASK VC4_MASK(3, 2)
  2541. +#define VC4_RENDER_CONFIG_FORMAT_SHIFT 2
  2542. +#define VC4_RENDER_CONFIG_FORMAT_BGR565_DITHERED 0
  2543. +#define VC4_RENDER_CONFIG_FORMAT_RGBA8888 1
  2544. +#define VC4_RENDER_CONFIG_FORMAT_BGR565 2
  2545. +
  2546. +#define VC4_RENDER_CONFIG_TILE_BUFFER_64BIT (1 << 1)
  2547. +#define VC4_RENDER_CONFIG_MS_MODE_4X (1 << 0)
  2548. +
  2549. +#define VC4_PRIMITIVE_LIST_FORMAT_16_INDEX (1 << 4)
  2550. +#define VC4_PRIMITIVE_LIST_FORMAT_32_XY (3 << 4)
  2551. +#define VC4_PRIMITIVE_LIST_FORMAT_TYPE_POINTS (0 << 0)
  2552. +#define VC4_PRIMITIVE_LIST_FORMAT_TYPE_LINES (1 << 0)
  2553. +#define VC4_PRIMITIVE_LIST_FORMAT_TYPE_TRIANGLES (2 << 0)
  2554. +#define VC4_PRIMITIVE_LIST_FORMAT_TYPE_RHT (3 << 0)
  2555. +
  2556. +enum vc4_texture_data_type {
  2557. + VC4_TEXTURE_TYPE_RGBA8888 = 0,
  2558. + VC4_TEXTURE_TYPE_RGBX8888 = 1,
  2559. + VC4_TEXTURE_TYPE_RGBA4444 = 2,
  2560. + VC4_TEXTURE_TYPE_RGBA5551 = 3,
  2561. + VC4_TEXTURE_TYPE_RGB565 = 4,
  2562. + VC4_TEXTURE_TYPE_LUMINANCE = 5,
  2563. + VC4_TEXTURE_TYPE_ALPHA = 6,
  2564. + VC4_TEXTURE_TYPE_LUMALPHA = 7,
  2565. + VC4_TEXTURE_TYPE_ETC1 = 8,
  2566. + VC4_TEXTURE_TYPE_S16F = 9,
  2567. + VC4_TEXTURE_TYPE_S8 = 10,
  2568. + VC4_TEXTURE_TYPE_S16 = 11,
  2569. + VC4_TEXTURE_TYPE_BW1 = 12,
  2570. + VC4_TEXTURE_TYPE_A4 = 13,
  2571. + VC4_TEXTURE_TYPE_A1 = 14,
  2572. + VC4_TEXTURE_TYPE_RGBA64 = 15,
  2573. + VC4_TEXTURE_TYPE_RGBA32R = 16,
  2574. + VC4_TEXTURE_TYPE_YUV422R = 17,
  2575. +};
  2576. +
  2577. +#define VC4_TEX_P0_OFFSET_MASK VC4_MASK(31, 12)
  2578. +#define VC4_TEX_P0_OFFSET_SHIFT 12
  2579. +#define VC4_TEX_P0_CSWIZ_MASK VC4_MASK(11, 10)
  2580. +#define VC4_TEX_P0_CSWIZ_SHIFT 10
  2581. +#define VC4_TEX_P0_CMMODE_MASK VC4_MASK(9, 9)
  2582. +#define VC4_TEX_P0_CMMODE_SHIFT 9
  2583. +#define VC4_TEX_P0_FLIPY_MASK VC4_MASK(8, 8)
  2584. +#define VC4_TEX_P0_FLIPY_SHIFT 8
  2585. +#define VC4_TEX_P0_TYPE_MASK VC4_MASK(7, 4)
  2586. +#define VC4_TEX_P0_TYPE_SHIFT 4
  2587. +#define VC4_TEX_P0_MIPLVLS_MASK VC4_MASK(3, 0)
  2588. +#define VC4_TEX_P0_MIPLVLS_SHIFT 0
  2589. +
  2590. +#define VC4_TEX_P1_TYPE4_MASK VC4_MASK(31, 31)
  2591. +#define VC4_TEX_P1_TYPE4_SHIFT 31
  2592. +#define VC4_TEX_P1_HEIGHT_MASK VC4_MASK(30, 20)
  2593. +#define VC4_TEX_P1_HEIGHT_SHIFT 20
  2594. +#define VC4_TEX_P1_ETCFLIP_MASK VC4_MASK(19, 19)
  2595. +#define VC4_TEX_P1_ETCFLIP_SHIFT 19
  2596. +#define VC4_TEX_P1_WIDTH_MASK VC4_MASK(18, 8)
  2597. +#define VC4_TEX_P1_WIDTH_SHIFT 8
  2598. +
  2599. +#define VC4_TEX_P1_MAGFILT_MASK VC4_MASK(7, 7)
  2600. +#define VC4_TEX_P1_MAGFILT_SHIFT 7
  2601. +# define VC4_TEX_P1_MAGFILT_LINEAR 0
  2602. +# define VC4_TEX_P1_MAGFILT_NEAREST 1
  2603. +
  2604. +#define VC4_TEX_P1_MINFILT_MASK VC4_MASK(6, 4)
  2605. +#define VC4_TEX_P1_MINFILT_SHIFT 4
  2606. +# define VC4_TEX_P1_MINFILT_LINEAR 0
  2607. +# define VC4_TEX_P1_MINFILT_NEAREST 1
  2608. +# define VC4_TEX_P1_MINFILT_NEAR_MIP_NEAR 2
  2609. +# define VC4_TEX_P1_MINFILT_NEAR_MIP_LIN 3
  2610. +# define VC4_TEX_P1_MINFILT_LIN_MIP_NEAR 4
  2611. +# define VC4_TEX_P1_MINFILT_LIN_MIP_LIN 5
  2612. +
  2613. +#define VC4_TEX_P1_WRAP_T_MASK VC4_MASK(3, 2)
  2614. +#define VC4_TEX_P1_WRAP_T_SHIFT 2
  2615. +#define VC4_TEX_P1_WRAP_S_MASK VC4_MASK(1, 0)
  2616. +#define VC4_TEX_P1_WRAP_S_SHIFT 0
  2617. +# define VC4_TEX_P1_WRAP_REPEAT 0
  2618. +# define VC4_TEX_P1_WRAP_CLAMP 1
  2619. +# define VC4_TEX_P1_WRAP_MIRROR 2
  2620. +# define VC4_TEX_P1_WRAP_BORDER 3
  2621. +
  2622. +#define VC4_TEX_P2_PTYPE_MASK VC4_MASK(31, 30)
  2623. +#define VC4_TEX_P2_PTYPE_SHIFT 30
  2624. +# define VC4_TEX_P2_PTYPE_IGNORED 0
  2625. +# define VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE 1
  2626. +# define VC4_TEX_P2_PTYPE_CHILD_IMAGE_DIMENSIONS 2
  2627. +# define VC4_TEX_P2_PTYPE_CHILD_IMAGE_OFFSETS 3
  2628. +
  2629. +/* VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE bits */
  2630. +#define VC4_TEX_P2_CMST_MASK VC4_MASK(29, 12)
  2631. +#define VC4_TEX_P2_CMST_SHIFT 12
  2632. +#define VC4_TEX_P2_BSLOD_MASK VC4_MASK(0, 0)
  2633. +#define VC4_TEX_P2_BSLOD_SHIFT 0
  2634. +
  2635. +/* VC4_TEX_P2_PTYPE_CHILD_IMAGE_DIMENSIONS */
  2636. +#define VC4_TEX_P2_CHEIGHT_MASK VC4_MASK(22, 12)
  2637. +#define VC4_TEX_P2_CHEIGHT_SHIFT 12
  2638. +#define VC4_TEX_P2_CWIDTH_MASK VC4_MASK(10, 0)
  2639. +#define VC4_TEX_P2_CWIDTH_SHIFT 0
  2640. +
  2641. +/* VC4_TEX_P2_PTYPE_CHILD_IMAGE_OFFSETS */
  2642. +#define VC4_TEX_P2_CYOFF_MASK VC4_MASK(22, 12)
  2643. +#define VC4_TEX_P2_CYOFF_SHIFT 12
  2644. +#define VC4_TEX_P2_CXOFF_MASK VC4_MASK(10, 0)
  2645. +#define VC4_TEX_P2_CXOFF_SHIFT 0
  2646. +
  2647. +#endif /* VC4_PACKET_H */
  2648. --- a/drivers/gpu/drm/vc4/vc4_plane.c
  2649. +++ b/drivers/gpu/drm/vc4/vc4_plane.c
  2650. @@ -29,6 +29,14 @@ struct vc4_plane_state {
  2651. u32 *dlist;
  2652. u32 dlist_size; /* Number of dwords in allocated for the display list */
  2653. u32 dlist_count; /* Number of used dwords in the display list. */
  2654. +
  2655. + /* Offset in the dlist to pointer word 0. */
  2656. + u32 pw0_offset;
  2657. +
  2658. + /* Offset where the plane's dlist was last stored in the
  2659. + hardware at vc4_crtc_atomic_flush() time.
  2660. + */
  2661. + u32 *hw_dlist;
  2662. };
  2663. static inline struct vc4_plane_state *
  2664. @@ -207,6 +215,8 @@ static int vc4_plane_mode_set(struct drm
  2665. /* Position Word 3: Context. Written by the HVS. */
  2666. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  2667. + vc4_state->pw0_offset = vc4_state->dlist_count;
  2668. +
  2669. /* Pointer Word 0: RGB / Y Pointer */
  2670. vc4_dlist_write(vc4_state, bo->paddr + offset);
  2671. @@ -258,6 +268,8 @@ u32 vc4_plane_write_dlist(struct drm_pla
  2672. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  2673. int i;
  2674. + vc4_state->hw_dlist = dlist;
  2675. +
  2676. /* Can't memcpy_toio() because it needs to be 32-bit writes. */
  2677. for (i = 0; i < vc4_state->dlist_count; i++)
  2678. writel(vc4_state->dlist[i], &dlist[i]);
  2679. @@ -272,6 +284,34 @@ u32 vc4_plane_dlist_size(struct drm_plan
  2680. return vc4_state->dlist_count;
  2681. }
  2682. +/* Updates the plane to immediately (well, once the FIFO needs
  2683. + * refilling) scan out from at a new framebuffer.
  2684. + */
  2685. +void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
  2686. +{
  2687. + struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  2688. + struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  2689. + uint32_t addr;
  2690. +
  2691. + /* We're skipping the address adjustment for negative origin,
  2692. + * because this is only called on the primary plane.
  2693. + */
  2694. + WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
  2695. + addr = bo->paddr + fb->offsets[0];
  2696. +
  2697. + /* Write the new address into the hardware immediately. The
  2698. + * scanout will start from this address as soon as the FIFO
  2699. + * needs to refill with pixels.
  2700. + */
  2701. + writel(addr, &vc4_state->hw_dlist[vc4_state->pw0_offset]);
  2702. +
  2703. + /* Also update the CPU-side dlist copy, so that any later
  2704. + * atomic updates that don't do a new modeset on our plane
  2705. + * also use our updated address.
  2706. + */
  2707. + vc4_state->dlist[vc4_state->pw0_offset] = addr;
  2708. +}
  2709. +
  2710. static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
  2711. .prepare_fb = NULL,
  2712. .cleanup_fb = NULL,
  2713. --- /dev/null
  2714. +++ b/drivers/gpu/drm/vc4/vc4_qpu_defines.h
  2715. @@ -0,0 +1,268 @@
  2716. +/*
  2717. + * Copyright © 2014 Broadcom
  2718. + *
  2719. + * Permission is hereby granted, free of charge, to any person obtaining a
  2720. + * copy of this software and associated documentation files (the "Software"),
  2721. + * to deal in the Software without restriction, including without limitation
  2722. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  2723. + * and/or sell copies of the Software, and to permit persons to whom the
  2724. + * Software is furnished to do so, subject to the following conditions:
  2725. + *
  2726. + * The above copyright notice and this permission notice (including the next
  2727. + * paragraph) shall be included in all copies or substantial portions of the
  2728. + * Software.
  2729. + *
  2730. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2731. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2732. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  2733. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2734. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2735. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2736. + * IN THE SOFTWARE.
  2737. + */
  2738. +
  2739. +#ifndef VC4_QPU_DEFINES_H
  2740. +#define VC4_QPU_DEFINES_H
  2741. +
  2742. +enum qpu_op_add {
  2743. + QPU_A_NOP,
  2744. + QPU_A_FADD,
  2745. + QPU_A_FSUB,
  2746. + QPU_A_FMIN,
  2747. + QPU_A_FMAX,
  2748. + QPU_A_FMINABS,
  2749. + QPU_A_FMAXABS,
  2750. + QPU_A_FTOI,
  2751. + QPU_A_ITOF,
  2752. + QPU_A_ADD = 12,
  2753. + QPU_A_SUB,
  2754. + QPU_A_SHR,
  2755. + QPU_A_ASR,
  2756. + QPU_A_ROR,
  2757. + QPU_A_SHL,
  2758. + QPU_A_MIN,
  2759. + QPU_A_MAX,
  2760. + QPU_A_AND,
  2761. + QPU_A_OR,
  2762. + QPU_A_XOR,
  2763. + QPU_A_NOT,
  2764. + QPU_A_CLZ,
  2765. + QPU_A_V8ADDS = 30,
  2766. + QPU_A_V8SUBS = 31,
  2767. +};
  2768. +
  2769. +enum qpu_op_mul {
  2770. + QPU_M_NOP,
  2771. + QPU_M_FMUL,
  2772. + QPU_M_MUL24,
  2773. + QPU_M_V8MULD,
  2774. + QPU_M_V8MIN,
  2775. + QPU_M_V8MAX,
  2776. + QPU_M_V8ADDS,
  2777. + QPU_M_V8SUBS,
  2778. +};
  2779. +
  2780. +enum qpu_raddr {
  2781. + QPU_R_FRAG_PAYLOAD_ZW = 15, /* W for A file, Z for B file */
  2782. + /* 0-31 are the plain regfile a or b fields */
  2783. + QPU_R_UNIF = 32,
  2784. + QPU_R_VARY = 35,
  2785. + QPU_R_ELEM_QPU = 38,
  2786. + QPU_R_NOP,
  2787. + QPU_R_XY_PIXEL_COORD = 41,
  2788. + QPU_R_MS_REV_FLAGS = 41,
  2789. + QPU_R_VPM = 48,
  2790. + QPU_R_VPM_LD_BUSY,
  2791. + QPU_R_VPM_LD_WAIT,
  2792. + QPU_R_MUTEX_ACQUIRE,
  2793. +};
  2794. +
  2795. +enum qpu_waddr {
  2796. + /* 0-31 are the plain regfile a or b fields */
  2797. + QPU_W_ACC0 = 32, /* aka r0 */
  2798. + QPU_W_ACC1,
  2799. + QPU_W_ACC2,
  2800. + QPU_W_ACC3,
  2801. + QPU_W_TMU_NOSWAP,
  2802. + QPU_W_ACC5,
  2803. + QPU_W_HOST_INT,
  2804. + QPU_W_NOP,
  2805. + QPU_W_UNIFORMS_ADDRESS,
  2806. + QPU_W_QUAD_XY, /* X for regfile a, Y for regfile b */
  2807. + QPU_W_MS_FLAGS = 42,
  2808. + QPU_W_REV_FLAG = 42,
  2809. + QPU_W_TLB_STENCIL_SETUP = 43,
  2810. + QPU_W_TLB_Z,
  2811. + QPU_W_TLB_COLOR_MS,
  2812. + QPU_W_TLB_COLOR_ALL,
  2813. + QPU_W_TLB_ALPHA_MASK,
  2814. + QPU_W_VPM,
  2815. + QPU_W_VPMVCD_SETUP, /* LD for regfile a, ST for regfile b */
  2816. + QPU_W_VPM_ADDR, /* LD for regfile a, ST for regfile b */
  2817. + QPU_W_MUTEX_RELEASE,
  2818. + QPU_W_SFU_RECIP,
  2819. + QPU_W_SFU_RECIPSQRT,
  2820. + QPU_W_SFU_EXP,
  2821. + QPU_W_SFU_LOG,
  2822. + QPU_W_TMU0_S,
  2823. + QPU_W_TMU0_T,
  2824. + QPU_W_TMU0_R,
  2825. + QPU_W_TMU0_B,
  2826. + QPU_W_TMU1_S,
  2827. + QPU_W_TMU1_T,
  2828. + QPU_W_TMU1_R,
  2829. + QPU_W_TMU1_B,
  2830. +};
  2831. +
  2832. +enum qpu_sig_bits {
  2833. + QPU_SIG_SW_BREAKPOINT,
  2834. + QPU_SIG_NONE,
  2835. + QPU_SIG_THREAD_SWITCH,
  2836. + QPU_SIG_PROG_END,
  2837. + QPU_SIG_WAIT_FOR_SCOREBOARD,
  2838. + QPU_SIG_SCOREBOARD_UNLOCK,
  2839. + QPU_SIG_LAST_THREAD_SWITCH,
  2840. + QPU_SIG_COVERAGE_LOAD,
  2841. + QPU_SIG_COLOR_LOAD,
  2842. + QPU_SIG_COLOR_LOAD_END,
  2843. + QPU_SIG_LOAD_TMU0,
  2844. + QPU_SIG_LOAD_TMU1,
  2845. + QPU_SIG_ALPHA_MASK_LOAD,
  2846. + QPU_SIG_SMALL_IMM,
  2847. + QPU_SIG_LOAD_IMM,
  2848. + QPU_SIG_BRANCH
  2849. +};
  2850. +
  2851. +enum qpu_mux {
  2852. + /* hardware mux values */
  2853. + QPU_MUX_R0,
  2854. + QPU_MUX_R1,
  2855. + QPU_MUX_R2,
  2856. + QPU_MUX_R3,
  2857. + QPU_MUX_R4,
  2858. + QPU_MUX_R5,
  2859. + QPU_MUX_A,
  2860. + QPU_MUX_B,
  2861. +
  2862. + /* non-hardware mux values */
  2863. + QPU_MUX_IMM,
  2864. +};
  2865. +
  2866. +enum qpu_cond {
  2867. + QPU_COND_NEVER,
  2868. + QPU_COND_ALWAYS,
  2869. + QPU_COND_ZS,
  2870. + QPU_COND_ZC,
  2871. + QPU_COND_NS,
  2872. + QPU_COND_NC,
  2873. + QPU_COND_CS,
  2874. + QPU_COND_CC,
  2875. +};
  2876. +
  2877. +enum qpu_pack_mul {
  2878. + QPU_PACK_MUL_NOP,
  2879. + QPU_PACK_MUL_8888 = 3, /* replicated to each 8 bits of the 32-bit dst. */
  2880. + QPU_PACK_MUL_8A,
  2881. + QPU_PACK_MUL_8B,
  2882. + QPU_PACK_MUL_8C,
  2883. + QPU_PACK_MUL_8D,
  2884. +};
  2885. +
  2886. +enum qpu_pack_a {
  2887. + QPU_PACK_A_NOP,
  2888. + /* convert to 16 bit float if float input, or to int16. */
  2889. + QPU_PACK_A_16A,
  2890. + QPU_PACK_A_16B,
  2891. + /* replicated to each 8 bits of the 32-bit dst. */
  2892. + QPU_PACK_A_8888,
  2893. + /* Convert to 8-bit unsigned int. */
  2894. + QPU_PACK_A_8A,
  2895. + QPU_PACK_A_8B,
  2896. + QPU_PACK_A_8C,
  2897. + QPU_PACK_A_8D,
  2898. +
  2899. + /* Saturating variants of the previous instructions. */
  2900. + QPU_PACK_A_32_SAT, /* int-only */
  2901. + QPU_PACK_A_16A_SAT, /* int or float */
  2902. + QPU_PACK_A_16B_SAT,
  2903. + QPU_PACK_A_8888_SAT,
  2904. + QPU_PACK_A_8A_SAT,
  2905. + QPU_PACK_A_8B_SAT,
  2906. + QPU_PACK_A_8C_SAT,
  2907. + QPU_PACK_A_8D_SAT,
  2908. +};
  2909. +
  2910. +enum qpu_unpack_r4 {
  2911. + QPU_UNPACK_R4_NOP,
  2912. + QPU_UNPACK_R4_F16A_TO_F32,
  2913. + QPU_UNPACK_R4_F16B_TO_F32,
  2914. + QPU_UNPACK_R4_8D_REP,
  2915. + QPU_UNPACK_R4_8A,
  2916. + QPU_UNPACK_R4_8B,
  2917. + QPU_UNPACK_R4_8C,
  2918. + QPU_UNPACK_R4_8D,
  2919. +};
  2920. +
  2921. +#define QPU_MASK(high, low) ((((uint64_t)1<<((high)-(low)+1))-1)<<(low))
  2922. +/* Using the GNU statement expression extension */
  2923. +#define QPU_SET_FIELD(value, field) \
  2924. + ({ \
  2925. + uint64_t fieldval = (uint64_t)(value) << field ## _SHIFT; \
  2926. + assert((fieldval & ~ field ## _MASK) == 0); \
  2927. + fieldval & field ## _MASK; \
  2928. + })
  2929. +
  2930. +#define QPU_GET_FIELD(word, field) ((uint32_t)(((word) & field ## _MASK) >> field ## _SHIFT))
  2931. +
  2932. +#define QPU_SIG_SHIFT 60
  2933. +#define QPU_SIG_MASK QPU_MASK(63, 60)
  2934. +
  2935. +#define QPU_UNPACK_SHIFT 57
  2936. +#define QPU_UNPACK_MASK QPU_MASK(59, 57)
  2937. +
  2938. +/**
  2939. + * If set, the pack field means PACK_MUL or R4 packing, instead of normal
  2940. + * regfile a packing.
  2941. + */
  2942. +#define QPU_PM ((uint64_t)1 << 56)
  2943. +
  2944. +#define QPU_PACK_SHIFT 52
  2945. +#define QPU_PACK_MASK QPU_MASK(55, 52)
  2946. +
  2947. +#define QPU_COND_ADD_SHIFT 49
  2948. +#define QPU_COND_ADD_MASK QPU_MASK(51, 49)
  2949. +#define QPU_COND_MUL_SHIFT 46
  2950. +#define QPU_COND_MUL_MASK QPU_MASK(48, 46)
  2951. +
  2952. +#define QPU_SF ((uint64_t)1 << 45)
  2953. +
  2954. +#define QPU_WADDR_ADD_SHIFT 38
  2955. +#define QPU_WADDR_ADD_MASK QPU_MASK(43, 38)
  2956. +#define QPU_WADDR_MUL_SHIFT 32
  2957. +#define QPU_WADDR_MUL_MASK QPU_MASK(37, 32)
  2958. +
  2959. +#define QPU_OP_MUL_SHIFT 29
  2960. +#define QPU_OP_MUL_MASK QPU_MASK(31, 29)
  2961. +
  2962. +#define QPU_RADDR_A_SHIFT 18
  2963. +#define QPU_RADDR_A_MASK QPU_MASK(23, 18)
  2964. +#define QPU_RADDR_B_SHIFT 12
  2965. +#define QPU_RADDR_B_MASK QPU_MASK(17, 12)
  2966. +#define QPU_SMALL_IMM_SHIFT 12
  2967. +#define QPU_SMALL_IMM_MASK QPU_MASK(17, 12)
  2968. +
  2969. +#define QPU_ADD_A_SHIFT 9
  2970. +#define QPU_ADD_A_MASK QPU_MASK(11, 9)
  2971. +#define QPU_ADD_B_SHIFT 6
  2972. +#define QPU_ADD_B_MASK QPU_MASK(8, 6)
  2973. +#define QPU_MUL_A_SHIFT 3
  2974. +#define QPU_MUL_A_MASK QPU_MASK(5, 3)
  2975. +#define QPU_MUL_B_SHIFT 0
  2976. +#define QPU_MUL_B_MASK QPU_MASK(2, 0)
  2977. +
  2978. +#define QPU_WS ((uint64_t)1 << 44)
  2979. +
  2980. +#define QPU_OP_ADD_SHIFT 24
  2981. +#define QPU_OP_ADD_MASK QPU_MASK(28, 24)
  2982. +
  2983. +#endif /* VC4_QPU_DEFINES_H */
  2984. --- /dev/null
  2985. +++ b/drivers/gpu/drm/vc4/vc4_render_cl.c
  2986. @@ -0,0 +1,448 @@
  2987. +/*
  2988. + * Copyright © 2014-2015 Broadcom
  2989. + *
  2990. + * Permission is hereby granted, free of charge, to any person obtaining a
  2991. + * copy of this software and associated documentation files (the "Software"),
  2992. + * to deal in the Software without restriction, including without limitation
  2993. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  2994. + * and/or sell copies of the Software, and to permit persons to whom the
  2995. + * Software is furnished to do so, subject to the following conditions:
  2996. + *
  2997. + * The above copyright notice and this permission notice (including the next
  2998. + * paragraph) shall be included in all copies or substantial portions of the
  2999. + * Software.
  3000. + *
  3001. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3002. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3003. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  3004. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3005. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3006. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3007. + * IN THE SOFTWARE.
  3008. + */
  3009. +
  3010. +/**
  3011. + * DOC: Render command list generation
  3012. + *
  3013. + * In the VC4 driver, render command list generation is performed by the
  3014. + * kernel instead of userspace. We do this because validating a
  3015. + * user-submitted command list is hard to get right and has high CPU overhead,
  3016. + * while the number of valid configurations for render command lists is
  3017. + * actually fairly low.
  3018. + */
  3019. +
  3020. +#include "uapi/drm/vc4_drm.h"
  3021. +#include "vc4_drv.h"
  3022. +#include "vc4_packet.h"
  3023. +
  3024. +struct vc4_rcl_setup {
  3025. + struct drm_gem_cma_object *color_read;
  3026. + struct drm_gem_cma_object *color_ms_write;
  3027. + struct drm_gem_cma_object *zs_read;
  3028. + struct drm_gem_cma_object *zs_write;
  3029. +
  3030. + struct drm_gem_cma_object *rcl;
  3031. + u32 next_offset;
  3032. +};
  3033. +
  3034. +static inline void rcl_u8(struct vc4_rcl_setup *setup, u8 val)
  3035. +{
  3036. + *(u8 *)(setup->rcl->vaddr + setup->next_offset) = val;
  3037. + setup->next_offset += 1;
  3038. +}
  3039. +
  3040. +static inline void rcl_u16(struct vc4_rcl_setup *setup, u16 val)
  3041. +{
  3042. + *(u16 *)(setup->rcl->vaddr + setup->next_offset) = val;
  3043. + setup->next_offset += 2;
  3044. +}
  3045. +
  3046. +static inline void rcl_u32(struct vc4_rcl_setup *setup, u32 val)
  3047. +{
  3048. + *(u32 *)(setup->rcl->vaddr + setup->next_offset) = val;
  3049. + setup->next_offset += 4;
  3050. +}
  3051. +
  3052. +
  3053. +/*
  3054. + * Emits a no-op STORE_TILE_BUFFER_GENERAL.
  3055. + *
  3056. + * If we emit a PACKET_TILE_COORDINATES, it must be followed by a store of
  3057. + * some sort before another load is triggered.
  3058. + */
  3059. +static void vc4_store_before_load(struct vc4_rcl_setup *setup)
  3060. +{
  3061. + rcl_u8(setup, VC4_PACKET_STORE_TILE_BUFFER_GENERAL);
  3062. + rcl_u16(setup,
  3063. + VC4_SET_FIELD(VC4_LOADSTORE_TILE_BUFFER_NONE,
  3064. + VC4_LOADSTORE_TILE_BUFFER_BUFFER) |
  3065. + VC4_STORE_TILE_BUFFER_DISABLE_COLOR_CLEAR |
  3066. + VC4_STORE_TILE_BUFFER_DISABLE_ZS_CLEAR |
  3067. + VC4_STORE_TILE_BUFFER_DISABLE_VG_MASK_CLEAR);
  3068. + rcl_u32(setup, 0); /* no address, since we're in None mode */
  3069. +}
  3070. +
  3071. +/*
  3072. + * Emits a PACKET_TILE_COORDINATES if one isn't already pending.
  3073. + *
  3074. + * The tile coordinates packet triggers a pending load if there is one, are
  3075. + * used for clipping during rendering, and determine where loads/stores happen
  3076. + * relative to their base address.
  3077. + */
  3078. +static void vc4_tile_coordinates(struct vc4_rcl_setup *setup,
  3079. + uint32_t x, uint32_t y)
  3080. +{
  3081. + rcl_u8(setup, VC4_PACKET_TILE_COORDINATES);
  3082. + rcl_u8(setup, x);
  3083. + rcl_u8(setup, y);
  3084. +}
  3085. +
  3086. +static void emit_tile(struct vc4_exec_info *exec,
  3087. + struct vc4_rcl_setup *setup,
  3088. + uint8_t x, uint8_t y, bool first, bool last)
  3089. +{
  3090. + struct drm_vc4_submit_cl *args = exec->args;
  3091. + bool has_bin = args->bin_cl_size != 0;
  3092. +
  3093. + /* Note that the load doesn't actually occur until the
  3094. + * tile coords packet is processed, and only one load
  3095. + * may be outstanding at a time.
  3096. + */
  3097. + if (setup->color_read) {
  3098. + rcl_u8(setup, VC4_PACKET_LOAD_TILE_BUFFER_GENERAL);
  3099. + rcl_u16(setup, args->color_read.bits);
  3100. + rcl_u32(setup,
  3101. + setup->color_read->paddr + args->color_read.offset);
  3102. + }
  3103. +
  3104. + if (setup->zs_read) {
  3105. + if (setup->color_read) {
  3106. + /* Exec previous load. */
  3107. + vc4_tile_coordinates(setup, x, y);
  3108. + vc4_store_before_load(setup);
  3109. + }
  3110. +
  3111. + rcl_u8(setup, VC4_PACKET_LOAD_TILE_BUFFER_GENERAL);
  3112. + rcl_u16(setup, args->zs_read.bits);
  3113. + rcl_u32(setup, setup->zs_read->paddr + args->zs_read.offset);
  3114. + }
  3115. +
  3116. + /* Clipping depends on tile coordinates having been
  3117. + * emitted, so we always need one here.
  3118. + */
  3119. + vc4_tile_coordinates(setup, x, y);
  3120. +
  3121. + /* Wait for the binner before jumping to the first
  3122. + * tile's lists.
  3123. + */
  3124. + if (first && has_bin)
  3125. + rcl_u8(setup, VC4_PACKET_WAIT_ON_SEMAPHORE);
  3126. +
  3127. + if (has_bin) {
  3128. + rcl_u8(setup, VC4_PACKET_BRANCH_TO_SUB_LIST);
  3129. + rcl_u32(setup, (exec->tile_bo->paddr +
  3130. + exec->tile_alloc_offset +
  3131. + (y * exec->bin_tiles_x + x) * 32));
  3132. + }
  3133. +
  3134. + if (setup->zs_write) {
  3135. + rcl_u8(setup, VC4_PACKET_STORE_TILE_BUFFER_GENERAL);
  3136. + rcl_u16(setup, args->zs_write.bits |
  3137. + (setup->color_ms_write ?
  3138. + VC4_STORE_TILE_BUFFER_DISABLE_COLOR_CLEAR : 0));
  3139. + rcl_u32(setup,
  3140. + (setup->zs_write->paddr + args->zs_write.offset) |
  3141. + ((last && !setup->color_ms_write) ?
  3142. + VC4_LOADSTORE_TILE_BUFFER_EOF : 0));
  3143. + }
  3144. +
  3145. + if (setup->color_ms_write) {
  3146. + if (setup->zs_write) {
  3147. + /* Reset after previous store */
  3148. + vc4_tile_coordinates(setup, x, y);
  3149. + }
  3150. +
  3151. + if (last)
  3152. + rcl_u8(setup, VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF);
  3153. + else
  3154. + rcl_u8(setup, VC4_PACKET_STORE_MS_TILE_BUFFER);
  3155. + }
  3156. +}
  3157. +
  3158. +static int vc4_create_rcl_bo(struct drm_device *dev, struct vc4_exec_info *exec,
  3159. + struct vc4_rcl_setup *setup)
  3160. +{
  3161. + struct drm_vc4_submit_cl *args = exec->args;
  3162. + bool has_bin = args->bin_cl_size != 0;
  3163. + uint8_t min_x_tile = args->min_x_tile;
  3164. + uint8_t min_y_tile = args->min_y_tile;
  3165. + uint8_t max_x_tile = args->max_x_tile;
  3166. + uint8_t max_y_tile = args->max_y_tile;
  3167. + uint8_t xtiles = max_x_tile - min_x_tile + 1;
  3168. + uint8_t ytiles = max_y_tile - min_y_tile + 1;
  3169. + uint8_t x, y;
  3170. + uint32_t size, loop_body_size;
  3171. +
  3172. + size = VC4_PACKET_TILE_RENDERING_MODE_CONFIG_SIZE;
  3173. + loop_body_size = VC4_PACKET_TILE_COORDINATES_SIZE;
  3174. +
  3175. + if (args->flags & VC4_SUBMIT_CL_USE_CLEAR_COLOR) {
  3176. + size += VC4_PACKET_CLEAR_COLORS_SIZE +
  3177. + VC4_PACKET_TILE_COORDINATES_SIZE +
  3178. + VC4_PACKET_STORE_TILE_BUFFER_GENERAL_SIZE;
  3179. + }
  3180. +
  3181. + if (setup->color_read) {
  3182. + loop_body_size += (VC4_PACKET_LOAD_TILE_BUFFER_GENERAL_SIZE);
  3183. + }
  3184. + if (setup->zs_read) {
  3185. + if (setup->color_read) {
  3186. + loop_body_size += VC4_PACKET_TILE_COORDINATES_SIZE;
  3187. + loop_body_size += VC4_PACKET_STORE_TILE_BUFFER_GENERAL_SIZE;
  3188. + }
  3189. + loop_body_size += VC4_PACKET_LOAD_TILE_BUFFER_GENERAL_SIZE;
  3190. + }
  3191. +
  3192. + if (has_bin) {
  3193. + size += VC4_PACKET_WAIT_ON_SEMAPHORE_SIZE;
  3194. + loop_body_size += VC4_PACKET_BRANCH_TO_SUB_LIST_SIZE;
  3195. + }
  3196. +
  3197. + if (setup->zs_write)
  3198. + loop_body_size += VC4_PACKET_STORE_TILE_BUFFER_GENERAL_SIZE;
  3199. + if (setup->color_ms_write) {
  3200. + if (setup->zs_write)
  3201. + loop_body_size += VC4_PACKET_TILE_COORDINATES_SIZE;
  3202. + loop_body_size += VC4_PACKET_STORE_MS_TILE_BUFFER_SIZE;
  3203. + }
  3204. + size += xtiles * ytiles * loop_body_size;
  3205. +
  3206. + setup->rcl = &vc4_bo_create(dev, size)->base;
  3207. + if (!setup->rcl)
  3208. + return -ENOMEM;
  3209. + list_add_tail(&to_vc4_bo(&setup->rcl->base)->unref_head,
  3210. + &exec->unref_list);
  3211. +
  3212. + rcl_u8(setup, VC4_PACKET_TILE_RENDERING_MODE_CONFIG);
  3213. + rcl_u32(setup,
  3214. + (setup->color_ms_write ?
  3215. + (setup->color_ms_write->paddr +
  3216. + args->color_ms_write.offset) :
  3217. + 0));
  3218. + rcl_u16(setup, args->width);
  3219. + rcl_u16(setup, args->height);
  3220. + rcl_u16(setup, args->color_ms_write.bits);
  3221. +
  3222. + /* The tile buffer gets cleared when the previous tile is stored. If
  3223. + * the clear values changed between frames, then the tile buffer has
  3224. + * stale clear values in it, so we have to do a store in None mode (no
  3225. + * writes) so that we trigger the tile buffer clear.
  3226. + */
  3227. + if (args->flags & VC4_SUBMIT_CL_USE_CLEAR_COLOR) {
  3228. + rcl_u8(setup, VC4_PACKET_CLEAR_COLORS);
  3229. + rcl_u32(setup, args->clear_color[0]);
  3230. + rcl_u32(setup, args->clear_color[1]);
  3231. + rcl_u32(setup, args->clear_z);
  3232. + rcl_u8(setup, args->clear_s);
  3233. +
  3234. + vc4_tile_coordinates(setup, 0, 0);
  3235. +
  3236. + rcl_u8(setup, VC4_PACKET_STORE_TILE_BUFFER_GENERAL);
  3237. + rcl_u16(setup, VC4_LOADSTORE_TILE_BUFFER_NONE);
  3238. + rcl_u32(setup, 0); /* no address, since we're in None mode */
  3239. + }
  3240. +
  3241. + for (y = min_y_tile; y <= max_y_tile; y++) {
  3242. + for (x = min_x_tile; x <= max_x_tile; x++) {
  3243. + bool first = (x == min_x_tile && y == min_y_tile);
  3244. + bool last = (x == max_x_tile && y == max_y_tile);
  3245. + emit_tile(exec, setup, x, y, first, last);
  3246. + }
  3247. + }
  3248. +
  3249. + BUG_ON(setup->next_offset != size);
  3250. + exec->ct1ca = setup->rcl->paddr;
  3251. + exec->ct1ea = setup->rcl->paddr + setup->next_offset;
  3252. +
  3253. + return 0;
  3254. +}
  3255. +
  3256. +static int vc4_rcl_surface_setup(struct vc4_exec_info *exec,
  3257. + struct drm_gem_cma_object **obj,
  3258. + struct drm_vc4_submit_rcl_surface *surf)
  3259. +{
  3260. + uint8_t tiling = VC4_GET_FIELD(surf->bits,
  3261. + VC4_LOADSTORE_TILE_BUFFER_TILING);
  3262. + uint8_t buffer = VC4_GET_FIELD(surf->bits,
  3263. + VC4_LOADSTORE_TILE_BUFFER_BUFFER);
  3264. + uint8_t format = VC4_GET_FIELD(surf->bits,
  3265. + VC4_LOADSTORE_TILE_BUFFER_FORMAT);
  3266. + int cpp;
  3267. +
  3268. + if (surf->pad != 0) {
  3269. + DRM_ERROR("Padding unset\n");
  3270. + return -EINVAL;
  3271. + }
  3272. +
  3273. + if (surf->hindex == ~0)
  3274. + return 0;
  3275. +
  3276. + if (!vc4_use_bo(exec, surf->hindex, VC4_MODE_RENDER, obj))
  3277. + return -EINVAL;
  3278. +
  3279. + if (surf->bits & ~(VC4_LOADSTORE_TILE_BUFFER_TILING_MASK |
  3280. + VC4_LOADSTORE_TILE_BUFFER_BUFFER_MASK |
  3281. + VC4_LOADSTORE_TILE_BUFFER_FORMAT_MASK)) {
  3282. + DRM_ERROR("Unknown bits in load/store: 0x%04x\n",
  3283. + surf->bits);
  3284. + return -EINVAL;
  3285. + }
  3286. +
  3287. + if (tiling > VC4_TILING_FORMAT_LT) {
  3288. + DRM_ERROR("Bad tiling format\n");
  3289. + return -EINVAL;
  3290. + }
  3291. +
  3292. + if (buffer == VC4_LOADSTORE_TILE_BUFFER_ZS) {
  3293. + if (format != 0) {
  3294. + DRM_ERROR("No color format should be set for ZS\n");
  3295. + return -EINVAL;
  3296. + }
  3297. + cpp = 4;
  3298. + } else if (buffer == VC4_LOADSTORE_TILE_BUFFER_COLOR) {
  3299. + switch (format) {
  3300. + case VC4_LOADSTORE_TILE_BUFFER_BGR565:
  3301. + case VC4_LOADSTORE_TILE_BUFFER_BGR565_DITHER:
  3302. + cpp = 2;
  3303. + break;
  3304. + case VC4_LOADSTORE_TILE_BUFFER_RGBA8888:
  3305. + cpp = 4;
  3306. + break;
  3307. + default:
  3308. + DRM_ERROR("Bad tile buffer format\n");
  3309. + return -EINVAL;
  3310. + }
  3311. + } else {
  3312. + DRM_ERROR("Bad load/store buffer %d.\n", buffer);
  3313. + return -EINVAL;
  3314. + }
  3315. +
  3316. + if (surf->offset & 0xf) {
  3317. + DRM_ERROR("load/store buffer must be 16b aligned.\n");
  3318. + return -EINVAL;
  3319. + }
  3320. +
  3321. + if (!vc4_check_tex_size(exec, *obj, surf->offset, tiling,
  3322. + exec->args->width, exec->args->height, cpp)) {
  3323. + return -EINVAL;
  3324. + }
  3325. +
  3326. + return 0;
  3327. +}
  3328. +
  3329. +static int
  3330. +vc4_rcl_ms_surface_setup(struct vc4_exec_info *exec,
  3331. + struct drm_gem_cma_object **obj,
  3332. + struct drm_vc4_submit_rcl_surface *surf)
  3333. +{
  3334. + uint8_t tiling = VC4_GET_FIELD(surf->bits,
  3335. + VC4_RENDER_CONFIG_MEMORY_FORMAT);
  3336. + uint8_t format = VC4_GET_FIELD(surf->bits,
  3337. + VC4_RENDER_CONFIG_FORMAT);
  3338. + int cpp;
  3339. +
  3340. + if (surf->pad != 0) {
  3341. + DRM_ERROR("Padding unset\n");
  3342. + return -EINVAL;
  3343. + }
  3344. +
  3345. + if (surf->bits & ~(VC4_RENDER_CONFIG_MEMORY_FORMAT_MASK |
  3346. + VC4_RENDER_CONFIG_FORMAT_MASK)) {
  3347. + DRM_ERROR("Unknown bits in render config: 0x%04x\n",
  3348. + surf->bits);
  3349. + return -EINVAL;
  3350. + }
  3351. +
  3352. + if (surf->hindex == ~0)
  3353. + return 0;
  3354. +
  3355. + if (!vc4_use_bo(exec, surf->hindex, VC4_MODE_RENDER, obj))
  3356. + return -EINVAL;
  3357. +
  3358. + if (tiling > VC4_TILING_FORMAT_LT) {
  3359. + DRM_ERROR("Bad tiling format\n");
  3360. + return -EINVAL;
  3361. + }
  3362. +
  3363. + switch (format) {
  3364. + case VC4_RENDER_CONFIG_FORMAT_BGR565_DITHERED:
  3365. + case VC4_RENDER_CONFIG_FORMAT_BGR565:
  3366. + cpp = 2;
  3367. + break;
  3368. + case VC4_RENDER_CONFIG_FORMAT_RGBA8888:
  3369. + cpp = 4;
  3370. + break;
  3371. + default:
  3372. + DRM_ERROR("Bad tile buffer format\n");
  3373. + return -EINVAL;
  3374. + }
  3375. +
  3376. + if (!vc4_check_tex_size(exec, *obj, surf->offset, tiling,
  3377. + exec->args->width, exec->args->height, cpp)) {
  3378. + return -EINVAL;
  3379. + }
  3380. +
  3381. + return 0;
  3382. +}
  3383. +
  3384. +int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec)
  3385. +{
  3386. + struct vc4_rcl_setup setup = {0};
  3387. + struct drm_vc4_submit_cl *args = exec->args;
  3388. + bool has_bin = args->bin_cl_size != 0;
  3389. + int ret;
  3390. +
  3391. + if (args->min_x_tile > args->max_x_tile ||
  3392. + args->min_y_tile > args->max_y_tile) {
  3393. + DRM_ERROR("Bad render tile set (%d,%d)-(%d,%d)\n",
  3394. + args->min_x_tile, args->min_y_tile,
  3395. + args->max_x_tile, args->max_y_tile);
  3396. + return -EINVAL;
  3397. + }
  3398. +
  3399. + if (has_bin &&
  3400. + (args->max_x_tile > exec->bin_tiles_x ||
  3401. + args->max_y_tile > exec->bin_tiles_y)) {
  3402. + DRM_ERROR("Render tiles (%d,%d) outside of bin config (%d,%d)\n",
  3403. + args->max_x_tile, args->max_y_tile,
  3404. + exec->bin_tiles_x, exec->bin_tiles_y);
  3405. + return -EINVAL;
  3406. + }
  3407. +
  3408. + ret = vc4_rcl_surface_setup(exec, &setup.color_read, &args->color_read);
  3409. + if (ret)
  3410. + return ret;
  3411. +
  3412. + ret = vc4_rcl_ms_surface_setup(exec, &setup.color_ms_write,
  3413. + &args->color_ms_write);
  3414. + if (ret)
  3415. + return ret;
  3416. +
  3417. + ret = vc4_rcl_surface_setup(exec, &setup.zs_read, &args->zs_read);
  3418. + if (ret)
  3419. + return ret;
  3420. +
  3421. + ret = vc4_rcl_surface_setup(exec, &setup.zs_write, &args->zs_write);
  3422. + if (ret)
  3423. + return ret;
  3424. +
  3425. + /* We shouldn't even have the job submitted to us if there's no
  3426. + * surface to write out.
  3427. + */
  3428. + if (!setup.color_ms_write && !setup.zs_write) {
  3429. + DRM_ERROR("RCL requires color or Z/S write\n");
  3430. + return -EINVAL;
  3431. + }
  3432. +
  3433. + return vc4_create_rcl_bo(dev, exec, &setup);
  3434. +}
  3435. --- /dev/null
  3436. +++ b/drivers/gpu/drm/vc4/vc4_trace.h
  3437. @@ -0,0 +1,63 @@
  3438. +/*
  3439. + * Copyright (C) 2015 Broadcom
  3440. + *
  3441. + * This program is free software; you can redistribute it and/or modify
  3442. + * it under the terms of the GNU General Public License version 2 as
  3443. + * published by the Free Software Foundation.
  3444. + */
  3445. +
  3446. +#if !defined(_VC4_TRACE_H_) || defined(TRACE_HEADER_MULTI_READ)
  3447. +#define _VC4_TRACE_H_
  3448. +
  3449. +#include <linux/stringify.h>
  3450. +#include <linux/types.h>
  3451. +#include <linux/tracepoint.h>
  3452. +
  3453. +#undef TRACE_SYSTEM
  3454. +#define TRACE_SYSTEM vc4
  3455. +#define TRACE_INCLUDE_FILE vc4_trace
  3456. +
  3457. +TRACE_EVENT(vc4_wait_for_seqno_begin,
  3458. + TP_PROTO(struct drm_device *dev, uint64_t seqno, uint64_t timeout),
  3459. + TP_ARGS(dev, seqno, timeout),
  3460. +
  3461. + TP_STRUCT__entry(
  3462. + __field(u32, dev)
  3463. + __field(u64, seqno)
  3464. + __field(u64, timeout)
  3465. + ),
  3466. +
  3467. + TP_fast_assign(
  3468. + __entry->dev = dev->primary->index;
  3469. + __entry->seqno = seqno;
  3470. + __entry->timeout = timeout;
  3471. + ),
  3472. +
  3473. + TP_printk("dev=%u, seqno=%llu, timeout=%llu",
  3474. + __entry->dev, __entry->seqno, __entry->timeout)
  3475. +);
  3476. +
  3477. +TRACE_EVENT(vc4_wait_for_seqno_end,
  3478. + TP_PROTO(struct drm_device *dev, uint64_t seqno),
  3479. + TP_ARGS(dev, seqno),
  3480. +
  3481. + TP_STRUCT__entry(
  3482. + __field(u32, dev)
  3483. + __field(u64, seqno)
  3484. + ),
  3485. +
  3486. + TP_fast_assign(
  3487. + __entry->dev = dev->primary->index;
  3488. + __entry->seqno = seqno;
  3489. + ),
  3490. +
  3491. + TP_printk("dev=%u, seqno=%llu",
  3492. + __entry->dev, __entry->seqno)
  3493. +);
  3494. +
  3495. +#endif /* _VC4_TRACE_H_ */
  3496. +
  3497. +/* This part must be outside protection */
  3498. +#undef TRACE_INCLUDE_PATH
  3499. +#define TRACE_INCLUDE_PATH .
  3500. +#include <trace/define_trace.h>
  3501. --- /dev/null
  3502. +++ b/drivers/gpu/drm/vc4/vc4_trace_points.c
  3503. @@ -0,0 +1,14 @@
  3504. +/*
  3505. + * Copyright (C) 2015 Broadcom
  3506. + *
  3507. + * This program is free software; you can redistribute it and/or modify
  3508. + * it under the terms of the GNU General Public License version 2 as
  3509. + * published by the Free Software Foundation.
  3510. + */
  3511. +
  3512. +#include "vc4_drv.h"
  3513. +
  3514. +#ifndef __CHECKER__
  3515. +#define CREATE_TRACE_POINTS
  3516. +#include "vc4_trace.h"
  3517. +#endif
  3518. --- /dev/null
  3519. +++ b/drivers/gpu/drm/vc4/vc4_v3d.c
  3520. @@ -0,0 +1,268 @@
  3521. +/*
  3522. + * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  3523. + * Copyright (C) 2013 Red Hat
  3524. + * Author: Rob Clark <robdclark@gmail.com>
  3525. + *
  3526. + * This program is free software; you can redistribute it and/or modify it
  3527. + * under the terms of the GNU General Public License version 2 as published by
  3528. + * the Free Software Foundation.
  3529. + *
  3530. + * This program is distributed in the hope that it will be useful, but WITHOUT
  3531. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  3532. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  3533. + * more details.
  3534. + *
  3535. + * You should have received a copy of the GNU General Public License along with
  3536. + * this program. If not, see <http://www.gnu.org/licenses/>.
  3537. + */
  3538. +
  3539. +#include "linux/component.h"
  3540. +#include "soc/bcm2835/raspberrypi-firmware.h"
  3541. +#include "vc4_drv.h"
  3542. +#include "vc4_regs.h"
  3543. +
  3544. +#ifdef CONFIG_DEBUG_FS
  3545. +#define REGDEF(reg) { reg, #reg }
  3546. +static const struct {
  3547. + uint32_t reg;
  3548. + const char *name;
  3549. +} vc4_reg_defs[] = {
  3550. + REGDEF(V3D_IDENT0),
  3551. + REGDEF(V3D_IDENT1),
  3552. + REGDEF(V3D_IDENT2),
  3553. + REGDEF(V3D_SCRATCH),
  3554. + REGDEF(V3D_L2CACTL),
  3555. + REGDEF(V3D_SLCACTL),
  3556. + REGDEF(V3D_INTCTL),
  3557. + REGDEF(V3D_INTENA),
  3558. + REGDEF(V3D_INTDIS),
  3559. + REGDEF(V3D_CT0CS),
  3560. + REGDEF(V3D_CT1CS),
  3561. + REGDEF(V3D_CT0EA),
  3562. + REGDEF(V3D_CT1EA),
  3563. + REGDEF(V3D_CT0CA),
  3564. + REGDEF(V3D_CT1CA),
  3565. + REGDEF(V3D_CT00RA0),
  3566. + REGDEF(V3D_CT01RA0),
  3567. + REGDEF(V3D_CT0LC),
  3568. + REGDEF(V3D_CT1LC),
  3569. + REGDEF(V3D_CT0PC),
  3570. + REGDEF(V3D_CT1PC),
  3571. + REGDEF(V3D_PCS),
  3572. + REGDEF(V3D_BFC),
  3573. + REGDEF(V3D_RFC),
  3574. + REGDEF(V3D_BPCA),
  3575. + REGDEF(V3D_BPCS),
  3576. + REGDEF(V3D_BPOA),
  3577. + REGDEF(V3D_BPOS),
  3578. + REGDEF(V3D_BXCF),
  3579. + REGDEF(V3D_SQRSV0),
  3580. + REGDEF(V3D_SQRSV1),
  3581. + REGDEF(V3D_SQCNTL),
  3582. + REGDEF(V3D_SRQPC),
  3583. + REGDEF(V3D_SRQUA),
  3584. + REGDEF(V3D_SRQUL),
  3585. + REGDEF(V3D_SRQCS),
  3586. + REGDEF(V3D_VPACNTL),
  3587. + REGDEF(V3D_VPMBASE),
  3588. + REGDEF(V3D_PCTRC),
  3589. + REGDEF(V3D_PCTRE),
  3590. + REGDEF(V3D_PCTR0),
  3591. + REGDEF(V3D_PCTRS0),
  3592. + REGDEF(V3D_PCTR1),
  3593. + REGDEF(V3D_PCTRS1),
  3594. + REGDEF(V3D_PCTR2),
  3595. + REGDEF(V3D_PCTRS2),
  3596. + REGDEF(V3D_PCTR3),
  3597. + REGDEF(V3D_PCTRS3),
  3598. + REGDEF(V3D_PCTR4),
  3599. + REGDEF(V3D_PCTRS4),
  3600. + REGDEF(V3D_PCTR5),
  3601. + REGDEF(V3D_PCTRS5),
  3602. + REGDEF(V3D_PCTR6),
  3603. + REGDEF(V3D_PCTRS6),
  3604. + REGDEF(V3D_PCTR7),
  3605. + REGDEF(V3D_PCTRS7),
  3606. + REGDEF(V3D_PCTR8),
  3607. + REGDEF(V3D_PCTRS8),
  3608. + REGDEF(V3D_PCTR9),
  3609. + REGDEF(V3D_PCTRS9),
  3610. + REGDEF(V3D_PCTR10),
  3611. + REGDEF(V3D_PCTRS10),
  3612. + REGDEF(V3D_PCTR11),
  3613. + REGDEF(V3D_PCTRS11),
  3614. + REGDEF(V3D_PCTR12),
  3615. + REGDEF(V3D_PCTRS12),
  3616. + REGDEF(V3D_PCTR13),
  3617. + REGDEF(V3D_PCTRS13),
  3618. + REGDEF(V3D_PCTR14),
  3619. + REGDEF(V3D_PCTRS14),
  3620. + REGDEF(V3D_PCTR15),
  3621. + REGDEF(V3D_PCTRS15),
  3622. + REGDEF(V3D_BGE),
  3623. + REGDEF(V3D_FDBGO),
  3624. + REGDEF(V3D_FDBGB),
  3625. + REGDEF(V3D_FDBGR),
  3626. + REGDEF(V3D_FDBGS),
  3627. + REGDEF(V3D_ERRSTAT),
  3628. +};
  3629. +
  3630. +int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused)
  3631. +{
  3632. + struct drm_info_node *node = (struct drm_info_node *) m->private;
  3633. + struct drm_device *dev = node->minor->dev;
  3634. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  3635. + int i;
  3636. +
  3637. + for (i = 0; i < ARRAY_SIZE(vc4_reg_defs); i++) {
  3638. + seq_printf(m, "%s (0x%04x): 0x%08x\n",
  3639. + vc4_reg_defs[i].name, vc4_reg_defs[i].reg,
  3640. + V3D_READ(vc4_reg_defs[i].reg));
  3641. + }
  3642. +
  3643. + return 0;
  3644. +}
  3645. +
  3646. +int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused)
  3647. +{
  3648. + struct drm_info_node *node = (struct drm_info_node *) m->private;
  3649. + struct drm_device *dev = node->minor->dev;
  3650. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  3651. + uint32_t ident1 = V3D_READ(V3D_IDENT1);
  3652. + uint32_t nslc = VC4_GET_FIELD(ident1, V3D_IDENT1_NSLC);
  3653. + uint32_t tups = VC4_GET_FIELD(ident1, V3D_IDENT1_TUPS);
  3654. + uint32_t qups = VC4_GET_FIELD(ident1, V3D_IDENT1_QUPS);
  3655. +
  3656. + seq_printf(m, "Revision: %d\n", VC4_GET_FIELD(ident1, V3D_IDENT1_REV));
  3657. + seq_printf(m, "Slices: %d\n", nslc);
  3658. + seq_printf(m, "TMUs: %d\n", nslc * tups);
  3659. + seq_printf(m, "QPUs: %d\n", nslc * qups);
  3660. + seq_printf(m, "Semaphores: %d\n", VC4_GET_FIELD(ident1, V3D_IDENT1_NSEM));
  3661. +
  3662. + return 0;
  3663. +}
  3664. +#endif /* CONFIG_DEBUG_FS */
  3665. +
  3666. +/*
  3667. + * Asks the firmware to turn on power to the V3D engine.
  3668. + *
  3669. + * This may be doable with just the clocks interface, though this
  3670. + * packet does some other register setup from the firmware, too.
  3671. + */
  3672. +int
  3673. +vc4_v3d_set_power(struct vc4_dev *vc4, bool on)
  3674. +{
  3675. + u32 packet = on;
  3676. +
  3677. + return rpi_firmware_property(vc4->firmware,
  3678. + RPI_FIRMWARE_SET_ENABLE_QPU,
  3679. + &packet, sizeof(packet));
  3680. +}
  3681. +
  3682. +static void vc4_v3d_init_hw(struct drm_device *dev)
  3683. +{
  3684. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  3685. +
  3686. + /* Take all the memory that would have been reserved for user
  3687. + * QPU programs, since we don't have an interface for running
  3688. + * them, anyway.
  3689. + */
  3690. + V3D_WRITE(V3D_VPMBASE, 0);
  3691. +}
  3692. +
  3693. +static int vc4_v3d_bind(struct device *dev, struct device *master, void *data)
  3694. +{
  3695. + struct platform_device *pdev = to_platform_device(dev);
  3696. + struct drm_device *drm = dev_get_drvdata(master);
  3697. + struct vc4_dev *vc4 = to_vc4_dev(drm);
  3698. + struct vc4_v3d *v3d = NULL;
  3699. + int ret;
  3700. +
  3701. + v3d = devm_kzalloc(&pdev->dev, sizeof(*v3d), GFP_KERNEL);
  3702. + if (!v3d)
  3703. + return -ENOMEM;
  3704. +
  3705. + v3d->pdev = pdev;
  3706. +
  3707. + v3d->regs = vc4_ioremap_regs(pdev, 0);
  3708. + if (IS_ERR(v3d->regs))
  3709. + return PTR_ERR(v3d->regs);
  3710. +
  3711. + vc4->v3d = v3d;
  3712. +
  3713. + ret = vc4_v3d_set_power(vc4, true);
  3714. + if (ret)
  3715. + return ret;
  3716. +
  3717. + if (V3D_READ(V3D_IDENT0) != V3D_EXPECTED_IDENT0) {
  3718. + DRM_ERROR("V3D_IDENT0 read 0x%08x instead of 0x%08x\n",
  3719. + V3D_READ(V3D_IDENT0), V3D_EXPECTED_IDENT0);
  3720. + return -EINVAL;
  3721. + }
  3722. +
  3723. + /* Reset the binner overflow address/size at setup, to be sure
  3724. + * we don't reuse an old one.
  3725. + */
  3726. + V3D_WRITE(V3D_BPOA, 0);
  3727. + V3D_WRITE(V3D_BPOS, 0);
  3728. +
  3729. + vc4_v3d_init_hw(drm);
  3730. +
  3731. + ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
  3732. + if (ret) {
  3733. + DRM_ERROR("Failed to install IRQ handler\n");
  3734. + return ret;
  3735. + }
  3736. +
  3737. + return 0;
  3738. +}
  3739. +
  3740. +static void vc4_v3d_unbind(struct device *dev, struct device *master,
  3741. + void *data)
  3742. +{
  3743. + struct drm_device *drm = dev_get_drvdata(master);
  3744. + struct vc4_dev *vc4 = to_vc4_dev(drm);
  3745. +
  3746. + drm_irq_uninstall(drm);
  3747. +
  3748. + /* Disable the binner's overflow memory address, so the next
  3749. + * driver probe (if any) doesn't try to reuse our old
  3750. + * allocation.
  3751. + */
  3752. + V3D_WRITE(V3D_BPOA, 0);
  3753. + V3D_WRITE(V3D_BPOS, 0);
  3754. +
  3755. + vc4_v3d_set_power(vc4, false);
  3756. +
  3757. + vc4->v3d = NULL;
  3758. +}
  3759. +
  3760. +static const struct component_ops vc4_v3d_ops = {
  3761. + .bind = vc4_v3d_bind,
  3762. + .unbind = vc4_v3d_unbind,
  3763. +};
  3764. +
  3765. +static int vc4_v3d_dev_probe(struct platform_device *pdev)
  3766. +{
  3767. + return component_add(&pdev->dev, &vc4_v3d_ops);
  3768. +}
  3769. +
  3770. +static int vc4_v3d_dev_remove(struct platform_device *pdev)
  3771. +{
  3772. + component_del(&pdev->dev, &vc4_v3d_ops);
  3773. + return 0;
  3774. +}
  3775. +
  3776. +static const struct of_device_id vc4_v3d_dt_match[] = {
  3777. + { .compatible = "brcm,vc4-v3d" },
  3778. + {}
  3779. +};
  3780. +
  3781. +struct platform_driver vc4_v3d_driver = {
  3782. + .probe = vc4_v3d_dev_probe,
  3783. + .remove = vc4_v3d_dev_remove,
  3784. + .driver = {
  3785. + .name = "vc4_v3d",
  3786. + .of_match_table = vc4_v3d_dt_match,
  3787. + },
  3788. +};
  3789. --- /dev/null
  3790. +++ b/drivers/gpu/drm/vc4/vc4_validate.c
  3791. @@ -0,0 +1,958 @@
  3792. +/*
  3793. + * Copyright © 2014 Broadcom
  3794. + *
  3795. + * Permission is hereby granted, free of charge, to any person obtaining a
  3796. + * copy of this software and associated documentation files (the "Software"),
  3797. + * to deal in the Software without restriction, including without limitation
  3798. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  3799. + * and/or sell copies of the Software, and to permit persons to whom the
  3800. + * Software is furnished to do so, subject to the following conditions:
  3801. + *
  3802. + * The above copyright notice and this permission notice (including the next
  3803. + * paragraph) shall be included in all copies or substantial portions of the
  3804. + * Software.
  3805. + *
  3806. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3807. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3808. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  3809. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3810. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3811. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3812. + * IN THE SOFTWARE.
  3813. + */
  3814. +
  3815. +/**
  3816. + * Command list validator for VC4.
  3817. + *
  3818. + * The VC4 has no IOMMU between it and system memory. So, a user with
  3819. + * access to execute command lists could escalate privilege by
  3820. + * overwriting system memory (drawing to it as a framebuffer) or
  3821. + * reading system memory it shouldn't (reading it as a texture, or
  3822. + * uniform data, or vertex data).
  3823. + *
  3824. + * This validates command lists to ensure that all accesses are within
  3825. + * the bounds of the GEM objects referenced. It explicitly whitelists
  3826. + * packets, and looks at the offsets in any address fields to make
  3827. + * sure they're constrained within the BOs they reference.
  3828. + *
  3829. + * Note that because of the validation that's happening anyway, this
  3830. + * is where GEM relocation processing happens.
  3831. + */
  3832. +
  3833. +#include "uapi/drm/vc4_drm.h"
  3834. +#include "vc4_drv.h"
  3835. +#include "vc4_packet.h"
  3836. +
  3837. +#define VALIDATE_ARGS \
  3838. + struct vc4_exec_info *exec, \
  3839. + void *validated, \
  3840. + void *untrusted
  3841. +
  3842. +
  3843. +/** Return the width in pixels of a 64-byte microtile. */
  3844. +static uint32_t
  3845. +utile_width(int cpp)
  3846. +{
  3847. + switch (cpp) {
  3848. + case 1:
  3849. + case 2:
  3850. + return 8;
  3851. + case 4:
  3852. + return 4;
  3853. + case 8:
  3854. + return 2;
  3855. + default:
  3856. + DRM_ERROR("unknown cpp: %d\n", cpp);
  3857. + return 1;
  3858. + }
  3859. +}
  3860. +
  3861. +/** Return the height in pixels of a 64-byte microtile. */
  3862. +static uint32_t
  3863. +utile_height(int cpp)
  3864. +{
  3865. + switch (cpp) {
  3866. + case 1:
  3867. + return 8;
  3868. + case 2:
  3869. + case 4:
  3870. + case 8:
  3871. + return 4;
  3872. + default:
  3873. + DRM_ERROR("unknown cpp: %d\n", cpp);
  3874. + return 1;
  3875. + }
  3876. +}
  3877. +
  3878. +/**
  3879. + * The texture unit decides what tiling format a particular miplevel is using
  3880. + * this function, so we lay out our miptrees accordingly.
  3881. + */
  3882. +static bool
  3883. +size_is_lt(uint32_t width, uint32_t height, int cpp)
  3884. +{
  3885. + return (width <= 4 * utile_width(cpp) ||
  3886. + height <= 4 * utile_height(cpp));
  3887. +}
  3888. +
  3889. +bool
  3890. +vc4_use_bo(struct vc4_exec_info *exec,
  3891. + uint32_t hindex,
  3892. + enum vc4_bo_mode mode,
  3893. + struct drm_gem_cma_object **obj)
  3894. +{
  3895. + *obj = NULL;
  3896. +
  3897. + if (hindex >= exec->bo_count) {
  3898. + DRM_ERROR("BO index %d greater than BO count %d\n",
  3899. + hindex, exec->bo_count);
  3900. + return false;
  3901. + }
  3902. +
  3903. + if (exec->bo[hindex].mode != mode) {
  3904. + if (exec->bo[hindex].mode == VC4_MODE_UNDECIDED) {
  3905. + exec->bo[hindex].mode = mode;
  3906. + } else {
  3907. + DRM_ERROR("BO index %d reused with mode %d vs %d\n",
  3908. + hindex, exec->bo[hindex].mode, mode);
  3909. + return false;
  3910. + }
  3911. + }
  3912. +
  3913. + *obj = exec->bo[hindex].bo;
  3914. + return true;
  3915. +}
  3916. +
  3917. +static bool
  3918. +vc4_use_handle(struct vc4_exec_info *exec,
  3919. + uint32_t gem_handles_packet_index,
  3920. + enum vc4_bo_mode mode,
  3921. + struct drm_gem_cma_object **obj)
  3922. +{
  3923. + return vc4_use_bo(exec, exec->bo_index[gem_handles_packet_index],
  3924. + mode, obj);
  3925. +}
  3926. +
  3927. +static uint32_t
  3928. +gl_shader_rec_size(uint32_t pointer_bits)
  3929. +{
  3930. + uint32_t attribute_count = pointer_bits & 7;
  3931. + bool extended = pointer_bits & 8;
  3932. +
  3933. + if (attribute_count == 0)
  3934. + attribute_count = 8;
  3935. +
  3936. + if (extended)
  3937. + return 100 + attribute_count * 4;
  3938. + else
  3939. + return 36 + attribute_count * 8;
  3940. +}
  3941. +
  3942. +bool
  3943. +vc4_check_tex_size(struct vc4_exec_info *exec, struct drm_gem_cma_object *fbo,
  3944. + uint32_t offset, uint8_t tiling_format,
  3945. + uint32_t width, uint32_t height, uint8_t cpp)
  3946. +{
  3947. + uint32_t aligned_width, aligned_height, stride, size;
  3948. + uint32_t utile_w = utile_width(cpp);
  3949. + uint32_t utile_h = utile_height(cpp);
  3950. +
  3951. + /* The shaded vertex format stores signed 12.4 fixed point
  3952. + * (-2048,2047) offsets from the viewport center, so we should
  3953. + * never have a render target larger than 4096. The texture
  3954. + * unit can only sample from 2048x2048, so it's even more
  3955. + * restricted. This lets us avoid worrying about overflow in
  3956. + * our math.
  3957. + */
  3958. + if (width > 4096 || height > 4096) {
  3959. + DRM_ERROR("Surface dimesions (%d,%d) too large", width, height);
  3960. + return false;
  3961. + }
  3962. +
  3963. + switch (tiling_format) {
  3964. + case VC4_TILING_FORMAT_LINEAR:
  3965. + aligned_width = round_up(width, utile_w);
  3966. + aligned_height = height;
  3967. + break;
  3968. + case VC4_TILING_FORMAT_T:
  3969. + aligned_width = round_up(width, utile_w * 8);
  3970. + aligned_height = round_up(height, utile_h * 8);
  3971. + break;
  3972. + case VC4_TILING_FORMAT_LT:
  3973. + aligned_width = round_up(width, utile_w);
  3974. + aligned_height = round_up(height, utile_h);
  3975. + break;
  3976. + default:
  3977. + DRM_ERROR("buffer tiling %d unsupported\n", tiling_format);
  3978. + return false;
  3979. + }
  3980. +
  3981. + stride = aligned_width * cpp;
  3982. + size = stride * aligned_height;
  3983. +
  3984. + if (size + offset < size ||
  3985. + size + offset > fbo->base.size) {
  3986. + DRM_ERROR("Overflow in %dx%d (%dx%d) fbo size (%d + %d > %d)\n",
  3987. + width, height,
  3988. + aligned_width, aligned_height,
  3989. + size, offset, fbo->base.size);
  3990. + return false;
  3991. + }
  3992. +
  3993. + return true;
  3994. +}
  3995. +
  3996. +static int
  3997. +validate_flush_all(VALIDATE_ARGS)
  3998. +{
  3999. + if (exec->found_increment_semaphore_packet) {
  4000. + DRM_ERROR("VC4_PACKET_FLUSH_ALL after "
  4001. + "VC4_PACKET_INCREMENT_SEMAPHORE\n");
  4002. + return -EINVAL;
  4003. + }
  4004. +
  4005. + return 0;
  4006. +}
  4007. +
  4008. +static int
  4009. +validate_start_tile_binning(VALIDATE_ARGS)
  4010. +{
  4011. + if (exec->found_start_tile_binning_packet) {
  4012. + DRM_ERROR("Duplicate VC4_PACKET_START_TILE_BINNING\n");
  4013. + return -EINVAL;
  4014. + }
  4015. + exec->found_start_tile_binning_packet = true;
  4016. +
  4017. + if (!exec->found_tile_binning_mode_config_packet) {
  4018. + DRM_ERROR("missing VC4_PACKET_TILE_BINNING_MODE_CONFIG\n");
  4019. + return -EINVAL;
  4020. + }
  4021. +
  4022. + return 0;
  4023. +}
  4024. +
  4025. +static int
  4026. +validate_increment_semaphore(VALIDATE_ARGS)
  4027. +{
  4028. + if (exec->found_increment_semaphore_packet) {
  4029. + DRM_ERROR("Duplicate VC4_PACKET_INCREMENT_SEMAPHORE\n");
  4030. + return -EINVAL;
  4031. + }
  4032. + exec->found_increment_semaphore_packet = true;
  4033. +
  4034. + /* Once we've found the semaphore increment, there should be one FLUSH
  4035. + * then the end of the command list. The FLUSH actually triggers the
  4036. + * increment, so we only need to make sure there
  4037. + */
  4038. +
  4039. + return 0;
  4040. +}
  4041. +
  4042. +static int
  4043. +validate_indexed_prim_list(VALIDATE_ARGS)
  4044. +{
  4045. + struct drm_gem_cma_object *ib;
  4046. + uint32_t length = *(uint32_t *)(untrusted + 1);
  4047. + uint32_t offset = *(uint32_t *)(untrusted + 5);
  4048. + uint32_t max_index = *(uint32_t *)(untrusted + 9);
  4049. + uint32_t index_size = (*(uint8_t *)(untrusted + 0) >> 4) ? 2 : 1;
  4050. + struct vc4_shader_state *shader_state;
  4051. +
  4052. + if (exec->found_increment_semaphore_packet) {
  4053. + DRM_ERROR("Drawing after VC4_PACKET_INCREMENT_SEMAPHORE\n");
  4054. + return -EINVAL;
  4055. + }
  4056. +
  4057. + /* Check overflow condition */
  4058. + if (exec->shader_state_count == 0) {
  4059. + DRM_ERROR("shader state must precede primitives\n");
  4060. + return -EINVAL;
  4061. + }
  4062. + shader_state = &exec->shader_state[exec->shader_state_count - 1];
  4063. +
  4064. + if (max_index > shader_state->max_index)
  4065. + shader_state->max_index = max_index;
  4066. +
  4067. + if (!vc4_use_handle(exec, 0, VC4_MODE_RENDER, &ib))
  4068. + return -EINVAL;
  4069. +
  4070. + if (offset > ib->base.size ||
  4071. + (ib->base.size - offset) / index_size < length) {
  4072. + DRM_ERROR("IB access overflow (%d + %d*%d > %d)\n",
  4073. + offset, length, index_size, ib->base.size);
  4074. + return -EINVAL;
  4075. + }
  4076. +
  4077. + *(uint32_t *)(validated + 5) = ib->paddr + offset;
  4078. +
  4079. + return 0;
  4080. +}
  4081. +
  4082. +static int
  4083. +validate_gl_array_primitive(VALIDATE_ARGS)
  4084. +{
  4085. + uint32_t length = *(uint32_t *)(untrusted + 1);
  4086. + uint32_t base_index = *(uint32_t *)(untrusted + 5);
  4087. + uint32_t max_index;
  4088. + struct vc4_shader_state *shader_state;
  4089. +
  4090. + if (exec->found_increment_semaphore_packet) {
  4091. + DRM_ERROR("Drawing after VC4_PACKET_INCREMENT_SEMAPHORE\n");
  4092. + return -EINVAL;
  4093. + }
  4094. +
  4095. + /* Check overflow condition */
  4096. + if (exec->shader_state_count == 0) {
  4097. + DRM_ERROR("shader state must precede primitives\n");
  4098. + return -EINVAL;
  4099. + }
  4100. + shader_state = &exec->shader_state[exec->shader_state_count - 1];
  4101. +
  4102. + if (length + base_index < length) {
  4103. + DRM_ERROR("primitive vertex count overflow\n");
  4104. + return -EINVAL;
  4105. + }
  4106. + max_index = length + base_index - 1;
  4107. +
  4108. + if (max_index > shader_state->max_index)
  4109. + shader_state->max_index = max_index;
  4110. +
  4111. + return 0;
  4112. +}
  4113. +
  4114. +static int
  4115. +validate_gl_shader_state(VALIDATE_ARGS)
  4116. +{
  4117. + uint32_t i = exec->shader_state_count++;
  4118. +
  4119. + if (i >= exec->shader_state_size) {
  4120. + DRM_ERROR("More requests for shader states than declared\n");
  4121. + return -EINVAL;
  4122. + }
  4123. +
  4124. + exec->shader_state[i].packet = VC4_PACKET_GL_SHADER_STATE;
  4125. + exec->shader_state[i].addr = *(uint32_t *)untrusted;
  4126. + exec->shader_state[i].max_index = 0;
  4127. +
  4128. + if (exec->shader_state[i].addr & ~0xf) {
  4129. + DRM_ERROR("high bits set in GL shader rec reference\n");
  4130. + return -EINVAL;
  4131. + }
  4132. +
  4133. + *(uint32_t *)validated = (exec->shader_rec_p +
  4134. + exec->shader_state[i].addr);
  4135. +
  4136. + exec->shader_rec_p +=
  4137. + roundup(gl_shader_rec_size(exec->shader_state[i].addr), 16);
  4138. +
  4139. + return 0;
  4140. +}
  4141. +
  4142. +static int
  4143. +validate_nv_shader_state(VALIDATE_ARGS)
  4144. +{
  4145. + uint32_t i = exec->shader_state_count++;
  4146. +
  4147. + if (i >= exec->shader_state_size) {
  4148. + DRM_ERROR("More requests for shader states than declared\n");
  4149. + return -EINVAL;
  4150. + }
  4151. +
  4152. + exec->shader_state[i].packet = VC4_PACKET_NV_SHADER_STATE;
  4153. + exec->shader_state[i].addr = *(uint32_t *)untrusted;
  4154. +
  4155. + if (exec->shader_state[i].addr & 15) {
  4156. + DRM_ERROR("NV shader state address 0x%08x misaligned\n",
  4157. + exec->shader_state[i].addr);
  4158. + return -EINVAL;
  4159. + }
  4160. +
  4161. + *(uint32_t *)validated = (exec->shader_state[i].addr +
  4162. + exec->shader_rec_p);
  4163. +
  4164. + return 0;
  4165. +}
  4166. +
  4167. +static int
  4168. +validate_tile_binning_config(VALIDATE_ARGS)
  4169. +{
  4170. + struct drm_device *dev = exec->exec_bo->base.dev;
  4171. + uint8_t flags;
  4172. + uint32_t tile_state_size, tile_alloc_size;
  4173. + uint32_t tile_count;
  4174. +
  4175. + if (exec->found_tile_binning_mode_config_packet) {
  4176. + DRM_ERROR("Duplicate VC4_PACKET_TILE_BINNING_MODE_CONFIG\n");
  4177. + return -EINVAL;
  4178. + }
  4179. + exec->found_tile_binning_mode_config_packet = true;
  4180. +
  4181. + exec->bin_tiles_x = *(uint8_t *)(untrusted + 12);
  4182. + exec->bin_tiles_y = *(uint8_t *)(untrusted + 13);
  4183. + tile_count = exec->bin_tiles_x * exec->bin_tiles_y;
  4184. + flags = *(uint8_t *)(untrusted + 14);
  4185. +
  4186. + if (exec->bin_tiles_x == 0 ||
  4187. + exec->bin_tiles_y == 0) {
  4188. + DRM_ERROR("Tile binning config of %dx%d too small\n",
  4189. + exec->bin_tiles_x, exec->bin_tiles_y);
  4190. + return -EINVAL;
  4191. + }
  4192. +
  4193. + if (flags & (VC4_BIN_CONFIG_DB_NON_MS |
  4194. + VC4_BIN_CONFIG_TILE_BUFFER_64BIT |
  4195. + VC4_BIN_CONFIG_MS_MODE_4X)) {
  4196. + DRM_ERROR("unsupported bining config flags 0x%02x\n", flags);
  4197. + return -EINVAL;
  4198. + }
  4199. +
  4200. + /* The tile state data array is 48 bytes per tile, and we put it at
  4201. + * the start of a BO containing both it and the tile alloc.
  4202. + */
  4203. + tile_state_size = 48 * tile_count;
  4204. +
  4205. + /* Since the tile alloc array will follow us, align. */
  4206. + exec->tile_alloc_offset = roundup(tile_state_size, 4096);
  4207. +
  4208. + *(uint8_t *)(validated + 14) =
  4209. + ((flags & ~(VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_MASK |
  4210. + VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_MASK)) |
  4211. + VC4_BIN_CONFIG_AUTO_INIT_TSDA |
  4212. + VC4_SET_FIELD(VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_32,
  4213. + VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE) |
  4214. + VC4_SET_FIELD(VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_128,
  4215. + VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE));
  4216. +
  4217. + /* Initial block size. */
  4218. + tile_alloc_size = 32 * tile_count;
  4219. +
  4220. + /*
  4221. + * The initial allocation gets rounded to the next 256 bytes before
  4222. + * the hardware starts fulfilling further allocations.
  4223. + */
  4224. + tile_alloc_size = roundup(tile_alloc_size, 256);
  4225. +
  4226. + /* Add space for the extra allocations. This is what gets used first,
  4227. + * before overflow memory. It must have at least 4096 bytes, but we
  4228. + * want to avoid overflow memory usage if possible.
  4229. + */
  4230. + tile_alloc_size += 1024 * 1024;
  4231. +
  4232. + exec->tile_bo = &vc4_bo_create(dev, exec->tile_alloc_offset +
  4233. + tile_alloc_size)->base;
  4234. + if (!exec->tile_bo)
  4235. + return -ENOMEM;
  4236. + list_add_tail(&to_vc4_bo(&exec->tile_bo->base)->unref_head,
  4237. + &exec->unref_list);
  4238. +
  4239. + /* tile alloc address. */
  4240. + *(uint32_t *)(validated + 0) = (exec->tile_bo->paddr +
  4241. + exec->tile_alloc_offset);
  4242. + /* tile alloc size. */
  4243. + *(uint32_t *)(validated + 4) = tile_alloc_size;
  4244. + /* tile state address. */
  4245. + *(uint32_t *)(validated + 8) = exec->tile_bo->paddr;
  4246. +
  4247. + return 0;
  4248. +}
  4249. +
  4250. +static int
  4251. +validate_gem_handles(VALIDATE_ARGS)
  4252. +{
  4253. + memcpy(exec->bo_index, untrusted, sizeof(exec->bo_index));
  4254. + return 0;
  4255. +}
  4256. +
  4257. +#define VC4_DEFINE_PACKET(packet, name, func) \
  4258. + [packet] = { packet ## _SIZE, name, func }
  4259. +
  4260. +static const struct cmd_info {
  4261. + uint16_t len;
  4262. + const char *name;
  4263. + int (*func)(struct vc4_exec_info *exec, void *validated,
  4264. + void *untrusted);
  4265. +} cmd_info[] = {
  4266. + VC4_DEFINE_PACKET(VC4_PACKET_HALT, "halt", NULL),
  4267. + VC4_DEFINE_PACKET(VC4_PACKET_NOP, "nop", NULL),
  4268. + VC4_DEFINE_PACKET(VC4_PACKET_FLUSH, "flush", NULL),
  4269. + VC4_DEFINE_PACKET(VC4_PACKET_FLUSH_ALL, "flush all state", validate_flush_all),
  4270. + VC4_DEFINE_PACKET(VC4_PACKET_START_TILE_BINNING, "start tile binning", validate_start_tile_binning),
  4271. + VC4_DEFINE_PACKET(VC4_PACKET_INCREMENT_SEMAPHORE, "increment semaphore", validate_increment_semaphore),
  4272. +
  4273. + VC4_DEFINE_PACKET(VC4_PACKET_GL_INDEXED_PRIMITIVE, "Indexed Primitive List", validate_indexed_prim_list),
  4274. +
  4275. + VC4_DEFINE_PACKET(VC4_PACKET_GL_ARRAY_PRIMITIVE, "Vertex Array Primitives", validate_gl_array_primitive),
  4276. +
  4277. + /* This is only used by clipped primitives (packets 48 and 49), which
  4278. + * we don't support parsing yet.
  4279. + */
  4280. + VC4_DEFINE_PACKET(VC4_PACKET_PRIMITIVE_LIST_FORMAT, "primitive list format", NULL),
  4281. +
  4282. + VC4_DEFINE_PACKET(VC4_PACKET_GL_SHADER_STATE, "GL Shader State", validate_gl_shader_state),
  4283. + VC4_DEFINE_PACKET(VC4_PACKET_NV_SHADER_STATE, "NV Shader State", validate_nv_shader_state),
  4284. +
  4285. + VC4_DEFINE_PACKET(VC4_PACKET_CONFIGURATION_BITS, "configuration bits", NULL),
  4286. + VC4_DEFINE_PACKET(VC4_PACKET_FLAT_SHADE_FLAGS, "flat shade flags", NULL),
  4287. + VC4_DEFINE_PACKET(VC4_PACKET_POINT_SIZE, "point size", NULL),
  4288. + VC4_DEFINE_PACKET(VC4_PACKET_LINE_WIDTH, "line width", NULL),
  4289. + VC4_DEFINE_PACKET(VC4_PACKET_RHT_X_BOUNDARY, "RHT X boundary", NULL),
  4290. + VC4_DEFINE_PACKET(VC4_PACKET_DEPTH_OFFSET, "Depth Offset", NULL),
  4291. + VC4_DEFINE_PACKET(VC4_PACKET_CLIP_WINDOW, "Clip Window", NULL),
  4292. + VC4_DEFINE_PACKET(VC4_PACKET_VIEWPORT_OFFSET, "Viewport Offset", NULL),
  4293. + VC4_DEFINE_PACKET(VC4_PACKET_CLIPPER_XY_SCALING, "Clipper XY Scaling", NULL),
  4294. + /* Note: The docs say this was also 105, but it was 106 in the
  4295. + * initial userland code drop.
  4296. + */
  4297. + VC4_DEFINE_PACKET(VC4_PACKET_CLIPPER_Z_SCALING, "Clipper Z Scale and Offset", NULL),
  4298. +
  4299. + VC4_DEFINE_PACKET(VC4_PACKET_TILE_BINNING_MODE_CONFIG, "tile binning configuration", validate_tile_binning_config),
  4300. +
  4301. + VC4_DEFINE_PACKET(VC4_PACKET_GEM_HANDLES, "GEM handles", validate_gem_handles),
  4302. +};
  4303. +
  4304. +int
  4305. +vc4_validate_bin_cl(struct drm_device *dev,
  4306. + void *validated,
  4307. + void *unvalidated,
  4308. + struct vc4_exec_info *exec)
  4309. +{
  4310. + uint32_t len = exec->args->bin_cl_size;
  4311. + uint32_t dst_offset = 0;
  4312. + uint32_t src_offset = 0;
  4313. +
  4314. + while (src_offset < len) {
  4315. + void *dst_pkt = validated + dst_offset;
  4316. + void *src_pkt = unvalidated + src_offset;
  4317. + u8 cmd = *(uint8_t *)src_pkt;
  4318. + const struct cmd_info *info;
  4319. +
  4320. + if (cmd > ARRAY_SIZE(cmd_info)) {
  4321. + DRM_ERROR("0x%08x: packet %d out of bounds\n",
  4322. + src_offset, cmd);
  4323. + return -EINVAL;
  4324. + }
  4325. +
  4326. + info = &cmd_info[cmd];
  4327. + if (!info->name) {
  4328. + DRM_ERROR("0x%08x: packet %d invalid\n",
  4329. + src_offset, cmd);
  4330. + return -EINVAL;
  4331. + }
  4332. +
  4333. +#if 0
  4334. + DRM_INFO("0x%08x: packet %d (%s) size %d processing...\n",
  4335. + src_offset, cmd, info->name, info->len);
  4336. +#endif
  4337. +
  4338. + if (src_offset + info->len > len) {
  4339. + DRM_ERROR("0x%08x: packet %d (%s) length 0x%08x "
  4340. + "exceeds bounds (0x%08x)\n",
  4341. + src_offset, cmd, info->name, info->len,
  4342. + src_offset + len);
  4343. + return -EINVAL;
  4344. + }
  4345. +
  4346. + if (cmd != VC4_PACKET_GEM_HANDLES)
  4347. + memcpy(dst_pkt, src_pkt, info->len);
  4348. +
  4349. + if (info->func && info->func(exec,
  4350. + dst_pkt + 1,
  4351. + src_pkt + 1)) {
  4352. + DRM_ERROR("0x%08x: packet %d (%s) failed to "
  4353. + "validate\n",
  4354. + src_offset, cmd, info->name);
  4355. + return -EINVAL;
  4356. + }
  4357. +
  4358. + src_offset += info->len;
  4359. + /* GEM handle loading doesn't produce HW packets. */
  4360. + if (cmd != VC4_PACKET_GEM_HANDLES)
  4361. + dst_offset += info->len;
  4362. +
  4363. + /* When the CL hits halt, it'll stop reading anything else. */
  4364. + if (cmd == VC4_PACKET_HALT)
  4365. + break;
  4366. + }
  4367. +
  4368. + exec->ct0ea = exec->ct0ca + dst_offset;
  4369. +
  4370. + if (!exec->found_start_tile_binning_packet) {
  4371. + DRM_ERROR("Bin CL missing VC4_PACKET_START_TILE_BINNING\n");
  4372. + return -EINVAL;
  4373. + }
  4374. +
  4375. + if (!exec->found_increment_semaphore_packet) {
  4376. + DRM_ERROR("Bin CL missing VC4_PACKET_INCREMENT_SEMAPHORE\n");
  4377. + return -EINVAL;
  4378. + }
  4379. +
  4380. + return 0;
  4381. +}
  4382. +
  4383. +static bool
  4384. +reloc_tex(struct vc4_exec_info *exec,
  4385. + void *uniform_data_u,
  4386. + struct vc4_texture_sample_info *sample,
  4387. + uint32_t texture_handle_index)
  4388. +
  4389. +{
  4390. + struct drm_gem_cma_object *tex;
  4391. + uint32_t p0 = *(uint32_t *)(uniform_data_u + sample->p_offset[0]);
  4392. + uint32_t p1 = *(uint32_t *)(uniform_data_u + sample->p_offset[1]);
  4393. + uint32_t p2 = (sample->p_offset[2] != ~0 ?
  4394. + *(uint32_t *)(uniform_data_u + sample->p_offset[2]) : 0);
  4395. + uint32_t p3 = (sample->p_offset[3] != ~0 ?
  4396. + *(uint32_t *)(uniform_data_u + sample->p_offset[3]) : 0);
  4397. + uint32_t *validated_p0 = exec->uniforms_v + sample->p_offset[0];
  4398. + uint32_t offset = p0 & VC4_TEX_P0_OFFSET_MASK;
  4399. + uint32_t miplevels = VC4_GET_FIELD(p0, VC4_TEX_P0_MIPLVLS);
  4400. + uint32_t width = VC4_GET_FIELD(p1, VC4_TEX_P1_WIDTH);
  4401. + uint32_t height = VC4_GET_FIELD(p1, VC4_TEX_P1_HEIGHT);
  4402. + uint32_t cpp, tiling_format, utile_w, utile_h;
  4403. + uint32_t i;
  4404. + uint32_t cube_map_stride = 0;
  4405. + enum vc4_texture_data_type type;
  4406. +
  4407. + if (!vc4_use_bo(exec, texture_handle_index, VC4_MODE_RENDER, &tex))
  4408. + return false;
  4409. +
  4410. + if (sample->is_direct) {
  4411. + uint32_t remaining_size = tex->base.size - p0;
  4412. + if (p0 > tex->base.size - 4) {
  4413. + DRM_ERROR("UBO offset greater than UBO size\n");
  4414. + goto fail;
  4415. + }
  4416. + if (p1 > remaining_size - 4) {
  4417. + DRM_ERROR("UBO clamp would allow reads outside of UBO\n");
  4418. + goto fail;
  4419. + }
  4420. + *validated_p0 = tex->paddr + p0;
  4421. + return true;
  4422. + }
  4423. +
  4424. + if (width == 0)
  4425. + width = 2048;
  4426. + if (height == 0)
  4427. + height = 2048;
  4428. +
  4429. + if (p0 & VC4_TEX_P0_CMMODE_MASK) {
  4430. + if (VC4_GET_FIELD(p2, VC4_TEX_P2_PTYPE) ==
  4431. + VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE)
  4432. + cube_map_stride = p2 & VC4_TEX_P2_CMST_MASK;
  4433. + if (VC4_GET_FIELD(p3, VC4_TEX_P2_PTYPE) ==
  4434. + VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE) {
  4435. + if (cube_map_stride) {
  4436. + DRM_ERROR("Cube map stride set twice\n");
  4437. + goto fail;
  4438. + }
  4439. +
  4440. + cube_map_stride = p3 & VC4_TEX_P2_CMST_MASK;
  4441. + }
  4442. + if (!cube_map_stride) {
  4443. + DRM_ERROR("Cube map stride not set\n");
  4444. + goto fail;
  4445. + }
  4446. + }
  4447. +
  4448. + type = (VC4_GET_FIELD(p0, VC4_TEX_P0_TYPE) |
  4449. + (VC4_GET_FIELD(p1, VC4_TEX_P1_TYPE4) << 4));
  4450. +
  4451. + switch (type) {
  4452. + case VC4_TEXTURE_TYPE_RGBA8888:
  4453. + case VC4_TEXTURE_TYPE_RGBX8888:
  4454. + case VC4_TEXTURE_TYPE_RGBA32R:
  4455. + cpp = 4;
  4456. + break;
  4457. + case VC4_TEXTURE_TYPE_RGBA4444:
  4458. + case VC4_TEXTURE_TYPE_RGBA5551:
  4459. + case VC4_TEXTURE_TYPE_RGB565:
  4460. + case VC4_TEXTURE_TYPE_LUMALPHA:
  4461. + case VC4_TEXTURE_TYPE_S16F:
  4462. + case VC4_TEXTURE_TYPE_S16:
  4463. + cpp = 2;
  4464. + break;
  4465. + case VC4_TEXTURE_TYPE_LUMINANCE:
  4466. + case VC4_TEXTURE_TYPE_ALPHA:
  4467. + case VC4_TEXTURE_TYPE_S8:
  4468. + cpp = 1;
  4469. + break;
  4470. + case VC4_TEXTURE_TYPE_ETC1:
  4471. + case VC4_TEXTURE_TYPE_BW1:
  4472. + case VC4_TEXTURE_TYPE_A4:
  4473. + case VC4_TEXTURE_TYPE_A1:
  4474. + case VC4_TEXTURE_TYPE_RGBA64:
  4475. + case VC4_TEXTURE_TYPE_YUV422R:
  4476. + default:
  4477. + DRM_ERROR("Texture format %d unsupported\n", type);
  4478. + goto fail;
  4479. + }
  4480. + utile_w = utile_width(cpp);
  4481. + utile_h = utile_height(cpp);
  4482. +
  4483. + if (type == VC4_TEXTURE_TYPE_RGBA32R) {
  4484. + tiling_format = VC4_TILING_FORMAT_LINEAR;
  4485. + } else {
  4486. + if (size_is_lt(width, height, cpp))
  4487. + tiling_format = VC4_TILING_FORMAT_LT;
  4488. + else
  4489. + tiling_format = VC4_TILING_FORMAT_T;
  4490. + }
  4491. +
  4492. + if (!vc4_check_tex_size(exec, tex, offset + cube_map_stride * 5,
  4493. + tiling_format, width, height, cpp)) {
  4494. + goto fail;
  4495. + }
  4496. +
  4497. + /* The mipmap levels are stored before the base of the texture. Make
  4498. + * sure there is actually space in the BO.
  4499. + */
  4500. + for (i = 1; i <= miplevels; i++) {
  4501. + uint32_t level_width = max(width >> i, 1u);
  4502. + uint32_t level_height = max(height >> i, 1u);
  4503. + uint32_t aligned_width, aligned_height;
  4504. + uint32_t level_size;
  4505. +
  4506. + /* Once the levels get small enough, they drop from T to LT. */
  4507. + if (tiling_format == VC4_TILING_FORMAT_T &&
  4508. + size_is_lt(level_width, level_height, cpp)) {
  4509. + tiling_format = VC4_TILING_FORMAT_LT;
  4510. + }
  4511. +
  4512. + switch (tiling_format) {
  4513. + case VC4_TILING_FORMAT_T:
  4514. + aligned_width = round_up(level_width, utile_w * 8);
  4515. + aligned_height = round_up(level_height, utile_h * 8);
  4516. + break;
  4517. + case VC4_TILING_FORMAT_LT:
  4518. + aligned_width = round_up(level_width, utile_w);
  4519. + aligned_height = round_up(level_height, utile_h);
  4520. + break;
  4521. + default:
  4522. + aligned_width = round_up(level_width, utile_w);
  4523. + aligned_height = level_height;
  4524. + break;
  4525. + }
  4526. +
  4527. + level_size = aligned_width * cpp * aligned_height;
  4528. +
  4529. + if (offset < level_size) {
  4530. + DRM_ERROR("Level %d (%dx%d -> %dx%d) size %db "
  4531. + "overflowed buffer bounds (offset %d)\n",
  4532. + i, level_width, level_height,
  4533. + aligned_width, aligned_height,
  4534. + level_size, offset);
  4535. + goto fail;
  4536. + }
  4537. +
  4538. + offset -= level_size;
  4539. + }
  4540. +
  4541. + *validated_p0 = tex->paddr + p0;
  4542. +
  4543. + return true;
  4544. + fail:
  4545. + DRM_INFO("Texture p0 at %d: 0x%08x\n", sample->p_offset[0], p0);
  4546. + DRM_INFO("Texture p1 at %d: 0x%08x\n", sample->p_offset[1], p1);
  4547. + DRM_INFO("Texture p2 at %d: 0x%08x\n", sample->p_offset[2], p2);
  4548. + DRM_INFO("Texture p3 at %d: 0x%08x\n", sample->p_offset[3], p3);
  4549. + return false;
  4550. +}
  4551. +
  4552. +static int
  4553. +validate_shader_rec(struct drm_device *dev,
  4554. + struct vc4_exec_info *exec,
  4555. + struct vc4_shader_state *state)
  4556. +{
  4557. + uint32_t *src_handles;
  4558. + void *pkt_u, *pkt_v;
  4559. + enum shader_rec_reloc_type {
  4560. + RELOC_CODE,
  4561. + RELOC_VBO,
  4562. + };
  4563. + struct shader_rec_reloc {
  4564. + enum shader_rec_reloc_type type;
  4565. + uint32_t offset;
  4566. + };
  4567. + static const struct shader_rec_reloc gl_relocs[] = {
  4568. + { RELOC_CODE, 4 }, /* fs */
  4569. + { RELOC_CODE, 16 }, /* vs */
  4570. + { RELOC_CODE, 28 }, /* cs */
  4571. + };
  4572. + static const struct shader_rec_reloc nv_relocs[] = {
  4573. + { RELOC_CODE, 4 }, /* fs */
  4574. + { RELOC_VBO, 12 }
  4575. + };
  4576. + const struct shader_rec_reloc *relocs;
  4577. + struct drm_gem_cma_object *bo[ARRAY_SIZE(gl_relocs) + 8];
  4578. + uint32_t nr_attributes = 0, nr_fixed_relocs, nr_relocs, packet_size;
  4579. + int i;
  4580. + struct vc4_validated_shader_info *validated_shader;
  4581. +
  4582. + if (state->packet == VC4_PACKET_NV_SHADER_STATE) {
  4583. + relocs = nv_relocs;
  4584. + nr_fixed_relocs = ARRAY_SIZE(nv_relocs);
  4585. +
  4586. + packet_size = 16;
  4587. + } else {
  4588. + relocs = gl_relocs;
  4589. + nr_fixed_relocs = ARRAY_SIZE(gl_relocs);
  4590. +
  4591. + nr_attributes = state->addr & 0x7;
  4592. + if (nr_attributes == 0)
  4593. + nr_attributes = 8;
  4594. + packet_size = gl_shader_rec_size(state->addr);
  4595. + }
  4596. + nr_relocs = nr_fixed_relocs + nr_attributes;
  4597. +
  4598. + if (nr_relocs * 4 > exec->shader_rec_size) {
  4599. + DRM_ERROR("overflowed shader recs reading %d handles "
  4600. + "from %d bytes left\n",
  4601. + nr_relocs, exec->shader_rec_size);
  4602. + return -EINVAL;
  4603. + }
  4604. + src_handles = exec->shader_rec_u;
  4605. + exec->shader_rec_u += nr_relocs * 4;
  4606. + exec->shader_rec_size -= nr_relocs * 4;
  4607. +
  4608. + if (packet_size > exec->shader_rec_size) {
  4609. + DRM_ERROR("overflowed shader recs copying %db packet "
  4610. + "from %d bytes left\n",
  4611. + packet_size, exec->shader_rec_size);
  4612. + return -EINVAL;
  4613. + }
  4614. + pkt_u = exec->shader_rec_u;
  4615. + pkt_v = exec->shader_rec_v;
  4616. + memcpy(pkt_v, pkt_u, packet_size);
  4617. + exec->shader_rec_u += packet_size;
  4618. + /* Shader recs have to be aligned to 16 bytes (due to the attribute
  4619. + * flags being in the low bytes), so round the next validated shader
  4620. + * rec address up. This should be safe, since we've got so many
  4621. + * relocations in a shader rec packet.
  4622. + */
  4623. + BUG_ON(roundup(packet_size, 16) - packet_size > nr_relocs * 4);
  4624. + exec->shader_rec_v += roundup(packet_size, 16);
  4625. + exec->shader_rec_size -= packet_size;
  4626. +
  4627. + for (i = 0; i < nr_relocs; i++) {
  4628. + enum vc4_bo_mode mode;
  4629. +
  4630. + if (i < nr_fixed_relocs && relocs[i].type == RELOC_CODE)
  4631. + mode = VC4_MODE_SHADER;
  4632. + else
  4633. + mode = VC4_MODE_RENDER;
  4634. +
  4635. + if (!vc4_use_bo(exec, src_handles[i], mode, &bo[i])) {
  4636. + return false;
  4637. + }
  4638. + }
  4639. +
  4640. + for (i = 0; i < nr_fixed_relocs; i++) {
  4641. + uint32_t o = relocs[i].offset;
  4642. + uint32_t src_offset = *(uint32_t *)(pkt_u + o);
  4643. + uint32_t *texture_handles_u;
  4644. + void *uniform_data_u;
  4645. + uint32_t tex;
  4646. +
  4647. + *(uint32_t *)(pkt_v + o) = bo[i]->paddr + src_offset;
  4648. +
  4649. + switch (relocs[i].type) {
  4650. + case RELOC_CODE:
  4651. + if (src_offset != 0) {
  4652. + DRM_ERROR("Shaders must be at offset 0 of "
  4653. + "the BO.\n");
  4654. + goto fail;
  4655. + }
  4656. +
  4657. + validated_shader = to_vc4_bo(&bo[i]->base)->validated_shader;
  4658. + if (!validated_shader)
  4659. + goto fail;
  4660. +
  4661. + if (validated_shader->uniforms_src_size >
  4662. + exec->uniforms_size) {
  4663. + DRM_ERROR("Uniforms src buffer overflow\n");
  4664. + goto fail;
  4665. + }
  4666. +
  4667. + texture_handles_u = exec->uniforms_u;
  4668. + uniform_data_u = (texture_handles_u +
  4669. + validated_shader->num_texture_samples);
  4670. +
  4671. + memcpy(exec->uniforms_v, uniform_data_u,
  4672. + validated_shader->uniforms_size);
  4673. +
  4674. + for (tex = 0;
  4675. + tex < validated_shader->num_texture_samples;
  4676. + tex++) {
  4677. + if (!reloc_tex(exec,
  4678. + uniform_data_u,
  4679. + &validated_shader->texture_samples[tex],
  4680. + texture_handles_u[tex])) {
  4681. + goto fail;
  4682. + }
  4683. + }
  4684. +
  4685. + *(uint32_t *)(pkt_v + o + 4) = exec->uniforms_p;
  4686. +
  4687. + exec->uniforms_u += validated_shader->uniforms_src_size;
  4688. + exec->uniforms_v += validated_shader->uniforms_size;
  4689. + exec->uniforms_p += validated_shader->uniforms_size;
  4690. +
  4691. + break;
  4692. +
  4693. + case RELOC_VBO:
  4694. + break;
  4695. + }
  4696. + }
  4697. +
  4698. + for (i = 0; i < nr_attributes; i++) {
  4699. + struct drm_gem_cma_object *vbo = bo[nr_fixed_relocs + i];
  4700. + uint32_t o = 36 + i * 8;
  4701. + uint32_t offset = *(uint32_t *)(pkt_u + o + 0);
  4702. + uint32_t attr_size = *(uint8_t *)(pkt_u + o + 4) + 1;
  4703. + uint32_t stride = *(uint8_t *)(pkt_u + o + 5);
  4704. + uint32_t max_index;
  4705. +
  4706. + if (state->addr & 0x8)
  4707. + stride |= (*(uint32_t *)(pkt_u + 100 + i * 4)) & ~0xff;
  4708. +
  4709. + if (vbo->base.size < offset ||
  4710. + vbo->base.size - offset < attr_size) {
  4711. + DRM_ERROR("BO offset overflow (%d + %d > %d)\n",
  4712. + offset, attr_size, vbo->base.size);
  4713. + return -EINVAL;
  4714. + }
  4715. +
  4716. + if (stride != 0) {
  4717. + max_index = ((vbo->base.size - offset - attr_size) /
  4718. + stride);
  4719. + if (state->max_index > max_index) {
  4720. + DRM_ERROR("primitives use index %d out of supplied %d\n",
  4721. + state->max_index, max_index);
  4722. + return -EINVAL;
  4723. + }
  4724. + }
  4725. +
  4726. + *(uint32_t *)(pkt_v + o) = vbo->paddr + offset;
  4727. + }
  4728. +
  4729. + return 0;
  4730. +
  4731. +fail:
  4732. + return -EINVAL;
  4733. +}
  4734. +
  4735. +int
  4736. +vc4_validate_shader_recs(struct drm_device *dev,
  4737. + struct vc4_exec_info *exec)
  4738. +{
  4739. + uint32_t i;
  4740. + int ret = 0;
  4741. +
  4742. + for (i = 0; i < exec->shader_state_count; i++) {
  4743. + ret = validate_shader_rec(dev, exec, &exec->shader_state[i]);
  4744. + if (ret)
  4745. + return ret;
  4746. + }
  4747. +
  4748. + return ret;
  4749. +}
  4750. --- /dev/null
  4751. +++ b/drivers/gpu/drm/vc4/vc4_validate_shaders.c
  4752. @@ -0,0 +1,521 @@
  4753. +/*
  4754. + * Copyright © 2014 Broadcom
  4755. + *
  4756. + * Permission is hereby granted, free of charge, to any person obtaining a
  4757. + * copy of this software and associated documentation files (the "Software"),
  4758. + * to deal in the Software without restriction, including without limitation
  4759. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  4760. + * and/or sell copies of the Software, and to permit persons to whom the
  4761. + * Software is furnished to do so, subject to the following conditions:
  4762. + *
  4763. + * The above copyright notice and this permission notice (including the next
  4764. + * paragraph) shall be included in all copies or substantial portions of the
  4765. + * Software.
  4766. + *
  4767. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4768. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4769. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  4770. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  4771. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  4772. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  4773. + * IN THE SOFTWARE.
  4774. + */
  4775. +
  4776. +/**
  4777. + * DOC: Shader validator for VC4.
  4778. + *
  4779. + * The VC4 has no IOMMU between it and system memory. So, a user with access
  4780. + * to execute shaders could escalate privilege by overwriting system memory
  4781. + * (using the VPM write address register in the general-purpose DMA mode) or
  4782. + * reading system memory it shouldn't (reading it as a texture, or uniform
  4783. + * data, or vertex data).
  4784. + *
  4785. + * This walks over a shader starting from some offset within a BO, ensuring
  4786. + * that its accesses are appropriately bounded, and recording how many texture
  4787. + * accesses are made and where so that we can do relocations for them in the
  4788. + * uniform stream.
  4789. + *
  4790. + * The kernel API has shaders stored in user-mapped BOs. The BOs will be
  4791. + * forcibly unmapped from the process before validation, and any cache of
  4792. + * validated state will be flushed if the mapping is faulted back in.
  4793. + *
  4794. + * Storing the shaders in BOs means that the validation process will be slow
  4795. + * due to uncached reads, but since shaders are long-lived and shader BOs are
  4796. + * never actually modified, this shouldn't be a problem.
  4797. + */
  4798. +
  4799. +#include "vc4_drv.h"
  4800. +#include "vc4_qpu_defines.h"
  4801. +
  4802. +struct vc4_shader_validation_state {
  4803. + struct vc4_texture_sample_info tmu_setup[2];
  4804. + int tmu_write_count[2];
  4805. +
  4806. + /* For registers that were last written to by a MIN instruction with
  4807. + * one argument being a uniform, the address of the uniform.
  4808. + * Otherwise, ~0.
  4809. + *
  4810. + * This is used for the validation of direct address memory reads.
  4811. + */
  4812. + uint32_t live_min_clamp_offsets[32 + 32 + 4];
  4813. + bool live_max_clamp_regs[32 + 32 + 4];
  4814. +};
  4815. +
  4816. +static uint32_t
  4817. +waddr_to_live_reg_index(uint32_t waddr, bool is_b)
  4818. +{
  4819. + if (waddr < 32) {
  4820. + if (is_b)
  4821. + return 32 + waddr;
  4822. + else
  4823. + return waddr;
  4824. + } else if (waddr <= QPU_W_ACC3) {
  4825. +
  4826. + return 64 + waddr - QPU_W_ACC0;
  4827. + } else {
  4828. + return ~0;
  4829. + }
  4830. +}
  4831. +
  4832. +static uint32_t
  4833. +raddr_add_a_to_live_reg_index(uint64_t inst)
  4834. +{
  4835. + uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  4836. + uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  4837. + uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  4838. + uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  4839. +
  4840. + if (add_a == QPU_MUX_A) {
  4841. + return raddr_a;
  4842. + } else if (add_a == QPU_MUX_B && sig != QPU_SIG_SMALL_IMM) {
  4843. + return 32 + raddr_b;
  4844. + } else if (add_a <= QPU_MUX_R3) {
  4845. + return 64 + add_a;
  4846. + } else {
  4847. + return ~0;
  4848. + }
  4849. +}
  4850. +
  4851. +static bool
  4852. +is_tmu_submit(uint32_t waddr)
  4853. +{
  4854. + return (waddr == QPU_W_TMU0_S ||
  4855. + waddr == QPU_W_TMU1_S);
  4856. +}
  4857. +
  4858. +static bool
  4859. +is_tmu_write(uint32_t waddr)
  4860. +{
  4861. + return (waddr >= QPU_W_TMU0_S &&
  4862. + waddr <= QPU_W_TMU1_B);
  4863. +}
  4864. +
  4865. +static bool
  4866. +record_validated_texture_sample(struct vc4_validated_shader_info *validated_shader,
  4867. + struct vc4_shader_validation_state *validation_state,
  4868. + int tmu)
  4869. +{
  4870. + uint32_t s = validated_shader->num_texture_samples;
  4871. + int i;
  4872. + struct vc4_texture_sample_info *temp_samples;
  4873. +
  4874. + temp_samples = krealloc(validated_shader->texture_samples,
  4875. + (s + 1) * sizeof(*temp_samples),
  4876. + GFP_KERNEL);
  4877. + if (!temp_samples)
  4878. + return false;
  4879. +
  4880. + memcpy(&temp_samples[s],
  4881. + &validation_state->tmu_setup[tmu],
  4882. + sizeof(*temp_samples));
  4883. +
  4884. + validated_shader->num_texture_samples = s + 1;
  4885. + validated_shader->texture_samples = temp_samples;
  4886. +
  4887. + for (i = 0; i < 4; i++)
  4888. + validation_state->tmu_setup[tmu].p_offset[i] = ~0;
  4889. +
  4890. + return true;
  4891. +}
  4892. +
  4893. +static bool
  4894. +check_tmu_write(uint64_t inst,
  4895. + struct vc4_validated_shader_info *validated_shader,
  4896. + struct vc4_shader_validation_state *validation_state,
  4897. + bool is_mul)
  4898. +{
  4899. + uint32_t waddr = (is_mul ?
  4900. + QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
  4901. + QPU_GET_FIELD(inst, QPU_WADDR_ADD));
  4902. + uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  4903. + uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  4904. + int tmu = waddr > QPU_W_TMU0_B;
  4905. + bool submit = is_tmu_submit(waddr);
  4906. + bool is_direct = submit && validation_state->tmu_write_count[tmu] == 0;
  4907. + uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  4908. +
  4909. + if (is_direct) {
  4910. + uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  4911. + uint32_t clamp_reg, clamp_offset;
  4912. +
  4913. + if (sig == QPU_SIG_SMALL_IMM) {
  4914. + DRM_ERROR("direct TMU read used small immediate\n");
  4915. + return false;
  4916. + }
  4917. +
  4918. + /* Make sure that this texture load is an add of the base
  4919. + * address of the UBO to a clamped offset within the UBO.
  4920. + */
  4921. + if (is_mul ||
  4922. + QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
  4923. + DRM_ERROR("direct TMU load wasn't an add\n");
  4924. + return false;
  4925. + }
  4926. +
  4927. + /* We assert that the the clamped address is the first
  4928. + * argument, and the UBO base address is the second argument.
  4929. + * This is arbitrary, but simpler than supporting flipping the
  4930. + * two either way.
  4931. + */
  4932. + clamp_reg = raddr_add_a_to_live_reg_index(inst);
  4933. + if (clamp_reg == ~0) {
  4934. + DRM_ERROR("direct TMU load wasn't clamped\n");
  4935. + return false;
  4936. + }
  4937. +
  4938. + clamp_offset = validation_state->live_min_clamp_offsets[clamp_reg];
  4939. + if (clamp_offset == ~0) {
  4940. + DRM_ERROR("direct TMU load wasn't clamped\n");
  4941. + return false;
  4942. + }
  4943. +
  4944. + /* Store the clamp value's offset in p1 (see reloc_tex() in
  4945. + * vc4_validate.c).
  4946. + */
  4947. + validation_state->tmu_setup[tmu].p_offset[1] =
  4948. + clamp_offset;
  4949. +
  4950. + if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  4951. + !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
  4952. + DRM_ERROR("direct TMU load didn't add to a uniform\n");
  4953. + return false;
  4954. + }
  4955. +
  4956. + validation_state->tmu_setup[tmu].is_direct = true;
  4957. + } else {
  4958. + if (raddr_a == QPU_R_UNIF || (sig != QPU_SIG_SMALL_IMM &&
  4959. + raddr_b == QPU_R_UNIF)) {
  4960. + DRM_ERROR("uniform read in the same instruction as "
  4961. + "texture setup.\n");
  4962. + return false;
  4963. + }
  4964. + }
  4965. +
  4966. + if (validation_state->tmu_write_count[tmu] >= 4) {
  4967. + DRM_ERROR("TMU%d got too many parameters before dispatch\n",
  4968. + tmu);
  4969. + return false;
  4970. + }
  4971. + validation_state->tmu_setup[tmu].p_offset[validation_state->tmu_write_count[tmu]] =
  4972. + validated_shader->uniforms_size;
  4973. + validation_state->tmu_write_count[tmu]++;
  4974. + /* Since direct uses a RADDR uniform reference, it will get counted in
  4975. + * check_instruction_reads()
  4976. + */
  4977. + if (!is_direct)
  4978. + validated_shader->uniforms_size += 4;
  4979. +
  4980. + if (submit) {
  4981. + if (!record_validated_texture_sample(validated_shader,
  4982. + validation_state, tmu)) {
  4983. + return false;
  4984. + }
  4985. +
  4986. + validation_state->tmu_write_count[tmu] = 0;
  4987. + }
  4988. +
  4989. + return true;
  4990. +}
  4991. +
  4992. +static bool
  4993. +check_register_write(uint64_t inst,
  4994. + struct vc4_validated_shader_info *validated_shader,
  4995. + struct vc4_shader_validation_state *validation_state,
  4996. + bool is_mul)
  4997. +{
  4998. + uint32_t waddr = (is_mul ?
  4999. + QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
  5000. + QPU_GET_FIELD(inst, QPU_WADDR_ADD));
  5001. +
  5002. + switch (waddr) {
  5003. + case QPU_W_UNIFORMS_ADDRESS:
  5004. + /* XXX: We'll probably need to support this for reladdr, but
  5005. + * it's definitely a security-related one.
  5006. + */
  5007. + DRM_ERROR("uniforms address load unsupported\n");
  5008. + return false;
  5009. +
  5010. + case QPU_W_TLB_COLOR_MS:
  5011. + case QPU_W_TLB_COLOR_ALL:
  5012. + case QPU_W_TLB_Z:
  5013. + /* These only interact with the tile buffer, not main memory,
  5014. + * so they're safe.
  5015. + */
  5016. + return true;
  5017. +
  5018. + case QPU_W_TMU0_S:
  5019. + case QPU_W_TMU0_T:
  5020. + case QPU_W_TMU0_R:
  5021. + case QPU_W_TMU0_B:
  5022. + case QPU_W_TMU1_S:
  5023. + case QPU_W_TMU1_T:
  5024. + case QPU_W_TMU1_R:
  5025. + case QPU_W_TMU1_B:
  5026. + return check_tmu_write(inst, validated_shader, validation_state,
  5027. + is_mul);
  5028. +
  5029. + case QPU_W_HOST_INT:
  5030. + case QPU_W_TMU_NOSWAP:
  5031. + case QPU_W_TLB_ALPHA_MASK:
  5032. + case QPU_W_MUTEX_RELEASE:
  5033. + /* XXX: I haven't thought about these, so don't support them
  5034. + * for now.
  5035. + */
  5036. + DRM_ERROR("Unsupported waddr %d\n", waddr);
  5037. + return false;
  5038. +
  5039. + case QPU_W_VPM_ADDR:
  5040. + DRM_ERROR("General VPM DMA unsupported\n");
  5041. + return false;
  5042. +
  5043. + case QPU_W_VPM:
  5044. + case QPU_W_VPMVCD_SETUP:
  5045. + /* We allow VPM setup in general, even including VPM DMA
  5046. + * configuration setup, because the (unsafe) DMA can only be
  5047. + * triggered by QPU_W_VPM_ADDR writes.
  5048. + */
  5049. + return true;
  5050. +
  5051. + case QPU_W_TLB_STENCIL_SETUP:
  5052. + return true;
  5053. + }
  5054. +
  5055. + return true;
  5056. +}
  5057. +
  5058. +static void
  5059. +track_live_clamps(uint64_t inst,
  5060. + struct vc4_validated_shader_info *validated_shader,
  5061. + struct vc4_shader_validation_state *validation_state)
  5062. +{
  5063. + uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);
  5064. + uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  5065. + uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  5066. + uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
  5067. + uint32_t add_a = QPU_GET_FIELD(inst, QPU_ADD_A);
  5068. + uint32_t add_b = QPU_GET_FIELD(inst, QPU_ADD_B);
  5069. + uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  5070. + uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  5071. + uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  5072. + bool ws = inst & QPU_WS;
  5073. + uint32_t lri_add_a, lri_add, lri_mul;
  5074. + bool add_a_is_min_0;
  5075. +
  5076. + /* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0),
  5077. + * before we clear previous live state.
  5078. + */
  5079. + lri_add_a = raddr_add_a_to_live_reg_index(inst);
  5080. + add_a_is_min_0 = (lri_add_a != ~0 &&
  5081. + validation_state->live_max_clamp_regs[lri_add_a]);
  5082. +
  5083. + /* Clear live state for registers written by our instruction. */
  5084. + lri_add = waddr_to_live_reg_index(waddr_add, ws);
  5085. + lri_mul = waddr_to_live_reg_index(waddr_mul, !ws);
  5086. + if (lri_mul != ~0) {
  5087. + validation_state->live_max_clamp_regs[lri_mul] = false;
  5088. + validation_state->live_min_clamp_offsets[lri_mul] = ~0;
  5089. + }
  5090. + if (lri_add != ~0) {
  5091. + validation_state->live_max_clamp_regs[lri_add] = false;
  5092. + validation_state->live_min_clamp_offsets[lri_add] = ~0;
  5093. + } else {
  5094. + /* Nothing further to do for live tracking, since only ADDs
  5095. + * generate new live clamp registers.
  5096. + */
  5097. + return;
  5098. + }
  5099. +
  5100. + /* Now, handle remaining live clamp tracking for the ADD operation. */
  5101. +
  5102. + if (cond_add != QPU_COND_ALWAYS)
  5103. + return;
  5104. +
  5105. + if (op_add == QPU_A_MAX) {
  5106. + /* Track live clamps of a value to a minimum of 0 (in either
  5107. + * arg).
  5108. + */
  5109. + if (sig != QPU_SIG_SMALL_IMM || raddr_b != 0 ||
  5110. + (add_a != QPU_MUX_B && add_b != QPU_MUX_B)) {
  5111. + return;
  5112. + }
  5113. +
  5114. + validation_state->live_max_clamp_regs[lri_add] = true;
  5115. + } if (op_add == QPU_A_MIN) {
  5116. + /* Track live clamps of a value clamped to a minimum of 0 and
  5117. + * a maximum of some uniform's offset.
  5118. + */
  5119. + if (!add_a_is_min_0)
  5120. + return;
  5121. +
  5122. + if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
  5123. + !(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF &&
  5124. + sig != QPU_SIG_SMALL_IMM)) {
  5125. + return;
  5126. + }
  5127. +
  5128. + validation_state->live_min_clamp_offsets[lri_add] =
  5129. + validated_shader->uniforms_size;
  5130. + }
  5131. +}
  5132. +
  5133. +static bool
  5134. +check_instruction_writes(uint64_t inst,
  5135. + struct vc4_validated_shader_info *validated_shader,
  5136. + struct vc4_shader_validation_state *validation_state)
  5137. +{
  5138. + uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
  5139. + uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
  5140. + bool ok;
  5141. +
  5142. + if (is_tmu_write(waddr_add) && is_tmu_write(waddr_mul)) {
  5143. + DRM_ERROR("ADD and MUL both set up textures\n");
  5144. + return false;
  5145. + }
  5146. +
  5147. + ok = (check_register_write(inst, validated_shader, validation_state, false) &&
  5148. + check_register_write(inst, validated_shader, validation_state, true));
  5149. +
  5150. + track_live_clamps(inst, validated_shader, validation_state);
  5151. +
  5152. + return ok;
  5153. +}
  5154. +
  5155. +static bool
  5156. +check_instruction_reads(uint64_t inst,
  5157. + struct vc4_validated_shader_info *validated_shader)
  5158. +{
  5159. + uint32_t raddr_a = QPU_GET_FIELD(inst, QPU_RADDR_A);
  5160. + uint32_t raddr_b = QPU_GET_FIELD(inst, QPU_RADDR_B);
  5161. + uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  5162. +
  5163. + if (raddr_a == QPU_R_UNIF ||
  5164. + (raddr_b == QPU_R_UNIF && sig != QPU_SIG_SMALL_IMM)) {
  5165. + /* This can't overflow the uint32_t, because we're reading 8
  5166. + * bytes of instruction to increment by 4 here, so we'd
  5167. + * already be OOM.
  5168. + */
  5169. + validated_shader->uniforms_size += 4;
  5170. + }
  5171. +
  5172. + return true;
  5173. +}
  5174. +
  5175. +struct vc4_validated_shader_info *
  5176. +vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
  5177. +{
  5178. + bool found_shader_end = false;
  5179. + int shader_end_ip = 0;
  5180. + uint32_t ip, max_ip;
  5181. + uint64_t *shader;
  5182. + struct vc4_validated_shader_info *validated_shader;
  5183. + struct vc4_shader_validation_state validation_state;
  5184. + int i;
  5185. +
  5186. + memset(&validation_state, 0, sizeof(validation_state));
  5187. +
  5188. + for (i = 0; i < 8; i++)
  5189. + validation_state.tmu_setup[i / 4].p_offset[i % 4] = ~0;
  5190. + for (i = 0; i < ARRAY_SIZE(validation_state.live_min_clamp_offsets); i++)
  5191. + validation_state.live_min_clamp_offsets[i] = ~0;
  5192. +
  5193. + shader = shader_obj->vaddr;
  5194. + max_ip = shader_obj->base.size / sizeof(uint64_t);
  5195. +
  5196. + validated_shader = kcalloc(sizeof(*validated_shader), 1, GFP_KERNEL);
  5197. + if (!validated_shader)
  5198. + return NULL;
  5199. +
  5200. + for (ip = 0; ip < max_ip; ip++) {
  5201. + uint64_t inst = shader[ip];
  5202. + uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
  5203. +
  5204. + switch (sig) {
  5205. + case QPU_SIG_NONE:
  5206. + case QPU_SIG_WAIT_FOR_SCOREBOARD:
  5207. + case QPU_SIG_SCOREBOARD_UNLOCK:
  5208. + case QPU_SIG_COLOR_LOAD:
  5209. + case QPU_SIG_LOAD_TMU0:
  5210. + case QPU_SIG_LOAD_TMU1:
  5211. + case QPU_SIG_PROG_END:
  5212. + case QPU_SIG_SMALL_IMM:
  5213. + if (!check_instruction_writes(inst, validated_shader,
  5214. + &validation_state)) {
  5215. + DRM_ERROR("Bad write at ip %d\n", ip);
  5216. + goto fail;
  5217. + }
  5218. +
  5219. + if (!check_instruction_reads(inst, validated_shader))
  5220. + goto fail;
  5221. +
  5222. + if (sig == QPU_SIG_PROG_END) {
  5223. + found_shader_end = true;
  5224. + shader_end_ip = ip;
  5225. + }
  5226. +
  5227. + break;
  5228. +
  5229. + case QPU_SIG_LOAD_IMM:
  5230. + if (!check_instruction_writes(inst, validated_shader,
  5231. + &validation_state)) {
  5232. + DRM_ERROR("Bad LOAD_IMM write at ip %d\n", ip);
  5233. + goto fail;
  5234. + }
  5235. + break;
  5236. +
  5237. + default:
  5238. + DRM_ERROR("Unsupported QPU signal %d at "
  5239. + "instruction %d\n", sig, ip);
  5240. + goto fail;
  5241. + }
  5242. +
  5243. + /* There are two delay slots after program end is signaled
  5244. + * that are still executed, then we're finished.
  5245. + */
  5246. + if (found_shader_end && ip == shader_end_ip + 2)
  5247. + break;
  5248. + }
  5249. +
  5250. + if (ip == max_ip) {
  5251. + DRM_ERROR("shader failed to terminate before "
  5252. + "shader BO end at %d\n",
  5253. + shader_obj->base.size);
  5254. + goto fail;
  5255. + }
  5256. +
  5257. + /* Again, no chance of integer overflow here because the worst case
  5258. + * scenario is 8 bytes of uniforms plus handles per 8-byte
  5259. + * instruction.
  5260. + */
  5261. + validated_shader->uniforms_src_size =
  5262. + (validated_shader->uniforms_size +
  5263. + 4 * validated_shader->num_texture_samples);
  5264. +
  5265. + return validated_shader;
  5266. +
  5267. +fail:
  5268. + if (validated_shader) {
  5269. + kfree(validated_shader->texture_samples);
  5270. + kfree(validated_shader);
  5271. + }
  5272. + return NULL;
  5273. +}
  5274. --- /dev/null
  5275. +++ b/include/uapi/drm/vc4_drm.h
  5276. @@ -0,0 +1,229 @@
  5277. +/*
  5278. + * Copyright © 2014-2015 Broadcom
  5279. + *
  5280. + * Permission is hereby granted, free of charge, to any person obtaining a
  5281. + * copy of this software and associated documentation files (the "Software"),
  5282. + * to deal in the Software without restriction, including without limitation
  5283. + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  5284. + * and/or sell copies of the Software, and to permit persons to whom the
  5285. + * Software is furnished to do so, subject to the following conditions:
  5286. + *
  5287. + * The above copyright notice and this permission notice (including the next
  5288. + * paragraph) shall be included in all copies or substantial portions of the
  5289. + * Software.
  5290. + *
  5291. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  5292. + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  5293. + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  5294. + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  5295. + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  5296. + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  5297. + * IN THE SOFTWARE.
  5298. + */
  5299. +
  5300. +#ifndef _UAPI_VC4_DRM_H_
  5301. +#define _UAPI_VC4_DRM_H_
  5302. +
  5303. +#include <drm/drm.h>
  5304. +
  5305. +#define DRM_VC4_SUBMIT_CL 0x00
  5306. +#define DRM_VC4_WAIT_SEQNO 0x01
  5307. +#define DRM_VC4_WAIT_BO 0x02
  5308. +#define DRM_VC4_CREATE_BO 0x03
  5309. +#define DRM_VC4_MMAP_BO 0x04
  5310. +#define DRM_VC4_CREATE_SHADER_BO 0x05
  5311. +
  5312. +#define DRM_IOCTL_VC4_SUBMIT_CL DRM_IOWR( DRM_COMMAND_BASE + DRM_VC4_SUBMIT_CL, struct drm_vc4_submit_cl)
  5313. +#define DRM_IOCTL_VC4_WAIT_SEQNO DRM_IOWR( DRM_COMMAND_BASE + DRM_VC4_WAIT_SEQNO, struct drm_vc4_wait_seqno)
  5314. +#define DRM_IOCTL_VC4_WAIT_BO DRM_IOWR( DRM_COMMAND_BASE + DRM_VC4_WAIT_BO, struct drm_vc4_wait_bo)
  5315. +#define DRM_IOCTL_VC4_CREATE_BO DRM_IOWR( DRM_COMMAND_BASE + DRM_VC4_CREATE_BO, struct drm_vc4_create_bo)
  5316. +#define DRM_IOCTL_VC4_MMAP_BO DRM_IOWR( DRM_COMMAND_BASE + DRM_VC4_MMAP_BO, struct drm_vc4_mmap_bo)
  5317. +#define DRM_IOCTL_VC4_CREATE_SHADER_BO DRM_IOWR( DRM_COMMAND_BASE + DRM_VC4_CREATE_SHADER_BO, struct drm_vc4_create_shader_bo)
  5318. +
  5319. +struct drm_vc4_submit_rcl_surface {
  5320. + uint32_t hindex; /* Handle index, or ~0 if not present. */
  5321. + uint32_t offset; /* Offset to start of buffer. */
  5322. + /*
  5323. + * Bits for either render config (color_ms_write) or load/store packet.
  5324. + */
  5325. + uint16_t bits;
  5326. + uint16_t pad;
  5327. +};
  5328. +
  5329. +/**
  5330. + * struct drm_vc4_submit_cl - ioctl argument for submitting commands to the 3D
  5331. + * engine.
  5332. + *
  5333. + * Drivers typically use GPU BOs to store batchbuffers / command lists and
  5334. + * their associated state. However, because the VC4 lacks an MMU, we have to
  5335. + * do validation of memory accesses by the GPU commands. If we were to store
  5336. + * our commands in BOs, we'd need to do uncached readback from them to do the
  5337. + * validation process, which is too expensive. Instead, userspace accumulates
  5338. + * commands and associated state in plain memory, then the kernel copies the
  5339. + * data to its own address space, and then validates and stores it in a GPU
  5340. + * BO.
  5341. + */
  5342. +struct drm_vc4_submit_cl {
  5343. + /* Pointer to the binner command list.
  5344. + *
  5345. + * This is the first set of commands executed, which runs the
  5346. + * coordinate shader to determine where primitives land on the screen,
  5347. + * then writes out the state updates and draw calls necessary per tile
  5348. + * to the tile allocation BO.
  5349. + */
  5350. + uint64_t bin_cl;
  5351. +
  5352. + /* Pointer to the shader records.
  5353. + *
  5354. + * Shader records are the structures read by the hardware that contain
  5355. + * pointers to uniforms, shaders, and vertex attributes. The
  5356. + * reference to the shader record has enough information to determine
  5357. + * how many pointers are necessary (fixed number for shaders/uniforms,
  5358. + * and an attribute count), so those BO indices into bo_handles are
  5359. + * just stored as uint32_ts before each shader record passed in.
  5360. + */
  5361. + uint64_t shader_rec;
  5362. +
  5363. + /* Pointer to uniform data and texture handles for the textures
  5364. + * referenced by the shader.
  5365. + *
  5366. + * For each shader state record, there is a set of uniform data in the
  5367. + * order referenced by the record (FS, VS, then CS). Each set of
  5368. + * uniform data has a uint32_t index into bo_handles per texture
  5369. + * sample operation, in the order the QPU_W_TMUn_S writes appear in
  5370. + * the program. Following the texture BO handle indices is the actual
  5371. + * uniform data.
  5372. + *
  5373. + * The individual uniform state blocks don't have sizes passed in,
  5374. + * because the kernel has to determine the sizes anyway during shader
  5375. + * code validation.
  5376. + */
  5377. + uint64_t uniforms;
  5378. + uint64_t bo_handles;
  5379. +
  5380. + /* Size in bytes of the binner command list. */
  5381. + uint32_t bin_cl_size;
  5382. + /* Size in bytes of the set of shader records. */
  5383. + uint32_t shader_rec_size;
  5384. + /* Number of shader records.
  5385. + *
  5386. + * This could just be computed from the contents of shader_records and
  5387. + * the address bits of references to them from the bin CL, but it
  5388. + * keeps the kernel from having to resize some allocations it makes.
  5389. + */
  5390. + uint32_t shader_rec_count;
  5391. + /* Size in bytes of the uniform state. */
  5392. + uint32_t uniforms_size;
  5393. +
  5394. + /* Number of BO handles passed in (size is that times 4). */
  5395. + uint32_t bo_handle_count;
  5396. +
  5397. + /* RCL setup: */
  5398. + uint16_t width;
  5399. + uint16_t height;
  5400. + uint8_t min_x_tile;
  5401. + uint8_t min_y_tile;
  5402. + uint8_t max_x_tile;
  5403. + uint8_t max_y_tile;
  5404. + struct drm_vc4_submit_rcl_surface color_read;
  5405. + struct drm_vc4_submit_rcl_surface color_ms_write;
  5406. + struct drm_vc4_submit_rcl_surface zs_read;
  5407. + struct drm_vc4_submit_rcl_surface zs_write;
  5408. + uint32_t clear_color[2];
  5409. + uint32_t clear_z;
  5410. + uint8_t clear_s;
  5411. +
  5412. + uint32_t pad:24;
  5413. +
  5414. +#define VC4_SUBMIT_CL_USE_CLEAR_COLOR (1 << 0)
  5415. + uint32_t flags;
  5416. +
  5417. + /* Returned value of the seqno of this render job (for the
  5418. + * wait ioctl).
  5419. + */
  5420. + uint64_t seqno;
  5421. +};
  5422. +
  5423. +/**
  5424. + * struct drm_vc4_wait_seqno - ioctl argument for waiting for
  5425. + * DRM_VC4_SUBMIT_CL completion using its returned seqno.
  5426. + *
  5427. + * timeout_ns is the timeout in nanoseconds, where "0" means "don't
  5428. + * block, just return the status."
  5429. + */
  5430. +struct drm_vc4_wait_seqno {
  5431. + uint64_t seqno;
  5432. + uint64_t timeout_ns;
  5433. +};
  5434. +
  5435. +/**
  5436. + * struct drm_vc4_wait_bo - ioctl argument for waiting for
  5437. + * completion of the last DRM_VC4_SUBMIT_CL on a BO.
  5438. + *
  5439. + * This is useful for cases where multiple processes might be
  5440. + * rendering to a BO and you want to wait for all rendering to be
  5441. + * completed.
  5442. + */
  5443. +struct drm_vc4_wait_bo {
  5444. + uint32_t handle;
  5445. + uint32_t pad;
  5446. + uint64_t timeout_ns;
  5447. +};
  5448. +
  5449. +/**
  5450. + * struct drm_vc4_create_bo - ioctl argument for creating VC4 BOs.
  5451. + *
  5452. + * There are currently no values for the flags argument, but it may be
  5453. + * used in a future extension.
  5454. + */
  5455. +struct drm_vc4_create_bo {
  5456. + uint32_t size;
  5457. + uint32_t flags;
  5458. + /** Returned GEM handle for the BO. */
  5459. + uint32_t handle;
  5460. + uint32_t pad;
  5461. +};
  5462. +
  5463. +/**
  5464. + * struct drm_vc4_create_shader_bo - ioctl argument for creating VC4
  5465. + * shader BOs.
  5466. + *
  5467. + * Since allowing a shader to be overwritten while it's also being
  5468. + * executed from would allow privlege escalation, shaders must be
  5469. + * created using this ioctl, and they can't be mmapped later.
  5470. + */
  5471. +struct drm_vc4_create_shader_bo {
  5472. + /* Size of the data argument. */
  5473. + uint32_t size;
  5474. + /* Flags, currently must be 0. */
  5475. + uint32_t flags;
  5476. +
  5477. + /* Pointer to the data. */
  5478. + uint64_t data;
  5479. +
  5480. + /** Returned GEM handle for the BO. */
  5481. + uint32_t handle;
  5482. + /* Pad, must be 0. */
  5483. + uint32_t pad;
  5484. +};
  5485. +
  5486. +/**
  5487. + * struct drm_vc4_mmap_bo - ioctl argument for mapping VC4 BOs.
  5488. + *
  5489. + * This doesn't actually perform an mmap. Instead, it returns the
  5490. + * offset you need to use in an mmap on the DRM device node. This
  5491. + * means that tools like valgrind end up knowing about the mapped
  5492. + * memory.
  5493. + *
  5494. + * There are currently no values for the flags argument, but it may be
  5495. + * used in a future extension.
  5496. + */
  5497. +struct drm_vc4_mmap_bo {
  5498. + /** Handle for the object being mapped. */
  5499. + uint32_t handle;
  5500. + uint32_t flags;
  5501. + /** offset into the drm node to use for subsequent mmap call. */
  5502. + uint64_t offset;
  5503. +};
  5504. +
  5505. +#endif /* _UAPI_VC4_DRM_H_ */