drm_crtc.c 103 KB

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