drm_crtc.c 97 KB

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