drm_crtc.c 151 KB

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