drm_crtc.c 153 KB

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