drm_crtc.c 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include <drm/drm_atomic.h>
  42. #include "drm_crtc_internal.h"
  43. #include "drm_internal.h"
  44. static struct drm_framebuffer *
  45. internal_framebuffer_create(struct drm_device *dev,
  46. struct drm_mode_fb_cmd2 *r,
  47. struct drm_file *file_priv);
  48. /* Avoid boilerplate. I'm tired of typing. */
  49. #define DRM_ENUM_NAME_FN(fnname, list) \
  50. const char *fnname(int val) \
  51. { \
  52. int i; \
  53. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  54. if (list[i].type == val) \
  55. return list[i].name; \
  56. } \
  57. return "(unknown)"; \
  58. }
  59. /*
  60. * Global properties
  61. */
  62. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  63. { DRM_MODE_DPMS_ON, "On" },
  64. { DRM_MODE_DPMS_STANDBY, "Standby" },
  65. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  66. { DRM_MODE_DPMS_OFF, "Off" }
  67. };
  68. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  69. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  70. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  71. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  72. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  73. };
  74. /*
  75. * Optional properties
  76. */
  77. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  78. { DRM_MODE_SCALE_NONE, "None" },
  79. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  80. { DRM_MODE_SCALE_CENTER, "Center" },
  81. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  82. };
  83. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  84. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  85. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  86. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  87. };
  88. /*
  89. * Non-global properties, but "required" for certain connectors.
  90. */
  91. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  92. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  93. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  94. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  95. };
  96. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  97. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  98. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  99. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  100. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  101. };
  102. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  103. drm_dvi_i_subconnector_enum_list)
  104. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  105. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  106. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  107. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  108. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  109. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  110. };
  111. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  112. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  113. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  114. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  115. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  116. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  117. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  118. };
  119. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  120. drm_tv_subconnector_enum_list)
  121. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  122. { DRM_MODE_DIRTY_OFF, "Off" },
  123. { DRM_MODE_DIRTY_ON, "On" },
  124. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  125. };
  126. struct drm_conn_prop_enum_list {
  127. int type;
  128. const char *name;
  129. struct ida ida;
  130. };
  131. /*
  132. * Connector and encoder types.
  133. */
  134. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  135. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  136. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  137. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  138. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  139. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  140. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  141. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  142. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  143. { DRM_MODE_CONNECTOR_Component, "Component" },
  144. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  145. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  146. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  147. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  148. { DRM_MODE_CONNECTOR_TV, "TV" },
  149. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  150. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  151. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  152. };
  153. static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
  154. { DRM_MODE_ENCODER_NONE, "None" },
  155. { DRM_MODE_ENCODER_DAC, "DAC" },
  156. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  157. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  158. { DRM_MODE_ENCODER_TVDAC, "TV" },
  159. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  160. { DRM_MODE_ENCODER_DSI, "DSI" },
  161. { DRM_MODE_ENCODER_DPMST, "DP MST" },
  162. };
  163. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  164. { SubPixelUnknown, "Unknown" },
  165. { SubPixelHorizontalRGB, "Horizontal RGB" },
  166. { SubPixelHorizontalBGR, "Horizontal BGR" },
  167. { SubPixelVerticalRGB, "Vertical RGB" },
  168. { SubPixelVerticalBGR, "Vertical BGR" },
  169. { SubPixelNone, "None" },
  170. };
  171. void drm_connector_ida_init(void)
  172. {
  173. int i;
  174. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  175. ida_init(&drm_connector_enum_list[i].ida);
  176. }
  177. void drm_connector_ida_destroy(void)
  178. {
  179. int i;
  180. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  181. ida_destroy(&drm_connector_enum_list[i].ida);
  182. }
  183. /**
  184. * drm_get_connector_status_name - return a string for connector status
  185. * @status: connector status to compute name of
  186. *
  187. * In contrast to the other drm_get_*_name functions this one here returns a
  188. * const pointer and hence is threadsafe.
  189. */
  190. const char *drm_get_connector_status_name(enum drm_connector_status status)
  191. {
  192. if (status == connector_status_connected)
  193. return "connected";
  194. else if (status == connector_status_disconnected)
  195. return "disconnected";
  196. else
  197. return "unknown";
  198. }
  199. EXPORT_SYMBOL(drm_get_connector_status_name);
  200. /**
  201. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  202. * @order: enum of subpixel_order
  203. *
  204. * Note you could abuse this and return something out of bounds, but that
  205. * would be a caller error. No unscrubbed user data should make it here.
  206. */
  207. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  208. {
  209. return drm_subpixel_enum_list[order].name;
  210. }
  211. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  212. static char printable_char(int c)
  213. {
  214. return isascii(c) && isprint(c) ? c : '?';
  215. }
  216. /**
  217. * drm_get_format_name - return a string for drm fourcc format
  218. * @format: format to compute name of
  219. *
  220. * Note that the buffer used by this function is globally shared and owned by
  221. * the function itself.
  222. *
  223. * FIXME: This isn't really multithreading safe.
  224. */
  225. const char *drm_get_format_name(uint32_t format)
  226. {
  227. static char buf[32];
  228. snprintf(buf, sizeof(buf),
  229. "%c%c%c%c %s-endian (0x%08x)",
  230. printable_char(format & 0xff),
  231. printable_char((format >> 8) & 0xff),
  232. printable_char((format >> 16) & 0xff),
  233. printable_char((format >> 24) & 0x7f),
  234. format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  235. format);
  236. return buf;
  237. }
  238. EXPORT_SYMBOL(drm_get_format_name);
  239. /*
  240. * Internal function to assign a slot in the object idr and optionally
  241. * register the object into the idr.
  242. */
  243. static int drm_mode_object_get_reg(struct drm_device *dev,
  244. struct drm_mode_object *obj,
  245. uint32_t obj_type,
  246. bool register_obj)
  247. {
  248. int ret;
  249. mutex_lock(&dev->mode_config.idr_mutex);
  250. ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  251. if (ret >= 0) {
  252. /*
  253. * Set up the object linking under the protection of the idr
  254. * lock so that other users can't see inconsistent state.
  255. */
  256. obj->id = ret;
  257. obj->type = obj_type;
  258. }
  259. mutex_unlock(&dev->mode_config.idr_mutex);
  260. return ret < 0 ? ret : 0;
  261. }
  262. /**
  263. * drm_mode_object_get - allocate a new modeset identifier
  264. * @dev: DRM device
  265. * @obj: object pointer, used to generate unique ID
  266. * @obj_type: object type
  267. *
  268. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  269. * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  270. * modeset identifiers are _not_ reference counted. Hence don't use this for
  271. * reference counted modeset objects like framebuffers.
  272. *
  273. * Returns:
  274. * New unique (relative to other objects in @dev) integer identifier for the
  275. * object.
  276. */
  277. int drm_mode_object_get(struct drm_device *dev,
  278. struct drm_mode_object *obj, uint32_t obj_type)
  279. {
  280. return drm_mode_object_get_reg(dev, obj, obj_type, true);
  281. }
  282. static void drm_mode_object_register(struct drm_device *dev,
  283. struct drm_mode_object *obj)
  284. {
  285. mutex_lock(&dev->mode_config.idr_mutex);
  286. idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  287. mutex_unlock(&dev->mode_config.idr_mutex);
  288. }
  289. /**
  290. * drm_mode_object_put - free a modeset identifer
  291. * @dev: DRM device
  292. * @object: object to free
  293. *
  294. * Free @id from @dev's unique identifier pool. Note that despite the _get
  295. * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  296. * for reference counted modeset objects like framebuffers.
  297. */
  298. void drm_mode_object_put(struct drm_device *dev,
  299. struct drm_mode_object *object)
  300. {
  301. mutex_lock(&dev->mode_config.idr_mutex);
  302. idr_remove(&dev->mode_config.crtc_idr, object->id);
  303. mutex_unlock(&dev->mode_config.idr_mutex);
  304. }
  305. static struct drm_mode_object *_object_find(struct drm_device *dev,
  306. uint32_t id, uint32_t type)
  307. {
  308. struct drm_mode_object *obj = NULL;
  309. mutex_lock(&dev->mode_config.idr_mutex);
  310. obj = idr_find(&dev->mode_config.crtc_idr, id);
  311. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  312. obj = NULL;
  313. if (obj && obj->id != id)
  314. obj = NULL;
  315. /* don't leak out unref'd fb's */
  316. if (obj &&
  317. (obj->type == DRM_MODE_OBJECT_FB ||
  318. obj->type == DRM_MODE_OBJECT_BLOB))
  319. obj = NULL;
  320. mutex_unlock(&dev->mode_config.idr_mutex);
  321. return obj;
  322. }
  323. /**
  324. * drm_mode_object_find - look up a drm object with static lifetime
  325. * @dev: drm device
  326. * @id: id of the mode object
  327. * @type: type of the mode object
  328. *
  329. * Note that framebuffers cannot be looked up with this functions - since those
  330. * are reference counted, they need special treatment. Even with
  331. * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  332. * rather than WARN_ON()).
  333. */
  334. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  335. uint32_t id, uint32_t type)
  336. {
  337. struct drm_mode_object *obj = NULL;
  338. /* Framebuffers are reference counted and need their own lookup
  339. * function.*/
  340. WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
  341. obj = _object_find(dev, id, type);
  342. return obj;
  343. }
  344. EXPORT_SYMBOL(drm_mode_object_find);
  345. /**
  346. * drm_framebuffer_init - initialize a framebuffer
  347. * @dev: DRM device
  348. * @fb: framebuffer to be initialized
  349. * @funcs: ... with these functions
  350. *
  351. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  352. * functions & device file and adds it to the master fd list.
  353. *
  354. * IMPORTANT:
  355. * This functions publishes the fb and makes it available for concurrent access
  356. * by other users. Which means by this point the fb _must_ be fully set up -
  357. * since all the fb attributes are invariant over its lifetime, no further
  358. * locking but only correct reference counting is required.
  359. *
  360. * Returns:
  361. * Zero on success, error code on failure.
  362. */
  363. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  364. const struct drm_framebuffer_funcs *funcs)
  365. {
  366. int ret;
  367. mutex_lock(&dev->mode_config.fb_lock);
  368. kref_init(&fb->refcount);
  369. INIT_LIST_HEAD(&fb->filp_head);
  370. fb->dev = dev;
  371. fb->funcs = funcs;
  372. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  373. if (ret)
  374. goto out;
  375. dev->mode_config.num_fb++;
  376. list_add(&fb->head, &dev->mode_config.fb_list);
  377. out:
  378. mutex_unlock(&dev->mode_config.fb_lock);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(drm_framebuffer_init);
  382. /* dev->mode_config.fb_lock must be held! */
  383. static void __drm_framebuffer_unregister(struct drm_device *dev,
  384. struct drm_framebuffer *fb)
  385. {
  386. mutex_lock(&dev->mode_config.idr_mutex);
  387. idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  388. mutex_unlock(&dev->mode_config.idr_mutex);
  389. fb->base.id = 0;
  390. }
  391. static void drm_framebuffer_free(struct kref *kref)
  392. {
  393. struct drm_framebuffer *fb =
  394. container_of(kref, struct drm_framebuffer, refcount);
  395. struct drm_device *dev = fb->dev;
  396. /*
  397. * The lookup idr holds a weak reference, which has not necessarily been
  398. * removed at this point. Check for that.
  399. */
  400. mutex_lock(&dev->mode_config.fb_lock);
  401. if (fb->base.id) {
  402. /* Mark fb as reaped and drop idr ref. */
  403. __drm_framebuffer_unregister(dev, fb);
  404. }
  405. mutex_unlock(&dev->mode_config.fb_lock);
  406. fb->funcs->destroy(fb);
  407. }
  408. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  409. uint32_t id)
  410. {
  411. struct drm_mode_object *obj = NULL;
  412. struct drm_framebuffer *fb;
  413. mutex_lock(&dev->mode_config.idr_mutex);
  414. obj = idr_find(&dev->mode_config.crtc_idr, id);
  415. if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  416. fb = NULL;
  417. else
  418. fb = obj_to_fb(obj);
  419. mutex_unlock(&dev->mode_config.idr_mutex);
  420. return fb;
  421. }
  422. /**
  423. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  424. * @dev: drm device
  425. * @id: id of the fb object
  426. *
  427. * If successful, this grabs an additional reference to the framebuffer -
  428. * callers need to make sure to eventually unreference the returned framebuffer
  429. * again, using @drm_framebuffer_unreference.
  430. */
  431. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  432. uint32_t id)
  433. {
  434. struct drm_framebuffer *fb;
  435. mutex_lock(&dev->mode_config.fb_lock);
  436. fb = __drm_framebuffer_lookup(dev, id);
  437. if (fb) {
  438. if (!kref_get_unless_zero(&fb->refcount))
  439. fb = NULL;
  440. }
  441. mutex_unlock(&dev->mode_config.fb_lock);
  442. return fb;
  443. }
  444. EXPORT_SYMBOL(drm_framebuffer_lookup);
  445. /**
  446. * drm_framebuffer_unreference - unref a framebuffer
  447. * @fb: framebuffer to unref
  448. *
  449. * This functions decrements the fb's refcount and frees it if it drops to zero.
  450. */
  451. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  452. {
  453. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  454. kref_put(&fb->refcount, drm_framebuffer_free);
  455. }
  456. EXPORT_SYMBOL(drm_framebuffer_unreference);
  457. /**
  458. * drm_framebuffer_reference - incr the fb refcnt
  459. * @fb: framebuffer
  460. *
  461. * This functions increments the fb's refcount.
  462. */
  463. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  464. {
  465. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  466. kref_get(&fb->refcount);
  467. }
  468. EXPORT_SYMBOL(drm_framebuffer_reference);
  469. /**
  470. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  471. * @fb: fb to unregister
  472. *
  473. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  474. * those used for fbdev. Note that the caller must hold a reference of it's own,
  475. * i.e. the object may not be destroyed through this call (since it'll lead to a
  476. * locking inversion).
  477. */
  478. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  479. {
  480. struct drm_device *dev = fb->dev;
  481. mutex_lock(&dev->mode_config.fb_lock);
  482. /* Mark fb as reaped and drop idr ref. */
  483. __drm_framebuffer_unregister(dev, fb);
  484. mutex_unlock(&dev->mode_config.fb_lock);
  485. }
  486. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  487. /**
  488. * drm_framebuffer_cleanup - remove a framebuffer object
  489. * @fb: framebuffer to remove
  490. *
  491. * Cleanup framebuffer. This function is intended to be used from the drivers
  492. * ->destroy callback. It can also be used to clean up driver private
  493. * framebuffers embedded into a larger structure.
  494. *
  495. * Note that this function does not remove the fb from active usuage - if it is
  496. * still used anywhere, hilarity can ensue since userspace could call getfb on
  497. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  498. *
  499. * Also, the framebuffer will not be removed from the lookup idr - for
  500. * user-created framebuffers this will happen in in the rmfb ioctl. For
  501. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  502. * drm_framebuffer_unregister_private.
  503. */
  504. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  505. {
  506. struct drm_device *dev = fb->dev;
  507. mutex_lock(&dev->mode_config.fb_lock);
  508. list_del(&fb->head);
  509. dev->mode_config.num_fb--;
  510. mutex_unlock(&dev->mode_config.fb_lock);
  511. }
  512. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  513. /**
  514. * drm_framebuffer_remove - remove and unreference a framebuffer object
  515. * @fb: framebuffer to remove
  516. *
  517. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  518. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  519. * passed-in framebuffer. Might take the modeset locks.
  520. *
  521. * Note that this function optimizes the cleanup away if the caller holds the
  522. * last reference to the framebuffer. It is also guaranteed to not take the
  523. * modeset locks in this case.
  524. */
  525. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  526. {
  527. struct drm_device *dev = fb->dev;
  528. struct drm_crtc *crtc;
  529. struct drm_plane *plane;
  530. struct drm_mode_set set;
  531. int ret;
  532. WARN_ON(!list_empty(&fb->filp_head));
  533. /*
  534. * drm ABI mandates that we remove any deleted framebuffers from active
  535. * useage. But since most sane clients only remove framebuffers they no
  536. * longer need, try to optimize this away.
  537. *
  538. * Since we're holding a reference ourselves, observing a refcount of 1
  539. * means that we're the last holder and can skip it. Also, the refcount
  540. * can never increase from 1 again, so we don't need any barriers or
  541. * locks.
  542. *
  543. * Note that userspace could try to race with use and instate a new
  544. * usage _after_ we've cleared all current ones. End result will be an
  545. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  546. * in this manner.
  547. */
  548. if (atomic_read(&fb->refcount.refcount) > 1) {
  549. drm_modeset_lock_all(dev);
  550. /* remove from any CRTC */
  551. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  552. if (crtc->primary->fb == fb) {
  553. /* should turn off the crtc */
  554. memset(&set, 0, sizeof(struct drm_mode_set));
  555. set.crtc = crtc;
  556. set.fb = NULL;
  557. ret = drm_mode_set_config_internal(&set);
  558. if (ret)
  559. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  560. }
  561. }
  562. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  563. if (plane->fb == fb)
  564. drm_plane_force_disable(plane);
  565. }
  566. drm_modeset_unlock_all(dev);
  567. }
  568. drm_framebuffer_unreference(fb);
  569. }
  570. EXPORT_SYMBOL(drm_framebuffer_remove);
  571. DEFINE_WW_CLASS(crtc_ww_class);
  572. /**
  573. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  574. * specified primary and cursor planes.
  575. * @dev: DRM device
  576. * @crtc: CRTC object to init
  577. * @primary: Primary plane for CRTC
  578. * @cursor: Cursor plane for CRTC
  579. * @funcs: callbacks for the new CRTC
  580. *
  581. * Inits a new object created as base part of a driver crtc object.
  582. *
  583. * Returns:
  584. * Zero on success, error code on failure.
  585. */
  586. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  587. struct drm_plane *primary,
  588. struct drm_plane *cursor,
  589. const struct drm_crtc_funcs *funcs)
  590. {
  591. struct drm_mode_config *config = &dev->mode_config;
  592. int ret;
  593. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  594. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  595. crtc->dev = dev;
  596. crtc->funcs = funcs;
  597. crtc->invert_dimensions = false;
  598. drm_modeset_lock_init(&crtc->mutex);
  599. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  600. if (ret)
  601. return ret;
  602. crtc->base.properties = &crtc->properties;
  603. list_add_tail(&crtc->head, &config->crtc_list);
  604. config->num_crtc++;
  605. crtc->primary = primary;
  606. crtc->cursor = cursor;
  607. if (primary)
  608. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  609. if (cursor)
  610. cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  611. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  612. drm_object_attach_property(&crtc->base, config->prop_active, 0);
  613. }
  614. return 0;
  615. }
  616. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  617. /**
  618. * drm_crtc_cleanup - Clean up the core crtc usage
  619. * @crtc: CRTC to cleanup
  620. *
  621. * This function cleans up @crtc and removes it from the DRM mode setting
  622. * core. Note that the function does *not* free the crtc structure itself,
  623. * this is the responsibility of the caller.
  624. */
  625. void drm_crtc_cleanup(struct drm_crtc *crtc)
  626. {
  627. struct drm_device *dev = crtc->dev;
  628. kfree(crtc->gamma_store);
  629. crtc->gamma_store = NULL;
  630. drm_modeset_lock_fini(&crtc->mutex);
  631. drm_mode_object_put(dev, &crtc->base);
  632. list_del(&crtc->head);
  633. dev->mode_config.num_crtc--;
  634. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  635. if (crtc->state && crtc->funcs->atomic_destroy_state)
  636. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  637. memset(crtc, 0, sizeof(*crtc));
  638. }
  639. EXPORT_SYMBOL(drm_crtc_cleanup);
  640. /**
  641. * drm_crtc_index - find the index of a registered CRTC
  642. * @crtc: CRTC to find index for
  643. *
  644. * Given a registered CRTC, return the index of that CRTC within a DRM
  645. * device's list of CRTCs.
  646. */
  647. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  648. {
  649. unsigned int index = 0;
  650. struct drm_crtc *tmp;
  651. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  652. if (tmp == crtc)
  653. return index;
  654. index++;
  655. }
  656. BUG();
  657. }
  658. EXPORT_SYMBOL(drm_crtc_index);
  659. /*
  660. * drm_mode_remove - remove and free a mode
  661. * @connector: connector list to modify
  662. * @mode: mode to remove
  663. *
  664. * Remove @mode from @connector's mode list, then free it.
  665. */
  666. static void drm_mode_remove(struct drm_connector *connector,
  667. struct drm_display_mode *mode)
  668. {
  669. list_del(&mode->head);
  670. drm_mode_destroy(connector->dev, mode);
  671. }
  672. /**
  673. * drm_display_info_set_bus_formats - set the supported bus formats
  674. * @info: display info to store bus formats in
  675. * @formats: array containing the supported bus formats
  676. * @num_formats: the number of entries in the fmts array
  677. *
  678. * Store the supported bus formats in display info structure.
  679. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  680. * a full list of available formats.
  681. */
  682. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  683. const u32 *formats,
  684. unsigned int num_formats)
  685. {
  686. u32 *fmts = NULL;
  687. if (!formats && num_formats)
  688. return -EINVAL;
  689. if (formats && num_formats) {
  690. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  691. GFP_KERNEL);
  692. if (!fmts)
  693. return -ENOMEM;
  694. }
  695. kfree(info->bus_formats);
  696. info->bus_formats = fmts;
  697. info->num_bus_formats = num_formats;
  698. return 0;
  699. }
  700. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  701. /**
  702. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  703. * @connector: connector to quwery
  704. *
  705. * The kernel supports per-connector configration of its consoles through
  706. * use of the video= parameter. This function parses that option and
  707. * extracts the user's specified mode (or enable/disable status) for a
  708. * particular connector. This is typically only used during the early fbdev
  709. * setup.
  710. */
  711. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  712. {
  713. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  714. char *option = NULL;
  715. if (fb_get_options(connector->name, &option))
  716. return;
  717. if (!drm_mode_parse_command_line_for_connector(option,
  718. connector,
  719. mode))
  720. return;
  721. if (mode->force) {
  722. const char *s;
  723. switch (mode->force) {
  724. case DRM_FORCE_OFF:
  725. s = "OFF";
  726. break;
  727. case DRM_FORCE_ON_DIGITAL:
  728. s = "ON - dig";
  729. break;
  730. default:
  731. case DRM_FORCE_ON:
  732. s = "ON";
  733. break;
  734. }
  735. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  736. connector->force = mode->force;
  737. }
  738. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  739. connector->name,
  740. mode->xres, mode->yres,
  741. mode->refresh_specified ? mode->refresh : 60,
  742. mode->rb ? " reduced blanking" : "",
  743. mode->margins ? " with margins" : "",
  744. mode->interlace ? " interlaced" : "");
  745. }
  746. /**
  747. * drm_connector_init - Init a preallocated connector
  748. * @dev: DRM device
  749. * @connector: the connector to init
  750. * @funcs: callbacks for this connector
  751. * @connector_type: user visible type of the connector
  752. *
  753. * Initialises a preallocated connector. Connectors should be
  754. * subclassed as part of driver connector objects.
  755. *
  756. * Returns:
  757. * Zero on success, error code on failure.
  758. */
  759. int drm_connector_init(struct drm_device *dev,
  760. struct drm_connector *connector,
  761. const struct drm_connector_funcs *funcs,
  762. int connector_type)
  763. {
  764. struct drm_mode_config *config = &dev->mode_config;
  765. int ret;
  766. struct ida *connector_ida =
  767. &drm_connector_enum_list[connector_type].ida;
  768. drm_modeset_lock_all(dev);
  769. ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  770. if (ret)
  771. goto out_unlock;
  772. connector->base.properties = &connector->properties;
  773. connector->dev = dev;
  774. connector->funcs = funcs;
  775. connector->connector_type = connector_type;
  776. connector->connector_type_id =
  777. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  778. if (connector->connector_type_id < 0) {
  779. ret = connector->connector_type_id;
  780. goto out_put;
  781. }
  782. connector->name =
  783. kasprintf(GFP_KERNEL, "%s-%d",
  784. drm_connector_enum_list[connector_type].name,
  785. connector->connector_type_id);
  786. if (!connector->name) {
  787. ret = -ENOMEM;
  788. goto out_put;
  789. }
  790. INIT_LIST_HEAD(&connector->probed_modes);
  791. INIT_LIST_HEAD(&connector->modes);
  792. connector->edid_blob_ptr = NULL;
  793. connector->status = connector_status_unknown;
  794. drm_connector_get_cmdline_mode(connector);
  795. /* We should add connectors at the end to avoid upsetting the connector
  796. * index too much. */
  797. list_add_tail(&connector->head, &config->connector_list);
  798. config->num_connector++;
  799. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  800. drm_object_attach_property(&connector->base,
  801. config->edid_property,
  802. 0);
  803. drm_object_attach_property(&connector->base,
  804. config->dpms_property, 0);
  805. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  806. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  807. }
  808. connector->debugfs_entry = NULL;
  809. out_put:
  810. if (ret)
  811. drm_mode_object_put(dev, &connector->base);
  812. out_unlock:
  813. drm_modeset_unlock_all(dev);
  814. return ret;
  815. }
  816. EXPORT_SYMBOL(drm_connector_init);
  817. /**
  818. * drm_connector_cleanup - cleans up an initialised connector
  819. * @connector: connector to cleanup
  820. *
  821. * Cleans up the connector but doesn't free the object.
  822. */
  823. void drm_connector_cleanup(struct drm_connector *connector)
  824. {
  825. struct drm_device *dev = connector->dev;
  826. struct drm_display_mode *mode, *t;
  827. if (connector->tile_group) {
  828. drm_mode_put_tile_group(dev, connector->tile_group);
  829. connector->tile_group = NULL;
  830. }
  831. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  832. drm_mode_remove(connector, mode);
  833. list_for_each_entry_safe(mode, t, &connector->modes, head)
  834. drm_mode_remove(connector, mode);
  835. ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  836. connector->connector_type_id);
  837. kfree(connector->display_info.bus_formats);
  838. drm_mode_object_put(dev, &connector->base);
  839. kfree(connector->name);
  840. connector->name = NULL;
  841. list_del(&connector->head);
  842. dev->mode_config.num_connector--;
  843. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  844. if (connector->state && connector->funcs->atomic_destroy_state)
  845. connector->funcs->atomic_destroy_state(connector,
  846. connector->state);
  847. memset(connector, 0, sizeof(*connector));
  848. }
  849. EXPORT_SYMBOL(drm_connector_cleanup);
  850. /**
  851. * drm_connector_index - find the index of a registered connector
  852. * @connector: connector to find index for
  853. *
  854. * Given a registered connector, return the index of that connector within a DRM
  855. * device's list of connectors.
  856. */
  857. unsigned int drm_connector_index(struct drm_connector *connector)
  858. {
  859. unsigned int index = 0;
  860. struct drm_connector *tmp;
  861. struct drm_mode_config *config = &connector->dev->mode_config;
  862. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  863. list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
  864. if (tmp == connector)
  865. return index;
  866. index++;
  867. }
  868. BUG();
  869. }
  870. EXPORT_SYMBOL(drm_connector_index);
  871. /**
  872. * drm_connector_register - register a connector
  873. * @connector: the connector to register
  874. *
  875. * Register userspace interfaces for a connector
  876. *
  877. * Returns:
  878. * Zero on success, error code on failure.
  879. */
  880. int drm_connector_register(struct drm_connector *connector)
  881. {
  882. int ret;
  883. drm_mode_object_register(connector->dev, &connector->base);
  884. ret = drm_sysfs_connector_add(connector);
  885. if (ret)
  886. return ret;
  887. ret = drm_debugfs_connector_add(connector);
  888. if (ret) {
  889. drm_sysfs_connector_remove(connector);
  890. return ret;
  891. }
  892. return 0;
  893. }
  894. EXPORT_SYMBOL(drm_connector_register);
  895. /**
  896. * drm_connector_unregister - unregister a connector
  897. * @connector: the connector to unregister
  898. *
  899. * Unregister userspace interfaces for a connector
  900. */
  901. void drm_connector_unregister(struct drm_connector *connector)
  902. {
  903. drm_sysfs_connector_remove(connector);
  904. drm_debugfs_connector_remove(connector);
  905. }
  906. EXPORT_SYMBOL(drm_connector_unregister);
  907. /**
  908. * drm_connector_unplug_all - unregister connector userspace interfaces
  909. * @dev: drm device
  910. *
  911. * This function unregisters all connector userspace interfaces in sysfs. Should
  912. * be call when the device is disconnected, e.g. from an usb driver's
  913. * ->disconnect callback.
  914. */
  915. void drm_connector_unplug_all(struct drm_device *dev)
  916. {
  917. struct drm_connector *connector;
  918. /* taking the mode config mutex ends up in a clash with sysfs */
  919. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  920. drm_connector_unregister(connector);
  921. }
  922. EXPORT_SYMBOL(drm_connector_unplug_all);
  923. /**
  924. * drm_encoder_init - Init a preallocated encoder
  925. * @dev: drm device
  926. * @encoder: the encoder to init
  927. * @funcs: callbacks for this encoder
  928. * @encoder_type: user visible type of the encoder
  929. *
  930. * Initialises a preallocated encoder. Encoder should be
  931. * subclassed as part of driver encoder objects.
  932. *
  933. * Returns:
  934. * Zero on success, error code on failure.
  935. */
  936. int drm_encoder_init(struct drm_device *dev,
  937. struct drm_encoder *encoder,
  938. const struct drm_encoder_funcs *funcs,
  939. int encoder_type)
  940. {
  941. int ret;
  942. drm_modeset_lock_all(dev);
  943. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  944. if (ret)
  945. goto out_unlock;
  946. encoder->dev = dev;
  947. encoder->encoder_type = encoder_type;
  948. encoder->funcs = funcs;
  949. encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  950. drm_encoder_enum_list[encoder_type].name,
  951. encoder->base.id);
  952. if (!encoder->name) {
  953. ret = -ENOMEM;
  954. goto out_put;
  955. }
  956. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  957. dev->mode_config.num_encoder++;
  958. out_put:
  959. if (ret)
  960. drm_mode_object_put(dev, &encoder->base);
  961. out_unlock:
  962. drm_modeset_unlock_all(dev);
  963. return ret;
  964. }
  965. EXPORT_SYMBOL(drm_encoder_init);
  966. /**
  967. * drm_encoder_cleanup - cleans up an initialised encoder
  968. * @encoder: encoder to cleanup
  969. *
  970. * Cleans up the encoder but doesn't free the object.
  971. */
  972. void drm_encoder_cleanup(struct drm_encoder *encoder)
  973. {
  974. struct drm_device *dev = encoder->dev;
  975. drm_modeset_lock_all(dev);
  976. drm_mode_object_put(dev, &encoder->base);
  977. kfree(encoder->name);
  978. list_del(&encoder->head);
  979. dev->mode_config.num_encoder--;
  980. drm_modeset_unlock_all(dev);
  981. memset(encoder, 0, sizeof(*encoder));
  982. }
  983. EXPORT_SYMBOL(drm_encoder_cleanup);
  984. /**
  985. * drm_universal_plane_init - Initialize a new universal plane object
  986. * @dev: DRM device
  987. * @plane: plane object to init
  988. * @possible_crtcs: bitmask of possible CRTCs
  989. * @funcs: callbacks for the new plane
  990. * @formats: array of supported formats (%DRM_FORMAT_*)
  991. * @format_count: number of elements in @formats
  992. * @type: type of plane (overlay, primary, cursor)
  993. *
  994. * Initializes a plane object of type @type.
  995. *
  996. * Returns:
  997. * Zero on success, error code on failure.
  998. */
  999. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1000. unsigned long possible_crtcs,
  1001. const struct drm_plane_funcs *funcs,
  1002. const uint32_t *formats, uint32_t format_count,
  1003. enum drm_plane_type type)
  1004. {
  1005. struct drm_mode_config *config = &dev->mode_config;
  1006. int ret;
  1007. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1008. if (ret)
  1009. return ret;
  1010. drm_modeset_lock_init(&plane->mutex);
  1011. plane->base.properties = &plane->properties;
  1012. plane->dev = dev;
  1013. plane->funcs = funcs;
  1014. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  1015. GFP_KERNEL);
  1016. if (!plane->format_types) {
  1017. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1018. drm_mode_object_put(dev, &plane->base);
  1019. return -ENOMEM;
  1020. }
  1021. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1022. plane->format_count = format_count;
  1023. plane->possible_crtcs = possible_crtcs;
  1024. plane->type = type;
  1025. list_add_tail(&plane->head, &config->plane_list);
  1026. config->num_total_plane++;
  1027. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1028. config->num_overlay_plane++;
  1029. drm_object_attach_property(&plane->base,
  1030. config->plane_type_property,
  1031. plane->type);
  1032. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  1033. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  1034. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  1035. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  1036. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  1037. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  1038. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  1039. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  1040. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  1041. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  1042. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  1043. }
  1044. return 0;
  1045. }
  1046. EXPORT_SYMBOL(drm_universal_plane_init);
  1047. /**
  1048. * drm_plane_init - Initialize a legacy plane
  1049. * @dev: DRM device
  1050. * @plane: plane object to init
  1051. * @possible_crtcs: bitmask of possible CRTCs
  1052. * @funcs: callbacks for the new plane
  1053. * @formats: array of supported formats (%DRM_FORMAT_*)
  1054. * @format_count: number of elements in @formats
  1055. * @is_primary: plane type (primary vs overlay)
  1056. *
  1057. * Legacy API to initialize a DRM plane.
  1058. *
  1059. * New drivers should call drm_universal_plane_init() instead.
  1060. *
  1061. * Returns:
  1062. * Zero on success, error code on failure.
  1063. */
  1064. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1065. unsigned long possible_crtcs,
  1066. const struct drm_plane_funcs *funcs,
  1067. const uint32_t *formats, uint32_t format_count,
  1068. bool is_primary)
  1069. {
  1070. enum drm_plane_type type;
  1071. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1072. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1073. formats, format_count, type);
  1074. }
  1075. EXPORT_SYMBOL(drm_plane_init);
  1076. /**
  1077. * drm_plane_cleanup - Clean up the core plane usage
  1078. * @plane: plane to cleanup
  1079. *
  1080. * This function cleans up @plane and removes it from the DRM mode setting
  1081. * core. Note that the function does *not* free the plane structure itself,
  1082. * this is the responsibility of the caller.
  1083. */
  1084. void drm_plane_cleanup(struct drm_plane *plane)
  1085. {
  1086. struct drm_device *dev = plane->dev;
  1087. drm_modeset_lock_all(dev);
  1088. kfree(plane->format_types);
  1089. drm_mode_object_put(dev, &plane->base);
  1090. BUG_ON(list_empty(&plane->head));
  1091. list_del(&plane->head);
  1092. dev->mode_config.num_total_plane--;
  1093. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1094. dev->mode_config.num_overlay_plane--;
  1095. drm_modeset_unlock_all(dev);
  1096. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1097. if (plane->state && plane->funcs->atomic_destroy_state)
  1098. plane->funcs->atomic_destroy_state(plane, plane->state);
  1099. memset(plane, 0, sizeof(*plane));
  1100. }
  1101. EXPORT_SYMBOL(drm_plane_cleanup);
  1102. /**
  1103. * drm_plane_index - find the index of a registered plane
  1104. * @plane: plane to find index for
  1105. *
  1106. * Given a registered plane, return the index of that CRTC within a DRM
  1107. * device's list of planes.
  1108. */
  1109. unsigned int drm_plane_index(struct drm_plane *plane)
  1110. {
  1111. unsigned int index = 0;
  1112. struct drm_plane *tmp;
  1113. list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
  1114. if (tmp == plane)
  1115. return index;
  1116. index++;
  1117. }
  1118. BUG();
  1119. }
  1120. EXPORT_SYMBOL(drm_plane_index);
  1121. /**
  1122. * drm_plane_from_index - find the registered plane at an index
  1123. * @dev: DRM device
  1124. * @idx: index of registered plane to find for
  1125. *
  1126. * Given a plane index, return the registered plane from DRM device's
  1127. * list of planes with matching index.
  1128. */
  1129. struct drm_plane *
  1130. drm_plane_from_index(struct drm_device *dev, int idx)
  1131. {
  1132. struct drm_plane *plane;
  1133. unsigned int i = 0;
  1134. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  1135. if (i == idx)
  1136. return plane;
  1137. i++;
  1138. }
  1139. return NULL;
  1140. }
  1141. EXPORT_SYMBOL(drm_plane_from_index);
  1142. /**
  1143. * drm_plane_force_disable - Forcibly disable a plane
  1144. * @plane: plane to disable
  1145. *
  1146. * Forces the plane to be disabled.
  1147. *
  1148. * Used when the plane's current framebuffer is destroyed,
  1149. * and when restoring fbdev mode.
  1150. */
  1151. void drm_plane_force_disable(struct drm_plane *plane)
  1152. {
  1153. int ret;
  1154. if (!plane->fb)
  1155. return;
  1156. plane->old_fb = plane->fb;
  1157. ret = plane->funcs->disable_plane(plane);
  1158. if (ret) {
  1159. DRM_ERROR("failed to disable plane with busy fb\n");
  1160. plane->old_fb = NULL;
  1161. return;
  1162. }
  1163. /* disconnect the plane from the fb and crtc: */
  1164. drm_framebuffer_unreference(plane->old_fb);
  1165. plane->old_fb = NULL;
  1166. plane->fb = NULL;
  1167. plane->crtc = NULL;
  1168. }
  1169. EXPORT_SYMBOL(drm_plane_force_disable);
  1170. static int drm_mode_create_standard_properties(struct drm_device *dev)
  1171. {
  1172. struct drm_property *prop;
  1173. /*
  1174. * Standard properties (apply to all connectors)
  1175. */
  1176. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1177. DRM_MODE_PROP_IMMUTABLE,
  1178. "EDID", 0);
  1179. if (!prop)
  1180. return -ENOMEM;
  1181. dev->mode_config.edid_property = prop;
  1182. prop = drm_property_create_enum(dev, 0,
  1183. "DPMS", drm_dpms_enum_list,
  1184. ARRAY_SIZE(drm_dpms_enum_list));
  1185. if (!prop)
  1186. return -ENOMEM;
  1187. dev->mode_config.dpms_property = prop;
  1188. prop = drm_property_create(dev,
  1189. DRM_MODE_PROP_BLOB |
  1190. DRM_MODE_PROP_IMMUTABLE,
  1191. "PATH", 0);
  1192. if (!prop)
  1193. return -ENOMEM;
  1194. dev->mode_config.path_property = prop;
  1195. prop = drm_property_create(dev,
  1196. DRM_MODE_PROP_BLOB |
  1197. DRM_MODE_PROP_IMMUTABLE,
  1198. "TILE", 0);
  1199. if (!prop)
  1200. return -ENOMEM;
  1201. dev->mode_config.tile_property = prop;
  1202. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1203. "type", drm_plane_type_enum_list,
  1204. ARRAY_SIZE(drm_plane_type_enum_list));
  1205. if (!prop)
  1206. return -ENOMEM;
  1207. dev->mode_config.plane_type_property = prop;
  1208. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1209. "SRC_X", 0, UINT_MAX);
  1210. if (!prop)
  1211. return -ENOMEM;
  1212. dev->mode_config.prop_src_x = prop;
  1213. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1214. "SRC_Y", 0, UINT_MAX);
  1215. if (!prop)
  1216. return -ENOMEM;
  1217. dev->mode_config.prop_src_y = prop;
  1218. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1219. "SRC_W", 0, UINT_MAX);
  1220. if (!prop)
  1221. return -ENOMEM;
  1222. dev->mode_config.prop_src_w = prop;
  1223. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1224. "SRC_H", 0, UINT_MAX);
  1225. if (!prop)
  1226. return -ENOMEM;
  1227. dev->mode_config.prop_src_h = prop;
  1228. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1229. "CRTC_X", INT_MIN, INT_MAX);
  1230. if (!prop)
  1231. return -ENOMEM;
  1232. dev->mode_config.prop_crtc_x = prop;
  1233. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1234. "CRTC_Y", INT_MIN, INT_MAX);
  1235. if (!prop)
  1236. return -ENOMEM;
  1237. dev->mode_config.prop_crtc_y = prop;
  1238. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1239. "CRTC_W", 0, INT_MAX);
  1240. if (!prop)
  1241. return -ENOMEM;
  1242. dev->mode_config.prop_crtc_w = prop;
  1243. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1244. "CRTC_H", 0, INT_MAX);
  1245. if (!prop)
  1246. return -ENOMEM;
  1247. dev->mode_config.prop_crtc_h = prop;
  1248. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1249. "FB_ID", DRM_MODE_OBJECT_FB);
  1250. if (!prop)
  1251. return -ENOMEM;
  1252. dev->mode_config.prop_fb_id = prop;
  1253. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1254. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  1255. if (!prop)
  1256. return -ENOMEM;
  1257. dev->mode_config.prop_crtc_id = prop;
  1258. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  1259. "ACTIVE");
  1260. if (!prop)
  1261. return -ENOMEM;
  1262. dev->mode_config.prop_active = prop;
  1263. return 0;
  1264. }
  1265. /**
  1266. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1267. * @dev: DRM device
  1268. *
  1269. * Called by a driver the first time a DVI-I connector is made.
  1270. */
  1271. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1272. {
  1273. struct drm_property *dvi_i_selector;
  1274. struct drm_property *dvi_i_subconnector;
  1275. if (dev->mode_config.dvi_i_select_subconnector_property)
  1276. return 0;
  1277. dvi_i_selector =
  1278. drm_property_create_enum(dev, 0,
  1279. "select subconnector",
  1280. drm_dvi_i_select_enum_list,
  1281. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1282. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1283. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1284. "subconnector",
  1285. drm_dvi_i_subconnector_enum_list,
  1286. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1287. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1288. return 0;
  1289. }
  1290. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1291. /**
  1292. * drm_create_tv_properties - create TV specific connector properties
  1293. * @dev: DRM device
  1294. * @num_modes: number of different TV formats (modes) supported
  1295. * @modes: array of pointers to strings containing name of each format
  1296. *
  1297. * Called by a driver's TV initialization routine, this function creates
  1298. * the TV specific connector properties for a given device. Caller is
  1299. * responsible for allocating a list of format names and passing them to
  1300. * this routine.
  1301. */
  1302. int drm_mode_create_tv_properties(struct drm_device *dev,
  1303. unsigned int num_modes,
  1304. char *modes[])
  1305. {
  1306. struct drm_property *tv_selector;
  1307. struct drm_property *tv_subconnector;
  1308. unsigned int i;
  1309. if (dev->mode_config.tv_select_subconnector_property)
  1310. return 0;
  1311. /*
  1312. * Basic connector properties
  1313. */
  1314. tv_selector = drm_property_create_enum(dev, 0,
  1315. "select subconnector",
  1316. drm_tv_select_enum_list,
  1317. ARRAY_SIZE(drm_tv_select_enum_list));
  1318. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1319. tv_subconnector =
  1320. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1321. "subconnector",
  1322. drm_tv_subconnector_enum_list,
  1323. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1324. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1325. /*
  1326. * Other, TV specific properties: margins & TV modes.
  1327. */
  1328. dev->mode_config.tv_left_margin_property =
  1329. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1330. dev->mode_config.tv_right_margin_property =
  1331. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1332. dev->mode_config.tv_top_margin_property =
  1333. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1334. dev->mode_config.tv_bottom_margin_property =
  1335. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1336. dev->mode_config.tv_mode_property =
  1337. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1338. "mode", num_modes);
  1339. for (i = 0; i < num_modes; i++)
  1340. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1341. i, modes[i]);
  1342. dev->mode_config.tv_brightness_property =
  1343. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1344. dev->mode_config.tv_contrast_property =
  1345. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1346. dev->mode_config.tv_flicker_reduction_property =
  1347. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1348. dev->mode_config.tv_overscan_property =
  1349. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1350. dev->mode_config.tv_saturation_property =
  1351. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1352. dev->mode_config.tv_hue_property =
  1353. drm_property_create_range(dev, 0, "hue", 0, 100);
  1354. return 0;
  1355. }
  1356. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1357. /**
  1358. * drm_mode_create_scaling_mode_property - create scaling mode property
  1359. * @dev: DRM device
  1360. *
  1361. * Called by a driver the first time it's needed, must be attached to desired
  1362. * connectors.
  1363. */
  1364. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1365. {
  1366. struct drm_property *scaling_mode;
  1367. if (dev->mode_config.scaling_mode_property)
  1368. return 0;
  1369. scaling_mode =
  1370. drm_property_create_enum(dev, 0, "scaling mode",
  1371. drm_scaling_mode_enum_list,
  1372. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1373. dev->mode_config.scaling_mode_property = scaling_mode;
  1374. return 0;
  1375. }
  1376. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1377. /**
  1378. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1379. * @dev: DRM device
  1380. *
  1381. * Called by a driver the first time it's needed, must be attached to desired
  1382. * connectors.
  1383. *
  1384. * Returns:
  1385. * Zero on success, negative errno on failure.
  1386. */
  1387. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1388. {
  1389. if (dev->mode_config.aspect_ratio_property)
  1390. return 0;
  1391. dev->mode_config.aspect_ratio_property =
  1392. drm_property_create_enum(dev, 0, "aspect ratio",
  1393. drm_aspect_ratio_enum_list,
  1394. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1395. if (dev->mode_config.aspect_ratio_property == NULL)
  1396. return -ENOMEM;
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1400. /**
  1401. * drm_mode_create_dirty_property - create dirty property
  1402. * @dev: DRM device
  1403. *
  1404. * Called by a driver the first time it's needed, must be attached to desired
  1405. * connectors.
  1406. */
  1407. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1408. {
  1409. struct drm_property *dirty_info;
  1410. if (dev->mode_config.dirty_info_property)
  1411. return 0;
  1412. dirty_info =
  1413. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1414. "dirty",
  1415. drm_dirty_info_enum_list,
  1416. ARRAY_SIZE(drm_dirty_info_enum_list));
  1417. dev->mode_config.dirty_info_property = dirty_info;
  1418. return 0;
  1419. }
  1420. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1421. /**
  1422. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1423. * @dev: DRM device
  1424. *
  1425. * Create the the suggested x/y offset property for connectors.
  1426. */
  1427. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1428. {
  1429. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1430. return 0;
  1431. dev->mode_config.suggested_x_property =
  1432. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1433. dev->mode_config.suggested_y_property =
  1434. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1435. if (dev->mode_config.suggested_x_property == NULL ||
  1436. dev->mode_config.suggested_y_property == NULL)
  1437. return -ENOMEM;
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1441. static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  1442. {
  1443. uint32_t total_objects = 0;
  1444. total_objects += dev->mode_config.num_crtc;
  1445. total_objects += dev->mode_config.num_connector;
  1446. total_objects += dev->mode_config.num_encoder;
  1447. group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
  1448. if (!group->id_list)
  1449. return -ENOMEM;
  1450. group->num_crtcs = 0;
  1451. group->num_connectors = 0;
  1452. group->num_encoders = 0;
  1453. return 0;
  1454. }
  1455. void drm_mode_group_destroy(struct drm_mode_group *group)
  1456. {
  1457. kfree(group->id_list);
  1458. group->id_list = NULL;
  1459. }
  1460. /*
  1461. * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
  1462. * the drm core's responsibility to set up mode control groups.
  1463. */
  1464. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  1465. struct drm_mode_group *group)
  1466. {
  1467. struct drm_crtc *crtc;
  1468. struct drm_encoder *encoder;
  1469. struct drm_connector *connector;
  1470. int ret;
  1471. ret = drm_mode_group_init(dev, group);
  1472. if (ret)
  1473. return ret;
  1474. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  1475. group->id_list[group->num_crtcs++] = crtc->base.id;
  1476. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  1477. group->id_list[group->num_crtcs + group->num_encoders++] =
  1478. encoder->base.id;
  1479. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1480. group->id_list[group->num_crtcs + group->num_encoders +
  1481. group->num_connectors++] = connector->base.id;
  1482. return 0;
  1483. }
  1484. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  1485. void drm_reinit_primary_mode_group(struct drm_device *dev)
  1486. {
  1487. drm_modeset_lock_all(dev);
  1488. drm_mode_group_destroy(&dev->primary->mode_group);
  1489. drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  1490. drm_modeset_unlock_all(dev);
  1491. }
  1492. EXPORT_SYMBOL(drm_reinit_primary_mode_group);
  1493. /**
  1494. * drm_mode_getresources - get graphics configuration
  1495. * @dev: drm device for the ioctl
  1496. * @data: data pointer for the ioctl
  1497. * @file_priv: drm file for the ioctl call
  1498. *
  1499. * Construct a set of configuration description structures and return
  1500. * them to the user, including CRTC, connector and framebuffer configuration.
  1501. *
  1502. * Called by the user via ioctl.
  1503. *
  1504. * Returns:
  1505. * Zero on success, negative errno on failure.
  1506. */
  1507. int drm_mode_getresources(struct drm_device *dev, void *data,
  1508. struct drm_file *file_priv)
  1509. {
  1510. struct drm_mode_card_res *card_res = data;
  1511. struct list_head *lh;
  1512. struct drm_framebuffer *fb;
  1513. struct drm_connector *connector;
  1514. struct drm_crtc *crtc;
  1515. struct drm_encoder *encoder;
  1516. int ret = 0;
  1517. int connector_count = 0;
  1518. int crtc_count = 0;
  1519. int fb_count = 0;
  1520. int encoder_count = 0;
  1521. int copied = 0, i;
  1522. uint32_t __user *fb_id;
  1523. uint32_t __user *crtc_id;
  1524. uint32_t __user *connector_id;
  1525. uint32_t __user *encoder_id;
  1526. struct drm_mode_group *mode_group;
  1527. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1528. return -EINVAL;
  1529. mutex_lock(&file_priv->fbs_lock);
  1530. /*
  1531. * For the non-control nodes we need to limit the list of resources
  1532. * by IDs in the group list for this node
  1533. */
  1534. list_for_each(lh, &file_priv->fbs)
  1535. fb_count++;
  1536. /* handle this in 4 parts */
  1537. /* FBs */
  1538. if (card_res->count_fbs >= fb_count) {
  1539. copied = 0;
  1540. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1541. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1542. if (put_user(fb->base.id, fb_id + copied)) {
  1543. mutex_unlock(&file_priv->fbs_lock);
  1544. return -EFAULT;
  1545. }
  1546. copied++;
  1547. }
  1548. }
  1549. card_res->count_fbs = fb_count;
  1550. mutex_unlock(&file_priv->fbs_lock);
  1551. /* mode_config.mutex protects the connector list against e.g. DP MST
  1552. * connector hot-adding. CRTC/Plane lists are invariant. */
  1553. mutex_lock(&dev->mode_config.mutex);
  1554. if (!drm_is_primary_client(file_priv)) {
  1555. mode_group = NULL;
  1556. list_for_each(lh, &dev->mode_config.crtc_list)
  1557. crtc_count++;
  1558. list_for_each(lh, &dev->mode_config.connector_list)
  1559. connector_count++;
  1560. list_for_each(lh, &dev->mode_config.encoder_list)
  1561. encoder_count++;
  1562. } else {
  1563. mode_group = &file_priv->master->minor->mode_group;
  1564. crtc_count = mode_group->num_crtcs;
  1565. connector_count = mode_group->num_connectors;
  1566. encoder_count = mode_group->num_encoders;
  1567. }
  1568. card_res->max_height = dev->mode_config.max_height;
  1569. card_res->min_height = dev->mode_config.min_height;
  1570. card_res->max_width = dev->mode_config.max_width;
  1571. card_res->min_width = dev->mode_config.min_width;
  1572. /* CRTCs */
  1573. if (card_res->count_crtcs >= crtc_count) {
  1574. copied = 0;
  1575. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1576. if (!mode_group) {
  1577. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1578. head) {
  1579. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1580. if (put_user(crtc->base.id, crtc_id + copied)) {
  1581. ret = -EFAULT;
  1582. goto out;
  1583. }
  1584. copied++;
  1585. }
  1586. } else {
  1587. for (i = 0; i < mode_group->num_crtcs; i++) {
  1588. if (put_user(mode_group->id_list[i],
  1589. crtc_id + copied)) {
  1590. ret = -EFAULT;
  1591. goto out;
  1592. }
  1593. copied++;
  1594. }
  1595. }
  1596. }
  1597. card_res->count_crtcs = crtc_count;
  1598. /* Encoders */
  1599. if (card_res->count_encoders >= encoder_count) {
  1600. copied = 0;
  1601. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1602. if (!mode_group) {
  1603. list_for_each_entry(encoder,
  1604. &dev->mode_config.encoder_list,
  1605. head) {
  1606. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1607. encoder->name);
  1608. if (put_user(encoder->base.id, encoder_id +
  1609. copied)) {
  1610. ret = -EFAULT;
  1611. goto out;
  1612. }
  1613. copied++;
  1614. }
  1615. } else {
  1616. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1617. if (put_user(mode_group->id_list[i],
  1618. encoder_id + copied)) {
  1619. ret = -EFAULT;
  1620. goto out;
  1621. }
  1622. copied++;
  1623. }
  1624. }
  1625. }
  1626. card_res->count_encoders = encoder_count;
  1627. /* Connectors */
  1628. if (card_res->count_connectors >= connector_count) {
  1629. copied = 0;
  1630. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1631. if (!mode_group) {
  1632. list_for_each_entry(connector,
  1633. &dev->mode_config.connector_list,
  1634. head) {
  1635. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1636. connector->base.id,
  1637. connector->name);
  1638. if (put_user(connector->base.id,
  1639. connector_id + copied)) {
  1640. ret = -EFAULT;
  1641. goto out;
  1642. }
  1643. copied++;
  1644. }
  1645. } else {
  1646. int start = mode_group->num_crtcs +
  1647. mode_group->num_encoders;
  1648. for (i = start; i < start + mode_group->num_connectors; i++) {
  1649. if (put_user(mode_group->id_list[i],
  1650. connector_id + copied)) {
  1651. ret = -EFAULT;
  1652. goto out;
  1653. }
  1654. copied++;
  1655. }
  1656. }
  1657. }
  1658. card_res->count_connectors = connector_count;
  1659. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1660. card_res->count_connectors, card_res->count_encoders);
  1661. out:
  1662. mutex_unlock(&dev->mode_config.mutex);
  1663. return ret;
  1664. }
  1665. /**
  1666. * drm_mode_getcrtc - get CRTC configuration
  1667. * @dev: drm device for the ioctl
  1668. * @data: data pointer for the ioctl
  1669. * @file_priv: drm file for the ioctl call
  1670. *
  1671. * Construct a CRTC configuration structure to return to the user.
  1672. *
  1673. * Called by the user via ioctl.
  1674. *
  1675. * Returns:
  1676. * Zero on success, negative errno on failure.
  1677. */
  1678. int drm_mode_getcrtc(struct drm_device *dev,
  1679. void *data, struct drm_file *file_priv)
  1680. {
  1681. struct drm_mode_crtc *crtc_resp = data;
  1682. struct drm_crtc *crtc;
  1683. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1684. return -EINVAL;
  1685. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1686. if (!crtc)
  1687. return -ENOENT;
  1688. drm_modeset_lock_crtc(crtc, crtc->primary);
  1689. crtc_resp->gamma_size = crtc->gamma_size;
  1690. if (crtc->primary->fb)
  1691. crtc_resp->fb_id = crtc->primary->fb->base.id;
  1692. else
  1693. crtc_resp->fb_id = 0;
  1694. if (crtc->state) {
  1695. crtc_resp->x = crtc->primary->state->src_x >> 16;
  1696. crtc_resp->y = crtc->primary->state->src_y >> 16;
  1697. if (crtc->state->enable) {
  1698. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  1699. crtc_resp->mode_valid = 1;
  1700. } else {
  1701. crtc_resp->mode_valid = 0;
  1702. }
  1703. } else {
  1704. crtc_resp->x = crtc->x;
  1705. crtc_resp->y = crtc->y;
  1706. if (crtc->enabled) {
  1707. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1708. crtc_resp->mode_valid = 1;
  1709. } else {
  1710. crtc_resp->mode_valid = 0;
  1711. }
  1712. }
  1713. drm_modeset_unlock_crtc(crtc);
  1714. return 0;
  1715. }
  1716. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1717. const struct drm_file *file_priv)
  1718. {
  1719. /*
  1720. * If user-space hasn't configured the driver to expose the stereo 3D
  1721. * modes, don't expose them.
  1722. */
  1723. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1724. return false;
  1725. return true;
  1726. }
  1727. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1728. {
  1729. /* For atomic drivers only state objects are synchronously updated and
  1730. * protected by modeset locks, so check those first. */
  1731. if (connector->state)
  1732. return connector->state->best_encoder;
  1733. return connector->encoder;
  1734. }
  1735. /* helper for getconnector and getproperties ioctls */
  1736. static int get_properties(struct drm_mode_object *obj, bool atomic,
  1737. uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  1738. uint32_t *arg_count_props)
  1739. {
  1740. int props_count;
  1741. int i, ret, copied;
  1742. props_count = obj->properties->count;
  1743. if (!atomic)
  1744. props_count -= obj->properties->atomic_count;
  1745. if ((*arg_count_props >= props_count) && props_count) {
  1746. for (i = 0, copied = 0; copied < props_count; i++) {
  1747. struct drm_property *prop = obj->properties->properties[i];
  1748. uint64_t val;
  1749. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  1750. continue;
  1751. ret = drm_object_property_get_value(obj, prop, &val);
  1752. if (ret)
  1753. return ret;
  1754. if (put_user(prop->base.id, prop_ptr + copied))
  1755. return -EFAULT;
  1756. if (put_user(val, prop_values + copied))
  1757. return -EFAULT;
  1758. copied++;
  1759. }
  1760. }
  1761. *arg_count_props = props_count;
  1762. return 0;
  1763. }
  1764. /**
  1765. * drm_mode_getconnector - get connector configuration
  1766. * @dev: drm device for the ioctl
  1767. * @data: data pointer for the ioctl
  1768. * @file_priv: drm file for the ioctl call
  1769. *
  1770. * Construct a connector configuration structure to return to the user.
  1771. *
  1772. * Called by the user via ioctl.
  1773. *
  1774. * Returns:
  1775. * Zero on success, negative errno on failure.
  1776. */
  1777. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1778. struct drm_file *file_priv)
  1779. {
  1780. struct drm_mode_get_connector *out_resp = data;
  1781. struct drm_connector *connector;
  1782. struct drm_encoder *encoder;
  1783. struct drm_display_mode *mode;
  1784. int mode_count = 0;
  1785. int encoders_count = 0;
  1786. int ret = 0;
  1787. int copied = 0;
  1788. int i;
  1789. struct drm_mode_modeinfo u_mode;
  1790. struct drm_mode_modeinfo __user *mode_ptr;
  1791. uint32_t __user *encoder_ptr;
  1792. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1793. return -EINVAL;
  1794. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1795. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1796. mutex_lock(&dev->mode_config.mutex);
  1797. connector = drm_connector_find(dev, out_resp->connector_id);
  1798. if (!connector) {
  1799. ret = -ENOENT;
  1800. goto out_unlock;
  1801. }
  1802. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1803. if (connector->encoder_ids[i] != 0)
  1804. encoders_count++;
  1805. if (out_resp->count_modes == 0) {
  1806. connector->funcs->fill_modes(connector,
  1807. dev->mode_config.max_width,
  1808. dev->mode_config.max_height);
  1809. }
  1810. /* delayed so we get modes regardless of pre-fill_modes state */
  1811. list_for_each_entry(mode, &connector->modes, head)
  1812. if (drm_mode_expose_to_userspace(mode, file_priv))
  1813. mode_count++;
  1814. out_resp->connector_id = connector->base.id;
  1815. out_resp->connector_type = connector->connector_type;
  1816. out_resp->connector_type_id = connector->connector_type_id;
  1817. out_resp->mm_width = connector->display_info.width_mm;
  1818. out_resp->mm_height = connector->display_info.height_mm;
  1819. out_resp->subpixel = connector->display_info.subpixel_order;
  1820. out_resp->connection = connector->status;
  1821. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1822. encoder = drm_connector_get_encoder(connector);
  1823. if (encoder)
  1824. out_resp->encoder_id = encoder->base.id;
  1825. else
  1826. out_resp->encoder_id = 0;
  1827. /*
  1828. * This ioctl is called twice, once to determine how much space is
  1829. * needed, and the 2nd time to fill it.
  1830. */
  1831. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1832. copied = 0;
  1833. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1834. list_for_each_entry(mode, &connector->modes, head) {
  1835. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1836. continue;
  1837. drm_mode_convert_to_umode(&u_mode, mode);
  1838. if (copy_to_user(mode_ptr + copied,
  1839. &u_mode, sizeof(u_mode))) {
  1840. ret = -EFAULT;
  1841. goto out;
  1842. }
  1843. copied++;
  1844. }
  1845. }
  1846. out_resp->count_modes = mode_count;
  1847. ret = get_properties(&connector->base, file_priv->atomic,
  1848. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1849. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1850. &out_resp->count_props);
  1851. if (ret)
  1852. goto out;
  1853. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1854. copied = 0;
  1855. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1856. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1857. if (connector->encoder_ids[i] != 0) {
  1858. if (put_user(connector->encoder_ids[i],
  1859. encoder_ptr + copied)) {
  1860. ret = -EFAULT;
  1861. goto out;
  1862. }
  1863. copied++;
  1864. }
  1865. }
  1866. }
  1867. out_resp->count_encoders = encoders_count;
  1868. out:
  1869. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1870. out_unlock:
  1871. mutex_unlock(&dev->mode_config.mutex);
  1872. return ret;
  1873. }
  1874. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  1875. {
  1876. struct drm_connector *connector;
  1877. struct drm_device *dev = encoder->dev;
  1878. bool uses_atomic = false;
  1879. /* For atomic drivers only state objects are synchronously updated and
  1880. * protected by modeset locks, so check those first. */
  1881. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1882. if (!connector->state)
  1883. continue;
  1884. uses_atomic = true;
  1885. if (connector->state->best_encoder != encoder)
  1886. continue;
  1887. return connector->state->crtc;
  1888. }
  1889. /* Don't return stale data (e.g. pending async disable). */
  1890. if (uses_atomic)
  1891. return NULL;
  1892. return encoder->crtc;
  1893. }
  1894. /**
  1895. * drm_mode_getencoder - get encoder configuration
  1896. * @dev: drm device for the ioctl
  1897. * @data: data pointer for the ioctl
  1898. * @file_priv: drm file for the ioctl call
  1899. *
  1900. * Construct a encoder configuration structure to return to the user.
  1901. *
  1902. * Called by the user via ioctl.
  1903. *
  1904. * Returns:
  1905. * Zero on success, negative errno on failure.
  1906. */
  1907. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1908. struct drm_file *file_priv)
  1909. {
  1910. struct drm_mode_get_encoder *enc_resp = data;
  1911. struct drm_encoder *encoder;
  1912. struct drm_crtc *crtc;
  1913. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1914. return -EINVAL;
  1915. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  1916. if (!encoder)
  1917. return -ENOENT;
  1918. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1919. crtc = drm_encoder_get_crtc(encoder);
  1920. if (crtc)
  1921. enc_resp->crtc_id = crtc->base.id;
  1922. else
  1923. enc_resp->crtc_id = 0;
  1924. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1925. enc_resp->encoder_type = encoder->encoder_type;
  1926. enc_resp->encoder_id = encoder->base.id;
  1927. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1928. enc_resp->possible_clones = encoder->possible_clones;
  1929. return 0;
  1930. }
  1931. /**
  1932. * drm_mode_getplane_res - enumerate all plane resources
  1933. * @dev: DRM device
  1934. * @data: ioctl data
  1935. * @file_priv: DRM file info
  1936. *
  1937. * Construct a list of plane ids to return to the user.
  1938. *
  1939. * Called by the user via ioctl.
  1940. *
  1941. * Returns:
  1942. * Zero on success, negative errno on failure.
  1943. */
  1944. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1945. struct drm_file *file_priv)
  1946. {
  1947. struct drm_mode_get_plane_res *plane_resp = data;
  1948. struct drm_mode_config *config;
  1949. struct drm_plane *plane;
  1950. uint32_t __user *plane_ptr;
  1951. int copied = 0;
  1952. unsigned num_planes;
  1953. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1954. return -EINVAL;
  1955. config = &dev->mode_config;
  1956. if (file_priv->universal_planes)
  1957. num_planes = config->num_total_plane;
  1958. else
  1959. num_planes = config->num_overlay_plane;
  1960. /*
  1961. * This ioctl is called twice, once to determine how much space is
  1962. * needed, and the 2nd time to fill it.
  1963. */
  1964. if (num_planes &&
  1965. (plane_resp->count_planes >= num_planes)) {
  1966. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1967. /* Plane lists are invariant, no locking needed. */
  1968. list_for_each_entry(plane, &config->plane_list, head) {
  1969. /*
  1970. * Unless userspace set the 'universal planes'
  1971. * capability bit, only advertise overlays.
  1972. */
  1973. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  1974. !file_priv->universal_planes)
  1975. continue;
  1976. if (put_user(plane->base.id, plane_ptr + copied))
  1977. return -EFAULT;
  1978. copied++;
  1979. }
  1980. }
  1981. plane_resp->count_planes = num_planes;
  1982. return 0;
  1983. }
  1984. /**
  1985. * drm_mode_getplane - get plane configuration
  1986. * @dev: DRM device
  1987. * @data: ioctl data
  1988. * @file_priv: DRM file info
  1989. *
  1990. * Construct a plane configuration structure to return to the user.
  1991. *
  1992. * Called by the user via ioctl.
  1993. *
  1994. * Returns:
  1995. * Zero on success, negative errno on failure.
  1996. */
  1997. int drm_mode_getplane(struct drm_device *dev, void *data,
  1998. struct drm_file *file_priv)
  1999. {
  2000. struct drm_mode_get_plane *plane_resp = data;
  2001. struct drm_plane *plane;
  2002. uint32_t __user *format_ptr;
  2003. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2004. return -EINVAL;
  2005. plane = drm_plane_find(dev, plane_resp->plane_id);
  2006. if (!plane)
  2007. return -ENOENT;
  2008. drm_modeset_lock(&plane->mutex, NULL);
  2009. if (plane->crtc)
  2010. plane_resp->crtc_id = plane->crtc->base.id;
  2011. else
  2012. plane_resp->crtc_id = 0;
  2013. if (plane->fb)
  2014. plane_resp->fb_id = plane->fb->base.id;
  2015. else
  2016. plane_resp->fb_id = 0;
  2017. drm_modeset_unlock(&plane->mutex);
  2018. plane_resp->plane_id = plane->base.id;
  2019. plane_resp->possible_crtcs = plane->possible_crtcs;
  2020. plane_resp->gamma_size = 0;
  2021. /*
  2022. * This ioctl is called twice, once to determine how much space is
  2023. * needed, and the 2nd time to fill it.
  2024. */
  2025. if (plane->format_count &&
  2026. (plane_resp->count_format_types >= plane->format_count)) {
  2027. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2028. if (copy_to_user(format_ptr,
  2029. plane->format_types,
  2030. sizeof(uint32_t) * plane->format_count)) {
  2031. return -EFAULT;
  2032. }
  2033. }
  2034. plane_resp->count_format_types = plane->format_count;
  2035. return 0;
  2036. }
  2037. /**
  2038. * drm_plane_check_pixel_format - Check if the plane supports the pixel format
  2039. * @plane: plane to check for format support
  2040. * @format: the pixel format
  2041. *
  2042. * Returns:
  2043. * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
  2044. * otherwise.
  2045. */
  2046. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  2047. {
  2048. unsigned int i;
  2049. for (i = 0; i < plane->format_count; i++) {
  2050. if (format == plane->format_types[i])
  2051. return 0;
  2052. }
  2053. return -EINVAL;
  2054. }
  2055. /*
  2056. * setplane_internal - setplane handler for internal callers
  2057. *
  2058. * Note that we assume an extra reference has already been taken on fb. If the
  2059. * update fails, this reference will be dropped before return; if it succeeds,
  2060. * the previous framebuffer (if any) will be unreferenced instead.
  2061. *
  2062. * src_{x,y,w,h} are provided in 16.16 fixed point format
  2063. */
  2064. static int __setplane_internal(struct drm_plane *plane,
  2065. struct drm_crtc *crtc,
  2066. struct drm_framebuffer *fb,
  2067. int32_t crtc_x, int32_t crtc_y,
  2068. uint32_t crtc_w, uint32_t crtc_h,
  2069. /* src_{x,y,w,h} values are 16.16 fixed point */
  2070. uint32_t src_x, uint32_t src_y,
  2071. uint32_t src_w, uint32_t src_h)
  2072. {
  2073. int ret = 0;
  2074. unsigned int fb_width, fb_height;
  2075. /* No fb means shut it down */
  2076. if (!fb) {
  2077. plane->old_fb = plane->fb;
  2078. ret = plane->funcs->disable_plane(plane);
  2079. if (!ret) {
  2080. plane->crtc = NULL;
  2081. plane->fb = NULL;
  2082. } else {
  2083. plane->old_fb = NULL;
  2084. }
  2085. goto out;
  2086. }
  2087. /* Check whether this plane is usable on this CRTC */
  2088. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2089. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2090. ret = -EINVAL;
  2091. goto out;
  2092. }
  2093. /* Check whether this plane supports the fb pixel format. */
  2094. ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  2095. if (ret) {
  2096. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2097. drm_get_format_name(fb->pixel_format));
  2098. goto out;
  2099. }
  2100. /* Give drivers some help against integer overflows */
  2101. if (crtc_w > INT_MAX ||
  2102. crtc_x > INT_MAX - (int32_t) crtc_w ||
  2103. crtc_h > INT_MAX ||
  2104. crtc_y > INT_MAX - (int32_t) crtc_h) {
  2105. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2106. crtc_w, crtc_h, crtc_x, crtc_y);
  2107. return -ERANGE;
  2108. }
  2109. fb_width = fb->width << 16;
  2110. fb_height = fb->height << 16;
  2111. /* Make sure source coordinates are inside the fb. */
  2112. if (src_w > fb_width ||
  2113. src_x > fb_width - src_w ||
  2114. src_h > fb_height ||
  2115. src_y > fb_height - src_h) {
  2116. DRM_DEBUG_KMS("Invalid source coordinates "
  2117. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2118. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2119. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2120. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2121. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2122. ret = -ENOSPC;
  2123. goto out;
  2124. }
  2125. plane->old_fb = plane->fb;
  2126. ret = plane->funcs->update_plane(plane, crtc, fb,
  2127. crtc_x, crtc_y, crtc_w, crtc_h,
  2128. src_x, src_y, src_w, src_h);
  2129. if (!ret) {
  2130. plane->crtc = crtc;
  2131. plane->fb = fb;
  2132. fb = NULL;
  2133. } else {
  2134. plane->old_fb = NULL;
  2135. }
  2136. out:
  2137. if (fb)
  2138. drm_framebuffer_unreference(fb);
  2139. if (plane->old_fb)
  2140. drm_framebuffer_unreference(plane->old_fb);
  2141. plane->old_fb = NULL;
  2142. return ret;
  2143. }
  2144. static int setplane_internal(struct drm_plane *plane,
  2145. struct drm_crtc *crtc,
  2146. struct drm_framebuffer *fb,
  2147. int32_t crtc_x, int32_t crtc_y,
  2148. uint32_t crtc_w, uint32_t crtc_h,
  2149. /* src_{x,y,w,h} values are 16.16 fixed point */
  2150. uint32_t src_x, uint32_t src_y,
  2151. uint32_t src_w, uint32_t src_h)
  2152. {
  2153. int ret;
  2154. drm_modeset_lock_all(plane->dev);
  2155. ret = __setplane_internal(plane, crtc, fb,
  2156. crtc_x, crtc_y, crtc_w, crtc_h,
  2157. src_x, src_y, src_w, src_h);
  2158. drm_modeset_unlock_all(plane->dev);
  2159. return ret;
  2160. }
  2161. /**
  2162. * drm_mode_setplane - configure a plane's configuration
  2163. * @dev: DRM device
  2164. * @data: ioctl data*
  2165. * @file_priv: DRM file info
  2166. *
  2167. * Set plane configuration, including placement, fb, scaling, and other factors.
  2168. * Or pass a NULL fb to disable (planes may be disabled without providing a
  2169. * valid crtc).
  2170. *
  2171. * Returns:
  2172. * Zero on success, negative errno on failure.
  2173. */
  2174. int drm_mode_setplane(struct drm_device *dev, void *data,
  2175. struct drm_file *file_priv)
  2176. {
  2177. struct drm_mode_set_plane *plane_req = data;
  2178. struct drm_plane *plane;
  2179. struct drm_crtc *crtc = NULL;
  2180. struct drm_framebuffer *fb = NULL;
  2181. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2182. return -EINVAL;
  2183. /*
  2184. * First, find the plane, crtc, and fb objects. If not available,
  2185. * we don't bother to call the driver.
  2186. */
  2187. plane = drm_plane_find(dev, plane_req->plane_id);
  2188. if (!plane) {
  2189. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2190. plane_req->plane_id);
  2191. return -ENOENT;
  2192. }
  2193. if (plane_req->fb_id) {
  2194. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2195. if (!fb) {
  2196. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2197. plane_req->fb_id);
  2198. return -ENOENT;
  2199. }
  2200. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2201. if (!crtc) {
  2202. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2203. plane_req->crtc_id);
  2204. return -ENOENT;
  2205. }
  2206. }
  2207. /*
  2208. * setplane_internal will take care of deref'ing either the old or new
  2209. * framebuffer depending on success.
  2210. */
  2211. return setplane_internal(plane, crtc, fb,
  2212. plane_req->crtc_x, plane_req->crtc_y,
  2213. plane_req->crtc_w, plane_req->crtc_h,
  2214. plane_req->src_x, plane_req->src_y,
  2215. plane_req->src_w, plane_req->src_h);
  2216. }
  2217. /**
  2218. * drm_mode_set_config_internal - helper to call ->set_config
  2219. * @set: modeset config to set
  2220. *
  2221. * This is a little helper to wrap internal calls to the ->set_config driver
  2222. * interface. The only thing it adds is correct refcounting dance.
  2223. *
  2224. * Returns:
  2225. * Zero on success, negative errno on failure.
  2226. */
  2227. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2228. {
  2229. struct drm_crtc *crtc = set->crtc;
  2230. struct drm_framebuffer *fb;
  2231. struct drm_crtc *tmp;
  2232. int ret;
  2233. /*
  2234. * NOTE: ->set_config can also disable other crtcs (if we steal all
  2235. * connectors from it), hence we need to refcount the fbs across all
  2236. * crtcs. Atomic modeset will have saner semantics ...
  2237. */
  2238. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  2239. tmp->primary->old_fb = tmp->primary->fb;
  2240. fb = set->fb;
  2241. ret = crtc->funcs->set_config(set);
  2242. if (ret == 0) {
  2243. crtc->primary->crtc = crtc;
  2244. crtc->primary->fb = fb;
  2245. }
  2246. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  2247. if (tmp->primary->fb)
  2248. drm_framebuffer_reference(tmp->primary->fb);
  2249. if (tmp->primary->old_fb)
  2250. drm_framebuffer_unreference(tmp->primary->old_fb);
  2251. tmp->primary->old_fb = NULL;
  2252. }
  2253. return ret;
  2254. }
  2255. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2256. /**
  2257. * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2258. * @mode: mode to query
  2259. * @hdisplay: hdisplay value to fill in
  2260. * @vdisplay: vdisplay value to fill in
  2261. *
  2262. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2263. * the appropriate layout.
  2264. */
  2265. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2266. int *hdisplay, int *vdisplay)
  2267. {
  2268. struct drm_display_mode adjusted;
  2269. drm_mode_copy(&adjusted, mode);
  2270. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2271. *hdisplay = adjusted.crtc_hdisplay;
  2272. *vdisplay = adjusted.crtc_vdisplay;
  2273. }
  2274. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2275. /**
  2276. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2277. * CRTC viewport
  2278. * @crtc: CRTC that framebuffer will be displayed on
  2279. * @x: x panning
  2280. * @y: y panning
  2281. * @mode: mode that framebuffer will be displayed under
  2282. * @fb: framebuffer to check size of
  2283. */
  2284. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2285. int x, int y,
  2286. const struct drm_display_mode *mode,
  2287. const struct drm_framebuffer *fb)
  2288. {
  2289. int hdisplay, vdisplay;
  2290. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2291. if (crtc->invert_dimensions)
  2292. swap(hdisplay, vdisplay);
  2293. if (hdisplay > fb->width ||
  2294. vdisplay > fb->height ||
  2295. x > fb->width - hdisplay ||
  2296. y > fb->height - vdisplay) {
  2297. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  2298. fb->width, fb->height, hdisplay, vdisplay, x, y,
  2299. crtc->invert_dimensions ? " (inverted)" : "");
  2300. return -ENOSPC;
  2301. }
  2302. return 0;
  2303. }
  2304. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2305. /**
  2306. * drm_mode_setcrtc - set CRTC configuration
  2307. * @dev: drm device for the ioctl
  2308. * @data: data pointer for the ioctl
  2309. * @file_priv: drm file for the ioctl call
  2310. *
  2311. * Build a new CRTC configuration based on user request.
  2312. *
  2313. * Called by the user via ioctl.
  2314. *
  2315. * Returns:
  2316. * Zero on success, negative errno on failure.
  2317. */
  2318. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2319. struct drm_file *file_priv)
  2320. {
  2321. struct drm_mode_config *config = &dev->mode_config;
  2322. struct drm_mode_crtc *crtc_req = data;
  2323. struct drm_crtc *crtc;
  2324. struct drm_connector **connector_set = NULL, *connector;
  2325. struct drm_framebuffer *fb = NULL;
  2326. struct drm_display_mode *mode = NULL;
  2327. struct drm_mode_set set;
  2328. uint32_t __user *set_connectors_ptr;
  2329. int ret;
  2330. int i;
  2331. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2332. return -EINVAL;
  2333. /* For some reason crtc x/y offsets are signed internally. */
  2334. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  2335. return -ERANGE;
  2336. drm_modeset_lock_all(dev);
  2337. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2338. if (!crtc) {
  2339. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2340. ret = -ENOENT;
  2341. goto out;
  2342. }
  2343. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2344. if (crtc_req->mode_valid) {
  2345. /* If we have a mode we need a framebuffer. */
  2346. /* If we pass -1, set the mode with the currently bound fb */
  2347. if (crtc_req->fb_id == -1) {
  2348. if (!crtc->primary->fb) {
  2349. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2350. ret = -EINVAL;
  2351. goto out;
  2352. }
  2353. fb = crtc->primary->fb;
  2354. /* Make refcounting symmetric with the lookup path. */
  2355. drm_framebuffer_reference(fb);
  2356. } else {
  2357. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2358. if (!fb) {
  2359. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2360. crtc_req->fb_id);
  2361. ret = -ENOENT;
  2362. goto out;
  2363. }
  2364. }
  2365. mode = drm_mode_create(dev);
  2366. if (!mode) {
  2367. ret = -ENOMEM;
  2368. goto out;
  2369. }
  2370. ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2371. if (ret) {
  2372. DRM_DEBUG_KMS("Invalid mode\n");
  2373. goto out;
  2374. }
  2375. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2376. /*
  2377. * Check whether the primary plane supports the fb pixel format.
  2378. * Drivers not implementing the universal planes API use a
  2379. * default formats list provided by the DRM core which doesn't
  2380. * match real hardware capabilities. Skip the check in that
  2381. * case.
  2382. */
  2383. if (!crtc->primary->format_default) {
  2384. ret = drm_plane_check_pixel_format(crtc->primary,
  2385. fb->pixel_format);
  2386. if (ret) {
  2387. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2388. drm_get_format_name(fb->pixel_format));
  2389. goto out;
  2390. }
  2391. }
  2392. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2393. mode, fb);
  2394. if (ret)
  2395. goto out;
  2396. }
  2397. if (crtc_req->count_connectors == 0 && mode) {
  2398. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2399. ret = -EINVAL;
  2400. goto out;
  2401. }
  2402. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2403. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2404. crtc_req->count_connectors);
  2405. ret = -EINVAL;
  2406. goto out;
  2407. }
  2408. if (crtc_req->count_connectors > 0) {
  2409. u32 out_id;
  2410. /* Avoid unbounded kernel memory allocation */
  2411. if (crtc_req->count_connectors > config->num_connector) {
  2412. ret = -EINVAL;
  2413. goto out;
  2414. }
  2415. connector_set = kmalloc_array(crtc_req->count_connectors,
  2416. sizeof(struct drm_connector *),
  2417. GFP_KERNEL);
  2418. if (!connector_set) {
  2419. ret = -ENOMEM;
  2420. goto out;
  2421. }
  2422. for (i = 0; i < crtc_req->count_connectors; i++) {
  2423. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2424. if (get_user(out_id, &set_connectors_ptr[i])) {
  2425. ret = -EFAULT;
  2426. goto out;
  2427. }
  2428. connector = drm_connector_find(dev, out_id);
  2429. if (!connector) {
  2430. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2431. out_id);
  2432. ret = -ENOENT;
  2433. goto out;
  2434. }
  2435. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2436. connector->base.id,
  2437. connector->name);
  2438. connector_set[i] = connector;
  2439. }
  2440. }
  2441. set.crtc = crtc;
  2442. set.x = crtc_req->x;
  2443. set.y = crtc_req->y;
  2444. set.mode = mode;
  2445. set.connectors = connector_set;
  2446. set.num_connectors = crtc_req->count_connectors;
  2447. set.fb = fb;
  2448. ret = drm_mode_set_config_internal(&set);
  2449. out:
  2450. if (fb)
  2451. drm_framebuffer_unreference(fb);
  2452. kfree(connector_set);
  2453. drm_mode_destroy(dev, mode);
  2454. drm_modeset_unlock_all(dev);
  2455. return ret;
  2456. }
  2457. /**
  2458. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2459. * universal plane handler call
  2460. * @crtc: crtc to update cursor for
  2461. * @req: data pointer for the ioctl
  2462. * @file_priv: drm file for the ioctl call
  2463. *
  2464. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2465. * translate legacy ioctl calls into universal plane handler calls, we need to
  2466. * wrap the native buffer handle in a drm_framebuffer.
  2467. *
  2468. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2469. * buffer with a pitch of 4*width; the universal plane interface should be used
  2470. * directly in cases where the hardware can support other buffer settings and
  2471. * userspace wants to make use of these capabilities.
  2472. *
  2473. * Returns:
  2474. * Zero on success, negative errno on failure.
  2475. */
  2476. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2477. struct drm_mode_cursor2 *req,
  2478. struct drm_file *file_priv)
  2479. {
  2480. struct drm_device *dev = crtc->dev;
  2481. struct drm_framebuffer *fb = NULL;
  2482. struct drm_mode_fb_cmd2 fbreq = {
  2483. .width = req->width,
  2484. .height = req->height,
  2485. .pixel_format = DRM_FORMAT_ARGB8888,
  2486. .pitches = { req->width * 4 },
  2487. .handles = { req->handle },
  2488. };
  2489. int32_t crtc_x, crtc_y;
  2490. uint32_t crtc_w = 0, crtc_h = 0;
  2491. uint32_t src_w = 0, src_h = 0;
  2492. int ret = 0;
  2493. BUG_ON(!crtc->cursor);
  2494. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2495. /*
  2496. * Obtain fb we'll be using (either new or existing) and take an extra
  2497. * reference to it if fb != null. setplane will take care of dropping
  2498. * the reference if the plane update fails.
  2499. */
  2500. if (req->flags & DRM_MODE_CURSOR_BO) {
  2501. if (req->handle) {
  2502. fb = internal_framebuffer_create(dev, &fbreq, file_priv);
  2503. if (IS_ERR(fb)) {
  2504. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2505. return PTR_ERR(fb);
  2506. }
  2507. } else {
  2508. fb = NULL;
  2509. }
  2510. } else {
  2511. fb = crtc->cursor->fb;
  2512. if (fb)
  2513. drm_framebuffer_reference(fb);
  2514. }
  2515. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2516. crtc_x = req->x;
  2517. crtc_y = req->y;
  2518. } else {
  2519. crtc_x = crtc->cursor_x;
  2520. crtc_y = crtc->cursor_y;
  2521. }
  2522. if (fb) {
  2523. crtc_w = fb->width;
  2524. crtc_h = fb->height;
  2525. src_w = fb->width << 16;
  2526. src_h = fb->height << 16;
  2527. }
  2528. /*
  2529. * setplane_internal will take care of deref'ing either the old or new
  2530. * framebuffer depending on success.
  2531. */
  2532. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2533. crtc_x, crtc_y, crtc_w, crtc_h,
  2534. 0, 0, src_w, src_h);
  2535. /* Update successful; save new cursor position, if necessary */
  2536. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2537. crtc->cursor_x = req->x;
  2538. crtc->cursor_y = req->y;
  2539. }
  2540. return ret;
  2541. }
  2542. static int drm_mode_cursor_common(struct drm_device *dev,
  2543. struct drm_mode_cursor2 *req,
  2544. struct drm_file *file_priv)
  2545. {
  2546. struct drm_crtc *crtc;
  2547. int ret = 0;
  2548. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2549. return -EINVAL;
  2550. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2551. return -EINVAL;
  2552. crtc = drm_crtc_find(dev, req->crtc_id);
  2553. if (!crtc) {
  2554. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2555. return -ENOENT;
  2556. }
  2557. /*
  2558. * If this crtc has a universal cursor plane, call that plane's update
  2559. * handler rather than using legacy cursor handlers.
  2560. */
  2561. drm_modeset_lock_crtc(crtc, crtc->cursor);
  2562. if (crtc->cursor) {
  2563. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2564. goto out;
  2565. }
  2566. if (req->flags & DRM_MODE_CURSOR_BO) {
  2567. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2568. ret = -ENXIO;
  2569. goto out;
  2570. }
  2571. /* Turns off the cursor if handle is 0 */
  2572. if (crtc->funcs->cursor_set2)
  2573. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2574. req->width, req->height, req->hot_x, req->hot_y);
  2575. else
  2576. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2577. req->width, req->height);
  2578. }
  2579. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2580. if (crtc->funcs->cursor_move) {
  2581. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2582. } else {
  2583. ret = -EFAULT;
  2584. goto out;
  2585. }
  2586. }
  2587. out:
  2588. drm_modeset_unlock_crtc(crtc);
  2589. return ret;
  2590. }
  2591. /**
  2592. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2593. * @dev: drm device for the ioctl
  2594. * @data: data pointer for the ioctl
  2595. * @file_priv: drm file for the ioctl call
  2596. *
  2597. * Set the cursor configuration based on user request.
  2598. *
  2599. * Called by the user via ioctl.
  2600. *
  2601. * Returns:
  2602. * Zero on success, negative errno on failure.
  2603. */
  2604. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2605. void *data, struct drm_file *file_priv)
  2606. {
  2607. struct drm_mode_cursor *req = data;
  2608. struct drm_mode_cursor2 new_req;
  2609. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2610. new_req.hot_x = new_req.hot_y = 0;
  2611. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2612. }
  2613. /**
  2614. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2615. * @dev: drm device for the ioctl
  2616. * @data: data pointer for the ioctl
  2617. * @file_priv: drm file for the ioctl call
  2618. *
  2619. * Set the cursor configuration based on user request. This implements the 2nd
  2620. * version of the cursor ioctl, which allows userspace to additionally specify
  2621. * the hotspot of the pointer.
  2622. *
  2623. * Called by the user via ioctl.
  2624. *
  2625. * Returns:
  2626. * Zero on success, negative errno on failure.
  2627. */
  2628. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2629. void *data, struct drm_file *file_priv)
  2630. {
  2631. struct drm_mode_cursor2 *req = data;
  2632. return drm_mode_cursor_common(dev, req, file_priv);
  2633. }
  2634. /**
  2635. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2636. * @bpp: bits per pixels
  2637. * @depth: bit depth per pixel
  2638. *
  2639. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2640. * Useful in fbdev emulation code, since that deals in those values.
  2641. */
  2642. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2643. {
  2644. uint32_t fmt;
  2645. switch (bpp) {
  2646. case 8:
  2647. fmt = DRM_FORMAT_C8;
  2648. break;
  2649. case 16:
  2650. if (depth == 15)
  2651. fmt = DRM_FORMAT_XRGB1555;
  2652. else
  2653. fmt = DRM_FORMAT_RGB565;
  2654. break;
  2655. case 24:
  2656. fmt = DRM_FORMAT_RGB888;
  2657. break;
  2658. case 32:
  2659. if (depth == 24)
  2660. fmt = DRM_FORMAT_XRGB8888;
  2661. else if (depth == 30)
  2662. fmt = DRM_FORMAT_XRGB2101010;
  2663. else
  2664. fmt = DRM_FORMAT_ARGB8888;
  2665. break;
  2666. default:
  2667. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2668. fmt = DRM_FORMAT_XRGB8888;
  2669. break;
  2670. }
  2671. return fmt;
  2672. }
  2673. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2674. /**
  2675. * drm_mode_addfb - add an FB to the graphics configuration
  2676. * @dev: drm device for the ioctl
  2677. * @data: data pointer for the ioctl
  2678. * @file_priv: drm file for the ioctl call
  2679. *
  2680. * Add a new FB to the specified CRTC, given a user request. This is the
  2681. * original addfb ioctl which only supported RGB formats.
  2682. *
  2683. * Called by the user via ioctl.
  2684. *
  2685. * Returns:
  2686. * Zero on success, negative errno on failure.
  2687. */
  2688. int drm_mode_addfb(struct drm_device *dev,
  2689. void *data, struct drm_file *file_priv)
  2690. {
  2691. struct drm_mode_fb_cmd *or = data;
  2692. struct drm_mode_fb_cmd2 r = {};
  2693. int ret;
  2694. /* convert to new format and call new ioctl */
  2695. r.fb_id = or->fb_id;
  2696. r.width = or->width;
  2697. r.height = or->height;
  2698. r.pitches[0] = or->pitch;
  2699. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2700. r.handles[0] = or->handle;
  2701. ret = drm_mode_addfb2(dev, &r, file_priv);
  2702. if (ret)
  2703. return ret;
  2704. or->fb_id = r.fb_id;
  2705. return 0;
  2706. }
  2707. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2708. {
  2709. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2710. switch (format) {
  2711. case DRM_FORMAT_C8:
  2712. case DRM_FORMAT_RGB332:
  2713. case DRM_FORMAT_BGR233:
  2714. case DRM_FORMAT_XRGB4444:
  2715. case DRM_FORMAT_XBGR4444:
  2716. case DRM_FORMAT_RGBX4444:
  2717. case DRM_FORMAT_BGRX4444:
  2718. case DRM_FORMAT_ARGB4444:
  2719. case DRM_FORMAT_ABGR4444:
  2720. case DRM_FORMAT_RGBA4444:
  2721. case DRM_FORMAT_BGRA4444:
  2722. case DRM_FORMAT_XRGB1555:
  2723. case DRM_FORMAT_XBGR1555:
  2724. case DRM_FORMAT_RGBX5551:
  2725. case DRM_FORMAT_BGRX5551:
  2726. case DRM_FORMAT_ARGB1555:
  2727. case DRM_FORMAT_ABGR1555:
  2728. case DRM_FORMAT_RGBA5551:
  2729. case DRM_FORMAT_BGRA5551:
  2730. case DRM_FORMAT_RGB565:
  2731. case DRM_FORMAT_BGR565:
  2732. case DRM_FORMAT_RGB888:
  2733. case DRM_FORMAT_BGR888:
  2734. case DRM_FORMAT_XRGB8888:
  2735. case DRM_FORMAT_XBGR8888:
  2736. case DRM_FORMAT_RGBX8888:
  2737. case DRM_FORMAT_BGRX8888:
  2738. case DRM_FORMAT_ARGB8888:
  2739. case DRM_FORMAT_ABGR8888:
  2740. case DRM_FORMAT_RGBA8888:
  2741. case DRM_FORMAT_BGRA8888:
  2742. case DRM_FORMAT_XRGB2101010:
  2743. case DRM_FORMAT_XBGR2101010:
  2744. case DRM_FORMAT_RGBX1010102:
  2745. case DRM_FORMAT_BGRX1010102:
  2746. case DRM_FORMAT_ARGB2101010:
  2747. case DRM_FORMAT_ABGR2101010:
  2748. case DRM_FORMAT_RGBA1010102:
  2749. case DRM_FORMAT_BGRA1010102:
  2750. case DRM_FORMAT_YUYV:
  2751. case DRM_FORMAT_YVYU:
  2752. case DRM_FORMAT_UYVY:
  2753. case DRM_FORMAT_VYUY:
  2754. case DRM_FORMAT_AYUV:
  2755. case DRM_FORMAT_NV12:
  2756. case DRM_FORMAT_NV21:
  2757. case DRM_FORMAT_NV16:
  2758. case DRM_FORMAT_NV61:
  2759. case DRM_FORMAT_NV24:
  2760. case DRM_FORMAT_NV42:
  2761. case DRM_FORMAT_YUV410:
  2762. case DRM_FORMAT_YVU410:
  2763. case DRM_FORMAT_YUV411:
  2764. case DRM_FORMAT_YVU411:
  2765. case DRM_FORMAT_YUV420:
  2766. case DRM_FORMAT_YVU420:
  2767. case DRM_FORMAT_YUV422:
  2768. case DRM_FORMAT_YVU422:
  2769. case DRM_FORMAT_YUV444:
  2770. case DRM_FORMAT_YVU444:
  2771. return 0;
  2772. default:
  2773. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2774. drm_get_format_name(r->pixel_format));
  2775. return -EINVAL;
  2776. }
  2777. }
  2778. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2779. {
  2780. int ret, hsub, vsub, num_planes, i;
  2781. ret = format_check(r);
  2782. if (ret) {
  2783. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2784. drm_get_format_name(r->pixel_format));
  2785. return ret;
  2786. }
  2787. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2788. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2789. num_planes = drm_format_num_planes(r->pixel_format);
  2790. if (r->width == 0 || r->width % hsub) {
  2791. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2792. return -EINVAL;
  2793. }
  2794. if (r->height == 0 || r->height % vsub) {
  2795. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2796. return -EINVAL;
  2797. }
  2798. for (i = 0; i < num_planes; i++) {
  2799. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2800. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2801. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2802. if (!r->handles[i]) {
  2803. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2804. return -EINVAL;
  2805. }
  2806. if ((uint64_t) width * cpp > UINT_MAX)
  2807. return -ERANGE;
  2808. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2809. return -ERANGE;
  2810. if (r->pitches[i] < width * cpp) {
  2811. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2812. return -EINVAL;
  2813. }
  2814. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  2815. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  2816. r->modifier[i], i);
  2817. return -EINVAL;
  2818. }
  2819. }
  2820. return 0;
  2821. }
  2822. static struct drm_framebuffer *
  2823. internal_framebuffer_create(struct drm_device *dev,
  2824. struct drm_mode_fb_cmd2 *r,
  2825. struct drm_file *file_priv)
  2826. {
  2827. struct drm_mode_config *config = &dev->mode_config;
  2828. struct drm_framebuffer *fb;
  2829. int ret;
  2830. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  2831. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2832. return ERR_PTR(-EINVAL);
  2833. }
  2834. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2835. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2836. r->width, config->min_width, config->max_width);
  2837. return ERR_PTR(-EINVAL);
  2838. }
  2839. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2840. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2841. r->height, config->min_height, config->max_height);
  2842. return ERR_PTR(-EINVAL);
  2843. }
  2844. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  2845. !dev->mode_config.allow_fb_modifiers) {
  2846. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  2847. return ERR_PTR(-EINVAL);
  2848. }
  2849. ret = framebuffer_check(r);
  2850. if (ret)
  2851. return ERR_PTR(ret);
  2852. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2853. if (IS_ERR(fb)) {
  2854. DRM_DEBUG_KMS("could not create framebuffer\n");
  2855. return fb;
  2856. }
  2857. return fb;
  2858. }
  2859. /**
  2860. * drm_mode_addfb2 - add an FB to the graphics configuration
  2861. * @dev: drm device for the ioctl
  2862. * @data: data pointer for the ioctl
  2863. * @file_priv: drm file for the ioctl call
  2864. *
  2865. * Add a new FB to the specified CRTC, given a user request with format. This is
  2866. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2867. * and uses fourcc codes as pixel format specifiers.
  2868. *
  2869. * Called by the user via ioctl.
  2870. *
  2871. * Returns:
  2872. * Zero on success, negative errno on failure.
  2873. */
  2874. int drm_mode_addfb2(struct drm_device *dev,
  2875. void *data, struct drm_file *file_priv)
  2876. {
  2877. struct drm_mode_fb_cmd2 *r = data;
  2878. struct drm_framebuffer *fb;
  2879. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2880. return -EINVAL;
  2881. fb = internal_framebuffer_create(dev, r, file_priv);
  2882. if (IS_ERR(fb))
  2883. return PTR_ERR(fb);
  2884. /* Transfer ownership to the filp for reaping on close */
  2885. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2886. mutex_lock(&file_priv->fbs_lock);
  2887. r->fb_id = fb->base.id;
  2888. list_add(&fb->filp_head, &file_priv->fbs);
  2889. mutex_unlock(&file_priv->fbs_lock);
  2890. return 0;
  2891. }
  2892. /**
  2893. * drm_mode_rmfb - remove an FB from the configuration
  2894. * @dev: drm device for the ioctl
  2895. * @data: data pointer for the ioctl
  2896. * @file_priv: drm file for the ioctl call
  2897. *
  2898. * Remove the FB specified by the user.
  2899. *
  2900. * Called by the user via ioctl.
  2901. *
  2902. * Returns:
  2903. * Zero on success, negative errno on failure.
  2904. */
  2905. int drm_mode_rmfb(struct drm_device *dev,
  2906. void *data, struct drm_file *file_priv)
  2907. {
  2908. struct drm_framebuffer *fb = NULL;
  2909. struct drm_framebuffer *fbl = NULL;
  2910. uint32_t *id = data;
  2911. int found = 0;
  2912. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2913. return -EINVAL;
  2914. mutex_lock(&file_priv->fbs_lock);
  2915. mutex_lock(&dev->mode_config.fb_lock);
  2916. fb = __drm_framebuffer_lookup(dev, *id);
  2917. if (!fb)
  2918. goto fail_lookup;
  2919. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2920. if (fb == fbl)
  2921. found = 1;
  2922. if (!found)
  2923. goto fail_lookup;
  2924. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2925. __drm_framebuffer_unregister(dev, fb);
  2926. list_del_init(&fb->filp_head);
  2927. mutex_unlock(&dev->mode_config.fb_lock);
  2928. mutex_unlock(&file_priv->fbs_lock);
  2929. drm_framebuffer_remove(fb);
  2930. return 0;
  2931. fail_lookup:
  2932. mutex_unlock(&dev->mode_config.fb_lock);
  2933. mutex_unlock(&file_priv->fbs_lock);
  2934. return -ENOENT;
  2935. }
  2936. /**
  2937. * drm_mode_getfb - get FB info
  2938. * @dev: drm device for the ioctl
  2939. * @data: data pointer for the ioctl
  2940. * @file_priv: drm file for the ioctl call
  2941. *
  2942. * Lookup the FB given its ID and return info about it.
  2943. *
  2944. * Called by the user via ioctl.
  2945. *
  2946. * Returns:
  2947. * Zero on success, negative errno on failure.
  2948. */
  2949. int drm_mode_getfb(struct drm_device *dev,
  2950. void *data, struct drm_file *file_priv)
  2951. {
  2952. struct drm_mode_fb_cmd *r = data;
  2953. struct drm_framebuffer *fb;
  2954. int ret;
  2955. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2956. return -EINVAL;
  2957. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2958. if (!fb)
  2959. return -ENOENT;
  2960. r->height = fb->height;
  2961. r->width = fb->width;
  2962. r->depth = fb->depth;
  2963. r->bpp = fb->bits_per_pixel;
  2964. r->pitch = fb->pitches[0];
  2965. if (fb->funcs->create_handle) {
  2966. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  2967. drm_is_control_client(file_priv)) {
  2968. ret = fb->funcs->create_handle(fb, file_priv,
  2969. &r->handle);
  2970. } else {
  2971. /* GET_FB() is an unprivileged ioctl so we must not
  2972. * return a buffer-handle to non-master processes! For
  2973. * backwards-compatibility reasons, we cannot make
  2974. * GET_FB() privileged, so just return an invalid handle
  2975. * for non-masters. */
  2976. r->handle = 0;
  2977. ret = 0;
  2978. }
  2979. } else {
  2980. ret = -ENODEV;
  2981. }
  2982. drm_framebuffer_unreference(fb);
  2983. return ret;
  2984. }
  2985. /**
  2986. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  2987. * @dev: drm device for the ioctl
  2988. * @data: data pointer for the ioctl
  2989. * @file_priv: drm file for the ioctl call
  2990. *
  2991. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  2992. * rectangle list. Generic userspace which does frontbuffer rendering must call
  2993. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  2994. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  2995. *
  2996. * Modesetting drivers which always update the frontbuffer do not need to
  2997. * implement the corresponding ->dirty framebuffer callback.
  2998. *
  2999. * Called by the user via ioctl.
  3000. *
  3001. * Returns:
  3002. * Zero on success, negative errno on failure.
  3003. */
  3004. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3005. void *data, struct drm_file *file_priv)
  3006. {
  3007. struct drm_clip_rect __user *clips_ptr;
  3008. struct drm_clip_rect *clips = NULL;
  3009. struct drm_mode_fb_dirty_cmd *r = data;
  3010. struct drm_framebuffer *fb;
  3011. unsigned flags;
  3012. int num_clips;
  3013. int ret;
  3014. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3015. return -EINVAL;
  3016. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3017. if (!fb)
  3018. return -ENOENT;
  3019. num_clips = r->num_clips;
  3020. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3021. if (!num_clips != !clips_ptr) {
  3022. ret = -EINVAL;
  3023. goto out_err1;
  3024. }
  3025. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3026. /* If userspace annotates copy, clips must come in pairs */
  3027. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3028. ret = -EINVAL;
  3029. goto out_err1;
  3030. }
  3031. if (num_clips && clips_ptr) {
  3032. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3033. ret = -EINVAL;
  3034. goto out_err1;
  3035. }
  3036. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3037. if (!clips) {
  3038. ret = -ENOMEM;
  3039. goto out_err1;
  3040. }
  3041. ret = copy_from_user(clips, clips_ptr,
  3042. num_clips * sizeof(*clips));
  3043. if (ret) {
  3044. ret = -EFAULT;
  3045. goto out_err2;
  3046. }
  3047. }
  3048. if (fb->funcs->dirty) {
  3049. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3050. clips, num_clips);
  3051. } else {
  3052. ret = -ENOSYS;
  3053. }
  3054. out_err2:
  3055. kfree(clips);
  3056. out_err1:
  3057. drm_framebuffer_unreference(fb);
  3058. return ret;
  3059. }
  3060. /**
  3061. * drm_fb_release - remove and free the FBs on this file
  3062. * @priv: drm file for the ioctl
  3063. *
  3064. * Destroy all the FBs associated with @filp.
  3065. *
  3066. * Called by the user via ioctl.
  3067. *
  3068. * Returns:
  3069. * Zero on success, negative errno on failure.
  3070. */
  3071. void drm_fb_release(struct drm_file *priv)
  3072. {
  3073. struct drm_device *dev = priv->minor->dev;
  3074. struct drm_framebuffer *fb, *tfb;
  3075. /*
  3076. * When the file gets released that means no one else can access the fb
  3077. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3078. * avoid upsetting lockdep since the universal cursor code adds a
  3079. * framebuffer while holding mutex locks.
  3080. *
  3081. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3082. * locks is impossible here since no one else but this function can get
  3083. * at it any more.
  3084. */
  3085. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3086. mutex_lock(&dev->mode_config.fb_lock);
  3087. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  3088. __drm_framebuffer_unregister(dev, fb);
  3089. mutex_unlock(&dev->mode_config.fb_lock);
  3090. list_del_init(&fb->filp_head);
  3091. /* This will also drop the fpriv->fbs reference. */
  3092. drm_framebuffer_remove(fb);
  3093. }
  3094. }
  3095. /**
  3096. * drm_property_create - create a new property type
  3097. * @dev: drm device
  3098. * @flags: flags specifying the property type
  3099. * @name: name of the property
  3100. * @num_values: number of pre-defined values
  3101. *
  3102. * This creates a new generic drm property which can then be attached to a drm
  3103. * object with drm_object_attach_property. The returned property object must be
  3104. * freed with drm_property_destroy.
  3105. *
  3106. * Note that the DRM core keeps a per-device list of properties and that, if
  3107. * drm_mode_config_cleanup() is called, it will destroy all properties created
  3108. * by the driver.
  3109. *
  3110. * Returns:
  3111. * A pointer to the newly created property on success, NULL on failure.
  3112. */
  3113. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3114. const char *name, int num_values)
  3115. {
  3116. struct drm_property *property = NULL;
  3117. int ret;
  3118. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3119. if (!property)
  3120. return NULL;
  3121. property->dev = dev;
  3122. if (num_values) {
  3123. property->values = kcalloc(num_values, sizeof(uint64_t),
  3124. GFP_KERNEL);
  3125. if (!property->values)
  3126. goto fail;
  3127. }
  3128. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3129. if (ret)
  3130. goto fail;
  3131. property->flags = flags;
  3132. property->num_values = num_values;
  3133. INIT_LIST_HEAD(&property->enum_list);
  3134. if (name) {
  3135. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3136. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3137. }
  3138. list_add_tail(&property->head, &dev->mode_config.property_list);
  3139. WARN_ON(!drm_property_type_valid(property));
  3140. return property;
  3141. fail:
  3142. kfree(property->values);
  3143. kfree(property);
  3144. return NULL;
  3145. }
  3146. EXPORT_SYMBOL(drm_property_create);
  3147. /**
  3148. * drm_property_create_enum - create a new enumeration property type
  3149. * @dev: drm device
  3150. * @flags: flags specifying the property type
  3151. * @name: name of the property
  3152. * @props: enumeration lists with property values
  3153. * @num_values: number of pre-defined values
  3154. *
  3155. * This creates a new generic drm property which can then be attached to a drm
  3156. * object with drm_object_attach_property. The returned property object must be
  3157. * freed with drm_property_destroy.
  3158. *
  3159. * Userspace is only allowed to set one of the predefined values for enumeration
  3160. * properties.
  3161. *
  3162. * Returns:
  3163. * A pointer to the newly created property on success, NULL on failure.
  3164. */
  3165. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3166. const char *name,
  3167. const struct drm_prop_enum_list *props,
  3168. int num_values)
  3169. {
  3170. struct drm_property *property;
  3171. int i, ret;
  3172. flags |= DRM_MODE_PROP_ENUM;
  3173. property = drm_property_create(dev, flags, name, num_values);
  3174. if (!property)
  3175. return NULL;
  3176. for (i = 0; i < num_values; i++) {
  3177. ret = drm_property_add_enum(property, i,
  3178. props[i].type,
  3179. props[i].name);
  3180. if (ret) {
  3181. drm_property_destroy(dev, property);
  3182. return NULL;
  3183. }
  3184. }
  3185. return property;
  3186. }
  3187. EXPORT_SYMBOL(drm_property_create_enum);
  3188. /**
  3189. * drm_property_create_bitmask - create a new bitmask property type
  3190. * @dev: drm device
  3191. * @flags: flags specifying the property type
  3192. * @name: name of the property
  3193. * @props: enumeration lists with property bitflags
  3194. * @num_props: size of the @props array
  3195. * @supported_bits: bitmask of all supported enumeration values
  3196. *
  3197. * This creates a new bitmask drm property which can then be attached to a drm
  3198. * object with drm_object_attach_property. The returned property object must be
  3199. * freed with drm_property_destroy.
  3200. *
  3201. * Compared to plain enumeration properties userspace is allowed to set any
  3202. * or'ed together combination of the predefined property bitflag values
  3203. *
  3204. * Returns:
  3205. * A pointer to the newly created property on success, NULL on failure.
  3206. */
  3207. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3208. int flags, const char *name,
  3209. const struct drm_prop_enum_list *props,
  3210. int num_props,
  3211. uint64_t supported_bits)
  3212. {
  3213. struct drm_property *property;
  3214. int i, ret, index = 0;
  3215. int num_values = hweight64(supported_bits);
  3216. flags |= DRM_MODE_PROP_BITMASK;
  3217. property = drm_property_create(dev, flags, name, num_values);
  3218. if (!property)
  3219. return NULL;
  3220. for (i = 0; i < num_props; i++) {
  3221. if (!(supported_bits & (1ULL << props[i].type)))
  3222. continue;
  3223. if (WARN_ON(index >= num_values)) {
  3224. drm_property_destroy(dev, property);
  3225. return NULL;
  3226. }
  3227. ret = drm_property_add_enum(property, index++,
  3228. props[i].type,
  3229. props[i].name);
  3230. if (ret) {
  3231. drm_property_destroy(dev, property);
  3232. return NULL;
  3233. }
  3234. }
  3235. return property;
  3236. }
  3237. EXPORT_SYMBOL(drm_property_create_bitmask);
  3238. static struct drm_property *property_create_range(struct drm_device *dev,
  3239. int flags, const char *name,
  3240. uint64_t min, uint64_t max)
  3241. {
  3242. struct drm_property *property;
  3243. property = drm_property_create(dev, flags, name, 2);
  3244. if (!property)
  3245. return NULL;
  3246. property->values[0] = min;
  3247. property->values[1] = max;
  3248. return property;
  3249. }
  3250. /**
  3251. * drm_property_create_range - create a new unsigned ranged property type
  3252. * @dev: drm device
  3253. * @flags: flags specifying the property type
  3254. * @name: name of the property
  3255. * @min: minimum value of the property
  3256. * @max: maximum value of the property
  3257. *
  3258. * This creates a new generic drm property which can then be attached to a drm
  3259. * object with drm_object_attach_property. The returned property object must be
  3260. * freed with drm_property_destroy.
  3261. *
  3262. * Userspace is allowed to set any unsigned integer value in the (min, max)
  3263. * range inclusive.
  3264. *
  3265. * Returns:
  3266. * A pointer to the newly created property on success, NULL on failure.
  3267. */
  3268. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3269. const char *name,
  3270. uint64_t min, uint64_t max)
  3271. {
  3272. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3273. name, min, max);
  3274. }
  3275. EXPORT_SYMBOL(drm_property_create_range);
  3276. /**
  3277. * drm_property_create_signed_range - create a new signed ranged property type
  3278. * @dev: drm device
  3279. * @flags: flags specifying the property type
  3280. * @name: name of the property
  3281. * @min: minimum value of the property
  3282. * @max: maximum value of the property
  3283. *
  3284. * This creates a new generic drm property which can then be attached to a drm
  3285. * object with drm_object_attach_property. The returned property object must be
  3286. * freed with drm_property_destroy.
  3287. *
  3288. * Userspace is allowed to set any signed integer value in the (min, max)
  3289. * range inclusive.
  3290. *
  3291. * Returns:
  3292. * A pointer to the newly created property on success, NULL on failure.
  3293. */
  3294. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3295. int flags, const char *name,
  3296. int64_t min, int64_t max)
  3297. {
  3298. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3299. name, I642U64(min), I642U64(max));
  3300. }
  3301. EXPORT_SYMBOL(drm_property_create_signed_range);
  3302. /**
  3303. * drm_property_create_object - create a new object property type
  3304. * @dev: drm device
  3305. * @flags: flags specifying the property type
  3306. * @name: name of the property
  3307. * @type: object type from DRM_MODE_OBJECT_* defines
  3308. *
  3309. * This creates a new generic drm property which can then be attached to a drm
  3310. * object with drm_object_attach_property. The returned property object must be
  3311. * freed with drm_property_destroy.
  3312. *
  3313. * Userspace is only allowed to set this to any property value of the given
  3314. * @type. Only useful for atomic properties, which is enforced.
  3315. *
  3316. * Returns:
  3317. * A pointer to the newly created property on success, NULL on failure.
  3318. */
  3319. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3320. int flags, const char *name, uint32_t type)
  3321. {
  3322. struct drm_property *property;
  3323. flags |= DRM_MODE_PROP_OBJECT;
  3324. if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3325. return NULL;
  3326. property = drm_property_create(dev, flags, name, 1);
  3327. if (!property)
  3328. return NULL;
  3329. property->values[0] = type;
  3330. return property;
  3331. }
  3332. EXPORT_SYMBOL(drm_property_create_object);
  3333. /**
  3334. * drm_property_create_bool - create a new boolean property type
  3335. * @dev: drm device
  3336. * @flags: flags specifying the property type
  3337. * @name: name of the property
  3338. *
  3339. * This creates a new generic drm property which can then be attached to a drm
  3340. * object with drm_object_attach_property. The returned property object must be
  3341. * freed with drm_property_destroy.
  3342. *
  3343. * This is implemented as a ranged property with only {0, 1} as valid values.
  3344. *
  3345. * Returns:
  3346. * A pointer to the newly created property on success, NULL on failure.
  3347. */
  3348. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3349. const char *name)
  3350. {
  3351. return drm_property_create_range(dev, flags, name, 0, 1);
  3352. }
  3353. EXPORT_SYMBOL(drm_property_create_bool);
  3354. /**
  3355. * drm_property_add_enum - add a possible value to an enumeration property
  3356. * @property: enumeration property to change
  3357. * @index: index of the new enumeration
  3358. * @value: value of the new enumeration
  3359. * @name: symbolic name of the new enumeration
  3360. *
  3361. * This functions adds enumerations to a property.
  3362. *
  3363. * It's use is deprecated, drivers should use one of the more specific helpers
  3364. * to directly create the property with all enumerations already attached.
  3365. *
  3366. * Returns:
  3367. * Zero on success, error code on failure.
  3368. */
  3369. int drm_property_add_enum(struct drm_property *property, int index,
  3370. uint64_t value, const char *name)
  3371. {
  3372. struct drm_property_enum *prop_enum;
  3373. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3374. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3375. return -EINVAL;
  3376. /*
  3377. * Bitmask enum properties have the additional constraint of values
  3378. * from 0 to 63
  3379. */
  3380. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3381. (value > 63))
  3382. return -EINVAL;
  3383. if (!list_empty(&property->enum_list)) {
  3384. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3385. if (prop_enum->value == value) {
  3386. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3387. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3388. return 0;
  3389. }
  3390. }
  3391. }
  3392. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3393. if (!prop_enum)
  3394. return -ENOMEM;
  3395. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3396. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3397. prop_enum->value = value;
  3398. property->values[index] = value;
  3399. list_add_tail(&prop_enum->head, &property->enum_list);
  3400. return 0;
  3401. }
  3402. EXPORT_SYMBOL(drm_property_add_enum);
  3403. /**
  3404. * drm_property_destroy - destroy a drm property
  3405. * @dev: drm device
  3406. * @property: property to destry
  3407. *
  3408. * This function frees a property including any attached resources like
  3409. * enumeration values.
  3410. */
  3411. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3412. {
  3413. struct drm_property_enum *prop_enum, *pt;
  3414. list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3415. list_del(&prop_enum->head);
  3416. kfree(prop_enum);
  3417. }
  3418. if (property->num_values)
  3419. kfree(property->values);
  3420. drm_mode_object_put(dev, &property->base);
  3421. list_del(&property->head);
  3422. kfree(property);
  3423. }
  3424. EXPORT_SYMBOL(drm_property_destroy);
  3425. /**
  3426. * drm_object_attach_property - attach a property to a modeset object
  3427. * @obj: drm modeset object
  3428. * @property: property to attach
  3429. * @init_val: initial value of the property
  3430. *
  3431. * This attaches the given property to the modeset object with the given initial
  3432. * value. Currently this function cannot fail since the properties are stored in
  3433. * a statically sized array.
  3434. */
  3435. void drm_object_attach_property(struct drm_mode_object *obj,
  3436. struct drm_property *property,
  3437. uint64_t init_val)
  3438. {
  3439. int count = obj->properties->count;
  3440. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3441. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3442. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3443. "you see this message on the same object type.\n",
  3444. obj->type);
  3445. return;
  3446. }
  3447. obj->properties->properties[count] = property;
  3448. obj->properties->values[count] = init_val;
  3449. obj->properties->count++;
  3450. if (property->flags & DRM_MODE_PROP_ATOMIC)
  3451. obj->properties->atomic_count++;
  3452. }
  3453. EXPORT_SYMBOL(drm_object_attach_property);
  3454. /**
  3455. * drm_object_property_set_value - set the value of a property
  3456. * @obj: drm mode object to set property value for
  3457. * @property: property to set
  3458. * @val: value the property should be set to
  3459. *
  3460. * This functions sets a given property on a given object. This function only
  3461. * changes the software state of the property, it does not call into the
  3462. * driver's ->set_property callback.
  3463. *
  3464. * Returns:
  3465. * Zero on success, error code on failure.
  3466. */
  3467. int drm_object_property_set_value(struct drm_mode_object *obj,
  3468. struct drm_property *property, uint64_t val)
  3469. {
  3470. int i;
  3471. for (i = 0; i < obj->properties->count; i++) {
  3472. if (obj->properties->properties[i] == property) {
  3473. obj->properties->values[i] = val;
  3474. return 0;
  3475. }
  3476. }
  3477. return -EINVAL;
  3478. }
  3479. EXPORT_SYMBOL(drm_object_property_set_value);
  3480. /**
  3481. * drm_object_property_get_value - retrieve the value of a property
  3482. * @obj: drm mode object to get property value from
  3483. * @property: property to retrieve
  3484. * @val: storage for the property value
  3485. *
  3486. * This function retrieves the softare state of the given property for the given
  3487. * property. Since there is no driver callback to retrieve the current property
  3488. * value this might be out of sync with the hardware, depending upon the driver
  3489. * and property.
  3490. *
  3491. * Returns:
  3492. * Zero on success, error code on failure.
  3493. */
  3494. int drm_object_property_get_value(struct drm_mode_object *obj,
  3495. struct drm_property *property, uint64_t *val)
  3496. {
  3497. int i;
  3498. /* read-only properties bypass atomic mechanism and still store
  3499. * their value in obj->properties->values[].. mostly to avoid
  3500. * having to deal w/ EDID and similar props in atomic paths:
  3501. */
  3502. if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3503. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3504. return drm_atomic_get_property(obj, property, val);
  3505. for (i = 0; i < obj->properties->count; i++) {
  3506. if (obj->properties->properties[i] == property) {
  3507. *val = obj->properties->values[i];
  3508. return 0;
  3509. }
  3510. }
  3511. return -EINVAL;
  3512. }
  3513. EXPORT_SYMBOL(drm_object_property_get_value);
  3514. /**
  3515. * drm_mode_getproperty_ioctl - get the property metadata
  3516. * @dev: DRM device
  3517. * @data: ioctl data
  3518. * @file_priv: DRM file info
  3519. *
  3520. * This function retrieves the metadata for a given property, like the different
  3521. * possible values for an enum property or the limits for a range property.
  3522. *
  3523. * Blob properties are special
  3524. *
  3525. * Called by the user via ioctl.
  3526. *
  3527. * Returns:
  3528. * Zero on success, negative errno on failure.
  3529. */
  3530. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3531. void *data, struct drm_file *file_priv)
  3532. {
  3533. struct drm_mode_get_property *out_resp = data;
  3534. struct drm_property *property;
  3535. int enum_count = 0;
  3536. int value_count = 0;
  3537. int ret = 0, i;
  3538. int copied;
  3539. struct drm_property_enum *prop_enum;
  3540. struct drm_mode_property_enum __user *enum_ptr;
  3541. uint64_t __user *values_ptr;
  3542. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3543. return -EINVAL;
  3544. drm_modeset_lock_all(dev);
  3545. property = drm_property_find(dev, out_resp->prop_id);
  3546. if (!property) {
  3547. ret = -ENOENT;
  3548. goto done;
  3549. }
  3550. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3551. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3552. list_for_each_entry(prop_enum, &property->enum_list, head)
  3553. enum_count++;
  3554. }
  3555. value_count = property->num_values;
  3556. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3557. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3558. out_resp->flags = property->flags;
  3559. if ((out_resp->count_values >= value_count) && value_count) {
  3560. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3561. for (i = 0; i < value_count; i++) {
  3562. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3563. ret = -EFAULT;
  3564. goto done;
  3565. }
  3566. }
  3567. }
  3568. out_resp->count_values = value_count;
  3569. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3570. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3571. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3572. copied = 0;
  3573. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3574. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3575. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3576. ret = -EFAULT;
  3577. goto done;
  3578. }
  3579. if (copy_to_user(&enum_ptr[copied].name,
  3580. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3581. ret = -EFAULT;
  3582. goto done;
  3583. }
  3584. copied++;
  3585. }
  3586. }
  3587. out_resp->count_enum_blobs = enum_count;
  3588. }
  3589. /*
  3590. * NOTE: The idea seems to have been to use this to read all the blob
  3591. * property values. But nothing ever added them to the corresponding
  3592. * list, userspace always used the special-purpose get_blob ioctl to
  3593. * read the value for a blob property. It also doesn't make a lot of
  3594. * sense to return values here when everything else is just metadata for
  3595. * the property itself.
  3596. */
  3597. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  3598. out_resp->count_enum_blobs = 0;
  3599. done:
  3600. drm_modeset_unlock_all(dev);
  3601. return ret;
  3602. }
  3603. /**
  3604. * drm_property_create_blob - Create new blob property
  3605. *
  3606. * Creates a new blob property for a specified DRM device, optionally
  3607. * copying data.
  3608. *
  3609. * @dev: DRM device to create property for
  3610. * @length: Length to allocate for blob data
  3611. * @data: If specified, copies data into blob
  3612. */
  3613. struct drm_property_blob *
  3614. drm_property_create_blob(struct drm_device *dev, size_t length,
  3615. const void *data)
  3616. {
  3617. struct drm_property_blob *blob;
  3618. int ret;
  3619. if (!length)
  3620. return NULL;
  3621. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3622. if (!blob)
  3623. return NULL;
  3624. blob->length = length;
  3625. blob->dev = dev;
  3626. if (data)
  3627. memcpy(blob->data, data, length);
  3628. mutex_lock(&dev->mode_config.blob_lock);
  3629. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3630. if (ret) {
  3631. kfree(blob);
  3632. mutex_unlock(&dev->mode_config.blob_lock);
  3633. return NULL;
  3634. }
  3635. kref_init(&blob->refcount);
  3636. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  3637. mutex_unlock(&dev->mode_config.blob_lock);
  3638. return blob;
  3639. }
  3640. EXPORT_SYMBOL(drm_property_create_blob);
  3641. /**
  3642. * drm_property_free_blob - Blob property destructor
  3643. *
  3644. * Internal free function for blob properties; must not be used directly.
  3645. *
  3646. * @kref: Reference
  3647. */
  3648. static void drm_property_free_blob(struct kref *kref)
  3649. {
  3650. struct drm_property_blob *blob =
  3651. container_of(kref, struct drm_property_blob, refcount);
  3652. WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
  3653. list_del(&blob->head);
  3654. drm_mode_object_put(blob->dev, &blob->base);
  3655. kfree(blob);
  3656. }
  3657. /**
  3658. * drm_property_unreference_blob - Unreference a blob property
  3659. *
  3660. * Drop a reference on a blob property. May free the object.
  3661. *
  3662. * @blob: Pointer to blob property
  3663. */
  3664. void drm_property_unreference_blob(struct drm_property_blob *blob)
  3665. {
  3666. struct drm_device *dev;
  3667. if (!blob)
  3668. return;
  3669. dev = blob->dev;
  3670. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3671. if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
  3672. &dev->mode_config.blob_lock))
  3673. mutex_unlock(&dev->mode_config.blob_lock);
  3674. else
  3675. might_lock(&dev->mode_config.blob_lock);
  3676. }
  3677. EXPORT_SYMBOL(drm_property_unreference_blob);
  3678. /**
  3679. * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
  3680. *
  3681. * Drop a reference on a blob property. May free the object. This must be
  3682. * called with blob_lock held.
  3683. *
  3684. * @blob: Pointer to blob property
  3685. */
  3686. static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
  3687. {
  3688. if (!blob)
  3689. return;
  3690. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3691. kref_put(&blob->refcount, drm_property_free_blob);
  3692. }
  3693. /**
  3694. * drm_property_reference_blob - Take a reference on an existing property
  3695. *
  3696. * Take a new reference on an existing blob property.
  3697. *
  3698. * @blob: Pointer to blob property
  3699. */
  3700. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  3701. {
  3702. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3703. kref_get(&blob->refcount);
  3704. return blob;
  3705. }
  3706. EXPORT_SYMBOL(drm_property_reference_blob);
  3707. /*
  3708. * Like drm_property_lookup_blob, but does not return an additional reference.
  3709. * Must be called with blob_lock held.
  3710. */
  3711. static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
  3712. uint32_t id)
  3713. {
  3714. struct drm_mode_object *obj = NULL;
  3715. struct drm_property_blob *blob;
  3716. WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
  3717. mutex_lock(&dev->mode_config.idr_mutex);
  3718. obj = idr_find(&dev->mode_config.crtc_idr, id);
  3719. if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
  3720. blob = NULL;
  3721. else
  3722. blob = obj_to_blob(obj);
  3723. mutex_unlock(&dev->mode_config.idr_mutex);
  3724. return blob;
  3725. }
  3726. /**
  3727. * drm_property_lookup_blob - look up a blob property and take a reference
  3728. * @dev: drm device
  3729. * @id: id of the blob property
  3730. *
  3731. * If successful, this takes an additional reference to the blob property.
  3732. * callers need to make sure to eventually unreference the returned property
  3733. * again, using @drm_property_unreference_blob.
  3734. */
  3735. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  3736. uint32_t id)
  3737. {
  3738. struct drm_property_blob *blob;
  3739. mutex_lock(&dev->mode_config.blob_lock);
  3740. blob = __drm_property_lookup_blob(dev, id);
  3741. if (blob) {
  3742. if (!kref_get_unless_zero(&blob->refcount))
  3743. blob = NULL;
  3744. }
  3745. mutex_unlock(&dev->mode_config.blob_lock);
  3746. return blob;
  3747. }
  3748. EXPORT_SYMBOL(drm_property_lookup_blob);
  3749. /**
  3750. * drm_property_replace_global_blob - atomically replace existing blob property
  3751. * @dev: drm device
  3752. * @replace: location of blob property pointer to be replaced
  3753. * @length: length of data for new blob, or 0 for no data
  3754. * @data: content for new blob, or NULL for no data
  3755. * @obj_holds_id: optional object for property holding blob ID
  3756. * @prop_holds_id: optional property holding blob ID
  3757. * @return 0 on success or error on failure
  3758. *
  3759. * This function will atomically replace a global property in the blob list,
  3760. * optionally updating a property which holds the ID of that property. It is
  3761. * guaranteed to be atomic: no caller will be allowed to see intermediate
  3762. * results, and either the entire operation will succeed and clean up the
  3763. * previous property, or it will fail and the state will be unchanged.
  3764. *
  3765. * If length is 0 or data is NULL, no new blob will be created, and the holding
  3766. * property, if specified, will be set to 0.
  3767. *
  3768. * Access to the replace pointer is assumed to be protected by the caller, e.g.
  3769. * by holding the relevant modesetting object lock for its parent.
  3770. *
  3771. * For example, a drm_connector has a 'PATH' property, which contains the ID
  3772. * of a blob property with the value of the MST path information. Calling this
  3773. * function with replace pointing to the connector's path_blob_ptr, length and
  3774. * data set for the new path information, obj_holds_id set to the connector's
  3775. * base object, and prop_holds_id set to the path property name, will perform
  3776. * a completely atomic update. The access to path_blob_ptr is protected by the
  3777. * caller holding a lock on the connector.
  3778. */
  3779. static int drm_property_replace_global_blob(struct drm_device *dev,
  3780. struct drm_property_blob **replace,
  3781. size_t length,
  3782. const void *data,
  3783. struct drm_mode_object *obj_holds_id,
  3784. struct drm_property *prop_holds_id)
  3785. {
  3786. struct drm_property_blob *new_blob = NULL;
  3787. struct drm_property_blob *old_blob = NULL;
  3788. int ret;
  3789. WARN_ON(replace == NULL);
  3790. old_blob = *replace;
  3791. if (length && data) {
  3792. new_blob = drm_property_create_blob(dev, length, data);
  3793. if (!new_blob)
  3794. return -EINVAL;
  3795. }
  3796. /* This does not need to be synchronised with blob_lock, as the
  3797. * get_properties ioctl locks all modesetting objects, and
  3798. * obj_holds_id must be locked before calling here, so we cannot
  3799. * have its value out of sync with the list membership modified
  3800. * below under blob_lock. */
  3801. if (obj_holds_id) {
  3802. ret = drm_object_property_set_value(obj_holds_id,
  3803. prop_holds_id,
  3804. new_blob ?
  3805. new_blob->base.id : 0);
  3806. if (ret != 0)
  3807. goto err_created;
  3808. }
  3809. if (old_blob)
  3810. drm_property_unreference_blob(old_blob);
  3811. *replace = new_blob;
  3812. return 0;
  3813. err_created:
  3814. drm_property_unreference_blob(new_blob);
  3815. return ret;
  3816. }
  3817. /**
  3818. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3819. * @dev: DRM device
  3820. * @data: ioctl data
  3821. * @file_priv: DRM file info
  3822. *
  3823. * This function retrieves the contents of a blob property. The value stored in
  3824. * an object's blob property is just a normal modeset object id.
  3825. *
  3826. * Called by the user via ioctl.
  3827. *
  3828. * Returns:
  3829. * Zero on success, negative errno on failure.
  3830. */
  3831. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3832. void *data, struct drm_file *file_priv)
  3833. {
  3834. struct drm_mode_get_blob *out_resp = data;
  3835. struct drm_property_blob *blob;
  3836. int ret = 0;
  3837. void __user *blob_ptr;
  3838. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3839. return -EINVAL;
  3840. drm_modeset_lock_all(dev);
  3841. mutex_lock(&dev->mode_config.blob_lock);
  3842. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  3843. if (!blob) {
  3844. ret = -ENOENT;
  3845. goto done;
  3846. }
  3847. if (out_resp->length == blob->length) {
  3848. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3849. if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  3850. ret = -EFAULT;
  3851. goto done;
  3852. }
  3853. }
  3854. out_resp->length = blob->length;
  3855. done:
  3856. mutex_unlock(&dev->mode_config.blob_lock);
  3857. drm_modeset_unlock_all(dev);
  3858. return ret;
  3859. }
  3860. /**
  3861. * drm_mode_connector_set_path_property - set tile property on connector
  3862. * @connector: connector to set property on.
  3863. * @path: path to use for property; must not be NULL.
  3864. *
  3865. * This creates a property to expose to userspace to specify a
  3866. * connector path. This is mainly used for DisplayPort MST where
  3867. * connectors have a topology and we want to allow userspace to give
  3868. * them more meaningful names.
  3869. *
  3870. * Returns:
  3871. * Zero on success, negative errno on failure.
  3872. */
  3873. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  3874. const char *path)
  3875. {
  3876. struct drm_device *dev = connector->dev;
  3877. int ret;
  3878. ret = drm_property_replace_global_blob(dev,
  3879. &connector->path_blob_ptr,
  3880. strlen(path) + 1,
  3881. path,
  3882. &connector->base,
  3883. dev->mode_config.path_property);
  3884. return ret;
  3885. }
  3886. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  3887. /**
  3888. * drm_mode_connector_set_tile_property - set tile property on connector
  3889. * @connector: connector to set property on.
  3890. *
  3891. * This looks up the tile information for a connector, and creates a
  3892. * property for userspace to parse if it exists. The property is of
  3893. * the form of 8 integers using ':' as a separator.
  3894. *
  3895. * Returns:
  3896. * Zero on success, errno on failure.
  3897. */
  3898. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  3899. {
  3900. struct drm_device *dev = connector->dev;
  3901. char tile[256];
  3902. int ret;
  3903. if (!connector->has_tile) {
  3904. ret = drm_property_replace_global_blob(dev,
  3905. &connector->tile_blob_ptr,
  3906. 0,
  3907. NULL,
  3908. &connector->base,
  3909. dev->mode_config.tile_property);
  3910. return ret;
  3911. }
  3912. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  3913. connector->tile_group->id, connector->tile_is_single_monitor,
  3914. connector->num_h_tile, connector->num_v_tile,
  3915. connector->tile_h_loc, connector->tile_v_loc,
  3916. connector->tile_h_size, connector->tile_v_size);
  3917. ret = drm_property_replace_global_blob(dev,
  3918. &connector->tile_blob_ptr,
  3919. strlen(tile) + 1,
  3920. tile,
  3921. &connector->base,
  3922. dev->mode_config.tile_property);
  3923. return ret;
  3924. }
  3925. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  3926. /**
  3927. * drm_mode_connector_update_edid_property - update the edid property of a connector
  3928. * @connector: drm connector
  3929. * @edid: new value of the edid property
  3930. *
  3931. * This function creates a new blob modeset object and assigns its id to the
  3932. * connector's edid property.
  3933. *
  3934. * Returns:
  3935. * Zero on success, negative errno on failure.
  3936. */
  3937. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  3938. const struct edid *edid)
  3939. {
  3940. struct drm_device *dev = connector->dev;
  3941. size_t size = 0;
  3942. int ret;
  3943. /* ignore requests to set edid when overridden */
  3944. if (connector->override_edid)
  3945. return 0;
  3946. if (edid)
  3947. size = EDID_LENGTH + (1 + edid->extensions);
  3948. ret = drm_property_replace_global_blob(dev,
  3949. &connector->edid_blob_ptr,
  3950. size,
  3951. edid,
  3952. &connector->base,
  3953. dev->mode_config.edid_property);
  3954. return ret;
  3955. }
  3956. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  3957. /* Some properties could refer to dynamic refcnt'd objects, or things that
  3958. * need special locking to handle lifetime issues (ie. to ensure the prop
  3959. * value doesn't become invalid part way through the property update due to
  3960. * race). The value returned by reference via 'obj' should be passed back
  3961. * to drm_property_change_valid_put() after the property is set (and the
  3962. * object to which the property is attached has a chance to take it's own
  3963. * reference).
  3964. */
  3965. bool drm_property_change_valid_get(struct drm_property *property,
  3966. uint64_t value, struct drm_mode_object **ref)
  3967. {
  3968. int i;
  3969. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  3970. return false;
  3971. *ref = NULL;
  3972. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  3973. if (value < property->values[0] || value > property->values[1])
  3974. return false;
  3975. return true;
  3976. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  3977. int64_t svalue = U642I64(value);
  3978. if (svalue < U642I64(property->values[0]) ||
  3979. svalue > U642I64(property->values[1]))
  3980. return false;
  3981. return true;
  3982. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3983. uint64_t valid_mask = 0;
  3984. for (i = 0; i < property->num_values; i++)
  3985. valid_mask |= (1ULL << property->values[i]);
  3986. return !(value & ~valid_mask);
  3987. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3988. struct drm_property_blob *blob;
  3989. if (value == 0)
  3990. return true;
  3991. blob = drm_property_lookup_blob(property->dev, value);
  3992. if (blob) {
  3993. *ref = &blob->base;
  3994. return true;
  3995. } else {
  3996. return false;
  3997. }
  3998. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  3999. /* a zero value for an object property translates to null: */
  4000. if (value == 0)
  4001. return true;
  4002. /* handle refcnt'd objects specially: */
  4003. if (property->values[0] == DRM_MODE_OBJECT_FB) {
  4004. struct drm_framebuffer *fb;
  4005. fb = drm_framebuffer_lookup(property->dev, value);
  4006. if (fb) {
  4007. *ref = &fb->base;
  4008. return true;
  4009. } else {
  4010. return false;
  4011. }
  4012. } else {
  4013. return _object_find(property->dev, value, property->values[0]) != NULL;
  4014. }
  4015. }
  4016. for (i = 0; i < property->num_values; i++)
  4017. if (property->values[i] == value)
  4018. return true;
  4019. return false;
  4020. }
  4021. void drm_property_change_valid_put(struct drm_property *property,
  4022. struct drm_mode_object *ref)
  4023. {
  4024. if (!ref)
  4025. return;
  4026. if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4027. if (property->values[0] == DRM_MODE_OBJECT_FB)
  4028. drm_framebuffer_unreference(obj_to_fb(ref));
  4029. }
  4030. }
  4031. /**
  4032. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  4033. * @dev: DRM device
  4034. * @data: ioctl data
  4035. * @file_priv: DRM file info
  4036. *
  4037. * This function sets the current value for a connectors's property. It also
  4038. * calls into a driver's ->set_property callback to update the hardware state
  4039. *
  4040. * Called by the user via ioctl.
  4041. *
  4042. * Returns:
  4043. * Zero on success, negative errno on failure.
  4044. */
  4045. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  4046. void *data, struct drm_file *file_priv)
  4047. {
  4048. struct drm_mode_connector_set_property *conn_set_prop = data;
  4049. struct drm_mode_obj_set_property obj_set_prop = {
  4050. .value = conn_set_prop->value,
  4051. .prop_id = conn_set_prop->prop_id,
  4052. .obj_id = conn_set_prop->connector_id,
  4053. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  4054. };
  4055. /* It does all the locking and checking we need */
  4056. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  4057. }
  4058. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4059. struct drm_property *property,
  4060. uint64_t value)
  4061. {
  4062. int ret = -EINVAL;
  4063. struct drm_connector *connector = obj_to_connector(obj);
  4064. /* Do DPMS ourselves */
  4065. if (property == connector->dev->mode_config.dpms_property) {
  4066. if (connector->funcs->dpms)
  4067. (*connector->funcs->dpms)(connector, (int)value);
  4068. ret = 0;
  4069. } else if (connector->funcs->set_property)
  4070. ret = connector->funcs->set_property(connector, property, value);
  4071. /* store the property value if successful */
  4072. if (!ret)
  4073. drm_object_property_set_value(&connector->base, property, value);
  4074. return ret;
  4075. }
  4076. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4077. struct drm_property *property,
  4078. uint64_t value)
  4079. {
  4080. int ret = -EINVAL;
  4081. struct drm_crtc *crtc = obj_to_crtc(obj);
  4082. if (crtc->funcs->set_property)
  4083. ret = crtc->funcs->set_property(crtc, property, value);
  4084. if (!ret)
  4085. drm_object_property_set_value(obj, property, value);
  4086. return ret;
  4087. }
  4088. /**
  4089. * drm_mode_plane_set_obj_prop - set the value of a property
  4090. * @plane: drm plane object to set property value for
  4091. * @property: property to set
  4092. * @value: value the property should be set to
  4093. *
  4094. * This functions sets a given property on a given plane object. This function
  4095. * calls the driver's ->set_property callback and changes the software state of
  4096. * the property if the callback succeeds.
  4097. *
  4098. * Returns:
  4099. * Zero on success, error code on failure.
  4100. */
  4101. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4102. struct drm_property *property,
  4103. uint64_t value)
  4104. {
  4105. int ret = -EINVAL;
  4106. struct drm_mode_object *obj = &plane->base;
  4107. if (plane->funcs->set_property)
  4108. ret = plane->funcs->set_property(plane, property, value);
  4109. if (!ret)
  4110. drm_object_property_set_value(obj, property, value);
  4111. return ret;
  4112. }
  4113. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4114. /**
  4115. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4116. * @dev: DRM device
  4117. * @data: ioctl data
  4118. * @file_priv: DRM file info
  4119. *
  4120. * This function retrieves the current value for an object's property. Compared
  4121. * to the connector specific ioctl this one is extended to also work on crtc and
  4122. * plane objects.
  4123. *
  4124. * Called by the user via ioctl.
  4125. *
  4126. * Returns:
  4127. * Zero on success, negative errno on failure.
  4128. */
  4129. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4130. struct drm_file *file_priv)
  4131. {
  4132. struct drm_mode_obj_get_properties *arg = data;
  4133. struct drm_mode_object *obj;
  4134. int ret = 0;
  4135. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4136. return -EINVAL;
  4137. drm_modeset_lock_all(dev);
  4138. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4139. if (!obj) {
  4140. ret = -ENOENT;
  4141. goto out;
  4142. }
  4143. if (!obj->properties) {
  4144. ret = -EINVAL;
  4145. goto out;
  4146. }
  4147. ret = get_properties(obj, file_priv->atomic,
  4148. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4149. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4150. &arg->count_props);
  4151. out:
  4152. drm_modeset_unlock_all(dev);
  4153. return ret;
  4154. }
  4155. /**
  4156. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4157. * @dev: DRM device
  4158. * @data: ioctl data
  4159. * @file_priv: DRM file info
  4160. *
  4161. * This function sets the current value for an object's property. It also calls
  4162. * into a driver's ->set_property callback to update the hardware state.
  4163. * Compared to the connector specific ioctl this one is extended to also work on
  4164. * crtc and plane objects.
  4165. *
  4166. * Called by the user via ioctl.
  4167. *
  4168. * Returns:
  4169. * Zero on success, negative errno on failure.
  4170. */
  4171. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4172. struct drm_file *file_priv)
  4173. {
  4174. struct drm_mode_obj_set_property *arg = data;
  4175. struct drm_mode_object *arg_obj;
  4176. struct drm_mode_object *prop_obj;
  4177. struct drm_property *property;
  4178. int i, ret = -EINVAL;
  4179. struct drm_mode_object *ref;
  4180. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4181. return -EINVAL;
  4182. drm_modeset_lock_all(dev);
  4183. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4184. if (!arg_obj) {
  4185. ret = -ENOENT;
  4186. goto out;
  4187. }
  4188. if (!arg_obj->properties)
  4189. goto out;
  4190. for (i = 0; i < arg_obj->properties->count; i++)
  4191. if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4192. break;
  4193. if (i == arg_obj->properties->count)
  4194. goto out;
  4195. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4196. DRM_MODE_OBJECT_PROPERTY);
  4197. if (!prop_obj) {
  4198. ret = -ENOENT;
  4199. goto out;
  4200. }
  4201. property = obj_to_property(prop_obj);
  4202. if (!drm_property_change_valid_get(property, arg->value, &ref))
  4203. goto out;
  4204. switch (arg_obj->type) {
  4205. case DRM_MODE_OBJECT_CONNECTOR:
  4206. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4207. arg->value);
  4208. break;
  4209. case DRM_MODE_OBJECT_CRTC:
  4210. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4211. break;
  4212. case DRM_MODE_OBJECT_PLANE:
  4213. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4214. property, arg->value);
  4215. break;
  4216. }
  4217. drm_property_change_valid_put(property, ref);
  4218. out:
  4219. drm_modeset_unlock_all(dev);
  4220. return ret;
  4221. }
  4222. /**
  4223. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4224. * @connector: connector to attach
  4225. * @encoder: encoder to attach @connector to
  4226. *
  4227. * This function links up a connector to an encoder. Note that the routing
  4228. * restrictions between encoders and crtcs are exposed to userspace through the
  4229. * possible_clones and possible_crtcs bitmasks.
  4230. *
  4231. * Returns:
  4232. * Zero on success, negative errno on failure.
  4233. */
  4234. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4235. struct drm_encoder *encoder)
  4236. {
  4237. int i;
  4238. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4239. if (connector->encoder_ids[i] == 0) {
  4240. connector->encoder_ids[i] = encoder->base.id;
  4241. return 0;
  4242. }
  4243. }
  4244. return -ENOMEM;
  4245. }
  4246. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4247. /**
  4248. * drm_mode_crtc_set_gamma_size - set the gamma table size
  4249. * @crtc: CRTC to set the gamma table size for
  4250. * @gamma_size: size of the gamma table
  4251. *
  4252. * Drivers which support gamma tables should set this to the supported gamma
  4253. * table size when initializing the CRTC. Currently the drm core only supports a
  4254. * fixed gamma table size.
  4255. *
  4256. * Returns:
  4257. * Zero on success, negative errno on failure.
  4258. */
  4259. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4260. int gamma_size)
  4261. {
  4262. crtc->gamma_size = gamma_size;
  4263. crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4264. GFP_KERNEL);
  4265. if (!crtc->gamma_store) {
  4266. crtc->gamma_size = 0;
  4267. return -ENOMEM;
  4268. }
  4269. return 0;
  4270. }
  4271. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4272. /**
  4273. * drm_mode_gamma_set_ioctl - set the gamma table
  4274. * @dev: DRM device
  4275. * @data: ioctl data
  4276. * @file_priv: DRM file info
  4277. *
  4278. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4279. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4280. *
  4281. * Called by the user via ioctl.
  4282. *
  4283. * Returns:
  4284. * Zero on success, negative errno on failure.
  4285. */
  4286. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4287. void *data, struct drm_file *file_priv)
  4288. {
  4289. struct drm_mode_crtc_lut *crtc_lut = data;
  4290. struct drm_crtc *crtc;
  4291. void *r_base, *g_base, *b_base;
  4292. int size;
  4293. int ret = 0;
  4294. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4295. return -EINVAL;
  4296. drm_modeset_lock_all(dev);
  4297. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4298. if (!crtc) {
  4299. ret = -ENOENT;
  4300. goto out;
  4301. }
  4302. if (crtc->funcs->gamma_set == NULL) {
  4303. ret = -ENOSYS;
  4304. goto out;
  4305. }
  4306. /* memcpy into gamma store */
  4307. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4308. ret = -EINVAL;
  4309. goto out;
  4310. }
  4311. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4312. r_base = crtc->gamma_store;
  4313. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4314. ret = -EFAULT;
  4315. goto out;
  4316. }
  4317. g_base = r_base + size;
  4318. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4319. ret = -EFAULT;
  4320. goto out;
  4321. }
  4322. b_base = g_base + size;
  4323. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4324. ret = -EFAULT;
  4325. goto out;
  4326. }
  4327. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4328. out:
  4329. drm_modeset_unlock_all(dev);
  4330. return ret;
  4331. }
  4332. /**
  4333. * drm_mode_gamma_get_ioctl - get the gamma table
  4334. * @dev: DRM device
  4335. * @data: ioctl data
  4336. * @file_priv: DRM file info
  4337. *
  4338. * Copy the current gamma table into the storage provided. This also provides
  4339. * the gamma table size the driver expects, which can be used to size the
  4340. * allocated storage.
  4341. *
  4342. * Called by the user via ioctl.
  4343. *
  4344. * Returns:
  4345. * Zero on success, negative errno on failure.
  4346. */
  4347. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4348. void *data, struct drm_file *file_priv)
  4349. {
  4350. struct drm_mode_crtc_lut *crtc_lut = data;
  4351. struct drm_crtc *crtc;
  4352. void *r_base, *g_base, *b_base;
  4353. int size;
  4354. int ret = 0;
  4355. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4356. return -EINVAL;
  4357. drm_modeset_lock_all(dev);
  4358. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4359. if (!crtc) {
  4360. ret = -ENOENT;
  4361. goto out;
  4362. }
  4363. /* memcpy into gamma store */
  4364. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4365. ret = -EINVAL;
  4366. goto out;
  4367. }
  4368. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4369. r_base = crtc->gamma_store;
  4370. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4371. ret = -EFAULT;
  4372. goto out;
  4373. }
  4374. g_base = r_base + size;
  4375. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4376. ret = -EFAULT;
  4377. goto out;
  4378. }
  4379. b_base = g_base + size;
  4380. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4381. ret = -EFAULT;
  4382. goto out;
  4383. }
  4384. out:
  4385. drm_modeset_unlock_all(dev);
  4386. return ret;
  4387. }
  4388. /**
  4389. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  4390. * @dev: DRM device
  4391. * @data: ioctl data
  4392. * @file_priv: DRM file info
  4393. *
  4394. * This schedules an asynchronous update on a given CRTC, called page flip.
  4395. * Optionally a drm event is generated to signal the completion of the event.
  4396. * Generic drivers cannot assume that a pageflip with changed framebuffer
  4397. * properties (including driver specific metadata like tiling layout) will work,
  4398. * but some drivers support e.g. pixel format changes through the pageflip
  4399. * ioctl.
  4400. *
  4401. * Called by the user via ioctl.
  4402. *
  4403. * Returns:
  4404. * Zero on success, negative errno on failure.
  4405. */
  4406. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  4407. void *data, struct drm_file *file_priv)
  4408. {
  4409. struct drm_mode_crtc_page_flip *page_flip = data;
  4410. struct drm_crtc *crtc;
  4411. struct drm_framebuffer *fb = NULL;
  4412. struct drm_pending_vblank_event *e = NULL;
  4413. unsigned long flags;
  4414. int ret = -EINVAL;
  4415. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  4416. page_flip->reserved != 0)
  4417. return -EINVAL;
  4418. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  4419. return -EINVAL;
  4420. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4421. if (!crtc)
  4422. return -ENOENT;
  4423. drm_modeset_lock_crtc(crtc, crtc->primary);
  4424. if (crtc->primary->fb == NULL) {
  4425. /* The framebuffer is currently unbound, presumably
  4426. * due to a hotplug event, that userspace has not
  4427. * yet discovered.
  4428. */
  4429. ret = -EBUSY;
  4430. goto out;
  4431. }
  4432. if (crtc->funcs->page_flip == NULL)
  4433. goto out;
  4434. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4435. if (!fb) {
  4436. ret = -ENOENT;
  4437. goto out;
  4438. }
  4439. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4440. if (ret)
  4441. goto out;
  4442. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4443. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4444. ret = -EINVAL;
  4445. goto out;
  4446. }
  4447. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4448. ret = -ENOMEM;
  4449. spin_lock_irqsave(&dev->event_lock, flags);
  4450. if (file_priv->event_space < sizeof(e->event)) {
  4451. spin_unlock_irqrestore(&dev->event_lock, flags);
  4452. goto out;
  4453. }
  4454. file_priv->event_space -= sizeof(e->event);
  4455. spin_unlock_irqrestore(&dev->event_lock, flags);
  4456. e = kzalloc(sizeof(*e), GFP_KERNEL);
  4457. if (e == NULL) {
  4458. spin_lock_irqsave(&dev->event_lock, flags);
  4459. file_priv->event_space += sizeof(e->event);
  4460. spin_unlock_irqrestore(&dev->event_lock, flags);
  4461. goto out;
  4462. }
  4463. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4464. e->event.base.length = sizeof(e->event);
  4465. e->event.user_data = page_flip->user_data;
  4466. e->base.event = &e->event.base;
  4467. e->base.file_priv = file_priv;
  4468. e->base.destroy =
  4469. (void (*) (struct drm_pending_event *)) kfree;
  4470. }
  4471. crtc->primary->old_fb = crtc->primary->fb;
  4472. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4473. if (ret) {
  4474. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4475. spin_lock_irqsave(&dev->event_lock, flags);
  4476. file_priv->event_space += sizeof(e->event);
  4477. spin_unlock_irqrestore(&dev->event_lock, flags);
  4478. kfree(e);
  4479. }
  4480. /* Keep the old fb, don't unref it. */
  4481. crtc->primary->old_fb = NULL;
  4482. } else {
  4483. /*
  4484. * Warn if the driver hasn't properly updated the crtc->fb
  4485. * field to reflect that the new framebuffer is now used.
  4486. * Failing to do so will screw with the reference counting
  4487. * on framebuffers.
  4488. */
  4489. WARN_ON(crtc->primary->fb != fb);
  4490. /* Unref only the old framebuffer. */
  4491. fb = NULL;
  4492. }
  4493. out:
  4494. if (fb)
  4495. drm_framebuffer_unreference(fb);
  4496. if (crtc->primary->old_fb)
  4497. drm_framebuffer_unreference(crtc->primary->old_fb);
  4498. crtc->primary->old_fb = NULL;
  4499. drm_modeset_unlock_crtc(crtc);
  4500. return ret;
  4501. }
  4502. /**
  4503. * drm_mode_config_reset - call ->reset callbacks
  4504. * @dev: drm device
  4505. *
  4506. * This functions calls all the crtc's, encoder's and connector's ->reset
  4507. * callback. Drivers can use this in e.g. their driver load or resume code to
  4508. * reset hardware and software state.
  4509. */
  4510. void drm_mode_config_reset(struct drm_device *dev)
  4511. {
  4512. struct drm_crtc *crtc;
  4513. struct drm_plane *plane;
  4514. struct drm_encoder *encoder;
  4515. struct drm_connector *connector;
  4516. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  4517. if (plane->funcs->reset)
  4518. plane->funcs->reset(plane);
  4519. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  4520. if (crtc->funcs->reset)
  4521. crtc->funcs->reset(crtc);
  4522. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  4523. if (encoder->funcs->reset)
  4524. encoder->funcs->reset(encoder);
  4525. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4526. connector->status = connector_status_unknown;
  4527. if (connector->funcs->reset)
  4528. connector->funcs->reset(connector);
  4529. }
  4530. }
  4531. EXPORT_SYMBOL(drm_mode_config_reset);
  4532. /**
  4533. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4534. * @dev: DRM device
  4535. * @data: ioctl data
  4536. * @file_priv: DRM file info
  4537. *
  4538. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4539. * TTM or something else entirely) and returns the resulting buffer handle. This
  4540. * handle can then be wrapped up into a framebuffer modeset object.
  4541. *
  4542. * Note that userspace is not allowed to use such objects for render
  4543. * acceleration - drivers must create their own private ioctls for such a use
  4544. * case.
  4545. *
  4546. * Called by the user via ioctl.
  4547. *
  4548. * Returns:
  4549. * Zero on success, negative errno on failure.
  4550. */
  4551. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4552. void *data, struct drm_file *file_priv)
  4553. {
  4554. struct drm_mode_create_dumb *args = data;
  4555. u32 cpp, stride, size;
  4556. if (!dev->driver->dumb_create)
  4557. return -ENOSYS;
  4558. if (!args->width || !args->height || !args->bpp)
  4559. return -EINVAL;
  4560. /* overflow checks for 32bit size calculations */
  4561. /* NOTE: DIV_ROUND_UP() can overflow */
  4562. cpp = DIV_ROUND_UP(args->bpp, 8);
  4563. if (!cpp || cpp > 0xffffffffU / args->width)
  4564. return -EINVAL;
  4565. stride = cpp * args->width;
  4566. if (args->height > 0xffffffffU / stride)
  4567. return -EINVAL;
  4568. /* test for wrap-around */
  4569. size = args->height * stride;
  4570. if (PAGE_ALIGN(size) == 0)
  4571. return -EINVAL;
  4572. /*
  4573. * handle, pitch and size are output parameters. Zero them out to
  4574. * prevent drivers from accidentally using uninitialized data. Since
  4575. * not all existing userspace is clearing these fields properly we
  4576. * cannot reject IOCTL with garbage in them.
  4577. */
  4578. args->handle = 0;
  4579. args->pitch = 0;
  4580. args->size = 0;
  4581. return dev->driver->dumb_create(file_priv, dev, args);
  4582. }
  4583. /**
  4584. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4585. * @dev: DRM device
  4586. * @data: ioctl data
  4587. * @file_priv: DRM file info
  4588. *
  4589. * Allocate an offset in the drm device node's address space to be able to
  4590. * memory map a dumb buffer.
  4591. *
  4592. * Called by the user via ioctl.
  4593. *
  4594. * Returns:
  4595. * Zero on success, negative errno on failure.
  4596. */
  4597. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4598. void *data, struct drm_file *file_priv)
  4599. {
  4600. struct drm_mode_map_dumb *args = data;
  4601. /* call driver ioctl to get mmap offset */
  4602. if (!dev->driver->dumb_map_offset)
  4603. return -ENOSYS;
  4604. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4605. }
  4606. /**
  4607. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4608. * @dev: DRM device
  4609. * @data: ioctl data
  4610. * @file_priv: DRM file info
  4611. *
  4612. * This destroys the userspace handle for the given dumb backing storage buffer.
  4613. * Since buffer objects must be reference counted in the kernel a buffer object
  4614. * won't be immediately freed if a framebuffer modeset object still uses it.
  4615. *
  4616. * Called by the user via ioctl.
  4617. *
  4618. * Returns:
  4619. * Zero on success, negative errno on failure.
  4620. */
  4621. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4622. void *data, struct drm_file *file_priv)
  4623. {
  4624. struct drm_mode_destroy_dumb *args = data;
  4625. if (!dev->driver->dumb_destroy)
  4626. return -ENOSYS;
  4627. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4628. }
  4629. /**
  4630. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4631. * @format: pixel format (DRM_FORMAT_*)
  4632. * @depth: storage for the depth value
  4633. * @bpp: storage for the bpp value
  4634. *
  4635. * This only supports RGB formats here for compat with code that doesn't use
  4636. * pixel formats directly yet.
  4637. */
  4638. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4639. int *bpp)
  4640. {
  4641. switch (format) {
  4642. case DRM_FORMAT_C8:
  4643. case DRM_FORMAT_RGB332:
  4644. case DRM_FORMAT_BGR233:
  4645. *depth = 8;
  4646. *bpp = 8;
  4647. break;
  4648. case DRM_FORMAT_XRGB1555:
  4649. case DRM_FORMAT_XBGR1555:
  4650. case DRM_FORMAT_RGBX5551:
  4651. case DRM_FORMAT_BGRX5551:
  4652. case DRM_FORMAT_ARGB1555:
  4653. case DRM_FORMAT_ABGR1555:
  4654. case DRM_FORMAT_RGBA5551:
  4655. case DRM_FORMAT_BGRA5551:
  4656. *depth = 15;
  4657. *bpp = 16;
  4658. break;
  4659. case DRM_FORMAT_RGB565:
  4660. case DRM_FORMAT_BGR565:
  4661. *depth = 16;
  4662. *bpp = 16;
  4663. break;
  4664. case DRM_FORMAT_RGB888:
  4665. case DRM_FORMAT_BGR888:
  4666. *depth = 24;
  4667. *bpp = 24;
  4668. break;
  4669. case DRM_FORMAT_XRGB8888:
  4670. case DRM_FORMAT_XBGR8888:
  4671. case DRM_FORMAT_RGBX8888:
  4672. case DRM_FORMAT_BGRX8888:
  4673. *depth = 24;
  4674. *bpp = 32;
  4675. break;
  4676. case DRM_FORMAT_XRGB2101010:
  4677. case DRM_FORMAT_XBGR2101010:
  4678. case DRM_FORMAT_RGBX1010102:
  4679. case DRM_FORMAT_BGRX1010102:
  4680. case DRM_FORMAT_ARGB2101010:
  4681. case DRM_FORMAT_ABGR2101010:
  4682. case DRM_FORMAT_RGBA1010102:
  4683. case DRM_FORMAT_BGRA1010102:
  4684. *depth = 30;
  4685. *bpp = 32;
  4686. break;
  4687. case DRM_FORMAT_ARGB8888:
  4688. case DRM_FORMAT_ABGR8888:
  4689. case DRM_FORMAT_RGBA8888:
  4690. case DRM_FORMAT_BGRA8888:
  4691. *depth = 32;
  4692. *bpp = 32;
  4693. break;
  4694. default:
  4695. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4696. drm_get_format_name(format));
  4697. *depth = 0;
  4698. *bpp = 0;
  4699. break;
  4700. }
  4701. }
  4702. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4703. /**
  4704. * drm_format_num_planes - get the number of planes for format
  4705. * @format: pixel format (DRM_FORMAT_*)
  4706. *
  4707. * Returns:
  4708. * The number of planes used by the specified pixel format.
  4709. */
  4710. int drm_format_num_planes(uint32_t format)
  4711. {
  4712. switch (format) {
  4713. case DRM_FORMAT_YUV410:
  4714. case DRM_FORMAT_YVU410:
  4715. case DRM_FORMAT_YUV411:
  4716. case DRM_FORMAT_YVU411:
  4717. case DRM_FORMAT_YUV420:
  4718. case DRM_FORMAT_YVU420:
  4719. case DRM_FORMAT_YUV422:
  4720. case DRM_FORMAT_YVU422:
  4721. case DRM_FORMAT_YUV444:
  4722. case DRM_FORMAT_YVU444:
  4723. return 3;
  4724. case DRM_FORMAT_NV12:
  4725. case DRM_FORMAT_NV21:
  4726. case DRM_FORMAT_NV16:
  4727. case DRM_FORMAT_NV61:
  4728. case DRM_FORMAT_NV24:
  4729. case DRM_FORMAT_NV42:
  4730. return 2;
  4731. default:
  4732. return 1;
  4733. }
  4734. }
  4735. EXPORT_SYMBOL(drm_format_num_planes);
  4736. /**
  4737. * drm_format_plane_cpp - determine the bytes per pixel value
  4738. * @format: pixel format (DRM_FORMAT_*)
  4739. * @plane: plane index
  4740. *
  4741. * Returns:
  4742. * The bytes per pixel value for the specified plane.
  4743. */
  4744. int drm_format_plane_cpp(uint32_t format, int plane)
  4745. {
  4746. unsigned int depth;
  4747. int bpp;
  4748. if (plane >= drm_format_num_planes(format))
  4749. return 0;
  4750. switch (format) {
  4751. case DRM_FORMAT_YUYV:
  4752. case DRM_FORMAT_YVYU:
  4753. case DRM_FORMAT_UYVY:
  4754. case DRM_FORMAT_VYUY:
  4755. return 2;
  4756. case DRM_FORMAT_NV12:
  4757. case DRM_FORMAT_NV21:
  4758. case DRM_FORMAT_NV16:
  4759. case DRM_FORMAT_NV61:
  4760. case DRM_FORMAT_NV24:
  4761. case DRM_FORMAT_NV42:
  4762. return plane ? 2 : 1;
  4763. case DRM_FORMAT_YUV410:
  4764. case DRM_FORMAT_YVU410:
  4765. case DRM_FORMAT_YUV411:
  4766. case DRM_FORMAT_YVU411:
  4767. case DRM_FORMAT_YUV420:
  4768. case DRM_FORMAT_YVU420:
  4769. case DRM_FORMAT_YUV422:
  4770. case DRM_FORMAT_YVU422:
  4771. case DRM_FORMAT_YUV444:
  4772. case DRM_FORMAT_YVU444:
  4773. return 1;
  4774. default:
  4775. drm_fb_get_bpp_depth(format, &depth, &bpp);
  4776. return bpp >> 3;
  4777. }
  4778. }
  4779. EXPORT_SYMBOL(drm_format_plane_cpp);
  4780. /**
  4781. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4782. * @format: pixel format (DRM_FORMAT_*)
  4783. *
  4784. * Returns:
  4785. * The horizontal chroma subsampling factor for the
  4786. * specified pixel format.
  4787. */
  4788. int drm_format_horz_chroma_subsampling(uint32_t format)
  4789. {
  4790. switch (format) {
  4791. case DRM_FORMAT_YUV411:
  4792. case DRM_FORMAT_YVU411:
  4793. case DRM_FORMAT_YUV410:
  4794. case DRM_FORMAT_YVU410:
  4795. return 4;
  4796. case DRM_FORMAT_YUYV:
  4797. case DRM_FORMAT_YVYU:
  4798. case DRM_FORMAT_UYVY:
  4799. case DRM_FORMAT_VYUY:
  4800. case DRM_FORMAT_NV12:
  4801. case DRM_FORMAT_NV21:
  4802. case DRM_FORMAT_NV16:
  4803. case DRM_FORMAT_NV61:
  4804. case DRM_FORMAT_YUV422:
  4805. case DRM_FORMAT_YVU422:
  4806. case DRM_FORMAT_YUV420:
  4807. case DRM_FORMAT_YVU420:
  4808. return 2;
  4809. default:
  4810. return 1;
  4811. }
  4812. }
  4813. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  4814. /**
  4815. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  4816. * @format: pixel format (DRM_FORMAT_*)
  4817. *
  4818. * Returns:
  4819. * The vertical chroma subsampling factor for the
  4820. * specified pixel format.
  4821. */
  4822. int drm_format_vert_chroma_subsampling(uint32_t format)
  4823. {
  4824. switch (format) {
  4825. case DRM_FORMAT_YUV410:
  4826. case DRM_FORMAT_YVU410:
  4827. return 4;
  4828. case DRM_FORMAT_YUV420:
  4829. case DRM_FORMAT_YVU420:
  4830. case DRM_FORMAT_NV12:
  4831. case DRM_FORMAT_NV21:
  4832. return 2;
  4833. default:
  4834. return 1;
  4835. }
  4836. }
  4837. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  4838. /**
  4839. * drm_rotation_simplify() - Try to simplify the rotation
  4840. * @rotation: Rotation to be simplified
  4841. * @supported_rotations: Supported rotations
  4842. *
  4843. * Attempt to simplify the rotation to a form that is supported.
  4844. * Eg. if the hardware supports everything except DRM_REFLECT_X
  4845. * one could call this function like this:
  4846. *
  4847. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  4848. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  4849. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  4850. *
  4851. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  4852. * transforms the hardware supports, this function may not
  4853. * be able to produce a supported transform, so the caller should
  4854. * check the result afterwards.
  4855. */
  4856. unsigned int drm_rotation_simplify(unsigned int rotation,
  4857. unsigned int supported_rotations)
  4858. {
  4859. if (rotation & ~supported_rotations) {
  4860. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  4861. rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
  4862. }
  4863. return rotation;
  4864. }
  4865. EXPORT_SYMBOL(drm_rotation_simplify);
  4866. /**
  4867. * drm_mode_config_init - initialize DRM mode_configuration structure
  4868. * @dev: DRM device
  4869. *
  4870. * Initialize @dev's mode_config structure, used for tracking the graphics
  4871. * configuration of @dev.
  4872. *
  4873. * Since this initializes the modeset locks, no locking is possible. Which is no
  4874. * problem, since this should happen single threaded at init time. It is the
  4875. * driver's problem to ensure this guarantee.
  4876. *
  4877. */
  4878. void drm_mode_config_init(struct drm_device *dev)
  4879. {
  4880. mutex_init(&dev->mode_config.mutex);
  4881. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  4882. mutex_init(&dev->mode_config.idr_mutex);
  4883. mutex_init(&dev->mode_config.fb_lock);
  4884. mutex_init(&dev->mode_config.blob_lock);
  4885. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  4886. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  4887. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  4888. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  4889. INIT_LIST_HEAD(&dev->mode_config.property_list);
  4890. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  4891. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  4892. idr_init(&dev->mode_config.crtc_idr);
  4893. idr_init(&dev->mode_config.tile_idr);
  4894. drm_modeset_lock_all(dev);
  4895. drm_mode_create_standard_properties(dev);
  4896. drm_modeset_unlock_all(dev);
  4897. /* Just to be sure */
  4898. dev->mode_config.num_fb = 0;
  4899. dev->mode_config.num_connector = 0;
  4900. dev->mode_config.num_crtc = 0;
  4901. dev->mode_config.num_encoder = 0;
  4902. dev->mode_config.num_overlay_plane = 0;
  4903. dev->mode_config.num_total_plane = 0;
  4904. }
  4905. EXPORT_SYMBOL(drm_mode_config_init);
  4906. /**
  4907. * drm_mode_config_cleanup - free up DRM mode_config info
  4908. * @dev: DRM device
  4909. *
  4910. * Free up all the connectors and CRTCs associated with this DRM device, then
  4911. * free up the framebuffers and associated buffer objects.
  4912. *
  4913. * Note that since this /should/ happen single-threaded at driver/device
  4914. * teardown time, no locking is required. It's the driver's job to ensure that
  4915. * this guarantee actually holds true.
  4916. *
  4917. * FIXME: cleanup any dangling user buffer objects too
  4918. */
  4919. void drm_mode_config_cleanup(struct drm_device *dev)
  4920. {
  4921. struct drm_connector *connector, *ot;
  4922. struct drm_crtc *crtc, *ct;
  4923. struct drm_encoder *encoder, *enct;
  4924. struct drm_framebuffer *fb, *fbt;
  4925. struct drm_property *property, *pt;
  4926. struct drm_property_blob *blob, *bt;
  4927. struct drm_plane *plane, *plt;
  4928. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  4929. head) {
  4930. encoder->funcs->destroy(encoder);
  4931. }
  4932. list_for_each_entry_safe(connector, ot,
  4933. &dev->mode_config.connector_list, head) {
  4934. connector->funcs->destroy(connector);
  4935. }
  4936. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  4937. head) {
  4938. drm_property_destroy(dev, property);
  4939. }
  4940. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  4941. head) {
  4942. drm_property_unreference_blob(blob);
  4943. }
  4944. /*
  4945. * Single-threaded teardown context, so it's not required to grab the
  4946. * fb_lock to protect against concurrent fb_list access. Contrary, it
  4947. * would actually deadlock with the drm_framebuffer_cleanup function.
  4948. *
  4949. * Also, if there are any framebuffers left, that's a driver leak now,
  4950. * so politely WARN about this.
  4951. */
  4952. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  4953. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  4954. drm_framebuffer_remove(fb);
  4955. }
  4956. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  4957. head) {
  4958. plane->funcs->destroy(plane);
  4959. }
  4960. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  4961. crtc->funcs->destroy(crtc);
  4962. }
  4963. idr_destroy(&dev->mode_config.tile_idr);
  4964. idr_destroy(&dev->mode_config.crtc_idr);
  4965. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  4966. }
  4967. EXPORT_SYMBOL(drm_mode_config_cleanup);
  4968. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  4969. unsigned int supported_rotations)
  4970. {
  4971. static const struct drm_prop_enum_list props[] = {
  4972. { DRM_ROTATE_0, "rotate-0" },
  4973. { DRM_ROTATE_90, "rotate-90" },
  4974. { DRM_ROTATE_180, "rotate-180" },
  4975. { DRM_ROTATE_270, "rotate-270" },
  4976. { DRM_REFLECT_X, "reflect-x" },
  4977. { DRM_REFLECT_Y, "reflect-y" },
  4978. };
  4979. return drm_property_create_bitmask(dev, 0, "rotation",
  4980. props, ARRAY_SIZE(props),
  4981. supported_rotations);
  4982. }
  4983. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  4984. /**
  4985. * DOC: Tile group
  4986. *
  4987. * Tile groups are used to represent tiled monitors with a unique
  4988. * integer identifier. Tiled monitors using DisplayID v1.3 have
  4989. * a unique 8-byte handle, we store this in a tile group, so we
  4990. * have a common identifier for all tiles in a monitor group.
  4991. */
  4992. static void drm_tile_group_free(struct kref *kref)
  4993. {
  4994. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  4995. struct drm_device *dev = tg->dev;
  4996. mutex_lock(&dev->mode_config.idr_mutex);
  4997. idr_remove(&dev->mode_config.tile_idr, tg->id);
  4998. mutex_unlock(&dev->mode_config.idr_mutex);
  4999. kfree(tg);
  5000. }
  5001. /**
  5002. * drm_mode_put_tile_group - drop a reference to a tile group.
  5003. * @dev: DRM device
  5004. * @tg: tile group to drop reference to.
  5005. *
  5006. * drop reference to tile group and free if 0.
  5007. */
  5008. void drm_mode_put_tile_group(struct drm_device *dev,
  5009. struct drm_tile_group *tg)
  5010. {
  5011. kref_put(&tg->refcount, drm_tile_group_free);
  5012. }
  5013. /**
  5014. * drm_mode_get_tile_group - get a reference to an existing tile group
  5015. * @dev: DRM device
  5016. * @topology: 8-bytes unique per monitor.
  5017. *
  5018. * Use the unique bytes to get a reference to an existing tile group.
  5019. *
  5020. * RETURNS:
  5021. * tile group or NULL if not found.
  5022. */
  5023. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5024. char topology[8])
  5025. {
  5026. struct drm_tile_group *tg;
  5027. int id;
  5028. mutex_lock(&dev->mode_config.idr_mutex);
  5029. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5030. if (!memcmp(tg->group_data, topology, 8)) {
  5031. if (!kref_get_unless_zero(&tg->refcount))
  5032. tg = NULL;
  5033. mutex_unlock(&dev->mode_config.idr_mutex);
  5034. return tg;
  5035. }
  5036. }
  5037. mutex_unlock(&dev->mode_config.idr_mutex);
  5038. return NULL;
  5039. }
  5040. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5041. /**
  5042. * drm_mode_create_tile_group - create a tile group from a displayid description
  5043. * @dev: DRM device
  5044. * @topology: 8-bytes unique per monitor.
  5045. *
  5046. * Create a tile group for the unique monitor, and get a unique
  5047. * identifier for the tile group.
  5048. *
  5049. * RETURNS:
  5050. * new tile group or error.
  5051. */
  5052. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5053. char topology[8])
  5054. {
  5055. struct drm_tile_group *tg;
  5056. int ret;
  5057. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5058. if (!tg)
  5059. return ERR_PTR(-ENOMEM);
  5060. kref_init(&tg->refcount);
  5061. memcpy(tg->group_data, topology, 8);
  5062. tg->dev = dev;
  5063. mutex_lock(&dev->mode_config.idr_mutex);
  5064. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5065. if (ret >= 0) {
  5066. tg->id = ret;
  5067. } else {
  5068. kfree(tg);
  5069. tg = ERR_PTR(ret);
  5070. }
  5071. mutex_unlock(&dev->mode_config.idr_mutex);
  5072. return tg;
  5073. }
  5074. EXPORT_SYMBOL(drm_mode_create_tile_group);