drm_crtc.c 150 KB

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