drm_crtc.c 86 KB

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