drm_crtc.c 104 KB

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