drm_crtc.c 131 KB

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