drm_crtc.c 126 KB

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